Skip to content

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

Closed
DotDebian wants to merge 3 commits into
boykopovar:mainfrom
DotDebian:feat/write-watch-prewalk
Closed

DotDebian wants to merge 3 commits into
boykopovar:mainfrom
DotDebian:feat/write-watch-prewalk

Conversation

@DotDebian

@DotDebian DotDebian commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

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 Astro Bot (PPSA21564) #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

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

🤖 Generated with Claude Code

DotDebian and others added 3 commits October 10, 2026 18:17
WindowsMappings::Collect handed GetWriteWatch the caller's whole remaining
capacity (65536 entries for the write tracker) for every private piece it
collects. The call's cost follows the entry count it is given, not the
pages of the range: about 7 us for a 64 KiB range with 65536 entries,
against 0.5 us with the 16 the range can actually report. Most collects
are small ranges that find nothing written, so this fixed cost was most
of each walk.

The count passed is now the smaller of the remaining capacity and the
range's 4 KiB pages. A range cannot report more pages than it has, so
the results are the same; a full buffer still returns early for the
caller to continue as before.

As suggested by exiled96 in discussion boykopovar#357, who measured the per-call
cost on Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A queue worker walks the write-watch state of every range it collects,
once per collect epoch, and there are 18 epochs per frame in Astro Bot
(one per ordering point). About 1,400 GetWriteWatch calls per frame
land on the worker that records the draws, at a fixed cost of about
10 us each, and almost none of them finds a written page (about 30
dirty pages per frame).

Mechanism. 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 (on the efficiency cores
of a hybrid CPU) looks at the ranges logged last time while the worker
records: GetWriteWatch without reset, holding no lock across the call
(WindowsMappings::ProbeClean resolves the range under the mappings mutex
and calls GetWriteWatch after releasing it). A range found clean has its
64 KiB blocks marked in walkedBlocks with a tracker generation taken
before the look, under the tracker mutex; a completed resetting walk
marks the blocks it covered whole the same way. A memoized collect of a
queue worker skips the blocks marked after its epoch began and walks
only the rest.

Safety, under the epoch contract of GuestMemory.hpp. The look starts
after the worker's ordering point, since a walk of the new epoch posts
it, so its generation is newer than the worker's epoch generation. A
CPU write made before that point either still has its dirty bit at the
look (the range is not marked and the worker walks it) or had it
cleared by a resetting walk, which stamps the block under the tracker
mutex before releasing it; 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 was taken, so it expires at the next ordering
point. A commit, release or mapping change (new mapping serial) or an
unwatch between the look and the mark drops the mark. Shared views,
uncommitted and non-watched memory are never marked. Uncached collects
always walk.

Measured on Astro Bot (PPSA21564) at the Crash Site, Windows 11, Core
Ultra 9 285K, RTX 5080, 150 s runs: worker walks 156,000 -> 53,000 per
10 s, 33.9 -> 29.1 us per draw packet, 9.6 -> 10.9 FPS. That run had
direct memory backed by write-watched private pages (boykopovar#637); on main
direct memory is mapped as shared views, which this leaves to the
existing per-view tracking, so the gain there is limited to private
memory until direct memory is watched the same way.

APS5_NO_PREWALK=1 disables it. The helper is started on the first epoch
that repeats a key and is stopped and joined when its worker returns.
The [guestmem] line (APS5_PROFILE_DRAW) counts looks, clean looks and
trimmed collects. agc_write_tracking covers a clean range being skipped,
a page written before the ordering point never being marked clean, and
a write after a look being seen at the next epoch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The repository allows comments only for documented technical debt. The
mechanism and its safety argument stay in the commit message of the
prewalk and in the pull request description.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Windows run, since the PR has no Intel or Windows results yet: Windows 11, WinLibs GCC 15.2.0 r7, Release, merged onto main d31b57a 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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* libc needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants