arthrop0d

Brief articles on varied topics.

Linux cgroups v2: Hierarchical Resource Control

linuxoperating systems

Linux cgroups v2 are kernel-managed groups of processes that impose resource policies on those processes. A cgroup can limit how much CPU, memory, or block-device I/O its members use, and cgroups can be arranged in a hierarchy so that limits apply at several levels.

The problem cgroups solve

Without cgroups, a process is mainly constrained by the machine as a whole. The scheduler shares CPU time among runnable processes, virtual memory competes for RAM, and I/O requests compete for storage bandwidth. A busy batch job can therefore make an API service slow, or one container can consume memory needed by its neighbors.

Traditional Unix tools such as nice, ulimit, and process priorities address individual processes or selected resource properties. They do not provide one consistent boundary around a service and all of its workers. Cgroups provide that boundary. A service manager, container runtime, or administrator can place a process in a cgroup; processes it starts normally inherit that membership.

You have probably encountered cgroups indirectly in a container error, a systemd setting, or a file such as memory.max. The interface is a virtual filesystem, usually mounted at /sys/fs/cgroup. Configuration is performed by reading and writing files there, not by making a special system call for each limit.

The unified hierarchy

Cgroups v2 uses one unified hierarchy. Directories represent cgroups, and the files inside them expose control settings and measurements:

/sys/fs/cgroup/
├── cgroup.controllers
├── cgroup.subtree_control
├── app.slice/
│   ├── cgroup.procs
│   ├── memory.max
│   └── worker/
│       └── cpu.max

The root cgroup contains all processes initially. Creating a directory creates a child cgroup. A child inherits the hierarchy's structure, but controllers must be enabled for that part of the tree before their control files become usable.

cgroup.controllers lists controllers available to a cgroup's children. cgroup.subtree_control lists the controllers enabled for those children. For example, writing +cpu +memory +io to a cgroup's cgroup.subtree_control enables those controllers below that cgroup. A controller must be available and enabled at every relevant ancestor; enabling it in a deep directory cannot bypass a parent that has not enabled it.

This produces nested budgets. Suppose a cgroup for a whole application has a memory limit of 4 GiB, with separate child cgroups for its web and worker processes. The children can have their own limits, but together they cannot escape the application's 4 GiB limit. The parent is an aggregate boundary, not merely a default copied into each child.

A cgroup generally acts as an internal grouping point or a leaf where processes run. In the normal “domain” mode of v2, a non-root cgroup containing processes cannot also distribute domain-controller resources to child cgroups. This rule prevents ambiguous accounting: put the processes in leaf cgroups, and use parent cgroups to organize and constrain them. Specialized threaded cgroups exist for thread-level layouts, but most services do not need them.

CPU control

The CPU controller governs scheduling time, not a fixed number of processor cores.

cpu.weight is a relative share, normally from 1 to 10,000. If two sibling cgroups are competing for CPU and have weights 100 and 200, the second receives roughly twice as much of the contested CPU time. Shares matter when there is contention; an idle machine does not make a group use only its proportional share.

cpu.max imposes a quota over a repeating period. Its value has the form:

50000 100000

The first number is the allowed CPU time in microseconds, and the second is the period in microseconds. This example permits 50 milliseconds of CPU time every 100 milliseconds—an aggregate limit of 50% of one CPU. Multiple threads can consume that quota in parallel, so it is not necessarily equivalent to pinning the process to half a core. max can replace the quota to mean unlimited.

When a group exhausts its quota, runnable tasks are throttled until the next period. They are delayed rather than killed.

Memory control

The memory controller accounts for memory charged to a cgroup, including ordinary process memory and relevant kernel-managed memory. Its main controls are:

memory.current reports current usage, while memory.events records events such as high-limit pressure and out-of-memory conditions. A parent accounts for the usage of its descendants, so a child can be below its own limit while the application as a whole reaches its parent limit.

I/O control

The I/O controller regulates block-device traffic, such as reads and writes to disks. io.max can set limits for a particular device, identified by its major and minor device numbers, using values such as rbps (read bytes per second), wbps (write bytes per second), riops, and wiops.

For example, a policy conceptually written in the cgroup's io.max file might limit writes to a device to 10 MiB per second. The kernel delays requests when the configured rate is exceeded. io.weight instead gives sibling cgroups relative priority for contested I/O, much like cpu.weight does for CPU time. Actual behavior depends on the storage stack and whether traffic is buffered, cached, or handled by a device that exposes meaningful queue control.

Assigning a process

The key membership file is cgroup.procs. Writing a process ID to a cgroup's file moves that process into the cgroup:

# conceptual shell operations
mkdir /sys/fs/cgroup/my-service
# enable controllers in the parent, then configure my-service
printf '50000 100000' > /sys/fs/cgroup/my-service/cpu.max
printf '1073741824' > /sys/fs/cgroup/my-service/memory.max
printf '12345' > /sys/fs/cgroup/my-service/cgroup.procs

The process and subsequently created child processes are then accounted for by my-service, unless they are deliberately moved elsewhere. A process can normally belong to only one cgroup in a hierarchy. Moving it requires permission over both the source and destination arrangements; this is why container runtimes and service managers perform setup with elevated privileges.

The process does not need to know it has been grouped. The kernel charges its resource use to the cgroup and enforces each applicable ancestor's policy. That combination—explicit membership, inherited hierarchy, and controller-specific limits—is what lets one machine safely host services with different resource budgets.

← All articles