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

Closed
github-bot wants to merge 1 commit from gh-pr/393/head into gh-pr/393/base
Owner

@⁠kxiserino on GitHub · 2026-11-24 02:53 UTC · original
kxiserino:gpu/batch-interval-coalescing-and-clean-guard-fastpath → main · +238 −17 · 2 files · 1 commits · closed without merging

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

**@⁠kxiserino** on GitHub · 2026-11-24 02:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/393) `kxiserino:gpu/batch-interval-coalescing-and-clean-guard-fastpath` → `main` · +238 −17 · 2 files · 1 commits · closed without merging ## 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)
A submission's refreshes of one host copy were merged into the single
range covering them all. Two small ranges at the low and high ends of a
big shared buffer then had the whole untouched middle asked for written
pages (and its stale pages copied). The batch now keeps each copy's ranges
and merges only those whose 64 KiB pages overlap or touch, at the flush.

`watch::writes` for guarded memory asks the kernel first, which fails for
views, and then `guard` queries the view. When every block of the range
is already guarded and clean, nothing was written there: the journal
answers without either system call. `JOURNAL` is still taken before
`GUARDS`; nothing takes them the other way round.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:02:20 +00:00
github-bot closed this pull request 2026-11-26 04:02:20 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:27 UTC · original

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.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/393#issuecomment-7122037918) 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.
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-24 09:18 UTC · original

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

**@⁠LooperSalty** on GitHub · 2026-11-24 09:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/393#issuecomment-7127234712) 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)
Author
Owner

@⁠AgneyaK on GitHub · 2026-11-24 18:24 UTC · original

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.

**@⁠AgneyaK** on GitHub · 2026-11-24 18:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/393#issuecomment-7140610291) 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.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!393
No description provided.