docker: correct stat response for rss, cache and swap memory in cgroups v1 (#25741)

#25138 refactoring accidentally removed
some of the memory stats that weren't available as concrete types in
containerapi.
This commit is contained in:
Piotr Kazmierczak
2025-04-24 15:17:56 +02:00
committed by GitHub
parent 4d7ed88a8d
commit 3ad0df71a8
4 changed files with 18 additions and 4 deletions

3
.changelog/25741.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
docker: Fix missing stats for rss, cache and swap memory for cgroups v1
```

View File

@@ -128,9 +128,9 @@ func (h *taskHandle) collectStats(ctx context.Context, destCh *usageSender, inte
// collectDockerStats performs the stats collection from the Docker API. It is // collectDockerStats performs the stats collection from the Docker API. It is
// split into its own function for the purpose of aiding testing. // split into its own function for the purpose of aiding testing.
func (h *taskHandle) collectDockerStats(ctx context.Context) (*containerapi.Stats, error) { func (h *taskHandle) collectDockerStats(ctx context.Context) (*containerapi.StatsResponse, error) {
var stats *containerapi.Stats var stats *containerapi.StatsResponse
statsReader, err := h.dockerClient.ContainerStats(ctx, h.containerID, false) statsReader, err := h.dockerClient.ContainerStats(ctx, h.containerID, false)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {

View File

@@ -22,12 +22,15 @@ import (
func TestDriver_DockerStatsCollector(t *testing.T) { func TestDriver_DockerStatsCollector(t *testing.T) {
ci.Parallel(t) ci.Parallel(t)
stats := &containerapi.Stats{} stats := &containerapi.StatsResponse{}
stats.CPUStats.ThrottlingData.Periods = 10 stats.CPUStats.ThrottlingData.Periods = 10
stats.CPUStats.ThrottlingData.ThrottledPeriods = 10 stats.CPUStats.ThrottlingData.ThrottledPeriods = 10
stats.CPUStats.ThrottlingData.ThrottledTime = 10 stats.CPUStats.ThrottlingData.ThrottledTime = 10
stats.MemoryStats.Stats = map[string]uint64{} stats.MemoryStats.Stats = map[string]uint64{}
stats.MemoryStats.Stats["rss"] = 6537216
stats.MemoryStats.Stats["cache"] = 1234
stats.MemoryStats.Stats["swap"] = 0
stats.MemoryStats.Stats["file_mapped"] = 1024 stats.MemoryStats.Stats["file_mapped"] = 1024
stats.MemoryStats.Usage = 5651904 stats.MemoryStats.Usage = 5651904
stats.MemoryStats.MaxUsage = 6651904 stats.MemoryStats.MaxUsage = 6651904
@@ -39,6 +42,9 @@ func TestDriver_DockerStatsCollector(t *testing.T) {
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
must.Eq(t, stats.MemoryStats.Stats["file_mapped"], ru.ResourceUsage.MemoryStats.MappedFile) must.Eq(t, stats.MemoryStats.Stats["file_mapped"], ru.ResourceUsage.MemoryStats.MappedFile)
must.Eq(t, stats.MemoryStats.Stats["rss"], ru.ResourceUsage.MemoryStats.RSS)
must.Eq(t, stats.MemoryStats.Stats["cache"], ru.ResourceUsage.MemoryStats.Cache)
must.Eq(t, stats.MemoryStats.Stats["swap"], ru.ResourceUsage.MemoryStats.Swap)
must.Eq(t, stats.MemoryStats.Usage, ru.ResourceUsage.MemoryStats.Usage) must.Eq(t, stats.MemoryStats.Usage, ru.ResourceUsage.MemoryStats.Usage)
must.Eq(t, stats.MemoryStats.MaxUsage, ru.ResourceUsage.MemoryStats.MaxUsage) must.Eq(t, stats.MemoryStats.MaxUsage, ru.ResourceUsage.MemoryStats.MaxUsage)
must.Eq(t, stats.CPUStats.ThrottlingData.ThrottledPeriods, ru.ResourceUsage.CpuStats.ThrottledPeriods) must.Eq(t, stats.CPUStats.ThrottlingData.ThrottledPeriods, ru.ResourceUsage.CpuStats.ThrottledPeriods)

View File

@@ -19,7 +19,7 @@ var (
DockerCgroupV2MeasuredMemStats = []string{"Cache", "Swap", "Usage"} DockerCgroupV2MeasuredMemStats = []string{"Cache", "Swap", "Usage"}
) )
func DockerStatsToTaskResourceUsage(s *containerapi.Stats, compute cpustats.Compute) *cstructs.TaskResourceUsage { func DockerStatsToTaskResourceUsage(s *containerapi.StatsResponse, compute cpustats.Compute) *cstructs.TaskResourceUsage {
var ( var (
totalCompute = compute.TotalCompute totalCompute = compute.TotalCompute
totalCores = compute.NumCores totalCores = compute.NumCores
@@ -34,7 +34,12 @@ func DockerStatsToTaskResourceUsage(s *containerapi.Stats, compute cpustats.Comp
} }
ms := &cstructs.MemoryStats{ ms := &cstructs.MemoryStats{
// containerapi exposes memory stat file as a map, consult
// https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
MappedFile: s.MemoryStats.Stats["file_mapped"], MappedFile: s.MemoryStats.Stats["file_mapped"],
Cache: s.MemoryStats.Stats["cache"],
RSS: s.MemoryStats.Stats["rss"],
Swap: s.MemoryStats.Stats["swap"],
Usage: s.MemoryStats.Usage, Usage: s.MemoryStats.Usage,
MaxUsage: s.MemoryStats.MaxUsage, MaxUsage: s.MemoryStats.MaxUsage,
Measured: measuredMems, Measured: measuredMems,