diff --git a/crates/metal2vulkan-runtime/src/host_ram.rs b/crates/metal2vulkan-runtime/src/host_ram.rs index 8d6af4c79..42e63000f 100644 --- a/crates/metal2vulkan-runtime/src/host_ram.rs +++ b/crates/metal2vulkan-runtime/src/host_ram.rs @@ -78,7 +78,7 @@ //! past the objects, and never goes back: the RAM the objects never reach stays as the guest //! left it, not resident nor pinned for the device. Before the window's next chunk it imports //! each chunk a copy staged through the CPU took while it was not imported asked for -//! ([`RamOnDevice::ask_for_copy`], `copy.rs`), wherever it lies: so a copy over scattered +//! ([`RamOnDevice::take_for_copies`], `copy.rs`), wherever it lies: so a copy over scattered //! chunks, which moves the window to each in turn, finds them all imported by its next turn. //! Counted: `WorkCounts::host_ram_chunks_imported_ahead`. //! - **An object over a chunk not imported yet** (outside the window, or the one the thread is @@ -595,6 +595,9 @@ struct Wake { /// The use ended (`HostRamUse`'s drop, which then waits for the thread): the thread ends at /// its next look, and a [`fault_in`] under way stops before its next step. ended: AtomicBool, + /// The wakes so far ([`Wake::notify`]), for the tests. + #[cfg(test)] + notified: std::sync::atomic::AtomicU64, } /// Clears [`Wake::running`] as the device's thread ends, however it ends. @@ -609,6 +612,8 @@ impl Drop for Ends { impl Wake { /// Wake the thread, or have its next wait return at once. fn notify(&self) { + #[cfg(test)] + self.notified.fetch_add(1, Ordering::Relaxed); *lock(&self.pending) = true; self.condvar.notify_one(); } @@ -652,7 +657,7 @@ struct ChunkSlot { refused: bool, /// A copy over the chunk found it not imported and imported its ranges alone, on a device /// whose plan is [`Plan::listed`] ([`RamOnDevice::run_alone`]), or was staged through the CPU - /// while it was not imported ([`RamOnDevice::ask_for_copy`], any other plan): the device's thread + /// while it was not imported ([`RamOnDevice::take_for_copies`], any other plan): the device's thread /// imports it next ([`RamOnDevice::next_asked`]). Changed inside [`ChunkSlot::import`]'s lock. asked: AtomicBool, /// The device's thread imported the chunk and no object took it since: given back once the @@ -1290,6 +1295,8 @@ impl RamOnDevice { left = rest; } } + // The device's thread's window moved past every chunk left, its thread woken once. + self.take_for_copies(&left, false); for slot in left { let (imports, waited) = self.for_copy(device, importer, slot, &ranges[&slot])?; made.waited += waited; @@ -1302,7 +1309,8 @@ impl RamOnDevice { /// The imports a copy over chunk `slot` reads or writes its bytes `ranges` (offsets in the /// chunk, by start, apart) through, never waiting for the chunk's, the device's thread's - /// window moved to the chunk as an object's take moves it: the chunk's import where it is + /// window moved past it already ([`RamOnDevice::copy_imports`]'s + /// [`RamOnDevice::take_for_copies`], as an object's take moves it): the chunk's import where it is /// made; else, where runs are imported alone ([`RamOnDevice::runs_alone`]), the device's thread /// runs and has not failed the chunk, its allocations leave the margin the thread leaves, its /// barriers order blits through two imports of the same pages, and the ranges (those less than @@ -1322,9 +1330,6 @@ impl RamOnDevice { ranges: &[Range], ) -> Result<(Vec>, Duration), Error> { let span = &self.slots[slot]; - if !self.plan.listed && self.take(slot) { - self.wake.notify(); - } if let Some(chunk) = self.made(slot) { return Ok((vec![chunk], Duration::ZERO)); } @@ -1382,7 +1387,7 @@ impl RamOnDevice { /// import of it will be made, and a copy Metal takes is never refused for when it came; so is /// one held back by objects' own imports of its pages ([`RamOnDevice::held_back`]'s /// [`Held::Back`]), whose import waits for them to go. The device's thread's window is not - /// moved: [`RamOnDevice::take_for_copy`] moves it. + /// moved: [`RamOnDevice::take_for_copies`] moves it. /// /// Why not alone. An import of a copy's ranges pins every page between them that lies /// less than 2 MiB from the next ([`copy::alone_ranges`]), on the encoding thread: a cache of @@ -1431,28 +1436,44 @@ impl RamOnDevice { } } - /// A copy takes chunk `slot` as [`RamOnDevice::for_copy`] takes it, without its import: the - /// device's thread's window moves to it (its thread woken where that moved it), on a device - /// whose plan is not [`Plan::listed`]. - pub(crate) fn take_for_copy(&self, slot: usize) { - if !self.plan.listed && self.take(slot) { + /// A copy takes the chunks `slots`, in order, without their imports: the device's thread's + /// window moves as a take of each in turn moves it ([`RamOnDevice::take`]), under one lock, on + /// a device whose plan is not [`Plan::listed`]; and, where `ask` (a copy staged through the + /// CPU, [`CopyTake::ThroughCpu`]), each not imported is asked of the thread, which imports it + /// before its window's next ([`RamOnDevice::next_ahead`]), so that the copies after it read + /// the chunk's import, wherever it lies. The thread is woken once, after them all, where the + /// window moved or a chunk was asked for, and not where neither was: a copy over a hundred + /// chunks wakes it once, not once a chunk (each wake of a parked thread costing the encoding + /// thread microseconds). + pub(crate) fn take_for_copies(&self, slots: &[usize], ask: bool) { + let mut wake = false; + if !self.plan.listed { + let mut window = lock(&self.window); + for &slot in slots { + if window.taken != Some(slot) { + *window = window.after_take(slot); + wake = true; + } + } + } + if ask { + for &slot in slots { + let span = &self.slots[slot]; + let import = lock(&span.import); + if matches!(*import, Import::None) && !span.asked.swap(true, Ordering::Relaxed) { + wake = true; + } + } + } + if wake { self.wake.notify(); } } - /// A copy staged through the CPU took chunk `slot` while it was not imported - /// ([`CopyTake::ThroughCpu`]): the device's thread imports it before its window's next - /// ([`RamOnDevice::next_ahead`]), woken for it, so that the copies after it read the chunk's - /// import, wherever it lies. - pub(crate) fn ask_for_copy(&self, slot: usize) { - let span = &self.slots[slot]; - let ask = { - let import = lock(&span.import); - matches!(*import, Import::None) && !span.asked.swap(true, Ordering::Relaxed) - }; - if ask { - self.wake.notify(); - } + /// The wakes of the device's thread so far, for the tests. + #[cfg(test)] + pub(crate) fn wakes(&self) -> u64 { + self.wake.notified.load(Ordering::Relaxed) } /// A copy into chunk `slot`'s bytes was recorded. @@ -1675,7 +1696,7 @@ impl RamOnDevice { } /// The chunk the device's thread imports next: the first a staged copy asked for - /// ([`RamOnDevice::ask_for_copy`], as [`RamOnDevice::next_asked`] finds it), so that every + /// ([`RamOnDevice::take_for_copies`], as [`RamOnDevice::next_asked`] finds it), so that every /// chunk such a copy took is imported, however far apart they lie; else the first of the /// window ([`Window::slots`]), within the RAM, that is neither imported nor failed by that /// thread, nor being imported now ([`Import::Making`]: by an object, which imports it @@ -2387,6 +2408,12 @@ pub(crate) struct RamSource { } impl RamSource { + /// The wakes of the device's threads of every RAM the device uses so far, for the tests. + #[cfg(test)] + pub(crate) fn wakes(&self) -> u64 { + lock(&self.rams).iter().map(|ram| ram.wakes()).sum() + } + /// Add `ram`, unless one of its ranges meets a range of a RAM the device uses. fn add(&self, ram: &Arc, plan: Plan) -> Option> { let mut rams = lock(&self.rams); @@ -3481,23 +3508,64 @@ mod tests { assert_eq!(last.next_ahead(), Some(full + 2), "the chunk taken"); done(&last, full + 2); assert_eq!(last.next_ahead(), None, "nothing past the RAM's end"); - // A staged copy's asks come first, each once, wherever the window lies. + // A staged copy's asks come first, each once, wherever the window lies: the copy's takes + // of chunks 1 and 3 leave it past the jump to 3 (chunk 4), and chunk 1 lies outside it. let asked = RamOnDevice::new(&ram, Plan::AHEAD); assert!(asked.take(full + 2)); - asked.ask_for_copy(1); - asked.ask_for_copy(3); + asked.take_for_copies(&[1, 3], true); assert_eq!(asked.next_ahead(), Some(1), "the first asked for"); done(&asked, 1); assert_eq!(asked.next_ahead(), Some(3)); done(&asked, 3); - assert_eq!(asked.next_ahead(), Some(full + 2), "then the window"); - done(&asked, full + 2); + assert_eq!(asked.next_ahead(), Some(4), "then the window"); + done(&asked, 4); assert_eq!(asked.next_ahead(), None); drop((on, fresh, last, asked, ram)); // SAFETY: the test's mapping, which nothing uses any more. unsafe { unmap_untouched(at, length) }; } + /// A copy's takes of its chunks move the device's thread's window as a take of each in turn + /// does, and wake the thread once, after them all: not once a chunk, and not at all where + /// the window moves nowhere and no chunk is asked for. A staged copy's asks are made in the + /// same wake. + #[test] + fn a_copy_s_takes_move_the_window_as_each_take_would_and_wake_the_thread_once() { + let length = (AHEAD_CHUNKS as u64 + 3) * CHUNK_BYTES; + let at = untouched_pages(length); + // SAFETY: the mapping is readable and writable until the end of the test, and nothing + // imports it. + let ram = Arc::new(unsafe { HostRam::new(&[(at, length)], None) }.unwrap()); + let each = RamOnDevice::new(&ram, Plan::AHEAD); + let all = RamOnDevice::new(&ram, Plan::AHEAD); + // Five chunks a copy touches, in its order: a run up, then a jump. + let slots = [0, 1, 2, 3, AHEAD_CHUNKS + 2]; + for &slot in &slots { + each.take(slot); + } + all.take_for_copies(&slots, false); + assert_eq!( + *lock(&all.window), + *lock(&each.window), + "the window of a take of each" + ); + assert_eq!(all.wakes(), 1, "one wake for the copy"); + // The chunk last taken again, nothing asked: no wake. + all.take_for_copies(&[AHEAD_CHUNKS + 2], false); + assert_eq!(all.wakes(), 1, "nothing moved, nothing asked"); + // A staged copy over chunks not imported, its window moved and its chunks asked for: one + // wake; asked again, and the window where it is: none. + all.take_for_copies(&[1, 3], true); + assert_eq!(all.wakes(), 2); + assert!(all.slots[1].asked.load(Ordering::Relaxed)); + assert!(all.slots[3].asked.load(Ordering::Relaxed)); + all.take_for_copies(&[3], true); + assert_eq!(all.wakes(), 2, "asked before, window on the chunk"); + drop((each, all, ram)); + // SAFETY: the test's mapping, which nothing uses any more. + unsafe { unmap_untouched(at, length) }; + } + #[test] fn a_listed_ram_s_thread_imports_the_chunks_asked_for_and_only_those() { // Four long chunks of address space mapped and never touched (no device imports them). diff --git a/crates/metal2vulkan-runtime/src/host_ram/copy.rs b/crates/metal2vulkan-runtime/src/host_ram/copy.rs index f2ad488ae..31dfec3e1 100644 --- a/crates/metal2vulkan-runtime/src/host_ram/copy.rs +++ b/crates/metal2vulkan-runtime/src/host_ram/copy.rs @@ -43,7 +43,7 @@ //! video clips at once took 1.4 to 2.1 s while chunks were being imported. No import is made or //! waited for, and no page pinned for it; each chunk is taken as an object takes it, and asked //! of the device's thread, which imports it before its window's next -//! (`RamOnDevice::ask_for_copy`), so the copies after it go through the chunks' imports. A +//! (`RamOnDevice::take_for_copies`), so the copies after it go through the chunks' imports. A //! copy over a chunk that takes no import while it runs is staged always, whatever else it //! touches, whether the device's thread tried the chunk or not, so that a copy Metal takes is //! never refused for when it came (`CopyTake::CpuOnly`): a chunk the device refuses whatever it @@ -1528,7 +1528,7 @@ impl BlitCommandEncoder { /// writes. One [`Staging::after_the_gpu`] starts a part of its own there, whose host step waits /// for the work before it to complete, and its uses are noted again at that part's start, /// where its copy lies. Each chunk is taken as an object over it takes it, and asked of the - /// device's thread where it is not imported ([`RamOnDevice::ask_for_copy`]). A buffer detached + /// device's thread where it is not imported ([`RamOnDevice::take_for_copies`]). A buffer detached /// since records nothing, as a command over it does. fn record_staged( &mut self, @@ -1577,10 +1577,8 @@ impl BlitCommandEncoder { if staging.after_the_gpu { self.core().note_left_uses(TRANSFER, &uses); } - for slot in touched.keys() { - ram.take_for_copy(*slot); - ram.ask_for_copy(*slot); - } + let slots: Vec = touched.keys().copied().collect(); + ram.take_for_copies(&slots, true); let counts = &device.shared().counts; counts.host_ram_copied(total, 0); counts.host_ram_copy_staged(); @@ -1593,7 +1591,7 @@ impl BlitCommandEncoder { /// place in the encoder, its read of the buffer noted there, and the CPU's copy into the /// pages at the start of a part of its own after it, its writes of the chunks' bytes noted /// there, by their keys. Each chunk is taken as an object over it takes it, and asked of the - /// device's thread where it is not imported ([`RamOnDevice::ask_for_copy`]). A buffer + /// device's thread where it is not imported ([`RamOnDevice::take_for_copies`]). A buffer /// detached since records nothing, as a command over it does. fn record_staged_scatter( &mut self, @@ -1643,10 +1641,8 @@ impl BlitCommandEncoder { } } self.core().note_left_uses(TRANSFER, &writes); - for slot in touched.keys() { - ram.take_for_copy(*slot); - ram.ask_for_copy(*slot); - } + let slots: Vec = touched.keys().copied().collect(); + ram.take_for_copies(&slots, true); let counts = &device.shared().counts; counts.host_ram_copied(total, 0); counts.host_ram_scatter_staged(); diff --git a/crates/metal2vulkan-runtime/src/tests/host_ram_copy_device.rs b/crates/metal2vulkan-runtime/src/tests/host_ram_copy_device.rs index 4de180c8e..ba56c14d7 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_ram_copy_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_ram_copy_device.rs @@ -1980,6 +1980,60 @@ fn a_first_scatter_over_chunks_the_thread_imports_is_in_its_pages_once_it_comple drop((over, used, ram)); } +/// A copy over chunks the device's thread imported, three of them, wakes the thread once, not +/// once a chunk (`RamOnDevice::take_for_copies`), and so does the staged copy before it, over +/// those chunks not imported yet, which asked for them. +#[test] +fn a_copy_over_several_chunks_wakes_the_device_s_thread_once() { + let Some((device, used, ram, start, before)) = ahead_ram() else { + return; + }; + let ahead = |counts: &WorkCounts| { + counts.host_ram_chunks_imported_ahead - before.host_ram_chunks_imported_ahead + }; + let wakes = || device.shared().host_ram.wakes(); + // A page in each of chunks 1, 2 and 3. + let runs: Vec> = (1..4) + .map(|chunk| { + let page = start + chunk * 64 * MIB + 5 * PAGE; + page..page + PAGE + }) + .collect(); + let total: u64 = runs.iter().map(|run| run.end - run.start).sum(); + let source = shared(&device, total, |at| pattern(at, 15)); + let staging = wakes(); + blit(&device, |encoder| { + encoder.copy_to_host_ram(&source, 0, &ram, &runs).unwrap(); + }); + let staged = wakes() - staging; + let imported = settled(&device, |counts| ahead(counts) == 4); + assert_eq!( + ahead(&imported), + 4, + "the start's chunk and the three asked for" + ); + let copying = wakes(); + blit(&device, |encoder| { + encoder.copy_to_host_ram(&source, 0, &ram, &runs).unwrap(); + }); + let copied = wakes() - copying; + let last = device.work_counts(); + eprintln!( + "HOST-RAM-COPY-WAKES {}: the staged copy woke the thread {staged} times, the copy through \ + the imports {copied}", + device.name() + ); + assert_eq!( + last.host_ram_scatters_staged - imported.host_ram_scatters_staged, + 0, + "the second copy went through the imports" + ); + assert_eq!(staged, 1, "the staged copy's takes and asks"); + assert_eq!(copied, 1, "the copy's takes of its three chunks"); + assert!(gathered(&runs) == contents(&source), "the scattered pages"); + drop((used, ram)); +} + /// A scatter of few ranges over chunks not imported yet, which an import of its ranges alone /// could take (fewer than `copy::MOST_ALONE`, none the whole chunk), is staged as one of many /// is: its encoding imports nothing, and its pages hold its bytes once it completed. The chunks diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index cd054eda8..918727a61 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 = 2352; +pub const RUNTIME_VERSION: u32 = 2353; /// 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