fix(agc): put a retired import's unchanged ranges back in the write watch #8926

Merged
EagleBearerYS merged 1 commit from upstream-pr/8926 into main 2026-10-26 01:11:44 +00:00
EagleBearerYS commented 2026-10-21 06:29:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-21T06:29:15Z.


What

On main, Resident Evil Village (PPSA01556) aborts in its intro, between 0:43 and 6:37, with:

FATAL: an address-based build copies 185 MiB of 2960 registered ranges, past 64 MiB (APS5_ADDRESS_COPY_MAX_MIB); the largest: 0x20c0c20000+0x10000 (0.1 MiB committed, outside the write-watched arena) 0x20c0430000+0x10000 (0.1 MiB committed, outside the write-watched arena) ...

on main 6696def6e6. The regression is from our #6457, "import adjacent registered ranges as aligned power-of-two blocks". It was merged on 2026-10-19. Boots of main with #6730, then with #6730 and #6479 reverted still abort. Main with all three of #6457, #6479 and #6730 reverted doesn't.

Cause:

  • On Windows a host import under 1 MiB leaves the write watch when it's made, and comes back only when its deferred settle read completes (#6678). retireImport drops the pending settle and never restores the watch. An import retired before it settles therefore leaves its range outside the write watch for good.
  • Once no import fits the budget, an address-based build can neither import such a range nor heap mirror it: CollectWrites returns 0, so acquireMirror refuses it. The build copies it, and enough of those copies reach APS5_ADDRESS_COPY_MAX_MIB.
  • Since #6457 this happens to every 64 KiB piece a title maps one at a time:
    • the map imports the piece on its own;
    • when its neighbour is registered, its run grows into a larger aligned block;
    • so refreshImports retires the piece's import, before it has settled. The trace prints that as "the range was mapped again", although the range is unchanged.
  • The trace: a Resident Evil Village run with APS5_TRACE_HOST_IMPORT=1 shows each page the FATAL lists, e.g. 0x2098900000+0x10000, as "ok … by map", then "retired: the range was mapped again", and never imported or watched again.
  • Credit: @keravyn-ops described this mechanism in #7047 on 2026-10-17, on a Radeon 780M where a full import budget retires such imports by eviction ("Unwatched imports never return to the write watch", experiment 2). They measured the same restore there: "unwatched heaps" fell from about 29,000 to about 500. This PR is that restore, now hit through #6457 on a discrete GPU.

The change:

  • When an unwatched import retires, refreshImports and the budget eviction (retireLeastRecent) note its ranges that are still registered as the same range. Once the import is gone, those ranges go back into the write watch, as a settled import's do (GuestMemory::Rewatch).
  • They're skipped where another unwatched import, or a live storage texture, still covers them.
  • Rewatch stamps the range as written, so the next build reads it in full.
  • Ranges the guest mapped again or released keep the existing path.
  • Imports made to replace the retired ones (the larger block) aren't changed.

Linux: imports stay watched there unless the import probe or APS5_WRITE_WATCH_IMPORTS=unwatch says otherwise, and Rewatch is Windows-only. So this changes nothing on Linux, and the new case is Windows-only.

Cost, measured on GitHub's Windows runner with a mock device (run, bench exp/rewatch-cost 4ef027a78, which is acc7b1792e, the same code as this PR's head a356b6a75a, plus a test-only benchmark commit; one run with main's code and three with this PR). The bench replays a Resident Evil Village-sized load: 2,077 refreshes that retire 5,765 unwatched imports (2,278 MiB), with the CPU storing to every 4th page of each imported piece.

  • Refresh work: about 2.8 s in all with this PR (2.78 to 2.80 s, a median of 620 µs per refresh), against 9.3 ms on main, which leaves those ranges unwatched. The whole workload takes 13.1 s, against 5.2 s.
  • Per-build copies: a repeat build over a retired 64 KiB piece takes 9 µs instead of 367 µs (medians), since the piece is watched again. The first build after the retirement takes about 470 µs either way.
  • Collects: a 32 MiB collect right after a refresh that put ranges back takes about 150 µs, against 0.3 µs.
  • A refresh that retires one piece takes about 440 µs (medians of 445 to 451 µs), more than expected for one Rewatch. That is being looked at as a separate follow-up.

