From 35df1ae61c980797dc5e6c2cb8945af72ab95dc1 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 10:19:58 -0800 Subject: [PATCH] Tests: benchmark retained bind-state contention Compare equal batches of retained pipeline-state snapshots across distinct command lists serially and in parallel. Each worker owns a separate exact-resolution carrier so the measurement isolates the global bind-state mutex rather than a shared reference-count cache line. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/benches/runtime.rs | 78 ++++++++++++++++++++++++++ crates/xcl/src/hle/d3d12x/bind.rs | 66 ++++++++++++++++++++++ crates/xcl/src/hle/d3d12x/pso_async.rs | 4 ++ 3 files changed, 148 insertions(+) diff --git a/crates/xcl/benches/runtime.rs b/crates/xcl/benches/runtime.rs index a1a9c118a1..8064745fca 100644 --- a/crates/xcl/benches/runtime.rs +++ b/crates/xcl/benches/runtime.rs @@ -241,6 +241,63 @@ impl Drop for EmissionWorkers { } } +#[cfg(feature = "pipeline-resolution-bench")] +struct BindWorkers { + start: Arc, + done: Arc, + stop: Arc, + threads: Vec>, +} + +#[cfg(feature = "pipeline-resolution-bench")] +impl BindWorkers { + 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!("bind-bench-{worker}")) + .spawn(move || loop { + start.wait(); + if stop.load(Ordering::Acquire) { + break; + } + let mut checksum = 0usize; + for _ in 0..EMISSION_BATCH { + checksum = checksum.wrapping_add(fixture.snapshot_worker(worker)); + } + black_box(checksum); + done.wait(); + }) + .expect("bind-state 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 BindWorkers { + fn drop(&mut self) { + self.stop.store(true, Ordering::Release); + self.start.wait(); + for thread in self.threads.drain(..) { + thread.join().expect("bind-state 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()) @@ -268,6 +325,25 @@ fn emission_bookkeeping_benches(c: &mut Criterion) { group.finish(); } +#[cfg(feature = "pipeline-resolution-bench")] +fn bind_state_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::bind::BindStateBench::new(workers)); + let pool = BindWorkers::new(fixture.clone(), workers); + // This is one retained-pipeline snapshot, not a whole draw or dispatch; those take STATES + // several more times while preparing their root arguments and ABI memory. + let mut group = c.benchmark_group("runtime/emission_bookkeeping/bind_pipeline_snapshot"); + group.throughput(Throughput::Elements((workers * EMISSION_BATCH) as u64)); + group.bench_function(&format!("distinct_lists_serial_{workers}"), |b| { + b.iter(|| black_box(&fixture).snapshot_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 [ @@ -425,5 +501,7 @@ fn main() { pipeline_resolution_benches, #[cfg(feature = "pipeline-resolution-bench")] emission_bookkeeping_benches, + #[cfg(feature = "pipeline-resolution-bench")] + bind_state_benches, ]); } diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index fe0b4fedb0..997ab009b9 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -74,6 +74,72 @@ fn with(list: *mut c_void, f: impl FnOnce(&mut State) -> R) -> R { f(STATES.lock().unwrap().get_or_insert_with(HashMap::new).entry(list as usize).or_default()) } +/// Opaque fixture for measuring the actual retained-pipeline state snapshot without a device. +#[cfg(feature = "pipeline-resolution-bench")] +#[doc(hidden)] +pub struct BindStateBench { + _resolutions: Vec, + lists: Vec, + _keys: Vec>, +} + +#[cfg(feature = "pipeline-resolution-bench")] +impl BindStateBench { + pub fn new(workers: usize) -> Self { + assert!(workers != 0); + let resolutions = (0..workers) + .map(|_| super::pso_async::PipelineResolutionBench::new()) + .collect::>(); + let keys = (0..workers).map(|index| Box::new(index as u8)).collect::>(); + let lists = keys.iter().map(|key| (&**key as *const u8) as usize).collect::>(); + { + let mut states = STATES.lock().unwrap(); + let states = states.get_or_insert_with(HashMap::new); + assert!(lists.iter().all(|list| !states.contains_key(list))); + for (worker, &list) in lists.iter().enumerate() { + states.insert(list, State { + pso: worker + 1, + resolution: Some(resolutions[worker].retained_resolution()), + ..Default::default() + }); + } + } + Self { _resolutions: resolutions, lists, _keys: keys } + } + + #[inline(never)] + pub fn snapshot_worker(&self, worker: usize) -> usize { + let (pso, resolution) = with(self.lists[worker] as *mut c_void, |state| { + (state.pso, state.resolution.clone()) + }); + std::hint::black_box(resolution); + pso + } + + #[inline(never)] + pub fn snapshot_workers_serial(&self, workers: usize, batch: usize) -> usize { + let mut checksum = 0usize; + for worker in 0..workers { + for _ in 0..batch { + checksum = checksum.wrapping_add(self.snapshot_worker(worker).rotate_left(worker as u32)); + } + } + checksum + } +} + +#[cfg(feature = "pipeline-resolution-bench")] +impl Drop for BindStateBench { + fn drop(&mut self) { + let removed = { + let mut states = STATES.lock().unwrap(); + let Some(states) = states.as_mut() else { return }; + self.lists.iter().filter_map(|list| states.remove(list)).collect::>() + }; + drop(removed); + } +} + fn native_backing(list: *mut c_void, allocator: usize) -> usize { if allocator == 0 { 0 diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index f2c5bc3e24..e0afc6b6e8 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -7794,6 +7794,10 @@ impl PipelineResolutionBench { Self::consume(self.retained_ready.resolve_for_emission()) } + pub(super) fn retained_resolution(&self) -> PipelineResolution { + self.retained_ready.clone() + } + #[inline(never)] pub fn registry_ready(&self) -> usize { let resolution = begin_pipeline_resolution(self.registry_original).expect("benchmark registry fixture disappeared");