5

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 <cmath> #include <sys/time.h> #include <cstdint> #include <cstdio> #include <thread> std::int64_t T() { struct timeval tv; gettimeofday(&tv, nullptr); return static_cast<std::int64_t>(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[1]); std::thread threads[128]; for (int i=0; i<n; i++) { threads[i] = std::thread(thread, i); } for (int i=0; i<n; i++) { threads[i].join(); } } 
2
  • Excellent posting, including a sample program that illustrates the problem, fantastic! I don't have the experience to help resolve, but maybe include the output of uname -srv. Do you have multiple distros available to test with (that your issue might be distro related)? Commented Apr 15 at 5:54
  • @shellter: thanks! I've added the output of uname to my question. Commented Apr 15 at 6:24

1 Answer 1

1

This really calls for cgroups2 group controllers, putting a limit on how much percentage of CPU time your process can get when CPU gets rare. Though I'm not really sure this solves the responsiveness issue; that might be a RAM bandwidth problem, maybe?

Anyways, I assume you have a modern Linux on which you develop. Try the systemd-run method instead of nice described in How to limit CPU usage with systemd-run ; systemd-run allows you to pick background or attached to your terminal (--pty).

2
  • Thanks for the answer! I tried systemd-run --property=CPUQuota=400% --pty --user ./cpu_load 12. It still caused slowdown, which is weird, because if I use ./cpu_load 4, it causes the same 400% load to the system, yet it doesn't cause a slowdown. But even if it worked, this is not a solution for me, because I want my compilation process to use all the CPU power when it is available, not generally limit the CPU usage. Then I tried systemd-run --property=CPUWeight=1 --pty --user ./cpu_load 12, it seems that it doesn't have any effect, no matter what number I give to CPUWeight. Commented Apr 14 at 19:34
  • Regarding RAM bandwidth: yes, a lot of things can make my actual editor slow (slower than the benchmark). But the benchmark program is not really memory intensive, so it shouldn't be the cause. Commented Apr 14 at 19:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.