perf(agc): drop per-batch fences when a timeline tracks completion #20066
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/20066"
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 meh7an on 2026-11-14T14:43:06Z.
What
Recordergave every recorded batch aVkFenceof its own and also signalled its timeline semaphore (serial = submissions + 1) in the samevkQueueSubmit.Recorder::Reap, whichVulkanDevice::WriteLabelOnGpuandSubmitRecordedcall on the draw committer, then asked the drivervkGetFenceStatusonce per in-flight batch until one was not signalled. On the S3K menu page that is 1.0 to 1.8% of the committer's samples inRecorder::Reapon main, 31 to 49 of those samples innvoglv64(the driver's fence query).perf(agc): drop per-batch fences when a timeline tracks completionReapreads the counter once (when the newest in-flight serial is above the highest value already read) and retires batches by comparing serials; no per-batch fence is created, queried, reset or pooled.APS5_BATCH_FENCES=1restores the fence per batch and asks the fence whether a batch completed. A recorder without a timeline (extension absent,APS5_NO_TIMELINE, semaphore creation failed,Recorder(context)in tests) never takes the new path: its code is main's.Recorder::fenceless(set in the constructor when the timeline exists and the switch is absent),Recorder::readTimeline()(readsvkGetSemaphoreCounterValueKHR, keeps the maximum inreachedSerial, ignores a failed read),Recorder::gpuDone(batch)(fenced:vkGetFenceStatus == VK_SUCCESS; fenceless:serial != 0 && (serial <= reachedSerial || serial <= readTimeline())).signaled,unsignaled,DescribePendingWrite, the profile block ofsyncThroughUnlocked,finish'ssignaledAtStartandReapaskgpuDone;finish(wait)waits on the timeline (WaitTimeline, same 5 s slices and warnings as the fence wait) only when the serial is abovereachedSerialafter a read. No public API changes;ensureOpenmakes no fence when fenceless andSubmitpasses the batch's fence, null then.Recordersubmits tocontext.queueonly (the guest queues are tags on batches). Each submit signals the one timeline with the next serial, in submission order. The first synchronization scope of avkQueueSubmitsemaphore signal includes every earlier submission on that queue, exactly as a fence's does, so a counter value N means batches 1..N completed: the same prefix a fence per batch gives.WaitSerialtoFinishUpToandsyncThroughUnlockedalready rely on this on main. A batch is never counted done early:reachedSerialonly holds values the counter returned, and a stale low read only keeps a batch in flight, as an unsignalled fence does.Reap,Sync,FinishUpToandSyncThroughpopinFlightfrom the front, so completion order is serial order in both modes.submittedandserialare set andDiscardUnsubmitteddiscards the batch;submissionsis not consumed, so the next batch signals the same value (as on main). There is no fence to reset.reachedSerialalone, soReapretires nothing (a lostvkGetFenceStatusis notVK_SUCCESSeither). A failed timeline wait takesfinish's existing path (vkDeviceWaitIdleunlessVK_ERROR_DEVICE_LOST,release, throw).~RecorderrunsSync(), which finishes every batch throughWaitTimelinewhile the semaphore lives, and the waiters ofunlockedWaitersare waited for beforevkDestroySemaphore: unchanged. There are no fence objects to destroy.inFlightruns under the GPU mutex, so the non-atomicreachedSerialis as safe asinFlight;syncThroughUnlockedcopies the function pointer before it releases the mutex and touchesthisonly after the relock.fenceless = false(or give lane batches fences) while its lane is active. Declared under Overlaps.docs/dev/ENVIRONMENT.mdhas a row forAPS5_BATCH_FENCES(tools/env_reference.py --check, 617 names).Not changed: the batch cap and the submit points (
APS5_BATCH_CAP), what a batch waits for, theProgressWatcher, the timeline values, and the workfinishdoes after a batch completes (release,publishPendingWrites,NoteFinished: that is what is left underReapbelow).Which thread limits the scene. On the S3K page the draw committer is the busiest CPU thread (70 to 78% of its samples outside a wait in the sampler windows below, 41 to 42% in retained trace slices; the queue worker 22%) and the GPU is 57% busy, so neither saturates and the frame is paced by the committer-to-GPU chain. A committer saving can show in the frame but does not have to; it showed as about +1%.
Scope of the measurement. One title (Sonic Origins S3K page), one GPU (RTX 2080 Ti, NVIDIA 617.14) on Windows. The saving is the driver's fence queries and the per-batch fence creation and reset; other drivers and platforms are not measured, and the change is not claimed to be faster there. It never makes more driver calls than the fence path (a reap reads the counter at most once where it polled one fence per in-flight batch), and
APS5_BATCH_FENCES=1is the old behaviour.Tested
Windows 11 (10.0.26300), NVIDIA GeForce RTX 2080 Ti (driver 617.14), Sonic Origins (PPSA05324) menu on the S3K page, 3840x2160, default environment. Measured commits: main
f0c9b11f91cand this PR4a2c126ca7a(one commit on top of it), clean builds of each. The code was written on main44270d9910a(commit1b044694cad); after the validation and mutation runs below main moved anddocs/dev/ENVIRONMENT.mdconflicted (theAPS5_BATCH_CAProw next to the new row), so the branch was rebased ontof0c9b11f91c;Recorder.cpp,Recorder.hppand the test merged without changes. The measurements below and the new test were run on the rebased commits; the validation protocol and the mutants on44270d9910aand1b044694cad.New test
agc_recorder_batch_fences(tests/RecorderBatchFences.cpp, a mock Vulkan device with an in-order controllable queue, held submissions, counters for fence creations, fence queries and waits, timeline reads and waits, and injectable failures; it runs on every platform the agc tests build on):APS5_BATCH_FENCESset) and no timeline. Every scenario runs on each, with the data and the counts checked in each run: fenceless batches are submitted with no fence and none is created; fenced and no-timeline batches have one each.WaitSerialwithFinishUpToand the unlockedSyncThrough(one semaphore wait for the whole prefix); aSyncThroughunder the GPU lock; a failed submit; failed waits (VK_ERROR_DEVICE_LOST,VK_ERROR_OUT_OF_HOST_MEMORY); a failed counter read; failed timeline creation; and a 400-step random walk (submits, reaps, waits, completions) of a fenceless recorder compared step by step with a fence-only recorder on the same sequence; every case ends with no leaked fence or batch.Recorder.cpp, each built and run: 14 caught (an off-by-one ingpuDone,readTimelinenever storing, a reap without its pre-read, the switch ignored, a fence made while fenceless,>=inReap,signaledalways true,finishalways or never reading the timeline,DescribePendingWritealways signalled, a failed read taken as a value, fenceless without a timeline (two variants, found by a hang timeout), afinishwaiting on a null fence (found by the crash)). 2 survive and are equivalent: a failed wait marking its already-popped batch as reached, and theserial != 0guard ofgpuDone(an in-flight batch never has serial 0).Vulkan validation protocol (
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ctest -V, same machine and GPU, one build tree, each arm rebuilt):agc_*tests that construct aRecorderand submit, wait or reap through it (label ordering, completion and write-back, submit progress and shutdown, kept bytes, batch cap and command batches, queue and pipelined waits,agc_recorder_*) andvideo_out_flip_labels*; main lacks the new test.video_out_flip_labels*), 78 of 78 accounted for. Main: 75 pass, the same 2 skips. This PR withAPS5_BATCH_FENCES=1: the same 78 of 78.was not found but was requested by env var VK_INSTANCE_LAYERSor aVUID-line.tools/check_conventions.py --base upstream/mainpasses. Full ctest was not run.Sampler (
APS5_SAMPLE_COMMITTER, four overlapping 60 s windows of 3,843 committer samples each, snapshots 0-3, 1-4, 2-5 and 3-6; one sample is about 0.0145 ms of a frame; a warm boot of the new main came first). Both arms in the same order on the same page; the same window number is the same time since the page opened:f0c9b11f91c(windows 0-3 / 1-4 / 2-5 / 3-6)4a2c126ca7aRecorder::Reapinclusivenvoglv64(the driver)VulkanDevice::WriteLabelOnGpuinclusiveVulkanDevice::SubmitRecordedinclusiveRecorder::Reapin every window, 29.5 on average (0.77% of the committer, about 0.4 ms of a 55 ms frame). The driver part ofReapfalls from 31-49 samples to 0-5; what is left underReapisfinish(release,publishPendingWrites,NoteFinished) and the single counter read (readTimeline, 0 to 5 samples).Reapshrinks over the run in both arms (the scene's batches per reap change); the comparison is window against window.Two traced runs per arm (
APS5_TRACE_SECONDS=200:15), in main, PR, PR, main order, both commit IDs in the summaries:f0c9b11f91c4a2c126ca7aGraphics.Drawper frame (retained events)Recorder.Submitper frameGraphics.Draw0.6 to 1.4 ms lower per frame andRecorder.Submit0.1 to 0.2 ms lower, p99 within the spread, GPU busy unchanged to slightly lower. The two main runs differ by 0.08 FPS and the two PR runs by 0.01; every PR run is faster than both main runs on FPS and p50. The sampler window's FPS (17.89 to 18.04, +0.8%) points the same way. The gain is small, as the sampler's 0.4 ms predicts, and the trace's committer difference (1.2 ms on average) is larger than the sampler's, so I read the trace figure as upper bound.-PCbisectkept the executable staged earlier from an older main's relinker; both arms ran it, with each arm's own.prxmodules swapped in. Traces:m321_traces_main_f0c9b11f91c.zipandm321_traces_pr_4a2c126ca7a.zip(two runs each, trace andaps5-summary.jsonl).Digest of main `
f0c9b11f91`, run 1seconds 200:15 from present 5121: 254 presents, frame p50 58.6 / p90 60.7 / p99 66.0 / max 70.6 ms, GPU busy 33.0 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
f0c9b11f91, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 33.0, Graphics.Draw 23.1, Graphics.Draw.draw_and_resource_release 14.3, Driver.Packet 13.0, VideoOut.Flip 11.6, Driver.Present 11.4, VideoOut.Flip.present 11.4, Recorder.WaitSerial 11.3, Driver.Present.render_fence_wait 10.6, Vulkan.Retire 10.6
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Digest of this PR `
4a2c126ca7`, run 1seconds 200:15 from present 5146: 258 presents, frame p50 57.7 / p90 61.2 / p99 66.1 / max 69.9 ms, GPU busy 32.9 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
4a2c126ca7, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 33.0, Graphics.Draw 21.9, Graphics.Draw.draw_and_resource_release 14.5, Driver.Packet 12.9, VideoOut.Flip 11.7, Driver.Present 11.5, VideoOut.Flip.present 11.5, Recorder.WaitSerial 11.4, Driver.Present.render_fence_wait 10.7, Vulkan.Retire 10.7
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Digest of this PR `
4a2c126ca7`, run 2seconds 200:15 from present 5139: 258 presents, frame p50 57.8 / p90 60.4 / p99 65.0 / max 67.0 ms, GPU busy 32.7 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
4a2c126ca7, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 32.7, Graphics.Draw 22.7, Graphics.Draw.draw_and_resource_release 13.4, Driver.Packet 12.8, VideoOut.Flip 11.6, Driver.Present 11.4, VideoOut.Flip.present 11.4, Recorder.WaitSerial 11.3, Driver.Present.render_fence_wait 10.5, Vulkan.Retire 10.5
The lists of longest waits and largest GPU batches are left out of this run to fit the description;
tools/trace_digest.pyprints them from the attached trace.Digest of main `
f0c9b11f91`, run 2seconds 200:15 from present 5096: 255 presents, frame p50 58.4 / p90 61.9 / p99 67.2 / max 69.8 ms, GPU busy 33.5 ms per present
NVIDIA GeForce RTX 2080 Ti, NVIDIA 617.14, Windows 10.0.26300, 3840x2160, commit
f0c9b11f91, dropped: yesIncomplete trace: recorded data was lost. Slice-based timings, waits and batch inventories cover retained events only; missing slices are not idle time. Summary statistics are reported separately.
Slowest frames (largest slices inside each, inclusive)
Persistent spikes (frame time above the fastest frame that separates it from a longer frame; one entry per stall, not per frame)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): batch 33.5, Graphics.Draw 23.3, Graphics.Draw.draw_and_resource_release 13.5, Driver.Packet 12.9, VideoOut.Flip 12.0, Driver.Present 11.9, VideoOut.Flip.present 11.9, Recorder.WaitSerial 11.8, Driver.Present.render_fence_wait 11.0, Vulkan.Retire 11.0
The lists of longest waits and largest GPU batches are left out of this run to fit the description;
tools/trace_digest.pyprints them from the attached trace.tools/pr_overlap.pyfails closed (exit 2): of the 1,534 open PRs, 132 share a file with this branch, and 84 of those conflict with main so it cannot compare them (30 of the 84 touchRecorder.cpporRecorder.hpp, the rest onlyENVIRONMENT.md). It does report three line conflicts: #17943 and #18029 (Recorder.cpplines 2160-2166 inensureOpen, where the batch's fence is made and their encoder thread allocates command buffers) and #17738 (theENVIRONMENT.mdrow next to ours). So I scanned the diffs of the open PRs that touchRecorder.cpp,Recorder.hpp,VulkanDevice.cpp,VulkanDevice.hpp,DeferredLabels.cpp,PacketMemory.cpp,PacketFlush.cpp,WaitMemory.cpp,Submission.cpp,QueueWorker.cpp,PacketExecution.cpporENVIRONMENT.md(193 of 1,533 open) for fence, timeline,Reap,WaitSerial,FinishUpTo,signaled,unsignaledand counter-read lines. No open PR drops batch fences, addsAPS5_BATCH_FENCESor changesReap's poll. Three diffs were too big forgh pr diff(#12315, #9508, #7642); they were read through the files API at the first scan on 2026-11-13, with no fence, timeline orReapline in theirRecorderorVulkanDevicepatches, and not re-read today.Overlaps to declare (this PR depends on none of them; whichever merges second rebases):
Recorderconstructor signature and theensureOpenlines above; #18173 (draft, conflicting) movesvkQueueSubmitinto a submitter with its own timeline values and has to keep one counter per queue (or setfencelessitself). None of them changes the fence poll,Reap,finishor the timeline reads.AsyncLaneActive()) add a second queue with its own timeline. One counter does not give prefix completion across two queues, so while the lane is active batches on it keep a fence (fenceless = false); that is theirs to set when they rebase.batch.fencedirectly behind a!= VK_NULL_HANDLEcheck, so with fenceless batches its idle-chunk test is false and it falls back to the slower path (correct, no gain) until it asksgpuDone.signaledandunsignaled, which now askgpuDonein both modes; #18829, #17850 and #19335 addReapandFinishUpTocalls and get the cheaper reap; #11587 (draft) reads the counter once for a MoltenVK idle check, independent of this change.ENVIRONMENT.mdrow next to the new one (textual only).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)