Skip to content

fix(agc): reuse the GPU timing query pools instead of destroying one per batch - #25421

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Gonic0:fix/agc-recycle-gpu-timing-query-pools
Nov 27, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Gonic0:fix/agc-recycle-gpu-timing-query-pools

Conversation

@Gonic0

@Gonic0 Gonic0 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

What

Under APS5_PROFILE_GPU, Recorder::release destroyed each batch's timestamp query pool when the batch completed, and beginTiming created a new one for the next batch; only the two-query pools of batch stamps went back to sparePools. GpuTimingEnabled() is a process-wide constant, so every spare pool has the same size in either mode: the full-size pools (MaxTimedRanges * 2 queries) now go back to the spare list too, capped at 64 as before.

This also avoids a crash on MoltenVK 1.4.2 (the Vulkan SDK 1.4.363.0 build). MoltenVK reads a query pool from a Metal command buffer completion handler without retaining it (MVKCommandEncoder::finishQueries), and that handler can run after the batch's fence signals, so destroying the pool then, which Vulkan allows, made MoltenVK read freed memory. agc_driver_profiled_dispatch_run died with SIGSEGV on a non-canonical address in that handler, on a Metal completion thread. MoltenVK fixed this on its main branch in KhronosGroup/MoltenVK#2839, which is not in a release yet.

It is a workaround for the timing pools only: the occlusion sample pools a batch owns (batch.samples, endedSamples) are still destroyed when it completes, and so is a timing pool when the spare list is full. The fragment adds a debt entry for that.

Tested

macOS 27.3 (Apple M5 Pro), x86_64 Release under Rosetta 2, main 0538bc2 with #17425 and #11049 (needed to build on macOS), ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1.

agc_driver_profiled_dispatch_run_tests run directly, MoltenVK:

Tree Exit codes
main 11 of 15 runs SIGSEGV (139), 4 pass
this PR 30 of 30 runs pass (0)

With the validation layer loaded, main passes 10 of 10: the layer changes the timing enough to hide the race, so the crash runs above are without it. A temporary SIGSEGV handler (not in this PR) showed the faulting frame as MVKCommandEncoder::finishQueries block, called from -[_MTLCommandBuffer didCompleteWithStartTime:endTime:error:]. On KosmicKrisp (Mesa, also on Metal) the test passes on main.

84 tests that exercise GPU timing, batch stamps or the recorder (ctest -R 'agc_gpu_timing|profiled_dispatch|target_memo|draw_cache_reuse|image_table_reuse|address_draw_space_reuse|agc_recorder|agc_driver_recorder|prepared_shaders|host_report' -j4 -V), with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, main against this PR:

main this PR
MoltenVK 83 of 84 pass; agc_driver_recorder fails same
KosmicKrisp 80 of 84 pass; agc_driver_recorder, agc_driver_prepared_shaders_abi_deferred_rectangle_pairs, agc_gpu_timing_complete, agc_gpu_timing_incomplete fail same
VUID- (MoltenVK) 24 VkDeviceCreateInfo-sType-unique, 18 vkCreateDescriptorSetLayout-support-09582 same
VUID- (KosmicKrisp) none none

Every log shows the loader inserting the layer (110 times) and no was not found but was requested by env var VK_INSTANCE_LAYERS. The sType-unique errors come from the recorder tests' own device (#21965).

readGpuTiming reads a recycled pool the same way as before (no VK_QUERY_RESULT_WAIT_BIT, anything other than VK_SUCCESS dropped), as the two-query pools already were. 64 spare full-size pools hold 64 × 1024 timestamps. No speed claim. Not run on Linux or Windows; there the change only affects APS5_PROFILE_GPU runs, which now reuse pools instead of creating one per batch.

tools/check_conventions.py --base upstream/main passes. tools/debt_fold.py --check passes for this fragment (run with only this fragment; two fragments already on main fail it on their own). tools/pr_overlap.py lists open PRs that touch Recorder.cpp; none of their diffs touches sparePools, beginTiming's pool choice or release.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Apple M5 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0) and KosmicKrisp, macOS 27.3
  • AI-assisted: yes

🤖 Generated with Claude Code

…per batch

Under APS5_PROFILE_GPU the recorder destroyed each batch's timestamp query pool when the batch completed and created a new one for the next batch; only the two-query pools of batch stamps went back to the spare list. GpuTimingEnabled() is fixed for the process, so every spare pool has the same size in either mode, and the full-size pools now go back to the spare list too.

On MoltenVK 1.4.2, destroying the pool raced the Metal completion handler that still reads it, and agc_driver_profiled_dispatch_run crashed in MVKCommandEncoder::finishQueries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added docs agc libSceAgc* labels Nov 22, 2026
@1tsethan

Copy link
Copy Markdown
Contributor

