It seems that the
nice
tool isn't as effective as I'd like it to be. I'd expected that if I run some CPU hungry program with nice level 19 (but using all CPU cores), then the kernel would still give almost a full core to a single-thread program if it needs it.
For example, if I start a compilation with nice -n 19 make -j12
, then I'd still be able to use my editor smoothly. Unfortunately this is not the case, if I start a compilation, my editor starts stuttering.
I remember having experimenting with nice levels a long time ago, and it certainly wasn't the case: even if some process took all the CPU power, if it was reniced to 19, other processes could run more-or-less smoothly.
I read about disabling autogroups (setting /proc/sys/kernel/sched_autogroup_enabled
to 0
), unfortunately this doesn't solve the issue.
I wrote a little C++ program to demonstrate the issue. It has a single parameter, which tells how many threads to use to load the CPU. And it regularly prints a time duration, which means how fast it can run. A smaller number means faster running speed.
Now, I run it ./cpu_load 1
in a terminal (simulating my editor), and it prints this:
618.01 ms
628.84 ms
633.68 ms
Now, I run nice -n 19 ./cpu_load 12
in another terminal, simulating compilation (note: my CPU has 12 HW threads, a 6 core machine). When doing this, the previous program slows down, and the timing it prints becomes larger, meaning it got a significant slow down:
1383.27 ms
1391.80 ms
1402.77 ms
Also, it seems that the nice level of 19 has almost no effect. The difference caused between ./cpu_load 12
and nice -n 19 ./cpu_load 12
is negligible. Also, I don't even have to use all the HW threads. If I use nice -n 19 ./cpu_load 9
, the program still gets the same slow down.
Is there a way to run the compilation while still having a responsive editor? What could be the reason that nice doesn't really work?
Note: the issue is surely not caused by CPU throttling.
Note2: according to the measurement, the editor only gets a ~2x slow down. But in reality, when using my actual editor, the difference is clearly much larger. When the CPU is not loaded, the editor is buttery smooth. But when the CPU is loaded, sometimes it doesn't get the chance to refresh the display for several tenths of a second.
My kernel is: Linux 6.13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.13.2-1~exp1 (2025-02-08)
Here's the CPU load program:
++
#include
#include
#include
#include
#include
std::int64_t T() {
struct timeval tv;
gettimeofday(&tv, nullptr);
return static_cast(tv.tv_sec) * 1000000 + tv.tv_usec;
}
void thread(int th) {
volatile double v = 0;
for (;;) {
// do some calculation, and print the time it took
std::int64_t b = T();
for (int i = 0; i < 100'000'000; i++) {
v += sin(i);
}
std::int64_t t = T() - b;
if (th == 0) {
printf("%5.2f ms\n", t / 1000.0f);
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
return 1;
}
int n = atoi(argv);
std::thread threads;
for (int i=0; i
Asked by geza
(151 rep)
Apr 14, 2025, 06:19 PM
Last activity: Apr 15, 2025, 06:24 AM
Last activity: Apr 15, 2025, 06:24 AM