RUNTIME 2231 split the guest's encoder at each synchronize of a Managed buffer into its own deferred segment (Deferred::SyncOut): every command buffer holding one paid an extra continuation VkCommandBuffer and a segment VkCommandBuffer, its batch could no longer fold its end (an extra VkSubmitInfo), and the copy was decided at submission. On the consumer's 2080 Geekbench walk that showed as ~1 extra no-dispatch command buffer per synchronize. Now the copy is decided at encode, from the kept writes of other command buffers plus this command buffer's own writes before the synchronize, and recorded in place with only the written regions; an unwritten synchronize records nothing. The copy is whole where an earlier segment is unknown, the command buffer writes memory no use names, or the buffer was handed out. At submission, settle_syncs_out adds a top-up command buffer just before the part holding the copy, covering only writes from other command buffers noted after the encode, between two all-stage barriers (WorkCounts::managed_synchronizes_topped_up, new). The SyncOut segment is gone (deferred.rs is back to its041038a914form); may_fold folds again. Verified (mansync, 4e3e22f4d6 on 8a81350e13; 2080 release, one run each, 26257), per command buffer,041038a914/8a81350e13/ fix: - all (40 written): VkSubmitInfos 1/2/1, VkCBs 2/4.1/2, GPU 95.61/95.66/95.62 ms - mixed (30 written, 10 CPU did_modify_range): VkSubmitInfos 2/3/2, VkCBs 3/5/3, copied 176/132/132 MB, GPU 95.4/71.6/71.6 ms - none (40 unwritten): VkCBs 1/3/1, GPU 95.05/0.014/0.007 ms - small (import shape: 1 MiB fill + 2 unwritten 335,872 B no-copy synchronizes): VkCBs 2/4/2, commit-to-completion 0.519/0.111/0.116 ms (keeps ~4.5x) vkQueueSubmit calls equal in every row; topped_up 0 in every fix row. Tests on the 2080 (26258): managed_copy_device, detached_contents, kept_padding, device_memory_overflow 41/41, with two new steps (a write committed before but encoded after the synchronize: topped up 1 and every byte read back; a write committed after: nothing copied, the next synchronize copies exactly that range). clippy plain 26228, --cfg m2v_gpu 26227, --cfg m2v_cpu_icd 26229 all 0; rustdoc 26230 = 0. RUNTIME_VERSION 2235 -> 2236.
343 lines
13 KiB
Rust
343 lines
13 KiB
Rust
//! Not a consumer's example: what `synchronize_resource` of `Managed` buffers over the caller's
|
|
//! pages costs a command buffer on a device without unified memory, where each such buffer is two
|
|
//! copies (the caller's pages and the GPU's own) and a synchronize copies the GPU's copy out to the
|
|
//! pages. Two shapes a guest workload showed:
|
|
//!
|
|
//! - **Synchronizes of 40 buffers a command buffer**, 4,399,104 bytes each (about 176 MB a command
|
|
//! buffer, 800 synchronizes over 20 command buffers), after a blit encoder that fills `written`
|
|
//! of them whole; the rest the CPU wrote and named with `did_modify_range` before the command
|
|
//! buffer (rows `all`, `mixed`), or nobody wrote (row `none`).
|
|
//! - **Synchronizes of two 335,872-byte buffers nobody wrote**, after a 1 MiB fill of a `Private`
|
|
//! buffer (row `small`), against the fill alone (row `fill`).
|
|
//!
|
|
//! ```sh
|
|
//! cargo run --release -p metal2vulkan-runtime --example managed_sync_out
|
|
//! ```
|
|
//!
|
|
//! After each command buffer the CPU checks a sample of every buffer's pages. It prints one line
|
|
//! a row, each value a command buffer's (medians of the times, means of the counts over the timed
|
|
//! ones):
|
|
//!
|
|
//! ```text
|
|
//! ROW <row> gpu_ms <GPU time> wall_ms <commit to completion> copied_mb <MB copied out>
|
|
//! subs <vkQueueSubmit calls> infos <VkSubmitInfos> vkcbs <VkCommandBuffers begun>
|
|
//! (encoders, continuations, batch work, batch barriers, internal) fillq <fill-queue
|
|
//! submissions> commit_us <commit's own time> topped_up <managed_synchronizes_topped_up> <device>
|
|
//! ```
|
|
//!
|
|
//! `topped_up` reads `-` where the device's counts have no such field.
|
|
|
|
use metal2vulkan_runtime::{
|
|
Buffer, CommandBufferStatus, CommandQueue, CpuCacheMode, Device, HazardTrackingMode,
|
|
HostMemory, ResourceOptions, ResourceRef, StorageMode, WorkCounts,
|
|
};
|
|
use std::ptr::NonNull;
|
|
use std::time::{Duration, Instant};
|
|
|
|
/// The example's result: every runtime refusal is a typed `metal2vulkan_runtime::Error`.
|
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
|
|
|
/// The longest the example waits for a command buffer.
|
|
const WAIT: Duration = Duration::from_secs(60);
|
|
|
|
/// The bytes of each of the 40 buffers: whole pages of any host.
|
|
const LARGE: u64 = 1074 * 4096;
|
|
/// The bytes of each of the two small buffers.
|
|
const SMALL: u64 = 82 * 4096;
|
|
/// The bytes of the small rows' fill.
|
|
const FILL: u64 = 1 << 20;
|
|
/// The command buffers run before the timed ones.
|
|
const WARM: usize = 3;
|
|
/// The first bytes of every buffer's pages.
|
|
const FIRST: u8 = 0x11;
|
|
|
|
/// Anonymous private pages of the example's own, unmapped when dropped, after the buffer over them.
|
|
struct Pages(NonNull<u8>, u64);
|
|
|
|
impl Pages {
|
|
fn new(bytes: u64) -> Result<Pages> {
|
|
// SAFETY: a new anonymous mapping, which nothing else uses.
|
|
let pointer = unsafe {
|
|
libc::mmap(
|
|
std::ptr::null_mut(),
|
|
bytes as usize,
|
|
libc::PROT_READ | libc::PROT_WRITE,
|
|
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
|
|
-1,
|
|
0,
|
|
)
|
|
};
|
|
if pointer == libc::MAP_FAILED {
|
|
return Err("mmap failed".into());
|
|
}
|
|
let pointer = NonNull::new(pointer.cast::<u8>()).ok_or("mmap gave null")?;
|
|
let pages = Pages(pointer, bytes);
|
|
pages.write(FIRST);
|
|
Ok(pages)
|
|
}
|
|
|
|
/// Write `value` over every byte, as the CPU.
|
|
fn write(&self, value: u8) {
|
|
// SAFETY: the mapping's bytes, which no command buffer uses while the CPU writes them.
|
|
unsafe { std::ptr::write_bytes(self.0.as_ptr(), value, self.1 as usize) };
|
|
}
|
|
|
|
/// The byte at `at`, as the CPU reads it.
|
|
fn byte(&self, at: u64) -> u8 {
|
|
// SAFETY: inside the mapping, which no command buffer writes while the CPU reads it.
|
|
unsafe { self.0.as_ptr().add(at as usize).read_volatile() }
|
|
}
|
|
|
|
/// Check a sample of its bytes hold `want`.
|
|
fn check(&self, want: u8, what: &str) -> Result<()> {
|
|
for at in (0..self.1).step_by(65_537).chain([self.1 - 1]) {
|
|
let byte = self.byte(at);
|
|
if byte != want {
|
|
return Err(format!("{what}: byte {at} is {byte:#x}, not {want:#x}").into());
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl Drop for Pages {
|
|
fn drop(&mut self) {
|
|
// SAFETY: the mapping `new` made; every buffer over it went before it.
|
|
unsafe { libc::munmap(self.0.as_ptr().cast(), self.1 as usize) };
|
|
}
|
|
}
|
|
|
|
/// `count` buffers of `bytes` each, each over pages of its own.
|
|
struct Buffers {
|
|
buffers: Vec<Buffer>,
|
|
// After the buffers: dropped once they went.
|
|
pages: Vec<Pages>,
|
|
}
|
|
|
|
impl Buffers {
|
|
fn new(device: &Device, count: usize, bytes: u64) -> Result<Buffers> {
|
|
let options = ResourceOptions::new(
|
|
StorageMode::Managed,
|
|
CpuCacheMode::DefaultCache,
|
|
HazardTrackingMode::Default,
|
|
);
|
|
let mut buffers = Vec::with_capacity(count);
|
|
let mut pages = Vec::with_capacity(count);
|
|
for _ in 0..count {
|
|
let host = Pages::new(bytes)?;
|
|
// SAFETY: the pages outlive the buffer (dropped after it, `Buffers`), and only its
|
|
// commands and this example's CPU accesses use them.
|
|
let buffer = unsafe {
|
|
device.new_buffer_with_bytes_no_copy(HostMemory::new(host.0), bytes, options, None)
|
|
}?;
|
|
buffers.push(buffer);
|
|
pages.push(host);
|
|
}
|
|
Ok(Buffers { buffers, pages })
|
|
}
|
|
}
|
|
|
|
/// One row's command buffers: what each does.
|
|
#[derive(Clone, Copy)]
|
|
enum Shape {
|
|
/// 40 large buffers: the GPU fills `written` of them, the CPU writes the rest where
|
|
/// `modified`, and a blit synchronizes all 40.
|
|
Large { written: usize, modified: bool },
|
|
/// The 1 MiB fill, then a blit synchronizing the two small buffers where `synchronized`.
|
|
Small { synchronized: bool },
|
|
}
|
|
|
|
/// One command buffer of `shape`, of round `round`: its GPU time and its wall time from the commit
|
|
/// to its completion, in seconds.
|
|
fn run(
|
|
queue: &CommandQueue,
|
|
shape: Shape,
|
|
buffers: &Buffers,
|
|
target: &Buffer,
|
|
round: u8,
|
|
) -> Result<(f64, f64)> {
|
|
let command_buffer = queue.command_buffer()?;
|
|
match shape {
|
|
Shape::Large { written, modified } => {
|
|
if modified {
|
|
for (buffer, pages) in buffers.buffers.iter().zip(&buffers.pages).skip(written) {
|
|
pages.write(round);
|
|
buffer.did_modify_range(0..LARGE)?;
|
|
}
|
|
}
|
|
if written > 0 {
|
|
let mut fill = command_buffer.blit_command_encoder()?;
|
|
for buffer in &buffers.buffers[..written] {
|
|
fill.fill_buffer(buffer, 0..LARGE, round)?;
|
|
}
|
|
fill.end_encoding()?;
|
|
}
|
|
let mut sync = command_buffer.blit_command_encoder()?;
|
|
for buffer in &buffers.buffers {
|
|
sync.synchronize_resource(ResourceRef::Buffer(buffer))?;
|
|
}
|
|
sync.end_encoding()?;
|
|
}
|
|
Shape::Small { synchronized } => {
|
|
let mut fill = command_buffer.blit_command_encoder()?;
|
|
fill.fill_buffer(target, 0..FILL, round)?;
|
|
fill.end_encoding()?;
|
|
if synchronized {
|
|
let mut sync = command_buffer.blit_command_encoder()?;
|
|
for buffer in &buffers.buffers {
|
|
sync.synchronize_resource(ResourceRef::Buffer(buffer))?;
|
|
}
|
|
sync.end_encoding()?;
|
|
}
|
|
}
|
|
}
|
|
let began = Instant::now();
|
|
command_buffer.commit()?;
|
|
command_buffer
|
|
.wait_until_completed_for(WAIT)
|
|
.map_err(|_| format!("a command buffer did not complete within {WAIT:?}"))?;
|
|
let wall = began.elapsed().as_secs_f64();
|
|
if command_buffer.status() != CommandBufferStatus::Completed {
|
|
let error = command_buffer.error();
|
|
return Err(format!(
|
|
"a command buffer ended {:?}: {error:?}",
|
|
command_buffer.status()
|
|
)
|
|
.into());
|
|
}
|
|
let gpu = command_buffer.gpu_end_time()? - command_buffer.gpu_start_time()?;
|
|
match shape {
|
|
Shape::Large { written, modified } => {
|
|
for (index, pages) in buffers.pages.iter().enumerate() {
|
|
let want = match index < written || modified {
|
|
true => round,
|
|
false => FIRST,
|
|
};
|
|
pages.check(want, &format!("round {round}, buffer {index}"))?;
|
|
}
|
|
}
|
|
Shape::Small { .. } => {
|
|
for (index, pages) in buffers.pages.iter().enumerate() {
|
|
pages.check(FIRST, &format!("round {round}, small buffer {index}"))?;
|
|
}
|
|
}
|
|
}
|
|
Ok((gpu, wall))
|
|
}
|
|
|
|
/// A field of `counts` by name, read from their `Debug` form: `None` where they have none.
|
|
fn field(counts: &WorkCounts, name: &str) -> Option<u64> {
|
|
let text = format!("{counts:?}");
|
|
let at = text.find(&format!(" {name}: "))? + name.len() + 3;
|
|
let digits: String = text[at..]
|
|
.chars()
|
|
.take_while(char::is_ascii_digit)
|
|
.collect();
|
|
digits.parse().ok()
|
|
}
|
|
|
|
/// The median of `values`, sorted here.
|
|
fn median(values: &mut [f64]) -> f64 {
|
|
values.sort_by(f64::total_cmp);
|
|
values[values.len() / 2]
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
let device = Device::system_default()?;
|
|
let unified = device.properties().has_unified_memory;
|
|
println!("device: {} (unified memory: {unified})", device.name());
|
|
let queue = device.new_command_queue()?;
|
|
let private = ResourceOptions::new(
|
|
StorageMode::Private,
|
|
CpuCacheMode::DefaultCache,
|
|
HazardTrackingMode::Default,
|
|
);
|
|
let target = device.new_buffer(FILL, private)?;
|
|
let rows = [
|
|
(
|
|
"all",
|
|
Shape::Large {
|
|
written: 40,
|
|
modified: false,
|
|
},
|
|
20,
|
|
),
|
|
(
|
|
"mixed",
|
|
Shape::Large {
|
|
written: 30,
|
|
modified: true,
|
|
},
|
|
20,
|
|
),
|
|
(
|
|
"none",
|
|
Shape::Large {
|
|
written: 0,
|
|
modified: false,
|
|
},
|
|
20,
|
|
),
|
|
("small", Shape::Small { synchronized: true }, 200),
|
|
(
|
|
"fill",
|
|
Shape::Small {
|
|
synchronized: false,
|
|
},
|
|
200,
|
|
),
|
|
];
|
|
for (name, shape, timed) in rows {
|
|
let buffers = match shape {
|
|
Shape::Large { .. } => Buffers::new(&device, 40, LARGE)?,
|
|
Shape::Small { .. } => Buffers::new(&device, 2, SMALL)?,
|
|
};
|
|
let mut round = 0u8;
|
|
let mut next = || {
|
|
round = round.wrapping_add(1).max(FIRST + 1);
|
|
round
|
|
};
|
|
for _ in 0..WARM {
|
|
run(&queue, shape, &buffers, &target, next())?;
|
|
}
|
|
let before = device.work_counts();
|
|
let mut gpu = Vec::with_capacity(timed);
|
|
let mut wall = Vec::with_capacity(timed);
|
|
for _ in 0..timed {
|
|
let (seconds, elapsed) = run(&queue, shape, &buffers, &target, next())?;
|
|
gpu.push(seconds * 1e3);
|
|
wall.push(elapsed * 1e3);
|
|
}
|
|
let after = device.work_counts();
|
|
let each =
|
|
|pick: fn(&WorkCounts) -> u64| (pick(&after) - pick(&before)) as f64 / timed as f64;
|
|
let topped_up = match (
|
|
field(&after, "managed_synchronizes_topped_up"),
|
|
field(&before, "managed_synchronizes_topped_up"),
|
|
) {
|
|
(Some(after), Some(before)) => (after - before).to_string(),
|
|
_ => "-".to_string(),
|
|
};
|
|
println!(
|
|
"ROW {name} gpu_ms {:.3} wall_ms {:.3} copied_mb {:.2} subs {:.2} infos {:.2} \
|
|
vkcbs {:.2} ({:.2}, {:.2}, {:.2}, {:.2}, {:.2}) fillq {:.2} commit_us {:.1} \
|
|
topped_up {topped_up} {}",
|
|
median(&mut gpu),
|
|
median(&mut wall),
|
|
each(|counts| counts.managed_bytes_copied_out) / 1e6,
|
|
each(|counts| counts.queue_submissions),
|
|
each(|counts| counts.submit_infos),
|
|
each(|counts| counts.command_buffers_begun),
|
|
each(|counts| counts.command_buffers_begun_encoders),
|
|
each(|counts| counts.command_buffers_begun_continuations),
|
|
each(|counts| counts.command_buffers_begun_batch_work),
|
|
each(|counts| counts.command_buffers_begun_batch_barriers),
|
|
each(|counts| counts.command_buffers_begun_internal),
|
|
each(|counts| counts.fill_queue_submissions),
|
|
each(|counts| counts.commit_nanos) / 1e3,
|
|
device.name()
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|