diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index 37d43a297..ee2bdf91b 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -3929,6 +3929,8 @@ impl BlockMaker { let fill = handle.is_none() && dedicated_to == vk::Buffer::null() && self.fill_types & 1 << type_index != 0; + #[cfg(all(test, m2v_gpu, target_os = "linux"))] + let _ = making_parts::take_fill(-1); let filled = fill && self.fill_first(device, info.allocation_size, type_index); let allocating = Instant::now(); #[cfg(all(test, m2v_gpu, target_os = "linux"))] @@ -3945,6 +3947,8 @@ impl BlockMaker { let allocated = Instant::now(); #[cfg(all(test, m2v_gpu, target_os = "linux"))] let after = making_parts::thread_times(); + #[cfg(all(test, m2v_gpu, target_os = "linux"))] + let allocated_on = making_parts::cpu(); let host_visible = self.host_visible & 1 << type_index != 0; let mapped = if host_visible { // SAFETY: the memory is host-visible, unmapped, and mapped whole once. @@ -3999,6 +4003,7 @@ impl BlockMaker { ], on_cpu: after.0.saturating_sub(before.0), runnable: after.1.saturating_sub(before.1), + fill: making_parts::take_fill(allocated_on), }); Ok(Block { memory, @@ -4034,13 +4039,19 @@ impl BlockMaker { .allocation_size(bytes) .memory_type_index(type_index) .push_next(&mut exportable); + #[cfg(all(test, m2v_gpu, target_os = "linux"))] + let began = making_parts::moment(); // SAFETY: `info` and its chain live for the call; the type index is the device's, and the // device exports pooled memory as the handle type ([`Memory::fill_plain_blocks`]). let Ok(memory) = (unsafe { crate::creation::allocate_memory(device, &info) }) else { return false; }; + #[cfg(all(test, m2v_gpu, target_os = "linux"))] + let allocated = making_parts::moment(); // SAFETY: nothing uses the memory, never mapped nor bound. unsafe { release::object(device, memory) }; + #[cfg(all(test, m2v_gpu, target_os = "linux"))] + making_parts::filled(began, allocated, making_parts::moment()); true } } diff --git a/crates/metal2vulkan-runtime/src/memory/making_parts.rs b/crates/metal2vulkan-runtime/src/memory/making_parts.rs index be332c7f6..256914007 100644 --- a/crates/metal2vulkan-runtime/src/memory/making_parts.rs +++ b/crates/metal2vulkan-runtime/src/memory/making_parts.rs @@ -1,10 +1,12 @@ -//! The parts of each block's making, for the reclaim bench (`tests::reclaim_submit_bench`): how -//! long the buffer of addresses, the fill, `vkAllocateMemory`, the mapping and the buffer's -//! binding took, -//! and how much of `vkAllocateMemory` the making thread spent on a CPU -//! (`/proc/thread-self/schedstat`), so that the bench can line each making up with the -//! submissions it held. +//! The parts of each block's making, for the reclaim bench (`tests::reclaim_submit_bench`) and the +//! fills' test (`tests::block_fills`): how long the buffer of addresses, the fill, `vkAllocateMemory`, +//! the mapping and the buffer's binding took, how much of `vkAllocateMemory` the making thread +//! spent on a CPU (`/proc/thread-self/schedstat`), and, where the block was filled through an +//! exportable allocation first, that allocation's and its free's parts and the CPUs the making +//! ran on, so that the bench can line each making up with the submissions it held and a missed +//! fill shows where its time went. +use std::cell::Cell; use std::sync::Mutex; use std::time::{Duration, Instant}; @@ -25,11 +27,34 @@ pub(crate) struct MakingParts { /// for one. pub(crate) on_cpu: Duration, pub(crate) runnable: Duration, + /// Where its pages were filled through an exportable allocation first, that fill's parts. + pub(crate) fill: Option, +} + +/// The parts of a block's fill through an exportable allocation (`memory.rs`'s +/// `BlockMaker::fill_first`). +#[derive(Clone, Copy, Debug)] +pub(crate) struct FillParts { + /// How long the exportable allocation took and its free took. + pub(crate) allocate: Duration, + pub(crate) free: Duration, + /// The time the making thread ran on a CPU during the exportable allocation, and during its + /// free. + pub(crate) allocate_on_cpu: Duration, + pub(crate) free_on_cpu: Duration, + /// The CPUs the making thread ran on as the exportable allocation began, as it ended, as its + /// free ended and as the block's own allocation ended (`sched_getcpu`). + pub(crate) cpus: [i32; 4], } /// Every making since the bench last drained it. pub(crate) static MAKINGS: Mutex> = Mutex::new(Vec::new()); +thread_local! { + /// The parts of the fill the calling thread made last, taken by its block's making. + static FILL: Cell> = const { Cell::new(None) }; +} + /// The calling thread's time on a CPU and waiting for one so far (`/proc/thread-self/schedstat`), /// zero where it is not readable. pub(super) fn thread_times() -> (Duration, Duration) { @@ -40,3 +65,64 @@ pub(super) fn thread_times() -> (Duration, Duration) { let mut next = || Duration::from_nanos(fields.next().unwrap_or(0)); (next(), next()) } + +/// The CPU the calling thread runs on. +pub(super) fn cpu() -> i32 { + // SAFETY: no argument; it reads the calling thread's CPU. + unsafe { libc::sched_getcpu() } +} + +/// A fill's moments ([`FILL`]): as its allocation began, as it ended and as its free ended, each +/// with the thread's times and CPU. +pub(super) type FillMoment = (Instant, (Duration, Duration), i32); + +/// The calling thread's moment now, for a fill's parts. +pub(super) fn moment() -> FillMoment { + (Instant::now(), thread_times(), cpu()) +} + +/// Record the calling thread's fill of these moments, for its block's making to take. +pub(super) fn filled(began: FillMoment, allocated: FillMoment, freed: FillMoment) { + FILL.set(Some(FillParts { + allocate: allocated.0 - began.0, + free: freed.0 - allocated.0, + allocate_on_cpu: allocated.1 .0.saturating_sub(began.1 .0), + free_on_cpu: freed.1 .0.saturating_sub(allocated.1 .0), + cpus: [began.2, allocated.2, freed.2, -1], + })); +} + +/// The calling thread's last fill, taken, the block's own allocation having ended on CPU `cpu`. +pub(super) fn take_fill(cpu: i32) -> Option { + FILL.take().map(|mut fill| { + fill.cpus[3] = cpu; + fill + }) +} + +/// A line of `making`'s fill, where it was filled first: the exportable allocation's and its +/// free's times (on a CPU), the block's own allocation's (on a CPU, waiting for one), and the CPUs +/// each ended on. +pub(crate) fn fill_line(making: &MakingParts) -> Option { + let fill = making.fill?; + let ms = |took: Duration| took.as_secs_f64() * 1e3; + Some(format!( + "{} MiB {}: fill allocated {:.2} ms (on a CPU {:.2}), freed {:.2} ms (on a CPU {:.2}); \ + own vkAllocateMemory {:.2} ms (on a CPU {:.2}, waiting for one {:.2}), {}; CPUs {:?}", + making.bytes >> 20, + if making.ahead { "ahead" } else { "in place" }, + ms(fill.allocate), + ms(fill.allocate_on_cpu), + ms(fill.free), + ms(fill.free_on_cpu), + ms(making.took[2]), + ms(making.on_cpu), + ms(making.runnable), + if making.took[2] < super::FILL_SAVED_BELOW { + "saved" + } else { + "missed" + }, + fill.cpus, + )) +} diff --git a/crates/metal2vulkan-runtime/src/tests/block_fills.rs b/crates/metal2vulkan-runtime/src/tests/block_fills.rs index e4a8cc83c..5aeb42167 100644 --- a/crates/metal2vulkan-runtime/src/tests/block_fills.rs +++ b/crates/metal2vulkan-runtime/src/tests/block_fills.rs @@ -36,6 +36,8 @@ fn plain_blocks_are_filled_exportable_first_where_their_making_holds_submissions .driver_facts .contains(&DriverFact::PlainAllocationsHoldSubmissions); let types = crate::device::lock(&device.shared().memory).fill_types(); + #[cfg(all(m2v_gpu, target_os = "linux"))] + let earlier = crate::memory::making_parts::MAKINGS.lock().unwrap().len(); let start = device.work_counts(); let mut held: Vec = Vec::new(); let mut counts = start; @@ -69,6 +71,12 @@ fn plain_blocks_are_filled_exportable_first_where_their_making_holds_submissions made under 1 ms, {missed} filled first and not", device.name(), ); + #[cfg(all(m2v_gpu, target_os = "linux"))] + for making in &crate::memory::making_parts::MAKINGS.lock().unwrap()[earlier..] { + if let Some(line) = crate::memory::making_parts::fill_line(making) { + println!("BLOCK-FILLS {}: fill of {line}", device.name()); + } + } match holds { true => { assert_ne!( diff --git a/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs b/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs index 405552eda..ea983ad06 100644 --- a/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs +++ b/crates/metal2vulkan-runtime/src/tests/reclaim_submit_bench.rs @@ -929,6 +929,12 @@ fn print_makings( in_fills.iter().filter(|&&took| took >= 8.0).count(), in_fills.iter().copied().fold(0.0, f64::max), ); + for line in makings + .iter() + .filter_map(crate::memory::making_parts::fill_line) + { + eprintln!("RECLAIM-SUBMIT {arm}, {name}: fill of {line}"); + } let mut slowest: Vec<_> = makings.iter().collect(); slowest.sort_by_key(|making| std::cmp::Reverse(making.took[2])); for making in slowest.into_iter().take(SLOWEST) {