9 Kernel Control Groups #
Kernel Control Groups (“cgroups”) are a kernel feature that allows assigning and limiting hardware and system resources for processes. Processes can also be organized in a hierarchical tree structure.
9.1 Overview #
Every process is assigned exactly one administrative cgroup. cgroups are ordered in a hierarchical tree structure. Resource limitations can be set for single processes or for whole branches of the hierarchy tree. Limitations for CPU, memory, disk I/O, or network bandwidth usage can be set.
On SUSE Linux Enterprise Server, systemd uses cgroups to organize all
processes in groups, which systemd calls slices. systemd also
provides an interface for setting cgroup properties.
The command systemd-cgls displays the hierarchy
tree.
This chapter is an overview. For more details, refer to the listed references.
9.2 Setting Resource Limits #
Be aware that resource consumption implicitly depends on the environment where your workload executes (e.g. size of data structures in libraries/kernel, forking behavior of utilities, computational efficiency), hence it is recommended to (re)calibrate your limits should the environment change.
Limitations for cgroups can be set with the
systemctl set-property command. The syntax is:
root #systemctl set-property [--runtime] NAME PROPERTY1=VALUE [PROPERTY2=VALUE]
Optionally, use the --runtime option. With this
option, set limits do not persist after the next reboot.
Replace NAME with a systemd service
slice, scope, socket, mount, or swap name. Replace properties with
one or more of the following:
CPUAccounting=[yes|no]Turns on CPU usage accounting. This property takes
yesandnoas arguments.Example:
root #systemctl set-property user.slice CPUAccounting=yesCPUQuota=PERCENTAGEAssigns a CPU time to processes. The value is a percentage followed by a
%as suffix. This requiresCPUAccounting=yes.Example:
root #systemctl set-property user.slice CPUQuota=50%MemoryAccounting=[yes|no]Turns on memory usage accounting. This property takes
yesandnoas arguments.Example:
root #systemctl set-property user.slice MemoryAccounting=yesMemoryLow=BYTESUnused memory from processes below this limit will not be reclaimed for other use. Use suffixes K, M, G or T for BYTES. This requires
MemoryAccounting=yes.Example:
root #systemctl set-property nginx.service MemoryLow=512MMemoryHigh=BYTESIf more memory above this limit is used, memory is aggressively taken away from the processes. Use suffixes K, M, G or T for BYTES. This requires
MemoryAccounting=yes.Example:
root #systemctl set-property nginx.service MemoryHigh=2GMemoryMax=BYTESSets a maximum limit for used memory. Processes will be killed if they use more memory than allowed. Use suffixes K, M, G or T for BYTES. This requires
MemoryAccounting=yes.Example:
root #systemctl set-property nginx.service MemoryMax=4GDeviceAllow=Allows read (
r), write (w) and mknod (m) access. The command takes a device node specifier and a list ofr,worm, separated by a white space.Example:
root #systemctl set-property system.slice DeviceAllow="/dev/sdb1 r"DevicePolicy=[auto|closed|strict]When set to
strict, only access to devices that are listed inDeviceAllowis allowed.closedadditionally allows access to standard pseudo devices including/dev/null,/dev/zero,/dev/full,/dev/random, and/dev/urandom.autoallows access to all devices if no specific rule is defined inDeviceAllow.autois the default setting.
For more details and a complete list of properties, see man
systemd.resource-control.
9.3 For More Information #
Kernel documentation (package
kernel-source): files in/usr/src/linux/Documentation/cgroup-v1and file/usr/src/linux/Documentation/cgroup-v2.txt.https://lwn.net/Articles/604609/—Brown, Neil: Control Groups Series (2014, 7 parts).
https://lwn.net/Articles/243795/—Corbet, Jonathan: Controlling memory use in containers (2007).
https://lwn.net/Articles/236038/—Corbet, Jonathan: Process containers (2007).