Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/xcl/src/gpu/agility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,18 @@ pub fn enable_dred_once() -> &'static Option<Result<(), String>> {
})
}

/// What `enable_dred_once` (`done`) switched on for the request `request`: None, nothing (unset,
/// or the settings were unavailable); else the page-fault output, and whether the breadcrumbs too.
fn dred_on(done: Option<&Option<Result<(), String>>>, request: Option<bool>) -> Option<bool> {
if matches!(done, Some(Some(Ok(())))) { request } else { None }
}

/// What `enable_dred_once` switched on: None, nothing; else whether the breadcrumbs are on too
/// (not with `XCL_DRED=pagefault`).
pub fn dred_switched_on() -> Option<bool> {
dred_on(DRED.get(), dred_request(std::env::var_os("XCL_DRED")))
}

/// Enable the debug layer at most once per process. The GPU test runner may create a
/// device before D3D12.X starts; enabling it again after that removes the device.
pub fn enable_debug_layer() -> &'static Result<(), String> {
Expand Down
13 changes: 13 additions & 0 deletions crates/xcl/src/gpu/agility_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,16 @@ fn resolve_dir_handles_explicit_override_and_implicit_fallback() {

let _ = std::fs::remove_dir_all(&root);
}

/// DRED's outputs count as switched on only when `enable_dred_once` set them: the breadcrumbs
/// with `XCL_DRED=1`, the page faults with it or `=pagefault`.
#[test]
fn dred_outputs_are_on_only_when_the_settings_took() {
let ok: Option<Result<(), String>> = Some(Ok(()));
assert_eq!(dred_on(Some(&ok), Some(true)), Some(true), "XCL_DRED=1");
assert_eq!(dred_on(Some(&ok), Some(false)), Some(false), "XCL_DRED=pagefault: page faults only");
let failed: Option<Result<(), String>> = Some(Err("unavailable".into()));
assert_eq!(dred_on(Some(&failed), Some(true)), None, "requested, but the settings were unavailable");
assert_eq!(dred_on(Some(&None), None), None, "not requested");
assert_eq!(dred_on(None, Some(true)), None, "not set up yet");
}
113 changes: 100 additions & 13 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12427,6 +12427,42 @@ mod dred_tests {
assert_eq!(over_video_budget(30 << 30, 0), None, "no budget known (the query failed)");
}

/// DRED's page-fault output at VA 0 with no allocations named is a fault at VA 0 when the
/// device state says a page fault happened (7), none when the state rules one out, and either
/// when the state is unknown (0) or not available.
#[test]
fn dred_output_at_va_0_with_no_allocations_follows_the_device_state() {
let (unknown, hung, fault, page_fault) = (D3D12_DRED_DEVICE_STATE_UNKNOWN.0, D3D12_DRED_DEVICE_STATE_HUNG.0, D3D12_DRED_DEVICE_STATE_FAULT.0, D3D12_DRED_DEVICE_STATE_PAGEFAULT.0);
assert_eq!(dred_unplaced_page_fault(Some(page_fault), 0, false, false), None, "state 7: a real fault at VA 0, logged as one");
for state in [Some(unknown), None] {
let line = dred_unplaced_page_fault(state, 0, false, false).expect("VA 0 and no allocations is not logged as a fault at VA 0");
assert!(line.contains("no fault recorded, or"), "{state:?}: neither reading is ruled out: {line}");
}
for state in [hung, fault] {
let line = dred_unplaced_page_fault(Some(state), 0, false, false).expect("not a page fault");
assert!(line.contains("no page fault"), "state {state}: {line}");
}
for state in [Some(unknown), Some(page_fault), None] {
assert_eq!(dred_unplaced_page_fault(state, 0x1_2345_0000, false, false), None);
assert_eq!(dred_unplaced_page_fault(state, 0, true, false), None, "an allocation named: logged as a fault");
assert_eq!(dred_unplaced_page_fault(state, 0, false, true), None);
}
}

