Skip to content

Commit

Permalink
drivers/docker: account for cgroup-v2 memory stats
Browse files Browse the repository at this point in the history
If the docker engine is running on cgroup-v2 host, then RSS and Max
Usage doesn't get reported.

Using a heauristic here to avoid adding more API calls to the Docker
Engine to infer cgroups version. Also, opted to avoid coordinating stats
collection with fingerprinting, which adds concurrency complexities.
  • Loading branch information
Mahmood Ali committed Apr 1, 2021
1 parent 4532272 commit f7b7b1c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions drivers/docker/util/stats_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,28 @@ import (

var (
DockerMeasuredCPUStats = []string{"Throttled Periods", "Throttled Time", "Percent"}
DockerMeasuredMemStats = []string{"RSS", "Cache", "Swap", "Usage", "Max Usage"}

// cgroup-v2 only exposes a subset of memory stats
DockerCgroupV1MeasuredMemStats = []string{"RSS", "Cache", "Swap", "Usage", "Max Usage"}
DockerCgroupV2MeasuredMemStats = []string{"Cache", "Swap", "Usage"}
)

func DockerStatsToTaskResourceUsage(s *docker.Stats) *cstructs.TaskResourceUsage {
measuredMems := DockerCgroupV1MeasuredMemStats

// use a simple heauristic to check if cgroup-v2 is used.
// go-dockerclient doesn't distinguish between 0 and not-present value
if s.MemoryStats.Stats.Rss == 0 && s.MemoryStats.MaxUsage == 0 && s.MemoryStats.Usage != 0 {
measuredMems = DockerCgroupV2MeasuredMemStats
}

ms := &cstructs.MemoryStats{
RSS: s.MemoryStats.Stats.Rss,
Cache: s.MemoryStats.Stats.Cache,
Swap: s.MemoryStats.Stats.Swap,
Usage: s.MemoryStats.Usage,
MaxUsage: s.MemoryStats.MaxUsage,
Measured: DockerMeasuredMemStats,
Measured: measuredMems,
}

cs := &cstructs.CpuStats{
Expand Down

0 comments on commit f7b7b1c

Please sign in to comment.