Score:1

Limit the sum of 2 processes cpu usage?

br flag

2 processes, call it A and B
A has high priority,B has low priority
I want to limit total cpu usage of these 2 processes under 80%

Sometimes A has nothing to do,cpu usage is 0%, at this time, I hope B execute and take up 80% cpu
Sometimes A is working,cpu usage is 80%, at this time, I hope B do nothing and not use cpu(or use only a little, 1%)
Sometimes A take up 30% cpu, at this time, B should take up 50% cpu

I am under Ubuntu 20.04, is there any way to do this?

Score:1
jp flag

To be this exact you probably need to use cgroups.

Here is a quick example I tested on Ubuntu 20.04. For simplicity, this is a single cpu VM and all commands were run as root.

  • install tools
apt-get install cgroup-tools stress
  • create a hierarchy of cgroups that will be cpu limited. The parent is named max80 and it has A and B beneath it.
cgcreate -g cpu:max80/A -g cpu:max80/B
  • limit the parent cgroup max80 to 80% of the CPU.
echo 1000000 > /sys/fs/cgroup/cpu/max80/cpu.cfs_period_us
echo  800000 > /sys/fs/cgroup/cpu/max80/cpu.cfs_quota_us
  • limit the child cgroup B to have 10% of shares. You mentioned 1% in your post, but 10% is easier to show. B will use all the available CPU, but if there is contention with processes in A then it will be limited to 10%.
echo $((1024 * 10 / 100 )) > /sys/fs/cgroup/cpu/max80/B/cpu.shares

In action

  • run a stress process in just A. The CPU usage will be 80% for the stress process doing the work.
root@ubuntu:~# cgexec -g cpu:max80/A stress --cpu 1 &
[1] 2040
stress: info: [2040] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
root@ubuntu:~# ps -o pid,%cpu,cmd --sort -%cpu -p $(pidof stress)
    PID %CPU CMD
   2041 80.4 stress --cpu 1
   2040  0.0 stress --cpu 1
root@ubuntu:~# killall stress
  • run a stress process in just B. The CPU usage will be 80%.
root@ubuntu:~# cgexec -g cpu:max80/B stress --cpu 1 &
[1] 2065
stress: info: [2065] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
root@ubuntu:~# ps -o pid,%cpu,cmd --sort -%cpu -p $(pidof stress)
    PID %CPU CMD
   2066 80.6 stress --cpu 1
   2065  0.0 stress --cpu 1
root@ubuntu:~# killall stress
  • run a stress process in A and B. The CPU usage will be split 90%/10%.
root@ubuntu:~# cgexec -g cpu:max80/A stress --cpu 1 &
[1] 2078
stress: info: [2078] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
root@ubuntu:~# cgexec -g cpu:max80/B stress --cpu 1 &
[2] 2080
stress: info: [2080] dispatching hogs: 1 cpu, 0 io, 0 vm, 0 hdd
root@ubuntu:~# ps -o pid,%cpu,cmd --sort -%cpu -p $(pidof stress)
    PID %CPU CMD
   2079 71.9 stress --cpu 1
   2081  7.2 stress --cpu 1
   2078  0.0 stress --cpu 1
   2080  0.0 stress --cpu 1
root@ubuntu:~# killall stress

Links

br flag
Thanks, very detail! I tried it, it did limit cpu usage under 80%, but only for 1 cpu, my PC has 8 cpus, so it actually take up 80% of 800%, rest 7 cpus still idle. I hope it can use 80% of all cpus, so 800% * 80% = 640%, is there any way to do this?
Andrew Lowther avatar
jp flag
I believe you would have to multiply the quota by the number of cores. `echo $((8 * 800000)) > /sys/fs/cgroup/cpu/max80/cpu.cfs_quota_us`. If you are using `stress` to test then you need to increase the worker count `stress --cpu 8`.
Score:0
cn flag

In Linux, the priority of running programs can be changes with the renice command. You can read about the command typing man renice on the terminal. Tuturials on the internet will get you up to speed in gaining an understanding of how this works, e.g. Tecmint, Computer Hope.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.