From 3e3c49165fa2760ab5bfe5e228dc99db4738e043 Mon Sep 17 00:00:00 2001 From: bvtterbroot <157503259+bvtterbroot@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:33:46 +0100 Subject: [PATCH] GPU: log the TDR settings, and warn when a GPU hang would freeze the PC With TdrLevel 0 Windows never resets a hung GPU: a slow or hung world frame freezes the whole desktop, and the runtime never sees a device removal it could report (#1054). With 1 the timeout is a blue screen. Both are set by hand, often to keep slow frames from being reset, and reports of "the PC froze" do not say so. When the host device is made on a hardware adapter, read TdrLevel, TdrDelay and TdrDdiDelay from HKLM\SYSTEM\CurrentControlSet\Control\ GraphicsDrivers, log them ("default" where a value is not set), and warn for TdrLevel 0 or 1 with what to set instead. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 42961e8e577e5f195db0982e34bb59e800998c77) --- crates/victory-gpu/Cargo.toml | 2 +- crates/victory-gpu/src/host.rs | 2 + crates/victory-gpu/src/lib.rs | 2 + crates/victory-gpu/src/tdr.rs | 145 +++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 crates/victory-gpu/src/tdr.rs diff --git a/crates/victory-gpu/Cargo.toml b/crates/victory-gpu/Cargo.toml index 5857ae1..cba972b 100644 --- a/crates/victory-gpu/Cargo.toml +++ b/crates/victory-gpu/Cargo.toml @@ -30,7 +30,7 @@ diagnostics = [] workspace = true [target.'cfg(windows)'.dependencies] -windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Security", "Win32_System_Diagnostics_Debug", "Win32_System_Kernel", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Performance", "Win32_System_ProcessStatus", "Win32_System_SystemInformation", "Win32_System_Threading"] } +windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Security", "Win32_System_Diagnostics_Debug", "Win32_System_Kernel", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Performance", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_Threading"] } windows = { workspace = true, features = [ "Win32_Graphics_Direct3D", "Win32_Graphics_Direct3D_Fxc", diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index edca8d4..78cef03 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -599,6 +599,8 @@ fn create() -> windows::core::Result { tracing::info!("host device on the software adapter (WARP)"); } else { D3D12CreateDevice(None, D3D_FEATURE_LEVEL_12_0, &mut device)?; + // Whether a GPU hang ends the run or freezes the PC (#1054). + crate::tdr::log(); } let device = device.ok_or_else(windows::core::Error::empty)?; // Before anything is made with it (`VICTORY_RECORD`). diff --git a/crates/victory-gpu/src/lib.rs b/crates/victory-gpu/src/lib.rs index e9a0cb2..532cf78 100644 --- a/crates/victory-gpu/src/lib.rs +++ b/crates/victory-gpu/src/lib.rs @@ -65,6 +65,8 @@ mod tiles; pub mod signature; mod switch; #[cfg(windows)] +mod tdr; +#[cfg(windows)] mod texture; #[cfg(windows)] mod watch; diff --git a/crates/victory-gpu/src/tdr.rs b/crates/victory-gpu/src/tdr.rs new file mode 100644 index 0000000..de0509b --- /dev/null +++ b/crates/victory-gpu/src/tdr.rs @@ -0,0 +1,145 @@ +//! Windows' timeout detection and recovery (TDR) for the host GPU, read once +//! when the host device is made (#1054). +//! +//! With `TdrLevel` 0 a GPU that stops answering is never reset: a slow or +//! hung world frame freezes the whole desktop, and the runtime never sees a +//! device removal it could report. With 1 the timeout is a blue screen. Both +//! are set by hand (often to keep slow frames from being reset), so the log +//! names them; raising `TdrDelay` is what gives slow frames room. + +use windows_sys::Win32::Foundation::ERROR_SUCCESS; +use windows_sys::Win32::System::Registry::{HKEY_LOCAL_MACHINE, RRF_RT_REG_DWORD, RegGetValueW}; + +/// Where Windows keeps the values. +const KEY: &str = r"SYSTEM\CurrentControlSet\Control\GraphicsDrivers"; + +/// The TDR values in the registry; `None` where one is not set and Windows +/// uses its default. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub(crate) struct Tdr { + /// 0 off, 1 bugcheck, 2 recover to VGA, 3 recover (the default). + pub(crate) level: Option, + /// Seconds the GPU may take before it counts as hung (default 2). + pub(crate) delay: Option, + /// Seconds a driver call may take (default 5). + pub(crate) ddi_delay: Option, +} + +impl Tdr { + pub(crate) fn read() -> Self { + Tdr { + level: dword("TdrLevel"), + delay: dword("TdrDelay"), + ddi_delay: dword("TdrDdiDelay"), + } + } + + /// What a hung GPU would do to the PC with these values, when that is + /// worse than a device removal the runtime reports. + pub(crate) fn warning(&self) -> Option<&'static str> { + match self.level { + Some(0) => Some( + "TdrLevel is 0: Windows never resets a hung GPU, so a GPU hang freezes \ + the whole PC instead of ending the run with a device removal; delete \ + TdrLevel (or set it to 3) and raise TdrDelay to give slow frames room", + ), + Some(1) => Some( + "TdrLevel is 1: a GPU timeout stops Windows with a blue screen instead \ + of ending the run with a device removal; delete TdrLevel (or set it \ + to 3) and raise TdrDelay to give slow frames room", + ), + _ => None, + } + } +} + +/// A DWORD value under [`KEY`]; `None` when it is missing or not a DWORD. +fn dword(name: &str) -> Option { + let key = wide(KEY); + let name = wide(name); + let mut value = 0u32; + let mut size = size_of::() as u32; + // SAFETY: `key` and `name` are NUL-terminated UTF-16 strings that outlive + // the call, and `value` has room for the `size` bytes a DWORD takes. + let status = unsafe { + RegGetValueW( + HKEY_LOCAL_MACHINE, + key.as_ptr(), + name.as_ptr(), + RRF_RT_REG_DWORD, + std::ptr::null_mut(), + (&raw mut value).cast(), + &mut size, + ) + }; + (status == ERROR_SUCCESS).then_some(value) +} + +fn wide(s: &str) -> Vec { + s.encode_utf16().chain(std::iter::once(0)).collect() +} + +fn shown(value: Option) -> String { + value.map_or_else(|| "default".to_owned(), |v| v.to_string()) +} + +/// Logs the values, and warns when a GPU hang would freeze the PC or stop +/// it with a blue screen. +pub(crate) fn log() { + let tdr = Tdr::read(); + tracing::info!( + level = %shown(tdr.level), + delay = %shown(tdr.delay), + ddi_delay = %shown(tdr.ddi_delay), + "GPU timeout detection (TdrLevel, TdrDelay, TdrDdiDelay)" + ); + if let Some(warning) = tdr.warning() { + tracing::warn!("{warning}"); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn level(level: Option) -> Tdr { + Tdr { + level, + ..Tdr::default() + } + } + + #[test] + fn off_and_bugcheck_warn() { + assert!(level(Some(0)).warning().unwrap().contains("freezes")); + assert!(level(Some(1)).warning().unwrap().contains("blue screen")); + } + + #[test] + fn recovery_and_the_default_do_not_warn() { + for l in [None, Some(2), Some(3)] { + assert_eq!(level(l).warning(), None, "TdrLevel {l:?}"); + } + } + + #[test] + fn delay_alone_does_not_warn() { + let tdr = Tdr { + level: Some(3), + delay: Some(60), + ddi_delay: Some(60), + }; + assert_eq!(tdr.warning(), None); + } + + #[test] + fn a_missing_value_reads_as_the_default() { + assert_eq!(dword("VictoryTestNoSuchValue"), None); + } + + #[test] + fn shown_names_the_default() { + assert_eq!(shown(None), "default"); + assert_eq!(shown(Some(60)), "60"); + } +}