Skip to content
amarbarosysPublic

About

Per-instruction CPU stall profiler for desktop, laptop and Android, in Mojo

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

corewait

check latest release license checks

Per-instruction CPU stall profiler for desktop, laptop and phones: hardware-counter samples joined to the disassembly, with a cause on every row. Devices and counters: docs/DEVICES.md.

corewait show --gui on the pointer-chase fixture: run line, cause shares, hottest rows

Capabilities

Capability Command Platform
Record a program under the profiler corewait record desktop OUT -- ./bin x86-64 Linux with perf (Zen 4 desktop, Skylake laptop over ssh); stat.tsv has the wall time (elapsed_us)
Record a phone, no root corewait record flip OUT -- ./bin Android arm64 over adb with simpleperf: Z Flip4 (Cortex-X2/A710/A510), Xiaomi (Oryon), OnePlus (A76)
Record a Fire TV Stick corewait record firestick OUT -- ./bin Fire TV Stick 4K 2nd gen, 32-bit ARM (Cortex-A55), adb
Cross-build the probe for a device corewait build flip host to the device ISA, no Mojo runtime on the device; the NDK from CW_NDK, ANDROID_NDK_HOME or the newest under /opt/android-sdk/ndk, checked first
Profile a Python or server process corewait record desktop OUT --dso auto -- python job.py x86-64 Linux; --pid PID samples a running process; only the sampled symbols are disassembled
Size a short kernel automatically corewait record desktop OUT -- ./short (prints auto: CW_DIV=N, R runs) perf devices and phones; under 1000 cycles samples; CW_AUTO=0 or an explicit CW_DIV turns it off; Fire TV Stick UNVERIFIED
Add a command as a plug-in corewait NAME ARGS runs libexec/corewait-NAME any; --help lists the plug-in's # corewait NAME comment lines
Read the cause of every row corewait show OUT any recording; dep is a latency-bound loop chain, a WARN line names a too small sample
Cause table only corewait show OUT --causes --fn SYMBOL any recording; the cause shares of one function
Show the window corewait show OUT --gui Linux desktop, needs mojo-imkit next to this repo
Package energy per hash corewait show OUT --hashes N local x86-64 Linux perf record, RAPL powercap; mJ/hash with 3 decimals, per run; a WARN line when the recording has no energy
Speed of light vs memory bandwidth corewait bw desktop all devices; then show OUT --bytes B --bw GBPS (bytes per run over the wall time of the stat pass; both flags or neither)
Compare two recordings corewait diff OUT_A OUT_B any two recordings of one binary; clone suffixes (.constprop.N, .llvm.N) do not split a function
Budget gate for CI corewait budget OUT FILE any recording; exit 1 on a breach; dep shares changed with the cause rule, re-measure old budgets
Add repeated recordings of one binary corewait merge OUT DIR... any recordings of one binary and one set of periods (else FAIL merge); CW_DIV=20 for a 40 ms kernel; runs summed
Samples inside a dumped JIT buffer corewait jit OUT DUMP.txt x86-64 Linux, RANDOMX_JITDUMP style dump
Live gate on every device ./check-live.sh all desktop and laptop LIVE OK with five probes, flip LIVE OK with four; oneplus chase reads dep (Limits); a device not attached prints PENDING
Hot source lines corewait lines OUT any recording of a binary with line tables (cc -g, mojo build -g1)
Annotated source listing corewait lines OUT --file SRC same

Detail: docs/api.md for every flag, docs/DEVICES.md for devices and counters.

corewait build DEV                            build/cwprobe-DEV: the probe cross-compiled for the device (no Mojo runtime on it)
corewait record DEV OUT [--dso LIB|auto] [--no-stat] [--pid PID] -- [K=V ...] BINARY [ARGS...]   samples.tsv + stat.tsv + objdump.txt + meta.txt in OUT
                                              --dso: report one loaded library of a host process such as python (auto, or a script BINARY: the one with the most cycles);
                                              dsos.tsv ranks all of them. --no-stat: no second run. --pid: sample a running server for as long as BINARY (`sleep 60`) runs