Independent Windows compatibility check for 7888d2ce7f on Windows 11 build 26200, UCRT64 GCC 16.2.0, Ninja Release. I applied the two changes in Recorder.cpp to the configured 732c68c9fd checkout and rebuilt the existing GPU timing and dispatch test targets. Vulkan was required, the shader cache was disabled, and the Khronos validation layer was off.

CTest RTX 3070 Ti AMD Radeon Graphics (Ryzen 7 7700X)
agc_driver_profiled_dispatch_run 5/5 5/5
agc_gpu_timing_key 5/5 5/5
agc_gpu_timing_complete 5/5 5/5
agc_gpu_timing_incomplete 5/5 5/5

The dispatch and batch-completion logs identified the requested adapter. These are patched Windows-driver runs.

Tested with Codex assistance.

@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 — looks good

Nothing in the inspected changes blocks merging; the two changed code paths consistently recycle timing query pools, and the spare list remains capped at 64.

What I ran

Reviewed head 7888d2ce7f6f734c5d3a3104446943af877a2b02 using repo_read.py to inspect the prepared diff and relevant source ranges. I did not run tests or build commands.

Results

Check Result Note
Diff and changed files passed Both changed files read
Runtime checks not run Author-reported results only

Findings

Blocking

None.

Non-blocking

None.

Not verified

Runtime behavior, tests, builds, other operating systems, and physical-console behavior were not verified. The reported test results in the PR description were not run during this review. review/files.json was not available through the reader, so no excluded-file list was available; the manifest lists both changed files as included.

To get this to a good state

Nothing is required.

@Doorriiaann

Copy link
Copy Markdown
Contributor

NVIDIA run on Linux: no regression in the GPU timing, profiled dispatch, recorder and reuse tests from the description.

Setup: RTX 3070 8 GB, NVIDIA 610.57.04, Ryzen 5 5600X, Debian 13 (kernel 6.12.115), GCC 13.3.0 Release, validation layer 1.4.335. Base is the merge-base 3b6291c, head is 7888d2c. ANYPS5_GPU=NVIDIA ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer ctest -R '(agc_gpu_timing|profiled_dispatch|target_memo|draw_cache_reuse|image_table_reuse|address_draw_space_reuse|agc_recorder|agc_driver_recorder|prepared_shaders|host_report)' -V, 3 runs each.

base this PR
all 84 matching tests 252/252 pass 252/252 pass

Every Vulkan test's log selects the RTX 3070 and shows the layer inserted, none reports it missing, and no log has a VUID- line.

AI-assisted: yes

@Helldiverr

Copy link
Copy Markdown
Contributor

Head 7888d2ce7f merged onto upstream/main f5b7e1d1ac: no defect found. The focused tests pass 3/3 on RDNA4 (gfx1201) with the Khronos validation layer loaded and no VUID-. Under APS5_PROFILE_GPU=1, the full-size pools are reused hundreds of times per test and the layer reports nothing.

Code.

  • beginTiming (Graphics/src/Recorder.cpp:2731) can now take any spare pool. Every spare pool has the same size, because full is GpuTimingEnabled(), a process-wide static (:2589).
  • A reused pool is reset with vkCmdResetQueryPool(commands, open->queries, 0, queryCount) (:2743) in the new batch's command buffer before its first timestamp. So a pool from the spare list is never written without a reset.
  • release (:5454) runs in three places: after the batch's fence wait and readGpuTiming (:5347), on the begin-failure path before submission (:2251), and on the wait-failure path (:5343). On the wait-failure path, destroying the pool would be just as unsafe as reusing it, so this is not a regression.
  • No code comments. The debt entry is added through a fragment, and it states the remaining gap (occlusion sample pools are still destroyed).

Existing runs. Doorriiaann ran 84 tests on NVIDIA Linux with the layer, and 1tsethan ran 4 tests on an RTX 3070 Ti and a Ryzen iGPU on Windows without the layer. This run adds three things:

  • discrete RDNA4 on the AMD Windows driver, with the layer loaded;
  • the whole recorder suite with GPU timing turned on;
  • a count showing that the full-size pools really are reused.

Of the 84 tests in the PR's list, only agc_gpu_timing_complete, agc_gpu_timing_incomplete and agc_driver_profiled_dispatch_run set APS5_PROFILE_GPU. In the rest, the changed lines run only with 2-query pools, which behave the same as before.

Probe 1: the suite with GPU timing on. I set APS5_PROFILE_GPU=1 for all 165 tests of agc_driver_recorder_tests, plus agc_driver_profiled_dispatch_run, the 4 agc_driver_prepared_graphics* tests and agc_driver_vertex_fetch. That is 171 tests, with the layer loaded.

tree result VUID-
main 170/171; agc_driver_recorder failed 0
head 171/171 0

