RAM throttle: keep the title's heartbeat during its own waits (opt-in, stacked on #96) #126

Open
github-bot wants to merge 3 commits from github-bot/gh-126 into main AGit
Owner

@⁠nidscode on GitHub · 2026-11-27 03:09 UTC · original
nidscode:perf/ram-keep-alive → main · +616 −1 · 8 files · 3 commits · open

What

VICTORY_RAM_KEEP_ALIVE=1, off by default and split out of #96 as you asked: while the RAM throttle from #96 holds a buffer or a submission back, it sets the title's heartbeat flag the way the title's own tick function does. Stacked on #96: the first two commits are #96's, and only the last one (RAM throttle: keep the title's heartbeat during its own waits) is new here.

Why

The throttle holds the title's submissions back (VICTORY_RAM_FRAME_WAIT_MS), and the main loop waits for them, so a world load it slows can go more than 30 s without a frame. The title's watchdog then ends it with a write to address 0 at +0x23cf683. On the 16 GB laptop that happened to 2 of 2 -Machine laptop loads (first-chance exception 0xc0000005 at …+0x23cf683 (write 0x0), with 588–679 MB of RAM free).

How it's bounded and checked

  • Only during the throttle's own waits. The two 10 ms wait loops in ram_gate and ram_gate_submission each set the flag once per step. Each wait is bounded by VICTORY_RAM_WAIT_MS or VICTORY_RAM_FRAME_WAIT_MS, so a hang anywhere else still meets the watchdog.

  • Build-checked, without borrowing the image. victory_loader::heartbeat::asked:

    1. checks that the tick function's 8 bytes (+0x2214a90) lie inside the image and in one committed, readable region (VirtualQuery; no guard page, no PAGE_NOACCESS);
    2. copies just those 8 bytes out with read_volatile and compares them with 1.0.2.1's C6 05 99 18 FA 04 01 C3 (mov byte [rip+disp32], 1; ret);
    3. takes the flag's address from that instruction's displacement (+0x71b6330);
    4. checks that the flag lies inside the image, in committed, writable memory.

    Another build, or memory that doesn't allow it, leaves the watchdog alone and logs why.

  • No safe API takes an address. victory_gpu::ram_keep_alive_at is unsafe fn(flag: *const AtomicU8), with the contract that the byte stays valid for writes for the rest of the process. run.rs calls it only with what asked returns. The title image is the prepared DLL, which is never unloaded.

  • Concurrent access. The throttle's threads store to the flag through an AtomicU8, so they never race each other. The title's own stores and its watchdog's read-and-clear are single-byte operations, which x86 doesn't tear. Whichever store lands last, the flag reads "ticked", or the watchdog clears it as on the console.

Tests

  • heartbeat::tests::the_heartbeat_flag_is_found_only_behind_the_known_tick_function
  • heartbeat::tests::a_region_is_checked_for_commit_access_and_bounds: committed read-write, read-only, a range across two regions, reserved but uncommitted, and an end past the address space.
  • victory-gpu 472 passed, twice in a row. One earlier run had watch::tests::what_the_kernel_writes_through_is_not_guarded_for_another_view fail, which looks timing-sensitive and is in code this doesn't touch.
  • Clippy: no warnings, with and without diagnostics. rustfmt is clean on the new file.

Measurements