Overlap with open PRs on the same path. Each was checked with git merge-tree against this head.

  • #4448 (Mouad-Scriptz-0) adds a third retirement: unwatched imports that no lookup used for sixty frames retire at the flip, and get their own Rewatch. This PR covers the two existing retirements, when the run changes and on eviction.
    • #4448 conflicts with main in the same two files with or without this PR.
    • Once both are in, RetireIdleImports can use rewatchRetired and get the same skips.
  • #7849 (MarcZX10) retires imports before a guest range cut, on the mutation path, which this PR leaves alone because the memory is changing there. Its refreshImports change removes the trimmed-import reattach and keeps the retire loop this PR adds to. The two merge cleanly.
  • #8527 (eliasbruno124-dev) orders deferred settles, the usual way back into the watch, with main-queue accesses. This PR covers imports retired before their settle. The two merge cleanly.
  • #8622 (LeandroLP23) imports a range mapped again on its first use instead of at map. In refreshImports it only clears its new set where the device changes, not in the retire loop this PR adds to. The two merge cleanly.
  • #9402 (janzezno) replaces refreshImports' loop over every import with a check lambda, which a full walk and a new incremental walk (only the imports a registry change can reach) both call. check is the only place either walk retires an import, so this PR's noteRetiredUnwatched goes there before the retire, and rewatchRetired after the walk (janzezno's composition). An import the incremental walk doesn't reach isn't retired, so it has nothing to put back. retireLeastRecent isn't touched by #9402. The two conflict textually in GuestBufferMemory.cpp; whichever merges second rebases into that shape, agreed in this thread.
  • #7505 and #7514 (AlxcBTW) touch neither refreshImports' retirement nor the budget eviction. Each conflicts with main in the same files with or without this PR.

