Memory line: take the texture census on a thread of its own, in chunks, every 60 s #3267

Manually merged
github merged 0 commits from github-bot/gh-3267 into gh-pr/3267/base 2026-11-30 14:16:47 +00:00 AGit
Owner

@⁠CuteBooty on GitHub · 2026-11-29 21:09 UTC · original
CuteBooty:pr-census-thread → main · +143 −17 · 2 files · 1 commits · merged 2026-11-30 14:12 UTC

What was wrong

With diagnostics, copystat::log writes the memory line every 5 s on the thread that presents frames. log_memory calls resource::census() and resource::targets_census(), and each of them walks every title texture alive (450-560k in a long session) while holding the global textures' lock for the whole walk. A pass took 22-146 ms in our logs. The presenting thread stalls for that long, and so does every other thread that makes or frees a texture in that time, because it waits for the same lock. In one play run on an 8 GB card, at least 12 of 41 hitches a minute lined up with these passes.

What changes

  • The census (both walks) is taken every VICTORY_CENSUS_S seconds, 60 by default, on a thread of its own named victory-census. Only one census runs at a time.
  • That thread holds the textures' lock for 4096 textures at a time, then lets go and yields, and goes on from the last address it saw. Textures made or freed between chunks may be counted or missed; that is good enough for a memory line.
  • The memory line prints the last census taken, plus one new line saying how old it is, how long it took and where it ran. Until the first census is done the census counts print as 0 and the line says so.
  • All the other numbers in the memory line are still written every 5 s.
  • Only copystat.rs and resource.rs in victory-gpu change, and only code under diagnostics. census and targets_census take a chunk: Option<usize>. A new helper walk_textures does the walk: whole under one lock with None, as before, or in chunks.

Switches

Switch Default Effect
VICTORY_CENSUS_S 60 Seconds between censuses; 0 = never take it
VICTORY_CENSUS_INLINE off 1 = take the census on the logging thread with the lock held for the whole walk. With VICTORY_CENSUS_S=5 this is the old behaviour

How it was tested

On this branch (main 829188e96 + this one commit):

  • cargo build -p victory --release: builds. Without diagnostics the compiled code is the same as main's, because everything changed is under diagnostics.
  • cargo test -p victory-gpu: passes. One run had host::card_steps::tests::places_written_after_sending_are_learned fail; it passed three times on its own and in the full run on main. It looks flaky under parallel tests and is not related to this change.
  • cargo clippy -p victory-gpu: no warnings in the changed code.
  • Diagnostics. On main 829188e96 the diagnostics build does not compile without the open PR #2386; the errors are the shadowed timing and the missing mb, none in this change. This PR does not include #2386. With #2386 applied on top for the check only:
    • cargo build -p victory --release --features diagnostics builds.
    • cargo clippy -p victory-gpu --features diagnostics gives 30 warnings, the same count as main + #2386, and none in the changed lines.
    • cargo test -p victory-gpu --features diagnostics: compose::tests::bgrx_plane_selects_a_typed_view fails (DXGI_FORMAT(88) vs 93). It fails the same way on main + #2386 without this change. The rest pass, apart from the occasional flaky test named above and watch::many_askers_tests::no_mapping_is_lost_between_askers once.

In play:

  • Hitches over 100 ms went from 36.8 a minute without this change to 9.4 a minute with it.
  • Seconds around a memory report that had a hitch went from 72% to 11.6%.
  • These are one run each, in different sittings, on builds that differ in more than this change. Read them as a sign, not as a controlled A/B.

Hardware: RTX 3070 8 GB, Ryzen 7 5800X3D, 32 GB RAM, Windows 11 build 26200, NVIDIA driver 621.24.

Related: #791 (VRAM growth on 8 GB cards; the memory line this census feeds is the main tool for watching it).

🤖 Generated with Claude Code

