perf(agc): wait only for CPU-completion labels before direct consumers #9858

Merged
Raghav314 merged 1 commit from upstream-pr/9858 into main 2026-11-02 07:58:55 +00:00
Raghav314 commented 2026-10-22 19:11:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Raghav314 on 2026-10-22T19:11:42Z.


What

CompletePendingCpuWrites synced the recorder when any completion was pending (Recorder::PendingCompletionLabels() != 0, which AfterRecordedWork also counts) and PendingLabelIn found any pending label in the range. PendingLabelIn also matches labels the GPU stores in stream order. An address-based build calls it over the whole registered heaps (completeSpaceWrites), so nearly every such build found a label there and waited for the open batch under the GPU mutex.

It now uses CompletionLabelIn, the test VulkanDevice's other copied-writer checks make: a label whose store waits for a batch's CPU completion, or a queued one. A label the GPU stores needs no wait: a dispatch flushes queued label stores that overlap its reads, or all of them for a build that holds a lease (VulkanDevice::dispatch), and draws do the same (Draw.cpp), before they are recorded.

Found on ASTRO's PLAYROOM (PPSA01325): with APS5_PROFILE_DRAW=1, every GPU wait over 0.5 ms inside an indirect dispatch's build had the stack ShaderResources::Complete → GuestBufferMemory::UploadFinish → completeSpaceWrites → CompletePendingCpuWrites → Recorder::SyncThrough → Recorder::Sync. In 12,181 of 12,181 of those syncs no copied writer overlapped the range, so the label test made them. That was about 905 ms per 10 s of GPU-mutex hold on queue 0x0.

Tested

CachyOS (Linux 7.3.0), RTX 4090, NVIDIA 615.78.08.

  • agc_copied_direct_ordering gains a case: a label stored on the GPU (RecordStore and NoteLabel) with an unrelated completion pending (AfterRecordedWork). A fresh direct consumer must not submit, and it reads the label's value once the dispatch's queued-store flush has run. With PendingLabelIn restored it fails with "a fresh direct consumer waited for a label the GPU stores in order". The existing CPU completion label case still passes.
  • Full Linux test run (ctest -E bda_device -j4): every test passes except agc_driver_sample_order, agc_driver_sample_order_grid_reference and agc_driver_sample_order_recipe_depth_locations, which fail the same way on main on this machine.
  • Focused tests with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1, ANYPS5_REQUIRE_VULKAN=1, ctest -V, on main and on this branch: the 62 tests that run agc_driver_recorder_tests pass on both. Neither log reports the missing layer. Both logs have the same four VUID-VkImageViewCreateInfo-usage-02275 messages, from agc_sampled_storage_base_level_usage (image view usage); the change adds none.
  • ASTRO's PLAYROOM (PPSA01325), 3840x2160, player standing still in the same spot, caches warm, APS5_TRACE_SECONDS=120:15 and no other APS5_TRACE_* or APS5_PROFILE_* switch, log not redirected, main and this branch in the same session:
presents in 15 s frame p50 p90 p99 max
main a6fc8a467d 216 68.6 ms 74.5 ms 87.7 ms 92.0 ms
this branch b6b6e15718 229 65.6 ms 69.6 ms 74.1 ms 78.8 ms

Both traces have trace_dropped: queue worker 0x0's event buffer filled about 4 s into the window. totals_full is false for every thread, so the summary numbers are complete; only the event timeline is short.

main:

