1

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 
2
  • Why don't you use /proc filesystem? You mostly need to check correct file/folder. Commented Nov 24, 2022 at 14:09
  • @askyagi could you elaborate more on what should I look for in /proc to determine CPU and memory usage in my group? Commented Nov 24, 2022 at 14:28

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.