The lavapipe test of both QueueTurnPolicy values (job m3-max-38162, msi, load 7.32): twelve command buffers of one queue committed at once, each about 37 ms there, and a small one of another queue committed after the first was scheduled. Fair: the small one's GPU run started 37.5 ms after its commit, between the burst's first and second (14 looks held, 5 let go under the idle budget). CommitOrder: 454.8 ms after, behind all twelve (0 held). Verified: m3-max-38177 (msi) at c46daf583b: clippy -p metal2vulkan-runtime --all-targets --all-features -D warnings 0 (compiles the example); nextest -p metal2vulkan --all-features fast_paths 36/36. The numbers: m3-max-38162 (msi lavapipe, load 7.32): under Fair the small command buffer's GPU run started 37.5 ms after its commit, between the burst's 1st and 2nd (14 looks held, 5 let go idle); under CommitOrder 454.8 ms, behind all 12 (0 held).
1336 lines
58 KiB
Rust
1336 lines
58 KiB
Rust
//! Not a consumer's example: command queues sharing `VkQueue`s (`QueueSharingPolicy`), each
|
|
//! workload run under the policy the arguments name, with the device's sharing counts after it.
|
|
//!
|
|
//! ```sh
|
|
//! cargo run --release -p metal2vulkan-runtime --example queue_share -- <mode> \
|
|
//! [--policy adaptive|per-queue] [--seconds S] [--dispatches N] [--unshared] \
|
|
//! [--threads T --in-flight F [--one-vk-queue]] [--long-ms L] [--period-ms P] \
|
|
//! [--isolation bounded|off] [--turns fair|commit-order] [--fixture PATH]
|
|
//! ```
|
|
//!
|
|
//! Every command buffer is one of two kinds. A *draw*: a render pass over a 512x512 `RGBA8Unorm`
|
|
//! texture, cleared and one triangle drawn (`colored_vertex` and `colored_fragment` of the
|
|
//! repository's owned fixture `runtime_examples.metal`, assembled by `llvm-as` on `PATH` from its
|
|
//! `.ll`, the repository's or `--fixture`'s, for a binary carried to another tree). A *long*
|
|
//! one: `--dispatches` dispatches of `scale_and_offset` over one buffer, sized as the run starts
|
|
//! so the command buffer's GPU time is `--long-ms` (30 ms by default). Latencies are a command
|
|
//! buffer's `commit` to the start of its completed handler, on the host's clock.
|
|
//!
|
|
//! - **check**: the device's check of its GPU's idling between `VkQueue`s: its verdict and
|
|
//! switch cost (run where no record of it is kept, read back where one is).
|
|
//! - **isolation**: a compositor thread commits draws at 4,800 a second and never waits; an
|
|
//! application thread commits long command buffers back to back, each waited for. The
|
|
//! compositor's latencies, and how many of them are stalls (over 10 ms). `--unshared` makes the
|
|
//! compositor's queue with `new_command_queue_unshared`.
|
|
//! - **pipelined**: as isolation, but the application keeps two long command buffers in flight,
|
|
//! so its queue holds no idle point.
|
|
//! - **frames**: a compositor thread commits one draw every `--period-ms` (16 ms by default) and
|
|
//! never waits, beside an application thread that keeps `--in-flight` long command buffers in
|
|
//! flight (3 by default). The compositor's latencies and how many ran over 16 ms, the
|
|
//! application's long command buffers a second, and the device's counts of command buffers that
|
|
//! waited behind another queue's batch (`WorkCounts::command_buffers_behind_other_queues` and
|
|
//! kin). `--unshared` makes the compositor's queue with `new_command_queue_unshared`;
|
|
//! `--isolation` sets `UnsharedIsolationPolicy` (`bounded`, the default, or `off`), and
|
|
//! `--turns` sets `QueueTurnPolicy` (`fair`, the default, or `commit-order`): without
|
|
//! `--unshared`, the compositor's plain queue and the application's take turns. The
|
|
//! application's GPU-busy share is its command buffers' GPU time over the span from its first
|
|
//! one's start to its last one's end; the GPU's idle share, that span less every command
|
|
//! buffer's GPU time, either queue's.
|
|
//! - **pairs**: a game's frames beside a compositor's queue: each frame a long command buffer and
|
|
//! then a draw, committed back to back on one queue, `--in-flight` frames in flight, a frame
|
|
//! starting once the draw of the frame that many before ran its completed handler; the compositor commits one draw every `--period-ms`, or none at all with
|
|
//! `--period-ms 0` (its queue still made first). `--unshared` and `--isolation` as for
|
|
//! **frames**. The application's frame period, its GPU time a frame (the long one's GPU start to
|
|
//! the draw's GPU end) and the GPU's idle share over its frames; each command buffer's commit to
|
|
//! its kernel start (the scheduler's hold), the GPU's gap before each long one, and the draw's GPU
|
|
//! end to its completed handler; the device's hold, submission and scheduler counts a frame
|
|
//! (`WorkCounts::batch_hold_nanos` and kin), and the first frames one by one.
|
|
//! - **trap**: as isolation, but the application alternates 500 ms of draws committed at 10,000 a
|
|
//! second with 10 long command buffers.
|
|
//! - **alternating**: as trap, with 2 ms of draws between each long command buffer; and
|
|
//! **slow-alternating**, 1,500 ms.
|
|
//! - **interleave**: two compositor threads, each its own queue, at 4,800 draws a second each; the
|
|
//! first's latencies, and the GPU's idle time between command buffers of different queues as a
|
|
//! share of the run (from their GPU start and end times, in commit order on the GPU).
|
|
//! - **rates**: 2 and 8 threads, each its own queue, committing draws as fast as they can with 3,
|
|
//! then 1, in flight a thread: command buffers a second, and the GPU's idle share at switches.
|
|
//! With `--threads` and `--in-flight`, that one shape, with each command buffer's GPU end and
|
|
//! commit to its completed handler, the device's submission, scheduler and wake counts per
|
|
//! command buffer, and the busiest threads' CPU (Linux); `--one-vk-queue` puts every thread's
|
|
//! queue on one `VkQueue`, each lent once (`with_vk_queue`) so it never moves.
|
|
//! - **idle-queues**: one thread committing draws in turn over 16, then 2,048 queues, 3 in flight:
|
|
//! command buffers a second, and the thread's time in `command_buffer` per command buffer.
|
|
//! - **churn**: command queues made and dropped as a guest's processes make them (about 350 in a
|
|
//! 250 s walk, here `--churn N` over `--seconds`): a compositor thread commits one draw every
|
|
//! `--period-ms` (16 ms) and never waits (`--unshared` makes its queue unshared), three kept
|
|
//! queues commit a draw every 8 ms each, and every `seconds / N` a new queue is made that commits
|
|
//! one long command buffer, then a draw at each later tick, and is dropped 1 to 8 ticks later
|
|
//! (a deterministic sequence). `--untimed` makes the device answer GPU times from the host, as a
|
|
//! consumer that does so does not share `VkQueue`s. The compositor's latencies, the most queues
|
|
//! live at once, and the device's counts of command buffers that waited behind another queue's.
|
|
|
|
use metal2vulkan::device::{
|
|
DevicePolicy, QueueSharingPolicy, QueueTurnPolicy, UnsharedIsolationPolicy,
|
|
};
|
|
use metal2vulkan::metal::{PixelFormat, PrimitiveType, TextureUsage};
|
|
use metal2vulkan::pipeline::{ColorAttachmentDescriptor, RenderPipelineDescriptor, StageSource};
|
|
use metal2vulkan_runtime::{
|
|
Buffer, ClearColor, CommandBuffer, CommandQueue, ComputePipelineState, Device, DeviceOptions,
|
|
LoadAction, PipelineOptions, RenderPassDescriptor, RenderPipelineState, ResourceOptions, Size,
|
|
StoreAction, Texture, TextureDescriptor,
|
|
};
|
|
use std::path::PathBuf;
|
|
use std::process::Command;
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
use std::sync::{Arc, Mutex};
|
|
use std::time::{Duration, Instant};
|
|
|
|
/// A compositor's latency past which it counts as a stall.
|
|
const STALL_MS: f64 = 10.0;
|
|
|
|
/// The draws a second of a compositor thread.
|
|
const COMPOSITOR_RATE: f64 = 4800.0;
|
|
|
|
/// The render target's side, in texels.
|
|
const SIDE: u64 = 512;
|
|
|
|
/// The floats the long command buffers' buffers hold at most.
|
|
const MOST_FLOATS: u64 = 96 << 20;
|
|
|
|
struct Options {
|
|
mode: String,
|
|
policy: QueueSharingPolicy,
|
|
isolation: UnsharedIsolationPolicy,
|
|
turns: QueueTurnPolicy,
|
|
seconds: f64,
|
|
dispatches: u64,
|
|
unshared: bool,
|
|
/// `rates` of this many threads alone (0: the default set).
|
|
threads: usize,
|
|
in_flight: usize,
|
|
/// `rates` with every thread's queue on one `VkQueue`, each lent once so it never moves.
|
|
one_vk_queue: bool,
|
|
/// A long command buffer's GPU time.
|
|
long: Duration,
|
|
/// `frames`: the compositor's period between commits.
|
|
period: Duration,
|
|
/// `churn`: the queues made over the run.
|
|
churn: usize,
|
|
/// The device answers GPU times from the host (`DeviceOptions::gpu_times_from_host`).
|
|
untimed: bool,
|
|
/// The fixture's LLVM text: the repository's where `None`.
|
|
fixture: Option<PathBuf>,
|
|
}
|
|
|
|
fn options() -> Options {
|
|
let mut args = std::env::args().skip(1);
|
|
let mut options = Options {
|
|
mode: args.next().unwrap_or_else(|| "isolation".to_owned()),
|
|
policy: QueueSharingPolicy::Adaptive,
|
|
isolation: UnsharedIsolationPolicy::Bounded,
|
|
turns: QueueTurnPolicy::Fair,
|
|
seconds: 2.0,
|
|
dispatches: 300,
|
|
unshared: false,
|
|
threads: 0,
|
|
in_flight: 3,
|
|
one_vk_queue: false,
|
|
long: Duration::from_millis(30),
|
|
period: Duration::from_millis(16),
|
|
churn: 340,
|
|
untimed: false,
|
|
fixture: None,
|
|
};
|
|
while let Some(arg) = args.next() {
|
|
let mut value = || args.next().unwrap_or_else(|| panic!("{arg} needs a value"));
|
|
match arg.as_str() {
|
|
"--policy" => {
|
|
options.policy = match value().as_str() {
|
|
"adaptive" => QueueSharingPolicy::Adaptive,
|
|
"per-queue" => QueueSharingPolicy::PerQueue,
|
|
other => panic!("no policy {other}"),
|
|
}
|
|
}
|
|
"--isolation" => {
|
|
options.isolation = match value().as_str() {
|
|
"bounded" => UnsharedIsolationPolicy::Bounded,
|
|
"off" => UnsharedIsolationPolicy::Off,
|
|
other => panic!("no isolation {other}"),
|
|
}
|
|
}
|
|
"--turns" => {
|
|
options.turns = match value().as_str() {
|
|
"fair" => QueueTurnPolicy::Fair,
|
|
"commit-order" => QueueTurnPolicy::CommitOrder,
|
|
other => panic!("no turns {other}"),
|
|
}
|
|
}
|
|
"--seconds" => options.seconds = value().parse().unwrap(),
|
|
"--dispatches" => options.dispatches = value().parse().unwrap(),
|
|
"--unshared" => options.unshared = true,
|
|
"--threads" => options.threads = value().parse().unwrap(),
|
|
"--in-flight" => options.in_flight = value().parse().unwrap(),
|
|
"--one-vk-queue" => options.one_vk_queue = true,
|
|
"--churn" => options.churn = value().parse().unwrap(),
|
|
"--untimed" => options.untimed = true,
|
|
"--fixture" => options.fixture = Some(PathBuf::from(value())),
|
|
"--long-ms" => {
|
|
options.long = Duration::from_secs_f64(value().parse::<f64>().unwrap() / 1e3)
|
|
}
|
|
"--period-ms" => {
|
|
options.period = Duration::from_secs_f64(value().parse::<f64>().unwrap() / 1e3)
|
|
}
|
|
other => panic!("unknown argument {other}"),
|
|
}
|
|
}
|
|
options
|
|
}
|
|
|
|
/// What every workload draws and dispatches with.
|
|
struct Kit {
|
|
device: Device,
|
|
draw_state: RenderPipelineState,
|
|
positions: Buffer,
|
|
colors: Buffer,
|
|
compute: ComputePipelineState,
|
|
input: Buffer,
|
|
output: Buffer,
|
|
scale: Buffer,
|
|
/// The grid of one long command buffer's dispatches.
|
|
grid: u64,
|
|
dispatches: u64,
|
|
/// The GPU time a long command buffer is sized for.
|
|
long_time: Duration,
|
|
}
|
|
|
|
impl Kit {
|
|
fn new(options: &Options) -> Kit {
|
|
let device = device(options);
|
|
let settled = device.settle_queue_switch_check(Duration::from_secs(10));
|
|
println!(
|
|
"device: {}; policy {:?}, isolation {:?}; check settled {settled}: {}",
|
|
device.name(),
|
|
options.policy,
|
|
options.isolation,
|
|
device.queue_switch_check_report()
|
|
);
|
|
let bitcode = fixture_bitcode(options.fixture.as_deref());
|
|
let library = metal2vulkan::library::Library::from_air(&bitcode).unwrap();
|
|
let library = device.new_library_with_compile_library(library);
|
|
let vertex = library.new_function_with_name("colored_vertex").unwrap();
|
|
let fragment = library.new_function_with_name("colored_fragment").unwrap();
|
|
let mut descriptor =
|
|
RenderPipelineDescriptor::new(StageSource::Function(vertex.function_descriptor()));
|
|
descriptor.fragment = Some(StageSource::Function(fragment.function_descriptor()));
|
|
descriptor.color_attachments[0] =
|
|
Some(ColorAttachmentDescriptor::new(PixelFormat::RGBA8Unorm));
|
|
let draw_state = device
|
|
.new_render_pipeline_state(&descriptor, &PipelineOptions::default())
|
|
.unwrap();
|
|
let positions: Vec<u8> = [[-1.0f32, -1.0], [1.0, -1.0], [0.0, 1.0]]
|
|
.iter()
|
|
.flatten()
|
|
.flat_map(|v| v.to_le_bytes())
|
|
.collect();
|
|
let colors: Vec<u8> = (0..3)
|
|
.flat_map(|_| [1.0f32, 0.5, 0.0, 1.0])
|
|
.flat_map(|v| v.to_le_bytes())
|
|
.collect();
|
|
let positions = device
|
|
.new_buffer_with_bytes(&positions, ResourceOptions::shared())
|
|
.unwrap();
|
|
let colors = device
|
|
.new_buffer_with_bytes(&colors, ResourceOptions::shared())
|
|
.unwrap();
|
|
let function = library.new_function_with_name("scale_and_offset").unwrap();
|
|
let compute = device
|
|
.new_compute_pipeline_state_with_function(&function, &PipelineOptions::default())
|
|
.unwrap();
|
|
let input = device
|
|
.new_buffer(MOST_FLOATS * 4, ResourceOptions::private())
|
|
.unwrap();
|
|
let output = device
|
|
.new_buffer(MOST_FLOATS * 4, ResourceOptions::private())
|
|
.unwrap();
|
|
let scale = device
|
|
.new_buffer_with_bytes(&1.0f32.to_le_bytes(), ResourceOptions::shared())
|
|
.unwrap();
|
|
let mut kit = Kit {
|
|
device,
|
|
draw_state,
|
|
positions,
|
|
colors,
|
|
compute,
|
|
input,
|
|
output,
|
|
scale,
|
|
grid: 1 << 20,
|
|
dispatches: options.dispatches,
|
|
long_time: options.long,
|
|
};
|
|
kit.size_long();
|
|
kit
|
|
}
|
|
|
|
/// Size the long command buffers' grid so one's GPU time is `long_time`.
|
|
fn size_long(&mut self) {
|
|
let queue = self.device.new_command_queue().unwrap();
|
|
for _ in 0..3 {
|
|
let command_buffer = self.long(&queue);
|
|
command_buffer.commit().unwrap();
|
|
command_buffer.wait_until_completed().unwrap();
|
|
let gpu =
|
|
command_buffer.gpu_end_time().unwrap() - command_buffer.gpu_start_time().unwrap();
|
|
let wanted = self.grid as f64 * self.long_time.as_secs_f64() / gpu.max(1e-6);
|
|
self.grid = (wanted as u64).clamp(1 << 16, MOST_FLOATS) & !63;
|
|
}
|
|
let command_buffer = self.long(&queue);
|
|
command_buffer.commit().unwrap();
|
|
command_buffer.wait_until_completed().unwrap();
|
|
let gpu = command_buffer.gpu_end_time().unwrap() - command_buffer.gpu_start_time().unwrap();
|
|
println!(
|
|
"long command buffer: {} dispatches of {} threads, {:.1} ms of GPU time",
|
|
self.dispatches,
|
|
self.grid,
|
|
gpu * 1e3
|
|
);
|
|
}
|
|
|
|
/// A long command buffer of `queue`'s, not committed.
|
|
fn long(&self, queue: &CommandQueue) -> CommandBuffer {
|
|
let command_buffer = queue.command_buffer().unwrap();
|
|
let mut encoder = command_buffer.compute_command_encoder().unwrap();
|
|
encoder.set_compute_pipeline_state(&self.compute).unwrap();
|
|
encoder.set_buffer(Some(&self.scale), 0, 2).unwrap();
|
|
for dispatch in 0..self.dispatches {
|
|
let (from, to) = match dispatch % 2 {
|
|
0 => (&self.input, &self.output),
|
|
_ => (&self.output, &self.input),
|
|
};
|
|
encoder.set_buffer(Some(from), 0, 0).unwrap();
|
|
encoder.set_buffer(Some(to), 0, 1).unwrap();
|
|
encoder
|
|
.dispatch_threads(Size::new(self.grid, 1, 1), Size::new(64, 1, 1))
|
|
.unwrap();
|
|
}
|
|
encoder.end_encoding().unwrap();
|
|
command_buffer
|
|
}
|
|
|
|
/// A draw command buffer of `queue`'s into `target`, not committed.
|
|
fn draw(&self, queue: &CommandQueue, target: &Texture) -> CommandBuffer {
|
|
self.draw_into(queue.command_buffer().unwrap(), target)
|
|
}
|
|
|
|
/// `command_buffer` with a draw into `target` encoded, not committed.
|
|
fn draw_into(&self, command_buffer: CommandBuffer, target: &Texture) -> CommandBuffer {
|
|
let mut pass = RenderPassDescriptor::new();
|
|
let color = &mut pass.color_attachments[0];
|
|
color.texture = Some(target.clone());
|
|
color.load_action = LoadAction::Clear;
|
|
color.store_action = StoreAction::Store;
|
|
color.clear_color = ClearColor::new(0.0, 0.0, 0.0, 1.0);
|
|
let mut encoder = command_buffer.render_command_encoder(&pass).unwrap();
|
|
encoder.set_render_pipeline_state(&self.draw_state).unwrap();
|
|
encoder
|
|
.set_vertex_buffer(Some(&self.positions), 0, 0)
|
|
.unwrap();
|
|
encoder.set_vertex_buffer(Some(&self.colors), 0, 1).unwrap();
|
|
encoder
|
|
.draw_primitives(PrimitiveType::Triangle, 0, 3, 1, 0)
|
|
.unwrap();
|
|
encoder.end_encoding().unwrap();
|
|
command_buffer
|
|
}
|
|
|
|
fn target(&self) -> Texture {
|
|
let mut texture = TextureDescriptor::texture_2d(PixelFormat::RGBA8Unorm, SIDE, SIDE, false);
|
|
texture.usage = TextureUsage::RENDER_TARGET;
|
|
texture.resource_options = ResourceOptions::private();
|
|
self.device.new_texture(&texture).unwrap()
|
|
}
|
|
|
|
fn queue(&self, unshared: bool) -> CommandQueue {
|
|
match unshared {
|
|
true => self.device.new_command_queue_unshared(64).unwrap(),
|
|
false => self.device.new_command_queue().unwrap(),
|
|
}
|
|
}
|
|
|
|
fn counts(&self) -> String {
|
|
let c = self.device.work_counts();
|
|
format!(
|
|
"moves {} joins {} gone long {} settles {} off unshared {} withheld {}",
|
|
c.queue_moves,
|
|
c.queue_joins,
|
|
c.queues_gone_long,
|
|
c.long_queue_settles,
|
|
c.queue_moves_off_unshared,
|
|
c.joins_withheld_from_unshared
|
|
)
|
|
}
|
|
}
|
|
|
|
/// The system default device, of the default policy but for `queue_sharing`,
|
|
/// `unshared_isolation` and `queue_turns`, as `options` say.
|
|
fn device(options: &Options) -> Device {
|
|
let mut device = DeviceOptions::default();
|
|
device.policy = DevicePolicy::UNTRUSTED;
|
|
device.policy.queue_sharing = options.policy;
|
|
device.policy.unshared_isolation = options.isolation;
|
|
device.policy.queue_turns = options.turns;
|
|
device.gpu_times_from_host = options.untimed;
|
|
Device::system_default_with(&device).unwrap()
|
|
}
|
|
|
|
/// The owned fixture's LLVM text, at `fixture` where given, assembled to AIR bitcode by `llvm-as`.
|
|
fn fixture_bitcode(fixture: Option<&std::path::Path>) -> Vec<u8> {
|
|
let ll = fixture.map_or_else(
|
|
|| {
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.join("../metal2vulkan-validation/fixtures/public/examples/runtime_examples.ll")
|
|
},
|
|
PathBuf::from,
|
|
);
|
|
let output = Command::new("llvm-as")
|
|
.arg("-o")
|
|
.arg("-")
|
|
.arg(&ll)
|
|
.output()
|
|
.expect("llvm-as on PATH");
|
|
assert!(
|
|
output.status.success(),
|
|
"{}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
output.stdout
|
|
}
|
|
|
|
/// Each draw's queue number and its GPU start and end there, in seconds.
|
|
type Times = Arc<Mutex<Vec<(usize, f64, f64)>>>;
|
|
|
|
/// A queue's changes of `VkQueue` or long mark: its command buffers made by then, the
|
|
/// microseconds into the run, and its `VkQueue` and mark.
|
|
type Seen = Vec<(u64, u128, (usize, bool))>;
|
|
|
|
/// The p50, p99 and largest of `samples` (milliseconds), and how many are stalls.
|
|
fn summary(samples: &mut [f64]) -> String {
|
|
if samples.is_empty() {
|
|
return "no samples".to_owned();
|
|
}
|
|
samples.sort_by(f64::total_cmp);
|
|
let at = |p: f64| samples[((samples.len() as f64 * p) as usize).min(samples.len() - 1)];
|
|
let stalls = samples.iter().filter(|&&s| s > STALL_MS).count();
|
|
format!(
|
|
"p50 {:.3} p90 {:.3} p99 {:.3} max {:.1} ms, {} stalls over {STALL_MS} ms, {} samples",
|
|
at(0.5),
|
|
at(0.9),
|
|
at(0.99),
|
|
samples[samples.len() - 1],
|
|
stalls,
|
|
samples.len()
|
|
)
|
|
}
|
|
|
|
/// A thread committing draws on `queue` at `rate` a second until `stop`, never waiting, each
|
|
/// draw's latency in `latencies` and, where `times` is given, its queue's number and its GPU start
|
|
/// and end there.
|
|
fn compositor(
|
|
kit: &Arc<Kit>,
|
|
queue: CommandQueue,
|
|
rate: f64,
|
|
stop: &Arc<AtomicBool>,
|
|
latencies: &Arc<Mutex<Vec<f64>>>,
|
|
times: Option<(usize, Times)>,
|
|
) -> std::thread::JoinHandle<()> {
|
|
let kit = Arc::clone(kit);
|
|
let stop = Arc::clone(stop);
|
|
let latencies = Arc::clone(latencies);
|
|
std::thread::spawn(move || {
|
|
let target = kit.target();
|
|
let period = Duration::from_secs_f64(1.0 / rate);
|
|
let mut next = Instant::now();
|
|
while !stop.load(Ordering::Relaxed) {
|
|
let command_buffer = kit.draw(&queue, &target);
|
|
let latencies = Arc::clone(&latencies);
|
|
let times = times.clone();
|
|
let committed = Arc::new(Mutex::new(Instant::now()));
|
|
let at = Arc::clone(&committed);
|
|
command_buffer
|
|
.add_completed_handler(Box::new(move |done: &CommandBuffer| {
|
|
let latency = at.lock().unwrap().elapsed().as_secs_f64() * 1e3;
|
|
latencies.lock().unwrap().push(latency);
|
|
if let Some((queue, times)) = × {
|
|
if let (Ok(start), Ok(end)) = (done.gpu_start_time(), done.gpu_end_time()) {
|
|
times.lock().unwrap().push((*queue, start, end));
|
|
}
|
|
}
|
|
}))
|
|
.unwrap();
|
|
*committed.lock().unwrap() = Instant::now();
|
|
command_buffer.commit().unwrap();
|
|
drop(command_buffer);
|
|
next += period;
|
|
let now = Instant::now();
|
|
match next > now {
|
|
true => std::thread::sleep(next - now),
|
|
false => next = now,
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/// The application's loop until `stop`, on its own thread: `long_every` long command buffers after
|
|
/// each `short` of draws at 10,000 a second (none where `short` is zero), each long one waited for,
|
|
/// or with `in_flight` long ones in flight where above 1, each long one's GPU start and end in
|
|
/// `times` where given, as queue 0. Answers how many long ones completed.
|
|
fn application(
|
|
kit: &Arc<Kit>,
|
|
(short, long_every, in_flight): (Duration, usize, usize),
|
|
stop: &Arc<AtomicBool>,
|
|
times: Option<Times>,
|
|
) -> std::thread::JoinHandle<u64> {
|
|
let kit = Arc::clone(kit);
|
|
let stop = Arc::clone(stop);
|
|
std::thread::spawn(move || {
|
|
let queue = kit.device.new_command_queue().unwrap();
|
|
let target = kit.target();
|
|
let mut flying: std::collections::VecDeque<CommandBuffer> = Default::default();
|
|
let mut longs = 0u64;
|
|
while !stop.load(Ordering::Relaxed) {
|
|
if !short.is_zero() {
|
|
let began = Instant::now();
|
|
let period = Duration::from_secs_f64(1.0 / 10_000.0);
|
|
let mut next = began;
|
|
while began.elapsed() < short && !stop.load(Ordering::Relaxed) {
|
|
let command_buffer = kit.draw(&queue, &target);
|
|
command_buffer.commit().unwrap();
|
|
next += period;
|
|
let now = Instant::now();
|
|
if next > now {
|
|
std::thread::sleep(next - now);
|
|
}
|
|
}
|
|
}
|
|
for _ in 0..long_every {
|
|
if stop.load(Ordering::Relaxed) {
|
|
break;
|
|
}
|
|
let command_buffer = kit.long(&queue);
|
|
if let Some(times) = × {
|
|
let times = Arc::clone(times);
|
|
command_buffer
|
|
.add_completed_handler(Box::new(move |done: &CommandBuffer| {
|
|
if let (Ok(start), Ok(end)) =
|
|
(done.gpu_start_time(), done.gpu_end_time())
|
|
{
|
|
times.lock().unwrap().push((0, start, end));
|
|
}
|
|
}))
|
|
.unwrap();
|
|
}
|
|
command_buffer.commit().unwrap();
|
|
flying.push_back(command_buffer);
|
|
while flying.len() >= in_flight.max(1) {
|
|
flying.pop_front().unwrap().wait_until_completed().unwrap();
|
|
longs += 1;
|
|
}
|
|
}
|
|
}
|
|
for command_buffer in flying {
|
|
command_buffer.wait_until_completed().unwrap();
|
|
longs += 1;
|
|
}
|
|
longs
|
|
})
|
|
}
|
|
|
|
/// A compositor beside an application of `short` draws between `long_every` long command buffers,
|
|
/// `in_flight` of them in flight: the compositor's latencies.
|
|
fn beside(kit: &Arc<Kit>, options: &Options, short: Duration, long_every: usize, in_flight: usize) {
|
|
let stop = Arc::new(AtomicBool::new(false));
|
|
let latencies = Arc::new(Mutex::new(Vec::new()));
|
|
let app = application(kit, (short, long_every, in_flight), &stop, None);
|
|
std::thread::sleep(Duration::from_millis(50));
|
|
let queue = kit.queue(options.unshared);
|
|
let comp = compositor(kit, queue, COMPOSITOR_RATE, &stop, &latencies, None);
|
|
std::thread::sleep(Duration::from_secs_f64(options.seconds));
|
|
stop.store(true, Ordering::Relaxed);
|
|
comp.join().unwrap();
|
|
app.join().unwrap();
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
let mut samples = latencies.lock().unwrap().clone();
|
|
println!(
|
|
"{} (policy {:?}, compositor {}, {} dispatches, short {:?} x{} in flight {}): compositor {}; {}",
|
|
options.mode,
|
|
options.policy,
|
|
if options.unshared { "unshared" } else { "shared" },
|
|
kit.dispatches,
|
|
short,
|
|
long_every,
|
|
in_flight,
|
|
summary(&mut samples),
|
|
kit.counts()
|
|
);
|
|
}
|
|
|
|
/// A compositor committing one draw every `options.period` beside an application keeping
|
|
/// `options.in_flight` long command buffers in flight: the compositor's latencies, the
|
|
/// application's long command buffers a second, and the device's counts of waits behind other
|
|
/// queues' batches over the run. The compositor's queue is made before the application starts, as
|
|
/// a compositor's exists before the applications it shows: other queues' batches are held back
|
|
/// only while an unshared queue exists.
|
|
fn frames(kit: &Arc<Kit>, options: &Options) {
|
|
let stop = Arc::new(AtomicBool::new(false));
|
|
let latencies = Arc::new(Mutex::new(Vec::new()));
|
|
let times: Times = Arc::new(Mutex::new(Vec::new()));
|
|
let before = kit.device.work_counts();
|
|
let began = Instant::now();
|
|
let shape = (Duration::ZERO, 1, options.in_flight);
|
|
let queue = kit.queue(options.unshared);
|
|
let app = application(kit, shape, &stop, Some(Arc::clone(×)));
|
|
std::thread::sleep(Duration::from_millis(50));
|
|
let rate = 1.0 / options.period.as_secs_f64();
|
|
let comp = compositor(
|
|
kit,
|
|
queue,
|
|
rate,
|
|
&stop,
|
|
&latencies,
|
|
Some((1, Arc::clone(×))),
|
|
);
|
|
std::thread::sleep(Duration::from_secs_f64(options.seconds));
|
|
stop.store(true, Ordering::Relaxed);
|
|
comp.join().unwrap();
|
|
let longs = app.join().unwrap();
|
|
let elapsed = began.elapsed().as_secs_f64();
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
let c = kit.device.work_counts();
|
|
let mut samples = latencies.lock().unwrap().clone();
|
|
let over_16 = samples.iter().filter(|&&ms| ms > 16.0).count();
|
|
let unshared_16 = c.unshared_command_buffers_behind_other_queues_over_16ms
|
|
- before.unshared_command_buffers_behind_other_queues_over_16ms;
|
|
let per_16 = |sum: u64| sum as f64 / unshared_16.max(1) as f64;
|
|
let (app_busy, gpu_idle) = busy_shares(×.lock().unwrap());
|
|
let app_spans: Vec<f64> = (times.lock().unwrap().iter())
|
|
.filter(|(queue, _, _)| *queue == 0)
|
|
.map(|(_, start, end)| end - start)
|
|
.collect();
|
|
let app_mean = app_spans.iter().sum::<f64>() / app_spans.len().max(1) as f64;
|
|
let submissions = c.queue_submissions - before.queue_submissions;
|
|
println!(
|
|
"frames (policy {:?}, isolation {:?}, turns {:?}, compositor {} every {:?}, application {} in flight of {:.1} ms): compositor {}, {over_16} over 16 ms; application {:.2} long command buffers/s ({:.2} ms of GPU time each), GPU-busy {:.1}%; GPU idle {:.2}%; {:.0} kernel submissions/s; {} looks held a head for isolation, {} for its turn, {} let go on an idle turn",
|
|
options.policy,
|
|
options.isolation,
|
|
options.turns,
|
|
if options.unshared { "unshared" } else { "shared" },
|
|
options.period,
|
|
options.in_flight,
|
|
kit.long_time.as_secs_f64() * 1e3,
|
|
summary(&mut samples),
|
|
longs as f64 / elapsed,
|
|
app_mean * 1e3,
|
|
app_busy * 100.0,
|
|
gpu_idle * 100.0,
|
|
submissions as f64 / elapsed,
|
|
c.heads_held_for_unshared_isolation - before.heads_held_for_unshared_isolation,
|
|
c.heads_held_for_queue_turns - before.heads_held_for_queue_turns,
|
|
c.heads_let_go_turn_idle - before.heads_let_go_turn_idle,
|
|
);
|
|
println!(
|
|
" behind other queues: {} ({} over 16 ms, {} over 33 ms, longest {:.1} ms); unshared {} ({} over 16 ms, {} over 33 ms, longest {:.1} ms); unshared waits over 16 ms: {:.2} batches ahead on average, at most {}, longest run {:.1} ms on average, {} behind one run over 16 ms",
|
|
c.command_buffers_behind_other_queues - before.command_buffers_behind_other_queues,
|
|
c.command_buffers_behind_other_queues_over_16ms
|
|
- before.command_buffers_behind_other_queues_over_16ms,
|
|
c.command_buffers_behind_other_queues_over_33ms
|
|
- before.command_buffers_behind_other_queues_over_33ms,
|
|
c.wait_behind_other_queues_longest_nanos as f64 / 1e6,
|
|
c.unshared_command_buffers_behind_other_queues
|
|
- before.unshared_command_buffers_behind_other_queues,
|
|
unshared_16,
|
|
c.unshared_command_buffers_behind_other_queues_over_33ms
|
|
- before.unshared_command_buffers_behind_other_queues_over_33ms,
|
|
c.unshared_wait_behind_other_queues_longest_nanos as f64 / 1e6,
|
|
per_16(c.unshared_waits_over_16ms_batches_ahead - before.unshared_waits_over_16ms_batches_ahead),
|
|
c.unshared_waits_over_16ms_most_batches_ahead,
|
|
per_16(
|
|
c.unshared_waits_over_16ms_longest_runs_nanos
|
|
- before.unshared_waits_over_16ms_longest_runs_nanos
|
|
) / 1e6,
|
|
c.unshared_waits_over_16ms_behind_one_long_run
|
|
- before.unshared_waits_over_16ms_behind_one_long_run,
|
|
);
|
|
}
|
|
|
|
/// One frame of `pairs`, in seconds of `host_time`'s clock: its start once its slot was free; the
|
|
/// long command buffer's commit, kernel start, GPU start and GPU end; and the draw's, then its
|
|
/// completed handler's start.
|
|
#[derive(Clone, Copy, Default)]
|
|
struct Pair {
|
|
start: f64,
|
|
long: [f64; 4],
|
|
draw: [f64; 5],
|
|
}
|
|
|
|
/// The p50 and p90 of `samples` (seconds), in milliseconds.
|
|
fn p50_p90(mut samples: Vec<f64>) -> String {
|
|
if samples.is_empty() {
|
|
return "none".to_owned();
|
|
}
|
|
samples.sort_by(f64::total_cmp);
|
|
let at = |p: f64| samples[((samples.len() as f64 * p) as usize).min(samples.len() - 1)] * 1e3;
|
|
format!("p50 {:.2} p90 {:.2}", at(0.5), at(0.9))
|
|
}
|
|
|
|
/// The `pairs` mode (the module docs).
|
|
fn pairs(kit: &Arc<Kit>, options: &Options) {
|
|
/// The frames before the measured ones.
|
|
const WARM: usize = 10;
|
|
let stop = Arc::new(AtomicBool::new(false));
|
|
let latencies = Arc::new(Mutex::new(Vec::new()));
|
|
let queue = kit.queue(options.unshared);
|
|
let comp = (!options.period.is_zero()).then(|| {
|
|
let rate = 1.0 / options.period.as_secs_f64();
|
|
compositor(kit, queue.clone(), rate, &stop, &latencies, None)
|
|
});
|
|
let app = kit.device.new_command_queue().unwrap();
|
|
let target = kit.target();
|
|
let in_flight = options.in_flight.max(1);
|
|
let slots = Arc::new((Mutex::new(in_flight), std::sync::Condvar::new()));
|
|
let frames: Arc<Mutex<Vec<Pair>>> = Arc::new(Mutex::new(Vec::new()));
|
|
let mut before = None;
|
|
let began = Instant::now();
|
|
let mut count = 0;
|
|
while began.elapsed().as_secs_f64() < options.seconds || count <= WARM {
|
|
{
|
|
let (free, freed) = &*slots;
|
|
let mut free = free.lock().unwrap();
|
|
while *free == 0 {
|
|
free = freed.wait(free).unwrap();
|
|
}
|
|
*free -= 1;
|
|
}
|
|
if count == WARM {
|
|
before = Some((kit.device.work_counts(), Instant::now()));
|
|
}
|
|
let start = metal2vulkan_runtime::host_time();
|
|
frames.lock().unwrap().push(Pair {
|
|
start,
|
|
..Pair::default()
|
|
});
|
|
let long = kit.long(&app);
|
|
let at = Arc::clone(&frames);
|
|
long.add_completed_handler(Box::new(move |done: &CommandBuffer| {
|
|
let pair = &mut at.lock().unwrap()[count];
|
|
pair.long[1] = done.kernel_start_time();
|
|
pair.long[2] = done.gpu_start_time().unwrap_or(0.0);
|
|
pair.long[3] = done.gpu_end_time().unwrap_or(0.0);
|
|
}))
|
|
.unwrap();
|
|
frames.lock().unwrap()[count].long[0] = metal2vulkan_runtime::host_time();
|
|
long.commit().unwrap();
|
|
drop(long);
|
|
let draw = kit.draw(&app, &target);
|
|
let (at, slot) = (Arc::clone(&frames), Arc::clone(&slots));
|
|
draw.add_completed_handler(Box::new(move |done: &CommandBuffer| {
|
|
let handled = metal2vulkan_runtime::host_time();
|
|
{
|
|
let pair = &mut at.lock().unwrap()[count];
|
|
pair.draw[1] = done.kernel_start_time();
|
|
pair.draw[2] = done.gpu_start_time().unwrap_or(0.0);
|
|
pair.draw[3] = done.gpu_end_time().unwrap_or(0.0);
|
|
pair.draw[4] = handled;
|
|
}
|
|
let (free, freed) = &*slot;
|
|
*free.lock().unwrap() += 1;
|
|
freed.notify_one();
|
|
}))
|
|
.unwrap();
|
|
frames.lock().unwrap()[count].draw[0] = metal2vulkan_runtime::host_time();
|
|
draw.commit().unwrap();
|
|
drop(draw);
|
|
count += 1;
|
|
}
|
|
{
|
|
let (free, freed) = &*slots;
|
|
let mut free = free.lock().unwrap();
|
|
while *free < in_flight {
|
|
free = freed.wait(free).unwrap();
|
|
}
|
|
}
|
|
let after = kit.device.work_counts();
|
|
let (before, from) = before.unwrap();
|
|
let elapsed = from.elapsed().as_secs_f64();
|
|
stop.store(true, Ordering::Relaxed);
|
|
if let Some(comp) = comp {
|
|
comp.join().unwrap();
|
|
}
|
|
drop(queue);
|
|
let all = frames.lock().unwrap().clone();
|
|
let measured = &all[WARM..];
|
|
let n = measured.len() as f64;
|
|
let periods: Vec<f64> = (all[WARM - 1..].windows(2))
|
|
.map(|w| w[1].start - w[0].start)
|
|
.collect();
|
|
let gpu: Vec<f64> = measured.iter().map(|f| f.draw[3] - f.long[2]).collect();
|
|
let busy: f64 = (measured.iter())
|
|
.map(|f| (f.long[3] - f.long[2]) + (f.draw[3] - f.draw[2]))
|
|
.sum();
|
|
let span = measured[measured.len() - 1].draw[3] - measured[0].long[2];
|
|
let long_held: Vec<f64> = measured.iter().map(|f| f.long[1] - f.long[0]).collect();
|
|
let draw_held: Vec<f64> = measured.iter().map(|f| f.draw[1] - f.draw[0]).collect();
|
|
let gaps: Vec<f64> = (all[WARM - 1..].windows(2))
|
|
.map(|w| w[1].long[2] - w[0].draw[3])
|
|
.collect();
|
|
let seen: Vec<f64> = measured.iter().map(|f| f.draw[4] - f.draw[3]).collect();
|
|
let commit_to_seen: Vec<f64> = measured.iter().map(|f| f.draw[4] - f.long[0]).collect();
|
|
let per = |a: u64, b: u64| (a - b) as f64 / n;
|
|
println!(
|
|
"pairs (isolation {:?}, compositor {} {}, {} frames in flight, a long command buffer of \
|
|
{:.1} ms and a draw a frame): {:.1} frames/s; frame period {} ms (mean {:.2}); GPU a frame \
|
|
{} ms; GPU idle {:.1}% of the frames' span",
|
|
options.isolation,
|
|
if options.unshared { "unshared" } else { "shared" },
|
|
match options.period.is_zero() {
|
|
true => "committing nothing".to_owned(),
|
|
false => format!("every {:?}", options.period),
|
|
},
|
|
in_flight,
|
|
kit.long_time.as_secs_f64() * 1e3,
|
|
n / elapsed,
|
|
p50_p90(periods.clone()),
|
|
periods.iter().sum::<f64>() / periods.len().max(1) as f64 * 1e3,
|
|
p50_p90(gpu),
|
|
(1.0 - busy / span.max(1e-9)) * 100.0,
|
|
);
|
|
println!(
|
|
" commit to kernel start: long {} ms, draw {} ms; GPU gap before a long one {} ms; the \
|
|
draw's GPU end to its completed handler {} ms; the long one's commit to the draw's \
|
|
handler {} ms",
|
|
p50_p90(long_held),
|
|
p50_p90(draw_held),
|
|
p50_p90(gaps.clone()),
|
|
p50_p90(seen),
|
|
p50_p90(commit_to_seen),
|
|
);
|
|
println!(
|
|
" a frame: batch_hold_nanos {:.2} ms, batches_held_past_a_look {:.2}, \
|
|
heads_held_for_unshared_isolation {:.2}, scheduler_slice_rescues {:.2}, \
|
|
scheduler_sleep_slices {:.2}, scheduler_wakes_completion {:.2}, scheduler_sleeps {:.2}, \
|
|
queue_submissions {:.2}, command_buffers_behind_other_queues {:.2}; compositor {}",
|
|
per(after.batch_hold_nanos, before.batch_hold_nanos) / 1e6,
|
|
per(
|
|
after.batches_held_past_a_look,
|
|
before.batches_held_past_a_look
|
|
),
|
|
per(
|
|
after.heads_held_for_unshared_isolation,
|
|
before.heads_held_for_unshared_isolation
|
|
),
|
|
per(
|
|
after.scheduler_slice_rescues,
|
|
before.scheduler_slice_rescues
|
|
),
|
|
per(after.scheduler_sleep_slices, before.scheduler_sleep_slices),
|
|
per(
|
|
after.scheduler_wakes_completion,
|
|
before.scheduler_wakes_completion
|
|
),
|
|
per(after.scheduler_sleeps, before.scheduler_sleeps),
|
|
per(after.queue_submissions, before.queue_submissions),
|
|
per(
|
|
after.command_buffers_behind_other_queues,
|
|
before.command_buffers_behind_other_queues
|
|
),
|
|
summary(&mut latencies.lock().unwrap().clone()),
|
|
);
|
|
let first: Vec<String> = (0..8.min(periods.len()))
|
|
.map(|i| {
|
|
let f = &measured[i];
|
|
format!(
|
|
"{:.1}/{:.1}/{:.1}/{:.1}",
|
|
periods[i] * 1e3,
|
|
gaps[i] * 1e3,
|
|
(f.long[1] - f.long[0]) * 1e3,
|
|
(f.draw[1] - f.draw[0]) * 1e3
|
|
)
|
|
})
|
|
.collect();
|
|
println!(
|
|
" first frames (period/GPU gap before its long one/long held/draw held, ms): {}",
|
|
first.join(" ")
|
|
);
|
|
}
|
|
|
|
/// The `churn` mode (the module docs).
|
|
fn churn(kit: &Arc<Kit>, options: &Options) {
|
|
let stop = Arc::new(AtomicBool::new(false));
|
|
let latencies = Arc::new(Mutex::new(Vec::new()));
|
|
let before = kit.device.work_counts();
|
|
let comp = compositor(
|
|
kit,
|
|
kit.queue(options.unshared),
|
|
1.0 / options.period.as_secs_f64(),
|
|
&stop,
|
|
&latencies,
|
|
None,
|
|
);
|
|
let kept: Vec<std::thread::JoinHandle<()>> = (0..3)
|
|
.map(|_| {
|
|
let kit = Arc::clone(kit);
|
|
let stop = Arc::clone(&stop);
|
|
std::thread::spawn(move || {
|
|
let queue = kit.device.new_command_queue().unwrap();
|
|
let target = kit.target();
|
|
while !stop.load(Ordering::Relaxed) {
|
|
kit.draw(&queue, &target).commit().unwrap();
|
|
std::thread::sleep(Duration::from_millis(8));
|
|
}
|
|
})
|
|
})
|
|
.collect();
|
|
let tick = Duration::from_secs_f64(options.seconds / options.churn.max(1) as f64);
|
|
let target = kit.target();
|
|
let mut live: Vec<(CommandQueue, usize, CommandBuffer)> = Vec::new();
|
|
let mut seed = 0x2545_f491_4f6c_dd1du64;
|
|
let mut most = 0;
|
|
let mut next = Instant::now();
|
|
for made in 0..options.churn {
|
|
live.retain(|(_, until, last)| {
|
|
let keep = *until > made;
|
|
if !keep {
|
|
last.wait_until_completed().unwrap();
|
|
}
|
|
keep
|
|
});
|
|
for (queue, _, last) in &mut live {
|
|
let draw = kit.draw(queue, &target);
|
|
draw.commit().unwrap();
|
|
*last = draw;
|
|
}
|
|
let queue = kit.device.new_command_queue().unwrap();
|
|
let long = kit.long(&queue);
|
|
long.commit().unwrap();
|
|
seed =
|
|
(seed.wrapping_mul(6_364_136_223_846_793_005)).wrapping_add(1_442_695_040_888_963_407);
|
|
live.push((queue, made + 1 + ((seed >> 33) % 8) as usize, long));
|
|
// The compositor's, the kept ones and the churn's.
|
|
most = most.max(1 + 3 + live.len());
|
|
next += tick;
|
|
let now = Instant::now();
|
|
if next > now {
|
|
std::thread::sleep(next - now);
|
|
}
|
|
}
|
|
stop.store(true, Ordering::Relaxed);
|
|
comp.join().unwrap();
|
|
for thread in kept {
|
|
thread.join().unwrap();
|
|
}
|
|
for (_, _, last) in &live {
|
|
last.wait_until_completed().unwrap();
|
|
}
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
let c = kit.device.work_counts();
|
|
let mut samples = latencies.lock().unwrap().clone();
|
|
let over_16 = samples.iter().filter(|&&ms| ms > 16.0).count();
|
|
println!(
|
|
"churn (policy {:?}, untimed {}, compositor {} every {:?}, {} queues made, one every {:.0} ms, each with one long command buffer of {:.1} ms, at most {most} live): compositor {}, {over_16} over 16 ms; {} queues past the round robin",
|
|
options.policy,
|
|
options.untimed,
|
|
if options.unshared { "unshared" } else { "shared" },
|
|
options.period,
|
|
options.churn,
|
|
tick.as_secs_f64() * 1e3,
|
|
kit.long_time.as_secs_f64() * 1e3,
|
|
summary(&mut samples),
|
|
c.queues_sharing_a_vk_queue - before.queues_sharing_a_vk_queue,
|
|
);
|
|
println!(
|
|
" behind other queues: {} ({} over 16 ms, {} over 33 ms, longest {:.1} ms); unshared {} ({} over 16 ms, {} over 33 ms, longest {:.1} ms)",
|
|
c.command_buffers_behind_other_queues - before.command_buffers_behind_other_queues,
|
|
c.command_buffers_behind_other_queues_over_16ms
|
|
- before.command_buffers_behind_other_queues_over_16ms,
|
|
c.command_buffers_behind_other_queues_over_33ms
|
|
- before.command_buffers_behind_other_queues_over_33ms,
|
|
c.wait_behind_other_queues_longest_nanos as f64 / 1e6,
|
|
c.unshared_command_buffers_behind_other_queues
|
|
- before.unshared_command_buffers_behind_other_queues,
|
|
c.unshared_command_buffers_behind_other_queues_over_16ms
|
|
- before.unshared_command_buffers_behind_other_queues_over_16ms,
|
|
c.unshared_command_buffers_behind_other_queues_over_33ms
|
|
- before.unshared_command_buffers_behind_other_queues_over_33ms,
|
|
c.unshared_wait_behind_other_queues_longest_nanos as f64 / 1e6,
|
|
);
|
|
}
|
|
|
|
/// Of `times` (queue, GPU start, GPU end), queue 0's GPU time over the span from its first start
|
|
/// to its last end, and the share of that span no command buffer of either queue ran in.
|
|
fn busy_shares(times: &[(usize, f64, f64)]) -> (f64, f64) {
|
|
let app: Vec<&(usize, f64, f64)> = times.iter().filter(|(queue, _, _)| *queue == 0).collect();
|
|
let (Some(from), Some(to)) = (
|
|
app.iter().map(|t| t.1).min_by(f64::total_cmp),
|
|
app.iter().map(|t| t.2).max_by(f64::total_cmp),
|
|
) else {
|
|
return (0.0, 0.0);
|
|
};
|
|
let span = (to - from).max(1e-9);
|
|
let busy: f64 = app.iter().map(|(_, start, end)| end - start).sum();
|
|
let mut all: Vec<(f64, f64)> = (times.iter())
|
|
.map(|&(_, start, end)| (start.max(from), end.min(to)))
|
|
.filter(|(start, end)| end > start)
|
|
.collect();
|
|
all.sort_by(|a, b| a.0.total_cmp(&b.0));
|
|
let (mut covered, mut reach) = (0.0, from);
|
|
for (start, end) in all {
|
|
if end > reach {
|
|
covered += end - start.max(reach);
|
|
reach = end;
|
|
}
|
|
}
|
|
(busy / span, 1.0 - covered / span)
|
|
}
|
|
|
|
/// The GPU's idle time between command buffers of different queues, as a share of the span of
|
|
/// `times` (queue, GPU start, GPU end), in GPU start order; with the switches counted.
|
|
fn switch_idle(times: &mut [(usize, f64, f64)]) -> (f64, usize) {
|
|
if times.len() < 2 {
|
|
return (0.0, 0);
|
|
}
|
|
times.sort_by(|a, b| a.1.total_cmp(&b.1));
|
|
let span = times[times.len() - 1].2 - times[0].1;
|
|
let mut idle = 0.0;
|
|
let mut switches = 0;
|
|
for pair in times.windows(2) {
|
|
if pair[0].0 != pair[1].0 {
|
|
switches += 1;
|
|
idle += (pair[1].1 - pair[0].2).max(0.0);
|
|
}
|
|
}
|
|
(idle / span.max(1e-9), switches)
|
|
}
|
|
|
|
fn interleave(kit: &Arc<Kit>, options: &Options) {
|
|
let stop = Arc::new(AtomicBool::new(false));
|
|
let first = Arc::new(Mutex::new(Vec::new()));
|
|
let second = Arc::new(Mutex::new(Vec::new()));
|
|
let times = Arc::new(Mutex::new(Vec::new()));
|
|
let a = compositor(
|
|
kit,
|
|
kit.queue(false),
|
|
COMPOSITOR_RATE,
|
|
&stop,
|
|
&first,
|
|
Some((0, Arc::clone(×))),
|
|
);
|
|
let b = compositor(
|
|
kit,
|
|
kit.queue(false),
|
|
COMPOSITOR_RATE,
|
|
&stop,
|
|
&second,
|
|
Some((1, Arc::clone(×))),
|
|
);
|
|
std::thread::sleep(Duration::from_secs_f64(options.seconds));
|
|
stop.store(true, Ordering::Relaxed);
|
|
a.join().unwrap();
|
|
b.join().unwrap();
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
let mut samples: Vec<f64> = first.lock().unwrap().iter().map(|ms| ms * 1e3).collect();
|
|
samples.sort_by(f64::total_cmp);
|
|
let at = |p: f64| samples[((samples.len() as f64 * p) as usize).min(samples.len() - 1)];
|
|
let (idle, switches) = switch_idle(&mut times.lock().unwrap());
|
|
println!(
|
|
"interleave (policy {:?}): first stream p50 {:.1} p99 {:.1} us; GPU idle at {switches} switches {:.1}% of the span; {}",
|
|
options.policy,
|
|
at(0.5),
|
|
at(0.99),
|
|
idle * 100.0,
|
|
kit.counts()
|
|
);
|
|
}
|
|
|
|
/// `threads` threads, each its own queue, committing draws with `in_flight` in flight: command
|
|
/// buffers a second over the run, and the GPU's idle share at switches.
|
|
/// The queues of `threads` threads: each its own where `one` is false; else all on the first's
|
|
/// `VkQueue`, made until the round robin comes back to it, each lent once so it never moves.
|
|
fn rate_queues(kit: &Kit, threads: usize, one: bool) -> Vec<CommandQueue> {
|
|
if !one {
|
|
return (0..threads).map(|_| kit.queue(false)).collect();
|
|
}
|
|
// SAFETY: the closures keep the handle only to compare it; nothing is submitted.
|
|
let raw = |queue: &CommandQueue| unsafe { queue.with_vk_queue(|_, raw, _| raw) };
|
|
let first = kit.queue(false);
|
|
let home = raw(&first);
|
|
let mut queues = vec![first];
|
|
let mut others = Vec::new();
|
|
while queues.len() < threads {
|
|
let queue = kit.queue(false);
|
|
match raw(&queue) == home {
|
|
true => queues.push(queue),
|
|
false => others.push(queue),
|
|
}
|
|
}
|
|
queues
|
|
}
|
|
|
|
/// Per-thread CPU time of this process by thread name, in clock ticks (Linux; empty elsewhere).
|
|
fn thread_ticks() -> Vec<(u64, String, u64)> {
|
|
let Ok(tasks) = std::fs::read_dir("/proc/self/task") else {
|
|
return Vec::new();
|
|
};
|
|
let mut ticks = Vec::new();
|
|
for task in tasks.flatten() {
|
|
let Ok(stat) = std::fs::read_to_string(task.path().join("stat")) else {
|
|
continue;
|
|
};
|
|
let (Some(open), Some(close)) = (stat.find('('), stat.rfind(')')) else {
|
|
continue;
|
|
};
|
|
let name = stat[open + 1..close].to_owned();
|
|
let fields: Vec<&str> = stat[close + 2..].split(' ').collect();
|
|
let (Some(user), Some(system)) = (fields.get(11), fields.get(12)) else {
|
|
continue;
|
|
};
|
|
let tid = task.file_name().to_string_lossy().parse().unwrap_or(0);
|
|
ticks.push((
|
|
tid,
|
|
name,
|
|
user.parse::<u64>().unwrap_or(0) + system.parse::<u64>().unwrap_or(0),
|
|
));
|
|
}
|
|
ticks
|
|
}
|
|
|
|
/// The busiest threads between `before` and `after`, as a share of `seconds` each.
|
|
fn busiest(before: &[(u64, String, u64)], after: &[(u64, String, u64)], seconds: f64) -> String {
|
|
let mut used: Vec<(f64, String)> = after
|
|
.iter()
|
|
.map(|(tid, name, ticks)| {
|
|
let was = before
|
|
.iter()
|
|
.find(|(t, _, _)| t == tid)
|
|
.map_or(0, |(_, _, t)| *t);
|
|
((ticks - was) as f64 / 100.0 / seconds, name.clone())
|
|
})
|
|
.filter(|(share, _)| *share > 0.02)
|
|
.collect();
|
|
used.sort_by(|a, b| b.0.total_cmp(&a.0));
|
|
used.iter()
|
|
.take(8)
|
|
.map(|(share, name)| format!("{name} {:.0}%", share * 100.0))
|
|
.collect::<Vec<_>>()
|
|
.join(", ")
|
|
}
|
|
|
|
/// The p50 and p99 of `samples`, in microseconds.
|
|
fn quantiles(samples: &mut [f64]) -> String {
|
|
if samples.is_empty() {
|
|
return "none".to_owned();
|
|
}
|
|
samples.sort_by(f64::total_cmp);
|
|
let at = |p: f64| samples[((samples.len() as f64 * p) as usize).min(samples.len() - 1)] * 1e6;
|
|
format!("p50 {:.1} p99 {:.1} us", at(0.5), at(0.99))
|
|
}
|
|
|
|
fn rate(kit: &Arc<Kit>, options: &Options, threads: usize, in_flight: usize) {
|
|
let stop = Arc::new(AtomicBool::new(false));
|
|
let times = Arc::new(Mutex::new(Vec::new()));
|
|
// Each command buffer's GPU end to its completed handler, and its commit to that handler.
|
|
let latencies = Arc::new(Mutex::new((Vec::new(), Vec::new())));
|
|
let queues = rate_queues(kit, threads, options.one_vk_queue);
|
|
let counts = kit.device.work_counts();
|
|
let cpu = thread_ticks();
|
|
let began = Instant::now();
|
|
let handles: Vec<_> = (queues.into_iter().enumerate())
|
|
.map(|(thread, queue)| {
|
|
let kit = Arc::clone(kit);
|
|
let stop = Arc::clone(&stop);
|
|
let times = Arc::clone(×);
|
|
let latencies = Arc::clone(&latencies);
|
|
std::thread::spawn(move || {
|
|
let target = kit.target();
|
|
let mut flying = std::collections::VecDeque::new();
|
|
let mut made = 0u64;
|
|
// Each change of the queue's `VkQueue` or long mark: at which command buffer,
|
|
// how far into the run.
|
|
let mut seen = Vec::new();
|
|
let started = Instant::now();
|
|
while !stop.load(Ordering::Relaxed) {
|
|
let command_buffer = kit.draw(&queue, &target);
|
|
let now = queue.vk_queue_sharing();
|
|
if seen.last().is_none_or(|&(_, _, last)| last != now) {
|
|
seen.push((made, started.elapsed().as_micros(), now));
|
|
}
|
|
let times = Arc::clone(×);
|
|
let latencies = Arc::clone(&latencies);
|
|
let committed = Arc::new(Mutex::new(0.0));
|
|
let at = Arc::clone(&committed);
|
|
command_buffer
|
|
.add_completed_handler(Box::new(move |done: &CommandBuffer| {
|
|
let now = metal2vulkan_runtime::host_time();
|
|
if let (Ok(start), Ok(end)) =
|
|
(done.gpu_start_time(), done.gpu_end_time())
|
|
{
|
|
times.lock().unwrap().push((thread, start, end));
|
|
let mut latencies = latencies.lock().unwrap();
|
|
latencies.0.push(now - end);
|
|
latencies.1.push(now - *at.lock().unwrap());
|
|
}
|
|
}))
|
|
.unwrap();
|
|
*committed.lock().unwrap() = metal2vulkan_runtime::host_time();
|
|
command_buffer.commit().unwrap();
|
|
flying.push_back(command_buffer);
|
|
made += 1;
|
|
while flying.len() >= in_flight {
|
|
let done: CommandBuffer = flying.pop_front().unwrap();
|
|
done.wait_until_completed().unwrap();
|
|
}
|
|
}
|
|
for done in flying {
|
|
done.wait_until_completed().unwrap();
|
|
}
|
|
(made, seen)
|
|
})
|
|
})
|
|
.collect();
|
|
std::thread::sleep(Duration::from_secs_f64(options.seconds));
|
|
stop.store(true, Ordering::Relaxed);
|
|
let each: Vec<(u64, Seen)> = handles.into_iter().map(|h| h.join().unwrap()).collect();
|
|
let made: u64 = each.iter().map(|(made, _)| made).sum();
|
|
let elapsed = began.elapsed().as_secs_f64();
|
|
let cpu_after = thread_ticks();
|
|
std::thread::sleep(Duration::from_millis(100));
|
|
let after = kit.device.work_counts();
|
|
let (idle, switches) = switch_idle(&mut times.lock().unwrap());
|
|
let mut latencies = latencies.lock().unwrap();
|
|
let per = |a: u64, b: u64| (a - b) as f64 / made.max(1) as f64;
|
|
println!(
|
|
"rates (policy {:?}{}): {threads} threads, {in_flight} in flight: {:.1}k command buffers/s; GPU idle at {switches} switches {:.1}%; {}",
|
|
options.policy,
|
|
if options.one_vk_queue { ", one VkQueue" } else { "" },
|
|
made as f64 / elapsed / 1e3,
|
|
idle * 100.0,
|
|
kit.counts()
|
|
);
|
|
println!(
|
|
" GPU end to handler {}; commit to handler {}",
|
|
quantiles(&mut latencies.0),
|
|
quantiles(&mut latencies.1)
|
|
);
|
|
println!(
|
|
" per command buffer: submissions {:.2}, submit infos {:.2}, VkCommandBuffers {:.2}, scheduler passes {:.2} (by waiters {:.2}, their batches {:.2}), scheduler sleeps {:.2} (woken by commits {:.2}, completions {:.2}), timeline waits {:.2}, fence waits {:.2}, waiter parks {:.2} (timed out {:.2}), waiter wakes {:.2}, handler-thread wakes {:.2}, command-buffer wakes {:.2}, completed where seen {:.2}, turns taken {:.2}, submit {:.1} us",
|
|
per(after.queue_submissions, counts.queue_submissions),
|
|
per(after.submit_infos, counts.submit_infos),
|
|
per(after.command_buffers_submitted, counts.command_buffers_submitted),
|
|
per(after.scheduler_passes, counts.scheduler_passes),
|
|
per(after.scheduler_passes_by_waiters, counts.scheduler_passes_by_waiters),
|
|
per(after.batches_submitted_by_waiters, counts.batches_submitted_by_waiters),
|
|
per(after.scheduler_sleeps, counts.scheduler_sleeps),
|
|
per(after.scheduler_wakes_commit, counts.scheduler_wakes_commit),
|
|
per(after.scheduler_wakes_completion, counts.scheduler_wakes_completion),
|
|
per(after.timeline_waits, counts.timeline_waits),
|
|
per(after.fence_waits, counts.fence_waits),
|
|
per(after.completion_waiter_parks, counts.completion_waiter_parks),
|
|
per(after.completion_waiter_parks_timed_out, counts.completion_waiter_parks_timed_out),
|
|
per(after.wakes_completion_waiters, counts.wakes_completion_waiters),
|
|
per(after.wakes_handler_threads, counts.wakes_handler_threads),
|
|
per(after.wakes_command_buffer_waiters, counts.wakes_command_buffer_waiters),
|
|
per(after.completed_where_seen, counts.completed_where_seen),
|
|
per(after.completion_turns_where_taken, counts.completion_turns_where_taken),
|
|
per(after.batch_submit_nanos, counts.batch_submit_nanos) / 1e3,
|
|
);
|
|
println!(" threads' CPU: {}", busiest(&cpu, &cpu_after, elapsed));
|
|
for (thread, (made, seen)) in each.iter().enumerate() {
|
|
let changes: Vec<String> = seen
|
|
.iter()
|
|
.map(|(at, us, (vk_queue, long))| {
|
|
format!(
|
|
"#{at} {us}us VkQueue {vk_queue}{}",
|
|
if *long { " long" } else { "" }
|
|
)
|
|
})
|
|
.collect();
|
|
println!(
|
|
" thread {thread}: {made} command buffers; {}",
|
|
changes.join(", ")
|
|
);
|
|
}
|
|
}
|
|
|
|
fn idle_queues(kit: &Arc<Kit>, options: &Options, count: usize) {
|
|
let queues: Vec<CommandQueue> = (0..count).map(|_| kit.queue(false)).collect();
|
|
let target = kit.target();
|
|
let mut flying = std::collections::VecDeque::new();
|
|
let mut made = 0u64;
|
|
let mut making = Duration::ZERO;
|
|
let began = Instant::now();
|
|
while began.elapsed().as_secs_f64() < options.seconds {
|
|
let queue = &queues[made as usize % count];
|
|
let start = Instant::now();
|
|
let command_buffer = queue.command_buffer().unwrap();
|
|
making += start.elapsed();
|
|
let command_buffer = kit.draw_into(command_buffer, &target);
|
|
command_buffer.commit().unwrap();
|
|
flying.push_back(command_buffer);
|
|
made += 1;
|
|
while flying.len() >= 3 {
|
|
let done: CommandBuffer = flying.pop_front().unwrap();
|
|
done.wait_until_completed().unwrap();
|
|
}
|
|
}
|
|
for done in flying {
|
|
done.wait_until_completed().unwrap();
|
|
}
|
|
let elapsed = began.elapsed().as_secs_f64();
|
|
println!(
|
|
"idle-queues (policy {:?}): {count} queues in turn: {:.1}k command buffers/s, command_buffer {:.0} ns each; {}",
|
|
options.policy,
|
|
made as f64 / elapsed / 1e3,
|
|
making.as_nanos() as f64 / made as f64,
|
|
kit.counts()
|
|
);
|
|
}
|
|
|
|
fn main() {
|
|
let options = options();
|
|
if options.mode == "check" {
|
|
let device = device(&options);
|
|
let settled = device.settle_queue_switch_check(Duration::from_secs(10));
|
|
println!(
|
|
"device: {}; settled {settled}: {}",
|
|
device.name(),
|
|
device.queue_switch_check_report()
|
|
);
|
|
return;
|
|
}
|
|
let kit = Arc::new(Kit::new(&options));
|
|
match options.mode.as_str() {
|
|
"isolation" => beside(&kit, &options, Duration::ZERO, 1, 1),
|
|
"pipelined" => beside(&kit, &options, Duration::ZERO, 1, 2),
|
|
"trap" => beside(&kit, &options, Duration::from_millis(500), 10, 1),
|
|
"alternating" => beside(&kit, &options, Duration::from_millis(2), 1, 1),
|
|
"slow-alternating" => beside(&kit, &options, Duration::from_millis(1500), 1, 1),
|
|
"frames" => frames(&kit, &options),
|
|
"pairs" => pairs(&kit, &options),
|
|
"interleave" => interleave(&kit, &options),
|
|
"rates" if options.threads > 0 => {
|
|
rate(&kit, &options, options.threads, options.in_flight);
|
|
}
|
|
"rates" => {
|
|
for in_flight in [3, 1] {
|
|
for threads in [2, 8] {
|
|
rate(&kit, &options, threads, in_flight);
|
|
}
|
|
}
|
|
}
|
|
"idle-queues" => {
|
|
idle_queues(&kit, &options, 16);
|
|
idle_queues(&kit, &options, 2048);
|
|
}
|
|
"churn" => churn(&kit, &options),
|
|
other => panic!("no mode {other}"),
|
|
}
|
|
}
|