fix(agc): reserve the shader preparation thread stacks on Windows instead of committing them #6598
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6598"
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?
Imported from boykopovar/AnyPS5. Originally posted by zaryabzip on 2026-10-15T22:17:47Z.
What
RegistrationPreparePoolcreates its threads with a 64 MiB stack throughpthread_attr_setstacksize. With winpthreads that size reaches_beginthreadexwithoutSTACK_SIZE_PARAM_IS_A_RESERVATION, and then_beginthreadexuses it as the commit size: "if this flag isn't specified, stack_size specifies the commit size". Committed stack pages "remove pages from the system total commit limit" (Thread Stack Size). So every pool thread takes 64 MiB of commit before it prepares anything.There are
hardware_concurrency() / 2preparation threads plus one library prebuild thread: 9 threads and 576 MiB on a 16-thread CPU.On Windows the pool threads are now created with
_beginthreadex(..., StackBytes, ..., STACK_SIZE_PARAM_IS_A_RESERVATION, ...):SizeOfStackCommit, 2 MiB in a relinkedapp.exe), and further pages are committed as the stack grows.libkernelscePthreadCreate) are unchanged.pthread_attr_setstacksizepath.Tested
Windows 11 (26200), i7-11800H, 16 GB RAM, NVIDIA GeForce RTX 3060 Laptop GPU (driver 617.42), WinLibs GCC 15.2.0, Release,
main5d9dcc42d.agc_driver_registration_prepare_stack(tests/execution/RegistrationPrepareStack.cpp):It measures the stack of a registration preparation thread and of the library prebuild thread from inside a task.
Everywhere: the task runs off the caller's thread, with at least 64 MiB reserved.
On Windows: at most 8 MiB committed.
With
main'sShaderRegistry.cppit fails:With this change it passes.
agc_driver_registration_prepare_shutdown,agc_driver_prepared_shaders(all 9 variants),agc_driver_prepared_publicationandagc_driver_preparation_scope_settle, 13 of 13.tools/check_conventions.pypasses.tools/pr_overlap.py --branchreports no conflicting PR. #1896, #5115 and #5431 could not be compared automatically; their diffs do not touch the pool's thread creation.VirtualQueryEx:main: 7 pool threads × 64 MiB committed = 448 MiB.main: all 3 runs reach the 7.00 GiB cap.One ends at 12.0 s:
One ends at 8.5 s with
std::bad_allocinlibSceAgcDriver.prx.One stays at the cap until the time limit.
Process commit at 5 s: 5,888 and 5,942 MiB (3,111 MiB in the slower first run).
main34454bd83with the same change: the commit plateau at the main menu falls from 6.89 to 6.45 GiB.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T22:28:20Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppcore/libs/prx/libSceAgcDriver/Execution/src/Driver/Shaders/ShaderRegistry.cppImported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T12:07:27Z.
Cross-validation on Windows 11 Pro 26220, NVIDIA RTX 4070 (driver 596.21), Ryzen 7 5700X (16 threads), WinLibs MinGW-w64 GCC 15.2, Release. PR head
bf55d23ffmerged on main673f7406c(clean), alone and stacked with #6701 (also clean).agc_driver_registration_prepare_stack,--repeat until-fail:3: 3 of 3. Negative control: withShaderRegistry.cppfrom main and the test kept, it fails withregistration prepare stack: registration preparation thread commits 64 MiB of its 64 MiB stack up front; restored, it passes.libspass on the stack. 25 related tests (write watch, registration preparation, allagc_driver_prepared_*, host import, untracked hints; serial) pass. Non-GPU suite (878 tests): 874 pass, 2 skipped;hardware_oracle_comparetimed out under load andguest_memoryfailed once under-j8(0xc0000409, alibkerneltest that does not load the driver); both pass serially.tools/check_conventions.pyandgit diff --checkpass.PrivateUsage, cross-checked withGet-Process) and working set every 5 s; pool stacks counted at 35 s from each thread's TEB andVirtualQueryEx. Medians, MiB:On this 16-thread CPU the 9 pool stacks (8 preparation + 1 prebuild) commit exactly 576.0 MiB on main in every run and 18.1 MiB with the PR (the relinked
app.exehas a 2 MiBSizeOfStackCommit, times 9). Process private bytes drop by 600 / 485 / 490 MiB at 10 / 30 / 60 s; the per-run values at 30 s do not overlap (main 3697 to 3850, PR 3200 to 3233). Working set does not move (+1 MiB), as expected for committed but untouched stack pages.agc_driver_*GPU suite. System-wide commit was no use as a signal on this shared host (it moved by several GiB per run from other work).Written with Claude Code assistance (validation by a subagent, reviewed in this session).
Imported from boykopovar/AnyPS5. Originally posted by savvaniss on 2026-10-17T13:10:18Z.
Intel and AMD Windows data: the new test passes on both GPUs with this change and fails on both without it.
Windows 11 Pro 26300, WinLibs GCC 15.2.0 r7, Release, every execution test built,
ANYPS5_NO_SHADER_CACHE=1, serial CTest. Intel UHD Graphics 770 (8086:a780), driver 32.0.101.7088. AMD Radeon RX 9070 XT (1002:7550), driver 32.0.31041.1004, withVkCacheemptied first. "Without" means this PR's test files onmainc9e6c45de, with none of its other changes.bf55d23ffmerged intomainc9e6c45de:agc_driver_registration_prepare_stack, ×3registration preparation thread commits 64 MiB of its 64 MiB stack up frontFull CTest (
--parallel 4) onmainc9e6c45dewith seven open GPU PRs merged (#6258, #6657, #6324, #6246, #6174, #6360, #6598): UHD 770 1381 of 1389, RX 9070 XT 1383 of 1389.mainalone passes 1378 of 1385 and 1381 of 1385. The merge adds only two failures:agc_buffer_pool(#6174, both GPUs) andagc_driver_indirect_dispatch_limit(#6360, RX 9070 XT).AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T06:36:09Z.
Review: CHANGES_REQUESTED
Thanks for the measurements; the commit-vs-reserve diagnosis looks right and the new test pins it on Windows. One bug in the Windows branch of the
RegistrationPreparePoolconstructor:ShaderRegistry.cpp:1187-1194: the_beginthreadexpath only does++threads. Thepthread_createpath also doesworkers.push_back(thread)andliveWorkers.fetch_add(1). On Windows that means:Close()(l.1170-1178) joins only what is inworkers, so it waits for no pool thread; they can still be runningMainwhile the pool or process tears down.Mainstill doesliveWorkers.fetch_sub(1)(l.1227) on exit, but nothing incremented it, so theunsignedcounter underflows andLiveWorkers()(l.1101) returns a huge value.WaitForSingleObject+CloseHandleinClose(), skipping the calling thread) and doliveWorkers.fetch_add(1)on success like the other path. Please also assert in the test (or inagc_driver_registration_prepare_shutdown) thatLiveWorkers()is 0 afterCloseAll(), so Windows CI covers it.Imported from boykopovar/AnyPS5. Originally posted by zaryabzip on 2026-10-19T21:57:23Z.
Fixed in
afcfe2d9e, rebased onmaina70080967.workersand raisesliveWorkerswhen it creates a thread.Close()waits for each handle (not the calling thread) and closes it.agc_driver_registration_prepare_stacknow requiresRegistrationPreparationThreads()to equal the threads both pools started, and 0 afterCloseRegistrationPreparation().Tested: the pool class alone on macOS under ThreadSanitizer (POSIX path, 5 runs), and the Windows path cross-compiled with MinGW-w64 GCC 16.2 (compiles and links, no warnings). Not yet run on Windows.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T14:26:34Z.
Checked the Windows branch of
RegistrationPreparePool: handles and ids are kept inworkers,liveWorkersis raised on creation,Close()waits for each handle (skipping the calling thread) and closes it, and the test pins the live count against the started threads and 0 after close; merged.