[PR #4625] [Engine] STRATA_POOL_SPIN_US=0 means zero: a parked worker with no hot stage goes to the kernel sleep #5118

Open
opened 2026-11-07 16:32:51 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4625
Author: @GoodSlopBadSlop
Created: 10/29/2026
Status: 🔄 Open

Base: main ← Head: f-pool-spin-zero


📝 Commits (3)

  • 32d0824 [Engine] STRATA_POOL_SPIN_US=0 means zero: a parked worker with no hot stage goes to the kernel sleep
  • f8464bb [Engine] pool_tasks_test: what a park at STRATA_POOL_SPIN_US=0 costs is measured and printed, and asserted only when asked
  • 07115a0 Merge branch 'Niko1221:main' into f-pool-spin-zero

📊 Changes

3 files changed (+180 additions, -1 deletions)

View changed files

📝 CMakeLists.txt (+27 -0)
📝 src/kernels/cpu/pool.cpp (+11 -1)
📝 src/kernels/cpu/pool_tasks_test.cpp (+142 -0)

📄 Description

Problem and result

STRATA_POOL_SPIN_US=0 did not mean zero. A parked worker waits kSpinBeforeSleep (20 ms) before it sleeps, and the park loop reads the clock once every 1024 pauses, so a worker parked with the knob at 0 still ran 1,024 strata_cpu_pause() calls before it blocked on sleep_cv_.

Before: with the knob at 0 a parked worker ran that pause stage and then slept. After: a hot stage of 0 skips the pause stage and blocks at once. Every other value of the knob, and the 20 ms default, takes the same branch and the same clock cadence it took before.

Measured on this box at f8464bb8, everything at -j 1: with the knob at 0 a parked worker costs 47 ms, 27 ms and 32 ms of CPU per 1,000 park phases (8 workers), against 151 ms, 153 ms and 147 ms for the same test built from main's pool.cpp, with an asserted bound of 1.00 of the cost of the same number of plain pauses. The three runs at this head pass (0.68, 0.31, 0.46); the three runs of main's file fail (1.96, 1.98, 1.95). At the built-in 20 ms default both builds cost about the same: 2,019 ms and 2,008 ms here, 2,028 ms and 2,030 ms on main's file.

The change is compiled only in the builds it was built and measured in: CMake defines STRATA_POOL_SPIN_ZERO for Linux with the CPU engine or the CUDA engine (CMakeLists.txt:2012-2016), and everywhere else src/kernels/cpu/pool.cpp:1001-1005 keeps spin_hot a constant true, which folds away into the park loop of the last release. Checked mechanically, at -j 1, with each configuration's own compile command for that file and the flag removed: pool.cpp from ffa80d73 and the one in this branch give a byte-identical .text and .rodata in the CPU configuration (74,859 and 121 bytes), in the HIP configuration (74,132 and 121) and in the SYCL configuration (100,311 and 512); with the flag defined the same source becomes 75,179 bytes, so the comparison is not comparing two copies of one object. No Windows, macOS/Metal, HIP or SYCL build compiles a different park loop from the one it compiles today.

The second commit adds the only kind of check that can see the difference, because the pauses change no output: the CPU time of a park. It is opt-in (-DSTRATA_POOL_SPIN_US_MICROBENCH=ON, off by default), registered under its own ctest name (pool_park_cost) only in builds that compile the change, and it prints by default so that only pool_tasks_test parkcost assert (or STRATA_POOL_PARK_ASSERT=1) can fail a run. It is added to src/kernels/cpu/pool_tasks_test.cpp without touching a line of it: 142 lines added, none changed. With the option off that file compiles to the same machine code as before (identical .text, 14,314 bytes), and pool_tasks_test parkcost is a wrong argument count exactly as it was.

Closes #4624 - that ticket carries the problem and the reproduction. Related: #9 (why the workers park at all), #921 (why the knob exists), #29 (the late-wake path the pool has to survive; the split checks in src/kernels/cpu/pool_tasks_test.cpp already run with the knob at 0, and they pass before and after this change).

Heads-up for review: #3706 also edits src/kernels/cpu/pool_tasks_test.cpp, so the two will conflict. The edit here is purely additive (a block after the helpers, a block before main's argument checks, and the includes both blocks need), which should make that conflict small.

Approach

One bool, and one flag that says where it is live. const bool spin_hot = spin_before_sleep_.count() != 0; (src/kernels/cpu/pool.cpp:1002) guards the pause stage at src/kernels/cpu/pool.cpp:1022; in a build without the flag the #else arm makes it a constant true, which is the loop the last release ran. The MSVC WAITPKG/_umwait branch above it is not touched, and it is only reached when STRATA_POOL_UMWAIT is set to a positive cycle count on MSVC proper (umwait_cycles, src/kernels/cpu/pool.cpp:43); every other build has um == 0 and takes the spin_hot branch. Nothing else in the pool, its plan, its header or its documentation changes: the comment on the knob in include/strata/kernels/cpu/pool.hpp is left as it is, and which builds give 0 its new meaning is said in the comments at src/kernels/cpu/pool.cpp:996-1000 and CMakeLists.txt:2006-2011, next to the flag itself.

The measurement (park_cost, src/kernels/cpu/pool_tasks_test.cpp:78) is one window of 1,000 tiny batches with a 200 us gap - every worker parks once per phase - minus the same batches run back to back, where nothing parks, timed with getrusage over 8 workers and priced against what the same number of strata_cpu_pause() iterations costs in one thread: 3 reps, bound 1.00 (:104), and a rep whose gap window used no more CPU than the back-to-back window is dropped and said rather than failed on (kept, :126 and :155). It is asked for by a keyword in front of the existing argument checks (:180), so every other invocation of that binary takes exactly the path it took before. The option is STRATA_POOL_SPIN_US_MICROBENCH (CMakeLists.txt:173).

Measured

Both arms at this head: src/kernels/cpu/pool.cpp replaced by main's (git show ffa80d73:src/kernels/cpu/pool.cpp), rebuilt at -j 1, run, restored. Knob at 0, 8 workers, no pinning, best of 3 reps per arm, load average 3.0-3.7 while they ran (a model was being served):

pool.cpp rep 1 rep 2 rep 3 exit of parkcost assert
this head 0.68 (47 ms) 0.31 (27 ms) 0.46 (32 ms) 0, passed
main (ffa80d73) 1.96 (151 ms) 1.98 (153 ms) 1.95 (147 ms) 1, failed

The failing line, verbatim from that run: "park cost: a parked worker at STRATA_POOL_SPIN_US=0 cost 151 ms of CPU in 1000 phases, 1.96 of the cost of 8192000 plain pauses: the worker spun before it slept, so the hot stage the knob turns off was still running", closed by "pool tasks: park cost: 3 of 3 reps measured parks; the best cost 151 ms = 1.96 of the cost of 8192000 plain pauses; the asserted bound is 1.00". At this head the matching lines read "park cost rep 3/3: 8 workers x 1000 phases at 200 us: 240 ms of CPU with the gap, 193 ms back to back, parks 47 ms; 8192000 plain pauses alone 69 ms; 0.68 of that" and "the best cost 47 ms = 0.68 of the cost of 8192000 plain pauses; the asserted bound is 1.00".

The same window at the built-in 20 ms default (in a scratch worktree, with the line put_env("STRATA_POOL_SPIN_US", "0") at :102 commented out so the pool keeps its built-in default), printing arm, twice per build: this head 2,019 ms = 7.45 and 2,008 ms = 7.05; main's pool.cpp in the same binaries and window 2,028 ms = 7.52 and 2,030 ms = 7.49. The default path costs what it cost before, inside run-to-run noise, and the default path is where most people live.

In the CUDA configuration at this head the same arm passed: "the best cost 20 ms = 0.22 of the cost of 8192000 plain pauses" with CUDA_VISIBLE_DEVICES="", so it is a CPU-only reading in a CUDA-configured build.

Two caveats about these numbers. The asserting arm has not been run on a fully idle box - this box has served a model throughout, and the quietest window I could get had load 3.0-3.7. And under load the back-to-back window itself swings by about 30% (memory bandwidth, and getrusage counts only this process), which is why the number is printed and only an explicit ask asserts.

Validation

Everything below was built and tested at -j 1, at head f8464bb8 (fix 32d08246, base ffa80d73 = current main). Box: Ubuntu 26.04.1, Intel Core Ultra 9 275HX (no AVX-512), gcc 15.2.0, 62 GiB RAM. A model server was running, so uptime is logged with every park-cost arm.

Passed

  • CPU engine, measurement off: configure rc=0, cmake --build -j 1 rc=0 (211 steps), 36 build rules carry -DSTRATA_POOL_SPIN_ZERO=1, none carries the microbench define. ctest -j 1 -> "99% tests passed, 1 tests failed out of 91" (the one failure is isa_guard_check, below).
  • CPU engine, -DSTRATA_POOL_SPIN_US_MICROBENCH=ON: same build rc=0 at -j 1, ctest -j 1 -> "99% tests passed, 1 tests failed out of 92"; the 92nd test is pool_park_cost, and it passes.
  • pool_tasks_test in every configuration built here: "360 bitwise comparisons passed, 192 of them in NUMA-split mode".
  • CUDA engine (nvcc 13.3.73, CMAKE_CUDA_ARCHITECTURES=120, host compiler gcc 15.2.0): configure rc=0, 36 rules carry the flag, cmake --build -j 1 rc=0 (211 steps). CUDA_VISIBLE_DEVICES="" ctest -j 1 -> "99% tests passed, 1 tests failed out of 92" (isa_guard_check); ctest -R pool -j 1 in that tree -> "100% tests passed, 0 tests failed out of 8" (pool_park_cost, pool_tasks_test, pool_affinity_test, pool_test, pool_cores_test, pool_plan_test, numa_pool_test, pool_stress) - the CUDA configuration registers pool_park_cost as well, so the same assertion runs there, and it reports the best cost 20 ms = 0.22 of the bound in its printing form. src/kernels/cpu/pool.cpp is compiled by the host compiler in this configuration too - it is not a .cu file - which is why the object check below, not an nvcc invocation, is the evidence about the emitted code.
  • HIP engine, built with Ubuntu 26.04's own ROCm packages (hipcc, rocm-dev, rocm-device-libs-21, libamd-comgr-dev, libhipblas-dev, librocblas-dev, clang-21/llvm-21, plus -DROCM_PATH=/usr, -DCMAKE_HIP_COMPILER=/usr/lib/llvm-21/bin/clang++ and -DCMAKE_HIP_FLAGS=--rocm-device-lib-path=/usr/lib/llvm-21/lib/clang/21/amdgcn/bitcode, with the CUDA side off): configure rc=0 with 0 rules carrying STRATA_POOL_SPIN_ZERO, 188 tests registered, cmake --build -j 1 rc=0 in 507 steps. ctest -R pool -j 1 -> "100% tests passed, 0 tests failed out of 7" (pool_tasks_test, pool_affinity_test, pool_test, pool_cores_test, pool_plan_test, numa_pool_test, pool_stress). The whole HIP ctest -j 1 --timeout 300 -> "64% tests passed, 68 tests failed out of 188" with 36 Skipped and 0 Not Run: 64 of the 68 failures stop at "no ROCm-capable device is detected" (this box has an NVIDIA card), and the other four are hip_prompt_attn_wmma, ple_parity, cvec_parity and isa_guard_check.
  • SYCL port (the separate project it uses - cmake -S sycl -B <tree> -DCMAKE_CXX_COMPILER=icx -DCMAKE_C_COMPILER=icx, as sycl/CMakeLists.txt:9-10 documents; oneAPI 2026.2 icx/icpx): configured fresh at f8464bb8, rc=0, 260 build edges, 56 tests registered, and 0 rules carrying STRATA_POOL_SPIN_ZERO. cmake --build -j 1 rc=0, [260/260]. ctest -R pool -j 1 -> "No tests were found!!!": the port registers 56 tests and none of them covers the CPU pool, so what this configuration can show is that it never sees the flag and that its pool.cpp object is unchanged (below). Its full ctest is in Failed.
  • The CUDA-on-AMD gfx906 variant (-DSTRATA_HIP_GFX906=ON with STRATA_ENABLE_HIP=OFF, which is what CMakeLists.txt:89 requires, plus -DCMAKE_HIP_FLAGS=--rocm-device-lib-path=/usr/lib/llvm-21/lib/clang/21/amdgcn/bitcode): configure rc=0, 358 rules define STRATA_HIP_GFX906=1, 205 use offload-arch=gfx906, and 0 define STRATA_POOL_SPIN_ZERO. The pool cone builds at -j 1 and ctest -R pool -j 1 -> "100% tests passed, 0 tests failed out of 7". That variant does not build as a whole, at this head or at ffa80d73 - see Failed.
  • Object byte identity, computed from each tree's own ninja compile command with the two defines stripped: pool.cpp ffa80d73 vs this head -> .text identical (CPU configuration 74,859 bytes; HIP configuration 74,132; SYCL configuration 100,311) and .rodata identical (121, 121, 512); in the HIP and SYCL configurations the object those trees actually build is identical to both, because they never get the flag. pool_tasks_test.cpp the same way -> .text identical at 14,314 bytes and .rodata at 44. With the flag (and with the microbench define) the two objects do differ - 75,179 bytes and 14,442 bytes - which is what makes the identity above a statement about the gate and not a vacuous one.
  • Gate experiment: with the CMake condition at CMakeLists.txt:2013 replaced by if(FALSE) in a scratch tree, configure prints "-- Strata: pool_park_cost not registered: this build compiles the park loop as the last release does" (CMakeLists.txt:2229), build.ninja carries 0 flag rules and registers pool_park_cost 0 times, pool_tasks_test still reports "360 bitwise comparisons passed, 192 of them in NUMA-split mode", pool_tasks_test parkcost and pool_tasks_test parkcost assert both exit 2 with no output at all, and ctest -R "pool_tasks_test|pool_park_cost" -j 1 -> "100% tests passed, 0 tests failed out of 1". CMakeLists.txt was restored afterwards and the repository is clean.
  • Terminal suite (tests/terminal, "2553 tests collected") run at f8464bb8 from a clean worktree (git status --porcelain empty) with the pinned environment (textual==8.2.8 at requirements-monitor.txt:2 and at packaging/locks/runtime-linux-x86_64-cp312.txt:41, regex==2026.9.10 at :35 of the same lock, pytest 9.0.2): "4 failed, 2498 passed, 52 skipped, 9 warnings, 360 subtests passed in 1579.55s (0:26:19)". What each of the four is, below.

Failed

  • isa_guard_check, the only failure in the CPU and CUDA ctest runs; the SYCL port does not register it among its 56 tests. It says the static guard wants two AVX-512 kernels hidden behind a runtime check. From the CPU tree's Testing/Temporary/LastTest.log, verbatim, with the build-tree path written as <tree>:
check_isa_guard: <tree>/libstrata_kernels_cpu.a: 798 functions, 140 hold wide-ISA instructions, 2 not allowed
  VIOLATION strata::kernels::cpu::iq512_reference_gu_rows(int, unsigned char const*, unsigned long, unsigned long, int, void const* const*, int, float* const*, int, int) (177 wide instructions, first vxorps): wide-ISA code in an unlisted public function [iq_avx512.cpp.o]
  VIOLATION strata::kernels::cpu::iq512x_rows(int, unsigned char const*, unsigned long, int, void const* const*, int, float* const*, int, int) (719 wide instructions, first vmovq): wide-ISA code in an unlisted public function [iq_avx512.cpp.o]

Both sit in src/kernels/cpu/iq_avx512.cpp, a file this patch does not touch. It fails the same way at ffa80d73: a build tree made for this run from that revision configures rc=0 with 0 rules carrying the new define, cmake --build <tree> --target strata_kernels_cpu -j 1 returns 0 in 44 steps, and the same command on that library -> "check_isa_guard: /libstrata_kernels_cpu.a: 798 functions, 140 hold wide-ISA instructions, 2 not allowed" with the same two VIOLATION lines. The exit status is 1 at both revisions. This change does not cause it and does not change it. Five other tests report Skipped here and a skip is not a failure: io_priority_test -> "SKIP (Windows only)", expert_multi_test -> "CPU lacks missing AVX512F AVX512BW AVX512VL AVX512-VNNI AVX512-VBMI: SKIPPED" (this CPU has no AVX-512 at all), and native_hadamard_test, tq_cpu_parity, tq_cpu_parity_avx2 want trellis ggml types this build does not carry. None of them touches pool.cpp.

  • The park-cost arm against main's pool.cpp - intended, that is the point of the measurement: pool_tasks_test parkcost assert exits 1 at 1.95-1.98 of the bound, three times out of three.
  • The SYCL port's own test suite, which needs a device this box does not have: ctest -j 1 --timeout 300 --output-on-failure in the fresh tree -> "38% tests passed, 35 tests failed out of 56" - 9 ***Failed, 26 Subprocess aborted***Exception, 0 Not Run. 34 of the 35 stop at "Unrecognized build options: -ze-opt-greater-than-4GB-buffer-required", "The program was built for 1 devices" and "Compilation failed": that JIT build option (sycl/CMakeLists.txt:93-96) is rejected by the only device here, [opencl:cpu] Intel(R) OpenCL, Intel(R) Core(TM) Ultra 9 275HX; one of those binaries run by hand gives the same lines. The 35th is ple_parity, which wants a model file that is not on this box. Those tests cover SYCL kernels, not the CPU pool. Built the same way into a second fresh tree at ffa80d73 (configure rc=0, 260 edges, 56 tests, 0 flag rules, cmake --build -j 1 rc=0 [260/260]), the same suite gives "39% tests passed, 34 tests failed out of 56" - 9 ***Failed, 25 Subprocess aborted***Exception, 0 Not Run, and 2 ***Skipped: there iq_parity_fixtures exits 3, which is its own skip code (sycl/CMakeLists.txt:360-362), because that checkout has no vendored gguf-py ("iq_fixture: missing dependency: No module named 'gguf'"), and iq_parity skips with it; at this head the fixture succeeds and iq_parity then dies on the same device failure as its neighbours. Same 56 test names at both revisions, the same environment cause behind 34 of the 35, and no test in that suite covers pool.cpp.
  • The gfx906 variant does not compile, and it is not this patch: at -j 1 src/core/dflash2.cpp fails with "error: 'cudaDeviceGetStreamPriorityRange' was not declared in this scope; did you mean 'hipDeviceGetStreamPriorityRange'?" (:222:5) and "error: 'cudaStreamCreateWithPriority' was not declared in this scope; did you mean 'hipStreamCreateWithPriority'?" (:223:9). Compiling that one file at ffa80d73 with the identical command line gives the same two errors, so it is confirmed pre-existing. dflash2.cpp and include/strata/platform/hip_compat are not in this patch; the last commit to touch them is f4561cbe. I propose no fix here - it belongs to that variant's owner.
  • Terminal, three of the four, which fail the same way at ffa80d73. Run as one group of four in a clean worktree of each revision (git status --porcelain empty in both): at ffa80d73 -> "3 failed, 2 passed, 1 warning, 2 subtests passed in 21.94s", at this head -> "3 failed, 2 passed, 2 subtests passed in 21.54s", and the three failures are the same tests in both:
    • tests/terminal/test_connections.py::ConnectionTests::test_deeply_nested_telemetry_returns_one_redacted_error ("assert pid == self.pid or pid == 0").
    • tests/terminal/test_update_with_model_ds.py::DeepSeekWithAModel::test_the_file_a_loaded_model_runs_from_is_never_touched ("AssertionError: 1 != None : the running program was left running" at tests/terminal/test_update_with_model_ds.py:149, with "coreutils: unknown program 'strata-dsv41'" on stderr).
    • tests/terminal/test_runtime.py::RuntimeTests::test_foreground_jsonl_emits_ready_and_stop_without_human_stdout, subtest signal='sigint', quiet=False -> "SUBFAILED(signal='sigint', quiet=False)". Run on its own at this head it also fails ("1 failed, 1 passed, 2 subtests passed in 18.18s"), and it passed on its own in two earlier runs at this head, so it is flaky; what is not flaky is that the same subtest fails in the same group at both revisions.
  • Terminal, the fourth, which I could not make fail outside the full run: tests/terminal/test_updater.py::EngineUpdate::test_an_interrupted_download_resumes failed in the 2553-test run, but on its own pytest tests/terminal/test_updater.py -q -> "51 passed, 5 subtests passed in 95.47s" and in the group of four above it passes. I did not reproduce it, and it does not touch the pool. None of these four tests touches the pool.

Toolchain note

Ubuntu 26.04.1 carries HIP 7.1 / LLVM 21 as ordinary distribution packages. What worked here: hipcc libamdhip64-dev rocm-dev rocm-device-libs-21 libhsa-runtime-dev libhsakmt-dev libamd-comgr-dev libhipblas-dev libhipblas-common-dev librocblas-dev rocm-cmake rocminfo clang-21 llvm-21, -DROCM_PATH=/usr, -DCMAKE_HIP_COMPILER=/usr/lib/llvm-21/bin/clang++, -DCMAKE_HIP_FLAGS=--rocm-device-lib-path=/usr/lib/llvm-21/lib/clang/21/amdgcn/bitcode was carried in both AMD command lines above and does no harm; with the packages listed above installed, a fresh configure in either AMD configuration succeeds without it (checked for both: a fresh configure returns 0 and prints "Configuring done"). Before rocm-device-libs-21 was installed the same configure stopped at the HIP compiler check, so the package list is the part that matters. This is the first route to HIP that matches what docs/AMD_HIP.md:107 tells a user to install, and every number above was built with it.

Not run

  • Windows (MSVC) and macOS (Metal) were never compiled here: no MSVC, no macOS SDK, and no GitHub Actions have run on this pull request. Those builds never define STRATA_POOL_SPIN_ZERO, and the object byte-identity checks above are the evidence offered in place of a build there. A maintainer approving a workflow run, or anyone with a Mac, could close that gap.
  • Any GPU execution. The CUDA runs above are CPU-only by construction (CUDA_VISIBLE_DEVICES=""), the HIP box has no AMD device, and the SYCL box sees only opencl:cpu. So no CUDA, HIP or SYCL kernel result is claimed anywhere in this text.
  • The five ctest results that report Skipped on this box were not run anywhere that could run them, and their reasons differ: expert_multi_test -> "CPU lacks missing AVX512F AVX512BW AVX512VL AVX512-VNNI AVX512-VBMI: SKIPPED" (this CPU reports no AVX-512 flag at all in /proc/cpuinfo); io_priority_test -> "io_priority_test: SKIP (Windows only)"; native_hadamard_test -> "this ggml has no trellis types (build with STRATA_GGML_DIR = the agention fork): skipped", and tq_cpu_parity and tq_cpu_parity_avx2 -> "this ggml has no trellis types (build with STRATA_GGML_DIR = the agention llama.cpp fork): skipped". None of the five touches pool.cpp.
  • The gfx906 variant's pool_park_cost and every test outside its pool cone: that variant does not build as a whole (Failed above), so of its 205 registered tests only the cone exists as an executable. Its full ctest -j 1 there -> "4% tests passed, 196 tests failed out of 205": 9 passed, 19 "(Failed)" and 177 "(Not Run)". Nothing about that variant's kernel path is claimed.
  • The park-cost assertion on a machine with nothing else running, and on a machine with AVX-512.

Limits

The change is Linux-only by construction; that is what the flag is for, and the two places it is live are the two places it is measured. The 20 ms default path is not re-tuned and no value other than 0 changes behaviour; its cost was measured on both builds and is in the table above. The measurement is opt-in, prints by default, and asserts only when asked.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4625 **Author:** [@GoodSlopBadSlop](https://github.com/GoodSlopBadSlop) **Created:** 10/29/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `f-pool-spin-zero` --- ### 📝 Commits (3) - [`32d0824`](https://github.com/Niko1221/Strata/commit/32d08246b10667a7c212f5adf069e0e1d5ba96be) [Engine] STRATA_POOL_SPIN_US=0 means zero: a parked worker with no hot stage goes to the kernel sleep - [`f8464bb`](https://github.com/Niko1221/Strata/commit/f8464bb83ff4ea34ef36549b0089c5411b62e319) [Engine] pool_tasks_test: what a park at STRATA_POOL_SPIN_US=0 costs is measured and printed, and asserted only when asked - [`07115a0`](https://github.com/Niko1221/Strata/commit/07115a087d2084c7fd126ef3506e185cc2ce31fc) Merge branch 'Niko1221:main' into f-pool-spin-zero ### 📊 Changes **3 files changed** (+180 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+27 -0) 📝 `src/kernels/cpu/pool.cpp` (+11 -1) 📝 `src/kernels/cpu/pool_tasks_test.cpp` (+142 -0) </details> ### 📄 Description ## Problem and result `STRATA_POOL_SPIN_US=0` did not mean zero. A parked worker waits `kSpinBeforeSleep` (20 ms) before it sleeps, and the park loop reads the clock once every 1024 pauses, so a worker parked with the knob at `0` still ran 1,024 `strata_cpu_pause()` calls before it blocked on `sleep_cv_`. Before: with the knob at `0` a parked worker ran that pause stage and then slept. After: a hot stage of `0` skips the pause stage and blocks at once. Every other value of the knob, and the 20 ms default, takes the same branch and the same clock cadence it took before. Measured on this box at `f8464bb8`, everything at `-j 1`: with the knob at `0` a parked worker costs **47 ms, 27 ms and 32 ms** of CPU per 1,000 park phases (8 workers), against **151 ms, 153 ms and 147 ms** for the same test built from `main`'s `pool.cpp`, with an asserted bound of 1.00 of the cost of the same number of plain pauses. The three runs at this head pass (0.68, 0.31, 0.46); the three runs of `main`'s file fail (1.96, 1.98, 1.95). At the built-in 20 ms default both builds cost about the same: 2,019 ms and 2,008 ms here, 2,028 ms and 2,030 ms on `main`'s file. The change is compiled only in the builds it was built and measured in: CMake defines `STRATA_POOL_SPIN_ZERO` for Linux with the CPU engine or the CUDA engine (`CMakeLists.txt:2012-2016`), and everywhere else `src/kernels/cpu/pool.cpp:1001-1005` keeps `spin_hot` a constant `true`, which folds away into the park loop of the last release. Checked mechanically, at `-j 1`, with each configuration's own compile command for that file and the flag removed: `pool.cpp` from `ffa80d73` and the one in this branch give a byte-identical `.text` and `.rodata` in the CPU configuration (74,859 and 121 bytes), in the HIP configuration (74,132 and 121) and in the SYCL configuration (100,311 and 512); with the flag defined the same source becomes 75,179 bytes, so the comparison is not comparing two copies of one object. No Windows, macOS/Metal, HIP or SYCL build compiles a different park loop from the one it compiles today. The second commit adds the only kind of check that can see the difference, because the pauses change no output: the CPU time of a park. It is opt-in (`-DSTRATA_POOL_SPIN_US_MICROBENCH=ON`, off by default), registered under its own ctest name (`pool_park_cost`) only in builds that compile the change, and it prints by default so that only `pool_tasks_test parkcost assert` (or `STRATA_POOL_PARK_ASSERT=1`) can fail a run. It is added to `src/kernels/cpu/pool_tasks_test.cpp` without touching a line of it: 142 lines added, none changed. With the option off that file compiles to the same machine code as before (identical `.text`, 14,314 bytes), and `pool_tasks_test parkcost` is a wrong argument count exactly as it was. ## Related discussion Closes #4624 - that ticket carries the problem and the reproduction. Related: #9 (why the workers park at all), #921 (why the knob exists), #29 (the late-wake path the pool has to survive; the split checks in `src/kernels/cpu/pool_tasks_test.cpp` already run with the knob at `0`, and they pass before and after this change). Heads-up for review: #3706 also edits `src/kernels/cpu/pool_tasks_test.cpp`, so the two will conflict. The edit here is purely additive (a block after the helpers, a block before `main`'s argument checks, and the includes both blocks need), which should make that conflict small. ## Approach One bool, and one flag that says where it is live. `const bool spin_hot = spin_before_sleep_.count() != 0;` (`src/kernels/cpu/pool.cpp:1002`) guards the pause stage at `src/kernels/cpu/pool.cpp:1022`; in a build without the flag the `#else` arm makes it a constant `true`, which is the loop the last release ran. The MSVC `WAITPKG`/`_umwait` branch above it is not touched, and it is only reached when `STRATA_POOL_UMWAIT` is set to a positive cycle count on MSVC proper (`umwait_cycles`, `src/kernels/cpu/pool.cpp:43`); every other build has `um == 0` and takes the `spin_hot` branch. Nothing else in the pool, its plan, its header or its documentation changes: the comment on the knob in `include/strata/kernels/cpu/pool.hpp` is left as it is, and which builds give `0` its new meaning is said in the comments at `src/kernels/cpu/pool.cpp:996-1000` and `CMakeLists.txt:2006-2011`, next to the flag itself. The measurement (`park_cost`, `src/kernels/cpu/pool_tasks_test.cpp:78`) is one window of 1,000 tiny batches with a 200 us gap - every worker parks once per phase - minus the same batches run back to back, where nothing parks, timed with `getrusage` over 8 workers and priced against what the same number of `strata_cpu_pause()` iterations costs in one thread: 3 reps, bound 1.00 (`:104`), and a rep whose gap window used no more CPU than the back-to-back window is dropped and said rather than failed on (`kept`, `:126` and `:155`). It is asked for by a keyword in front of the existing argument checks (`:180`), so every other invocation of that binary takes exactly the path it took before. The option is `STRATA_POOL_SPIN_US_MICROBENCH` (`CMakeLists.txt:173`). ## Measured Both arms at this head: `src/kernels/cpu/pool.cpp` replaced by `main`'s (`git show ffa80d73:src/kernels/cpu/pool.cpp`), rebuilt at `-j 1`, run, restored. Knob at `0`, 8 workers, no pinning, best of 3 reps per arm, load average 3.0-3.7 while they ran (a model was being served): | `pool.cpp` | rep 1 | rep 2 | rep 3 | exit of `parkcost assert` | | --- | --- | --- | --- | --- | | this head | 0.68 (47 ms) | 0.31 (27 ms) | 0.46 (32 ms) | 0, passed | | `main` (`ffa80d73`) | 1.96 (151 ms) | 1.98 (153 ms) | 1.95 (147 ms) | 1, failed | The failing line, verbatim from that run: "park cost: a parked worker at STRATA_POOL_SPIN_US=0 cost 151 ms of CPU in 1000 phases, 1.96 of the cost of 8192000 plain pauses: the worker spun before it slept, so the hot stage the knob turns off was still running", closed by "pool tasks: park cost: 3 of 3 reps measured parks; the best cost 151 ms = 1.96 of the cost of 8192000 plain pauses; the asserted bound is 1.00". At this head the matching lines read "park cost rep 3/3: 8 workers x 1000 phases at 200 us: 240 ms of CPU with the gap, 193 ms back to back, parks 47 ms; 8192000 plain pauses alone 69 ms; 0.68 of that" and "the best cost 47 ms = 0.68 of the cost of 8192000 plain pauses; the asserted bound is 1.00". The same window at the **built-in 20 ms default** (in a scratch worktree, with the line `put_env("STRATA_POOL_SPIN_US", "0")` at `:102` commented out so the pool keeps its built-in default), printing arm, twice per build: this head **2,019 ms = 7.45** and **2,008 ms = 7.05**; `main`'s `pool.cpp` in the same binaries and window **2,028 ms = 7.52** and **2,030 ms = 7.49**. The default path costs what it cost before, inside run-to-run noise, and the default path is where most people live. In the CUDA configuration at this head the same arm passed: "the best cost 20 ms = 0.22 of the cost of 8192000 plain pauses" with `CUDA_VISIBLE_DEVICES=""`, so it is a CPU-only reading in a CUDA-configured build. Two caveats about these numbers. The asserting arm has **not** been run on a fully idle box - this box has served a model throughout, and the quietest window I could get had load 3.0-3.7. And under load the back-to-back window itself swings by about 30% (memory bandwidth, and `getrusage` counts only this process), which is why the number is printed and only an explicit ask asserts. ## Validation Everything below was built and tested at `-j 1`, at head `f8464bb8` (fix `32d08246`, base `ffa80d73` = current `main`). Box: Ubuntu 26.04.1, Intel Core Ultra 9 275HX (no AVX-512), gcc 15.2.0, 62 GiB RAM. A model server was running, so `uptime` is logged with every park-cost arm. ### Passed - CPU engine, measurement off: configure rc=0, `cmake --build -j 1` rc=0 (211 steps), 36 build rules carry `-DSTRATA_POOL_SPIN_ZERO=1`, none carries the microbench define. `ctest -j 1` -> "99% tests passed, 1 tests failed out of 91" (the one failure is `isa_guard_check`, below). - CPU engine, `-DSTRATA_POOL_SPIN_US_MICROBENCH=ON`: same build rc=0 at `-j 1`, `ctest -j 1` -> "99% tests passed, 1 tests failed out of 92"; the 92nd test is `pool_park_cost`, and it passes. - `pool_tasks_test` in every configuration built here: "360 bitwise comparisons passed, 192 of them in NUMA-split mode". - CUDA engine (nvcc 13.3.73, `CMAKE_CUDA_ARCHITECTURES=120`, host compiler gcc 15.2.0): configure rc=0, 36 rules carry the flag, `cmake --build -j 1` rc=0 (211 steps). `CUDA_VISIBLE_DEVICES="" ctest -j 1` -> "99% tests passed, 1 tests failed out of 92" (`isa_guard_check`); `ctest -R pool -j 1` in that tree -> "100% tests passed, 0 tests failed out of 8" (`pool_park_cost`, `pool_tasks_test`, `pool_affinity_test`, `pool_test`, `pool_cores_test`, `pool_plan_test`, `numa_pool_test`, `pool_stress`) - the CUDA configuration registers `pool_park_cost` as well, so the same assertion runs there, and it reports the best cost 20 ms = 0.22 of the bound in its printing form. `src/kernels/cpu/pool.cpp` is compiled by the host compiler in this configuration too - it is not a `.cu` file - which is why the object check below, not an nvcc invocation, is the evidence about the emitted code. - HIP engine, built with Ubuntu 26.04's own ROCm packages (`hipcc`, `rocm-dev`, `rocm-device-libs-21`, `libamd-comgr-dev`, `libhipblas-dev`, `librocblas-dev`, `clang-21`/`llvm-21`, plus `-DROCM_PATH=/usr`, `-DCMAKE_HIP_COMPILER=/usr/lib/llvm-21/bin/clang++` and `-DCMAKE_HIP_FLAGS=--rocm-device-lib-path=/usr/lib/llvm-21/lib/clang/21/amdgcn/bitcode`, with the CUDA side off): configure rc=0 with **0** rules carrying `STRATA_POOL_SPIN_ZERO`, 188 tests registered, `cmake --build -j 1` rc=0 in 507 steps. `ctest -R pool -j 1` -> "100% tests passed, 0 tests failed out of 7" (`pool_tasks_test`, `pool_affinity_test`, `pool_test`, `pool_cores_test`, `pool_plan_test`, `numa_pool_test`, `pool_stress`). The whole HIP `ctest -j 1 --timeout 300` -> "64% tests passed, 68 tests failed out of 188" with 36 Skipped and **0 Not Run**: 64 of the 68 failures stop at "no ROCm-capable device is detected" (this box has an NVIDIA card), and the other four are `hip_prompt_attn_wmma`, `ple_parity`, `cvec_parity` and `isa_guard_check`. - SYCL port (the separate project it uses - `cmake -S sycl -B <tree> -DCMAKE_CXX_COMPILER=icx -DCMAKE_C_COMPILER=icx`, as `sycl/CMakeLists.txt:9-10` documents; oneAPI 2026.2 `icx`/`icpx`): configured fresh at `f8464bb8`, rc=0, 260 build edges, 56 tests registered, and **0** rules carrying `STRATA_POOL_SPIN_ZERO`. `cmake --build -j 1` rc=0, `[260/260]`. `ctest -R pool -j 1` -> "No tests were found!!!": the port registers 56 tests and none of them covers the CPU pool, so what this configuration can show is that it never sees the flag and that its `pool.cpp` object is unchanged (below). Its full `ctest` is in Failed. - The CUDA-on-AMD `gfx906` variant (`-DSTRATA_HIP_GFX906=ON` with `STRATA_ENABLE_HIP=OFF`, which is what `CMakeLists.txt:89` requires, plus `-DCMAKE_HIP_FLAGS=--rocm-device-lib-path=/usr/lib/llvm-21/lib/clang/21/amdgcn/bitcode`): configure rc=0, 358 rules define `STRATA_HIP_GFX906=1`, 205 use `offload-arch=gfx906`, and **0** define `STRATA_POOL_SPIN_ZERO`. The pool cone builds at `-j 1` and `ctest -R pool -j 1` -> "100% tests passed, 0 tests failed out of 7". That variant does not build as a whole, at this head or at `ffa80d73` - see Failed. - Object byte identity, computed from each tree's own ninja compile command with the two defines stripped: `pool.cpp` `ffa80d73` vs this head -> `.text` identical (CPU configuration 74,859 bytes; HIP configuration 74,132; SYCL configuration 100,311) and `.rodata` identical (121, 121, 512); in the HIP and SYCL configurations the object those trees actually build is identical to both, because they never get the flag. `pool_tasks_test.cpp` the same way -> `.text` identical at 14,314 bytes and `.rodata` at 44. With the flag (and with the microbench define) the two objects do differ - 75,179 bytes and 14,442 bytes - which is what makes the identity above a statement about the gate and not a vacuous one. - Gate experiment: with the CMake condition at `CMakeLists.txt:2013` replaced by `if(FALSE)` in a scratch tree, configure prints "-- Strata: pool_park_cost not registered: this build compiles the park loop as the last release does" (`CMakeLists.txt:2229`), `build.ninja` carries 0 flag rules and registers `pool_park_cost` 0 times, `pool_tasks_test` still reports "360 bitwise comparisons passed, 192 of them in NUMA-split mode", `pool_tasks_test parkcost` and `pool_tasks_test parkcost assert` both exit 2 with no output at all, and `ctest -R "pool_tasks_test|pool_park_cost" -j 1` -> "100% tests passed, 0 tests failed out of 1". `CMakeLists.txt` was restored afterwards and the repository is clean. - Terminal suite (`tests/terminal`, "2553 tests collected") run at `f8464bb8` from a clean worktree (`git status --porcelain` empty) with the pinned environment (`textual==8.2.8` at `requirements-monitor.txt:2` and at `packaging/locks/runtime-linux-x86_64-cp312.txt:41`, `regex==2026.9.10` at `:35` of the same lock, pytest 9.0.2): "4 failed, 2498 passed, 52 skipped, 9 warnings, 360 subtests passed in 1579.55s (0:26:19)". What each of the four is, below. ### Failed - `isa_guard_check`, the only failure in the CPU and CUDA `ctest` runs; the SYCL port does not register it among its 56 tests. It says the static guard wants two AVX-512 kernels hidden behind a runtime check. From the CPU tree's `Testing/Temporary/LastTest.log`, verbatim, with the build-tree path written as `<tree>`: ``` check_isa_guard: <tree>/libstrata_kernels_cpu.a: 798 functions, 140 hold wide-ISA instructions, 2 not allowed VIOLATION strata::kernels::cpu::iq512_reference_gu_rows(int, unsigned char const*, unsigned long, unsigned long, int, void const* const*, int, float* const*, int, int) (177 wide instructions, first vxorps): wide-ISA code in an unlisted public function [iq_avx512.cpp.o] VIOLATION strata::kernels::cpu::iq512x_rows(int, unsigned char const*, unsigned long, int, void const* const*, int, float* const*, int, int) (719 wide instructions, first vmovq): wide-ISA code in an unlisted public function [iq_avx512.cpp.o] ``` Both sit in `src/kernels/cpu/iq_avx512.cpp`, a file this patch does not touch. **It fails the same way at `ffa80d73`**: a build tree made for this run from that revision configures rc=0 with 0 rules carrying the new define, `cmake --build <tree> --target strata_kernels_cpu -j 1` returns 0 in 44 steps, and the same command on that library -> "check_isa_guard: <tree>/libstrata_kernels_cpu.a: 798 functions, 140 hold wide-ISA instructions, 2 not allowed" with the same two `VIOLATION` lines. The exit status is 1 at both revisions. This change does not cause it and does not change it. Five other tests report `Skipped` here and a skip is not a failure: `io_priority_test` -> "SKIP (Windows only)", `expert_multi_test` -> "CPU lacks missing AVX512F AVX512BW AVX512VL AVX512-VNNI AVX512-VBMI: SKIPPED" (this CPU has no AVX-512 at all), and `native_hadamard_test`, `tq_cpu_parity`, `tq_cpu_parity_avx2` want trellis ggml types this build does not carry. None of them touches `pool.cpp`. - The park-cost arm against `main`'s `pool.cpp` - intended, that is the point of the measurement: `pool_tasks_test parkcost assert` exits 1 at 1.95-1.98 of the bound, three times out of three. - The SYCL port's own test suite, which needs a device this box does not have: `ctest -j 1 --timeout 300 --output-on-failure` in the fresh tree -> "38% tests passed, 35 tests failed out of 56" - 9 `***Failed`, 26 `Subprocess aborted***Exception`, 0 Not Run. 34 of the 35 stop at "Unrecognized build options: -ze-opt-greater-than-4GB-buffer-required", "The program was built for 1 devices" and "Compilation failed": that JIT build option (`sycl/CMakeLists.txt:93-96`) is rejected by the only device here, `[opencl:cpu] Intel(R) OpenCL, Intel(R) Core(TM) Ultra 9 275HX`; one of those binaries run by hand gives the same lines. The 35th is `ple_parity`, which wants a model file that is not on this box. Those tests cover SYCL kernels, not the CPU pool. Built the same way into a second fresh tree at `ffa80d73` (configure rc=0, 260 edges, 56 tests, 0 flag rules, `cmake --build -j 1` rc=0 `[260/260]`), the same suite gives "39% tests passed, 34 tests failed out of 56" - 9 `***Failed`, 25 `Subprocess aborted***Exception`, 0 Not Run, and 2 `***Skipped`: there `iq_parity_fixtures` exits 3, which is its own skip code (`sycl/CMakeLists.txt:360-362`), because that checkout has no vendored `gguf-py` ("iq_fixture: missing dependency: No module named 'gguf'"), and `iq_parity` skips with it; at this head the fixture succeeds and `iq_parity` then dies on the same device failure as its neighbours. Same 56 test names at both revisions, the same environment cause behind 34 of the 35, and no test in that suite covers `pool.cpp`. - The `gfx906` variant does not compile, and it is not this patch: at `-j 1` `src/core/dflash2.cpp` fails with "error: 'cudaDeviceGetStreamPriorityRange' was not declared in this scope; did you mean 'hipDeviceGetStreamPriorityRange'?" (`:222:5`) and "error: 'cudaStreamCreateWithPriority' was not declared in this scope; did you mean 'hipStreamCreateWithPriority'?" (`:223:9`). Compiling that one file at `ffa80d73` with the identical command line gives the same two errors, so it is **confirmed pre-existing**. `dflash2.cpp` and `include/strata/platform/hip_compat` are not in this patch; the last commit to touch them is `f4561cbe`. I propose no fix here - it belongs to that variant's owner. - Terminal, three of the four, which **fail the same way at `ffa80d73`**. Run as one group of four in a clean worktree of each revision (`git status --porcelain` empty in both): at `ffa80d73` -> "3 failed, 2 passed, 1 warning, 2 subtests passed in 21.94s", at this head -> "3 failed, 2 passed, 2 subtests passed in 21.54s", and the three failures are the same tests in both: - `tests/terminal/test_connections.py::ConnectionTests::test_deeply_nested_telemetry_returns_one_redacted_error` ("assert pid == self.pid or pid == 0"). - `tests/terminal/test_update_with_model_ds.py::DeepSeekWithAModel::test_the_file_a_loaded_model_runs_from_is_never_touched` ("AssertionError: 1 != None : the running program was left running" at `tests/terminal/test_update_with_model_ds.py:149`, with "coreutils: unknown program 'strata-dsv41'" on stderr). - `tests/terminal/test_runtime.py::RuntimeTests::test_foreground_jsonl_emits_ready_and_stop_without_human_stdout`, subtest `signal='sigint', quiet=False` -> "SUBFAILED(signal='sigint', quiet=False)". Run on its own at this head it also fails ("1 failed, 1 passed, 2 subtests passed in 18.18s"), and it passed on its own in two earlier runs at this head, so it is flaky; what is not flaky is that the same subtest fails in the same group at both revisions. - Terminal, the fourth, which I could not make fail outside the full run: `tests/terminal/test_updater.py::EngineUpdate::test_an_interrupted_download_resumes` failed in the 2553-test run, but on its own `pytest tests/terminal/test_updater.py -q` -> "51 passed, 5 subtests passed in 95.47s" and in the group of four above it passes. I did not reproduce it, and it does not touch the pool. None of these four tests touches the pool. ### Toolchain note Ubuntu 26.04.1 carries HIP 7.1 / LLVM 21 as ordinary distribution packages. What worked here: `hipcc libamdhip64-dev rocm-dev rocm-device-libs-21 libhsa-runtime-dev libhsakmt-dev libamd-comgr-dev libhipblas-dev libhipblas-common-dev librocblas-dev rocm-cmake rocminfo clang-21 llvm-21`, `-DROCM_PATH=/usr`, `-DCMAKE_HIP_COMPILER=/usr/lib/llvm-21/bin/clang++`, `-DCMAKE_HIP_FLAGS=--rocm-device-lib-path=/usr/lib/llvm-21/lib/clang/21/amdgcn/bitcode` was carried in both AMD command lines above and does no harm; with the packages listed above installed, a fresh configure in either AMD configuration succeeds without it (checked for both: a fresh configure returns 0 and prints "Configuring done"). Before `rocm-device-libs-21` was installed the same configure stopped at the HIP compiler check, so the package list is the part that matters. This is the first route to HIP that matches what `docs/AMD_HIP.md:107` tells a user to install, and every number above was built with it. ### Not run - Windows (MSVC) and macOS (Metal) were never compiled here: no MSVC, no macOS SDK, and no GitHub Actions have run on this pull request. Those builds never define `STRATA_POOL_SPIN_ZERO`, and the object byte-identity checks above are the evidence offered in place of a build there. A maintainer approving a workflow run, or anyone with a Mac, could close that gap. - Any GPU execution. The CUDA runs above are CPU-only by construction (`CUDA_VISIBLE_DEVICES=""`), the HIP box has no AMD device, and the SYCL box sees only `opencl:cpu`. So no CUDA, HIP or SYCL kernel result is claimed anywhere in this text. - The five `ctest` results that report `Skipped` on this box were not run anywhere that could run them, and their reasons differ: `expert_multi_test` -> "CPU lacks missing AVX512F AVX512BW AVX512VL AVX512-VNNI AVX512-VBMI: SKIPPED" (this CPU reports no AVX-512 flag at all in `/proc/cpuinfo`); `io_priority_test` -> "io_priority_test: SKIP (Windows only)"; `native_hadamard_test` -> "this ggml has no trellis types (build with STRATA_GGML_DIR = the agention fork): skipped", and `tq_cpu_parity` and `tq_cpu_parity_avx2` -> "this ggml has no trellis types (build with STRATA_GGML_DIR = the agention llama.cpp fork): skipped". None of the five touches `pool.cpp`. - The `gfx906` variant's `pool_park_cost` and every test outside its pool cone: that variant does not build as a whole (Failed above), so of its 205 registered tests only the cone exists as an executable. Its full `ctest -j 1` there -> "4% tests passed, 196 tests failed out of 205": 9 passed, 19 "(Failed)" and 177 "(Not Run)". Nothing about that variant's kernel path is claimed. - The park-cost assertion on a machine with nothing else running, and on a machine with AVX-512. ## Limits The change is Linux-only by construction; that is what the flag is for, and the two places it is live are the two places it is measured. The 20 ms default path is not re-tuned and no value other than `0` changes behaviour; its cost was measured on both builds and is in the table above. The measurement is opt-in, prints by default, and asserts only when asked. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5118
No description provided.