diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index cae13913f2..b8dccd616f 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -3448,6 +3448,8 @@ pub unsafe fn init(dev: &ID3D12Device) -> Vec<(usize, usize, u64)> { let old_queue = QUEUE.lock().unwrap().replace(s.queue.clone()); let _old_rb = READBACKS.lock().unwrap().replace(Arc::clone(&s.readbacks)); let mut state = STATE.lock().unwrap(); + // (the ranges made of the state before are not this one's: `LAST_MAPPED_RANGES`) + MAPPED_GEN.fetch_add(1, Ordering::Release); let old = state.replace(s); let s = state.as_mut().unwrap(); for (a, b) in std::mem::take(&mut *EARLY.lock().unwrap()) { @@ -7084,6 +7086,12 @@ impl State { /// tiles of GPU views show (never the views' own addresses): where a CPU write has to reach the /// GPU. pub fn mapped_ranges() -> Vec<(usize, usize)> { + if let Some((gen, r)) = LAST_MAPPED_RANGES.lock().unwrap().as_ref() { + if *gen == MAPPED_GEN.load(Ordering::Acquire) { + RANGES_TAKEN.add(1, 1); + return r.clone(); + } + } let t0 = super::look_stats::deep().then(std::time::Instant::now); let g = lock_for_submit(); let _held = holding(Holder::Ranges); @@ -7091,9 +7099,24 @@ pub fn mapped_ranges() -> Vec<(usize, usize)> { HELD.add(HELD_RANGES_WAIT, t0.elapsed().as_micros() as u64); } let Some(s) = g.as_ref() else { return Vec::new() }; - s.watched_ranges() + let out = s.watched_ranges(); + // (read with the lock held: the runs change, and it goes up, only under it) + *LAST_MAPPED_RANGES.lock().unwrap() = Some((MAPPED_GEN.load(Ordering::Acquire), out.clone())); + RANGES_TAKEN.add(0, 1); + out } +/// The last `mapped_ranges` and the `MAPPED_GEN` they were made at: asked again with no tile +/// mapped, unmapped or made writable since, they are answered without `STATE`. The watched ranges +/// are made again whenever any part of their key changed (`title_textures::watch_key`: the +/// textures' blocks, the reservations, the shown pages too), and the lock may be held then by a +/// placed buffer, a commit, a CPU read or another queue's submit (a retail title's world: a submit +/// waited 0.1–0.3 ms for it there). Taken alone or under `STATE`, never around it. +static LAST_MAPPED_RANGES: Mutex)>> = Mutex::new(None); +/// `mapped_ranges` [made under the lock, answered from `LAST_MAPPED_RANGES`] (`XCL_LOOK_STATS=2`, +/// with the sparse lock split line) +static RANGES_TAKEN: super::look_stats::Sums<2> = super::look_stats::Sums::new(); + const LOOK_FIELDS: usize = 37; const ACTIVE_US: usize = 0; const REFRESH_WAIT_US: usize = 1; @@ -7760,7 +7783,12 @@ impl State { LOCK_SPLIT.add(LS_UNMAP_TILES, work(2)); if LOCK_SPLIT.add(LS_SUBMITS, 1) >= 1024 { let x = LOCK_SPLIT.take(); - crate::xlog_info!("D3D12.X: {}; {}", lock_split_line(&x), holders_line(&HELD.take(), x[LS_SUBMITS])); + let [made, kept] = RANGES_TAKEN.take(); + crate::xlog_info!( + "D3D12.X: {}; {}; mapped ranges asked for {made} times under the lock, {kept} times not", + lock_split_line(&x), + holders_line(&HELD.take(), x[LS_SUBMITS]) + ); } } let t_maintained = t_started.map(|_| t_locked); @@ -13563,4 +13591,45 @@ mod tests { assert_eq!(iso.rb.lock().unwrap().submitting, 0); } } + + /// `mapped_ranges` asked again with no tile mapped or unmapped since answers without the state's + /// lock; once `MAPPED_GEN` moved it takes the lock again. + #[test] + fn mapped_ranges_made_at_this_generation_need_no_lock() { + let fake = vec![(0x7000_0000usize, 0x7001_0000usize)]; + // (other tests move the generation too: tried a few times) + let mut answered = false; + for _ in 0..20 { + let g = STATE.lock().unwrap_or_else(|e| e.into_inner()); + let gen = MAPPED_GEN.load(Ordering::Acquire); + *LAST_MAPPED_RANGES.lock().unwrap() = Some((gen, fake.clone())); + let (tx, rx) = std::sync::mpsc::channel(); + let asker = std::thread::spawn(move || { + let _ = tx.send(mapped_ranges()); + }); + let got = rx.recv_timeout(std::time::Duration::from_millis(500)); + let moved = MAPPED_GEN.load(Ordering::Acquire) != gen; + drop(g); + asker.join().unwrap(); + if moved { + continue; + } + assert_eq!(got.expect("answered with the state's lock held elsewhere"), fake); + answered = true; + break; + } + assert!(answered, "the generation never stood still"); + // a tile mapped or unmapped since: made again, under the lock + let g = STATE.lock().unwrap_or_else(|e| e.into_inner()); + MAPPED_GEN.fetch_add(1, Ordering::Release); + let (tx, rx) = std::sync::mpsc::channel(); + let asker = std::thread::spawn(move || { + let _ = tx.send(mapped_ranges()); + }); + assert!(rx.recv_timeout(std::time::Duration::from_millis(50)).is_err(), "a stale answer without the lock"); + drop(g); + let again = rx.recv_timeout(std::time::Duration::from_secs(10)).expect("answered once the lock was free"); + asker.join().unwrap(); + assert_ne!(again, fake); + } }