查看系统是否支持cgroup v2
$ mount | grep cgroup
cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
判断内核是否支持cgroup v2
$ cat /proc/filesystems | grep cgroup
nodev cgroup
nodev cgroup2
查看cgroup v2可管理的系统资源类型
$ cd /sys/fs/cgroup/
$ cat cgroup.controllers
cpuset cpu io memory hugetlb pids rdma misc
查看cgroup2开启的控制器
$ cat cgroup.subtree_control
memory pids
可以看见,默认没有开启cpu控制器
在根控制组开启CPU控制器
需要切换到root操作
# echo "+cpu" > cgroup.subtree_control
再次查看cgroup2开启的控制器
$ cat cgroup.subtree_control
cpu memory pids
说明已经生效
创建一个任务组
在root cgroup下创建
# cd /sys/fs/cgroup/
# mkdir my-test
# cd my-test/
# ll
total 0
drwxr-xr-x 2 root root 0 9月 1 07:17 ./
dr-xr-xr-x 14 root root 0 9月 1 07:17 ../
-r--r--r-- 1 root root 0 9月 1 07:17 cgroup.controllers
-r--r--r-- 1 root root 0 9月 1 07:17 cgroup.events
-rw-r--r-- 1 root root 0 9月 1 07:17 cgroup.freeze
--w------- 1 root root 0 9月 1 07:17 cgroup.kill
-rw-r--r-- 1 root root 0 9月 1 07:17 cgroup.max.depth
-rw-r--r-- 1 root root 0 9月 1 07:17 cgroup.max.descendants
-rw-r--r-- 1 root root 0 9月 1 07:17 cgroup.pressure
-rw-r--r-- 1 root root 0 9月 1 07:17 cgroup.procs
-r--r--r-- 1 root root 0 9月 1 07:17 cgroup.stat
-rw-r--r-- 1 root root 0 9月 1 07:17 cgroup.subtree_control
-rw-r--r-- 1 root root 0 9月 1 07:17 cgroup.threads
-rw-r--r-- 1 root root 0 9月 1 07:17 cgroup.type
-rw-r--r-- 1 root root 0 9月 1 07:17 cpu.idle
-rw-r--r-- 1 root root 0 9月 1 07:17 cpu.max
-rw-r--r-- 1 root root 0 9月 1 07:17 cpu.max.burst
-rw-r--r-- 1 root root 0 9月 1 07:17 cpu.pressure
-r--r--r-- 1 root root 0 9月 1 07:17 cpu.stat
-rw-r--r-- 1 root root 0 9月 1 07:17 cpu.uclamp.max
-rw-r--r-- 1 root root 0 9月 1 07:17 cpu.uclamp.min
-rw-r--r-- 1 root root 0 9月 1 07:17 cpu.weight
-rw-r--r-- 1 root root 0 9月 1 07:17 cpu.weight.nice
-rw-r--r-- 1 root root 0 9月 1 07:17 io.pressure
-r--r--r-- 1 root root 0 9月 1 07:17 memory.current
-r--r--r-- 1 root root 0 9月 1 07:17 memory.events
-r--r--r-- 1 root root 0 9月 1 07:17 memory.events.local
-rw-r--r-- 1 root root 0 9月 1 07:17 memory.high
-rw-r--r-- 1 root root 0 9月 1 07:17 memory.low
-rw-r--r-- 1 root root 0 9月 1 07:17 memory.max
-rw-r--r-- 1 root root 0 9月 1 07:17 memory.min
-r--r--r-- 1 root root 0 9月 1 07:17 memory.numa_stat
-rw-r--r-- 1 root root 0 9月 1 07:17 memory.oom.group
-r--r--r-- 1 root root 0 9月 1 07:17 memory.peak
-rw-r--r-- 1 root root 0 9月 1 07:17 memory.pressure
--w------- 1 root root 0 9月 1 07:17 memory.reclaim
-r--r--r-- 1 root root 0 9月 1 07:17 memory.stat
-r--r--r-- 1 root root 0 9月 1 07:17 memory.swap.current
-r--r--r-- 1 root root 0 9月 1 07:17 memory.swap.events
-rw-r--r-- 1 root root 0 9月 1 07:17 memory.swap.high
-rw-r--r-- 1 root root 0 9月 1 07:17 memory.swap.max
-r--r--r-- 1 root root 0 9月 1 07:17 memory.zswap.current
-rw-r--r-- 1 root root 0 9月 1 07:17 memory.zswap.max
-r--r--r-- 1 root root 0 9月 1 07:17 pids.current
-r--r--r-- 1 root root 0 9月 1 07:17 pids.events
-rw-r--r-- 1 root root 0 9月 1 07:17 pids.max
-r--r--r-- 1 root root 0 9月 1 07:17 pids.peak
可以看见,系统会自动填充任务组下的内容
查看my-test可用的控制器
# cat cgroup.controllers
cpu memory pids
看得出比root组的少很多,每个child cgroup不会继承其父节点的控制器,只有在父节点cgroup.subtree_control显式配置开启的控制器才能在child cgroup中使用。
配置任务组my-test的带宽配制
// 在my-test 路径
# echo "50000 100000" > cpu.max
# cat cpu.max
50000 100000
这个含义是任务组my-test的带宽配制为每个100毫秒周期最多执行50毫秒,也就是本cgroup管理的进程在单核CPU上的使用率不会超过50%。
把线程加入本任务组
格式 echo <pid> > cgroup.procs
我们开始做实验
死循环脚本如下
// loop.sh
#!/bin/bash
while true; do
:
done
未添加到my-test组进行测试
$ ./loop.sh
top 结果
top - 11:42:45 up 32 min, 2 users, load average: 0.73, 0.65, 0.73
Tasks: 304 total, 3 running, 301 sleeping, 0 stopped, 0 zombie
%Cpu(s): 13.5 us, 0.6 sy, 0.0 ni, 85.7 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 7404.0 total, 1608.9 free, 2778.5 used, 3016.6 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 3494.7 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4116 jun 20 0 7340 3456 3200 R 100.0 0.0 0:05.06 loop.sh
2376 jun 20 0 33.0g 231148 154976 S 3.8 3.0 3:41.88 chrome
1645 jun 20 0 5544040 270196 128072 S 1.3 3.6 1:43.09 gnome-shell
可见,loop占用单cpu 100 %
添加loop进程到my-test组
$ ./loop.sh &
[1] 4404
添加pid
在路径 /sys/fs/cgroup/my-test 操作
# echo 4404 > cgroup.procs
top的结果
top - 11:55:56 up 45 min, 2 users, load average: 1.10, 1.12, 0.97
Tasks: 307 total, 2 running, 305 sleeping, 0 stopped, 0 zombie
%Cpu(s): 9.7 us, 0.5 sy, 0.0 ni, 89.7 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 7404.0 total, 1221.9 free, 2946.7 used, 3235.4 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 3122.9 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4404 jun 20 0 7340 3456 3200 R 50.0 0.0 1:00.67 loop.sh
4178 jun 20 0 4020804 191516 61568 S 22.3 2.5 3:51.63 sunloginclient
2572 jun 20 0 1131.0g 295312 134620 S 2.3 3.9 1:25.49 chrome
3672 jun 20 0 4315184 171680 68100 S 2.3 2.3 1:41.73 sunlogincli
可见,最大cpu占用率不超过50 %