diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 0bfa801ef..79018d500 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -2588,8 +2588,11 @@ made, with no lock, and never rewritten for layouts. `Device::quiesce(timeout)` is for a process that must end while a handle of the device is still out. With every wait bounded by the timeout, it stops the submit thread (ready work is submitted, held work ends `NotRun`) and the block-maker, waits for the GPU to complete what was submitted, lets the -completion thread retire it and end, and then runs `BlockMappings::unmap` on the calling thread for -every allocation still mapped. It frees nothing: every block's memory and mapping stays valid until +completion thread retire it and end, lets the reaper write the pipeline cache's blob where the +cache changed since its last write and end, and then runs `BlockMappings::unmap` on the calling +thread for every allocation still mapped. The reaper's write waits for a pipeline creation still +running until the same deadline, and past it is skipped (`Metrics::pipeline_cache_writes_skipped`); +a write begun before the deadline runs to its end, and where it ends past it quiesce answers false. It frees nothing: every block's memory and mapping stays valid until the process ends. After it, no runtime thread touches a block; later calls that need a thread return `Error::Quiesced`. It answers true once the GPU went idle and every thread ended in time, and false at once in a handler or a listener, whose thread cannot wait for itself. A second call @@ -3536,8 +3539,10 @@ finds a creation begun since creation was quiet is skipped until creation is qui (`Metrics::pipeline_cache_looks_skipped`), and a creation that begins during a look waits for that look alone (`Metrics::pipeline_cache_lock_waits` counts those waits, `pipeline_cache_lock_wait_nanos` their time and `pipeline_cache_lock_wait_max_nanos` the longest). -The device's drop writes the blob once more where the size changed at all. A crash between writes -loses only the pipelines made since the last; no creation waits for a write. +The device's drop writes the blob once more where the size changed at all, and so does a quiesce +before it, so a process that ends with `_exit` after a quiesce keeps what it made (Quiesce and +exit). A crash between writes loses only the pipelines made since the last; no creation waits for +a write. The cache holds at most 128 MiB when the reaper looks: one long recorded session's 448 pipelines write 62.6 MB of data on KosmicKrisp (the Mac host's driver) and 1.5-11.1 MB on NVIDIA's, RADV's diff --git a/crates/metal2vulkan-runtime/src/cache.rs b/crates/metal2vulkan-runtime/src/cache.rs index c713432eb..4e85b5f5d 100644 --- a/crates/metal2vulkan-runtime/src/cache.rs +++ b/crates/metal2vulkan-runtime/src/cache.rs @@ -22,9 +22,13 @@ //! appended to, once creation is quiet (no creation running and none for [`QUIET_NANOS`]) after a //! creation: at most once a quiet period, and only where the size the driver gives grew by more //! than an eighth since the last write, so a run that makes nothing new writes nothing and a -//! device's writes over its life come to a few times the blob's size. The device's drop writes it -//! once more where the size changed at all. A crash between writes loses only the pipelines made -//! since the last. No guest call waits for a write. +//! device's writes over its life come to a few times the blob's size. The reaper's end, at a +//! quiesce or at the device's teardown, writes it once more where the size changed at all, so a +//! process that ends after a quiesce, with no drop, keeps what it made: where a creation still +//! runs, it waits for it until the quiesce's deadline, past which it is skipped and counted +//! (`Metrics::pipeline_cache_writes_skipped`). The device's drop writes it where it changed since. +//! A crash between writes loses only the pipelines made since the last. No guest call waits for a +//! write. //! //! **Reads exclude creations.** Every creation holds a lock shared ([`Caches::creations`]) from //! before it takes a cache until it ends, and the reaper's look holds it exclusive around its @@ -137,7 +141,7 @@ use std::sync::{ Arc, Condvar, Mutex, OnceLock, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard, TryLockError, }; -use std::time::{Duration, SystemTime}; +use std::time::{Duration, Instant, SystemTime}; /// The cap of the lowerings in a cache directory where the consumer names none: half of it, what /// the store is sure to keep, is about seven times the 9.16 MB a long recorded session's 282 @@ -151,6 +155,10 @@ pub(crate) const PIPELINE_CACHE_BOUND: u64 = 128 << 20; /// How long pipeline creation stays quiet, no creation running and none ending, before the /// reaper looks at the cache: 20 ms, in nanoseconds of [`host_time`]. const QUIET_NANOS: u64 = 20_000_000; +/// How often the reaper's last write of the blob ([`Caches::last_write`]) tries again for +/// [`Caches::creations`] while a creation holds it. +const LAST_WRITE_STEP: Duration = Duration::from_millis(10); + /// The first bytes of a blob, before its data's length and checksum. const MAGIC: &[u8; 8] = b"m2vblob1"; /// The start of a device's blob's name, under the cache directory. @@ -1768,6 +1776,56 @@ impl Caches { } } + /// At the reaper's end, a quiesce's or the device's teardown: writes the blob where the + /// cache's size changed since the last write, so a process that ends after a quiesce, with no + /// drop, keeps what it made. A creation still running holds it: it tries for + /// [`Caches::creations`] each [`LAST_WRITE_STEP`] until `until`, past which the write is + /// skipped and counted (`Metrics::pipeline_cache_writes_skipped`), and with no `until` until + /// the creation ends. It waits for no GPU work: a creation compiles on the CPU, and reading a + /// cache's data waits for nothing the GPU does. + /// + /// # Safety + /// + /// `device` is the device of the caches. + pub(crate) unsafe fn last_write( + &self, + device: &ash::Device, + counters: &Counters, + until: Option, + ) { + let Some(path) = self.blob_to_write() else { + return; + }; + let alone = loop { + if let Some(alone) = self.alone() { + break alone; + } + if until.is_some_and(|until| Instant::now() >= until) { + counters.count(Metric::PipelineCacheWritesSkipped); + return; + } + std::thread::sleep(LAST_WRITE_STEP); + }; + let (current, written) = { + let pipelines = lock(&self.pipelines); + (pipelines.current.clone(), pipelines.written) + }; + let Some(current) = current else { + return; + }; + // SAFETY: the cache is the device's, and no creation runs while `alone` is held. + let size = unsafe { data_size(device, current.0) }; + if size == written { + return; + } + // SAFETY: as above. + let data = unsafe { device.get_pipeline_cache_data(current.0) }; + drop(alone); + if let Ok(data) = data { + self.write(path, &data, size, counters); + } + } + /// At the device's drop, no creation running: writes the blob where the cache's data changed /// since the last write; then destroys every cache. /// diff --git a/crates/metal2vulkan-runtime/src/counters.rs b/crates/metal2vulkan-runtime/src/counters.rs index d1b1559cf..6ef8c2f0d 100644 --- a/crates/metal2vulkan-runtime/src/counters.rs +++ b/crates/metal2vulkan-runtime/src/counters.rs @@ -3003,10 +3003,11 @@ pub struct Metrics { /// Compiled contents, compute or render, a state's make dropped because an equal one landed /// in the device's map of contents first; the state takes the map's. No Metal counterpart. pub kernel_makes_dropped: u64, - /// Writes of the pipeline cache's data over the device's blob skipped because the data was + /// Writes of the pipeline cache's data over the device's blob skipped: because the data was /// past the bound of 128 MiB, as at the device's teardown where the cache grew past it after - /// the reaper's last look: the blob keeps what it held, and a later device seeds from that. - /// Metal has no counterpart. + /// the reaper's last look, or because a creation still ran at a quiesce's deadline, which the + /// reaper's last write waits until ([`Device::quiesce`](crate::Device::quiesce)). The blob + /// keeps what it held, and a later device seeds from that. Metal has no counterpart. pub pipeline_cache_writes_skipped: u64, /// Bytes written to the cache directory ([`DeviceOptions::pipeline_cache_dir`]): lowered /// kernels, and the device's pipeline cache blob each time it is written: once creation is diff --git a/crates/metal2vulkan-runtime/src/device.rs b/crates/metal2vulkan-runtime/src/device.rs index d822b37e9..5f97d3cac 100644 --- a/crates/metal2vulkan-runtime/src/device.rs +++ b/crates/metal2vulkan-runtime/src/device.rs @@ -1181,8 +1181,13 @@ impl Device { /// every command buffer it holds or has not yet submitted with /// [`CommandBufferError::NotRun`](crate::CommandBufferError::NotRun), and ends; the /// block-maker thread ends; the GPU completes everything submitted; the completion thread - /// retires it, runs the completed handlers, and ends; then [`BlockMappings::unmap`] runs on - /// this thread for every allocation still mapped. After it, no runtime thread touches a block, + /// retires it, runs the completed handlers, and ends; the reaper thread writes the pipeline + /// cache's blob ([`DeviceOptions::pipeline_cache_dir`]) where the cache changed since its last + /// write, waiting for a pipeline creation still running until the same deadline, past which it + /// skips the write (`Metrics::pipeline_cache_writes_skipped`), and ends; then + /// [`BlockMappings::unmap`] runs on this thread for every allocation still mapped. A write + /// begun before the deadline runs to its end, and where it ends past it the call answers + /// false; cut short by the process's end, it leaves the blob as it was. After it, no runtime thread touches a block, /// unless the call answered false. A thread past the timeout ends alone: the completion /// thread once the GPU completed what was submitted and it retired that, running its /// completed handlers, so what the work held goes then. Such a thread is not joined here; the device's last handle joins @@ -1422,7 +1427,7 @@ fn halt( false => idle &= finish(complete, core, deadline, &mut left.complete), true => left.complete = complete, } - core.reaper.stop(); + core.reaper.stop(deadline); idle &= finish(reaper, core, deadline, &mut left.reaper); // The consumer's mappings go here, on the thread that stops the device, whichever thread // later drops the core's last reference and frees the memory. diff --git a/crates/metal2vulkan-runtime/src/reaper.rs b/crates/metal2vulkan-runtime/src/reaper.rs index ea73d85bd..d5f0d26c3 100644 --- a/crates/metal2vulkan-runtime/src/reaper.rs +++ b/crates/metal2vulkan-runtime/src/reaper.rs @@ -3,10 +3,11 @@ //! texture whose image an upload held past it (`crate::submit::Work::release`), runs a teardown //! that starts in a handler or a listener, writes the lowerings the pipeline states hand it to //! the store, and looks at the pipeline cache once creation is quiet, writing its blob or -//! replacing it past its bound (`crate::cache`). No driver free runs on the completion thread, -//! so no completion waits behind one. Device memory it does not free: a texture's allocation of -//! its own, a surface plane's import and a copy's staging buffer go to the block-maker thread, -//! whose form of every free a submission waits out at most one of (`crate::submit_calls`). A +//! replacing it past its bound (`crate::cache`), and writes the blob once more as it ends. No +//! driver free runs on the completion thread, so no completion waits behind one. Device memory it +//! does not free: a texture's allocation of its own, a surface plane's import and a copy's staging +//! buffer go to the block-maker thread, whose form of every free a submission waits out at most +//! one of (`crate::submit_calls`). A //! texture's or a sampler state's index, views, image or sampler released while work was in flight //! come from the block-maker thread once the command buffers committed before the release retired //! (`crate::memory::Pool::release`). @@ -21,6 +22,7 @@ use crate::times::host_time; use ash::vk; use std::collections::VecDeque; use std::sync::{Arc, Condvar, Mutex, PoisonError, Weak}; +use std::time::Instant; /// The reaper's queue. pub(crate) struct Reaper { @@ -32,6 +34,9 @@ struct State { jobs: VecDeque, /// The reaper ends once the queue is empty. stop: bool, + /// The deadline the stop gives the reaper's last write of the pipeline cache's blob + /// ([`Caches::last_write`](crate::cache::Caches::last_write)): a quiesce's; None for none. + until: Option, } pub(crate) enum Job { @@ -120,6 +125,7 @@ impl Reaper { state: Mutex::new(State { jobs: VecDeque::new(), stop: false, + until: None, }), changed: Condvar::new(), } @@ -144,9 +150,13 @@ impl Reaper { lock(&self.state).stop } - /// Tells the reaper to end once its queue is empty. - pub(crate) fn stop(&self) { - lock(&self.state).stop = true; + /// Tells the reaper to end once its queue is empty, its last write of the pipeline cache's + /// blob waiting for a creation until `until` at the latest. + pub(crate) fn stop(&self, until: Option) { + let mut state = lock(&self.state); + state.stop = true; + state.until = until; + drop(state); self.changed.notify_one(); } @@ -348,8 +358,9 @@ enum Next { Look, /// Write a lowering, refusal or record to the store. Write(String, Vec), - /// End: the reaper was told to, and its queue is empty. - End, + /// End: the reaper was told to, and its queue is empty; the pipeline cache's blob is written + /// last, by the stop's deadline. + End(Option), } /// The reaper thread, until it is stopped and its queue is empty: the pipeline cache first. @@ -369,7 +380,7 @@ pub(crate) fn run(core: Arc) { break Next::Write(file, bytes); } if state.stop { - break Next::End; + break Next::End(state.until); } // A creation that ends after this read sets its mark and then pokes under the // lock, which this thread holds until it waits. @@ -396,7 +407,11 @@ pub(crate) fn run(core: Arc) { // SAFETY: the caches are the core's device's. Next::Look => unsafe { core.caches.look(&core.device, &core.counters) }, Next::Write(file, bytes) => core.caches.write_kept(&file, &bytes, &core.counters), - Next::End => return, + Next::End(until) => { + // SAFETY: the caches are the core's device's. + unsafe { core.caches.last_write(&core.device, &core.counters, until) }; + return; + } } } }