GPU: refresh a buffer copy's ranges by page; answer clean guarded views from the journal #393

Closed
kxiserino wants to merge 1 commit from upstream-pr/393 into main
kxiserino commented 2026-11-24 02:53:51 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kxiserino on 2026-11-24T02:53:51Z.


Summary

Two costs in the per-submission refresh of host buffer copies.

  1. Batch::add merged every range of a copy into one bounding range. A submission that reads a few small ranges at the low and high ends of one big shared buffer (a heap of over 100 MB, say) then had Shadow::update ask watch::writes (and so GetWriteWatch, or the guard walk) about the whole untouched middle, and copy whatever was stale there, though nothing declared it read.

    • Batch now keeps a Vec<(from, to)> per copy. At the flush, coalesce_ranges(&mut ranges, watch::PAGE) sorts them and merges ranges whose 64 KiB pages overlap or touch (next_from / page <= cur_to.div_ceil(page)). Ranges with a whole untouched page between them stay apart, and each one is updated by itself.
    • Empty ranges are dropped, and address + len saturates.
    • The new ranges are a subset of the old bounding range. They still cover every declared byte, so nothing read goes stale. The ranges shaders write (pending) are skipped by update as before.
  2. watch::writes(.., guarded = true) on a clean guarded view. Views can't be write-watched, so every asking first called GetWriteWatch, which fails for them, and then guard's VirtualQuery. If every block of the range is already guarded and still clean, nothing has written there since it was guarded: a title write faults into our handler, which removes the block from clean. writing() and title_memory_remapped() remove it too. So the journal already has the answer.

    • New fast path: when guarded && first < last and guards().clean.range(first..last).count() == last - first, skip both syscalls, bump asked and return written.range(first..last) as usual.
    • Lock order: JOURNAL is taken, then GUARDS, the same order guard() already uses. install(), guard(), open(), writing(), title_memory_remapped() and the vectored handler only ever take GUARDS, and none of them take JOURNAL. Nothing takes the two locks in the opposite order, so this can't deadlock.
    • A const assert pins BLOCK == PAGE, which the fast path relies on (blocks are pages).

Related: #119 makes guard's query cheaper. This change skips the query when every block is clean, so the two complement each other.

Testing

All the tests are synthetic: they use their own allocations and pagefile sections, and no title data.

  • host::batch: empty_ranges_are_dropped, ranges_in_one_page_merge, ranges_in_touching_pages_merge, ranges_pages_apart_stay_apart (low and high ends of a 256 MiB range), ranges_at_the_top_of_the_address_space_do_not_overflow.
  • watch::tests::clean_guarded_views_are_answered_from_the_journal covers repeated writes(.., true) on a clean mapped section. A test-only thread-local counter shows the kernel isn't asked again, and the answer stays the same. A later title write (through the handler), writing() and title_memory_remapped() are each still reported, then the range settles back onto the fast path.
  • watch::tests::partly_clean_ranges_ask_the_kernel checks that a partly guarded range still takes the slow path, and that unguarded askers never take the journal's word.
  • The new watch tests drop their pages' journal entries before unmapping. Otherwise written_pages_are_reported_to_every_asker could see stale entries if its allocation lands at the same address.

Results:

  • cargo test -p victory-gpu: 50 passed. With -- --test-threads=1: 50 passed. With --features diagnostics -- --test-threads=1: 52 passed.
  • cargo clippy -p victory-gpu --features diagnostics -- -D warnings and the same without features: clean.
  • cargo clippy -p victory-gpu --all-targets [--features diagnostics] -- -D warnings: the only error is the existing missing SAFETY comment at host.rs:267 on main, which #355 fixes. With that line patched locally, both are clean.
  • cargo xtask policy: asset policy: 151 files ok.

