diff --git a/crates/metal2vulkan-runtime/src/host_ram.rs b/crates/metal2vulkan-runtime/src/host_ram.rs index 8d6a4388e..8d6af4c79 100644 --- a/crates/metal2vulkan-runtime/src/host_ram.rs +++ b/crates/metal2vulkan-runtime/src/host_ram.rs @@ -951,10 +951,12 @@ impl RamOnDevice { /// [`Import::Making`], so an object over the chunk the device's thread (or another object) /// is importing waits for that import. A failed import is not kept: the next object over the /// chunk tries again. An object's call moves the device's thread's window to this chunk - /// ([`RamOnDevice::take`]), and wakes the thread where that moved it, on a device that - /// imports ahead (not [`Plan::listed`]). An import ahead is counted once the chunk is in its - /// slot, so that the count of imports ahead never runs before the chunks an object finds - /// there. + /// ([`RamOnDevice::took`]) once the object holds the chunk: found made, marked in flight by + /// the object itself, or held back. The window's first chunk is the one taken, so a thread + /// woken before the object marked it in flight could import it in the object's place, the + /// object then waiting for that import at the thread's pace and the chunk counted as + /// imported ahead. An import ahead is counted once the chunk is in its slot, so that the + /// count of imports ahead never runs before the chunks an object finds there. fn chunk( &self, device: &Device, @@ -962,8 +964,12 @@ impl RamOnDevice { slot: usize, by: By, ) -> Result, Error> { - if by == By::Object && !self.plan.listed && self.take(slot) { - self.wake.notify(); + // A test's pause of an object's call, before it holds the chunk. + #[cfg(test)] + let pause = lock(&device.shared().host_ram.taking_pause).clone(); + #[cfg(test)] + if let Some(pause) = pause.filter(|_| by == By::Object) { + pause(self.slots[slot].span.clone()); } let span = &self.slots[slot]; let mut import = lock(&span.import); @@ -973,7 +979,12 @@ impl RamOnDevice { let claims = loop { loop { match &*import { - Import::Made(chunk) => return Ok(Arc::clone(chunk)), + Import::Made(chunk) => { + let chunk = Arc::clone(chunk); + drop(import); + self.took(by, slot); + return Ok(chunk); + } Import::Making => { import = (span.made.wait(import)).unwrap_or_else(PoisonError::into_inner); } @@ -992,6 +1003,8 @@ impl RamOnDevice { }; let beside = match self.held_back(device, importer, slot, &claims) { Held::Back => { + drop((import, claims)); + self.took(by, slot); return Err(crate::error::internal( "a guest RAM chunk a live import of an object's own holds pages of", )); @@ -1006,6 +1019,7 @@ impl RamOnDevice { slot: span, ended: false, }; + self.took(by, slot); // A test's pause of the import, marked in flight, before it is made. #[cfg(test)] let pause = lock(&device.shared().host_ram.making_pause).clone(); @@ -1640,6 +1654,15 @@ impl RamOnDevice { gone } + /// An object's call over chunk `slot` ([`RamOnDevice::chunk`]) holds it now, made or in + /// flight: the window moves to it ([`RamOnDevice::take`]), and the device's thread is woken + /// where that moved it, on a device that imports ahead (not [`Plan::listed`]). + fn took(&self, by: By, slot: usize) { + if by == By::Object && !self.plan.listed && self.take(slot) { + self.wake.notify(); + } + } + /// An object took chunk `slot`: the window moves to it ([`Window::after_take`]) unless it is /// the chunk last taken. Whether it moved. fn take(&self, slot: usize) -> bool { @@ -2330,7 +2353,8 @@ fn place_runs( Ok((run.end <= spans[slot].end).then_some(slot)) } -/// A test's pause in a chunk's import ([`RamSource::making_pause`]), given the chunk's span. +/// A test's pause in a chunk's import ([`RamSource::making_pause`]) or an object's take of it +/// ([`RamSource::taking_pause`]), given the chunk's span. #[cfg(test)] pub(crate) type MakingPause = Arc) + Send + Sync>; @@ -2350,6 +2374,10 @@ pub(crate) struct RamSource { /// marked in flight (`Import::Making`) and before it is made ([`RamOnDevice::chunk`]). #[cfg(test)] pub(crate) making_pause: Mutex>, + /// The tests' pause in an object's call for a chunk's import, called with the chunk's span + /// before the object holds the chunk ([`RamOnDevice::chunk`]). + #[cfg(test)] + pub(crate) taking_pause: Mutex>, /// Whether the driver refused a dma-buf a RAM's source made of a span or a list of its pages /// for a cause that lasts (`Device::ram_span_as_dma_buf`, `ram_runs_as_dma_buf`): from then /// on, for the device's life, no span or list asks a source, and no RAM's plan is diff --git a/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs b/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs index 6aef77c2e..5629c0968 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs @@ -39,7 +39,9 @@ //! back for a chunk a jump skipped. A run of heaps that stays inside the window imports nothing //! on the thread making them; a heap outside it imports its own chunk. An object over a chunk //! the thread imported, made while the chunk's state is looked at (as the thread looks at each -//! chunk of its window), is bound in the chunk. +//! chunk of its window), is bound in the chunk. An object over a chunk outside the window moves +//! the window only once it holds the chunk: the thread never imports that chunk in its place, +//! however long the object takes to reach it, and imports the one past it. //! - **Down the RAM.** A heap over a chunk not imported, at the RAM's top, imports the chunk, in //! which the heaps over it after that are bound, and its bytes go from the CPU to the GPU and //! back; a heap next to it below imports its chunk and opens the window below, so a run of heaps @@ -991,6 +993,61 @@ fn the_window_ahead_moves_with_the_objects_and_imports_no_more() { ); } +/// An object over a chunk outside the window, held before it holds the chunk +/// (`RamSource::taking_pause`) for longer than the device's thread takes to start a chunk's +/// import, imports the chunk itself: the window moves to the chunk only once the object marked +/// its import in flight, so the thread, woken by the move, finds it in flight and imports the one +/// past it alone. Were the window moved before, the thread would import the object's chunk +/// meanwhile (counted ahead), and the object wait for that import. +#[test] +fn an_object_holds_its_chunk_before_the_window_moves_to_it() { + let Some(device) = ahead_device() else { + return; + }; + let (ram, start, dropped) = fresh_ram(&device, 4 * 64 * MIB); + let before = device.work_counts(); + let used = device.use_host_ram(&ram).unwrap(); + let ahead = |counts: &crate::WorkCounts| { + counts.host_ram_chunks_imported_ahead - before.host_ram_chunks_imported_ahead + }; + let by_objects = |counts: &crate::WorkCounts| { + counts.host_ram_chunks_imported - before.host_ram_chunks_imported - ahead(counts) + }; + settled(&device, |counts| ahead(counts) == 1); + let page = device.shared().host_import.as_ref().unwrap().page; + // The object's call held for as long as `settled` gives the thread to show an import. + *device.shared().host_ram.taking_pause.lock().unwrap() = Some(Arc::new(|_span| { + std::thread::sleep(Duration::from_millis(300)) + })); + let (runs, gates): (Arc>, Arc>) = Default::default(); + let in_chunk = buffer( + &device, + start + 2 * 64 * MIB + 8 * page, + 4 * page, + resolver(&runs, itself), + gate(&gates, true), + ) + .unwrap(); + *device.shared().host_ram.taking_pause.lock().unwrap() = None; + assert_eq!( + by_objects(&device.work_counts()), + 1, + "the object imported its chunk, the thread not in its place" + ); + let made = settled(&device, |counts| ahead(counts) == 2); + assert_eq!( + (ahead(&made), by_objects(&made)), + (2, 1), + "the thread imported the chunk past the object's alone" + ); + assert_eq!(in_chunk.host_backing(), Some(HostBacking::InHostRam)); + drop((in_chunk, used, ram)); + assert!( + settles(&dropped), + "the keep-alive goes after the last chunk" + ); +} + /// On a device that refuses pointers to shared memory (`Device::imports_shared_host_pointers` /// false: RADV, whose amdgpu userptr takes private anonymous pages alone), a RAM of shared memory /// (a `memfd` mapped shared, as QEMU's `memory-backend-memfd,share=on` RAM is) imports no chunk, diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 6d85c0115..cd054eda8 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -94,7 +94,7 @@ pub const ARTIFACT_FORMAT_VERSION: u32 = 523; /// changes no behaviour, and then updates that hash alone with the commit saying why, or changes /// behaviour and also bumps this version. The commit that bumps it says what changed; this comment /// keeps no history. -pub const RUNTIME_VERSION: u32 = 2351; +pub const RUNTIME_VERSION: u32 = 2352; /// The digest of the sources this build of the crate was compiled from: SHA-256, as lowercase hex, /// of the manifest, the build script, and exactly the files the crate roots' module tree compiles