diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md index 0c45c5ba3..b1c5d7d2c 100644 --- a/crates/metal2vulkan-runtime/README.md +++ b/crates/metal2vulkan-runtime/README.md @@ -571,7 +571,15 @@ Each range is widened to whole multiples of the device's `nonCoherentAtomSize`, pool's allocation, which the runtime maps whole. `Metrics::shared_flushes` and `shared_invalidates` count the calls, `shared_flushed_bytes` and `shared_invalidated_bytes` their bytes, and `shared_coherence_failures` the calls that did not return success; the work goes on -after one. On a device whose Shared type is host-coherent all five stay 0. +after one. `shared_reach_flushed_bytes`, `shared_retire_flushed_bytes`, +`shared_event_flushed_bytes` and `shared_staging_flushed_bytes` count each flush's bytes again by +the point that made it (before a submission, as a command buffer completes, before a shared +event's value is seen, around the runtime's own host accesses), a settle's flush at its settle's +point, so they sum to `shared_flushed_bytes`; `shared_invalidated_bytes` is the completions' and +the events' plus what `get_bytes` settled. `shared_flush_nanos` and `shared_invalidate_nanos` are +the host time inside the calls, read at each call and its return. These are the runtime's own +measures; Metal has no counterpart. On a device whose Shared type is host-coherent all eleven stay +0, and no clock is read for them. On a device whose Shared type is not host-coherent, a range that a command buffer reaches only through an address, with no binding or `use_resource` declaration naming its buffer, is neither @@ -1083,7 +1091,10 @@ the Direct3D 11 texture's payload until it is freed. cached and not coherent can. The device tests run the calls on our devices through a view that reads their cached types as not coherent (`--cfg m2v_shared_view`, docs/VALIDATION.md), on memory that is coherent in fact; neither the calls' time nor their effect has been measured on a device - whose memory is not. + whose memory is not. On a device that makes the calls, `Metrics::shared_reach_flushed_bytes`, + `shared_retire_flushed_bytes`, `shared_event_flushed_bytes` and `shared_staging_flushed_bytes` + measure which of those points carries their bytes, and `shared_flush_nanos` and + `shared_invalidate_nanos` the host time inside them. - On a device whose Shared type is not host-coherent, every dma-buf import is refused (`Limit::ImportCoherence`, Imported blocks), so pages that only a dma-buf imports there (a mapping no pointer import takes) have no no-copy route. The refusal is a proxy, and where the diff --git a/crates/metal2vulkan-runtime/src/coherence.rs b/crates/metal2vulkan-runtime/src/coherence.rs index e6598b631..ba68e0773 100644 --- a/crates/metal2vulkan-runtime/src/coherence.rs +++ b/crates/metal2vulkan-runtime/src/coherence.rs @@ -45,10 +45,15 @@ //! pages the consumer writes through its own cached mapping, which no flush reaches. //! Each call is counted, with its bytes, in [`Metrics`](crate::Metrics)'s `shared_flushes`, //! `shared_flushed_bytes`, `shared_invalidates` and `shared_invalidated_bytes`; a call that fails -//! in `shared_coherence_failures`. +//! in `shared_coherence_failures`. Each flush's bytes are counted once more by its [`Point`], so +//! `shared_reach_flushed_bytes`, `shared_retire_flushed_bytes`, `shared_event_flushed_bytes` +//! and `shared_staging_flushed_bytes` sum to `shared_flushed_bytes`, a settle's flush counted +//! once at its point; and the host time inside each call, read by [`host_time`] at its call and +//! at its return, in `shared_flush_nanos` or `shared_invalidate_nanos`. use crate::device::{lock, Core}; use crate::memory::{Memory, MemoryClass}; +use crate::times::host_time; use ash::vk; use std::sync::{Arc, Mutex, Weak}; @@ -290,42 +295,51 @@ impl Coherence { ranges.extend_from_slice(&held.written); } merge(&mut ranges); - self.settle(core, &ranges); + self.settle(core, Point::EventSeen, &ranges); } - /// Flushes `ranges` (`vkFlushMappedMemoryRanges`), counted; nothing for none. - pub(crate) fn flush(&self, core: &Core, ranges: &[Range]) { + /// Flushes `ranges` (`vkFlushMappedMemoryRanges`) at `point`, counted and timed; nothing for + /// none. + pub(crate) fn flush(&self, core: &Core, point: Point, ranges: &[Range]) { if ranges.is_empty() { return; } // SAFETY: each range is of an allocation the runtime maps whole and keeps while the range // is named (a block a command buffer or upload keeps, the staging region, a staging // buffer, the zero source), its ends at atoms or the allocation's end. - let failed = with_mapped(ranges, |mapped| unsafe { - core.device.flush_mapped_memory_ranges(mapped).is_err() + let (failed, nanos) = with_mapped(ranges, |mapped| unsafe { + let called = host_time(); + let failed = core.device.flush_mapped_memory_ranges(mapped).is_err(); + (failed, host_time().saturating_sub(called)) }); - core.counters.coherence(Call::Flush, bytes(ranges), failed); + let call = Call::Flush(point); + core.counters.coherence(call, bytes(ranges), nanos, failed); } - /// Flushes `ranges`, then invalidates them (`vkInvalidateMappedMemoryRanges`), counted: the - /// GPU's writes there are visible to the host's reads after it, and the host's own writes made - /// before it are kept. Nothing for none. - pub(crate) fn settle(&self, core: &Core, ranges: &[Range]) { + /// Flushes `ranges`, then invalidates them (`vkInvalidateMappedMemoryRanges`), at `point`, + /// counted and each call timed: the GPU's writes there are visible to the host's reads after + /// it, and the host's own writes made before it are kept. Nothing for none. + pub(crate) fn settle(&self, core: &Core, point: Point, ranges: &[Range]) { if ranges.is_empty() { return; } // SAFETY: as in `flush`. - let (flushed, invalidated) = with_mapped(ranges, |mapped| unsafe { - let flushed = core.device.flush_mapped_memory_ranges(mapped).is_err(); - ( - flushed, - core.device.invalidate_mapped_memory_ranges(mapped).is_err(), - ) - }); + let [(flushed, flush_nanos), (invalidated, invalidate_nanos)] = + with_mapped(ranges, |mapped| unsafe { + let called = host_time(); + let flushed = core.device.flush_mapped_memory_ranges(mapped).is_err(); + let returned = host_time(); + let invalidated = core.device.invalidate_mapped_memory_ranges(mapped).is_err(); + let after = host_time(); + [ + (flushed, returned.saturating_sub(called)), + (invalidated, after.saturating_sub(returned)), + ] + }); let bytes = bytes(ranges); - core.counters.coherence(Call::Flush, bytes, flushed); - core.counters - .coherence(Call::Invalidate, bytes, invalidated); + let counters = &core.counters; + counters.coherence(Call::Flush(point), bytes, flush_nanos, flushed); + counters.coherence(Call::Invalidate, bytes, invalidate_nanos, invalidated); } } @@ -343,13 +357,32 @@ fn with_mapped(ranges: &[Range], call: impl FnOnce(&[vk::MappedMemoryRange]) call(&mapped) } -/// Which call was made. +/// Which call was made: a flush at its point, or an invalidate, which only a settle makes, after +/// its flush of the same ranges. #[derive(Clone, Copy)] pub(crate) enum Call { - Flush, + Flush(Point), Invalidate, } +/// Where a flush, or a settle's flush and invalidate, is made: each counts its bytes in its own +/// field of [`Metrics`](crate::Metrics). +#[derive(Clone, Copy)] +pub(crate) enum Point { + /// Before a submission, over the Shared memory its command buffers reach and its Managed + /// copy-ins read (`shared_reach_flushed_bytes`). + Reach, + /// As a command buffer retires, over what its commands may write + /// (`shared_retire_flushed_bytes`). + Retirement, + /// Before the host sees a shared event's value a GPU signal raised, over what every command + /// buffer in flight may write (`shared_event_flushed_bytes`). + EventSeen, + /// The runtime's own host accesses: `replace_region`'s staging span, `upload_region`'s named + /// bytes, `get_bytes`'s staging span and the zero source (`shared_staging_flushed_bytes`). + Staging, +} + /// The bytes `ranges` name. fn bytes(ranges: &[Range]) -> u64 { ranges.iter().map(|range| range.bytes()).sum() diff --git a/crates/metal2vulkan-runtime/src/command.rs b/crates/metal2vulkan-runtime/src/command.rs index 09f67860d..e12c5cbd8 100644 --- a/crates/metal2vulkan-runtime/src/command.rs +++ b/crates/metal2vulkan-runtime/src/command.rs @@ -1958,7 +1958,7 @@ pub(crate) fn retire( let held = gpu.coherence; if let Some(held) = held { if let (Some(reach), Some(coherence)) = (&held.reach, &core.coherence) { - coherence.settle(core, &reach.written); + coherence.settle(core, coherence::Point::Retirement, &reach.written); coherence.retired(reach); } } diff --git a/crates/metal2vulkan-runtime/src/counters.rs b/crates/metal2vulkan-runtime/src/counters.rs index 7d98769fd..2a7672450 100644 --- a/crates/metal2vulkan-runtime/src/counters.rs +++ b/crates/metal2vulkan-runtime/src/counters.rs @@ -975,6 +975,12 @@ struct Atomics { shared_invalidates: AtomicU64, shared_invalidated_bytes: AtomicU64, shared_coherence_failures: AtomicU64, + shared_reach_flushed_bytes: AtomicU64, + shared_retire_flushed_bytes: AtomicU64, + shared_event_flushed_bytes: AtomicU64, + shared_staging_flushed_bytes: AtomicU64, + shared_flush_nanos: AtomicU64, + shared_invalidate_nanos: AtomicU64, /// Segment ends whose barrier reaches the host, for the tests. #[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))] host_reaches: AtomicU64, @@ -1196,18 +1202,44 @@ impl Counters { } /// A `vkFlushMappedMemoryRanges` or `vkInvalidateMappedMemoryRanges` call over `bytes` bytes - /// of Shared memory ([`Metrics::shared_flushes`] and the three after it), `failed` where it - /// did not return success ([`Metrics::shared_coherence_failures`]). - pub(crate) fn coherence(&self, call: crate::coherence::Call, bytes: u64, failed: bool) { + /// of Shared memory ([`Metrics::shared_flushes`] and the three after it) that took `nanos` + /// nanoseconds ([`Metrics::shared_flush_nanos`], [`Metrics::shared_invalidate_nanos`]), a + /// flush's bytes counted again by its point ([`Metrics::shared_reach_flushed_bytes`] and the + /// three after it), `failed` where it did not return success + /// ([`Metrics::shared_coherence_failures`]). + pub(crate) fn coherence( + &self, + call: crate::coherence::Call, + bytes: u64, + nanos: u64, + failed: bool, + ) { + use crate::coherence::{Call, Point}; let a = &self.atomics; - let (calls, total) = match call { - crate::coherence::Call::Flush => (&a.shared_flushes, &a.shared_flushed_bytes), - crate::coherence::Call::Invalidate => { - (&a.shared_invalidates, &a.shared_invalidated_bytes) + let (calls, total, time) = match call { + Call::Flush(point) => { + let at = match point { + Point::Reach => &a.shared_reach_flushed_bytes, + Point::Retirement => &a.shared_retire_flushed_bytes, + Point::EventSeen => &a.shared_event_flushed_bytes, + Point::Staging => &a.shared_staging_flushed_bytes, + }; + at.fetch_add(bytes, Relaxed); + ( + &a.shared_flushes, + &a.shared_flushed_bytes, + &a.shared_flush_nanos, + ) } + Call::Invalidate => ( + &a.shared_invalidates, + &a.shared_invalidated_bytes, + &a.shared_invalidate_nanos, + ), }; calls.fetch_add(1, Relaxed); total.fetch_add(bytes, Relaxed); + time.fetch_add(nanos, Relaxed); if failed { a.shared_coherence_failures.fetch_add(1, Relaxed); } @@ -1901,6 +1933,12 @@ impl Counters { shared_invalidates: a.shared_invalidates.load(Relaxed), shared_invalidated_bytes: a.shared_invalidated_bytes.load(Relaxed), shared_coherence_failures: a.shared_coherence_failures.load(Relaxed), + shared_reach_flushed_bytes: a.shared_reach_flushed_bytes.load(Relaxed), + shared_retire_flushed_bytes: a.shared_retire_flushed_bytes.load(Relaxed), + shared_event_flushed_bytes: a.shared_event_flushed_bytes.load(Relaxed), + shared_staging_flushed_bytes: a.shared_staging_flushed_bytes.load(Relaxed), + shared_flush_nanos: a.shared_flush_nanos.load(Relaxed), + shared_invalidate_nanos: a.shared_invalidate_nanos.load(Relaxed), submit_call_nanos: a.submit_call_nanos.load(Relaxed), submit_call_max_nanos: a.submit_call_max_nanos.load(Relaxed), pipeline_creation_nanos: a.pipeline_creation_nanos.load(Relaxed), @@ -2246,7 +2284,7 @@ pub struct Metrics { /// ([`shared_invalidates`](Self::shared_invalidates)), one after each `replace_region` through /// a staging span, one at each `upload_region` of named bytes in Shared memory, and one at the /// zero source's make. Only a device whose Shared type is not host-coherent makes them; on - /// one whose type is, this and the four after it stay 0 (the runtime README, "Shared memory's + /// one whose type is, this and the ten after it stay 0 (the runtime README, "Shared memory's /// coherence"). pub shared_flushes: u64, /// The bytes those calls named, each range widened to whole multiples of the device's @@ -2264,6 +2302,49 @@ pub struct Metrics { /// (`VK_ERROR_OUT_OF_HOST_MEMORY` or `VK_ERROR_OUT_OF_DEVICE_MEMORY`): the work went on as /// it would after a success. pub shared_coherence_failures: u64, + /// No Metal counterpart, the runtime's own measure: of + /// [`shared_flushed_bytes`](Self::shared_flushed_bytes), those the flush before each + /// submission named, over the Shared memory its command buffers reach (each buffer, buffer + /// under a texture and heap block they keep, whole) and the bytes its Managed copy-ins read. + /// + /// This and the three after it count each flush's bytes once, at the point that made it, so + /// they sum to [`shared_flushed_bytes`](Self::shared_flushed_bytes); a settle's flush counts + /// at its settle's point. Every invalidate follows a settle's flush of the same ranges, so + /// [`shared_invalidated_bytes`](Self::shared_invalidated_bytes) is + /// [`shared_retire_flushed_bytes`](Self::shared_retire_flushed_bytes) and + /// [`shared_event_flushed_bytes`](Self::shared_event_flushed_bytes), plus the part of + /// [`shared_staging_flushed_bytes`](Self::shared_staging_flushed_bytes) that `get_bytes` + /// settled. Both hold for a read made while no call is under way, and for the difference of + /// two such reads. + pub shared_reach_flushed_bytes: u64, + /// No Metal counterpart, the runtime's own measure: of + /// [`shared_flushed_bytes`](Self::shared_flushed_bytes), those the settle as each command + /// buffer retires named, over what its commands may write, before its status reads completed + /// or its handlers run; each is invalidated after its flush. + pub shared_retire_flushed_bytes: u64, + /// No Metal counterpart, the runtime's own measure: of + /// [`shared_flushed_bytes`](Self::shared_flushed_bytes), those the settles before a shared + /// event's value a GPU signal raised reaches the host named, each over what every command + /// buffer then committed and not yet retired may write, once for each value seen above the + /// last; each is invalidated after its flush. + pub shared_event_flushed_bytes: u64, + /// No Metal counterpart, the runtime's own measure: of + /// [`shared_flushed_bytes`](Self::shared_flushed_bytes), those the runtime's own host + /// accesses named: the staging span `Texture::replace_region` writes, the named bytes + /// `Texture::upload_region` copies, the staging span `Texture::get_bytes` reads (flushed, + /// then invalidated) and the zero source of fills, at its make. + pub shared_staging_flushed_bytes: u64, + /// No Metal counterpart, the runtime's own measure: the nanoseconds of + /// [`host_time`](crate::host_time) the `vkFlushMappedMemoryRanges` calls over Shared memory + /// took, from each call to its return, in total: the time inside the driver, on the thread + /// that made the call. A device whose Shared type is host-coherent makes none and reads no + /// clock for them, so this and the one after it stay 0 there. + pub shared_flush_nanos: u64, + /// No Metal counterpart, the runtime's own measure: the nanoseconds of + /// [`host_time`](crate::host_time) the `vkInvalidateMappedMemoryRanges` calls over Shared + /// memory took, from each call to its return, in total, as + /// [`shared_flush_nanos`](Self::shared_flush_nanos) counts the flushes'. + pub shared_invalidate_nanos: u64, /// The nanoseconds of [`host_time`](crate::host_time) those calls took, from each call to /// its return, in total: the time inside the driver, which a driver may spend holding the call /// while another thread is inside a memory call diff --git a/crates/metal2vulkan-runtime/src/fill.rs b/crates/metal2vulkan-runtime/src/fill.rs index dddb3a88c..066945f6a 100644 --- a/crates/metal2vulkan-runtime/src/fill.rs +++ b/crates/metal2vulkan-runtime/src/fill.rs @@ -315,8 +315,11 @@ impl Filler { .memory(self.zeros_memory) .offset(0) .size(vk::WHOLE_SIZE)]; + let called = crate::times::host_time(); let flushed = device.flush_mapped_memory_ranges(&zeros); - counters.coherence(crate::coherence::Call::Flush, ZEROS, flushed.is_err()); + let nanos = crate::times::host_time().saturating_sub(called); + let call = crate::coherence::Call::Flush(crate::coherence::Point::Staging); + counters.coherence(call, ZEROS, nanos, flushed.is_err()); } device.unmap_memory(self.zeros_memory); } diff --git a/crates/metal2vulkan-runtime/src/submit.rs b/crates/metal2vulkan-runtime/src/submit.rs index 183b1fe72..2f3bbec73 100644 --- a/crates/metal2vulkan-runtime/src/submit.rs +++ b/crates/metal2vulkan-runtime/src/submit.rs @@ -63,7 +63,7 @@ use crate::alias; use crate::buffer::{BufferInner, BufferObject, DeviceCopy}; use crate::capture::{self, Changes, CopyIn, Modified}; -use crate::coherence::{self, Range, Ranges}; +use crate::coherence::{self, Point, Range, Ranges}; use crate::command::{self, CbInner, CommandBufferError, DeviceLoss, Handler, Pending, Segment}; use crate::complete; use crate::counters::{Counters, Gauge, Metric, Part, Site}; @@ -2759,14 +2759,14 @@ fn submit(core: &Core, value: u64, uploads: Vec, batch: Batch) { let Flushes { reached, copied } = *flushes; if let Some(coherence) = &core.coherence { match (copied.is_empty(), &reached[..]) { - (true, [one]) => coherence.flush(core, &one.reached), + (true, [one]) => coherence.flush(core, Point::Reach, &one.reached), _ => { let mut flushed = copied; flushed.extend( (reached.iter()).flat_map(|ranges| ranges.reached.iter().copied()), ); coherence::merge(&mut flushed); - coherence.flush(core, &flushed); + coherence.flush(core, Point::Reach, &flushed); } } } diff --git a/crates/metal2vulkan-runtime/src/tests/coherence.rs b/crates/metal2vulkan-runtime/src/tests/coherence.rs index 0294cdad0..8cdd746b6 100644 --- a/crates/metal2vulkan-runtime/src/tests/coherence.rs +++ b/crates/metal2vulkan-runtime/src/tests/coherence.rs @@ -10,6 +10,15 @@ //! with its bytes, and none fails. A device whose one host-visible, host-cached buffer type also //! holds the parameter ring is refused under the view, with its refusal. //! +//! Every count also checks the points' identity: the flushed bytes by point (before a +//! submission, at a retirement, before an event's value is seen, around the runtime's own host +//! accesses) sum to the flushed bytes, and the invalidated bytes are the retirements' and the +//! events' plus at most the staging's. Each site below asserts its bytes at its own point. A +//! device with no coherence state counts nothing at any point and no time in a call; where calls +//! ran, the host time inside the flushes and the invalidates is not zero. On every device, +//! lavapipe among them, a coherence state the test makes over the viewed Shared type counts a +//! flush and a settle of a Shared buffer's range at each point alone. +//! //! Under the view, each of these sites makes its calls too, each counted by its ranges' bytes //! widened to the device's atom, with no call failing, and the bytes read after them are the //! bytes written: @@ -58,15 +67,44 @@ mod on_device { use metal2vulkan_devtools::{engine_dir, llvm_assemble, AssembleError}; use std::sync::atomic::Ordering; - /// From `before` to `after`: the flushes, their bytes, the invalidates, their bytes, and the - /// calls that failed. - fn calls(before: &Metrics, after: &Metrics) -> [u64; 5] { - [ + /// From `before` to `after`: the flushes, their bytes, the invalidates, their bytes, the + /// calls that failed, then the flushed bytes by point: before a submission, at a retirement, + /// before an event's value is seen, and around the runtime's own host accesses + /// (`crate::coherence::Point`). Asserts the identity between them: the points' bytes sum to + /// the flushed bytes, and the invalidated bytes are the retirements' and the events' plus at + /// most the staging's. + fn calls(before: &Metrics, after: &Metrics) -> [u64; 9] { + let made = [ after.shared_flushes - before.shared_flushes, after.shared_flushed_bytes - before.shared_flushed_bytes, after.shared_invalidates - before.shared_invalidates, after.shared_invalidated_bytes - before.shared_invalidated_bytes, after.shared_coherence_failures - before.shared_coherence_failures, + after.shared_reach_flushed_bytes - before.shared_reach_flushed_bytes, + after.shared_retire_flushed_bytes - before.shared_retire_flushed_bytes, + after.shared_event_flushed_bytes - before.shared_event_flushed_bytes, + after.shared_staging_flushed_bytes - before.shared_staging_flushed_bytes, + ]; + let [_, flushed, _, invalidated, _, reach, retire, event, staging] = made; + assert_eq!( + reach + retire + event + staging, + flushed, + "the points' flushed bytes sum to the flushed bytes: {made:?}" + ); + assert!( + (retire + event..=retire + event + staging).contains(&invalidated), + "the invalidated bytes are the retirements' and the events' settles, and get_bytes's: \ + {made:?}" + ); + made + } + + /// From `before` to `after`: the host nanoseconds inside the flushes, then inside the + /// invalidates. + fn nanos(before: &Metrics, after: &Metrics) -> [u64; 2] { + [ + after.shared_flush_nanos - before.shared_flush_nanos, + after.shared_invalidate_nanos - before.shared_invalidate_nanos, ] } @@ -78,10 +116,21 @@ mod on_device { /// The calls of one command buffer that runs in one submission, reaching `reached` bytes of /// Shared memory and maybe writing `written` of them: the submission's flush of every byte it - /// reaches, then, where it may write any, its completion's flush and invalidate of those. - fn one_submission(reached: u64, written: u64) -> [u64; 5] { + /// reaches, then, where it may write any, its completion's flush and invalidate of those; the + /// first counted at the submission's point, the settle at the retirement's. + fn one_submission(reached: u64, written: u64) -> [u64; 9] { let settles = u64::from(written > 0); - [1 + settles, reached + written, settles, written, 0] + [ + 1 + settles, + reached + written, + settles, + written, + 0, + reached, + written, + 0, + 0, + ] } /// The device's `nonCoherentAtomSize`, held by the coherence state the view built. @@ -230,7 +279,7 @@ mod on_device { ); assert!(bytes.iter().all(|&byte| byte == 0x5a), "{name}: the fill"); assert_eq!(read, texels, "{name}: the texture's bytes"); - assert_eq!(into_private, [0; 5], "{name}: a fill into a Private buffer"); + assert_eq!(into_private, [0; 9], "{name}: a fill into a Private buffer"); let Some(atom) = atom else { assert_eq!( (made.shared_flushes, made.shared_invalidates), @@ -242,8 +291,19 @@ mod on_device { ("replace_region", staged_in), ("get_bytes", staged_out), ] { - assert_eq!(calls, [0; 5], "{name}: {work}, with no coherence state"); + assert_eq!(calls, [0; 9], "{name}: {work}, with no coherence state"); } + let total = counters.metrics(); + assert_eq!( + calls(&Metrics::default(), &total), + [0; 9], + "{name}: no call over the device's life, with no coherence state" + ); + assert_eq!( + nanos(&Metrics::default(), &total), + [0; 2], + "{name}: no time in a call, with no coherence state" + ); return; }; assert!( @@ -255,9 +315,10 @@ mod on_device { let widened = atoms(OFFSET, LEN, atom); assert_eq!( into_shared, - [2, 2 * widened, 1, widened, 0], + [2, 2 * widened, 1, widened, 0, widened, widened, 0, 0], "{name}: a fill into a Shared buffer: its submission's flush, then its completion's \ - flush and invalidate, each over the buffer's atoms ({widened} bytes)" + flush and invalidate, each over the buffer's atoms ({widened} bytes), at the \ + submission's point and the retirement's" ); assert_eq!( [staged_in[0], staged_in[2], staged_in[4]], @@ -268,6 +329,11 @@ mod on_device { staged_in[1] >= 64, "{name}: replace_region flushed its 64 bytes" ); + assert_eq!( + staged_in[5..], + [0, 0, 0, staged_in[1]], + "{name}: replace_region's flush, at the staging point" + ); assert_eq!( [staged_out[0], staged_out[2], staged_out[4]], [1, 1, 0], @@ -277,6 +343,34 @@ mod on_device { staged_out[1] >= 64 && staged_out[3] >= 64, "{name}: get_bytes flushed and invalidated its 64 bytes" ); + assert_eq!( + [ + staged_out[5], + staged_out[6], + staged_out[7], + staged_out[8], + staged_out[3] + ], + [0, 0, 0, staged_out[1], staged_out[1]], + "{name}: get_bytes's settle, at the staging point, invalidating what it flushed" + ); + let total = counters.metrics(); + assert!( + made.shared_staging_flushed_bytes >= crate::fill::ZEROS, + "{name}: the zero source's flush, at the staging point: {made:?}" + ); + calls(&Metrics::default(), &total); + let [flush, invalidate] = nanos(&Metrics::default(), &total); + println!( + "coherence: {name}: over the device's life, {} flushes in {flush} ns, {} invalidates \ + in {invalidate} ns", + total.shared_flushes, total.shared_invalidates + ); + assert!( + flush > 0 && invalidate > 0, + "{name}: the host time inside the flushes ({flush} ns) and the invalidates \ + ({invalidate} ns)" + ); } /// Every device of the cfg's kind, made as the device tests make it: it holds a coherence @@ -316,6 +410,88 @@ mod on_device { }); } + /// Every device of the cfg's kind, lavapipe among them, with a coherence state the test makes + /// of its own over the device's Shared type read through the refused device's view + /// (`crate::tests::command::on_device::viewed`), which leaves that type not coherent: the + /// device itself is made as the device tests make it, so this runs where the floor refuses + /// the view. At each point, a flush and then a settle of a Shared buffer's range count its + /// atoms at that point alone and at no other, the settle's invalidate among the invalidated + /// bytes, with no call failing; and the host time inside the calls is counted, the flushes' + /// apart from the invalidates'. + #[test] + fn a_coherence_state_counts_each_call_at_its_point_and_times_it() { + use crate::buffer::BufferObject; + use crate::coherence::{Coherence, Point}; + const OFFSET: u64 = 320; + const LEN: u64 = 100; + let runtime = crate::tests::validation::instance(&[]); + let (_, vk_instance) = runtime.raw(); + on_each_device("coherence_points", &runtime, |name, _, device| { + let physical = device.raw().physical_device; + // SAFETY: the device's physical device is one of the instance's. + let (memory, limits) = unsafe { + ( + vk_instance.get_physical_device_memory_properties(physical), + vk_instance.get_physical_device_properties(physical).limits, + ) + }; + let memory = crate::tests::command::on_device::viewed(memory); + let atom = limits.non_coherent_atom_size; + let coherence = (Coherence::of(&memory, device.memory_types(), atom)) + .unwrap_or_else(|| panic!("{name}: the view leaves the Shared type not coherent")); + settled(&device); + let counters = device.counters(); + let block = (device.take_block(MemoryClass::Shared, 4096)).expect("a Shared block"); + let buffer = (device.new_buffer(block.id, OFFSET, LEN, Some(0))).expect("a buffer"); + let range = (buffer.shared_range(coherence.atom)) + .unwrap_or_else(|| panic!("{name}: the buffer's range in mapped Shared memory")); + let widened = atoms(block.offset + OFFSET, LEN, coherence.atom); + let core = device.core(); + let start = counters.metrics(); + let points = [ + ("the submission's", Point::Reach), + ("the retirement's", Point::Retirement), + ("the event's", Point::EventSeen), + ("the staging's", Point::Staging), + ]; + for (at, (what, point)) in points.into_iter().enumerate() { + let mut alone = [1, widened, 0, 0, 0, 0, 0, 0, 0]; + alone[5 + at] = widened; + let mut settled_alone = alone; + settled_alone[2..4].copy_from_slice(&[1, widened]); + let before = counters.metrics(); + coherence.flush(core, point, &[range]); + let flushed = counters.metrics(); + coherence.settle(core, point, &[range]); + let settled = counters.metrics(); + let flush = calls(&before, &flushed); + let settle = calls(&flushed, &settled); + println!( + "coherence_points: {name}: at {what} point, a flush {flush:?}, a settle \ + {settle:?}; the buffer {widened} bytes" + ); + assert_eq!( + flush, alone, + "{name}: a flush at {what} point, counted there alone" + ); + assert_eq!( + settle, settled_alone, + "{name}: a settle at {what} point, its flush counted there alone" + ); + } + let [flush, invalidate] = nanos(&start, &counters.metrics()); + println!( + "coherence_points: {name}: 8 flushes in {flush} ns, 4 invalidates in {invalidate} \ + ns" + ); + assert!( + flush > 0 && invalidate > 0, + "{name}: the host time inside the flushes ({flush} ns) and the invalidates \ + ({invalidate} ns)" + ); + }); + } + /// Every device of the cfg's kind, read through the refused device's view /// (`crate::tests::command::on_device::cached_types_incoherent`), decided by the floor's choice /// over the view (`each_device_under_the_view`). A device the choice opens has a Shared type @@ -454,7 +630,7 @@ mod on_device { (queue, state): (&CommandQueue, &ComputePipelineState), [held, out]: [&Buffer; 2], declare: impl FnOnce(&mut ComputeCommandEncoder), - ) -> [u64; 5] { + ) -> [u64; 9] { let counters = device.counters(); let before = counters.metrics(); let command_buffer = queue.command_buffer().expect("a command buffer"); @@ -787,9 +963,10 @@ mod on_device { ); assert_eq!( at_sight, - [2, 2 * widened, 1, widened, 0], + [2, 2 * widened, 1, widened, 0, widened, 0, widened, 0], "{name}: {way}: the submission's flush, then the settle before the value is \ - seen, each over the buffer's atoms" + seen, each over the buffer's atoms, at the submission's point and the \ + event's" ); assert!( bytes.iter().all(|&byte| byte == value), @@ -800,6 +977,11 @@ mod on_device { [1, widened, 0], "{name}: {way}: the completion's invalidate" ); + assert_eq!( + at_completion[5..], + [0, widened, 0, 0], + "{name}: {way}: the completion's settle, at the retirement's point" + ); } }); } diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs index fe2f5a008..c2c92f2a5 100644 --- a/crates/metal2vulkan-runtime/src/texture.rs +++ b/crates/metal2vulkan-runtime/src/texture.rs @@ -41,6 +41,7 @@ use crate::alias::{Moved, Placement}; use crate::arrays::{Owner, ResourceId}; use crate::buffer::{Buffer, BufferInner, BufferObject}; +use crate::coherence::Point; use crate::copy_out; use crate::counters::{Counters, Gauge, Metric, OverCause, OverCopyKind, Part, Site}; use crate::device::{lock, Core, Device}; @@ -4734,7 +4735,7 @@ impl Texture { // The rows written are the GPU's before the copy runs (`crate::coherence`). if let Some(coherence) = &core.coherence { let written = stage.range(copy.len(), coherence.atom); - coherence.flush(core, written.as_slice()); + coherence.flush(core, Point::Staging, written.as_slice()); } core.counters.copied(copy.len()); core.work.upload( @@ -4811,7 +4812,7 @@ impl Texture { // (`crate::coherence`). if let Some(coherence) = &core.coherence { let read = bytes.shared_span(start, copy.extent(), coherence.atom); - coherence.flush(core, read.as_slice()); + coherence.flush(core, Point::Staging, read.as_slice()); } core.work.upload( core, @@ -4936,7 +4937,7 @@ impl Texture { // What the copy wrote is the host's before it reads it (`crate::coherence`). if let Some(coherence) = &core.coherence { let written = stage.range(copy.len(), coherence.atom); - coherence.settle(core, written.as_slice()); + coherence.settle(core, Point::Staging, written.as_slice()); } // SAFETY: the stage holds every row packed, the copy into it completed and its writes // are visible to the host, and no other call takes its span while this one reads it; diff --git a/crates/metal2vulkan/tests/api_signatures.tsv b/crates/metal2vulkan/tests/api_signatures.tsv index 5c85a6ab1..66bded54e 100644 --- a/crates/metal2vulkan/tests/api_signatures.tsv +++ b/crates/metal2vulkan/tests/api_signatures.tsv @@ -1575,12 +1575,18 @@ field metal2vulkan-runtime::Metrics.scheduled_to_gpu_start_behind_nanos pub sche field metal2vulkan-runtime::Metrics.scheduled_to_gpu_start_nanos pub scheduled_to_gpu_start_nanos: u64 field metal2vulkan-runtime::Metrics.segment_end_barriers pub segment_end_barriers: u64 field metal2vulkan-runtime::Metrics.shared_coherence_failures pub shared_coherence_failures: u64 +field metal2vulkan-runtime::Metrics.shared_event_flushed_bytes pub shared_event_flushed_bytes: u64 +field metal2vulkan-runtime::Metrics.shared_flush_nanos pub shared_flush_nanos: u64 field metal2vulkan-runtime::Metrics.shared_flushed_bytes pub shared_flushed_bytes: u64 field metal2vulkan-runtime::Metrics.shared_flushes pub shared_flushes: u64 +field metal2vulkan-runtime::Metrics.shared_invalidate_nanos pub shared_invalidate_nanos: u64 field metal2vulkan-runtime::Metrics.shared_invalidated_bytes pub shared_invalidated_bytes: u64 field metal2vulkan-runtime::Metrics.shared_invalidates pub shared_invalidates: u64 field metal2vulkan-runtime::Metrics.shared_long_reuses pub shared_long_reuses: u64 +field metal2vulkan-runtime::Metrics.shared_reach_flushed_bytes pub shared_reach_flushed_bytes: u64 field metal2vulkan-runtime::Metrics.shared_remade_after_frees pub shared_remade_after_frees: u64 +field metal2vulkan-runtime::Metrics.shared_retire_flushed_bytes pub shared_retire_flushed_bytes: u64 +field metal2vulkan-runtime::Metrics.shared_staging_flushed_bytes pub shared_staging_flushed_bytes: u64 field metal2vulkan-runtime::Metrics.shared_take_allocations pub shared_take_allocations: SiteAllocations field metal2vulkan-runtime::Metrics.sparse_binds pub sparse_binds: u64 field metal2vulkan-runtime::Metrics.stage_fills pub stage_fills: u64