gpu: trim the title's working set under RAM pressure (VICTORY_TRIM_BELOW_MB) #113

Closed
github-bot wants to merge 1 commit from github-bot/gh-112 into main AGit
Owner

@⁠SovietBearz0r on GitHub · 2026-12-05 12:40 UTC · original
SovietBearz0r:minivi-workingset-trim → main · +533 −2 · 5 files · 1 commits · closed without merging

Trim the title's own working set under RAM pressure: VICTORY_TRIM_BELOW_MB.

What

New crates/victory-gpu/src/trim.rs: a background thread, armed only when available physical memory drops below the threshold (VICTORY_TRIM_BELOW_MB; unset = 1536 MiB, auto-on for hosts with <= 20 GiB RAM, off above; 0 disables), trims only title-private committed memory — never images, mapped sections, other private memory, guard pages or write-combined ranges, never in-place heaps — in gentle 16 MiB paced VirtualUnlock chunks (or K32EmptyWorkingSet with VICTORY_TRIM_GENTLE=0), with TOCTOU re-verification and 30 s/growth gating. Wired into watch::title_memory_allocated/freed, the write-combined range journal, inplace::open (read-exclusion guard) and residency::refresh (thread start).

Verification

  • cargo test -p victory-gpu --lib: 716 passed (4 new: threshold parsing, pacing, region classification, in-place/WC subtraction); normal and diagnostics builds compile.
  • cargo check -p victory, cargo xtask policy, clippy: clean.
  • Needs in-game before trusting: on a <= 20 GiB host, check no multi-second trimming stalls during streaming, smooth soft-fault return of cold pages, and that free RAM actually rises. It is auto-off on larger hosts (this machine has 64 GB, so it did not run here).
  • Risk: memory-pressure code that pages out the title's cold data — worst case paging hitching, not corruption; the exclusions and per-chunk re-checks are the safety story.

Source: Androtim/MiniVI dc8197ef (+554, trim.rs), adapted to this tree's residency/watch/inplace hooks.

**@⁠SovietBearz0r** on GitHub · 2026-12-05 12:40 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/112) `SovietBearz0r:minivi-workingset-trim` → `main` · +533 −2 · 5 files · 1 commits · closed without merging Trim the title's own working set under RAM pressure: `VICTORY_TRIM_BELOW_MB`. ## What New `crates/victory-gpu/src/trim.rs`: a background thread, armed only when available physical memory drops below the threshold (`VICTORY_TRIM_BELOW_MB`; unset = 1536 MiB, auto-on for hosts with <= 20 GiB RAM, off above; `0` disables), trims only title-private committed memory — never images, mapped sections, other private memory, guard pages or write-combined ranges, never in-place heaps — in gentle 16 MiB paced `VirtualUnlock` chunks (or `K32EmptyWorkingSet` with `VICTORY_TRIM_GENTLE=0`), with TOCTOU re-verification and 30 s/growth gating. Wired into `watch::title_memory_allocated/freed`, the write-combined range journal, `inplace::open` (read-exclusion guard) and `residency::refresh` (thread start). ## Verification - `cargo test -p victory-gpu --lib`: 716 passed (4 new: threshold parsing, pacing, region classification, in-place/WC subtraction); normal and diagnostics builds compile. - `cargo check -p victory`, `cargo xtask policy`, clippy: clean. - **Needs in-game before trusting**: on a <= 20 GiB host, check no multi-second trimming stalls during streaming, smooth soft-fault return of cold pages, and that free RAM actually rises. It is auto-off on larger hosts (this machine has 64 GB, so it did not run here). - Risk: memory-pressure code that pages out the title's cold data — worst case paging hitching, not corruption; the exclusions and per-chunk re-checks are the safety story. Source: Androtim/MiniVI `dc8197ef` (+554, `trim.rs`), adapted to this tree's residency/watch/inplace hooks.
Ported from MiniVI dc8197ef (new crates/victory-gpu/src/trim.rs, 528
lines; its watch.rs/inplace.rs/residency.rs/lib.rs hooks; adapted there
from LinkoArts/VIctory 7839a18/2fa2113, GPL-3.0).