**@⁠CuteBooty** on GitHub · 2026-11-29 21:09 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3267) `CuteBooty:pr-census-thread` → `main` · +143 −17 · 2 files · 1 commits · merged 2026-11-30 14:12 UTC ## What was wrong With `diagnostics`, `copystat::log` writes the memory line every 5 s on the thread that presents frames. `log_memory` calls `resource::census()` and `resource::targets_census()`, and each of them walks every title texture alive (450-560k in a long session) while holding the global textures' lock for the whole walk. A pass took 22-146 ms in our logs. The presenting thread stalls for that long, and so does every other thread that makes or frees a texture in that time, because it waits for the same lock. In one play run on an 8 GB card, at least 12 of 41 hitches a minute lined up with these passes. ## What changes - The census (both walks) is taken every `VICTORY_CENSUS_S` seconds, 60 by default, on a thread of its own named `victory-census`. Only one census runs at a time. - That thread holds the textures' lock for 4096 textures at a time, then lets go and yields, and goes on from the last address it saw. Textures made or freed between chunks may be counted or missed; that is good enough for a memory line. - The memory line prints the last census taken, plus one new line saying how old it is, how long it took and where it ran. Until the first census is done the census counts print as 0 and the line says so. - All the other numbers in the memory line are still written every 5 s. - Only `copystat.rs` and `resource.rs` in `victory-gpu` change, and only code under `diagnostics`. `census` and `targets_census` take a `chunk: Option<usize>`. A new helper `walk_textures` does the walk: whole under one lock with `None`, as before, or in chunks. ## Switches | Switch | Default | Effect | |---|---|---| | `VICTORY_CENSUS_S` | `60` | Seconds between censuses; `0` = never take it | | `VICTORY_CENSUS_INLINE` | off | `1` = take the census on the logging thread with the lock held for the whole walk. With `VICTORY_CENSUS_S=5` this is the old behaviour | ## How it was tested On this branch (main 829188e96 + this one commit): - `cargo build -p victory --release`: builds. Without `diagnostics` the compiled code is the same as main's, because everything changed is under `diagnostics`. - `cargo test -p victory-gpu`: passes. One run had `host::card_steps::tests::places_written_after_sending_are_learned` fail; it passed three times on its own and in the full run on main. It looks flaky under parallel tests and is not related to this change. - `cargo clippy -p victory-gpu`: no warnings in the changed code. - **Diagnostics.** On main 829188e96 the diagnostics build does not compile without the open PR #2386; the errors are the shadowed `timing` and the missing `mb`, none in this change. This PR does **not** include #2386. With #2386 applied on top for the check only: - `cargo build -p victory --release --features diagnostics` builds. - `cargo clippy -p victory-gpu --features diagnostics` gives 30 warnings, the same count as main + #2386, and none in the changed lines. - `cargo test -p victory-gpu --features diagnostics`: `compose::tests::bgrx_plane_selects_a_typed_view` fails (`DXGI_FORMAT(88)` vs `93`). It fails the same way on main + #2386 without this change. The rest pass, apart from the occasional flaky test named above and `watch::many_askers_tests::no_mapping_is_lost_between_askers` once. In play: - Hitches over 100 ms went from **36.8 a minute** without this change to **9.4 a minute** with it. - Seconds around a memory report that had a hitch went from **72%** to **11.6%**. - These are **one run each**, in different sittings, on builds that differ in more than this change. Read them as a sign, not as a controlled A/B. Hardware: RTX 3070 8 GB, Ryzen 7 5800X3D, 32 GB RAM, Windows 11 build 26200, NVIDIA driver 621.24. Related: #791 (VRAM growth on 8 GB cards; the memory line this census feeds is the main tool for watching it). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The diagnostics memory line runs every 5 s on the presenting thread and
walked every title texture twice (census, targets_census), 450-560k of
them, holding the textures' lock for the whole walk. Each pass took
22-146 ms, and every texture made or freed in that time waited for the
lock: at least 12 of 41 hitches a minute in one play run on an 8 GB card.

Now the census is taken every VICTORY_CENSUS_S seconds (60 by default,
0 = never) on a thread of its own ("victory-census") that holds the lock
for 4096 textures at a time. The memory line prints the last census and
one line saying how old it is and how long it took; its other numbers
are still every 5 s. VICTORY_CENSUS_INLINE=1 with VICTORY_CENSUS_S=5
gives back the old behaviour (inline, the whole walk under the lock).

Related: #791.

(cherry picked from commit a4ceae1f3a2cf9edcfe52b3595f616007901d757)

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

@⁠ErRusso on GitHub · 2026-11-29 21:25 UTC · original · reviewed

Reviewed against main (fbe3dc20). The code is sound:

  • walk_textures holds the lock for the whole of each chunk while each runs, so the Resource dereference stays covered by the same argument as let_go_idle; resuming from Excluded(last address) on the BTreeSet is correct, and the walk stops once a chunk comes back short.
  • One census at a time (RUNNING), reset when the spawn fails; the memory line says how old the census is.

Notes:

  • The failing check job is not this PR: clippy stops on an unused unwind_copy in crates/victory-loader/src/crash.rs:804, which the PR does not touch. A rebase after that is fixed on main should turn it green.
  • This only affects diagnostics builds, so release players see no change. In particular it does not address #3279 (the ~700 ms procmem walk), which is a different walk. On main that one already runs on its own thread (procmem.rs:203), so my guess, not measured, is that its 93k VirtualQuery calls hold the process's address-space lock and stall the other threads' VirtualAlloc/VirtualProtect. The chunking approach from this PR could be applied there too.
**@⁠ErRusso** on GitHub · 2026-11-29 21:25 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/3267#pullrequestreview-6104874727) · **reviewed** Reviewed against main (fbe3dc20). The code is sound: - `walk_textures` holds the lock for the whole of each chunk while `each` runs, so the `Resource` dereference stays covered by the same argument as `let_go_idle`; resuming from `Excluded(last address)` on the `BTreeSet` is correct, and the walk stops once a chunk comes back short. - One census at a time (`RUNNING`), reset when the spawn fails; the memory line says how old the census is. Notes: - The failing `check` job is not this PR: clippy stops on an unused `unwind_copy` in `crates/victory-loader/src/crash.rs:804`, which the PR does not touch. A rebase after that is fixed on main should turn it green. - This only affects `diagnostics` builds, so release players see no change. In particular it does not address #3279 (the ~700 ms `procmem` walk), which is a different walk. On main that one already runs on its own thread (`procmem.rs:203`), so my guess, not measured, is that its 93k `VirtualQuery` calls hold the process's address-space lock and stall the other threads' `VirtualAlloc`/`VirtualProtect`. The chunking approach from this PR could be applied there too.
github-bot changed target branch from main to gh-pr/3267/base 2026-11-30 14:16:35 +00:00
github manually merged commit f055f7c453 into gh-pr/3267/base 2026-11-30 14:16:37 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!3267
No description provided.