Skip to content

cli: add stax stalls — automatic work-gap detector - #8

Merged
fasterthanlime merged 1 commit into
mainfrom
cli-stalls-detector
Aug 7, 2026
Merged

fasterthanlime merged 1 commit into
mainfrom
cli-stalls-detector

Conversation

@fasterthanlime

Copy link
Copy Markdown
Collaborator

"it stalled a whole second, just show me that." No manual markers needed.

What it does

stax stalls calls the existing timeline RPC (which already buckets wall-time across all threads), computes the median per-bin on-CPU ns, flags bins below a threshold of median (default 20%), merges consecutive flagged bins into stall windows, filters by minimum duration (default 100ms), and ranks by duration. Each result prints a ready-to-paste flame command:

$ stax stalls --run 3
# stax stalls · 201 buckets × 195ms · 38.97s recorded · median 59ms on-CPU/bin · threshold 20%
# 3 stall(s) totalling 974ms (2.5% of recording)

  #1  390ms at t=0.000s
       on-CPU 9ms (8% of median), target: 0 ns
       stax flame --window 0.000..0.390

  #2  390ms at t=0.584s
       on-CPU 9ms (8% of median), target: 0 ns
       stax flame --window 0.584..0.974

  #3  195ms at t=38.965s
       on-CPU 0ms (0% of median), target: 0 ns
       stax flame --window 38.965..39.160

Copy the flame command, paste it, see what the process was doing during the freeze. Done.

Why not "sort off-CPU intervals by duration"

A render freeze produces the opposite signature from a parked thread. The main thread keeps running (on-CPU samples accumulate, often on a hot spin) while target-span throughput collapses to zero. Sorting off-CPU intervals by duration surfaces chronic background starvation (always present, not the stall) and is blind to an on-CPU busy-spin or a throughput collapse.

The timeline-bucket approach catches all three:

  • on-CPU busy-spin (main thread spinning, producing samples but no progress)
  • throughput collapse (target spans go to zero)
  • parked-thread stall (everyone off-CPU)

…because all three manifest as "total process on-CPU + target work per bin dropped below a fraction of normal."

Design

The detection algorithm (detect_stalls) is a pure function of &[TimelineBucket], extracted from the RPC glue so it is unit-testable without a server. Five tests cover:

  • Single-gap detection in steady throughput
  • Adjacent stalled-bin merging into one window
  • Short-gap filtering below --min-duration-ns
  • Duration-descending ranking (longest stall first)
  • Zero-throughput guard (process never ran → don't report everything as stalled)

Verification

  • cargo test -p stax green (35 tests, including 5 new stall tests)
  • Exercised live against a binnacle render recording: correctly detects startup gaps (t=0–0.39s, on-CPU 8% of median — process in GPU teardown and thread spawning) and end-of-recording tail, with flame commands that drill into the exact window.

Usage

stax stalls                          # default: threshold 20%, min 100ms
stax stalls --threshold 0.1          # lower bar: flag bins under 10% of median
stax stalls --min-duration-ns 50000000  # only stalls ≥ 50ms
stax stalls -n 5                     # top 5
stax stalls --run 3                  # against a saved/historical run

"it stalled a whole second, just show me that." No manual markers.

`stax stalls` calls the existing timeline RPC (which already buckets
wall-time across all threads), computes the median per-bin on-CPU ns,
flags bins below a threshold of median (default 20%), merges consecutive
flagged bins into stall windows, filters by minimum duration (default
100ms), and ranks by duration. Each result prints a ready-to-paste
`stax flame --window <start>..<end>` command so you immediately see
what the process was doing during the freeze.

Why a work-gap detector, not "longest off-CPU interval": a render freeze
produces the opposite signature from a parked thread. The main thread
keeps running (on-CPU samples accumulate, often on a hot spin) while
target-span throughput collapses to zero. Sorting off-CPU intervals by
duration surfaces chronic background starvation (always present, not the
stall) and is blind to an on-CPU busy-spin. The timeline-bucket approach
catches both: any bin where total process throughput drops below a
fraction of normal counts, regardless of which threads are on or off
CPU at the time.

The detection algorithm (`detect_stalls`) is a pure function of
`&[TimelineBucket]`, extracted from the RPC glue so it is unit-testable
without a server. Five tests cover: single-gap detection, adjacent-bin
merging, short-gap filtering, duration-descending ranking, and the
zero-throughput guard.

Verified live against a binnacle render recording: correctly detects
startup gaps (t=0–0.39s, on-CPU 8% of median — process in GPU teardown
and thread spawning) and the end-of-recording tail, with flame commands
that drill into the exact window.
@fasterthanlime
fasterthanlime merged commit 996fb50 into main Aug 7, 2026
2 of 4 checks passed
@fasterthanlime
fasterthanlime deleted the cli-stalls-detector branch August 7, 2026 08:33
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.

1 participant