Files
metal2vulkan/crates/metal2vulkan-runtime/examples/host_ab/allocs.rs
T
Anees Iqbal a755eb6d27 host_ab: the consumer's frames call by call (calls), with allocations per call
The steps this packet landed as, in order:
- Box a render encoder's lowering where it is made
- Allocate less per draw and per pass end
- Make room once for an encoder's per-resource range lists and a command buffer's first sets
- host_ab calls: each frame's wall time, and the runtime's replay and scheduler counts
- host_ab calls: draws after one useResources of 0, 1, 256 or 4096 buffers
- Take a draw's used resources while its encoder's residency is unchanged
- Wake the recorder from the scheduler's thread, so a commit wakes one thread
- Hold an encoder's first tracker part in place, so a pass open allocates as before the box

Content: the tree of `development` at b775ade3a0, unchanged.
2026-10-26 13:19:05 +02:00

67 lines
1.9 KiB
Rust

//! The process's allocator, counting each thread's allocations while [`counting`] runs: the
//! calls workload's `*_allocs` metrics. Outside it each allocation pays one relaxed load more
//! than the system allocator's own work.
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::sync::atomic::{AtomicBool, Ordering};
/// Allocations are counted.
static ON: AtomicBool = AtomicBool::new(false);
struct Counting;
#[global_allocator]
static ALLOCATOR: Counting = Counting;
thread_local! {
/// Allocations (and reallocations) this thread made.
static MADE: Cell<u64> = const { Cell::new(0) };
}
fn note() {
if ON.load(Ordering::Relaxed) {
// A thread being torn down no longer counts.
let _ = MADE.try_with(|made| made.set(made.get() + 1));
}
}
/// Run `f` with allocations counted.
pub fn counting<T>(f: impl FnOnce() -> T) -> T {
ON.store(true, Ordering::Relaxed);
let out = f();
ON.store(false, Ordering::Relaxed);
out
}
/// The allocations this thread made so far.
pub fn this_thread() -> u64 {
MADE.try_with(Cell::get).unwrap_or(0)
}
// SAFETY: every call goes to the system allocator as it came.
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
note();
// SAFETY: the caller's contract, passed on.
unsafe { System.alloc(layout) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
note();
// SAFETY: as `alloc`.
unsafe { System.alloc_zeroed(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
// SAFETY: as `alloc`.
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
note();
// SAFETY: as `alloc`.
unsafe { System.realloc(ptr, layout, new_size) }
}
}