Repository navigation
perf(agc): replace winpthreads locks and TLS on the draw path - #6292
Conversation
|
Windows / AMD results for this head, in a scene where queue worker 0 is the limit: ASTRO BOT (PPSA21564 01.007.000), 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.
Method. No input, warm shader cache (1,904 hits, 0 misses),
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. |
6170a0c to
54b0ce0
Compare
|
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
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.
|
… report boykopovar#6292 moves the APS5_PROFILE_DRAW check in Driver::timed to the top and rewrites the report's condition, the two lines this branch's driver-time line sat between, so the two conflicted. The line now follows the report. It reads the same flag and times, so it counts the same.
562e4c8 to
760bdf5
Compare
|
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 One ask, about where the new test lives. |
760bdf5 to
ee2cd67
Compare
|
Thanks, and thanks for checking the slots against
|
ee2cd67 to
c4a6636
Compare
CnRJay
left a comment
There was a problem hiding this comment.
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 onstd::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_onlyfailing 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. HostThreadSloton Windows is aTlsAllocslot read through the TEB. Say what happens if the slots are exhausted orTlsAllocfails (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
mainrepeatedly (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: theHostThreadSlotandDriver::timedchanges (no behavior change on contention) first, the mutex replacement second.
|
GTA V (PPSA04264) data and pitfalls, from our older POC base (gtav-next11, Oct 16; RTX 4090, i9-13900K, GCC 15.2), not this head.
Method: I can run GTA's open world on your head (committer us/draw, base vs PR) if useful. AI-assisted: yes (Claude Code) |
c4a6636 to
7074425
Compare
7074425 to
2659ec3
Compare
|
Checked the rebased head against current main: same patch as the reviewed one; the remaining |
What
On Windows the driver is built with MinGW:
std::mutexlocks and unlocks through winpthreads (pthread_mutex_lock,pthread_mutex_unlock);thread_localis emulated: every access calls__emutls_get_address, which callspthread_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_lock1.22,pthread_mutex_unlock0.82,__emutls_get_address2.15 (1.80 of it inpthread_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.std::atomic::wait, which spins 12 pauses and 4 yields itself before it sleeps). Likestd::mutex, it is neither recursive nor fair.std::mutexwith 4 to 8 threads on one lock, and that waiting at once beatsstd::mutexat every thread count. Without the spin, main'sagc_driver_multisample_fill_host_onlyfailed most runs: waiters that sleep at once widen a race main already has (agc_driver_multisample_fill_host_only fails intermittently: the sentinel fill after an absorbed multisampled clear does not land #6512), a multisampled target's clear recorded on another device's recorder. fix(agc): record a multisampled target's commands on its own device #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.std::mutexes of the draw path. perf(agc): pipeline queue-0 draw commits behind the packet worker #1201 already made two more aSlimMutex(the pending storage writes and the unit shadows), and those stay as they are.std::mutex: the five astd::condition_variablewaits with, and the sparse and settle queue mutexes, which the context hands out asstd::mutex*.DeviceUseGatekeeps 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.GpuMutextakes anAgcDriver::Mutexand 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.TlsAlloc), read and written through the thread environment block in one or two instructions. Elsewhere it is athread_local.ThreadOwned).Driver::timedreads no clock unlessAPS5_PROFILE_DRAWasks for its report. A wait packet reads it only forAPS5_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:try_lock, and a sleeping waiter's wake;With this change, per frame:
pthread_mutex_lock1.22 to 0.31,pthread_mutex_unlock0.82 to 0.20,__emutls_get_address2.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.
surfacesMutexastd::recursive_mutex, and its callers lock it again while they hold it. It stays astd::recursive_mutexand is not switched toAgcDriver::Mutex.AgcDriver::Mutex.agc_driver_image_array_window_past_layer_limitis not run: MinGW does not build itsalignas(65536).guest_kqueueexited with 0xc0000409 once under--parallel 4. It passed 6 of 6 runs alone.IndirectDispatch.cppkeeps main'sGuestBufferMemory.hppinclude, and the buffer pool keeps main's memory property fields, next toAgcDriver::Mutex.Snapshots().mismatched, soGuestBufferMemory.cpponly turnsreportMutexinto anAgcDriver::Mutex.agc_driver_image_array_window_past_layer_limitis not run: main does not compileImageArrayWindowPastLayerLimit.cppwith MinGW (alignas(65536)).agc_driver_pass_input_hazard(added in ba4571a) fails ("a pass writing the indices an open pass reads in place took no barrier") on plain main 1d41645 as well, 0 of 4 runs alone.AdoptLatestCollectEpochandlastBumpedEpochread the collect epoch through theHostThreadSlot.LockSpinning) inside its recursion guard.dumpRequest's new suffix and the descriptor pool'sfreeDescriptorSetsstay, withAgcDriver::Mutex.AcquireCachetakes theAgcDriver::Mutex.agc_driver_bda_devicefails (vkWaitForFences graphics: Vulkan result -4) on plain main 0bc3003 as well, 0 of 4 runs alone.drawCacheMutex, main added the draw relocation fields (perf(agc): find cached draws across moved user-data pointers #4943), and perf(agc): journal the pipelines of a run and build them ahead on the next one #5875 keyed the compute pipelines byComputePipelineKeyand dropped the pipeline store'sscratchKey.drawCacheMutex,computePipelinesMutexand the pipeline store's mutex stayAgcDriver::Mutexes, and every lock of them is astd::lock_guardorstd::unique_lockas before. The collect memo ring goes through its thread slot as before. perf(agc): pipeline queue-0 draw commits behind the packet worker #1201's per-epoch collect map next to it keeps itsthread_local, like the other thread-locals main added since.tools/check_conventions.pypasses:HostThreadSlottakesTlsAllocandTlsSetValuefrom<processthreadsapi.h>, and the GPU mutex's unlock keeps main's comment where it was.agc_driver_gpu_indirect_arguments_misaligned_writer_ahead, which main itself does not compile with MinGW), including the newagc_driver_locks.agc_driver_multisample_fill_host_onlyfailed 0 of 200 runs (8 at a time) on main 72c5eed. On 44df216 it also failed 0 of 400 withAPS5_PIPELINED_DRAWS=0(4 at a time) and 0 of 40 alone, and without fix(agc): record a multisampled target's commands on its own device #6522's line it failed 40 of 200 and 7 of 40.APS5_TRACE_SECONDS=200:15on the same machine in one session, caches warm. Traces attached.This pair overstates the change. The baseline's window ran slow outside the draw path:
Driver.Packetminus 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.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)Traces and summary lines (baseline b8acbb0, this change 824084e):
locks-traces.zip
The two summary lines, with
tools/trace_digest.pyfor each trace:Baseline (summary commit a38ad5e)
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 a38ad5e (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)
Threads (ms per frame)
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
This change (summary commit b8acbb0)
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 b8acbb0 (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)
Threads (ms per frame)
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