- GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。
- GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。
cgroups 是Linux内核提供的可以限制进程所使用资源的机制,可以对 cpu,内存等资源实现精细化的控制.
什么是cgroups
- 控制族群(cgroup) – 关联一组task和一组subsystem的配置参数。一个task对应一个进程, cgroup是资源分片的最小单位。
- 子系统(subsystem) – 资源管理器,一个subsystem对应一项资源的管理,如 cpu, cpuset, memory等
- cpu 子系统,主要限制 cpu 使用率。
- cpuacct 子系统,可以统计 cgroups 中的进程的 cpu 使用报告。
- cpuset 子系统,可以为 cgroups 中的进程分配单独的 cpu 节点或者内存节点。
- memory 子系统,可以限制进程的 memory 使用量。
- blkio 子系统,可以限制进程的块设备 io。
- devices 子系统,可以控制进程能够访问某些设备。
- net_cls 子系统,可以标记 cgroups 中进程的网络数据包,然后可以使用 tc 模块(traffic control)对数据包进行控制。
- freezer 子系统,可以挂起或者恢复 cgroups 中的进程。
- ns 子系统,可以使不同 cgroups 下面的进程使用不同的 namespace。
- 层级(hierarchy) – 关联一个到多个subsystem和一组树形结构的cgroup. 和cgroup不同,hierarchy包含的是可管理的subsystem
mount -t cgroup 可以查看当前已经挂载的子系统
- 任务(task)- 每个cgroup都会有一个task列表文件tasks,一个task就对应一个进程。
cpu 子系统
cgroups的创建很简单,只需要在相应的子系统下创建目录即可。下面我们到 cpu 子系统下创建测试文件夹:
1.创建cpu 系统的cgroup
/sys/fs/cgroup/cpu/ctest
执行完成创建就可以看到
root@moyu20:/sys/fs/cgroup/cpu/ctest# ls -l
总用量 0
-rw-r--r-- 1 root root 0 7月 29 15:25 cgroup.clone_children
-rw-r--r-- 1 root root 0 7月 29 15:25 cgroup.procs
-r--r--r-- 1 root root 0 7月 29 15:25 cpuacct.stat
-rw-r--r-- 1 root root 0 7月 29 15:25 cpuacct.usage
-r--r--r-- 1 root root 0 7月 29 15:25 cpuacct.usage_all
-r--r--r-- 1 root root 0 7月 29 15:25 cpuacct.usage_percpu
-r--r--r-- 1 root root 0 7月 29 15:25 cpuacct.usage_percpu_sys
-r--r--r-- 1 root root 0 7月 29 15:25 cpuacct.usage_percpu_user
-r--r--r-- 1 root root 0 7月 29 15:25 cpuacct.usage_sys
-r--r--r-- 1 root root 0 7月 29 15:25 cpuacct.usage_user
-rw-r--r-- 1 root root 0 7月 29 15:25 cpu.cfs_period_us
-rw-r--r-- 1 root root 0 7月 29 15:25 cpu.cfs_quota_us
-rw-r--r-- 1 root root 0 7月 29 15:25 cpu.shares
-r--r--r-- 1 root root 0 7月 29 15:25 cpu.stat
-rw-r--r-- 1 root root 0 7月 29 15:25 cpu.uclamp.max
-rw-r--r-- 1 root root 0 7月 29 15:25 cpu.uclamp.min
-rw-r--r-- 1 root root 0 7月 29 15:25 notify_on_release
-rw-r--r-- 1 root root 0 7月 29 15:25 tasks
已经把cpu子系统 对应控制文件创建好了
2.创建进程加入cgroup
查看当前进程
root@moyu20:/sys/fs/cgroup/cpu/ctest# echo $$
1273114
root@moyu20:/sys/fs/cgroup/cpu/ctest# ps -aux|grep 1273114
root 1273114 0.0 0.0 13988 4944 pts/4 S 15:10 0:00 bash
root 1304325 0.0 0.0 12132 2512 pts/4 S+ 15:35 0:00 grep --color=auto 1273114
将进程号添加到 tasks 中
echo $$ > tasks
查看task 会将当前bash 进行与所有子进程都放入tasks 中
root@moyu20:/sys/fs/cgroup/cpu/ctest# cat tasks
1273114
1306199
3 验证
制作一个耗时cpu 的进程
while true;do echo;done;
查看监控可以非常容易发现 cpu 跑满了一个cpu
╰─○ top -p 1273114
top - 15:40:21 up 23 days, 5:12, 1 user, load average: 1.71, 1.36, 1.23
任务: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 12.7 us, 12.0 sy, 0.0 ni, 75.2 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 15605.0 total, 971.2 free, 12135.4 used, 2498.4 buff/cache
MiB Swap: 15718.7 total, 1156.2 free, 14562.5 used. 2939.0 avail Mem
进程号 USER PR NI VIRT RES SHR %CPU %MEM TIME+ COMMAND
1273114 root 20 0 13988 4944 3396 R 100.0 0.0 0:29.73 bash
cgoups 利用 cpu.cfs_quota_us 与cfs_period_us 限制cpu 的频率分配, 分配为 cfs_quota_us / cfs_period_us
限制分配0.25 个cpu
echo 25000 > cpu.cfs_quota_us
while true;do echo;done;
再查看监控
╰─○ top -p 1273114
top - 15:45:43 up 23 days, 5:17, 1 user, load average: 1.28, 1.51, 1.36
任务: 1 total, 1 running, 0 sleeping, 0 stopped, 0 zombie
%Cpu(s): 7.1 us, 3.9 sy, 0.0 ni, 89.0 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 15605.0 total, 209.3 free, 11560.7 used, 3835.0 buff/cache
MiB Swap: 15718.7 total, 953.0 free, 14765.7 used. 3522.6 avail Mem
进程号 USER PR NI VIRT RES SHR %CPU %MEM TIME+ COMMAND
1273114 root 20 0 13988 4944 3396 R 25.0 0.0 1:24.41 bash
你可以清楚地看到,这一过程被限制在最高25%。
[En]
You can clearly see that the process is limited to a maximum of 25%.
同理可以创建 memory , blkio 等cgroup 用于限制系统资源
4.移除cgroup
退出tasks
╭─root@moyu20 /sys/fs/cgroup/cpu
╰─# cgdelete cpu:ctest
5.cgroup 在Docker 中的使用
docker 是目前常用容器,它就依赖cgroup 对资源进行限制
创建一个centos7 分配0.25 个cpu 与1g 内存
docker run -itd --name test7 -m 1g --cpus 0.25 centos:7 bash
可以到
╰─○ docker inspect test7|more
[
{
"Id": "7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8",
"Created": "2022-07-28T09:29:30.247354976Z",
在宿主机上可以看到Docker 创建了容器相关的 cpu 的cgroup
root@moyu20:/sys/fs/cgroup/cpu/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8$ ls
cgroup.clone_children cpuacct.usage cpuacct.usage_percpu_sys cpuacct.usage_user cpu.shares cpu.uclamp.min
cgroup.procs cpuacct.usage_all cpuacct.usage_percpu_user cpu.cfs_period_us cpu.stat notify_on_release
cpuacct.stat cpuacct.usage_percpu cpuacct.usage_sys cpu.cfs_quota_us cpu.uclamp.max tasks
root@moyu20:/sys/fs/cgroup/cpu/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8$ cat cpu.cfs_quota_us
25000
进入容器在Docker 中
docker exec -it test7 bash
可以看到相同的目录已经被 mount 到 /sys/fs/cgroup/cpu 下了
[root@7d1d6b186509 ~]# cat /sys/fs/cgroup/cpu/cpu.cfs_quota_us
25000
类似 top, free 等命令查看 /proc/ 目录都是宿主机的信息
就会导致 free 的实际值并不准确
[root@7d1d6b186509 cpu]# while true;do echo;done;
[root@7d1d6b186509 cpu]# free -g
total used free shared buff/cache available
Mem: 15 10 0 0 3 3
Swap: 15 14 0
[root@7d1d6b186509 /]# top
top - 08:03:26 up 23 days, 5:35, 0 users, load average: 2.73, 1.83, 1.54
Tasks: 4 total, 1 running, 3 sleeping, 0 stopped, 0 zombie
%Cpu(s): 11.6 us, 2.2 sy, 0.0 ni, 86.0 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 15979492 total, 392728 free, 11511792 used, 4074972 buff/cache
KiB Swap: 16095996 total, 1042540 free, 15053456 used. 3935784 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 11844 292 16 S 0.0 0.0 0:00.02 bash
34 root 20 0 11820 2920 2516 S 0.0 0.0 0:19.85 bash
55 root 20 0 11844 2968 2556 S 0.0 0.0 0:00.02 bash
72 root 20 0 56212 3732 3176 R 0.0 0.0 0:00.00 top
实际上,分配的资源已经用完了,但是这些命令没有在容器中显示正确的结果。主机系统资源使用情况的实际显示会误导资源使用。需要注意
[En]
Actually, the allocated resources have been used up, but these commands do not display the correct results in the container. The actual display of the resource usage of the host system will mislead the use of resources. * need to pay attention to *
判断进程是否属于cgroup 可以通过 /proc//cgroup 进行判断
[root@7d1d6b186509 ~]# cat /proc/1/cgroup
13:hugetlb:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
12:net_cls,net_prio:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
11:freezer:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
10:cpuset:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
9:devices:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
8:rdma:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
7:memory:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
6:misc:/
5:pids:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
4:blkio:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
3:perf_event:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
2:cpu,cpuacct:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
1:name=systemd:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
0::/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8
可以明显看到这个进程被分配到docker 下的cgroup 中了
关于 GreatSQL
GreatSQL是由万里数据库维护的MySQL分支,专注于提升MGR可靠性及性能,支持InnoDB并行查询特性,是适用于金融级应用的MySQL分支版本。
GreatSQL社区 Gitee GitHub Bilibili
技术交流群:
微信:扫码添加
GreatSQL社区助手
微信好友,发送验证信息加群
。
Original: https://www.cnblogs.com/greatsql/p/16626469.html
Author: GreatSQL
Title: Linux 利用Cgroup 资源控制
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/504942/
转载文章受原作者版权保护。转载请注明原作者出处!