Repository navigation
fix: read cgroupv2 cpu usage from the cgroup that owns cpu.max - #42
Draft
DanielleMaywood wants to merge 2 commits into
Draft
DanielleMaywood wants to merge 2 commits into
DanielleMaywood wants to merge 2 commits into
Conversation
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.
Fixes #41.
In cgroup v2,
cpuTotal()foundcpu.maxby walking up to an ancestor, butcpuUsed()still readcpu.statfrom the process's own cgroup. In docker-in-docker and devcontainer workspaces the agent sits in/init.scopewhile nested containers sit in sibling/docker/<id>, so usage was massively understated (0% reported vs 34% actual) even when the container was busy.Now usage is read from the same cgroup that owns the quota, so used and total describe the same domain. When the process's own cgroup holds the quota, or there is no quota, behavior is unchanged. If the limit owner has no
cpu.stat, it falls back to the old behavior.This mirrors #39, which does the same for memory:
cpuQuota()becomescpuLimit()returning the owning cgroup, andcpuUsedUs()is split out so usage can be read from that owner.Added
CPU/UsageFromLimitOwnerwith an/init.scope+/docker/<id>fixture; it fails onmain(0.1 cores) and passes with this change (2.0 cores).Verification and decisions
Kernel behavior
cpu.statincludes descendants:cgroup_base_stat_flush()inkernel/cgroup/rstat.c(v6.17) adds each cgroup's CPU time into its parent'sbstat, which is whatcpu.statprints.cpu.stat.localis the file that excludes descendants.cpu.maxapplies to the cgroup and everything below it (cgroup v2 docs).Live check (sysbox workspace, root
cpu.max=1200000 100000, agent in/init.scope, 4 busy loops in a nested container):ContainerCPU()main0.017/12 cores (0%)4.05/12 cores (34%)Decisions
if limit.owner.path != s.pathinsideif limit != nilbecause it also setsr.Totalthere.cpuUsed()has nothing else to do in that block, so the two conditions are combined.cpuPeriod()as before. Changing where the period comes from is out of scope.Checks
go test -race -count=3 ./...passes, skippingTestCgroupV2DetectionandTestIsContainerized, which also fail onmainwhen run inside a container.golangci-lintv1.64.7 andgofumptv0.6.0 are clean.Generated by Coder Agents on behalf of @DanielleMaywood.