Limit process CPU use with cgroups version 1 and cgroup tools #cgroups #linux 2022-09-22 Tweet #!/bin/bash sudo cgdelete cpu:/testgroup sudo cgcreate -g cpu:/testgroup # Test with and without the line below sudo cgset -r cpu.cfs_quota_us=50000 testgroup # See how much time do we need to complete the operation echo "Starting CPU intensive operations" date stress-ng --qsort 1 --qsort-ops 200 & sleep 1 PID=$(pgrep stress-ng-qsort) sudo cgclassify -g cpu:testgroup $PID wait echo "Finished" date The results with cfs_quota_us=50000 are below. The top utility shows about 50% CPU usage. ➜ ./cpu_stress.sh Starting CPU intensive operations czw, 22 wrz 2022, 17:41:33 CEST stress-ng: info: [3345901] defaulting to a 86400 second (1 day, 0.00 secs) run per stressor stress-ng: info: [3345901] dispatching hogs: 1 qsort stress-ng: info: [3345901] successful run completed in 33.05s Finished czw, 22 wrz 2022, 17:42:06 CEST If the line is commented out (so no limitations apply), top shows 100% CPU usage and the output is: ➜ ./cpu_stress.sh Starting CPU intensive operations czw, 22 wrz 2022, 17:42:53 CEST stress-ng: info: [3348203] defaulting to a 86400 second (1 day, 0.00 secs) run per stressor stress-ng: info: [3348203] dispatching hogs: 1 qsort stress-ng: info: [3348203] successful run completed in 15.98s Finished czw, 22 wrz 2022, 17:43:09 CEST The corresponding file entries in sysfs: $ cat /sys/fs/cgroup/cpu/testgroup/cpu.cfs_quota_us 50000 $ cat /sys/fs/cgroup/cpu/testgroup/tasks 3353629