fix(agc): poll for completions with a high-resolution timer on Windows #1474

Merged
popcoder565 merged 1 commit from upstream-pr/1474 into main 2026-10-08 14:20:23 +00:00
popcoder565 commented 2026-10-08 08:45:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-08T08:45:19Z.


What

On Windows, an end-of-pipe interrupt for a submission with GPU work reached the guest's equeue about 15 ms after the GPU had finished. The label written by the same RELEASE_MEM was visible within 0.5 ms.

The interrupt waits for the recorded work to be reaped (AfterRecordedWork in PacketMemory.cpp). With the queues idle, that reap comes from the queue worker, which waits on changed for 1 ms and then calls reapCompletionLabels(). With MinGW, that wait_for goes through winpthreads. winpthreads ends a timed wait only once GetTickCount64 has advanced by the timeout, and GetTickCount64 moves in 15.6 ms steps (#1471). The worker therefore reaped about every 15.6 ms instead of every millisecond. Any guest wait on a GPU completion event paid that delay.

On Windows, the worker now sleeps with PollSleep, the driver's 200 µs high-resolution timer sleep that WAIT_REG_MEM polling and the rewind-tail wait already use, and then checks again. On other platforms it still waits on changed for 1 ms.

The wait for a wait-free submission's order release (orderReleased) uses the same poll. One of the things that releases it, completions reaped by another thread, is not notified, so it was also checked only every 15.6 ms.

Trade-off on Windows: a submission or release that arrives while the worker sleeps in PollSleep is seen when the sleep ends, about 0.5 ms later, instead of on the notify. This only happens while completions are pending.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H, RTX 3060 Laptop (driver 616.92).

I added a probe mode to video_out_flip_tests locally (not in this PR):

  • Each round submits a DCB with a short compute dispatch, followed by a RELEASE_MEM that writes a label with INT_SEL 2.
  • The CPU then polls the label and an equeue with sceAgcDriverAddEqEvent(eq, 0).
  • A run is 120 rounds, of which the first 20 are dropped.

Medians per run:

Rounds Label after submit, main 8388121a This change Event after label, main This change
Back to back, 1000 shader iterations (3 runs) 0.37 to 0.38 ms 0.10 to 0.12 ms 15.2 to 15.5 ms 0.49 to 0.50 ms
sleep_for(5 ms) between rounds (15.6 ms on Windows), 1000 iterations 0.63 ms 0.44 ms 14.3 ms 0.42 ms
Back to back, 200000 iterations (about 1 ms of GPU work) 1.33 ms 1.14 ms 14.5 ms 0.47 ms

The event itself arrived 15.6 to 15.9 ms after the submit on main, and 0.60 ms after it with this change (back to back, 1000 iterations).

  • Full ctest: 421 of 423 pass. agc_driver_srgb8_color_target failed (#1434). guest_raise_exception timed out; it loads only libc and libkernel, and this is the Windows hang #1421 fixes. I then ran the 203 agc_ tests twice more: one run passed all of them, and in the other only agc_driver_dcc_keys_first_write (#1434) failed.
  • video_out_flip_tests (not in ctest, #1040), run twice, gives the same results as on main (d549f768): decode, controls, present, backtoback, dcc, pacing and aftergpu pass, unavailable fails, and lifetime and reopen terminate.

Not run on Linux, whose code path is unchanged. No title tested.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4784201723 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1474). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-08T08:45:19Z. --- ### What On Windows, an end-of-pipe interrupt for a submission with GPU work reached the guest's equeue about 15 ms after the GPU had finished. The label written by the same `RELEASE_MEM` was visible within 0.5 ms. The interrupt waits for the recorded work to be reaped (`AfterRecordedWork` in `PacketMemory.cpp`). With the queues idle, that reap comes from the queue worker, which waits on `changed` for 1 ms and then calls `reapCompletionLabels()`. With MinGW, that `wait_for` goes through winpthreads. winpthreads ends a timed wait only once `GetTickCount64` has advanced by the timeout, and `GetTickCount64` moves in 15.6 ms steps (#1471). The worker therefore reaped about every 15.6 ms instead of every millisecond. Any guest wait on a GPU completion event paid that delay. On Windows, the worker now sleeps with `PollSleep`, the driver's 200 µs high-resolution timer sleep that WAIT_REG_MEM polling and the rewind-tail wait already use, and then checks again. On other platforms it still waits on `changed` for 1 ms. The wait for a wait-free submission's order release (`orderReleased`) uses the same poll. One of the things that releases it, completions reaped by another thread, is not notified, so it was also checked only every 15.6 ms. Trade-off on Windows: a submission or release that arrives while the worker sleeps in `PollSleep` is seen when the sleep ends, about 0.5 ms later, instead of on the notify. This only happens while completions are pending. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Intel Core i7-12700H, RTX 3060 Laptop (driver 616.92). I added a probe mode to `video_out_flip_tests` locally (not in this PR): - Each round submits a DCB with a short compute dispatch, followed by a `RELEASE_MEM` that writes a label with `INT_SEL` 2. - The CPU then polls the label and an equeue with `sceAgcDriverAddEqEvent(eq, 0)`. - A run is 120 rounds, of which the first 20 are dropped. Medians per run: | Rounds | Label after submit, `main` 8388121a | This change | Event after label, `main` | This change | |---|---|---|---|---| | Back to back, 1000 shader iterations (3 runs) | 0.37 to 0.38 ms | 0.10 to 0.12 ms | 15.2 to 15.5 ms | 0.49 to 0.50 ms | | `sleep_for(5 ms)` between rounds (15.6 ms on Windows), 1000 iterations | 0.63 ms | 0.44 ms | 14.3 ms | 0.42 ms | | Back to back, 200000 iterations (about 1 ms of GPU work) | 1.33 ms | 1.14 ms | 14.5 ms | 0.47 ms | The event itself arrived 15.6 to 15.9 ms after the submit on `main`, and 0.60 ms after it with this change (back to back, 1000 iterations). - Full ctest: 421 of 423 pass. `agc_driver_srgb8_color_target` failed (#1434). `guest_raise_exception` timed out; it loads only `libc` and `libkernel`, and this is the Windows hang #1421 fixes. I then ran the 203 `agc_` tests twice more: one run passed all of them, and in the other only `agc_driver_dcc_keys_first_write` (#1434) failed. - `video_out_flip_tests` (not in ctest, #1040), run twice, gives the same results as on `main` (d549f768): `decode`, `controls`, `present`, `backtoback`, `dcc`, `pacing` and `aftergpu` pass, `unavailable` fails, and `lifetime` and `reopen` terminate. Not run on Linux, whose code path is unchanged. No title tested. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-08 11:57:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T11:57:54Z.

Review: APPROVED


Reviewed the complete diff, PR description, and current Linux, Windows, Conventions, and Progress report checks. The Windows polling path releases and reacquires the queue mutex around PollSleep, while non-Windows keeps the condition-variable wait. The polling latency trade-off is documented. I found no issues.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5456194659 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1474#pullrequestreview-5456194659). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T11:57:54Z. **Review:** APPROVED --- Reviewed the complete diff, PR description, and current Linux, Windows, Conventions, and Progress report checks. The Windows polling path releases and reacquires the queue mutex around PollSleep, while non-Windows keeps the condition-variable wait. The polling latency trade-off is documented. I found no issues.
adriadam10 commented 2026-10-08 14:20:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T14:20:19Z.


Checked the diff, build and full ctest (462/462) on the PR merged onto current main, and CI (Linux, Windows, conventions) is green. PollSleep is the existing helper from WaitMemory, the queue lock is released around the sleep, and the non-Windows path keeps the 1 ms wait_for. The Windows latency numbers are yours and we couldn't run them here (no Windows host). Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6061922056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1474#issuecomment-6061922056). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T14:20:19Z. --- Checked the diff, build and full ctest (462/462) on the PR merged onto current `main`, and CI (Linux, Windows, conventions) is green. `PollSleep` is the existing helper from WaitMemory, the queue lock is released around the sleep, and the non-Windows path keeps the 1 ms `wait_for`. The Windows latency numbers are yours and we couldn't run them here (no Windows host). Merging.
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!1474
No description provided.