cgroups

resource limit method in Linux

Uses your system voice; voice and rate in settings.

Q307299Snapshot

≈ 7 min read

Names in other languages

  • Englishcgroups
  • FrançaisCgroups
  • 日本語cgroups
  • 中文cgroups
  • 简体中文cgroups
  • 繁體中文cgroups
  • 中文cgroups
  • 中文cgroups
  • 中文cgroups

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

cgroups (abbreviated from control groups) is a Linux kernel feature that limits, accounts for, and isolates the resource usage (CPU, memory, disk I/O, etc.) of a collection of processes. Engineers at Google started the work on this feature in 2006 under the name "process containers".

In late 2007, the nomenclature changed to "control groups" to avoid confusion caused by multiple meanings of the term "container" in the Linux kernel context, and the control groups functionality was merged into the Linux kernel mainline in kernel version 2.6.24, which was released in January 20081. Since then, developers have added controllers for the kernel's own memory allocation, netfilter firewalling, the OOM killer, and many other parts.

A major change in the history of cgroups is cgroup v2, which removes the ability to use multiple process hierarchies and to discriminate between threads as found in the original cgroup (now called "v1"). Work on the single, unified hierarchy started with the repurposing of v1's dummy hierarchy as a place for holding all controllers not yet used by others in 2014. cgroup v2 was merged in Linux kernel 4.5 (2016).

Use

A control group (abbreviated as cgroup) is a collection of processes that are bound by the same criteria and associated with a set of parameters or limits. These groups can be hierarchical, meaning that each group inherits limits from its parent group.

The kernel provides access to multiple controllers (also called subsystems) through the cgroup interface; for example, the "memory" controller limits memory use, "cpuacct" accounts CPU usage, etc. Control groups can be used in multiple ways: By accessing the cgroup virtual file system manually. By creating and managing groups on the fly using tools like cgcreate , cgexec , and cgclassify (from libcgroup ).

Through the "rules engine daemon" that can automatically move processes of certain users, groups, or commands to cgroups as specified in its configuration. Indirectly through other software that uses cgroups, such as Docker, Firejail, LXC, libvirt, systemd, Open Grid Scheduler/Grid Engine, and Google's developmentally defunct lmctfy.

The Linux kernel documentation contains some technical details of the setup and use of control groups version 1 and version 2. These files are named according to the controllers that handle them. For example, deal with the cgroup system itself and deal with the memory subsystem.

Example: to request the kernel to 1 gigabyte of memory from anywhere in the system, one can run echo "1G swappiness=50" > /sys/fs/cgroup/memory.reclaim . To create a subgroup, one simply creates a new directory under an existing group (including the top-level one). The files corresponding to available controls for this group are automatically created.

For example, running mkdir /sys/fs/cgroup/example; ls /sys/fs/cgroup/example would produce a list of files largely similar to the one above, but with noticeable changes. On one example system, these files are added: cgroup.events cgroup.freeze cgroup.kill cgroup.type cpu.idle cpu.max cpu.max.burst cpu.pressure cpu.uclamp.max cpu.uclamp.min cpu.weight cpu.weight.nice memory.current memory.events memory.events.local memory.high memory.low memory.max memory.min memory.oom.group memory.peak memory.swap.current memory.swap.events memory.swap.high memory.swap.max memory.swap.peak memory.zswap.current memory.zswap.max pids.current pids.events pids.events.local pids.max pids.peak These changes are not unexpected because some controls and statistics only make sense on a subset of processes (e.g. nice level being the CPU priority of processes relative to the rest of the system).

Processes are assigned to subgroups by writing to /proc//cgroup . The cgroup a process is in can be found by reading the same file. On systems based on systemd, a hierarchy of subgroups is predefined to encapsulate every process directly and indirectly launched by systemd under a subgroup: the very basis of how systemd manages processes.

An explanation of the nomenclature of these groups can be found in the Red Hat Enterprise Linux 7 manual. Red Hat also provides a guide on creating a systemd service file that causes a process to run in a separate cgroup. systemd-cgtop command can be used to show top control groups by their resource usage.

Namespace isolation

While not technically part of the cgroups work, a related feature of the Linux kernel is namespace isolation, where groups of processes are separated such that they cannot "see" resources in other groups. For example, a PID namespace provides a separate enumeration of process identifiers within each namespace.

Also available are mount, user, UTS (Unix Time Sharing), network and SysV IPC namespaces. The PID namespace provides isolation for the allocation of process identifiers (PIDs), lists of processes and their details. While the new namespace is isolated from other siblings, processes in its "parent" namespace still see all processes in child namespaces—albeit with different PID numbers.

Network namespace isolates the network interface controllers (physical or virtual), iptables firewall rules, routing tables etc. Network namespaces can be connected with each other using the "veth" virtual Ethernet device. "UTS" namespace allows changing the hostname. Mount namespace allows creating a different file system layout, or making certain mount points read-only.

IPC namespace isolates the System V inter-process communication between namespaces. User namespace isolates the user IDs between namespaces. Cgroup namespace Namespaces are created with the "unshare" command or syscall, or as "new" flags in a "clone" syscall.

The "ns" subsystem was added early in cgroups development to integrate namespaces and control groups. If the "ns" cgroup was mounted, each namespace would also create a new group in the cgroup hierarchy. This was an experiment that was later judged to be a poor fit for the cgroups API, and removed from the kernel.

Linux namespaces were inspired by the more general namespace functionality used heavily throughout Plan 9 from Bell Labs.

Adoption

Various projects use cgroups as their basis, including CoreOS, Docker (in 2013), Hadoop, Jelastic, Kubernetes, lmctfy (Let Me Contain That For You), LXC (Linux Containers), systemd, Mesos and Mesosphere, HTCondor, and Flatpak. Major Linux distributions also adopted it such as Red Hat Enterprise Linux (RHEL) 6.0 in November 2010, three years before adoption by the mainline Linux kernel.

On 29 October 2019, the Fedora Project modified Fedora 31 to use CgroupsV2 by default. Job Objects, similar feature on Windows

Text derived from https://en.wikipedia.org/w/index.php?oldid=1362914898 under CC BY-SA 4.0; modifications have been made.

References

  1. ↩Wikipedia “cgroups” revision 1362914898 · retrieved 2026-10-20 release date (×1) · confidence 0.95 revision URL