Skip to content

Commit

Permalink
fix container RSS memory calculation with cgroup v2 stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
def committed Jun 17, 2024
1 parent e46f8d8 commit b8211bf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
6 changes: 1 addition & 5 deletions cgroup/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,13 @@ func (cg *Cgroup) memoryStatV1() (*MemoryStat, error) {
}

func (cg *Cgroup) memoryStatV2() (*MemoryStat, error) {
current, err := readUintFromFile(path.Join(cgRoot, cg.subsystems[""], "memory.current"))
if err != nil {
return nil, err
}
vars, err := readVariablesFromFile(path.Join(cgRoot, cg.subsystems[""], "memory.stat"))
if err != nil {
return nil, err
}
limit, _ := readUintFromFile(path.Join(cgRoot, cg.subsystems[""], "memory.max"))
return &MemoryStat{
RSS: current - vars["file"],
RSS: vars["anon"] + vars["file_mapped"],
Cache: vars["file"],
Limit: limit,
}, nil
Expand Down
4 changes: 2 additions & 2 deletions cgroup/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func TestCgroup_MemoryStat(t *testing.T) {
cg, _ = NewFromProcessCgroupFile(path.Join("fixtures/proc/400/cgroup"))
stat, err = cg.MemoryStat()
assert.Nil(t, err)
assert.Equal(t, uint64(48648192-1044480), stat.RSS)
assert.Equal(t, uint64(44892160+0), stat.RSS)
assert.Equal(t, uint64(1044480), stat.Cache)
assert.Equal(t, uint64(0), stat.Limit)

cg, _ = NewFromProcessCgroupFile(path.Join("fixtures/proc/500/cgroup"))
stat, err = cg.MemoryStat()
assert.Nil(t, err)
assert.Equal(t, uint64(131047424-50835456), stat.RSS)
assert.Equal(t, uint64(75247616+4038656), stat.RSS)
assert.Equal(t, uint64(50835456), stat.Cache)
assert.Equal(t, uint64(4294967296), stat.Limit)

Expand Down

0 comments on commit b8211bf

Please sign in to comment.