Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v0.51] cgroups: fix memory usage on cgroup v1 #2157

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion pkg/cgroups/memory_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
package cgroups

import (
"fmt"
"path/filepath"
"strconv"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fs"
Expand Down Expand Up @@ -63,9 +65,26 @@ func (c *linuxMemHandler) Stat(ctr *CgroupControl, m *cgroups.Stats) error {
} else {
memoryRoot = ctr.getCgroupv1Path(Memory)
limitFilename = "memory.limit_in_bytes"
if memUsage.Usage.Usage, err = readFileAsUint64(filepath.Join(memoryRoot, "memory.usage_in_bytes")); err != nil {

path := filepath.Join(memoryRoot, "memory.stat")
values, err := readCgroup2MapPath(path)
if err != nil {
return err
}

// cgroup v1 does not have a single "anon" field, but we can calculate it
// from total_active_anon and total_inactive_anon
memUsage.Usage.Usage = 0
for _, key := range []string{"total_active_anon", "total_inactive_anon"} {
if _, found := values[key]; !found {
continue
}
res, err := strconv.ParseUint(values[key][0], 10, 64)
if err != nil {
return fmt.Errorf("parse %s from %s: %w", key, path, err)
}
memUsage.Usage.Usage += res
}
}

memUsage.Usage.Limit, err = readFileAsUint64(filepath.Join(memoryRoot, limitFilename))
Expand Down