A bench of a Managed buffer's copies in across a guest RAM's chunk end
What: tests/host_ram_managed.rs, a new device-test module for Managed no-copy buffers over a guest RAM, with one ignored release bench, MANAGED-RAM-COPY-IN. Its buffer is 32 MiB centred on the end of the RAM's first chunk, and both chunks are imported. Ranges of 256 KiB, 4 MiB and 32 MiB across the chunk end are written and named before each of 20 commits. Each commit reads the range's first and last bytes, and they are checked. It reports the commit-call and to-completion medians and the bytes copied in, on the default device and on one of exact_answers(). host_ram_device.rs exposes its RAM helpers and gains held_device(policy). Why: such a buffer's host copy is its pages alone, because its own import would be over pages the chunks' imports hold. Its copies in are staged by the CPU at the commit today. This bench measures that before the GPU copies them from the chunks' imports. Verified: not run at this commit. It is run on the 2080 before and after the change.
This commit is contained in:
1 parent
bb728adca5
commit
0ab26709c9
3 files changed
+198
-14
No files matched your search
@@ -77,6 +77,7 @@ use crate::{
|
||||
HeapDescriptor, HeapType, HostBacking, HostMemory, HostRam, ImportGate, ImportRequest,
|
||||
KeepAlive, MessageSink, Problem, ResourceOptions, RunResolver, Size,
|
||||
};
|
||||
use metal2vulkan::device::DevicePolicy;
|
||||
use std::collections::BTreeSet;
|
||||
use std::ops::Range;
|
||||
use std::ptr::NonNull;
|
||||
@@ -84,8 +85,8 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
const WAIT: Duration = Duration::from_secs(60);
|
||||
const MIB: u64 = 1 << 20;
|
||||
pub(super) const WAIT: Duration = Duration::from_secs(60);
|
||||
pub(super) const MIB: u64 = 1 << 20;
|
||||
/// The RAM's bytes: two whole chunks and a short third.
|
||||
const RAM_BYTES: u64 = 128 * MIB + 256 * 1024;
|
||||
|
||||
@@ -106,7 +107,7 @@ impl Drop for Mapping {
|
||||
}
|
||||
|
||||
/// A RAM of `RAM_BYTES` in a pattern, its first byte, and whether its keep-alive was dropped.
|
||||
fn new_ram() -> (Arc<HostRam>, u64, Arc<AtomicBool>) {
|
||||
pub(super) fn new_ram() -> (Arc<HostRam>, u64, Arc<AtomicBool>) {
|
||||
// SAFETY: a fresh private anonymous mapping.
|
||||
let start = unsafe {
|
||||
libc::mmap(
|
||||
@@ -138,7 +139,7 @@ fn pattern(address: u64) -> u8 {
|
||||
}
|
||||
|
||||
/// Write the pattern over `range` of host addresses, while no command buffer runs.
|
||||
fn fill_pattern(range: Range<u64>) {
|
||||
pub(super) fn fill_pattern(range: Range<u64>) {
|
||||
for address in range {
|
||||
// SAFETY: bytes of the test's RAM, which no command buffer uses now.
|
||||
unsafe { *(address as *mut u8) = pattern(address) };
|
||||
@@ -146,7 +147,7 @@ fn fill_pattern(range: Range<u64>) {
|
||||
}
|
||||
|
||||
/// The bytes of `range` of host addresses, while no command buffer runs.
|
||||
fn bytes(range: Range<u64>) -> Vec<u8> {
|
||||
pub(super) fn bytes(range: Range<u64>) -> Vec<u8> {
|
||||
let length = (range.end - range.start) as usize;
|
||||
// SAFETY: as above.
|
||||
unsafe { std::slice::from_raw_parts(range.start as *const u8, length) }.to_vec()
|
||||
@@ -155,21 +156,27 @@ fn bytes(range: Range<u64>) -> Vec<u8> {
|
||||
/// A device of the cfg's kind whose uses of a RAM start no thread importing its chunks ahead
|
||||
/// (`Device::hold_host_ram_imports`); `None` where it imports no host memory.
|
||||
fn device() -> Option<Device> {
|
||||
let device = ahead_device()?;
|
||||
held_device(DevicePolicy::default())
|
||||
}
|
||||
|
||||
/// [`device`], of `policy`.
|
||||
pub(super) fn held_device(policy: DevicePolicy) -> Option<Device> {
|
||||
let device = ahead_device_with(None, BTreeSet::new(), policy)?;
|
||||
device.hold_host_ram_imports();
|
||||
Some(device)
|
||||
}
|
||||
|
||||
/// A device of the cfg's kind, as the product makes it; `None` where it imports no host memory.
|
||||
pub(super) fn ahead_device() -> Option<Device> {
|
||||
ahead_device_with(None, BTreeSet::new())
|
||||
ahead_device_with(None, BTreeSet::new(), DevicePolicy::default())
|
||||
}
|
||||
|
||||
/// [`ahead_device`], its messages given to `messages`, announcing the kinds of `announce_each`
|
||||
/// at each occurrence.
|
||||
/// [`ahead_device`], of `policy`, its messages given to `messages`, announcing the kinds of
|
||||
/// `announce_each` at each occurrence.
|
||||
fn ahead_device_with(
|
||||
messages: Option<MessageSink>,
|
||||
announce_each: BTreeSet<Degradation>,
|
||||
policy: DevicePolicy,
|
||||
) -> Option<Device> {
|
||||
let options = DeviceOptions {
|
||||
kinds: match cfg!(m2v_cpu_icd) {
|
||||
@@ -182,6 +189,7 @@ fn ahead_device_with(
|
||||
},
|
||||
messages,
|
||||
announce_each,
|
||||
policy,
|
||||
..DeviceOptions::default()
|
||||
};
|
||||
let device = Device::system_default_with(&options).unwrap();
|
||||
@@ -257,7 +265,7 @@ fn buffer(
|
||||
}
|
||||
|
||||
/// Commit one command buffer whose blit encoder `encode` fills, and wait for it.
|
||||
fn blit(device: &Device, encode: impl FnOnce(&mut crate::BlitCommandEncoder)) {
|
||||
pub(super) fn blit(device: &Device, encode: impl FnOnce(&mut crate::BlitCommandEncoder)) {
|
||||
let queue = device.new_command_queue().unwrap();
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
let mut encoder = command_buffer.blit_command_encoder().unwrap();
|
||||
@@ -269,7 +277,7 @@ fn blit(device: &Device, encode: impl FnOnce(&mut crate::BlitCommandEncoder)) {
|
||||
}
|
||||
|
||||
/// The buffer's first `length` bytes as the GPU reads them.
|
||||
fn gpu_bytes(device: &Device, buffer: &Buffer, length: u64) -> Vec<u8> {
|
||||
pub(super) fn gpu_bytes(device: &Device, buffer: &Buffer, length: u64) -> Vec<u8> {
|
||||
let own = device
|
||||
.new_buffer(length, ResourceOptions::shared())
|
||||
.unwrap();
|
||||
@@ -284,7 +292,7 @@ fn gpu_bytes(device: &Device, buffer: &Buffer, length: u64) -> Vec<u8> {
|
||||
}
|
||||
|
||||
/// Wait until `flag` is set, a release on a handler thread having run.
|
||||
fn settles(flag: &AtomicBool) -> bool {
|
||||
pub(super) fn settles(flag: &AtomicBool) -> bool {
|
||||
let started = Instant::now();
|
||||
while !flag.load(Ordering::SeqCst) {
|
||||
if started.elapsed() > Duration::from_secs(20) {
|
||||
@@ -980,7 +988,11 @@ fn the_window_ahead_moves_with_the_objects_and_imports_no_more() {
|
||||
#[test]
|
||||
fn a_ram_of_shared_memory_a_device_refuses_imports_no_chunk_and_copies_its_objects() {
|
||||
let heard = Arc::new(AtomicUsize::new(0));
|
||||
let Some(device) = ahead_device_with(Some(copies_heard(&heard)), BTreeSet::new()) else {
|
||||
let Some(device) = ahead_device_with(
|
||||
Some(copies_heard(&heard)),
|
||||
BTreeSet::new(),
|
||||
DevicePolicy::default(),
|
||||
) else {
|
||||
return;
|
||||
};
|
||||
if device.imports_shared_host_pointers().unwrap() {
|
||||
@@ -2973,7 +2985,7 @@ fn pages_refused_before_the_driver_with_no_source_are_copied() {
|
||||
}
|
||||
});
|
||||
let each = BTreeSet::from([Degradation::CopiedHostPages]);
|
||||
let Some(device) = ahead_device_with(Some(sink), each) else {
|
||||
let Some(device) = ahead_device_with(Some(sink), each, DevicePolicy::default()) else {
|
||||
return;
|
||||
};
|
||||
device.hold_host_ram_imports();
|
||||
|
||||
@@ -0,0 +1,170 @@
|
||||
//! `Managed` no-copy buffers over a guest RAM (`host_ram.rs`) on a device without unified memory
|
||||
//! (`managed_copy.rs`): two copies, the caller's pages and the GPU's own, the buffer's pages
|
||||
//! across the end of a chunk whose import, and the next chunk's, live.
|
||||
//!
|
||||
//! - **The bench across a chunk's end** (ignored, `MANAGED-RAM-COPY-IN`; run in release): what a
|
||||
//! `did_modify_range` of 256 KiB, 4 MiB and 32 MiB across the end of the RAM's first chunk,
|
||||
//! written by the CPU before each commit of a command buffer that reads the range's first and
|
||||
//! last bytes, costs the commit call and the wall time to the completion, on the default device
|
||||
//! and on one of `DevicePolicy::exact_answers()`: the medians, and the bytes copied in.
|
||||
//!
|
||||
//! Device tests: compiled only under the GPU safety cfgs (`m2v_gpu`, `m2v_cpu_icd`), on the kind
|
||||
//! of device the cfg selects. A device that imports no host memory, or has unified memory (where
|
||||
//! a `Managed` buffer is one copy), says so and passes.
|
||||
|
||||
use super::host_ram_device::{gate, held_device, itself, new_ram, resolver, settles, MIB, WAIT};
|
||||
use crate::{
|
||||
Buffer, CpuCacheMode, Device, HazardTrackingMode, HostBacking, HostMemory, ResourceOptions,
|
||||
StorageMode,
|
||||
};
|
||||
use metal2vulkan::device::DevicePolicy;
|
||||
use std::ptr::NonNull;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// The end of the RAM's first chunk, from its first byte (`host_ram.rs`'s `CHUNK_BYTES`).
|
||||
const CHUNK_END: u64 = 64 * MIB;
|
||||
|
||||
/// The bench's buffer: 32 MiB, its middle the first chunk's end.
|
||||
const BENCH_BYTES: u64 = 32 * MIB;
|
||||
|
||||
/// The ranges the bench names, each its middle the first chunk's end.
|
||||
const BENCH_RANGES: [u64; 3] = [256 << 10, 4 * MIB, 32 * MIB];
|
||||
|
||||
/// The commits of each of the bench's ranges.
|
||||
const BENCH_COMMITS: u32 = 20;
|
||||
|
||||
/// The options of a `Managed` buffer.
|
||||
fn managed() -> ResourceOptions {
|
||||
ResourceOptions::new(
|
||||
StorageMode::Managed,
|
||||
CpuCacheMode::DefaultCache,
|
||||
HazardTrackingMode::Default,
|
||||
)
|
||||
}
|
||||
|
||||
/// A device of `policy` whose RAM's chunks are each imported by the first object over it
|
||||
/// (`host_ram_device.rs`'s `held_device`), on which a `Managed` no-copy buffer is two copies;
|
||||
/// `None` where it imports no host memory or has unified memory.
|
||||
fn two_copy_device(policy: DevicePolicy) -> Option<Device> {
|
||||
let device = held_device(policy)?;
|
||||
if device.properties().has_unified_memory {
|
||||
eprintln!(
|
||||
"HOST-RAM-MANAGED {}: unified memory; a Managed buffer is one copy",
|
||||
device.name()
|
||||
);
|
||||
return None;
|
||||
}
|
||||
Some(device)
|
||||
}
|
||||
|
||||
/// The memory at host address `at` of the test's RAM, its pages their own run of it, under a gate
|
||||
/// that admits every import.
|
||||
fn memory_at(at: u64) -> HostMemory {
|
||||
let (runs, gates): (Arc<Mutex<_>>, Arc<Mutex<_>>) = Default::default();
|
||||
// SAFETY: the RAM's bytes, mapped until its keep-alive goes, after every device let go.
|
||||
unsafe { HostMemory::new(NonNull::new(at as *mut u8).unwrap()) }
|
||||
.with_runs(resolver(&runs, itself))
|
||||
.with_import_gate(gate(&gates, true))
|
||||
}
|
||||
|
||||
/// `Shared` buffers of four pages, one in each of the RAM's two whole chunks from `start`, far
|
||||
/// from the pages between them, which import the chunks.
|
||||
fn import_both_chunks(device: &Device, start: u64) -> (Buffer, Buffer) {
|
||||
let page = device.shared().host_import.as_ref().unwrap().page;
|
||||
let in_chunk = |at: u64| {
|
||||
// SAFETY: as in `memory_at`.
|
||||
unsafe {
|
||||
device.new_buffer_with_bytes_no_copy(
|
||||
memory_at(at),
|
||||
4 * page,
|
||||
ResourceOptions::default(),
|
||||
None,
|
||||
)
|
||||
}
|
||||
.unwrap()
|
||||
};
|
||||
(in_chunk(start + 16 * page), in_chunk(start + 96 * MIB))
|
||||
}
|
||||
|
||||
/// The median of `values`, in milliseconds.
|
||||
fn median_ms(mut values: Vec<Duration>) -> f64 {
|
||||
values.sort();
|
||||
values[values.len() / 2].as_secs_f64() * 1e3
|
||||
}
|
||||
|
||||
/// The bench across a chunk's end (the module docs).
|
||||
#[test]
|
||||
#[ignore = "a release-mode benchmark"]
|
||||
fn managed_ram_copy_in_bench() {
|
||||
for exact in [false, true] {
|
||||
let policy = match exact {
|
||||
true => DevicePolicy::default().exact_answers(),
|
||||
false => DevicePolicy::default(),
|
||||
};
|
||||
let Some(device) = two_copy_device(policy) else {
|
||||
return;
|
||||
};
|
||||
let named = match exact {
|
||||
true => "exact answers",
|
||||
false => "default",
|
||||
};
|
||||
let (ram, start, dropped) = new_ram();
|
||||
let used = device.use_host_ram(&ram).unwrap();
|
||||
let chunks = import_both_chunks(&device, start);
|
||||
let at = start + CHUNK_END - BENCH_BYTES / 2;
|
||||
// SAFETY: as in `memory_at`.
|
||||
let buffer = unsafe {
|
||||
device.new_buffer_with_bytes_no_copy(memory_at(at), BENCH_BYTES, managed(), None)
|
||||
}
|
||||
.unwrap();
|
||||
assert_eq!(buffer.host_backing(), Some(HostBacking::DeviceCopy));
|
||||
let queue = device.new_command_queue().unwrap();
|
||||
let out = device.new_buffer(8192, ResourceOptions::shared()).unwrap();
|
||||
for range in BENCH_RANGES {
|
||||
let named_range = BENCH_BYTES / 2 - range / 2..BENCH_BYTES / 2 + range / 2;
|
||||
let before = device.work_counts();
|
||||
let (mut commits, mut whole) = (Vec::new(), Vec::new());
|
||||
for commit in 0..BENCH_COMMITS {
|
||||
let byte = commit as u8 + 1;
|
||||
// SAFETY: the RAM's bytes, which no command buffer reads now.
|
||||
unsafe {
|
||||
std::ptr::write_bytes((at + named_range.start) as *mut u8, byte, range as usize)
|
||||
};
|
||||
let began = Instant::now();
|
||||
buffer.did_modify_range(named_range.clone()).unwrap();
|
||||
let command_buffer = queue.command_buffer().unwrap();
|
||||
let mut blit = command_buffer.blit_command_encoder().unwrap();
|
||||
blit.copy_from_buffer_to_buffer(&buffer, named_range.start, &out, 0, 4096)
|
||||
.unwrap();
|
||||
blit.copy_from_buffer_to_buffer(&buffer, named_range.end - 4096, &out, 4096, 4096)
|
||||
.unwrap();
|
||||
blit.end_encoding().unwrap();
|
||||
let committing = Instant::now();
|
||||
command_buffer.commit().unwrap();
|
||||
commits.push(committing.elapsed());
|
||||
assert!(command_buffer.wait_until_completed_for(WAIT).is_ok());
|
||||
whole.push(began.elapsed());
|
||||
assert_eq!(command_buffer.error(), None);
|
||||
let contents = out.contents().unwrap();
|
||||
// SAFETY: the out buffer's bytes, which no command writes now.
|
||||
let seen = unsafe { (*contents.as_ptr(), *contents.as_ptr().add(4096)) };
|
||||
assert_eq!(seen, (byte, byte), "{range}: commit {commit}");
|
||||
}
|
||||
let after = device.work_counts();
|
||||
let (commit, wall) = (median_ms(commits.clone()), median_ms(whole));
|
||||
let most = commits.iter().max().unwrap().as_secs_f64() * 1e3;
|
||||
eprintln!(
|
||||
"MANAGED-RAM-COPY-IN {} {named}, {} KiB a commit across a chunk's end: commit \
|
||||
{commit:.3} ms (most {most:.3}), to completion {wall:.3} ms, {:.2} GB/s; copied \
|
||||
in {} bytes",
|
||||
device.name(),
|
||||
range >> 10,
|
||||
range as f64 / (wall * 1e6),
|
||||
after.managed_bytes_copied_in - before.managed_bytes_copied_in,
|
||||
);
|
||||
}
|
||||
drop((buffer, out, queue, chunks, used, ram, device));
|
||||
assert!(settles(&dropped), "the RAM's keep-alive did not go");
|
||||
}
|
||||
}
|
||||
@@ -319,6 +319,8 @@ mod host_ram_dma_buf;
|
||||
mod host_ram_first_fill_bench;
|
||||
#[cfg(all(m2v_gpu, target_os = "linux"))]
|
||||
mod host_ram_listed_bench;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
mod host_ram_managed;
|
||||
#[cfg(all(m2v_gpu, target_os = "linux"))]
|
||||
mod host_ram_surface_fill_bench;
|
||||
#[cfg(any(m2v_gpu, m2v_cpu_icd))]
|
||||
|
||||
Reference in new issue
Block a user