Laptop: i7-13700H, RTX 3050 6 GB, 16 GB, -Machine laptop (#100), Series S, 240 s runs under a guard that stops the game below 400 MB of free RAM.

Runs Result
without this switch 2 both ended by the title's watchdog during the world load
with VICTORY_RAM_KEEP_ALIVE=1 (the first version, fc74f10) 2 both ran the full 240 s: 175 / 177 s in the world, 118 / 112 frames, median 1,471 / 1,455 ms
with VICTORY_RAM_KEEP_ALIVE=1, this version (5fca9d5, on main + #96 + #100) 2 neither met the watchdog, and both logged the heartbeat check passing. One ran the full 240 s (176 s in the world, 122 frames, median 1,366 ms). The other was stopped by my external guard for low RAM (332 MB free) 55 s into the world: a RAM dip, not the watchdog

🤖 Generated with Claude Code

**@⁠nidscode** on GitHub · 2026-11-27 03:09 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/126) `nidscode:perf/ram-keep-alive` → `main` · +616 −1 · 8 files · 3 commits · open ## What `VICTORY_RAM_KEEP_ALIVE=1`, off by default and split out of #96 as you asked: while the RAM throttle from #96 holds a buffer or a submission back, it sets the title's heartbeat flag the way the title's own tick function does. **Stacked on #96**: the first two commits are #96's, and only the last one (`RAM throttle: keep the title's heartbeat during its own waits`) is new here. ## Why The throttle holds the title's submissions back (`VICTORY_RAM_FRAME_WAIT_MS`), and the main loop waits for them, so a world load it slows can go more than 30 s without a frame. The title's watchdog then ends it with a write to address 0 at +0x23cf683. On the 16 GB laptop that happened to **2 of 2** `-Machine laptop` loads (`first-chance exception 0xc0000005 at …+0x23cf683 (write 0x0)`, with 588–679 MB of RAM free). ## How it's bounded and checked - **Only during the throttle's own waits.** The two 10 ms wait loops in `ram_gate` and `ram_gate_submission` each set the flag once per step. Each wait is bounded by `VICTORY_RAM_WAIT_MS` or `VICTORY_RAM_FRAME_WAIT_MS`, so a hang anywhere else still meets the watchdog. - **Build-checked, without borrowing the image.** `victory_loader::heartbeat::asked`: 1. checks that the tick function's 8 bytes (+0x2214a90) lie inside the image and in **one committed, readable region** (`VirtualQuery`; no guard page, no `PAGE_NOACCESS`); 2. copies just those 8 bytes out with `read_volatile` and compares them with 1.0.2.1's `C6 05 99 18 FA 04 01 C3` (`mov byte [rip+disp32], 1; ret`); 3. takes the flag's address from that instruction's displacement (+0x71b6330); 4. checks that the flag lies inside the image, in **committed, writable** memory. Another build, or memory that doesn't allow it, leaves the watchdog alone and logs why. - **No safe API takes an address.** `victory_gpu::ram_keep_alive_at` is `unsafe fn(flag: *const AtomicU8)`, with the contract that the byte stays valid for writes for the rest of the process. `run.rs` calls it only with what `asked` returns. The title image is the prepared DLL, which is never unloaded. - **Concurrent access.** The throttle's threads store to the flag through an `AtomicU8`, so they never race each other. The title's own stores and its watchdog's read-and-clear are single-byte operations, which x86 doesn't tear. Whichever store lands last, the flag reads "ticked", or the watchdog clears it as on the console. ## Tests - `heartbeat::tests::the_heartbeat_flag_is_found_only_behind_the_known_tick_function` - `heartbeat::tests::a_region_is_checked_for_commit_access_and_bounds`: committed read-write, read-only, a range across two regions, reserved but uncommitted, and an end past the address space. - victory-gpu 472 passed, twice in a row. One earlier run had `watch::tests::what_the_kernel_writes_through_is_not_guarded_for_another_view` fail, which looks timing-sensitive and is in code this doesn't touch. - Clippy: no warnings, with and without diagnostics. rustfmt is clean on the new file. ## Measurements Laptop: i7-13700H, RTX 3050 6 GB, 16 GB, `-Machine laptop` (#100), Series S, 240 s runs under a guard that stops the game below 400 MB of free RAM. | | Runs | Result | |---|---|---| | without this switch | 2 | both ended by the title's watchdog during the world load | | with `VICTORY_RAM_KEEP_ALIVE=1` (the first version, fc74f10) | 2 | both ran the full 240 s: 175 / 177 s in the world, 118 / 112 frames, median 1,471 / 1,455 ms | | with `VICTORY_RAM_KEEP_ALIVE=1`, **this version** (5fca9d5, on `main` + #96 + #100) | 2 | neither met the watchdog, and both logged the heartbeat check passing. One ran the full 240 s (176 s in the world, 122 frames, median 1,366 ms). The other was stopped by my external guard for low RAM (332 MB free) 55 s into the world: a RAM dip, not the watchdog | 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On a 16 GB PC physical RAM runs out while the world streams in: the
memory the layer shares with the card goes from under 1 GB to 4-5 GB in
about 20 s, faster than Windows writes other pages out, and free RAM
falls to nothing. These settings, all off by default, slow that down:

- VICTORY_RAM_FLOOR_MB: a new buffer in the PC's memory (upload or
  shared with the card) waits while less than the floor would be left
  free, VICTORY_RAM_WAIT_MS at most (250 by default), and all threads
  together at most VICTORY_RAM_WAIT_SHARE_MS of every second (600, at
  most 1000). It only slows the making; it never refuses.
- VICTORY_RAM_FRAME_WAIT_MS: under the floor a submission also waits at
  its start, up to that long and at most half of the time, so the
  title's own streaming slows too (well inside its 30 s watchdog).
- VICTORY_WINDOWS_BAR_PERCENT: the share of the card's budget up to
  which windows outside buffers go to the card's memory
  (VICTORY_WINDOWS_BAR_MB), 50 to 95; 80 by default, as before.

