diff --git a/crates/metal2vulkan-runtime/src/device/carry.rs b/crates/metal2vulkan-runtime/src/device/carry.rs index 6491eea64..6f08b2c0e 100644 --- a/crates/metal2vulkan-runtime/src/device/carry.rs +++ b/crates/metal2vulkan-runtime/src/device/carry.rs @@ -506,7 +506,8 @@ impl DeviceShared { /// A lost generation whose recovery carried its objects on, the line serving past it and none /// of its command buffers left to record, on which no work is made again: its device-level /// objects of work (the internal submissions' and the staging queue's pools, the runtime's own - /// kernels, the GPU pools' blocks, the mipmap scratch), its parked buffers and images and the + /// kernels, the GPU pools' blocks, the mipmap scratch, the live buffers' table, + /// `LiveBuffers::close`), its parked buffers and images and the /// empty blocks it keeps go, and it parks and keeps none from now on. So the objects left on /// it hold the bare `VkDevice` and their own memory alone, and a carried object's memory there /// goes back to the device (`Recycler::close`, `memory::keep_none`). Its teardown destroys the @@ -519,6 +520,8 @@ impl DeviceShared { // Its queues idle, each under its lock: its submission checks' thread may submit there // still (`DeviceShared::queues_idle`). self.queues_idle(); + // Before the recycler closes: the table's buffers park there, and go with it. + self.live_buffers.close(); if let Some(internal) = lock(&self.internal).take() { internal.destroy(self.vk()); } diff --git a/crates/metal2vulkan-runtime/src/live_buffers.rs b/crates/metal2vulkan-runtime/src/live_buffers.rs index 15ed851e6..9a6c388b5 100644 --- a/crates/metal2vulkan-runtime/src/live_buffers.rs +++ b/crates/metal2vulkan-runtime/src/live_buffers.rs @@ -28,7 +28,8 @@ //! - **The table** (`LiveTable`). One resident set of every listed buffer, coalesced as a root //! table's (`bind::ResidentSet`), uploaded to a buffer of the runtime's own at a 32-byte-aligned //! device address. It is made at the first commit that needs it after a change of the list (a -//! buffer made or gone, a rebind), kept by the device until the next change, and shared by +//! buffer made or gone, a rebind), kept by the device until the next change (or, on a +//! generation a recovery left, its rest: `LiveBuffers::close`), and shared by //! every command buffer committed meanwhile. A command buffer holds it weakly from its commit //! to its first submission, which takes the table of the list then, the same one where the //! list is unchanged, and holds it, and with it each listed buffer's pages, until it completes @@ -106,11 +107,14 @@ struct Inner { rebinds: u64, /// The table of the current generation, once made: dropped at the next change, so a gone /// buffer's pages go with the last command buffer that holds an older table. It holds the - /// device only while a listed buffer lives, which holds it too. + /// device only while a listed buffer lives, which holds it too; on a generation a recovery + /// left, whose listed buffers it carried on, until that generation's rest + /// ([`close`](LiveBuffers::close)). current: Option>, /// The buffer of a table that no command buffer held when a change replaced it, which the /// next table's set is written into where it fits; kept only while a buffer is listed, so it - /// holds the device only while a listed buffer does. + /// holds the device only while a listed buffer does, and as [`current`](Self::current) on a + /// generation a recovery left. spare: Option, } @@ -300,6 +304,23 @@ impl LiveBuffers { lock(&self.inner).generation } + /// The device, a generation a recovery left, came to rest (`device/carry.rs`): none of its + /// command buffers records, nor is one made there again, so none takes or holds a table of + /// it. Its table and spare, buffers of the runtime's own there, go: they held the device + /// while a listed buffer lived, which held it too, but the recovery carried the listed + /// buffers on, and they hold it no longer, so the lost `VkDevice` would live while they do, + /// and past them (the table lives until the next change, which no command buffer makes + /// there). + pub(super) fn close(&self) { + let (table, spare) = { + let mut inner = lock(&self.inner); + (inner.current.take(), inner.spare.take()) + }; + // Dropped with no lock held: a last hold's drop frees pages. + drop(table); + drop(spare); + } + /// The table of the live buffers now: the one made since the last change of the list, else a /// new one, which the device keeps until the next change. Refused as `device` refuses its upload. pub(super) fn table(&self, device: &Device) -> Result, Error> { diff --git a/crates/metal2vulkan-runtime/src/tests/device_recovery_live_table.rs b/crates/metal2vulkan-runtime/src/tests/device_recovery_live_table.rs new file mode 100644 index 000000000..3c8606fc6 --- /dev/null +++ b/crates/metal2vulkan-runtime/src/tests/device_recovery_live_table.rs @@ -0,0 +1,133 @@ +//! A lost generation's table of the live buffers (`live_buffers.rs`) goes at the generation's +//! rest (`LiveBuffers::close`), so the lost `VkDevice` goes while the buffers the table listed +//! live on, carried: the table's buffer, of the runtime's own there, held the generation while a +//! listed buffer lived, and the recovery carried the listed buffers on. +//! +//! A device test: compiled only under the validation crate's GPU safety cfgs, on the kind of device +//! the cfg selects (docs/VALIDATION.md). No GPU fault: the loss is marked (`Device::recover_for_test`). +//! The function is the owned fixture `examples/runtime_examples.ll` (`llvm-as`). + +use super::device_recovery::{device, outcome, Scratch}; +use super::device_recovery_carry::{fill, live_vk_devices, read, BYTES}; +use super::encoder_holds::library; +use crate::{ + Buffer, CommandBufferStatus, CommandQueue, ComputePipelineState, Device, IndirectCommandBuffer, + IndirectCommandBufferDescriptor, PipelineOptions, ResourceOptions, ResourceRef, Size, +}; +use metal2vulkan::metal::{IndirectCommandType, ResourceUsage}; +use metal2vulkan::pipeline::{ComputePipelineDescriptor, StageSource}; + +/// The floats an indirect command's dispatch writes, `i` each. +const WORDS: u64 = 64; + +/// `scale_and_offset` of `device`'s example library, for indirect command buffers. +pub(super) fn indirect_state(device: &Device) -> ComputePipelineState { + let function = library(device, "examples/runtime_examples.ll") + .new_function_with_name("scale_and_offset") + .unwrap(); + let mut descriptor = + ComputePipelineDescriptor::new(StageSource::Function(function.function_descriptor())); + descriptor.support_indirect_command_buffers = true; + device + .new_compute_pipeline_state(&descriptor, &PipelineOptions::default()) + .unwrap() +} + +/// An indirect command buffer of `device` of one concurrent dispatch of 3 kernel buffers, in +/// `Shared` storage. +pub(super) fn dispatches(device: &Device) -> IndirectCommandBuffer { + let mut descriptor = IndirectCommandBufferDescriptor::new(); + descriptor.command_types = IndirectCommandType::CONCURRENT_DISPATCH_THREADS; + descriptor.max_kernel_buffer_bind_count = 3; + device + .new_indirect_command_buffer(&descriptor, 1, ResourceOptions::shared()) + .unwrap() +} + +/// Encode `icb`'s command 0 as `scale_and_offset` of `state` from `input` into `output`, scaled +/// by `scale`, over [`WORDS`] threads. +pub(super) fn encode_command( + icb: &IndirectCommandBuffer, + state: &ComputePipelineState, + [input, output, scale]: [&Buffer; 3], +) { + let command = icb.indirect_compute_command_at(0).unwrap(); + command.set_compute_pipeline_state(state).unwrap(); + command.set_kernel_buffer(Some(input), 0, 0).unwrap(); + command.set_kernel_buffer(Some(output), 0, 1).unwrap(); + command.set_kernel_buffer(Some(scale), 0, 2).unwrap(); + command + .concurrent_dispatch_threads(Size::new(WORDS, 1, 1), Size::new(32, 1, 1)) + .unwrap(); +} + +/// `icb`'s command 0 executed by `queue`'s GPU work, its buffers resident, after `output` is set +/// to all-ones bytes and `input` and `scale` to zero, completed: `output`'s floats. +pub(super) fn execute( + device: &Device, + queue: &CommandQueue, + icb: &IndirectCommandBuffer, + [input, output, scale]: [&Buffer; 3], +) -> Vec { + fill(queue, input, 0); + fill(queue, scale, 0); + fill(queue, output, 0xff); + let command_buffer = queue.command_buffer().unwrap(); + let mut encoder = command_buffer.compute_command_encoder().unwrap(); + for (buffer, usage) in [ + (input, ResourceUsage::READ), + (output, ResourceUsage::WRITE), + (scale, ResourceUsage::READ), + ] { + (encoder.use_resource(ResourceRef::Buffer(buffer), usage)).unwrap(); + } + encoder.execute_commands_in_buffer(icb, 0..1).unwrap(); + encoder.end_encoding().unwrap(); + command_buffer.commit().unwrap(); + assert_eq!( + outcome(&command_buffer), + (CommandBufferStatus::Completed, None) + ); + let bytes = read(device, queue, output).unwrap(); + (bytes.as_chunks::<4>().0.iter()) + .take(WORDS as usize) + .map(|word| f32::from_le_bytes(*word)) + .collect() +} + +/// What [`execute`] reads back from a dispatch that ran: `i` each. +pub(super) fn dispatched() -> Vec { + (0..WORDS).map(|i| i as f32).collect() +} + +/// An indirect command buffer's execution makes the generation's table of the live buffers, to +/// resolve its records' addresses (`CommandBuffer::residency`); the buffer and its state go +/// before the recovery, its three buffers live past it. The lost `VkDevice` goes while they +/// live, and stays gone once the program lets go of them; without the table's close at the +/// generation's rest, two holds of the lost generation stayed for the life of the process. +#[test] +fn a_lost_generation_s_live_buffers_table_goes_at_its_rest() { + let scratch = Scratch::new(); + let device = device(&scratch.0); + let queue = device.new_command_queue().unwrap(); + let compute = indirect_state(&device); + let icb = dispatches(&device); + let private = || { + device + .new_buffer(BYTES, ResourceOptions::private()) + .unwrap() + }; + let (input, output, scale) = (private(), private(), private()); + let buffers = [&input, &output, &scale]; + encode_command(&icb, &compute, buffers); + let tables = device.work_counts().live_tables_uploaded; + assert_eq!(execute(&device, &queue, &icb, buffers), dispatched()); + // The execution made the generation's table: the path under test ran. + assert!(device.work_counts().live_tables_uploaded > tables); + drop(icb); + drop(compute); + device.recover_for_test().unwrap(); + assert_eq!(live_vk_devices(&device, 1), 1, "the buffers live"); + drop((input, output, scale, queue)); + assert_eq!(live_vk_devices(&device, 1), 1, "all let go of"); +} diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs index 584efbb2e..9b58e9dd2 100644 --- a/crates/metal2vulkan-runtime/src/tests/mod.rs +++ b/crates/metal2vulkan-runtime/src/tests/mod.rs @@ -149,6 +149,8 @@ mod device_recovery_handles; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod device_recovery_heap; #[cfg(any(m2v_gpu, m2v_cpu_icd))] +mod device_recovery_live_table; +#[cfg(any(m2v_gpu, m2v_cpu_icd))] mod device_recovery_no_copy; #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod device_recovery_no_copy_heap;