fix: Fix memory stats not showing on cgroup v2 (#4607)
Push container / Push branches and PRs (push) Has been cancelled
Deploy VitePress site to Pages / build (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
Test / Typecheck (push) Has been cancelled
Test / JavaScript Tests (push) Has been cancelled
Test / Go Tests (push) Has been cancelled
Test / Go Staticcheck (push) Has been cancelled
Test / Integration Tests (push) Has been cancelled

This commit is contained in:
Uğur Tafralı
2026-04-10 16:48:25 +03:00
committed by GitHub
parent 44239614d1
commit f5f736b6ff
3 changed files with 42 additions and 13 deletions
+6
View File
@@ -15,6 +15,12 @@ func calculateMemUsageUnixNoCache(mem container.MemoryStats) float64 {
if v := mem.Stats["inactive_file"]; v < mem.Usage {
return float64(mem.Usage - v)
}
// cgroup v2 fallback: Usage may be 0 on some kernels; use anon memory
if mem.Usage == 0 {
if anon, ok := mem.Stats["anon"]; ok {
return float64(anon)
}
}
return float64(mem.Usage)
}
+25
View File
@@ -49,6 +49,31 @@ func Test_calculateMemUsageUnixNoCache(t *testing.T) {
},
want: 100,
},
{
name: "cgroup v2 with zero Usage falls back to anon",
args: args{
mem: container.MemoryStats{
Usage: 0,
Stats: map[string]uint64{
"inactive_file": 0,
"anon": 50,
},
},
},
want: 50,
},
{
name: "cgroup v2 with zero Usage and no anon returns 0",
args: args{
mem: container.MemoryStats{
Usage: 0,
Stats: map[string]uint64{
"inactive_file": 0,
},
},
},
want: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
+11 -13
View File
@@ -290,19 +290,17 @@ func (d *DockerClient) ContainerStats(ctx context.Context, id string, stats chan
networkTx += netStats.TxBytes
}
if cpuPercent > 0 || mem > 0 {
select {
case <-ctx.Done():
return nil
case stats <- container.ContainerStat{
ID: id,
CPUPercent: cpuPercent,
MemoryPercent: memPercent,
MemoryUsage: mem,
NetworkRxTotal: networkRx,
NetworkTxTotal: networkTx,
}:
}
select {
case <-ctx.Done():
return nil
case stats <- container.ContainerStat{
ID: id,
CPUPercent: cpuPercent,
MemoryPercent: memPercent,
MemoryUsage: mem,
NetworkRxTotal: networkRx,
NetworkTxTotal: networkTx,
}:
}
}
}