roadmap BUILT

629. The context-switch statistic stops costing the switch path

(Number provisional until the merge queue lands it.) The drift study in #1486 (drift since the 2026-09-26 floors, still open) found that milestone 126 (the procps package)'s machine_statistics::context_switch() cost about 3% of a yield on every ISA. It was one fetch_add per schedule() on the machine statistics page, reached through a pointer chase from the core id. calef ruled on 2026-10-03 at 01:41 UTC, on #1486, to move the count onto the timer tick so the switch pays nothing. This lane measured that no form both keeps the published meaning and costs the switch nothing, and wrote the options up on #1489. calef then ruled option B there on 2026-10-03: keep the count exact and make it as cheap as it can be.

What was built

What the statistic promises, and who reads it

The CONTEXT_SWITCHES word is "context switches this core has made": exact, monotonic, per core. vmstat prints it as cs, per second since boot, summed over cores. That is the column Linux and FreeBSD print, and both count exactly on the switch: Linux with rq->nr_switches++ in __schedule (kernel/sched/core.c), FreeBSD with VM_CNT_INC(v_swtch) in mi_switch (sys/kern/kern_synch.c). Both were read from upstream sources on 2026-10-03. top and free do not read it. The meaning is unchanged. A reader can now see the count up to one tick late, which the protocol already allowed for every other word.

Measured

Debug icount (what the gate measures) on all three ISAs, and riscv64 release icount built by hand the way #1486 did it. The before column is main at 5af5c712c with the old toolchain pin. The free column is the counter stubbed out, which equals option A below. B was re-measured on d6a902a9b with nightly-2026-10-02 and matched to the tick on every row shown.

row ISA before free built (B)
yield_switch riscv64 192,465 (+2.87%) 187,103 188,345 (+0.66%)
ctx_switch riscv64 526,420 (+2.65%) 512,838 516,252 (+0.67%)
ipc_rtt riscv64 174,034 (+1.56%) 171,359 172,963 (+0.94%)
spawn_reap riscv64 35,576 (+1.75%) 34,965 34,990 (+0.07%)
yield_switch aarch64 1,163,476 (+3.32%) 1,126,053 1,136,488 (+0.93%)
ctx_switch aarch64 3,153,501 (+3.15%) 3,057,234 3,084,758 (+0.90%)
ipc_rtt aarch64 1,056,406 (+0.89%) 1,047,088 1,049,353 (+0.22%)
spawn_reap aarch64 220,189 (+0.36%) 219,391 219,622 (+0.11%)
yield_switch x86_64 19,860,248 (+2.89%) 19,302,080 19,418,061 (+0.60%)
ipc_rtt x86_64 17,603,719 (+1.58%) 17,329,713 17,387,730 (+0.33%)
spawn_reap x86_64 2,886,003 (+0.35%) 2,876,069 2,926,145 (+1.74%)
yield_switch, release riscv64 12,920 (+4.53%) 12,360 12,480 (+0.97%)
ctx_switch, release riscv64 59,616 (+2.08%) 58,404 58,715 (+0.53%)
ipc_rtt, release riscv64 11,980 (+2.39%) 11,700 11,760 (+0.51%)

x86_64 has no ctx_switch row. On riscv64 a tick is 100 instructions, so a switch costs, in instructions at -O0 and in release: 134 and 14 before, 31 and 3 now. That is about 77% of the cost #1486 found, recovered.

The tick pays for the copy. Measured with a temporary bench row calling sched::count_tick 10,000 times with interrupts masked, it went from 888 to 1,068 instructions at -O0 on riscv64, and from 52 to 60 in release. aarch64 went from 928 to 1,111 and x86_64 from 884 to 1,063. That is under 20,000 instructions a second per core at 100 Hz. script/fastpath-footprint is unchanged or smaller on all three ISAs: riscv64 ipc_fastpath went from 6,052 B to 6,032. script/icount's handler span did not move, because the statistics half runs after the timer is re-armed.

Follow-on

BUGS

Index row

vmstat's cs stays an exact per-core count, but the switch now adds one to its own PerCpu block and the tick publishes the total. That cuts the statistic's cost from about 3% of a yield to under 1% on every ISA, and from 14 instructions a switch to 3 in release.