Repository navigation
feat(containers): export container memory working set - #426
Merged
Merged
Conversation
Add container_resources_memory_working_set_bytes, computed as cAdvisor and the kubelet do: memory.current - inactive_file on cgroup v2, memory.usage_in_bytes - total_inactive_file on cgroup v1, floored at 0. The existing rss and cache metrics are unchanged.
There was a problem hiding this comment.
Code Review
This pull request introduces a new metric, container_resources_memory_working_set_bytes, which measures memory usage minus the inactive page cache, matching the behavior of kubelet's container_memory_working_set_bytes for both cgroup v1 and v2. The changes include parsing memory usage files (memory.usage_in_bytes for v1 and memory.current for v2), calculating the working set memory, exposing the metric via Prometheus, and adding corresponding unit tests and fixtures. I have no feedback to provide as there are no review comments.
RamanKharchee
approved these changes
Dec 3, 2026
4 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
Adds
container_resources_memory_working_set_bytes: memory usage minus inactive page cache, which is how the kubelet computescontainer_memory_working_set_bytes(the number behind evictions andkubectl top):memory.current − inactive_filememory.usage_in_bytes − total_inactive_fileThe agent's existing metrics don't give you this number.
rss_bytes(anon + mapped file) leaves out kernel memory and active page cache, so it reads low.rss + cachecounts inactive cache that the kernel can reclaim, so it reads high on any workload that streams files.rss_bytesandcache_bytesare unchanged.Decision: if the usage file can't be read, the whole memory stat is dropped, as v1 already does for
memory.limit_in_bytes. The agent skips the root cgroup, and every non-root memory cgroup hasmemory.current/memory.usage_in_bytes, so this doesn't drop anything in practice. It also avoids exporting a working set of 0 when usage is unknown.How was this tested?
cgroupunit tests: added usage fixtures for the v1 cgroups and working-set assertions for all five v1/v2 fixtures, plusTestWorkingSetfor the floor at 0.eBPF changes?
No.
Checklist
go vet ./cgroup/ ./containers/passgo test ./cgroup/ ./containers/passes (on Linux; CI skipscontainers)## [Unreleased]Engineering detail
Local e2e: built the image from this branch and ran the agent binary as a systemd unit on a local single-node k3s VM (Ubuntu 24.04, kernel 6.8, arm64). Test workloads: one that read a 200 MiB file once (mostly inactive cache), one holding 150 MiB of anonymous memory, and one with both. Each comparison read both sources back to back.
cgroup v2, against the kubelet's
container_memory_working_set_bytes(/metrics/cadvisor), 10 containers, 3 snapshots:working_set_bytesrss_bytesrss_bytes + cache_bytesAll 10 containers were within ±2% in every snapshot. For the file-reading workload, the kubelet reports 6.2 MiB,
rss + cachereads 32×, and the new metric matches.cgroup v1 (same VM booted with
systemd.unified_cgroup_hierarchy=0; this kubelet version refuses v1, so the comparison is againstmemory.usage_in_bytes − total_inactive_fileread directly): 8/8 cgroups within ±2% (all 1.000–1.001), including the same three workloads. No errors or panics in the agent log in either run.