A background thread trims the title's own committed private memory from
the process working set, in paced 16 MiB VirtualUnlock chunks, while
available physical memory is under VICTORY_TRIM_BELOW_MB (unset: 1536 MiB
on hosts with at most 20 GiB of RAM, off above that; 0 disables), at most
every 30 s and only after the set grew by 2 GiB since the last trim
settled. Only allocations the runtime gave the title are considered
(watch::title_memory_allocated / title_memory_freed), minus every region
opened as a host D3D12 heap (inplace::open, whole or part) and minus
write-combined title ranges (watch::title_memory_write_combined now also
keeps the ranges); images, mapped sections, other private memory, guard
pages and write-combined memory are never touched. Every chunk is
re-verified under OPEN_LOCK.read() with a fresh VirtualQuery and a
generation bump, so a heap opened meanwhile cannot race it;
VICTORY_TRIM_GENTLE=0 uses one K32EmptyWorkingSet call instead.

Pressure comes from the same residency samples as the once-a-second
sweep. Threshold parsing, the trim pacing, the region classification and
the subtraction of in-place/write-combined parts are pure tests; the
actual trimming needs a live run under memory pressure (in-game A/B
needed before this is trusted: it can page the title's cold data, so it
is off on hosts above 20 GiB).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-05 12:44:49 +00:00
Author
Owner

@⁠ReTerryDavis on GitHub · 2026-12-05 17:37 UTC · original

Closing this one — VICTORY_TRIM_BELOW_MB is auto-ON on <= 20 GiB RAM hosts despite no in-game testing on a constrained-RAM machine, re-introduces working-set trimming (VirtualUnlock / K32EmptyWorkingSet) right after 8746753 reverted that approach, and has OFF-path issues:

  • Unconditional memory leak when OFF (watch.rs:584): title_memory_write_combined always locks WRITE_COMBINED_RANGES and pushes (at, end) forever without pruning on title_memory_freed, even on > 20 GiB hosts where trimming never runs.
  • trim_gently holds OPEN_LOCK.read() across VirtualQuery + 16 MiB VirtualUnlock syscalls, blocking inplace::open's write lock, and inplace.rs skips From::Section(_) heaps.
**@⁠ReTerryDavis** on GitHub · 2026-12-05 17:37 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/112#issuecomment-7442340364) Closing this one — `VICTORY_TRIM_BELOW_MB` is **auto-ON** on `<= 20 GiB` RAM hosts despite no in-game testing on a constrained-RAM machine, re-introduces working-set trimming (`VirtualUnlock` / `K32EmptyWorkingSet`) right after `8746753` reverted that approach, and has OFF-path issues: - **Unconditional memory leak when OFF (`watch.rs:584`):** `title_memory_write_combined` always locks `WRITE_COMBINED_RANGES` and pushes `(at, end)` forever without pruning on `title_memory_freed`, even on `> 20 GiB` hosts where trimming never runs. - `trim_gently` holds `OPEN_LOCK.read()` across `VirtualQuery` + 16 MiB `VirtualUnlock` syscalls, blocking `inplace::open`'s write lock, and `inplace.rs` skips `From::Section(_)` heaps.
github-bot closed this pull request 2026-12-05 17:43:25 +00:00
Author
Owner

@⁠SovietBearz0r on GitHub · 2026-12-05 18:27 UTC · original

Review addressed and resubmitted as #160 (I can't reopen this one: GitHub doesn't let the author reopen a PR closed by the maintainer). The new PR is rebased on current main and its body lists point-by-point what changed; closed this thread is fine, please use the new one.

**@⁠SovietBearz0r** on GitHub · 2026-12-05 18:27 UTC · [original](https://github.com/ReTerryDavis/ReVIctory/pull/112#issuecomment-7443146704) Review addressed and resubmitted as #160 (I can't reopen this one: GitHub doesn't let the author reopen a PR closed by the maintainer). The new PR is rebased on current main and its body lists point-by-point what changed; closed this thread is fine, please use the new one.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.