In-game frame time is not measured here.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573546530 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/393). Originally posted by [kxiserino](https://github.com/kxiserino) on 2026-11-24T02:53:51Z. --- ## Summary Two costs in the per-submission refresh of host buffer copies. 1. **`Batch::add` merged every range of a copy into one bounding range.** A submission that reads a few small ranges at the low and high ends of one big shared buffer (a heap of over 100 MB, say) then had `Shadow::update` ask `watch::writes` (and so `GetWriteWatch`, or the guard walk) about the whole untouched middle, and copy whatever was stale there, though nothing declared it read. - `Batch` now keeps a `Vec<(from, to)>` per copy. At the flush, `coalesce_ranges(&mut ranges, watch::PAGE)` sorts them and merges ranges whose 64 KiB pages overlap or touch (`next_from / page <= cur_to.div_ceil(page)`). Ranges with a whole untouched page between them stay apart, and each one is updated by itself. - Empty ranges are dropped, and `address + len` saturates. - The new ranges are a subset of the old bounding range. They still cover every declared byte, so nothing read goes stale. The ranges shaders write (`pending`) are skipped by `update` as before. 2. **`watch::writes(.., guarded = true)` on a clean guarded view.** Views can't be write-watched, so every asking first called `GetWriteWatch`, which fails for them, and then `guard`'s `VirtualQuery`. If every block of the range is already guarded and still clean, nothing has written there since it was guarded: a title write faults into our handler, which removes the block from `clean`. `writing()` and `title_memory_remapped()` remove it too. So the journal already has the answer. - New fast path: when `guarded && first < last` and `guards().clean.range(first..last).count() == last - first`, skip both syscalls, bump `asked` and return `written.range(first..last)` as usual. - **Lock order:** `JOURNAL` is taken, then `GUARDS`, the same order `guard()` already uses. `install()`, `guard()`, `open()`, `writing()`, `title_memory_remapped()` and the vectored handler only ever take `GUARDS`, and none of them take `JOURNAL`. Nothing takes the two locks in the opposite order, so this can't deadlock. - A `const` assert pins `BLOCK == PAGE`, which the fast path relies on (blocks are pages). Related: #119 makes `guard`'s query cheaper. This change skips the query when every block is clean, so the two complement each other. ## Testing All the tests are synthetic: they use their own allocations and pagefile sections, and no title data. - `host::batch`: `empty_ranges_are_dropped`, `ranges_in_one_page_merge`, `ranges_in_touching_pages_merge`, `ranges_pages_apart_stay_apart` (low and high ends of a 256 MiB range), `ranges_at_the_top_of_the_address_space_do_not_overflow`. - `watch::tests::clean_guarded_views_are_answered_from_the_journal` covers repeated `writes(.., true)` on a clean mapped section. A test-only thread-local counter shows the kernel isn't asked again, and the answer stays the same. A later title write (through the handler), `writing()` and `title_memory_remapped()` are each still reported, then the range settles back onto the fast path. - `watch::tests::partly_clean_ranges_ask_the_kernel` checks that a partly guarded range still takes the slow path, and that unguarded askers never take the journal's word. - The new watch tests drop their pages' journal entries before unmapping. Otherwise `written_pages_are_reported_to_every_asker` could see stale entries if its allocation lands at the same address. Results: - `cargo test -p victory-gpu`: 50 passed. With `-- --test-threads=1`: 50 passed. With `--features diagnostics -- --test-threads=1`: 52 passed. - `cargo clippy -p victory-gpu --features diagnostics -- -D warnings` and the same without features: clean. - `cargo clippy -p victory-gpu --all-targets [--features diagnostics] -- -D warnings`: the only error is the existing missing `SAFETY` comment at `host.rs:267` on `main`, which #355 fixes. With that line patched locally, both are clean. - `cargo xtask policy`: `asset policy: 151 files ok`. In-game frame time is not measured here. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jeremygold02 commented 2026-11-24 06:55:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:27:31Z.


Thanks @kxiserino for this journal-backed range handling. I tested head 6e7186c09b15f25822b05707f0b7ded00f57a747 with:

cargo test --locked -p victory-gpu --features diagnostics --lib watch::tests:: -- --test-threads=1

Result: 4 passed, 0 failed. The watch tests cover clean guarded reads answered from the journal, partially clean ranges falling back to the kernel, guarded kernel reads, and reporting written pages to all askers. These are synthetic watch/journal unit tests, not a title workload.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122037918 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/393#issuecomment-7122037918). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:27:31Z. --- Thanks @kxiserino for this journal-backed range handling. I tested head `6e7186c09b15f25822b05707f0b7ded00f57a747` with: ``` cargo test --locked -p victory-gpu --features diagnostics --lib watch::tests:: -- --test-threads=1 ``` Result: 4 passed, 0 failed. The watch tests cover clean guarded reads answered from the journal, partially clean ranges falling back to the kernel, guarded kernel reads, and reporting written pages to all askers. These are synthetic watch/journal unit tests, not a title workload.
LooperSalty commented 2026-11-24 10:03:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LooperSalty on 2026-11-24T09:18:31Z.


Measured in the world on an RTX 4070 SUPER + i7-13700KF (diag build, same ~20-PR stack before and after, 2 minutes in the world, VICTORY_SHADER_LOOP_LIMIT=32), per frame from copystat:

  • before (#464 + #490 + #497): 1,734 watches, 413 ms, 11,314 MB checked;
  • with this PR + a small local change that remembers, per submission, the ranges already found clean (the Batch forgot them after each flush(), so the same ~174 MB range was asked again by every list): 875 watches, 224 ms, 3,350 MB checked.

What remains on that path here is ~81,000 guard page faults per frame and ~136 MB of unchanged textures hashed per frame, so the frame time only moved from about 550 to 500 ms; #185's per-reservation write-watch looks like the next step.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7127234712 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/393#issuecomment-7127234712). Originally posted by [LooperSalty](https://github.com/LooperSalty) on 2026-11-24T09:18:31Z. --- Measured in the world on an RTX 4070 SUPER + i7-13700KF (diag build, same ~20-PR stack before and after, 2 minutes in the world, `VICTORY_SHADER_LOOP_LIMIT=32`), per frame from `copystat`: - before (#464 + #490 + #497): 1,734 watches, **413 ms**, 11,314 MB checked; - with this PR + a small local change that remembers, per submission, the ranges already found clean (the `Batch` forgot them after each `flush()`, so the same ~174 MB range was asked again by every list): 875 watches, **224 ms**, 3,350 MB checked. What remains on that path here is ~81,000 guard page faults per frame and ~136 MB of unchanged textures hashed per frame, so the frame time only moved from about 550 to 500 ms; #185's per-reservation write-watch looks like the next step. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
AgneyaK commented 2026-11-24 19:04:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AgneyaK on 2026-11-24T18:24:50Z.


Test report on a 6 GB / 16 GB laptop: this PR takes write-watch time from 475 ms to 13 ms per world frame, and frames get 16% shorter.

Setup: RTX 3050 6 GB Laptop (NVIDIA, driver 32.0.16.2124), i7-13700H, 16 GB RAM, Windows 11 26300, diag build. Title 1.0.2.1, Series X profile, the story load. Base tree: main ffe310e + the PRs listed in #1232 + #431. Each PR was merged onto that base, and each row is one run of 300 s from launch. "World frame" is the average of the fps, ms/frame (…) lines under 10 fps with 20+ draws. Write-watch numbers are the last 8 copystat lines.

tree world frame preparing copies write-watch askings / frame their time / frame
base (+ #431) 3,966 ms 1,988 447 867 475 ms
+ #393 3,347 ms 1,579 100 585 13 ms
+ #440 + #393 3,221 ms 1,553 67 572 14 ms
+ #515 5,256 ms 3,228 1,223 884 1,605 ms
+ #440 + #515 + #393 4,090 ms 2,243 201 550 29 ms
  • The askings' time is almost gone, and copies drop from 447 to 100 ms per frame.
  • Merges cleanly onto the base above.
  • Run-to-run spread on this machine is about ±10%. The write-watch and copy columns moved far more than that.

Not tested: more than one run, a release build, AMD. I didn't check correctness beyond the run not failing (no device removal or title stop in the 300 s window).

AI use: Claude Code (Claude Opus 5.5) built the trees, ran the title on my machine and drafted this report; results are from the runs described.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140610291 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/393#issuecomment-7140610291). Originally posted by [AgneyaK](https://github.com/AgneyaK) on 2026-11-24T18:24:50Z. --- Test report on a 6 GB / 16 GB laptop: this PR takes write-watch time from 475 ms to 13 ms per world frame, and frames get 16% shorter. **Setup:** RTX 3050 6 GB Laptop (NVIDIA, driver 32.0.16.2124), i7-13700H, 16 GB RAM, Windows 11 26300, diag build. Title 1.0.2.1, Series X profile, the story load. Base tree: `main` `ffe310e` + the PRs listed in #1232 + #431. Each PR was merged onto that base, and each row is one run of 300 s from launch. "World frame" is the average of the `fps, ms/frame (…)` lines under 10 fps with 20+ draws. Write-watch numbers are the last 8 `copystat` lines. | tree | world frame | preparing | copies | write-watch askings / frame | their time / frame | |---|---:|---:|---:|---:|---:| | base (+ #431) | 3,966 ms | 1,988 | 447 | 867 | 475 ms | | + #393 | **3,347 ms** | 1,579 | **100** | 585 | **13 ms** | | + #440 + #393 | **3,221 ms** | 1,553 | 67 | 572 | 14 ms | | + #515 | 5,256 ms | 3,228 | 1,223 | 884 | **1,605 ms** | | + #440 + #515 + #393 | 4,090 ms | 2,243 | 201 | 550 | 29 ms | - The askings' time is almost gone, and copies drop from 447 to 100 ms per frame. - Merges cleanly onto the base above. - Run-to-run spread on this machine is about ±10%. The write-watch and copy columns moved far more than that. Not tested: more than one run, a release build, AMD. I didn't check correctness beyond the run not failing (no device removal or title stop in the 300 s window). AI use: Claude Code (Claude Opus 5.5) built the trees, ran the title on my machine and drafted this report; results are from the runs described.
upstream-sync closed this pull request 2026-11-24 23:04:08 +00:00

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
Marvisak/VIctory!393
No description provided.