mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #4625] [Engine] STRATA_POOL_SPIN_US=0 means zero: a parked worker with no hot stage goes to the kernel sleep #5118
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#5118
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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 sleepf8464bb[Engine] pool_tasks_test: what a park at STRATA_POOL_SPIN_US=0 costs is measured and printed, and asserted only when asked07115a0Merge 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=0did not mean zero. A parked worker waitskSpinBeforeSleep(20 ms) before it sleeps, and the park loop reads the clock once every 1024 pauses, so a worker parked with the knob at0still ran 1,024strata_cpu_pause()calls before it blocked onsleep_cv_.Before: with the knob at
0a parked worker ran that pause stage and then slept. After: a hot stage of0skips 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 at0a 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 frommain'spool.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 ofmain'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 onmain's file.The change is compiled only in the builds it was built and measured in: CMake defines
STRATA_POOL_SPIN_ZEROfor Linux with the CPU engine or the CUDA engine (CMakeLists.txt:2012-2016), and everywhere elsesrc/kernels/cpu/pool.cpp:1001-1005keepsspin_hota constanttrue, 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.cppfromffa80d73and the one in this branch give a byte-identical.textand.rodatain 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 onlypool_tasks_test parkcost assert(orSTRATA_POOL_PARK_ASSERT=1) can fail a run. It is added tosrc/kernels/cpu/pool_tasks_test.cppwithout 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), andpool_tasks_test parkcostis 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.cppalready run with the knob at0, 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 beforemain'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 atsrc/kernels/cpu/pool.cpp:1022; in a build without the flag the#elsearm makes it a constanttrue, which is the loop the last release ran. The MSVCWAITPKG/_umwaitbranch above it is not touched, and it is only reached whenSTRATA_POOL_UMWAITis set to a positive cycle count on MSVC proper (umwait_cycles,src/kernels/cpu/pool.cpp:43); every other build hasum == 0and takes thespin_hotbranch. Nothing else in the pool, its plan, its header or its documentation changes: the comment on the knob ininclude/strata/kernels/cpu/pool.hppis left as it is, and which builds give0its new meaning is said in the comments atsrc/kernels/cpu/pool.cpp:996-1000andCMakeLists.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 withgetrusageover 8 workers and priced against what the same number ofstrata_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,:126and: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 isSTRATA_POOL_SPIN_US_MICROBENCH(CMakeLists.txt:173).Measured
Both arms at this head:
src/kernels/cpu/pool.cppreplaced bymain's (git show ffa80d73:src/kernels/cpu/pool.cpp), rebuilt at-j 1, run, restored. Knob at0, 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.cppparkcost assertmain(ffa80d73)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:102commented 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'spool.cppin 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
getrusagecounts 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 headf8464bb8(fix32d08246, baseffa80d73= currentmain). 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, souptimeis logged with every park-cost arm.Passed
cmake --build -j 1rc=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 isisa_guard_check, below).-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 ispool_park_cost, and it passes.pool_tasks_testin every configuration built here: "360 bitwise comparisons passed, 192 of them in NUMA-split mode".CMAKE_CUDA_ARCHITECTURES=120, host compiler gcc 15.2.0): configure rc=0, 36 rules carry the flag,cmake --build -j 1rc=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 1in 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 registerspool_park_costas 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.cppis compiled by the host compiler in this configuration too - it is not a.cufile - which is why the object check below, not an nvcc invocation, is the evidence about the emitted code.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 carryingSTRATA_POOL_SPIN_ZERO, 188 tests registered,cmake --build -j 1rc=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 HIPctest -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 arehip_prompt_attn_wmma,ple_parity,cvec_parityandisa_guard_check.cmake -S sycl -B <tree> -DCMAKE_CXX_COMPILER=icx -DCMAKE_C_COMPILER=icx, assycl/CMakeLists.txt:9-10documents; oneAPI 2026.2icx/icpx): configured fresh atf8464bb8, rc=0, 260 build edges, 56 tests registered, and 0 rules carryingSTRATA_POOL_SPIN_ZERO.cmake --build -j 1rc=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 itspool.cppobject is unchanged (below). Its fullctestis in Failed.gfx906variant (-DSTRATA_HIP_GFX906=ONwithSTRATA_ENABLE_HIP=OFF, which is whatCMakeLists.txt:89requires, plus-DCMAKE_HIP_FLAGS=--rocm-device-lib-path=/usr/lib/llvm-21/lib/clang/21/amdgcn/bitcode): configure rc=0, 358 rules defineSTRATA_HIP_GFX906=1, 205 useoffload-arch=gfx906, and 0 defineSTRATA_POOL_SPIN_ZERO. The pool cone builds at-j 1andctest -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 atffa80d73- see Failed.pool.cppffa80d73vs this head ->.textidentical (CPU configuration 74,859 bytes; HIP configuration 74,132; SYCL configuration 100,311) and.rodataidentical (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.cppthe same way ->.textidentical at 14,314 bytes and.rodataat 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.CMakeLists.txt:2013replaced byif(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.ninjacarries 0 flag rules and registerspool_park_cost0 times,pool_tasks_teststill reports "360 bitwise comparisons passed, 192 of them in NUMA-split mode",pool_tasks_test parkcostandpool_tasks_test parkcost assertboth exit 2 with no output at all, andctest -R "pool_tasks_test|pool_park_cost" -j 1-> "100% tests passed, 0 tests failed out of 1".CMakeLists.txtwas restored afterwards and the repository is clean.tests/terminal, "2553 tests collected") run atf8464bb8from a clean worktree (git status --porcelainempty) with the pinned environment (textual==8.2.8atrequirements-monitor.txt:2and atpackaging/locks/runtime-linux-x86_64-cp312.txt:41,regex==2026.9.10at:35of 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 CUDActestruns; 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'sTesting/Temporary/LastTest.log, verbatim, with the build-tree path written as<tree>:Both sit in
src/kernels/cpu/iq_avx512.cpp, a file this patch does not touch. It fails the same way atffa80d73: 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 1returns 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 twoVIOLATIONlines. The exit status is 1 at both revisions. This change does not cause it and does not change it. Five other tests reportSkippedhere 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), andnative_hadamard_test,tq_cpu_parity,tq_cpu_parity_avx2want trellis ggml types this build does not carry. None of them touchespool.cpp.main'spool.cpp- intended, that is the point of the measurement:pool_tasks_test parkcost assertexits 1 at 1.95-1.98 of the bound, three times out of three.ctest -j 1 --timeout 300 --output-on-failurein the fresh tree -> "38% tests passed, 35 tests failed out of 56" - 9***Failed, 26Subprocess 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 isple_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 atffa80d73(configure rc=0, 260 edges, 56 tests, 0 flag rules,cmake --build -j 1rc=0[260/260]), the same suite gives "39% tests passed, 34 tests failed out of 56" - 9***Failed, 25Subprocess aborted***Exception, 0 Not Run, and 2***Skipped: thereiq_parity_fixturesexits 3, which is its own skip code (sycl/CMakeLists.txt:360-362), because that checkout has no vendoredgguf-py("iq_fixture: missing dependency: No module named 'gguf'"), andiq_parityskips with it; at this head the fixture succeeds andiq_paritythen 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 coverspool.cpp.gfx906variant does not compile, and it is not this patch: at-j 1src/core/dflash2.cppfails 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 atffa80d73with the identical command line gives the same two errors, so it is confirmed pre-existing.dflash2.cppandinclude/strata/platform/hip_compatare not in this patch; the last commit to touch them isf4561cbe. I propose no fix here - it belongs to that variant's owner.ffa80d73. Run as one group of four in a clean worktree of each revision (git status --porcelainempty in both): atffa80d73-> "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" attests/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, subtestsignal='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.tests/terminal/test_updater.py::EngineUpdate::test_an_interrupted_download_resumesfailed in the 2553-test run, but on its ownpytest 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/bitcodewas 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"). Beforerocm-device-libs-21was 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 whatdocs/AMD_HIP.md:107tells a user to install, and every number above was built with it.Not run
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.CUDA_VISIBLE_DEVICES=""), the HIP box has no AMD device, and the SYCL box sees onlyopencl:cpu. So no CUDA, HIP or SYCL kernel result is claimed anywhere in this text.ctestresults that reportSkippedon 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", andtq_cpu_parityandtq_cpu_parity_avx2-> "this ggml has no trellis types (build with STRATA_GGML_DIR = the agention llama.cpp fork): skipped". None of the five touchespool.cpp.gfx906variant'spool_park_costand 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 fullctest -j 1there -> "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.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
0changes 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.