Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions crates/xcl/benches/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,63 @@ impl Drop for EmissionWorkers {
}
}

#[cfg(feature = "pipeline-resolution-bench")]
struct BindWorkers {
start: Arc<Barrier>,
done: Arc<Barrier>,
stop: Arc<AtomicBool>,
threads: Vec<JoinHandle<()>>,
}

#[cfg(feature = "pipeline-resolution-bench")]
impl BindWorkers {
fn new(fixture: Arc<xcl::hle::d3d12x::bind::BindStateBench>, 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())
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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,
]);
}
66 changes: 66 additions & 0 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,72 @@ fn with<R>(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<super::pso_async::PipelineResolutionBench>,
lists: Vec<usize>,
_keys: Vec<Box<u8>>,
}

#[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::<Vec<_>>();
let keys = (0..workers).map(|index| Box::new(index as u8)).collect::<Vec<_>>();
let lists = keys.iter().map(|key| (&**key as *const u8) as usize).collect::<Vec<_>>();
{
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::<Vec<_>>()
};
drop(removed);
}
}

fn native_backing(list: *mut c_void, allocator: usize) -> usize {
if allocator == 0 {
0
Expand Down
4 changes: 4 additions & 0 deletions crates/xcl/src/hle/d3d12x/pso_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down