Skip to content

Commit

Permalink
libctr/fs2: best effort collect memory stats
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Hunt <[email protected]>
  • Loading branch information
haircommander committed Sep 28, 2023
1 parent cd71d92 commit 9782b83
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions libcontainer/cgroups/fs2/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/cgroups/fs2/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down

0 comments on commit 9782b83

Please sign in to comment.