perf(agc): look ahead of the queue workers' write-watch walks on Windows #2863

Closed
DotDebian wants to merge 3 commits from upstream-pr/2863 into main
DotDebian commented 2026-10-10 16:49:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-10T16:49:44Z.


What

Problem

On Windows a queue worker walks the write-watch state of every range it collects, once per collect epoch. Astro Bot has 18 epochs per frame (one per ordering point), and the worker that records the draws makes about 1,400 GetWriteWatch calls per frame at a fixed cost of about 10 us each. Almost none of them finds a written page (about 30 dirty pages per frame).

Change

Two commits:

  1. Size the GetWriteWatch buffer to the range (WindowsMappings::Collect). The call was given the whole remaining capacity (65,536 entries) for every piece. Its cost follows the entry count, so a 64 KiB range cost about 7 us instead of 0.5 us. The count is now the smaller of the capacity and the range's pages, which gives the same results. As suggested by @exiled96 in discussion #357.
  2. Prewalk.
    • A worker's memoized walks of one epoch are logged under the address of the epoch's first walk.
    • When a later epoch starts with a walk of that address, a helper thread of the worker looks at the ranges logged last time while the worker records. On a hybrid CPU the helper runs on the efficiency cores.
    • The look calls GetWriteWatch without reset and outside every lock (WindowsMappings::ProbeClean). A clean range has its 64 KiB blocks marked in walkedBlocks, under the tracker mutex, with a generation taken before the look. A completed resetting walk marks the blocks it covered whole.
    • A memoized collect of a queue worker skips the blocks marked after its epoch began.

Safety (epoch contract of GuestMemory.hpp)

  • The look starts after the worker's ordering point, because a walk of the new epoch posts it.
  • A CPU write made before that point has one of two outcomes:
    • its dirty bit is still set at the look, so the range is not marked and the worker walks it;
    • a resetting walk already cleared it. That walk stamps the block under the tracker mutex before releasing it, and the mark is made under that mutex after the look, so the stamp is already in.
  • A mark only serves epochs that began before its generation, so it expires at the next ordering point. This is the same granularity as the existing collect memo: one walk per frame instead would not be safe.
  • A commit, release or mapping change (new mapping serial), or an unwatch, between the look and the mark drops the mark.
  • Shared views, uncommitted memory and unwatched memory are never marked. Uncached collects always walk.

Measurement

Host: Windows 11, Core Ultra 9 285K, RTX 5080. Astro Bot (PPSA21564) at the Crash Site, 150 s runs:

before prewalk
worker walks per 10 s 156,000 53,000
us per draw packet (queue 0) 33.9 29.1
FPS 9.6 10.9

