Control Groups (cgroups)
Relevant source files
The following files were used as context for generating this wiki page:
Control Groups (cgroups) organize processes into hierarchical groups to manage and monitor system resources. The cgroup framework allows the kernel to limit, account for, and isolate resource usage—such as CPU, memory, and disk I/O—for specific sets of tasks.
The kernel provides two primary versions of the cgroup interface: v1 and v2. Version 1 supports multiple concurrent hierarchies, while version 2 implements a unified hierarchy to simplify resource management and resolve internal consistency issues.
Sources: Documentation/admin-guide/index.rst:87, Documentation/core-api/index.rst:54
Core Architecture
The cgroup system relies on a hierarchical structure where each node in the tree represents a group of processes. Subsystems, also known as resource controllers, attach to these hierarchies to enforce specific resource policies.
Unified vs. Legacy Hierarchies
In cgroup v1, you can mount each controller on a separate hierarchy, allowing a process to belong to different groups for different resources. In cgroup v2, all controllers reside on a single unified hierarchy. This ensures that process membership is consistent across all resource types.
The diagram above represents the unified hierarchy model where multiple resource controllers manage the same group structure.
Sources: Documentation/admin-guide/cgroup-v2.rst, Documentation/admin-guide/cgroup-v1/index.rst
Resource Controllers
Resource controllers are the kernel components that perform the actual work of limiting and monitoring resources.
Common Controllers
| Controller | Description |
|---|---|
| cpu | Regulates CPU cycles using scheduler weights or hard limits. |
| memory | Tracks and limits resident set size (RSS), address space, and swap usage. |
| io | Controls block I/O bandwidth and operations per second. |
| pids | Limits the number of processes that can be created within a cgroup. |
| cpuset | Assigns specific CPUs and Memory Nodes to a group of tasks. |
Sources: Documentation/admin-guide/index.rst:88-92, Documentation/core-api/index.rst:54
Interface and Management
The cgroup system uses a virtual filesystem (typically mounted at /sys/fs/cgroup) to expose management interfaces to userspace. You interact with cgroups by creating directories and writing to specific control files.
Key Management Operations
- Creation: Create a directory within the cgroup filesystem to initialize a new group.
- Membership: Write a process ID (PID) to the
cgroup.procsfile to move a process into a group. - Control: Modify controller-specific files (e.g.,
cpu.maxormemory.high) to set resource boundaries. - Monitoring: Read status files (e.g.,
cpu.statormemory.events) to retrieve usage statistics.
This sequence illustrates the standard workflow for creating a group, assigning a process, and applying a resource limit.
Sources: Documentation/admin-guide/cgroup-v2.rst, Documentation/translations/zh_CN/admin-guide/index.rst:55
Version Differences
The transition from v1 to v2 introduced several structural changes to improve usability and eliminate edge cases in resource distribution.
| Feature | cgroup v1 | cgroup v2 |
|---|---|---|
| Hierarchy | Multiple (per-controller) | Single Unified |
| Task Assignment | Thread-level or Process-level | Process-level (mostly) |
| Empty Groups | Notifications via release_agent | Notifications via cgroup.events |
| Internal Nodes | Can contain both tasks and subgroups | Cannot contain both tasks and subgroups (Internal Process Constraint) |
Sources: Documentation/admin-guide/cgroup-v2.rst, Documentation/admin-guide/cgroup-v1/index.rst
Summary
Control Groups provide a robust framework for managing system resources in the Linux kernel. By organizing tasks into hierarchies and applying controllers, administrators can ensure quality of service, prevent resource exhaustion, and facilitate containerization. Version 2 represents the modern standard, providing a unified and consistent model for all resource management needs.
Sources: Documentation/index.rst:55, Documentation/core-api/index.rst:54