corewait pick OUT LIB|auto                    report another library of OUT/perf.data without a new run
corewait show OUT [BINARY] [--top N] [--causes] [--fn SYMBOL] [--k K] [--bytes B --bw GBPS] [--hashes N] [--gui]
                                              one row per instruction: cycles, mem/fe/br events, cause, text;
                                              --hashes N: with a local perf record, the RAPL package energy line (J, W, mJ/hash for N hashes)
                                              --bytes/--bw add a speed-of-light line: achieved GB/s over the device bw (docs/DEVICES.md)
                                              --gui: the same tables in a window (picture above), build/corewait-gui (imkit), built
                                              only when mojo-libs/libs/imkit is checked out next to this repo; the CLI never needs it
corewait diff OUT_A OUT_B [--top N]           per-row cycle delta, rows of same-named functions aligned by LCS on the text with register names masked, any size
                                              (O(nm) table up to 40M cells, then Hirschberg: memory linear in the rows; CW_ALIGN_CELLS moves the switch)
corewait budget OUT FILE                      FILE lines "fn cause max-percent"; exit 1 on a breach (a CI gate for kernels)
corewait jit OUT DUMP.txt [NAME]              OUT/NAME/ (default jit): the samples inside a dumped JIT code buffer (RANDOMX_JITDUMP), one function per label;
                                              then `corewait show OUT/NAME`
corewait merge OUT DIR...                     add repeated recordings of one binary (CW_DIV=20 shortens the sampling periods for a 40 ms kernel)
corewait stat DEV OUT                         OUT/stat.tsv again from OUT/stat.raw (an event the CPU did not count is left out)
corewait bw DEV                               sequential-read GB/s with 1, 2, 4 and all concurrent stream probes
probes/cwprobe.mojo                           chase | stream | fmachain | branchy | busy, ~0.5 s each, one dominant cause
check.sh                                      CPU-only gate: build, record smoke, golden fixtures, mutant, 1M-row speed, bad input, register roles
check-live.sh DEV|all [freeze]                records the five probes on a device, checks each top cause and bw vs stream

Devices (desktop laptop xiaomi flip oneplus firestick) are one table at the top of corewait; the remote ones need CW_LAPTOP_SSH or CW_XIAOMI_ADB, CW_FLIP_ADB, CW_ONEPLUS_ADB, CW_FIRESTICK_ADB (environment, or ~/.config/corewait/devices.env). The Fire TV Stick is a 32-bit ARM userland: its probe is probes/cwprobe32.c. A phone is locked through $CW_LOCK_DIR/phone-SERIAL.lock (default ~/.cache/corewait), taken by record and bw; the laptop is used only in ~/corewait. fixtures/DEV-PROBE/ holds committed recordings and fixtures/golden/ the frozen tables (FREEZE=1 ./check.sh rewrites them, ./check-live.sh DEV freeze re-records a device).

ComfyUI or any Python job: corewait record desktop OUT --no-stat -- .venv/bin/python job.py, or --pid SERVER -- sleep 60 while a prompt runs. Under gpu-wait pass flags, not env: gpu-wait run --room cpu -- corewait .... Read dsos.tsv first. A BLAS or OpenMP pool spins while idle and tops it with no real work (one spin loop of blas_thread_server held 30 % of a cKDTree run): set OPENBLAS_NUM_THREADS=1 or OMP_WAIT_POLICY=passive as K=V, or pick the library you care about. Only the symbols that hold samples are disassembled: a record --no-stat --dso of libLLVM.so (172 MB) took 3.6 s, and 30.7 s with CW_OBJDUMP=full, the whole-file pass (7800X3D, load average 33).