/// An output DRED did not give is told apart from one it was not asked for.
#[test]
fn missing_dred_output_says_whether_it_was_switched_on() {
assert_eq!(dred_page_fault_missing(0x8000_4005, true), "DRED page-fault output not available (0x80004005)");
assert!(dred_page_fault_missing(0x8000_4005, false).contains("not switched on"));
assert_eq!(dred_breadcrumbs_missing(Ok(true), true), None, "breadcrumbs there");
assert_eq!(dred_breadcrumbs_missing(Ok(true), false), None);
for got in [Ok(false), Err(0x8000_4005)] {
assert!(dred_breadcrumbs_missing(got, false).is_some_and(|l| l.contains("not switched on; XCL_DRED=1 does")), "{got:?}");
}
assert_eq!(dred_breadcrumbs_missing(Err(0x8000_4005), true).as_deref(), Some("DRED: no breadcrumbs (0x80004005)"));
assert_eq!(dred_breadcrumbs_missing(Ok(false), true).as_deref(), Some("DRED: no breadcrumbs (empty output)"));
}

#[test]
fn a_late_partially_executed_list_is_reported_before_unstarted_lists() {
let progress = [0u32, 1, 2];
Expand All @@ -12449,6 +12485,42 @@ mod dred_tests {
}
}

/// The line for DRED's page-fault output when it is VA 0 with no allocations named, given the
/// device state DRED reported (`state`, None: not available). That is what it gives with no fault
/// recorded, but the lists may be empty for a real fault too: the state tells them apart when it
/// is known (a page fault, or another removal), else it is logged as either. `None`: an address
/// other than 0, an allocation named or a page-fault state, a fault to log.
fn dred_unplaced_page_fault(state: Option<i32>, va: u64, existing: bool, freed: bool) -> Option<&'static str> {
if va != 0 || existing || freed || state == Some(D3D12_DRED_DEVICE_STATE_PAGEFAULT.0) {
return None;
}
Some(match state {
Some(s) if s != D3D12_DRED_DEVICE_STATE_UNKNOWN.0 => "DRED page-fault output: VA 0x0, no allocations named (no page fault: the device state is another removal)",
_ => "DRED page-fault output: VA 0x0, no allocations named (no fault recorded, or a fault DRED could not place)",
})
}

/// The line for a page-fault output DRED did not give (`hr`), with the page-fault output
/// switched on (`on`, `agility::dred_switched_on`) or not.
fn dred_page_fault_missing(hr: u32, on: bool) -> String {
if on {
format!("DRED page-fault output not available ({hr:#x})")
} else {
"DRED: no page-fault output (not switched on; XCL_DRED=1 or XCL_DRED=pagefault does)".into()
}
}

/// The line for breadcrumbs DRED gave (`Ok(true)`: some, `Ok(false)`: none) or did not (`Err`),
/// with the breadcrumbs switched on (`on`) or not. None: there are some to report.
fn dred_breadcrumbs_missing(got: Result<bool, u32>, on: bool) -> Option<String> {
match got {
Ok(true) => None,
_ if !on => Some("DRED: no breadcrumbs (not switched on; XCL_DRED=1 does)".into()),
Err(hr) => Some(format!("DRED: no breadcrumbs ({hr:#x})")),
Ok(false) => Some("DRED: no breadcrumbs (empty output)".into()),
}
}

