diff --git a/crates/xcl/src/hle/d3d12x/build_queue.rs b/crates/xcl/src/hle/d3d12x/build_queue.rs index fec7b62dfe..c445bb7817 100644 --- a/crates/xcl/src/hle/d3d12x/build_queue.rs +++ b/crates/xcl/src/hle/d3d12x/build_queue.rs @@ -249,30 +249,37 @@ impl Inner { } } + fn take_ready(&self, state: &mut State, low: Priority, high: Priority) -> Option<(Work, Arc)> { + for priority in (low.index()..=high.index()).rev() { + let priority = match priority { + 0 => Priority::Speculative, + 1 => Priority::Preload, + 2 => Priority::BoundSoon, + _ => Priority::Foreground, + }; + while let Some(ticket) = state.pop(priority) { + let current = state.jobs.get(&ticket.id).is_some_and(|job| { + job.priority == priority && job.generation == ticket.generation + }); + if current { + let job = state.jobs.remove(&ticket.id).unwrap(); + if priority == Priority::Preload && job.preload_rank.is_some() { + self.ranked_preloads.started.fetch_add(1, Ordering::Relaxed); + } + job.completion.set(JobStatus::Running); + return Some((job.work, job.completion)); + } + } + } + None + } + fn take(&self, wait: bool, low: Priority, high: Priority) -> Option<(Work, Arc)> { let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner()); let changed = self.changed(low, high); loop { - for priority in (low.index()..=high.index()).rev() { - let priority = match priority { - 0 => Priority::Speculative, - 1 => Priority::Preload, - 2 => Priority::BoundSoon, - _ => Priority::Foreground, - }; - while let Some(ticket) = state.pop(priority) { - let current = state.jobs.get(&ticket.id).is_some_and(|job| { - job.priority == priority && job.generation == ticket.generation - }); - if current { - let job = state.jobs.remove(&ticket.id).unwrap(); - if priority == Priority::Preload && job.preload_rank.is_some() { - self.ranked_preloads.started.fetch_add(1, Ordering::Relaxed); - } - job.completion.set(JobStatus::Running); - return Some((job.work, job.completion)); - } - } + if let Some(job) = self.take_ready(&mut state, low, high) { + return Some(job); } if state.stopped || !wait { return None; @@ -281,6 +288,20 @@ impl Inner { } } + fn take_while(&self, low: Priority, high: Priority, keep_running: &mut impl FnMut() -> bool) -> Option<(Work, Arc)> { + let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner()); + let changed = self.changed(low, high); + loop { + if !keep_running() || state.stopped { + return None; + } + if let Some(job) = self.take_ready(&mut state, low, high) { + return Some(job); + } + state = changed.wait_timeout(state, std::time::Duration::from_millis(250)).unwrap_or_else(|e| e.into_inner()).0; + } + } + fn shutdown(&self) { let jobs = { let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner()); @@ -400,6 +421,13 @@ impl Worker { run(job); } } + + /// Run until `keep_running` closes, checking it while idle without stopping the queue. + pub(super) fn run_while(self, mut keep_running: impl FnMut() -> bool) { + while let Some(job) = self.inner.take_while(self.low, self.high, &mut keep_running) { + run(job); + } + } } fn run((work, completion): (Work, Arc)) { @@ -513,6 +541,23 @@ mod tests { preload_thread.join().unwrap(); } + #[test] + fn conditional_foreground_worker_retires_without_stopping_the_queue() { + let q = BuildQueue::new(); + let keep = Arc::new(AtomicBool::new(true)); + let worker = q.worker_between(Priority::BoundSoon, Priority::Foreground); + let worker_keep = keep.clone(); + let thread = std::thread::spawn(move || worker.run_while(|| worker_keep.load(Ordering::Acquire))); + let preload = q.submit(Priority::Preload, Box::new(|| {})).unwrap(); + let (tx, rx) = std::sync::mpsc::channel(); + q.submit(Priority::Foreground, Box::new(move || tx.send(()).unwrap())).unwrap(); + rx.recv_timeout(std::time::Duration::from_secs(2)).unwrap(); + assert_eq!(preload.status(), JobStatus::Queued, "the replacement stays in its priority band"); + keep.store(false, Ordering::Release); + thread.join().unwrap(); + assert!(q.try_run_one_between(Priority::Preload, Priority::Preload), "retirement leaves the queue usable"); + } + #[test] fn promoted_preload_moves_to_the_foreground_worker_band() { let q = BuildQueue::new(); diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index 36c522ffbe..0f7ad5e4df 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -74,6 +74,8 @@ thread_local! { static BUILDING: std::cell::RefCell>> = const { std::cell::RefCell::new(None) }; /// this is one of the workers reserved for low-priority preload builds static PRELOAD_WORKER: std::cell::Cell = const { std::cell::Cell::new(false) }; + /// this thread serves the normal (`BoundSoon` through `Foreground`) worker band + static NORMAL_WORKER: std::cell::Cell = const { std::cell::Cell::new(false) }; } /// This thread is a worker and one of the title's threads waits for what it is building: the @@ -471,11 +473,85 @@ fn start_normal_workers() { let n = crate::gpu::recomp::dxc::compile_threads(); for k in 0..n { let worker = q.worker_between(Priority::BoundSoon, Priority::Foreground); - let _ = std::thread::Builder::new().name(format!("xcl-pso-{k}")).stack_size(64 << 20).spawn(move || worker.run()); + let _ = std::thread::Builder::new().name(format!("xcl-pso-{k}")).stack_size(64 << 20).spawn(move || { + NORMAL_WORKER.set(true); + worker.run(); + }); } }); } +/// Normal workers waiting for a huge driver-compile slot, and bounded replacements serving their +/// priority band. Preload workers have their own band and do not need foreground replacements. +static PARKED_NORMAL: AtomicUsize = AtomicUsize::new(0); +static NORMAL_HELPERS: AtomicUsize = AtomicUsize::new(0); +static NORMAL_HELPERS_STARTED: AtomicUsize = AtomicUsize::new(0); + +/// A normal pipeline worker is about to wait for a huge driver-compile slot. A bounded helper +/// serves its queue until workers return; calls from title and preload threads do nothing. +pub(super) fn parked_for_big_compile() -> ParkedWorker { + if !NORMAL_WORKER.get() { + return ParkedWorker(false); + } + let parked = PARKED_NORMAL.fetch_add(1, Ordering::AcqRel) + 1; + let most = parked.min(crate::gpu::recomp::dxc::compile_threads()); + let mut helpers = NORMAL_HELPERS.load(Ordering::Acquire); + while helpers < most { + match NORMAL_HELPERS.compare_exchange(helpers, helpers + 1, Ordering::AcqRel, Ordering::Acquire) { + Ok(_) => { + spawn_normal_helper(helpers); + break; + } + Err(now) => helpers = now, + } + } + ParkedWorker(true) +} + +pub(super) struct ParkedWorker(bool); + +impl Drop for ParkedWorker { + fn drop(&mut self) { + if self.0 { + PARKED_NORMAL.fetch_sub(1, Ordering::AcqRel); + } + } +} + +fn spawn_normal_helper(k: usize) { + let worker = queue().worker_between(Priority::BoundSoon, Priority::Foreground); + let n = NORMAL_HELPERS_STARTED.fetch_add(1, Ordering::Relaxed) + 1; + if n.is_power_of_two() { + crate::xlog_info!("D3D12.X: a pipeline worker waits for a huge compile slot – a helper serves its queue meanwhile ({n} helpers so far)"); + } + let spawned = std::thread::Builder::new().name(format!("xcl-pso-h{k}")).stack_size(64 << 20).spawn(move || { + NORMAL_WORKER.set(true); + let mut retired = false; + worker.run_while(|| { + retired = retire_normal_helper(); + !retired + }); + if !retired { + NORMAL_HELPERS.fetch_sub(1, Ordering::AcqRel); + } + }); + if spawned.is_err() { + NORMAL_HELPERS.fetch_sub(1, Ordering::AcqRel); + } +} + +/// Reserve this helper's retirement once more helpers exist than parked normal workers. +fn retire_normal_helper() -> bool { + let mut helpers = NORMAL_HELPERS.load(Ordering::Acquire); + while helpers > PARKED_NORMAL.load(Ordering::Acquire) { + match NORMAL_HELPERS.compare_exchange(helpers, helpers - 1, Ordering::AcqRel, Ordering::Acquire) { + Ok(_) => return true, + Err(now) => helpers = now, + } + } + false +} + fn start_preload_workers() { static STARTED: std::sync::Once = std::sync::Once::new(); STARTED.call_once(|| { @@ -1370,6 +1446,58 @@ mod used_tests { static WORKER_SATURATION_LOCK: Mutex<()> = Mutex::new(()); + /// Normal workers parked at the huge-driver-compile gate leave their priority band live, and + /// the bounded replacement workers retire after the gate opens. + #[test] + fn parked_normal_workers_leave_the_queue_to_bounded_helpers() { + let _sat = WORKER_SATURATION_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + use std::time::{Duration, Instant}; + + struct OpenGate(Arc<(Mutex, Condvar)>); + impl Drop for OpenGate { + fn drop(&mut self) { + *self.0 .0.lock().unwrap() = true; + self.0 .1.notify_all(); + } + } + + let outside = parked_for_big_compile(); + assert!(!outside.0, "a title/test thread must not start a replacement"); + drop(outside); + + start_normal_workers(); + let n = crate::gpu::recomp::dxc::compile_threads(); + let (started_tx, started_rx) = mpsc::channel(); + let gate = Arc::new((Mutex::new(false), Condvar::new())); + let open = OpenGate(gate.clone()); + for _ in 0..n { + let (tx, gate) = (started_tx.clone(), gate.clone()); + queue().submit(Priority::Foreground, Box::new(move || { + let _parked = parked_for_big_compile(); + tx.send(()).unwrap(); + let mut open = gate.0.lock().unwrap(); + while !*open { + open = gate.1.wait(open).unwrap(); + } + })).unwrap(); + } + for _ in 0..n { + started_rx.recv_timeout(Duration::from_secs(10)).expect("every blocker parked"); + } + assert!(NORMAL_HELPERS.load(Ordering::Acquire) <= n, "replacement count is bounded by the normal pool"); + let (ran_tx, ran_rx) = mpsc::channel(); + queue().submit(Priority::Foreground, Box::new(move || ran_tx.send(()).unwrap())).unwrap(); + ran_rx.recv_timeout(Duration::from_secs(10)).expect("a replacement served the queue"); + drop(open); + + let deadline = Instant::now() + Duration::from_secs(10); + while (PARKED_NORMAL.load(Ordering::Acquire) != 0 || NORMAL_HELPERS.load(Ordering::Acquire) != 0) && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } + assert_eq!(PARKED_NORMAL.load(Ordering::Acquire), 0); + assert_eq!(NORMAL_HELPERS.load(Ordering::Acquire), 0, "replacement workers retired"); + } + /// A queued normal worker job whose stand-in was released before a worker starts it is /// skipped as orphaned instead of entering the compiler. #[test] diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 1b6a583aac..e05bf72b39 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -4156,6 +4156,7 @@ fn gate_compute_driver_compile(has_cached_blob: bool) -> bool { fn big_compile_turn(slots: &'static Slots, commit_free: impl Fn() -> bool, wanted: impl Fn() -> bool) -> SlotGuard { const LOOK: std::time::Duration = std::time::Duration::from_millis(50); let mut n = slots.n.lock().unwrap(); + let mut parked = None; loop { let is_wanted = wanted(); let cap = if is_wanted { slots.wanted_most() } else { slots.most() }; @@ -4163,6 +4164,9 @@ fn big_compile_turn(slots: &'static Slots, commit_free: impl Fn() -> bool, wante *n += 1; return SlotGuard(slots); } + if parked.is_none() { + parked = Some(super::pso_async::parked_for_big_compile()); + } n = slots.cv.wait_timeout(n, LOOK).unwrap().0; } }