diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 2ab398890..964eacfec 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -206,7 +206,9 @@ timestamps of their own (`runtime_command_buffer_gpu_nanos`); the copies of textures over a buffer that no linear image holds are split by cause (`over_copies_pitch` and the rest, in Textures below); the exported blocks a public call handed out a device address of, each counted at the first (`exported_blocks_addressed`, the exported -block's rule in Textures below), 0 where none went out; +block's rule in Textures below), 0 where none went out; the pipeline cache's replacements past its +bound and its blob writes skipped past it (`pipeline_caches_replaced`, +`pipeline_cache_writes_skipped`, The cache directory below); `gauges()` are live counts per kind of resource, every one zero after teardown, and `handles_out`, 1 while a handle of the device is out (Teardown). They are relaxed atomics, always on. What an encoder's commands count arrives when the encoder ends. @@ -2775,10 +2777,13 @@ loses only the pipelines made since the last; no creation waits for a write. The cache holds at most 32 MiB when the reaper looks (about five times the 6.25 MB of pipeline data a long recorded session made): one past it is replaced by a fresh, empty cache that creations take -from then on, and is destroyed once no creation holds it. The next write replaces the blob with -the fresh cache's data, so the blob holds at most 32 MiB too, with no second file and no -generations. `Gauges::pipeline_cache_bytes` is the cache's size at the reaper's last look, and is -zero once the device is torn down. With no directory the cache is the same, made empty, and +from then on, and is destroyed once no creation holds it, each replacement counted in +`Metrics::pipeline_caches_replaced`. The next write replaces the blob with the fresh cache's data, +so the blob holds at most 32 MiB too, with no second file and no generations. A write of data past +32 MiB, the teardown's of a cache that grew past it after the reaper's last look, is skipped and +counted in `Metrics::pipeline_cache_writes_skipped`; the blob keeps what it held. +`Gauges::pipeline_cache_bytes` is the cache's size at the reaper's last look, and is zero once the +device is torn down. With no directory the cache is the same, made empty, and nothing is written. **The lowerings** live in two generations, `g/` (young) and `g/` (old), each holding at most diff --git a/crates/metal2vulkan-runtime/src/cache.rs b/crates/metal2vulkan-runtime/src/cache.rs index ab49b06d2..b84ca83b7 100644 --- a/crates/metal2vulkan-runtime/src/cache.rs +++ b/crates/metal2vulkan-runtime/src/cache.rs @@ -38,9 +38,11 @@ //! //! The cache holds at most [`PIPELINE_CACHE_BOUND`]: a cache past it when the reaper looks is //! replaced by a fresh, empty one that creations take from then on, and destroyed once no creation -//! holds it; the next write replaces the blob with the fresh cache's data, so the blob holds at -//! most the bound too, with no second file. [`Gauges::pipeline_cache_bytes`] is the cache's size -//! at the reaper's last look. Without a directory the cache is the same, and nothing is on disk. +//! holds it (counted in `Metrics::pipeline_caches_replaced`); the next write replaces the blob with +//! the fresh cache's data, so the blob holds at most the bound too, with no second file. A write +//! of data past the bound, the teardown's of a cache that grew past it after the last look, is +//! skipped (`Metrics::pipeline_cache_writes_skipped`). [`Gauges::pipeline_cache_bytes`] is the +//! cache's size at the reaper's last look. Without a directory the cache is the same, and nothing is on disk. //! //! **The lowerings.** The store keeps one file per lowered function (a kernel, or a render //! pipeline's vertex or fragment function) under `translations/`, named by the hex of its @@ -1673,6 +1675,7 @@ impl Caches { pipelines.written = fresh_size; drop(pipelines); counters.set(Gauge::PipelineCacheBytes, fresh_size); + counters.count(Metric::PipelineCachesReplaced); return None; } let written = lock(&self.pipelines).written; @@ -1709,10 +1712,11 @@ impl Caches { } /// Writes `data`, the cache's data the driver gave at size `size`, over the device's blob at - /// `path`, where it is within the bound. + /// `path`, where it is within the bound; past it, the write is skipped and counted. fn write(&self, path: &Path, data: &[u8], size: u64, counters: &Counters) { let len = data.len() as u64; if len > self.bound.load(Relaxed) { + counters.count(Metric::PipelineCacheWritesSkipped); return; } if write_whole(path, &[&frame(data), data]).is_ok() { diff --git a/crates/metal2vulkan-runtime/src/counters.rs b/crates/metal2vulkan-runtime/src/counters.rs index d09084bad..7170d32b6 100644 --- a/crates/metal2vulkan-runtime/src/counters.rs +++ b/crates/metal2vulkan-runtime/src/counters.rs @@ -228,6 +228,10 @@ pub(crate) enum Metric { CreationsBeforeSeed, /// [`Metrics::cache_writes_dropped`]. CacheWritesDropped, + /// [`Metrics::pipeline_caches_replaced`]. + PipelineCachesReplaced, + /// [`Metrics::pipeline_cache_writes_skipped`]. + PipelineCacheWritesSkipped, /// [`Metrics::ring_writes`]. RingWrites, /// [`Metrics::pushes`]. @@ -304,6 +308,8 @@ const METRIC_ORDER: [Metric; METRICS] = [ Metric::RenderRecordsStale, Metric::CreationsBeforeSeed, Metric::CacheWritesDropped, + Metric::PipelineCachesReplaced, + Metric::PipelineCacheWritesSkipped, Metric::RingWrites, Metric::Pushes, Metric::PushesHeld, @@ -652,6 +658,8 @@ struct Atomics { pipeline_cache_lock_wait_nanos: AtomicU64, pipeline_cache_lock_wait_max_nanos: AtomicU64, cache_writes_dropped: AtomicU64, + pipeline_caches_replaced: AtomicU64, + pipeline_cache_writes_skipped: AtomicU64, ring_writes: AtomicU64, pushes: AtomicU64, pushes_held: AtomicU64, @@ -832,6 +840,8 @@ impl Counters { Metric::RenderRecordsStale => &a.render_records_stale, Metric::CreationsBeforeSeed => &a.creations_before_seed, Metric::CacheWritesDropped => &a.cache_writes_dropped, + Metric::PipelineCachesReplaced => &a.pipeline_caches_replaced, + Metric::PipelineCacheWritesSkipped => &a.pipeline_cache_writes_skipped, Metric::RingWrites => &a.ring_writes, Metric::Pushes => &a.pushes, Metric::PushesHeld => &a.pushes_held, @@ -1651,6 +1661,8 @@ impl Counters { pipeline_cache_lock_wait_nanos: a.pipeline_cache_lock_wait_nanos.load(Relaxed), pipeline_cache_lock_wait_max_nanos: a.pipeline_cache_lock_wait_max_nanos.load(Relaxed), cache_writes_dropped: a.cache_writes_dropped.load(Relaxed), + pipeline_caches_replaced: a.pipeline_caches_replaced.load(Relaxed), + pipeline_cache_writes_skipped: a.pipeline_cache_writes_skipped.load(Relaxed), cache_bytes_written: a.cache_bytes_written.load(Relaxed), ring_writes: a.ring_writes.load(Relaxed), pushes: a.pushes.load(Relaxed), @@ -2260,6 +2272,16 @@ pub struct Metrics { /// 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, + /// The reaper's replacements of the device's `VkPipelineCache` by a fresh, empty one, at a + /// look that found the cache past its bound of 32 MiB: creations take the fresh one from then + /// on, so the pipelines the old one held compile again where made again. Metal has no + /// counterpart: Metal's compiler service keeps its own cache. + pub pipeline_caches_replaced: u64, + /// Writes of the pipeline cache's data over the device's blob skipped because the data was + /// past the bound of 32 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. + 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 /// quiet where the cache grew by more than an eighth since the last write, and at the diff --git a/crates/metal2vulkan-runtime/src/tests/translate.rs b/crates/metal2vulkan-runtime/src/tests/translate.rs index b42ac0b34..4992dec8d 100644 --- a/crates/metal2vulkan-runtime/src/tests/translate.rs +++ b/crates/metal2vulkan-runtime/src/tests/translate.rs @@ -1995,13 +1995,18 @@ mod on_device { /// The pipeline cache filled past its bound: at the look after a creation takes it past, the /// reaper replaces it with a fresh one, which the gauge falls to, keeps the old one while it is /// held, and destroys it at the first look after it is let go; creations go on through the - /// fresh cache, and the next write replaces the blob with the fresh cache's data alone. + /// fresh cache, and the next write replaces the blob with the fresh cache's data alone. The + /// replacement counts once in `Metrics::pipeline_caches_replaced`. Then a creation grows the + /// cache while another holds creation open, so no look comes before the teardown, and with + /// the bound lowered past the cache the teardown's write is skipped, counted once in + /// `Metrics::pipeline_cache_writes_skipped`, and the blob keeps the fresh cache's data. #[test] fn every_device_replaces_its_pipeline_cache_past_its_bound() { - let (Some(first), Some(second), Some(third)) = ( + let (Some(first), Some(second), Some(third), Some(fourth)) = ( descriptor("a1_bool_from_float"), descriptor("a1_sign_and_division"), descriptor("a1_sign_and_division_precise"), + descriptor("ob1_storage_buffer"), ) else { return; }; @@ -2044,6 +2049,8 @@ mod on_device { first_blob, "{name}: no write at the replacement" ); + let replaced = counters.metrics().pipeline_caches_replaced; + assert_eq!(replaced, 1, "{name}: one replacement counted"); drop(held); caches.set_bound(crate::cache::PIPELINE_CACHE_BOUND); let c = state_and_look(name, &device, &third); @@ -2059,12 +2066,35 @@ mod on_device { 24 + now, "{name}: the blob holds the fresh cache" ); - drop((a, b, c)); + // Creation held open keeps the reaper from looking until the teardown stops it. + let core = std::sync::Arc::clone(device.core()); + let blob = caches.blob().expect("a cache directory").to_path_buf(); + let open = crate::cache::creation(&core); + let pipelines = counters.metrics().pipelines_created; + let d = state(name, &device, &fourth); + assert!( + counters.metrics().pipelines_created > pipelines, + "{name}: the fourth state made its VkPipeline" + ); + caches.set_bound(1); + drop((a, b, c, d)); drop(device); + drop(open); + assert_eq!( + counters.metrics().pipeline_cache_writes_skipped, + 0, + "{name}" + ); + drop(core); + let skipped = counters.metrics().pipeline_cache_writes_skipped; + assert_eq!(skipped, 1, "{name}: the teardown's write skipped"); + let kept = std::fs::metadata(&blob).map_or(0, |meta| meta.len()); + assert_eq!(kept, 24 + now, "{name}: the blob keeps the fresh cache"); within("the teardown", || counters.gauges() == Gauges::default()); let _ = std::fs::remove_dir_all(&dir); println!( - "{name}: {full} bytes past a bound of {}, replaced by {fresh}, grown to {now}", + "{name}: {full} bytes past a bound of {}, replaced by {fresh}, grown to {now}; \ + replacements {replaced}, writes skipped at the teardown {skipped}", full - 1 ); } diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 0929f4594..794c57acd 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -1572,6 +1572,8 @@ field metal2vulkan-runtime::Metrics.pipeline_cache_lock_wait_max_nanos pub pipel field metal2vulkan-runtime::Metrics.pipeline_cache_lock_wait_nanos pub pipeline_cache_lock_wait_nanos: u64 field metal2vulkan-runtime::Metrics.pipeline_cache_lock_waits pub pipeline_cache_lock_waits: u64 field metal2vulkan-runtime::Metrics.pipeline_cache_looks_skipped pub pipeline_cache_looks_skipped: u64 +field metal2vulkan-runtime::Metrics.pipeline_cache_writes_skipped pub pipeline_cache_writes_skipped: u64 +field metal2vulkan-runtime::Metrics.pipeline_caches_replaced pub pipeline_caches_replaced: u64 field metal2vulkan-runtime::Metrics.pipeline_creation_nanos pub pipeline_creation_nanos: u64 field metal2vulkan-runtime::Metrics.pipelines_created pub pipelines_created: u64 field metal2vulkan-runtime::Metrics.pipelines_from_stage_fills pub pipelines_from_stage_fills: u64