[GH-ISSUE #4624] [Engine] STRATA_POOL_SPIN_US=0 still runs the pause stage: a parked worker sleeps only after 1,024 pauses #2095

Open
opened 2026-11-04 01:56:39 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @GoodSlopBadSlop on GitHub (Oct 29, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/4624

Area

[Engine] (speed, kernels, models, the server and API)

Desktop app, Terminal or source?

Source (START-HERE.bat, setup.sh or a build)

What happened

STRATA_POOL_SPIN_US=0 does not mean zero. A parked CPU expert-pool worker still runs a pause stage before it
blocks.

spin_before_sleep_ is what the knob sets (src/kernels/cpu/pool.cpp:748, documented at
include/strata/kernels/cpu/pool.hpp:281 as "a test knob: a short spin makes the workers sleep constantly"). At 0
the duration is already expired when the loop first looks, but the loop reads the clock only once every 1,024 pauses,
so the worker runs 1,024 strata_cpu_pause() before it blocks. The comment above the loop says as much: "The clock is
read once every 1024 pauses, so the spin itself is unchanged" (src/kernels/cpu/pool.cpp:991-992). The counter is
declared before the wait and advanced inside the mask test, so the count is exact, not approximate.

const auto parked_at = std::chrono::steady_clock::now();    // pool.cpp:993
uint32_t spins = 0;                                         // pool.cpp:994
while (epoch_.load(std::memory_order_acquire) == seen) {    // pool.cpp:1000
    if (stop_.load(std::memory_order_relaxed)) return;
    ...                                                   // the Windows _umwait arm, clock every 16 waits
    strata_cpu_pause();                                   // pool.cpp:1013
    if ((++spins & 1023u) != 0) continue;                 // pool.cpp:1014
    if (std::chrono::steady_clock::now() - parked_at < spin_before_sleep_) continue;   // pool.cpp:1016
    ...                                                   // lock sleep_mu_, mark kSleeping, block on sleep_cv_
}

At 0 that is 1,024 pauses and one clock read per park. Every park pays that stage, and a worker that is going to
sleep was never going to use it.

Why it matters

  • src/kernels/cpu/pool_tasks_test.cpp:122 sets the knob to 0 to force every worker to sleep between batches, and
    relies on it to drive the late-wake path of issue #29. The test wants a park with no hot stage; what it gets is a
    1,024-pause hot stage.
  • The knob exists because issue #921 asked for a way to trade the 20 ms spin away on GPU-heavy decode. Setting it to
    0 - the natural reading of "turn it off" - leaves the whole stage in place.
  • Nothing in the tree can tell the two behaviours apart by output: the pause stage changes no result. Only cost can,
    and until now nothing measured cost. Nothing in the tree asserts that the default 20 ms reaches the pool either -
    the default value appears at include/strata/kernels/cpu/pool.hpp:282 and is used at :349 and
    src/kernels/cpu/pool.cpp:748, and no test reads it back.

What I expected instead

0 skips the pause stage and the worker blocks on sleep_cv_ immediately. I filed pull request #4625 with that change:

const bool spin_hot = spin_before_sleep_.count() != 0;

One bool in the same loop, so the branch a reader sees matches the setting; any other value of the knob keeps the loop
exactly as it is. The default is kSpinBeforeSleep{20} ms (include/strata/kernels/cpu/pool.hpp:282), so the default
path takes the same branch, with the same spins & 1023u cadence, as before this change.

#4625 also carries the guard that keeps unmeasured platforms untouched: the define STRATA_POOL_SPIN_ZERO is set on
the CPU kernel target on Linux, and not in the HIP or gfx906 variants (CMakeLists.txt:2013-2016); where it is
absent the source reads const bool spin_hot = true; (src/kernels/cpu/pool.cpp:1004, inside the
#if defined(STRATA_POOL_SPIN_ZERO) at :1001), which folds the loop back to what it is today. Three files, 180 lines
added, 1 removed: the gate, the bool, and the cost check.

Smallest steps or command that reproduce it

Reproduce the cost check that ships in #4625: configure the CPU engine with
-DSTRATA_BUILD_TESTS=ON -DSTRATA_POOL_SPIN_US_MICROBENCH=ON and run ./pool_tasks_test parkcost assert.
It parks 8 workers over 1,000 phases with a 200 µs gap, three repetitions, and
prices a park against the cost of 8,192,000 plain pauses; the bound is a ratio of 1.00, so it fails when a parked
worker costs more than pausing by hand (src/kernels/cpu/pool_tasks_test.cpp:103-105). No extra switch is needed to
make the change live: the Linux CPU configuration defines STRATA_POOL_SPIN_ZERO itself
(CMakeLists.txt:2013-2016).

Two arms, one tree: this branch's src/kernels/cpu/pool.cpp, and that file replaced by main's
(git show ffa80d73:src/kernels/cpu/pool.cpp) with nothing else changed.

What this does not claim

No Windows run: the check needs getrusage, and its section is compiled only on Linux and only where the change
itself is compiled (src/kernels/cpu/pool_tasks_test.cpp:38), so a Windows build carries no measurement to assert
rather than a measurement that reports itself skipped. No macOS or Metal build.

The 1,024-pause count is read from the loop above, not measured: nothing in the tree counts pauses, and adding a
counter per pause iteration would cost more than the pause it counts. That part is static reading of the loop; the
cost difference in the logs below is an observed run.

Reproduction status

Reproduced locally

Strata version

Strata 0.2.10 · engine 0.1.56, built from source at ffa80d73 and at the head of #4625 (f8464bb8). main is now
Strata 0.2.11 · engine 0.1.57, so these numbers are from 0.2.10 / 0.1.56.

Graphics card, memory and system

NVIDIA GeForce RTX 5090 Laptop GPU 24 GiB (driver 615.71.09) · 62.2 GiB RAM · Linux 7.0.0-42-generic (Ubuntu 26.04.1) ·
Intel Core Ultra 9 275HX (24 cores, 24 threads, no AVX-512), gcc 15.2.0

Model, size and settings

No model is needed for the reproduction: pool_tasks_test builds its own Q4_0 expert and its own 8-worker pool, so
the two arms differ only in the pool.cpp they were built from. The setting under discussion is
STRATA_POOL_SPIN_US=0 against the built-in 20 ms default. The runs were made while a model server was serving on the
box (load average 3.0-3.7), which is why the measurement prints and asserts only when asked. Each arm was built one
job at a time so the build itself did not load the machine; that is how the runs were made, not a requirement to
repeat them.

Logs

With pool.cpp from ffa80d73 and nothing else changed, all three repetitions fail:

the best cost 151 ms = 1.96 of the cost of 8192000 plain pauses; the asserted bound is 1.00
the best cost 153 ms = 1.98 of the cost of 8192000 plain pauses; the asserted bound is 1.00
the best cost 147 ms = 1.95 of the cost of 8192000 plain pauses; the asserted bound is 1.00

With the change in #4625, all three pass:

the best cost 47 ms = 0.68 of the cost of 8192000 plain pauses; the asserted bound is 1.00
the best cost 27 ms = 0.31 of the cost of 8192000 plain pauses; the asserted bound is 1.00
the best cost 32 ms = 0.46 of the cost of 8192000 plain pauses; the asserted bound is 1.00

With the knob left at its default the two builds cost the same, which is the point of the change: 2019 ms = 7.45 and
2008 ms = 7.05 at the head, 2028 ms = 7.52 and 2030 ms = 7.49 with pool.cpp from ffa80d73. Checked mechanically with each
configuration's own compile command for that file and the flag removed: pool.cpp from ffa80d73
and the one in #4625 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. Windows, macOS/Metal, HIP and
SYCL builds therefore compile the same park loop the last release compiles; the Linux CPU and CUDA configurations do
compile the new branch, and the default-cost rows above are the measured cost of that. With the change in the tree the
pool tests pass unchanged: "360 bitwise comparisons passed, 192 of them in NUMA-split mode".

No engine log applies to this finding - it comes from a test binary, not from a served model, so there is no
strata doctor output or strata-<model>.log to attach. The full build and test log for every configuration is
attached to pull request #4625.

Originally created by @GoodSlopBadSlop on GitHub (Oct 29, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/4624 ### Area [Engine] (speed, kernels, models, the server and API) ### Desktop app, Terminal or source? Source (START-HERE.bat, setup.sh or a build) ### What happened `STRATA_POOL_SPIN_US=0` does not mean zero. A parked CPU expert-pool worker still runs a pause stage before it blocks. `spin_before_sleep_` is what the knob sets (`src/kernels/cpu/pool.cpp:748`, documented at `include/strata/kernels/cpu/pool.hpp:281` as "a test knob: a short spin makes the workers sleep constantly"). At `0` the duration is already expired when the loop first looks, but the loop reads the clock only once every 1,024 pauses, so the worker runs 1,024 `strata_cpu_pause()` before it blocks. The comment above the loop says as much: "The clock is read once every 1024 pauses, so the spin itself is unchanged" (`src/kernels/cpu/pool.cpp:991-992`). The counter is declared before the wait and advanced inside the mask test, so the count is exact, not approximate. ```cpp const auto parked_at = std::chrono::steady_clock::now(); // pool.cpp:993 uint32_t spins = 0; // pool.cpp:994 while (epoch_.load(std::memory_order_acquire) == seen) { // pool.cpp:1000 if (stop_.load(std::memory_order_relaxed)) return; ... // the Windows _umwait arm, clock every 16 waits strata_cpu_pause(); // pool.cpp:1013 if ((++spins & 1023u) != 0) continue; // pool.cpp:1014 if (std::chrono::steady_clock::now() - parked_at < spin_before_sleep_) continue; // pool.cpp:1016 ... // lock sleep_mu_, mark kSleeping, block on sleep_cv_ } ``` At `0` that is 1,024 pauses and one clock read per park. Every park pays that stage, and a worker that is going to sleep was never going to use it. #### Why it matters - `src/kernels/cpu/pool_tasks_test.cpp:122` sets the knob to `0` to force every worker to sleep between batches, and relies on it to drive the late-wake path of issue #29. The test wants a park with no hot stage; what it gets is a 1,024-pause hot stage. - The knob exists because issue #921 asked for a way to trade the 20 ms spin away on GPU-heavy decode. Setting it to `0` - the natural reading of "turn it off" - leaves the whole stage in place. - Nothing in the tree can tell the two behaviours apart by output: the pause stage changes no result. Only cost can, and until now nothing measured cost. Nothing in the tree asserts that the default 20 ms reaches the pool either - the default value appears at `include/strata/kernels/cpu/pool.hpp:282` and is used at :349 and `src/kernels/cpu/pool.cpp:748`, and no test reads it back. #### What I expected instead `0` skips the pause stage and the worker blocks on `sleep_cv_` immediately. I filed pull request #4625 with that change: ```cpp const bool spin_hot = spin_before_sleep_.count() != 0; ``` One bool in the same loop, so the branch a reader sees matches the setting; any other value of the knob keeps the loop exactly as it is. The default is `kSpinBeforeSleep{20}` ms (`include/strata/kernels/cpu/pool.hpp:282`), so the default path takes the same branch, with the same `spins & 1023u` cadence, as before this change. #4625 also carries the guard that keeps unmeasured platforms untouched: the define `STRATA_POOL_SPIN_ZERO` is set on the CPU kernel target on Linux, and not in the HIP or `gfx906` variants (`CMakeLists.txt:2013-2016`); where it is absent the source reads `const bool spin_hot = true;` (`src/kernels/cpu/pool.cpp:1004`, inside the `#if defined(STRATA_POOL_SPIN_ZERO)` at :1001), which folds the loop back to what it is today. Three files, 180 lines added, 1 removed: the gate, the bool, and the cost check. ### Smallest steps or command that reproduce it Reproduce the cost check that ships in #4625: configure the CPU engine with `-DSTRATA_BUILD_TESTS=ON -DSTRATA_POOL_SPIN_US_MICROBENCH=ON` and run `./pool_tasks_test parkcost assert`. It parks 8 workers over 1,000 phases with a 200 µs gap, three repetitions, and prices a park against the cost of 8,192,000 plain pauses; the bound is a ratio of 1.00, so it fails when a parked worker costs more than pausing by hand (`src/kernels/cpu/pool_tasks_test.cpp:103-105`). No extra switch is needed to make the change live: the Linux CPU configuration defines `STRATA_POOL_SPIN_ZERO` itself (`CMakeLists.txt:2013-2016`). Two arms, one tree: this branch's `src/kernels/cpu/pool.cpp`, and that file replaced by `main`'s (`git show ffa80d73:src/kernels/cpu/pool.cpp`) with nothing else changed. #### What this does not claim No Windows run: the check needs `getrusage`, and its section is compiled only on Linux and only where the change itself is compiled (`src/kernels/cpu/pool_tasks_test.cpp:38`), so a Windows build carries no measurement to assert rather than a measurement that reports itself skipped. No macOS or Metal build. The 1,024-pause count is read from the loop above, not measured: nothing in the tree counts pauses, and adding a counter per pause iteration would cost more than the pause it counts. That part is static reading of the loop; the cost difference in the logs below is an observed run. ### Reproduction status Reproduced locally ### Strata version Strata 0.2.10 · engine 0.1.56, built from source at `ffa80d73` and at the head of #4625 (`f8464bb8`). `main` is now Strata 0.2.11 · engine 0.1.57, so these numbers are from 0.2.10 / 0.1.56. ### Graphics card, memory and system NVIDIA GeForce RTX 5090 Laptop GPU 24 GiB (driver 615.71.09) · 62.2 GiB RAM · Linux 7.0.0-42-generic (Ubuntu 26.04.1) · Intel Core Ultra 9 275HX (24 cores, 24 threads, no AVX-512), gcc 15.2.0 ### Model, size and settings No model is needed for the reproduction: `pool_tasks_test` builds its own Q4_0 expert and its own 8-worker pool, so the two arms differ only in the `pool.cpp` they were built from. The setting under discussion is `STRATA_POOL_SPIN_US=0` against the built-in 20 ms default. The runs were made while a model server was serving on the box (load average 3.0-3.7), which is why the measurement prints and asserts only when asked. Each arm was built one job at a time so the build itself did not load the machine; that is how the runs were made, not a requirement to repeat them. ### Logs With `pool.cpp` from `ffa80d73` and nothing else changed, all three repetitions fail: ``` the best cost 151 ms = 1.96 of the cost of 8192000 plain pauses; the asserted bound is 1.00 the best cost 153 ms = 1.98 of the cost of 8192000 plain pauses; the asserted bound is 1.00 the best cost 147 ms = 1.95 of the cost of 8192000 plain pauses; the asserted bound is 1.00 ``` With the change in #4625, all three pass: ``` the best cost 47 ms = 0.68 of the cost of 8192000 plain pauses; the asserted bound is 1.00 the best cost 27 ms = 0.31 of the cost of 8192000 plain pauses; the asserted bound is 1.00 the best cost 32 ms = 0.46 of the cost of 8192000 plain pauses; the asserted bound is 1.00 ``` With the knob left at its default the two builds cost the same, which is the point of the change: 2019 ms = 7.45 and 2008 ms = 7.05 at the head, 2028 ms = 7.52 and 2030 ms = 7.49 with `pool.cpp` from `ffa80d73`. Checked mechanically with each configuration's own compile command for that file and the flag removed: `pool.cpp` from `ffa80d73` and the one in #4625 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. Windows, macOS/Metal, HIP and SYCL builds therefore compile the same park loop the last release compiles; the Linux CPU and CUDA configurations do compile the new branch, and the default-cost rows above are the measured cost of that. With the change in the tree the pool tests pass unchanged: "360 bitwise comparisons passed, 192 of them in NUMA-split mode". No engine log applies to this finding - it comes from a test binary, not from a served model, so there is no `strata doctor` output or `strata-<model>.log` to attach. The full build and test log for every configuration is attached to pull request #4625.
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#2095
No description provided.