diff --git a/crates/xcl/benches/runtime.rs b/crates/xcl/benches/runtime.rs index 6d1480d0c3..a1a9c118a1 100644 --- a/crates/xcl/benches/runtime.rs +++ b/crates/xcl/benches/runtime.rs @@ -180,6 +180,94 @@ fn pipeline_resolution_benches(c: &mut Criterion) { group.finish(); } +#[cfg(feature = "pipeline-resolution-bench")] +const EMISSION_BATCH: usize = 1_024; + +#[cfg(feature = "pipeline-resolution-bench")] +struct EmissionWorkers { + start: Arc, + done: Arc, + stop: Arc, + threads: Vec>, +} + +#[cfg(feature = "pipeline-resolution-bench")] +impl EmissionWorkers { + fn new( + fixture: Arc, + workers: usize, + ) -> Self { + let start = Arc::new(Barrier::new(workers + 1)); + let done = Arc::new(Barrier::new(workers + 1)); + let stop = Arc::new(AtomicBool::new(false)); + let threads = (0..workers) + .map(|worker| { + let fixture = fixture.clone(); + let start = start.clone(); + let done = done.clone(); + let stop = stop.clone(); + std::thread::Builder::new() + .name(format!("emission-bench-{worker}")) + .spawn(move || loop { + start.wait(); + if stop.load(Ordering::Acquire) { + break; + } + for _ in 0..EMISSION_BATCH { + fixture.record_worker(worker); + } + done.wait(); + }) + .expect("emission benchmark worker") + }) + .collect(); + Self { start, done, stop, threads } + } + + fn run(&self) { + self.start.wait(); + self.done.wait(); + } +} + +#[cfg(feature = "pipeline-resolution-bench")] +impl Drop for EmissionWorkers { + fn drop(&mut self) { + self.stop.store(true, Ordering::Release); + self.start.wait(); + for thread in self.threads.drain(..) { + thread.join().expect("emission benchmark worker panicked"); + } + } +} + +#[cfg(feature = "pipeline-resolution-bench")] +fn emission_bookkeeping_benches(c: &mut Criterion) { + let workers = std::thread::available_parallelism().map_or(4, |count| count.get()) + .saturating_sub(1).clamp(2, 8); + let fixture = Arc::new(xcl::hle::d3d12x::list_x::EmissionBookkeepingBench::new(workers)); + + let mut group = c.benchmark_group("runtime/emission_bookkeeping/record_existing"); + group.throughput(Throughput::Elements(1)); + for (slot, depth) in [1usize, 16, 64, 256].into_iter().enumerate() { + group.bench_with_input(BenchmarkId::from_parameter(depth), &slot, |b, &slot| { + b.iter(|| black_box(&fixture).record_at_depth(black_box(slot))); + }); + } + group.finish(); + + let pool = EmissionWorkers::new(fixture.clone(), workers); + let mut group = c.benchmark_group("runtime/emission_bookkeeping/contention"); + group.throughput(Throughput::Elements((workers * EMISSION_BATCH) as u64)); + group.bench_function(&format!("distinct_lists_serial_{workers}"), |b| { + b.iter(|| black_box(&fixture).record_workers_serial(workers, EMISSION_BATCH)); + }); + group.bench_function(&format!("distinct_lists_parallel_{workers}"), |b| { + b.iter(|| pool.run()); + }); + group.finish(); +} + fn utility_benches(c: &mut Criterion) { let mut group = c.benchmark_group("runtime/util"); for (name, pattern, value) in [ @@ -335,5 +423,7 @@ fn main() { allocator_benches, #[cfg(feature = "pipeline-resolution-bench")] pipeline_resolution_benches, + #[cfg(feature = "pipeline-resolution-bench")] + emission_bookkeeping_benches, ]); } diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index f8c0c5605d..821fac58db 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -929,6 +929,79 @@ pub(super) fn record_native_generation(list: usize, generation: Option, + _keys: Vec>, +} + +#[cfg(feature = "pipeline-resolution-bench")] +impl EmissionBookkeepingBench { + pub fn new(workers: usize) -> Self { + assert!(workers != 0); + let generations = super::pso_async::NativeGenerationBenchSet::new(256); + let keys = (0..4 + workers).map(|index| Box::new(index as u8)).collect::>(); + let lists = keys.iter().map(|key| (&**key as *const u8) as usize).collect::>(); + let depth_lists = [lists[0], lists[1], lists[2], lists[3]]; + let worker_lists = lists[4..].to_vec(); + assert!(LISTS + .lock() + .unwrap() + .as_ref() + .is_none_or(|states| lists.iter().all(|list| !states.contains_key(list)))); + for (slot, depth) in [1usize, 16, 64, 256].into_iter().enumerate() { + for index in 0..depth { + record_native_generation(depth_lists[slot], Some(generations.get(index))); + } + } + for (worker, &list) in worker_lists.iter().enumerate() { + record_native_generation(list, Some(generations.get(worker))); + } + Self { generations, depth_lists, worker_lists, _keys: keys } + } + + /// Depth one hits the last-pin shortcut. Larger depths hit the identity index with an + /// existing non-last generation. + #[inline(never)] + pub fn record_at_depth(&self, slot: usize) { + record_native_generation(self.depth_lists[slot], Some(self.generations.get(0))); + } + + #[inline(never)] + pub fn record_worker(&self, worker: usize) { + record_native_generation(self.worker_lists[worker], Some(self.generations.get(worker))); + } + + #[inline(never)] + pub fn record_workers_serial(&self, workers: usize, batch: usize) { + for worker in 0..workers { + for _ in 0..batch { + self.record_worker(worker); + } + } + } +} + +#[cfg(feature = "pipeline-resolution-bench")] +impl Drop for EmissionBookkeepingBench { + fn drop(&mut self) { + let removed = { + let mut lists = LISTS.lock().unwrap(); + let Some(lists) = lists.as_mut() else { return }; + self.depth_lists + .iter() + .chain(&self.worker_lists) + .filter_map(|list| lists.remove(list)) + .collect::>() + }; + drop(removed); + } +} + #[cfg(test)] pub(super) fn native_generation_count_for_test(list: usize) -> usize { LISTS.lock().unwrap().as_ref().and_then(|lists| lists.get(&list)).map_or(0, |state| state.native_generations.len()) diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index d447d07b19..f2c5bc3e24 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -7664,6 +7664,12 @@ struct PipelineResolutionBenchPso { refs: AtomicUsize, } +// The benchmark vtable is immutable and its only mutable state is atomic reference counting. +#[cfg(feature = "pipeline-resolution-bench")] +unsafe impl Send for PipelineResolutionBenchPso {} +#[cfg(feature = "pipeline-resolution-bench")] +unsafe impl Sync for PipelineResolutionBenchPso {} + #[cfg(feature = "pipeline-resolution-bench")] unsafe extern "system" fn pipeline_resolution_bench_add_ref(this: *mut std::ffi::c_void) -> u32 { (*(this as *mut PipelineResolutionBenchPso)).refs.fetch_add(1, Ordering::Relaxed) as u32 + 1 @@ -7674,6 +7680,49 @@ unsafe extern "system" fn pipeline_resolution_bench_release(this: *mut std::ffi: (*(this as *mut PipelineResolutionBenchPso)).refs.fetch_sub(1, Ordering::Relaxed) as u32 - 1 } +/// Distinct exact-generation identities backed by one fake COM object. Field order keeps every +/// generation owner alive only while the fake object and its vtable still exist. +#[cfg(feature = "pipeline-resolution-bench")] +pub(super) struct NativeGenerationBenchSet { + generations: Vec, + _raw_owner: ID3D12PipelineState, + _native: Box, + _vtable: Box<[usize; 3]>, +} + +#[cfg(feature = "pipeline-resolution-bench")] +impl NativeGenerationBenchSet { + pub(super) fn new(count: usize) -> Self { + assert!(count != 0); + let vtable = Box::new([ + 0, + pipeline_resolution_bench_add_ref as usize, + pipeline_resolution_bench_release as usize, + ]); + let mut native = Box::new(PipelineResolutionBenchPso { + vtable: vtable.as_ptr(), + refs: AtomicUsize::new(1), + }); + let raw = (&mut *native as *mut PipelineResolutionBenchPso).cast::(); + let raw_owner = unsafe { ID3D12PipelineState::from_raw(raw) }; + let generations = (0..count) + .map(|index| NativeGeneration { + generation: Arc::new(Generation { + id: index as u64 + 1, + raw: raw as usize, + _owner: raw_owner.clone(), + }), + recipe: Weak::new(), + }) + .collect(); + Self { generations, _raw_owner: raw_owner, _native: native, _vtable: vtable } + } + + pub(super) fn get(&self, index: usize) -> NativeGeneration { + self.generations[index].clone() + } +} + #[cfg(feature = "pipeline-resolution-bench")] impl PipelineResolutionBench { pub fn new() -> Self {