[pr-fix][memory] CPU mirror files leave 4 GiB free on their drive #339

Open
Andreww2009 wants to merge 1 commit from Andreww2009/Darner-Opt:claude/cpu-mirror-room-left into main
First-time contributor

What it changes

  • The problem on main: with DARNER_CARD_CPU_FILE=1 (a darner play default) the CPU mirrors are temporary files in the cache's cpu-mirrors folder, made without a look at the drive's free space, so on an almost full drive they take its last bytes. One session here (v2026.12.03.01, Series X, the cache on C:):
    • the mirror files grew from file_mib=994 as the world began to file_mib=3385 ten minutes later, and C: reached 0 bytes free (the mirrors were 3.3 GB of what filled it; other programs wrote there too);
    • no mirror file failed (no file-backed CPU mirror unavailable line); 7 of the 8 host pipeline library saves between 09:10 and 09:22 UTC did (host pipeline library not saved error=... (os error 112)), and one shared pipeline lists not saved. A save writes a whole new copy of the library (1,180,832,868 bytes in the one that landed) before renaming it;
    • 3.3 of the 13.7 minutes in the world have no log lines.

MappedStorage::new (crates/darner-gpu/src/fileback.rs) now reads the drive's free space (GetDiskFreeSpaceExW) before it makes a file, and refuses one that would leave less than 4 GiB free (io::ErrorKind::StorageFull, with the numbers in the message). The callers already handle a failed file: CardCpu::allocate warns once (file-backed CPU mirror unavailable; falling back to private memory) and keeps that mirror in private memory, and CardCpu::lazy keeps its reserved-only mirror. Each later mirror tries a file again, so files resume when the drive has room. A drive whose free space cannot be read is not refused. In this crate's unit tests no room is kept, since they make files in whatever temp folder the runner has. docs/windows-memory.md says the same in one sentence.

Nothing changes on a drive with more than 4 GiB plus the file free. shadow.rs and the launchers are untouched; the 4 GiB is a constant, not a new switch.