Tested

  • agc_driver_graphics, new retiredImportWatchTests (Windows):

    • a 64 KiB piece in the write-watched arena is imported on its own;
    • then its neighbour is registered and the import budget is cut below one piece;
    • after the next build, the piece must be back in the write watch, and both pieces heap mirrored.
    • Red with main's GuestBufferMemory.cpp: "AGC graphics: a 64 KiB piece whose import retired when its run grew into a block stayed outside the write watch", in 3 of 3 runs.
  • GitHub's runners (run), targeted build of the graphics tests:

    • Windows: red with main's implementation, as above. Green ×10 with this change on agc_driver_graphics and the agc_heap_mirror_* tests, 27 tests. The libc and libkernel suites pass.
    • Linux: the same tests pass before and after. The new case is Windows-only, and nothing else changes there.
    • A full Windows build of current main stops at tests/execution/ImageArrayWindowPastLayerLimit.cpp:33 ("requested alignment '65536' exceeds object file maximum 8192", MinGW), so the runs build the tested targets only.
  • Resident Evil Village (PPSA01556) on Windows (RTX 3070), on main 1ff5c8ac5c plus 63d5414338 (this commit's code; the head differs only in its test), warm shader cache, APS5_TRACE_HOST_IMPORT=1: no copy-cap FATAL and no "outside the write-watched arena" copy in 43 minutes.

    • The same pages are still retired when their run grows ("retired: the range was mapped again", 5,766 times by the end). Now each is imported again by a build or a map, or covered by the wider block, e.g. 0x1f00000000+0x10000 to +0x30000 retired, then "host import of 0x1f00000000+0x80000 ok … by build".
    • Without the change, same base 1ff5c8ac5c, same build setup and shader cache, untraced: the copy-cap FATAL at 1:16, "an address-based build copies 121 MiB of 1943 registered ranges, past 64 MiB (APS5_ADDRESS_COPY_MAX_MIB); the largest: 0x2099d00000+0x10000 (0.1 MiB committed, outside the write-watched arena) …". On main 6696def6e6 it aborted between 0:43 and 6:37 in every run.
    • That run didn't reach gameplay: the load after Continue wasn't finished at the end, and its time isn't measured yet.
    • GPU clocks weren't held; this run measures no GPU timing.
  • SILENT HILL: Townfall (PPSA33286), by @keravyn-ops (comment), with #8622 (not ours).

    • Setup as they give it: Ryzen Z1 Extreme with a Radeon 780M, 16 GiB, 3 GiB UMA frame buffer, Windows 11 26100, AMD driver 32.0.23044.19, the 720p backbuffer patch and -nothreadtimeout. APS5_PROFILE_DRAW=1, APS5_ADDRESS_COPY_MAX_MIB=8192, the default import budget (1056 MiB) unless stated, warm shader cache, no input, 360 s per run, one run per row. main cecb540a63, with #8622 at bf66eeccfd and this PR at acc7b1792e (the same code as a356b6a75a), each merged onto that main. GPU clocks weren't reported.
    • Without this PR they measured 1.11–1.34 FPS on main and 1.17–1.32 with #8622, with 6.19 M and 5.53 M CPU-copy operations. The rows with this PR:
    build FPS (after 30 presents) CPU-copy operations unwatched heaps end
    main + this PR – (1 present) – 57 #7017 abort at 144 s, still in the start-up sequence
    main + #8622 + this PR 3.96–6.13 12 k, 527 MiB 544 out of memory at 297 s, in the 4095 MiB phase
    main + #8622 + this PR, APS5_HOST_IMPORT_MIB=1536 5.43–7.49 16 k, 1342 MiB 925 out of memory at 99 s, in the 4095 MiB phase
    • In their words, the main + this PR row "is a single run that hit the intermittent #7017 abort before the first frame, so it says nothing about #8926 alone".
    • Both runs with #8622 and this PR end out of memory "once the title reaches the phase with the 4095 MiB buffer", as their earlier APS5_HOST_IMPORT_MIB=2600 runs on #7047 did: a vkAllocateMemory of a 256 MiB device buffer fails (initialResult=-2), with heap 1 at 6221 of 2949 MiB.

Checklist

  • Based on main 1ff5c8ac5c, and merges cleanly with main 0aa55ef293; no other open PR implements the same functions (see Overlap)
  • 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: #9159 (no order with #9402, agreed in this thread; see Overlap)
  • Fixes: none (the Resident Evil Village intro abort above has no issue)
  • Related: #7047, #6457, #6678, #9136
  • Unblocks: none claimed (Resident Evil Village (PPSA01556) no longer aborts in its intro; it hasn't reached gameplay in these runs)
  • GPUs tested: NVIDIA GeForce RTX 3070 (Windows, the Resident Evil Village run); GitHub's runners
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4947393238 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8926). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-21T06:29:15Z. --- ### What On main, Resident Evil Village (PPSA01556) aborts in its intro, between 0:43 and 6:37, with: ``` FATAL: an address-based build copies 185 MiB of 2960 registered ranges, past 64 MiB (APS5_ADDRESS_COPY_MAX_MIB); the largest: 0x20c0c20000+0x10000 (0.1 MiB committed, outside the write-watched arena) 0x20c0430000+0x10000 (0.1 MiB committed, outside the write-watched arena) ... ``` on main 6696def6e6. The regression is from our #6457, "import adjacent registered ranges as aligned power-of-two blocks". It was merged on 2026-10-19. Boots of main with #6730, then with #6730 and #6479 reverted still abort. Main with all three of #6457, #6479 and #6730 reverted doesn't. **Cause:** - On Windows a host import under 1 MiB leaves the write watch when it's made, and comes back only when its deferred settle read completes (#6678). `retireImport` drops the pending settle and never restores the watch. An import retired before it settles therefore leaves its range outside the write watch for good. - Once no import fits the budget, an address-based build can neither import such a range nor heap mirror it: `CollectWrites` returns 0, so `acquireMirror` refuses it. The build copies it, and enough of those copies reach `APS5_ADDRESS_COPY_MAX_MIB`. - Since #6457 this happens to every 64 KiB piece a title maps one at a time: - the map imports the piece on its own; - when its neighbour is registered, its run grows into a larger aligned block; - so `refreshImports` retires the piece's import, before it has settled. The trace prints that as "the range was mapped again", although the range is unchanged. - **The trace:** a Resident Evil Village run with `APS5_TRACE_HOST_IMPORT=1` shows each page the FATAL lists, e.g. 0x2098900000+0x10000, as "ok … by map", then "retired: the range was mapped again", and never imported or watched again. - **Credit:** @keravyn-ops described this mechanism in #7047 on 2026-10-17, on a Radeon 780M where a full import budget retires such imports by eviction ("Unwatched imports never return to the write watch", experiment 2). They measured the same restore there: "unwatched heaps" fell from about 29,000 to about 500. This PR is that restore, now hit through #6457 on a discrete GPU. **The change:** - When an unwatched import retires, `refreshImports` and the budget eviction (`retireLeastRecent`) note its ranges that are still registered as the same range. Once the import is gone, those ranges go back into the write watch, as a settled import's do (`GuestMemory::Rewatch`). - They're skipped where another unwatched import, or a live storage texture, still covers them. - `Rewatch` stamps the range as written, so the next build reads it in full. - Ranges the guest mapped again or released keep the existing path. - Imports made to replace the retired ones (the larger block) aren't changed. **Linux:** imports stay watched there unless the import probe or `APS5_WRITE_WATCH_IMPORTS=unwatch` says otherwise, and `Rewatch` is Windows-only. So this changes nothing on Linux, and the new case is Windows-only. **Cost**, measured on GitHub's Windows runner with a mock device ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39580615406), bench `exp/rewatch-cost` 4ef027a78, which is acc7b1792e, the same code as this PR's head a356b6a75a, plus a test-only benchmark commit; one run with `main`'s code and three with this PR). The bench replays a Resident Evil Village-sized load: 2,077 refreshes that retire 5,765 unwatched imports (2,278 MiB), with the CPU storing to every 4th page of each imported piece. - **Refresh work:** about 2.8 s in all with this PR (2.78 to 2.80 s, a median of 620 µs per refresh), against 9.3 ms on `main`, which leaves those ranges unwatched. The whole workload takes 13.1 s, against 5.2 s. - **Per-build copies:** a repeat build over a retired 64 KiB piece takes 9 µs instead of 367 µs (medians), since the piece is watched again. The first build after the retirement takes about 470 µs either way. - **Collects:** a 32 MiB collect right after a refresh that put ranges back takes about 150 µs, against 0.3 µs. - A refresh that retires one piece takes about 440 µs (medians of 445 to 451 µs), more than expected for one `Rewatch`. That is being looked at as a separate follow-up. **Overlap with open PRs on the same path.** Each was checked with `git merge-tree` against this head. - **#4448** (Mouad-Scriptz-0) adds a third retirement: unwatched imports that no lookup used for sixty frames retire at the flip, and get their own `Rewatch`. This PR covers the two existing retirements, when the run changes and on eviction. - #4448 conflicts with `main` in the same two files with or without this PR. - Once both are in, `RetireIdleImports` can use `rewatchRetired` and get the same skips. - **#7849** (MarcZX10) retires imports before a guest range cut, on the mutation path, which this PR leaves alone because the memory is changing there. Its `refreshImports` change removes the trimmed-import reattach and keeps the retire loop this PR adds to. The two merge cleanly. - **#8527** (eliasbruno124-dev) orders deferred settles, the usual way back into the watch, with main-queue accesses. This PR covers imports retired before their settle. The two merge cleanly. - **#8622** (LeandroLP23) imports a range mapped again on its first use instead of at map. In `refreshImports` it only clears its new set where the device changes, not in the retire loop this PR adds to. The two merge cleanly. - **#9402** (janzezno) replaces `refreshImports`' loop over every import with a `check` lambda, which a full walk and a new incremental walk (only the imports a registry change can reach) both call. `check` is the only place either walk retires an import, so this PR's `noteRetiredUnwatched` goes there before the retire, and `rewatchRetired` after the walk ([janzezno's composition](https://github.com/boykopovar/AnyPS5/pull/8926#issuecomment-6348260057)). An import the incremental walk doesn't reach isn't retired, so it has nothing to put back. `retireLeastRecent` isn't touched by #9402. The two conflict textually in `GuestBufferMemory.cpp`; whichever merges second rebases into that shape, agreed in this thread. - **#7505** and **#7514** (AlxcBTW) touch neither `refreshImports`' retirement nor the budget eviction. Each conflicts with `main` in the same files with or without this PR. ### Tested - **`agc_driver_graphics`, new `retiredImportWatchTests` (Windows):** - a 64 KiB piece in the write-watched arena is imported on its own; - then its neighbour is registered and the import budget is cut below one piece; - after the next build, the piece must be back in the write watch, and both pieces heap mirrored. - **Red** with main's `GuestBufferMemory.cpp`: "AGC graphics: a 64 KiB piece whose import retired when its run grew into a block stayed outside the write watch", in 3 of 3 runs. - **GitHub's runners** ([run](https://github.com/EagleBearerYS/AnyPS5/actions/runs/39552710372)), targeted build of the graphics tests: - **Windows:** red with main's implementation, as above. Green ×10 with this change on `agc_driver_graphics` and the `agc_heap_mirror_*` tests, 27 tests. The libc and libkernel suites pass. - **Linux:** the same tests pass before and after. The new case is Windows-only, and nothing else changes there. - A full Windows build of current `main` stops at `tests/execution/ImageArrayWindowPastLayerLimit.cpp:33` ("requested alignment '65536' exceeds object file maximum 8192", MinGW), so the runs build the tested targets only. - **Resident Evil Village (PPSA01556)** on Windows (RTX 3070), on main 1ff5c8ac5c plus 63d5414338 (this commit's code; the head differs only in its test), warm shader cache, `APS5_TRACE_HOST_IMPORT=1`: no copy-cap FATAL and no "outside the write-watched arena" copy in 43 minutes. - The same pages are still retired when their run grows ("retired: the range was mapped again", 5,766 times by the end). Now each is imported again by a build or a map, or covered by the wider block, e.g. 0x1f00000000+0x10000 to +0x30000 retired, then "host import of 0x1f00000000+0x80000 ok … by build". - **Without the change,** same base 1ff5c8ac5c, same build setup and shader cache, untraced: the copy-cap FATAL at 1:16, "an address-based build copies 121 MiB of 1943 registered ranges, past 64 MiB (APS5_ADDRESS_COPY_MAX_MIB); the largest: 0x2099d00000+0x10000 (0.1 MiB committed, outside the write-watched arena) …". On main 6696def6e6 it aborted between 0:43 and 6:37 in every run. - That run didn't reach gameplay: the load after Continue wasn't finished at the end, and its time isn't measured yet. - GPU clocks weren't held; this run measures no GPU timing. - **SILENT HILL: Townfall (PPSA33286), by @keravyn-ops** ([comment](https://github.com/boykopovar/AnyPS5/issues/7047#issuecomment-6321147369)), with #8622 (not ours). - Setup as they give it: Ryzen Z1 Extreme with a Radeon 780M, 16 GiB, 3 GiB UMA frame buffer, Windows 11 26100, AMD driver 32.0.23044.19, the 720p backbuffer patch and `-nothreadtimeout`. `APS5_PROFILE_DRAW=1`, `APS5_ADDRESS_COPY_MAX_MIB=8192`, the default import budget (1056 MiB) unless stated, warm shader cache, no input, 360 s per run, one run per row. `main` cecb540a63, with #8622 at bf66eeccfd and this PR at acc7b1792e (the same code as a356b6a75a), each merged onto that `main`. GPU clocks weren't reported. - Without this PR they measured 1.11–1.34 FPS on `main` and 1.17–1.32 with #8622, with 6.19 M and 5.53 M CPU-copy operations. The rows with this PR: | build | FPS (after 30 presents) | CPU-copy operations | `unwatched heaps` | end | |---|---|---|---|---| | `main` + this PR | – (1 present) | – | 57 | #7017 abort at 144 s, still in the start-up sequence | | `main` + #8622 + this PR | 3.96–6.13 | 12 k, 527 MiB | 544 | out of memory at 297 s, in the 4095 MiB phase | | `main` + #8622 + this PR, `APS5_HOST_IMPORT_MIB=1536` | 5.43–7.49 | 16 k, 1342 MiB | 925 | out of memory at 99 s, in the 4095 MiB phase | - In their words, the `main` + this PR row "is a single run that hit the intermittent #7017 abort before the first frame, so it says nothing about #8926 alone". - Both runs with #8622 and this PR end out of memory "once the title reaches the phase with the 4095 MiB buffer", as their earlier `APS5_HOST_IMPORT_MIB=2600` runs on #7047 did: a `vkAllocateMemory` of a 256 MiB device buffer fails (`initialResult=-2`), with heap 1 at 6221 of 2949 MiB. ### Checklist - [x] Based on `main` 1ff5c8ac5c, and merges cleanly with `main` 0aa55ef293; no other open PR implements the same functions (see Overlap) - [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: #9159 (no order with #9402, agreed in this thread; see Overlap) - [x] Fixes: none (the Resident Evil Village intro abort above has no issue) - [x] Related: #7047, #6457, #6678, #9136 - [x] Unblocks: none claimed (Resident Evil Village (PPSA01556) no longer aborts in its intro; it hasn't reached gameplay in these runs) - [x] GPUs tested: NVIDIA GeForce RTX 3070 (Windows, the Resident Evil Village run); GitHub's runners - [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!8926
No description provided.