Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Busy-spin loop

One thread, pinned to one CPU, never sleeps. Each turn it polls every source once, does the application’s work, and fires due timers. Everything that can block or take long runs on another thread.

The turn

stepcallbounded by
1. network I/Oyour socket or kernel-bypass poll: receive, run the handlers, flush sendsthe backend’s read burst
2. Aeron insubscription.poll_fn(handler, FRAGMENT_LIMIT)FRAGMENT_LIMIT
3. Aeron outpublication.offer of what the handlers queuedstops at the first retryable error; the rest waits for the next turn
4. timersone TSC read (quanta) and a comparethe timer’s own work

The application is one struct the loop hands every event to. The loop owns the sources, the clock and the timers; the struct owns the state.

Animation, in words: two turns of the ring, with illustrative counts. Turn 1: network I/O reads two packets (at most the read burst), the handlers run and queue m1. Aeron in has five fragments waiting but FRAGMENT_LIMIT is 3, so it reads three and the other two stay in the image; the handler queues m2 and m3. Aeron out commits m1, then m2 gets BackPressured, a retryable error, so the loop stops offering and keeps m2 and m3. Timers read the TSC once and compare: nothing is due (clocks). Turn 2: the socket has nothing, so the read returns at once; Aeron in reads the two leftover fragments; Aeron out commits m2 then m3, in queue order; a timer is due and its work runs. Never in the loop: allocation (reserved before it), blocking calls, connects and TLS handshakes (a helper thread), and host checks (the housekeeping thread), those threads on a CPU other than the loop’s. Why it wins: each step is bounded and back-pressure carries over instead of waiting; on one Azure Cobalt VM with helpers off its CPU the loop measured p50 0.02 µs, p99.99 0.03 µs, max 4.81 to 5.52 µs, the max being the hypervisor’s 3.5 to 5.5 µs floor (shared CPUs). When it loses: it burns a whole CPU even when idle, and a cpu: 1 pod puts the helpers on the loop’s CPU. The animation shows order, not timings.

Never inside the loop

neverwhyinstead
a blocking call: file or socket I/O outside the poll, DNS, sleep, a lock, a channel recvthe thread stops until it returnsbefore the loop, or on the housekeeping thread
a connect or TLS handshakewaits on the peer and costs CPU each timea helper thread that dials and hands the connected socket over
poll_blocking, or add_* with a timeoutbusy-waits for the media driverbefore the loop; in it, the async poller’s poll() once a turn
allocationthe allocator can lock and faultreserve before the loop (VecDeque::with_capacity)
logging that formats and writesa write blocks when stdout is fulla bounded non-blocking queue to a logging thread
host checks, ThrottleWatch, FaultWatch, ClockWatcheach reads a file or asks the kernelthe housekeeping thread
Instant::now() or SystemTime::now() for durationsa vDSO call eachquanta::Clock (the TSC), calibrated before the loop (clocks)
creating a threadit inherits the loop’s CPU mask: it would run on the loop’s CPUbefore the loop pins itself
waiting for a retryable Aeron error to clearstalls every other sourcekeep the message, retry next turn

Start-up order

A new thread inherits its creator’s CPU mask. So the loop thread moves to the helper CPU first, creates every other thread there (housekeeping, the Aeron client conductor, logging), and only then pins itself to its own CPU.

podthe loopthe helpers (housekeeping, aeron-client, logging)
cpu: 1the only CPUthe same CPU: give production pods 2
x86 with SMT, cpu: 2one hyperthreadits sibling
Graviton, cpu: 2one corethe other core
exclusive CPUs plus the node’s shared CPUsthe exclusive CPU (ERGON_ISOLATED_CPUS)the shared CPUs, where the process starts (shared CPUs)

Housekeeping

The checks that must run again while the loop runs, every few seconds, on a thread off the loop’s CPU. Each reads what it opened before the loop:

watchfindsfix
ThrottleWatch (T9)the container’s CPU quota stopped ita Guaranteed pod, whole CPUs, the static CPU manager (checklist)
FaultWatch (T19)the loop took a page faultmlockall and write every buffer once before the loop
ClockWatch (T11, T12)time sync or the kernel’s clock source changedtime sync on the node (clocks)
  • Open the FaultWatch for the loop’s thread before the loop starts.
  • A loop that has not warmed up faults on its first turns: expect T19 warnings for them.
  • The other checks run once, before the loop (where the checks run).

The Aeron calls one by one: rusteron-client.