perf(agc): destroy deferred MoltenVK objects when the queue is already idle #11587
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11587"
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 imellag on 2026-10-26T21:48:17Z.
What
On MoltenVK, #10062 destroys released buffers and device memory only while the graphics queue is idle (KhronosGroup/MoltenVK#2876). Before every recorder or command batch submission it drains the queue once 512 objects wait or the oldest has waited 100 ms. A title that releases buffers all the time reaches the 100 ms bound about ten times a second, and each
vkQueueWaitIdlestops the queue worker until the GPU has finished everything queued. That cost was not measured in #10062.With this change the recorder also passes whether every batch it has submitted has completed: its timeline semaphore against its submission count, read with
vkGetSemaphoreCounterValueKHR, without waiting. When they have, and the oldest deferred object has waited 30 ms, the drain runs at once. It still callsvkQueueWaitIdle, so the guarantee #2876 needs is unchanged, but the queue is already idle. A title whose GPU finishes a frame before the next one starts now drains about once a frame, between frames.CommandBatchsubmissions.DeferredObjectslookup per submission.waitIdle(an empty command buffer committed and waited for), and Planet of Lana lost about 2% against no deferral. With 30 ms it is about 1%.agc_deferred_destructioncovers three new cases:Tested
Current
main(2026-10-27), clean commits. Base9f6232f229ebismain3fea2a937bplus the 8 macOS PRs listed on #1 (fork branchbench/main-boot-1027); the change87853364575cis the base plus this commit (bench/dd-pr).mainnow has #11142, which removed most buffer creations, so the drains this PR avoids are rare there: the base committer spends 0.1 ms per frame inRecorder.Submit, not 3.5 ms.trace_digest, 95:10: base 302 presents, p50 32.9 / p90 34.9 / p99 44.5 / max 53.0 ms; this PR 306 presents, p50 32.6 / p90 34.6 / p99 37.7 / max 42.5 ms.agc_deferred_destructionandagc_buffer_poolpass 3 of 3.The runs below are from the earlier base (
main035ac96eddwith the macOS PRs), before #11142.macOS 27.0.1, Apple M4 Pro, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), x86-64 Release under Rosetta 2.
maindoes not build and run on macOS, so this ran onmain035ac96edd+ #10366 #10552 #7631 #7804 #11249 #10669 #10540 #11447 #11047 #11048 #10876 #9263 and a local patch that loadslibvulkan.1.dylib, without and with this commit. Every run had mimalloc injected withDYLD_INSERT_LIBRARIES(as #11130 does on request), and the same shader cache.Planet of Lana (PPSA19450), New Game into the village, 120 s runs, alternated, fps from 85 s:
main(#10062 as merged)APS5_NO_DEFERRED_DESTRUCTION=1tools/trace_digest.pyon theAPS5_TRACE_SUMMARY_EVERY=10summaries, gameplay intervals (attached):main: 70-110 s, 277-284 presents per 10 s, p50 33.7-34.7, p90 44.8-45.9, p99 49.2-54.5 ms, largest phase Driver.Packet 23.9-24.4 ms per frameSummary lines of the attached traces (
APS5_TRACE_SECONDS=95:10 APS5_TRACE_EVENTS=1048576, window 95-105 s; the change build is marked dirty because the 30 ms constant was edited before the commit, its code equals this head apart from the debt text and the test):main:This PR:
Stray (PPSA02100), the title #10062 was written for, 180 s runs pressing cross into the game,
MTL_DEBUG_LAYER=1, a MoltenVK without residency sets (where #2876 still crashes without the deferral):APS5_NO_DEFERRED_DESTRUCTION=1: segmentation fault after about 30 s (status 139), so #2876 still reproduces on this setup.main: 3 of 3 runs reach 180 s, no device loss, no Metal assertion.WAIT_REG_MEM(the AGC wait diagnostic printed). The same diagnostic appears in 5 of 12 Stray runs on 2026-10-24 without this change, so I don't attribute it to this PR.Tests:
agc_deferred_destructionandagc_buffer_poolpass withANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation: noVUID-line, no "was not found but was requested by env var VK_INSTANCE_LAYERS".agc_deferred_destructionuses a mock device.tools/check_conventions.py --base upstream/mainpasses.tools/pr_overlap.pyfinds no conflicting lines. It could not compare the open pull requests that conflict withmain; of those, #5609 also editsRecorder.cpp.Not tested: Linux and Windows (the path only runs on MoltenVK), GCC.
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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T22:08:11Z.
Review: APPROVED
Reviewed head
8738e58545556d451c19edf05d62167d93dcb081. The 30 ms path is gated on the recorder timeline having completed all submitted batches; the existing 512-object and 100 ms drains remain the fallback when that condition is false. The added mock cases cover young idle, older busy, and older idle queues.I downloaded both attached event traces and reran
tools/trace_digest.pyagainst the corresponding pasted summary lines. It matched the reported windows and no-drop flags: 287 presents on the baseline and 294 on the change, with p90/p99 of 44.751/48.517 ms and 35.790/39.741 ms respectively. The baseline’s macOS compatibility stack is disclosed in the PR description; both captures use the same title, scene, machine and MoltenVK setup. The report gives useful frame-tail improvement, while the no-deferral comparison indicates some remaining overhead.Conventions, labels, eligibility, intake and report checks pass. Product, relinker and write-watch jobs are path-skipped. The author reports focused Vulkan validation tests and native title runs on the stated machine. No issue found in the reviewed code or evidence.
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:49:23Z.
Review: COMMENTED
Two things I would want settled before this goes in.
1. The numbers on current main do not show a gain. On main after #11142 the PR's own figures are fps 30.31 / 30.63 (base) against 30.48 / 30.69 (change), which is inside the run-to-run spread of the base itself (0.3 fps). The p99 improvement (44.5 to 37.7 ms) is one
trace_digestcapture per arm, so there is no repeat to tell it from noise. The description also says the base committer now spends 0.1 ms per frame inRecorder.Submit. The larger wins come from the earlier base, which predates #11142 and no longer exists. Please repeat thetrace_digestpair at least twice per arm on the current base, or say plainly that this is a no-op there.2. Cost and tuning.
Recorder.cpp: every submission with a non-empty deferred list now callsvkGetSemaphoreCounterValueKHR. That is cheap on MoltenVK, but it is new work on a path that runs per batch.IdleDrainAge = 30 mswas picked from one title ("with 8 ms Planet of Lana lost about 2%, with 30 ms about 1%"). That is a threshold tuned to one title, and the change still loses about 1% against no deferral there. Why is that acceptable, and what is the reasoning for 30 rather than a value derived from the frame time?vkQueueWaitIdleruns. If another thread can submit in between, the "already idle" drain can still block on new work. Is submission to this queue serialised againstSubmit's drain? If so, a comment in the PR description would help; if not, the guarantee in the description is weaker than stated.The new test cases cover the age/idle matrix well, but none of them can fail on a wrong
queueIdlecomputation inRecorder.cpp, since the test passes the flag in by hand.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T09:53:23Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the review supports merging, with the optional note below.
What I ran
Reviewed head
e0aecc7bb67dcdf5b027e04e750c0d242103aea0usingrepo_read.pyto inspect the prepared description, commit list, manifest, diff, and targeted source context. No commands or tests were run.Results
Findings
Blocking
None.
Non-blocking
queueIdlefalse, and a successful query must reach the prior submission count before the idle-age path can drain. Consider a focused test for this decision path, including a failed query and a counter below the submitted serial.Not verified
manifest.json; no changed files were excluded there.files.jsonwas unavailable through the reader, so any exclusions beyond the manifest are not verified.To get this to a good state
Nothing is required. The optional test coverage point is listed above.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.