Skip to content
systemcoding.dev
Virtualization

Inspect a Process's cgroups With /proc/PID/cgroup and systemd-cgls

Read /proc/PID/cgroup to see which cgroup a process is in, then use systemd-cgls and systemd-cgtop to walk the whole tree and find a container's group.

6 min read
Summarize in AI
Flat illustration of a magnifier over lines of a log, with the QEMU, Linux and C++ logos.

Before you set a limit on a process, you need to know which cgroup it already lives in. Linux answers that in one file, and systemd draws the whole tree for you. This post reads /proc/PID/cgroup on cgroup v2, translates a path like /user.slice/user-1000.slice/session-3.scope into plain English, then uses systemd-cgls, systemd-cgtop and the cgroup files themselves to see what a group contains and what it is allowed to use. It assumes Linux 6.12 with systemd 256 on the unified hierarchy.

The one-line answer in /proc

Every process has a file, /proc/PID/cgroup, that lists the cgroups it belongs to. self stands for the process reading it.

Shell
cat /proc/self/cgroup
Text
0::/user.slice/user-1000.slice/session-3.scope

The format is hierarchy-ID:controller-list:cgroup-path. On cgroup v2 there is one hierarchy, so the ID is always 0 and the controller list is empty, which is why the line starts with 0::. The path is relative to the cgroup mount, so this shell lives in /sys/fs/cgroup/user.slice/user-1000.slice/session-3.scope. On a v1 or hybrid system you would see one line per controller, with names like memory or cpu,cpuacct in the middle field.

Read the path from the left. user.slice is the slice systemd uses for all user sessions. user-1000.slice is the slice for UID 1000. session-3.scope is the scope unit for login session 3, which holds the processes of that session. Slices are containers for other units; scopes hold processes that systemd did not start itself but is tracking, such as login sessions; services hold processes systemd launched.

Look at any process, not just yourself

Substitute a PID. This finds the cgroup of the SSH daemon.

Shell
cat /proc/$(pidof -s sshd)/cgroup
Text
0::/system.slice/ssh.service

You can read other processes' cgroup files without privileges on a default system, because /proc/PID/cgroup is world readable. That makes it the right first move when you are debugging a container: take the PID of its main process from the host and read its cgroup path.

Shell
docker inspect --format '{{.State.Pid}}' web
cat /proc/48213/cgroup
Text
48213
0::/system.slice/docker-7c3a9f0e1b6d....scope

With the systemd cgroup driver, Docker creates one .scope unit per container, named after the container ID. The ID is truncated here for readability, and your PID and ID will differ. With the cgroupfs driver the path is /docker/<id> instead. The namespace matters too: inside a container with its own cgroup namespace, the same command prints 0::/, because the container sees its own group as the root.

Draw the tree with systemd-cgls

systemd-cgls prints the hierarchy as a tree, with processes under the units that own them.

Shell
systemd-cgls --no-pager /user.slice
Text
Control group /user.slice:
└─user-1000.slice
  ├─[email protected]
  │ ├─session.slice
  │ │ └─dbus.service
  │ │   └─1450 /usr/bin/dbus-broker-launch --scope user
  │ └─init.scope
  │   ├─1393 /usr/lib/systemd/systemd --user
  └─session-3.scope
    ├─1722 sshd: user [priv]
    ├─1745 sshd: user@pts/0
    ├─1746 -bash
    └─2101 systemd-cgls --no-pager /user.slice

You can see the shell, -bash, and the systemd-cgls command itself, both in session-3.scope, and the user's systemd instance and its services beside it. Run it with no arguments to see the whole machine, or pass a unit name such as systemd-cgls -u ssh.service. The exact PIDs and the units listed on your machine will differ.

Without systemd the same walk works with plain tools: find /sys/fs/cgroup -name cgroup.procs lists every group's process file, and cat on any of them prints PIDs.

Check what a group contains and what it may use

A cgroup directory holds the answers. These commands use the session scope from above.

Shell
cd /sys/fs/cgroup/user.slice/user-1000.slice/session-3.scope
cat cgroup.controllers
cat cgroup.procs
cat memory.current
cat memory.max
Text
memory pids
1722
1745
1746
2205
14884864
max

cgroup.controllers lists the controllers this group has available, cgroup.procs lists the PIDs inside, memory.current is the group's memory use in bytes, and memory.max is its limit, where max means unlimited. The last PID in cgroup.procs is the cat itself, so it changes on every run. A group's memory.current includes page cache charged to it, which is why it is larger than the sum of the processes' resident sizes.

Two other files help when you are confused about a limit. memory.events counts how often the group hit its limit, and cgroup.type says whether it is a normal domain group or a threaded one. For CPU, cpu.stat shows usage and throttling.

Watch it live with systemd-cgtop

systemd-cgtop is top for cgroups. It sorts groups by CPU, memory or I/O.

Shell
systemd-cgtop --batch --iterations=1 --depth=2
Text
CGroup                                  Tasks   %CPU   Memory  Input/s Output/s
/                                         351   12.4     3.1G        -        -
/system.slice                             118    3.2     1.4G        -        -
/user.slice                                28    8.9     1.2G        -        -
/user.slice/user-1000.slice                28    8.9     1.2G        -        -

The numbers vary with load. Memory accounting needs the controller enabled; if a column shows a dash, the corresponding controller is not turned on for that group, and systemd enables it when a unit sets a matching option such as MemoryAccounting=yes or a MemoryMax limit.

Finding which group is using too much

A practical sequence for a slow machine: run systemd-cgtop -m to sort by memory, note the busiest group, then systemd-cgls -u <unit> to list its processes, then cat /proc/PID/cgroup on a suspect process to confirm the mapping. If the group is a container scope, the unit name leads you back to the container ID. The route works the same way for a process you did not start, which is the difference between this approach and reading the service's configuration.

When /proc/PID/cgroup surprises you

A process can be in a different cgroup than its parent. A container runtime moves its init process into its own group after forking. A systemd service that sets Delegate=yes lets the service create child groups and move its own processes among them. Threads can differ from their process in a threaded group. In each case the file is the truth, and the configuration is only an intention. If the path ends in (deleted), the group has been removed while the process was still running, which happens when a unit is stopped and the process escaped.

A worked investigation

Suppose a build server feels slow and top shows a process called cc1plus using most of the memory. Start with its PID, then read its cgroup, then read that group's files.

Shell
pid=$(pgrep -o cc1plus)
cat /proc/$pid/cgroup
cat /sys/fs/cgroup$(cut -d: -f3 /proc/$pid/cgroup)/memory.max
Text
0::/system.slice/ci-runner.service
max

The compiler belongs to the ci-runner.service unit and nothing limits its memory, which explains how it can starve the machine. The cure is a unit setting, not a signal: add MemoryMax=8G to the service with systemctl edit ci-runner.service, and systemd writes the value into the group's memory.max. The cut -d: -f3 trick extracts the path field from the file and glues it onto the mount point, which works for any process on a v2 system and is the shortest way from a PID to its limits. On a machine that runs containers under a different manager, the path will look different, but the same two commands apply.

Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.