perf(residency): incremental background working-set trim with hysteresis and sustained-pressure gating #6

Manually merged
github merged 1 commit from github-bot/gh-6 into mega-upgrade-v3 2026-12-04 20:20:16 +00:00 AGit
Owner

@⁠MahanYTT on GitHub · 2026-12-04 17:40 UTC · original
MahanYTT:perf/memory-trim-freeze → mega-upgrade-v3 · +539 −131 · 1 files · 1 commits · merged 2026-12-04 20:18 UTC

Root Cause Analysis

Analysis of the 170-second gameplay log (RTX 5070 Ti 16 GB, 32,403 MB RAM, 49 GB pagefile) and crates/victory-gpu/src/residency.rs (mod trim) identified three compounding causes behind the 699–1,282 ms working-set trim stalls and cascading working-set re-faulting:

  1. Monolithic K32EmptyWorkingSet kernel lock contention & full working-set wipe:
    • Even on a separate thread, K32EmptyWorkingSet(GetCurrentProcess()) sweeps the entire process working set (7.7–13.0 GB, up to ~3.3M pages) in a single syscall, holding the Windows kernel working-set lock for 699–1,282 ms (L26033: 1,282 ms, 13,028 -> 2,019 MB; L31901: 1,120 ms, 10,440 -> 1,465 MB; L36541: 699 ms, 7,748 -> 1,421 MB), stalling active render/title threads (L26047: p95/longest 258.1 ms) and evicting the active working set down to ~1.4 GB so the next frames immediately soft/hard-fault it back in.
    • At L36541, evicting 6,327 MB of working set only freed 509 MB of physical RAM (3,205 -> 3,714 MB).
  2. Overly aggressive auto size threshold (40% of RAM):
    • set_line("auto") was (total / 5 * 2).clamp(6 GiB, 20 GiB) = 12,961 MB on 32 GB RAM, triggering L26033 at 13,028 MB even with ~5.8 GB free.
  3. No sustained-pressure filter, no recovery hysteresis, and short 30 s cooldown:
    • L31901 (3,183 MB free) and L36541 (3,205 MB free) fired on single-sample dips just 35–57 MB below the 3,240 MB line, repeating 55 seconds apart because the 30 s cooldown re-armed after freeing 892 MB (>= 512 MB).

