perf(agc): wait for the display keys' writer before a present takes the device lock's time #10160

Merged
EagleBearerYS merged 3 commits from upstream-pr/10160 into main 2026-10-26 05:26:32 +00:00
EagleBearerYS commented 2026-10-23 11:17:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-23T11:17:29Z.


What

The presenter checks a display buffer's DCC keys under the device lock.

  • What went wrong: when the batch with the kernel that writes those keys hadn't finished, the check waited for the GPU while holding the lock, and the queue worker recording the next frame waited behind the presenter.
  • The fix: PresentDisplayBuffer now first syncs through the key range with the unlocked wait the flush hook uses.
    • The presenter's acquisition is the outermost on its thread, and nothing has been decided yet, so the lock is released for the GPU wait and retaken for the completions.
    • The key check that follows finds the batch finished.
    • It's one line, for tiled display buffers with DCC keys (dccAddress != 0, tilingMode == 0).

Authorship: the fix is @fable-void's (fable-void/AnyPS5@9ed1466f35), carried unchanged under his authorship as 96bccd4c94. His message reports, in a title's menu on Windows: GPU waits under the lock went from 721 (1.38 s) to 1 (0 ms) per 10 s, and the queue worker's waits for the lock from 1.47 s to 0.18 s per 10 s. The counter and the test are ours.

The commits:

  1. 96bccd4c94 (fable-void): the fix.
  2. a3af6b1149: a profile-only counter on the [present] line behind APS5_PROFILE_DRAW: waits outside the GPU mutex for the display keys' writer: N, X ms. With the profile off it costs one test of a static const bool.
  3. 54196438fc: the test, agc_driver_present_key_writer_wait (tests/execution/PresentKeyWriterWait.cpp).
    • It calls the real VulkanDevice::PresentDisplayBuffer headless, holding the GPU mutex once, as Driver::Present does, over a display buffer whose keys a recorded GPU copy is still writing.
    • It requires three things: the present reached the swapchain (the key check saw the written keys); GuestMemory::GpuHoldSerial() advanced (the presenter released and retook the mutex for the writer); and the writer is no longer pending.
    • A control with no writer must leave the serial unchanged.
    • There's no test-only hook.

