Files
metal2vulkan/crates/metal2vulkan-runtime/src/tests/drag_submit_bench.rs
T
Anees Iqbal 96e1041dbf RUNTIME 2538: submission checks: an answer that load could have made stands only for now
What: a merge or import tax answer now stands as far as it earned
(`Stands`). A merge `Holds` clear of the load and repeated, an import tax
`Holds` past its margin and repeated, and an `Unchecked` of a refused call
or a device with no pointer import are recorded. A `Fails` clear of the
load whose every try ran quiet stands for the device's life and is never
recorded. Every other answer stands for now: the checks' thread keeps its
command buffers and fence and checks it again 1, 2, 4 ... 64 s after the
first answers, then each minute, until none stands for now. A check again
replaces the verdict (tries that all split keep it), so a device whose
first merge answer was a loaded `Fails` groups once a check again answers
`Holds`. A record's `Fails` is a hint and no answer: a device that reads
one checks that check again at its making. Before each round and each
submission of a phase the checks stand by, at most 20 ms, until the
device's work is quiet (`DeviceShared::work_quiet_for`, shared with the
checks' thread through `WorkShare`).

The stop rule's thresholds (`Load::ran_quiet`): every stand-by found the
device's work quiet, no round or submission took past QUIET_ROUND_MOST
(2 ms, from its first call to its fence's reset), and all of them no more
than QUIET_TRY_MOST (30 ms). A merge `Fails` must also be clear
(MERGE_FAILS_CLEAR, 0.75 of the calls apart) and repeated
(MERGE_REPEATS); an import tax `Fails` must agree in its halves with a
held import adding at most half its bound (TAX_FAILS_CLEAR).

Why: on the Radeon 780M beside other jobs (m3-max-39740, load average
18-24 on 16 threads), two of four devices answered merge `Fails` at 0.72
and 0.745, both forms lifted by about the same 58-80 us, short of the
record but held for the device's life, so those processes grouped no
batch at all. At 0.75 and up with its repeats, the same load would have
written a `Fails` record that every later device of that release read
without checking. Threads of the process's own spinning or touching fresh
pages (m3-max-39786, 2x the host's threads) lifted every call alike and
left all eight devices at `Holds` (0.23-0.64), so the device test injects
the observed shape instead (`lift_next_merge_tries`: 70 us on the first
call of every round, its stand-bys not quiet).

Costs: a driver that does not merge or whose imports cost nothing pays
that check at every device's making, as before any record, and once more
at least where no try of its first answer ran quiet. An answer for now
costs a check again at each step of the schedule. The import tax is
included: a loaded tax `Fails` placed the device's long buffers in fresh
pages for its life on a driver whose imports do cost (39740's
b202c-bench run: 148.6 against 121.5 us, short of its margin).

CHECK_VERSION 9 (a record may hold no answer of a check, and a `Fails` in
one is no answer). Tests: unit tests of what each answer earns, the
schedule, the quiet bounds and the record; device tests that a recorded
`Fails` is checked again and that a lifted merge answer is checked again
and comes to the answer of a device beside it, with no `Fails` recorded.
in_place_check takes its answers through `answer_next_device`, since a
record can no longer hold a `Fails` as an answer. Forced verdicts
(`SubmitChecks::force`) stand for the device's life.

With it: the drag bench reports the submission checks' answers before and after its measurements
(test only); a lifted try's count goes down by AtomicU32::try_update (fetch_update's new name).

Verified (launchreg lane, tip 942672e8cb = these three commits on development 01415e33d6):
m3-max-39884 (msi): clippy -D warnings plain, m2v_cpu_icd and m2v_gpu 0; lavapipe submit_check,
in_place_check, teardown_threads, relay_facts, fresh_pages, padding_unobserved, host_ram_dma_buf,
dma_buf_parked_images, exported_textures, icb_records_policy, pointer_residency, fast_paths,
queue_quiet and view_check 114/114; view_check_device's stand-by test 3/3. The control (the fix
without the re-check of loaded and recorded answers) fails the new device tests as meant: lavapipe
(m3-max-39818) 2 of 2, RX 550 (39863) 2 of 2, Radeon 780M (39885) the recorded-Fails test; the fix
passes 26/26 on the RX 550 and the 780M. On the 780M (39907, the fix on an earlier base) a device
made under host load answered merge Pending (one try split), then "Holds, recorded (14.4 us in one
call of 4 against 39.4 us apart)" after its re-checks, and grouped; every later device answered
Holds in 3 tries.
2026-11-17 02:01:04 -08:00

329 lines
16 KiB
Rust

//! What a guest's window drag costs its committing thread where the device's calls of commands
//! are slow for the imports it lists, and some of the calls hold no command buffer
//! (`device/submit_calls.rs`). A guest's RAM of no-copy buffers over a sealed `memfd`'s pages,
//! each its own dma-buf (udmabuf) of [`LISTED_BYTES`], as a guest's RAM chunks are on a host of
//! unified memory, each bound once by a dispatch, so kept imported for good and listed in every
//! kernel submission of commands (`host_residency.rs`): [`LISTED`] of them, then as many more.
//! With each, after [`WARM`] frames, [`FRAMES`] frames of [`PARTS`] command buffers committed with
//! `commit_and_submit` one after another on one queue, as reims commits the parts it cuts from a
//! compositor's command buffer: each a dispatch, but the last, which encodes nothing but an
//! event's signal, so its batch holds no command buffer (the empty last part reims's decoder cut
//! from most of a window drag's buffers on the Radeon 780M; one that signals nothing makes no
//! call); the frame's parts are waited for before the next frame. Each with the driver's calls,
//! then with each call of commands made [`MIXED_PAUSE`] longer but one in each [`CALLS`]
//! (`Device::slow_submit_calls`, `Device::fast_submit_call_every`), a short call among slow ones,
//! as one guest queue's smaller batches come among another's, then but one in each three, as the
//! Iris Xe's compositor parts straddled the bound (`device/submit_calls.rs`, "Measured, not
//! named"). Each of those three times: as the queue's cost leaves the batches, then each at its
//! commit, then each left to the scheduler's thread (`Device::leave_calls_from`), so that what a
//! committing thread saves by leaving a batch stands beside what its GPU start loses.
//!
//! Printed (`DRAG-SUBMIT`), each: the parts submitted at their commit and those left for slow
//! calls (`WorkCounts::batches_submitted_at_commit`,
//! `WorkCounts::scheduler_at_commit_left_slow_calls`), the commit calls' time a part (the parts
//! with work's median, 90th percentile and mean; the empty ones' mean; all of them a frame), the
//! time from each part with work's commit call to its GPU start (`CommandBuffer::gpu_start_time`;
//! median and 90th percentile), the batch calls' mean (`WorkCounts::batch_submit_nanos` a
//! `queue_submissions`, the `VkCommandBuffer`s a call) and the queue's submission cost at the
//! end. And (`DRAG-PATH`) where the time from a commit call to its GPU start went, for each part
//! with work (medians and 90th percentiles): to the start of its submission
//! (`CommandBuffer::kernel_start_time`: the committing thread's, or the scheduler's thread's once
//! woken and at it), from there to the return of the call that carried it
//! (`CommandBuffer::submit_returned_time`, the device noting queue times: its planning, any
//! hold in a group, and the call), and from there to its GPU start; and why the commits' passes
//! left batches (the `WorkCounts::scheduler_at_commit_left_*` counts) and the groups held past
//! a pass for two submissions in flight (`WorkCounts::groups_kept_pending`); the device's
//! submission checks' report (`Device::submit_check_report`) heads them and ends them, as the
//! merge check decides whether its batches group. And (`DRAG-FRAME`)
//! each frame's whole (medians and 90th percentiles): from its first commit call to the GPU start
//! and the GPU end of its last part with work, and its commit calls' time, the committing
//! thread's for the frame. A part's time from its own commit call leaves out the commit calls
//! before it, which hold back its commit where the committing thread submits each part itself.
//!
//! Ignored: run it in release under `--cfg m2v_gpu` where `/dev/udmabuf` opens,
//! `cargo test --release -p metal2vulkan-runtime --lib drag_submit_bench -- --ignored --nocapture`.
use super::blit_holds::{Scratch, WAIT};
use super::compute_parked::Kernel;
use super::host_residency::{udmabuf_openable, Ram};
use super::submit_tax_bench::bind_all;
use crate::device::submit_calls::CALLS;
use crate::gpu_time;
use crate::{Buffer, CommandBufferStatus, EventRef, HostMemory, ResourceOptions};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
/// The imports of the first measurement, and of each more.
const LISTED: usize = 140;
/// Each import's bytes: a guest RAM chunk's.
const LISTED_BYTES: usize = 2 << 20;
/// The parts of a frame, the last empty.
const PARTS: usize = 13;
/// The frames before each measurement.
const WARM: usize = 20;
/// The frames measured.
const FRAMES: usize = 200;
/// How much longer each call of commands but one in each [`CALLS`], then one in each three, takes
/// in the second and third measurements of each.
const MIXED_PAUSE: Duration = Duration::from_micros(100);
/// What a measurement's frames noted, in microseconds, of its parts: each commit call's (those with
/// work, the empty ones'); and of those with work, from the commit call to the GPU start, to the
/// start of the part's submission, from there to the return of the call that carried it, and
/// from there to the GPU start. And of each frame: from its first commit call to its last part
/// with work's GPU start and GPU end, and its commit calls' time.
#[derive(Default)]
struct Times {
work: Vec<f64>,
empty: Vec<f64>,
started: Vec<f64>,
picked: Vec<f64>,
called: Vec<f64>,
ran: Vec<f64>,
frame_started: Vec<f64>,
frame_ended: Vec<f64>,
frame_commits: Vec<f64>,
}
/// The `at` quantile of `values`, sorted.
fn quantile(values: &[f64], at: f64) -> f64 {
let mut sorted = values.to_vec();
sorted.sort_by(f64::total_cmp);
sorted
.get(((sorted.len() as f64 * at) as usize).min(sorted.len().saturating_sub(1)))
.copied()
.unwrap_or(0.0)
}
/// The mean of `values`.
fn mean(values: &[f64]) -> f64 {
values.iter().sum::<f64>() / values.len().max(1) as f64
}
#[test]
#[ignore = "a release benchmark: run with --ignored --nocapture"]
fn drag_submit_bench() {
if !udmabuf_openable("drag_submit_bench") {
return;
}
let scratch = Scratch::new("drag-submit");
let kernel = Kernel::new(&scratch);
kernel.warm();
let device = &kernel.device;
// Each command buffer's submission's return (`CommandBuffer::submit_returned_time`).
device.shared().timing.note_queue_times();
println!(
"DRAG-PATH on {}: completion {:?}, submissions merge {}; its submission checks: {}",
device.name(),
device.compile_device().policy().completion,
device.submissions_merge(),
device.submit_check_report(),
);
let len = 2 * LISTED * LISTED_BYTES;
let ram = Ram::new(len, 0, len);
let calls = Arc::new(AtomicUsize::new(0));
let output = kernel.output(PARTS as u64);
let queue = device.new_command_queue().unwrap();
let event = device.new_event().unwrap();
let signalled = AtomicU64::new(0);
let mut listed: Vec<Buffer> = Vec::new();
for _ in 0..2 {
let made: Vec<Buffer> = (listed.len()..listed.len() + LISTED)
.map(|at| {
// SAFETY: the view outlives every buffer (dropped before `ram`), and the source
// makes dma-bufs of its pages.
unsafe {
device.new_buffer_with_bytes_no_copy(
HostMemory::with_dma_buf(
ram.pointer(at * LISTED_BYTES),
ram.source(&calls),
),
LISTED_BYTES as u64,
ResourceOptions::default(),
None,
)
}
.unwrap()
})
.collect();
bind_all(&kernel, &made);
listed.extend(made);
let frame = |times: Option<&mut Times>| {
let mut parts = Vec::with_capacity(PARTS);
let mut took = Vec::with_capacity(PARTS);
let mut committed = Vec::with_capacity(PARTS);
for part in 0..PARTS {
let command_buffer = queue.command_buffer().unwrap();
if part + 1 < PARTS {
kernel.dispatch(&command_buffer, (&output, part as u64 * 256));
} else {
let value = signalled.fetch_add(1, Ordering::Relaxed) + 1;
command_buffer
.encode_signal_event(EventRef::from(&event), value)
.unwrap();
}
committed.push(gpu_time::host_nanos());
let called = Instant::now();
command_buffer.commit_and_submit().unwrap();
took.push(called.elapsed().as_nanos() as f64 / 1e3);
parts.push(command_buffer);
}
for command_buffer in &parts {
command_buffer.wait_until_completed_for(WAIT).unwrap();
assert_eq!(command_buffer.status(), CommandBufferStatus::Completed);
}
if let Some(times) = times {
times.work.extend_from_slice(&took[..PARTS - 1]);
times.empty.push(took[PARTS - 1]);
times.frame_commits.push(took.iter().sum());
let first = gpu_time::seconds(committed[0]);
let last = &parts[PARTS - 2];
if let (Ok(start), Ok(end)) = (last.gpu_start_time(), last.gpu_end_time()) {
times.frame_started.push((start - first) * 1e6);
times.frame_ended.push((end - first) * 1e6);
}
for (command_buffer, &at) in parts.iter().zip(&committed).take(PARTS - 1) {
let at = gpu_time::seconds(at);
let Ok(start) = command_buffer.gpu_start_time() else {
continue;
};
times.started.push((start - at) * 1e6);
let (kernel, returned) = (
command_buffer.kernel_start_time(),
command_buffer.submit_returned_time(),
);
if kernel > 0.0 && returned > 0.0 {
times.picked.push((kernel - at) * 1e6);
times.called.push((returned - kernel) * 1e6);
times.ran.push((start - returned) * 1e6);
}
}
}
};
for ((calls_were, pause, every), (placed, bound)) in [
("the driver's", None, None),
(
"100 us longer but one in each 15",
Some(MIXED_PAUSE),
Some(CALLS as u64),
),
(
"100 us longer but one in each 3",
Some(MIXED_PAUSE),
Some(3),
),
]
.into_iter()
.flat_map(|leg| {
[
("as their cost leaves them", None),
("each at its commit", Some(u64::MAX)),
("each left to the scheduler's thread", Some(1)),
]
.map(|placed| (leg, placed))
}) {
device.slow_submit_calls(pause);
device.fast_submit_call_every(every);
device.leave_calls_from(bound);
for _ in 0..WARM {
frame(None);
}
let mut times = Times::default();
let before = device.work_counts();
let started = Instant::now();
for _ in 0..FRAMES {
frame(Some(&mut times));
}
let Times {
work,
empty,
started: gpu_started,
picked,
called,
ran,
frame_started,
frame_ended,
frame_commits,
} = times;
let elapsed = started.elapsed().as_secs_f64();
let after = device.work_counts();
let moved = |count: fn(&crate::WorkCounts) -> u64| count(&after) - count(&before);
let parts = (FRAMES * PARTS) as f64;
let at_commit = moved(|c| c.batches_submitted_at_commit);
let slow = moved(|c| c.scheduler_at_commit_left_slow_calls);
let submissions = moved(|c| c.queue_submissions).max(1) as f64;
let cost = device.shared().queues[queue.vk_queue_sharing().0]
.calls
.cost();
println!(
"DRAG-SUBMIT listed {} (live dma-buf memories {}), calls of commands \
{calls_were}, batches {placed}, on {}: {} parts ({FRAMES} frames of {PARTS}, the \
last empty) in {elapsed:.2} s; at commit {at_commit} ({:.1}%), left for slow \
calls {slow} ({:.1}%); commit call a part, us: with work p50 {:.1} p90 {:.1} \
mean {:.1}, empty mean {:.1}, all a frame {:.0}; commit to GPU start, with \
work, us: p50 {:.1} p90 {:.1} ({} read); batch calls {} of mean {:.1} us, \
{:.1} VkCommandBuffers a call; the queue's submission cost {:.1} us",
listed.len(),
after.dma_buf_memories_live,
device.name(),
parts,
100.0 * at_commit as f64 / parts,
100.0 * slow as f64 / parts,
quantile(&work, 0.5),
quantile(&work, 0.9),
mean(&work),
mean(&empty),
(work.iter().sum::<f64>() + empty.iter().sum::<f64>()) / FRAMES as f64,
quantile(&gpu_started, 0.5),
quantile(&gpu_started, 0.9),
gpu_started.len(),
submissions,
moved(|c| c.batch_submit_nanos) as f64 / submissions / 1e3,
moved(|c| c.command_buffers_submitted) as f64 / submissions,
cost as f64 / 1e3,
);
let spread = |values: &[f64]| (quantile(values, 0.5), quantile(values, 0.9));
println!(
"DRAG-PATH listed {}, calls of commands {calls_were}, batches {placed}: commit to \
submission start p50/p90 {:.1?} us, to the call's return {:.1?}, to GPU start \
{:.1?} ({} read); passes left: under way {}, taken in {}, behind its queue {}, \
turns {}, slow calls {slow}, other {}; groups' calls {}, of {} batches, kept \
for two in flight {}",
listed.len(),
spread(&picked),
spread(&called),
spread(&ran),
picked.len(),
moved(|c| c.scheduler_at_commit_left_passing),
moved(|c| c.scheduler_at_commit_left_taken_in),
moved(|c| c.scheduler_at_commit_left_behind_its_queue),
moved(|c| c.scheduler_at_commit_left_turns),
moved(|c| c.scheduler_at_commit_left_other),
moved(|c| c.grouped_submissions),
moved(|c| c.batches_grouped),
moved(|c| c.groups_kept_pending),
);
println!(
"DRAG-FRAME listed {}, calls of commands {calls_were}, batches {placed}: a frame's \
first commit call to its last part's GPU start p50/p90 {:.1?} us, to its GPU \
end {:.1?} ({} read); its commit calls {:.1?} us",
listed.len(),
spread(&frame_started),
spread(&frame_ended),
frame_started.len(),
spread(&frame_commits),
);
}
device.slow_submit_calls(None);
device.fast_submit_call_every(None);
device.leave_calls_from(None);
}
println!(
"DRAG-PATH after the measurements: submissions merge {}; its submission checks: {}",
device.submissions_merge(),
device.submit_check_report(),
);
drop(listed);
}