Skip to content

fix: report the cgroupv2 limit with the least room left for container cpu and memory - #44

Open
david-fraley wants to merge 1 commit into
mainfrom
dfraley/cgroupv2-tightest-limit
Open

david-fraley wants to merge 1 commit into
mainfrom
dfraley/cgroupv2-tightest-limit

Conversation

@david-fraley

Copy link
Copy Markdown

Fixes #40 (ENG-3479). Also fixes #38 and #41, so this supersedes #39, #42, and #43.

On cgroup v2, ContainerCPU and ContainerMemory stopped at the nearest cgroup with a limit and read usage from the process's own cgroup. The kernel enforces every limit from the process's cgroup up to the container root, so clistat could report 2% while an ancestor limit was about to kill the process, or 0 cores while nested containers were using 4.

How it works now

  1. Walk from the process's cgroup up to the container root and collect every cgroup with a limit (cpu.max quota or memory.max).
  2. For each one, read usage from that same cgroup. cpu.stat and memory.current include all descendants, so siblings that share the limit are counted.
  3. Report the limit with the least room left (limit minus usage). Ties go to the cgroup nearest the process.
  4. No limit anywhere: report the process's own usage with no total, as before.

If a limit owner has no usage files, the process's own usage is used for that limit (the same fallback #39 and #43 used). cgroup v1 and the public API are unchanged. The README has a new "How container limits are reported" section that documents this and says that limits above the container root, such as a Kubernetes pod limit behind a private cgroup namespace, are not visible.

Verification

Decisions

Generated by Coder Agents on behalf of @david-fraley.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant