The 43 milliseconds that belonged to the machine

Contents
In four of eight benchmark runs, 64 work-stealing threads took a scheduler’s p99 from about half a millisecond to between 7 and 43 milliseconds, while 24 threads on the same 24 CPUs stayed under 1.5. That was exactly the symptom the question I had designed asked me to explain, so I wrote it into the answer, with a mechanism. In the 32 runs since, with the same code and with the next two versions, it never went above 3 milliseconds. The 43 milliseconds belonged to the machine that evening, not to the scheduler.
Benchmarks that lie, a two-machine edition: how one run ranked tail latencies it could not rank, the tracing that found the tail that does recur, the A/B that took a finding back, and what the same benchmark did on the 64-core server the question was about.
This is a story about measuring, told through one exercise in principal-katas, the systems-design katas I keep: the dag-scheduler kata, a C++ executor for graphs of dependent tasks. It is not a question anyone asked me in an interview. I designed it from work that was actually done: a parallel execution engine for an endpoint security product, which turns every event on a machine into a graph of analysis tasks and has 5 ms at the 99th percentile to finish one. The design itself, and why its dependency counter needs release and acquire rather than relaxed, is in the worked answer. This post is about four days of benchmark runs behind its last section, three on my own machine and one on a rented server, what they let me claim, and what they made me take back. Every number here is in a log in this post’s evidence folder, and every figure reads those logs.
How to read this. Where you see a Predict box, stop and answer it before reading on. The six figures are interactive and draw only recorded measurements, except the one labelled as an illustration. The box marked “safe to skip” holds the arithmetic of a percentile; the main text never depends on it.
What you need to know already: what a thread is, and what p99 means in a sentence (99 % of events were faster). Work stealing, open-loop load and the rest are explained where they first matter.
1. The question, and the machine it ran on
The engine in the brief is modelled on that one. It has up to a million tasks active, 64 worker threads, zero to eight dependencies a task, no global scheduler lock, and a p99 under 5 ms an event. On a security product’s agent the latency is the product: an event that waits is an event analysed late. The kata’s follow-up question hands you a table. A work-stealing scheduler goes from 16 worker threads to 64:
| 16 workers | 64 workers | |
|---|---|---|
| throughput | 850K tasks/s | 1.4M tasks/s |
| CPU utilisation | 88 % | 42 % |
| p50 event latency | 1.2 ms | 900 µs |
| p99 event latency | 4 ms | 18 ms |
| steal attempts/s | 200K | 9M |
Throughput rises, the median improves, and the 99th percentile gets four and a half times worse. Explain why, and redesign the scheduler. The case is real, but the table’s numbers are not measurements from it; I wrote them for the question to illustrate the symptom. Answering it well means reproducing what you can of it, on a machine you have, and saying clearly what you could not.
The scheduler
A work-stealing scheduler gives each worker thread its own queue of ready tasks, a deque. A worker pushes and pops at one end of its own deque, which needs no lock and no contention. When its deque is empty, it becomes a thief: it picks another worker at random and tries to take a task from the other end of that worker’s deque. A failed steal costs the thief a few cache-line reads, and costs the victim too, because the thief pulled the lines holding the deque’s indices away from the victim’s core.
What an idle worker does is the policy the follow-up is about. The kata’s executor has two:
- greedy — an idle worker never sleeps. It loops: try a random victim, pause, try again, and yield its CPU to the OS after every sweep’s worth of misses.
- budgeted — at most a fixed number of workers search at once. The rest sleep, and a searcher that finds work wakes one sleeper to take its place.
A third change is about the tasks, not the threads. Fused arms group eight small leaf tasks into one task, so there are an eighth as many to hand out and an eighth as many to steal.
The workload
Phase 6 of the kata’s benchmark sends events into the scheduler. Each event is a small graph: a chain of four tasks of 100 µs, beside 256 independent leaf tasks of 2 µs, 912 µs of work in all. Events arrive on a schedule, at 13,000 a second (11.9 cores of work) and at 20,000 a second (18.2 cores). The machine has 24 logical CPUs, so the second rate is 76 % of it, and 16 workers cannot keep up with it at all.
Latency is open-loop, and it is measured from when an event was due, not from when the benchmark got round to submitting it. A closed-loop benchmark, which only sends the next event when the last one finished, hides exactly the stalls you care about: while the system is stuck, nothing new is sent, so nothing new is late. This is what Gil Tene calls coordinated omission. Here, if the machine stalls for 15 ms, every event due during those 15 ms is charged for the wait.
Each arm runs for 1.5 s per rate, three times, interleaved with the other arms, and the number reported for a run is the median of those three. A run of phases 0, 4 and 6 takes about four and a half minutes.
The machine
An Intel Core Ultra 9 285K: 8 performance cores and 16 efficiency cores, 24 logical CPUs, one socket, Windows 11, clang 21.1.7, Release. The follow-up’s table is about a 64-core server, probably with two sockets. On this machine 64 workers are 64 threads on 24 CPUs, which is oversubscription: more runnable threads than CPUs, so the OS has to take some off their CPU to run others. That difference matters later, and it is the first thing a fair answer has to say. Section 8 runs the same benchmark on a 64-core, two-socket server, section 9 is about why the difference mattered more than I expected, and section 10 is about production systems that fell into the same traps.
2. One run ranked everything
The first answer I wrote quoted single runs. Its critical-path section, which compares four ways to order a task’s successors, said that the arm resolving successors by rank and running the last one inline “has the best p99 at 90 % on the fork (676 µs)”. Its redesign said “the best arm in both tables is 24 workers, budget 4, fused”, at 462 µs. Its section on starvation said rank priority showed “no systematically worse maximum”. All three were readings of one run each. The runs are not in the repository; the numbers survive in that version of the answer.
Before opening a pull request, I re-ran the benchmark to check the answer still described its code: two full runs, numbers read from the second. In that second run, the arm with “the best p99 at 90 %” had the worst, at 12.5 ms. At 13,000 events/s, four arms beat the one the answer had crowned at 444 µs: 32 greedy workers at 409 µs, and three more at 427 µs. And on starvation, the rank arms had the worse maximum in six of nine cells.
Three claims, three reversals, from one more run. The tempting reading is that the new run was right and the old one wrong. But the new run was also one run. So I ran six more.
Four arms, one graph, 90 % load: which has the worst p99?
The critical-path phase. Each row is one way to order a task's successors. Step through the runs: the medians stay put and the tails re-rank.
The figure is the critical-path phase at 90 % of capacity: four arms, the same graph, the same work. Step through the runs. The medians do not move: every arm’s p50 is 409 µs in every run. The p99s move by more than a hundred times, and the arm with the worst p99 changes from run to run. Across the runs on all three days, every one of the four arms had at least one run above 10 ms on this graph at this load, and every one also had a run of 604 µs or less.
So of the three reversals, one was the re-run’s fluke: over eight runs the rank arms had the worse maximum in three to six cells of nine, about half, which is what chance gives, so the original “no systematically worse maximum” was right after all. The other two were the original run’s. Neither run could rank these arms. Near saturation, a p99 from one run is a sample of the rarest event, not a measurement of the arm.
Transferable lesson: a tail comparison that rests on one run is a coin toss you have not tossed again. Re-running to “check” a tail claim with one more run replaces one coin toss with another.
3. Eight runs, and a pattern I believed
With eight runs of the follow-up’s phase I had ranges instead of points, and most of the follow-up reproduced. Steal attempts did explode: at 13,000 events a second, 5.0 × 10⁷ a second with 16 greedy workers and 1.6 × 10⁸ with 64, 98 % of them finding nothing. Thieves did tax the work they stole from: with 16 idle thieves, one owner pushing and popping its own chain of tasks was four to six times slower per hop than alone. And sleeping hurt: with 64 workers, the textbook budget, half of them allowed to search, had a p99 above 2 ms in every run at 20,000 events/s, where greedy workers mostly stayed under one.
Then there was the follow-up’s own symptom.
Eight runs on one evening, every arm at 20,000 events a second
Each dot is one run's p99. Most arms keep a tight range. Look at 64 greedy workers, then colour the dots by which run of its pair each one was.
At 20,000 events a second, 24 greedy workers, one per CPU, kept their p99 between 480 µs and 1.49 ms in all eight runs. 64 greedy workers did that in four runs, and in the other four their p99 was 26.2, 14.2, 43.2 and 7.11 ms, with the median unmoved at 409 µs. More workers, the same median, a tail tens of times worse: the follow-up’s table, on one 24-CPU machine. Each of those numbers is already the median of three interleaved repetitions, so it is not one bad repetition either.
Predict. The runs came in pairs: the benchmark is run twice and the second run is the one you read, because a freshly linked binary’s first run is slower. Press the figure’s “Colour by place in pair”. What do the four spikes have in common, and what would you check before believing the mechanism below?
Answer
Every spike was the first run of its pair; none of the second runs spiked. A pattern that tidy is a property of how the runs were taken until proven otherwise, and it should have stopped me there. It did not: I explained it.
I wrote it into the answer. Commit
fc6619e says, of 64 greedy workers at
20,000/s: “That is the follow-up’s symptom on one socket: more workers, the same median, a tail tens of
times worse.” And it offered a cause:
The likeliest cause is oversubscription near saturation: 64 runnable threads on 24 CPUs, with 18 cores of real work to do. Sometimes the OS deschedules a worker in the middle of a 100 µs chain step, and the event then waits out a time slice, which on Windows is tens of milliseconds, the size of the spikes.
It is a good story. It has a mechanism, the mechanism has the right size, and it explains why fusing tasks eight to one made the spikes go away (an eighth as many hand-offs that can land on a thread that is not running). It was labelled a hypothesis. It was also exactly the kind of story that makes you stop looking, so I decided to measure it before keeping it.
4. Ask why before believing it
To test “a worker was descheduled in the middle of a task”, the benchmark has to see it happen. It turned out the benchmark already had the instrument, by accident.
A body that spins to a deadline
The tasks in this benchmark do no real work. Each body spins until a deadline, start + work, reading
the CPU’s timestamp counter:
// Returns the clock when it stopped. A thread taken off its CPU mid-body returns late.
inline std::uint64_t burn_from(std::uint64_t start, std::uint64_t ticks) noexcept {
const std::uint64_t until = start + ticks;
std::uint64_t t;
while ((t = TscClock::now()) < until) {}
return t;
}
A body that spins to a count runs late in a way that tells you nothing. Every loop iteration costs a little more under contention. A body that spins to a deadline can only finish late in one way: it was not running when the deadline passed. If the OS takes the thread off its CPU for 20 ms in the middle of a 100 µs body, the loop resumes, reads a clock already past its deadline, and returns nearly 20 ms late. So the time past the deadline is a reading of time spent off the CPU: never more than it, and close to it whenever the stall is long. The benchmark now counts every body that finishes more than 250 µs late as stalled, and records on the event that one of its bodies was.
Where an event spends its time
A late event can also lose its time before any body runs. Each event now carries four timestamps:
- due — when the schedule says it arrives;
- submitted — when the benchmark’s submitting thread handed it to the scheduler, under the injection queue’s lock;
- taken — when a worker took it off the injection queue;
- done — when its last task finished.
An event slower than 2 ms is then attributed. It is stalled if one of its bodies was. It is late if it was taken more than 1 ms after it was due, and late by submitter if more than half of that lateness came before the submitting thread had even queued it. Otherwise it is neither: it started on time, nothing it ran was descheduled, and it simply waited behind other events’ work.
Time the benchmark did not spend running
An illustration, with round numbers. Nothing here was recorded.
Illustration. In the benchmark, a body more than 250 µs past its deadline counts as stalled, and an event is attributed only when it is slower than 2 ms.
The first version of this did not have the submitted timestamp, and “late” lumped two different things together. When the submitting thread itself falls behind, no scheduler can fix it. When the scheduler is slow to take a queued event, the scheduler is at fault. I only split them after the first tracing runs showed late events piling up under one policy and I could not say whose fault they were.
The reducer
Here is the instrument on its own, small enough to paste. It runs 100 µs bodies, each spinning to a deadline, on half as many threads as CPUs, then as many, twice as many and four times as many, and counts the bodies that finished more than 250 µs late:
// A body that spins to a deadline can finish late only by being off its CPU.
// Build: c++ -O2 -std=c++20 -pthread deadline_overrun.cpp -o deadline_overrun
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <thread>
#include <vector>
using Clock = std::chrono::steady_clock;
struct alignas(64) Tally {
long bodies = 0, late = 0;
double worst_us = 0;
};
// Spin until start + work; return how far past that deadline the spin ended.
double spin_to_deadline(Clock::time_point start, std::chrono::microseconds work) {
const auto until = start + work;
Clock::time_point now;
while ((now = Clock::now()) < until) {}
return std::chrono::duration<double, std::micro>(now - until).count();
}
Tally run(int threads, std::chrono::seconds length) {
std::vector<Tally> per(threads);
std::vector<std::thread> pool;
const auto stop = Clock::now() + length;
for (int t = 0; t < threads; ++t)
pool.emplace_back([&per, stop, t] {
while (Clock::now() < stop) {
const double late_by = spin_to_deadline(Clock::now(), std::chrono::microseconds(100));
++per[t].bodies;
if (late_by > 250) {
++per[t].late;
per[t].worst_us = std::max(per[t].worst_us, late_by);
}
}
});
for (auto& th : pool) th.join();
Tally all;
for (const auto& p : per) {
all.bodies += p.bodies;
all.late += p.late;
all.worst_us = std::max(all.worst_us, p.worst_us);
}
return all;
}
int main() {
const int cpus = static_cast<int>(std::max(1u, std::thread::hardware_concurrency()));
std::printf("%d logical CPUs; 100 us bodies, each spinning to its deadline, 3 s per row\n", cpus);
std::printf("%8s %10s %16s %10s\n", "threads", "bodies", "> 250 us late", "worst");
for (int threads : {std::max(1, cpus / 2), cpus, 2 * cpus, 4 * cpus}) {
const Tally r = run(threads, std::chrono::seconds(3));
std::printf("%8d %10ld %16ld %7.1f ms\n", threads, r.bodies, r.late, r.worst_us / 1000);
}
}
On the 24-CPU machine, copied back out of this page and built with clang 21.1.7 at -O2:
24 logical CPUs; 100 us bodies, each spinning to its deadline, 3 s per row
threads bodies > 250 us late worst
12 358432 30 1.6 ms
24 627948 11698 5.4 ms
48 644791 29255 19.8 ms
96 650906 29697 109.6 ms
That is the first of three runs. Across all three, at half a thread per CPU, 9 to 30 bodies out of about 358,000 came back late, by at most 1.6 ms. At one thread per CPU, which is what a scheduler sized to the machine runs, it was 11,698 to 17,263, by as much as 40 ms: one busy thread per CPU leaves nothing for the OS and everything else the machine is doing, so something gets taken off. At two and four threads per CPU, 17,928 to 30,420 bodies came back late, the worst by 110 ms.
Two things follow. The instrument sees what it claims to see. And on a busy machine, descheduling is not rare; it is constant. Hold on to that, because it means descheduling cannot, on its own, explain something that happened on one evening and never again.
5. What the tracing found
Fourteen traced runs on the morning after, and the first result was the one I had not planned for: 64 greedy workers did not spike once. Their p99 at 20,000 events/s stayed between 533 and 640 µs in all fourteen. A finding that does not recur when you look at it closely is a finding about the looking, or about the day, and I did not yet know which. Section 6 settles it. First, what the tracing did find, because it found something that does recur.
Why each slow event was slow, at 20,000 events a second
Every event slower than 2 ms, split by where it lost its time. Bars are shares; the number is how many slow events there were.
Descheduling explains little. At 20,000 events a second, bodies were descheduled in every arm in the figure, 81 to 5,016 of them per run, even at one worker per CPU, because the benchmark’s own submitting thread spins too, so 24 workers plus the submitter already oversubscribe 24 CPUs by one. But in the greedy arms’ larger episodes, the runs where hundreds to thousands of events were slower than 2 ms, a stalled body explains 6 to 22 % of them. The rest are mostly “neither”: events that started on time, ran on CPUs that stayed theirs, and queued behind other events’ work at 76 % load. That is just queueing, and no mechanism needs inventing for it.
The budget’s tail is largely the producer’s. With 64 workers and a budget of four searchers, the
worst arm in most runs, 57 to 87 % of the slow events left the injection queue late, and in more
than half of those the submitting thread had queued the event late. The scheduler had not been slow to
take them. Under a budget, submit takes the injection lock and wakes a sleeping worker, and a wake is
a system call the caller pays for: 3 to 5 µs at the median, and up to 102 µs at the 99th percentile, in
phase 0’s measurement of it across the runs. At 20,000 events a second there are 50 µs between
arrivals. A submitter that pays tens of microseconds for some wakes, and waits for a lock that searching
workers also take, falls behind its own schedule. A real product’s I/O thread would pay the same.
That is a finding about the design, and it is in the answer now: under a search budget, part of the tail is charged to the producer, and a budget that saves CPU by sleeping moves cost onto whoever wakes the sleepers.
6. The A/B that ended it
Fourteen traced runs without a spike left three explanations, and they predict different things:
- The tracing changed the behaviour. It adds a few clock reads and stores on the hot path. If the instrument suppresses the effect, the untraced code should still spike.
- The code changed. Phase 6 had also gained a 48-worker arm, which changes what runs before the 64-greedy arm and how long the phase takes. If that matters, the old code should still spike.
- The machine changed. The eight runs with spikes finished between 19:49 and 20:56 on Saturday evening. Everything since ran from six on Sunday morning on. If that is the cause, neither the old code nor the untraced new code should spike now.
Predict. You have the old binary and the new one with tracing switched off. How do you run them so that whatever the machine is doing cannot favour one of them?
Answer
Alternate them, in the same pairs as the original runs: A, B, A, B, A, B, each one a pair of runs of the same phases. Both binaries then get the same number of first-of-pair runs, which is where every spike had landed, and the same share of whatever the machine does over the hour. Running all of A then all of B would hand any drift in the machine to one side.
So that is what I ran. A was built from fc6619e,
whose code is the code that spiked (the commits between change only prose). B was the current code,
with a switch, KEYSTONE_STALL_TRACE=0, that turns the tracing off. Six pairs, alternated, phases 0, 4
and 6, 55 minutes.
64 greedy workers, every run, grouped by when and what ran
p99 at 20,000 events a second. The first row is Saturday evening. Everything below it ran later, including the very same code.
Neither spiked. A’s 64 greedy workers ranged from 533 µs to 2.99 ms; B’s from 551 to 960 µs. The code that produced four spikes in eight runs on Saturday evening produced none in six runs on Sunday morning, on the same machine, interleaved with its successor. Explanations 1 and 2 predicted spikes, and there were none. The 43 milliseconds belonged to the machine that evening.
What on the machine, I cannot tell you, and I will not guess in the answer. Nothing recorded the rest of the system during those runs: no trace of other processes, no power plan. The one reading the benchmark does take rules out the obvious suspect. Every phase starts by timing a pause instruction, whose cost is a fixed number of cycles, so a higher reading means a slower clock. At the start of phase 6, three of the four runs that spiked read 8 or 9 ns, the machine’s normal; the fourth read 11. The slowest phase-6 start that evening, 16 ns, was in a run that did not spike. It was not a throttled CPU. A’s three runs above 1 ms against B’s none is too thin a difference to call, with six runs a side.
One more batch closes it. On Monday morning I merged the kata’s pull request and ran the merged code,
b854a17, with tracing on: three more pairs.
64 greedy workers stayed between 516 µs and 1.92 ms. That makes 32 runs since Saturday evening, of
three versions of the code, and the worst was 2.99 ms. In those same six runs, the arm that kept
being bad was the budget of four without fusion: the worst p99 in every one of them, from 25 to 68 ms,
and again most of its slow events had started late, more than half of those queued late by the
submitter. A finding that recurs looks like that.
So I took the claim back. Commit 8870e60
replaces the sentence above with this one: “So the spikes were a condition of the machine that day, not a
property of 64 workers, and the 43.2 ms in the table belongs to that day.” The answer now says the
follow-up’s worse tail did not reproduce on one socket, and says what the tracing found instead.
Notice what made the retraction possible. The claim was in a file, with a commit, saying which runs it came from. A finding that lives in a chat message or a slide does not get retracted. It gets repeated.
7. Even the question’s table does not add up
One more measurement claim deserves the same treatment: the one I wrote into the question. Multiply its throughput by its task duration and you get the cores spent running tasks. Multiply its utilisation by its worker count and you get the cores that were busy.
16 workers: 850,000 tasks/s × 450 ns = 0.38 cores of task work, 88 % × 16 = 14.1 cores busy
64 workers: 1,400,000 tasks/s × 450 ns = 0.63 cores of task work, 42 % × 64 = 26.9 cores busy
The question's table: cores busy, against cores running tasks
Arithmetic on the numbers the question gives. If its tasks take 450 ns, almost none of the busy CPU was spent running them.
Task bodies are 2.7 % of the busy CPU with 16 workers and 2.3 % with 64. Something the table does not name was 97 % of the cost before a single thief was added. Steal attempts rise 45 times for 4 times the workers while utilisation falls from 88 % to 42 %, so the extra thieves were mostly asleep, not spinning. And the median improving while the p99 worsens means two populations of events: ones that stayed on one worker and got faster, and ones that crossed a wake or a remote steal and got much slower.
None of that is a reason to dismiss the question. It is a reason to read its table the way I should have read my own eight runs: as evidence that has to add up before it can be explained. On this machine the regime is different anyway: 64 greedy workers spent 21 cores doing 11.9 cores of work, 57 % useful against the table’s 2–3 %.
8. Sixty-four cores, two sockets
Everything so far ran on a 24-CPU desktop chip, and the follow-up’s table is about a 64-core server.
“It did not reproduce on one socket” is an honest answer, but only half of one. So I rented the machine
the question was about: an AWS c6i.metal, which is a whole server rather than a slice of one. It has
two Intel Xeon Platinum 8375C sockets of 32 cores each, and ran Ubuntu 24.04 and clang 18.1.3. I
switched simultaneous multithreading off, so that one CPU was one core. The benchmark sizes itself to
the machine it runs on. On 64 CPUs its two rates are 13,000 events a second (11.9 cores of work, as
before) and 53,000 (48.3 cores, 76 % of the machine, as 20,000 was of the desktop).
On this machine the symptom showed up, and not on one evening. In all ten runs of the merged code, at 13,000 events a second, 16, 32 and 64 greedy workers had a p99 of 429 µs, and 64 workers with a budget of four searchers had 3.43 to 3.98 ms. Every run had more workers and a worse tail. At 53,000 a second the 64-worker arms’ p99s ran from 5.6 ms to 1.48 s, and most of those runs fell seconds behind, while 128 greedy workers, two per CPU, kept up in every run at 3.25 to 3.98 ms.
A finding that recurs ten times in ten is the opposite of Saturday evening, and I was ready to write it down. One number did not fit. Greedy workers never sleep, so 64 of them on 64 CPUs should keep all 64 busy. At 13,000 events a second the process used between 33 and 57 cores.
Predict. A pool that never sleeps, on a machine with as many CPUs as it has workers, uses 40 of them. Where are the other 24, and what would you measure to find out?
Answer
Nothing inside the process can tell you. Its own CPU time says how much it ran, not where. Every CPU’s busy time can, and the kernel keeps it. I added a table that reads it around each run, and a switch that pins worker i to CPU 1 + i, wrapping round, and leaves CPU 0 to the benchmark’s submitting thread. With the OS placing the threads, 1 to 28 CPUs sat idle at 13,000 events a second, while the pool’s threads were switched off their CPUs 0.6 to 1.8 million times a second. The OS had doubled threads up on some CPUs and left others empty. The pool is 64 spinning workers plus a spinning submitter, 65 threads for 64 CPUs, and they had not spread out.
Then the comparison, run the way section 6 taught: five pairs, the same binary each time, OS-placed and pinned, alternating which went first. Ranges over the five runs of each:
| arm, load | OS-placed: p99 · idle CPUs | pinned: p99 · idle CPUs |
|---|---|---|
| 64 greedy, 13,000/s | 429–452 µs · 1–28 | 429 µs · 0 |
| 64, budget of 4, 13,000/s | 3.25–3.98 ms · 0 | 475–588 µs · 0 |
| 64 greedy, 53,000/s | 0.74–1.11 s · 3–20 | 429–497 µs · 0 |
| 64, budget of 4, 53,000/s | 0.53–1.20 s · 0–15 | 1.36–1.40 ms · 0 |
| 128 budgeted, 13,000/s | 4.16–4.52 ms · 0 | 4.34–5.60 ms · 0 |
Pinned, 64 greedy workers held p99 under half a millisecond at 76 % of the machine. Their capacity was 67,356 to 67,544 events a second, against 36,835 to 61,197 when the OS placed them. In every one of the five pairs, pinning took the budget-of-four arm from milliseconds to about half of one, and 64 greedy workers at 53,000 a second from p99s of most of a second to under half a millisecond. Most of the follow-up’s symptom, on the follow-up’s own machine, was where the operating system put the threads.
What pinning did not remove is the scheduler’s own, and it is the more interesting answer to the question. 128 budgeted workers, two per CPU, some of them asleep, stayed at 4.2 to 5.6 ms either way. Three in four of their slow events had a task body taken off its CPU mid-run, the stall that section 4’s instrument measures: a woken worker landing on a CPU that another worker is using. That is oversubscription multiplying the cost of waking sleepers, and it does not care where threads are pinned. And one arm got worse when pinned. With tasks fused eight to one, the budget of four went from 425 to 429 µs placed to 1.27 to 1.36 ms pinned, in every pair. I have a guess, that a pinned sleeper must wake on its own idle CPU, as likely to be on the other socket as not, where the OS would have run it next to the thread that woke it. It was not measured, so it stays a guess, in the answer too.
The lesson is about section 6’s last line. “A finding that recurs looks like that” was true, and it is
not enough. The tail recurred in ten runs out of ten, and it still was not the scheduler’s.
Recurring tells you a finding is real. It does not tell you whose it is. Commit
c4da707 puts all of this in the kata’s
answer, with the pinned and OS-placed ranges side by side.
9. Know the machine it runs on
One binary, one question, three answers. On a 24-CPU desktop chip, more workers did not make the tail reliably worse. On a 64-core server with the threads placed by the OS, they did, in every run. On the same server with the threads pinned, mostly they did not. None of the three is wrong. Each is a fact about the code and a machine, and an answer that does not name its machine has only told you about the code.
These are the facts about a machine that changed an answer here, and what each one cost:
- How many CPUs, and how many threads want them. Count every thread that wants a CPU, not the pool size. That includes the producer, the I/O threads and the OS. On the desktop, 64 workers on 24 CPUs were oversubscribed by design. On the server, 64 workers and one spinning submitter were oversubscribed by one thread, which nobody had designed, and it left up to 28 CPUs idle. Even one busy thread per CPU is too many for a machine that is doing anything else. The reducer in section 4 saw 11,698 to 17,263 bodies come back late at exactly that.
- What kind of cores. The desktop chip is hybrid: performance cores and efficiency cores. Across 34 runs, moving a cache line from one performance core to another took a median 36 ns, and to an efficiency core 72 ns. A thief on the wrong kind of core pays twice as much to look.
- How many sockets. On the server a cache line took a median 100 ns to reach another core on the same socket and 176 ns to reach one on the other, 1.76 times as long. With workers spread over both sockets, a thief that picks its victim at random picks one on the other socket about half the time.
- Simultaneous multithreading. With it on, that server reports 128 CPUs, and the benchmark would have sized itself to 128. I turned it off so that a CPU meant a core. I did not measure it on, and an answer measured either way should say which.
- Where the OS puts the threads. On the server this was the largest effect of all, and nothing measured inside the process could show it. Only every CPU’s busy time, read from outside, could.
- What else the machine is doing. Saturday evening, from section 6. Nothing recorded it, so it has no cause, only a date.
Two habits follow. Record the machine with the results. The kata’s run script writes the CPU layout, the sockets and the cores on each, the SMT state, the frequency governor, the clock source and the compiler into every bundle, before any result depends on them. Measure the machine before you measure on it. The benchmark’s first phase times a pause instruction, the clock, the skew between cores’ clocks and a cache line’s trip between cores, and prints them, so that every number after it can be read against them. That phase is how the second socket’s 1.76 got into this post.
And when you cannot run on the machine the question is about, say so in the answer, as the first thing, the way section 1 tried to. “The follow-up’s worse tail did not reproduce on one socket” was true. It was also, it turned out, mostly a sentence about one socket.
10. The same failures, in production
None of this needed a rented server to happen. Every trap in this post has a public, production-sized twin, and reading them costs less than repeating them. These are other people’s reports, quoted from their sources. I did not measure any of them.
Idle cores beside waiting threads, in the Linux kernel itself. In “The Linux Scheduler: a Decade of Wasted Cores” (Lozi et al., EuroSys 2016), four bugs “cause the scheduler to leave cores idle while runnable threads are waiting for their turn to run”. The cost was “in the range 13-24% for typical Linux workloads”, reaching “138× in some corner cases”. One of them, Overload-on-Wakeup, was triggered by a commercial database “configured with 64 worker threads (1 thread per core)” on a 64-core, eight-node machine. A woken thread was placed only among its own node’s cores, so it could wake “on an already busy core and miss opportunities to use idle cores on other nodes”. Fixing it sped up the full TPC-H benchmark by 13.2 %. Worse for anyone measuring, in that TPC-H workload each occurrence “lasted only a few hundreds of milliseconds – too short to detect with tools like htop, sar or perf”. They found it by checking one invariant: “make sure that ready threads are scheduled on available cores”. That is the question section 8’s per-CPU table asks. Their kernels were Linux 3.17 to 4.3, and I do not know whether anything like those bugs left the c6i.metal’s CPUs idle. The point is that the invariant is worth checking, because it was broken in production for years and nobody’s dashboard showed it.
Two pools on one set of cores, at PostHog. In “Untangling Tokio and Rayon in
production” (Matheus Batista, 2026), a
feature-flag service’s “p99 and p95 response times would spike up to 2.5 seconds”. Rayon’s par_iter
ran inside request handlers on the Tokio runtime, so “the worker thread, which was supposed to be
dealing with I/O, sat blocked waiting for Rayon”. On top of that there was “100% thread
oversubscription”: “we created twice the number of threads we should have”. The pods “weren’t running
out of CPU on average, they were getting throttled in short bursts”. The fix handed the work to Rayon,
bounded how much could be in flight, and sized the thread pools to the CPU actually granted. The
result went “from 2.5 second spikes to a flat 94 ms line”. That is section 9’s first item, counting
every thread that wants a CPU and not just the pool, found in production.
A runtime that sized itself to the wrong machine: Go before 1.25. From the Go team’s post on
container-aware GOMAXPROCS (Pratt and Amedee, 2025):
“From Go 1.5 through Go 1.24, GOMAXPROCS defaulted to the total number of CPU cores on the machine”,
and “Go was unaware of CPU limits set by orchestration platforms”. The result: “throttling can cause
substantial tail latency impact”. Go 1.25 made it “take into account its container environment by
default”. The machine a program can see is not always the machine it runs on. Phase 6 sizes itself to
the CPUs it sees, so it carries the same assumption.
The search budget, from the people who shipped it. Tokio’s “Making the Tokio scheduler 10x faster” (Carl Lerche, 2019) gives the rule the kata’s “budgeted” arm copies: “The max number of searchers is half the total number of processors”. The same post explains why idle workers have to be kept quiet: “Overeager notification results in a thundering herd problem.” That rule was made for an async runtime’s tasks. In this benchmark’s 2 µs CPU-bound leaves, it was the policy whose p99 passed 2 ms at 20,000 events a second in all eight of the first desktop runs. A scheduling rule fits a workload and a machine. It is not a law, and the only way to know whether it fits yours is to measure it there.
11. Guidelines for a good measurement
Every guideline here is something this one benchmark got wrong at least once, or got right only because of an earlier mistake.
Before you measure
- Know the machine, and write it down. CPUs, kinds of core, sockets, SMT, governor, compiler, and what else is running. An answer is about a machine (section 9).
- Measure the instrument first. The clock’s resolution, the timer’s own cost, and whether cores agree on the time. The kata’s check on that agreement judged a single estimate, whose spread across the server’s runs, −103 to +35 ns, was wider than the ±100 ns it was checked against. It failed once in nineteen runs, and now takes the median of five.
- Measure what the user sees. Latency runs from when an event was due, in an open loop, so a stall is charged to every event it delayed (section 1).
While you measure
- Interleave the arms inside a run, so that the clock’s drift is shared by everything compared.
- Repeat, and report tails as ranges. Medians were stable to the histogram bucket. p99s near saturation moved by more than a hundred times, and a single run could rank the arms either way (section 2).
- Alternate whole builds or configurations in pairs: A, B, A, B. That stops the machine’s day from favouring one side. It settled Saturday’s spikes (section 6), and it is how OS placement and pinning were compared (section 8).
- Look outside the process. Read every CPU’s busy time beside your own. A pool that never sleeps while CPUs sit idle has a placement problem, not a scheduling one. When placement is not what you are measuring, control it: pin, or leave room.
Before you explain
- A finding has to recur before it gets a mechanism, and recurring is not enough to say whose it is. Saturday’s spikes did not recur. The server’s tail recurred ten times in ten and was mostly the OS’s.
- Attribute before you explain. A body that spins to a deadline measures its own descheduling. Timestamps at due, submitted, taken and done split latency into the producer’s, the queue’s and the work’s (section 4).
- Split every “late” by whose fault it was. Under a budget, more than half the late events were the producer’s (section 5).
- Check the arithmetic of any table you are asked to explain, including your own (section 7).
After
- Correct the source, with the runs that corrected it. The kata’s answer has been corrected three times now, each in a commit that names its runs. A finding that lives in a chat message gets repeated, not retracted.
- Publish evidence that checks itself. Every number in this post is in
claims.csv, and a script recomputes each one from the raw logs.
Apply it to your own benchmark:
- Does the result say which machine it came from: CPUs, kinds of core, sockets, SMT?
- Did the benchmark measure its own clock before anything depended on it?
- Is open-loop latency measured from when the event was due?
- Does any tail number you quote come from more than one run? From more than one day?
- Do the runs come in pairs, and have you looked at first against second?
- When you compare two builds or settings, did they alternate, or run one after the other?
- Did you count every runnable thread, not just the pool, against the CPUs?
- Did you read every CPU’s busy time, not only your process’s?
- Can your workload tell you which events were descheduled, and which started late?
Run it yourself
The code as measured on the desktop is PR #19, merged as
b854a17.
Section 8’s per-CPU table and pinning switch are PR #20,
merged as 0356867.
For the desktop measurement:
git clone https://github.com/shaia/principal-katas
cd principal-katas && git checkout b854a17
cd dag-scheduler-kata
cmake -S cpp -B cpp/build -G Ninja -DCMAKE_BUILD_TYPE=Release
cmake --build cpp/build
cpp/bin/solution --phase 0,4,6 # run it twice and read the second
cpp/bin/solution --phase 0,4,6
KEYSTONE_STALL_TRACE=0 cpp/bin/solution --phase 6 # the same phase, tracing off
On Windows the binary is solution.exe, and the kata’s
README
pins the compiler. Phase 6 sizes itself to your machine: 16, 32 and 64 workers, your CPU count C, and 2C,
at 12 cores of work and at 0.76 C. Table c at the end of phase 6 is the attribution.
Things worth trying:
- Run phase 6 four times in a row at different times of day and plot 64 greedy’s p99. Does your machine have an evening?
- Run the reducer while compiling something large. How late do bodies get at one thread per CPU?
- Lower
rate_for(0.76 * c)inbench/scaling.cppto 0.6 C. At what load do the greedy arms’ slow events disappear? - In
scheduler.hpp, makenotify_workskip the wake when the submitter is the caller. How much of the budget’s late-by-submitter tail goes away, and what does it cost? - On a Linux machine with two sockets, check out
c4da707, where phase 6 also prints every CPU’s busy time andKEYSTONE_PIN=1pins the workers. Thencpp/remote/run.sh --smt-off --pin-ab 5runs section 8’s comparison and bundles the logs. How many of your CPUs sit idle under 64 greedy workers?
Further reading: the kata’s question, §10 of the C++ answer, which this post backs, and the session text the question came from, preserved whole. For more benchmarks that lied: the cache that was working perfectly, where alternating pair by pair first earned its place, the 1.5× that three benchmarks tried to hide, and the thermal drift in Your profiler said dispatch.
A note on the evidence
What I observed. Every number in this post is in a log in the evidence
folder, which lists each run with when it
finished and which code it ran: 40 runs of the kata’s benchmark on one Intel Core Ultra 9
285K (24 logical CPUs, Windows 11, clang 21.1.7, Release) between 2026-11-21 and 2026-11-23, every one
of which passed its own correctness checks. Section 8’s 21 runs are in their own folder with their own
manifest: one AWS c6i.metal (two Xeon Platinum 8375C sockets, SMT off, 64 CPUs, Ubuntu 24.04, clang
18.1.3) on 2026-11-24, ten of the merged code and eleven of
71d8cd4, which added the per-CPU table and
the pinning switch. Every scheduler invariant held in all of them. One run failed phase 0’s check that
the two sockets’ clocks agree to within 100 ns, which section 11 is about. claims.csv lists each
number this post states, and check_claims.py recomputes every one of them from the raw logs.
What I computed rather than measured. The percentile arithmetic in the box, and section 7’s arithmetic on the question’s table, whose numbers I wrote to illustrate the question. The deadline figure is an illustration of the mechanism, not a recording. Section 10’s cases are other people’s reports, quoted from the sources it links. I measured none of them, and the claims checker does not cover them.
What I did not measure. What happened on the desktop on Saturday evening: nothing recorded the rest of the system, so the spikes have no cause here, only an absence afterwards. On the server: why the OS left CPUs idle (the per-CPU table shows that it did, not why), why pinning made the fused budget’s tail worse (section 8’s explanation is a guess), anything with SMT on, and whether fewer workers than cores would have beaten 64. Section 9’s “half the time across the socket” is arithmetic on random victims, not a count of steals.
What I corrected. The kata’s answer, three times: once when eight runs replaced single-run tail
rankings (fc6619e), once when the claim that
commit added, that 64 workers reproduce the follow-up’s tail, did not survive the A/B
(8870e60), and once when the 64-core server
showed that the tail does reproduce there, and is mostly the OS’s placement
(c4da707). Between the server’s two
batches, I also believed for an hour that the budget-of-four tail was the scheduler’s, because it
recurred in every run. The pinned runs took that back before it was written anywhere.
What I would still change. Record the system alongside the benchmark: the other processes, the power plan, and the OS’s own scheduling trace for the run, so that the next evening like that one has a cause and not only a date. On Linux, record where each thread ran, not only how busy each CPU was. And give phase 6 more than three repetitions per arm: a median of three hides one bad repetition, but not two.
The 43 milliseconds that belonged to the machine