diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 300400f42..20020d9da 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -481,10 +481,11 @@ A consumer that maps the pool's memory itself, into a guest for instance, passes `DeviceOptions::block_mappings`. Its hooks run per allocation, not per block: - `BlockMappings::map(allocation)` runs once for every allocation of the pool, before any block of - it goes out, with the allocation's number, class, length, device address and host mapping. It - runs where the allocation is made: in `Device::new` for each class's first, on the block-maker - thread for one made ahead of need (`Metrics::allocations_mapped`), and inside the take, on the - consumer's thread, for a take that no free range fits (`Metrics::allocations_mapped_on_takes`). + it goes out, with the allocation's number, class, length, device address and host mapping, and + the host time its make began with the make's length (`make_start`, `make_nanos`). It runs where + the allocation is made: in `Device::new` for each class's first, on the block-maker thread for + one made ahead of need (`Metrics::allocations_mapped`), and inside the take, on the consumer's + thread, for a take that no free range fits (`Metrics::allocations_mapped_on_takes`). It answers false where the consumer has no place for the allocation: the runtime frees it and hands out none of its blocks, and a take that made it returns `Limit::AllocationNotMapped` (`Metrics::allocation_map_refusals`). diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs index 78b15e1a8..931c7cc44 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs @@ -284,6 +284,11 @@ pub struct AllocationInfo { /// The host mapping of its first byte, which lives as long as the allocation, for a Shared /// allocation; `None` for a Private one, which has no host mapping. pub contents: Option>, + /// The host time its make began ([`host_time`](crate::host_time)). + pub make_start: u64, + /// The make's length in nanoseconds, from [`make_start`](Self::make_start) until the + /// allocation was made, before [`BlockMappings::map`] is called. + pub make_nanos: u64, } // SAFETY: plain data, as `BlockInfo`. @@ -312,7 +317,8 @@ pub trait BlockMappings: Send + Sync { /// ahead of need, on the thread in [`Device::new`](crate::Device::new) for each class's first, /// and inside [`Device::take_block`](crate::Device::take_block), on the consumer's thread, for /// a take that no free range fits. Never under the pool's lock. It makes no call into the - /// runtime and drops no runtime handle. + /// runtime and drops no runtime handle. `allocation` states when the make began and how long + /// it took ([`AllocationInfo::make_start`], [`AllocationInfo::make_nanos`]). fn map(&self, allocation: &AllocationInfo) -> bool; /// Returns only once every mapping of `allocation` the consumer made is gone and none of its @@ -611,6 +617,9 @@ struct Allocation { idle_since: u64, /// What its free waits for, noted when it became idle. wait: Wait, + /// The host time its make began and the make's length in nanoseconds + /// ([`AllocationInfo::make_start`]). + made: (u64, u64), /// A spilled allocation whose last block came back: no take lands in it, its bytes are not /// the class's free bytes, and the idle rule frees it next, so the next allocation tries /// device-local memory again. @@ -1178,6 +1187,7 @@ impl Pool { let started = crate::host_time(); let mut allocation = make_allocation(core, class, len.max(ALLOCATION_LEN), Some(site))?; let made = crate::host_time(); + allocation.made = (started, made.saturating_sub(started)); let bytes = allocation.len; (self.counters).timed(Part::TakeMake, bytes, made.saturating_sub(started), true); let mapped = self.map(&allocation, class, true); @@ -1574,6 +1584,8 @@ impl Allocation { len: self.len, gpu_address: self.address, contents: self.mapping.map(Mapping::get), + make_start: self.made.0, + make_nanos: self.made.1, } } } @@ -1630,13 +1642,9 @@ pub(crate) fn buffer_alignment(device: &ash::Device) -> u64 { /// `None` where it was not made or not mapped. fn ahead(core: &Core, class: MemoryClass) -> Option { let started = crate::host_time(); - let allocation = make_allocation(core, class, ALLOCATION_LEN, None).ok()?; - (core.counters).timed( - Part::MakerMake, - allocation.len, - crate::host_time() - started, - true, - ); + let mut allocation = make_allocation(core, class, ALLOCATION_LEN, None).ok()?; + allocation.made = (started, crate::host_time() - started); + (core.counters).timed(Part::MakerMake, allocation.len, allocation.made.1, true); if !core.pool.map(&allocation, class, false) { free_allocation(core, allocation); return None; @@ -2021,6 +2029,7 @@ fn make_allocation( placed: 0, texture: false, idle_since: crate::host_time(), + made: (0, 0), wait: Wait::Progress(0), retiring: false, reused: false, diff --git a/crates/metal2vulkan-runtime/src/tests/memory.rs b/crates/metal2vulkan-runtime/src/tests/memory.rs index 5ae2e583e..36e967458 100644 --- a/crates/metal2vulkan-runtime/src/tests/memory.rs +++ b/crates/metal2vulkan-runtime/src/tests/memory.rs @@ -155,6 +155,8 @@ mod on_device { gpu_address: u64, contents: Option, thread: Option, + /// Whether its make began no later than the map, and took time. + timed: bool, } #[derive(Clone, Debug)] @@ -176,6 +178,7 @@ mod on_device { gpu_address: allocation.gpu_address, contents: allocation.contents.map(|c| c.as_ptr() as usize), thread: thread::current().name().map(str::to_string), + timed: allocation.make_start <= crate::host_time() && allocation.make_nanos > 0, }); true } @@ -216,6 +219,10 @@ mod on_device { panic!("{name}: block {:?} of an allocation not mapped", block.id) }); assert_eq!(allocation.class, block.class, "{name}"); + assert!( + allocation.timed, + "{name}: {allocation:?}'s make, before its map" + ); assert!( block.offset + block.len <= allocation.len, "{name}: block {:?} at {} of {} bytes in {} bytes", diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 5e7a44999..5eb630b8f 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -1297,6 +1297,8 @@ field metal2vulkan-runtime::AllocationInfo.contents pub contents: Option>