Skip to content

perf(agc): skip the held-read walk while no queued draw holds a read range - #10689

Closed
johandavidcm wants to merge 6 commits into
boykopovar:mainfrom
johandavidcm:perf/agc-held-reads-prefilter
Closed

johandavidcm wants to merge 6 commits into
boykopovar:mainfrom
johandavidcm:perf/agc-held-reads-prefilter

Conversation

@johandavidcm

@johandavidcm johandavidcm commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

What

DrawPipeline::HeldReads took the draw pipeline's mutex and walked every queued item's read ranges on each call. StorageTexture::FlushPending asks it once for every in-place draw input (InPlaceDrawInput, Draw.cpp:1215), so with in-place inputs the draw committer took that lock on every draw, against the worker's Enqueue, and found a held range about once in a thousand calls.

In Yakuza 0 Director's Cut (PPSA31327) with APS5_DRAW_INPUTS_IN_PLACE=1 and APS5_DRAW_PIPELINE=1, a gameplay-only sample of the committer put HeldReads at 9.3 % of its time, 8.2 % of it waiting for that mutex; the [draw] pipelined line showed counted about 360 thousand held reads found per 10 s.

  • The pipeline now counts the queued items that carry read ranges (itemsWithReads). HeldReads answers false without the lock while the count is zero.
  • The count moves under the lock where items are enqueued and retired, and when ReleaseRunningReads clears the running item's ranges, so it can only be stale towards the walk: a reader that sees a non-zero count takes the lock and walks as before.
  • APS5_NO_HELD_READ_PREFILTER=1 walks on every call as before. docs/dev/ENVIRONMENT.md gets the row.
  • DrawPipeline::HeldReadQueries exposes the existing walk counter for the test.

Depends on #9490, which caches the APS5_NO_QUEUED_DRAW_READ_ORDER lookup in the same function; this branch has it merged, and the diff above it is the change here.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, on main 6cd36e6 + #9490 (head 156b9b1).

New test agc_driver_held_reads_prefilter (tests/execution/HeldReadsPrefilter.cpp): parks a commit on the committer and checks, through the public API, that a queue holding no read ranges reports nothing and does not take the lock (HeldReadQueries stays flat), that a queued range is reported for an overlapping query and not for a disjoint one, and that a drained queue reports nothing.

  • Without the prefilter (the return false line replaced by a no-op) it fails with a query against a queue holding no read ranges took the pipeline lock.
  • With the change it passes, also with --repeat until-fail:20.

Pass with the change: agc_driver_queued_index_read_order, agc_driver_vertex_table_read_order, agc_driver_draw_pipeline_trace, agc_driver_draw_pipeline_trace_disabled, agc_driver_draw_finisher_trace, agc_driver_draw_finisher_trace_disabled.

With ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation (Vulkan SDK 1.4.363.0; the loader log shows the layer inserted), ctest -R <those tests> -V passes on main's DrawPipeline.cpp and with this change, and neither log has a VUID- line or a missing-layer message.

PPSA31327 01.015.000, gameplay in Kamurocho, character standing still, 1920x1080, warm caches, scripted boot through the first dialogue (APS5_PAD_INPUT_FILE, APS5_NO_PAD_INPUT=1), APS5_TRACE_SECONDS=140:60, both builds with APS5_DRAW_PIPELINE=1 APS5_DRAW_INPUTS_IN_PLACE=1, runs alternated.

Pair Base p50 (presents in 60 s) This change p50 (presents) Change
1 228.8 ms (261) 228.5 ms (261) -0.1 %
2 219.5 ms (272) 216.1 ms (277) -1.6 %
3 229.0 ms (260) 219.3 ms (273) -4.2 %
4 221.5 ms (269) 214.7 ms (277) -3.1 %
mean 224.7 ms 219.7 ms -2.2 %

The frame moves less than the committer because the worker, which waits on the committer for room, takes part of the slack. The committer-side counters (APS5_PROFILE_DRAW, medians of the last gameplay intervals of 10 s, one profile run of each build):

per 10 s Base This change
committer time per draw ([draw] pipelined) 25.2 µs 23.3 µs (-7.5 %)
held reads found 353k 370k (the walk still runs whenever a queued item holds reads)
vertex phase of [draws] (holds InPlaceDrawInput → FlushPending → HeldReads) 1,651 ms 1,161 ms (-30 %)
worker wait for room per draw 5.6 µs 4.6 µs

