CGroups **************************************** 要查看系统是否支持 CGroups ,只需要执行 :command:`cat /boot/config-\`uname -r\` | grep 'CONFIG_CGROUPS'` 即可。 另外还可以通过 :command:`mount -t cgroup2` 查看已经挂载的 CGroup V2 文件系统。CGroups 没有实现相关的系统调用,而是实现了 VFS 来对 CGroups 进行操作。 要查看 CGroup 支持的子系统,可以使用 :command:`cat /proc/cgroups` ,而要查看进程所属的 cgroup,可以直接使用 :command:`cat /proc/${PID}/cgroup` 来查看。一个进程只能属于一个 cgroup 下面的笔记只讲述 CGroup V2,而不涉及 V1 .. seealso:: - `使用cgroup和tc限制带宽 - 行木辛 - 博客园 `_ - `Linux---使用 nice、cpulimit 和 cgroups 限制 cpu 占用率_loyachen的专栏-CSDN博客_linux限制cpu核数 `_ - `探索cgroup2 - 知乎 `_ CGroups 的基本结构 **************************************** 一般来说,CGroups V2 的默认挂载点为 :file:`/sys/fs/cgroup` 。其目录结构和子目录结构是一样的,对于一般信息而言,储存在 *cgroup.\** 文件中 : +------------------------+----------------------+ | 文件 | 内容 | +========================+======================+ | cgroup.controllers | 支持的子系统 | +------------------------+----------------------+ | cgroup.events | | +------------------------+----------------------+ | cgroup.freeze | | +------------------------+----------------------+ | cgroup.kill | | +------------------------+----------------------+ | cgroup.max.depth | | +------------------------+----------------------+ | cgroup.max.descendants | | +------------------------+----------------------+ | cgroup.procs | 从属此 cgroup 的进程 | +------------------------+----------------------+ | cgroup.stat | | +------------------------+----------------------+ | cgroup.subtree_control | | +------------------------+----------------------+ | cgroup.threads | | +------------------------+----------------------+ | cgroup.type | | +------------------------+----------------------+ CPU 子系统 **************************************** CPU 子系统的信息储存在 *cpu.\** 文件中: +-----------------+-----------------+ | 文件 | 内容 | +=================+=================+ | cpu.idle | | +-----------------+-----------------+ | cpu.max | 使用 CPU 的上限 | +-----------------+-----------------+ | cpu.max.burst | | +-----------------+-----------------+ | cpu.pressure | | +-----------------+-----------------+ | cpu.stat | | +-----------------+-----------------+ | cpu.weight | | +-----------------+-----------------+ | cpu.weight.nice | | +-----------------+-----------------+ cpu.max 文件分为两个字段,第一个字段称为 quota,表示进程可以使用多少份 CPU;第二个字段称为 period,表示将 CPU 分为多少份 quota 的值可以为正整数或者 max,max 代表无限制。 另外 quota 的值可以超过 period 的值。 尽管 cpu.max 分为两个字段,但是写入时只写入 quota 即可,period 一般为 100_000 一个限制 CPU 使用的例子为 :: cd /sys/fs/cgroup/ mkdir nvidia-powerd echo 991 >> cgroup.procs cat cpu.max echo 1000 > cpu.max .. seealso:: - `使用cgroups控制进程cpu配额 - 简书 `_ IO 子系统 **************************************** IO 子系统的信息储存在 *io.\** 文件中: +---------------+------+ | 文件 | 内容 | +===============+======+ | io.bfq.weight | | +---------------+------+ | io.latency | | +---------------+------+ | io.max | | +---------------+------+ | io.pressure | | +---------------+------+ | io.prio.class | | +---------------+------+ | io.stat | | +---------------+------+ | io.weight | | +---------------+------+ 内存子系统 **************************************** 内存子系统的信息储存在 *memory.\** 文件中: +---------------------+------+ | 文件 | 内容 | +=====================+======+ | memory.current | | +---------------------+------+ | memory.events | | +---------------------+------+ | memory.events.local | | +---------------------+------+ | memory.high | | +---------------------+------+ | memory.low | | +---------------------+------+ | memory.max | | +---------------------+------+ | memory.min | | +---------------------+------+ | memory.numa_stat | | +---------------------+------+ | memory.oom.group | | +---------------------+------+ | memory.pressure | | +---------------------+------+ | memory.stat | | +---------------------+------+ | memory.swap.current | | +---------------------+------+ | memory.swap.events | | +---------------------+------+ | memory.swap.high | | +---------------------+------+ | memory.swap.max | | +---------------------+------+ 如果进程使用的内存超过了 memory.max,则进程将会触发 OOM