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
43 systemd.services.httpd.serviceConfig.CPUShares = 512;
47 By default, every cgroup has 1024 CPU shares, so this will halve the CPU
48 allocation of the `httpd.service` cgroup.
50 There also is a `memory` hierarchy that controls memory allocation
51 limits; by default, all processes are in the top-level cgroup, so any
52 service or session can exhaust all available memory. Per-cgroup memory
53 limits can be specified in `configuration.nix`; for instance, to limit
54 `httpd.service` to 512 MiB of RAM (excluding swap):
58 systemd.services.httpd.serviceConfig.MemoryLimit = "512M";
62 The command `systemd-cgtop` shows a continuously updated list of all
63 cgroups with their CPU and memory usage.