How it was tested

  • Tested revision: this PR's single commit, on f3840d73 (the tests and gates ran on the same tree just before it was committed)
  • Commands and results (Windows 11 Home build 26300, Intel Core Ultra 9 275HX, RTX 5080 Laptop GPU, driver 621.47, Rust 1.98.1):
    • cargo test --locked -p darner-gpu --lib fileback -- --nocapture --test-threads=1: 5 passed (the 3 there were, and 2 new: the room arithmetic, and a file that would leave too little room is refused with StorageFull, no OS error, and no file made).
    • cargo test --locked -p darner-gpu --lib a_mirror_let_go_keeps_its_address: 1 passed (it makes a mirror file through MappedStorage::new).
    • cargo test --locked -p darner-gpu --lib, both test binaries built once and run alternately, 8 times each (order swapped every run):
      • main f3840d73: 2 passed, 5 failed, 1 still running after 300 s (stopped). Failed: watch::tests::ask_once_by_res_splits_reservations_without_refusals (3), watch::tests::a_late_fault_leaves_the_guard_taken_since (2), watch::many_askers_tests::no_write_is_lost_when_faults_take_no_lock (1), host::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end (1).
      • this change: 3 passed, 5 failed. Failed: host::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end (4), watch::tests::ask_once_by_res_splits_reservations_without_refusals (3), watch::tests::a_late_fault_leaves_the_guard_taken_since (1), host::refresh_tests::chunked_nothing_new_with_matches_individual (1).
      • Two more single runs with this change: one passed (1758 passed), one failed watch::tests::ask_once_by_res_splits_reservations_without_refusals and descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync (#274 also saw that one fail once with its own change).
      • None of these tests reach the changed code: in unit tests no mirror folder is set, so only the fileback tests and a_mirror_let_go_keeps_its_address make a mirror file. main fails by chance too (#274 saw 7 of 8 main runs fail; #224 and #262 work on such failures).
    • Cost of the check: GetDiskFreeSpaceExW took 9.2 µs a call (median of 5 × 20,000) on the cache's drive and 15.2 µs on C:, against 440–580 µs for making, mapping and deleting one mirror-sized file the same way (both timed through Python ctypes, so a little high). With the ~4,400 files of a session (#101's count) that is about 40–70 ms in a whole session.
  • Changes a default: no. It only acts where a mirror file would leave less than 4 GiB free; that mirror is then private memory, as it already is when a file cannot be made.
  • A/B result: not a speed change; the in-game behaviour without it is the session above.
  • Gates run on that revision: both clippy gates (clean) and cargo xtask policy (555 files ok). Gate 3 not run as a whole: cargo test -p darner-gpu --lib above instead, the only crate changed.
  • Gameplay: not run with this change (see the checklist).
  • Hardware dependence: none expected on Windows. Under Proton the mirrors may be on /dev/shm (DARNER_MIRRORS_TMPFS), where the same check would keep 4 GiB of tmpfs free; not tested there.
  • Coverage gaps: not run in the game; no drive was filled to see the refusal end to end (the test asks for more room than the drive has); not run on Linux/Proton or on a network drive.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
    • Not run in the game: the check acts only below 4 GiB free, and I did not fill a drive to play on one. The session above is without this change.
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #101 (closed; removes the mirror files a PC reset left behind)
  • Replaces or overlaps: none found

🤖 Generated with Claude Code

## What it changes - The problem on main: with `DARNER_CARD_CPU_FILE=1` (a `darner play` default) the CPU mirrors are temporary files in the cache's `cpu-mirrors` folder, made without a look at the drive's free space, so on an almost full drive they take its last bytes. One session here (v2026.12.03.01, Series X, the cache on C:): - the mirror files grew from `file_mib=994` as the world began to `file_mib=3385` ten minutes later, and C: reached 0 bytes free (the mirrors were 3.3 GB of what filled it; other programs wrote there too); - no mirror file failed (no `file-backed CPU mirror unavailable` line); 7 of the 8 host pipeline library saves between 09:10 and 09:22 UTC did (`host pipeline library not saved error=... (os error 112)`), and one `shared pipeline lists not saved`. A save writes a whole new copy of the library (1,180,832,868 bytes in the one that landed) before renaming it; - 3.3 of the 13.7 minutes in the world have no log lines. `MappedStorage::new` (`crates/darner-gpu/src/fileback.rs`) now reads the drive's free space (`GetDiskFreeSpaceExW`) before it makes a file, and refuses one that would leave less than 4 GiB free (`io::ErrorKind::StorageFull`, with the numbers in the message). The callers already handle a failed file: `CardCpu::allocate` warns once (`file-backed CPU mirror unavailable; falling back to private memory`) and keeps that mirror in private memory, and `CardCpu::lazy` keeps its reserved-only mirror. Each later mirror tries a file again, so files resume when the drive has room. A drive whose free space cannot be read is not refused. In this crate's unit tests no room is kept, since they make files in whatever temp folder the runner has. `docs/windows-memory.md` says the same in one sentence. Nothing changes on a drive with more than 4 GiB plus the file free. `shadow.rs` and the launchers are untouched; the 4 GiB is a constant, not a new switch. ## How it was tested - Tested revision: this PR's single commit, on `f3840d73` (the tests and gates ran on the same tree just before it was committed) - Commands and results (Windows 11 Home build 26300, Intel Core Ultra 9 275HX, RTX 5080 Laptop GPU, driver 621.47, Rust 1.98.1): - `cargo test --locked -p darner-gpu --lib fileback -- --nocapture --test-threads=1`: 5 passed (the 3 there were, and 2 new: the room arithmetic, and a file that would leave too little room is refused with `StorageFull`, no OS error, and no file made). - `cargo test --locked -p darner-gpu --lib a_mirror_let_go_keeps_its_address`: 1 passed (it makes a mirror file through `MappedStorage::new`). - `cargo test --locked -p darner-gpu --lib`, both test binaries built once and run alternately, 8 times each (order swapped every run): - `main` `f3840d73`: 2 passed, 5 failed, 1 still running after 300 s (stopped). Failed: `watch::tests::ask_once_by_res_splits_reservations_without_refusals` (3), `watch::tests::a_late_fault_leaves_the_guard_taken_since` (2), `watch::many_askers_tests::no_write_is_lost_when_faults_take_no_lock` (1), `host::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end` (1). - this change: 3 passed, 5 failed. Failed: `host::card_steps::tests::shared_scratch_eliminates_flight_settle_end_to_end` (4), `watch::tests::ask_once_by_res_splits_reservations_without_refusals` (3), `watch::tests::a_late_fault_leaves_the_guard_taken_since` (1), `host::refresh_tests::chunked_nothing_new_with_matches_individual` (1). - Two more single runs with this change: one passed (1758 passed), one failed `watch::tests::ask_once_by_res_splits_reservations_without_refusals` and `descriptor::tests::records_copied_as_bytes_are_made_at_the_next_sync` (#274 also saw that one fail once with its own change). - None of these tests reach the changed code: in unit tests no mirror folder is set, so only the `fileback` tests and `a_mirror_let_go_keeps_its_address` make a mirror file. `main` fails by chance too (#274 saw 7 of 8 `main` runs fail; #224 and #262 work on such failures). - Cost of the check: `GetDiskFreeSpaceExW` took 9.2 µs a call (median of 5 × 20,000) on the cache's drive and 15.2 µs on C:, against 440–580 µs for making, mapping and deleting one mirror-sized file the same way (both timed through Python ctypes, so a little high). With the ~4,400 files of a session (#101's count) that is about 40–70 ms in a whole session. - Changes a default: no. It only acts where a mirror file would leave less than 4 GiB free; that mirror is then private memory, as it already is when a file cannot be made. - A/B result: not a speed change; the in-game behaviour without it is the session above. - Gates run on that revision: both clippy gates (clean) and `cargo xtask policy` (555 files ok). Gate 3 not run as a whole: `cargo test -p darner-gpu --lib` above instead, the only crate changed. - Gameplay: not run with this change (see the checklist). - Hardware dependence: none expected on Windows. Under Proton the mirrors may be on `/dev/shm` (`DARNER_MIRRORS_TMPFS`), where the same check would keep 4 GiB of tmpfs free; not tested there. - Coverage gaps: not run in the game; no drive was filled to see the refusal end to end (the test asks for more room than the drive has); not run on Linux/Proton or on a network drive. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (docs, tests only). - Not run in the game: the check acts only below 4 GiB free, and I did not fill a drive to play on one. The session above is without this change. - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #101 (closed; removes the mirror files a PC reset left behind) - Replaces or overlaps: none found 🤖 Generated with [Claude Code](https://claude.com/claude-code)
With DARNER_CARD_CPU_FILE=1 the CPU mirrors are temporary files in the
cache's cpu-mirrors folder, made without a look at the drive's free space.
They grow all through a session (994 -> 3,385 MiB in one) and took a full
system drive's last bytes: 7 of 8 host pipeline library saves failed with
os error 112 and minutes of the log were lost, while no mirror file failed.

MappedStorage::new now refuses a file that would leave less than 4 GiB
free (io::ErrorKind::StorageFull); the callers' existing fallback keeps
that mirror in private memory. This crate's unit tests keep no room.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
First-time contributor

6a9b0df covers the UNC gap in the free-space query. The launcher canonicalizes the mirror directory, which can produce a verbatim UNC path. The Windows API requires the trailing backslash for UNC directories, and a query failure takes this PR's fallback path. GetDiskFreeSpaceExW parameter documentation

The same argument test failed on 3e6e6c2 and passed on the follow-up for ordinary UNC, verbatim UNC, a path already ending in a backslash, and a local drive path. The real Windows free-space query succeeded on the local directory before and after. UNC validation used synthetic paths. Focused cargo check, the asset policy and the diff check passed.

If you cherry-pick or rebase this commit, please keep the Git author. If you fold it into your own commit, please add this trailer.

Co-authored-by: andrewpozdnakov7-jpg <[email protected]>

[6a9b0df](https://git.gtahub.st/andrewpozdnakov7-jpg/Darner/commit/6a9b0df90fbb47f52adbe54543e572c1bb5b02e9) covers the UNC gap in the free-space query. The launcher canonicalizes the mirror directory, which can produce a verbatim UNC path. The Windows API requires the trailing backslash for UNC directories, and a query failure takes this PR's fallback path. [GetDiskFreeSpaceExW parameter documentation](https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskfreespaceexw) The same argument test failed on `3e6e6c2` and passed on the follow-up for ordinary UNC, verbatim UNC, a path already ending in a backslash, and a local drive path. The real Windows free-space query succeeded on the local directory before and after. UNC validation used synthetic paths. Focused `cargo check`, the asset policy and the diff check passed. If you cherry-pick or rebase this commit, please keep the Git author. If you fold it into your own commit, please add this trailer. `Co-authored-by: andrewpozdnakov7-jpg <296776509+[email protected]>`
Contributor

Code review only (read the diff at 3e6e6c27), nothing built or run.

  • Trade-off worth stating: a refused mirror falls back to private memory (shadow.rs:244), which is commit, the thing the file mirrors exist to save (docs/windows-memory.md). When the cache and the page file share a nearly full drive, Windows can't grow the page file much either. So on those PCs this likely moves some pressure from disk to commit (lazy mirrors that stay reserved-only don't count). Probably still better than a full C:, but a run on a drive with only a few GB free, watching available commit and the fallback count, would show it. A sentence in the docs would help testers who hit "out of memory" afterwards.
  • The UNC gap that andrewpozdnakov7-jpg raised is real per the GetDiskFreeSpaceExW docs (UNC needs a trailing backslash). Without a fix, a UNC mirror folder skips the check (fails open, harmless). I haven't read their commit.
  • Minor: the fallback warns once, so how many mirrors went to private memory isn't visible. A count in an existing memory line would help with RAM reports.

Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.

Code review only (read the diff at `3e6e6c27`), nothing built or run. - **Trade-off worth stating:** a refused mirror falls back to private memory (`shadow.rs:244`), which is commit, the thing the file mirrors exist to save (`docs/windows-memory.md`). When the cache and the page file share a nearly full drive, Windows can't grow the page file much either. So on those PCs this likely moves some pressure from disk to commit (lazy mirrors that stay reserved-only don't count). Probably still better than a full C:, but a run on a drive with only a few GB free, watching available commit and the fallback count, would show it. A sentence in the docs would help testers who hit "out of memory" afterwards. - The UNC gap that andrewpozdnakov7-jpg raised is real per the `GetDiskFreeSpaceExW` docs (UNC needs a trailing backslash). Without a fix, a UNC mirror folder skips the check (fails open, harmless). I haven't read their commit. - Minor: the fallback warns once, so how many mirrors went to private memory isn't visible. A count in an existing memory line would help with RAM reports. Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.
First-time contributor

