Expand description
How much CPU a check’s process tree is spending — the second half of
“is this silent check stuck, or just quiet?” (ADR-0008, hooks.liveness).
The silence budget used to be the whole test: a tool that printed nothing
for amont.idleTimeout was killed. vitest without a terminal prints its
summary and nothing before it, so a suite with every test passing looked
exactly like a hang. What a hang does NOT do is burn CPU, so this module
measures that, and the caller keeps a silent check alive while its tree is
measurably working.
Three facts shape everything below, each measured, not assumed:
- A sum over LIVE processes goes down while a suite works. A test
runner that forks a worker per file loses each worker’s CPU the moment
it exits. So every process is counted WITH the children it has already
reaped (Linux
cutime/cstime, macOSri_child_*), which only grows. - Reaping moves CPU from child to parent. A child credited while alive
would be credited again when its parent’s reaped-children counter jumps.
Trackersubtracts a vanished process’s last known total from its nearest surviving ancestor, so old work is never counted twice — however late the reaping, at any depth. - macOS reports mach ticks, not nanoseconds. On Apple Silicon one tick is 41.67 ns; read as nanoseconds a busy suite looks ~42x idle. The timebase is applied on every read.
No process is spawned and no crate is added (the crate is dependency-free,
decisions:ADR-0020): Linux reads /proc/<pid>/stat, macOS calls libproc,
which std already links. Everything else gets Snapshot::Unavailable
and the caller falls back to the silence-only rule.
A snapshot is BOUNDED — a process cap, a retry cap and a wall deadline
(Limits) — and anything that hits a bound is Snapshot::Partial,
which earns no credit. The figures are a heuristic answering “is anything
working?”, not CPU accounting.
Structs§
- Id
- A process, as distinct from its pid: pids are reused, and a reused pid
must not inherit the old process’s CPU.
startis the kernel’s own start stamp (Linuxstarttime, macOSri_proc_start_abstime) — compared for equality only, never converted to wall time. - Limits
- The bounds on one snapshot. The sampler runs on its own thread, so these protect nothing about the ceiling clock; they keep one look at a huge or churning tree from turning into an unbounded one.
- Proc
- One process in a snapshot: who it is, whose child it is, and the CPU it and every child it has reaped have used, in nanoseconds.
- Tracker
- Turns successive snapshots into windows of work, remembering which processes it has seen so a reparented worker keeps counting.
- Window
- How much work one window between two complete snapshots showed.
Enums§
- Observation
- What one sample told the caller.
- Read
- What
readfound at a pid. - Snapshot
- What one look at the tree produced. Only
Completeis ever measured from: a snapshot that stopped short would read a missing process as zero work, which is the one error this module must never make in the “stuck” direction.
Constants§
- PARTIALS_
BEFORE_ RESET - How many partial snapshots in a row the tracker spans before it gives the baseline up: past this, “the next complete one” is not coming.
- SUPPORTED
- Whether
snapshotcan measure anything on this platform at all.
Functions§
- gain_ns
- The CPU spent between two complete snapshots.
- parse_
proc_ stat - Parse one
/proc/<pid>/statinto aProc. Bytes, not a string: the command name is up to 15 arbitrary bytes and may hold), spaces, a newline or non-UTF-8, so the fields are found after the LAST). After it: state [0], ppid [1], utime/stime/cutime/cstime [11..=14], starttime [19]. cutime/cstime are printed signed; a negative one is 0. - snapshot
- One snapshot of the tree under
rooton this platform. - walk
- Walk the tree under
root, plus everyseenprocess still alive, withinlimits. Platform code supplies how to list a pid’s children and how to read one pid; the walk, the bounds and the reuse guard live here so they are tested on every OS.