perf(agc): on Windows a small compared host import takes its whole 64 KiB tracker blocks off the write watch, and their other pages fall to full revalidation walks (DEATHLOOP) #12329

Closed
opened 2026-10-28 10:03:31 +00:00 by BunnyFl · 0 comments
BunnyFl commented 2026-10-28 10:03:31 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-10-28T10:03:31Z.


What happens

On Windows, with tibzejoker's port of #6192, DEATHLOOP (PPSA01670 01.405.000) still spends 5.5-6 s of every 10 s of its opening cutscene in [rescache] revalidate, almost all of it full walks after a collect is refused because the range is not covered by the write watch. A local diagnostic classified those refusals by why the first uncovered page left the watch. Most of them are on pages that were never imported: they share a 64 KiB tracker block with a small host import that is still compared, and WriteWatchCoverage::Exclude takes the whole block off the watch.

Two local changes together remove those refusals. With them the cutscene reaches its first gameplay banner in about half the time. I'm asking whether either direction is acceptable before making a PR, because each one changes a behaviour that is there on purpose.

Setup

Windows 11 Pro 10.0.26200, RX 9070 XT (AMD 26.10.1), Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7, Release. main 46fe6f2437 with tibzejoker's port-6192 (8150c25a68) merged on top (the only conflict was the test list in libSceAgcDriver/CMakeLists.txt, both sides kept). APS5_FORCE_SDR=1, scripted pad input from a fresh save (menus, then Break the Loop), APS5_PROFILE_DRAW=1 APS5_TRACE_SUMMARY_EVERY=10. The cutscene renders the same content in every run: the "The Longest Day" banner appears after about 2,900 presents in each, so I compare time to that present.

Where the refused collects come from

A local diagnostic (not part of any change) recorded, per 4 KiB page and per 64 KiB block, which call took it off the watch, and classified every refused collect by its first uncovered page. One played session of about 5 minutes on the tree with change 1 below:

