shirou / gopsutil

psutil for golang
Other
10.51k stars 1.58k forks source link

cgroup2 support? #1416

Open fafrd opened 1 year ago

fafrd commented 1 year ago

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

    hostname, _ := os.Hostname()

    cgmem, err := docker.CgroupMemDocker(hostname)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    fmt.Printf("CgroupMemStat: %+v\n", cgmem)

output:

(ins) ~/erigon-testing/erigon $ docker run --memory 256m -it image /bin/bash
root@71173d45655b:/# /usr/local/bin/erigon
open /sys/fs/cgroup/memory/system.slice/docker-71173d45655b.scope/memory.stat: no such file or directory

BUT... when i manually look around the filesystem, I can get the memory max here:

root@71173d45655b:/# cat /sys/fs/cgroup/memory.max
268435456

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):

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]

saul-data commented 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.

coderZoe commented 2 months ago

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.