Open fafrd opened 1 year ago
Thought I'd drop this in here, looking at memory percentage usage in cgroups v2
From: cat /sys/fs/cgroup/memory.stat cat /sys/fs/cgroup/memory.max
https://github.com/opencontainers/runc/blob/main/libcontainer/cgroups/fs2/memory.go
Notes include:
// cgroup v1 usage_in_bytes
reports memory usage as the sum of
// - rss (NR_ANON_MAPPED)
// - cache (NR_FILE_PAGES)
// cgroup v1 reports SwapUsage values as mem+swap combined
// cgroup v2 reports rss and cache as anon and file.
// sum anon
+ file
to report the same value as usage_in_bytes
in v1.
// sum swap usage as combined mem+swap usage for consistency as well.
Compared to cgroupv1, cgroupv2 has not only changes in file paths but also many data metrics have changed. I found this issue: https://github.com/hashicorp/nomad/issues/16230
From what I've seen so far, if you want to check the memory usage of a Docker container, you can subtract the inactive_file from memory.stat in memory.current. This result is consistent with the MEM USAGE output from docker stats <container_id> --no-stream
.
Describe the bug I'm trying to get the total memory allocated to my docker container, from inside the container. gopsutil tries to read
/sys/fs/cgroup/memory/
, which does not exist on my system.To Reproduce
output:
BUT... when i manually look around the filesystem, I can get the memory max here:
It looks like this is a difference between cgroup and cgroup2. cgroup2 has started to get adopted in distros around 2021: https://github.com/opencontainers/runc/blob/main/docs/cgroup-v2.md. So gopsutil is going to need to support cgroups2 sooner or later
Expected behavior I would expect to see the value 268435456 returned, which represents 256 megabytes of memory allocated to the container.
Environment (please complete the following information):
ver
]/etc/os-release
and the result ofuname -a
]sw_vers
anduname -a
freebsd-version -k -r -u
anduname -a
]uname -a
]Host is Debian 11 (linux 5.10.0-20-amd64) x86_64; running docker version 23.0.0; docker guest is ubuntu:20.04.
Additional context [Cross-compiling? Paste the command you are using to cross-compile and the result of the corresponding
go env
]