Skip to content

Commit 4e8ba95

Browse files
committed
Add more memory cgroup metrics
1 parent 255cff7 commit 4e8ba95

File tree

9 files changed

+125
-4
lines changed

9 files changed

+125
-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

docs/storage/prometheus.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,21 @@ Metric name | Type | Description | Unit (where applicable) | option parameter |
5555
`container_memory_bandwidth_bytes` | Gauge | Total memory bandwidth usage statistics for container counted with RDT Memory Bandwidth Monitoring (MBM). | bytes | resctrl |
5656
`container_memory_bandwidth_local_bytes` | Gauge | Local memory bandwidth usage statistics for container counted with RDT Memory Bandwidth Monitoring (MBM). | bytes | resctrl |
5757
`container_memory_cache` | Gauge | Total page cache memory | bytes | memory |
58+
`container_memory_dirty` | Gauge | Size of memory that are waiting to get written back to the disk | bytes | memory |
5859
`container_memory_failcnt` | Counter | Number of memory usage hits limits | | memory |
5960
`container_memory_failures_total` | Counter | Cumulative count of memory allocation failures | | memory |
6061
`container_memory_mapped_file` | Gauge | Size of memory mapped files | bytes | memory |
6162
`container_memory_max_usage_bytes` | Gauge | Maximum memory usage recorded | bytes | memory |
6263
`container_memory_migrate` | Gauge | Memory migrate status | | cpuset |
6364
`container_memory_numa_pages` | Gauge | Number of used pages per NUMA node | | memory_numa |
6465
`container_memory_rss` | Gauge | Size of RSS | bytes | memory |
66+
`container_memory_shmem` | Gauge | Size of shmem | bytes | memory |
67+
`container_memory_sock` | Gauge | Size of memory used in network transmission buffers (cgroupv2-only) | bytes | memory |
6568
`container_memory_swap` | Gauge | Container swap usage | bytes | memory |
69+
`container_memory_unevictable` | Gauge | Size of unevictable memory | bytes | memory |
6670
`container_memory_usage_bytes` | Gauge | Current memory usage, including all memory regardless of when it was accessed | bytes | memory |
6771
`container_memory_working_set_bytes` | Gauge | Current working set | bytes | memory |
72+
`container_memory_writeback` | Gauge | Size of file/anon cache that are queued for syncing to disk | bytes | memory |
6873
`container_network_advance_tcp_stats_total` | Gauge | advanced tcp connections statistic for container | | advtcp |
6974
`container_network_receive_bytes_total` | Counter | Cumulative count of bytes received | bytes | network |
7075
`container_network_receive_errors_total` | Counter | Cumulative count of errors encountered while receiving | | network |

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{

metrics/testdata/prometheus_metrics

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ container_last_seen{container_env_foo_env="prod",container_label_foo_label="bar"
142142
# HELP container_memory_cache Number of bytes of page cache memory.
143143
# TYPE container_memory_cache gauge
144144
container_memory_cache{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 14 1395066363000
145+
# HELP container_memory_dirty Size of memory that are waiting to get written back to the disk in bytes.
146+
# TYPE container_memory_dirty gauge
147+
container_memory_dirty{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 19 1395066363000
145148
# HELP container_memory_failcnt Number of memory usage hits limits
146149
# TYPE container_memory_failcnt counter
147150
container_memory_failcnt{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 0 1395066363000
@@ -180,6 +183,12 @@ container_memory_numa_pages{container_env_foo_env="prod",container_label_foo_lab
180183
# HELP container_memory_rss Size of RSS in bytes.
181184
# TYPE container_memory_rss gauge
182185
container_memory_rss{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 15 1395066363000
186+
# HELP container_memory_shmem Size of shmem in bytes.
187+
# TYPE container_memory_shmem gauge
188+
container_memory_shmem{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 18 1395066363000
189+
# HELP container_memory_sock Size of memory used in network transmission buffers in bytes.
190+
# TYPE container_memory_sock gauge
191+
container_memory_sock{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 22 1395066363000
183192
# HELP container_memory_swap Container swap usage in bytes.
184193
# TYPE container_memory_swap gauge
185194
container_memory_swap{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 8192 1395066363000
@@ -189,12 +198,18 @@ container_memory_total_active_file_bytes{container_env_foo_env="prod",container_
189198
# HELP container_memory_total_inactive_file_bytes Current total inactive file in bytes.
190199
# TYPE container_memory_total_inactive_file_bytes gauge
191200
container_memory_total_inactive_file_bytes{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 6 1395066363000
201+
# HELP container_memory_unevictable Size of unevictable memory in bytes.
202+
# TYPE container_memory_unevictable gauge
203+
container_memory_unevictable{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 21 1395066363000
192204
# HELP container_memory_usage_bytes Current memory usage in bytes, including all memory regardless of when it was accessed
193205
# TYPE container_memory_usage_bytes gauge
194206
container_memory_usage_bytes{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 8 1395066363000
195207
# HELP container_memory_working_set_bytes Current working set in bytes.
196208
# TYPE container_memory_working_set_bytes gauge
197209
container_memory_working_set_bytes{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 9 1395066363000
210+
# HELP container_memory_writeback Size of file/anon cache that are queued for syncing to disk in bytes.
211+
# TYPE container_memory_writeback gauge
212+
container_memory_writeback{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 20 1395066363000
198213
# HELP container_network_advance_tcp_stats_total advance tcp connections statistic for container
199214
# TYPE container_network_advance_tcp_stats_total gauge
200215
container_network_advance_tcp_stats_total{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",tcp_state="activeopens",zone_name="hello"} 1.1038621e+07 1395066363000

metrics/testdata/prometheus_metrics_whitelist_filtered

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ container_last_seen{container_env_foo_env="prod",id="testcontainer",image="test"
142142
# HELP container_memory_cache Number of bytes of page cache memory.
143143
# TYPE container_memory_cache gauge
144144
container_memory_cache{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 14 1395066363000
145+
# HELP container_memory_dirty Size of memory that are waiting to get written back to the disk in bytes.
146+
# TYPE container_memory_dirty gauge
147+
container_memory_dirty{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 19 1395066363000
145148
# HELP container_memory_failcnt Number of memory usage hits limits
146149
# TYPE container_memory_failcnt counter
147150
container_memory_failcnt{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 0 1395066363000
@@ -180,6 +183,12 @@ container_memory_numa_pages{container_env_foo_env="prod",id="testcontainer",imag
180183
# HELP container_memory_rss Size of RSS in bytes.
181184
# TYPE container_memory_rss gauge
182185
container_memory_rss{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 15 1395066363000
186+
# HELP container_memory_shmem Size of shmem in bytes.
187+
# TYPE container_memory_shmem gauge
188+
container_memory_shmem{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 18 1395066363000
189+
# HELP container_memory_sock Size of memory used in network transmission buffers in bytes.
190+
# TYPE container_memory_sock gauge
191+
container_memory_sock{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 22 1395066363000
183192
# HELP container_memory_swap Container swap usage in bytes.
184193
# TYPE container_memory_swap gauge
185194
container_memory_swap{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 8192 1395066363000
@@ -189,12 +198,18 @@ container_memory_total_active_file_bytes{container_env_foo_env="prod",id="testco
189198
# HELP container_memory_total_inactive_file_bytes Current total inactive file in bytes.
190199
# TYPE container_memory_total_inactive_file_bytes gauge
191200
container_memory_total_inactive_file_bytes{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 6 1395066363000
201+
# HELP container_memory_unevictable Size of unevictable memory in bytes.
202+
# TYPE container_memory_unevictable gauge
203+
container_memory_unevictable{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 21 1395066363000
192204
# HELP container_memory_usage_bytes Current memory usage in bytes, including all memory regardless of when it was accessed
193205
# TYPE container_memory_usage_bytes gauge
194206
container_memory_usage_bytes{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 8 1395066363000
195207
# HELP container_memory_working_set_bytes Current working set in bytes.
196208
# TYPE container_memory_working_set_bytes gauge
197209
container_memory_working_set_bytes{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 9 1395066363000
210+
# HELP container_memory_writeback Size of file/anon cache that are queued for syncing to disk in bytes.
211+
# TYPE container_memory_writeback gauge
212+
container_memory_writeback{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 20 1395066363000
198213
# HELP container_network_advance_tcp_stats_total advance tcp connections statistic for container
199214
# TYPE container_network_advance_tcp_stats_total gauge
200215
container_network_advance_tcp_stats_total{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",tcp_state="activeopens",zone_name="hello"} 1.1038621e+07 1395066363000

0 commit comments

Comments
 (0)