#1416·gopsutil

cgroup2 support?

Author: fafrdCreated Feb 10, 2023Updated Apr 10, 2026

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

go
    hostname, _ := os.Hostname()

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

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

output:

bash
(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:

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

  • Windows: [paste the result of ver]
  • Linux: [paste contents of /etc/os-release and the result of uname -a]
  • Mac OS: [paste the result of sw_vers and uname -a
  • FreeBSD: [paste the result of freebsd-version -k -r -u and uname -a]
  • OpenBSD: [paste the result of 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]