diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index b60da49c4..07f045b7e 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -2686,9 +2686,10 @@ nothing is written. half of `DeviceOptions::pipeline_cache_cap` (128 MiB where it is `None`: half of it is about seven times the 9.16 MB a long recorded session's 282 lowerings fill). A lookup tries young, then old, and a hit in old moves the file into young by a rename. A new file goes into young, written by the -reaper off every guest thread: a state hands its lowering over and returns, and where the lowerings -waiting for the reaper hold 8 MiB it is dropped and counted (`Metrics::cache_writes_dropped`), never -waited for, and lowered again by a later device. The write that takes young past half the cap starts +reaper off every guest thread: a state hands its lowering over and returns, and where it would take +the lowerings waiting for the reaper past 8 MiB it is dropped and counted +(`Metrics::cache_writes_dropped`), never waited for, and lowered again by a later device. A lowering +larger than 8 MiB by itself is queued where none waits, so the heaviest kernels are kept too. The write that takes young past half the cap starts the next generation: young becomes old, and the old one is removed whole, by a thread the store starts for it. So a file stays for at least half the cap, and at most the whole cap, of files written or moved after its last use. Half the cap is what the store is sure to keep: a set of diff --git a/crates/metal2vulkan-runtime/src/cache.rs b/crates/metal2vulkan-runtime/src/cache.rs index 7e0d2d1ee..c4d206cfc 100644 --- a/crates/metal2vulkan-runtime/src/cache.rs +++ b/crates/metal2vulkan-runtime/src/cache.rs @@ -74,7 +74,8 @@ //! old; a hit in old is renamed into young, which is its mark of use. Generations start at 1, and //! the first has no old: a lookup there tries young alone. A write goes into young, made by the //! reaper: a state hands its lowering over ([`keep_translation`]) and returns, and a lowering that -//! would take the ones waiting past [`PENDING_BYTES`] is dropped and counted, never waited for. The +//! would take the ones waiting past [`PENDING_BYTES`] is dropped and counted, never waited for, +//! except that one larger than that by itself is queued where nothing else waits. The //! bytes young holds are counted in `state`, 24 bytes beside the generations: young's number, its //! bytes and a checksum of both, read and written under a lock on the file, which every store on //! the directory, in this process or another, holds open for its life. The write that takes young @@ -135,7 +136,8 @@ const BLOB: &str = "pipelines"; /// The most blobs a cache directory keeps: four kinds of device or driver versions. pub(crate) const BLOBS_KEPT: usize = 4; /// The most bytes of lowerings waiting for the reaper to write them: about the 9.16 MB a long -/// recorded session's 282 lowerings fill. A lowering past it is not kept. +/// recorded session's 282 lowerings fill. A lowering larger than this alone is queued where none +/// waits, so the heaviest kernels, the costliest to lower again, are kept too. pub(crate) const PENDING_BYTES: u64 = 8 << 20; /// What a translation file holds where the translator refused the lowering: bytes no lowering /// encodes to, and that a file cut short is not. @@ -925,9 +927,10 @@ struct Pipelines { pub(crate) struct PipelineCache(vk::PipelineCache); /// Hands the bytes of a lowered function of key digest `key` to the reaper, which writes them to -/// the store, off every guest thread. Where the lowerings waiting for it hold [`PENDING_BYTES`] -/// already, they are dropped and counted (`Metrics::cache_writes_dropped`), never waited for: a -/// lowering is only a cache, and a later state lowers it again. +/// the store, off every guest thread. Where it would take the lowerings waiting for it past +/// [`PENDING_BYTES`], it is dropped and counted (`Metrics::cache_writes_dropped`), never waited +/// for: a lowering is only a cache, and a later state lowers it again. One larger than the bound +/// by itself is queued where none waits. pub(crate) fn keep_translation(core: &Core, key: &[u8], bytes: Vec) { keep(core, Caches::translation_file(key), bytes); } @@ -967,7 +970,8 @@ pub(crate) fn keep_observer(core: &Core, key: &[u8]) { keep_translation(core, key, OBSERVES.to_vec()); } -/// The files waiting for the reaper to write them, in order, at most `bound` bytes of them. +/// The files waiting for the reaper to write them, in order: at most `bound` bytes of them, or one +/// file larger than that alone. pub(crate) struct Pending { files: VecDeque<(String, Vec)>, bytes: u64, @@ -975,7 +979,7 @@ pub(crate) struct Pending { } impl Pending { - /// An empty queue of at most `bound` bytes. + /// An empty queue of at most `bound` bytes, or of one file larger than that. pub(crate) fn new(bound: u64) -> Pending { Pending { files: VecDeque::new(), @@ -984,11 +988,12 @@ impl Pending { } } - /// Queues `bytes` under `file`; false, queuing nothing, where they would take the queue past - /// its bound. + /// Queues `bytes` under `file`; false, queuing nothing, where they would take a queue that + /// holds a file past its bound. An empty queue takes a file of any size, so a file larger than + /// the bound is kept, alone: what it holds is bytes already in memory, made before the push. pub(crate) fn push(&mut self, file: String, bytes: Vec) -> bool { let len = bytes.len() as u64; - if self.bytes.saturating_add(len) > self.bound { + if !self.files.is_empty() && self.bytes.saturating_add(len) > self.bound { return false; } self.bytes += len; diff --git a/crates/metal2vulkan-runtime/src/counters.rs b/crates/metal2vulkan-runtime/src/counters.rs index 77a79d1bb..75fbcff38 100644 --- a/crates/metal2vulkan-runtime/src/counters.rs +++ b/crates/metal2vulkan-runtime/src/counters.rs @@ -2188,8 +2188,10 @@ pub struct Metrics { /// difference of two reads does not apply to it. pub pipeline_cache_lock_wait_max_nanos: u64, /// Lowerings, refusals and render contents' records not kept in the cache directory: each was - /// handed to the reaper, which writes them off every guest thread, while the ones waiting for - /// it held 8 MiB, and was dropped rather than waited for. A later device lowers it again. + /// handed to the reaper, which writes them off every guest thread, where it would have taken + /// the ones waiting for it past 8 MiB, and was dropped rather than waited for (one larger than + /// 8 MiB alone is kept where none waits). A later device lowers it again. Metal has no + /// counterpart: Metal's compiler service keeps its own cache. pub cache_writes_dropped: 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/tests/cache.rs b/crates/metal2vulkan-runtime/src/tests/cache.rs index 496bc948e..80ef3920a 100644 --- a/crates/metal2vulkan-runtime/src/tests/cache.rs +++ b/crates/metal2vulkan-runtime/src/tests/cache.rs @@ -660,8 +660,9 @@ fn a_directory_keeps_at_most_four_blobs_the_oldest_removed() { assert!(scratch.0.join("state").exists(), "the state stays"); } -/// The lowerings waiting for the reaper: a file that would take the queue past its bound is -/// refused and nothing is queued; the rest come out in order, and each taken makes room. +/// The lowerings waiting for the reaper: a file that would take a queue holding files past its +/// bound is refused and nothing is queued; the rest come out in order, and each taken makes room. +/// An empty queue takes a file larger than the bound, alone, and refuses the next until it is taken. #[test] fn the_write_queue_refuses_a_file_past_its_bound() { let mut pending = Pending::new(10); @@ -677,6 +678,13 @@ fn the_write_queue_refuses_a_file_past_its_bound() { assert_eq!(pending.pop(), Some(("b".into(), vec![2; 6]))); assert_eq!(pending.pop(), Some(("c".into(), vec![3; 4]))); assert_eq!(pending.pop(), None); + assert!( + pending.push("e".into(), vec![5; 11]), + "larger than the bound, into an empty queue: kept alone" + ); + assert!(!pending.push("f".into(), vec![6]), "behind it: dropped"); + assert_eq!(pending.pop(), Some(("e".into(), vec![5; 11]))); + assert!(pending.push("f".into(), vec![6]), "room again"); } #[test]