hle: track title working-set memory, bound xasync threads, and map XUser devices #687

Open
wbkepler wants to merge 2 commits from upstream-pr/687 into main
wbkepler commented 2026-11-24 06:54:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T06:54:07Z.


Summary

  • xmem.rs (XMemGetWorkingSetStatistics):
    • Tracks title-owned memory separately from host allocations (heap via XMemAllocDefault/XMemFreeDefault, non-placeholder committed XMemVirtualAlloc regions via VirtualQueryEx, and physical pages with GPU-optimal breakdown) so host D3D12 mirrors and shader compiler RAM do not trigger premature title asset eviction/OOM.
  • xasync.rs:
    • Replaces per-callback delayed-task OS thread spawning with a single lazily started shared timer thread (BinaryHeap<Reverse<DelayedTask>> + Condvar) and bounds the POOL worker count.
  • xuser.rs (XUserFindUserByLocalId / find_for_device):
    • Maps non-null, non-zero 32-byte APP_LOCAL_DEVICE_ID handles to the offline local user, returning USER_NOT_FOUND for all-zero IDs and E_INVALIDARG for null pointers.

Verification

  • cargo clippy --workspace --all-targets — passed (zero new warnings)
  • cargo clippy -p victory --features diagnostics --all-targets — passed
  • cargo test --workspace — passed (including 4 new unit tests in xmem/xasync/xuser)
  • cargo xtask policy — passed (151 files ok)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6576378646 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/687). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T06:54:07Z. --- ## Summary - **`xmem.rs` (`XMemGetWorkingSetStatistics`)**: - Tracks title-owned memory separately from host allocations (heap via `XMemAllocDefault`/`XMemFreeDefault`, non-placeholder committed `XMemVirtualAlloc` regions via `VirtualQueryEx`, and physical pages with GPU-optimal breakdown) so host D3D12 mirrors and shader compiler RAM do not trigger premature title asset eviction/OOM. - **`xasync.rs`**: - Replaces per-callback delayed-task OS thread spawning with a single lazily started shared timer thread (`BinaryHeap<Reverse<DelayedTask>>` + `Condvar`) and bounds the `POOL` worker count. - **`xuser.rs` (`XUserFindUserByLocalId` / `find_for_device`)**: - Maps non-null, non-zero 32-byte `APP_LOCAL_DEVICE_ID` handles to the offline local user, returning `USER_NOT_FOUND` for all-zero IDs and `E_INVALIDARG` for null pointers. ## Verification - `cargo clippy --workspace --all-targets` — passed (zero new warnings) - `cargo clippy -p victory --features diagnostics --all-targets` — passed - `cargo test --workspace` — passed (including 4 new unit tests in `xmem`/`xasync`/`xuser`) - `cargo xtask policy` — passed (151 files ok)
catminou4 commented 2026-11-24 07:01:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T06:56:51Z.

Review: COMMENTED


