diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 0210c522f1..1d72b95008 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -3320,6 +3320,129 @@ fn wait_is_resolved(outcome: GpuWaitOutcome, cpu_satisfied: bool) -> bool { } } +/// Why a `Wait32/64BitValueX` the memory did not hold yet was waited for on the submitting +/// thread instead of the GPU (`try_gpu_wait`'s first refusal). Counted so that it is known which +/// waits are worth moving to the GPU, and how much submit-thread time each kind costs. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum CpuWaitWhy { + /// `Wait32BitValueX` + Width32, + /// not `GREATER_EQUAL` + NotGreaterEqual, + /// a mask other than all ones + Masked, + /// `>= 0` + Zero, + /// no `Write64BitValue*` was recorded to the address (written by the CPU or a shader) + Untracked, + /// the address was written a smaller value after a larger one + NotMonotonic, + /// no recorded write reaches the value yet + NotRecorded, + /// eligible, but the GPU wait was not set up (no queue, no fence, `queue.Wait` failed, or + /// the fence passed the value while the memory does not hold it) + Other, +} + +impl CpuWaitWhy { + const ALL: [Self; 8] = [ + Self::Width32, + Self::NotGreaterEqual, + Self::Masked, + Self::Zero, + Self::Untracked, + Self::NotMonotonic, + Self::NotRecorded, + Self::Other, + ]; + + fn name(self) -> &'static str { + match self { + Self::Width32 => "32-bit", + Self::NotGreaterEqual => "not >=", + Self::Masked => "masked", + Self::Zero => ">= 0", + Self::Untracked => "untracked", + Self::NotMonotonic => "not monotonic", + Self::NotRecorded => "not recorded", + Self::Other => "other", + } + } +} + +/// `CpuWaitWhy` of `w`, in `try_gpu_wait`'s order. `cell`: (non-monotonic, largest recorded +/// value) of its address in the sync graph, if it has one. +fn cpu_wait_why(w: &PendingWait, cell: Option<(bool, u64)>) -> CpuWaitWhy { + if w.width != 8 { + CpuWaitWhy::Width32 + } else if w.comparison != D3D12_COMPARISON_FUNC_GREATER_EQUAL.0 as u32 { + CpuWaitWhy::NotGreaterEqual + } else if w.mask != u64::MAX { + CpuWaitWhy::Masked + } else if w.value == 0 { + CpuWaitWhy::Zero + } else { + match cell { + None => CpuWaitWhy::Untracked, + Some((true, _)) => CpuWaitWhy::NotMonotonic, + Some((false, recorded)) if recorded < w.value => CpuWaitWhy::NotRecorded, + Some(_) => CpuWaitWhy::Other, + } + } +} + +/// CPU waits since the last report, by `CpuWaitWhy`: (waits, nanoseconds) +static CPU_WAITS: [(AtomicU64, AtomicU64); 8] = [const { (AtomicU64::new(0), AtomicU64::new(0)) }; 8]; +/// waits the memory did not hold that went to the GPU since the last report +static GPU_WAITS: AtomicU64 = AtomicU64::new(0); +/// when the last report was made, in milliseconds since `CPU_WAIT_EPOCH` +static CPU_WAIT_REPORTED_MS: AtomicU64 = AtomicU64::new(0); +static CPU_WAIT_EPOCH: OnceLock = OnceLock::new(); +/// a report at most this often, and only when a wait was made on the CPU +const CPU_WAIT_REPORT_MS: u64 = 10_000; + +/// A wait of kind `why` took `took` on the submitting thread; reports the counts at most every +/// `CPU_WAIT_REPORT_MS`. +fn note_cpu_wait(why: CpuWaitWhy, took: Duration) { + let i = CpuWaitWhy::ALL.iter().position(|&k| k == why).unwrap(); + CPU_WAITS[i].0.fetch_add(1, Ordering::Relaxed); + CPU_WAITS[i].1.fetch_add(took.as_nanos().min(u64::MAX as u128) as u64, Ordering::Relaxed); + let now = CPU_WAIT_EPOCH.get_or_init(Instant::now).elapsed().as_millis() as u64; + let last = CPU_WAIT_REPORTED_MS.load(Ordering::Relaxed); + if now.saturating_sub(last) < CPU_WAIT_REPORT_MS + || CPU_WAIT_REPORTED_MS.compare_exchange(last, now, Ordering::Relaxed, Ordering::Relaxed).is_err() + { + return; + } + let counts: Vec<(u64, u64)> = + CPU_WAITS.iter().map(|(n, ns)| (n.swap(0, Ordering::Relaxed), ns.swap(0, Ordering::Relaxed))).collect(); + let gpu = GPU_WAITS.swap(0, Ordering::Relaxed); + if let Some(line) = cpu_wait_report(&counts, gpu, Duration::from_millis(now - last)) { + crate::xlog_info!("{line}"); + } +} + +/// The report line of `counts` (`CPU_WAITS` order), `gpu` waits on the GPU, over `span`; `None` +/// when no wait was made on the CPU. +fn cpu_wait_report(counts: &[(u64, u64)], gpu: u64, span: Duration) -> Option { + let (n, ns) = counts.iter().fold((0u64, 0u64), |(a, b), &(n, ns)| (a + n, b.saturating_add(ns))); + if n == 0 { + return None; + } + let kinds: Vec = CpuWaitWhy::ALL + .iter() + .zip(counts) + .filter(|(_, &(n, _))| n != 0) + .map(|(k, &(n, ns))| format!("{} {n} ({:.1} ms)", k.name(), ns as f64 / 1e6)) + .collect(); + Some(format!( + "D3D12.X: memory waits on the CPU in the last {:.0} s: {n} ({:.1} ms of submit threads; {}), on the GPU: {gpu}", + span.as_secs_f64(), + ns as f64 / 1e6, + kinds.join(", ") + )) +} + /// Attempt to satisfy a `Wait64BitValueX(GREATER_EQUAL, u64::MAX)` on the GPU via /// `queue.Wait(&fence, w.value)` (or same-queue FIFO ordering) instead of blocking the /// submission thread on the CPU. @@ -3493,12 +3616,21 @@ unsafe fn wait_all_on_noting_cpu( } let outcome = try_gpu_wait(queue, w, list_writes); if wait_is_resolved(outcome, false) { + if outcome == GpuWaitOutcome::Handled { + GPU_WAITS.fetch_add(1, Ordering::Relaxed); + } continue; } on_cpu = true; sync_trace(|| format!("q{} wait {:#x} (op {}) {:#x}: on the CPU", queue_id(queue as usize), w.va, w.comparison, w.value)); + let why = cpu_wait_why(w, { + let g = SYNC_GRAPH.lock().unwrap(); + g.as_ref().and_then(|gr| gr.cells.get(&w.va)).map(|c| (c.non_monotonic, c.recorded_max)) + }); let memory_stall = crate::stall::span(crate::stall::StallKind::MemorySynchronization); + let started = Instant::now(); let satisfied = w.wait_until_satisfied(timeout); + note_cpu_wait(why, started.elapsed()); drop(memory_stall); if wait_is_resolved(outcome, satisfied) { continue; @@ -7742,6 +7874,35 @@ mod tests { assert_eq!(compare_u64(8, 0, 1), Some(true)); // ALWAYS } + #[test] + fn a_cpu_wait_is_counted_by_why_the_gpu_did_not_take_it() { + let ge = D3D12_COMPARISON_FUNC_GREATER_EQUAL.0 as u32; + let w = |width, comparison, value, mask| PendingWait { va: 0x1000, width, comparison, value, mask, after_write64: false }; + let tracked = Some((false, 9)); + assert_eq!(cpu_wait_why(&w(4, ge, 5, u64::MAX), tracked), CpuWaitWhy::Width32); + assert_eq!(cpu_wait_why(&w(8, D3D12_COMPARISON_FUNC_EQUAL.0 as u32, 5, u64::MAX), tracked), CpuWaitWhy::NotGreaterEqual); + assert_eq!(cpu_wait_why(&w(8, ge, 5, 0xFFFF), tracked), CpuWaitWhy::Masked); + assert_eq!(cpu_wait_why(&w(8, ge, 0, u64::MAX), tracked), CpuWaitWhy::Zero); + assert_eq!(cpu_wait_why(&w(8, ge, 5, u64::MAX), None), CpuWaitWhy::Untracked); + assert_eq!(cpu_wait_why(&w(8, ge, 5, u64::MAX), Some((true, 9))), CpuWaitWhy::NotMonotonic); + assert_eq!(cpu_wait_why(&w(8, ge, 10, u64::MAX), tracked), CpuWaitWhy::NotRecorded); + assert_eq!(cpu_wait_why(&w(8, ge, 9, u64::MAX), tracked), CpuWaitWhy::Other); + // the 32-bit check comes first, as in try_gpu_wait + assert_eq!(cpu_wait_why(&w(4, D3D12_COMPARISON_FUNC_EQUAL.0 as u32, 0, 0xFF), None), CpuWaitWhy::Width32); + } + + #[test] + fn the_cpu_wait_report_names_only_the_kinds_that_happened() { + let mut counts = [(0u64, 0u64); 8]; + assert_eq!(cpu_wait_report(&counts, 7, Duration::from_secs(10)), None, "no CPU wait, no report"); + counts[0] = (3, 1_500_000); + counts[4] = (1, 2_000_000); + assert_eq!( + cpu_wait_report(&counts, 7, Duration::from_secs(10)).unwrap(), + "D3D12.X: memory waits on the CPU in the last 10 s: 4 (3.5 ms of submit threads; 32-bit 3 (1.5 ms), untracked 1 (2.0 ms)), on the GPU: 7" + ); + } + #[test] fn pending_wait_32_and_64_bit_with_mask_and_cross_thread_signal() { let cell32 = Arc::new(AtomicU32::new(0xDEAD_0000));