first uncovered page refused collects distinct pages
in a block shared with an import the refresh path keeps unwatched (GuestMemory::Unwatch from GuestBufferMemory.cpp's refresh), page itself not in the import 992,463 733
same, import unwatched by ImportWatched with staysWatched false 57,350 57
inside an import the refresh path keeps unwatched 70,073 56
inside an import unwatched by ImportWatched 114 4
host region, outside the arena, or never excluded 0 0

So about 94 % of the refusals are on neighbours of the unwatched imports, not on the imports themselves.

The two changes

  1. Queue partial-block imports for the deferred settle too (importRunAt): an unwatched import that is not a whole number of 64 KiB blocks is queued like a whole-block one instead of only marked settleOnDemand. #9941 kept those out of the deferred path on purpose ("They still aren't queued, so the deferred path is unchanged for them"); its own-pages rewatch and the shared-block check in rewatchSettledImports are what then return them.
  2. Exclude only the pages a range touches (WriteWatchCoverage::Exclude): set the page bits of the range in each block instead of the whole block's mask. Covers and Restore already work per page, so partly excluded blocks already occur after a Restore; a caller asking about a whole block still gets "not covered".
Diffs (against 46fe6f2437)
--- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
+++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
@@ -2221,12 +2221,10 @@
     if (deferredSettles.load(std::memory_order_relaxed) && entry.alias == nullptr && state.watchPrivateImports && writable) {
-        if (base % settleBlockBytes != 0 || bytes % settleBlockBytes != 0) {
-            entry.settleOnDemand = entry.unwatched;
-        } else if (entry.unwatched) {
+        if (entry.unwatched) {
             state.pendingSettles.insert_or_assign(base, PendingSettle{});
             state.settleWanted.push_back(base);
-        } else if (settled && !GuestMemory::Watched(base, static_cast<std::size_t>(bytes))) {
+        } else if (base % settleBlockBytes == 0 && bytes % settleBlockBytes == 0 && settled && !GuestMemory::Watched(base, static_cast<std::size_t>(bytes))) {
--- a/core/libs/prx/libSceAgcDriver/Execution/include/WriteWatchCoverage.hpp
+++ b/core/libs/prx/libSceAgcDriver/Execution/include/WriteWatchCoverage.hpp
@@ -42,14 +42,16 @@
         if (!excludedBlocks(address, bytes, first, last)) return false;
+        const auto offset = std::max(address, base) - base;
+        const auto firstPage = offset / PageBytes;
+        const auto endPage = (std::min<std::uint64_t>(address + bytes - base, size) + PageBytes - 1) / PageBytes;
         bool changed = false;
         for (auto block = first; block <= last; ++block) {
-            const auto pages = (std::min<std::uint64_t>(BlockBytes, size - block * BlockBytes) + PageBytes - 1) / PageBytes;
-            const auto mask = pageMask(0, pages);
-            changed = changed || excluded[block] != mask;
+            const auto mask = pageMask(block == first ? firstPage % PagesPerBlock : 0, block == last ? (endPage - 1) % PagesPerBlock + 1 : PagesPerBlock);
+            changed = changed || (excluded[block] & mask) != mask;
             auto& stamp = excludedGenerations[block];
             stamp = std::max(stamp, std::max<std::uint64_t>(generation, 1));
-            excluded[block] = mask;
+            excluded[block] |= mask;
         }

With both, the same diagnostic counts 0 refusals on neighbours in a played session (219,970 remain inside 127 pages of imports).

Measurements

Three alternating runs per build, 420 s each, same input. Base is main + the port + change 1; the other adds change 2:

base + change 2
present 2,900 (banner) reached at 334 s, 306 s, not by 370 s 152 s, 146 s, 145 s
cutscene presents per 10 s, mean 39.6, 44.0, 28.1 92.4, 112.3, 112.4
[rescache] revalidate per 10 s, cutscene windows 5.5-6.0 s (one run 1.3-1.9 s) 1.0-2.2 s

A second set the next night, both builds also carrying #11265, #11368 and a local f16 change, three runs each at 600 s: banner at 353 / 517 / 392 s without change 2 and 154 / 178 / 179 s with it.

What I could not show:

  • Change 2 on plain main (no port, no change 1), one run each: no difference (revalidate 7.3-7.7 s vs 6.7-6.9 s per 10 s, no cutscene speed-up).
  • Change 1 alone on the port, one run: no difference either.
  • The port with change 2 but without change 1 was not run.
  • No trace capture (APS5_TRACE_SECONDS), only the 10 s summaries.

What they conflict with

  • agc_write_watch_coverage asserts the whole-block behaviour (a range crossing into the import is watched fails with change 2: it imports a page at block + 4096 and expects page 0 of that block unwatched).
  • tests/Recorder.cpp case (m) requires that a deferred settle does not return an import that is not a whole number of 64 KiB blocks to the write watch (line 2755 on main), which change 1 is meant to reverse. I have not run the recorder tests on these changes.
  • af4e074a02 gives the unwatch hook the whole excluded block span so pending storage results in the block are saved; with change 2 the hook still gets that span (ExcludedSpan is unchanged), so it saves more than strictly needed, which I believe is harmless but have not tested.

Questions

  1. Is block-granular exclusion still needed now that Covers/Restore work per page, or is there a reader that relies on a whole block being excluded (for example something that walks or stamps whole blocks for an import)?
  2. Was keeping partial-block imports out of the deferred settle only about not changing the deferred path in #9941, or is there a correctness reason?

If both are fine I can open a PR with the two changes, updated tests and a run on current main with the port.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6076714104 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/12329). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-10-28T10:03:31Z. --- ### What happens On Windows, with tibzejoker's port of #6192, DEATHLOOP (PPSA01670 01.405.000) still spends 5.5-6 s of every 10 s of its opening cutscene in `[rescache] revalidate`, almost all of it full walks after a collect is refused because the range is not covered by the write watch. A local diagnostic classified those refusals by why the first uncovered page left the watch. Most of them are on pages that were never imported: they share a 64 KiB tracker block with a small host import that is still compared, and `WriteWatchCoverage::Exclude` takes the whole block off the watch. Two local changes together remove those refusals. With them the cutscene reaches its first gameplay banner in about half the time. I'm asking whether either direction is acceptable before making a PR, because each one changes a behaviour that is there on purpose. ### Setup Windows 11 Pro 10.0.26200, RX 9070 XT (AMD 26.10.1), Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7, Release. `main` `46fe6f2437` with tibzejoker's `port-6192` (`8150c25a68`) merged on top (the only conflict was the test list in `libSceAgcDriver/CMakeLists.txt`, both sides kept). `APS5_FORCE_SDR=1`, scripted pad input from a fresh save (menus, then Break the Loop), `APS5_PROFILE_DRAW=1 APS5_TRACE_SUMMARY_EVERY=10`. The cutscene renders the same content in every run: the "The Longest Day" banner appears after about 2,900 presents in each, so I compare time to that present. ### Where the refused collects come from A local diagnostic (not part of any change) recorded, per 4 KiB page and per 64 KiB block, which call took it off the watch, and classified every refused collect by its first uncovered page. One played session of about 5 minutes on the tree with change 1 below: | first uncovered page | refused collects | distinct pages | |---|---|---| | in a block shared with an import the refresh path keeps unwatched (`GuestMemory::Unwatch` from `GuestBufferMemory.cpp`'s refresh), page itself not in the import | 992,463 | 733 | | same, import unwatched by `ImportWatched` with `staysWatched` false | 57,350 | 57 | | inside an import the refresh path keeps unwatched | 70,073 | 56 | | inside an import unwatched by `ImportWatched` | 114 | 4 | | host region, outside the arena, or never excluded | 0 | 0 | So about 94 % of the refusals are on neighbours of the unwatched imports, not on the imports themselves. ### The two changes 1. **Queue partial-block imports for the deferred settle too** (`importRunAt`): an unwatched import that is not a whole number of 64 KiB blocks is queued like a whole-block one instead of only marked `settleOnDemand`. #9941 kept those out of the deferred path on purpose ("They still aren't queued, so the deferred path is unchanged for them"); its own-pages rewatch and the shared-block check in `rewatchSettledImports` are what then return them. 2. **Exclude only the pages a range touches** (`WriteWatchCoverage::Exclude`): set the page bits of the range in each block instead of the whole block's mask. `Covers` and `Restore` already work per page, so partly excluded blocks already occur after a `Restore`; a caller asking about a whole block still gets "not covered". <details><summary>Diffs (against <code>46fe6f2437</code>)</summary> ```diff --- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp @@ -2221,12 +2221,10 @@ if (deferredSettles.load(std::memory_order_relaxed) && entry.alias == nullptr && state.watchPrivateImports && writable) { - if (base % settleBlockBytes != 0 || bytes % settleBlockBytes != 0) { - entry.settleOnDemand = entry.unwatched; - } else if (entry.unwatched) { + if (entry.unwatched) { state.pendingSettles.insert_or_assign(base, PendingSettle{}); state.settleWanted.push_back(base); - } else if (settled && !GuestMemory::Watched(base, static_cast<std::size_t>(bytes))) { + } else if (base % settleBlockBytes == 0 && bytes % settleBlockBytes == 0 && settled && !GuestMemory::Watched(base, static_cast<std::size_t>(bytes))) { --- a/core/libs/prx/libSceAgcDriver/Execution/include/WriteWatchCoverage.hpp +++ b/core/libs/prx/libSceAgcDriver/Execution/include/WriteWatchCoverage.hpp @@ -42,14 +42,16 @@ if (!excludedBlocks(address, bytes, first, last)) return false; + const auto offset = std::max(address, base) - base; + const auto firstPage = offset / PageBytes; + const auto endPage = (std::min<std::uint64_t>(address + bytes - base, size) + PageBytes - 1) / PageBytes; bool changed = false; for (auto block = first; block <= last; ++block) { - const auto pages = (std::min<std::uint64_t>(BlockBytes, size - block * BlockBytes) + PageBytes - 1) / PageBytes; - const auto mask = pageMask(0, pages); - changed = changed || excluded[block] != mask; + const auto mask = pageMask(block == first ? firstPage % PagesPerBlock : 0, block == last ? (endPage - 1) % PagesPerBlock + 1 : PagesPerBlock); + changed = changed || (excluded[block] & mask) != mask; auto& stamp = excludedGenerations[block]; stamp = std::max(stamp, std::max<std::uint64_t>(generation, 1)); - excluded[block] = mask; + excluded[block] |= mask; } ``` </details> With both, the same diagnostic counts 0 refusals on neighbours in a played session (219,970 remain inside 127 pages of imports). ### Measurements Three alternating runs per build, 420 s each, same input. Base is `main` + the port + change 1; the other adds change 2: | | base | + change 2 | |---|---|---| | present 2,900 (banner) reached at | 334 s, 306 s, not by 370 s | 152 s, 146 s, 145 s | | cutscene presents per 10 s, mean | 39.6, 44.0, 28.1 | 92.4, 112.3, 112.4 | | `[rescache] revalidate` per 10 s, cutscene windows | 5.5-6.0 s (one run 1.3-1.9 s) | 1.0-2.2 s | A second set the next night, both builds also carrying #11265, #11368 and a local f16 change, three runs each at 600 s: banner at 353 / 517 / 392 s without change 2 and 154 / 178 / 179 s with it. What I could not show: - Change 2 on plain `main` (no port, no change 1), one run each: no difference (`revalidate` 7.3-7.7 s vs 6.7-6.9 s per 10 s, no cutscene speed-up). - Change 1 alone on the port, one run: no difference either. - The port with change 2 but without change 1 was not run. - No trace capture (`APS5_TRACE_SECONDS`), only the 10 s summaries. ### What they conflict with - `agc_write_watch_coverage` asserts the whole-block behaviour (`a range crossing into the import is watched` fails with change 2: it imports a page at `block + 4096` and expects page 0 of that block unwatched). - `tests/Recorder.cpp` case (m) requires that a deferred settle does not return an import that is not a whole number of 64 KiB blocks to the write watch (line 2755 on `main`), which change 1 is meant to reverse. I have not run the recorder tests on these changes. - `af4e074a02` gives the unwatch hook the whole excluded block span so pending storage results in the block are saved; with change 2 the hook still gets that span (`ExcludedSpan` is unchanged), so it saves more than strictly needed, which I believe is harmless but have not tested. ### Questions 1. Is block-granular exclusion still needed now that `Covers`/`Restore` work per page, or is there a reader that relies on a whole block being excluded (for example something that walks or stamps whole blocks for an import)? 2. Was keeping partial-block imports out of the deferred settle only about not changing the deferred path in #9941, or is there a correctness reason? If both are fine I can open a PR with the two changes, updated tests and a run on current `main` with the port. AI-assisted: yes (Claude Code)
Sign in to join this conversation.
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#12329
No description provided.