Skip to content

Commit

Permalink
Merge pull request #3306 from 9bany/ft/export-memory-kernal-usage
Browse files Browse the repository at this point in the history
Export container kernel memory usage
  • Loading branch information
iwankgb committed Jul 4, 2023
2 parents 8164b38 + cd663bd commit 0f004a2
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions container/libcontainer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ func setMemoryStats(s *cgroups.Stats, ret *info.ContainerStats) {
ret.Memory.Usage = s.MemoryStats.Usage.Usage
ret.Memory.MaxUsage = s.MemoryStats.Usage.MaxUsage
ret.Memory.Failcnt = s.MemoryStats.Usage.Failcnt
ret.Memory.KernelUsage = s.MemoryStats.KernelUsage.Usage

if cgroups.IsCgroup2UnifiedMode() {
ret.Memory.Cache = s.MemoryStats.Stats["file"]
Expand Down
4 changes: 4 additions & 0 deletions info/v1/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ type MemoryStats struct {

Failcnt uint64 `json:"failcnt"`

// Size of kernel memory allocated in bytes.
// Units: Bytes.
KernelUsage uint64 `json:"kernel"`

ContainerData MemoryStatsMemoryData `json:"container_data,omitempty"`
HierarchicalData MemoryStatsMemoryData `json:"hierarchical_data,omitempty"`
}
Expand Down
1 change: 1 addition & 0 deletions info/v1/test/datagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func GenerateRandomStats(numStats, numCores int, duration time.Duration) []*info
stats.Memory.Cache = uint64(rand.Int63n(4096))
stats.Memory.RSS = uint64(rand.Int63n(4096))
stats.Memory.MappedFile = uint64(rand.Int63n(4096))
stats.Memory.KernelUsage = uint64(rand.Int63n(4096))
stats.ReferencedMemory = uint64(rand.Int63n(1000))
ret[i] = stats
}
Expand Down
7 changes: 7 additions & 0 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc, includedMetri
getValues: func(s *info.ContainerStats) metricValues {
return metricValues{{value: float64(s.Memory.RSS), timestamp: s.Timestamp}}
},
}, {
name: "container_memory_kernel_usage",
help: "Size of kernel memory allocated in bytes.",
valueType: prometheus.GaugeValue,
getValues: func(s *info.ContainerStats) metricValues {
return metricValues{{value: float64(s.Memory.KernelUsage), timestamp: s.Timestamp}}
},
}, {
name: "container_memory_mapped_file",
help: "Size of memory mapped files in bytes.",
Expand Down
9 changes: 5 additions & 4 deletions metrics/prometheus_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,11 @@ func (p testSubcontainersInfoProvider) GetRequestedContainersInfo(string, v2.Req
Unevictable: map[uint8]uint64{0: 8900, 1: 20000},
},
},
Cache: 14,
RSS: 15,
MappedFile: 16,
Swap: 8192,
Cache: 14,
RSS: 15,
MappedFile: 16,
KernelUsage: 17,
Swap: 8192,
},
Hugetlb: map[string]info.HugetlbStats{
"2Mi": {
Expand Down
3 changes: 3 additions & 0 deletions metrics/testdata/prometheus_metrics
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ container_memory_failures_total{container_env_foo_env="prod",container_label_foo
container_memory_failures_total{container_env_foo_env="prod",container_label_foo_label="bar",failure_type="pgfault",id="testcontainer",image="test",name="testcontaineralias",scope="hierarchy",zone_name="hello"} 12 1395066363000
container_memory_failures_total{container_env_foo_env="prod",container_label_foo_label="bar",failure_type="pgmajfault",id="testcontainer",image="test",name="testcontaineralias",scope="container",zone_name="hello"} 11 1395066363000
container_memory_failures_total{container_env_foo_env="prod",container_label_foo_label="bar",failure_type="pgmajfault",id="testcontainer",image="test",name="testcontaineralias",scope="hierarchy",zone_name="hello"} 13 1395066363000
# HELP container_memory_kernel_usage Size of kernel memory allocated in bytes.
# TYPE container_memory_kernel_usage gauge
container_memory_kernel_usage{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 17 1395066363000
# HELP container_memory_mapped_file Size of memory mapped files in bytes.
# TYPE container_memory_mapped_file gauge
container_memory_mapped_file{container_env_foo_env="prod",container_label_foo_label="bar",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 16 1395066363000
Expand Down
3 changes: 3 additions & 0 deletions metrics/testdata/prometheus_metrics_whitelist_filtered
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ container_memory_failures_total{container_env_foo_env="prod",failure_type="pgfau
container_memory_failures_total{container_env_foo_env="prod",failure_type="pgfault",id="testcontainer",image="test",name="testcontaineralias",scope="hierarchy",zone_name="hello"} 12 1395066363000
container_memory_failures_total{container_env_foo_env="prod",failure_type="pgmajfault",id="testcontainer",image="test",name="testcontaineralias",scope="container",zone_name="hello"} 11 1395066363000
container_memory_failures_total{container_env_foo_env="prod",failure_type="pgmajfault",id="testcontainer",image="test",name="testcontaineralias",scope="hierarchy",zone_name="hello"} 13 1395066363000
# HELP container_memory_kernel_usage Size of kernel memory allocated in bytes.
# TYPE container_memory_kernel_usage gauge
container_memory_kernel_usage{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 17 1395066363000
# HELP container_memory_mapped_file Size of memory mapped files in bytes.
# TYPE container_memory_mapped_file gauge
container_memory_mapped_file{container_env_foo_env="prod",id="testcontainer",image="test",name="testcontaineralias",zone_name="hello"} 16 1395066363000
Expand Down

0 comments on commit 0f004a2

Please sign in to comment.