tdr.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //! Windows' timeout detection and recovery (TDR) for the host GPU, read once
  2. //! when the host device is made (#1054).
  3. //!
  4. //! With `TdrLevel` 0 a GPU that stops answering is never reset: a slow or
  5. //! hung world frame freezes the whole desktop, and the runtime never sees a
  6. //! device removal it could report. With 1 the timeout is a blue screen. Both
  7. //! are set by hand (often to keep slow frames from being reset), so the log
  8. //! names them; raising `TdrDelay` is what gives slow frames room.
  9. use windows_sys::Win32::Foundation::ERROR_SUCCESS;
  10. use windows_sys::Win32::System::Registry::{HKEY_LOCAL_MACHINE, RRF_RT_REG_DWORD, RegGetValueW};
  11. /// Where Windows keeps the values.
  12. const KEY: &str = r"SYSTEM\CurrentControlSet\Control\GraphicsDrivers";
  13. /// The TDR values in the registry; `None` where one is not set and Windows
  14. /// uses its default.
  15. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
  16. pub(crate) struct Tdr {
  17. /// 0 off, 1 bugcheck, 2 recover to VGA, 3 recover (the default).
  18. pub(crate) level: Option<u32>,
  19. /// Seconds the GPU may take before it counts as hung (default 2).
  20. pub(crate) delay: Option<u32>,
  21. /// Seconds a driver call may take (default 5).
  22. pub(crate) ddi_delay: Option<u32>,
  23. }
  24. impl Tdr {
  25. pub(crate) fn read() -> Self {
  26. Tdr {
  27. level: dword("TdrLevel"),
  28. delay: dword("TdrDelay"),
  29. ddi_delay: dword("TdrDdiDelay"),
  30. }
  31. }
  32. /// What a hung GPU would do to the PC with these values, when that is
  33. /// worse than a device removal the runtime reports.
  34. pub(crate) fn warning(&self) -> Option<&'static str> {
  35. match self.level {
  36. Some(0) => Some(
  37. "TdrLevel is 0: Windows never resets a hung GPU, so a GPU hang freezes \
  38. the whole PC instead of ending the run with a device removal; delete \
  39. TdrLevel (or set it to 3) and raise TdrDelay to give slow frames room",
  40. ),
  41. Some(1) => Some(
  42. "TdrLevel is 1: a GPU timeout stops Windows with a blue screen instead \
  43. of ending the run with a device removal; delete TdrLevel (or set it \
  44. to 3) and raise TdrDelay to give slow frames room",
  45. ),
  46. _ => None,
  47. }
  48. }
  49. }
  50. /// A DWORD value under [`KEY`]; `None` when it is missing or not a DWORD.
  51. fn dword(name: &str) -> Option<u32> {
  52. let key = wide(KEY);
  53. let name = wide(name);
  54. let mut value = 0u32;
  55. let mut size = size_of::<u32>() as u32;
  56. // SAFETY: `key` and `name` are NUL-terminated UTF-16 strings that outlive
  57. // the call, and `value` has room for the `size` bytes a DWORD takes.
  58. let status = unsafe {
  59. RegGetValueW(
  60. HKEY_LOCAL_MACHINE,
  61. key.as_ptr(),
  62. name.as_ptr(),
  63. RRF_RT_REG_DWORD,
  64. std::ptr::null_mut(),
  65. (&raw mut value).cast(),
  66. &mut size,
  67. )
  68. };
  69. (status == ERROR_SUCCESS).then_some(value)
  70. }
  71. fn wide(s: &str) -> Vec<u16> {
  72. s.encode_utf16().chain(std::iter::once(0)).collect()
  73. }
  74. fn shown(value: Option<u32>) -> String {
  75. value.map_or_else(|| "default".to_owned(), |v| v.to_string())
  76. }
  77. /// Logs the values, and warns when a GPU hang would freeze the PC or stop
  78. /// it with a blue screen.
  79. pub(crate) fn log() {
  80. let tdr = Tdr::read();
  81. tracing::info!(
  82. level = %shown(tdr.level),
  83. delay = %shown(tdr.delay),
  84. ddi_delay = %shown(tdr.ddi_delay),
  85. "GPU timeout detection (TdrLevel, TdrDelay, TdrDdiDelay)"
  86. );
  87. if let Some(warning) = tdr.warning() {
  88. tracing::warn!("{warning}");
  89. }
  90. }
  91. #[cfg(test)]
  92. mod tests {
  93. use super::*;
  94. fn level(level: Option<u32>) -> Tdr {
  95. Tdr {
  96. level,
  97. ..Tdr::default()
  98. }
  99. }
  100. #[test]
  101. fn off_and_bugcheck_warn() {
  102. assert!(level(Some(0)).warning().unwrap().contains("freezes"));
  103. assert!(level(Some(1)).warning().unwrap().contains("blue screen"));
  104. }
  105. #[test]
  106. fn recovery_and_the_default_do_not_warn() {
  107. for l in [None, Some(2), Some(3)] {
  108. assert_eq!(level(l).warning(), None, "TdrLevel {l:?}");
  109. }
  110. }
  111. #[test]
  112. fn delay_alone_does_not_warn() {
  113. let tdr = Tdr {
  114. level: Some(3),
  115. delay: Some(60),
  116. ddi_delay: Some(60),
  117. };
  118. assert_eq!(tdr.warning(), None);
  119. }
  120. #[test]
  121. fn a_missing_value_reads_as_the_default() {
  122. assert_eq!(dword("VictoryTestNoSuchValue"), None);
  123. }
  124. #[test]
  125. fn shown_names_the_default() {
  126. assert_eq!(shown(None), "default");
  127. assert_eq!(shown(Some(60)), "60");
  128. }
  129. }