perf(agc): shard per-draw statistics counters per thread #19348
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/19348"
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 chitters2004 on 2026-11-13T05:01:43Z.
What
Several statistics the AGC driver counts on every draw are single
std::atomiccounters written from more than one thread. Each relaxedfetch_addfrom another core takes the counter's cache line away from the thread that wrote it last.Measured on the machine below with MinGW GCC 15.2 (winlibs,
-O3), one shared relaxedfetch_addcosts, per add per thread: 1.5-2.0 ns from 1 thread, 12.5-16 ns from 2, 35.5-39 ns from 4, 75-76 ns from 8 and 152-154 ns from 16 (agc_profile_counter_tests --bench N, two runs).The hot counters and their writers, found by recording the writing thread of each counter in
agc_driver_village_draw_cost_tests:DrawPipeline.cpp:Driver::draw→NoteDrawKey), about 3 adds per draw.heldReadRanges: the queue worker, and the draw finisher underAPS5_DRAW_PIPELINE, 1 per draw.heldReadQueriesandheldReadHits: the guest's own thread, throughHeldReads.These sit on two adjacent cache lines beside
runningCommitters.Recorder.cpp:readsNoted: the draw committer, inNotePendingRead, about 4 adds per draw.readQueries,readHitsandreadStaleIgnored: the queue workers, inPendingReadOverlaps, for every buffer copy a queue executes.These share one cache line.
ProfileCounter(Execution/include/ProfileCounter.hpp) keeps 16 cache-line-aligned slots:HostThreadSlot(the existinglibc/include/HostThreadSlot.hpp: a TEB read on Windows x64, a nativethread_localelsewhere).Addis a relaxedfetch_addon that thread's slot.Sumadds the slots, for reports.Takeadds and resets them, for the 10 s window reports.Up to 16 threads each own a line; past that, two threads share one slot, never all of them. The counters above are now
ProfileCounters. The printed reports and thePipelinedMetadataPassesandHeldReadQueriesaccessors are unchanged.APS5_SHARED_PROFILE_COUNTERS=1puts every thread on slot 0, the single shared counter this replaces, so the change can be measured on one build. It is listed indocs/dev/ENVIRONMENT.md.How the slot is found matters on Windows. A first version used
thread_local; MinGW GCC emulates it, so everyAddcalled__emutls_get_address(11-13 ns) and the counter lost to a shared atomic below four threads (13-15 ns against 1.5-1.9 ns on 1 thread, 16-20 against 13 on 2). Variants benched on this machine, ns per add per thread, 1/2/4/8/16 threads, second run:thread_localslot (first version)TlsGetValueTlsSlotsread (HostThreadSlot, shipped)fetch_addThe shipped form is 1.5-2.3 ns at every thread count on the final head. The single-writer forms save about 1.5 ns per add but need a context passed to every call site (or exclusive slot ownership); at about 8 adds per draw that is 12 ns of a 31.5 us draw, so they are left out. The thread-id hash collides on the 16 slots (13-15 ns at 8 threads in the first run). The bench source is not part of this pull request.
The first commit names the driver's threads on every platform.
PinWorkerThreadnamed the queue workers and the presenter only on Windows, and the draw committer and finisher were never named. On Linux every driver thread kept the process's name.VillageDrawCost, which finds the queue worker and the committer by name, therefore reported a driver CPU of 0.00 there, and perf, top and gdb could not tell the threads apart.NameDriverThreadsets the OS name (SetThreadDescriptionon Windows,pthread_setname_npelsewhere) to names that fit Linux's 15 bytes:qworker 0x<id>,presenter,draw-commitanddraw-finish. On Windows the queue workers take the short name instead ofqueue worker 0x<id>. Tracy and the performance trace keep their names, whichtools/trace_digest.pyreads.Performance
Machine: Windows 11 10.0.26320, AMD Ryzen 7 5800X (16 threads), 32 GiB, NVIDIA GeForce RTX 3080 driver 621.14 on PCIe Gen4 x16 (the link reads Gen1 at idle and is Gen4-capable at x16), Windows Defender real-time protection off, Release build (MinGW GCC 15.2). Title PPSA04263 01.011.001, save
play1-first-autosave(restored before each boot), 3840x2160, standing scene after the intro. Main isedb7d91caa67(the merge base of this branch) and the change is8610baceca75; the libraries of both were built in the same tree, incrementally, and each boot copies its own into the runtime. WindowAPS5_TRACE_SECONDS=112:30: 20 s afterstood_still, which the four boots reached at 88.8 to 93.4 s after process start (nav script withNAV_FAST=1, used by all four arms). One warm-up boot per side after the rebuild (not samples), then main, change, change, main (A1 B1 B2 A2); caches warm. OnlyAPS5_TRACE_SECONDSwas set; the harness itself also setsAPS5_TRACE_SUMMARY_EVERY=10for both sides (it is in every summary line below). Every boot checked CLEAN byquiet-check.py. A first ABBA of the same binaries used an anchor 50 s afterstood_still(the cold warm-up boot of main stood at 122.6 s and set it), its window ended after the boot did, and it is discarded and not listed.Driver.Drawms/frameGraphics.Drawms/frameedb7d91caa8610bacecaMeans (n=2 each): main frame 114.67 ms (A1 119.70, A2 109.65), change 111.86 ms (B1 111.05, B2 112.67);
Driver.Drawmain 82.07 against change 80.02 ms per frame.Reading: this change makes no measurable difference to GTA frame time. The two main boots differ by 10.1 ms (A1 is slow, A2 is not), the two change boots by 1.6 ms, and the main-against-change gap of 2.8 ms is inside that same-binary spread, so n=2 cannot resolve it. A same-binary control on the earlier head (the switch
APS5_SHARED_PROFILE_COUNTERS=1against the sharded counters, four boots, CLEAN) showed the same: 112.2 ms shared against 112.0 ms sharded, with the sharded boots alone spanning 109.6 to 112.8 ms. That fits the arithmetic: the counters take about 8 adds per draw, 8 x 13-39 ns = 0.1-0.3 us per draw when shared and 0.01 us when sharded, so at about 1,470 draws per frame the most this can move is 0.1-0.4 ms per frame. The change removes cross-thread cache-line traffic that grows with the number of writer threads and with copies running beside draws; it is shown here by the microbenchmark and is not claimed as a GTA speed-up.Summary lines (
aps5-summary.jsonl, theseconds 112:30line of each run, in the order A1, B1, B2, A2; the traces are kept locally underPPSA04263/perf/c02-pc7b-<boot>and can be attached on request):Tested
bb6cb0338e), local Windowsctest -Vwith the Khronos validation layer (SDK 1.4.335.0),VK_KHRONOS_VALIDATION_VALIDATE_SYNC=1, RTX 3080, same machine for both trees:agc_driver_pipelined_metadata_pass,_draw_pipeline,_plan_verify,agc_driver_queued_index_read_order,agc_driver_village_draw_cost,agc_driver_recorder: 6/6 pass on main and on the change, 0VUID-lines and 0 sync hazards. Not repeated locally after the rebase ontoedb7d91caa; the fork CI run below covers the rebased head. On the rebased head, locally:agc_profile_counter,agc_profile_counter_sharedandagc_profile_counter_tls_expansion: 3/3 pass.agc_profile_counter_testschecks that no add is lost from 1, 2, 4, 16 and 20 threads, that up to 16 threads get slots of their own, that the switch puts all threads on one slot, and thatTakereturns every thread's adds and resets every slot. A counter with one slot, aSumthat reads only the first slot and aTakethat does not reset all fail it.--tls-expansion(Windows) takes the first 64 TLS indices so the counter's index is in the TEB expansion slots.8610baceca(baseedb7d91caa): linux-gate (lavapipe, validation layer) 11/11 pass on the change and 9/9 on main, 0 VUIDs, nothing failing on the change but not on main; windows 11 of 12 pass on the change (the three newagc_profile_counter*tests pass),agc_driver_recorderfails on main as well (lavapipe on the Windows runner), 0 VUIDs; tsan 3 pass, 8 skipped (stubbed in that job). Verdict GREEN.tools/check_conventions.py --base edb7d91caa6on the change: all checks passed. The branch merges cleanly into origin/maina6fbf9ce96(git merge-tree).thread_local, toolchain not recorded): one shared atomic 4.7 ns from 1 thread, 47 from 2, 115 from 4;ProfileCounter4.8, 4.7, 4.7. Not repeated on the final head.Windows bench on the rebased head
8610baceca(the first block is the second run),agc_profile_counter_tests --bench N, ns per add per thread, two runs:Conflicts: #17697 is merged and this branch is rebased onto it. Textual overlap remains only with #15456 (
DrawPipeline.cpp); whichever merges second keeps both sides' lines.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)🤖 Generated with Claude Code