From 9782b83e2ca70667fd3c9464ab4ca64bc8502143 Mon Sep 17 00:00:00 2001 From: Peter Hunt Date: Wed, 27 Sep 2023 10:31:08 -0400 Subject: [PATCH] libctr/fs2: best effort collect memory stats Signed-off-by: Peter Hunt --- libcontainer/cgroups/fs2/memory.go | 13 ++++++++++--- libcontainer/cgroups/fs2/memory_test.go | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/libcontainer/cgroups/fs2/memory.go b/libcontainer/cgroups/fs2/memory.go index f2a670e4130..222e1acddaa 100644 --- a/libcontainer/cgroups/fs2/memory.go +++ b/libcontainer/cgroups/fs2/memory.go @@ -154,16 +154,23 @@ func getMemoryDataV2(path, name string) (cgroups.MemoryData, error) { }, } + var lastErr error for _, f := range files { value, err := fscommon.GetCgroupParamUint(path, f.name) if err != nil { - if name != "" && os.IsNotExist(err) { + if os.IsNotExist(err) { + // Peak usage is only available since kernel v5.14, so collect best-effort. + if strings.HasSuffix(v.name, "peak") { + continue + } // Ignore EEXIST as there's no swap accounting // if kernel CONFIG_MEMCG_SWAP is not set or // swapaccount=0 kernel boot parameter is given. - return cgroups.MemoryData{}, nil + if name == "swap" { + return cgroups.MemoryData{}, nil + } } - return cgroups.MemoryData{}, err + return memoryData, err } *f.value = value } diff --git a/libcontainer/cgroups/fs2/memory_test.go b/libcontainer/cgroups/fs2/memory_test.go index 460efbe5cb0..fd71e1b58dc 100644 --- a/libcontainer/cgroups/fs2/memory_test.go +++ b/libcontainer/cgroups/fs2/memory_test.go @@ -71,7 +71,7 @@ func TestStatMemoryPodCgroupNotFound(t *testing.T) { t.Errorf("expected error when statting memory for cgroupv2 root, but was nil") } - if !strings.Contains(err.Error(), "memory.current: no such file or directory") { + if !strings.Contains(err.Error(), "memory.peak: no such file or directory") { t.Errorf("expected error to contain 'memory.current: no such file or directory', but was %s", err.Error()) } }