How a row gets its cause (src/corewait.mojo, header): a sample lands a few instructions after what stalled, so the cycles and memory (branch-miss) events of a row go to the nearest preceding load (branch), up to K rows back (--k 0 turns that off). Then, first match: mem, branch, dep, fe, busy. An event counts for a row only with 10 samples of it and at least half as large a share of the event as of the cycles; mem also needs one memory event per 2500 cycles of the row (fewer cannot explain a fifth of it). dep is a row of a loop whose loop-carried chain is latency-bound. A loop is a branch row whose target is at or before its own address in the same function; its body is target to branch. A register read before it is written in the body, and written in it, is carried; the body is walked once per carried register with fixed latency classes (int ALU, mov, lea, shift, logic 1; multiply 3; FP and FMA 4; a load on the chain +4; divide and square root 15; a call clears the chain). The loop is latency-bound when the longest chain is at least 2 and above a quarter of the body rows, and then every row whose innermost loop it is reads dep (unless mem or branch matched first). Measured on the desktop: a popcnt loop at ipc 4.57 is busy 100 % (the old rule said dep 99.9 %), a scalar FP sum at ipc 0.46 is dep 100 % (it said busy 87.7 %). The destination-reading forms count as reads of the chain: x86 two-operand forms, vfmadd, vpdp*, vpmadd52*, vpternlog*; AArch64 fmla fmls mla mls sdot udot saba uaba bsl bit bif movk ins. A zero idiom (xor eax, eax) reads nothing. The run: line comes from the whole-run stat.tsv, not from the samples. When the selected cycles are fewer than 200 samples a WARN line says that rows under 10 samples carry no cause.

An objdump.txt is read in 8 MB chunks and an instruction line is cut to 4096 bytes: llvm-objdump prints a branch target's whole mangled symbol, so a Mojo kernel's file holds 1.4 MB lines (the kinestride smooth recording: 1.1 GB, 14,601 lines). diff of two of them: 4.5 s and 59 MB, was 36 s and 5.7 GB and then a refusal.

