Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cgroupv2 mem usage calculation to match docker cli #3370

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion agent/stats/utils_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// dockerStatsToContainerStats returns a new object of the ContainerStats object from docker stats.
func dockerStatsToContainerStats(dockerStats *types.StatsJSON) (*ContainerStats, error) {
cpuUsage := dockerStats.CPUStats.CPUUsage.TotalUsage / numCores
memoryUsage := dockerStats.MemoryStats.Usage - dockerStats.MemoryStats.Stats["cache"]
memoryUsage := getMemUsage(dockerStats.MemoryStats)
storageReadBytes, storageWriteBytes := getStorageStats(dockerStats)
networkStats := getNetworkStats(dockerStats)
return &ContainerStats{
Expand All @@ -40,6 +40,21 @@ func dockerStatsToContainerStats(dockerStats *types.StatsJSON) (*ContainerStats,
}, nil
}

func getMemUsage(mem types.MemoryStats) uint64 {
if config.CgroupV2 {
// for cgroupv2 systems, mem usage calculation uses the same method that the docker cli uses
// https://github.com/docker/cli/blob/e198123693b1aaa724041fff602c7d75c8fe4b57/cli/command/container/stats_helpers.go#L227-L249
// see https://github.com/aws/amazon-ecs-agent/issues/3323
if v, ok := mem.Stats["inactive_file"]; ok && v < mem.Usage {
return mem.Usage - v
}
}
if v, ok := mem.Stats["cache"]; ok && v < mem.Usage {
return mem.Usage - v
}
return mem.Usage
}

func validateDockerStats(dockerStats *types.StatsJSON) error {
if config.CgroupV2 {
// PercpuUsage is not available in cgroupv2
Expand Down