The memory line reports what the throttle did and the windows on the
card; while it holds things back it reports every 5 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The share of a second the throttle may wait (VICTORY_RAM_WAIT_SHARE_MS)
was a second and its spent milliseconds in two atomics: a thread moving to
a new second reset the spent time and could erase a charge another thread
had just made. They are one word now (the second above 20 bits, the
milliseconds below), charged with fetch_update; a thread that read the
clock before another moved the word on is charged to the word's own
second, so the word never goes back and no charge is lost. A charge the
share has no room for is not recorded. A test runs eight threads over two
seconds, half of them asking for the earlier second after the turn: each
second gives exactly its share.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The throttle holds the title's submissions back (VICTORY_RAM_FRAME_WAIT_MS)
and the title's main loop waits for them, so a world load it slows can go
more than 30 s without a frame; the title's watchdog then ends it (2 of 2
loads on a 16 GB laptop). With VICTORY_RAM_KEEP_ALIVE=1 (off by default)
the throttle sets the title's heartbeat flag as the title's own tick
function does, only in its own waits, which VICTORY_RAM_WAIT_MS and
VICTORY_RAM_FRAME_WAIT_MS bound: a hang anywhere else still meets the
watchdog.

victory_loader:💓:asked checks before anything is written: the
tick function's 8 bytes (+0x2214a90, 1.0.2.1: mov byte [rip+disp32], 1;
ret) lie in the image, in one committed readable region, are copied out
and compared, and give the flag's address; the flag lies in the image, in
committed writable memory (VirtualQuery). Another build, or memory that
does not allow it, leaves the watchdog alone and says why. The setter on
the GPU side is unsafe (the flag must stay valid for writes for the rest
of the process: the title image is never unloaded), and the throttle's
threads store to the flag atomically.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 03:16:05 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 03:14 UTC · original

Thanks for splitting this out, @⁠nidscode. Both heartbeat loader tests passed on 5fca9d5. The signature and page checks cover the address lookup. There's still a timing case to cover before we can rely on the watchdog: one buffer waiter can finish as another starts, and each can set the flag while the title's render path has stopped making progress. A limit per wait still allows an extension across successive waits.

Could you cap the extension across waiters and renew it only when the submission the title is waiting for makes real progress? A test with staggered waiters and no progress should show that the heartbeat writes stop. This also needs the duration checks from #96.

One build fix: restore #[cfg(windows)] on the existing inplace re-export in victory-gpu and add it to the new ram_keep_alive_at re-export in victory-hle. Both source symbols are Windows-only.

**@⁠TaynanGT** on GitHub · 2026-11-27 03:14 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/126#issuecomment-7208826483) Thanks for splitting this out, @⁠nidscode. Both heartbeat loader tests passed on `5fca9d5`. The signature and page checks cover the address lookup. There's still a timing case to cover before we can rely on the watchdog: one buffer waiter can finish as another starts, and each can set the flag while the title's render path has stopped making progress. A limit per wait still allows an extension across successive waits. Could you cap the extension across waiters and renew it only when the submission the title is waiting for makes real progress? A test with staggered waiters and no progress should show that the heartbeat writes stop. This also needs the duration checks from #96. One build fix: restore `#[cfg(windows)]` on the existing `inplace` re-export in `victory-gpu` and add it to the new `ram_keep_alive_at` re-export in `victory-hle`. Both source symbols are Windows-only.
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 04:01 UTC · original

Thanks for reporting the load results, @⁠nidscode. I approved CI for 5fca9d5. Clippy caught the constant-only assert! in heartbeat.rs:146 under -D warnings; the invariant can be checked at compile time. The open behavior check is still the staggered waiters: the heartbeat should stop when there’s no submission progress, even if another waiter starts. CI run

**@⁠TaynanGT** on GitHub · 2026-11-27 04:01 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/126#issuecomment-7209458153) Thanks for reporting the load results, @⁠nidscode. I approved CI for `5fca9d5`. Clippy caught the constant-only `assert!` in `heartbeat.rs:146` under `-D warnings`; the invariant can be checked at compile time. The open behavior check is still the staggered waiters: the heartbeat should stop when there’s no submission progress, even if another waiter starts. [CI run](https://github.com/TaynanGT/Vice-Tay/actions/runs/45932556937)
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 04:18 UTC · original

I verified the Windows-only re-exports and compile-time range check in a local follow-up, @⁠nidscode. Both heartbeat tests and Clippy passed. For the runtime side, the current frame counter measures frame waits, so a useful next version needs a shared deadline tied to progress on the submission the title is waiting for. The staggered-waiter test remains the check for that behavior.

**@⁠TaynanGT** on GitHub · 2026-11-27 04:18 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/126#issuecomment-7209691875) I verified the Windows-only re-exports and compile-time range check in a local follow-up, @⁠nidscode. Both heartbeat tests and Clippy passed. For the runtime side, the current frame counter measures frame waits, so a useful next version needs a shared deadline tied to progress on the submission the title is waiting for. The staggered-waiter test remains the check for that behavior.
This pull request has changes conflicting with the target branch.
  • apps/victory/src/run.rs
  • crates/victory-gpu/src/host.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/126/head:github-bot/gh-126'
git switch 'github-bot/gh-126'

Merge

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