22

I have a regular process that's not so important but will consume very much CPU power. I have another process which is really important, but it spends most of the time idle, but when it gets a job it really needs high computing power.

I tried running with nice -20 ./low_priority_process and nice --20 ./high_priority_process but still the lower priority process consumes significant amount of CPU when the high priority process is in need.

How can I run a process that will really yield or even auto-suspend when another process is using CPU power?

Matthias Braun
  • 7,797
  • 7
  • 45
  • 54
uray
  • 3,830
  • 11
  • 36
  • 42
  • How are you determining that the high priority process is actually in need? – muru Sep 22 '14 at 19:13
  • it will spawn a lot of threads, or the process eat the cpu more than 50% of cpu capacity – uray Sep 22 '14 at 19:13
  • Use `RR` scheduling for the high priority process. – Ramesh Sep 22 '14 at 19:28
  • @Ramesh What possible difference will that make? – Ken Sharp Oct 10 '19 at 00:56
  • I'm way too late to the party, but I can see from your use case that you're having issues not only with the CPU usage, but also with the I/O and RAM memory. Playing HQ videos consumes a lot of CPU, hdd I/O and RAM memory at the same time. Your issue is probably NOT (only) CPU. – Mladen B. Feb 17 '21 at 12:56
  • Contrary to a common misconception, running a "nice" process does not mean, it will _never_ run while regular processes have anything to do. It only means, it will run _less often_. To get, what you want on BSD systems there are `idprio` and `rtprio` to run things at "idle" and "real time" priorities respectively. On Linux you may have to go the `cgroup`-route, as @peterph suggested... – Mikhail T. Sep 28 '22 at 19:42

4 Answers4

19

Have a look at cgroups, it should provide exactly what you need - CPU reservations (and more). I'd suggest reading controlling priority of applications using cgroups.

That said, put the important yet often idle processes into group with allocated 95% of CPU and your other applications into another one with allocated 5% - you'll get (almost) all of the power for your jobs when needed, while the constantly power hungry process will only get 5% at most at those times. When the computational surges disappear all CPU performance will be thrown at the remaining processes. As a benefit, if you create a special cgroup (with minimal performance requirements) for processes like sshd, you'll be able to log in no matter what is trying to get all CPU it can - some CPU time will be reserved for sshd.

peterph
  • 30,520
  • 2
  • 69
  • 75
  • 1
    is there a command like `cpulimit` or `nice` to modify the cgroup of process? because i if cgroup is an API call, i am in position can't recompile any of those app to use cgroup – uray Sep 22 '14 at 19:41
  • No, you just mount the cgroup hierarchy somewhere, create per-group directories and write PIDs of processes into some files. All this has to be done as root (or, more precisely with appropriate privileges). Some init systems (namely `systemd`, at least in some cases) "steal" the cgroups interface from users who have to use the init system interface (usually a special command). Read the linked answer and wikipedia article. Really. :) – peterph Sep 22 '14 at 19:45
  • Oh, and there is also `libcgroup` package, which comes with utilities for working with cgroups including a daemon (`cgrulesengd`) that can actually sort processes into groups depending on some conditions. – peterph Sep 22 '14 at 19:51
10

If the process priority (nice value) is low then it will not be interrupting a higher priority process. The reason you're seeing the low priority process still consuming a significant amount of CPU when the higher priority process is running is because the higher priority process is not that busy. Probably waiting on IO. Use chrt -p -i 0 $PID to run the process at an even lower priority than nice 19 -p $PID (assuming we're talking about Linux here).

chrt -p -i 0 $PID puts the process into the "true" idle scheduler.

http://linux.die.net/man/1/chrt

mgutt
  • 377
  • 2
  • 16
Ken Sharp
  • 529
  • 1
  • 6
  • 18
1

For future-comers, here is a full example of nice with stress.

  1. The test machine has 2 CPUs
$ lscpu
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              2 
On-line CPU(s) list: 0,1
Thread(s) per core:  2
...
  1. Install stress: apt-get install stress
  2. Make the 2 CPUs busy with a low-priority call to stress: nice -20 stress --cpu 2
  3. Check CPU usage with top:
                                                 v
                                                 v
                                                 v
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                   
15894 ubuntu    39  19    8240     96      0 R  99.7  0.0   0:06.43 stress                                                                                    
15895 ubuntu    39  19    8240     96      0 R  99.7  0.0   0:06.42 stress                                                                                    

This shows that both CPUs are fully occupied.

  1. Launch a single-cpu stress process with high priority: nice --20 stress --cpu 1
  2. Check cpu usage again with top
                                                 v
                                                 v
                                                 v
                                                 v
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                   
15928 ubuntu    20   0    8240    100      0 R  99.7  0.0   0:24.02 stress                                                                                    
15894 ubuntu    39  19    8240     96      0 R  51.2  0.0   1:12.46 stress                                                                                    
15895 ubuntu    39  19    8240     96      0 R  48.8  0.0   1:12.35 stress                                                                                    

This shows that the single-core stress process gets its full CPU, whereas the lower-priority processes both share the remaining 1 cpu

  1. On the other hand, killing all the above stress calls and just triggering a single 3-process stress --cpu 3 would give 66% CPU to each
Shadi
  • 129
  • 3
0

Try this example to run a process as a low process.

If you're job is nice tar xvf asets.zip

Use

nice tar xvf assets.zip

After that, issue

top to monitor the process unzip with

ps aux | grep "tar"

Try something specific with cpulimit

wget -O cpulimit.zip https://github.com/opsengine/cpulimit/archive/master.zip
unzip cpulimit.zip
cd cpulimit-master
make
sudo cp src/cpulimit /usr/bin

cpulimit -l 50 python 0 9999999999 > /dev/null &

unixmiah
  • 348
  • 1
  • 12
  • yeah `-20` is "low priority" and `--20` is "high priority" look at double dash for `nice` command. and yes I am understand completely about nice value, but my question is, is there a way to tell scheduler other than using nice value – uray Sep 22 '14 at 19:18
  • 2
    @user77710 are you sure? Neither the [Ubuntu manpage](http://manpages.ubuntu.com/manpages/trusty/en/man1/nice.1.html) nor the [POSIX manpage](http://manpages.ubuntu.com/manpages/trusty/en/man1/nice.1posix.html) specify this syntax. Both use `-n -20`, `-n 20` etc. – muru Sep 22 '14 at 19:20
  • yeah whatever, the syntax is not important, my point is settng nice value of very low and very high for both processes does not give the result of what i wanted – uray Sep 22 '14 at 19:24
  • 2
    @user77710 WHAT? "the syntax is not important"? How can you be sure that you're setting the niceness values then? – muru Sep 22 '14 at 19:30
  • because i can see the process nice value on `htop` or `top` or `ps` or whatever – uray Sep 22 '14 at 19:31
  • @Ken Shark, try my new answer. – unixmiah Mar 17 '17 at 15:46