Tested

  • Resident Evil Village (PPSA01556), Windows, RTX 3070 Laptop GPU, clocks not held. Static menu, 5 minutes, APS5_PROFILE_DRAW=1, on our lane F tree (12aa911767), which carries this fix with fable-void's other shared-code fixes:
    [present] ... waits outside the GPU mutex for the display keys' writer: 5, 9.8 ms
    
    That's per 10 s: GPU waits that main would make with the device lock held (this PR's commit alone was not run in the title).
  • The test (agc_driver_present_key_writer_wait):
    • Red, against this PR with only the fix's line removed: "AGC graphics: the presenter synchronized with the display keys' writer without releasing the GPU mutex".
    • Green with it.
  • Linux, CI's configure flags (lavapipe, on a cloud worker):
    • the test plus the present/display-buffer set, --repeat until-fail:20: 17 tests ran 20 times each, and 17 video_out_* tests skip with no display;
    • CI's Linux job mirrored: 1,934 tests, of which 1,844 pass, 86 skip and 4 fail, the four being that machine's IPv6 tests.
  • GitHub's runners (run 39933804836, this head on main 44a59edc3a):
    • Linux: red with main's implementation (agc_driver_present_key_writer_wait: "the presenter synchronized with the display keys' writer without releasing the GPU mutex"): 2 pass, 1 fails. Green ×10 with this PR: all 3 run and pass (the new test, agc_display_buffer_dcc and agc_driver_dcc_key_presync), none skip.
    • Windows: builds. The runner has no Vulkan device, so on both trees 1 runs and passes (agc_display_buffer_dcc, ×10 with this PR) and the 2 device tests skip.
    • The libc and libkernel suites pass on both.
  • Validation layer (Khronos 1.3.275, lavapipe): main's driver and this PR over the 12 non-video_out tests give the same single VUID-VkDeviceCreateInfo-pNext-pNext in the same tests with the same counts. No new VUID-.
  • Resident Evil Village (PPSA01556), a menu pair, on an NVIDIA GeForce RTX 3070 Laptop GPU (driver 617.42), Windows 11. The static menu, 5 minutes per run, in the order with, without, without, with, from a warm shader cache with APS5_LAZY_SHADER_PREPARE=1, APS5_NO_GPL=1 and APS5_HOST_IMPORT_MIB=6400. GPU clocks weren't locked (P0 in 98.8–99.4 % of samples, mean 1,398–1,412 MHz).
    • The trees: without this PR, test/10160-village-base (d55142d279, main 137a8fab89 plus #8926's fix, which the title needs to get past its intro); with it, test/10160-village (0f8178cbca), the same tree plus this PR's three commits, the same as 96bccd4c94, a3af6b1149 and 54196438fc by patch-id.

    • Speed, from the 10-s summary rows over 399–461 s and 519–581 s of each run, after the menu stood in all four:

      run build fps frame p50 (median of rows) GPU busy per present (median of rows)
      1 with 0.389 2,535 ms 277.6 ms
      2 without 0.406 2,442 ms 279.5 ms
      3 without 0.368 2,727 ms 276.0 ms
      4 with 0.382 2,105 ms 278.9 ms

      The means are 0.386 fps with and 0.387 without, and the runs of one build differ as much as the builds do, so the frame rate is unchanged within noise. At this menu the presenter is busy 3.3–4.1 ms per frame in all four traces (trace_digest.py's thread table). This PR makes no frame-rate claim.

    • The menu takes this PR's path: in the 60-s traces of runs 1 and 4 (with), the presenter thread retakes the GPU mutex at the hook site, the site this PR's unlocked wait for the display keys' writer retakes it at, 3 and 6 times (GpuMutex.wait.hook; the trace records a retake only when it had to wait, so these are lower bounds). In runs 2 and 3 (without), the presenter has no such slice.

    • Memory, from 1 minute into the menu to the end of each run: private memory 18,481–19,381 MB at 1 minute, growing 723–907 MB per minute in all four, with no commit floor.

    • Closes: runs 1, 2 and 3 end with a crash in the presenter at close, after the measured window, in both builds; run 4 closed cleanly.

    • Summary lines (aps5-summary.jsonl), the rows of the 60-s trace windows (APS5_TRACE_SECONDS=460:60) of run 2 (without) and run 1 (with):

      {"commit":"d55142d279a0","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"6400","APS5_LAZY_SHADER_PREPARE":"1","APS5_NO_GPL":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"460:60","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3070 Laptop GPU","driver":"NVIDIA 617.42","os":"Windows 10.0.26300","cpu":"AMD Ryzen 9 5900HS with Radeon Graphics","cpu_threads":16,"memory_mib":15776,"title_id":"PPSA01556","title":"Resident Evil Village","content_version":"01.210.000","resolution":"3840x2160","window":{"spec":"seconds 460:60","first_present":2587,"ms":61597.811},"presents":24,"frame_ms":{"p50":2532.486,"p90":2938.668,"p99":3711.990,"max":3711.990},"gpu_busy_ms_per_present":280.063,"worker_busy_pct":{"queue worker 0x0":99.698,"queue worker 0x38":0.138,"queue worker 0x20":93.610,"queue worker 0x28":96.828},"top_phases_ms_per_frame":{"Driver.Packet":7447.818,"Driver.Packet.memory_wait":4571.201,"Driver.Packet.draw":2059.353,"Driver.Draw":2058.176,"Graphics.Draw":1780.472,"Graphics.Draw.draw_and_resource_release":1755.706,"Resources.Complete":803.815,"Driver.Packet.dispatch":599.660},"trace_dropped":false,"trace":"aps5-trace-d55142d279a0-3524.json.gz"}
      
      {"commit":"0f8178cbca31","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"6400","APS5_LAZY_SHADER_PREPARE":"1","APS5_NO_GPL":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"460:60","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3070 Laptop GPU","driver":"NVIDIA 617.42","os":"Windows 10.0.26300","cpu":"AMD Ryzen 9 5900HS with Radeon Graphics","cpu_threads":16,"memory_mib":15776,"title_id":"PPSA01556","title":"Resident Evil Village","content_version":"01.210.000","resolution":"3840x2160","window":{"spec":"seconds 460:60","first_present":1181,"ms":62201.146},"presents":23,"frame_ms":{"p50":2652.280,"p90":3043.922,"p99":3484.024,"max":3484.024},"gpu_busy_ms_per_present":278.929,"worker_busy_pct":{"queue worker 0x0":99.689,"queue worker 0x38":0.065,"queue worker 0x20":93.795,"queue worker 0x28":96.836},"top_phases_ms_per_frame":{"Driver.Packet":7849.574,"Driver.Packet.memory_wait":4808.638,"Driver.Packet.draw":2208.895,"Driver.Draw":2207.691,"Graphics.Draw":1908.668,"Graphics.Draw.draw_and_resource_release":1882.055,"Resources.Complete":855.785,"Driver.Packet.dispatch":651.564},"trace_dropped":false,"trace":"aps5-trace-0f8178cbca31-15520.json.gz"}
      
    • Files: each run's trace, its trace-window summary line and tools/trace_digest.py output: run 1, run 2, run 3, run 4.

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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none (lock time at present; PPSA01556 measured)
  • GPUs tested: NVIDIA GeForce RTX 3070 Laptop GPU (Windows, the title run); Mesa llvmpipe (Linux, tests)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4981908012 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10160). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-23T11:17:29Z. --- ### What The presenter checks a display buffer's DCC keys under the device lock. - **What went wrong:** when the batch with the kernel that writes those keys hadn't finished, the check waited for the GPU while holding the lock, and the queue worker recording the next frame waited behind the presenter. - **The fix:** `PresentDisplayBuffer` now first syncs through the key range with the unlocked wait the flush hook uses. - The presenter's acquisition is the outermost on its thread, and nothing has been decided yet, so the lock is released for the GPU wait and retaken for the completions. - The key check that follows finds the batch finished. - It's one line, for tiled display buffers with DCC keys (`dccAddress != 0`, `tilingMode == 0`). **Authorship:** the fix is @fable-void's (fable-void/AnyPS5@9ed1466f35), carried unchanged under his authorship as 96bccd4c94. His message reports, in a title's menu on Windows: GPU waits under the lock went from 721 (1.38 s) to 1 (0 ms) per 10 s, and the queue worker's waits for the lock from 1.47 s to 0.18 s per 10 s. The counter and the test are ours. **The commits:** 1. **96bccd4c94** (fable-void): the fix. 2. **a3af6b1149:** a profile-only counter on the `[present]` line behind `APS5_PROFILE_DRAW`: `waits outside the GPU mutex for the display keys' writer: N, X ms`. With the profile off it costs one test of a `static const bool`. 3. **54196438fc:** the test, `agc_driver_present_key_writer_wait` (`tests/execution/PresentKeyWriterWait.cpp`). - It calls the real `VulkanDevice::PresentDisplayBuffer` headless, holding the GPU mutex once, as `Driver::Present` does, over a display buffer whose keys a recorded GPU copy is still writing. - It requires three things: the present reached the swapchain (the key check saw the written keys); `GuestMemory::GpuHoldSerial()` advanced (the presenter released and retook the mutex for the writer); and the writer is no longer pending. - A control with no writer must leave the serial unchanged. - There's no test-only hook. ### Tested - **Resident Evil Village (PPSA01556),** Windows, RTX 3070 Laptop GPU, clocks not held. Static menu, 5 minutes, `APS5_PROFILE_DRAW=1`, on our lane F tree (12aa911767), which carries this fix with fable-void's other shared-code fixes: ``` [present] ... waits outside the GPU mutex for the display keys' writer: 5, 9.8 ms ``` That's per 10 s: GPU waits that `main` would make with the device lock held (this PR's commit alone was not run in the title). - **The test** (`agc_driver_present_key_writer_wait`): - **Red**, against this PR with only the fix's line removed: "AGC graphics: the presenter synchronized with the display keys' writer without releasing the GPU mutex". - **Green** with it. - **Linux, CI's configure flags (lavapipe, on a cloud worker):** - the test plus the present/display-buffer set, `--repeat until-fail:20`: 17 tests ran 20 times each, and 17 `video_out_*` tests skip with no display; - CI's Linux job mirrored: 1,934 tests, of which 1,844 pass, 86 skip and 4 fail, the four being that machine's IPv6 tests. - **GitHub's runners** ([run 39933804836](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39933804836), this head on `main` 44a59edc3a): - **Linux:** red with `main`'s implementation (`agc_driver_present_key_writer_wait`: "the presenter synchronized with the display keys' writer without releasing the GPU mutex"): 2 pass, 1 fails. Green ×10 with this PR: all 3 run and pass (the new test, `agc_display_buffer_dcc` and `agc_driver_dcc_key_presync`), none skip. - **Windows:** builds. The runner has no Vulkan device, so on both trees 1 runs and passes (`agc_display_buffer_dcc`, ×10 with this PR) and the 2 device tests skip. - The libc and libkernel suites pass on both. - **Validation layer** (Khronos 1.3.275, lavapipe): `main`'s driver and this PR over the 12 non-`video_out` tests give the same single `VUID-VkDeviceCreateInfo-pNext-pNext` in the same tests with the same counts. No new `VUID-`. - **Resident Evil Village (PPSA01556), a menu pair**, on an NVIDIA GeForce RTX 3070 Laptop GPU (driver 617.42), Windows 11. The static menu, 5 minutes per run, in the order with, without, without, with, from a warm shader cache with `APS5_LAZY_SHADER_PREPARE=1`, `APS5_NO_GPL=1` and `APS5_HOST_IMPORT_MIB=6400`. GPU clocks weren't locked (P0 in 98.8–99.4 % of samples, mean 1,398–1,412 MHz). - **The trees:** without this PR, [`test/10160-village-base`](https://github.com/EagleBearerYS/AnyPS5/tree/test/10160-village-base) (d55142d279, `main` 137a8fab89 plus #8926's fix, which the title needs to get past its intro); with it, [`test/10160-village`](https://github.com/EagleBearerYS/AnyPS5/tree/test/10160-village) (0f8178cbca), the same tree plus this PR's three commits, the same as 96bccd4c94, a3af6b1149 and 54196438fc by patch-id. - **Speed**, from the 10-s summary rows over 399–461 s and 519–581 s of each run, after the menu stood in all four: | run | build | fps | frame p50 (median of rows) | GPU busy per present (median of rows) | |---|---|---|---|---| | 1 | with | 0.389 | 2,535 ms | 277.6 ms | | 2 | without | 0.406 | 2,442 ms | 279.5 ms | | 3 | without | 0.368 | 2,727 ms | 276.0 ms | | 4 | with | 0.382 | 2,105 ms | 278.9 ms | The means are 0.386 fps with and 0.387 without, and the runs of one build differ as much as the builds do, so the frame rate is unchanged within noise. At this menu the presenter is busy 3.3–4.1 ms per frame in all four traces (`trace_digest.py`'s thread table). This PR makes no frame-rate claim. - **The menu takes this PR's path:** in the 60-s traces of runs 1 and 4 (with), the presenter thread retakes the GPU mutex at the `hook` site, the site this PR's unlocked wait for the display keys' writer retakes it at, 3 and 6 times (`GpuMutex.wait.hook`; the trace records a retake only when it had to wait, so these are lower bounds). In runs 2 and 3 (without), the presenter has no such slice. - **Memory**, from 1 minute into the menu to the end of each run: private memory 18,481–19,381 MB at 1 minute, growing 723–907 MB per minute in all four, with no commit floor. - **Closes:** runs 1, 2 and 3 end with a crash in the presenter at close, after the measured window, in both builds; run 4 closed cleanly. - **Summary lines** (`aps5-summary.jsonl`), the rows of the 60-s trace windows (`APS5_TRACE_SECONDS=460:60`) of run 2 (without) and run 1 (with): ```json {"commit":"d55142d279a0","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"6400","APS5_LAZY_SHADER_PREPARE":"1","APS5_NO_GPL":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"460:60","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3070 Laptop GPU","driver":"NVIDIA 617.42","os":"Windows 10.0.26300","cpu":"AMD Ryzen 9 5900HS with Radeon Graphics","cpu_threads":16,"memory_mib":15776,"title_id":"PPSA01556","title":"Resident Evil Village","content_version":"01.210.000","resolution":"3840x2160","window":{"spec":"seconds 460:60","first_present":2587,"ms":61597.811},"presents":24,"frame_ms":{"p50":2532.486,"p90":2938.668,"p99":3711.990,"max":3711.990},"gpu_busy_ms_per_present":280.063,"worker_busy_pct":{"queue worker 0x0":99.698,"queue worker 0x38":0.138,"queue worker 0x20":93.610,"queue worker 0x28":96.828},"top_phases_ms_per_frame":{"Driver.Packet":7447.818,"Driver.Packet.memory_wait":4571.201,"Driver.Packet.draw":2059.353,"Driver.Draw":2058.176,"Graphics.Draw":1780.472,"Graphics.Draw.draw_and_resource_release":1755.706,"Resources.Complete":803.815,"Driver.Packet.dispatch":599.660},"trace_dropped":false,"trace":"aps5-trace-d55142d279a0-3524.json.gz"} ``` ```json {"commit":"0f8178cbca31","dirty":false,"env":{"APS5_HOST_IMPORT_MIB":"6400","APS5_LAZY_SHADER_PREPARE":"1","APS5_NO_GPL":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"460:60","APS5_TRACE_SUMMARY_EVERY":"10"},"gpu":"NVIDIA GeForce RTX 3070 Laptop GPU","driver":"NVIDIA 617.42","os":"Windows 10.0.26300","cpu":"AMD Ryzen 9 5900HS with Radeon Graphics","cpu_threads":16,"memory_mib":15776,"title_id":"PPSA01556","title":"Resident Evil Village","content_version":"01.210.000","resolution":"3840x2160","window":{"spec":"seconds 460:60","first_present":1181,"ms":62201.146},"presents":23,"frame_ms":{"p50":2652.280,"p90":3043.922,"p99":3484.024,"max":3484.024},"gpu_busy_ms_per_present":278.929,"worker_busy_pct":{"queue worker 0x0":99.689,"queue worker 0x38":0.065,"queue worker 0x20":93.795,"queue worker 0x28":96.836},"top_phases_ms_per_frame":{"Driver.Packet":7849.574,"Driver.Packet.memory_wait":4808.638,"Driver.Packet.draw":2208.895,"Driver.Draw":2207.691,"Graphics.Draw":1908.668,"Graphics.Draw.draw_and_resource_release":1882.055,"Resources.Complete":855.785,"Driver.Packet.dispatch":651.564},"trace_dropped":false,"trace":"aps5-trace-0f8178cbca31-15520.json.gz"} ``` - **Files:** each run's trace, its trace-window summary line and `tools/trace_digest.py` output: [run 1](https://github.com/EagleBearerYS/AnyPS5/tree/pr-assets/10160/b1-pr-0f8178cbca), [run 2](https://github.com/EagleBearerYS/AnyPS5/tree/pr-assets/10160/a1-base-d55142d279), [run 3](https://github.com/EagleBearerYS/AnyPS5/tree/pr-assets/10160/a2-base-d55142d279), [run 4](https://github.com/EagleBearerYS/AnyPS5/tree/pr-assets/10160/b2-pr-0f8178cbca). ### Checklist - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none (lock time at present; PPSA01556 measured) - [x] GPUs tested: NVIDIA GeForce RTX 3070 Laptop GPU (Windows, the title run); Mesa llvmpipe (Linux, tests) - [x] AI-assisted: yes
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!10160
No description provided.