perf(agc): share collect walks between threads after the caller's ordering point #17786
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17786"
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 klosixgaming-arch on 2026-11-09T15:55:30Z.
What
Queue worker 0 and the draw committer spend a large share of their time in Windows write-watch walks (
GetWriteWatchthroughcollectWindowsWrites). These walks check whether the game's CPU wrote a surface before a draw or dispatch reads it. On Cyberpunk 2077's character creator a diagnostic counted about 280,000 walks per 10 s over 166 GB of address range, taking 3.1 s. Only 0.7% of them found a CPU write. The same 3-64 MB render targets were walked hundreds of times, because the collect memo is per thread and is reset at every ordering point (about 1,400 per 10 s).This change shares walks between threads by the time they started:
Submit(enqueuedAt), not the time the worker reaches it. With the usual backlog of a few hundred ms, this lets walks made while earlier submissions ran serve the later ones. A submission with a rewind tail uses the current time. When a submission does not bump the epoch (it was enqueued before the last bump), its point is raised to its submit time.WAIT_REG_MEMthat a label stored by this driver satisfies takes the ordering point of the queue that stored it. A queue's point never moves earlier. The rules for that inheritance:UnchangedSinceCollected.commitStore) never pass on a point.APS5_NO_SHARED_COLLECT_WALKSturns the lookups off, andAPS5_ABcan toggle it. WithAPS5_PROFILE_DRAW, an[epoch]line reports per queue how many ordering points fell at the bump and how far back the rest lie.Why a shared walk is never too old
A collect takes a shared walk only when the walk started strictly after the caller's ordering point (
started > since). The start time is read before the walk takes the tracker lock and callsGetWriteWatch, so the walk reports every CPU store that was visible before its start. Both times come fromsteady_clock. So it is enough that every CPU store a packet must see is visible before the packet's point:Submit.SubmitreadsenqueuedAtwhile it holds the driver's submit mutex. Taking the mutex makes the submitting thread's earlier stores visible first. A store by another thread is ordered before the submission only if the guest synchronized it with the submitting thread beforeSubmit, and x64 makes a store visible to all threads at once. So these stores are visible beforeenqueuedAt, and a walk that started after it reports them.Submitand before the worker reaches the packet. On the GPU, such a store is ordered before a packet only through aWAIT_REG_MEMthat a label stored after the store satisfies. A packet that no such wait orders is not ordered after the store on hardware either: the GPU may read the memory before or after it. Such a packet may take a walk made betweenSubmitand the store. For the packets after the wait:publishQueueSinceat each of its ordering points; a predicated packet also publishes its read time) before it ran the label's packet, and it records the store in the label history after that. The waiting worker finds the store in the history under the same mutex before it reads the published point. So it reads a point at least as late as the writer's point at the label store, and by the same two cases on the writer's queue, every CPU store the label store was ordered after was visible before that point. The published point is a running maximum. A later point than needed only costs a walk.main, there is no new ordering point. The queue's own earlier point already covers what the label store was ordered after.CheckStoreAfterSubmitinagc_collect_memo_generationpins case 2.Tested
Body Type on current
main, 2026-11-15Cyberpunk 2077 (PPSA03974) 02.120.000, character creator Body Type, 3200x1800; Windows 11 (10.0.26200), RTX 5090 (NVIDIA 617.42), Ryzen 9 9950X3D. Both builds are
mainb39603dbdc+ #19071 (the game only reaches the creator with it); the change is merged on top. Each line is the 20 s trace window opened about 80 s after entering the creator, one boot each, the same environment on both sides.main+ #19071 (262be96458), 136 presents in the window:This PR on the same base (
e66d0510c5), 145 presents in the window:In-run A/B on this PR's build,
APS5_AB=10:APS5_NO_SHARED_COLLECT_WALKSfrom 150 s, phases after 200 s (Body Type): n 14 B-A -8.39 +- 0.39 presents/10 s (-11.6% +- 0.5) A 72.2 B 63.8. B turns shared walks off, so the PR is A. Shared walks off costs -11.6% +- 0.5; in other words this PR is worth about +13% here.Traces, the two summary lines,
tools/trace_digest.pyoutput (digest-mbase1.txt,digest-m17786a.txt) and the A/B phases: https://github.com/klosixgaming-arch/AnyPS5/releases/tag/perf-captures-2026-11-15Windows, NVIDIA GeForce RTX 5090. Cyberpunk 2077 (PPSA03974), character creator Body Type screen, 20 s windows.
Current
maincannot reach this screen on its own:APS5_CAPTURE_UNPROVEN_PENDINGcapture that my Cyberpunk tree carries).So both builds below are
main364ceeba2fwith #16974, #17791 and that capture commit. The change build adds only this PR's commit.Same machine and session, same settings on both sides, caches warm, no
APS5_PROFILE_*. Results:The median frame is 20% shorter (471 → 377 ms), and presents go up 24%.
Summary lines
The four
aps5-trace-*.json.gzfiles are named in the lines above. I will attach them to this PR.In-run A/B
On my older Cyberpunk tree (
f5fe807353plus #11485, #10443 withAPS5_SETTLE_FLOOR_KIB=64, and dispatch relocation), I ran an in-run A/B with:APS5_AB=30:APS5_NO_SHARED_COLLECT_WALKS;APS5_AB_START=170s;APS5_PROFILE_DRAW=1.There were 6 A and 6 B phases of 30 s, split with
tools/ab_split.py:A is this change and B is the old behaviour, in the same process. Frame dumps of the Body Type screen render correctly in every run.
Tests:
agc_collect_memo_generationgains a cross-platformCheckSharedWalks. It covers reuse after an earlier point, a walk before the point, a raised point, sharing off, and points never moving earlier. It passes on Windows and fails when sharing is off.agc_collect_memo_generationalso gainsCheckStoreAfterSubmit. Another queue walks the range after the submission's point, and then the CPU stores to it:Submit, and one ordered by a point inherited from a queue that stored its label after the store.It passes on Windows (test commit
1c7b4446f5, before the merge ofmain). Withstarted > sincedropped fromsharedWalkCovers,CheckSharedWalksfails, and withCheckStoreAfterSubmitrun first, (ii) fails.These also pass on Windows:
agc_collect_*,agc_write_tracking*,agc_runtime_switches*,agc_driver_same_queue_label_wait*,agc_driver_finisher_label_order*,agc_driver_copied_writer_label_order*,agc_driver_unimported_label_order*,agc_driver_two_device_pending_store_label,agc_driver_wait_shutdown,agc_driver_fill_completion_labels*,agc_driver_alias_copy_label,agc_driver_label_after_import_copy,agc_driver_queue_zero_idle_wait,agc_driver_present_key_writer_wait,agc_predication_tests,agc_wait_on_address_size,agc_pipeline_queue_failure_wait,agc_driver_resource_cache_collect_refusalsand the recorder tests.agc_driver_pipelined_predicated_label_orderandagc_driver_unregistered_label_behind_write_backdid not build on my machine: their object dependency paths exceed the Windows path limit.Known limit: under
APS5_SUBMIT_PROGRESS,submitOpenWorkrecords deferred labels as queue 0 stores. Such a label passes on queue 0's point instead of its own queue's.Series
These PRs come from the same work on Cyberpunk 2077's character creator, Body Type screen, where the draw committer and queue worker 0 are the limit. They go together:
Each gain was measured with the earlier rows already in place, so the rows add up. The last row was measured on the intro movie, the others on Body Type; both scenes are limited by the same draw committer. Most of them are small or zero on
mainalone, becausemainis limited elsewhere on this screen; each PR says what it measured there. Not a PR:ANYPS5_PRESENT_MODE=mailboxgave +12 to 14% andimmediate+18 to 21% on the same screen, because FIFO holds the flip that the next frame's rendering wait waits for.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)