• GreatSQL社区原创内容未经受权不得随便应用,转载请分割小编并注明起源。
  • GreatSQL是MySQL的国产分支版本,应用上与MySQL统一。

cgroups 是Linux内核提供的能够限度过程所应用资源的机制,能够对 cpu,内存等资源实现精细化的管制.

什么是cgroups

  • 管制族群(cgroup) - 关联一组task和一组subsystem的配置参数。一个task对应一个过程, cgroup是资源分片的最小单位。
  • 子系统(subsystem) - 资源管理器,一个subsystem对应一项资源的治理,如 cpu, cpuset, memory等

    1. cpu 子系统,次要限度 cpu 使用率。
    2. cpuacct 子系统,能够统计 cgroups 中的过程的 cpu 应用报告。
    3. cpuset 子系统,能够为 cgroups 中的过程调配独自的 cpu 节点或者内存节点。
    4. memory 子系统,能够限度过程的 memory 使用量。
    5. blkio 子系统,能够限度过程的块设施 io。
    6. devices 子系统,能够管制过程可能拜访某些设施。
    7. net_cls 子系统,能够标记 cgroups 中过程的网络数据包,而后能够应用 tc 模块(traffic control)对数据包进行管制。
    8. freezer 子系统,能够挂起或者复原 cgroups 中的过程。
    9. 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 $$1273114root@moyu20:/sys/fs/cgroup/cpu/ctest# ps -aux|grep 1273114root     1273114  0.0  0.0  13988  4944 pts/4    S    15:10   0:00 bashroot     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 12731141306199

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 stMiB Mem :  15605.0 total,    971.2 free,  12135.4 used,   2498.4 buff/cacheMiB 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 stMiB Mem :  15605.0 total,    209.3 free,  11560.7 used,   3835.0 buff/cacheMiB 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% 了

同理能够创立 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$ lscgroup.clone_children  cpuacct.usage         cpuacct.usage_percpu_sys   cpuacct.usage_user  cpu.shares      cpu.uclamp.mincgroup.procs           cpuacct.usage_all     cpuacct.usage_percpu_user  cpu.cfs_period_us   cpu.stat        notify_on_releasecpuacct.stat           cpuacct.usage_percpu  cpuacct.usage_sys          cpu.cfs_quota_us    cpu.uclamp.max  tasksroot@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   availableMem:             15          10           0           0           3           3Swap:            15          14           0[root@7d1d6b186509 /]# toptop - 08:03:26 up 23 days,  5:35,  0 users,  load average: 2.73, 1.83, 1.54Tasks:   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 stKiB Mem : 15979492 total,   392728 free, 11511792 used,  4074972 buff/cacheKiB 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   

理论曾经将调配的资源应用完了,然而在容器内这些命令并不能显示正确的后果,理论显示的宿主零碎的资源应用状况,会对应用的状况产生误导,须要留神

判断过程是否属于cgroup 能够通过 /proc/<pid>/cgroup 进行判断

[root@7d1d6b186509 ~]# cat /proc/1/cgroup 13:hugetlb:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c812:net_cls,net_prio:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c811:freezer:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c810:cpuset:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c89:devices:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c88:rdma:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c87:memory:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c86:misc:/5:pids:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c84:blkio:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c83:perf_event:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c82:cpu,cpuacct:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c81:name=systemd:/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c80::/docker/7d1d6b1865094f5e357f89f615dcdf57ce50954f6299d8a7aea41b4ca0cc73c8

能够显著看到这个过程被调配到docker 下的cgroup 中了

## 对于 GreatSQL

GreatSQL是由万里数据库保护的MySQL分支,专一于晋升MGR可靠性及性能,反对InnoDB并行查问个性,是实用于金融级利用的MySQL分支版本。

GreatSQL社区 Gitee GitHub Bilibili

https://greatsql.cn/

技术交换群:

微信:扫码增加GreatSQL社区助手微信好友,发送验证信息加群