Cgroups Explained
cgroups |
cgroups |
Author: | v1: Paul Menage, Rohit Seth, Memory Controller by Balbir Singh, CPU controller by Srivatsa Vaddagiri v2: Tejun Heo |
Developer: | Tejun Heo, Johannes Weiner, Michal Hocko, Waiman Long, Roman Gushchin, Chris Down et al. |
Programming Language: | C |
Operating System: | Linux |
Genre: | System software |
License: | GPL and LGPL |
Website: | , |
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.[1]) of a collection of processes.
Engineers at Google started the work on this feature in 2006 under the name "process containers".[2] 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.[3] Since then, developers have added many new features and controllers, such as support for kernfs in 2014,[4] firewalling,[5] and unified hierarchy.[6] cgroup v2 was merged in Linux kernel 4.5 with significant changes to the interface and internal functionality.[7]
Versions
There are two versions of cgroups.
Cgroups was originally written by Paul Menage and Rohit Seth, and merged into the mainline Linux kernel in 2007. Afterwards this is called cgroups version 1.[8]
Development and maintenance of cgroups was then taken over by Tejun Heo. Tejun Heo redesigned and rewrote cgroups. This rewrite is now called version 2, the documentation of cgroup-v2 first appeared in Linux kernel 4.5 released on 14 March 2016.[9]
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 not to exceed a configured memory limit, which also includes the file system cache,[10] [11] I/O bandwidth limit,[12] CPU quota limit,[13] CPU set limit,[14] or maximum open files.[15]
- Prioritization
some groups may get a larger share of CPU utilization[16] or disk I/O throughput[17]
- Accounting
measures a group's resource usage, which may be used, for example, for billing purposes[18]
- Control
freezing groups of processes, their checkpointing and restartingUse
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,[19] libvirt, systemd, Open Grid Scheduler/Grid Engine,[20] and Google's developmentally defunct lmctfy.
The Linux kernel documentation contains some technical details of the setup and use of control groups version 1[21] and version 2.[22] systemd-cgtop
[23] command can be used to show top control groups by their resource usage.
Redesign
Redesign of cgroups started in 2013,[24] with additional changes brought by versions 3.15 and 3.16 of the Linux kernel.[25] [26] [27]
Namespace isolation
See main article: Linux namespaces. 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.[28]
- 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.[29]
- "UTS" namespace allows changing the hostname.
- Mount namespace allows creating a different file system layout, or making certain mount points read-only.[30]
- IPC namespace isolates the System V inter-process communication between namespaces.
- User namespace isolates the user IDs between namespaces.[31]
- Cgroup namespace[32]
Namespaces are created with the "unshare" command or syscall, or as "new" flags in a "clone" syscall.[33]
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.[34]
Unified hierarchy
Kernfs was introduced into the Linux kernel with version 3.14 in March 2014, the main author being Tejun Heo.[35] 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.[36]
Kernel memory control groups (kmemcg)
Kernel memory control groups (kmemcg) were merged into version 3.8 of the Linux kernel mainline.[37] [38] [39] The kmemcg controller can limit the amount of memory that the kernel can utilize to manage its own internal processes.
cgroup awareness of OOM killer
Linux Kernel 4.19 (October 2018) introduced cgroup awareness of OOM killer implementation which adds an ability to kill a cgroup as a single unit and so guarantee the integrity of the workload.[40]
Adoption
Various projects use cgroups as their basis, including CoreOS, Docker (in 2013), Hadoop, Jelastic, Kubernetes,[41] lmctfy (Let Me Contain That For You), LXC (Linux Containers), systemd, Mesos and Mesosphere,[41] and HTCondor.
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.[42]
On 29 October 2019, the Fedora Project modified Fedora 31 to use CgroupsV2 by default[43]
See also
- Operating system–level virtualization implementations
- Process group
- Tc (Linux) a traffic control utility slightly overlapping in functionality with network-oriented cgroup settings
- Job object the equivalent Windows concept, as managed by that platform’s Object Manager
External links
- Official Linux kernel documentation on cgroups v1 and cgroups v2
- Red Hat Resource Management Guide on cgroups
- Ubuntu manpage on cgroups
- Linux kernel Namespaces and cgroups by Rami Rosen (2013)
- Namespaces and cgroups, the basis of Linux containers (including cgroups v2), slides of a talk by Rami Rosen, Netdev 1.1, Seville, Spain, 2016
- [//lwn.net/Articles/679786 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
Notes and References
- Web site: Control Group v2 — The Linux Kernel documentation . 2023-06-24 . www.kernel.org.
- News: 29 May 2007 . Jonathan Corbet . [//lwn.net/Articles/236038/ Process containers ]. LWN.net .
- News: 29 October 2007 . Jonathan Corbet . [//lwn.net/Articles/256389/ Notes from a container ]. . 2015-04-14 . 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..
- Web site: cgroup: convert to kernfs . Linux kernel mailing list. 2014-01-28.
- Web site: netfilter: x_tables: lightweight process control group matching . 2014-04-23 . dead . https://web.archive.org/web/20140424095421/http://www.spinics.net/lists/netdev/msg264727.html . 24 April 2014 . dmy .
- Web site: cgroup: prepare for the default unified hierarchy . 2014-03-13.
- Web site: cgroup v2: Multiple hierarchies.
- Web site: diff between Linux kernel 4.4 and 4.5 . 2016-03-14.
- Web site: Documentation/cgroup-v2.txt as appeared in Linux kernel 4.5 . 2016-03-14.
- News: 31 July 2007 . Jonathan Corbet . [//lwn.net/Articles/243795/ Controlling memory use in containers ]. LWN .
- Web site: Containers: Challenges with the memory resource controller and its performance . Balbir Singh, Vaidynathan Srinivasan . July 2007. Ottawa Linux Symposium .
- Web site: Carvalho . André . Using cgroups to limit I/O . 2022-09-12 . andrestc.com. 18 October 2017 .
- Web site: Luu . Dan . The container throttling problem . 2022-09-12 . danluu.com.
- Web site: Derr . Simon . 2004 . CPUSETS . 2022-09-12.
- Web site: setrlimit(2) — Arch manual pages . 2023-11-27 . man.archlinux.org.
- News: 23 October 2007 . Jonathan Corbet . Kernel space: Fair user scheduling for Linux . Network World . 2012-08-22 . https://web.archive.org/web/20131019123524/http://www.networkworld.com/news/2007/101207-kernel.html . 19 October 2013 . dead . dmy-all .
- 19 November 2008 . Kamkamezawa Hiroyu . Cgroup and Memory Resource Controller . PDF presentation slides . Japan Linux Symposium . dead . https://web.archive.org/web/20110722113016/http://www.linuxfoundation.jp/jp_uploads/seminar20081119/CgroupMemcgMaster.pdf . 22 July 2011 . dmy-all .
- Dave Hansen . Resource Management . PDF presentation slides . Linux Foundation .
- News: Matt Helsley . 3 February 2009 . LXC: Linux container tools . IBM developerWorks .
- Web site: Grid Engine cgroups Integration. 2012-05-22 . Scalable Logic.
- Web site: cgroups . kernel.org.
- Web site: Torvalds/Linux. GitHub. 13 February 2022.
- Web site: Systemd-cgtop.
- Web site: All About the Linux Kernel: Cgroup's Redesign . . 2013-08-15 . 2014-05-19 . 28 April 2019 . https://web.archive.org/web/20190428203713/https://www.linuxfoundation.org/blog/2013/08/all-about-the-linux-kernel-cgroups-redesign/ . dead .
- Web site: [//lwn.net/Articles/601840/ The unified control group hierarchy in 3.16 ]. . 2014-06-11.
- Web site: Pull cgroup updates for 3.15 from Tejun Heo . . 2014-04-03.
- Web site: Pull cgroup updates for 3.16 from Tejun Heo . . 2014-06-09.
- News: Pavel Emelyanov, Kir Kolyshkin . 19 November 2007 . [//lwn.net/Articles/259217/ PID namespaces in the 2.6.24 kernel ]. LWN.net .
- News: 30 January 2007 . Jonathan Corbet . [//lwn.net/Articles/219794/ Network namespaces ]. LWN.net .
- News: Serge E. Hallyn, Ram Pai . 17 September 2007 . Applying mount namespaces . IBM developerWorks .
- News: 27 February 2013. Michael Kerrisk. [//lwn.net/Articles/532593/ Namespaces in operation, part 5: User namespaces]. lwn.net Linux Info from the Source.
- Web site: LKML: Linus Torvalds: Linux 4.6-rc1.
- Web site: 11 January 2006 . Janak Desai . Linux kernel documentation on unshare .
- Web site: The Use of Name Spaces in Plan 9 . 1992 . 15 February 2015 . https://web.archive.org/web/20140906153815/http://www.cs.bell-labs.com/sys/doc/names.html . 6 September 2014 . dead . dmy-all .
- Web site: [//lwn.net/Articles/584019/ kernfs, sysfs, driver-core: implement synchronous self-removal ]. . 2014-02-03 . 2014-04-07.
- Web site: Linux kernel source tree: kernel/git/torvalds/linux.git: cgroups: convert to kernfs . 2014-02-11 . 2014-05-23 . kernel.org.
- Web site: memcg: kmem controller infrastructure . kernel.org source code. 2012-12-18 .
- Web site: memcg: kmem accounting basic infrastructure . kernel.org source code . 2012-12-18.
- Web site: memcg: add documentation about the kmem controller . . 2012-12-18.
- Web site: Linux_4.19 - Linux Kernel Newbies .
- Web site: Mesosphere to Bring Google's Kubernetes to Mesos . Mesosphere.io . 2014-07-10 . 2014-07-13 . https://web.archive.org/web/20150906165229/https://mesosphere.com/blog/2014/07/10/mesosphere-announces-kubernetes-on-mesos/ . 6 September 2015 . dead .
- Web site: 6.0 Release Notes. redhat.com. 12 September 2023.
- Web site: 1732114 – Modify Fedora 31 to use CgroupsV2 by default.