mount -t cgroup -o cpu none /sys/fs/cgroup/cpu
It is possible to comount multiple controllers against the same hierarchy. For example, here the cpu and cpuacct controllers are comounted against a single hierarchy:
mount -t cgroup -o cpu,cpuacct none /sys/fs/cgroup/cpu,cpuacct
Comounting controllers has the effect that a process is in the same cgroup for all of the comounted controllers. Separately mounting controllers allows a process to be in cgroup /foo1 for one controller while being in /foo2/foo3 for another. It is possible to comount all v1 controllers against the same hierarchy:
mount -t cgroup -o all cgroup /sys/fs/cgroup
(One can achieve the same result by omitting -o all, since it is the default if no controllers are explicitly specified.) It is not possible to mount the same controller against multiple cgroup hierarchies. For example, it is not possible to mount both the cpu and cpuacct controllers against one hierarchy, and to mount the cpu controller alone against another hierarchy. It is possible to create multiple mount points with exactly the same set of comounted controllers. However, in this case all that results is multiple mount points providing a view of the same hierarchy. Note that on many systems, the v1 controllers are automatically mounted under /sys/fs/cgroup; in particular, systemd(1) automatically creates such mount points. umount(8) command, as in the following example:
But note well: a cgroup filesystem is unmounted only if it is not busy, that is, it has no child cgroups. If this is not the case, then the only effect of the umount(8) is to make the mount invisible. Thus, to ensure that the mount point is really removed, one must first remove all child cgroups, which in turn can be done only after all member processes have been moved from those cgroups to the root cgroup.
- cpu (since Linux 2.6.24; CONFIG_CGROUP_SCHED)
- Cgroups can be guaranteed a minimum number of "CPU shares" when a system is busy. This does not limit a cgroup's CPU usage if the CPUs are not busy. For further information, see Documentation/scheduler/sched-design-CFS.txt.
- In Linux 3.2, this controller was extended to provide CPU "bandwidth" control. If the kernel is configured with CONFIG_CFS_BANDWIDTH, then within each scheduling period (defined via a file in the cgroup directory), it is possible to define an upper limit on the CPU time allocated to the processes in a cgroup. This upper limit applies even if there is no other competition for the CPU. Further information can be found in the kernel source file Documentation/scheduler/sched-bwc.txt.
- cpuacct (since Linux 2.6.24; CONFIG_CGROUP_CPUACCT)
- This provides accounting for CPU usage by groups of processes.
- Further information can be found in the kernel source file Documentation/cgroup-v1/cpuacct.txt.
- cpuset (since Linux 2.6.24; CONFIG_CPUSETS)
- This cgroup can be used to bind the processes in a cgroup to a specified set of CPUs and NUMA nodes.
- Further information can be found in the kernel source file Documentation/cgroup-v1/cpusets.txt.
- memory (since Linux 2.6.25; CONFIG_MEMCG)
- The memory controller supports reporting and limiting of process memory, kernel memory, and swap used by cgroups.
- Further information can be found in the kernel source file Documentation/cgroup-v1/memory.txt.
- devices (since Linux 2.6.26; CONFIG_CGROUP_DEVICE)
- This supports controlling which processes may create (mknod) devices as well as open them for reading or writing. The policies may be specified as whitelists and blacklists. Hierarchy is enforced, so new rules must not violate existing rules for the target or ancestor cgroups.
- Further information can be found in the kernel source file Documentation/cgroup-v1/devices.txt.
- freezer (since Linux 2.6.28; CONFIG_CGROUP_FREEZER)
- The freezer cgroup can suspend and restore (resume) all processes in a cgroup. Freezing a cgroup /A also causes its children, for example, processes in /A/B, to be frozen.
- Further information can be found in the kernel source file Documentation/cgroup-v1/freezer-subsystem.txt.
- net_cls (since Linux 2.6.29; CONFIG_CGROUP_NET_CLASSID)
- This places a classid, specified for the cgroup, on network packets created by a cgroup. These classids can then be used in firewall rules, as well as used to shape traffic using tc(8). This applies only to packets leaving the cgroup, not to traffic arriving at the cgroup.
- Further information can be found in the kernel source file Documentation/cgroup-v1/net_cls.txt.
- blkio (since Linux 2.6.33; CONFIG_BLK_CGROUP)
- The blkio cgroup controls and limits access to specified block devices by applying IO control in the form of throttling and upper limits against leaf nodes and intermediate nodes in the storage hierarchy.
- Two policies are available. The first is a proportional-weight time-based division of disk implemented with CFQ. This is in effect for leaf nodes using CFQ. The second is a throttling policy which specifies upper I/O rate limits on a device.
- Further information can be found in the kernel source file Documentation/cgroup-v1/blkio-controller.txt.
- perf_event (since Linux 2.6.39; CONFIG_CGROUP_PERF)
- This controller allows perf monitoring of the set of processes grouped in a cgroup.
- Further information can be found in the kernel source file tools/perf/Documentation/perf-record.txt.
- net_prio (since Linux 3.3; CONFIG_CGROUP_NET_PRIO)
- This allows priorities to be specified, per network interface, for cgroups.
- Further information can be found in the kernel source file Documentation/cgroup-v1/net_prio.txt.
- hugetlb (since Linux 3.5; CONFIG_CGROUP_HUGETLB)
- This supports limiting the use of huge pages by cgroups.
- Further information can be found in the kernel source file Documentation/cgroup-v1/hugetlb.txt.
- pids (since Linux 4.3; CONFIG_CGROUP_PIDS)
- This controller permits limiting the number of process that may be created in a cgroup (and its descendants).
- Further information can be found in the kernel source file Documentation/cgroup-v1/pids.txt.
- rdma (since Linux 4.11; CONFIG_CGROUP_RDMA)
- The RDMA controller permits limiting the use of RDMA/IB-specific resources per cgroup.
- Further information can be found in the kernel source file Documentation/cgroup-v1/rdma.txt.
This creates a new empty cgroup. A process may be moved to this cgroup by writing its PID into the cgroup's cgroup.procs file:
echo $$ > /sys/fs/cgroup/cpu/cg1/cgroup.procs
Only one PID at a time should be written to this file. Writing the value 0 to a cgroup.procs file causes the writing process to be moved to the corresponding cgroup. When writing a PID into the cgroup.procs, all threads in the process are moved into the new cgroup at once. Within a hierarchy, a process can be a member of exactly one cgroup. Writing a process's PID to a cgroup.procs file automatically removes it from the cgroup of which it was previously a member. The cgroup.procs file can be read to obtain a list of the processes that are members of a cgroup. The returned list of PIDs is not guaranteed to be in order. Nor is it guaranteed to be free of duplicates. (For example, a PID may be recycled while reading from the list.) In cgroups v1, an individual thread can be moved to another cgroup by writing its thread ID (i.e., the kernel thread ID returned by clone(2) and gettid(2)) to the tasks file in a cgroup directory. This file can be read to discover the set of threads that are members of the cgroup.
mount -o release_agent=pathname ...
Whether or not the release_agent program is invoked when a particular cgroup becomes empty is determined by the value in the notify_on_release file in the corresponding cgroup directory. If this file contains the value 0, then the release_agent program is not invoked. If it contains the value 1, the release_agent program is invoked. The default value for this file in the root cgroup is 0. At the time when a new cgroup is created, the value in this file is inherited from the corresponding file in the parent cgroup.
mount -t cgroup -o none,name=somename none /some/mount/point
Multiple instances of such hierarchies can be mounted; each hierarchy must have a unique name. The only purpose of such hierarchies is to track processes. (See the discussion of release notification below.) An example of this is the name=systemd cgroup hierarchy that is used by systemd(1) to track services and user sessions.
- Cgroups v2 provides a unified hierarchy against which all controllers are mounted.
- "Internal" processes are not permitted. With the exception of the root cgroup, processes may reside only in leaf nodes (cgroups that do not themselves contain child cgroups). The details are somewhat more subtle than this, and are described below.
- Active cgroups must be specified via the files cgroup.controllers and cgroup.subtree_control.
- The tasks file has been removed. In addition, the cgroup.clone_children file that is employed by the cpuset controller has been removed.
- An improved mechanism for notification of empty cgroups is provided by the cgroup.events file.
mount -t cgroup2 none /mnt/cgroup2
A cgroup v2 controller is available only if it is not currently in use via a mount against a cgroup v1 hierarchy. Or, to put things another way, it is not possible to employ the same controller against both a v1 hierarchy and the unified v2 hierarchy. This means that it may be necessary first to unmount a v1 controller (as described above) before that controller is available in v2. Since systemd(1) makes heavy use of some v1 controllers by default, it can in some cases be simpler to boot the system with selected v1 controllers disabled. To do this, specify the cgroup_no_v1=list option on the kernel boot command line; list is a comma-separated list of the names of the controllers to disable, or the word all to disable all v1 controllers. (This situation is correctly handled by systemd(1), which falls back to operating without the specified controllers.) Note that on many modern systems, systemd(1) automatically mounts the cgroup2 filesystem at /sys/fs/cgroup/unified during the boot process.
- io (since Linux 4.5)
- This is the successor of the version 1 blkio controller.
- memory (since Linux 4.5)
- This is the successor of the version 1 memory controller.
- pids (since Linux 4.5)
- This is the same as the version 1 pids controller.
- perf_event (since Linux 4.11)
- This is the same as the version 1 perf_event controller.
- rdma (since Linux 4.11)
- This is the same as the version 1 rdma controller.
- cpu (since Linux 4.15)
- This is the successor to the version 1 cpu and cpuacct controllers.
- This read-only file exposes a list of the controllers that are available in this cgroup. The contents of this file match the contents of the cgroup.subtree_control file in the parent cgroup.
- This is a list of controllers that are active (enabled) in the cgroup. The set of controllers in this file is a subset of the set in the cgroup.controllers of this cgroup. The set of active controllers is modified by writing strings to this file containing space-delimited controller names, each preceded by '+' (to enable a controller) or '-' (to disable a controller), as in the following example:
echo '+pids -memory' > x/y/cgroup.subtree_control
- An attempt to enable a controller that is not present in cgroup.controllers leads to an ENOENT error when writing to the cgroup.subtree_control file.
- This is the total number of visible (i.e., living) descendant cgroups underneath this cgroup.
- This is the total number of dying descendant cgroups underneath this cgroup. A cgroup enters the dying state after being deleted. It remains in that state for an undefined period (which will depend on system load) while resources are freed before the cgroup is destroyed. Note that the presence of some cgroups in the dying state is normal, and is not indicative of any problem.
- A process can't be made a member of a dying cgroup, and a dying cgroup can't be brought back to life.
- cgroup.max.depth (since Linux 4.14)
- This file defines a limit on the depth of nesting of descendant cgroups. A value of 0 in this file means that no descendant cgroups can be created. An attempt to create a descendant whose nesting level exceeds the limit fails (mkdir(2) fails with the error EAGAIN).
- Writing the string "max" to this file means that no limit is imposed. The default value in this file is "max".
- cgroup.max.descendants (since Linux 4.14)
- This file defines a limit on the number of live descendant cgroups that this cgroup may have. An attempt to create more descendants than allowed by the limit fails (mkdir(2) fails with the error EAGAIN).
- Writing the string "max" to this file means that no limit is imposed. The default value in this file is "max".
- Changing the ownership of the root of the subtree means that any new cgroups created under the subtree (and the files they contain) will also be owned by the delegatee.
- Changing the ownership of this file means that the delegatee can move processes into the root of the delegated subtree.
- Changing the ownership of this file means that that the delegatee can enable controllers (that are present in /dlgt_grp/cgroup.controllers) in order to further redistribute resources at lower levels in the subtree. (As an alternative to changing the ownership of this file, the delegater might instead add selected controllers to this file.)
- Changing the ownership of this file is necessary if a threaded subtree is being delegated (see the description of "thread mode", below). This permits the delegatee to write thread IDs to the file. (The ownership of this file can also be changed when delegating a domain subtree, but currently this serves no purpose, since, as described below, it is not possible to move a thread between domain cgroups by writing its thread ID to the cgroup.tasks file.)
mount -t cgroup2 -o remount,nsdelegate \ none /sys/fs/cgroup/unified
The effect of this mount option is to cause cgroup namespaces to automatically become delegation boundaries. More specifically, the following restrictions apply for processes inside the cgroup namespace:
- Writes to controller interface files in the root directory of the namespace will fail with the error EPERM. Processes inside the cgroup namespace can still write to delegatable files in the root directory of the cgroup namespace such as cgroup.procs and cgroup.subtree_control, and can create subhierarchy underneath the root directory.
- Attempts to migrate processes across the namespace boundary are denied (with the error ENOENT). Processes inside the cgroup namespace can still (subject to the containment rules described below) move processes between cgroups within the subhierarchy under the namespace root.
- A process in the inferior hierarchy could change the resource controller settings in the root directory of the that hierarchy. (These resource controller settings are intended to allow control to be exercised from the parent cgroup; a process inside the child cgroup should not be allowed to modify them.)
- A process inside the inferior hierarchy could move processes into and out of the inferior hierarchy if the cgroups in the superior hierarchy were somehow visible.
- The writer has write permission on the cgroup.procs file in the destination cgroup.
- The writer has write permission on the cgroup.procs file in the common ancestor of the source and destination cgroups. (In some cases, the common ancestor may be the source or destination cgroup itself.)
- If the cgroup v2 filesystem was mounted with the nsdelegate option, the writer must be able to see the source and destination cgroups from its cgroup namespace.
- Before Linux 4.11: the effective UID of the writer (i.e., the delegatee) matches the real user ID or the saved set-user-ID of the target process. (This was a historical requirement inherited from cgroups v1 that was later deemed unnecessary, since the other rules suffice for containment in cgroups v2.)
- No thread-granularity control: all of the threads of a process must be in the same cgroup.
- No internal processes: a cgroup can't both have member processes and exercise controllers on child cgroups.
- The creation of threaded subtrees in which the threads of a process may be spread across cgroups inside the tree. (A threaded subtree may contain multiple multithreaded processes.)
- The concept of threaded controllers, which can distribute resources across the cgroups in a threaded subtree.
- A relaxation of the "no internal processes rule", so that, within a threaded subtree, a cgroup can both contain member threads and exercise resource control over child cgroups.
- This is a normal v2 cgroup that provides process-granularity control. If a process is a member of this cgroup, then all threads of the process are (by definition) in the same cgroup. This is the default cgroup type, and provides the same behavior that was provided for cgroups in the initial cgroups v2 implementation.
- This cgroup is a member of a threaded subtree. Threads can be added to this cgroup, and controllers can be enabled for the cgroup.
- domain threaded
- This is a domain cgroup that serves as the root of a threaded subtree. This cgroup type is also known as "threaded root".
- domain invalid
- This is a cgroup inside a threaded subtree that is in an "invalid" state. Processes can't be added to the cgroup, and controllers can't be enabled for the cgroup. The only thing that can be done with this cgroup (other than deleting it) is to convert it to a threaded cgroup by writing the string "threaded" to the cgroup.type file.
- The rationale for the existence of this "interim" type during the creation of a threaded subtree (rather than the kernel simply immediately converting all cgroups under the threaded root to the type threaded) is to allow for possible future extensions to the thread mode model
- Threaded controllers: these controllers support thread-granularity for resource control and can be enabled inside threaded subtrees, with the result that the corresponding controller-interface files appear inside the cgroups in the threaded subtree. As at Linux 4.15, the following controllers are threaded: cpu, perf_event, and pids.
- Domain controllers: these controllers support only process granularity for resource control. From the perspective of a domain controller, all threads of a process are always in the same cgroup. Domain controllers can't be enabled inside a threaded subtree.
- We write the string "threaded" to the cgroup.type file of a cgroup y/z that currently has the type domain. This has the following effects:
- The type of the cgroup y/z becomes threaded.
- The type of the parent cgroup, y, becomes domain threaded. The parent cgroup is the root of a threaded subtree (also known as the "threaded root").
- All other cgroups under y that were not already of type threaded (because they were inside already existing threaded subtrees under the new threaded root) are converted to type domain invalid. Any subsequently created cgroups under y will also have the type domain invalid.
- We write the string "threaded" to each of the domain invalid cgroups under y, in order to convert them to the type threaded. As a consequence of this step, all threads under the threaded root now have the type threaded and the threaded subtree is now fully usable. The requirement to write "threaded" to each of these cgroups is somewhat cumbersome, but allows for possible future extensions to the thread-mode model.
- In an existing cgroup, z, that currently has the type domain, we (1) enable one or more threaded controllers and (2) make a process a member of z. (These two steps can be done in either order.) This has the following consequences:
- The type of z becomes domain threaded.
- All of the descendant cgroups of x that were not already of type threaded are converted to type domain invalid.
- As before, we make the threaded subtree usable by writing the string "threaded" to each of the domain invalid cgroups under y, in order to convert them to the type threaded.
- The writer must have write permission on the cgroup.threads file in the destination cgroup.
- The writer must have write permission on the cgroup.procs file in the common ancestor of the source and destination cgroups. (In some cases, the common ancestor may be the source or destination cgroup itself.)
- The source and destination cgroups must be in the same threaded subtree. (Outside a threaded subtree, an attempt to move a thread by writing its thread ID to the cgroup.threads file in a different domain cgroup fails with the error EOPNOTSUPP.)
- Only the string "threaded" may be written. In other words, the only explicit transition that is possible is to convert a domain cgroup to type threaded.
- The string "threaded" can be written only if the current value in cgroup.type is one of the following
- domain, to start the creation of a threaded subtree via the first of the pathways described above;
- domain invalid, to convert one of the cgroups in a threaded subtree into a usable (i.e., threaded) state;
- threaded, which has no effect (a "no-op").
- We can't write to a cgroup.type file if the parent's type is domain invalid. In other words, the cgroups of a threaded subtree must be converted to the threaded state in a top-down manner.
- There can be no member processes in the descendant cgroups of x. (The cgroup x can itself have member processes.)
- No domain controllers may be enabled in x's cgroup.subtree_control file.
- The string "threaded" is written to a child cgroup.
- A threaded controller is enabled inside the cgroup and a process is made a member of the cgroup.
- All domain invalid descendants of x that are not in lower-level threaded subtrees revert to the type domain.
- The root cgroups in any lower-level threaded subtrees revert to the type domain threaded.
- The type of that cgroup becomes threaded.
- The type of any descendants of that cgroup that are not part of lower-level threaded subtrees changes to domain invalid.
- An attempt to mount a cgroup version 1 filesystem specified neither the name= option (to mount a named hierarchy) nor a controller name (or all).
- /proc/cgroups (since Linux 2.6.24)
- This file contains information about the controllers that are compiled into the kernel. An example of the contents of this file (reformatted for readability) is the following:
#subsys_name hierarchy num_cgroups enabled cpuset 4 1 1 cpu 8 1 1 cpuacct 8 1 1 blkio 6 1 1 memory 3 1 1 devices 10 84 1 freezer 7 1 1 net_cls 9 1 1 perf_event 5 1 1 net_prio 9 1 1 hugetlb 0 1 0 pids 2 1 1
- The fields in this file are, from left to right:
- The name of the controller.
- The unique ID of the cgroup hierarchy on which this controller is mounted. If multiple cgroups v1 controllers are bound to the same hierarchy, then each will show the same hierarchy ID in this field. The value in this field will be 0 if:
- the controller is not mounted on a cgroups v1 hierarchy;
- the controller is bound to the cgroups v2 single unified hierarchy; or
- the controller is disabled (see below).
- The number of control groups in this hierarchy using this controller.
- This field contains the value 1 if this controller is enabled, or 0 if it has been disabled (via the cgroup_disable kernel command-line boot parameter).
- /proc/[pid]/cgroup (since Linux 2.6.24)
- This file describes control groups to which the process with the corresponding PID belongs. The displayed information differs for cgroups version 1 and version 2 hierarchies.
- For each cgroup hierarchy of which the process is a member, there is one entry containing three colon-separated fields:
- For example:
- The colon-separated fields are, from left to right:
- For cgroups version 1 hierarchies, this field contains a unique hierarchy ID number that can be matched to a hierarchy ID in /proc/cgroups. For the cgroups version 2 hierarchy, this field contains the value 0.
- For cgroups version 1 hierarchies, this field contains a comma-separated list of the controllers bound to the hierarchy. For the cgroups version 2 hierarchy, this field is empty.
- This field contains the pathname of the control group in the hierarchy to which the process belongs. This pathname is relative to the mount point of the hierarchy.
- /sys/kernel/cgroup/delegate (since Linux 4.15)
- This file exports a list of the cgroups v2 files (one per line) that are delegatable (i.e., whose ownership should be changed to the user ID of the delegatee). In the future, the set of delegatable files may change or grow, and this file provides a way for the kernel to inform user-space applications of which files must be delegated. As at Linux 4.15, one sees the following when inspecting this file:
$ cat /sys/kernel/cgroup/delegate cgroup.procs cgroup.subtree_control cgroup.threads
- /sys/kernel/cgroup/features (since Linux 4.15)
- Over time, the set of cgroups v2 features that are provided by the kernel may change or grow, or some features may not be enabled by default. This file provides a way for user-space applications to discover what features the running kernel supports and has enabled. Features are listed one per line:
$ cat /sys/kernel/cgroup/features nsdelegate
- The entries that can appear in this file are:
- nsdelegate (since Linux 4.15)
- The kernel supports the nsdelegate mount option.