Disclaimer: this answer is for benefit of those who find that QA and want to control processes run by themselves and want to limit CPU usage regardless of current total loads of the system.
I've found on my Linux Mint cpulimit
did not help. Two ways worked though:
cputool
: e.g. cputool -c 10 -- stress -c 4
(stress
is (IMO) small useful testing tool to stress test the system)
Downside: cannot as easily change usage once started.
cgroups
Code (surprisingly if I delete this line code formatting below messes up):
sudo cgcreate -g cpu:mygroup1
cat /sys/fs/cgroup/mygroup1/cpu.max # not necessary, tried to find reason for error and tech details for reference
max 100000
sudo cgset -r cpu.max="200000 100000" mygroup1
sudo cgexec -g cpu:mygroup1 sudo -u username1 -g groupname1 stress -c 4
stress: info: [125425] dispatching hogs: 4 cpu, 0 io, 0 vm, 0 hdd
User cannot start process with cgroups
unless gives access rights, I have not learned how to add rights (I hope it is possible) but instead use sudo.
# in other terminal to change usage (this syntax changes 1st value only):
sudo cgset -r cpu.max=100000 mygroup1
Notes for cgroups:
cpu.max
has two values: the first value is the allowed time quota in microseconds for which all processes collectively in a child group can run during one period. The second value specifies the length of the period. For multicore/multiprocessor systems 1st is quota for all cores, 2nd is for one, so setting 1st two times greater than 2nd is expected to result in CPU usage of 2 divided by total number of cores.
On my system min valid value is 1000, max is 1000000.
Using second value of 100000 (default) resulted in additional 2x-3x speed penalty when I run ffmpeg
, using 1000000 resulted in no noticable penalty.
Surprise for me - why for GHz processors interrupts each hundred milliseconds matter so much but each second is not?
cgroups
can be used w/out cgcreate
, cgset
, cgexec
(they are in cgroup-tools
package which for Linux Mint distro required additional installation). IMO good description how to do that:
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/managing_monitoring_and_updating_the_kernel/using-cgroups-v2-to-control-distribution-of-cpu-time-for-applications_managing-monitoring-and-updating-the-kernel, how to start a process in cgroup: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/resource_management_guide/starting_a_process.
cpulimit
in conjunction with your search script. Have a policy and recommend the use ofcpulimit
, then search for over 10% and then limit to 5% (so users are encouraged to usecpulimit
). Also make sure you can detect multiple processes adding up to more that 10% for a single user.cpulimit
is way better than just killing the process since it can be restarted by the user later on (as pointed in one of your comments). Thank you!