Skip to content

Commit 3fb2230

Browse files
committed
Add more memory cgroup metrics
1 parent 255cff7 commit 3fb2230

File tree

6 files changed

+90
-4
lines changed

6 files changed

+90
-4
lines changed

container/libcontainer/handler.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,16 +805,29 @@ func setMemoryStats(s *cgroups.Stats, ret *info.ContainerStats) {
805805
ret.Memory.RSS = s.MemoryStats.Stats["anon"]
806806
ret.Memory.Swap = s.MemoryStats.SwapUsage.Usage - s.MemoryStats.Usage.Usage
807807
ret.Memory.MappedFile = s.MemoryStats.Stats["file_mapped"]
808+
ret.Memory.Shmem = s.MemoryStats.Stats["shmem"]
809+
ret.Memory.Dirty = s.MemoryStats.Stats["file_dirty"]
810+
ret.Memory.Writeback = s.MemoryStats.Stats["file_writeback"]
811+
ret.Memory.Unevictable = s.MemoryStats.Stats["unevictable"]
812+
ret.Memory.Sock = s.MemoryStats.Stats["sock"]
808813
} else if s.MemoryStats.UseHierarchy {
809814
ret.Memory.Cache = s.MemoryStats.Stats["total_cache"]
810815
ret.Memory.RSS = s.MemoryStats.Stats["total_rss"]
811816
ret.Memory.Swap = s.MemoryStats.Stats["total_swap"]
812817
ret.Memory.MappedFile = s.MemoryStats.Stats["total_mapped_file"]
818+
ret.Memory.Shmem = s.MemoryStats.Stats["total_shmem"]
819+
ret.Memory.Dirty = s.MemoryStats.Stats["total_dirty"]
820+
ret.Memory.Writeback = s.MemoryStats.Stats["total_writeback"]
821+
ret.Memory.Unevictable = s.MemoryStats.Stats["total_unevictable"]
813822
} else {
814823
ret.Memory.Cache = s.MemoryStats.Stats["cache"]
815824
ret.Memory.RSS = s.MemoryStats.Stats["rss"]
816825
ret.Memory.Swap = s.MemoryStats.Stats["swap"]
817826
ret.Memory.MappedFile = s.MemoryStats.Stats["mapped_file"]
827+
ret.Memory.Shmem = s.MemoryStats.Stats["shmem"]
828+
ret.Memory.Dirty = s.MemoryStats.Stats["dirty"]
829+
ret.Memory.Writeback = s.MemoryStats.Stats["writeback"]
830+
ret.Memory.Unevictable = s.MemoryStats.Stats["unevictable"]
818831
}
819832
if v, ok := s.MemoryStats.Stats["pgfault"]; ok {
820833
ret.Memory.ContainerData.Pgfault = v

info/v1/container.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,27 @@ type MemoryStats struct {
390390
// The amount of memory used for mapped files (includes tmpfs/shmem)
391391
MappedFile uint64 `json:"mapped_file"`
392392

393+
// The amount of cached filesystem data that is swap-backed, such as tmpfs,
394+
// shm segments, shared anonymous mmap()s
395+
// Units: Bytes.
396+
Shmem uint64 `json:"shmem"`
397+
398+
// The amount of bytes that are waiting to get written back to the disk
399+
// Units: Bytes.
400+
Dirty uint64 `json:"dirty"`
401+
402+
// The amount of bytes of file/anon cache that are queued for syncing to disk
403+
// Units: Bytes.
404+
Writeback uint64 `json:"writeback"`
405+
406+
// The amount of bytes of memory that cannot be reclaimed (mlocked etc)
407+
// Units: Bytes.
408+
Unevictable uint64 `json:"unevictable"`
409+
410+
// The amount of memory used in network transmission buffers
411+
// Units: Bytes.
412+
Sock uint64 `json:"sock"`
413+
393414
// The amount of working set memory, this includes recently accessed memory,
394415
// dirty memory, and kernel memory. Working set is <= "usage".
395416
// Units: Bytes.

info/v1/test/datagen.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ func GenerateRandomStats(numStats, numCores int, duration time.Duration) []*info
4747
stats.Memory.Cache = uint64(rand.Int63n(4096))
4848
stats.Memory.RSS = uint64(rand.Int63n(4096))
4949
stats.Memory.MappedFile = uint64(rand.Int63n(4096))
50+
stats.Memory.Shmem = uint64(rand.Int63n(4096))
51+
stats.Memory.Dirty = uint64(rand.Int63n(4096))
52+
stats.Memory.Writeback = uint64(rand.Int63n(4096))
53+
stats.Memory.Unevictable = uint64(rand.Int63n(4096))
54+
stats.Memory.Sock = uint64(rand.Int63n(4096))
5055
stats.Memory.KernelUsage = uint64(rand.Int63n(4096))
5156
stats.ReferencedMemory = uint64(rand.Int63n(1000))
5257
ret[i] = stats

info/v2/conversion_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,15 @@ func TestContainerStatsFromV1(t *testing.T) {
140140
Usage: 1,
141141
Cache: 2,
142142
RSS: 3,
143-
WorkingSet: 4,
144-
Failcnt: 5,
145-
TotalActiveFile: 6,
146-
TotalInactiveFile: 7,
143+
Shmem: 4,
144+
Dirty: 5,
145+
Writeback: 6,
146+
Unevictable: 7,
147+
Sock: 8,
148+
WorkingSet: 9,
149+
Failcnt: 10,
150+
TotalActiveFile: 11,
151+
TotalInactiveFile: 12,
147152
ContainerData: v1.MemoryStatsMemoryData{
148153
Pgfault: 1,
149154
Pgmajfault: 2,

metrics/prometheus.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/google/cadvisor/container"
2424
info "github.com/google/cadvisor/info/v1"
2525
v2 "github.com/google/cadvisor/info/v2"
26+
"github.com/opencontainers/runc/libcontainer/cgroups"
2627

2728
"github.com/prometheus/client_golang/prometheus"
2829

@@ -398,6 +399,42 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc, includedMetri
398399
getValues: func(s *info.ContainerStats) metricValues {
399400
return metricValues{{value: float64(s.Memory.MappedFile), timestamp: s.Timestamp}}
400401
},
402+
}, {
403+
name: "container_memory_shmem",
404+
help: "Size of shmem in bytes.",
405+
valueType: prometheus.GaugeValue,
406+
getValues: func(s *info.ContainerStats) metricValues {
407+
return metricValues{{value: float64(s.Memory.Shmem), timestamp: s.Timestamp}}
408+
},
409+
}, {
410+
name: "container_memory_dirty",
411+
help: "Size of memory that are waiting to get written back to the disk in bytes.",
412+
valueType: prometheus.GaugeValue,
413+
getValues: func(s *info.ContainerStats) metricValues {
414+
return metricValues{{value: float64(s.Memory.Dirty), timestamp: s.Timestamp}}
415+
},
416+
}, {
417+
name: "container_memory_writeback",
418+
help: "Size of file/anon cache that are queued for syncing to disk in bytes.",
419+
valueType: prometheus.GaugeValue,
420+
getValues: func(s *info.ContainerStats) metricValues {
421+
return metricValues{{value: float64(s.Memory.Writeback), timestamp: s.Timestamp}}
422+
},
423+
}, {
424+
name: "container_memory_unevictable",
425+
help: "Size of unevictable memory in bytes.",
426+
valueType: prometheus.GaugeValue,
427+
getValues: func(s *info.ContainerStats) metricValues {
428+
return metricValues{{value: float64(s.Memory.Unevictable), timestamp: s.Timestamp}}
429+
},
430+
}, {
431+
name: "container_memory_sock",
432+
help: "Size of memory used in network transmission buffers in bytes.",
433+
valueType: prometheus.GaugeValue,
434+
condition: func(s info.ContainerSpec) bool { return cgroups.IsCgroup2UnifiedMode() },
435+
getValues: func(s *info.ContainerStats) metricValues {
436+
return metricValues{{value: float64(s.Memory.Sock), timestamp: s.Timestamp}}
437+
},
401438
}, {
402439
name: "container_memory_swap",
403440
help: "Container swap usage in bytes.",

metrics/prometheus_fake.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ func (p testSubcontainersInfoProvider) GetRequestedContainersInfo(string, v2.Req
357357
RSS: 15,
358358
MappedFile: 16,
359359
KernelUsage: 17,
360+
Shmem: 18,
361+
Dirty: 19,
362+
Writeback: 20,
363+
Unevictable: 21,
364+
Sock: 22,
360365
Swap: 8192,
361366
},
362367
Hugetlb: map[string]info.HugetlbStats{

0 commit comments

Comments
 (0)