gpu: opt-in working-set trim of cold title memory (VICTORY_TRIM_BELOW_MB, review fixes) #160

Open
github-bot wants to merge 1 commit from github-bot/gh-159 into main AGit
Owner

@⁠SovietBearz0r on GitHub · 2026-12-05 18:27 UTC · original
SovietBearz0r:minivi-workingset-trim → main · +766 −2 · 6 files · 1 commits · open

Replaces the closed #113 with the review fixed: strictly opt-in (no RAM-size auto-on), the OFF-path journal leak removed, and no lock held across syscalls.

Maintainer points addressed

  • Auto-ON → strictly opt-in: VICTORY_TRIM_BELOW_MB=<MiB> is the only switch; unset/0/invalid = off (below_from(None) == 0). Off path starts no thread, journals nothing, hooks early-return. README marks it experimental/off.
  • OFF-path WRITE_COMBINED_RANGES leak: the journal moved out of watch.rs into trim.rs, written only while armed, and pruned on title_memory_freed (MEM_RELEASE drops all ranges under the base; decommit cuts intersecting ranges) — tested.
  • Lock scope: OPEN_LOCK.read() is never held across VirtualQuery/VirtualUnlock/Sleep; a bounded batch (≤64 × 16 MiB) is snapshotted under a short syscall-free read hold, then syscalls run outside it with per-chunk re-query (still committed MEM_PRIVATE, not guard/WC/no-access) and re-check against current TITLE/OPENED/journal state. The unavoidable query→call window is documented (# TOCTOU); chunking/batching are pure tested helpers.
  • inplace::From::Section: documented (a section heap is a MEM_MAPPED view; the scan never trims mapped memory) instead of a silent skip; From::Region/Part still recorded.
  • Removed VICTORY_TRIM_GENTLE=0/K32EmptyWorkingSet (emptied the whole process working set, not only title-private memory). Title-private-only rule, image/mapped/heap/guard/WC exclusions, 16 MiB paced unlock, 30 s/2 GiB gating kept.

Verification

  • cargo test -p victory-gpu --lib: 724 passed, 0 failed, 21 ignored (+8 trim tests: default-off/no-thread, parsing, gating, classification, subtraction/join, journal prune, 16 MiB chunk + batch bound, allocation/exclusion gate); --all-features 801 passed; cargo check -p victory; policy 443 files; cargo test -p xtask 9; clippy -D warnings clean.
  • Still needed: constrained-RAM in-game run (≤20 GiB host, real pressure) — verify free RAM actually rises (working_set_before/5s_after log), no multi-second streaming stalls, no soft-fault hitching, no per-frame regressions. VirtualUnlock on non-VirtualLocked pages is not guaranteed to drop them on every Windows build, so a run must show the working set moving.
  • Residual TOCTOU in the query→call window (documented); worst case one chunk unlocked once on memory that was title-private when checked — VirtualUnlock only makes pages pageable, contents are not discarded.

Supersedes #113 (closed); branch force-pushed 2f7116f3.

**@⁠SovietBearz0r** on GitHub · 2026-12-05 18:27 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/159) `SovietBearz0r:minivi-workingset-trim` → `main` · +766 −2 · 6 files · 1 commits · open Replaces the closed #113 with the review fixed: strictly opt-in (no RAM-size auto-on), the OFF-path journal leak removed, and no lock held across syscalls. ## Maintainer points addressed - **Auto-ON → strictly opt-in:** `VICTORY_TRIM_BELOW_MB=<MiB>` is the only switch; unset/`0`/invalid = off (`below_from(None) == 0`). Off path starts no thread, journals nothing, hooks early-return. README marks it experimental/off. - **OFF-path `WRITE_COMBINED_RANGES` leak:** the journal moved out of `watch.rs` into `trim.rs`, written only while armed, and pruned on `title_memory_freed` (MEM_RELEASE drops all ranges under the base; decommit cuts intersecting ranges) — tested. - **Lock scope:** `OPEN_LOCK.read()` is never held across `VirtualQuery`/`VirtualUnlock`/`Sleep`; a bounded batch (≤64 × 16 MiB) is snapshotted under a short syscall-free read hold, then syscalls run outside it with per-chunk re-query (still committed `MEM_PRIVATE`, not guard/WC/no-access) and re-check against current `TITLE`/`OPENED`/journal state. The unavoidable query→call window is documented (`# TOCTOU`); chunking/batching are pure tested helpers. - **`inplace::From::Section`:** documented (a section heap is a `MEM_MAPPED` view; the scan never trims mapped memory) instead of a silent skip; `From::Region/Part` still recorded. - Removed `VICTORY_TRIM_GENTLE=0`/`K32EmptyWorkingSet` (emptied the whole process working set, not only title-private memory). Title-private-only rule, image/mapped/heap/guard/WC exclusions, 16 MiB paced unlock, 30 s/2 GiB gating kept. ## Verification - `cargo test -p victory-gpu --lib`: **724 passed, 0 failed, 21 ignored** (+8 trim tests: default-off/no-thread, parsing, gating, classification, subtraction/join, journal prune, 16 MiB chunk + batch bound, allocation/exclusion gate); `--all-features` 801 passed; `cargo check -p victory`; policy 443 files; `cargo test -p xtask` 9; clippy `-D warnings` clean. - **Still needed:** constrained-RAM in-game run (≤20 GiB host, real pressure) — verify free RAM actually rises (`working_set_before/5s_after` log), no multi-second streaming stalls, no soft-fault hitching, no per-frame regressions. `VirtualUnlock` on non-`VirtualLock`ed pages is not guaranteed to drop them on every Windows build, so a run must show the working set moving. - Residual TOCTOU in the query→call window (documented); worst case one chunk unlocked once on memory that was title-private when checked — `VirtualUnlock` only makes pages pageable, contents are not discarded. Supersedes #113 (closed); branch force-pushed `2f7116f3`.
Rework of #112 after review, on current main. The feature stays an
experiment: strictly off unless `VICTORY_TRIM_BELOW_MB=<MiB>` is given,
and it still needs an in-game run on a constrained-RAM host before it can
be trusted.

