Score:1

Obtain statistics of CPU and memory usage from one cgroup

fk flag

Context

I have a system in which I have to check periodically usage:

  • relative (percents) for CPU
  • and absolute (GB) for memory.

How it's currently implemented

Language we're using is python.

system-wide psutil

For system-wide stats it's trivial since we can use psutil.cpu_percent() and psutil.virtual_memory().

cgroup (v1)

The problem is that sometimes the script is run inside container, and then we'd like to monitor usage of particular cgroup instead of system-wide statistics.

There's already implementation for cgroups in our system which bases on:

  • for CPU:
    • cpu.cfs_period_us
    • cpu.cfs_quota_us
    • cpuacct.usage
  • for memory:
    • memory.usage_in_bytes
    • memory.limit_in_bytes

What I need

Here I need an answer for two questions:

  • Is the present implementation based on reading values directly from files ok for cgroup?
  • If no, are there any tools which supports obtaining such kind of information easily.
  • If yes, how can I adapt this solution to cgroup2?

Why current solution doesn't work for cgroup2

The main problem I have now is lack of support for cgroup2 since current implementation is based on nested files structure.

cgroup on Ubuntu 20.04

$ cat /proc/mounts | grep cgroup
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0
cgroup2 /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,name=systemd 0 0
cgroup /sys/fs/cgroup/rdma cgroup rw,nosuid,nodev,noexec,relatime,rdma 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
cgroup /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0

cgroup2 on Ubuntu 22.04

$ cat /proc/mounts | grep cgroup
cgroup2 /sys/fs/cgroup cgroup2 rw,nosuid,nodev,noexec,relatime 0 0
asktyagi avatar
in flag
Why don't you use `/proc` filesystem? You mostly need to check correct file/folder.
Jakub Kuszneruk avatar
fk flag
@askyagi could you elaborate more on what should I look for in `/proc` to determine CPU and memory usage in my group?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.