Mojo binaries work with no fix. On Gonzales build-amd/gonzales (72 MB text) the CPU phases of the bathroom scene (--gpu --spp 1 --resolution 64x64, measured 2026-11-16) resolve to named functions, and two records agree on the top 5: bvh::_bvh_split 50.2 % and 54.0 %, host_textures::_load_host_texture 27.8 % and 25.0 %, then build_bvh2_node, pbrt_parser::finalize_scene, __init__::main (merged: 52.0 % and 26.5 %). record took 3.8 s at 566 MB peak RSS (21 MB objdump), show 0.19 s at 70 MB, merge 2.3 s at 55 MB. show --top N prints the fn table and the instruction rows; --causes prints only the cause table. Under gpu-wait pass the full path of ROCm tools (/opt/rocm/bin is not on the job's PATH) and --mem 8.

Source lines

corewait lines OUT [--top N] [--fn SYMBOL] [--k K] [--file SRC] turns the rows of show into source lines. It asks llvm-symbolizer (shipped with the llvm-objdump corewait already needs) for the file and line of every instruction address of OUT/objdump.txt, with inlined frames, keeps the answer in OUT/lines.tsv, and prints two tables: line (top N source lines: cyc%, cycles, dominant cause, the share of every cause, file:line, source text) and inl (cycles per innermost inlined function). --file SRC prints SRC once with cyc% and cause in the margin. The line cycles add up to the total of corewait show OUT --causes for the same selection (checks/lines.sh, step b).

$ clang -O2 -g -fdebug-prefix-map=$PWD=. kernel.c -o kernel        # fixtures/lines/kernel.c
$ corewait record desktop OUT -- ./kernel
$ corewait lines OUT --top 2
line    cyc%       cycles  cause    mem%    fe%   dep%    br%  busy%  file:line  source
       66.5%   1595000000  mem     100.0    0.0    0.0    0.0    0.0  ./kernel.c:13  p = t[p]; /* HOT-MEM */
        9.9%    238000000  branch    0.0    0.0    0.0  100.0    0.0  ./kernel.c:18  for (long i = 0; i < n; i++)
inl     cyc%       cycles  cause   innermost function
       66.5%   1597000000  mem     chase
       18.2%    438000000  branch  flip

Proven (2026-11-29, desktop): on fixtures/lines (two inlined kernels in one function) the hottest line is the pointer-chase line with cause mem, the random-branch line reads branch, and both inlined kernels are named. A Mojo probe built by hand with mojo build -g1 gives line-level rows too: probes/cwprobe.mojo chase, 96.3 % on the p = a[unsafe_offset=Int(p)] line (a standard-library range.mojo line takes 3.1 %).

Limits:

  • It needs line tables in the binary (cc -g, mojo build -g1). With none it prints FAIL lines: no line info in BINARY and leaves no lines.tsv.
  • The binary of the record (hostbin in meta.txt) must be the recorded one, checked by sha256, until lines.tsv exists; after that the record is self-contained.
  • A row moved by the skid rule (--k) is counted on the line of the load it moved to, not the line of the instruction that was sampled.
  • Inlined frames are as good as the DWARF: the compiler's own line 0 (compiler-made code) stays as file:0, and a -g1 Mojo build names the def and the statement, not a column.
  • Measured on the desktop only. Not measured: lines on a laptop, phone or Fire TV Stick record.

Docs

  • docs/api.md: every subcommand and flag, exit codes, output files, environment variables.
  • docs/design.md: layout, data flow, memory model, what was rejected.
  • docs/DEVICES.md: devices, counters and measured bandwidth.

Limits

Proven: the rule reads busy on a popcnt loop (ipc 4.57) and dep on a scalar FP sum (ipc 0.46), both recorded on the Zen 4 desktop (fixtures/lcd/busy, fixtures/lcd/fpsum); fmachain stays dep, chase and stream mem, branchy branch on all five frozen devices; the AArch64 accumulate forms are proven on synthetic fixtures (fixtures/idiom/a64rmw, fixtures/lcd/neon), not yet on a phone recording of a new kernel. What the cause rule cannot see:

  • A chain through a call: a call clears every latency, so a loop that waits on a callee reads busy.
  • A chain through memory at -O0: a value stored and loaded each iteration is not a register, so it is not carried.
  • A dependence carried by the caller's loop: a kernel body without its own loop (an inlined step called per element) has no chain to walk.
  • Fixed latency classes: one number per class, not per CPU. A popcnt or a pmulld is 1 and 3 here, not 3 and 10, and the 15 cycles of a divide hold for no particular core. Flags (an adc chain) and a second destination (ldp, a post-index writeback) are not tracked.
  • A loop over 4096 rows is not walked, and an indirect jump ends no loop.
  • fe without a ground-truth probe: no committed kernel is front-end bound by construction, so the fe rows are a counter reading, not a verified cause.

Recording and devices:

  • OnePlus: chase reads dep, not mem. raw-stall-backend-mem reads 0.0 % on that phone, so only bus reads count, about 7 samples per 0.5 s at period 1000000, under the 10-sample floor. Not fixed: no phone was attached to prove a shorter period.
  • The bw step of check-live.sh compares two timings taken apart; on a loaded desktop it failed 2 of 3 runs (load 20 to 31).
  • Fire TV Stick and Xiaomi: the live gate and automatic sizing are UNVERIFIED (absent).
  • merge refuses recordings of another binary or other periods; an auto-sized recording needs an explicit CW_DIV to merge.
  • Phone elapsed_us includes the process start (simpleperf total test time).
  • Disassembly is by sampled symbol range; a stripped library with samples next to a section boundary takes the whole-file pass.

For coding agents

skills/corewait/SKILL.md teaches an agent the loop: record, read the cause mix, narrow to one function, change one thing, record again, diff, then lock it with budget. It gives the five commands that cover most work, a table of what to do for each cause, and the mistakes that cost a turn (skid, the sample noise floor, short kernels, Python hosts, phones). Copy or symlink the folder into the agent's skills directory (for Claude Code: $HOME/.claude/skills/corewait or a project's .claude/skills/corewait).

License

Apache-2.0, see LICENSE and NOTICE. Copyright 2026 AMARBARO · amarbaro.org labs.

About

Per-instruction CPU stall profiler for desktop, laptop and Android, in Mojo

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages