Linux cgroups
linux cgroups是linux提供的限制,控制和分离一个进程集资源的功能,子进程承接所有父进程cgroups限制
主要提供如下功能
- Resource limitation: 限制资源使用
- Prioritization: 优先级控制
- Accounting: 审计和计费
- Control: 挂起恢复执行进程
提供如下子模型
- blkio — this subsystem sets limits on input/output access to and from block devices such as physical drives (disk, solid state, or USB).
- cpu — this subsystem uses the scheduler to provide cgroup tasks access to the CPU.
- cpuacct — this subsystem generates automatic reports on CPU resources used by tasks in a cgroup.
- cpuset — this subsystem assigns individual CPUs (on a multicore system) and memory nodes to tasks in a cgroup.
- devices — this subsystem allows or denies access to devices by tasks in a cgroup.
- freezer — this subsystem suspends or resumes tasks in a cgroup.
- memory — this subsystem sets limits on memory use by tasks in a cgroup and generates automatic reports on memory resources used by those tasks.
- net_cls — this subsystem tags network packets with a class identifier (classid) that allows the Linux traffic controller (tc) to identify packets originating from a particular cgroup task.
- net_prio — this subsystem provides a way to dynamically set the priority of network traffic per network interface.
- ns — the namespace subsystem.
- perf_event — this subsystem identifies cgroup membership of tasks and can be used for performance analysis.
探索cgroups
cgroups在linux是一个cgroup类型的文件系统,可以通过mount命令查看所有cgroups
> sudo mount -t cgroup
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_prio,net_cls)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroups之cpu
查看cgroup的cpu目录,其中子目录为一个cgroup
❯ ls -la /sys/fs/cgroup/cpu/
total 0
drwxr-xr-x 5 root root 0 Apr 10 15:43 .
drwxr-xr-x 13 root root 340 Apr 10 15:43 ..
-rw-r--r-- 1 root root 0 Apr 10 15:43 cgroup.clone_children
--w--w--w- 1 root root 0 Apr 10 15:43 cgroup.event_control
-rw-r--r-- 1 root root 0 Apr 10 15:43 cgroup.procs
-r--r--r-- 1 root root 0 Apr 10 15:43 cgroup.sane_behavior
-r--r--r-- 1 root root 0 Apr 10 15:43 cpuacct.stat
-rw-r--r-- 1 root root 0 Apr 10 15:43 cpuacct.usage
-r--r--r-- 1 root root 0 Apr 10 15:43 cpuacct.usage_percpu
-rw-r--r-- 1 root root 0 Apr 10 15:43 cpu.cfs_period_us
-rw-r--r-- 1 root root 0 Apr 10 15:43 cpu.cfs_quota_us
-rw-r--r-- 1 root root 0 Apr 10 15:43 cpu.rt_period_us
-rw-r--r-- 1 root root 0 Apr 10 15:43 cpu.rt_runtime_us
-rw-r--r-- 1 root root 0 Apr 10 15:43 cpu.shares
-r--r--r-- 1 root root 0 Apr 10 15:43 cpu.stat
drwxr-xr-x 3 root root 0 Apr 12 23:32 docker
-rw-r--r-- 1 root root 0 Apr 10 15:43 notify_on_release
-rw-r--r-- 1 root root 0 Apr 10 15:43 release_agent
drwxr-xr-x 75 root root 0 Apr 12 15:59 system.slice
-rw-r--r-- 1 root root 0 Apr 10 15:43 tasks
drwxr-xr-x 2 root root 0 Apr 10 15:44 user.slice
几个重要文件
- tasks: 存放受本cgroup控制的进程pid
- cpu.cfs_quota_us: CPU使用上限,100000代表一个cpu,-1代表不限制
- cpuacct.usage: 显示当前使用率HZ
- cpuacct.usage_percpu: 按cpu显示当前使用率HZ
cgroups之memroy
查看cgroup的memroy目录,其中子目录为一个cgroup
❯ ls -la /sys/fs/cgroup/memory
total 0
-rw-r--r-- 1 root root 0 Apr 10 15:43 cgroup.clone_children
--w--w--w- 1 root root 0 Apr 10 15:43 cgroup.event_control
-rw-r--r-- 1 root root 0 Apr 10 15:43 cgroup.procs
-r--r--r-- 1 root root 0 Apr 10 15:43 cgroup.sane_behavior
drwxr-xr-x 3 root root 0 Apr 12 23:55 docker
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.failcnt
--w------- 1 root root 0 Apr 10 15:43 memory.force_empty
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.kmem.failcnt
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.kmem.limit_in_bytes
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.kmem.max_usage_in_bytes
-r--r--r-- 1 root root 0 Apr 10 15:43 memory.kmem.slabinfo
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.kmem.tcp.failcnt
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.kmem.tcp.limit_in_bytes
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.kmem.tcp.max_usage_in_bytes
-r--r--r-- 1 root root 0 Apr 10 15:43 memory.kmem.tcp.usage_in_bytes
-r--r--r-- 1 root root 0 Apr 10 15:43 memory.kmem.usage_in_bytes
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.limit_in_bytes
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.max_usage_in_bytes
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.memsw.failcnt
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.memsw.limit_in_bytes
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.memsw.max_usage_in_bytes
-r--r--r-- 1 root root 0 Apr 10 15:43 memory.memsw.usage_in_bytes
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.move_charge_at_immigrate
-r--r--r-- 1 root root 0 Apr 10 15:43 memory.numa_stat
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.oom_control
---------- 1 root root 0 Apr 10 15:43 memory.pressure_level
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.soft_limit_in_bytes
-r--r--r-- 1 root root 0 Apr 10 15:43 memory.stat
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.swappiness
-r--r--r-- 1 root root 0 Apr 10 15:43 memory.usage_in_bytes
-rw-r--r-- 1 root root 0 Apr 10 15:43 memory.use_hierarchy
-rw-r--r-- 1 root root 0 Apr 10 15:43 notify_on_release
-rw-r--r-- 1 root root 0 Apr 10 15:43 release_agent
drwxr-xr-x 75 root root 0 Apr 12 15:59 system.slice
-rw-r--r-- 1 root root 0 Apr 10 15:43 tasks
drwxr-xr-x 2 root root 0 Apr 10 15:44 user.slice
- tasks: 存放受本cgroup控制的进程pid
- memory.limit_in_bytes: MEM使用上限,应用超过此上限会触发cgroup oom-kill
- memory.stat: MEM使用统计信息
来源:CSDN
作者:m0_45406092
链接:https://blog.csdn.net/m0_45406092/article/details/103482880