{"commit":"a6fc8a467d09","dirty":false,"env":{"APS5_PLAYGO_UNLISTED_CHUNKS":"installed","APS5_TRACE_SECONDS":"120:15"},"gpu":"NVIDIA GeForce RTX 4090","driver":"NVIDIA 615.78.08","os":"Linux 7.3.0-3-cachyos","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31742,"title_id":"PPSA01325","title":"ASTRO's PLAYROOM","content_version":"01.905.000","resolution":"3840x2160","window":{"spec":"seconds 120:15","first_present":2888,"ms":15047.571},"presents":216,"frame_ms":{"p50":68.637,"p90":74.477,"p99":87.744,"max":92.017},"gpu_busy_ms_per_present":29.669,"worker_busy_pct":{"queue worker 0x0":99.105,"queue worker 0x3d":99.566,"queue worker 0x52":0.031},"top_phases_ms_per_frame":{"Driver.Packet":138.336,"Driver.Packet.memory_wait":63.340,"Driver.Packet.draw":48.501,"Driver.Draw":48.114,"Graphics.Draw":42.451,"Graphics.Draw.draw_and_resource_release":36.858,"Driver.Draw.shader_compile_and_link":30.274,"Shader.DrawStage":22.732},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":4.217,"events_lost":676681,"totals_full":false},"draw committer":{"events_full_after_s":13.979,"events_lost":21150,"totals_full":false}},"trace":"aps5-trace-a6fc8a467d09-250704.json.gz"}

This branch:

{"commit":"b6b6e15718eb","dirty":false,"env":{"APS5_PLAYGO_UNLISTED_CHUNKS":"installed","APS5_TRACE_SECONDS":"120:15"},"gpu":"NVIDIA GeForce RTX 4090","driver":"NVIDIA 615.78.08","os":"Linux 7.3.0-3-cachyos","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31742,"title_id":"PPSA01325","title":"ASTRO's PLAYROOM","content_version":"01.905.000","resolution":"3840x2160","window":{"spec":"seconds 120:15","first_present":2906,"ms":15036.021},"presents":229,"frame_ms":{"p50":65.601,"p90":69.562,"p99":74.071,"max":78.838},"gpu_busy_ms_per_present":31.197,"worker_busy_pct":{"queue worker 0x0":99.018,"queue worker 0x3d":99.601,"queue worker 0x52":0.037},"top_phases_ms_per_frame":{"Driver.Packet":130.343,"Driver.Packet.memory_wait":59.671,"Driver.Packet.draw":47.348,"Driver.Draw":46.961,"Graphics.Draw":41.947,"Graphics.Draw.draw_and_resource_release":36.228,"Driver.Draw.shader_compile_and_link":30.378,"Shader.DrawStage":22.699},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":3.978,"events_lost":726935,"totals_full":false},"draw committer":{"events_full_after_s":13.308,"events_lost":35480,"totals_full":false}},"trace":"aps5-trace-b6b6e15718eb-252018.json.gz"}

Traces: aps5-trace-a6fc8a467d09-250704.json.gz (main) and aps5-trace-b6b6e15718eb-252018.json.gz (this branch), attached.

aps5-trace-a6fc8a467d09-250704.json.gz
aps5-trace-b6b6e15718eb-252018.json.gz

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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4090, NVIDIA 615.78.08, CachyOS (Linux 7.3.0)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4972750979 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9858). Originally posted by [Raghav314](https://github.com/Raghav314) on 2026-10-22T19:11:42Z. --- ### What `CompletePendingCpuWrites` synced the recorder when any completion was pending (`Recorder::PendingCompletionLabels() != 0`, which `AfterRecordedWork` also counts) and `PendingLabelIn` found any pending label in the range. `PendingLabelIn` also matches labels the GPU stores in stream order. An address-based build calls it over the whole registered heaps (`completeSpaceWrites`), so nearly every such build found a label there and waited for the open batch under the GPU mutex. It now uses `CompletionLabelIn`, the test `VulkanDevice`'s other copied-writer checks make: a label whose store waits for a batch's CPU completion, or a queued one. A label the GPU stores needs no wait: a dispatch flushes queued label stores that overlap its reads, or all of them for a build that holds a lease (`VulkanDevice::dispatch`), and draws do the same (`Draw.cpp`), before they are recorded. Found on ASTRO's PLAYROOM (PPSA01325): with `APS5_PROFILE_DRAW=1`, every GPU wait over 0.5 ms inside an indirect dispatch's build had the stack `ShaderResources::Complete` → `GuestBufferMemory::UploadFinish` → `completeSpaceWrites` → `CompletePendingCpuWrites` → `Recorder::SyncThrough` → `Recorder::Sync`. In 12,181 of 12,181 of those syncs no copied writer overlapped the range, so the label test made them. That was about 905 ms per 10 s of GPU-mutex hold on queue 0x0. ### Tested CachyOS (Linux 7.3.0), RTX 4090, NVIDIA 615.78.08. - `agc_copied_direct_ordering` gains a case: a label stored on the GPU (`RecordStore` and `NoteLabel`) with an unrelated completion pending (`AfterRecordedWork`). A fresh direct consumer must not submit, and it reads the label's value once the dispatch's queued-store flush has run. With `PendingLabelIn` restored it fails with "a fresh direct consumer waited for a label the GPU stores in order". The existing CPU completion label case still passes. - Full Linux test run (`ctest -E bda_device -j4`): every test passes except `agc_driver_sample_order`, `agc_driver_sample_order_grid_reference` and `agc_driver_sample_order_recipe_depth_locations`, which fail the same way on `main` on this machine. - Focused tests with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1`, `ANYPS5_REQUIRE_VULKAN=1`, `ctest -V`, on `main` and on this branch: the 62 tests that run `agc_driver_recorder_tests` pass on both. Neither log reports the missing layer. Both logs have the same four `VUID-VkImageViewCreateInfo-usage-02275` messages, from `agc_sampled_storage_base_level_usage` (image view usage); the change adds none. - ASTRO's PLAYROOM (PPSA01325), 3840x2160, player standing still in the same spot, caches warm, `APS5_TRACE_SECONDS=120:15` and no other `APS5_TRACE_*` or `APS5_PROFILE_*` switch, log not redirected, `main` and this branch in the same session: | | presents in 15 s | frame p50 | p90 | p99 | max | | --- | --- | --- | --- | --- | --- | | `main` a6fc8a467d09 | 216 | 68.6 ms | 74.5 ms | 87.7 ms | 92.0 ms | | this branch b6b6e15718eb | 229 | 65.6 ms | 69.6 ms | 74.1 ms | 78.8 ms | Both traces have `trace_dropped`: queue worker 0x0's event buffer filled about 4 s into the window. `totals_full` is false for every thread, so the summary numbers are complete; only the event timeline is short. `main`: ```json {"commit":"a6fc8a467d09","dirty":false,"env":{"APS5_PLAYGO_UNLISTED_CHUNKS":"installed","APS5_TRACE_SECONDS":"120:15"},"gpu":"NVIDIA GeForce RTX 4090","driver":"NVIDIA 615.78.08","os":"Linux 7.3.0-3-cachyos","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31742,"title_id":"PPSA01325","title":"ASTRO's PLAYROOM","content_version":"01.905.000","resolution":"3840x2160","window":{"spec":"seconds 120:15","first_present":2888,"ms":15047.571},"presents":216,"frame_ms":{"p50":68.637,"p90":74.477,"p99":87.744,"max":92.017},"gpu_busy_ms_per_present":29.669,"worker_busy_pct":{"queue worker 0x0":99.105,"queue worker 0x3d":99.566,"queue worker 0x52":0.031},"top_phases_ms_per_frame":{"Driver.Packet":138.336,"Driver.Packet.memory_wait":63.340,"Driver.Packet.draw":48.501,"Driver.Draw":48.114,"Graphics.Draw":42.451,"Graphics.Draw.draw_and_resource_release":36.858,"Driver.Draw.shader_compile_and_link":30.274,"Shader.DrawStage":22.732},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":4.217,"events_lost":676681,"totals_full":false},"draw committer":{"events_full_after_s":13.979,"events_lost":21150,"totals_full":false}},"trace":"aps5-trace-a6fc8a467d09-250704.json.gz"} ``` This branch: ```json {"commit":"b6b6e15718eb","dirty":false,"env":{"APS5_PLAYGO_UNLISTED_CHUNKS":"installed","APS5_TRACE_SECONDS":"120:15"},"gpu":"NVIDIA GeForce RTX 4090","driver":"NVIDIA 615.78.08","os":"Linux 7.3.0-3-cachyos","cpu":"AMD Ryzen 7 7800X3D 8-Core Processor","cpu_threads":16,"memory_mib":31742,"title_id":"PPSA01325","title":"ASTRO's PLAYROOM","content_version":"01.905.000","resolution":"3840x2160","window":{"spec":"seconds 120:15","first_present":2906,"ms":15036.021},"presents":229,"frame_ms":{"p50":65.601,"p90":69.562,"p99":74.071,"max":78.838},"gpu_busy_ms_per_present":31.197,"worker_busy_pct":{"queue worker 0x0":99.018,"queue worker 0x3d":99.601,"queue worker 0x52":0.037},"top_phases_ms_per_frame":{"Driver.Packet":130.343,"Driver.Packet.memory_wait":59.671,"Driver.Packet.draw":47.348,"Driver.Draw":46.961,"Graphics.Draw":41.947,"Graphics.Draw.draw_and_resource_release":36.228,"Driver.Draw.shader_compile_and_link":30.378,"Shader.DrawStage":22.699},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":3.978,"events_lost":726935,"totals_full":false},"draw committer":{"events_full_after_s":13.308,"events_lost":35480,"totals_full":false}},"trace":"aps5-trace-b6b6e15718eb-252018.json.gz"} ``` Traces: `aps5-trace-a6fc8a467d09-250704.json.gz` (`main`) and `aps5-trace-b6b6e15718eb-252018.json.gz` (this branch), attached. [aps5-trace-a6fc8a467d09-250704.json.gz](https://github.com/user-attachments/files/33839111/aps5-trace-a6fc8a467d09-250704.json.gz) [aps5-trace-b6b6e15718eb-252018.json.gz](https://github.com/user-attachments/files/33839115/aps5-trace-b6b6e15718eb-252018.json.gz) ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4090, NVIDIA 615.78.08, CachyOS (Linux 7.3.0) - [x] AI-assisted: yes
panicDa3m0n commented 2026-10-23 08:10:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by panicDa3m0n on 2026-10-23T08:10:28Z.

Review: COMMENTED


I read this at b6b6e15718eb, one commit on a6fc8a467d09, and checked the reasoning against the code at that head and on current main (21554447d).

The narrower test holds for the labels it drops.

  • Every completion label goes through AfterCompletions, which tables it per dword, with behindCompletion set whenever the completion will store it (Recorder.cpp:3448-3457). It has no wide-label path, so CompletionLabelIn sees all of them.
  • A label stored on the GPU and later overlapped by a write-back becomes behindCompletion in NoteWrittenBack (markBehindCompletion, Recorder.cpp:3498), so it is still waited for.
  • What the test no longer reports are labels the GPU stores in stream order: NoteLabel entries, tabled up to 64 bytes or kept in wideLabels above that. A queued store of one lands before the commands that read it:
    • before a dispatch, when it touches the dispatch's reads or writes, or always for a build that holds a lease (VulkanDevice.cpp:4793);
    • before a draw, in the same way (Draw.cpp:2683).
  • Queued labels of another queue still make it sync, as before.
  • It is also the test VulkanDevice already makes for copied writers (VulkanDevice.cpp:2292, 2378, 2662).

The image callers. CompletePendingCpuWrites is also called while image resources are built, before their uploads (ShaderResources.cpp 4711, 4796, 5014, 5074 and 5108 on 21554447d). A label the GPU orders over that memory still lands before the upload reads it:

  • A storage image upload flushes the overlapping queued stores before the detile reads them (Texture.cpp:2721 and 3186 on 21554447d).
  • A sampled texture's snapshot is read with ReadTextureSurface, through the flush hook (DccMetadata.cpp:769-772). FlushForAccess first records a queued label that overlaps the range (Recorder.cpp:1023-1028 on 21554447d). It then syncs through a pending write over the range (1035-1040), and NoteLabel notes a label's range as one.

#9445 makes the same change. #9445 (Zenodeon, opened 2026-10-21) changes the same condition in CompletePendingCpuWrites from PendingLabelIn to CompletionLabelIn. MarcZX10's review there asks only for a branch update and a fresh validation run. The two tests cover different sides:

  • #9445 checks the classification: a GPU label is pending but not a completion label;
  • this PR checks the direct consumer that no longer waits for a GPU-ordered label.

The maintainers may want to keep one of the two changes and both tests.

Against current main. CompletePendingCpuWrites and CompletionLabelIn are unchanged on 21554447d. main has one caller more, for the FMASK/CMASK range, which also completes CPU writes before the GPU reads, so the change applies to it as well.

CONTRIBUTING checks.

  • Two summary lines from different commits: main is the PR's parent a6fc8a467d09, with the same machine and scene. Both traces are attached.
  • Focused validation runs on main and on the branch with no new VUID-.
  • The new case fails with PendingLabelIn restored. The GPUs tested line is filled in.

Observations on the numbers, not blockers.

  • One run per side in a 15 s window: frame p50 is 3.0 ms lower and p99 13.7 ms lower.
  • gpu_busy_ms_per_present goes from 29.7 to 31.2 ms. That fits the GPU being fed more often once the build stops waiting.
  • Two things would show the mechanism directly, independent of frame noise:
    • the count of syncs from this site (CountSync(4)) on main and on the branch in the same session;
    • the 12,181 syncs of the profile, measured once with the change.
  • The change only decides whether the CPU waits and records no GPU commands. So a run on another vendor seems less important here than for a change to recorded work. That is my reading; the maintainers decide.

I have #9712 open, which also touches ShaderResources.cpp and tests/Recorder.cpp. The overlap bot finds no shared lines with this PR, and neither change depends on the other.

AI-assisted: I reviewed this with an AI assistant. The code references above were checked against the head and main named here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5625595885 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9858#pullrequestreview-5625595885). Originally posted by [panicDa3m0n](https://github.com/panicDa3m0n) on 2026-10-23T08:10:28Z. **Review:** COMMENTED --- I read this at `b6b6e15718eb`, one commit on `a6fc8a467d09`, and checked the reasoning against the code at that head and on current `main` (`21554447d`). **The narrower test holds for the labels it drops.** - Every completion label goes through `AfterCompletions`, which tables it per dword, with `behindCompletion` set whenever the completion will store it (`Recorder.cpp:3448-3457`). It has no wide-label path, so `CompletionLabelIn` sees all of them. - A label stored on the GPU and later overlapped by a write-back becomes `behindCompletion` in `NoteWrittenBack` (`markBehindCompletion`, `Recorder.cpp:3498`), so it is still waited for. - What the test no longer reports are labels the GPU stores in stream order: `NoteLabel` entries, tabled up to 64 bytes or kept in `wideLabels` above that. A queued store of one lands before the commands that read it: - before a dispatch, when it touches the dispatch's reads or writes, or always for a build that holds a lease (`VulkanDevice.cpp:4793`); - before a draw, in the same way (`Draw.cpp:2683`). - Queued labels of another queue still make it sync, as before. - It is also the test `VulkanDevice` already makes for copied writers (`VulkanDevice.cpp:2292`, `2378`, `2662`). **The image callers.** `CompletePendingCpuWrites` is also called while image resources are built, before their uploads (`ShaderResources.cpp` 4711, 4796, 5014, 5074 and 5108 on `21554447d`). A label the GPU orders over that memory still lands before the upload reads it: - A storage image upload flushes the overlapping queued stores before the detile reads them (`Texture.cpp:2721` and `3186` on `21554447d`). - A sampled texture's snapshot is read with `ReadTextureSurface`, through the flush hook (`DccMetadata.cpp:769-772`). `FlushForAccess` first records a queued label that overlaps the range (`Recorder.cpp:1023-1028` on `21554447d`). It then syncs through a pending write over the range (`1035-1040`), and `NoteLabel` notes a label's range as one. **#9445 makes the same change.** #9445 (Zenodeon, opened 2026-10-21) changes the same condition in `CompletePendingCpuWrites` from `PendingLabelIn` to `CompletionLabelIn`. MarcZX10's review there asks only for a branch update and a fresh validation run. The two tests cover different sides: - #9445 checks the classification: a GPU label is pending but not a completion label; - this PR checks the direct consumer that no longer waits for a GPU-ordered label. The maintainers may want to keep one of the two changes and both tests. **Against current `main`.** `CompletePendingCpuWrites` and `CompletionLabelIn` are unchanged on `21554447d`. `main` has one caller more, for the FMASK/CMASK range, which also completes CPU writes before the GPU reads, so the change applies to it as well. **CONTRIBUTING checks.** - Two summary lines from different commits: `main` is the PR's parent `a6fc8a467d09`, with the same machine and scene. Both traces are attached. - Focused validation runs on `main` and on the branch with no new `VUID-`. - The new case fails with `PendingLabelIn` restored. The `GPUs tested` line is filled in. **Observations on the numbers, not blockers.** - One run per side in a 15 s window: frame p50 is 3.0 ms lower and p99 13.7 ms lower. - `gpu_busy_ms_per_present` goes from 29.7 to 31.2 ms. That fits the GPU being fed more often once the build stops waiting. - Two things would show the mechanism directly, independent of frame noise: - the count of syncs from this site (`CountSync(4)`) on `main` and on the branch in the same session; - the 12,181 syncs of the profile, measured once with the change. - The change only decides whether the CPU waits and records no GPU commands. So a run on another vendor seems less important here than for a change to recorded work. That is my reading; the maintainers decide. I have #9712 open, which also touches `ShaderResources.cpp` and `tests/Recorder.cpp`. The overlap bot finds no shared lines with this PR, and neither change depends on the other. AI-assisted: I reviewed this with an AI assistant. The code references above were checked against the head and `main` named here.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9858
No description provided.