Repository navigation
procstats: support cgroup v2 - #238
Merged
Merged
Conversation
Read CPU quota/period from cpu.max, shares from cpu.weight, and the memory limit from memory.max when /sys/fs/cgroup is the unified hierarchy. Parse the "0::/path" entry in /proc/<pid>/cgroup, which was previously dropped, so v2 pods no longer fall back to 1/NumCPU and total system memory. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Add benchmarks for parsing /proc/<pid>/cgroup and cpu.max and for reading the live cgroup CPU and memory files. From review: - Check for cgroup v2 once instead of an os.Stat on every read. - Read small cgroup files with a single open/read/close on Linux. - Fall back to sysinfo when no memory cgroup is found, instead of reporting a limit of 0. - Map cpu.weight so the v2 default (100) matches the v1 default shares (1024). - Reject malformed cpu.max and default the period to 100ms when absent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
b.Loop keeps the compiler from eliminating calls whose results are discarded, which matters for the pure parseCPUMax benchmark. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kevinburkesegment
approved these changes
Oct 30, 2026
Merged
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.
Problem
procstatsonly read cgroup v1 files (cpu.cfs_quota_us,cpu.cfs_period_us,cpu.shares,memory.limit_in_bytes) and dropped the0::/pathline in/proc/<pid>/cgroup. On cgroup v2 pods the reads failed silently, so CPU percent fell back to1/NumCPUand the reported memory limit (Available) to 0.Change
/sys/fs/cgroup/cgroup.controllers; v1 and hybrid systems keep the existing paths.cpu.max(maxreported as -1µs, matching v1; period defaults to 100ms if absent; malformed input is rejected).cpu.weight, mapping the v2 default (100) to the v1 default (1024) so the existingshares/1024ratio still applies.memory.max(maxtreated as unlimited).0::entry and addProcCGroup.Unified();proc_linux.gofalls back to it when there is no v1cpu,cpuacctentry.os.ReadFileon error or large files).Testing
cpu.maxparsing (including malformed input) and weight→shares (procstats/linux/cgroup_v2_test.go).go test ./procstats/linuxpasses on linux/arm64 in a cgroup v2 container (TestReadProcSchedskipped: Docker Desktop's kernel has no/proc/<pid>/sched, unrelated) and on macOS.docker run --cpus=1.5 --memory=256m,stat -fc %T /sys/fs/cgroup=cgroup2fs):mainno such file)no such file)no such file)main.ParseProcCGroupnow also returns the0::entry on hybrid hosts.Benchmarks
New benchmarks:
BenchmarkParseProcCGroup(v1/v2),BenchmarkParseCPUMax, and live-fileBenchmarkReadCPU{Period,Quota,Shares}/BenchmarkReadMemoryLimit.Run on linux/arm64 in a cgroup v2 Docker container, laptop on AC power with no video/music running; 10 runs per side,
mainand this PR interleaved each round, compared withbenchstat. Benchmarks useb.Loop()so results that are discarded can't be optimized away:mainmaindoes not support v2, so its read numbers are the cost of failing to find the v1 files, not a working baseline. The added cost is ~0.4µs per CPU read and ~4µs per memory read, once per collection. v1 parsing is unchanged.Review pass
Asked Claude to review the first version for performance and correctness. Applied: caching the v2 check (was an
os.Statper read), the lighter small-file read, the memory 0→sysinfo fallback, thecpu.weightmapping, and strictercpu.maxparsing.Not addressed here:
cpu.maxis still read twice per collection (avoiding it needs a new exported function), and reading another pid's cgroup or limits set on a parent cgroup are unchanged from v1 behaviour.🤖 Generated with Claude Code