Note on MakeResident / VICTORY_WAIT_RESIDENT & memguard.rs:

  • VICTORY_WAIT_RESIDENT (new card memory waited for until it is mapped: 20000 times (17 coalesced), 1798.2 ms) lives in crate::host (outside this PR's exclusive file scope of residency.rs and memguard.rs).
  • crates/victory-gpu/src/memguard.rs was audited and had 0 compile refusals in the 170 s log and no blocking calls; left untouched.

Summary of Changes (crates/victory-gpu/src/residency.rs)

  • Persistent non-blocking background worker (TrimWorker): Replaced per-trim thread spawning with a single lazily-initialized persistent "victory-trim" thread (OnceLock + Mutex/Condvar mailbox + AtomicBool in-flight guard) so trim::look is strictly non-blocking and coalesces concurrent triggers; short-circuits before calling K32GetProcessMemoryInfo when trimming is disabled.
  • Incremental chunked working-set trim (trim_regions): Replaced monolithic K32EmptyWorkingSet with a 2-phase incremental VirtualUnlock pass in 32 MiB chunks with 2 ms yields between chunks — clean file-mapped MEM_MAPPED regions (>= 1 MiB) first, then large MEM_PRIVATE regions (>= 4 MiB), stopping early via trim_goal_met once either target_working_set (75% of size line or 50% of WS floored at 4 GiB / min_ws) or recovery_free_target (line + 512 MiB) is reached. Retained K32EmptyWorkingSet strictly as a last-resort fallback during genuine emergency OOM (physical < line / 3) when incremental trimming cannot recover line / 2.
  • Improved thresholds, sustained-pressure gating, hysteresis & cooldowns (TrimGate):
    • Raised auto size line from 40% ([6, 20] GiB) to 50% ([8, 24] GiB) (16 GiB on 32 GB RAM).
    • Required 3 consecutive 1-second samples below line for non-emergency low-memory trims (emergency < line / 3 still fires immediately).
    • Added 512 MiB recovery hysteresis after low-memory trims so hovering near line cannot thrash unless free RAM genuinely recovers above line + 512 MiB or drops > 512 MiB deeper.
    • Raised cooldowns to 90 s (yield >= 1 GiB), 180 s (yield < 1 GiB or size-based trims), and 15 s (emergency).

Verification

  • Verified mod trim and all trim unit tests with clippy-driver --edition 2024 -D warnings (unsafe_op_in_unsafe_fn, unused_must_use, clippy::all, clippy::undocumented_unsafe_blocks): 0 warnings.
  • Added focused unit tests in crates/victory-gpu/src/residency.rs:
    • memory_is_short_below_a_tenth_of_the_pc_within_three_and_eight_gib (updated for 50% [8, 24] GiB auto line & 90s/180s cooldowns)
    • trim_gate_requires_sustained_shortage_and_hysteresis_before_retriggering
    • trim_gate_emergency_bypasses_streak_and_uses_short_cooldown
    • trim_targets_preserve_active_working_set_and_stop_on_recovery
    • trim_gate_enforces_size_cooldown_min_ws_hysteresis_and_deep_shortage_override
  • All 6 tests pass (0.00s). Windows CI will run the full workspace build/test suite.
**@⁠MahanYTT** on GitHub · 2026-12-04 17:40 UTC · [original](https://github.com/MahanYTT/VIctory-MA/pull/6) `MahanYTT:perf/memory-trim-freeze` → `mega-upgrade-v3` · +539 −131 · 1 files · 1 commits · merged 2026-12-04 20:18 UTC ## Root Cause Analysis Analysis of the 170-second gameplay log (`RTX 5070 Ti 16 GB`, `32,403 MB` RAM, `49 GB` pagefile) and `crates/victory-gpu/src/residency.rs` (`mod trim`) identified three compounding causes behind the **699–1,282 ms** working-set trim stalls and cascading working-set re-faulting: 1. **Monolithic `K32EmptyWorkingSet` kernel lock contention & full working-set wipe**: - Even on a separate thread, `K32EmptyWorkingSet(GetCurrentProcess())` sweeps the entire process working set (7.7–13.0 GB, up to ~3.3M pages) in a single syscall, holding the Windows kernel working-set lock for **699–1,282 ms** (`L26033`: 1,282 ms, `13,028 -> 2,019 MB`; `L31901`: 1,120 ms, `10,440 -> 1,465 MB`; `L36541`: 699 ms, `7,748 -> 1,421 MB`), stalling active render/title threads (`L26047`: `p95/longest 258.1 ms`) and evicting the active working set down to ~1.4 GB so the next frames immediately soft/hard-fault it back in. - At `L36541`, evicting **6,327 MB** of working set only freed **509 MB** of physical RAM (`3,205 -> 3,714 MB`). 2. **Overly aggressive `auto` size threshold (`40%` of RAM)**: - `set_line("auto")` was `(total / 5 * 2).clamp(6 GiB, 20 GiB)` = **12,961 MB** on 32 GB RAM, triggering `L26033` at `13,028 MB` even with ~5.8 GB free. 3. **No sustained-pressure filter, no recovery hysteresis, and short 30 s cooldown**: - `L31901` (`3,183 MB` free) and `L36541` (`3,205 MB` free) fired on single-sample dips just **35–57 MB** below the `3,240 MB` line, repeating 55 seconds apart because the 30 s cooldown re-armed after freeing 892 MB (`>= 512 MB`). > **Note on `MakeResident` / `VICTORY_WAIT_RESIDENT` & `memguard.rs`:** > - `VICTORY_WAIT_RESIDENT` (`new card memory waited for until it is mapped: 20000 times (17 coalesced), 1798.2 ms`) lives in `crate::host` (outside this PR's exclusive file scope of `residency.rs` and `memguard.rs`). > - `crates/victory-gpu/src/memguard.rs` was audited and had 0 compile refusals in the 170 s log and no blocking calls; left untouched. ## Summary of Changes (`crates/victory-gpu/src/residency.rs`) - **Persistent non-blocking background worker (`TrimWorker`)**: Replaced per-trim thread spawning with a single lazily-initialized persistent `"victory-trim"` thread (`OnceLock` + `Mutex`/`Condvar` mailbox + `AtomicBool` in-flight guard) so `trim::look` is strictly non-blocking and coalesces concurrent triggers; short-circuits before calling `K32GetProcessMemoryInfo` when trimming is disabled. - **Incremental chunked working-set trim (`trim_regions`)**: Replaced monolithic `K32EmptyWorkingSet` with a 2-phase incremental `VirtualUnlock` pass in `32 MiB` chunks with `2 ms` yields between chunks — clean file-mapped `MEM_MAPPED` regions (`>= 1 MiB`) first, then large `MEM_PRIVATE` regions (`>= 4 MiB`), stopping early via `trim_goal_met` once either `target_working_set` (75% of size line or 50% of WS floored at `4 GiB` / `min_ws`) or `recovery_free_target` (`line + 512 MiB`) is reached. Retained `K32EmptyWorkingSet` strictly as a last-resort fallback during genuine emergency OOM (`physical < line / 3`) when incremental trimming cannot recover `line / 2`. - **Improved thresholds, sustained-pressure gating, hysteresis & cooldowns (`TrimGate`)**: - Raised `auto` size line from 40% (`[6, 20]` GiB) to **50% (`[8, 24]` GiB)** (16 GiB on 32 GB RAM). - Required **3 consecutive 1-second samples** below `line` for non-emergency low-memory trims (emergency `< line / 3` still fires immediately). - Added **512 MiB recovery hysteresis** after low-memory trims so hovering near `line` cannot thrash unless free RAM genuinely recovers above `line + 512 MiB` or drops `> 512 MiB` deeper. - Raised cooldowns to **90 s** (yield >= 1 GiB), **180 s** (yield < 1 GiB or size-based trims), and **15 s** (emergency). ## Verification - Verified `mod trim` and all trim unit tests with `clippy-driver --edition 2024 -D warnings` (`unsafe_op_in_unsafe_fn`, `unused_must_use`, `clippy::all`, `clippy::undocumented_unsafe_blocks`): **0 warnings**. - Added focused unit tests in `crates/victory-gpu/src/residency.rs`: - `memory_is_short_below_a_tenth_of_the_pc_within_three_and_eight_gib` (updated for 50% `[8, 24]` GiB `auto` line & 90s/180s cooldowns) - `trim_gate_requires_sustained_shortage_and_hysteresis_before_retriggering` - `trim_gate_emergency_bypasses_streak_and_uses_short_cooldown` - `trim_targets_preserve_active_working_set_and_stop_on_recovery` - `trim_gate_enforces_size_cooldown_min_ws_hysteresis_and_deep_shortage_override` - All 6 tests pass (`0.00s`). Windows CI will run the full workspace build/test suite.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 17:44:40 +00:00
github manually merged commit 4ba58378c8 into mega-upgrade-v3 2026-12-04 20:20:16 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.