From 691ca316134060db0508730aa7757bc5e6c1e280 Mon Sep 17 00:00:00 2001 From: Odin Ugedal Date: Mon, 22 Feb 2021 08:47:23 +0100 Subject: [PATCH] Fix memory stats for cgroup v2 In cgroup v2, MemoryStats.UseHierarchy has no meaning, since all resources are tracked as if UseHierarchy=true in v1. cgroup v2 also have some other names for the stats. For those interested, the mapping can be found here: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/memcontrol.c#n4026 --- container/libcontainer/handler.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/container/libcontainer/handler.go b/container/libcontainer/handler.go index 1094b10392..2f49abc273 100644 --- a/container/libcontainer/handler.go +++ b/container/libcontainer/handler.go @@ -834,7 +834,12 @@ func setMemoryStats(s *cgroups.Stats, ret *info.ContainerStats) { ret.Memory.MaxUsage = s.MemoryStats.Usage.MaxUsage ret.Memory.Failcnt = s.MemoryStats.Usage.Failcnt - if s.MemoryStats.UseHierarchy { + if cgroups.IsCgroup2UnifiedMode() { + ret.Memory.Cache = s.MemoryStats.Stats["file"] + ret.Memory.RSS = s.MemoryStats.Stats["anon"] + ret.Memory.Swap = s.MemoryStats.SwapUsage.Usage + ret.Memory.MappedFile = s.MemoryStats.Stats["file_mapped"] + } else if s.MemoryStats.UseHierarchy { ret.Memory.Cache = s.MemoryStats.Stats["total_cache"] ret.Memory.RSS = s.MemoryStats.Stats["total_rss"] ret.Memory.Swap = s.MemoryStats.Stats["total_swap"]