1 .\" Copyright (C) 2015 Serge Hallyn <serge@hallyn.com>
2 .\" and Copyright (C) 2016 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH CGROUPS 7 2016-07-17 "Linux" "Linux Programmer's Manual"
28 cgroups \- Linux control groups
30 Control cgroups, usually referred to as cgroups,
31 are a Linux kernel feature which allow processes to
32 be organized into hierarchical groups whose usage of
33 various types of resources can then be limited and monitored.
34 The kernel's cgroup interface is provided through
35 a pseudo-filesystem called cgroupfs.
36 Grouping is implemented in the core cgroup kernel code,
37 while resource tracking and limits are implemented in
38 a set of per-resource-type subsystems (memory, CPU, and so on).
43 is a collection of processes that are bound to a set of
44 limits or parameters defined via the cgroup filesystem.
48 is a kernel component that modifies the behavior of
49 the processes in a cgroup.
50 Various subsystems have been implemented, making it possible to do things
51 such as limiting the amount of CPU time and memory available to a cgroup,
52 accounting for the CPU time used by a cgroup,
53 and freezing and resuming execution of the processes in a cgroup.
54 Subsystems are sometimes also known as
55 .IR "resource controllers"
56 (or simply, controllers).
58 The cgroups for a controller are arranged in a
60 This hierarchy is defined by creating, removing, and
61 renaming subdirectories within the cgroup filesystem.
62 At each level of the hierarchy, attributes (e.g., limits) can be defined.
63 The limits, control, and accounting provided by cgroups generally have
64 effect throughout the subhierarchy underneath the cgroup where the
65 attributes are defined.
66 Thus, for example, the limits placed on
67 a cgroup at a higher level in the hierarchy cannot be exceeded
68 by descendant cgroups.
70 .SS Cgroups version 1 and version 2
71 The initial release of the cgroups implementation was in Linux 2.6.24.
72 Over time, various cgroup controllers have been added
73 to allow the management of various types of resources.
74 However, the development of these controllers was largely uncoordinated,
75 with the result that many inconsistencies arose between controllers
76 and management of the cgroup hierarchies became rather complex.
77 (A longer description of these problems can be found in
78 the kernel source file
79 .IR Documentation/cgroup\-v2.txt .)
81 Because of the problems with the initial cgroups implementation
83 starting in Linux 3.10, work began on a new,
84 orthogonal implementation to remedy these problems.
85 Initially marked experimental, and hidden behind the
86 .I "\-o\ __DEVEL__sane_behavior"
87 mount option, the new version (cgroups version 2)
88 was eventually made official with the release of Linux 4.5.
89 Differences between the two versions are described in the text below.
91 Although cgroups v2 is intended as a replacement for cgroups v1,
92 the older system continues to exist
93 (and for compatibility reasons is unlikely to be removed).
94 Currently, cgroups v2 implements only a subset of the controllers
95 available in cgroups v1.
96 The two systems are implemented so that both v1 controllers and
97 v2 controllers can be mounted on the same system.
98 Thus, for example, it is possible to use those controllers
99 that are supported under version 2,
100 while also using version 1 controllers
101 where version 2 does not yet support those controllers.
102 The only restriction here is that a controller can't be simultaneously
103 employed in both a cgroups v1 hierarchy and in the cgroups v2 hierarchy.
105 .SS Cgroups version 1
106 Under cgroups v1, each controller may be mounted against a separate
107 cgroup filesystem that provides its own hierarchical organization of the
108 processes on the system.
109 It is also possible comount multiple (or even all) cgroups v1 controllers
110 against the same cgroup filesystem, meaning that the comounted controllers
111 manage the same hierarchical organization of processes.
113 For each mounted hierarchy,
114 the directory tree mirrors the control group hierarchy.
115 Each control group is represented by a directory, with each of its child
116 control cgroups represented as a child directory.
118 .IR /user/joe/1.session
119 represents control group
121 which is a child of cgroup
125 Under each cgroup directory is a set of files which can be read or
126 written to, reflecting resource limits and a few general cgroup
129 In addition, in cgroups v1,
130 cgroups can be mounted with no bound controller, in which case
131 they serve only to track processes.
132 (See the discussion of release notification below.)
133 An example of this is the
135 cgroup which is used by
137 to track services and user sessions.
139 .SS Tasks (threads) versus processes
140 In cgroups v1, a distinction is drawn between
144 In this view, a process can consist of multiple tasks
145 (more commonly called threads, from a user-space perspective,
146 and called such in the remainder of this man page).
147 In cgroups v1, it is possible to independently manipulate
148 the cgroup memberships of the threads in a process.
149 Because this ability caused certain problems,
150 .\" FIXME Add some text describing why this was a problem.
151 the ability to independently manipulate the cgroup memberships
152 of the threads in a process has been removed in cgroups v2.
153 Cgroups v2 allows manipulation of cgroup membership only for processes
154 (which has the effect of changing the cgroup membership of
155 all threads in the process).
157 .SS Mounting v1 controllers
158 The use of cgroups requires a kernel built with the
159 .BR CONFIG_CGROUP\option.
160 In addition, each of the v1 controllers has an associated
161 configuration option that must be set in order to employ that controller.
163 In order to use a v1 controller,
164 it must be mounted against a cgroup filesystem.
165 The usual place for such mounts is under a
167 filesystem mounted at
169 Thus, one might mount the
171 controller as follows:
175 mount \-t cgroup \-o cpu none /sys/fs/cgroup/cpu
179 It is possible to comount multiple controllers against the same hierarchy.
180 For example, here the
184 controllers are comounted against a single hierarchy:
188 mount \-t cgroup \-o cpu,cpuacct none /sys/fs/cgroup/cpu,cpuacct
192 Comounting controllers has the effect that a process is in the same cgroup for
193 all of the comounted controllers.
194 Separately mounting controllers allows a process to
197 for one controller while being in
201 It is possible to comount all v1 controllers against the same hierarchy:
205 mount \-t cgroup \-o all cgroup /sys/fs/cgroup
209 (One can achieve the same result by omitting
211 since it is the default if no controllers are explicitly specified.)
213 It is not possible to mount the same controller
214 against multiple cgroup hierarchies.
215 For example, it is not possible to mount both the
219 controllers against one hierarchy, and to mount the
221 controller alone against another hierarchy.
222 It is possible to create multiple mount points with exactly
223 the same set of comounted controllers.
224 However, in this case all that results is multiple mount points
225 providing a view of the same hierarchy.
227 Note that on many systems, the v1 controllers are automatically mounted under
231 automatically creates such mount points.
233 .SS Cgroups version 1 controllers
234 Each of the cgroups version 1 controllers is governed
235 by a kernel configuration option (listed below).
236 Additionally, the availability of the cgroups feature is governed by the
238 kernel configuration option.
240 .IR cpu " (since Linux 2.6.24; " \fBCONFIG_CGROUP_SCHED\fP )
241 Cgroups can be guaranteed a minimum number of "CPU shares"
242 when a system is busy.
243 This does not limit a cgroup's CPU usage if the CPUs are not busy.
244 For further information, see
245 .IR Documentation/scheduler/sched-design-CFS.txt .
248 this controller was extended to provide CPU "bandwidth" control.
249 If the kernel is configured with
250 .BR COONFIG_CFS_BANDWIDTH ,
251 then within each scheduling period
252 (defined via a file in the cgroup directory), it is possible to define
253 an upper limit on the CPU time allocated to the processes in a cgroup.
254 This upper limit applies even if there is no other competition for the CPU.
255 Further information can be found in the kernel source file
256 .IR Documentation/scheduler/sched\-bwc.txt .
258 .IR cpuacct " (since Linux 2.6.24; " \fBCONFIG_CGROUP_CPUACCT\fP )
259 This provides accounting for CPU usage by groups of processes.
261 Further information can be found in the kernel source file
262 .IR Documentation/cgroup\-v1/cpuacct.txt .
264 .IR cpuset " (since Linux 2.6.24; " \fBCONFIG_CPUSETS\fP )
265 This cgroup can be used to bind the processes in a cgroup to
266 a specified set of CPUs and NUMA nodes.
268 Further information can be found in the kernel source file
269 .IR Documentation/cgroup\-v1/cpusets.txt .
271 .IR memory " (since Linux 2.6.25; " \fBCONFIG_MEMCG\fP )
272 The memory controller supports reporting and limiting of process memory, kernel
273 memory, and swap used by cgroups.
275 Further information can be found in the kernel source file
276 .IR Documentation/cgroup\-v1/memory.txt .
278 .IR devices " (since Linux 2.6.26; " \fBCONFIG_CGROUP_DEVICE\fP )
279 This supports controlling which processes may create (mknod) devices as
280 well as open them for reading or writing.
281 The policies may be specified as whitelists and blacklists.
282 Hierarchy is enforced, so new rules must not
283 violate existing rules for the target or ancestor cgroups.
285 Further information can be found in the kernel source file
286 .IR Documentation/cgroup-v1/devices.txt .
288 .IR freezer " (since Linux 2.6.28; " \fBCONFIG_CGROUP_FREEZER\fP )
291 cgroup can suspend and restore (resume) all processes in a cgroup.
294 also causes its children, for example, processes in
298 Further information can be found in the kernel source file
299 .IR Documentation/cgroup-v1/freezer-subsystem.txt .
301 .IR net_cls " (since Linux 2.6.29; " \fBCONFIG_CGROUP_NET_CLASSID\fP )
302 This places a classid, specified for the cgroup, on network packets
304 These classids can then be used in firewall rules,
305 as well as used to shape traffic using
307 This applies only to packets
308 leaving the cgroup, not to traffic arriving at the cgroup.
310 Further information can be found in the kernel source file
311 .IR Documentation/cgroup-v1/net_cls.txt .
313 .IR blkio " (since Linux 2.6.33; " \fBCONFIG_BLK_CGROUP\fP )
316 cgroup controls and limits access to specified block devices by
317 applying IO control in the form of throttling and upper limits against leaf
318 nodes and intermediate nodes in the storage hierarchy.
320 Two policies are available.
321 The first is a proportional-weight time-based division
322 of disk implemented with CFQ.
323 This is in effect for leaf nodes using CFQ.
324 The second is a throttling policy which specifies
325 upper I/O rate limits on a device.
327 Further information can be found in the kernel source file
328 .IR Documentation/cgroup-v1/blkio-controller.txt .
330 .IR perf_event " (since Linux 2.6.39; " \fBCONFIG_CGROUP_PERF\fP )
331 This controller allows
333 monitoring of the set of processes grouped in a cgroup.
335 Further information can be found in the kernel source file
336 .IR tools/perf/Documentation/perf-record.txt .
338 .IR net_prio " (since Linux 3.3; " \fBCONFIG_CGROUP_NET_PRIO\fP )
339 This allows priorities to be specified, per network interface, for cgroups.
341 Further information can be found in the kernel source file
342 .IR Documentation/cgroup-v1/net_prio.txt .
344 .IR hugetlb " (since Linux 3.5; " \fBCONFIG_CGROUP_HUGETLB\fP )
345 This supports limiting the use of huge pages by cgroups.
347 Further information can be found in the kernel source file
348 .IR Documentation/cgroup-v1/hugetlb.txt .
350 .IR pids " (since Linux 4.3; " \fBCONFIG_CGROUP_PIDS\fP )
351 This controller permits limiting the number of process that may be created
352 in a cgroup (and its descendants).
354 Further information can be found in the kernel source file
355 .IR Documentation/cgroup-v1/pids.txt .
357 .SS Creating cgroups and moving processes
358 A cgroup filesystem initially contains a single root cgroup, '/',
359 which all processes belong to.
360 A new cgroup is created by creating a directory in the cgroup filesystem:
362 mkdir /sys/fs/cgroup/cpu/cg1
364 This creates a new empty cgroup.
366 A process may be moved to this cgroup by writing its PID into the cgroup's
370 echo $$ > /sys/fs/cgroup/cpu/cg1/cgroup.procs
372 Only one PID at a time should be written to this file.
374 Writing the value 0 to a
376 file causes the writing process to be moved to the corresponding cgroup.
378 When writing a PID into the
380 all threads in the process are moved into the new cgroup at once.
382 Within a hierarchy, a process can be a member of exactly one cgroup.
383 Writing a process's PID to a
385 file automatically removes it from the cgroup of
386 which it was previously a member.
390 file can be read to obtain a list of the processes that are
392 The returned list of PIDs is not guaranteed to be in order.
393 Nor is it guaranteed to be free of duplicates.
394 (For example, a PID may be recycled while reading from the list.)
396 In cgroups v1 (but not cgroups v2), an individual thread can be moved to
397 another cgroup by writing its thread ID
398 (i.e., the kernel thread ID returned by
404 file in a cgroup directory.
405 This file can be read to discover the set of threads
406 that are members of the cgroup.
407 This file is not present in cgroup v2 directories.
411 it must first have no child cgroups and contain no (nonzombie) processes.
412 So long as that is the case, one can simply
413 remove the corresponding directory pathname.
414 Note that files in a cgroup directory cannot and need not be
417 .SS Cgroups v1 release notification
418 Two files can be used to determine whether the kernel provides
419 notifications when a cgroup becomes empty.
420 A cgroup is considered to be empty when it contains no child
421 cgroups and no member processes.
423 A special file in the root directory of each cgroup hierarchy,
425 can be used to register the pathname of a program that may be invoked when
426 a cgroup in the hierarchy becomes empty.
427 The pathname of the newly empty cgroup (relative to the cgroup mount point)
428 is provided as the sole command-line argument when the
433 program might remove the cgroup directory,
434 or perhaps repopulate with a process.
436 The default value of the
438 file is empty, meaning that no release agent is invoked.
442 program is invoked when a particular cgroup becomes empty is determined
444 .IR notify_on_release
445 file in the corresponding cgroup directory.
446 If this file contains the value 0, then the
448 program is not invoked.
449 If it contains the value 1, the
452 The default value for this file in the root cgroup is 0.
453 At the time when a new cgroup is created,
454 the value in this file is inherited from the corresponding file
455 in the parent cgroup.
457 .SS Cgroups version 2
459 all mounted controllers reside in a single unified hierarchy.
460 While (different) controllers may be simultaneously
461 mounted under the v1 and v2 hierarchies,
462 it is not possible to mount the same controller simultaneously
463 under both the v1 and the v2 hierarchies.
465 The new behaviors in cgroups v2 are summarized here,
466 and in some cases elaborated in the following subsections.
468 Cgroups v2 provides a unified hierarchy against
469 which all controllers are mounted.
471 "Internal" processes are not permitted.
472 With the exception of the root cgroup, processes may reside
473 only in leaf nodes (cgroups that do not themselves contain child cgroups).
475 Active cgroups must be specified via the files
476 .IR cgroup.controllers
478 .IR cgroup.subtree_control .
482 file has been removed.
484 .I cgroup.clone_children
485 file that is employed by the
487 controller has been removed.
489 An improved mechanism for notification of empty cgroups is provided by the
493 For more changes, see the
494 .I Documentation/cgroup-v2.txt
495 file in the kernel source.
497 .SS Cgroups v2 unified hierarchy
498 In cgroups v1, the ability to mount different controllers
499 against different hierarchies was intended to allow great flexibility
500 for application design.
501 In practice, though, the flexibility turned out to less useful than expected,
502 and in many cases added complexity.
503 Therefore, in cgroups v2,
504 all available controllers are mounted against a single hierarchy.
505 The available controllers are automatically mounted,
506 meaning that it is not necessary (or possible) to specify the controllers
507 when mounting the cgroup v2 filesystem using a command such as the following:
509 mount -t cgroup2 none /mnt/cgroup2
511 A cgroup v2 controller is available only if it is not currently in use
512 via a mount against a cgroup v1 hierarchy.
513 Or, to put things another way, it is not possible to employ
514 the same controller against both a v1 hierarchy and the unified v2 hierarchy.
516 .SS Cgroups v2 """no internal processes""" rule
517 With the exception of the root cgroup, processes may reside
518 only in leaf nodes (cgroups that do not themselves contain child cgroups).
519 This avoids the need to decide how to partition resources between
520 processes which are members of cgroup A and processes in child cgroups of A.
522 For instance, if cgroup
524 exists, then a process may reside in
528 This is to avoid an ambiguity in cgroups v1
529 with respect to the delegation of resources between processes in
531 and its child cgroups.
532 The recommended approach in cgroups v2 is to create a subdirectory called
534 for any nonleaf cgroup which should contain processes, but no child cgroups.
535 Thus, processes which previously would have gone into
539 This has the advantage of making explicit
540 the relationship between processes in
546 .SS Cgroups v2 subtree control
550 .IR cgroup.controllers
551 file contains the list of controllers which were active in its parent, A.
552 This is the list of controllers which are available to this cgroup.
553 No controllers are active until they are enabled through the
554 .IR cgroup.subtree_control
555 file, by writing the list of space-delimited names of the controllers,
556 each preceded by '+' (to enable) or '\-' (to disable).
559 controller is not enabled in
561 then it cannot be enabled in
564 .SS Cgroups v2 cgroup.events file
565 With cgroups v2, a new mechanism is provided to obtain notification
566 about when a cgroup becomes empty.
570 .IR notify_on_release
571 files are removed, and replaced by a new, more general-purpose file,
573 This file contains key-value pairs
574 (delimited by newline characters, with the key and value separated by spaces)
575 that identify events or state for a cgroup.
576 Currently, only one key appears in this file,
578 which has either the value 0,
579 meaning that the cgroup (and its descendants)
580 contain no (nonzombie) processes,
581 or 1, meaning that the cgroup contains member processes.
585 file can be monitored, in order to receive notification when a cgroup
586 transitions between the populated and unpopulated states (or vice versa).
587 When monitoring this file using
591 events, and when monitoring the file using
598 .IR notify_on_release
599 mechanism offers at least two advantages over the cgroups v1
602 First, it allows for cheaper notification,
603 since a single process can monitor multiple
606 By contrast, the cgroups v1 mechanism requires the creation
607 of a process for each notification.
608 Second, notification can be delegated to a process that lives inside
609 a container associated with the newly empty cgroup.
613 .IR /proc/cgroups " (since Linux 2.6.24)"
614 This file contains information about the controllers
615 that are compiled into the kernel.
616 An example of the contents of this file (reformatted for readability)
621 #subsys_name hierarchy num_cgroups enabled
637 The fields in this file are, from left to right:
640 The name of the controller.
642 The unique ID of the cgroup hierarchy on which this controller is mounted.
643 If multiple cgroups v1 controllers are bound to the same hierarchy,
644 then each will show the same hierarchy ID in this field.
645 The value in this field will be 0 if:
648 the controller is not mounted on a cgroups v1 hierarchy;
650 the controller is bound to the cgroups v2 single unified hierarchy; or
652 the controller is disabled (see below).
655 The number of control groups in this hierarchy using this controller.
657 This field contains the value 1 if this controller is enabled,
658 or 0 if it has been disabled (via the
660 kernel command-line boot parameter).
663 .IR /proc/[pid]/cgroup " (since Linux 2.6.24)"
664 This file describes control groups to which the process
665 with the corresponding PID belongs.
666 The displayed information differs for
667 cgroups version 1 and version 2 hierarchies.
669 For each cgroup hierarchy of which the process is a member,
670 there is one entry containing three
671 colon-separated fields of the form:
673 hierarchy-ID:controller-list:cgroup-path
679 5:cpuacct,cpu,cpuset:/daemons
683 The colon-separated fields are, from left to right:
686 For cgroups version 1 hierarchies,
687 this field contains a unique hierarchy ID number
688 that can be matched to a hierarchy ID in
690 For the cgroups version 2 hierarchy, this field contains the value 0.
692 For cgroups version 1 hierarchies,
693 this field contains a comma-separated list of the controllers
694 bound to the hierarchy.
695 For the cgroups version 2 hierarchy, this field is empty.
697 This field contains the pathname of the control group in the hierarchy
698 to which the process belongs.
699 This pathname is relative to the mount point of the hierarchy.
702 The following errors can occur for
706 An attempt to mount a cgroup version 1 filesystem specified neither the
708 option (to mount a named hierarchy) nor a controller name (or
711 A child process created via
713 inherits its parent's cgroup memberships.
714 A process's cgroup memberships are preserved across
721 .BR perf_event_open (2),
723 .BR cgroup_namespaces (7),
727 .BR user_namespaces (7)