That agc_driver_recorder failure is intermittent under APS5_PROFILE_GPU=1 and is not caused by this PR. The failing case is 64 KiB R_X 512x512, 3 mips, no DCC, an access to the first unit: an earlier case's image is still in the live registry over the reused memory. It failed the same way in 5 of 26 runs with main's Recorder.cpp and in 2 of 7 runs with this head. Without APS5_PROFILE_GPU, it passed in the 1 main run and the 3 head runs below. 21 of the 26 main runs used main's Recorder.cpp built in the #25852 tree, where #25852 does nothing without its environment variable.

Probe 2: pool reuse count. I added a local, uncommitted counter to the head's beginTiming. It prints at each power of two, so these figures are lower bounds. I ran it with APS5_PROFILE_GPU=1 and the layer loaded:

test pools created spare pools reused
agc_driver_recorder at least 8 at least 512
agc_driver_prepared_graphics at least 2 at least 64
agc_driver_profiled_dispatch_run at least 1 at least 32
agc_driver_vertex_fetch at least 1 at least 32
agc_gpu_timing_complete, _incomplete at least 1 at least 2

Every reused pool was a full-size pool (full=1), and all 6 tests passed with 0 VUID-. This R9700 cannot reproduce the MoltenVK use-after-free itself.

Negative control. None. The PR adds no test. On main, the crash only shows on MoltenVK.

GPU.

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_gpu_timing_key|agc_gpu_timing_complete|agc_gpu_timing_incomplete|agc_driver_profiled_dispatch_run|agc_recorder_command_buffer_release|agc_driver_recorder)$" -V
run profiled_dispatch_run gpu_timing_key (CPU) gpu_timing_complete gpu_timing_incomplete driver_recorder command_buffer_release
main Passed 1.13 s Passed 0.03 s Passed 0.11 s Passed 0.11 s Passed 2.69 s Passed 0.12 s
head 1 Passed 0.42 s Passed 0.03 s Passed 0.11 s Passed 0.11 s Passed 1.98 s Passed 0.14 s
head 2 Passed 0.45 s Passed 0.03 s Passed 0.10 s Passed 0.11 s Passed 2.04 s Passed 0.12 s
head 3 Passed 0.44 s Passed 0.03 s Passed 0.10 s Passed 0.10 s Passed 2.03 s Passed 0.12 s

agc_gpu_timing_key creates no Vulkan instance. The other five select the R9700. No test skipped.

Validation.

  • Each focused run has 6 Insert instance layer "VK_LAYER_KHRONOS_validation" lines. Each 171-test probe run has 159, and the reuse-count run has 7.
  • was not found but was requested by env var VK_INSTANCE_LAYERS appears in none of them.
  • VUID- lines: 0 in every log, on main, head and probes.

Host:

  • GPU: AMD Radeon AI PRO R9700 (gfx1201, 1002:7551), driver 32.0.31044.16, Vulkan 1.4.349
  • Validation layer: Khronos 1.4.363 (LunarG SDK)
  • CPU: Ryzen 9 9950X3D
  • OS: Windows 11 Pro 26300
  • Toolchain: WinLibs GCC 15.2, Release
  • Commits measured: main f5b7e1d1ac; head b36ddec815 (7888d2ce7f merged onto f5b7e1d1ac)

AI-assisted: yes

@github-actions

github-actions Bot commented Nov 23, 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 26613 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp L2779-2781, L5648

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 5609 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 6186 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 7642 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 9289 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 9356 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 9387 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 9508 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 9712 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 10193 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 10442 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 11444 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 13809 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 13863 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 13920 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 13928 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 13929 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 15136 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 15570 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 16879 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 17611 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 18173 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 18390 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 20172 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 20315 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 20519 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 22190 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 22424 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 22647 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 22787 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 25010 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
PR 27343 core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp

@Gonic0

Gonic0 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Additional evidence from a title, Apple M5 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), Grand Theft Auto V (PPSA04264):

  • Without this change, APS5_PROFILE_GPU=1 ends the title about one second after the swapchain is created (exit status 139, 2 of 2 runs). Under lldb the fault is in a Metal completion handler, on com.Metal.CompletionQueueDispatch, through a freed pointer:
    frame #0: 0x0000000200000000
    frame #1: libMoltenVK.dylib`invocation function for block in MVKCommandEncoder::finishQueries() + 50
    frame #2: Metal`MTLDispatchListApply + 34
    frame #3: Metal`-[_MTLCommandBuffer didCompleteWithStartTime:endTime:error:] + 660
    
  • With exactly this diff applied, 3 of 3 runs were still running after 20 s and each printed its [gputime] report. A longer run with the pools kept alive reached gameplay and printed 11 [gputime] reports over 125 s.

@oneandonlydean
oneandonlydean merged commit 3789527 into boykopovar:main Nov 27, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants