Quick guide for cgroups
What are cgroups?
Summary - what is a cgroup
cgroups ...
- stand for control groups
- handle management, accounting of system resources like CPU, memory, I/O
- associates a set of tasks with a set of parameters for one or more subsystems
- on their own allow for simple job tracking
- combined with other subsystems (so-called resource controllers) allow for resource accounting / monitoring / limiting of resources
- provided through the cgroupfs pseudo filesystem
- are organized in hierarchies - hierarchies are defined by creating subdirectories in the cgroup filessystem
- each process is in exactly one node in each hierarchy (cpu hierarchy, memory hierarchy, ...)
- limit how much you can use of a system resource (quantity)
Definitions - what is a cgroup
The following is the definition of cgroups v1 from the kernel documentation: https://www.kernel.org/doc/Documentation/cgroup-v1/cgroups.txt:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
man 7 cgroups
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
https://lwn.net/Articles/679786/:
1 2 3 |
|
The relationship of containers and cgroups
Containers are basically just a bunch of cgroups plus namespace isolation (plus some extra features):
https://en.wikipedia.org/wiki/LXC:
1 2 3 |
|
https://en.wikipedia.org/wiki/Docker_(software):
1 |
|
cgroups versions
cgroup comes in 2 versions. cgroups v2 are to replace cgroups v1 eventually. However, for reasons of backwards compatibility, both will probably be around for a very long time.
cgroups v1 have several issues ...
- uncoordinated development of resource controllers
- inconsistencies between controllers
- complex hierarchy management
Solution: cgroups v2.
man 7 cgroups
:
1 2 3 4 5 6 7 8 |
|
Backwards compatibility
- cgroups v1 is unlikely to be removed
- cgroups v1 and v2 can coexist
- cgroups v2 only implemented a subset of v1's functionality
- users can use resource controllers supported in v2 and use v1 controllers for features which are unsupported in v2
man 7 cgroups
:
1 2 3 4 5 6 7 8 9 10 11 |
|
Which version of cgroups are you running?
cgroups are mounted as a virtual filesystem. Hence, verify with the mount command which version is currently in use.
Default in RHEL 7
RHEL 7 uses cgroups v1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Default in Fedora 28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Default in RHEL 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Support for cgroupv2?
In RHEL 7
No, cgroups v2 is not in the kernel in RHEL 7:
1 2 |
|
In RHEL 8
Yes:
1 2 3 4 |
|
Types of cgroups v1 resource controllers
cpu resource controller
- cpu tracking based on cgroups
cpuset resource controller
https://www.kernel.org/doc/Documentation/cgroup-v1/hugetlb.txt:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
hugetlb resource controller
- controls amount of hugepages usable by a process
- by default, a process can request as many hugepages as it wants
Looking at meminfo, we see that 4 hugepages are used:
1 2 3 4 5 6 7 8 9 |
|
One way to find out which processes are using hugepages, is to check the hugetlb cgroups:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
|
memory cgroup
- keep track of pages used by each group
- allow the OOM (out of memory) killer to trigger on a specific memory cgroup only
- kernel can "freeze" the cgroup
blockio cgroup
- keep track of I/Os for each group
- throttle each group
- writes go through page cache unless O_DIRECT is set
net_cls cgroup
- automatically set traffic classs for egress traffic (use tc/iptables)
net_prio cgroup
- automatically set traffic classs for egress traffic (use queuing disciplines)
devices cgroup
- which group can read/write from which device in /dev
freezer cgroup
- used to freeze / stop all processes in a group (SIGSTOP / SIGCONT)
mounting, unmounting and comounting cgroup v1 resource controllers
man 7 cgroups
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
man 7 cgroup
1 2 3 4 5 6 7 8 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
man 7 cgroups
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Processes in cgroups
Processes start in the same cgroups as their parent
A process can be moved by:
1 |
|
Listing the cgroups that a process is in
https://www.kernel.org/doc/Documentation/cgroup-v1/cgroups.txt:
1 2 3 |
|
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
notify_on_release
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
Resources
https://en.wikipedia.org/wiki/Cgroups
https://www.kernel.org/doc/Documentation/cgroup-v1/
https://lwn.net/Articles/679786/
man 7 cgroups