Skip to content

Add procfs processor for cgroup, shm and child process resources - #32

Open
prabhkodes wants to merge 3 commits into
polyaxon:masterfrom
prabhkodes:feat/procfs-resources
Open

prabhkodes wants to merge 3 commits into
polyaxon:masterfrom
prabhkodes:feat/procfs-resources

Conversation

@prabhkodes

@prabhkodes prabhkodes commented Oct 27, 2026 •

Copy link
Copy Markdown

Closes #31

Summary

This PR adds a procfs resource processor. It reads container metrics from /proc and /sys/fs/cgroup on Linux. It writes them with the same resource writer as the psutil and GPU metrics.

psutil shows the node, not the pod. A job at 68% of its pod memory limit logs memory = 20%. This PR makes the pod limits visible.

Metrics

Five metrics are on by default. Each one shows one failure mode.

metric shows
cgroup_memory_percent memory use against the pod limit
cgroup_oom_kill number of OOM kills
cgroup_cpu_throttled_usec time lost to the CPU quota
shm_percent /dev/shm use
proc_children_state_d worker processes stuck in disk IO

Set POLYAXON_COLLECT_RESOURCES_DETAILED=true to log 17 more metrics. See CORE_METRICS in the processor for the split.

Notes

  • The cgroup directories come from /proc/self/cgroup and /proc/self/mountinfo, so a bind-mounted subtree (Docker with the host cgroup namespace on cgroup v1) does not hide a parent limit. Memory current, max and percent come from the limited level with the least headroom, which is the one the OOM killer reaches first: a pod limit on the parent also counts the siblings' usage. The CPU quota is the smallest one on the path, each level with its own period.
  • The processor logs cgroup metrics only when the cgroup has a memory limit or a CPU quota.
  • The processor is off when /proc/self does not exist. All reads are best-effort. A failure cannot stop a run.
  • Existing metrics do not change. No new dependencies.

Tests

  • 29 tests with a fake /proc tree. They cover cgroup v1, cgroup v2, limits on a parent cgroup, unlimited cgroups, both detail levels, mount roots and subtree aliases from a fake mountinfo, a hybrid v1/v2 host, and a parent with less headroom than the child. The last four fail on the previous commit.
  • Processor and serialization tests: 44 passed. ruff and mypy on the processor: clean. Python 3.8 syntax unchanged.
  • Docker, cgroup v2: a 512 MiB pod with a 300 MiB sibling and a 400 MiB app cgroup reports the pod at 61% instead of the app at 3%.

Precedent. The same walk is already in production elsewhere. Rust's std::thread::available_parallelism takes the smallest cpu.max from the process cgroup up to the mount (quota_v2). The JDK fixed limits on parent nested cgroups in JDK-8322420 (2024) and the hierarchical memory limit in JDK-8370572 (2025). Go 1.25 finds the process cgroup through /proc/self/cgroup and /proc/self/mountinfo (cgroup_linux.go), reading the leaf only. vLLM's CPU-quota reader walks the hierarchy (_cgroup_cpu_limit); vllm-project/vllm#64104 adds the same for memory. The same fix for OpenObserve merged on 2026-10-28 (openobserve/openobserve#15906).

@prabhkodes

Copy link
Copy Markdown
Author

How to use

Nothing to configure. On Linux, any run that already logs resources gets the five core metrics:

from traceml import tracking

tracking.init(collect_resources=True)  # same as today

The metrics land next to cpu, memory and load, in resources/metric/*.plx under the run artifacts, and in the run's Resources tab in the UI.

To log all 22 metrics, set one env var before the run starts:

POLYAXON_COLLECT_RESOURCES_DETAILED=true python train.py

On Polyaxon, polyaxon/cli#58 lets you set this from the component spec instead.

psutil reports the node's cpu and memory, so inside a pod a job at
68% of its memory limit and throttled by its cpu quota is logged as
20% memory and 0% cpu. Read the container-aware counters the kernel
exposes under /proc and /sys/fs/cgroup (v2 and v1) and log them
through the existing resource writer:

- cgroup memory usage, limit, shmem and oom_kill count
- cgroup cpu quota and throttling counters
- /dev/shm usage
- current process rss split, threads and file descriptors
- direct children count, rss and D-state count

Linux only; skipped when /proc/self is missing and every read is
@prabhkodes
prabhkodes force-pushed the feat/procfs-resources branch 2 times, most recently from 324c983 to 90363bd Compare October 27, 2026 13:29
The processor read memory.max and cpu.max on the process's own cgroup
only. Kubernetes sets the pod limit on the parent cgroup and Slurm nests
job and step, so the leaf said "max" and the cgroup metrics were dropped.
Walk from the process's cgroup up to the root, take the smallest limit,
and read usage where that limit is enforced, so percent is what the OOM
killer sees. Same for the cpu quota and throttling counters.
@prabhkodes

Copy link
Copy Markdown
Author

Update in 0cfe446: the limit is now read along the cgroup hierarchy, not only on the process's own cgroup.

Kubernetes sets the pod limit on the parent cgroup and Slurm nests job and step. The leaf then says max, and the first version dropped the cgroup metrics. The processor now walks from the process's cgroup up to the root, takes the smallest limit, and reads usage where that limit is enforced, so cgroup_memory_percent is what the OOM killer sees.

Checked in a container with --memory=1g --cpus=2 and the process in an unlimited child cgroup: before {}, after cgroup_memory_max=1073741824, cgroup_cpu_quota_cpus=2.0. Three new tests, full suite 208 passed.

…m is least

The hierarchy walk joined the path from /proc/self/cgroup onto
/sys/fs/cgroup and kept the directories that exist. That path is
relative to the hierarchy root, but a mount can start below it: Docker
with the host cgroup namespace on cgroup v1 mounts /docker/<id> while
/proc/self/cgroup says /docker/<id>/pod/app, so nothing below the mount
point was found and a nested limit was missed. Read (mount point, mount
root) from /proc/self/mountinfo, strip the mount root from the path and
join the rest onto the mount point. When several mounts contain the
process, take the one with the shortest root, which shows the most
ancestors. Without a matching mount the old join onto cgroup_root is
kept, so tests can point at a fake tree.

Memory was read on the level with the smallest limit. A parent limit
also counts the siblings' usage, so a parent with a larger limit can be
closer to it, and that is where the OOM killer fires first. Report
current, max and percent of the limited level with the least headroom.
The cpu quota stays the smallest one.

cgroup v1 is tried when v2 reports no limit, so a systemd hybrid host,
where v2 is mounted without controllers, still reads its v1 limits.
@prabhkodes

Copy link
Copy Markdown
Author

@md-mq could you review this? It adds a procfs processor that logs memory against the limit of the run's own cgroup and its parents, so a pod or nested limit shows instead of host RAM. polyaxon/cli#58 adds the plugin flag and depends on this PR.

@md-mq

md-mq commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

@prabhkodes I will review it next week, since we are also going to introduce tracking for TPU as well. thank you for the PR.

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.

Log container-aware resources (cgroup, /dev/shm, child processes) next to cpu/memory/load

2 participants