cgroups
cgroups (abbreviated from control groups) is a Linux kernel feature that limits, accounts for, and isolates the resource usage (CPU, memory, disk I/O, network, etc.) of a collection of processes.
Original author(s) | v1: Paul Menage, Rohit Seth v2: Tejun Heo |
---|---|
Developer(s) | Tejun Heo, Johannes Weiner, Michal Hocko, Waiman Long, Roman Gushchin, Chris Down et al. |
Initial release | 2007 |
Written in | C |
Operating system | Linux |
Type | System software |
License | GPL and LGPL |
Website | www |
Engineers at Google (primarily Paul Menage and Rohit Seth) started the work on this feature in 2006 under the name "process containers".[1] 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 2008.[2] Since then, developers have added many new features and controllers, such as support for kernfs in 2014,[3] firewalling,[4] and unified hierarchy.[5] cgroup v2 was merged in Linux kernel 4.5 with significant changes to the interface and internal functionality.[6]
Versions
There are two versions of cgroups.
Cgroups was originally written by Paul Menage and Rohit Seth, and mainlined into the Linux kernel in 2007. Afterwards this is called cgroups version 1.[7]
Then development and maintenance of cgroups was taken over by Tejun Heo. Tejun Heo redesigned and rewrote cgroups. This rewrite is now called version 2, the documentation of cgroups-v2 first appeared in Linux kernel 4.5 released on 14 March 2016.[8]
Unlike v1, cgroup v2 has only a single process hierarchy and discriminates between processes, not threads.
Features
One of the design goals of cgroups is to provide a unified interface to many different use cases, from controlling single processes (by using nice, for example) to full operating system-level virtualization (as provided by OpenVZ, Linux-VServer or LXC, for example). Cgroups provides:
- Resource limiting
- groups can be set to not exceed a configured memory limit, which also includes the file system cache[9][10]
- Prioritization
- some groups may get a larger share of CPU utilization[11] or disk I/O throughput[12]
- Accounting
- measures a group's resource usage, which may be used, for example, for billing purposes[13]
- Control
- freezing groups of processes, their checkpointing and restarting[13]
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;[2] 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,[14] libvirt, systemd, Open Grid Scheduler/Grid Engine,[15] and Google's developmentally defunct lmctfy.
The Linux kernel documentation contains some technical details of the setup and use of control groups version 1[16] and version 2.[17] systemd-cgtop
[18] command can be used to show top control groups by their resource usage.
Redesign
Redesign of cgroups started in 2013,[19] with additional changes brought by versions 3.15 and 3.16 of the Linux kernel.[20][21][22]
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, 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.[23]
- 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.[24]
- "UTS" namespace allows changing the hostname.
- Mount namespace allows creating a different file system layout, or making certain mount points read-only.[25]
- IPC namespace isolates the System V inter-process communication between namespaces.
- User namespace isolates the user IDs between namespaces.[26]
- Cgroup namespace[27]
Namespaces are created with the "unshare" command or syscall, or as new flags in a "clone" syscall.[28]
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.[29]
Unified hierarchy
Kernfs was introduced into the Linux kernel with version 3.14 in March 2014, the main author being Tejun Heo.[30] One of the main motivators for a separate kernfs is the cgroups file system. Kernfs is basically created by splitting off some of the sysfs logic into an independent entity, thus easing for other kernel subsystems the implementation of their own virtual file system with handling for device connect and disconnect, dynamic creation and removal, and other attributes. Redesign continued into version 3.15 of the Linux kernel.[31]
Adoption
Various projects use cgroups as their basis, including CoreOS, Docker (in 2013), Hadoop, Jelastic, Kubernetes,[36] lmctfy (Let Me Contain That For You), LXC (LinuX Containers), systemd, Mesos and Mesosphere,[36] and HTCondor. Major Linux distributions also adopted it such as Red Hat Enterprise Linux (RHEL) 6.0 in November 2010, three years later adoption by the mainline Linux kernel.[37]
On 29 October 2019, the Fedora Project modified Fedora 31 to use CgroupsV2 by default[38]
See also
- Operating system–level virtualization implementations
- Process group
- Tc (Linux) – a traffic control utility that has slight functional overlap with network-oriented cgroup settings
References
- Jonathan Corbet (29 May 2007). "Process containers". LWN.net.
- Jonathan Corbet (29 October 2007). "Notes from a container". LWN.net. Retrieved 14 April 2015.
The original 'containers' name was considered to be too generic – this code is an important part of a container solution, but it's far from the whole thing. So containers have now been renamed 'control groups' (or 'cgroups') and merged for 2.6.24.
- "cgroup: convert to kernfs". lkml. 28 January 2014.
- "netfilter: x_tables: lightweight process control group matching". 23 April 2014. Archived from the original on 24 April 2014.
- "cgroup: prepare for the default unified hierarchy". 13 March 2014.
- "cgroup v2: Multiple hierarchies".
- "diff between Linux kernel 4.4 and 4.5". 14 March 2016.
- "Documentation/cgroup-v2.txt as appeared in Linux kernel 4.5". 14 March 2016.
- Jonathan Corbet (31 July 2007). "Controlling memory use in containers". LWN.
- Balbir Singh, Vaidynathan Srinivasan (July 2007). "Containers: Challenges with the memory resource controller and its performance" (PDF). Ottawa Linux Symposium.
- Jonathan Corbet (23 October 2007). "Kernel space: Fair user scheduling for Linux". Network World. Archived from the original on 19 October 2013. Retrieved 22 August 2012.
- Kamkamezawa Hiroyu (19 November 2008). Cgroup and Memory Resource Controller (PDF). Japan Linux Symposium. Archived from the original (PDF presentation slides) on 22 July 2011.
- Dave Hansen. Resource Management (PDF presentation slides). Linux Foundation.
- Matt Helsley (3 February 2009). "LXC: Linux container tools". IBM developerWorks.
- "Grid Engine cgroups Integration". Scalable Logic. 22 May 2012.
- "cgroups". kernel.org.
- https://github.com/torvalds/linux/blob/master/Documentation/admin-guide/cgroup-v2.rst
- https://www.freedesktop.org/software/systemd/man/systemd-cgtop.html
- "All About the Linux Kernel: Cgroup's Redesign". Linux.com. 15 August 2013. Retrieved 19 May 2014.
- "The unified control group hierarchy in 3.16". LWN.net. 11 June 2014.
- "Pull cgroup updates for 3.15 from Tejun Heo". kernel.org. 3 April 2014.
- "Pull cgroup updates for 3.16 from Tejun Heo". kernel.org. 9 June 2014.
- Pavel Emelyanov, Kir Kolyshkin (19 November 2007). "PID namespaces in the 2.6.24 kernel". LWN.net.
- Jonathan Corbet (30 January 2007). "Network namespaces". LWN.net.
- Serge E. Hallyn, Ram Pai (17 September 2007). "Applying mount namespaces". IBM developerWorks.
- Michael Kerrisk (27 February 2013). "Namespaces in operation, part 5: User namespaces". lwn.net Linux Info from the Source.
- https://lkml.org/lkml/2016/3/26/132/
- Janak Desai (11 January 2006). "Linux kernel documentation on unshare".
- "The Use of Name Spaces in Plan 9". 1992. Archived from the original on 6 September 2014. Retrieved 15 February 2015.
- "kernfs, sysfs, driver-core: implement synchronous self-removal". LWN.net. 3 February 2014. Retrieved 7 April 2014.
- "Linux kernel source tree: kernel/git/torvalds/linux.git: cgroups: convert to kernfs". kernel.org. 11 February 2014. Retrieved 23 May 2014.
- "memcg: kmem controller infrastructure". kernel.org source code. 18 December 2012.
- "memcg: kmem accounting basic infrastructure". kernel.org source code. 18 December 2012.
- "memcg: add documentation about the kmem controller". kernel.org. 18 December 2012.
- https://kernelnewbies.org/Linux_4.19#Memory_management
- "Mesosphere to Bring Google's Kubernetes to Mesos". Mesosphere.io. 10 July 2014. Archived from the original on 6 September 2015. Retrieved 13 July 2014.
- https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/pdf/6.0_Release_Notes/Red_Hat_Enterprise_Linux-6-6.0_Release_Notes-en-US.pdf
- https://bugzilla.redhat.com/show_bug.cgi?id=1732114
External links
- Linux kernel documentation on cgroups
- Linux kernel Namespaces and cgroups by Rami Rosen
- Namespaces and cgroups, the basis of Linux containers (including cgroups v2), slides of a talk by Rami Rosen, Netdev 1.1, Seville, Spain, 2016
- Understanding the new control groups API, LWN.net, by Rami Rosen, March 2016
- Large-scale cluster management at Google with Borg, April 2015, by Abhishek Verma, Luis Pedrosa, Madhukar Korupolu, David Oppenheimer, Eric Tune and John Wilkes
- Job Objects, similar feature on Windows