The fd and dma-buf helpers, fields and test hooks that only unix code reaches are compiled on unix alone, so the Windows build carries no dead code. The examples' anonymous pages use VirtualAlloc/VirtualFree/VirtualProtect on Windows and their thread and process CPU times GetThreadTimes/GetProcessTimes. Verified: cargo fmt; the Windows and Linux clippy rounds follow as jobs.
658 lines
26 KiB
Rust
658 lines
26 KiB
Rust
//! Not a consumer's example: a command buffer's fixed cost on the submit and completion path, the
|
|
//! shape of a consumer that records a command buffer, adds handlers, commits and never waits.
|
|
//!
|
|
//! ```sh
|
|
//! cargo run --release -p metal2vulkan-runtime --example submit_path [-- latency|cross|cpu|threads|empty|idle|all [--seen] [--bare] [--at-commit] [--checked]]
|
|
//! ```
|
|
//!
|
|
//! - **latency**: one thread commits a tiny blit command buffer (a fill of 16 bytes) with a
|
|
//! scheduled handler and a completed handler, or a completion-seen handler instead of the
|
|
//! completed one (`seen`), and does not wait for it: it blocks on a condition variable the
|
|
//! handler signals as it starts. Timed: `commit` to the handler's start, and the command
|
|
//! buffer's `GPUEndTime` to the handler's start (both on the runtime's host clock,
|
|
//! `host_time`), at idle gaps of 0, 100 us, 300 us and 2 ms between command buffers. With
|
|
//! `--bare`, also with no scheduled handler: a lone fill and its one callback.
|
|
//! - **cross**: the latency shape over two queues: command buffer A on the first signals an event
|
|
//! at its end, a shared one (whose wait the host holds until its value is reached) and a plain
|
|
//! one (whose wait goes to the GPU), and B on the second waits for it, each with a scheduled
|
|
//! handler and the handler `callback` names; A is committed, then B. Timed: A's commit to each
|
|
//! handler's start, at the same gaps; counted, the rounds where B's handler started before A's
|
|
//! or saw A's status short of `Completed` (Metal's completion follows the GPU's: none).
|
|
//! - **cpu**: the same command buffers committed as fast as the queue's slots allow, never waited
|
|
//! for: command buffers a second, and the CPU per command buffer of the runtime's threads (by
|
|
//! thread on Linux, from `/proc/self/task/*/schedstat`; elsewhere the process's CPU less the
|
|
//! committing thread's).
|
|
//! - **threads**: the cpu shape on 1 and on 8 threads at once, each with a queue of its own:
|
|
//! command buffers a second in all, and the CPU per command buffer.
|
|
//! - **empty**: a command buffer with nothing encoded, committed and waited for, back to back and
|
|
//! at a 300 us gap; and committed as fast as the slots allow, never waited for.
|
|
//! - **idle**: after one fill committed and waited for, wakes a second over [`IDLE_SECONDS`] with
|
|
//! nothing committed: on Linux the voluntary context switches by thread group, from
|
|
//! `/proc/self/task/*/status`; everywhere the scheduler's sleeps and the sleeps that ended at
|
|
//! its slice, from the device's work counts (macOS's per-process switch counts read back
|
|
//! inconsistently, so none is printed there).
|
|
//!
|
|
//! With `--at-commit`, latency, cpu and threads commit with `CommandBuffer::commit_and_submit`
|
|
//! (a runtime extension: the committing thread submits where no pass is under way) instead of
|
|
//! `commit`. With `--checked`, their fills carry a submission check that passes them
|
|
//! (`CommandBuffer::set_submission_check`, a runtime extension), as a consumer's detach check does.
|
|
|
|
use metal2vulkan_runtime::{
|
|
host_time, Buffer, CommandBuffer, CommandBufferStatus, CommandQueue, Device, EventRef,
|
|
ResourceOptions, Submission,
|
|
};
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::{Arc, Condvar, Mutex};
|
|
use std::time::{Duration, Instant};
|
|
|
|
/// Whether command buffers are committed with `commit_and_submit` (`--at-commit`).
|
|
static AT_COMMIT: AtomicBool = AtomicBool::new(false);
|
|
|
|
/// Whether the fills carry a submission check (`--checked`).
|
|
static CHECKED: AtomicBool = AtomicBool::new(false);
|
|
|
|
/// Commit `command_buffer` as `--at-commit` says.
|
|
fn commit(command_buffer: &CommandBuffer) {
|
|
match AT_COMMIT.load(Ordering::Relaxed) {
|
|
true => command_buffer.commit_and_submit().unwrap(),
|
|
false => command_buffer.commit().unwrap(),
|
|
}
|
|
}
|
|
|
|
/// Command buffers per measured row.
|
|
const ROUNDS: usize = 2000;
|
|
|
|
/// How long a full-rate row runs.
|
|
const RATE_SECONDS: f64 = 3.0;
|
|
|
|
/// How long the idle row watches the process.
|
|
const IDLE_SECONDS: f64 = 3.0;
|
|
|
|
/// The idle gaps of the latency rows.
|
|
const GAPS_US: [u64; 4] = [0, 100, 300, 2000];
|
|
|
|
/// Which handler the latency and cpu rows signal from.
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
enum Callback {
|
|
/// A completed handler, with a scheduled handler before it.
|
|
Completed,
|
|
/// A completion-seen handler, with a scheduled handler before it.
|
|
Seen,
|
|
}
|
|
|
|
impl Callback {
|
|
fn name(self) -> &'static str {
|
|
match self {
|
|
Callback::Completed => "completed",
|
|
Callback::Seen => "seen",
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A one-shot signal from a handler to the committing thread: the instant the handler started,
|
|
/// and the seconds from its command buffer's `GPUEndTime` to then on the host clock.
|
|
#[derive(Default)]
|
|
struct Signal {
|
|
at: Mutex<Option<(Instant, f64)>>,
|
|
changed: Condvar,
|
|
}
|
|
|
|
impl Signal {
|
|
fn set(&self, after_gpu_end: f64) {
|
|
let now = Instant::now();
|
|
*self.at.lock().unwrap() = Some((now, after_gpu_end));
|
|
self.changed.notify_one();
|
|
}
|
|
|
|
fn take(&self) -> (Instant, f64) {
|
|
let mut at = self.at.lock().unwrap();
|
|
loop {
|
|
if let Some(at) = at.take() {
|
|
return at;
|
|
}
|
|
at = self.changed.wait(at).unwrap();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A tiny blit command buffer: a fill of 16 bytes of `target`, with a scheduled handler where
|
|
/// `scheduled`, and the handler `callback` names, which runs `then` with the seconds from the
|
|
/// command buffer's `GPUEndTime` to its start.
|
|
fn tiny(
|
|
queue: &CommandQueue,
|
|
target: &Buffer,
|
|
(callback, scheduled): (Callback, bool),
|
|
then: impl FnOnce(f64) + Send + 'static,
|
|
) -> CommandBuffer {
|
|
let command_buffer = queue.command_buffer().unwrap();
|
|
let mut blit = command_buffer.blit_command_encoder().unwrap();
|
|
blit.fill_buffer(target, 0..16, 0xa5).unwrap();
|
|
blit.end_encoding().unwrap();
|
|
if CHECKED.load(Ordering::Relaxed) {
|
|
command_buffer
|
|
.set_submission_check(Arc::new(|_: &Submission<'_>| None))
|
|
.unwrap();
|
|
}
|
|
if scheduled {
|
|
command_buffer
|
|
.add_scheduled_handler(Box::new(|_: &CommandBuffer| {}))
|
|
.unwrap();
|
|
}
|
|
let handler = Box::new(move |buffer: &CommandBuffer| {
|
|
let now = host_time();
|
|
then(now - buffer.gpu_end_time().unwrap_or(now))
|
|
});
|
|
match callback {
|
|
Callback::Completed => command_buffer.add_completed_handler(handler).unwrap(),
|
|
Callback::Seen => command_buffer.add_completion_seen_handler(handler).unwrap(),
|
|
}
|
|
command_buffer
|
|
}
|
|
|
|
/// Sleep `gap` microseconds (none for 0).
|
|
fn idle(gap: u64) {
|
|
if gap > 0 {
|
|
std::thread::sleep(Duration::from_micros(gap));
|
|
}
|
|
}
|
|
|
|
/// The p50, p90 and p99 of `samples`, in microseconds.
|
|
fn percentiles(samples: &mut [f64]) -> (f64, f64, f64) {
|
|
samples.sort_by(f64::total_cmp);
|
|
let at = |p: f64| samples[((samples.len() as f64 * p) as usize).min(samples.len() - 1)];
|
|
(at(0.5), at(0.9), at(0.99))
|
|
}
|
|
|
|
fn latency(device: &Device, callback: Callback, scheduled: bool) {
|
|
let queue = device.new_command_queue().unwrap();
|
|
let target = device.new_buffer(16, ResourceOptions::shared()).unwrap();
|
|
let name = match scheduled {
|
|
true => callback.name().to_owned(),
|
|
false => format!("{} bare", callback.name()),
|
|
};
|
|
for gap in GAPS_US {
|
|
let signal = Arc::new(Signal::default());
|
|
let (mut samples, mut after_end) = (Vec::with_capacity(ROUNDS), Vec::new());
|
|
for round in 0..ROUNDS + 50 {
|
|
idle(gap);
|
|
let signalled = Arc::clone(&signal);
|
|
let command_buffer = tiny(&queue, &target, (callback, scheduled), move |after| {
|
|
signalled.set(after)
|
|
});
|
|
let committed = Instant::now();
|
|
commit(&command_buffer);
|
|
drop(command_buffer);
|
|
let (started, after) = signal.take();
|
|
if round >= 50 {
|
|
samples.push(started.duration_since(committed).as_secs_f64() * 1e6);
|
|
after_end.push(after * 1e6);
|
|
}
|
|
}
|
|
let (p50, p90, p99) = percentiles(&mut samples);
|
|
let (e50, e90, e99) = percentiles(&mut after_end);
|
|
println!(
|
|
"latency {name:<14} gap {gap:>4} us: commit to handler p50 {p50:7.1} p90 {p90:7.1} \
|
|
p99 {p99:7.1} us; GPU end to handler p50 {e50:7.1} p90 {e90:7.1} p99 {e99:7.1} us"
|
|
);
|
|
}
|
|
}
|
|
|
|
/// One round's handler starts of the cross row: A's, and B's with whether A's handler had started
|
|
/// and its status was `Completed` then.
|
|
type Starts = (Option<Instant>, Option<(Instant, bool)>);
|
|
|
|
/// The cross row's [`Starts`], and their signal to the committing thread.
|
|
#[derive(Default)]
|
|
struct Cross {
|
|
starts: Mutex<Starts>,
|
|
changed: Condvar,
|
|
}
|
|
|
|
impl Cross {
|
|
fn take(&self) -> (Instant, Instant, bool) {
|
|
let mut starts = self.starts.lock().unwrap();
|
|
loop {
|
|
if let (Some(a), Some((b, completed))) = *starts {
|
|
*starts = (None, None);
|
|
return (a, b, completed);
|
|
}
|
|
starts = self.changed.wait(starts).unwrap();
|
|
}
|
|
}
|
|
}
|
|
|
|
fn cross(device: &Device, callback: Callback, shared: bool) {
|
|
let (first, second) = (
|
|
device.new_command_queue().unwrap(),
|
|
device.new_command_queue().unwrap(),
|
|
);
|
|
let (shared_event, plain_event) = (
|
|
device.new_shared_event().unwrap(),
|
|
device.new_event().unwrap(),
|
|
);
|
|
let event = || match shared {
|
|
true => EventRef::from(&shared_event),
|
|
false => EventRef::from(&plain_event),
|
|
};
|
|
let target = device.new_buffer(32, ResourceOptions::shared()).unwrap();
|
|
let mut value = 0;
|
|
for gap in GAPS_US {
|
|
let starts = Arc::new(Cross::default());
|
|
let (mut to_a, mut to_b) = (Vec::with_capacity(ROUNDS), Vec::with_capacity(ROUNDS));
|
|
let mut overtaken = 0;
|
|
for round in 0..ROUNDS + 50 {
|
|
idle(gap);
|
|
value += 1;
|
|
let at_a = Arc::clone(&starts);
|
|
let a = tiny(&first, &target, (callback, true), move |_| {
|
|
at_a.starts.lock().unwrap().0 = Some(Instant::now());
|
|
at_a.changed.notify_one();
|
|
});
|
|
a.encode_signal_event(event(), value).unwrap();
|
|
let b = second.command_buffer().unwrap();
|
|
b.encode_wait_for_event(event(), value).unwrap();
|
|
let mut blit = b.blit_command_encoder().unwrap();
|
|
blit.fill_buffer(&target, 16..32, 0x5a).unwrap();
|
|
blit.end_encoding().unwrap();
|
|
b.add_scheduled_handler(Box::new(|_: &CommandBuffer| {}))
|
|
.unwrap();
|
|
let (at_b, signaller) = (Arc::clone(&starts), a.clone());
|
|
let handler = Box::new(move |_: &CommandBuffer| {
|
|
let completed = signaller.status() == CommandBufferStatus::Completed;
|
|
let mut starts = at_b.starts.lock().unwrap();
|
|
let before_a = starts.0.is_none();
|
|
starts.1 = Some((Instant::now(), completed && !before_a));
|
|
at_b.changed.notify_one();
|
|
});
|
|
match callback {
|
|
Callback::Completed => b.add_completed_handler(handler).unwrap(),
|
|
Callback::Seen => b.add_completion_seen_handler(handler).unwrap(),
|
|
}
|
|
let committed = Instant::now();
|
|
commit(&a);
|
|
commit(&b);
|
|
drop((a, b));
|
|
let (a_started, b_started, in_order) = starts.take();
|
|
if round >= 50 {
|
|
to_a.push(a_started.duration_since(committed).as_secs_f64() * 1e6);
|
|
to_b.push(b_started.duration_since(committed).as_secs_f64() * 1e6);
|
|
overtaken += usize::from(!in_order);
|
|
}
|
|
}
|
|
let (a50, a90, a99) = percentiles(&mut to_a);
|
|
let (b50, b90, b99) = percentiles(&mut to_b);
|
|
let kind = match shared {
|
|
true => "shared",
|
|
false => "plain",
|
|
};
|
|
println!(
|
|
"cross {:<10} {kind:<6} gap {gap:>4} us: A's commit to A's handler p50 {a50:7.1} p90 \
|
|
{a90:7.1} p99 {a99:7.1} us, to B's p50 {b50:7.1} p90 {b90:7.1} p99 {b99:7.1} us; B \
|
|
before A {overtaken}/{ROUNDS}",
|
|
callback.name()
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The group a thread named `comm` is counted in.
|
|
fn group_of(comm: &str) -> &'static str {
|
|
match comm {
|
|
c if c.starts_with("metal2vulkan-sc") => "scheduler",
|
|
c if c.starts_with("metal2vulkan-qu") => "queue-waiters",
|
|
c if c.starts_with("metal2vulkan-ha") => "handlers",
|
|
c if c.starts_with("metal2vulkan") => "other-m2v",
|
|
c if c.starts_with("submit_path") => "committer+main",
|
|
_ => "driver/other",
|
|
}
|
|
}
|
|
|
|
/// Voluntary context switches by thread group, each of the process's threads by its name's
|
|
/// prefix: Linux only, none elsewhere.
|
|
fn switches_by_group() -> Vec<(String, u64)> {
|
|
let mut groups: Vec<(String, u64)> = Vec::new();
|
|
if let Ok(tasks) = std::fs::read_dir("/proc/self/task") {
|
|
for task in tasks.flatten() {
|
|
let path = task.path();
|
|
let comm = std::fs::read_to_string(path.join("comm")).unwrap_or_default();
|
|
let status = std::fs::read_to_string(path.join("status")).unwrap_or_default();
|
|
let switches: u64 = status
|
|
.lines()
|
|
.find_map(|line| line.strip_prefix("voluntary_ctxt_switches:"))
|
|
.and_then(|n| n.trim().parse().ok())
|
|
.unwrap_or(0);
|
|
let group = group_of(comm.trim()).to_owned();
|
|
match groups.iter_mut().find(|(n, _)| *n == group) {
|
|
Some((_, total)) => *total += switches,
|
|
None => groups.push((group, switches)),
|
|
}
|
|
}
|
|
}
|
|
groups
|
|
}
|
|
|
|
fn idle_wakes(device: &Device) {
|
|
let queue = device.new_command_queue().unwrap();
|
|
let target = device.new_buffer(16, ResourceOptions::shared()).unwrap();
|
|
let signal = Arc::new(Signal::default());
|
|
let signalled = Arc::clone(&signal);
|
|
let command_buffer = tiny(&queue, &target, (Callback::Completed, true), move |after| {
|
|
signalled.set(after)
|
|
});
|
|
command_buffer.commit().unwrap();
|
|
signal.take();
|
|
command_buffer.wait_until_completed().unwrap();
|
|
std::thread::sleep(Duration::from_millis(300));
|
|
let (before, counts) = (switches_by_group(), device.work_counts());
|
|
std::thread::sleep(Duration::from_secs_f64(IDLE_SECONDS));
|
|
let (after, later) = (switches_by_group(), device.work_counts());
|
|
if !after.is_empty() {
|
|
let mut line = "idle wakes a second:".to_owned();
|
|
for (name, count) in &after {
|
|
let earlier = before
|
|
.iter()
|
|
.find(|(n, _)| n == name)
|
|
.map_or(0, |(_, n)| *n);
|
|
let per = count.saturating_sub(earlier) as f64 / IDLE_SECONDS;
|
|
line.push_str(&format!(" {name} {per:.1}"));
|
|
}
|
|
println!("{line} (voluntary context switches, the main thread's one sleep among them)");
|
|
}
|
|
println!(
|
|
"idle scheduler a second: {:.1} sleeps, {:.1} ended at the slice",
|
|
(later.scheduler_sleeps - counts.scheduler_sleeps) as f64 / IDLE_SECONDS,
|
|
(later.scheduler_sleep_slices - counts.scheduler_sleep_slices) as f64 / IDLE_SECONDS,
|
|
);
|
|
}
|
|
|
|
/// Nanoseconds of CPU by thread group: on Linux each of the process's threads by its name's
|
|
/// prefix; elsewhere `other` alone (the process less the calling thread).
|
|
fn cpu_by_group() -> Vec<(String, u64)> {
|
|
let mut groups: Vec<(String, u64)> = Vec::new();
|
|
let mut add = |name: String, nanos: u64| match groups.iter_mut().find(|(n, _)| *n == name) {
|
|
Some((_, total)) => *total += nanos,
|
|
None => groups.push((name, nanos)),
|
|
};
|
|
if let Ok(tasks) = std::fs::read_dir("/proc/self/task") {
|
|
for task in tasks.flatten() {
|
|
let path = task.path();
|
|
let comm = std::fs::read_to_string(path.join("comm")).unwrap_or_default();
|
|
let stat = std::fs::read_to_string(path.join("schedstat")).unwrap_or_default();
|
|
let nanos: u64 = stat
|
|
.split_whitespace()
|
|
.next()
|
|
.and_then(|n| n.parse().ok())
|
|
.unwrap_or(0);
|
|
add(group_of(comm.trim()).to_owned(), nanos);
|
|
}
|
|
return groups;
|
|
}
|
|
// The process's CPU less this thread's.
|
|
#[cfg(unix)]
|
|
// SAFETY: plain calls with out-parameters of the right types.
|
|
let (process, own) = unsafe {
|
|
let mut usage: libc::rusage = std::mem::zeroed();
|
|
libc::getrusage(libc::RUSAGE_SELF, &mut usage);
|
|
let process = (usage.ru_utime.tv_sec as u64 * 1_000_000
|
|
+ usage.ru_utime.tv_usec as u64
|
|
+ usage.ru_stime.tv_sec as u64 * 1_000_000
|
|
+ usage.ru_stime.tv_usec as u64)
|
|
* 1000;
|
|
let mut spec: libc::timespec = std::mem::zeroed();
|
|
libc::clock_gettime(libc::CLOCK_THREAD_CPUTIME_ID, &mut spec);
|
|
(
|
|
process,
|
|
spec.tv_sec as u64 * 1_000_000_000 + spec.tv_nsec as u64,
|
|
)
|
|
};
|
|
#[cfg(windows)]
|
|
let (process, own) = (process_cpu_nanos(), thread_cpu_nanos());
|
|
add("committer".to_owned(), own);
|
|
add("all-but-committer".to_owned(), process.saturating_sub(own));
|
|
groups
|
|
}
|
|
|
|
/// This thread's CPU so far, in nanoseconds.
|
|
#[cfg(unix)]
|
|
fn thread_cpu_nanos() -> u64 {
|
|
// SAFETY: a plain call with an out-parameter of the right type.
|
|
let spec = unsafe {
|
|
let mut spec: libc::timespec = std::mem::zeroed();
|
|
libc::clock_gettime(libc::CLOCK_THREAD_CPUTIME_ID, &mut spec);
|
|
spec
|
|
};
|
|
spec.tv_sec as u64 * 1_000_000_000 + spec.tv_nsec as u64
|
|
}
|
|
|
|
/// This thread's CPU so far, in nanoseconds: its kernel and user times (`GetThreadTimes`), in
|
|
/// 100 ns units.
|
|
#[cfg(windows)]
|
|
fn thread_cpu_nanos() -> u64 {
|
|
use windows_sys::Win32::Foundation::FILETIME;
|
|
use windows_sys::Win32::System::Threading::{GetCurrentThread, GetThreadTimes};
|
|
let mut times = [FILETIME::default(); 4];
|
|
let [created, exited, kernel, user] = &mut times;
|
|
// SAFETY: the calling thread's pseudo-handle, and four `FILETIME`s for the call to fill.
|
|
let read = unsafe { GetThreadTimes(GetCurrentThread(), created, exited, kernel, user) };
|
|
assert_ne!(read, 0, "GetThreadTimes");
|
|
let ticks = |t: &FILETIME| (u64::from(t.dwHighDateTime) << 32) | u64::from(t.dwLowDateTime);
|
|
(ticks(kernel) + ticks(user)) * 100
|
|
}
|
|
|
|
/// The process's CPU so far, every thread's, in nanoseconds: its kernel and user times
|
|
/// (`GetProcessTimes`), in 100 ns units.
|
|
#[cfg(windows)]
|
|
fn process_cpu_nanos() -> u64 {
|
|
use windows_sys::Win32::Foundation::FILETIME;
|
|
use windows_sys::Win32::System::Threading::{GetCurrentProcess, GetProcessTimes};
|
|
let mut times = [FILETIME::default(); 4];
|
|
let [created, exited, kernel, user] = &mut times;
|
|
// SAFETY: this process's pseudo-handle, and four `FILETIME`s for the call to fill.
|
|
let read = unsafe { GetProcessTimes(GetCurrentProcess(), created, exited, kernel, user) };
|
|
assert_ne!(read, 0, "GetProcessTimes");
|
|
let ticks = |t: &FILETIME| (u64::from(t.dwHighDateTime) << 32) | u64::from(t.dwLowDateTime);
|
|
(ticks(kernel) + ticks(user)) * 100
|
|
}
|
|
|
|
/// The CPU each group spent between `before` and `after`, per command buffer, in microseconds.
|
|
fn print_cpu(label: &str, before: &[(String, u64)], after: &[(String, u64)], count: usize) {
|
|
let mut line = format!("{label}:");
|
|
for (name, nanos) in after {
|
|
let earlier = before
|
|
.iter()
|
|
.find(|(n, _)| n == name)
|
|
.map_or(0, |(_, n)| *n);
|
|
let per = (nanos.saturating_sub(earlier)) as f64 / 1e3 / count as f64;
|
|
line.push_str(&format!(" {name} {per:.2}"));
|
|
}
|
|
println!("{line} (us of CPU per command buffer)");
|
|
}
|
|
|
|
fn cpu(device: &Device, callback: Callback) {
|
|
let queue = device.new_command_queue().unwrap();
|
|
let target = device.new_buffer(16, ResourceOptions::shared()).unwrap();
|
|
// Warm.
|
|
for _ in 0..200 {
|
|
commit(&tiny(&queue, &target, (callback, true), |_| {}));
|
|
}
|
|
std::thread::sleep(Duration::from_millis(200));
|
|
let before = cpu_by_group();
|
|
let start = Instant::now();
|
|
let mut count = 0;
|
|
while start.elapsed().as_secs_f64() < RATE_SECONDS {
|
|
commit(&tiny(&queue, &target, (callback, true), |_| {}));
|
|
count += 1;
|
|
}
|
|
let elapsed = start.elapsed().as_secs_f64();
|
|
let after = cpu_by_group();
|
|
println!(
|
|
"cpu {:<9} {:.0} command buffers a second ({count} in {elapsed:.2} s)",
|
|
callback.name(),
|
|
count as f64 / elapsed
|
|
);
|
|
print_cpu(
|
|
&format!("cpu {:<9}", callback.name()),
|
|
&before,
|
|
&after,
|
|
count,
|
|
);
|
|
let last = queue.command_buffer().unwrap();
|
|
last.commit().unwrap();
|
|
last.wait_until_completed().unwrap();
|
|
}
|
|
|
|
fn empty(device: &Device) {
|
|
let queue = device.new_command_queue().unwrap();
|
|
for gap in [0, 300] {
|
|
let mut samples = Vec::with_capacity(ROUNDS);
|
|
for round in 0..ROUNDS + 50 {
|
|
idle(gap);
|
|
let command_buffer = queue.command_buffer().unwrap();
|
|
let committed = Instant::now();
|
|
command_buffer.commit().unwrap();
|
|
command_buffer.wait_until_completed().unwrap();
|
|
if round >= 50 {
|
|
samples.push(committed.elapsed().as_secs_f64() * 1e6);
|
|
}
|
|
}
|
|
let (p50, p90, p99) = percentiles(&mut samples);
|
|
println!(
|
|
"empty commit and wait, gap {gap:>4} us: p50 {p50:7.1} p90 {p90:7.1} p99 {p99:7.1} us"
|
|
);
|
|
}
|
|
let before_counts = device.work_counts();
|
|
let before = cpu_by_group();
|
|
let start = Instant::now();
|
|
let mut count = 0;
|
|
while start.elapsed().as_secs_f64() < RATE_SECONDS {
|
|
queue.command_buffer().unwrap().commit().unwrap();
|
|
count += 1;
|
|
}
|
|
let elapsed = start.elapsed().as_secs_f64();
|
|
let after = cpu_by_group();
|
|
let last = queue.command_buffer().unwrap();
|
|
last.commit().unwrap();
|
|
last.wait_until_completed().unwrap();
|
|
let after_counts = device.work_counts();
|
|
println!(
|
|
"empty never waited: {:.0} command buffers a second ({count} in {elapsed:.2} s), {} queue submissions",
|
|
count as f64 / elapsed,
|
|
after_counts.queue_submissions - before_counts.queue_submissions
|
|
);
|
|
print_cpu("empty never waited", &before, &after, count);
|
|
}
|
|
|
|
/// The cpu shape on `threads` threads at once, each with a queue of its own.
|
|
fn threads(device: &Device, threads: usize) {
|
|
let queues: Vec<(CommandQueue, Buffer)> = (0..threads)
|
|
.map(|_| {
|
|
let queue = device.new_command_queue().unwrap();
|
|
let target = device.new_buffer(16, ResourceOptions::shared()).unwrap();
|
|
(queue, target)
|
|
})
|
|
.collect();
|
|
for (queue, target) in &queues {
|
|
for _ in 0..200 {
|
|
commit(&tiny(queue, target, (Callback::Completed, true), |_| {}));
|
|
}
|
|
}
|
|
std::thread::sleep(Duration::from_millis(200));
|
|
let before = cpu_by_group();
|
|
let start = Instant::now();
|
|
// Each committing thread's own CPU, read before it ends (its schedstat goes with it).
|
|
let (count, committers): (usize, u64) = std::thread::scope(|scope| {
|
|
let running: Vec<_> = (queues.iter())
|
|
.map(|(queue, target)| {
|
|
scope.spawn(move || {
|
|
let (mut count, began) = (0, thread_cpu_nanos());
|
|
while start.elapsed().as_secs_f64() < RATE_SECONDS {
|
|
commit(&tiny(queue, target, (Callback::Completed, true), |_| {}));
|
|
count += 1;
|
|
}
|
|
(count, thread_cpu_nanos() - began)
|
|
})
|
|
})
|
|
.collect();
|
|
(running.into_iter())
|
|
.map(|thread| thread.join().unwrap())
|
|
.fold((0, 0), |(count, nanos), (more, spent)| {
|
|
(count + more, nanos + spent)
|
|
})
|
|
});
|
|
let elapsed = start.elapsed().as_secs_f64();
|
|
let after = cpu_by_group();
|
|
println!(
|
|
"threads {threads}: {:.0} command buffers a second ({count} in {elapsed:.2} s), \
|
|
committing threads {:.2} us of CPU per command buffer",
|
|
count as f64 / elapsed,
|
|
committers as f64 / 1e3 / count as f64
|
|
);
|
|
print_cpu(&format!("threads {threads}"), &before, &after, count);
|
|
for (queue, _) in &queues {
|
|
let last = queue.command_buffer().unwrap();
|
|
last.commit().unwrap();
|
|
last.wait_until_completed().unwrap();
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let device = Device::system_default().unwrap();
|
|
println!("device: {}", device.name());
|
|
let what = std::env::args().nth(1).unwrap_or_else(|| "all".to_owned());
|
|
let seen = std::env::args().any(|arg| arg == "--seen");
|
|
let bare = std::env::args().any(|arg| arg == "--bare");
|
|
let at_commit = std::env::args().any(|arg| arg == "--at-commit");
|
|
AT_COMMIT.store(at_commit, Ordering::Relaxed);
|
|
let checked = std::env::args().any(|arg| arg == "--checked");
|
|
CHECKED.store(checked, Ordering::Relaxed);
|
|
println!(
|
|
"committed with {}{}",
|
|
if at_commit {
|
|
"commit_and_submit"
|
|
} else {
|
|
"commit"
|
|
},
|
|
if checked {
|
|
", each with a submission check"
|
|
} else {
|
|
""
|
|
}
|
|
);
|
|
let callbacks: &[Callback] = match seen {
|
|
true => &[Callback::Completed, Callback::Seen],
|
|
false => &[Callback::Completed],
|
|
};
|
|
if what == "latency" || what == "all" {
|
|
for &callback in callbacks {
|
|
latency(&device, callback, true);
|
|
}
|
|
if bare {
|
|
for &callback in callbacks {
|
|
latency(&device, callback, false);
|
|
}
|
|
}
|
|
}
|
|
if what == "cross" || what == "all" {
|
|
for &callback in callbacks {
|
|
for shared in [true, false] {
|
|
cross(&device, callback, shared);
|
|
}
|
|
}
|
|
}
|
|
if what == "cpu" || what == "all" {
|
|
for &callback in callbacks {
|
|
cpu(&device, callback);
|
|
}
|
|
}
|
|
if what == "threads" || what == "all" {
|
|
for count in [1, 8] {
|
|
threads(&device, count);
|
|
}
|
|
}
|
|
if what == "empty" || what == "all" {
|
|
empty(&device);
|
|
}
|
|
if what == "idle" || what == "all" {
|
|
idle_wakes(&device);
|
|
}
|
|
}
|