From 198cf51e5a99fcb04fd45b5665c893153adfdeb5 Mon Sep 17 00:00:00 2001 From: Anees Iqbal Date: Sun, 22 Nov 2026 02:33:17 -0800 Subject: [PATCH] rt: AllocationInfo states when each allocation's make began and how long it took For the consumer: AllocationInfo, the value BlockMappings::map receives, gains two fields. make_start is the host time (host_time) at which the allocation's make began. make_nanos is the make's length in nanoseconds, ending before the hook is called. The change is additive: AllocationInfo is #[non_exhaustive] and stays Clone + Copy. Why: the hook runs after the make. A consumer that sees a slow guest answer could tell only that a make ended during it, not whether the make covered it. How: both make sites already read both ends of the make for their timed parts. The pair is now kept on the pool's Allocation (made), set right after make_allocation returns, and Allocation::info passes it on: - The take's own allocation, memory.rs own(), timed as Part::TakeMake. - The block-maker's allocation made ahead of need, memory.rs ahead(), timed as Part::MakerMake. Device::new's first allocation of each class also comes through ahead() (stock()). make_allocation has no other caller. The values are the same ones the timed parts take. Docs: the two fields' docs (no Metal counterpart, as the struct says), BlockMappings::map's doc, and the runtime README's sentence on BlockMappings::map. The signatures table is regenerated. Test: the hook in the runtime's memory tests records, at each map, whether make_start is no later than host_time() at the map and make_nanos is not zero. assert_mapped asserts it for each block's allocation it checks. It runs in every_device_hands_out_blocks_and_buffers_and_frees_them_through_the_hook (each class's first allocations from Device::new, and a long take's own allocation) and in every_device_maps_each_allocation_before_its_blocks_go_out_and_refuses_one_not_mapped. Verified (tree 4204b286a4, the same tree as this commit): compile job m3-max-66884. The signatures and refusal-surface checks, fmt, clippy (plain, --cfg m2v_gpu, --cfg m2v_cpu_icd), workspace Rustdoc -D warnings and scripts/api-docs all exit 0. The runtime suite on lavapipe: 642 tests run, 642 passed, 3 skipped. The job's targeted run lists every_device_maps_each_allocation_before_its_blocks_go_out_and_refuses_one_not_mapped as passed on lavapipe; the job's log keeps no per-test lines for the full suite. --- crates/metal2vulkan-runtime/README.md | 9 ++++--- crates/metal2vulkan-runtime/src/memory.rs | 25 +++++++++++++------ .../metal2vulkan-runtime/src/tests/memory.rs | 7 ++++++ crates/metal2vulkan/tests/api_signatures.tsv | 2 ++ 4 files changed, 31 insertions(+), 12 deletions(-) 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>