Skip to content

feat(supervisor): a starved Envoy stall line names the top CPU consumers over the stall (#1392) - #1435

Merged
bpalermo merged 23 commits into
mainfrom
feat/1392-stall-top-consumers
Oct 8, 2026
Merged

bpalermo merged 23 commits into
mainfrom
feat/1392-stall-top-consumers

Conversation

@bpalermo

@bpalermo bpalermo commented Oct 8, 2026 •

Copy link
Copy Markdown
Owner

Closes #1392

What changes

The supervisor's envoy thread stall line said an Envoy thread was starved and that the node was busy. It did not say who was on the CPUs, and #1320 and #1389 both stopped there. A line with a [starved] thread now also names the top CPU consumers on the node over the stall:

envoy thread stall epoch=210 pid=15 windowMs=1000 thresholdMs=200
  threads=["wrk:worker_0[starved] cpu=310ms runq=611ms blocked=0ms"]
  nodeBusyPct=99.1 ... nodePSICPUSomeMs=1010
  topCgroups="/podruntime/kubelet=1240ms /kubepods/burstable/pod0f3c1a2b-1111-4222-8333-444455556666=820ms /podruntime/runtime=180ms /=95ms"
  topCgroupsOverMs=1000
  topProcs="unavailable: /proc shows only this pod's PID namespace (no hostPID)"
  trackedEpochs=1 handoffPeer=-1

This line is an illustration of the format. It is not from a cluster: the change has not run on a cluster.

field meaning
topCgroups <cgroup path>=<n>ms, largest first, at most --stall-top-consumers (5). CPU time from cgroup v2 cpu.stat usage_usec. Entries do not overlap: each excludes the cgroups below it that have their own entry; a pod is one entry; / is what was charged to the root cgroup itself after the listed cgroups below it are taken out (commonly kernel threads; a userspace task attached to the root is counted there too).
topCgroupsOverMs The interval the figures cover: that line's stall, not the window, starting where the wait began. A runqueue wait is charged when it ends, so a 5 s wait is reported in the second it ended and a 500 ms wait seen 100 ms into a window began 400 ms before it. The start is inferred per starved thread (the tick the wait was seen on, minus the wait) and the consumers are taken from the last one-second sample at or before the earliest start. Two epochs starved in the same window each get their own interval from one sample. It is the interval actually measured.
topCgroupsTruncated true, and only then on the line, when the stall began before the oldest sample kept (32 cgroup samples, 4 process scans): the figures cover only the end of the stall.
topProcs, topProcsOverMs, topProcsTruncated <comm>(<pid>)=<n>ms from /proc/<pid>/stat utime+stime. Only in the host PID namespace.
unavailable: <reason> The value of either field when its source cannot be read. The stall line is logged regardless.

[blocked] and [busy] lines are unchanged: a blocked thread is not waiting for a CPU and a busy one is the consumer. Such a window rolls the cheap cgroup baseline like a quiet one and never triggers a /proc scan.

What the proxy pod can read (read from the chart, not from a node)

  • privileged: true, hostNetwork: true, no hostPID, no /proc or /sys hostPath mount.
  • cgroups: available today. containerd gives a privileged container the host's cgroup namespace (internal/cri/server/container_create.go at v2.2.4: "cgroupns is not used when running in cgroup v1 mode or in privileged"), so /sys/fs/cgroup in the proxy container is the node's hierarchy. This is the source the feature relies on.
  • processes: not available. Without hostPID /proc lists the container's own processes. The code detects that (PID 2 is not kthreadd), makes no scan, and says so in the field. It lights up by itself if the DaemonSet ever sets hostPID: true. I did not change the chart.

Cost and the baseline trade-off

A delta needs a "before", and a stall is only known once it is over (the kernel charges a runqueue wait when it ends), so the baseline cannot be taken lazily when a stall begins.

  • cgroups: one cpu.stat per cgroup down to pod level, once per one-second window, always. The directory listing is dearer than the reads, so the set of cgroups is listed every 10 s and reused in between (a pod newer than the last listing is counted in its parent's entry until then). The 10 s hold in starved windows too: the sample a stall line is made from is then the listing, inside the same budget, once per interval. A history of 32 samples lets a multi-second stall be measured over its whole length.
  • processes: a rolling baseline every 10 s, one more scan per starved window (that scan is the next window's baseline, so a stall that lasts gets exact per-window deltas from its second line).
  • Each scan has a 250 ms wall-time budget, checked before every file, before every directory listing and after the last, and an entry cap (1024 cgroups, 8192 processes); past either, the field is unavailable rather than a partial sample.
  • No figure exceeds the interval on every CPU of the node. The count is the node's online CPUs (/sys/devices/system/cpu/online, read every 10 s), not the CPUs the proxy may run on. If it cannot be read, no CPU-count cap is applied and the startup line says so (nodeCPUs).

Measured on a developer machine (x86, 20 threads, kernel 6.8), BenchmarkConsumerSample plus strace -c differences between 100 and 300 iterations:

sample entries files system calls time
cgroups, per second 134 cgroups 134 402 (open, read, close each) 2.6 ms
cgroup listing, per 10 s 126 directories 3 getdents64 per directory plus open and close about 7 ms
processes, per scan 705 processes 707 about 3 per process plus 7 for /proc 15.4 ms

Scaled to the 300 processes the issue asks about: about 300 files, 907 system calls, 6.6 ms per scan. The cgroup sample is about 0.3 % of one core at ~130 cgroups. A later run on the same machine under less load gave 1.6 ms for 119 cgroups and 12.2 ms for 561 processes, so read these as 13–19 µs per cgroup and about 22 µs per process. These are workstation numbers; nothing was measured on an arm64 Talos node.

The flag

--stall-top-consumers (default 5, at most 20, 0 turns the consumer sampling off). The chart is not changed and does not pass it, so the default applies on every proxy pod.

The flag set lives in agent/internal/supervisorcmd/supervisor.go, which #1433 was editing in parallel and which I was told to stay out of. I left it alone until #1433 merged, then rebased onto it and added the one registration line and its test.

Tests

cpuconsumers_test.go drives the sampler through an interface (statFS) with an in-memory filesystem: delta arithmetic and exclusivity, ordering and top-N, a process that exits, PID reuse (start time changes), a process started inside the interval, unreadable and malformed entries, cgroup v2 absent, a container's own cgroup namespace, a pod created or removed between samples, the listing cadence, the stall-length baseline, name cleaning and truncation, the time budget and the entry caps, and the stall sampler end to end (starved carries the fields, blocked and busy do not, everything unreadable still logs the stall). One test reads the real /proc and /sys/fs/cgroup.

Red first: 55 small mutations of the implementation, each run against the test that covers it; all 55 fail that test, and the unmutated tree passes. The mutation runs found three tests with no teeth (the fields attached to a blocked epoch's line; a /proc scan paid for a blocked window; a removed cgroup kept in the known set); each was strengthened and then seen red. The flag test was seen red three ways (not registered, default 0, bound to nothing).

Review, all threads fixed and resolved:

  • Round 1 (Copilot, 3): a newly listed cgroup is clamped against what is left of its parent's delta, not the whole of it, so entries cannot overlap; the time budget is checked at every file instead of every 64th; each starved line gets the consumers over its own stall instead of the longest one in the window.

  • CodeQL (go/incorrect-integer-conversion): the microsecond counters are unsigned 64-bit and were converted to a signed Duration unchecked. The conversions now saturate (usecDuration, ticksToUsec), with a test. Fixed, not dismissed.

  • Round 2 (Copilot, 2): a cgroup listing that fails (over the entry cap or the budget) is retried once per 10 s instead of every window, and the last good set stays in use; the startup log prints the capped N.

  • Round 3 (Copilot, 4): the root cpu.stat read is inside the scan budget; a process the baseline scan listed but could not read is remembered and not charged as new (failing the whole scan instead would let one never-readable entry turn the field off for good), and every per-process figure is held to interval x CPUs; the runbook says the interval is the one actually measured and what / is.

  • Round 7 (Copilot, 4), each with a test seen failing first:

    • A node starved in every window never listed its cgroups again, so a pod created during a long incident stayed inside its parent's entry. A report now lists when the 10 s interval is up, once per interval, inside the scan budget.
    • The interval could leave out the beginning of the starvation. It now starts at the inferred start of the wait. In the test, a 500 ms wait seen 100 ms into a window was attributed to /init=50ms over 1000 ms; it is now /podruntime/kubelet=900ms /init=50ms over 2000 ms. A stall older than the history is marked …Truncated=true.
    • The cgroup walk listed one more directory after the budget was spent. The budget is now checked before every listing.
    • The sanity cap used runtime.NumCPU, this process's CPUs, for node-wide counters. On a simulated 4096-CPU node the old code reported the pod at 20000 ms and dropped the kubelet's 1000 CPU-seconds from the list. The cap is now the node's online CPU count, or none when that cannot be read.
  • Round 8 (Copilot, 1), test seen failing first: the sampler was ticked with the ticker's timestamp, which is when the tick was due. A supervisor that was itself off the CPU for 3 s then placed a 3 s wait 3 s too early. The ticker is now only a wake-up and the sampler is ticked with the time of the reading.

A CI failure that is not this change

One test run on this branch failed in TestEnvoyChildSeesItsRestartEpoch/first_start_and_in-pod_hot_restart ("config change did not trigger epoch 1" after 5 s) and passed on re-run. That test's supervisor does not start the stall sampler (StallSampleInterval is 0 in its Config), so none of this change's code runs in it. Timing of the subtest, 20 runs each, four at a time: 0.62–0.63 s with this branch's package and 0.62–0.63 s with main's. The cause is in the supervisor: Run arms the config watcher in a goroutine and forks the first Envoy without waiting for it, so a config written right after epoch 0 starts can precede the watch and is lost. A one-second delay before the watch is added reproduces the same failure at the same line every time. Not fixed here.

Verified locally

  • bazel test //agent/internal/proxy/hotrestart:all //agent/internal/supervisorcmd:all //agent/cmd/proxy-supervisor:all (includes deps_test), scripts/check-proxy-supervisor-deps.sh, the race detector on hotrestart_test, the lint build, make format-check, make gazelle.
  • No new dependency: stdlib only (syscall, os, path, slices, cmp).

Not verified

🤖 Generated with Claude Code

https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

Copilot AI balanced review requested due to automatic review settings October 8, 2026 14:02

Copilot AI 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.

🟡 Changes recommended

Cgroup accounting can overlap, per-epoch logs can use the wrong interval, and scan deadlines are not reliably enforced.

3 open findings
What changed in this PR

Adds bounded CPU-consumer attribution to starved Envoy stall diagnostics.

Changes:

  • Samples per-cgroup and per-process CPU usage.
  • Adds top-consumer fields to starved stall logs.
  • Documents behavior, cost, and interpretation.
File Description
docs/​runbook.md Documents consumer diagnostics.
supervisor.go Adds consumer-count configuration.
stallsampler.go Attaches consumers to stall logs.
cpuconsumers.go Implements CPU sampling and ranking.
cpuconsumers_test.go Tests sampling and integration behavior.
BUILD.bazel Registers new source and tests.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 14:17
@bpalermo
bpalermo force-pushed the feat/1392-stall-top-consumers branch from 81d74a1 to 222d3a2 Compare October 8, 2026 14:17

Copilot AI 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.

🟡 Changes recommended

Failed cgroup relists can trigger costly hierarchy scans every second instead of every ten seconds.

2 open findings
3 resolved since last review

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 14:23
@bpalermo
bpalermo force-pushed the feat/1392-stall-top-consumers branch from 222d3a2 to 66f7c08 Compare October 8, 2026 14:23

Copilot AI 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.

🔵 Needs a closer look

A failed cgroup relist discards the cache and can trigger expensive hierarchy walks every second.

2 open findings

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

…ers over the stall (#1392)

The "envoy thread stall" line said a thread was starved and the node busy,
not who was on the CPUs; #1320 and #1389 both ended there. A line with a
[starved] thread now carries topCgroups / topCgroupsOverMs (cgroup v2
cpu.stat usage_usec deltas, exclusive per cgroup, one entry per pod) and
topProcs / topProcsOverMs (/proc/<pid>/stat utime+stime deltas).

The proxy container is privileged, so it has the host cgroup namespace and
reads the node's hierarchy with today's mounts. It has no hostPID, so the
process scan is skipped and the field says why.

Cgroups are sampled once per one-second window (a delta needs a "before"
and a stall is only known when it is over); the directories are listed
every 10 s. Processes get a 10 s rolling baseline plus one scan per starved
window. Scans are bounded in time and entries, and an error becomes an
"unavailable: <reason>" value: the stall line is never withheld.

--stall-top-consumers sizes the list (default 5, at most 20, 0 = off). The
chart is unchanged, so the default applies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
Copilot AI balanced review requested due to automatic review settings October 8, 2026 14:49
@bpalermo
bpalermo force-pushed the feat/1392-stall-top-consumers branch from 66f7c08 to 69e84f6 Compare October 8, 2026 14:49

Copilot AI 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.

🟡 Changes recommended

Sustained starvation prevents periodic cgroup relisting, and parts of cgroup scanning bypass the documented time budget.

1 open finding
2 resolved since last review

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

…the stall line

Review on #1435. Deltas under one millisecond passed the > 0 filter and
rendered as name=0ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
Copilot AI balanced review requested due to automatic review settings October 8, 2026 15:09

Copilot AI 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.

🟡 Changes recommended

Partial process baselines can report lifetime CPU as an interval delta, and the cgroup root read bypasses the scan budget.

4 open findings
1 resolved since last review

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Comment thread docs/runbook.md Resolved
Comment thread docs/runbook.md Resolved
…et and never reports an unread process as new

The root cpu.stat read now happens inside the 250 ms scan budget, which is
checked right after it.

A process the baseline scan listed but could not read or parse (for a reason
other than having exited) is remembered, and the next scan does not charge it
as a new process with its whole lifetime's CPU. Every per-process figure is
also held to the interval times the CPU count.

The runbook says the reported interval is the one actually measured (the end
of a stall longer than the kept history), and that "/" is what was charged to
the root cgroup itself, not "no cgroup".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
Copilot AI balanced review requested due to automatic review settings October 8, 2026 16:27

Copilot AI 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.

🔵 Needs a closer look

Consumer intervals can omit part of waits that end before the reporting window closes.

0 open findings

4 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Anchor consumer interval to the observed runqueue delta timestamp

agent/​internal/​proxy/​hotrestart/​stallsampler.go:446

The consumer interval is anchored to the end of the reporting window, not to when the wait actually ended. schedstat can expose a long runqueue delta on any 100 ms tick in the window; if a 5 s wait is observed on the first tick, topConsumers later uses closeWindow(now)-5s, omitting up to 900 ms from the start of the real wait and including post-stall CPU instead. Retain when the runqueue delta was observed (for example the earliest sampleTime-dRunq) and derive each epoch's consumer start from that timestamp; add a case where a long wait ends early in the one-second window.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 16:34

Copilot AI 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.

🟡 Changes recommended

Transient cgroup filesystem errors can corrupt sampling baselines and produce false top-consumer attribution.

1 open finding

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 8, 2026 20:09

Copilot AI 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.

🟡 Changes recommended

An aborted cgroup scan can corrupt the retained last-good path set through in-place slice compaction.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

…as it was

readKnownCgroups compacted the known paths in place. A scan that skipped a
removed cgroup and then ran out of budget returned with the backing array
already written over, so the next entry was in the set twice until the next
listing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

Copilot AI 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.

🔵 Needs a closer look

Aggregated runqueue deltas can omit the beginning of multi-wait starvation intervals and misattribute CPU consumers.

0 open findings

1 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Runnable wait start miscomputed from cumulative schedstat deltas

agent/​internal/​proxy/​hotrestart/​stallsampler.go:285

schedstat is cumulative, so one dRunq can contain several waits separated by periods when the thread ran. Subtracting their total from this tick is only the start time when the delta represents one continuous wait. If a wait crossed the window boundary and later waits were charged before this sample (especially after a delayed sampler tick), began can fall inside the window and max(wall, …) then omits the pre-window part and its CPU consumers. Track the beginning of the runnable episode across samples (or otherwise conservatively retain the earliest possible start) rather than deriving it solely from the aggregate delta.

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 8, 2026 20:26

Copilot AI 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.

🟡 Changes recommended

A transient cgroup directory-listing failure can discard the last valid path set and suppress attribution.

1 open finding

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 8, 2026 21:52
bpalermo and others added 2 commits October 8, 2026 18:53
A ReadDir error was taken for a cgroup without children. EIO on the root
during a relist therefore replaced the known set with an empty one, and the
stall line had no cgroups until the next listing. Only a directory that is
gone, or that this user may not list, is a leaf now, and never the root; any
other error fails the walk, which keeps the set of the last listing that
worked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr

Copilot AI 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.

🟡 Changes recommended

Delayed supervisor scheduling can still omit the beginning of an Envoy runqueue stall from consumer attribution.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

…at much earlier

The start of a runqueue wait was the tick it was seen on minus its length.
A supervisor that was itself descheduled sees the wait ticks after it ended,
and the consumers were then taken from a baseline after the wait began: a
3 s wait that ended at 4 s and was seen at 6 s was reported over 3 s, without
the cgroup that held the CPUs in its first second. The start now also goes
back by how late the tick was.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr
Copilot AI balanced review requested due to automatic review settings October 8, 2026 22:20

Copilot AI 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.

🔵 Needs a closer look

The substantial low-level Linux accounting change has not yet been validated in its target cluster environment.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 8, 2026 22:29

Copilot AI 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.

🔵 Needs a closer look

The substantial low-level host sampling logic has not yet been validated in its target cluster environment.

0 open findings

🧠 Review effort: Balanced

@bpalermo
bpalermo merged commit 518b17f into main Oct 8, 2026
31 checks passed
@bpalermo
bpalermo deleted the feat/1392-stall-top-consumers branch October 8, 2026 22:42
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.

supervisor: the Envoy stall line does not say which processes took the CPU

3 participants