perf(agc): move hot flush-hook and SRT walk thread-locals to TEB slots #16420
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16420"
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 17bryen on 2026-11-06T16:57:07Z.
What
With MinGW every
thread_localaccess goes through__emutls_get_address, and with winpthreads that ends inpthread_getspecific, whose thread lookup takes one process-wide spin lock. #6615 moved the revalidation path's thread-locals to TEB slots (HostThreadSlot,ThreadScratch) and left the rest as they were. Sampling Echoes of Aincrad (PPSA22969)'s queue worker in a 5.9k-draw cave scene (APS5_SAMPLE_WORKER, 30 s) still showed 9.6 to 12.9 samples per frame in__emutls_get_address, 5.5 to 7.2 of them in the spin lock, reached from the sites below, so this moves them too:SrtEvaluatingBranch(SrtWalker.cpp), read on every pending-page shader read and set and restored by everySrtBranchScopeof an SRT walk, is aHostThreadSlot<bool>; the reference-returning accessor becomes a getter and a setter, andSrtBranchScopeuses them.HostThreadSlot.hppjoinsshaderCacheKeySources, as the cache key check requires for a header the recompiler includes.completionDepth(Recorder.cpp), read three times by every flush-hook access and bySyncThrough, is aHostThreadSlot<int>behindcompletionDepth();finish()'sInCompletionguard sets it.threadWaitedMs(Recorder.cpp) is aHostThreadSlot<double>.onOwnLiveStack's stack bounds,CounterFor's per-thread memory counters andepochCollectMap's map (GuestMemory.cpp) go throughThreadScratch: created on first use and destroyed with the thread byThreadOwned, as before.FlushDepthPlanes's re-entry flag (DepthSurface.cpp) is aHostThreadSlot<bool>.Driver::timed's per-worker profile (PacketExecution.cpp,APS5_PROFILE_DRAWonly) goes throughThreadScratch.Every slot starts zeroed, which is each variable's former initial value. Thread-locals elsewhere stay as they are.
The second commit gives
tools/tests/test_shader_cache_runtime_sources.py's fixture a stand-inHostThreadSlot.hppbeside itsHostThreadLocal.hppandAtomicSharedPtr.hpp, since the fixture configures the recompiler'sCMakeLists.txtand that now lists the header.Tested
Windows 11 (10.0.26200), WinLibs GCC 15.2.0 (UCRT), Release, NVIDIA GeForce RTX 5090, driver 617.14, AMD Ryzen 9 9950X3D.
mainis6ed10ccd1f, this PR31dde4f9e1on it (the measured driver commit is135d481eb1; the second commit touches only the test).Full suite on this PR, 4,357 tests: the 2,873 tests outside the GPU patterns with
VK_DRIVER_FILESpointing at no ICD all pass; the 1,484 tests matching^(agc|shader|recompiler|videoout|video_out|guest_video_out|vulkan)|gpuwithANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationat-j 4pass except two:agc_driver_direct_write_completionfails 3 of 3 alone on this PR and 3 of 3 alone on untouchedmain6ed10ccd1f("the completion of a buffer write dropped image results recorded after it"); not this change.agc_driver_unregistered_label_on_gpufailed once under the parallel run and passes 4 of 4 alone on this PR and 3 of 3 alone onmain.shader_cache_runtime_sourcesshards pass with the second commit; without it they fail at configure, as the NVIDIA run below found.Validation comparison on the focused set,
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer,ctest --test-dir build -R 'agc_driver_(shader_memory|guest_memory|depth_plane|image_memory_hooks|pipelined_dispatch|storage_flush|direct_write_completion)|agc_shader_srt|agc_collect|agc_private_collect|agc_unwatch_hook|agc_recorder_completion|shader_memory' -V(36 tests), onmainand on this PR: 35 of 36 pass on each. The loader insertedVK_LAYER_KHRONOS_validation(SDK 1.4.363.1) in 31 test processes in each log,was not found but was requestedappears 0 times in each, and neither log contains aVUID-line, so the change adds none.python3 tools/check_conventions.py --base upstream/mainpasses.python3 tools/pr_overlap.py --branch perf/agc-hot-thread-locals-teb(1,886 open PRs) finds one textual overlap: #16088 addsRemoveTree.hppon the line next to this PR'sHostThreadSlot.hppinshaderCacheKeySources; whichever lands second keeps both lines. 42 PRs that share a file could not be compared because they no longer merge withmain; none of them, nor any other open PR, moves these thread-locals (the open PRs were searched for the variable names,HostThreadSlot,ThreadScratch,emutlsandTEB).Echoes of Aincrad (PPSA22969), the first cave after loading the save, standing still, 3840x2160, two captures each of
main(baseline) and this PR, alternating, same session, warm shader cache.APS5_TRACE_SECONDS=1:30opened byAPS5_TRACE_TRIGGER_FILE90 s after the load,APS5_SAMPLE_WORKERon, no otherAPS5_*switch, log not redirected. Summary lines, digests, traces and the worker's collapsed stacks are below the checklist.Driver.Packet.draw(ms)Driver.Packet(ms)__emutls_get_addresspthread_spin_lockworker.collapsed.txtfiles, symbolized withnmagainst the respectivelibSceAgcDriver.prxand the system libraries' export tables (about 1 kHz, so about 550 samples per frame; a third of them in the worker's waits).tools/performance_compare.py, run 1 against run 1: frame p50 -4.8%, p90 -6.4%, mean -5.0%,Driver.Packet.draw-5.3%, GPU busy -0.4%; run 2 against run 2: p50 -13.6%, p90 -9.6%, mean -7.4%,Driver.Packet.draw-7.6%. The mean frame moved 20 and 30 ms in the two pairs; the p50 difference between the pairs (19 and 55 ms) shows the single-capture noise, so the mean is the figure to read.APS5_PROFILE_DRAWcaptures of the same view, not from these four, which ran with profiling off as the issue form asks.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 the worker's collapsed stacks (one zip, a folder per run with its
aps5-summary.jsonl):aincrad-teb-traces.zip(attached)The four summary lines, with
tools/trace_digest.py --stacks worker.collapsed.txtfor each trace:Baseline run 1 (main
6ed10ccd1f)seconds 1:30 from present 1378: 74 presents, frame p50 394.4 / p90 470.0 / p99 712.4 / max 712.4 ms, GPU busy 185.0 ms per present
NVIDIA GeForce RTX 5090, NVIDIA 617.14, Windows 10.0.26200, 3840x2160, commit
6ed10ccd1f, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 394.7, Driver.Packet.memory_wait 261.9, batch 184.6, Driver.Packet.dispatch 80.7, Graphics.Draw 79.0, Driver.WorkerWait 69.5, Driver.WorkerWait.dequeue_order 69.5, Graphics.Draw.draw_and_resource_release 56.7, Driver.Dispatch 46.8, Driver.Packet.draw 40.2
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Worker stacks (40960 samples, 12.6 % truncated; total leaves out the 6 frames every complete stack shares)
Baseline run 2 (main
6ed10ccd1f)seconds 1:30 from present 1383: 74 presents, frame p50 403.2 / p90 450.4 / p99 609.9 / max 609.9 ms, GPU busy 0.0 ms per present
NVIDIA GeForce RTX 5090, NVIDIA 617.14, Windows 10.0.26200, 3840x2160, commit
6ed10ccd1f, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 396.8, Driver.Packet.memory_wait 262.9, batch 188.6, Graphics.Draw 84.5, Driver.Packet.dispatch 80.9, Driver.WorkerWait 67.0, Driver.WorkerWait.dequeue_order 67.0, Graphics.Draw.draw_and_resource_release 62.9, Driver.Dispatch 46.6, Driver.Packet.draw 41.3
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Worker stacks (37148 samples, 12.6 % truncated; total leaves out the 0 frames every complete stack shares)
This PR run 1 (
135d481eb1)seconds 1:30 from present 1191: 78 presents, frame p50 375.4 / p90 440.0 / p99 580.7 / max 580.7 ms, GPU busy 184.1 ms per present
NVIDIA GeForce RTX 5090, NVIDIA 617.14, Windows 10.0.26200, 3840x2160, commit
135d481eb1, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 382.2, Driver.Packet.memory_wait 248.5, batch 184.7, Driver.Packet.dispatch 79.6, Graphics.Draw 75.7, Driver.WorkerWait 62.6, Driver.WorkerWait.dequeue_order 62.6, Graphics.Draw.draw_and_resource_release 55.8, Driver.Dispatch 46.5, Driver.Packet.draw 42.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Worker stacks (39707 samples, 13.3 % truncated; total leaves out the 6 frames every complete stack shares)
This PR run 2 (
135d481eb1)seconds 1:30 from present 1504: 82 presents, frame p50 348.4 / p90 407.2 / p99 1016.6 / max 1016.6 ms, GPU busy 177.2 ms per present
NVIDIA GeForce RTX 5090, NVIDIA 617.14, Windows 10.0.26200, 3840x2160, commit
135d481eb1, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 366.5, Driver.Packet.memory_wait 246.3, batch 176.8, Driver.Packet.dispatch 73.2, Graphics.Draw 62.8, Driver.WorkerWait 61.4, Driver.WorkerWait.dequeue_order 61.4, Graphics.Draw.draw_and_resource_release 45.7, Driver.Dispatch 41.5, Driver.Packet.draw 37.0
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Worker stacks (37149 samples, 12.1 % truncated; total leaves out the 6 frames every complete stack shares)