/// If the device was removed: the reason, DRED's page fault (address, the allocations around
/// it) and the operations each command list got to – into the log, once.
pub fn log_device_removed() {
Expand All @@ -12467,14 +12539,26 @@ pub fn log_device_removed() {
crate::xlog_error!("system commit at the removal: {} of {} MB ({} MB left)", commit >> 20, limit >> 20, limit.saturating_sub(commit) >> 20);
}
unsafe {
// (DRED 1.2: 3 hung, 6 fault, 7 page fault; 0 when the removal did not come from work
// the GPU was doing – then there is no page fault and there are no breadcrumbs either)
match dev.cast::<ID3D12DeviceRemovedExtendedData2>() {
Ok(d) => crate::xlog_error!("DRED device state: {}", d.GetDeviceState().0),
Err(e) => crate::xlog_error!("DRED device state: not available ({:#x})", e.code().0 as u32),
}
// (DRED 1.2: 3 hung, 6 fault, 7 page fault; 0 is unknown, which does not rule out a fault
// of the GPU's work or a page fault)
let state = match dev.cast::<ID3D12DeviceRemovedExtendedData2>() {
Ok(d) => {
let state = d.GetDeviceState();
crate::xlog_error!("DRED device state: {}{}", state.0, if state == D3D12_DRED_DEVICE_STATE_UNKNOWN { " (unknown)" } else { "" });
Some(state.0)
}
Err(e) => {
crate::xlog_error!("DRED device state: not available ({:#x})", e.code().0 as u32);
None
}
};
let switched_on = crate::gpu::agility::dred_switched_on();
let Ok(dred) = dev.cast::<ID3D12DeviceRemovedExtendedData>() else { return };
if let Ok(pf) = dred.GetPageFaultAllocationOutput() {
let page_fault = dred.GetPageFaultAllocationOutput();
if let Err(e) = &page_fault {
crate::xlog_error!("{}", dred_page_fault_missing(e.code().0 as u32, switched_on.is_some()));
}
if let Ok(pf) = page_fault {
let names = |mut n: *const D3D12_DRED_ALLOCATION_NODE| {
let mut v = Vec::new();
while !n.is_null() && v.len() < 8 {
Expand All @@ -12486,14 +12570,17 @@ pub fn log_device_removed() {
v.join(", ")
};
let va = pf.PageFaultVA;
let window = uma_window_vas().iter().position(|&w| va >= w && va < w + 0x1_0000_0000).map(|k| format!(" = window {k} + {:#x}", va - uma_window_vas()[k])).unwrap_or_default();
crate::xlog_error!("DRED page fault at GPU VA {va:#x}{window}; allocations there: [{}]; recently freed: [{}]", names(pf.pHeadExistingAllocationNode), names(pf.pHeadRecentFreedAllocationNode));
if let Some(line) = dred_unplaced_page_fault(state, va, !pf.pHeadExistingAllocationNode.is_null(), !pf.pHeadRecentFreedAllocationNode.is_null()) {
crate::xlog_error!("{line}");
} else {
let window = uma_window_vas().iter().position(|&w| va >= w && va < w + 0x1_0000_0000).map(|k| format!(" = window {k} + {:#x}", va - uma_window_vas()[k])).unwrap_or_default();
crate::xlog_error!("DRED page fault at GPU VA {va:#x}{window}; allocations there: [{}]; recently freed: [{}]", names(pf.pHeadExistingAllocationNode), names(pf.pHeadRecentFreedAllocationNode));
}
}
let breadcrumbs = dred.GetAutoBreadcrumbsOutput();
match &breadcrumbs {
Err(e) => crate::xlog_error!("DRED: no breadcrumbs ({:#x})", e.code().0 as u32),
Ok(bc) if bc.pHeadAutoBreadcrumbNode.is_null() => crate::xlog_error!("DRED: no breadcrumbs (no list was in flight)"),
Ok(_) => {}
let got = breadcrumbs.as_ref().map(|bc| !bc.pHeadAutoBreadcrumbNode.is_null()).map_err(|e| e.code().0 as u32);
if let Some(line) = dred_breadcrumbs_missing(got, switched_on == Some(true)) {
crate::xlog_error!("{line}");
}
if let Ok(bc) = breadcrumbs {
report_dred_lists(bc.pHeadAutoBreadcrumbNode, |node, done| {
Expand Down
Loading