I added the requested note in f838569. The low-disk fallback can raise system commit, and a page file on the same nearly full drive may have little room to grow. The docs now tie the reduction in private commit to the file-backed mirrors.

The docs commit is based on 3e6e6c27 and merges cleanly with the earlier UNC path fix 6a9b0df. Asset policy and diff checks passed. The UNC fix was checked with synthetic UNC paths and a local Windows volume. A live share and a near-full drive remain untested.

If you cherry-pick or rebase this commit, please keep the Git author. If you fold it into another commit, please add this trailer.

Co-authored-by: andrewpozdnakov7-jpg <[email protected]>

I added the requested note in [f838569](https://git.gtahub.st/andrewpozdnakov7-jpg/Darner/commit/f838569a858f471ec237f11ef19117eb1c1dc2e9). The low-disk fallback can raise system commit, and a page file on the same nearly full drive may have little room to grow. The docs now tie the reduction in private commit to the file-backed mirrors. The docs commit is based on `3e6e6c27` and merges cleanly with the earlier UNC path fix `6a9b0df`. Asset policy and diff checks passed. The UNC fix was checked with synthetic UNC paths and a local Windows volume. A live share and a near-full drive remain untested. If you cherry-pick or rebase this commit, please keep the Git author. If you fold it into another commit, please add this trailer. `Co-authored-by: andrewpozdnakov7-jpg <296776509+[email protected]>`
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'claude/cpu-mirror-room-left:Andreww2009-claude/cpu-mirror-room-left'
git switch 'Andreww2009-claude/cpu-mirror-room-left'

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 'Andreww2009-claude/cpu-mirror-room-left'
git switch 'Andreww2009-claude/cpu-mirror-room-left'
git rebase 'main'
git switch 'main'
git merge --ff-only 'Andreww2009-claude/cpu-mirror-room-left'
git switch 'Andreww2009-claude/cpu-mirror-room-left'
git rebase 'main'
git switch 'main'
git merge --no-ff 'Andreww2009-claude/cpu-mirror-room-left'
git switch 'main'
git merge --squash 'Andreww2009-claude/cpu-mirror-room-left'
git switch 'main'
git merge --ff-only 'Andreww2009-claude/cpu-mirror-room-left'
git switch 'main'
git merge 'Andreww2009-claude/cpu-mirror-room-left'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!339
No description provided.