From 7a63914dcaf56cd930a91b3874ffcca88e80ad3c Mon Sep 17 00:00:00 2001 From: gridoe <135166302+gridoe@users.noreply.github.com> Date: Tue, 24 Nov 2026 03:53:31 +0200 Subject: [PATCH] D3D12.X: bound wait_for_commit so big background compiles cannot deadlock wait_for_commit previously polled GlobalMemoryStatusEx in an unbounded loop until 3 GB of pagefile commit were free, while holding BIG_COMPILES (most: 1). On Windows with a system-managed dynamic pagefile (e.g. 32 GB RAM + 15 GB initial pagefile = ~47 GB commit limit), once a retail title commits ~41-42 GB, ullAvailPageFile remains below 3 GB because Windows only grows pagefile.sys on an actual allocation rather than while threads poll GlobalMemoryStatusEx. The background worker then slept indefinitely holding BIG_COMPILES, starving every queued compute PSO behind it. Add a 5 s grace timeout (wait_for_commit_with) while keeping BIG_COMPILES serialized to 1 and the 3 GB target unchanged, log once if the grace window expires, and add unit tests covering both headroom recovery and bounded return. Assisted-by: Antigravity (Gemini) --- crates/xcl/src/hle/d3d12x/real.rs | 69 +++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 8 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 26019155e..f9f0ca4eb 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -3318,17 +3318,46 @@ impl Drop for SlotGuard { static BIG_COMPILES: Slots = Slots { n: Mutex::new(0), cv: std::sync::Condvar::new(), most: 1 }; -/// Before a big driver compile in the background: until 3 GB of commit are free (each takes -/// ~1.5 GB; the title loading next to it must not run out – no minute's grace as for DXC). -fn wait_for_commit() { +/// Free commit target before starting a big background driver compile (`BIG_COMPILES`, most: 1). +const BIG_COMMIT_RESERVE: u64 = 3 << 30; +/// Maximum time `wait_for_commit` waits below `BIG_COMMIT_RESERVE`: on Windows with a +/// system-managed dynamic pagefile, `ullAvailPageFile` does not expand while threads only poll +/// `GlobalMemoryStatusEx`, so an unbounded loop deadlocks `BIG_COMPILES` once the title commits +/// near the initial pagefile limit. +const BIG_COMMIT_GRACE: std::time::Duration = std::time::Duration::from_secs(5); + +fn avail_pagefile_bytes() -> Option { use windows_sys::Win32::System::SystemInformation::{GlobalMemoryStatusEx, MEMORYSTATUSEX}; + let mut m: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; + m.dwLength = std::mem::size_of::() as u32; + (unsafe { GlobalMemoryStatusEx(&mut m) } != 0).then_some(m.ullAvailPageFile) +} + +fn wait_for_commit_with(mut avail: impl FnMut() -> Option, reserve: u64, grace: std::time::Duration, poll: std::time::Duration) -> bool { + let start = std::time::Instant::now(); loop { - let mut m: MEMORYSTATUSEX = unsafe { std::mem::zeroed() }; - m.dwLength = std::mem::size_of::() as u32; - if unsafe { GlobalMemoryStatusEx(&mut m) } == 0 || m.ullAvailPageFile >= 3 << 30 { - return; + match avail() { + None => return true, + Some(free) if free >= reserve => return true, + Some(_) if start.elapsed() >= grace => return false, + Some(_) => std::thread::sleep(poll), } - std::thread::sleep(std::time::Duration::from_millis(200)); + } +} + +/// Before a big driver compile in the background: wait up to `BIG_COMMIT_GRACE` for 3 GB of +/// free commit (each big compile takes ~1.5 GB), then proceed one at a time (`BIG_COMPILES`) +/// rather than deadlocking against an unexpanded Windows dynamic pagefile. +fn wait_for_commit() { + if !wait_for_commit_with(avail_pagefile_bytes, BIG_COMMIT_RESERVE, BIG_COMMIT_GRACE, std::time::Duration::from_millis(200)) { + static LOGGED: std::sync::Once = std::sync::Once::new(); + LOGGED.call_once(|| { + let free_mb = avail_pagefile_bytes().unwrap_or(0) >> 20; + crate::xlog_warn!( + "D3D12.X: proceeding with serialized big compute PSO compile after {} s with {free_mb} MB free commit", + BIG_COMMIT_GRACE.as_secs() + ); + }); } } @@ -5433,4 +5462,28 @@ mod console_format_query_tests { assert!(pc.SizeInBytes != u64::MAX && pc.SizeInBytes >= 1920 * 1080 * 4 && pc.Alignment > 0, "PC {format} as {}: {:#x}", d.Format.0, pc.SizeInBytes); } } + + #[test] + fn wait_for_commit_returns_when_headroom_appears() { + let mut calls = 0u32; + let met = wait_for_commit_with( + || { + calls += 1; + Some(if calls < 3 { 1 << 30 } else { 3 << 30 }) + }, + 3 << 30, + std::time::Duration::from_secs(2), + std::time::Duration::from_millis(5), + ); + assert!(met); + assert_eq!(calls, 3); + } + + #[test] + fn wait_for_commit_does_not_hang_forever_below_reserve() { + let start = std::time::Instant::now(); + let met = wait_for_commit_with(|| Some(2 << 30), 3 << 30, std::time::Duration::from_millis(30), std::time::Duration::from_millis(5)); + assert!(!met); + assert!(start.elapsed() < std::time::Duration::from_secs(1)); + } }