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

Closed
nidscode wants to merge 399 commits from upstream-pr/126 into main
nidscode commented 2026-11-27 03:09:56 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by nidscode on 2026-11-27T03:09:56Z.


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

<!-- greyfinch-sync:TaynanGT/Vice-Tay:issue:5520506127 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/126). Originally posted by [nidscode](https://github.com/nidscode) on 2026-11-27T03:09:56Z. --- ## 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)
TaynanGT commented 2026-11-27 03:14:45 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T03:14:45Z.


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.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7208826483 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/126#issuecomment-7208826483). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T03:14:45Z. --- 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 commented 2026-11-27 04:01:33 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T04:01:33Z.


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

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7209458153 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/126#issuecomment-7209458153). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T04:01:33Z. --- 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)
TaynanGT commented 2026-11-27 04:18:26 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T04:18:26Z.


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.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7209691875 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/126#issuecomment-7209691875). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T04:18:26Z. --- 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 commented 2026-11-30 17:16:55 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-30T17:16:55Z.


main has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current main and open a new PR; I'll review it there.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7286015022 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/126#issuecomment-7286015022). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-30T17:16:55Z. --- `main` has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current `main` and open a new PR; I'll review it there.

Pull request closed

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
TaynanGT/Vice-Tay!126
No description provided.