Skip to main content

Module affinity

Module affinity 

Source
Expand description

Serve-process CPU affinity control (WARATAH_SERVE_CORES).

§Behavior

  • Unset / off / false (case-insensitive; 0 is core zero, not an off token): no affinity syscall is ever made. This is the DEFAULT for every subcommand, serving and batch alike — the process runs on the stock scheduler and the storage worker pool runs unpinned at available_parallelism() width. A serving subcommand (start, standalone) announces the off state on stderr once; batch tools stay silent.
  • A core-list spec (6, 4-7, 2,3,6-8): opt-in confinement for any subcommand. (On Linux the sched_setaffinity calls are best-effort: a set outside the process’s cpuset degrades to unpinned.)

§Why the default is off (the 0.4.0 disconfirmation)

Pinning was originally default-on: an unset env auto-selected every physical core of one L3 domain (CPPC-ranked, deterministic) and pinned threads 1:1 onto it. The motivation was real — pre-0.4.0 small hot queries were bimodal (lubm-100 q13 hot passes ~1.4 ms for a few executions, then settling at ~0.65 ms; Oxigraph, same corpus, flat at ~0.5 ms), workers pinned onto distinct physical cores mitigated it, and SMT-sibling sets did not. But the fixed-resident storage worker pool (waratah_storage/src/worker_pool.rs) removed the per-call spawn_blocking thread churn that caused the original disease, and re-benchmarking 0.4.0 measured static pinning as strictly harmful on a quiet machine: q13 latencies gained a spike tail (30 spikes per 1,800 iterations vs 2 unpinned; p99 0.90 vs 0.51 ms) because 30+ runtime, storage-pool, and blocking threads queue on 8 pinned cores; q02 hot p50 382 ms pinned vs 284 ms unpinned; bulk load and concurrent-engine throughput worse; and a static 16-logical-CPU mask recreated the original bimodality verbatim by forcing SMT-sibling packing whenever more than 8 threads were runnable — the unpinned scheduler avoids sibling packing adaptively, a static mask cannot. The hardware mechanism behind the original bimodality (SMT sibling contention vs private-cache re-warm on migration vs CPPC boost effects) was never discriminated by an instrumented experiment; the shared-L3-re-warm explanation once written here is withdrawn (contradicted by full-machine-set mitigation and the unified L3). Auto-selection machinery was deleted with the flip; explicit core lists remain the opt-in.

§What a resolved set does in this process

  • the runtime is built with worker_threads = <set size>, and ONE start hook pins every newly-spawned runtime thread. Tokio delivers on_thread_start to the WORKER threads and to the BLOCKING-POOL threads alike (the blocking pool clones the runtime’s hook and runs it at the top of every blocking thread — verified against tokio 1.53.x blocking/pool.rs; the blocking pool runs the synchronous RocksDB scan closures). The policy is slot_pin_target: today EVERY thread takes cores[slot % len] from a process-global spawn counter, so workers (first len slots) land 1:1 on distinct set cores and blocking threads (later slots) wrap back onto worker cores — a blocking scan thread is permanently paired with one worker’s core. Whether that pairing helps or hurts is part of the open mechanism question above; the policy stays untouched until the instrumented experiment reports.
  • Pinning is best-effort: a sched_setaffinity failure (e.g. a container cpuset that excludes the requested cores) is logged and the thread simply stays unpinned.

serve_pin (a progressive self-pin called at the top of every scan closure) existed between the feature’s landing and its first audit. It was designed under the assumption that tokio does not deliver the start hook to blocking threads — false, so its fast path hit on every invocation and it never pinned anything in any shipped build. Deleted rather than kept as a second pinner because its narrowing rule (freeze the thread onto the core it first ran on) is precisely the unvalidated behavior the mechanism experiment is meant to evaluate.

In start mode the partition/registry children are confined by the LAUNCHER: confine_child attaches a pre-exec hook that narrows each child’s initial thread to the same set before exec, so every thread the child creates afterwards (tonic workers, blocking pools, RocksDB background compaction) inherits the mask — without any cooperation from the child binary. The set is also written into the child’s environment as an explicit spec so a future in-child runtime hook resolves to the same cores.

Structs§

CoreSet
A validated set of CPU ids the serve process may run on.

Enums§

EnvSpec 🔒
What the raw WARATAH_SERVE_CORES value asks for.
PinTarget 🔒
What the start hook does to one newly-spawned runtime thread. The enum is the seam for the blocking-thread policy decision: adding a variant forces the hook’s match (and the policy tests) to handle it.

Constants§

MAX_CORE_ID 🔒
SERVE_CORES_ENV
Core-set spec for the serve runtime, e.g. WARATAH_SERVE_CORES=4-7.

Statics§

SERVE_CORES 🔒
Resolved once per process by init_serve_cores; read everywhere else.

Functions§

build_serve_runtime
The CLI’s tokio runtime: the same multi-thread + io + time runtime #[tokio::main] builds, except when serve_core_set is Some — then the worker pool is sized to the core set and ONE start hook pins every newly-spawned runtime thread. Tokio delivers the hook to the worker threads AND to blocking-pool threads (the blocking pool clones the runtime’s after_start callback — see the module docs), so both populations pin through slot_pin_target.
confine_child
Confine a not-yet-spawned child to set, launcher-side: a pre-exec hook (Linux) narrows the child’s initial thread to the set before exec, so every thread the child creates afterwards — runtime workers, blocking pools, RocksDB background compaction — inherits the mask, without any cooperation from the child binary. The set is also written into the child’s environment as an explicit spec so the child agrees with the launcher. The hook is best-effort: a sched_setaffinity failure (spec outside the child’s cpuset) is swallowed and the child keeps its inherited mask rather than failing the spawn.
init_serve_cores
Resolve and cache the serve core set for this process. Call exactly once, before build_serve_runtime. Unset means off for every subcommand; serving=true (the serving subcommands start / standalone) only makes the off state announce itself on stderr — batch tools stay silent.
parse_core_id 🔒
parse_core_set
Parse a core-set spec: single cores and inclusive ranges, comma separated (6, 4-7, 2,3,6-8), order-insensitive, duplicates collapsed and ranges merged. Rejects empty elements, non-numeric or reversed ranges, and core ids above the kernel’s cpu_set_t width.
parse_env_spec 🔒
pin_to_single_core 🔒
resolve 🔒
Resolve the serve core set for this process. Unset and off/false resolve to no set — the default for every subcommand; serving=true only makes the off state announce itself on stderr (batch tools stay silent). A valid core-list spec opts in; an invalid spec warns once and runs unpinned.
serve_core_set
The resolved serve core set for this process: None when off (unset — the default — or an off/false token or an invalid spec), Some for an explicit core-list spec.
set_affinity 🔒
slot_pin_target 🔒
The pin policy for spawn slot slot of a resolved set. Current behavior, unchanged since the feature landed: EVERY runtime-spawned thread — worker or blocking-pool, both run the start hook — takes cores[slot % len] from a process-global spawn counter. Worker slots 0..len land 1:1 on distinct cores; blocking threads take later slots, which wrap onto worker cores (permanent worker/scan-thread pairing — see the module docs for the open policy question).