Skip to content

Commit

Permalink
libctr/fs2: Initialize MaxUsage field with memory.peak value
Browse files Browse the repository at this point in the history
as well as rework the function to reduce duplication, and update the test for it

Signed-off-by: Peter Hunt <[email protected]>
  • Loading branch information
haircommander committed Sep 28, 2023
1 parent 1614cab commit c28247d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
53 changes: 35 additions & 18 deletions libcontainer/cgroups/fs2/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,43 @@ func getMemoryDataV2(path, name string) (cgroups.MemoryData, error) {
if name != "" {
moduleName = "memory." + name
}
usage := moduleName + ".current"
limit := moduleName + ".max"

value, err := fscommon.GetCgroupParamUint(path, usage)
if err != nil {
if name != "" && os.IsNotExist(err) {
// 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
files := []struct {
name string
value *uint64
}{
{
name: moduleName + ".current",
value: &memoryData.Usage,
},
{
name: moduleName + ".max",
value: &memoryData.Limit,
},
{
name: moduleName + ".peak",
value: &memoryData.MaxUsage,
},
}

for _, f := range files {
value, err := fscommon.GetCgroupParamUint(path, f.name)
if err != nil {
if os.IsNotExist(err) {
// Peak usage is only available since kernel v5.14, so collect best-effort.
if strings.HasSuffix(f.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.
if name == "swap" {
return cgroups.MemoryData{}, nil
}
}
return memoryData, err
}
return cgroups.MemoryData{}, err
}
memoryData.Usage = value

value, err = fscommon.GetCgroupParamUint(path, limit)
if err != nil {
return cgroups.MemoryData{}, err
*f.value = value
}
memoryData.Limit = value

return memoryData, nil
}
Expand Down
16 changes: 15 additions & 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 All @@ -94,6 +94,10 @@ func TestStatMemoryPodCgroup(t *testing.T) {
t.Fatal(err)
}

if err := os.WriteFile(filepath.Join(fakeCgroupDir, "memory.peak"), []byte("10000000000000"), 0o644); err != nil {
t.Fatal(err)
}

gotStats := cgroups.NewStats()

// use a fake root path to trigger the pod cgroup lookup.
Expand All @@ -107,6 +111,16 @@ func TestStatMemoryPodCgroup(t *testing.T) {
if gotStats.MemoryStats.Usage.Usage != expectedUsageBytes {
t.Errorf("parsed cgroupv2 memory.stat doesn't match expected result: \ngot %#v\nexpected %#v\n", gotStats.MemoryStats.Usage.Usage, expectedUsageBytes)
}

var expectedLimitBytes uint64 = 999999999
if gotStats.MemoryStats.Usage.Limit != expectedLimitBytes {
t.Errorf("parsed cgroupv2 memory.stat doesn't match expected result: \ngot %#v\nexpected %#v\n", gotStats.MemoryStats.Usage.Limit, expectedLimitBytes)
}

var expectedMaxUsageBytes uint64 = 10000000000000
if gotStats.MemoryStats.Usage.MaxUsage != expectedMaxUsageBytes {
t.Errorf("parsed cgroupv2 memory.stat doesn't match expected result: \ngot %#v\nexpected %#v\n", gotStats.MemoryStats.Usage.MaxUsage, expectedMaxUsageBytes)
}
}

func TestRootStatsFromMeminfo(t *testing.T) {
Expand Down

0 comments on commit c28247d

Please sign in to comment.