GPU busy per present is unchanged (72-77 ms both ways). The counters are the mechanism; the title's frame is the result.

Summary lines (pair 3: base, then this change)
{"commit":"5f4ff174e827","dirty":false,"env":{"APS5_DRAW_INPUTS_IN_PLACE":"1","APS5_DRAW_PIPELINE":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"140:60"},"gpu":"NVIDIA GeForce RTX 4070","driver":"NVIDIA 621.01","os":"Windows 10.0.26200","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31892,"title_id":"PPSA31327","title":"Yakuza 0 Director's Cut","content_version":"01.015.000","resolution":"1920x1080","window":{"spec":"seconds 140:60","first_present":3007,"ms":60060.227},"presents":260,"frame_ms":{"p50":228.977,"p90":238.270,"p99":272.747,"max":328.078},"gpu_busy_ms_per_present":77.251,"worker_busy_pct":{"queue worker 0x0":98.180},"top_phases_ms_per_frame":{"Driver.Packet":226.715,"Graphics.Draw":196.089,"Driver.Packet.draw":193.767,"Driver.Draw":192.199,"Driver.DrawFinish":166.756,"Driver.Draw.prepare":158.977,"Graphics.Draw.draw_and_resource_release":150.126,"Driver.DrawFinish.shader_compile_and_link":116.339},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":4.163,"events_lost":3542234,"totals_full":false},"draw finisher":{"events_full_after_s":8.160,"events_lost":1698727,"totals_full":false},"draw committer":{"events_full_after_s":12.636,"events_lost":991325,"totals_full":false}},"trace":"aps5-trace-5f4ff174e827-84060.json.gz"}
{"commit":"156b9b11d498","dirty":false,"env":{"APS5_DRAW_INPUTS_IN_PLACE":"1","APS5_DRAW_PIPELINE":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_SECONDS":"140:60"},"gpu":"NVIDIA GeForce RTX 4070","driver":"NVIDIA 621.01","os":"Windows 10.0.26200","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31892,"title_id":"PPSA31327","title":"Yakuza 0 Director's Cut","content_version":"01.015.000","resolution":"1920x1080","window":{"spec":"seconds 140:60","first_present":3026,"ms":60063.155},"presents":273,"frame_ms":{"p50":219.289,"p90":226.611,"p99":235.173,"max":327.792},"gpu_busy_ms_per_present":76.967,"worker_busy_pct":{"queue worker 0x0":98.113},"top_phases_ms_per_frame":{"Driver.Packet":215.791,"Driver.Packet.draw":183.004,"Driver.Draw":181.571,"Graphics.Draw":177.628,"Driver.DrawFinish":156.106,"Driver.Draw.prepare":149.026,"Graphics.Draw.draw_and_resource_release":146.527,"Driver.DrawFinish.shader_compile_and_link":114.131},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":4.211,"events_lost":3487719,"totals_full":false},"draw finisher":{"events_full_after_s":8.266,"events_lost":1672678,"totals_full":false},"draw committer":{"events_full_after_s":13.393,"events_lost":919577,"totals_full":false}},"trace":"aps5-trace-156b9b11d498-93240.json.gz"}

tools/check_conventions.py --base origin/main passes.

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: perf(agc): read APS5_NO_QUEUED_DRAW_READ_ORDER once in the held-read check #9490
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4070, driver 621.01, Windows 11 Pro 26200
  • AI-assisted: yes

JJenkx and others added 3 commits October 22, 2026 01:41
…check

DrawPipeline::HeldReads called std::getenv on every call. It runs for
each StorageTexture::FlushPending, which InPlaceDrawInput calls while the
draw committer records a draw. On Windows, UCRT's getenv takes the
environment lock and scans the block on each call: on the title screen of
Spirit of the Island (PPSA13934), 375 of the draw committer's 5124
steady-state samples (7.3 %) were in ucrtbase under HeldReads, against
under 0.3 % for the range scan itself.

The variable is now read once into a static, as WaitMemory.cpp already
does for the same variable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…range

DrawPipeline::HeldReads took the pipeline mutex and walked every queued item's read
ranges on each call. StorageTexture::FlushPending asks it for every in-place draw
input, so with in-place inputs the committer took that lock once per draw, against the
worker's enqueues, and found a held range about once in a thousand calls.

The pipeline now counts the queued items that carry read ranges. HeldReads answers
false without the lock while that count is zero; the count moves under the lock where
items are enqueued, retired and when the running item's reads are released, so it can
only be stale towards the walk. APS5_NO_HELD_READ_PREFILTER=1 walks as before.
HeldReadQueries counts the walks for the test.
@github-actions github-actions Bot added the perf Performance change label Oct 24, 2026
@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6186 docs/dev/ENVIRONMENT.md L258
PR 9898 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp L321-323
PR 10677 core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp L413

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.

Pull request Shared files not compared
PR 1896 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp
docs/dev/ENVIRONMENT.md
PR 6188 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp
PR 6924 docs/dev/ENVIRONMENT.md
PR 7449 docs/dev/ENVIRONMENT.md
PR 7457 docs/dev/ENVIRONMENT.md
PR 7484 docs/dev/ENVIRONMENT.md
PR 7604 docs/dev/ENVIRONMENT.md
PR 7642 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp
docs/dev/ENVIRONMENT.md
PR 8004 docs/dev/ENVIRONMENT.md
PR 8246 docs/dev/ENVIRONMENT.md
PR 8484 docs/dev/ENVIRONMENT.md
PR 8542 docs/dev/ENVIRONMENT.md
PR 9012 docs/dev/ENVIRONMENT.md
PR 9176 docs/dev/ENVIRONMENT.md
PR 9248 docs/dev/ENVIRONMENT.md
PR 9287 docs/dev/ENVIRONMENT.md
PR 9289 docs/dev/ENVIRONMENT.md
PR 9345 docs/dev/ENVIRONMENT.md
PR 9356 docs/dev/ENVIRONMENT.md
PR 9387 docs/dev/ENVIRONMENT.md
PR 9508 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp
docs/dev/ENVIRONMENT.md
PR 9676 docs/dev/ENVIRONMENT.md
PR 9852 docs/dev/ENVIRONMENT.md
PR 10193 core/libs/prx/libSceAgcDriver/Execution/include/Driver/Draw/DrawPipeline.hpp
core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPipeline.cpp
docs/dev/ENVIRONMENT.md
PR 10442 docs/dev/ENVIRONMENT.md
PR 10547 docs/dev/ENVIRONMENT.md
PR 10757 docs/dev/ENVIRONMENT.md

@github-actions github-actions Bot added docs agc libSceAgc* labels Oct 24, 2026

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 156b9b11d498ca9b7f8af26e325acb572036ffca. The counter is updated while holding the same mutex that protects items; retirement and ReleaseRunningReads decrement it under that mutex. Therefore a zero observed by the lock-free prefilter cannot hide a queued read range, while a positive observation falls back to the existing locked range scan. The focused test covers the empty, overlapping, disjoint, and drained cases, and the report includes paired Windows validation-layer and title measurements. Local AGC execution is unavailable on this host because the CPU feature gate aborts before the tests; I have not counted those tests as locally passing. Hosted intake, conventions, and labels checks pass; product build jobs were skipped by policy.

@oneandonlydean oneandonlydean self-assigned this Oct 25, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@johandavidcm Please make HeldReadsPrefilter.cpp clean up its parked commit on every exit. The lambda captures Parked by reference, but a failed Require before Release destroys that local while the commit can still read parked.release for up to ten seconds; the missing-prefilter negative control takes this path. Keep the state alive until the commit ends, release and drain it during exception cleanup, and exercise a planted assertion failure and a startup timeout with bounded cleanup.

@thuanquang

Copy link
Copy Markdown
Contributor

I read head 156b9b11d4, its merge with current main and the new test (code reading only; nothing built or run here). The prefilter itself looks sound to me: every path that adds or removes an item's reads (Enqueue, the retire in run(), ReleaseRunningReads) moves itemsWithReads once under the pipeline mutex, and a zero it reads can only miss an Enqueue that races the query, which the locked walk could also order after the query.

Two test gaps, separate from Dean's point about the parked commit:

  1. Merged with current main, perf(agc): skip the queued-draw read check when a storage flush stores nothing #8911's check can no longer fail. perf(agc): skip the queued-draw read check when a storage flush stores nothing #8911 (merged 2026-10-25 15:19Z, after the approval here) added HeldReadQueries() as a count of every query, and QueuedIndexReadOrder.cpp (main L375-377) asserts that a FlushPending with nothing to store leaves it unchanged, i.e. that nothingToStore() returns before HeldReads. The merge is clean and keeps one HeldReadQueries(), but on this branch the count sits behind the prefilter (DrawPipeline.cpp L475 returns before the fetch_add at L478). No queued item holds reads at that point of the test, so if perf(agc): skip the queued-draw read check when a storage flush stores nothing #8911's early return regressed, HeldReads would answer from the prefilter without counting and the assertion would still pass. Counting queries before the prefilter, with a separate walk counter for this PR's test, keeps both checks meaningful.

  2. Nothing checks that the count returns to zero. The only prefilter assertion (HeldReadsPrefilter.cpp L54-56) runs before any item with reads has been queued. After block 2's Release(), L66 checks only the answer, which a locked walk of the empty queue gives too. With the retire decrement (DrawPipeline.cpp L414) deleted, or moved after the std::move at L415 where it would test an emptied vector, itemsWithReads stays at 1 and the test passes, while the prefilter is off for the rest of the process. ReleaseRunningReads (L493-498, the pipelined DMA copy's path) isn't exercised. A walk-count-unchanged check around the L66 query, plus a parked commit that calls ReleaseRunningReads() and checks that a query skips the walk while it is still running, would cover both decrements.

AI-assisted: yes (Claude Code)

@babyhuey

babyhuey commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Linux run of this head 156b9b11d49, merged onto main a7a2eece499 (clean merge, local only), with the selection CI's skipped Linux product job uses: anyps5-build CI image (Ubuntu 24.04, GCC 13, Release, BUILD_TESTING=ON), lavapipe with ANYPS5_REQUIRE_VULKAN=1 GALLIUM_OVERRIDE_CPU_CAPS=avx. The tree was configured fresh after the merge, then select_tests.py select / ninja -f select.ninja selected / --target libs / select_tests.py ctest --timeout 120.

  • Selected 1443 tests: all pass.

Edited: the first version of this comment reported 23 failures and called them lavapipe-only AGC tests that also fail on main. That was wrong. My container ran with Docker's default seccomp profile, which blocks userfaultfd, so the write-watch tests failed on main and on this PR alike. With --security-opt seccomp=unconfined, main passes all 2,274 tests and this head passes all 1443 selected tests, so those tests are now actually covered.

AI-assisted: yes (Claude Code)

…ort the walks

HeldReadQueries counts every query, as main's QueuedIndexReadOrder test
expects since boykopovar#8911; behind the prefilter it missed the queries the
prefilter answered. HeldReadWalks counts the locked walks, and the
[draw] pipelined line prints both per report.
… every exit

The commit owns its state through a shared pointer and a guard releases
and drains it during unwinding, so a failed check no longer leaves a
commit reading a destroyed local. The test also checks that queries are
counted ahead of the prefilter, that the walk stops after the holding
item retires and after ReleaseRunningReads, and that a planted failure
and a startup timeout both clean up within a bound.
@johandavidcm

Copy link
Copy Markdown
Contributor Author

Head is now f0ffd6016 (merged with main 9f386f3; the diff above the merge is the PR plus the two commits below).

@oneandonlydean, done in test(agc): keep the held-read prefilter test's parked commit alive on every exit: the commit owns its state through a shared_ptr captured by value, and a guard (ParkedCommit) releases the commit and drains the queue in its destructor, so a Require that fails after parking, or the start-up wait that times out, unwinds through the release and the drain before any local goes away. The test exercises both: a planted Require(false) after parking, and a commit that never announces itself against a 200 ms start-up deadline. Each has to surface its own error, leave the pipeline not busy and no read held, and finish within 5 s (the commit's own loop stops at 10 s as the backstop).

@thuanquang, both points, in perf(agc): count every held-read query ahead of the prefilter and report the walks:

  1. HeldReadQueries counts every query before the prefilter, so perf(agc): skip the queued-draw read check when a storage flush stores nothing #8911's check in QueuedIndexReadOrder keeps its meaning (it passes on this head); HeldReadWalks counts the locked walks. The [draw] pipelined line prints both per report (N held-read queries of which M walked the queue), so a title's log shows the mechanism directly.
  2. The test now checks that the count returns to zero on both decrements: after the holding item retires, the query over its range answers false with the walk count flat; and a parked commit that calls ReleaseRunningReads() on itself is still running when a query over its range answers false with the walk count flat.

Mutations, each rebuilt and run on its own, the test restored in between:

  • the prefilter removed (the early return false replaced by a no-op): a query against a queue holding no read ranges walked the queue
  • the retire decrement removed: a query after the item holding the range retired still walked the queue
  • the ReleaseRunningReads decrement removed: a query after the running item released its reads still walked the queue
  • the query count moved behind the prefilter (under the lock, where it was): a query against a queue holding no read ranges was not counted

@babyhuey, thank you for the Linux run.

Overlap bot: #9898 and #10677 touch neighbouring lines of DrawPipeline.cpp (the counters in Enqueue, the retire in run); neither is a dependency, and whichever lands second rebases.

A correction to the description's measurements, which I am redoing: the four pairs compared a base that carried the port of #6192 (main 6cd36e6 + #8098 + the port) against a change build without it (main 6cd36e6 + #9490 + this commit), so they were not the like-for-like pair the text claims. The "read ranges walked" row also quoted the [draw] pipelined line's cumulative count of enqueued read ranges, which this change does not move. I am re-measuring on current main: base main 9f386f3 + #8098, change the same plus #9490 and this branch, four alternated pairs, and one profile of the change with and without APS5_NO_HELD_READ_PREFILTER=1 to show queries against walks per 10 s. The description gets the new table and loses the wrong row and sentence.

AI-assisted: yes

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@johandavidcm the shared ownership, cleanup guard and query/walk checks address my source asks. I still need the performance comparison corrected before carrying its result. Your proposed new baseline is main plus #8098, while the candidate also adds #9490; main 9f386f3 still does the per-call getenv that #9490 removes. Please include the same #9490 change in both arms, or compare this head with and without APS5_NO_HELD_READ_PREFILTER=1, so the game pair isolates the prefilter. Keep the other code and settings matched, replace the withdrawn table in the description, and distinguish queries, queue walks and the cumulative enqueued-range count. The 2.2% figure from the old pair cannot carry.

@johandavidcm

Copy link
Copy Markdown
Contributor Author

@oneandonlydean, re-measured as you asked, with this head on both arms: one build (main 9f386f3 + #8098 for the title's audio + this branch, so #9490 is in both), the base arm with APS5_NO_HELD_READ_PREFILTER=1, four alternated pairs and one profile per arm, same scene and settings as before (PPSA31327, Kamurocho, APS5_DRAW_PIPELINE=1 APS5_DRAW_INPUTS_IN_PLACE=1, APS5_TRACE_SECONDS=140:60, RTX 4070, driver 621.01).

The prefilter no longer does anything measurable in this title, because #8911 already removed the calls it targeted. The [draw] pipelined counters per 10 s of gameplay, last six reports of each profile:

per 10 s prefilter off prefilter on
held-read queries 661-675 633-661
of which walked the queue under the lock all of them 372-391
held reads found (stores held) 0 0
committer time per commit 22.3-22.5 µs 22.3-22.6 µs
read ranges enqueued since start (cumulative, not touched by this PR) 54.5 M to 80.5 M over the window 54.5 M to 80.4 M

On main 6cd36e6, before #8911, the same scene found about 370 thousand held reads per 10 s. #8911 returns from FlushPending before HeldReads when nothing is stored, which here is nearly every call; what the prefilter still skips is about 280 lock acquisitions per 10 s.

Frame pairs, p50 and presents in 60 s, off then on: 197.6 / 195.8 ms (303 / 305), 212.9 / 207.6 (280 / 288), 209.1 / 201.8 (285 / 296), 209.8 / 200.5 (284 / 298). All four favour the prefilter by 0.9-4.4 %, but performance_compare puts the gap in every phase alike and in GPU busy per present (up to 4 ms per frame), which skipping 280 mutex acquisitions on the committer cannot cause. I read it as run-to-run variance and do not claim it.

So the cost that motivated this PR is gone on main, and I don't see a reason to keep it. Unless a reviewer wants the query and walk counters on their own, I'll close it. Thank you for the reviews; the parked-commit cleanup carries over to the next test that parks a commit.

AI-assisted: yes

@johandavidcm

Copy link
Copy Markdown
Contributor Author

Closing: #8911 removed the calls this prefilter was for, and the same-binary pairs above show no measurable change left in the title. Thank you for the reviews; the parked-commit cleanup carries over to the next test that parks a commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants