replay: the new runtime's waits are bounded under the hang protocol
The rt player waited on command buffers with the runtime's own wait_until_completed, which has no bound, and its wait record named the waitUntilCompleted selector as a literal: the bounded-wait test (gpu::tests::device_waits_exist_only_in_the_bounded_wait_module) failed on it. gpu::wait gains new_runtime: a command buffer's Retired flag, set by its completed handler, and wait_until_retired, which sends the command buffer (the queue's send_now, as the runtime's own host wait does) and waits on the flag for at most the process's bound. Past it the hang is recorded and the process ends with the hang exit code, as every bounded wait does. How the command buffer ended stays the caller's to read, so the player reads its error into the verdict as before, and a device fault writes no tripwire (the old player's path writes one). The player keeps each in-flight commit's queue with its flag; both of its waits (an index's owed commits, and a command buffer the player drains) go through it. NewRuntime::new takes the device's kind, which names the waits' backend. Verified: see the check job (clippy in both configurations, the replay and gpu unit tests, the rt_replay device tests on lavapipe).
This commit is contained in:
1 parent
521ab39b86
commit
465d885136
5 files changed
+168
-48
No files matched your search
@@ -328,7 +328,8 @@ fn open_rt(device_type: DeviceType, cache: PathBuf) -> Result<(NewRuntime, Strin
|
||||
for (_, name, physical) in candidates {
|
||||
let mut options = metal2vulkan_rt::DeviceOptions::default();
|
||||
options.pipeline_cache_dir = Some(cache.clone());
|
||||
let opened = metal2vulkan_rt::Device::new(&physical, options).and_then(NewRuntime::new);
|
||||
let opened = metal2vulkan_rt::Device::new(&physical, options)
|
||||
.and_then(|device| NewRuntime::new(device, device_type));
|
||||
match opened {
|
||||
Ok(backend) => return Ok((backend, name)),
|
||||
Err(why) => refused.push(format!("{name}: {why}")),
|
||||
|
||||
@@ -19,7 +19,8 @@
|
||||
//! A submission that completes with a device-fault status (Vulkan `VK_ERROR_DEVICE_LOST`; Metal
|
||||
//! `MTLCommandBufferError` timeout, page fault, access revoked, or device removed) is a suspected
|
||||
//! GPU fault: it writes the tripwire too, then fails the operation normally, because nothing is
|
||||
//! in flight any more. A CPU implementation (lavapipe, SwiftShader) is an ordinary killable
|
||||
//! in flight any more; a wait on the new runtime's command buffers ([`new_runtime`]) leaves how
|
||||
//! the command buffer ended to its caller instead. A CPU implementation (lavapipe, SwiftShader) is an ordinary killable
|
||||
//! process: its timeouts and faults follow the same exit rules but never write the host GPU
|
||||
//! tripwire.
|
||||
|
||||
@@ -423,6 +424,80 @@ pub(crate) mod runtime {
|
||||
}
|
||||
}
|
||||
|
||||
/// Bounded waits on the new runtime's command buffers (`metal2vulkan_rt`): the replay player's
|
||||
/// backend over it. The runtime's own wait has no bound, so the wait is on the command buffer's
|
||||
/// completed handler, after the queue's wait hint has sent it. How the command buffer ended is the
|
||||
/// caller's to read (the player reads its error into the play's verdict): a wait here says only
|
||||
/// that its work is no longer in flight, so an ending with a device fault writes no tripwire.
|
||||
pub(crate) mod new_runtime {
|
||||
use super::WaitOutcome;
|
||||
use super::{hang, judge, wait_timeout, CompletionSource, SubmissionBackend, Verdict};
|
||||
use metal2vulkan_rt::{CommandBuffer, CommandQueue, Error};
|
||||
use std::sync::{Arc, Condvar, Mutex, PoisonError};
|
||||
use std::time::Duration;
|
||||
|
||||
/// Whether a command buffer has retired: its completed handler ran, which the runtime runs
|
||||
/// once it has let go of everything the command buffer held.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Retired {
|
||||
done: Mutex<bool>,
|
||||
condvar: Condvar,
|
||||
}
|
||||
|
||||
impl Retired {
|
||||
/// The flag of `command_buffer`, not yet committed, which its completed handler sets.
|
||||
pub(crate) fn watch(command_buffer: &CommandBuffer) -> Result<Arc<Self>, Error> {
|
||||
let retired = Arc::new(Self::default());
|
||||
let flag = Arc::clone(&retired);
|
||||
command_buffer.add_completed_handler(move |_| {
|
||||
*flag.done.lock().unwrap_or_else(PoisonError::into_inner) = true;
|
||||
flag.condvar.notify_all();
|
||||
})?;
|
||||
Ok(retired)
|
||||
}
|
||||
|
||||
/// Whether the command buffer has retired.
|
||||
pub(crate) fn is_set(&self) -> bool {
|
||||
*self.done.lock().unwrap_or_else(PoisonError::into_inner)
|
||||
}
|
||||
}
|
||||
|
||||
struct Waited<'a>(&'a Retired);
|
||||
|
||||
impl CompletionSource for Waited<'_> {
|
||||
fn wait(&mut self, timeout: Duration) -> WaitOutcome {
|
||||
let done = self.0.done.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let (done, _) = (self.0.condvar)
|
||||
.wait_timeout_while(done, timeout, |done| !*done)
|
||||
.unwrap_or_else(PoisonError::into_inner);
|
||||
match *done {
|
||||
true => WaitOutcome::Completed,
|
||||
false => WaitOutcome::TimedOut,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Wait until `command_buffer`, committed on `queue`, has retired (`retired`), under the hang
|
||||
/// protocol: a wait past the process's bound records the hang, named by `operation`, and ends
|
||||
/// the process ([`HANG_EXIT_CODE`](super::HANG_EXIT_CODE)), so this returns only once it has
|
||||
/// retired.
|
||||
pub(crate) fn wait_until_retired(
|
||||
queue: &CommandQueue,
|
||||
command_buffer: &CommandBuffer,
|
||||
retired: &Retired,
|
||||
backend: SubmissionBackend,
|
||||
operation: &str,
|
||||
) {
|
||||
queue.send_now(command_buffer);
|
||||
// The handler's flag is set or the bound elapsed: completed or a hang, nothing else.
|
||||
if let Verdict::Hang(record) =
|
||||
judge(&mut Waited(retired), backend, operation, wait_timeout())
|
||||
{
|
||||
hang(&record)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Bounded waits on Metal command buffers. [`commit_and_wait`](metal::commit_and_wait) is public
|
||||
/// for the crate's binaries (the format scout's Metal side).
|
||||
#[cfg(target_os = "macos")]
|
||||
|
||||
@@ -106,11 +106,8 @@ impl NewRuntime {
|
||||
if !self.seconds.named() {
|
||||
return Ok(());
|
||||
}
|
||||
let queue = self.queue_of.get(&call.receiver).copied();
|
||||
let command_buffer = match queue.and_then(|queue| self.objects.get(&queue)) {
|
||||
Some(Object::Queue(queue)) => queue.command_buffer().map_err(failed(call))?,
|
||||
_ => return Err(refuse(call, "a commit of a command buffer of no queue")),
|
||||
};
|
||||
let queue = self.queue_for(call)?;
|
||||
let command_buffer = queue.command_buffer().map_err(failed(call))?;
|
||||
let mut blit = command_buffer
|
||||
.blit_command_encoder()
|
||||
.map_err(failed(call))?;
|
||||
@@ -123,7 +120,8 @@ impl NewRuntime {
|
||||
}
|
||||
}
|
||||
blit.end_encoding();
|
||||
self.commit(command_buffer, 0).map_err(failed(call))?;
|
||||
self.commit(&queue, command_buffer, 0)
|
||||
.map_err(failed(call))?;
|
||||
for place in places {
|
||||
self.places.give(place, self.commits, Vec::new());
|
||||
}
|
||||
@@ -172,7 +170,8 @@ impl NewRuntime {
|
||||
"parallelRenderCommandEncoderWithDescriptor:" => self.skip_encoder(),
|
||||
"commit" => {
|
||||
self.copy_named(call)?;
|
||||
self.commit(command_buffer, call.receiver)
|
||||
let queue = self.queue_for(call)?;
|
||||
self.commit(&queue, command_buffer, call.receiver)
|
||||
.map_err(failed(call))?;
|
||||
// Its first use committed, a no-copy Managed buffer's GPU copy changes only by
|
||||
// `didModifyRange:` from here.
|
||||
|
||||
@@ -22,12 +22,14 @@ use super::player::{Backend, Counters, HeldBuffer, Made, PlayError};
|
||||
use super::stream::Call;
|
||||
use super::translator;
|
||||
use super::value::Value;
|
||||
use crate::gpu::wait::new_runtime::{wait_until_retired, Retired};
|
||||
use crate::gpu::wait::SubmissionBackend;
|
||||
use crate::gpu::DeviceType;
|
||||
use metal2vulkan::pipeline::{LinkedFunction, LinkedFunctions};
|
||||
use metal2vulkan_rt as rt;
|
||||
use places::{class_index, Place, Places};
|
||||
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
|
||||
use std::ops::Range;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// What the play skipped because the runtime does not build it yet.
|
||||
@@ -196,6 +198,33 @@ pub enum ManagedCopies {
|
||||
Two,
|
||||
}
|
||||
|
||||
/// A committed command buffer not yet seen retired ([`NewRuntime::commit`]).
|
||||
struct InFlight {
|
||||
/// Its place in the commit order, from 1.
|
||||
commit: u64,
|
||||
/// Its stream id (0 for one of the play's own).
|
||||
id: u64,
|
||||
queue: rt::CommandQueue,
|
||||
command_buffer: rt::CommandBuffer,
|
||||
/// Set by its completed handler, which the runtime runs once it has let go of what the
|
||||
/// command buffer held, the textures and samplers it named with their indices among them,
|
||||
/// which its status alone does not say (the status changes first).
|
||||
retired: Arc<Retired>,
|
||||
}
|
||||
|
||||
impl InFlight {
|
||||
/// Waits until it has retired, the wait bounded under the hang protocol (`gpu::wait`).
|
||||
fn wait(&self, backend: SubmissionBackend, operation: &str) {
|
||||
wait_until_retired(
|
||||
&self.queue,
|
||||
&self.command_buffer,
|
||||
&self.retired,
|
||||
backend,
|
||||
operation,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The new runtime on one device.
|
||||
pub struct NewRuntime {
|
||||
device: rt::Device,
|
||||
@@ -215,10 +244,12 @@ pub struct NewRuntime {
|
||||
outside: HashSet<u64>,
|
||||
/// Each constexpr sampler's index, by its state words.
|
||||
constexprs: HashMap<[u64; 2], u32>,
|
||||
/// The commits so far, and the committed command buffers not yet seen retired, in order, each
|
||||
/// with the flag its completed handler sets ([`commit`](Self::commit)).
|
||||
/// The commits so far, and the committed command buffers not yet seen retired, in order
|
||||
/// ([`commit`](Self::commit)).
|
||||
commits: u64,
|
||||
in_flight: VecDeque<(u64, u64, rt::CommandBuffer, Arc<AtomicBool>)>,
|
||||
in_flight: VecDeque<InFlight>,
|
||||
/// The device's kind, which names the backend of every bounded wait (`gpu::wait`).
|
||||
backend: SubmissionBackend,
|
||||
/// The committed command buffers no record played so far lets complete, by stream id, as the
|
||||
/// player last told them (`Backend::held`): a placement never waits for one.
|
||||
held: HashMap<u64, HeldBuffer>,
|
||||
@@ -487,8 +518,9 @@ fn texture_class(usage: rt::TextureUsage) -> Option<rt::IndexClass> {
|
||||
}
|
||||
|
||||
impl NewRuntime {
|
||||
/// The backend on `device`: every texture and sampler index granted to it.
|
||||
pub fn new(device: rt::Device) -> Result<Self, rt::Error> {
|
||||
/// The backend on `device`, of `device_type`: every texture and sampler index granted to it,
|
||||
/// and each wait for a command buffer bounded under the hang protocol (`gpu::wait`).
|
||||
pub fn new(device: rt::Device, device_type: DeviceType) -> Result<Self, rt::Error> {
|
||||
for class in [rt::IndexClass::Texture, rt::IndexClass::Sampler] {
|
||||
let range = class.range();
|
||||
device.grant_indices(class, range.start, range.end - range.start)?;
|
||||
@@ -505,6 +537,7 @@ impl NewRuntime {
|
||||
constexprs: HashMap::new(),
|
||||
commits: 0,
|
||||
in_flight: VecDeque::new(),
|
||||
backend: SubmissionBackend::vulkan(device_type),
|
||||
held: HashMap::new(),
|
||||
sampler_descriptors: HashMap::new(),
|
||||
completed: 0,
|
||||
@@ -665,51 +698,64 @@ impl NewRuntime {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Commits `command_buffer`, the stream's `id` (0 for one of the play's own), as the next
|
||||
/// commit, in flight until its completed handler runs: the runtime runs it once it has let go
|
||||
/// of what the command buffer held, the textures and samplers it named with their indices
|
||||
/// among them, which its status alone does not say (the status changes first).
|
||||
fn commit(&mut self, command_buffer: rt::CommandBuffer, id: u64) -> Result<(), rt::Error> {
|
||||
let retired = Arc::new(AtomicBool::new(false));
|
||||
let flag = Arc::clone(&retired);
|
||||
command_buffer.add_completed_handler(move |_| flag.store(true, Ordering::Release))?;
|
||||
/// Commits `command_buffer`, of `queue` and the stream's `id` (0 for one of the play's own), as
|
||||
/// the next commit, in flight until its completed handler runs (`InFlight::retired`).
|
||||
fn commit(
|
||||
&mut self,
|
||||
queue: &rt::CommandQueue,
|
||||
command_buffer: rt::CommandBuffer,
|
||||
id: u64,
|
||||
) -> Result<(), rt::Error> {
|
||||
let retired = Retired::watch(&command_buffer)?;
|
||||
command_buffer.commit()?;
|
||||
self.commits += 1;
|
||||
self.in_flight
|
||||
.push_back((self.commits, id, command_buffer, retired));
|
||||
self.in_flight.push_back(InFlight {
|
||||
commit: self.commits,
|
||||
id,
|
||||
queue: queue.clone(),
|
||||
command_buffer,
|
||||
retired,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The queue of `call`'s receiver, a command buffer of the stream.
|
||||
fn queue_for(&self, call: &Call) -> Result<rt::CommandQueue, PlayError> {
|
||||
let queue = self.queue_of.get(&call.receiver);
|
||||
match queue.and_then(|queue| self.objects.get(queue)) {
|
||||
Some(Object::Queue(queue)) => Ok(queue.clone()),
|
||||
_ => Err(refuse(call, "a command buffer of no queue")),
|
||||
}
|
||||
}
|
||||
|
||||
/// The commits that have retired ([`commit`](Self::commit)), every earlier one with them.
|
||||
fn completed(&mut self) -> u64 {
|
||||
while let Some((at, _, _, retired)) = self.in_flight.front() {
|
||||
if !retired.load(Ordering::Acquire) {
|
||||
while let Some(front) = self.in_flight.front() {
|
||||
if !front.retired.is_set() {
|
||||
break;
|
||||
}
|
||||
self.completed = *at;
|
||||
self.completed = front.commit;
|
||||
self.in_flight.pop_front();
|
||||
}
|
||||
self.completed
|
||||
}
|
||||
|
||||
/// Waits until the commits up to `commit` have retired: `wait_until_completed` returns once the
|
||||
/// command buffer's handlers have run. Where one of them is held (`held`), no wait can end:
|
||||
/// that one's stream id, unwaited.
|
||||
/// Waits until the commits up to `commit` have retired, each wait bounded (`gpu::wait`): one
|
||||
/// past the bound ends the process as a hang. Where one of them is held (`held`), no wait can
|
||||
/// end: that one's stream id, unwaited.
|
||||
fn wait_for(&mut self, commit: u64) -> Result<(), u64> {
|
||||
let held = (self.in_flight.iter())
|
||||
.take_while(|(at, ..)| *at <= commit)
|
||||
.find(|(_, id, ..)| self.held.contains_key(id));
|
||||
if let Some(&(_, id, ..)) = held {
|
||||
return Err(id);
|
||||
.take_while(|flight| flight.commit <= commit)
|
||||
.find(|flight| self.held.contains_key(&flight.id));
|
||||
if let Some(flight) = held {
|
||||
return Err(flight.id);
|
||||
}
|
||||
while self.completed() < commit {
|
||||
let Some((_, _, front, retired)) = self.in_flight.front() else {
|
||||
let Some(front) = self.in_flight.front() else {
|
||||
return Ok(());
|
||||
};
|
||||
// Refused only in a scheduled handler, where the player never runs.
|
||||
if front.wait_until_completed().is_err() || !retired.load(Ordering::Acquire) {
|
||||
return Ok(());
|
||||
}
|
||||
let operation = "the replay's wait for an index's commits";
|
||||
front.wait(self.backend, operation);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -1933,12 +1979,11 @@ impl Backend for NewRuntime {
|
||||
fn wait(&mut self, command_buffer: u64) -> Result<(), PlayError> {
|
||||
match self.objects.get(&command_buffer) {
|
||||
Some(Object::CommandBuffer(waited)) => {
|
||||
waited
|
||||
.wait_until_completed()
|
||||
.map_err(|error| PlayError::Runtime {
|
||||
selector: "waitUntilCompleted".into(),
|
||||
error: error.to_string(),
|
||||
})?;
|
||||
// One not in flight has retired already.
|
||||
let flight = (self.in_flight.iter()).find(|flight| flight.id == command_buffer);
|
||||
if let Some(flight) = flight {
|
||||
flight.wait(self.backend, "the replay's wait for a command buffer");
|
||||
}
|
||||
match waited.error() {
|
||||
None => Ok(()),
|
||||
Some(error) => Err(PlayError::Runtime {
|
||||
|
||||
@@ -104,7 +104,7 @@ fn play(test: &str, path: &Path) -> (Report, Vec<String>) {
|
||||
/// runtime then gives each of `objects`.
|
||||
fn played(test: &str, path: &Path, objects: &[u64]) -> (Report, Vec<String>, Vec<u32>) {
|
||||
let cache = ScratchCache::new().unwrap();
|
||||
let mut backend = NewRuntime::new(rt_device(cache.path())).unwrap();
|
||||
let mut backend = NewRuntime::new(rt_device(cache.path()), gpu::TEST_DEVICE).unwrap();
|
||||
let mut reader = StreamReader::open(&[path.to_path_buf()], None).unwrap();
|
||||
let (report, ended) = Player::new(&mut backend).play(&mut reader);
|
||||
let lines = backend.lines();
|
||||
@@ -642,7 +642,7 @@ fn a_placement_a_held_commit_owes_stops_the_play_naming_what_holds_it() {
|
||||
let other = uints(&[("minFilter", 1), ("magFilter", 1)]);
|
||||
let path = held_placement(&dir, other);
|
||||
let cache = ScratchCache::new().unwrap();
|
||||
let mut backend = NewRuntime::new(rt_device(cache.path())).unwrap();
|
||||
let mut backend = NewRuntime::new(rt_device(cache.path()), gpu::TEST_DEVICE).unwrap();
|
||||
let mut reader = StreamReader::open(&[path], None).unwrap();
|
||||
let (_, ended) = Player::new(&mut backend).play(&mut reader);
|
||||
match ended {
|
||||
|
||||
Reference in new issue
Block a user