Caveat: these runs had direct memory backed by write-watched private pages (#637). On main, direct memory is mapped as shared views. The prewalk leaves views to their existing per-view tracking, so its gain on main is limited to private memory until direct memory is watched the same way (#637 or #2054). Neither commit depends on those PRs. The buffer sizing (commit 1) was not measured separately here; the 7 to 0.5 us figure is @exiled96's.

Disabling

APS5_NO_PREWALK=1. The helper starts on the first epoch that repeats a key and is stopped and joined when its worker returns. Under APS5_PROFILE_DRAW the [guestmem] line counts looks, clean looks and trimmed collects.

Tested

Windows 11, MinGW GCC 15.2 Release, Astro Bot (PPSA21564) for the measurement above.

  • agc_write_tracking gains CheckPrewalk (Windows), which checks three cases:
    • clean ranges of the last epoch are skipped;
    • a page written before the ordering point is never marked clean;
    • a write after a look is seen at the next epoch.
  • The test fails if the look marks a dirty range clean (checked by mutation).
  • Full ctest on Windows (MinGW GCC 15.2, Release): 709/709 passed. agc_write_tracking, agc_shared_write and the graphics tests also pass with APS5_NO_PREWALK=1.

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 (the measured gain needs #637 or #2054, see What)
  • AI-assisted: yes (Claude Code)

Discussion: #357. Written with AI assistance (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4812859678 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2863). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-10T16:49:44Z. --- ### What **Problem** On Windows a queue worker walks the write-watch state of every range it collects, once per collect epoch. Astro Bot has 18 epochs per frame (one per ordering point), and the worker that records the draws makes about 1,400 `GetWriteWatch` calls per frame at a fixed cost of about 10 us each. Almost none of them finds a written page (about 30 dirty pages per frame). **Change** Two commits: 1. **Size the `GetWriteWatch` buffer to the range** (`WindowsMappings::Collect`). The call was given the whole remaining capacity (65,536 entries) for every piece. Its cost follows the entry count, so a 64 KiB range cost about 7 us instead of 0.5 us. The count is now the smaller of the capacity and the range's pages, which gives the same results. As suggested by @exiled96 in discussion #357. 2. **Prewalk.** - A worker's memoized walks of one epoch are logged under the address of the epoch's first walk. - When a later epoch starts with a walk of that address, a helper thread of the worker looks at the ranges logged last time while the worker records. On a hybrid CPU the helper runs on the efficiency cores. - The look calls `GetWriteWatch` without reset and outside every lock (`WindowsMappings::ProbeClean`). A clean range has its 64 KiB blocks marked in `walkedBlocks`, under the tracker mutex, with a generation taken before the look. A completed resetting walk marks the blocks it covered whole. - A memoized collect of a queue worker skips the blocks marked after its epoch began. **Safety (epoch contract of `GuestMemory.hpp`)** - The look starts after the worker's ordering point, because a walk of the new epoch posts it. - A CPU write made before that point has one of two outcomes: - its dirty bit is still set at the look, so the range is not marked and the worker walks it; - a resetting walk already cleared it. That walk stamps the block under the tracker mutex before releasing it, and the mark is made under that mutex after the look, so the stamp is already in. - A mark only serves epochs that began before its generation, so it expires at the next ordering point. This is the same granularity as the existing collect memo: one walk per frame instead would not be safe. - A commit, release or mapping change (new mapping serial), or an unwatch, between the look and the mark drops the mark. - Shared views, uncommitted memory and unwatched memory are never marked. Uncached collects always walk. **Measurement** Host: Windows 11, Core Ultra 9 285K, RTX 5080. Astro Bot (PPSA21564) at the Crash Site, 150 s runs: | | before | prewalk | |---|---|---| | worker walks per 10 s | 156,000 | 53,000 | | us per draw packet (queue 0) | 33.9 | 29.1 | | FPS | 9.6 | 10.9 | Caveat: these runs had direct memory backed by write-watched private pages (#637). On `main`, direct memory is mapped as shared views. The prewalk leaves views to their existing per-view tracking, so its gain on `main` is limited to private memory until direct memory is watched the same way (#637 or #2054). Neither commit depends on those PRs. The buffer sizing (commit 1) was not measured separately here; the 7 to 0.5 us figure is @exiled96's. **Disabling** `APS5_NO_PREWALK=1`. The helper starts on the first epoch that repeats a key and is stopped and joined when its worker returns. Under `APS5_PROFILE_DRAW` the `[guestmem]` line counts looks, clean looks and trimmed collects. ### Tested Windows 11, MinGW GCC 15.2 Release, Astro Bot (PPSA21564) for the measurement above. - `agc_write_tracking` gains `CheckPrewalk` (Windows), which checks three cases: - clean ranges of the last epoch are skipped; - a page written before the ordering point is never marked clean; - a write after a look is seen at the next epoch. - The test fails if the look marks a dirty range clean (checked by mutation). - Full `ctest` on Windows (MinGW GCC 15.2, Release): 709/709 passed. `agc_write_tracking`, `agc_shared_write` and the graphics tests also pass with `APS5_NO_PREWALK=1`. ### 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 (the measured gain needs #637 or #2054, see What) - [x] AI-assisted: yes (Claude Code) Discussion: #357. Written with AI assistance (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-10 19:18:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T19:18:10Z.


Please split the GetWriteWatch buffer sizing into its own PR; it's small and exact and can go in on its own. The prewalk's 9.6 → 10.9 FPS was measured with direct memory as private watched pages (#637), which main doesn't have, so it needs numbers on main or a Depends on: #637 (#218).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6101237189 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2863#issuecomment-6101237189). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T19:18:10Z. --- Please split the `GetWriteWatch` buffer sizing into its own PR; it's small and exact and can go in on its own. The prewalk's 9.6 → 10.9 FPS was measured with direct memory as private watched pages (#637), which main doesn't have, so it needs numbers on main or a `Depends on: #637` (#218).
adriadam10 commented 2026-10-11 00:15:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T00:15:41Z.

Review: CHANGES_REQUESTED


Following up on @oneandonlydean's request, which is still open:

  • Split the GetWriteWatch buffer sizing (WindowsMappings::Collect, core/libs/prx/libc/include/WindowsMappings.hpp:270) into its own PR. It is small, exact and independent of the prewalk.
  • The prewalk numbers (9.6 → 10.9 FPS, 156,000 → 53,000 walks) were measured with direct memory as private watched pages (#637). On main direct memory is mapped as shared views, which the prewalk leaves alone, so the gain on main is not shown. Either post before/after numbers on main, or declare Depends on: #637 (or #2054) instead of none, and rebase once that lands (#218).
  • Please add the prewalk helper thread and the "gain limited to private memory" caveat to docs/dev/TechnicalDebt.md, under an accurate section (not at the end of one).

Checked: the diff builds and the Linux ctest scope is green. The prewalk code is _WIN32 only, so we could not run CheckPrewalk here; the Windows result rests on your run.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5481298834 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2863#pullrequestreview-5481298834). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T00:15:41Z. **Review:** CHANGES_REQUESTED --- Following up on @oneandonlydean's request, which is still open: - Split the `GetWriteWatch` buffer sizing (`WindowsMappings::Collect`, `core/libs/prx/libc/include/WindowsMappings.hpp:270`) into its own PR. It is small, exact and independent of the prewalk. - The prewalk numbers (9.6 → 10.9 FPS, 156,000 → 53,000 walks) were measured with direct memory as private watched pages (#637). On `main` direct memory is mapped as shared views, which the prewalk leaves alone, so the gain on `main` is not shown. Either post before/after numbers on `main`, or declare `Depends on: #637` (or #2054) instead of `none`, and rebase once that lands (#218). - Please add the prewalk helper thread and the "gain limited to private memory" caveat to `docs/dev/TechnicalDebt.md`, under an accurate section (not at the end of one). Checked: the diff builds and the Linux ctest scope is green. The prewalk code is `_WIN32` only, so we could not run `CheckPrewalk` here; the Windows result rests on your run.
Silverash-i8 commented 2026-10-11 01:57:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Silverash-i8 on 2026-10-11T01:57:32Z.


Windows run, since the PR has no Intel or Windows results yet: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto main d31b57a4 in one build with #3217, #3202, #3167, #3137, #2863, #2849 and #2847, on an RTX 5060 Laptop and the Arrow Lake iGPU (ANYPS5_GPU=Intel).

  • agc_write_tracking (WriteTracking.cpp) passes on both GPUs, along with the rest of the write-watch tests.

Full ctest of that build on both GPUs has no failure that isn't also on plain main here (guest_filesystem needs the symlink privilege, #3169; the Intel ones are listed on #2114 and #3207).

AI-assisted (Claude Code); I ran this on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104401651 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2863#issuecomment-6104401651). Originally posted by [Silverash-i8](https://github.com/Silverash-i8) on 2026-10-11T01:57:32Z. --- Windows run, since the PR has no Intel or Windows results yet: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto `main` d31b57a4 in one build with #3217, #3202, #3167, #3137, #2863, #2849 and #2847, on an RTX 5060 Laptop and the Arrow Lake iGPU (`ANYPS5_GPU=Intel`). - `agc_write_tracking` (`WriteTracking.cpp`) passes on both GPUs, along with the rest of the write-watch tests. Full `ctest` of that build on both GPUs has no failure that isn't also on plain `main` here (`guest_filesystem` needs the symlink privilege, #3169; the Intel ones are listed on #2114 and #3207). AI-assisted (Claude Code); I ran this on my machine.
Mouhand-Kaddo commented 2026-10-11 07:42:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Mouhand-Kaddo on 2026-10-11T07:42:18Z.


Additional platform validation of standalone PR head 4f9aadb3c9f17518a7ef4cffdd21a60d6dee8537 (2026-10-11):

Linux: passed with skips — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja; Release, BUILD_TESTING=ON.

3 checks, 0 failures, 1 CTest skips. Built the selected targets and dependencies; this is focused validation, not a full build or full-suite run.

Checks: agc_write_tracking, agc_shared_write, agc_driver_graphics.

Command after building: ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^(agc_write_tracking|agc_shared_write|agc_driver_graphics)$".

This scope checks CPU-side behavior; no GPU execution result is claimed.

Internal limitation: heap-mirror subcases returned early because the guest arena was not write-watched in WSL. Zero CTest skips does not include that early return.

No title/gameplay validation. These results cover the standalone head, not a merge with current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106783845 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2863#issuecomment-6106783845). Originally posted by [Mouhand-Kaddo](https://github.com/Mouhand-Kaddo) on 2026-10-11T07:42:18Z. --- <!-- anyps5-platform-validation:4f9aadb3c9f17518a7ef4cffdd21a60d6dee8537 --> Additional platform validation of standalone PR head `4f9aadb3c9f17518a7ef4cffdd21a60d6dee8537` (2026-10-11): **Linux: passed with skips** — Arch Linux in WSL2 (kernel 6.6.87.2), GCC 16.2.1 20260810; CMake 4.3.1 / Ninja; Release, `BUILD_TESTING=ON`. 3 checks, 0 failures, 1 CTest skips. Built the selected targets and dependencies; this is focused validation, not a full build or full-suite run. Checks: `agc_write_tracking`, `agc_shared_write`, `agc_driver_graphics`. Command after building: `ctest --test-dir <build> --verbose --no-tests=error --timeout 30 -R "^(agc_write_tracking|agc_shared_write|agc_driver_graphics)$"`. This scope checks CPU-side behavior; no GPU execution result is claimed. Internal limitation: heap-mirror subcases returned early because the guest arena was not write-watched in WSL. Zero CTest skips does not include that early return. No title/gameplay validation. These results cover the standalone head, not a merge with current main.
blawesom commented 2026-10-12 13:12:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-12T13:12:37Z.


Rebase handoff — rebased onto current main, authorship unchanged (all three commits).

Two test-file conflicts with main's newer write-tracking tests, resolved by keeping both sides: main's ArenaHostHole/CheckHostHole and this PR's CheckPrewalk are independent additions, and both are called from the Windows block; the style commit's comment removal is preserved.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV — note the prewalk itself is Windows-only code, so this verifies the rebase doesn't regress the Linux build/suite, not the prewalk behavior):

  • builds; tools/check_conventions.py --base origin/main passes
  • full suite: 952/952 passed

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/2863
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is d14548903.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6124982896 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2863#issuecomment-6124982896). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-12T13:12:37Z. --- **Rebase handoff** — rebased onto current `main`, authorship unchanged (all three commits). Two test-file conflicts with main's newer write-tracking tests, resolved by keeping both sides: main's `ArenaHostHole`/`CheckHostHole` and this PR's `CheckPrewalk` are independent additions, and both are called from the Windows block; the style commit's comment removal is preserved. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV — note the prewalk itself is Windows-only code, so this verifies the rebase doesn't regress the Linux build/suite, not the prewalk behavior): - builds; `tools/check_conventions.py --base origin/main` passes - **full suite: 952/952 passed** **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/2863 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `d14548903`.
oneandonlydean commented 2026-10-14 02:24:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:24:31Z.


Closing: the 2026-10-10 request for numbers on main (or Depends on: #637) and the TechnicalDebt entry has had no reply or push. The GetWriteWatch sizing went in through #4875.
Reopen it, or comment here, when it's addressed; it can be picked up again then.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159491022 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2863#issuecomment-6159491022). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:24:31Z. --- Closing: the 2026-10-10 request for numbers on `main` (or `Depends on: #637`) and the TechnicalDebt entry has had no reply or push. The `GetWriteWatch` sizing went in through #4875. Reopen it, or comment here, when it's addressed; it can be picked up again then.

Pull request closed

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!2863
No description provided.