diff --git a/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs b/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs index 52f63770c..71bbe4b3d 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_ram_device.rs @@ -58,8 +58,8 @@ //! //! - **Shared RAM a device refuses.** On a device that refuses pointers to shared memory (RADV), //! a RAM of a `memfd` mapped shared with no dma-buf source imports no chunk, and an object over -//! it is refused (`Unsupported`, `HostMemory`); the device's thread reaches its first chunk and -//! faults none of it in. Where the device imports such memory, the thread faults each chunk it +//! it is a copy of its pages (`HostBacking::Copied`); the device's thread reaches its first chunk +//! and faults none of it in. Where the device imports such memory, the thread faults each chunk it //! imports ahead in first (on Linux). //! //! Every other test holds the device's thread back (`Device::hold_host_ram_imports`), so that @@ -71,14 +71,13 @@ use super::dispatch_pending::state; use crate::host_ram::AHEAD_CHUNKS; use crate::{ - Buffer, CommandBufferStatus, Device, DeviceKind, DeviceOptions, Error, EventRef, + Buffer, CommandBufferStatus, Degradation, Device, DeviceKind, DeviceOptions, Error, EventRef, HeapDescriptor, HeapType, HostBacking, HostMemory, HostRam, ImportGate, ImportRequest, - KeepAlive, Problem, ResourceOptions, RunResolver, Size, + KeepAlive, MessageSink, Problem, ResourceOptions, RunResolver, Size, }; -use metal2vulkan::error::{Feature, Unsupported}; use std::ops::Range; use std::ptr::NonNull; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; @@ -160,6 +159,11 @@ fn device() -> Option { /// A device of the cfg's kind, as the product makes it; `None` where it imports no host memory. pub(super) fn ahead_device() -> Option { + ahead_device_with(None) +} + +/// [`ahead_device`], its messages given to `messages`. +fn ahead_device_with(messages: Option) -> Option { let options = DeviceOptions { kinds: match cfg!(m2v_cpu_icd) { true => vec![DeviceKind::Cpu], @@ -169,6 +173,7 @@ pub(super) fn ahead_device() -> Option { DeviceKind::Virtual, ], }, + messages, ..DeviceOptions::default() }; let device = Device::system_default_with(&options).unwrap(); @@ -188,6 +193,17 @@ pub(super) fn ahead_device() -> Option { Some(device) } +/// A sink counting into `heard` the `copied-host-pages` notices it hears. +pub(super) fn copies_heard(heard: &Arc) -> MessageSink { + let heard = Arc::clone(heard); + MessageSink::new(move |message| { + let notice = message.degradation.as_ref(); + if notice.is_some_and(|notice| notice.degradation == Degradation::CopiedHostPages) { + heard.fetch_add(1, Ordering::Relaxed); + } + }) +} + /// A resolver answering `answer` of each window it is asked, each window into `asked`. pub(super) fn resolver( asked: &Arc>>, @@ -276,7 +292,7 @@ fn settles(flag: &AtomicBool) -> bool { /// `memory-backend-memfd,share=on` RAM is (anonymous shared memory off Linux), on a device that /// imports pointers to shared memory (`Device::imports_shared_host_pointers`), and private /// anonymous memory, as QEMU's `memory-backend-ram` RAM is, on one that refuses them (RADV: -/// [`a_ram_of_shared_memory_a_device_refuses_imports_no_chunk_and_refuses_its_objects`] checks +/// [`a_ram_of_shared_memory_a_device_refuses_imports_no_chunk_and_copies_its_objects`] checks /// shared RAM there); its first byte, and whether its keep-alive was dropped. pub(super) fn fresh_ram(device: &Device, length: u64) -> (Arc, u64, Arc) { fresh_ram_in(device, length, RamPages::Huge) @@ -796,14 +812,16 @@ fn the_window_ahead_moves_with_the_objects_and_imports_no_more() { /// On a device that refuses pointers to shared memory (`Device::imports_shared_host_pointers` /// false: RADV, whose amdgpu userptr takes private anonymous pages alone), a RAM of shared memory /// (a `memfd` mapped shared, as QEMU's `memory-backend-memfd,share=on` RAM is) imports no chunk, -/// ahead or for an object, and an object over it is refused as its own import of the pointer is -/// (`Unsupported`, `HostMemory`), the RAM given no dma-buf source: the chunk's import by its -/// pointer is refused (`WorkCounts::host_ram_pointer_imports_failed`) and none goes as a dma-buf -/// (`host_ram_dma_buf_imports`). The use still ends, the RAM's keep-alive going after it. A device -/// that imports such pointers says so and passes. +/// ahead or for an object, and an object over it is a copy of its pages, the RAM given no dma-buf +/// source ([`HostBacking::Copied`], counted in `WorkCounts::copied_host_buffers_device_refused` +/// and announced, `copied-host-pages`): the chunk's import by its pointer is refused +/// (`host_ram_pointer_imports_failed`) and none goes as a dma-buf (`host_ram_dma_buf_imports`). +/// The use still ends, the RAM's keep-alive going after it. A device that imports such pointers +/// says so and passes. #[test] -fn a_ram_of_shared_memory_a_device_refuses_imports_no_chunk_and_refuses_its_objects() { - let Some(device) = ahead_device() else { +fn a_ram_of_shared_memory_a_device_refuses_imports_no_chunk_and_copies_its_objects() { + let heard = Arc::new(AtomicUsize::new(0)); + let Some(device) = ahead_device_with(Some(copies_heard(&heard))) else { return; }; if device.imports_shared_host_pointers().unwrap() { @@ -825,16 +843,21 @@ fn a_ram_of_shared_memory_a_device_refuses_imports_no_chunk_and_refuses_its_obje resolver(&runs, itself), gate(&gates, true), ); + let made = made.unwrap(); assert_eq!( - made.err(), - Some(Error::Unsupported(Unsupported::not_representable( - Feature::HostMemory - ))), + made.host_backing(), + Some(HostBacking::Copied), "an object over the shared RAM" ); // The device's thread met the same refusal for the RAM's first chunk, or meets it now. std::thread::sleep(Duration::from_millis(300)); let after = device.work_counts(); + assert_eq!( + after.copied_host_buffers_device_refused - before.copied_host_buffers_device_refused, + 1, + "the object copied for the device's refusal" + ); + assert_eq!(heard.load(Ordering::Relaxed), 1, "the copy announced"); assert_eq!( after.host_ram_chunks_imported - before.host_ram_chunks_imported, 0, @@ -850,8 +873,7 @@ fn a_ram_of_shared_memory_a_device_refuses_imports_no_chunk_and_refuses_its_obje ); assert!(failed >= 1, "the chunk's pointer import was refused"); assert_eq!(dma_bufs, 0, "no dma-buf without a source"); - drop(used); - drop(ram); + drop((made, used, ram)); assert!(settles(&dropped), "the RAM's keep-alive dropped"); } diff --git a/crates/metal2vulkan-runtime/src/tests/host_ram_dma_buf.rs b/crates/metal2vulkan-runtime/src/tests/host_ram_dma_buf.rs index aa175e932..35ce7e707 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_ram_dma_buf.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_ram_dma_buf.rs @@ -52,18 +52,18 @@ //! //! A device test: compiled only under the GPU safety cfgs (`m2v_gpu`, `m2v_cpu_icd`), on Linux. -use super::host_ram_device::runs_alone; -use super::host_residency::{device, udmabuf, udmabuf_list, Scratch}; +use super::host_ram_device::{copies_heard, runs_alone}; +use super::host_residency::{device, device_with, udmabuf, udmabuf_list, Scratch}; use crate::submit_check::{Check, Verdict}; use crate::{ Buffer, CommandBufferStatus, Device, DmaBufChunk, DmaBufRequest, DmaBufRunsSource, - DmaBufSource, Error, HostBacking, HostMemory, HostRam, KeepAlive, Origin, ResourceOptions, - RunResolver, Size, TextureDescriptor, + DmaBufSource, Error, HostBacking, HostMemory, HostRam, KeepAlive, MessageSink, Origin, + ResourceOptions, RunResolver, Size, TextureDescriptor, }; use metal2vulkan::metal::{PixelFormat, TextureUsage}; use std::os::fd::{AsRawFd, FromRawFd, OwnedFd}; use std::ptr::NonNull; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; @@ -275,12 +275,13 @@ fn fill_pattern(view: u64, end: u64) { /// A device of the cfg's kind, its checks answered, whose uses of a RAM import each chunk at /// the first object over it (`Device::hold_host_ram_imports`), as one the device's thread has -/// not reached is; `None` where `/dev/udmabuf` cannot be opened or it imports no host memory. -fn held_device(scratch: &Scratch) -> Option { +/// not reached is, its messages given to `messages`; `None` where `/dev/udmabuf` cannot be +/// opened or it imports no host memory. +fn held_device(scratch: &Scratch, messages: Option) -> Option { if !udmabuf_openable() { return None; } - let device = device(&scratch.0); + let device = device_with(&scratch.0, messages); if device.shared().host_import.is_err() { eprintln!("HOST-RAM-DMA-BUF: the device imports no host memory here; nothing to test"); return None; @@ -335,7 +336,7 @@ fn udmabuf_openable() -> bool { #[test] fn a_ram_with_a_source_takes_the_route_its_device_s_checks_choose() { let scratch = Scratch::new(); - let Some(device) = held_device(&scratch) else { + let Some(device) = held_device(&scratch, None) else { return; }; let taxed = device.pointer_imports_tax_submissions(); @@ -482,7 +483,7 @@ fn a_ram_with_a_source_takes_the_route_its_device_s_checks_choose() { #[test] fn where_the_pointer_goes_first_a_refused_one_takes_the_dma_buf() { let scratch = Scratch::new(); - let Some(device) = held_device(&scratch) else { + let Some(device) = held_device(&scratch, None) else { return; }; (device.shared().submit_checks).force(Check::ImportTax, Verdict::Fails); @@ -525,12 +526,15 @@ fn where_the_pointer_goes_first_a_refused_one_takes_the_dma_buf() { } /// With no source, a chunk whose pointer the device refuses is not imported, and the object over -/// it takes its own import, refused as its pointer is (`Unsupported`, `HostMemory`), nothing -/// taken as a dma-buf; a device that takes the pointer binds the object in the chunk. +/// it is a copy of its pages ([`HostBacking::Copied`], counted in +/// `WorkCounts::copied_host_buffers_device_refused` and announced, `copied-host-pages`), nothing +/// taken as a dma-buf; a device that takes the pointer binds the object in the chunk, announcing +/// nothing. #[test] -fn without_a_source_a_refused_pointer_refuses_the_object() { +fn without_a_source_a_refused_pointer_copies_the_object() { let scratch = Scratch::new(); - let Some(device) = held_device(&scratch) else { + let heard = Arc::new(AtomicUsize::new(0)); + let Some(device) = held_device(&scratch, Some(copies_heard(&heard))) else { return; }; let (ram, start, view, asked, _dropped) = ram(false); @@ -555,13 +559,20 @@ fn without_a_source_a_refused_pointer_refuses_the_object() { match (failed, made) { (0, Ok(buffer)) => { assert_eq!(buffer.host_backing(), Some(HostBacking::InHostRam)); + assert_eq!(heard.load(Ordering::Relaxed), 0, "nothing announced"); assert_eq!( after.host_ram_chunks_imported - before.host_ram_chunks_imported, 1 ); } - (_, Err(Error::Unsupported(refusal))) => { - assert_eq!(refusal.feature, metal2vulkan::error::Feature::HostMemory); + (1.., Ok(buffer)) => { + assert_eq!(buffer.host_backing(), Some(HostBacking::Copied)); + assert_eq!(heard.load(Ordering::Relaxed), 1, "the copy announced"); + assert_eq!( + after.copied_host_buffers_device_refused + - before.copied_host_buffers_device_refused, + 1 + ); assert_eq!( after.host_ram_chunks_imported, before.host_ram_chunks_imported diff --git a/crates/metal2vulkan-runtime/src/tests/host_residency.rs b/crates/metal2vulkan-runtime/src/tests/host_residency.rs index cfd0f2a8e..cddd63540 100644 --- a/crates/metal2vulkan-runtime/src/tests/host_residency.rs +++ b/crates/metal2vulkan-runtime/src/tests/host_residency.rs @@ -33,7 +33,7 @@ use crate::{ Buffer, CommandQueue, Device, DeviceKind, DeviceOptions, DmaBufChunk, DmaBufRequest, - DmaBufSource, HostMemory, ResourceOptions, ShaderCacheOptions, WorkCounts, + DmaBufSource, HostMemory, MessageSink, ResourceOptions, ShaderCacheOptions, WorkCounts, }; use std::os::fd::{AsRawFd, FromRawFd, OwnedFd}; use std::path::{Path, PathBuf}; @@ -199,6 +199,11 @@ impl Drop for Scratch { /// The device the cfg selects: a CPU device under `m2v_cpu_icd`, else a GPU, its shader cache /// in `cache`, its submission checks answered. pub(super) fn device(cache: &Path) -> Device { + device_with(cache, None) +} + +/// [`device`], its messages given to `messages`. +pub(super) fn device_with(cache: &Path, messages: Option) -> Device { let options = DeviceOptions { kinds: match cfg!(m2v_cpu_icd) { true => vec![DeviceKind::Cpu], @@ -212,6 +217,7 @@ pub(super) fn device(cache: &Path) -> Device { directory: Some(cache.to_owned()), ..ShaderCacheOptions::default() }, + messages, ..DeviceOptions::default() }; let device = Device::system_default_with(&options).unwrap(); diff --git a/crates/metal2vulkan-runtime/src/tests/pointer_residency.rs b/crates/metal2vulkan-runtime/src/tests/pointer_residency.rs index 5a1669331..ecdb1afeb 100644 --- a/crates/metal2vulkan-runtime/src/tests/pointer_residency.rs +++ b/crates/metal2vulkan-runtime/src/tests/pointer_residency.rs @@ -4,7 +4,8 @@ //! The memory is guest RAM as QEMU's `memory-backend-memfd,share=on` makes it, a sealed `memfd` //! mapped shared, imported by its pointer (`HostMemory::new`), as ANV, NVIDIA and lavapipe import //! it; on a device that refuses that pointer (RADV, which imports the `memfd`'s dma-bufs, -//! `tests/host_residency.rs`), private anonymous pages, which RADV imports by their pointer. Where +//! `tests/host_residency.rs`, and makes a no-copy buffer over it with no source a copy of its +//! pages), private anonymous pages, which RADV imports by their pointer. Where //! the device's check found its pointer imports follow their address (`submit_check.rs`, //! `Device::pointer_imports_follow_their_address`: ANV on i915, RADV and lavapipe answer so), the //! device releases such an import while idle and imports it again at the same pointer at its next @@ -34,7 +35,7 @@ use super::host_residency::{ counting, device, gpu_fill, gpu_read, host_page, read, release, residency, Ram, Scratch, }; -use crate::{Buffer, Device, Error, HostMemory, ResourceOptions}; +use crate::{Buffer, Device, Error, HostBacking, HostMemory, ResourceOptions}; use metal2vulkan::error::Feature; use std::ptr::NonNull; @@ -178,9 +179,10 @@ impl Drop for Pages { } /// A no-copy buffer over `bytes.len()` bytes of new pages holding `bytes`, imported by its -/// pointer: guest RAM's view, or, where the device refuses its pointer, private anonymous pages -/// (the module docs). `None`, printed, where the device refuses both, which a device with the fact -/// never does. The buffer drops before the pages. +/// pointer: guest RAM's view, or, where the device refuses its pointer (the call refused, or the +/// buffer a copy of the pages, `HostBacking::Copied`), private anonymous pages (the module docs). +/// `None`, printed, where the device refuses both, which a device with the fact never does. The +/// buffer drops before the pages. fn no_copy(test: &str, device: &Device, bytes: &[u8]) -> Option<(Pages, Buffer)> { let mut refused = Vec::new(); for pages in [Pages::shared(bytes.len()), Pages::private(bytes.len())] { @@ -196,6 +198,9 @@ fn no_copy(test: &str, device: &Device, bytes: &[u8]) -> Option<(Pages, Buffer)> ) }; match made { + Ok(buffer) if buffer.host_backing() == Some(HostBacking::Copied) => { + refused.push(format!("{}: copied", pages.kind())); + } Ok(buffer) => { let imported = device.work_counts().host_imports_live - before; assert_eq!(imported, 1, "{test}: one import of the pointer");