perf(agc): replace winpthreads locks and TLS on the draw path #6292

Merged
meh7an merged 1 commit from upstream-pr/6292 into main 2026-10-23 09:54:00 +00:00
meh7an commented 2026-10-15 09:26:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-15T09:26:26Z.


What

On Windows the driver is built with MinGW:

  • std::mutex locks and unlocks through winpthreads (pthread_mutex_lock, pthread_mutex_unlock);
  • thread_local is emulated: every access calls __emutls_get_address, which calls pthread_getspecific.

A draw takes a few dozen different mutexes, and every packet reads several thread-locals. On the S2 page of Sonic Origins Plus's main menu, the queue worker spent these samples per frame (APS5 sampler, 15 s): pthread_mutex_lock 1.22, pthread_mutex_unlock 0.82, __emutls_get_address 2.15 (1.80 of it in pthread_getspecific), and the device use gate 0.37. That is about 10% of its frame.

  • AgcDriver::Mutex (Execution/include/Mutex.hpp) is an inline mutex on one atomic word.
    • An uncontended lock or unlock is one atomic instruction. A contended lock waits on the word at once (std::atomic::wait, which spins 12 pauses and 4 yields itself before it sleeps). Like std::mutex, it is neither recursive nor fair.
    • The first version spun 100 pauses before waiting. @simonbronk's contention benchmark showed that this costs 3 to 7 times the CPU of std::mutex with 4 to 8 threads on one lock, and that waiting at once beats std::mutex at every thread count. Without the spin, main's agc_driver_multisample_fill_host_only failed most runs: waiters that sleep at once widen a race main already has (#6512), a multisampled target's clear recorded on another device's recorder. #6522 fixes that race, so this PR depends on it. The game numbers below come from the spinning version; on that page the contended path takes no samples (0.00 per frame in three sampled runs of my fork's dev), so they hold.
    • It replaces 74 plain std::mutexes of the draw path. #1201 already made two more a SlimMutex (the pending storage writes and the unit shadows), and those stay as they are.
    • Seven stay std::mutex: the five a std::condition_variable waits with, and the sparse and settle queue mutexes, which the context hands out as std::mutex*.
  • DeviceUseGate keeps its user count and the replacement flag in one atomic word instead of a mutex and a condition variable. A packet enters and leaves with one atomic instruction each.
  • GpuMutex takes an AgcDriver::Mutex and counts its own recursion. Its owner token is the thread environment block's address (CurrentThreadToken) instead of a thread-local's.
  • HostThreadSlot (libc/include/HostThreadSlot.hpp) holds a per-thread value of up to 8 bytes.
    • On x64 Windows it is a TLS slot (TlsAlloc), read and written through the thread environment block in one or two instructions. Elsewhere it is a thread_local.
    • It holds the packet tag, the read site, the collect epoch, and the pointers to the thread's collect memo and deferred labels (still allocated with ThreadOwned).
  • Driver::timed reads no clock unless APS5_PROFILE_DRAW asks for its report. A wait packet reads it only for APS5_TRACE_GPU's slow-wait line (suggested by @simonbronk).
  • agc_locks (tests/Locks.cpp, a plain CPU test, so the Windows CI job runs it too) checks:
    • the mutex under contention (short and long holds), its try_lock, and a sleeping waiter's wake;
    • the gate: no user inside during a replacement, one replacement at a time;
    • the GPU mutex's recursion across threads;
    • the thread slots: zero on a new thread, direct and expansion slots, and the packet tag and read site defaults.

With this change, per frame: pthread_mutex_lock 1.22 to 0.31, pthread_mutex_unlock 0.82 to 0.20, __emutls_get_address 2.15 to 1.48, and the device use gate 0.37 to 0.05. What remains is the mutexes kept or outside the draw path, and thread-locals this change does not touch.

Tested

Windows 11 (10.0.26300), WinLibs GCC 15.2.0 (UCRT), Release, NVIDIA GeForce RTX 2080 Ti, driver 617.14.

  • Rebased onto main 53a0a27bb.
    • Main's 84e3b73f1 made the depth surfaces' surfacesMutex a std::recursive_mutex, and its callers lock it again while they hold it. It stays a std::recursive_mutex and is not switched to AgcDriver::Mutex.
    • The buffer pool keeps main's new member defaults and image budget fields next to AgcDriver::Mutex.
    • On that main, with this change, the full ctest suite passes 1836 of 1838.
      • agc_driver_image_array_window_past_layer_limit is not run: MinGW does not build its alignas(65536).
      • guest_kqueue exited with 0xc0000409 once under --parallel 4. It passed 6 of 6 runs alone.
  • Rebased onto main 1d4164552.
    • IndirectDispatch.cpp keeps main's GuestBufferMemory.hpp include, and the buffer pool keeps main's memory property fields, next to AgcDriver::Mutex.
    • Main moved the mismatch reports to Snapshots().mismatched, so GuestBufferMemory.cpp only turns reportMutex into an AgcDriver::Mutex.
    • On that main, with this change, the full ctest suite passes 1740 of 1742.
      • agc_driver_image_array_window_past_layer_limit is not run: main does not compile ImageArrayWindowPastLayerLimit.cpp with MinGW (alignas(65536)).
      • agc_driver_pass_input_hazard (added in ba4571a25) fails ("a pass writing the indices an open pass reads in place took no barrier") on plain main 1d4164552 as well, 0 of 4 runs alone.
  • Rebased onto main 0bc3003f8.
    • Main's new AdoptLatestCollectEpoch and lastBumpedEpoch read the collect epoch through the HostThreadSlot.
    • The GPU mutex keeps main's handoff spin (LockSpinning) inside its recursion guard.
    • dumpRequest's new suffix and the descriptor pool's freeDescriptorSets stay, with AgcDriver::Mutex.
    • AcquireCache takes the AgcDriver::Mutex.
    • On that main, with this change, the full ctest suite passes 1522 of 1523. agc_driver_bda_device fails (vkWaitForFences graphics: Vulkan result -4) on plain main 0bc3003f8 as well, 0 of 4 runs alone.
  • Rebased onto main c84249186. Next to drawCacheMutex, main added the draw relocation fields (#4943), and #5875 keyed the compute pipelines by ComputePipelineKey and dropped the pipeline store's scratchKey. drawCacheMutex, computePipelinesMutex and the pipeline store's mutex stay AgcDriver::Mutexes, and every lock of them is a std::lock_guard or std::unique_lock as before. The collect memo ring goes through its thread slot as before. #1201's per-epoch collect map next to it keeps its thread_local, like the other thread-locals main added since. tools/check_conventions.py passes: HostThreadSlot takes TlsAlloc and TlsSetValue from <processthreadsapi.h>, and the GPU mutex's unlock keeps main's comment where it was.
    • On that main, with this change, the full ctest suite passes 1322 of 1323 (the one not run is agc_driver_gpu_indirect_arguments_misaligned_writer_ahead, which main itself does not compile with MinGW), including the new agc_driver_locks.
    • With #6522 as well, agc_driver_multisample_fill_host_only failed 0 of 200 runs (8 at a time) on main 72c5eed1f. On 44df21632 it also failed 0 of 400 with APS5_PIPELINED_DRAWS=0 (4 at a time) and 0 of 40 alone, and without #6522's line it failed 40 of 200 and 7 of 40.
  • Sonic Origins Plus, main menu, S2 page (the 3D background), 1233x694 window.
    • A sampled pair (APS5 sampler, 15 s) took the profile numbers above. The FPS shown in the title went from 7.75 to 8.38 (median of six readings).
    • Captured with APS5_TRACE_SECONDS=200:15 on the same machine in one session, caches warm. Traces attached.
    • Baseline b8acbb0d6, this change 824084ed7 (the summaries name the commits before them, see the note):
Metric Baseline This PR Change
frame_ms.p50 156.721 129.749 -17.2%
frame_ms.p90 163.045 139.339 -14.5%
mean_frame_ms 158.259 130.959 -17.3%
gpu_busy_ms_per_present 78.608 85.850 +9.2%
Driver.Packet (ms per frame) 155.677 128.738 -17.3%
Driver.Packet.draw (ms per frame) 118.731 117.440 -1.1%

This pair overstates the change. The baseline's window ran slow outside the draw path: Driver.Packet minus its draw part is 37.0 ms against 11.3 ms with this change. Its title FPS, 6.70, is also below every other run of that build (7.51 to 8.34). The sampled pair, 7.75 to 8.38 FPS (+8%), is the better estimate.

Note: measuring base. These numbers come from my fork's dev, on main 0a56aaaf4, the last main before #5596. On Windows, later mains compare direct imports on every use, which keeps this menu at 3-4 FPS and hides a change of this size.

  • The baseline is measure/coarse-2026-10-15 (b8acbb0d6).
  • This change is measure/locks-2026-10-15 (824084ed7).
  • Each build was made before its commit, so the summaries name the commit before it, dirty: a38ad5e9f for the baseline and b8acbb0d6 for this change.

Both carry these commits from my fork:

  • the fixes a71073ce3, c0d875634, e3c68960e, 604f0f373 and 10ff693aa (#5778);
  • make-exe and the stage dumps (522230d62, deee71fde);
  • the crash-report sampler (8d9c3103e..f641e1ec9), which took the profiles;
  • #6037, #6066, #6077, #6067, #6082 (its first version, before #5906), #6095 and #6266;
  • seven smaller steps kept in my fork: e83260247, 97b1ff6c9, 84f5bf0ff, 52704cf7c (covered on main by #5062), c0c3a6b58, 467f66896 and f7a41addb.

The comparison tool marks the window's first present (2260 vs 2475) as a configuration difference: the window opens 200 s into each run. Both summaries set trace_dropped, because the worker filled its event buffer at about 2,700 draws per frame. The phase totals above stay complete.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #6522
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 2080 Ti, driver 617.14, Windows 11
  • AI-assisted: yes

Traces and summary lines (baseline b8acbb0d6, this change 824084ed7):
locks-traces.zip

The two summary lines, with tools/trace_digest.py for each trace:

Baseline (summary commit a38ad5e9fc89)

seconds 200:15 from present 2260: 95 presents, frame p50 156.7 / p90 163.0 / p99 190.3 / max 190.3 ms, GPU busy 78.6 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit a38ad5e9fc89 (dirty), dropped: yes

{"commit":"a38ad5e9fc89","dirty":true,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"200:15"},"gpu":"NVIDIA GeForce RTX 2080 Ti","driver":"NVIDIA 617.14","os":"Windows 10.0.26300","resolution":"3840x2160","window":{"spec":"seconds 200:15","first_present":2260,"ms":15034.646},"presents":95,"frame_ms":{"p50":156.721,"p90":163.045,"p99":190.275,"max":190.275},"gpu_busy_ms_per_present":78.608,"worker_busy_pct":{"queue worker 0x0":98.389},"top_phases_ms_per_frame":{"Driver.Packet":155.677,"Driver.Packet.draw":118.731,"Driver.Draw":117.267,"Driver.Draw.draw_and_resource_release":54.084,"Graphics.Draw":52.74,"Driver.Draw.shader_compile_and_link":45.397,"Graphics.Draw.draw_and_resource_release":42.561,"Shader.DrawStage":38.974},"trace_dropped":true,"trace":"aps5-trace-a38ad5e9fc89-37256.json.gz"}

Slowest frames (largest slices inside each, inclusive)

  • present 2288: 190.3 ms at 4435 ms: batch (gpu) 81.7, Driver.Present (presenter) 26.2, Driver.Present.render_fence_wait (presenter) 25.9, Vulkan.Retire (presenter) 25.9
  • present 2270: 183.1 ms at 1586 ms: Driver.Packet (queue worker 0x0) 172.8, Driver.Packet.draw (queue worker 0x0) 144.6, Driver.Draw (queue worker 0x0) 143.1, batch (gpu) 80.2
  • present 2316: 180.7 ms at 8871 ms: batch (gpu) 76.3, Driver.Present (presenter) 23.3, Driver.Present.render_fence_wait (presenter) 22.8, Vulkan.Retire (presenter) 22.8
  • present 2342: 180.1 ms at 12960 ms: batch (gpu) 75.6, Driver.Present (presenter) 27.0, Driver.Present.render_fence_wait (presenter) 26.5, Vulkan.Retire (presenter) 26.5
  • present 2326: 178.7 ms at 10450 ms: batch (gpu) 76.4, Driver.Present (presenter) 23.4, Driver.Present.render_fence_wait (presenter) 23.0, Vulkan.Retire (presenter) 23.0

Threads (ms per frame)

thread busy wait untraced in slices < 20 us largest
queue worker 0x0 34.3 (22 %) 0.1 (0 %) 123.8 51.7 Driver.Packet 34.4, Driver.Draw 0.0
presenter 0.5 (0 %) 26.5 (17 %) 131.3 0.1 Driver.Present 27.0
gpu 78.5 (50 %) 0.0 (0 %) 79.8 0.0 batch 78.5

Largest slices (ms per frame, inclusive, all threads): batch 78.5, Driver.Packet 34.4, Driver.Packet.draw 27.8, Driver.Draw 27.5, Driver.Present 27.0, Driver.Present.render_fence_wait 26.4, Vulkan.Retire 26.4, Vulkan.Retire.wait 26.4, Driver.Draw.draw_and_resource_release 6.1, Graphics.Draw 6.0

Longest waits

  • Driver.Present.render_fence_wait (presenter) 31.6 ms at 4790 ms, present 2290
  • Driver.Present.render_fence_wait (presenter) 30.6 ms at 4626 ms, present 2289
  • Driver.Present.render_fence_wait (presenter) 29.7 ms at 1107 ms, present 2267
  • Driver.Present.render_fence_wait (presenter) 29.6 ms at 4121 ms, present 2286
  • Driver.Present.render_fence_wait (presenter) 29.5 ms at 1 ms, present 2260
  • Driver.Present.render_fence_wait (presenter) 29.4 ms at 1270 ms, present 2268
  • Driver.Present.render_fence_wait (presenter) 29.2 ms at 3009 ms, present 2279
  • Driver.Present.render_fence_wait (presenter) 29.2 ms at 4277 ms, present 2287
  • Driver.Present.render_fence_wait (presenter) 29.2 ms at 14237 ms, present 2350
  • Driver.Present.render_fence_wait (presenter) 29.1 ms at 7324 ms, present 2306
This change (summary commit b8acbb0d65c3)

seconds 200:15 from present 2475: 115 presents, frame p50 129.7 / p90 139.3 / p99 153.5 / max 153.5 ms, GPU busy 85.8 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit b8acbb0d65c3 (dirty), dropped: yes

{"commit":"b8acbb0d65c3","dirty":true,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"200:15"},"gpu":"NVIDIA GeForce RTX 2080 Ti","driver":"NVIDIA 617.14","os":"Windows 10.0.26300","resolution":"3840x2160","window":{"spec":"seconds 200:15","first_present":2475,"ms":15060.273},"presents":115,"frame_ms":{"p50":129.749,"p90":139.339,"p99":153.497,"max":153.537},"gpu_busy_ms_per_present":85.85,"worker_busy_pct":{"queue worker 0x0":98.33},"top_phases_ms_per_frame":{"Driver.Packet":128.738,"Driver.Packet.draw":117.44,"Driver.Draw":116.398,"Driver.Draw.draw_and_resource_release":52.218,"Graphics.Draw":51.006,"Driver.Draw.shader_compile_and_link":45.265,"Graphics.Draw.draw_and_resource_release":41.533,"Shader.DrawStage":39.233},"trace_dropped":true,"trace":"aps5-trace-b8acbb0d65c3-37744.json.gz"}

Slowest frames (largest slices inside each, inclusive)

  • present 2580: 153.5 ms at 13736 ms: batch (gpu) 89.8, Driver.Present (presenter) 34.2, Driver.Present.render_fence_wait (presenter) 33.8, Vulkan.Retire (presenter) 33.8
  • present 2492: 153.5 ms at 2205 ms: Driver.Packet (queue worker 0x0) 144.4, Driver.Packet.draw (queue worker 0x0) 139.3, Driver.Draw (queue worker 0x0) 138.3, batch (gpu) 88.2
  • present 2573: 152.5 ms at 12812 ms: batch (gpu) 88.0, Driver.Present (presenter) 31.5, Driver.Present.render_fence_wait (presenter) 31.0, Vulkan.Retire (presenter) 31.0
  • present 2544: 150.9 ms at 8988 ms: batch (gpu) 85.6, Driver.Present (presenter) 32.0, Driver.Present.render_fence_wait (presenter) 31.5, Vulkan.Retire (presenter) 31.5
  • present 2521: 147.8 ms at 5947 ms: batch (gpu) 82.9, Driver.Present (presenter) 31.8, Driver.Present.render_fence_wait (presenter) 31.2, Vulkan.Retire (presenter) 31.2

Threads (ms per frame)

thread busy wait untraced in slices < 20 us largest
queue worker 0x0 23.5 (18 %) 0.1 (0 %) 107.4 42.1 Driver.Packet 23.6, Driver.Completion 0.0
presenter 0.5 (0 %) 31.5 (24 %) 99.0 0.1 Driver.Present 32.0
gpu 85.7 (65 %) 0.0 (0 %) 45.2 0.0 batch 85.7

Largest slices (ms per frame, inclusive, all threads): batch 85.7, Driver.Present 32.0, Driver.Present.render_fence_wait 31.4, Vulkan.Retire 31.4, Vulkan.Retire.wait 31.4, Driver.Packet 23.6, Driver.Packet.draw 22.7, Driver.Draw 22.5, Driver.Draw.draw_and_resource_release 4.9, Graphics.Draw 4.8

Longest waits

  • Driver.Present.render_fence_wait (presenter) 37.0 ms at 2359 ms, present 2493
  • Driver.Present.render_fence_wait (presenter) 36.6 ms at 2489 ms, present 2494
  • Driver.Present.render_fence_wait (presenter) 36.4 ms at 11618 ms, present 2564
  • Driver.Present.render_fence_wait (presenter) 36.0 ms at 14151 ms, present 2583
  • Driver.Present.render_fence_wait (presenter) 35.7 ms at 1282 ms, present 2485
  • Driver.Present.render_fence_wait (presenter) 35.7 ms at 10582 ms, present 2556
  • Driver.Present.render_fence_wait (presenter) 35.4 ms at 1022 ms, present 2483
  • Driver.Present.render_fence_wait (presenter) 35.3 ms at 12273 ms, present 2569
  • Driver.Present.render_fence_wait (presenter) 34.9 ms at 382 ms, present 2478
  • Driver.Present.render_fence_wait (presenter) 34.5 ms at 3151 ms, present 2499
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4871082596 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292). Originally posted by [meh7an](https://github.com/meh7an) on 2026-10-15T09:26:26Z. --- ### What On Windows the driver is built with MinGW: - `std::mutex` locks and unlocks through winpthreads (`pthread_mutex_lock`, `pthread_mutex_unlock`); - `thread_local` is emulated: every access calls `__emutls_get_address`, which calls `pthread_getspecific`. A draw takes a few dozen different mutexes, and every packet reads several thread-locals. On the S2 page of Sonic Origins Plus's main menu, the queue worker spent these samples per frame (APS5 sampler, 15 s): `pthread_mutex_lock` 1.22, `pthread_mutex_unlock` 0.82, `__emutls_get_address` 2.15 (1.80 of it in `pthread_getspecific`), and the device use gate 0.37. That is about 10% of its frame. - `AgcDriver::Mutex` (`Execution/include/Mutex.hpp`) is an inline mutex on one atomic word. - An uncontended lock or unlock is one atomic instruction. A contended lock waits on the word at once (`std::atomic::wait`, which spins 12 pauses and 4 yields itself before it sleeps). Like `std::mutex`, it is neither recursive nor fair. - The first version spun 100 pauses before waiting. @simonbronk's contention benchmark showed that this costs 3 to 7 times the CPU of `std::mutex` with 4 to 8 threads on one lock, and that waiting at once beats `std::mutex` at every thread count. Without the spin, main's `agc_driver_multisample_fill_host_only` failed most runs: waiters that sleep at once widen a race main already has (#6512), a multisampled target's clear recorded on another device's recorder. #6522 fixes that race, so this PR depends on it. The game numbers below come from the spinning version; on that page the contended path takes no samples (0.00 per frame in three sampled runs of my fork's dev), so they hold. - It replaces 74 plain `std::mutex`es of the draw path. #1201 already made two more a `SlimMutex` (the pending storage writes and the unit shadows), and those stay as they are. - Seven stay `std::mutex`: the five a `std::condition_variable` waits with, and the sparse and settle queue mutexes, which the context hands out as `std::mutex*`. - `DeviceUseGate` keeps its user count and the replacement flag in one atomic word instead of a mutex and a condition variable. A packet enters and leaves with one atomic instruction each. - `GpuMutex` takes an `AgcDriver::Mutex` and counts its own recursion. Its owner token is the thread environment block's address (`CurrentThreadToken`) instead of a thread-local's. - `HostThreadSlot` (`libc/include/HostThreadSlot.hpp`) holds a per-thread value of up to 8 bytes. - On x64 Windows it is a TLS slot (`TlsAlloc`), read and written through the thread environment block in one or two instructions. Elsewhere it is a `thread_local`. - It holds the packet tag, the read site, the collect epoch, and the pointers to the thread's collect memo and deferred labels (still allocated with `ThreadOwned`). - `Driver::timed` reads no clock unless `APS5_PROFILE_DRAW` asks for its report. A wait packet reads it only for `APS5_TRACE_GPU`'s slow-wait line (suggested by @simonbronk). - `agc_locks` (`tests/Locks.cpp`, a plain CPU test, so the Windows CI job runs it too) checks: - the mutex under contention (short and long holds), its `try_lock`, and a sleeping waiter's wake; - the gate: no user inside during a replacement, one replacement at a time; - the GPU mutex's recursion across threads; - the thread slots: zero on a new thread, direct and expansion slots, and the packet tag and read site defaults. With this change, per frame: `pthread_mutex_lock` 1.22 to 0.31, `pthread_mutex_unlock` 0.82 to 0.20, `__emutls_get_address` 2.15 to 1.48, and the device use gate 0.37 to 0.05. What remains is the mutexes kept or outside the draw path, and thread-locals this change does not touch. ### Tested Windows 11 (10.0.26300), WinLibs GCC 15.2.0 (UCRT), Release, NVIDIA GeForce RTX 2080 Ti, driver 617.14. - Rebased onto main 53a0a27bb. - Main's 84e3b73f1 made the depth surfaces' `surfacesMutex` a `std::recursive_mutex`, and its callers lock it again while they hold it. It stays a `std::recursive_mutex` and is not switched to `AgcDriver::Mutex`. - The buffer pool keeps main's new member defaults and image budget fields next to `AgcDriver::Mutex`. - On that main, with this change, the full ctest suite passes 1836 of 1838. - `agc_driver_image_array_window_past_layer_limit` is not run: MinGW does not build its `alignas(65536)`. - `guest_kqueue` exited with 0xc0000409 once under `--parallel 4`. It passed 6 of 6 runs alone. - Rebased onto main 1d4164552. - `IndirectDispatch.cpp` keeps main's `GuestBufferMemory.hpp` include, and the buffer pool keeps main's memory property fields, next to `AgcDriver::Mutex`. - Main moved the mismatch reports to `Snapshots().mismatched`, so `GuestBufferMemory.cpp` only turns `reportMutex` into an `AgcDriver::Mutex`. - On that main, with this change, the full ctest suite passes 1740 of 1742. - `agc_driver_image_array_window_past_layer_limit` is not run: main does not compile `ImageArrayWindowPastLayerLimit.cpp` with MinGW (`alignas(65536)`). - `agc_driver_pass_input_hazard` (added in ba4571a25) fails ("a pass writing the indices an open pass reads in place took no barrier") on plain main 1d4164552 as well, 0 of 4 runs alone. - Rebased onto main 0bc3003f8. - Main's new `AdoptLatestCollectEpoch` and `lastBumpedEpoch` read the collect epoch through the `HostThreadSlot`. - The GPU mutex keeps main's handoff spin (`LockSpinning`) inside its recursion guard. - `dumpRequest`'s new suffix and the descriptor pool's `freeDescriptorSets` stay, with `AgcDriver::Mutex`. - `AcquireCache` takes the `AgcDriver::Mutex`. - On that main, with this change, the full ctest suite passes 1522 of 1523. `agc_driver_bda_device` fails (`vkWaitForFences graphics: Vulkan result -4`) on plain main 0bc3003f8 as well, 0 of 4 runs alone. - Rebased onto main c84249186. Next to `drawCacheMutex`, main added the draw relocation fields (#4943), and #5875 keyed the compute pipelines by `ComputePipelineKey` and dropped the pipeline store's `scratchKey`. `drawCacheMutex`, `computePipelinesMutex` and the pipeline store's mutex stay `AgcDriver::Mutex`es, and every lock of them is a `std::lock_guard` or `std::unique_lock` as before. The collect memo ring goes through its thread slot as before. #1201's per-epoch collect map next to it keeps its `thread_local`, like the other thread-locals main added since. `tools/check_conventions.py` passes: `HostThreadSlot` takes `TlsAlloc` and `TlsSetValue` from `<processthreadsapi.h>`, and the GPU mutex's unlock keeps main's comment where it was. - On that main, with this change, the full ctest suite passes 1322 of 1323 (the one not run is `agc_driver_gpu_indirect_arguments_misaligned_writer_ahead`, which main itself does not compile with MinGW), including the new `agc_driver_locks`. - With #6522 as well, `agc_driver_multisample_fill_host_only` failed 0 of 200 runs (8 at a time) on main 72c5eed1f. On 44df21632 it also failed 0 of 400 with `APS5_PIPELINED_DRAWS=0` (4 at a time) and 0 of 40 alone, and without #6522's line it failed 40 of 200 and 7 of 40. - Sonic Origins Plus, main menu, S2 page (the 3D background), 1233x694 window. - A sampled pair (APS5 sampler, 15 s) took the profile numbers above. The FPS shown in the title went from 7.75 to 8.38 (median of six readings). - Captured with `APS5_TRACE_SECONDS=200:15` on the same machine in one session, caches warm. Traces attached. - Baseline b8acbb0d6, this change 824084ed7 (the summaries name the commits before them, see the note): | Metric | Baseline | This PR | Change | | --- | ---: | ---: | ---: | | frame_ms.p50 | 156.721 | 129.749 | -17.2% | | frame_ms.p90 | 163.045 | 139.339 | -14.5% | | mean_frame_ms | 158.259 | 130.959 | -17.3% | | gpu_busy_ms_per_present | 78.608 | 85.850 | +9.2% | | Driver.Packet (ms per frame) | 155.677 | 128.738 | -17.3% | | Driver.Packet.draw (ms per frame) | 118.731 | 117.440 | -1.1% | This pair overstates the change. The baseline's window ran slow outside the draw path: `Driver.Packet` minus its draw part is 37.0 ms against 11.3 ms with this change. Its title FPS, 6.70, is also below every other run of that build (7.51 to 8.34). The sampled pair, 7.75 to 8.38 FPS (+8%), is the better estimate. > **Note: measuring base.** These numbers come from my fork's dev, on main 0a56aaaf4, the last main before #5596. On Windows, later mains compare direct imports on every use, which keeps this menu at 3-4 FPS and hides a change of this size. > - The baseline is [measure/coarse-2026-10-15](https://github.com/meh7an/AnyPS5/commits/measure/coarse-2026-10-15) (b8acbb0d6). > - This change is [measure/locks-2026-10-15](https://github.com/meh7an/AnyPS5/commits/measure/locks-2026-10-15) (824084ed7). > - Each build was made before its commit, so the summaries name the commit before it, `dirty`: a38ad5e9f for the baseline and b8acbb0d6 for this change. > > Both carry these commits from my fork: > - the fixes a71073ce3, c0d875634, e3c68960e, 604f0f373 and 10ff693aa (#5778); > - make-exe and the stage dumps (522230d62, deee71fde); > - the crash-report sampler (8d9c3103e..f641e1ec9), which took the profiles; > - #6037, #6066, #6077, #6067, #6082 (its first version, before #5906), #6095 and #6266; > - seven smaller steps kept in my fork: e83260247, 97b1ff6c9, 84f5bf0ff, 52704cf7c (covered on main by #5062), c0c3a6b58, 467f66896 and f7a41addb. > > The comparison tool marks the window's first present (2260 vs 2475) as a configuration difference: the window opens 200 s into each run. Both summaries set `trace_dropped`, because the worker filled its event buffer at about 2,700 draws per frame. The phase totals above stay complete. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #6522 - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 2080 Ti, driver 617.14, Windows 11 - [x] AI-assisted: yes Traces and summary lines (baseline b8acbb0d6, this change 824084ed7): [locks-traces.zip](https://github.com/user-attachments/files/33485013/locks-traces.zip) The two summary lines, with `tools/trace_digest.py` for each trace: <details><summary>Baseline (summary commit a38ad5e9fc89)</summary> **seconds 200:15** from present 2260: 95 presents, frame p50 156.7 / p90 163.0 / p99 190.3 / max 190.3 ms, GPU busy 78.6 ms per present NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit a38ad5e9fc89 (dirty), dropped: yes ```json {"commit":"a38ad5e9fc89","dirty":true,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"200:15"},"gpu":"NVIDIA GeForce RTX 2080 Ti","driver":"NVIDIA 617.14","os":"Windows 10.0.26300","resolution":"3840x2160","window":{"spec":"seconds 200:15","first_present":2260,"ms":15034.646},"presents":95,"frame_ms":{"p50":156.721,"p90":163.045,"p99":190.275,"max":190.275},"gpu_busy_ms_per_present":78.608,"worker_busy_pct":{"queue worker 0x0":98.389},"top_phases_ms_per_frame":{"Driver.Packet":155.677,"Driver.Packet.draw":118.731,"Driver.Draw":117.267,"Driver.Draw.draw_and_resource_release":54.084,"Graphics.Draw":52.74,"Driver.Draw.shader_compile_and_link":45.397,"Graphics.Draw.draw_and_resource_release":42.561,"Shader.DrawStage":38.974},"trace_dropped":true,"trace":"aps5-trace-a38ad5e9fc89-37256.json.gz"} ``` **Slowest frames** (largest slices inside each, inclusive) - present 2288: 190.3 ms at 4435 ms: batch (gpu) 81.7, Driver.Present (presenter) 26.2, Driver.Present.render_fence_wait (presenter) 25.9, Vulkan.Retire (presenter) 25.9 - present 2270: 183.1 ms at 1586 ms: Driver.Packet (queue worker 0x0) 172.8, Driver.Packet.draw (queue worker 0x0) 144.6, Driver.Draw (queue worker 0x0) 143.1, batch (gpu) 80.2 - present 2316: 180.7 ms at 8871 ms: batch (gpu) 76.3, Driver.Present (presenter) 23.3, Driver.Present.render_fence_wait (presenter) 22.8, Vulkan.Retire (presenter) 22.8 - present 2342: 180.1 ms at 12960 ms: batch (gpu) 75.6, Driver.Present (presenter) 27.0, Driver.Present.render_fence_wait (presenter) 26.5, Vulkan.Retire (presenter) 26.5 - present 2326: 178.7 ms at 10450 ms: batch (gpu) 76.4, Driver.Present (presenter) 23.4, Driver.Present.render_fence_wait (presenter) 23.0, Vulkan.Retire (presenter) 23.0 **Threads** (ms per frame) | thread | busy | wait | untraced | in slices < 20 us | largest | |---|---|---|---|---|---| | queue worker 0x0 | 34.3 (22 %) | 0.1 (0 %) | 123.8 | 51.7 | Driver.Packet 34.4, Driver.Draw 0.0 | | presenter | 0.5 (0 %) | 26.5 (17 %) | 131.3 | 0.1 | Driver.Present 27.0 | | gpu | 78.5 (50 %) | 0.0 (0 %) | 79.8 | 0.0 | batch 78.5 | **Largest slices** (ms per frame, inclusive, all threads): batch 78.5, Driver.Packet 34.4, Driver.Packet.draw 27.8, Driver.Draw 27.5, Driver.Present 27.0, Driver.Present.render_fence_wait 26.4, Vulkan.Retire 26.4, Vulkan.Retire.wait 26.4, Driver.Draw.draw_and_resource_release 6.1, Graphics.Draw 6.0 **Longest waits** - Driver.Present.render_fence_wait (presenter) 31.6 ms at 4790 ms, present 2290 - Driver.Present.render_fence_wait (presenter) 30.6 ms at 4626 ms, present 2289 - Driver.Present.render_fence_wait (presenter) 29.7 ms at 1107 ms, present 2267 - Driver.Present.render_fence_wait (presenter) 29.6 ms at 4121 ms, present 2286 - Driver.Present.render_fence_wait (presenter) 29.5 ms at 1 ms, present 2260 - Driver.Present.render_fence_wait (presenter) 29.4 ms at 1270 ms, present 2268 - Driver.Present.render_fence_wait (presenter) 29.2 ms at 3009 ms, present 2279 - Driver.Present.render_fence_wait (presenter) 29.2 ms at 4277 ms, present 2287 - Driver.Present.render_fence_wait (presenter) 29.2 ms at 14237 ms, present 2350 - Driver.Present.render_fence_wait (presenter) 29.1 ms at 7324 ms, present 2306 </details> <details><summary>This change (summary commit b8acbb0d65c3)</summary> **seconds 200:15** from present 2475: 115 presents, frame p50 129.7 / p90 139.3 / p99 153.5 / max 153.5 ms, GPU busy 85.8 ms per present NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit b8acbb0d65c3 (dirty), dropped: yes ```json {"commit":"b8acbb0d65c3","dirty":true,"env":{"APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"200:15"},"gpu":"NVIDIA GeForce RTX 2080 Ti","driver":"NVIDIA 617.14","os":"Windows 10.0.26300","resolution":"3840x2160","window":{"spec":"seconds 200:15","first_present":2475,"ms":15060.273},"presents":115,"frame_ms":{"p50":129.749,"p90":139.339,"p99":153.497,"max":153.537},"gpu_busy_ms_per_present":85.85,"worker_busy_pct":{"queue worker 0x0":98.33},"top_phases_ms_per_frame":{"Driver.Packet":128.738,"Driver.Packet.draw":117.44,"Driver.Draw":116.398,"Driver.Draw.draw_and_resource_release":52.218,"Graphics.Draw":51.006,"Driver.Draw.shader_compile_and_link":45.265,"Graphics.Draw.draw_and_resource_release":41.533,"Shader.DrawStage":39.233},"trace_dropped":true,"trace":"aps5-trace-b8acbb0d65c3-37744.json.gz"} ``` **Slowest frames** (largest slices inside each, inclusive) - present 2580: 153.5 ms at 13736 ms: batch (gpu) 89.8, Driver.Present (presenter) 34.2, Driver.Present.render_fence_wait (presenter) 33.8, Vulkan.Retire (presenter) 33.8 - present 2492: 153.5 ms at 2205 ms: Driver.Packet (queue worker 0x0) 144.4, Driver.Packet.draw (queue worker 0x0) 139.3, Driver.Draw (queue worker 0x0) 138.3, batch (gpu) 88.2 - present 2573: 152.5 ms at 12812 ms: batch (gpu) 88.0, Driver.Present (presenter) 31.5, Driver.Present.render_fence_wait (presenter) 31.0, Vulkan.Retire (presenter) 31.0 - present 2544: 150.9 ms at 8988 ms: batch (gpu) 85.6, Driver.Present (presenter) 32.0, Driver.Present.render_fence_wait (presenter) 31.5, Vulkan.Retire (presenter) 31.5 - present 2521: 147.8 ms at 5947 ms: batch (gpu) 82.9, Driver.Present (presenter) 31.8, Driver.Present.render_fence_wait (presenter) 31.2, Vulkan.Retire (presenter) 31.2 **Threads** (ms per frame) | thread | busy | wait | untraced | in slices < 20 us | largest | |---|---|---|---|---|---| | queue worker 0x0 | 23.5 (18 %) | 0.1 (0 %) | 107.4 | 42.1 | Driver.Packet 23.6, Driver.Completion 0.0 | | presenter | 0.5 (0 %) | 31.5 (24 %) | 99.0 | 0.1 | Driver.Present 32.0 | | gpu | 85.7 (65 %) | 0.0 (0 %) | 45.2 | 0.0 | batch 85.7 | **Largest slices** (ms per frame, inclusive, all threads): batch 85.7, Driver.Present 32.0, Driver.Present.render_fence_wait 31.4, Vulkan.Retire 31.4, Vulkan.Retire.wait 31.4, Driver.Packet 23.6, Driver.Packet.draw 22.7, Driver.Draw 22.5, Driver.Draw.draw_and_resource_release 4.9, Graphics.Draw 4.8 **Longest waits** - Driver.Present.render_fence_wait (presenter) 37.0 ms at 2359 ms, present 2493 - Driver.Present.render_fence_wait (presenter) 36.6 ms at 2489 ms, present 2494 - Driver.Present.render_fence_wait (presenter) 36.4 ms at 11618 ms, present 2564 - Driver.Present.render_fence_wait (presenter) 36.0 ms at 14151 ms, present 2583 - Driver.Present.render_fence_wait (presenter) 35.7 ms at 1282 ms, present 2485 - Driver.Present.render_fence_wait (presenter) 35.7 ms at 10582 ms, present 2556 - Driver.Present.render_fence_wait (presenter) 35.4 ms at 1022 ms, present 2483 - Driver.Present.render_fence_wait (presenter) 35.3 ms at 12273 ms, present 2569 - Driver.Present.render_fence_wait (presenter) 34.9 ms at 382 ms, present 2478 - Driver.Present.render_fence_wait (presenter) 34.5 ms at 3151 ms, present 2499 </details>
SoRadGaming commented 2026-10-15 11:14:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SoRadGaming on 2026-10-15T11:14:59Z.


Windows / AMD results for this head, in a scene where queue worker 0 is the limit: ASTRO BOT (PPSA21564 01.007.000), -lvl hub_crashsite (the full Crash Site hub).

Host: Windows 11 Pro 26200, Ryzen 7 5800X3D, Radeon RX 9070 XT (AMD proprietary driver 32.0.23033.1002), 32 GB, WinLibs GCC 15.2.0 r7, Release.

Builds.

  • The base is main c0be1f69b + #6060 + #6273 + #6188 (draw-ahead) + #6266, plus two local collect-memo changes.
  • The change is that tree with this PR merged.
  • The merge had two conflicts:
    • In Driver.hpp, #6188 adds draw-cache members next to drawCacheMutex. I kept them, with drawCacheMutex as AgcDriver::Mutex.
    • In GuestMemory.cpp, around the collect memo and the epoch check of the 64 KiB widening (#6273). I kept the widening on Windows, with its epoch read through HostThreadSlot<std::uint64_t, CollectEpochSlot>.
  • Both builds use one relinked executable.

Method. No input, warm shader cache (1,904 hits, 0 misses), APS5_NO_SNAPSHOT_CHECK=1 APS5_RESOURCE_CACHE_ENTRIES=4096, no profiling switches. FPS is the window's present counter over 120 s, after a 120 s settle once the level has started. Runs alternate.

hub FPS 10 s samples after the settle
base 6.27 / 6.25 / 6.23 / 6.20 5.7-6.4
base + this PR 6.64 / 6.60 (+6.1 %) 6.1-6.8

Seven launches with this PR went through boot, loading and at least 3 minutes in the hub: the two measured above, and five more held 3 minutes each, at 6.62-6.67 FPS. None had a stop, a hang or an error in the log.

AI-assisted (Claude Code); the builds and runs are on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6190743546 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6190743546). Originally posted by [SoRadGaming](https://github.com/SoRadGaming) on 2026-10-15T11:14:59Z. --- Windows / AMD results for this head, in a scene where queue worker 0 is the limit: ASTRO BOT (PPSA21564 01.007.000), `-lvl hub_crashsite` (the full Crash Site hub). Host: Windows 11 Pro 26200, Ryzen 7 5800X3D, Radeon RX 9070 XT (AMD proprietary driver 32.0.23033.1002), 32 GB, WinLibs GCC 15.2.0 r7, Release. **Builds.** - The base is `main` c0be1f69b + #6060 + #6273 + #6188 (draw-ahead) + #6266, plus two local collect-memo changes. - The change is that tree with this PR merged. - The merge had two conflicts: - In `Driver.hpp`, #6188 adds draw-cache members next to `drawCacheMutex`. I kept them, with `drawCacheMutex` as `AgcDriver::Mutex`. - In `GuestMemory.cpp`, around the collect memo and the epoch check of the 64 KiB widening (#6273). I kept the widening on Windows, with its epoch read through `HostThreadSlot<std::uint64_t, CollectEpochSlot>`. - Both builds use one relinked executable. **Method.** No input, warm shader cache (1,904 hits, 0 misses), `APS5_NO_SNAPSHOT_CHECK=1 APS5_RESOURCE_CACHE_ENTRIES=4096`, no profiling switches. FPS is the window's present counter over 120 s, after a 120 s settle once the level has started. Runs alternate. | | hub FPS | 10 s samples after the settle | |---|---|---| | base | 6.27 / 6.25 / 6.23 / 6.20 | 5.7-6.4 | | base + this PR | **6.64 / 6.60** (+6.1 %) | 6.1-6.8 | Seven launches with this PR went through boot, loading and at least 3 minutes in the hub: the two measured above, and five more held 3 minutes each, at 6.62-6.67 FPS. None had a stop, a hang or an error in the log. AI-assisted (Claude Code); the builds and runs are on my machine.
simonbronk commented 2026-10-15 12:40:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-15T12:40:37Z.


Independent Windows microbenchmark of AgcDriver::Mutex at head 54b0ce01a165784a47031550691fbc385f72f548 (AI-assisted).

The uncontended improvement reproduces, but I found a contention/CPU tradeoff worth measuring before tuning the spin budget. This does not contradict the title results already posted: it is a saturated synthetic critical section, not a frame workload.

Windows 11 22631, i7-8700 (6 cores / 12 logical CPUs), WinLibs GCC 15.2.0 r7, -std=c++20 -O3 -DNDEBUG -static -pthread. The exact unchanged Execution/include/Mutex.hpp is included directly; the baseline is std::mutex from that compiler. No change to caller sites, TLS or DeviceUseGate is exercised.

Five paired batches per thread count, alternating std/PR order on alternate batches; medians below. Each worker increments one protected counter; a simultaneous start gate precedes timing. 1 thread: 1,000,000 acquisitions total. 2/4/8 threads: 400,000 acquisitions total. Wall time includes completion/join; worker cycles are summed QueryThreadCycleTime deltas divided by total acquisitions (not wall time).

Threads std wall ms PR wall ms std cycles/acquisition PR cycles/acquisition
1 19.55 11.94 62.0 37.8
2 14.59 14.47 173.5 216.6
4 12.00 33.17 135.7 1009.8
8 18.68 47.51 534.6 2947.4

All protected totals were exact: 11 million increments per mutex implementation. Also checked successful free try_lock, failure of try_lock while a different thread owns the mutex, and a waiter blocked during a 20 ms hold and waking after unlock (2 s completion bound). These are basic probes, not a memory-model proof or starvation test.

I then changed only spin < 100 to spin < 0 in an external copy of the same header and repeated the five paired batches; a separate spin < 8 trial is included too. The no-spin trial gave:

Threads contemporaneous std wall ms no-spin wall ms no-spin cycles/acquisition
1 20.95 12.17 38.3
2 13.88 9.73 151.5
4 12.41 10.81 290.8
8 15.41 14.03 682.5

The uncontended fast path is retained; on this saturated test, avoiding the explicit spin loop substantially reduces elapsed time and aggregate worker cycles. The experiments ran sequentially, each with its own interleaved std control; they are not a randomized cross-variant title study. Eight spins still cost more than zero in the 4/8-thread cases. This suggests testing a smaller/adaptive spin budget or retaining the old mutex on strongly contended sites, rather than assuming the uncontended result applies to every lock. I am not proposing zero spins as a shipping default without representative title traces.

Attached reproducible probe, unchanged pinned header, both one-constant experimental copies, runners and raw samples. No repo source edit, new PR or push. No GPU/title/FPS/full-build/project-CTest/Linux run, TLS/gate/GpuMutex integration, fairness, long critical-section throughput or oversubscription test. Existing GPU/title results from other contributors are not my validation.
windows-lock-contention-pr6292.zip

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6192275452 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6192275452). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-15T12:40:37Z. --- Independent Windows microbenchmark of `AgcDriver::Mutex` at head `54b0ce01a165784a47031550691fbc385f72f548` (AI-assisted). The uncontended improvement reproduces, but I found a contention/CPU tradeoff worth measuring before tuning the spin budget. This does not contradict the title results already posted: it is a saturated synthetic critical section, not a frame workload. Windows 11 22631, i7-8700 (6 cores / 12 logical CPUs), WinLibs GCC 15.2.0 r7, `-std=c++20 -O3 -DNDEBUG -static -pthread`. The exact unchanged `Execution/include/Mutex.hpp` is included directly; the baseline is `std::mutex` from that compiler. No change to caller sites, TLS or `DeviceUseGate` is exercised. Five paired batches per thread count, alternating std/PR order on alternate batches; medians below. Each worker increments one protected counter; a simultaneous start gate precedes timing. 1 thread: 1,000,000 acquisitions total. 2/4/8 threads: 400,000 acquisitions total. Wall time includes completion/join; worker cycles are summed `QueryThreadCycleTime` deltas divided by total acquisitions (not wall time). | Threads | std wall ms | PR wall ms | std cycles/acquisition | PR cycles/acquisition | |---:|---:|---:|---:|---:| | 1 | 19.55 | 11.94 | 62.0 | 37.8 | | 2 | 14.59 | 14.47 | 173.5 | 216.6 | | 4 | 12.00 | 33.17 | 135.7 | 1009.8 | | 8 | 18.68 | 47.51 | 534.6 | 2947.4 | All protected totals were exact: 11 million increments per mutex implementation. Also checked successful free `try_lock`, failure of `try_lock` while a different thread owns the mutex, and a waiter blocked during a 20 ms hold and waking after unlock (2 s completion bound). These are basic probes, not a memory-model proof or starvation test. I then changed **only** `spin < 100` to `spin < 0` in an external copy of the same header and repeated the five paired batches; a separate `spin < 8` trial is included too. The no-spin trial gave: | Threads | contemporaneous std wall ms | no-spin wall ms | no-spin cycles/acquisition | |---:|---:|---:|---:| | 1 | 20.95 | 12.17 | 38.3 | | 2 | 13.88 | 9.73 | 151.5 | | 4 | 12.41 | 10.81 | 290.8 | | 8 | 15.41 | 14.03 | 682.5 | The uncontended fast path is retained; on this saturated test, avoiding the explicit spin loop substantially reduces elapsed time and aggregate worker cycles. The experiments ran sequentially, each with its own interleaved std control; they are not a randomized cross-variant title study. Eight spins still cost more than zero in the 4/8-thread cases. This suggests testing a smaller/adaptive spin budget or retaining the old mutex on strongly contended sites, rather than assuming the uncontended result applies to every lock. **I am not proposing zero spins as a shipping default without representative title traces.** Attached reproducible probe, unchanged pinned header, both one-constant experimental copies, runners and raw samples. No repo source edit, new PR or push. No GPU/title/FPS/full-build/project-CTest/Linux run, TLS/gate/GpuMutex integration, fairness, long critical-section throughput or oversubscription test. Existing GPU/title results from other contributors are not my validation. [windows-lock-contention-pr6292.zip](https://github.com/user-attachments/files/33492799/windows-lock-contention-pr6292.zip)
simonbronk commented 2026-10-15 12:52:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-15T12:52:10Z.


Small clock-gating follow-up for head 54b0ce01a165784a47031550691fbc385f72f548, independent of the mutex contention experiment (AI-assisted).

The profiling-gating part still leaves one unconditional clock read in Driver::execute, in the opcode == 0x3c || opcode == 0x93 wait-packet branch. waitStart is consumed only by the following traceGpu && ... diagnostic; with APS5_TRACE_GPU absent it is never used. This also affects waits already satisfied by the time the worker sees them.

Suggested one-line patch in Execution/src/Driver/Packets/PacketExecution.cpp:

-            const auto waitStart = std::chrono::steady_clock::now();
+            const auto waitStart = traceGpu ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{};

The wait call, gateOpened() = true and the existing traceGpu && elapsed > 200 ms diagnostic stay in place. This follows the conditional clock read already used for PacketTimer in this PR.

I checked the timing/control region with an external standalone C++ probe, not Driver::execute or an emulator build. It preserves the start-read/conditional-end-read sequence, calls a synthetic already-satisfied wait through a profiling-disabled wrapper, and records the work and gate side effects. Both functions are kept out of line in the probe. Windows 11 22631, i7-8700, WinLibs GCC 15.2.0 r7, -std=c++20 -O3 -DNDEBUG -static; seven paired batches of one million calls per mode/version, alternating order:

Probe mode Original ns/call Guarded ns/call
Trace off 44.406 1.671
Trace on 89.311 92.522

The point is avoiding one unnecessary clock read on the default path, not a whole-packet speedup of this magnitude: almost all actual wait/driver work is absent from this probe. Trace-on was slightly slower here, and no trace-on improvement is claimed. All 28 million synthetic calls retained the expected work count and open gate. Four additional 250 ms synthetic waits (both versions, trace off/on) verify the diagnostic predicate stays false with tracing off and true with tracing on.

Attached applicable patch, pinned original file, probe, runner and samples. The runner regenerates the patch against the stated head. No repository source edit, separate PR or push. No full build/CTest, actual waitMemory, packet decoder, GPU, game/FPS or current-main composition validation. The 200 ms test checks only the extracted control logic; it does not check the real diagnostic packet dump. Please run the relevant driver checks before integrating.
wait-packet-clock-gating.zip

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6192500704 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6192500704). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-15T12:52:10Z. --- Small clock-gating follow-up for head `54b0ce01a165784a47031550691fbc385f72f548`, independent of the mutex contention experiment (AI-assisted). The profiling-gating part still leaves one unconditional clock read in `Driver::execute`, in the `opcode == 0x3c || opcode == 0x93` wait-packet branch. `waitStart` is consumed only by the following `traceGpu && ...` diagnostic; with `APS5_TRACE_GPU` absent it is never used. This also affects waits already satisfied by the time the worker sees them. Suggested one-line patch in `Execution/src/Driver/Packets/PacketExecution.cpp`: ```diff - const auto waitStart = std::chrono::steady_clock::now(); + const auto waitStart = traceGpu ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point{}; ``` The wait call, `gateOpened() = true` and the existing `traceGpu && elapsed > 200 ms` diagnostic stay in place. This follows the conditional clock read already used for `PacketTimer` in this PR. I checked the timing/control region with an **external standalone C++ probe**, not `Driver::execute` or an emulator build. It preserves the start-read/conditional-end-read sequence, calls a synthetic already-satisfied wait through a profiling-disabled wrapper, and records the work and gate side effects. Both functions are kept out of line in the probe. Windows 11 22631, i7-8700, WinLibs GCC 15.2.0 r7, `-std=c++20 -O3 -DNDEBUG -static`; seven paired batches of one million calls per mode/version, alternating order: | Probe mode | Original ns/call | Guarded ns/call | |---|---:|---:| | Trace off | 44.406 | 1.671 | | Trace on | 89.311 | 92.522 | The point is avoiding one unnecessary clock read on the default path, **not a whole-packet speedup of this magnitude**: almost all actual wait/driver work is absent from this probe. Trace-on was slightly slower here, and no trace-on improvement is claimed. All 28 million synthetic calls retained the expected work count and open gate. Four additional 250 ms synthetic waits (both versions, trace off/on) verify the diagnostic predicate stays false with tracing off and true with tracing on. Attached applicable patch, pinned original file, probe, runner and samples. The runner regenerates the patch against the stated head. No repository source edit, separate PR or push. **No full build/CTest, actual waitMemory, packet decoder, GPU, game/FPS or current-main composition validation.** The 200 ms test checks only the extracted control logic; it does not check the real diagnostic packet dump. Please run the relevant driver checks before integrating. [wait-packet-clock-gating.zip](https://github.com/user-attachments/files/33493255/wait-packet-clock-gating.zip)
simonbronk commented 2026-10-15 13:40:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-15T13:40:25Z.


Follow-up on the integration/conventions blockers at head 54b0ce01a165784a47031550691fbc385f72f548: I prepared a candidate against main a5e2a80c6b.

The actual Git merge has one include conflict in DepthSurface.cpp. The candidate keeps the existing GuestMemory.hpp include and adds Mutex.hpp once. It also replaces the two hand-written TLS imports with <processthreadsapi.h> inside the existing Windows x86-64 GNU guard, and removes three prose comment lines in GuestMemory.cpp flagged by the comment policy. The mutex algorithm, spin count and TLS load/store code are unchanged; my earlier waitStart clock patch is separate.

Validation:

  • The trusted conventions checker from that pinned main passed against the complete candidate Git tree (exit 0), rather than a reduced fixture.
  • Four native Windows x86-64 GCC 15.2 UCRT probes passed: original/SDK headers, each before/after windows.h, with -Wall -Wextra -Werror. Each exercised direct and expansion TLS slots plus 8 threads × 100,000 Set/Get iterations, isolation and fresh-thread zero values; zero errors.
  • The bundle's application helper was checked against both merge-side orders, including Git's HEAD marker, and all three resulting file hashes match the validated candidate. It refuses unexpected input before writing any file.

The attached bundle includes the three resolved files, checked application helper, exact-revision instructions, probe source, both tested headers and results. This is a local integration candidate, not a push to your branch. Full emulator build, CTest, other platforms, GPU/title execution and FPS were not tested; hosted CI still needs to run after integration. Prepared and reviewed with AI assistance.

pr6292-integration-candidate.zip

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6193472807 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6193472807). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-15T13:40:25Z. --- Follow-up on the integration/conventions blockers at head 54b0ce01a165784a47031550691fbc385f72f548: I prepared a candidate against main a5e2a80c6bc3a460404267fe600e7e28d0664438. The actual Git merge has one include conflict in DepthSurface.cpp. The candidate keeps the existing GuestMemory.hpp include and adds Mutex.hpp once. It also replaces the two hand-written TLS imports with <processthreadsapi.h> inside the existing Windows x86-64 GNU guard, and removes three prose comment lines in GuestMemory.cpp flagged by the comment policy. The mutex algorithm, spin count and TLS load/store code are unchanged; my earlier waitStart clock patch is separate. Validation: - The trusted conventions checker from that pinned main passed against the complete candidate Git tree (exit 0), rather than a reduced fixture. - Four native Windows x86-64 GCC 15.2 UCRT probes passed: original/SDK headers, each before/after windows.h, with -Wall -Wextra -Werror. Each exercised direct and expansion TLS slots plus 8 threads × 100,000 Set/Get iterations, isolation and fresh-thread zero values; zero errors. - The bundle's application helper was checked against both merge-side orders, including Git's HEAD marker, and all three resulting file hashes match the validated candidate. It refuses unexpected input before writing any file. The attached bundle includes the three resolved files, checked application helper, exact-revision instructions, probe source, both tested headers and results. This is a local integration candidate, not a push to your branch. Full emulator build, CTest, other platforms, GPU/title execution and FPS were not tested; hosted CI still needs to run after integration. Prepared and reviewed with AI assistance. [pr6292-integration-candidate.zip](https://github.com/user-attachments/files/33495494/pr6292-integration-candidate.zip)
meh7an commented 2026-10-15 16:37:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-15T16:37:58Z.


Thanks, all. Rebased onto main 92e50418f as adcaa64ad, with these changes:

  • @SoRadGaming, thanks for the ASTRO BOT numbers on AMD.
  • @simonbronk, the wait-packet clock read now happens only for APS5_TRACE_GPU, as you suggested.
  • Conventions: HostThreadSlot takes TlsAlloc and TlsSetValue from <processthreadsapi.h>, and the comment this PR added is gone. Rather than deleting main's "Still the holder here" comment, the GPU mutex's unlock moves mutex.unlock() into main's if (outermost) block, so that comment stays as it was. tools/check_conventions.py passes.
  • On that main, the full ctest suite passes (1295 tests).

On the spin, I tried dropping it (lockContended going straight to exchange(Contended) and the wait, which spins 12 pauses and 4 yields itself in libstdc++ before it sleeps). It made agc_driver_multisample_fill_host_only fail most runs. Runs one at a time, 40 each:

Build APS5_PIPELINED_DRAWS on off
main 92e50418f 1 failed 34 failed
main + this PR 1 failed 24 failed
main + this PR, no spin 36 failed

Each failure is the test's "the sentinel fill did not land": the fill is not in guest memory yet when WaitIdle returns. Main already has this race, and pipelined draws mostly hide it. Waiters that sleep at once widen its window. So the spin stays in this PR. Dropping it can follow once that race is fixed, with your benchmark as the reason. On the page I measure, the contended path takes no samples (0.00 per frame in three sampled runs today), so the PR's numbers do not depend on the spin either way.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6197205452 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6197205452). Originally posted by [meh7an](https://github.com/meh7an) on 2026-10-15T16:37:58Z. --- Thanks, all. Rebased onto main 92e50418f as adcaa64ad, with these changes: - @SoRadGaming, thanks for the ASTRO BOT numbers on AMD. - @simonbronk, the wait-packet clock read now happens only for `APS5_TRACE_GPU`, as you suggested. - Conventions: `HostThreadSlot` takes `TlsAlloc` and `TlsSetValue` from `<processthreadsapi.h>`, and the comment this PR added is gone. Rather than deleting main's "Still the holder here" comment, the GPU mutex's unlock moves `mutex.unlock()` into main's `if (outermost)` block, so that comment stays as it was. `tools/check_conventions.py` passes. - On that main, the full ctest suite passes (1295 tests). On the spin, I tried dropping it (`lockContended` going straight to `exchange(Contended)` and the wait, which spins 12 pauses and 4 yields itself in libstdc++ before it sleeps). It made `agc_driver_multisample_fill_host_only` fail most runs. Runs one at a time, 40 each: | Build | `APS5_PIPELINED_DRAWS` on | off | | --- | ---: | ---: | | main 92e50418f | 1 failed | 34 failed | | main + this PR | 1 failed | 24 failed | | main + this PR, no spin | 36 failed | | Each failure is the test's "the sentinel fill did not land": the fill is not in guest memory yet when `WaitIdle` returns. Main already has this race, and pipelined draws mostly hide it. Waiters that sleep at once widen its window. So the spin stays in this PR. Dropping it can follow once that race is fixed, with your benchmark as the reason. On the page I measure, the contended path takes no samples (0.00 per frame in three sampled runs today), so the PR's numbers do not depend on the spin either way.
meh7an commented 2026-10-15 19:18:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-15T19:18:05Z.


@simonbronk, the spin is gone now (091a0cc82, rebased onto main 44df21632): a contended lock waits on the word at once.

The race it was hiding turned out to be main's, #6512. In agc_driver_multisample_fill_host_only, recordCommands in MultisampleTarget.cpp recorded a multisampled target's barrier and clear into the active recorder, which belongs to another VkDevice than the target's image. The validation layer reports it (VUID-vkCmdClearColorImage-commonparent), and the next batch's fill was lost now and then. #6522 fixes that, so this PR now depends on it.

On main 44df21632 with #6522 and this PR without the spin, runs of that test:

Without #6522's line With it
one at a time 7 of 40 failed 0 of 40
8 at a time 40 of 200 failed 0 of 200
APS5_PIPELINED_DRAWS=0, 4 at a time 0 of 400

The full suite passes 1299 of 1300 there; the one not run is agc_driver_gpu_indirect_arguments_misaligned_writer_ahead, which main itself does not compile with MinGW.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6200332030 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6200332030). Originally posted by [meh7an](https://github.com/meh7an) on 2026-10-15T19:18:05Z. --- @simonbronk, the spin is gone now (091a0cc82, rebased onto main 44df21632): a contended lock waits on the word at once. The race it was hiding turned out to be main's, #6512. In `agc_driver_multisample_fill_host_only`, `recordCommands` in `MultisampleTarget.cpp` recorded a multisampled target's barrier and clear into the active recorder, which belongs to another `VkDevice` than the target's image. The validation layer reports it (`VUID-vkCmdClearColorImage-commonparent`), and the next batch's fill was lost now and then. #6522 fixes that, so this PR now depends on it. On main 44df21632 with #6522 and this PR without the spin, runs of that test: | | Without #6522's line | With it | | --- | ---: | ---: | | one at a time | 7 of 40 failed | 0 of 40 | | 8 at a time | 40 of 200 failed | 0 of 200 | | `APS5_PIPELINED_DRAWS=0`, 4 at a time | | 0 of 400 | The full suite passes 1299 of 1300 there; the one not run is `agc_driver_gpu_indirect_arguments_misaligned_writer_ahead`, which main itself does not compile with MinGW.
simonbronk commented 2026-10-15 19:48:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-15T19:48:24Z.


@meh7an thanks for the update. Here is a title measurement of 091a0cc82 on Windows. It's inconclusive for this scene.

Onimusha: Way of the Sword (PPSA27836), Windows 11 26200, RTX 5070 (NVIDIA 617.42), WinLibs GCC 15.2, Release. Base: main 44df21632 plus the title's PRs (#5907 #5841 #5884 #5915). Test: the same tree plus #6522 (09f574989) and this PR. Scene: boot to the language menu, no input, warm shader cache, APS5_TRACE_SECONDS=60:30. Runs alternated base, PR, PR, base, base, PR.

build run presents p50 ms p90 ms p99 ms GPU busy ms/present
base 1 188 89.8 136.6 1001.9 29.4
base 2 200 87.6 109.2 1060.3 29.0
base 3 220 89.5 100.4 950.6 29.7
+ #6522 + #6292 1 175 87.4 371.8 1024.8 29.4
+ #6522 + #6292 2 226 87.6 95.6 896.6 29.4
+ #6522 + #6292 3 173 89.3 371.2 1100.9 29.3
  • The median p50 moves from 89.5 to 87.6 ms (about 2%). That is within the run-to-run spread here.
  • The two p90 values near 371 ms come from this title's periodic stalls (#6453: guest munmap bursts that wait for GPU work). Those stalls land in or out of the 30 s window by chance, so I wouldn't read them as a regression of this PR.
  • On this menu the queue worker is about 92% busy. Most of that time goes to resource builds ([resources] images and stage B phases) and to waits, not to lock or TLS overhead. So a 2% effect is about what I'd expect. No functional difference: both builds reach the same screens.

AI-assisted: Claude Code.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6200891399 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6200891399). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-15T19:48:24Z. --- @meh7an thanks for the update. Here is a title measurement of 091a0cc82 on Windows. It's inconclusive for this scene. Onimusha: Way of the Sword (PPSA27836), Windows 11 26200, RTX 5070 (NVIDIA 617.42), WinLibs GCC 15.2, Release. Base: `main` 44df21632 plus the title's PRs (#5907 #5841 #5884 #5915). Test: the same tree plus #6522 (09f574989) and this PR. Scene: boot to the language menu, no input, warm shader cache, `APS5_TRACE_SECONDS=60:30`. Runs alternated base, PR, PR, base, base, PR. | build | run | presents | p50 ms | p90 ms | p99 ms | GPU busy ms/present | |---|---|---|---|---|---|---| | base | 1 | 188 | 89.8 | 136.6 | 1001.9 | 29.4 | | base | 2 | 200 | 87.6 | 109.2 | 1060.3 | 29.0 | | base | 3 | 220 | 89.5 | 100.4 | 950.6 | 29.7 | | + #6522 + #6292 | 1 | 175 | 87.4 | 371.8 | 1024.8 | 29.4 | | + #6522 + #6292 | 2 | 226 | 87.6 | 95.6 | 896.6 | 29.4 | | + #6522 + #6292 | 3 | 173 | 89.3 | 371.2 | 1100.9 | 29.3 | - The median p50 moves from 89.5 to 87.6 ms (about 2%). That is within the run-to-run spread here. - The two p90 values near 371 ms come from this title's periodic stalls (#6453: guest `munmap` bursts that wait for GPU work). Those stalls land in or out of the 30 s window by chance, so I wouldn't read them as a regression of this PR. - On this menu the queue worker is about 92% busy. Most of that time goes to resource builds (`[resources]` images and stage B phases) and to waits, not to lock or TLS overhead. So a 2% effect is about what I'd expect. No functional difference: both builds reach the same screens. AI-assisted: Claude Code.
moong15 commented 2026-10-19 13:16:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by moong15 on 2026-10-19T13:16:54Z.


Evidence from Stellar Blade on Windows (MinGW build): I measured this change as part of #6615's stack, which includes it. Ported onto a downstream perf branch on origin/main 4ffb0cd12 (RX 9070 XT, 4K, warm cache, two base/port pairs, 49-150 s of gameplay, whole traces), the stack gave +4.5% and +9.5% presents (about +7%). p50 dropped 9-13% and p99 16-22%. Peak commit was 0.75-1.2 GB higher. I couldn't isolate #6292 on its own. Full table and trace_digest output: https://github.com/boykopovar/AnyPS5/pull/6615#issuecomment-6268138557.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6268140380 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6268140380). Originally posted by [moong15](https://github.com/moong15) on 2026-10-19T13:16:54Z. --- Evidence from Stellar Blade on Windows (MinGW build): I measured this change as part of #6615's stack, which includes it. Ported onto a downstream perf branch on origin/main 4ffb0cd12 (RX 9070 XT, 4K, warm cache, two base/port pairs, 49-150 s of gameplay, whole traces), the stack gave **+4.5% and +9.5% presents** (about +7%). p50 dropped 9-13% and p99 16-22%. Peak commit was 0.75-1.2 GB higher. I couldn't isolate #6292 on its own. Full table and trace_digest output: https://github.com/boykopovar/AnyPS5/pull/6615#issuecomment-6268138557.
oneandonlydean commented 2026-10-20 12:59:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T12:59:23Z.


Thanks. The mutex, the use gate, the GPU mutex's recursion and the TEB slot reads all check out. The offsets match the x64 TEB layout, and a small harness built from HostThreadSlot.hpp kept per-thread values isolated in both a direct slot and an expansion slot (index 74) across three threads, matching TlsGetValue throughout.

One ask, about where the new test lives. tests/execution/Locks.cpp is picked up by the execution-test glob, and the Windows CI job configures ANYPS5_AGC_EXECUTION_TESTS=F16ToF32Unpack;F64FmaSubnormals;RegistrationPrepareShutdown. Every other execution test, including this one, is built from ExecutionTestNotBuilt.cpp there and reports a skip. Linux CI does run it, but on Linux HostThreadSlot is a plain thread_local. So no CI job runs the TEB code this PR adds. The test doesn't need Vulkan, so could you register it as a plain CPU test outside tests/execution (like agc_lds_lock_budget), so it runs on the Windows job too?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291410701 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6291410701). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T12:59:23Z. --- Thanks. The mutex, the use gate, the GPU mutex's recursion and the TEB slot reads all check out. The offsets match the x64 TEB layout, and a small harness built from `HostThreadSlot.hpp` kept per-thread values isolated in both a direct slot and an expansion slot (index 74) across three threads, matching `TlsGetValue` throughout. One ask, about where the new test lives. `tests/execution/Locks.cpp` is picked up by the execution-test glob, and the Windows CI job configures `ANYPS5_AGC_EXECUTION_TESTS=F16ToF32Unpack;F64FmaSubnormals;RegistrationPrepareShutdown`. Every other execution test, including this one, is built from `ExecutionTestNotBuilt.cpp` there and reports a skip. Linux CI does run it, but on Linux `HostThreadSlot` is a plain `thread_local`. So no CI job runs the TEB code this PR adds. The test doesn't need Vulkan, so could you register it as a plain CPU test outside `tests/execution` (like `agc_lds_lock_budget`), so it runs on the Windows job too?
meh7an commented 2026-10-20 13:43:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by meh7an on 2026-10-20T13:43:48Z.


Thanks, and thanks for checking the slots against TlsGetValue with a harness.

  • The test is now tests/Locks.cpp, a plain CPU test named agc_locks, registered like agc_shared_write.
    • It links libSceAgcDriver and libc and needs no Vulkan device, only the headers.
    • It is out of the execution-test glob, so the Windows job builds and runs it instead of the ExecutionTestNotBuilt.cpp skip.
    • Linux runs it as before, with HostThreadSlot as a plain thread_local.
  • On main 1d4164552 it passes on Windows, where the slots read the TEB.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6292299071 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#issuecomment-6292299071). Originally posted by [meh7an](https://github.com/meh7an) on 2026-10-20T13:43:48Z. --- Thanks, and thanks for checking the slots against `TlsGetValue` with a harness. - The test is now `tests/Locks.cpp`, a plain CPU test named `agc_locks`, registered like `agc_shared_write`. - It links `libSceAgcDriver` and `libc` and needs no Vulkan device, only the headers. - It is out of the execution-test glob, so the Windows job builds and runs it instead of the `ExecutionTestNotBuilt.cpp` skip. - Linux runs it as before, with `HostThreadSlot` as a plain `thread_local`. - On main 1d4164552 it passes on Windows, where the slots read the TEB.
CnRJay commented 2026-10-21 17:54:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-21T17:54:00Z.

Review: COMMENTED


The profile is a convincing reason (pthread_mutex_lock/unlock, __emutls_get_address and the device use gate are about 10% of the queue worker's frame on Sonic Origins Plus), the first spinning version was caught and fixed on @simonbronk's contention benchmark, and the frame effect is measured on two machines (7.75 to 8.38 fps title reading on the 2080 Ti; SoRadGaming's Astro Bot Crash Site on a Radeon). The risk is in what it replaces.

  • It swaps 74 std::mutexes for a hand-written, non-fair, non-recursive mutex built on std::atomic::wait. The description's own account is that waiters that sleep at once widened a race main already has (agc_driver_multisample_fill_host_only failing most runs, #6512), which #6522 fixes. So this PR changes scheduling enough to expose latent races, and the safe merge order is #6522 first. State that in the dependency list and keep the test running in a loop in CI (--repeat until-fail), since the next latent race will show up in the same way and in some other test.
  • Priority and fairness: a lock that sleeps with no spin hands the CPU to another waiter at once. The benchmark (4-8 threads on one lock) shows it beating std::mutex; add the case that matters here, the GPU mutex held across a long operation by the queue worker while the packet worker and a loader thread wait, with the hold time of a real submit, and report the waiters' wake latency.
  • HostThreadSlot on Windows is a TlsAlloc slot read through the TEB. Say what happens if the slots are exhausted or TlsAlloc fails (it should throw at startup), and that a slot is released at shutdown.
  • GpuMutex's owner token is the TEB address. If a thread could exit while holding it, a later thread that is given the same TEB address would appear to own the lock. A one-line statement that no thread can exit while holding the GPU mutex (or a test) would settle it.
  • 708 lines across the whole draw path conflicts with main repeatedly (the description lists five rebases). The conflicts show the risk: every change to a draw-path mutex now needs the new type. A staged landing would help: the HostThreadSlot and Driver::timed changes (no behavior change on contention) first, the mutex replacement second.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5603955754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6292#pullrequestreview-5603955754). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-21T17:54:00Z. **Review:** COMMENTED --- The profile is a convincing reason (`pthread_mutex_lock/unlock`, `__emutls_get_address` and the device use gate are about 10% of the queue worker's frame on Sonic Origins Plus), the first spinning version was caught and fixed on @simonbronk's contention benchmark, and the frame effect is measured on two machines (7.75 to 8.38 fps title reading on the 2080 Ti; SoRadGaming's Astro Bot Crash Site on a Radeon). The risk is in what it replaces. - It swaps 74 `std::mutex`es for a hand-written, non-fair, non-recursive mutex built on `std::atomic::wait`. The description's own account is that waiters that sleep at once widened a race main already has (`agc_driver_multisample_fill_host_only` failing most runs, #6512), which #6522 fixes. So this PR changes scheduling enough to expose latent races, and the safe merge order is #6522 first. State that in the dependency list and keep the test running in a loop in CI (`--repeat until-fail`), since the next latent race will show up in the same way and in some other test. - Priority and fairness: a lock that sleeps with no spin hands the CPU to another waiter at once. The benchmark (4-8 threads on one lock) shows it beating `std::mutex`; add the case that matters here, the GPU mutex held across a long operation by the queue worker while the packet worker and a loader thread wait, with the hold time of a real submit, and report the waiters' wake latency. - `HostThreadSlot` on Windows is a `TlsAlloc` slot read through the TEB. Say what happens if the slots are exhausted or `TlsAlloc` fails (it should throw at startup), and that a slot is released at shutdown. - `GpuMutex`'s owner token is the TEB address. If a thread could exit while holding it, a later thread that is given the same TEB address would appear to own the lock. A one-line statement that no thread can exit while holding the GPU mutex (or a test) would settle it. - 708 lines across the whole draw path conflicts with `main` repeatedly (the description lists five rebases). The conflicts show the risk: every change to a draw-path mutex now needs the new type. A staged landing would help: the `HostThreadSlot` and `Driver::timed` changes (no behavior change on contention) first, the mutex replacement second.
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
boykopovar/AnyPS5!6292
No description provided.