dstorage: adaptive read-ahead map for streaming files + HUD telemetry #106

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

@⁠luniiya on GitHub · 2026-12-01 19:52 UTC · original
luniiya:read-ahead-map → main · +726 −16 · 9 files · 1 commits · open

What

Adds a learned, adaptive DirectStorage read-ahead map (crates/victory-hle/src/modules/read_map.rs) plus live HUD telemetry (Read map and Map cost rows in panel.rs):

  • Learns what the title streams automatically (nothing hand-picked): records every DirectStorage read in 1 MiB pieces — first-read order, bytes read per piece, session use count, age, and read-burst groups (bursts with pauses ≤ 1.5 s, up to 256 pieces).
  • Prefetches into the OS page cache while the title is quiet (no title read in flight for ≥ 40 ms), in sorted disk-offset sweeps across three priority tiers:
    1. The rest of any known burst group the title just entered (capped at the 8 most recent groups so fast travel drops stale areas),
    2. Ahead on the previous session's path (LEAD = 1536 pieces),
    3. Highest-value pieces ranked by recency-decayed expected value (uses × bytes × 16) / (16 + age × 8), skipping one-off sparse reads (< 64 KiB, 1 session).
  • Low recording overhead:
    • Background worker caches filenames locally and tracks prefetched bytes via an atomic (zero mutex locking during disk I/O).
    • Periodic saves (every 180 s, off the game's disk at ~/.cache/victory/read-map.txt in run.sh) snapshot compact state under the lock and release it before sorting/formatting, so DirectStorage reads never stall.
    • Optional shipped starter map fallback (VICTORY_READ_MAP_DEFAULT, tools/read-map-share.sh strips non-G:\ paths; stores only title paths and 1 MiB piece indices, no game bytes).
  • HUD telemetry: adds Read map (GB prefetched + first-read hit %) and Map cost (avg hit ms / avg miss ms + background I/O seconds) to the F3 HUD, plus xmem HW-RT / SW-RT in the top badge.
  • Knobs: VICTORY_READ_MAP= (empty disables), VICTORY_PREFETCH_MB (default 40960 MiB).

Why

When the game files live on a rotating HDD (or slower storage), DirectStorage issues thousands of small scattered reads while streaming the world (~15–45 ms each). Because the OS page cache starts cold (or only holds a few GB), game startup, save loads, and driving through the world stall waiting on disk seeks.

Reading the learned pieces ahead in sorted batches while the title is quiet turns scattered 15–45 ms HDD reads into ~0.01–0.04 ms page-cache hits without storing any extra copies in process RAM (it lives in evictable OS page cache).

Results

Measured on a 2 TB rotating SMR HDD (ST2000DM008) with 125 GB system RAM, RX 7800 XT 16 GB (ReBAR on), Ryzen 7 5800X, Arch/CachyOS, GE-Proton11-14:

  • Save load / start time: game opens and loads into the world in under 10 seconds (save load < 2 s once warm).
  • Read latency (read map: last minute logs & HUD):
    • Prefetched first reads: 0.01–0.04 ms avg (50–61%+ hit rate, first_reads_known_not_yet = 0 after prefetch completes).
    • Unseen (new area) HDD reads: 14–44 ms avg.
    • Example live HUD reading mid-session: Read map: 34.5 GB (61% hit), Map cost: 0.04/0ms 46s bg.
    • Steady-state map after several sessions: ~56k pieces / ~890 groups (~34.5 GB prefetched in ~46–143 s of quiet background sweeps).

Platform

Works on both Linux/Wine and Windows (uses CreateFileW + FileExt::seek_read to warm the OS file cache, and lock-free atomics for the HUD). Disabled cleanly if VICTORY_READ_MAP is unset or empty.

How this was tested

  • build.sh check passes cleanly with zero warnings on main (37bb30f).
  • Tested in multi-hour play sessions on local-stack (main + PRs #31, #34, #35, #43, #44, #45, #47, #48, #58, #63, #67, #75, #93, #100, #101) with VICTORY_OUTPUT=2560x1440, VICTORY_BVH_HARDWARE=1, VICTORY_UI_PLANE=3840x2160.
**@⁠luniiya** on GitHub · 2026-12-01 19:52 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/106) `luniiya:read-ahead-map` → `main` · +726 −16 · 9 files · 1 commits · open ## What Adds a learned, adaptive DirectStorage read-ahead map (`crates/victory-hle/src/modules/read_map.rs`) plus live HUD telemetry (`Read map` and `Map cost` rows in `panel.rs`): - **Learns what the title streams automatically** (nothing hand-picked): records every DirectStorage read in 1 MiB pieces — first-read order, bytes read per piece, session use count, age, and read-burst groups (bursts with pauses ≤ 1.5 s, up to 256 pieces). - **Prefetches into the OS page cache while the title is quiet** (no title read in flight for ≥ 40 ms), in sorted disk-offset sweeps across three priority tiers: 1. The rest of any known burst group the title just entered (capped at the 8 most recent groups so fast travel drops stale areas), 2. Ahead on the previous session's path (`LEAD = 1536` pieces), 3. Highest-value pieces ranked by recency-decayed expected value `(uses × bytes × 16) / (16 + age × 8)`, skipping one-off sparse reads (`< 64 KiB`, 1 session). - **Low recording overhead**: - Background worker caches filenames locally and tracks prefetched bytes via an atomic (zero mutex locking during disk I/O). - Periodic saves (every 180 s, off the game's disk at `~/.cache/victory/read-map.txt` in `run.sh`) snapshot compact state under the lock and release it before sorting/formatting, so DirectStorage reads never stall. - Optional shipped starter map fallback (`VICTORY_READ_MAP_DEFAULT`, `tools/read-map-share.sh` strips non-`G:\` paths; stores only title paths and 1 MiB piece indices, no game bytes). - **HUD telemetry**: adds `Read map` (GB prefetched + first-read hit %) and `Map cost` (avg hit ms / avg miss ms + background I/O seconds) to the F3 HUD, plus `xmem HW-RT` / `SW-RT` in the top badge. - **Knobs**: `VICTORY_READ_MAP=` (empty disables), `VICTORY_PREFETCH_MB` (default `40960` MiB). ## Why When the game files live on a rotating HDD (or slower storage), DirectStorage issues thousands of small scattered reads while streaming the world (~15–45 ms each). Because the OS page cache starts cold (or only holds a few GB), game startup, save loads, and driving through the world stall waiting on disk seeks. Reading the learned pieces ahead in sorted batches while the title is quiet turns scattered 15–45 ms HDD reads into ~0.01–0.04 ms page-cache hits without storing any extra copies in process RAM (it lives in evictable OS page cache). ## Results Measured on a 2 TB rotating SMR HDD (ST2000DM008) with 125 GB system RAM, RX 7800 XT 16 GB (ReBAR on), Ryzen 7 5800X, Arch/CachyOS, GE-Proton11-14: - **Save load / start time**: game opens and loads into the world in **under 10 seconds** (save load < 2 s once warm). - **Read latency (`read map: last minute` logs & HUD)**: - Prefetched first reads: **0.01–0.04 ms avg** (50–61%+ hit rate, `first_reads_known_not_yet = 0` after prefetch completes). - Unseen (new area) HDD reads: **14–44 ms avg**. - Example live HUD reading mid-session: `Read map: 34.5 GB (61% hit)`, `Map cost: 0.04/0ms 46s bg`. - Steady-state map after several sessions: ~56k pieces / ~890 groups (~34.5 GB prefetched in ~46–143 s of quiet background sweeps). ## Platform Works on both Linux/Wine and Windows (uses `CreateFileW` + `FileExt::seek_read` to warm the OS file cache, and lock-free atomics for the HUD). Disabled cleanly if `VICTORY_READ_MAP` is unset or empty. ## How this was tested - `build.sh check` passes cleanly with zero warnings on `main` (37bb30f). - Tested in multi-hour play sessions on `local-stack` (`main` + PRs #31, #34, #35, #43, #44, #45, #47, #48, #58, #63, #67, #75, #93, #100, #101) with `VICTORY_OUTPUT=2560x1440`, `VICTORY_BVH_HARDWARE=1`, `VICTORY_UI_PLANE=3840x2160`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 19:54:36 +00:00
github-bot force-pushed github-bot/gh-106 from 3fd95e9989 to 9d6f286520 2026-12-01 21:58:09 +00:00 Compare
github-bot force-pushed github-bot/gh-106 from 9d6f286520 to 13cab999db 2026-12-01 22:08:40 +00:00 Compare
github-bot force-pushed github-bot/gh-106 from 13cab999db to d676e937f8 2026-12-01 22:30:08 +00:00 Compare
Author
Owner

@⁠Luxuride on GitHub · 2026-12-04 12:03 UTC · original

This is the most speculative thing in this batch: a large heuristic cache that spends 40 GiB of I/O by default, with no tests and no way for a maintainer on Linux to run any of it.

  • On by default and effectively uncapped: run.sh:33 exports VICTORY_READ_MAP unconditionally, and crates/victory-hle/src/modules/read_map.rs:150 is .unwrap_or(40960) MiB. On the HDD case this is written for, that roughly doubles the reads the title waits behind, from a second handle and a competing thread.
  • run.sh:34 exports VICTORY_READ_MAP_DEFAULT=$(win "$root/config/read-map.txt") but no such file exists in the PR or on main (config/ has only native-compute.txt and native-keys.default.txt), so the "shipped map" tier is dead code until someone runs tools/read-map-share.sh.
  • Unrelated change in the same diff: the F3 panel's "xmem RT" tag becomes a host_bvh()-dependent "xmem HW-RT"/"xmem SW-RT" (main still has plain "xmem RT" at crates/victory-gpu/src/host/panel.rs:2167). That belongs in its own PR if it is wanted.
  • 580 lines with no #[cfg(test)] at all, and the nine HUD atomics are kept in release builds.

To make it reviewable: opt-in by default, a budget in the low GiB, ship or delete the default-map tier, pull the panel label out, and test next_batch / load-save round-trip / the score.

**@⁠Luxuride** on GitHub · 2026-12-04 12:03 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/106#issuecomment-7410914691) This is the most speculative thing in this batch: a large heuristic cache that spends 40 GiB of I/O by default, with no tests and no way for a maintainer on Linux to run any of it. - On by default and effectively uncapped: `run.sh:33` exports `VICTORY_READ_MAP` unconditionally, and `crates/victory-hle/src/modules/read_map.rs:150` is `.unwrap_or(40960)` MiB. On the HDD case this is written for, that roughly doubles the reads the title waits behind, from a second handle and a competing thread. - `run.sh:34` exports `VICTORY_READ_MAP_DEFAULT=$(win "$root/config/read-map.txt")` but no such file exists in the PR or on main (`config/` has only `native-compute.txt` and `native-keys.default.txt`), so the "shipped map" tier is dead code until someone runs `tools/read-map-share.sh`. - Unrelated change in the same diff: the F3 panel's `"xmem RT"` tag becomes a `host_bvh()`-dependent "xmem HW-RT"/"xmem SW-RT" (main still has plain `"xmem RT"` at `crates/victory-gpu/src/host/panel.rs:2167`). That belongs in its own PR if it is wanted. - 580 lines with no `#[cfg(test)]` at all, and the nine HUD atomics are kept in release builds. To make it reviewable: opt-in by default, a budget in the low GiB, ship or delete the default-map tier, pull the panel label out, and test `next_batch` / load-save round-trip / the score.
This pull request has changes conflicting with the target branch.
  • README.md
  • crates/victory-hle/src/modules/dstorage.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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