Part 3 of 7 · 2 min read
Controllers
Think of the dials on a shared water supply: one for volume, one for pressure, one for the number of taps open. Each dial has a label and a range. The analogy stops in one place: the dials of a building are fixed, while a cgroup has a few controllers that the kernel may or may not have switched on.
In plain words: Each controller has its own files. The test machine lists its controllers in /proc/cgroups: cpuset, cpu, cpuacct, blkio, memory, devices, freezer and pids, among others. The main limits:
| Controller | Version 1 file (test machine) | Version 2 file | Meaning |
|---|---|---|---|
| memory | memory.limit_in_bytes |
memory.max |
Memory cap of the group in bytes |
| cpu | cpu.cfs_quota_us and cpu.cfs_period_us |
cpu.max ("quota period") |
CPU time the group may use in each period |
| pids | pids.max |
pids.max |
The most processes and threads in the group |
| io | blkio.throttle.* |
io.max |
Disk bytes and operations per second |
Every group also has files that report use: memory.max_usage_in_bytes, memory.failcnt (the number of times the limit was hit), cpu.stat (including throttling) and pids.current.
A worked example. To make a memory group of 20 MiB and put a shell in it (the version 1 layout of the test machine):
$ mkdir /sys/fs/cgroup/memory/demo
$ echo 20971520 > /sys/fs/cgroup/memory/demo/memory.limit_in_bytes
$ sh -c 'echo $$ > /sys/fs/cgroup/memory/demo/cgroup.procs; exec ./mem'
20 MiB = 20 x 1,048,576 = 20,971,520 bytes. The shell writes its own ID into cgroup.procs, and then exec replaces it with the program, which stays in the group, as do all its children.
The key lines. The unit of a limit is bytes, microseconds or counts, as the file says. A value of the maximum number (or max in version 2) means no limit.
Controller
A controller (or subsystem) is the part of the cgroup mechanism that manages one resource, such as memory, processor time or process count. Each controller exposes its own limit and accounting files in every group.
Example: the memory controller provides memory.limit_in_bytes and memory.failcnt.
Check yourself:
1. Which file moves a process into a group?
2. What does a value of max mean in version 2?
3. Which two files give the CPU limit in version 1?