Skip to content

Commit

Permalink
calculateMemUsageUnixNoCache: subtract total_inactive_file, not cache
Browse files Browse the repository at this point in the history
The new stat definition corresponds to containerd/CRI and cadvisor.

https://github.com/containerd/cri/blob/c1115d4e57f55a5f45fb3efd29d3181ce26d5c6a/pkg/server/container_stats_list_unix.go#L106-L129
google/cadvisor@307d1b1

Fix moby/moby#40727

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Apr 3, 2020
1 parent 6e98ebc commit 15c3d1c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cli/command/container/stats_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,15 @@ func calculateNetwork(network map[string]types.NetworkStats) (float64, float64)

// calculateMemUsageUnixNoCache calculate memory usage of the container.
// Page cache is intentionally excluded to avoid misinterpretation of the output.
//
// From https://github.com/google/cadvisor/commit/307d1b1cb320fef66fab02db749f07a459245451
// and https://github.com/containerd/cri/commit/6b8846cdf8b8c98c1d965313d66bc8489166059a
func calculateMemUsageUnixNoCache(mem types.MemoryStats) float64 {
return float64(mem.Usage - mem.Stats["cache"])
if v, isCgroup1 := mem.Stats["total_inactive_file"]; isCgroup1 {
return float64(mem.Usage - v)
}
// cgroup v2
return float64(mem.Usage - mem.Stats["inactive_file"])
}

func calculateMemPercentUnixNoCache(limit float64, usedNoCache float64) float64 {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/stats_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestCalculateMemUsageUnixNoCache(t *testing.T) {
// Given
stats := types.MemoryStats{Usage: 500, Stats: map[string]uint64{"cache": 400}}
stats := types.MemoryStats{Usage: 500, Stats: map[string]uint64{"total_inactive_file": 400}}

// When
result := calculateMemUsageUnixNoCache(stats)
Expand Down

0 comments on commit 15c3d1c

Please # to comment.