Memory tests: each fill's allocation, free and CPUs beside its block's own allocation

The reclaim bench and tests::block_fills print, for each pooled block filled through an
exportable allocation first, how long that allocation and its free took (and on a CPU), how
long the block's own vkAllocateMemory took (on a CPU, waiting for one), and the CPUs each part
ended on, so that a missed fill shows where its time went (memory/making_parts.rs, test-only).

Verified: built and run under --cfg m2v_gpu on the 780M (see the branch's later commits).
(cherry picked from commit a290c0c7bc7b10f92db3f7ca8cac3e3317a570e5)
This commit is contained in:
Anees Iqbal committed 2026-11-10 02:11:06 -08:00
1 parent fcf1219b03
commit 82f046a119
4 files changed
+117 -6

No files matched your search

+11
View File
@@ -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
}
}
@@ -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<FillParts>,
}
/// 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<Vec<MakingParts>> = 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<Option<FillParts>> = 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<FillParts> {
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<String> {
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,
))
}
@@ -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<Texture> = 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!(
@@ -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) {