I used to run a few resource-intensive tools with cgexec
in cgroups v1. I'm not so sure this is supported in cgroups v2; at least I couldn't get it to work.
After too much time spent looking into it this is how I managed to get things going for me in Ubuntu 22.04 + cgroups v2.
1. Create a cgroup europe
and (in my case) tweak permissions
$ cd /sys/fs/cgroup/
$ sudo mkdir europe create
$ ls -ld europe
drwxr-x--- 2 root root 0 out 28 22:50 europe
$ sudo chmod a+rx europe
$ ls -ld europe
drwxr-xr-x 2 root root 0 out 28 22:50 europe
Without changing permissions two things happen to me: can't cat stuff inside group without sudo, and systemd-cgtop --depth 1
returns Failed to refresh: Permission denied
.
2. Limit CPU to 3 cores and RAM to 16G
I had multiple controllers available (cgroup.controllers
), but the only ones being passed down the subtree were memory
and pids
so I added the missing cpu
controller.
$ cat cgroup.controllers // all available
cpuset cpu io memory hugetlb pids rdma misc
$ cat cgroup.subtree_control // for subgroups
memory pids
$ sudo bash -c 'echo '+cpu' > cgroup.subtree_control'
$ cat cgroup.subtree_control // now includes CPU
cpu memory pids
$ cat europe/cgroup.controllers // can confirm in subgrp
cpu memory pids
Now limit CPU and RAM:
$ cat europe/memory.max
max
$ cat europe/cpu.max
max 100000
$ echo '16G' | sudo tee europe/memory.max
16G
$ echo '300000 100000' | sudo tee europe/cpu.max
300000 100000
$ cat europe/memory.max
17179869184
$ cat europe/cpu.max
300000 100000
3. Run some stuff
We can add PIDs to to europe/cgroup.procs
that will then be executing in group europe
.
Instead of starting a process and then adding its PID what I do is add the PID of a terminal window. Then everything run in that shell will be executed in the group.
Open another terminal:
$ cat /sys/fs/cgroup/europe/cgroup.procs
$ echo $$ | sudo tee /sys/fs/cgroup/europe/cgroup.procs
25982
$ cat europe/cgroup.procs
25982
26104
$ cat europe/cgroup.procs
25982
26105
$ cat europe/cgroup.procs
25982
26106
The first PID is the one referring to the terminal (matches echo $$
in bash) and the second PID is the command cat
that's why everytime I ran the command the second PID was different.
4. Remove group when not needed
$ cat /sys/fs/cgroup/europe/cgroup.procs
$ sudo rmdir /sys/fs/cgroup/europe
If there are still processes in europe/cgroup.procs
we get an error. In this example close the terminal that was added to the group in the previous step.