A commit copies into each copied host buffer the bytes the CPU changed in its pages, before any of the command buffer's work is submitted. A batch that waits for an event runs its commands only once the event reaches the value, and on Metal those commands read the pages as they stand then. So a CPU write made after the commit and before the host's signal (or before a later command buffer's signal) never reached the copy, and the waiting command buffer read the bytes from before it. The commit now notes, before its copy in, the batches that wait for an event value not yet reached (CopiesAgain::unreached, from each event's timeline). When the scheduler submits such a batch, which it does only once the wait is met, submit_batch first copies in again (copy_in_again): the copies the command buffer reaches, as at the commit, take the CPU's changes since. This runs with no lock of the command buffer held, since a copy over logged pages waits for the log's publication. A batch with no wait, or whose every wait was already reached at the commit, copies nothing more. The new counter is WorkCounts::copied_copy_ins_after_waits. The same mechanism serves a reader held by an event across queues: the signal of a batch submitted while a copy is live is seen only at its completion turn, after the merge back. The FAST-PATHS gap that listed event-ordered reads across queues as stale now says the held reader copies in again; that case is expected from the code and not run. Docs: copied.rs (Coherence, the two-memories bullet, copy_in), dirty_pages.rs (stamps), HOWTO (copied host buffers), FAST-PATHS (cost row and gap), api_signatures.tsv (the new field). Verified: waited_reads in the jobs named in the report. The copied leg fails at cae2e24b4d and passes here; the no-wait copied leg makes 0 copies in again.
101 lines
4.3 KiB
Rust
101 lines
4.3 KiB
Rust
//! Copied host buffers copied in again after an event wait (`copied.rs`, **Coherence**).
|
|
//!
|
|
//! A commit copies into each copy its commands reach the bytes the CPU changed in its pages,
|
|
//! before any of its work is submitted. A batch that waits for an event runs its commands once
|
|
//! the event reaches the value, and Metal's reads there see the pages as they stand then: a CPU
|
|
//! write made after the commit and before the signal (the host's, or a command buffer's committed
|
|
//! after it) is read. So a batch whose wait the commit found not reached, as it stood before the
|
|
//! commit's copy in, copies in again as it is submitted, which the scheduler does only once the
|
|
//! wait is met: the copies the command buffer's commands reach, as at the commit, take the bytes
|
|
//! the CPU changed since. Counted: `WorkCounts::copied_copy_ins_after_waits`.
|
|
//!
|
|
//! A batch with no wait, and one whose every wait the commit found reached, copies in nothing
|
|
//! more: every CPU write before its commands may run was made before the commit's copy in. A wait
|
|
//! a GPU signal meets that a copy's merge back must come before is held on the host until that
|
|
//! merge (`event_timeline.rs`), so the copy in again after it takes the merged bytes too.
|
|
|
|
use super::batches::Batch;
|
|
use super::{lock, recording_error, CommandBufferShared};
|
|
use crate::copied::CopiesReached;
|
|
use crate::error::{CommandBufferError, Error, MemoryKind};
|
|
use crate::event::EventShared;
|
|
use crate::event_timeline::WaitFor;
|
|
use ash::vk;
|
|
use std::sync::Arc;
|
|
|
|
/// The batches of a committed command buffer that copy in again, and the copies its commands
|
|
/// reach.
|
|
pub(super) struct CopiesAgain {
|
|
/// The batches, by index, not yet submitted.
|
|
batches: Vec<usize>,
|
|
/// The copies its commands may read, and those they may write, as the commit found them
|
|
/// (`CopiedBuffers::copy_in`).
|
|
reached: Arc<(CopiesReached, CopiesReached)>,
|
|
}
|
|
|
|
impl CopiesAgain {
|
|
/// The indices of `batches` that wait for an event value not reached now.
|
|
pub(super) fn unreached(batches: &[Batch<Arc<EventShared>, vk::Semaphore>]) -> Vec<usize> {
|
|
(batches.iter().enumerate())
|
|
.filter(|(_, batch)| {
|
|
(batch.waits.iter())
|
|
.any(|(event, value)| event.wait(*value) != Some(WaitFor::Reached))
|
|
})
|
|
.map(|(index, _)| index)
|
|
.collect()
|
|
}
|
|
|
|
/// The copies in again of `batches`, reaching `reached`; `None` where there are none.
|
|
pub(super) fn new(
|
|
batches: Vec<usize>,
|
|
reached: (CopiesReached, CopiesReached),
|
|
) -> Option<CopiesAgain> {
|
|
(!batches.is_empty()).then(|| CopiesAgain {
|
|
batches,
|
|
reached: Arc::new(reached),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl CommandBufferShared {
|
|
/// Copy in again for batch `index`, where its commit left one for it, once: as it is
|
|
/// submitted, its waits met. Copied with no lock of the command buffer's held, as a copy over
|
|
/// logged pages waits for the log's publication; where the host has no memory for a copy's
|
|
/// base, the command buffer fails as out of memory, as its commit would have.
|
|
pub(super) fn copy_in_again(&self, index: usize) -> Result<(), CommandBufferError> {
|
|
let reached = {
|
|
let mut state = lock(&self.state);
|
|
let Some(again) = &mut state.copies_again else {
|
|
return Ok(());
|
|
};
|
|
let Some(at) = again.batches.iter().position(|&batch| batch == index) else {
|
|
return Ok(());
|
|
};
|
|
again.batches.swap_remove(at);
|
|
let reached = Arc::clone(&again.reached);
|
|
if again.batches.is_empty() {
|
|
state.copies_again = None;
|
|
}
|
|
reached
|
|
};
|
|
let device = &self.device;
|
|
let shared = device.shared();
|
|
let (used, written) = &*reached;
|
|
let copied_in = shared.copied.copy_in(
|
|
&shared.counts,
|
|
shared.dirty.tally(),
|
|
(used, written),
|
|
|| shared.dirty.in_flight(),
|
|
&mut |length| device.copied_base(length),
|
|
);
|
|
if copied_in.is_err() {
|
|
return Err(recording_error(
|
|
device,
|
|
Error::OutOfMemory(MemoryKind::Host),
|
|
));
|
|
}
|
|
shared.counts.copied_copy_ins_after_waits();
|
|
Ok(())
|
|
}
|
|
}
|