perf(gpu/mem): add safe title-private working-set trimming via VICTORY_TRIM_BELOW_MB (#276) #384

Manually merged
github merged 3 commits from github-bot/gh-384 into gh-pr/384/base 2026-12-03 11:32:56 +00:00 AGit
Owner

@⁠Androtim on GitHub · 2026-12-03 10:43 UTC · original
Androtim:perf/safe-working-set-trim → main · +554 −3 · 6 files · 3 commits · merged 2026-12-03 11:31 UTC

Summary

Adds safe, paced working-set trimming of cold title-private RAM pages when host physical RAM runs low (VICTORY_TRIM_BELOW_MB), targeting 16 GiB RAM systems without risking GPU device loss (0x887A0005).

Adapted from GPL-3.0 LinkoArts/VIctory commits 7839a18 and 2fa2113 (originally derived from tshadofr/Leila patches 0012 and 0055), with two clean refinements:

  • Computes in_place range subtraction once per region instead of twice.
  • Uses checked base.checked_add(len) bounds, half-open at..end removal on sub-decommits, and allocation_base(at) normalization in opened_whole.
  • Auto-enables at 1536 MiB free-RAM threshold on <= 20 GiB total RAM hosts when VICTORY_TRIM_BELOW_MB is unset, while remaining disabled (0) on > 20 GiB hosts unless explicitly set.

Safety Invariants

  • Tracks title allocations (watch::title_memory_allocated / title_memory_freed), D3D12 in-place host heaps (inplace::open), and write-combined ranges (shadow::write_combined_ranges).
  • Gentle mode (default, VICTORY_TRIM_GENTLE=1) trims strictly MEM_COMMIT + MEM_PRIVATE pages belonging to tracked title allocations after subtracting all in-place heaps and write-combined ranges, and never touches MEM_IMAGE, MEM_MAPPED, PAGE_WRITECOMBINE, PAGE_GUARD, PAGE_NOACCESS, runtime heaps, stacks, or driver allocations.
  • Rate-limited: checks every 2s, trims at most once per 30s, and requires +2048 MiB working-set growth since the previous settled trim.

Verification

  • cargo test -p victory-gpu --lib trim::tests (4 passed)
  • cargo clippy -p victory-gpu --all-targets -- -D warnings (clean)
  • Gate 1: GitHub CI check
  • Gate 2: Read-only Reasoning Audit (#282)
  • Gate 3: Live RTX 4060 Ti gameplay test with active trim trigger (VICTORY_TRIM_BELOW_MB=16000)

Related: #276

**@⁠Androtim** on GitHub · 2026-12-03 10:43 UTC · [original](https://github.com/Androtim/MiniVI/pull/384) `Androtim:perf/safe-working-set-trim` → `main` · +554 −3 · 6 files · 3 commits · merged 2026-12-03 11:31 UTC ## Summary Adds safe, paced working-set trimming of cold title-private RAM pages when host physical RAM runs low (`VICTORY_TRIM_BELOW_MB`), targeting 16 GiB RAM systems without risking GPU device loss (`0x887A0005`). Adapted from GPL-3.0 `LinkoArts/VIctory` commits `7839a18` and `2fa2113` (originally derived from `tshadofr/Leila` patches `0012` and `0055`), with two clean refinements: - Computes `in_place` range subtraction once per region instead of twice. - Uses checked `base.checked_add(len)` bounds, half-open `at..end` removal on sub-decommits, and `allocation_base(at)` normalization in `opened_whole`. - Auto-enables at `1536 MiB` free-RAM threshold on `<= 20 GiB` total RAM hosts when `VICTORY_TRIM_BELOW_MB` is unset, while remaining disabled (`0`) on `> 20 GiB` hosts unless explicitly set. ## Safety Invariants - Tracks title allocations (`watch::title_memory_allocated` / `title_memory_freed`), D3D12 in-place host heaps (`inplace::open`), and write-combined ranges (`shadow::write_combined_ranges`). - Gentle mode (default, `VICTORY_TRIM_GENTLE=1`) trims strictly `MEM_COMMIT` + `MEM_PRIVATE` pages belonging to tracked title allocations after subtracting all in-place heaps and write-combined ranges, and never touches `MEM_IMAGE`, `MEM_MAPPED`, `PAGE_WRITECOMBINE`, `PAGE_GUARD`, `PAGE_NOACCESS`, runtime heaps, stacks, or driver allocations. - Rate-limited: checks every `2s`, trims at most once per `30s`, and requires `+2048 MiB` working-set growth since the previous settled trim. ## Verification - [x] `cargo test -p victory-gpu --lib trim::tests` (`4 passed`) - [x] `cargo clippy -p victory-gpu --all-targets -- -D warnings` (clean) - [ ] Gate 1: GitHub CI `check` - [ ] Gate 2: Read-only Reasoning Audit (`#282`) - [ ] Gate 3: Live RTX 4060 Ti gameplay test with active trim trigger (`VICTORY_TRIM_BELOW_MB=16000`) Related: #276
Adapts LinkoArts/VIctory commits 7839a18 and 2fa2113 (GPL-3.0; originally from tshadofr/Leila patches 0012 and 0055).

- Adds paced 16 MiB VirtualUnlock trimming strictly scoped to committed title-private allocations, subtracting all D3D12 in-place host heaps (whole and partial) and write-combined title ranges while leaving images, mapped sections, guard pages, runtime heaps, and driver allocations untouched.
- Explicit VICTORY_TRIM_BELOW_MB=<MB> sets threshold (0 disables). Unset auto-enables at 1536 MiB on <= 20 GiB RAM hosts and stays disabled on > 20 GiB hosts.
- VICTORY_TRIM_GENTLE=0 opts into single-call K32EmptyWorkingSet.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-03 10:47:54 +00:00
Addresses Gate 2 Reasoning Audit findings:
- [P1] Serialize inplace::open (OPEN_LOCK write) against per-chunk VirtualUnlock (OPEN_LOCK read) and re-verify STATE_GEN + live VirtualQuery + TITLE/OPENED/WRITE_COMBINED immediately prior to every 16 MiB VirtualUnlock call.
- [P2] Keep AllocationBase in TITLE on partial MEM_DECOMMIT (len > 0); only remove on MEM_RELEASE (len == 0).
- [P2] Split surviving sub-ranges in shadow::write_combined_freed on partial decommits and bump STATE_GEN on WC updates.
Author
Owner

@⁠Androtim on GitHub · 2026-12-03 11:05 UTC · original

3-Gate Verification Summary (4c9bde6)

  • Gate 1 (GitHub CI check): PASS (4m06s)
  • Gate 2 (Deep Reasoning Audit, #282 Pass #12): VERIFIED CLEAN (0 P0, 0 P1) — [P1] scan-to-unlock TOCTOU race eliminated via OPEN_LOCK + STATE_GEN + per-chunk VirtualQuery re-verification (5fe71eb), and shadow::write_combined_regions contract preserved (4c9bde6).
  • Gate 3 (Live RTX 4060 Ti 1440p DLSS Quality gameplay with VICTORY_TRIM_BELOW_MB=24000): PASS — 3 active gentle VirtualUnlock trim passes mid-gameplay, 0 DRED, 0 0x887A0005, peak dedicated VRAM 5529 / 6736 MiB, crisp visuals.

Ready for merge!

**@⁠Androtim** on GitHub · 2026-12-03 11:05 UTC · [original](https://github.com/Androtim/MiniVI/pull/384#issuecomment-7376197994) ### 3-Gate Verification Summary (`4c9bde6`) - **Gate 1 (GitHub CI `check`):** `PASS` (`4m06s`) - **Gate 2 (Deep Reasoning Audit, #282 Pass #12):** `VERIFIED CLEAN (0 P0, 0 P1)` — `[P1]` scan-to-unlock TOCTOU race eliminated via `OPEN_LOCK` + `STATE_GEN` + per-chunk `VirtualQuery` re-verification (`5fe71eb`), and `shadow::write_combined_regions` contract preserved (`4c9bde6`). - **Gate 3 (Live RTX 4060 Ti 1440p DLSS Quality gameplay with `VICTORY_TRIM_BELOW_MB=24000`):** `PASS` — 3 active gentle `VirtualUnlock` trim passes mid-gameplay, `0` DRED, `0` `0x887A0005`, peak dedicated VRAM `5529 / 6736 MiB`, crisp visuals. Ready for merge!
github-bot changed target branch from main to gh-pr/384/base 2026-12-03 11:32:54 +00:00
github manually merged commit 4c9bde612c into gh-pr/384/base 2026-12-03 11:32:56 +00:00
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/Androtim--MiniVI!384
No description provided.