diff --git a/crates/metal2vulkan-rt/src/memory.rs b/crates/metal2vulkan-rt/src/memory.rs index 50a7271b5..e82992d3d 100644 --- a/crates/metal2vulkan-rt/src/memory.rs +++ b/crates/metal2vulkan-rt/src/memory.rs @@ -318,8 +318,8 @@ impl Drop for Block { } /// One `VkDeviceMemory` of a class and the ranges of it no block holds. -struct Allocation { - memory: vk::DeviceMemory, +pub(crate) struct Allocation { + pub(crate) memory: vk::DeviceMemory, len: u64, mapping: Option, device_local: bool, @@ -1238,7 +1238,11 @@ fn new_buffer(device: &ash::Device, len: u64) -> Result { /// Makes an allocation of `len` bytes of `class` with device addresses, mapped whole if /// Shared, its free ranges all of it. Its bytes are undefined: nothing writes them, and a Shared /// allocation's pages become resident as they are first written. -fn make_allocation(core: &Core, class: MemoryClass, len: u64) -> Result { +pub(crate) fn make_allocation( + core: &Core, + class: MemoryClass, + len: u64, +) -> Result { let device = &core.device; let buffer = vk::BufferCreateInfo::default() .size(len) @@ -1367,7 +1371,7 @@ fn free_idle(core: &Core, allocation: Allocation, kept: &[Memory]) { } /// Frees an allocation, whose blocks are all retired. Freeing the memory also unmaps it. -fn free_allocation(core: &Core, allocation: Allocation) { +pub(crate) fn free_allocation(core: &Core, allocation: Allocation) { // SAFETY: no buffer is bound to it any more, and no GPU work uses it. unsafe { core.device.free_memory(allocation.memory, None) }; core.counters.fall(Gauge::Blocks); diff --git a/crates/metal2vulkan-rt/src/record.rs b/crates/metal2vulkan-rt/src/record.rs index 7b0cf0a9b..62a2e4faa 100644 --- a/crates/metal2vulkan-rt/src/record.rs +++ b/crates/metal2vulkan-rt/src/record.rs @@ -183,13 +183,22 @@ impl Recorders { } /// Takes back a recorder whose work has completed, or that never submitted: its pool is reset - /// for the next one, or, past [`IDLE_MAX`], given to the reaper to destroy. + /// for the next one, its memory given back to the driver, or, past [`IDLE_MAX`], given to the + /// reaper to destroy. + /// + /// The reset releases the pool's memory, so a recorder that once recorded a long command + /// buffer keeps none of it. A pool reset keeping its memory made NVIDIA's Linux driver hold + /// the next `vkQueueSubmit2` of the pool's command buffers until any `vkFreeMemory` of + /// host-visible memory beside it ended: 20 to 70 ms beside a 64 MiB free on an RTX 2080 and + /// an RTX 3090, against 0.2 ms with the memory released, as with a new pool. The release + /// cost no measurable time per command buffer there, on a Radeon RX 7900 XTX or on an Iris Xe + /// (probe 6be5985659, jobs m3-max-46001 to m3-max-46004). pub(crate) fn give_back(&self, core: &Core, mut recorder: Recorder) { // SAFETY: no command buffer of the pool is pending. A failed reset (a lost device) leaves // the pool to be destroyed. let reset = unsafe { core.device - .reset_command_pool(recorder.pool, vk::CommandPoolResetFlags::empty()) + .reset_command_pool(recorder.pool, vk::CommandPoolResetFlags::RELEASE_RESOURCES) }; let mut idle = lock(&self.idle); if reset.is_ok() && idle.len() < IDLE_MAX { diff --git a/crates/metal2vulkan-rt/src/tests/memory.rs b/crates/metal2vulkan-rt/src/tests/memory.rs index a570d29de..7d3e000a2 100644 --- a/crates/metal2vulkan-rt/src/tests/memory.rs +++ b/crates/metal2vulkan-rt/src/tests/memory.rs @@ -130,6 +130,8 @@ fn the_idle_rule_keeps_idle_allocations_under_the_window_s_peak() { #[cfg(any(m2v_gpu, m2v_cpu_icd))] mod on_device { use crate::device::lock; + use crate::memory::{free_allocation, make_allocation}; + use crate::tests::command::on_device::each_device; use crate::tests::devices_of_the_cfgs_kind; use crate::{ Block, BlockId, BlockInfo, BlockMappings, Buffer, CommandBufferStatus, Device, @@ -1197,6 +1199,85 @@ mod on_device { } } + /// A commit is not held for a free of host-visible memory beside it: a command buffer + /// committed 1 ms into the free of a 64 MiB Shared allocation the GPU used completes in less + /// than half the free's time. NVIDIA's Linux driver held the submission of a command buffer + /// from a pool reset keeping its memory for the whole of such a free (20 to 70 ms on an RTX + /// 2080 and 3090); the recorders' pools are reset releasing it. A device whose free takes + /// under 10 ms only prints its times. + #[test] + fn every_device_completes_a_commit_beside_a_free_of_shared_memory_unheld() { + each_device("commit beside a free", |name, _, device| { + let core = device.core(); + let queue = device.new_command_queue(4).expect("a queue"); + let target = device + .take_block(MemoryClass::Private, 4096) + .expect("a block"); + let kept = device + .new_buffer(target.id, 0, 256, None) + .expect("a buffer"); + let to = device.block(target.id).expect("a held block").memory.buffer; + let commit = |fill: vk::Buffer| { + let command_buffer = queue.command_buffer().expect("a command buffer"); + command_buffer + .record_for_test(&[&kept], |device, command| unsafe { + device.cmd_fill_buffer(command, fill, 0, 256, 7) + }) + .expect("a fill"); + let started = Instant::now(); + command_buffer.commit().expect("a commit"); + command_buffer.wait_until_completed(); + started.elapsed() + }; + // The queue's recorders reused, as a program's are. + for _ in 0..3 { + commit(to); + } + for _ in 0..3 { + let allocation = + make_allocation(core, MemoryClass::Shared, 64 << 20).expect("an allocation"); + let info = vk::BufferCreateInfo::default() + .size(256) + .usage(crate::floor::BUFFER_USAGE) + .sharing_mode(vk::SharingMode::EXCLUSIVE); + // SAFETY: the device is live; the buffer is bound once, to memory of its usage's + // type, and destroyed once the fill that uses it completed. + let used = unsafe { + let used = core.device.create_buffer(&info, None).expect("a buffer"); + (core.device) + .bind_buffer_memory(used, allocation.memory, 0) + .expect("its memory"); + used + }; + commit(used); + // SAFETY: the fill that used it completed. + unsafe { core.device.destroy_buffer(used, None) }; + let (freed, completed) = thread::scope(|scope| { + let committed = scope.spawn(|| { + thread::sleep(Duration::from_millis(1)); + commit(to) + }); + let started = Instant::now(); + free_allocation(core, allocation); + let freed = started.elapsed(); + (freed, committed.join().expect("the commit")) + }); + println!( + "{name}: a 64 MiB Shared free took {freed:?}; a commit 1 ms into it completed \ + in {completed:?}" + ); + if freed >= Duration::from_millis(10) { + assert!( + completed < freed / 2, + "{name}: the commit waited for the free: {completed:?} of {freed:?}" + ); + } + thread::sleep(Duration::from_millis(100)); + } + drop((kept, target, queue)); + }); + } + /// Idle allocations are freed only in quiet gaps, and a submission during a free is counted. /// Four Shared blocks of 16 MiB are given back while command buffers are committed every /// 2 ms; the clock moves past the window, so the idle rule frees all four but the reserve, yet