gpu: ask GetWriteWatch per reservation to avoid refused askings (VICTORY_ASK_BY_RES) #4395
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4395"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shiman1901 on 2026-11-30T19:46:46Z.
Cut the write-watch askings at the boundaries of the write-watched reservations, so a merged run no longer crosses two reservations and gets refused.
Ref: #2415 (
ScarlettPort c74db40), where these refusals were about 390 a frame.What it changes
ReservationCache(watch.rs): a page ->ResSpan { end, watchable, res_id }map, filled byVirtualQueryplus a non-destructiveGetWriteWatch(0, ...)probe (count = 1). The probe does not reset dirty pages (checked on Win11 26300: 5 dirty pages, count=1 probe, thenRESETstill sees all 5).RES_GEN,RES_COUNT):title_placeholder_reserved,title_memory_allocated,title_memory_remapped,title_memory_unmapping,title_memory_freed,title_memory_made_realdrop the cached spans. A probe in flight cannot cache what it found of memory that changed hands.ask_once,ask_ahead, andask_restcut their ranges at reservation boundaries and ask once per touched write-watched reservation; a refusal falls back to the old per-piece recursion with the span invalidated.VICTORY_ASK_BY_RES(default on;=0is the old path, byte for byte;VICTORY_ABflips it live).Kernel probe (Win11 26300, real kernel)
MEM_RESERVEhole between two committed 64 KiB blocks inside oneMEM_WRITE_WATCHreservation:GetWriteWatchsucceeds (r=0, 2 pages).count=1, flags=0thenRESET, count=16: first returns 1 page without resetting; the second sees all 5.So the refusal is the allocation boundary, not holes or protection.
Synthetic microbench (Ryzen 7 5700X, Win11 26300; NOT an in-game measurement)
32 x 16 MiB write-watched + 16 x 16 MiB plain allocations, 300 query pieces, 120 dirty 4 KiB pages/frame, 5 warmup + 50 measured, one core:
GetWriteWatch/frame, 237-244 refusedThis is a model of the kernel calls, not a game run.
Tests
Four new Windows unit tests in
watch.rs:res_runs_splits_reservations_and_respects_gap_and_maxask_once_by_res_splits_reservations_without_refusalsres_probe_preserves_dirty_pages_on_cold_reservationres_cache_invalidates_on_freed_and_allocatedCC @dyshay @burber @Damix49
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Damix49 on 2026-11-30T21:03:37Z.
Tested in game on a Ryzen 9 5900X + RTX 3070 Ti (8 GB) + 32 GB DDR4-3600, Windows 11, driver 617.42. PR head
8a0221ebon84bd61e2, release build with--features ab.It doesn't build as posted (Rust 2024 edition). Two one-line fixes were needed:
genis a reserved keyword in the 2024 edition.allheldis declared, butall_heldis used below it inask_more.Tests: with those fixes,
cargo test --release -p victory-gpu --lib -- watch::gives 73 passed, 0 failed, including the four newres_*/ask_once_by_res_*tests.In game, A/B in one run (
VICTORY_AB=30,VICTORY_ASK_BY_RES): Series S 1280×720, DLSS Quality, frame generation off,VICTORY_VRAM_BUDGET_MB=6000. The camera stayed still in one spot, ~6,100 draws in every phase. I left out the phases while pipelines were still being made. That leaves 24 phases of 30 s (12 min):VICTORY_ASK_BY_RESNo measurable difference on this machine. The ~1 ms between the two is within the phase-to-phase spread, so the write-watch askings aren't what sets the frame here.
Unrelated to this PR, but seen in the same run: on this base the process reached ~56 GB private, with 0.4 GB of RAM free on 32 GB (see #2415 / #4079).
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by IRennie74 on 2026-11-30T22:38:17Z.
Two things from reading the diff, and a number that bears on it.
res_clip_pieces(watch.rs:1075) keeps a probed span only ifRES_GENhas not moved since the probe began, and everytitle_memory_remapped/_unmapping/_allocated/_made_realanywhere in the process bumps it (:4558,:4579,:2886,:4817). The probe of a write-watched span callsallocation_end(:1031), which walks every region of the allocation withVirtualQuery, so it is the slowest probe and the one most likely to overlap a remap on some title thread. When that happens it is thrown away and the same walk runs again next submission, on the submitting thread. Comparing against the ranges invalidated since the probe began, not a single global counter, would keep the cache warm. A count of probes that were not cached on the frame line would show whether this happens.toggled!turns it on for everyone, while the only in-game A/B so far (above) shows no change.asked_for!until a run shows a gain seems safer, since the fallback rewrites all three askings.On a Ryzen 5 5600X + RTX 3060 (Series S, DLSS Performance), the submitting thread is busy 44-54 ms of a 55-58 ms frame, and asking the write watch is 3-4 ms of that. So if the refusals are real there, this is where they would show. I can run
VICTORY_AB=…,VICTORY_ASK_BY_RESon that PC once the above is settled.Overlap: #4489 also rewrites
ask_once(asask_once_direct) and addsask_once_delta_reask_direct, which copies the old merge loop and would skip the reservation cut. Whichever lands second has to route that path throughres_runstoo.The CI failure here is main's (
lower.rs:114loop_caps,shadow.rsranged,host.rs:5092), not this PR's. Thegen/allheldbuild errors from the comment above are gone from the current head.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shiman1901 on 2026-11-30T22:39:04Z.
Thanks @Damix49 for running the watch:: suite (73/73 passing!) and the 12-minute in-game A/B on 5900X + 3070 Ti — and for catching gen / all_held! Both typos were already fixed in
792dcf93(0 Clippy warnings in watch.rs). Good to know write-watch askings weren't the bottleneck on that scene.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shiman1901 on 2026-11-30T23:01:12Z.
Thanks for the sharp review @IRennie74 — both points are addressed in
14e2cc09:RES_GENcheck with a singleMutex<ResCache>holding a 256-entry ring of recently invalidated page ranges (recent: VecDeque<(usize, usize)>) plus aRES_PROBINGatomic sores_invalidatestill fast-paths without locking when the cache is empty and no probe is in flight. A completed probe is only discarded if an overlapping range (or a whole-allocationlen == 0free, or ring overflow) occurred while probing. Added a 5th unit test (res_unrelated_invalidation_keeps_probe_cached) covering unrelated vs overlapping invalidations.ask_by_res()fromtoggled!toasked_for!("VICTORY_ASK_BY_RES")— off by default, enabled withVICTORY_ASK_BY_RES=1and still flippable mid-run viaVICTORY_AB=...,VICTORY_ASK_BY_RES.Good note on
#4489(ask_once_direct/ask_once_delta_reask_direct) as well — whoever merges second will need to route those throughres_clip_pieces/res_runs.CI (
14e2cc09) reports 0 errors / 0 warnings inwatch.rs(only the 3 pre-existing lints onmain). Whenever you have time for aVICTORY_ABpass on your 5600X + 3060, curious to see whether the 3–4 ms write-watch askings move!Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by davemathews12 on 2026-12-01T02:23:20Z.
@shiman1901 One more path your reservation split could cover, and possibly the costlier one.
writes_withcallskernel_written(first, last)directly (not throughask_once/ask_ahead/ask_rest), andkernel_writtenreturnsNonefor a range that isn't write-watched memory of one reservation. On the guarded path that falls toguard(), which returnsNonewhen the first block is neither protected nor absent and the range isn't all view (!known && !is_view). ThenShadow::update_freestoresNOT_WATCHEDfor the copy, and that is never undone: from then on every refresh copies the whole range (update_free→self.copy(from, to)), the cost draw.rs's comment puts at "some 30 ms a frame".So a copy whose range crosses a reservation boundary (or mixes a write-watched reservation with view memory) can be downgraded for the rest of the run by one refused asking that your split would have answered.
Two possible fixes, either fits your PR:
writes_with's asking throughask_by_res_runstoo, so a cross-reservation range is answered piece by piece;NOT_WATCHEDpermanent for a refusal of a range that spans reservations.In a diagnostics build,
say_not_watchednames the ranges that were downgraded, so a run with and withoutVICTORY_ASK_BY_RES=1would show whether any are cross-reservation and how many bytes a frame they cost. Code read only; I haven't measured how often it happens.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by davemathews12 on 2026-12-01T02:25:34Z.
Also:
say_not_watchedqueries onlyfrom, so for a range that crosses into a second allocation it would print a write-watched allocation and look like nothing is wrong. Logging theAllocationBaseofto - 1beside it would show the crossing directly.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shiman1901 on 2026-12-01T11:19:29Z.
Great catch @davemathews12 — both are now in
b668fa69:writes_withreservation split (watch.rs): addedkernel_written_by_res(first, last, ask_by_res()). The single-reservation fast path still callskernel_written(first, last)directly with zero cache overhead; when that is refused andVICTORY_ASK_BY_RES=1, it clips[first, last)withres_clip_pieces, verifies 100% watchable coverage (so mixed write-watched + view ranges still fall through toguard()), and queries each reservation run viares_runs(&tagged, 0). If a later run is refused mid-way after an earlier run already reset kernel dirty bits, the earlier run's dirty pages are noted intoJOURNALbefore falling back so no writes are lost. Added a 6th Windows unit test (writes_across_two_reservations_succeeds_with_ask_by_res) using two adjacentVirtualAlloc2MEM_REPLACE_PLACEHOLDER | MEM_WRITE_WATCHreservations.end_allocationinsay_not_watched(shadow.rs): now also queriesVirtualQuery(to - 1), logsend_allocationandcrosses_allocation = end_base != 0 && end_base != base, and deduplicatesSEENby(base, end_base)so cross-allocation ranges starting in an already-seen allocation are still reported.CI Clippy (
b668fa69) reports 0 errors / 0 warnings across both changed files (watch.rs,shadow.rs) inlibandlib test(blocked only by the 5 pre-existing F1-menu Clippy lints currently onmain5fabb46e).Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:42:01Z.
Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.
Pull request closed