You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(supervisor): a starved Envoy stall line names the top CPU consumers over the stall (#1392) - #1435
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:
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.statusage_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).
The inferred start of a wait can be up to one sampling tick (100 ms) late, because the wait may have ended up to one tick before it was seen. The baseline is the sample at or before the start asked for, which absorbs that unless the true start falls within one tick before a one-second sample boundary.
The node's CPU count was read from sysfs on a developer machine only; that the proxy container sees the node's /sys/devices/system/cpu/online is expected (sysfs is not namespaced for it) and was not checked in a pod.
…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
…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
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.
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.
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
…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
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
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.
Closes #1392
What changes
The supervisor's
envoy thread stallline 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:This line is an illustration of the format. It is not from a cluster: the change has not run on a cluster.
topCgroups<cgroup path>=<n>ms, largest first, at most--stall-top-consumers(5). CPU time from cgroup v2cpu.statusage_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).topCgroupsOverMstopCgroupsTruncatedtrue, 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>msfrom/proc/<pid>/statutime+stime. Only in the host PID namespace.unavailable: <reason>[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/procscan.What the proxy pod can read (read from the chart, not from a node)
privileged: true,hostNetwork: true, nohostPID, no/procor/syshostPath mount.internal/cri/server/container_create.goat v2.2.4: "cgroupns is not used when running in cgroup v1 mode or in privileged"), so/sys/fs/cgroupin the proxy container is the node's hierarchy. This is the source the feature relies on.hostPID/proclists the container's own processes. The code detects that (PID 2 is notkthreadd), makes no scan, and says so in the field. It lights up by itself if the DaemonSet ever setshostPID: 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.
cpu.statper 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.unavailablerather than a partial sample./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),
BenchmarkConsumerSampleplusstrace -cdifferences between 100 and 300 iterations:getdents64per directory plus open and close/procScaled 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.godrives 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/procand/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
/procscan 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 signedDurationunchecked. 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.statread 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:
/init=50msover 1000 ms; it is now/podruntime/kubelet=900ms /init=50msover 2000 ms. A stall older than the history is marked…Truncated=true.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
testrun on this branch failed inTestEnvoyChildSeesItsRestartEpoch/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 (StallSampleIntervalis 0 in itsConfig), 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:Runarms 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(includesdeps_test),scripts/check-proxy-supervisor-deps.sh, the race detector onhotrestart_test, the lint build,make format-check,make gazelle.syscall,os,path,slices,cmp).Not verified
/init,/system/*,/podruntime/{kubelet,runtime},/kubepods/...) is taken from the issue and from the cgroup series talos-main: worker-04 saturates for 2–4 s outside any proxy handoff — 28 of 40 mesh_dns probe timeouts in the 10-08 soak, on a 30 s phase that follows the kubelet/cAdvisor scrapes #1389 quotes, and the host-cgroup-namespace behaviour from containerd's source; neither was read from a pod./sys/devices/system/cpu/onlineis expected (sysfs is not namespaced for it) and was not checked in a pod./podruntime/kubeletat the top of the :12/:42 seconds would confirm the scrape, and a pod UID there would name the second load.🤖 Generated with Claude Code
https://claude.ai/code/session_01NArCRTfMPZkw4Y97U9Gdsr