Static review only (victory-hle is Windows-only; I can't compile or run the new tests on macOS):

Scope is large but coherent — three related hardening changes:

  • xmem working-set statistics: tracks heap blocks and non-placeholder XMemVirtualAlloc regions separately, queries committed pages via VirtualQueryEx at report time, uses saturating arithmetic throughout, and cleans stale regions on overlap/reuse. The AllocationBase == base check in virtual_used correctly scopes commits to the reservation. Excluding host allocations (GPU mirrors, shader compiler) from title working-set stats is the right call.
  • xasync bounded pool + shared timer: replacing thread-per-delayed-callback with one timer thread and capping pool workers at available_parallelism is the right shape for a console runtime (XAsync callbacks must not exhaust OS threads). The new tests cover deadline ordering and burst capping.
  • xuser find_for_device: small, with a test.

Points a Windows run should confirm (I can't):

  1. XAsync callback ordering guarantees the title relies on — the shared timer fires delayed callbacks in deadline order, but if the pool reorders same-deadline callbacks vs. the old per-thread behavior, a title could observe a change.
  2. default_max_workers on the test machine (16 threads?) — the cap test asserts growth is bounded, but actual throughput under a real title burst is only observable on Windows.
  3. VIRTUAL_REGIONS grows monotonically between cleanups — fine for a session, but a title that allocates/frees millions of tiny regions could make virtual_used O(regions × regions-in-range); the overlap sweep on insert bounds it in practice.

No correctness objections from the static read.

<!-- greyfinch-sync:Marvisak/VIctory:review:6034943704 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/687#pullrequestreview-6034943704). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T06:56:51Z. **Review:** COMMENTED --- Static review only (victory-hle is Windows-only; I can't compile or run the new tests on macOS): Scope is large but coherent — three related hardening changes: - **xmem working-set statistics**: tracks heap blocks and non-placeholder `XMemVirtualAlloc` regions separately, queries committed pages via `VirtualQueryEx` at report time, uses saturating arithmetic throughout, and cleans stale regions on overlap/reuse. The `AllocationBase == base` check in `virtual_used` correctly scopes commits to the reservation. Excluding host allocations (GPU mirrors, shader compiler) from title working-set stats is the right call. - **xasync bounded pool + shared timer**: replacing thread-per-delayed-callback with one timer thread and capping pool workers at `available_parallelism` is the right shape for a console runtime (XAsync callbacks must not exhaust OS threads). The new tests cover deadline ordering and burst capping. - **xuser `find_for_device`**: small, with a test. Points a Windows run should confirm (I can't): 1. XAsync callback **ordering** guarantees the title relies on — the shared timer fires delayed callbacks in deadline order, but if the pool reorders same-deadline callbacks vs. the old per-thread behavior, a title could observe a change. 2. `default_max_workers` on the test machine (16 threads?) — the cap test asserts growth is bounded, but actual throughput under a real title burst is only observable on Windows. 3. `VIRTUAL_REGIONS` grows monotonically between cleanups — fine for a session, but a title that allocates/frees millions of tiny regions could make `virtual_used` O(regions × regions-in-range); the overlap sweep on insert bounds it in practice. No correctness objections from the static read.
LAKSHYA-cmd-tech commented 2026-11-24 07:07:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T07:04:49Z.

Review: APPROVED


LGTM! Tested on Windows 11 with MSVC Rust 1.98.1. All 90 unit tests in ictory-hle and
o_dll_planting passed cleanly. Tracking title memory separately from host/D3D12 mirrors in XMemGetWorkingSetStatistics prevents premature title asset eviction, and the bounded thread pool with single lazy timer thread reduces OS thread contention.

<!-- greyfinch-sync:Marvisak/VIctory:review:6035017685 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/687#pullrequestreview-6035017685). Originally posted by [LAKSHYA-cmd-tech](https://github.com/LAKSHYA-cmd-tech) on 2026-11-24T07:04:49Z. **Review:** APPROVED --- LGTM! Tested on Windows 11 with MSVC Rust 1.98.1. All 90 unit tests in ictory-hle and o_dll_planting passed cleanly. Tracking title memory separately from host/D3D12 mirrors in XMemGetWorkingSetStatistics prevents premature title asset eviction, and the bounded thread pool with single lazy timer thread reduces OS thread contention.
geiyiliyabumo commented 2026-11-24 10:03:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T09:42:55Z.


Independent verification of b7d3263d1 merged with main (8e79c80, clean merge) on Windows 11 Pro (26100), Rust 1.98.1 (x86_64-pc-windows-msvc): PASS (with two small edge-case notes below).

Verification

  • cargo xtask policy → asset policy: 153 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings → pass (0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings → pass (0 warnings)
  • cargo test -p victory-hle --locked → 98 passed + 1 integration test (no_dll_planting), 0 failed (all 4 new tests in xmem, xasync, and xuser pass)
  • Full cargo test --workspace --locked passed (frame::tests::waits_end_on_ticks_and_count_frames hit the known CPU-load timing flake once under concurrent compilation and passed in 0.03s on re-run).

Code-review notes

  1. Stale-region address reuse in xmem.rs (XMemVirtualAlloc, already_tracked): if a previously tracked reservation [rbase, rbase + rlen) is freed via VirtualFree and a subsequent XMemVirtualAlloc lands at a sub-address inside [rbase, rbase + rlen) before XMemGetWorkingSetStatistics runs to prune stale entries, already_tracked matches the stale range without checking mbi.AllocationBase == rbase and skips record_virtual_region. When virtual_used() later runs and sees mbi.AllocationBase != rbase, the stale entry is pruned and the new allocation is never tracked. Checking VirtualQueryEx's AllocationBase (or only skipping when base is non-null, i.e., a commit inside an existing reservation) avoids that gap.
  2. Delayed callbacks on terminated queues (xasync.rs, Timer::run): when item.queue.terminated is set before due, Timer::run skips deliver and drops item.task once due expires rather than running it with canceled = true (and retains Arc<Queue> in TIMER.heap until due).

AI-assisted; synthetic tests only, no game files.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7127788322 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/687#issuecomment-7127788322). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T09:42:55Z. --- Independent verification of `b7d3263d1` merged with `main` (`8e79c80`, clean merge) on Windows 11 Pro (`26100`), Rust `1.98.1` (`x86_64-pc-windows-msvc`): **PASS** (with two small edge-case notes below). ### Verification - `cargo xtask policy` → `asset policy: 153 files ok` - `cargo clippy --workspace --all-targets --locked -- -D warnings` → **pass** (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` → **pass** (0 warnings) - `cargo test -p victory-hle --locked` → **98 passed + 1 integration test (`no_dll_planting`), 0 failed** (all 4 new tests in `xmem`, `xasync`, and `xuser` pass) - Full `cargo test --workspace --locked` passed (`frame::tests::waits_end_on_ticks_and_count_frames` hit the known CPU-load timing flake once under concurrent compilation and passed in `0.03s` on re-run). ### Code-review notes 1. **Stale-region address reuse in `xmem.rs` (`XMemVirtualAlloc`, `already_tracked`)**: if a previously tracked reservation `[rbase, rbase + rlen)` is freed via `VirtualFree` and a subsequent `XMemVirtualAlloc` lands at a sub-address inside `[rbase, rbase + rlen)` *before* `XMemGetWorkingSetStatistics` runs to prune stale entries, `already_tracked` matches the stale range without checking `mbi.AllocationBase == rbase` and skips `record_virtual_region`. When `virtual_used()` later runs and sees `mbi.AllocationBase != rbase`, the stale entry is pruned and the new allocation is never tracked. Checking `VirtualQueryEx`'s `AllocationBase` (or only skipping when `base` is non-null, i.e., a commit inside an existing reservation) avoids that gap. 2. **Delayed callbacks on terminated queues (`xasync.rs`, `Timer::run`)**: when `item.queue.terminated` is set before `due`, `Timer::run` skips `deliver` and drops `item.task` once `due` expires rather than running it with `canceled = true` (and retains `Arc<Queue>` in `TIMER.heap` until `due`). AI-assisted; synthetic tests only, no game files.
Marvisak commented 2026-11-25 01:08:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:34:53Z.


Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-hle/src/modules/xasync.rs, crates/victory-hle/src/modules/xmem.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148660439 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/687#issuecomment-7148660439). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:34:53Z. --- <!-- victory-triage v1 head=73eba29bfaea2def15cb270711389bf80f464247 verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-hle/src/modules/xasync.rs`, `crates/victory-hle/src/modules/xmem.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/687:upstream-pr/687'
git switch 'upstream-pr/687'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/687'
git switch 'upstream-pr/687'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/687'
git switch 'upstream-pr/687'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/687'
git switch 'main'
git merge --squash 'upstream-pr/687'
git switch 'main'
git merge --ff-only 'upstream-pr/687'
git switch 'main'
git merge 'upstream-pr/687'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!687
No description provided.