Skip to content

feat(containers): export container memory working set - #426

Merged
mayankpande88 merged 1 commit into
mainfrom
feat/memory-working-set
Dec 3, 2026
Merged

mayankpande88 merged 1 commit into
mainfrom
feat/memory-working-set

Conversation

@mayankpande88

Copy link
Copy Markdown
Contributor

What this PR does

Adds container_resources_memory_working_set_bytes: memory usage minus inactive page cache, which is how the kubelet computes container_memory_working_set_bytes (the number behind evictions and kubectl top):

  • cgroup v2: memory.current − inactive_file
  • cgroup v1: memory.usage_in_bytes − total_inactive_file
  • 0 if inactive file pages exceed usage (same floor as cAdvisor)

The 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 + cache counts inactive cache that the kernel can reclaim, so it reads high on any workload that streams files. rss_bytes and cache_bytes are 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 has memory.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?

  • cgroup unit tests: added usage fixtures for the v1 cgroups and working-set assertions for all five v1/v2 fixtures, plus TestWorkingSet for the floor at 0.
  • Compared the built agent with the kubelet's working set and with the cgroup files on a cgroup v2 node and on a cgroup v1 node (details below).

eBPF changes?

No.

Checklist

  • gofmt and go vet ./cgroup/ ./containers/ pass
  • go test ./cgroup/ ./containers/ passes (on Linux; CI skips containers)
  • CHANGELOG.md updated under ## [Unreleased]
  • No real credentials, internal hostnames, or production captures in test fixtures
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:

ratio to kubelet working set p10 median p90
new working_set_bytes 1.000 1.000 1.002
rss_bytes 0.027 0.935 0.997
rss_bytes + cache_bytes 0.314 0.964 32.457

All 10 containers were within ±2% in every snapshot. For the file-reading workload, the kubelet reports 6.2 MiB, rss + cache reads 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 against memory.usage_in_bytes − total_inactive_file read 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.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mayankpande88
mayankpande88 merged commit d6f08be into main Dec 3, 2026
7 checks passed
@mayankpande88
mayankpande88 deleted the feat/memory-working-set branch December 3, 2026 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants