notes

The kernel bench harness: why each row is built the way it is

Name: provisional.

An appendix to notes/benchmarks.md. It carries the reasons behind kernel/src/bench.rs and its children under kernel/src/bench/: how the bench boot ends, and why each row's window, workload and gate are what they are. The numbers are on the main page.

How the bench boot ends

--features bench makes the boot diverge into bench::run before the milestone tour. It runs every benchmark in a fixed order, prints machine-readable lines, prints bench: done and halts. It never semihosts. Under HVF the semihosting hlt traps to the guest instead of exiting, so the contract is output-based in both modes: xtask bench owns the QEMU process, watches for the marker and terminates it. The kernel parks in wfi, so a forgotten bench QEMU costs nothing.

Each line is bench: <name> <counter_ticks> <iters>, with the counter frequency printed first; xtask does the division. Warmup iterations run untimed before each measurement, so thread spawn and first-rendezvous costs land outside the window. Every program the boot enters is measured first through trust::require_program, the chain the progenitor would have run; the hashing happens at set-up, outside every timed window.

The two instruments, icount and HVF, and why icount is a coarse tripwire and HVF never gates, are in the main page's first table and in icount drift.

The tick window

TickWindow::open waits for this core's next tick and close panics if another arrived since. A ping-pong row (ipc_rtt, ipc_rtt_el0) is bimodal: each receive either finds its message waiting or blocks for it, and a tick inside the window can flip the loop between the two. Under a free-running tick an unrelated change moves those rows by whole percent. At the bench build's TICK_HERTZ of 1, a window that opens on a tick has a full second before the next, and the close proves on every run that the row fit. The opening tick also fixes a ping-pong's starting mode, so ipc_rtt always measures the receiver-first mode. yield_switch and spawn_reap still do not mask interrupts; this only moves where involuntary switches fall. Ticks are per core, so a thread that changes cores inside the window escapes the check, and a --real --smp run prints a probe line instead.

map_new: preemption masked across the window

The window is about 250 microseconds of guest time and the scheduler tick outside the bench build is 10 ms. Whether a timer interrupt lands inside it depends on the phase the boot left the timer in, which moves when unrelated code changes size. preemption-in-the-window.md has the +26.4% reading that showed it. Masking removes the coin flip: the interrupt is pending at restore and taken there, outside the window, which is why bench-probe: map_new_preemptions can still read 1. Under -icount a masked window also excludes whatever the rest of the machine would have done inside it, which is what the row needs: the row prices mapping a page, not a deschedule in the middle of mapping one. It is safe because nothing on the path blocks (map_new takes only spin locks). A row that means to measure scheduling, or whose body can block, must not mask.

The preemption, fence and mask probes are counts, not durations, so they are probes and never timed rows. A 10% tolerance over a count is not a tripwire, and their interesting values are exact.

rfence_self: one remote fence, on one hart

The 2026-08-15 map_new reading had two rival explanations, remote fences against an over-reporting mask or the mask arithmetic itself, and they differ by orders of magnitude, so one fence's cost decides it (riscv-map-new-and-the-rfence-probe.md). A hart may name itself in an SBI hart mask. The firmware serves that legal call with a local fence, and it exercises the whole ecall-into-firmware path a shootdown pays and a local sfence.vma does not. It must be single-hart: under -icount all harts share one virtual clock, so a second hart's idle wfi dumps quantized time into the open window.

The cycle probe

cycles_per_tick converts every board row from ticks of the reference counter into the cycles the literature uses. It is a probe, never a row, because the rate moves with frequency scaling. Its cycles_per_tick_means line names both halves of the ratio on each architecture, because a rate printed under one name on three machines invites comparison across them. notes/pmu.md has the counters, and the aarch64 filter is calef's ruling A1 of 2026-10-07 UTC. Under emulation the ratio is a fact about the emulator, and the line says so.

The EL0 rows

The os_primitives_benchmarker roles must match fixtures/src/os_primitives_benchmarker.rs. The two crates share no header, so the numbers are hand copies of each other. MAP_EL0_ITERS and MAP_EL0_WARMUP must equal its MAP_ITERS and MAP_WARMUP: the target region has room for their sum plus overhead, and maps past what it funds fail cheaply and skew the row.

SPAWN_EL0_BUDGET is 64 pages on purpose. Each child is split, run and destroyed strictly LIFO, so its pages return to the budget (§16 (object revocation: reclaim the objects a process built)) and only one child lives at a time. That 64 funds a 100-iteration loop, which would need over 1,000 pages without return-to-parent, is itself the proof that LIFO reclaim works.

sink_throughput reports bytes rather than iterations, because bytes compare against a Unix pipe. bench/host/pipe_throughput.rs measures a Unix pipe twice: with the same sixteen-byte writes, which isolates the cost of having no buffer, and with 64 KiB writes, which is what Unix gets. Only the first is apples to apples. The producer holds WRITE and nothing else, as the left of a shell | does.

ipc_rtt_el0_cycles: the round trip in cycles, read rather than converted

A build with cycle_counter_grant beside bench grants the ipc_rtt_el0 client the cycle counter (the per-thread grant of §139 (who may read the cycle counter)) and has it read the counter across the same timed loop. Two probe lines follow the row, which does not change:

bench: ipc_rtt_el0 10643349 5000
bench-probe: ipc_rtt_el0_cycles 35696.65 per round trip (178483253 cycles over 5000 iters; 15.99 per tick across the same 11155207 ticks)
bench-probe: ipc_rtt_el0_cycles_means core cycles at EL0 and EL1 (PMCCNTR_EL0, PMCCFILTR_EL0 0)

That is aarch64 under TCG, so every figure in it is the emulator's. It is the number milestone 101 (the L4 calibration) wants from a board, because cycles_per_tick times ipc_rtt_el0 assumes the core's clock held still between two windows, and this reads one window. The per-tick figure is the check: it should match cycles_per_tick, and where it does not, the core changed frequency between them and this line is the one to believe.

The filesystem rows

Every filesystem row is --real --smp only. Each is device- and interrupt-driven, interrupt timing is not part of -icount's instruction clock, and an icount baseline for them would enshrine that non-determinism.

fs_read is a warm read loop through the confined stack. After the first read it is served from the FS server's memory, like a Linux buffered read from the page cache, so the row is the confined IPC path plus a copy (about 260 ns under HVF, release, 2026-09-26). The device path is what fs_seq_read and its siblings measure, because their 1 MiB file is over the memo's 256 KiB limit. An isolated file-server tax is not measured here: a few hundred nanoseconds of serving layer sits inside a block read's run-to-run spread. The per-hop tax is relay_rtt's, where the gate sits.

fs_throughput prints a row and a MiB/s probe per phase. The row's ns/iter is the per-request latency, which compares with fs_read; the probe compares with another operating system. One transfer is 4096 bytes and cannot be more, because a request carries its payload through the one page the client shares with the server, so these are a request rate in disguise. filesystem-throughput.md has the numbers. The kernel side computes MiB/s in integer hundredths, where the counter frequency and transfer size are both known; a phase whose ticks did not advance is a broken run and reports zero.

The keyboard rows

keyboard_up and keyboard_event time the virtio keyboard driver, and only script/bench --keyboard prints them, on any of the three architectures. That flag attaches the keyboard and a monitor socket. An ordinary bench boot finds no keyboard, prints neither row, and gates nothing new. --keyboard refuses --check, --save and --real, because no baseline holds the rows and a host actor is in the loop.

keyboard_up runs from the queuing of the driver's spawn to its KEYBOARD_UP report, after the kernel has wired the device. That covers loading the image, feature negotiation, queue setup and posting the event buffers, and the host plays no part in it.

keyboard_event sums 16 key presses. Each runs from the kernel delivering the keyboard's interrupt (sched::watched_interrupt_at, a bench-only timestamp in interrupt_notify) to the driver's COMMIT reaching the bench thread, which stands in for the compositor. The wait for the host is outside the window, and under icount an idle guest skips that wait anyway. The kernel prints a cue before each press, and xtask answers it with one sendkey after 200 ms of real time. By then the previous release has had its own interrupt, so every press is one interrupt carrying an EV_KEY and an EV_SYN. Two runs on aarch64 gave identical counts.

Measured 2026-10-24 (UTC) on Linux x86_64 under the pinned QEMU 11.1.2 and nightly-2026-10-24, debug userspace, keyboard_driver as it ships. Ticks are each architecture's own counter (62.5 MHz on aarch64, 10 MHz on riscv64, the calibrated TSC on x86_64), so compare a row only along its line.

keyboard_up keyboard_event, per press
aarch64 55,298 2,107
riscv64 2,712 341
x86_64 535,253 32,978

The per-press counts equal the hand-written column of the trial below on all three. Bring-up does not, because the image and its wiring differ between the two runs.

The same rows timed a trial of the driver on the virtio-drivers crate, which milestone 784 (reuse what the tree wrote) refused (the trial's numbers).

Multi-hart throughput

Every other row is hart-pinned, so it is per-core path length and cannot show the placement of §28 (SMP placement: two random choices at spawn, message-shaped stealing, local wakes). smp_throughput runs N independent workloads and times them by wall clock. It needs more than one hart, and under -icount all vCPUs share one virtual clock and TCG serializes them onto one host thread, so it runs only on the --real --smp boot and never gates. The scaling factor is the solo ns/iter over the all ns/iter.

E1 and E4: one real core

E1 (ipc_thread_scaling) and E4 (app_displacement) are tier A of milestone 134 (the register of measures) (design/roadmap/0134-the-measurements-that-decide.md). E1 asks whether the kernel's own IPC path goes cache-cold as more threads cycle through it; E4 asks what an IPC-heavy kernel costs an unrelated application's working set, Liedtke's actual claim. Both need a real cache and one hart, so every thread contends for the same core's cache. real_single_hart_or_skip checks both. A board card needs single_hart, which parks radon's secondaries, and smp_throughput then self-skips on that card, so the runbook takes two cards (footprint-perturbation.md).

The real-core test is one value: the counter frequency QEMU virt states, 62.5 MHz on aarch64 and 10 MHz on riscv64. NIFE_ACCEL is host-side and never reaches the guest. It fails safe: an unknown machine reads as real. It says "not QEMU virt", not "has a 32 KB L1", so the runbook records which machine a number came from.

SCALE_MAXIMUM_PAIRS is 48, so the sweep stops at 96 threads, where the roadmap says "N from 2 to 128". Read "N" as distinct threads cycling through IPC, as the prediction ("somewhere between 16 and 32 threads") does: 96 is three times past the predicted knee. It stays at 48 although sched::MAXIMUM_THREADS is now 1,024, because the sweep is a published measurement and moving its end would make every later run incomparable. Change it only if the knee moves.

E4 sweeps working sets of 4 to 128 KiB, below, at and above a typical 32 KB L1d and into L2, so it can show whether displacement tracks one cache level. Its low-load condition is 8 pairs, inside E1's flat region, so E1's curve predicts a null result there. Its high-load condition is SCALE_MAXIMUM_PAIRS pairs, past E1's knee (on radon the knee is at 16 threads, 1.68x), which is where it can tell the kernel's path slowing from eviction of the application's cache (cache-experiments.md). The workload yields about 64 times per run in every condition, so a compute-only thread cannot finish before the background pairs run and the conditions differ only in what else is ready. It keeps the minimum of 5 batches, one more than the pipelines' 4, because a batch that long catches more host preemptions.

The IPC topology rows

relay_rtt is a two-hop request (client, relay, backend, relay, client), the shape the FS server and net_stack both use. Its difference from ipc_rtt is what one confined intermediary costs, deterministic and gated, which the real servers' end-to-end paths cannot be under -icount.

broker_rtt is the opt-in rung of milestone 23 (a capability-routed component OS with live replacement) and §41 (the endpoint is the broker): broker interposed so a producer never blocks on an absent consumer. Read against call_reply, the same client and backend with nothing between, the difference is the whole tax, paid on every request in the steady state. The default rung has no row of its own because it has no cost of its own: no process stands in the data path, so its steady state is call_reply.

The x86_64 I/O-bitmap rows

tss_iomap_switch writes the whole 8 KiB bitmap on every resume in both threads, since a real switch-in hook fires whichever thread it lands on. tss_iomap_lazy_switch prices what ships, set_port_range_grant, on the worst case: the two threads disagree (no ports, and COM1's production range), so every switch crosses a holder. tss_iomap_lazy_nop is the common case, both threads holding nothing, where the lazy form costs a load and a branch. Each reads against yield_switch from the same boot. x86-tss-iomap.md has the numbers.