Maintainer points addressed:

- Strictly OFF by default. `VICTORY_TRIM_BELOW_MB=<MiB>` is the only
  switch: unset, `0`, or a value that is not a number means off, and
  nothing arms itself from the host's RAM size. Off means no thread, no
  per-pass work, and no journals (`watch`/`inplace` hooks early-return).
- OFF-path leak fixed: the write-combined range journal moved from
  `watch.rs` into `trim.rs` and is only written while the switch is armed;
  `title_memory_freed` prunes it (every range under a released allocation
  by base when `len == 0`, every intersecting range cut back on decommit),
  so it cannot grow for the run. Unit-tested (`prune_written`, the
  accounting count, and the whole-allocation vs sub-range cases).
- Lock scope: `OPEN_LOCK.read()` is never held across `VirtualQuery`,
  `VirtualUnlock`, or `Sleep` any more. A pass walks the address space with
  `VirtualQuery` outside the lock, checks the exclusions of one bounded
  batch (at most 64 x 16 MiB of candidates) under a short read-lock hold,
  drops it, and only then does the syscalls. Each 16 MiB chunk is
  re-queried (still committed `MEM_PRIVATE`, not guard/write-combined/
  no-access) and re-checked against the current `TITLE`/`OPENED`/
  write-combined state immediately before its `VirtualUnlock`; when
  `STATE_GEN` moved the snapshot is refreshed, and the unavoidable
  query-to-call window is documented in `trim.rs`.
- `inplace.rs` `From::Section(_)`: documented why nothing is recorded (a
  section heap is a file-mapping view, `MEM_MAPPED`, which the scan never
  trims) instead of leaving a bare skip.
- Dropped `VICTORY_TRIM_GENTLE=0` / `K32EmptyWorkingSet`: it emptied the
  whole process working set (runtime heaps, images included), not only
  title-private committed memory as the feature promises.

Kept: only title-private committed memory; images, mapped sections,
in-place heaps, guard pages and write-combined ranges excluded; 16 MiB
paced `VirtualUnlock` chunks; 30 s / 2 GiB growth gating; `=0` disables.

Verification (private target dir): `cargo test -p victory-gpu --lib`
724 passed, 0 failed, 21 ignored (8 new `trim` tests: default off and no
thread started, switch parsing, gating, region classification,
subtraction/join, journal prune/accounting, 16 MiB chunking and batch
bounding, allocation/exclusion gate); `--all-features` 801 passed;
`cargo check -p victory`, `cargo xtask policy`, `cargo test -p xtask`,
clippy `-p victory-gpu --all-targets` with and without `--all-features`
(`-D warnings`): clean; `git diff --check`: clean. README documents the
switch as experimental and off.

Still needed: an in-game run on a host with <= 20 GiB RAM under real
memory pressure -- free RAM must rise, no multi-second trimming stalls
during streaming, and no hitching from soft-faulting the cold pages back.

Ported from MiniVI dc8197ef (crates/victory-gpu/src/trim.rs; adapted
there from LinkoArts/VIctory 7839a18/2fa2113, GPL-3.0).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 18:30:09 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/160/head:github-bot/gh-159'
git switch 'github-bot/gh-159'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-159'
git switch 'github-bot/gh-159'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-159'
git switch 'github-bot/gh-159'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-159'
git switch 'main'
git merge --squash 'github-bot/gh-159'
git switch 'main'
git merge --ff-only 'github-bot/gh-159'
git switch 'main'
git merge 'github-bot/gh-159'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.