1 # Control Groups {#sec-cgroups}
3 To keep track of the processes in a running system, systemd uses
4 *control groups* (cgroups). A control group is a set of processes used
5 to allocate resources such as CPU, memory or I/O bandwidth. There can be
6 multiple control group hierarchies, allowing each kind of resource to be
9 The command `systemd-cgls` lists all control groups in the `systemd`
10 hierarchy, which is what systemd uses to keep track of the processes
11 belonging to each service or user session:
19 │ ├─ 2682 kdeinit4: kdeinit4 Running...
21 │ └─10851 sh -c less -R
24 │ ├─2444 httpd -f /nix/store/3pyacby5cpr55a03qwbnndizpciwq161-httpd.conf -DNO_DETACH
27 │ └─2376 dhcpcd --config /nix/store/f8dif8dsi2yaa70n03xir8r653776ka6-dhcpcd.conf
31 Similarly, `systemd-cgls cpu` shows the cgroups in the CPU hierarchy,
32 which allows per-cgroup CPU scheduling priorities. By default, every
33 systemd service gets its own CPU cgroup, while all user sessions are in
34 the top-level CPU cgroup. This ensures, for instance, that a thousand
35 run-away processes in the `httpd.service` cgroup cannot starve the CPU
36 for one process in the `postgresql.service` cgroup. (By contrast, it
37 they were in the same cgroup, then the PostgreSQL process would get
38 1/1001 of the cgroup's CPU time.) You can limit a service's CPU share in
42 systemd.services.httpd.serviceConfig.CPUShares = 512;
45 By default, every cgroup has 1024 CPU shares, so this will halve the CPU
46 allocation of the `httpd.service` cgroup.
48 There also is a `memory` hierarchy that controls memory allocation
49 limits; by default, all processes are in the top-level cgroup, so any
50 service or session can exhaust all available memory. Per-cgroup memory
51 limits can be specified in `configuration.nix`; for instance, to limit
52 `httpd.service` to 512 MiB of RAM (excluding swap):
55 systemd.services.httpd.serviceConfig.MemoryLimit = "512M";
58 The command `systemd-cgtop` shows a continuously updated list of all
59 cgroups with their CPU and memory usage.