Files
metal2vulkan/crates/metal2vulkan-runtime/src/memory.rs
T
Anees Iqbal 6a14aa4192 rt: the parameter ring frees its idle blocks past the first
What: the ring's idle rule (Ring::due), run on the block-maker thread beside the pool's: a block
past the first goes once every chunk of it has been free for the idle age (250 ms), where the
ring, at its fewest free chunks since the block fell idle, kept more than a block's chunks free
without it, so neither a take in that time nor the next would have asked for a block. The fewest
is set at the give-back that made the block idle, from the chunks the ring has free once that
give-back is done, and falls with each take. The first block stays, and so does a block holding
the nil buffer, the zero range or the whole range. A take now takes a chunk of the oldest block
with one free, so the newest blocks fall idle first; each block keeps its own free chunks. A
block past the first that falls idle wakes the block-maker thread (Pool::nudge); the thread
frees it as memory of its own (Loose::RingBlock: its fault span noted freed, the buffer
destroyed, the memory freed, the blocks gauge falling). A block that does not meet the rule now
cannot meet it in the same idle time, so the thread sets no wake for it.

Why: the planner's cache ruling (1). The ring kept every block it made for the device's life, so
a burst of parameter writes left its peak of 2 MiB blocks of the ring type (device-local and
host-visible, small without resizable BAR) until the device went. Freed at idle alone, a ring in
steady use would free its second block and make it again each idle age. A second condition, no
take for the idle age, which took an idle ring down to its first block, was dropped by the
planner's ruling: whether a take waited for a block it then made again could not be read on a
recorded play, so an idle ring keeps its first block and the one ahead of it.

Docs: the ring module doc, Ring::due, Pool::nudge, memory::run, the runtime README's internals
paragraph.

Verified: red on the RX 7900 XTX (RADV 26.2.6) with the parent's runtime and this branch's tests,
m3-max-79046: the burst test timed out waiting for the ring back to its first block and the one
ahead; the steady-use test passed (nothing is freed). Green in the same job at this code: the
ring, ahead and memory modules, 33 passed, "6 blocks after the burst, 2 once idle; the next take
made none". At this tree (f882510ace, the same tree): clippy -D warnings plain, under m2v_cpu_icd
and under m2v_gpu, rustdoc -D warnings, and the workspace's rustdoc of private items, all exit 0;
the runtime's suite on lavapipe 904 passed, 3 skipped, with the same line (m3-max-79050).

The cost line, one job on the Radeon 780M (RADV PHOENIX, Mesa 26.2.6), the release replay player
on stream glbench-780m-r2 with a fresh pipeline cache each play, arms a (the parent's runtime)
and b (this one) alternated a b a b (m3-max-79045): 792 frames each; blocks made a 300 and 299, b
300 and 299, 37.88 and 37.75 per 100 frames in each arm, with block bytes made equal pair by pair
(the one-block difference between the pairs is a pool allocation, 33.5 MB, in both arms). This
play makes no ring block more than the parent.
2026-12-02 09:38:04 -08:00

3069 lines
128 KiB
Rust

//! Memory: the blocks the consumer takes and releases, each a range of one of the device's
//! allocations; the pool that hands them out from the allocations' free ranges and frees idle
//! allocations; the allocations its own thread makes ahead of need, while a class holds no wholly
//! free unit, and the runtime's fixed-size internals it makes for the rest of the device
//! ([`Internal`]); and how memory is made and freed. A block's bytes are undefined: the runtime
//! zeroes nothing ahead of need. A buffer reads zero where
//! [`Device::new_buffer`](crate::Device::new_buffer) zeroes its range.
use crate::counters::{Counters, Gauge, Metric, Part, Site};
use crate::device::{lock, Core};
use crate::error::{Error, Limit};
use crate::fault::RangeKind;
use crate::floor;
use crate::places::Ranges;
use crate::reaper::{Free, Job};
use crate::submit_calls::SubmitCalls;
use ash::vk;
use std::cell::RefCell;
use std::collections::{HashSet, VecDeque};
use std::panic::{self, AssertUnwindSafe};
use std::ptr::NonNull;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, PoisonError};
use std::time::Duration;
/// The length of each allocation the block-maker thread makes, and of an allocation a take makes
/// where no free range fits it, unless the take is longer: 16 MiB of either class, the unit.
///
/// A driver may hold the device's other calls (pipeline creation, submissions, other allocations)
/// for the whole of an allocation's `vkAllocateMemory` and `vkFreeMemory`, and the hold grows with
/// the bytes. reims measured it on an RTX 3090 and an RTX 2080 (NVIDIA's Linux driver 620.70.06):
/// one thread allocating memory with a device address while another times empty
/// `vkQueueSubmit`s in a loop, the longest of which takes 1.76 ms on the 3090 and 2.08 ms on the
/// 2080 with no allocation beside it. A 16 MiB Private allocation holds no submission past that,
/// and one of 256 MiB holds one for 6.6 to 8 ms. A 16 MiB Shared allocation (host-visible,
/// host-coherent and host-cached) holds one for 3.3 ms at the median and 4.5 ms at most, and one
/// of 256 MiB for 51 ms on the 3090 and 58 ms on the 2080. Freeing 16 MiB of Private memory holds
/// none past the baseline, and 256 MiB about 5 ms; freeing 16 MiB of Shared memory holds one for
/// 4.1 ms at the median and up to 19 ms, and 256 MiB for 93 ms at the median and up to 242 ms. So
/// allocations are short, and a take longer than one pays its own length
/// once: the allocation it makes, a long allocation, serves later takes of that length.
const ALLOCATION_LEN: u64 = 16 << 20;
/// A block's length, and its first byte's offset in its allocation, are multiples of this, or of
/// the device's `bufferImageGranularity` where that is larger: no block shares a page of that
/// granularity with another, so a block of buffers lies beside a block of optimal images.
pub(crate) const GRANULE: u64 = 64 << 10;
/// The least alignment of an allocation's device address, which its blocks' buffers take as their
/// base: Metal's offset rule for a device buffer aligns the offset to the data type's size, at
/// most 16 bytes, from a base at a multiple of 16 ([`Limit::AllocationAddress`]).
pub(crate) const BASE_ADDRESS: u64 = 16;
/// A block's first byte lies at a multiple of its length rounded up to a power of two, up to this
/// much, in its allocation: an image placed in a block at an offset its alignment allows from the
/// block's first byte is placed at one from the allocation's.
const BASE_ALIGN: u64 = 2 << 20;
/// The idle age, in nanoseconds of [`host_time`](crate::host_time): 250 ms, one value for every
/// device. An allocation is idle once it holds no live block: its idle time starts when its last
/// live block comes back, and only a take that lands in it again ends it. A program that keeps
/// committing gives back what it stopped using: the free waits only for the work committed
/// before the allocation went idle ([`Wait`]). The
/// block-maker thread frees an allocation idle this long, after the consumer's unmap of it, one at
/// a time and between the allocations it makes, so a free never runs beside its own
/// `vkAllocateMemory`; a class's reserve ([`State::whole`]) is not freed. A driver may hold a
/// submission made during a free for the rest of it (on the NVIDIA cards measured in
/// Thunderbolt enclosures, freeing host-visible memory takes about 1.1 ms per MiB):
/// [`Metrics::submissions_during_frees`](crate::Metrics::submissions_during_frees) counts them.
pub(crate) const IDLE_AGE_NANOS: u64 = 250_000_000;
/// How soon after a free of its class, in nanoseconds of [`host_time`](crate::host_time), an
/// allocation of the pool is counted as one remade
/// ([`Metrics::shared_remade_after_frees`](crate::Metrics::shared_remade_after_frees)): a second.
/// A preprocess region is counted so after one freed past the bound
/// ([`Metrics::preprocess_regions_remade`](crate::Metrics::preprocess_regions_remade)).
const REMADE_WITHIN_NANOS: u64 = 1_000_000_000;
/// Whether what is made at `now` is made within [`REMADE_WITHIN_NANOS`] of a free at `freed`.
pub(crate) fn remade(freed: u64, now: u64) -> bool {
now.saturating_sub(freed) < REMADE_WITHIN_NANOS
}
/// A texture block or an allocation is sparse while it holds something and what it holds is less
/// than its length over this: one sixteenth
/// ([`Gauges::texture_block_sparse_bytes`](crate::Gauges::texture_block_sparse_bytes),
/// [`Gauges::private_take_sparse_bytes`](crate::Gauges::private_take_sparse_bytes)).
pub(crate) const SPARSE: u64 = 16;
/// What a release waits for before its memory, object or index goes: the command buffers
/// committed before it, which may reach it with no handle of it, by address (a heap's buffers
/// through an argument buffer) or through set 1 (a heap's textures and sampler states by their
/// resource IDs). Such a reader may read another block's bytes by then, as on a Mac, but never
/// memory or an object the device freed. Nothing is kept past the work in flight, and no command
/// buffer committed after the release is waited for.
#[derive(Clone, Copy, Debug)]
pub(crate) enum Wait {
/// Every command buffer committed up to this place in the commit order is to be submitted.
Commits(u64),
/// Then the submission of this progress value is to complete: the last that carried one of
/// them, or none (0).
Progress(u64),
}
impl Wait {
/// Whether the work it waits for completed, on the block-maker thread without the pool's
/// lock: a wait for commits moves on, once each of them is submitted, to the progress value
/// of the last submission that carried one of them (`Work::settled`).
fn over(&mut self, core: &Core) -> bool {
if let Wait::Commits(commits) = *self {
match core.work.settled(commits) {
Some(progress) => *self = Wait::Progress(progress),
None => return false,
}
}
matches!(*self, Wait::Progress(progress) if core.done.completed() >= progress)
}
/// The progress value of the submission it waits for: None where it waits for none, or has
/// not learned it yet.
fn progress(self) -> Option<u64> {
match self {
Wait::Progress(0) | Wait::Commits(_) => None,
Wait::Progress(progress) => Some(progress),
}
}
}
/// A release's mark: what it waits for, and the commits made when it was noted, by which the
/// releases waiting are ordered ([`Pool::release_marked`]).
#[derive(Clone, Copy, Debug)]
pub(crate) struct Mark {
wait: Wait,
commits: u64,
}
impl Mark {
/// The later of two marks: the one noted at more commits, `self` where they are level.
pub(crate) fn later(self, other: Mark) -> Mark {
match other.commits > self.commits {
true => other,
false => self,
}
}
}
/// The idle clock, the one clock for everything the runtime gives back: what became idle at
/// `since` and stayed idle is due at `now` ([`host_time`](crate::host_time)) once
/// [`IDLE_AGE_NANOS`] has passed (`None`); before then, the host time it becomes due.
pub(crate) fn idle_due(since: u64, now: u64) -> Option<u64> {
let due = since.saturating_add(IDLE_AGE_NANOS);
(now < due).then_some(due)
}
/// `MTLStorageMode` of a block, as the buffers in it see it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MemoryClass {
/// `MTLStorageModePrivate`: device-local memory with no host mapping.
Private,
/// `MTLStorageModeShared`: host-visible and host-cached memory, host-coherent too where the
/// device has such a type ([`floor::MemoryTypes::shared`]), mapped for the block's life. On a
/// discrete GPU it is system memory the GPU reads across its bus, as on a Mac with a discrete
/// GPU. Where that type is not host-coherent, the runtime flushes what commands reach of it
/// before each submission, and flushes and invalidates what they may write before the host
/// sees their progress (the crate's README, "Shared memory's coherence"): a range the commands
/// reach only through an address, with no binding or declaration naming it, is neither, nor
/// are the consumer's own host reads and writes outside those points.
Shared,
}
impl MemoryClass {
const ALL: [MemoryClass; 2] = [MemoryClass::Private, MemoryClass::Shared];
/// No Metal counterpart: the length of the allocations the device makes for the class ahead
/// of need, 16 MiB of either class.
///
/// A [`Device::take_block`](crate::Device::take_block) is a range of one of the class's
/// allocations, the first with room; the block-maker thread makes another while none of the
/// class's allocations of this length is wholly free. A take that no free range fits makes an
/// allocation of this length, which later takes share, or of its own where it is longer, which
/// only later takes of that length share.
pub const fn block_len(self) -> u64 {
ALLOCATION_LEN
}
/// The gauge of the bytes of the class's live allocations.
fn allocation_gauge(self) -> Gauge {
match self {
MemoryClass::Private => Gauge::PrivateAllocationBytes,
MemoryClass::Shared => Gauge::SharedAllocationBytes,
}
}
/// The gauge of the class's idle bytes.
fn idle_gauge(self) -> Gauge {
match self {
MemoryClass::Private => Gauge::PrivateIdleBytes,
MemoryClass::Shared => Gauge::SharedIdleBytes,
}
}
/// The gauge of the class's blocks taken and not given back.
fn placed_gauge(self) -> Gauge {
match self {
MemoryClass::Private => Gauge::PrivatePlacedBytes,
MemoryClass::Shared => Gauge::SharedPlacedBytes,
}
}
/// The gauge of the class's live buffers' bytes.
pub(crate) fn buffer_gauge(self) -> Gauge {
match self {
MemoryClass::Private => Gauge::PrivateBufferBytes,
MemoryClass::Shared => Gauge::SharedBufferBytes,
}
}
fn index(self) -> usize {
match self {
MemoryClass::Private => 0,
MemoryClass::Shared => 1,
}
}
}
/// No Metal counterpart: a block's number. Numbers start at 1 and are never reused during the
/// device's life: each take is a new number.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct BlockId(pub(crate) u32);
impl BlockId {
/// The number.
pub fn get(self) -> u32 {
self.0
}
}
/// No Metal counterpart: an allocation's number, the `VkDeviceMemory` blocks are ranges of.
/// Numbers start at 1 and are never reused during the device's life.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AllocationId(u32);
impl AllocationId {
/// The number.
pub fn get(self) -> u32 {
self.0
}
}
/// Nearest Metal counterpart: the memory of a placement `MTLHeap`, which the consumer places
/// buffers in ([`Device::new_buffer`](crate::Device::new_buffer)); for an imported block
/// (`Device::import_dma_buf` on Unix,
/// [`Device::import_host_memory`](crate::Device::import_host_memory)), the memory of a no-copy
/// buffer (`newBufferWithBytesNoCopy:length:options:deallocator:`); for an exported block
/// ([`Device::new_exported_block`](crate::Device::new_exported_block)), none.
///
/// Plain data, which the block's handle ([`Block`](crate::Block)) derefs to: the block lives
/// until its last handle and its last buffer, command buffer or upload are gone, or until the
/// device is torn down. It is a range of one allocation, whose one `VkBuffer`, with every buffer
/// usage and a device address, spans it; an imported or exported block is the whole of an
/// allocation of its own. Only an exported block is exportable.
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub struct BlockInfo {
/// The block's number.
pub id: BlockId,
/// Its storage mode.
pub class: MemoryClass,
/// The length asked for, rounded up to 64 KiB (to the device's `bufferImageGranularity`
/// where that is larger); an imported block's, the length imported.
pub len: u64,
/// The allocation it is a range of, which [`BlockMappings::map`] was told of before this
/// block went out; an imported or exported block's own, of which no hook is told.
pub allocation: AllocationId,
/// The offset of its first byte in that allocation.
pub offset: u64,
/// `gpuAddress` of the block's first byte: its allocation's device address plus
/// [`offset`](Self::offset); `None` for an exported block, an address of whose memory goes
/// out only as [`Buffer::gpu_address`](crate::Buffer::gpu_address) of a buffer in it.
pub gpu_address: Option<u64>,
/// `contents` of the block's first byte: the host mapping of a Shared block, `None` for a
/// Private one; for an imported block, the host memory imported, or `None` for a dma-buf,
/// whose pages the consumer reaches through its own mappings.
pub contents: Option<NonNull<u8>>,
/// Whether its memory type is device-local. A Private block is, unless the device-local type
/// was out of memory when its allocation was made and it went to the next type its buffer
/// takes (counted in [`Gauges::spilled_bytes`](crate::Gauges::spilled_bytes)); a Shared
/// block is where the device's Shared type is, and an imported one where the type imported
/// into is.
pub device_local: bool,
}
// SAFETY: plain data. `contents` is an address; reading or writing through it is the consumer's
// own unsafe code, under the contract of `BlockMappings`.
unsafe impl Send for BlockInfo {}
// SAFETY: as above.
unsafe impl Sync for BlockInfo {}
/// No Metal counterpart: an allocation of the pool, as [`BlockMappings::map`] is told of it.
#[non_exhaustive]
#[derive(Clone, Copy, Debug)]
pub struct AllocationInfo {
/// The allocation's number, which the blocks of it carry
/// ([`BlockInfo::allocation`](crate::BlockInfo::allocation)).
pub id: AllocationId,
/// The storage mode of its blocks.
pub class: MemoryClass,
/// Its length: 16 MiB ([`MemoryClass::block_len`]), or a take's own length where that is
/// longer.
pub len: u64,
/// The device address of its first byte.
pub gpu_address: u64,
/// The host mapping of its first byte, which lives as long as the allocation, for a Shared
/// allocation; `None` for a Private one, which has no host mapping.
pub contents: Option<NonNull<u8>>,
/// The host time its make began ([`host_time`](crate::host_time)).
pub make_start: u64,
/// The make's length in nanoseconds, from [`make_start`](Self::make_start) until the
/// allocation was made, before [`BlockMappings::map`] is called.
pub make_nanos: u64,
}
// SAFETY: plain data, as `BlockInfo`.
unsafe impl Send for AllocationInfo {}
// SAFETY: as above.
unsafe impl Sync for AllocationInfo {}
/// No Metal counterpart: the consumer's hooks around each allocation of the pool, for a consumer
/// that maps the allocations into a guest's address range itself.
///
/// The runtime calls [`map`](BlockMappings::map) once for every allocation of the pool it makes,
/// before any block of it goes out, and [`unmap`](BlockMappings::unmap) once for every allocation
/// `map` accepted, before the allocation is freed. The guest's range holds whole allocations,
/// their free ranges and the runtime's own staging blocks included; the consumer hands the guest
/// blocks alone. A block given back stays mapped with its allocation: a guest kernel that writes
/// a free range damages only its own later blocks' bytes. Imported and exported blocks are not
/// the pool's, and neither hook runs for them.
pub trait BlockMappings: Send + Sync {
/// The allocation `allocation` describes was made. True once the consumer has mapped it; false
/// where it has no place for it (no guest address): the runtime then frees the allocation and
/// hands out none of its blocks, and a take that made it returns
/// [`Limit::AllocationNotMapped`], counted in
/// [`Metrics::allocation_map_refusals`](crate::Metrics::allocation_map_refusals).
///
/// It runs where the allocation is made: on the block-maker thread for the allocations made
/// ahead of need, on the thread in [`Device::new`](crate::Device::new) for each class's first,
/// and on the consumer's thread inside a call whose take no free range fits:
/// [`Device::take_block`](crate::Device::take_block), and the calls that take a block of their
/// own (a texture's place, a visible function table, an indirect command buffer, a dispatch
/// whose kernel's private memory takes a new block of the device's arena). Never under the
/// pool's or the arena's lock. It makes no call into the runtime and drops no runtime handle,
/// and it never waits for a lock that a thread inside a runtime call holds: that call may be
/// waiting for the hook (a call that waits for a make of the block-maker thread, which runs the
/// hooks between its makes, or the device's last drop), so such a wait never ends.
/// `allocation` states when the make began and how long it took
/// ([`AllocationInfo::make_start`], [`AllocationInfo::make_nanos`]).
///
/// Where it panics, the runtime frees the allocation and the panic goes on: out of the
/// consumer's call on a take's thread, or to the end of the block-maker thread, which then
/// makes and frees nothing more. That end stops the pool as
/// [`Device::quiesce`](crate::Device::quiesce) does: a take of the pool's memory is
/// [`Error::Quiesced`] from then on, and no call waits for the ended thread.
fn map(&self, allocation: &AllocationInfo) -> bool;
/// Returns only once every mapping of `allocation` the consumer made is gone and none of its
/// threads touches the allocation's memory. The memory stays mapped and valid until it
/// returns, and is freed after.
///
/// It runs once per allocation `map` accepted: on the block-maker thread before an idle
/// allocation's free; in [`Device::quiesce`](crate::Device::quiesce), on the thread that calls
/// it, for every allocation still mapped; or at teardown, on the thread that ends it, once the
/// runtime's other threads are joined. Never under the pool's or the arena's lock. It makes no
/// call into the runtime and drops no runtime handle, and, as [`map`](Self::map), it never
/// waits for a lock that a thread inside a runtime call holds.
///
/// Where it panics on the block-maker thread, the runtime keeps the allocation, whose
/// mappings may stand, and frees it at teardown with no second unmap; the panic ends the
/// thread, which stops the pool as a panic of [`map`](Self::map) there does.
fn unmap(&self, allocation: AllocationId);
}
/// A block: a range of an allocation, its allocation's `VkBuffer`, and its mapping.
#[derive(Clone, Copy)]
pub(crate) struct Memory {
pub(crate) id: BlockId,
pub(crate) class: MemoryClass,
pub(crate) len: u64,
/// Its allocation, and its first byte's offset there.
allocation: AllocationId,
memory: vk::DeviceMemory,
base: u64,
/// The allocation's `VkBuffer`, which spans the allocation ([`range`](Self::range)).
buffer: vk::Buffer,
/// The device address of the block's first byte.
pub(crate) address: u64,
pub(crate) mapping: Option<Mapping>,
/// Its memory type is device-local.
device_local: bool,
/// A Private block in another memory type than the Private one, which was out of memory.
spilled: bool,
/// An import's block ([`import`]) or an export's ([`export`]): an allocation of its own,
/// which no pool holds and which is freed once the block is given back.
pub(crate) own: Option<Own>,
}
/// What a block that is an allocation of its own is.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Own {
/// An import of memory the consumer brought ([`import`]): of `handle`, into the memory type
/// `memory_type`.
Import {
handle: crate::ImportHandle,
memory_type: u32,
},
/// A dedicated allocation the consumer shares by a handle of `handle`'s kind ([`export`],
/// [`export_surface`]), of `bytes`, its allocation size.
Export {
handle: crate::ExportHandle,
bytes: u64,
},
/// An exported surface's buffer ([`crate::Surface::buffer`]): on one more import of its
/// dma-buf, an allocation of its own (`import`), or on the opaque handle's one allocation,
/// which the block under it holds and frees ([`surface_view`]).
Surface {
/// Its memory is an import of its own, freed with it.
import: bool,
/// The other API's queue family, which its hand-overs name (`crate::handed`).
family: u32,
},
/// An import over several dma-bufs ([`crate::Device::import_dma_bufs`]): its sparse buffer,
/// bound to its chunks' imports by the bind that signals `bind` on the binds' timeline
/// ([`crate::import::Sparse`]), whose memory requirements name `space` bytes of the sparse
/// address space. Its memory is its chunks', each a block of its own given back after it
/// ([`sparse_block`]).
#[cfg(unix)]
Sparse {
/// The bind's value.
bind: u64,
/// The bytes of the sparse address space it takes.
space: u64,
},
/// One dma-buf's import under such a buffer: memory and no buffer.
#[cfg(unix)]
Chunk,
}
impl Memory {
/// The allocation the block is a range of; an image bound in the block binds at
/// [`base`](Self::base) plus its offset in the block.
pub(crate) fn device_memory(&self) -> vk::DeviceMemory {
self.memory
}
/// The offset of the block's first byte in its allocation.
pub(crate) fn base(&self) -> u64 {
self.base
}
/// The `VkBuffer` that commands reach the block's byte `offset` through, and that byte's offset
/// in it: the allocation's buffer, which spans the allocation, at the block's base plus
/// `offset`.
pub(crate) fn range(&self, offset: u64) -> (vk::Buffer, u64) {
(self.buffer, self.base + offset)
}
/// Whether it is a Private block in another memory type than the Private one.
pub(crate) fn spilled(&self) -> bool {
self.spilled
}
/// Whether it is memory the consumer brought: one import, or several behind one buffer.
pub(crate) fn imported(&self) -> bool {
match self.own {
Some(Own::Import { .. }) => true,
#[cfg(unix)]
Some(Own::Sparse { .. }) => true,
_ => false,
}
}
/// Whether it is a dedicated allocation the consumer shares with another API ([`export`],
/// [`export_surface`]), Private and never mapped.
pub(crate) fn exported(&self) -> bool {
matches!(self.own, Some(Own::Export { .. }))
}
/// The other API's queue family, where another API shares the memory and each submission
/// part that uses it hands it over (`crate::handed`): an exported block's, an exported
/// surface's buffer's (a dma-buf's on its own import of the surface's dma-buf). `None` for
/// every other block, an import of another API's memory among them.
pub(crate) fn handed_family(&self) -> Option<u32> {
match self.own {
Some(Own::Export { handle, .. }) => Some(handle.family()),
Some(Own::Surface { family, .. }) => Some(family),
_ => None,
}
}
pub(crate) fn info(&self) -> BlockInfo {
BlockInfo {
id: self.id,
class: self.class,
len: self.len,
allocation: self.allocation,
offset: self.base,
gpu_address: (!self.exported()).then_some(self.address),
contents: self.mapping.map(Mapping::get),
device_local: self.device_local,
}
}
}
/// A host address in a Shared allocation's mapping, which lives as long as the allocation: its
/// first byte, a block's or a buffer's.
#[derive(Clone, Copy)]
pub(crate) struct Mapping(NonNull<u8>);
// SAFETY: an address. The runtime writes through it only while it owns the bytes: `new_buffer`
// zeroing the range of the buffer it makes.
unsafe impl Send for Mapping {}
// SAFETY: as above.
unsafe impl Sync for Mapping {}
impl Mapping {
pub(crate) fn new(address: NonNull<u8>) -> Mapping {
Mapping(address)
}
pub(crate) fn get(self) -> NonNull<u8> {
self.0
}
/// The address `offset` bytes on.
///
/// # Safety
///
/// `self` is an allocation's or a block's first byte and `offset` is at most its length.
pub(crate) unsafe fn at(self, offset: u64) -> NonNull<u8> {
// SAFETY: the mapping spans the memory, and `offset` is inside it or one past its end;
// memory that was mapped has a length that fits the address space.
unsafe { self.0.add(offset as usize) }
}
}
/// The consumer's release of an imported block's memory: Metal's deallocator, run once the
/// memory is freed.
pub(crate) type Release = Box<dyn FnOnce() + Send>;
/// A block that has been handed out. The consumer's hold on it, each buffer of it, and each
/// command buffer and upload that uses it until it retires are one reference each; the last one
/// dropped gives it back to the pool, or, for an import, to the block-maker thread to free.
pub(crate) struct Block {
pub(crate) memory: Memory,
core: Arc<Core>,
/// An import's release, which runs once its memory is freed: behind a lock, as the block is
/// shared between threads and the release is only `Send`.
release: Mutex<Option<Release>>,
/// The block whose allocation this block's buffer is bound in ([`surface_view`]): given back
/// after this one, so the block-maker thread destroys the buffer before it frees the memory.
_under: Option<Arc<Block>>,
/// An import over several dma-bufs' chunks, in order (`sparse_block`, on Unix): given back
/// after this one, so the block-maker thread destroys the sparse buffer before it frees any
/// of their memory, the last of them holding the release.
_chunks: Vec<Block>,
/// Of an exported block: set once a public call hands out a device address of its memory
/// ([`address_out`](Self::address_out)), which the textures over it read at each submission
/// (`crate::over::ExportedOvers`).
pub(crate) addressed: Option<Arc<AtomicBool>>,
/// Of an exported surface's buffer in place ([`crate::Surface::buffer`]): the surface's
/// memory, which its planes alias and which a part hands over whole
/// (`crate::handed::Aliases`). `None` for every other block.
pub(crate) aliases: Option<Arc<crate::handed::Aliases>>,
}
impl Block {
pub(crate) fn new(memory: Memory, core: Arc<Core>) -> Block {
note(&core, &memory);
Block {
addressed: memory.exported().then(Arc::default),
memory,
core,
release: Mutex::new(None),
_under: None,
_chunks: Vec::new(),
aliases: None,
}
}
/// A public call handed out a device address of the block's memory, where a shader may write
/// through it outside every bound slot: an exported block is marked, and counted at the first
/// (`Metrics::exported_blocks_addressed`).
pub(crate) fn address_out(&self) {
let marked =
(self.addressed.as_ref()).is_some_and(|flag| !flag.swap(true, Ordering::Relaxed));
if marked {
self.core.counters.count(Metric::ExportedBlocksAddressed);
}
}
/// An import's block ([`import`]), whose `release` runs once its memory is freed.
pub(crate) fn imported(memory: Memory, core: Arc<Core>, release: Option<Release>) -> Block {
note(&core, &memory);
Block {
memory,
core,
release: Mutex::new(release),
_under: None,
_chunks: Vec::new(),
addressed: None,
aliases: None,
}
}
/// A surface's buffer on the allocation `under` holds ([`surface_view`]).
fn over(memory: Memory, core: Arc<Core>, under: Arc<Block>) -> Block {
note(&core, &memory);
Block {
memory,
core,
release: Mutex::new(None),
_under: Some(under),
_chunks: Vec::new(),
addressed: None,
aliases: None,
}
}
}
/// Notes an imported or exported block's allocation of its own, or an exported surface's buffer,
/// among the device's ranges a shader reaches by device address (`crate::fault::Spans`), until
/// [`free_own`] frees it. A block of the pool, whose allocation is noted, and a chunk under an
/// import over several dma-bufs, which has no buffer, are not.
fn note(core: &Core, memory: &Memory) {
let kind = match memory.own {
None => return,
Some(Own::Import { .. }) => RangeKind::Import,
Some(Own::Export { .. }) => RangeKind::Export(memory.id),
Some(Own::Surface { .. }) => RangeKind::Surface,
#[cfg(unix)]
Some(Own::Sparse { .. }) => RangeKind::Sparse,
#[cfg(unix)]
Some(Own::Chunk) => return,
};
(core.spans).made(memory.buffer, memory.address, memory.len, kind);
}
impl Drop for Block {
fn drop(&mut self) {
if self.memory.own.is_some() {
let release = (self.release.get_mut())
.unwrap_or_else(PoisonError::into_inner)
.take();
self.core.pool.give_back_own(self.memory, release);
} else {
self.core.pool.give_back(self.memory);
}
}
}
/// One `VkDeviceMemory` of a class, the `VkBuffer` that spans it, and the ranges of it no block
/// holds.
struct Allocation {
id: AllocationId,
class: MemoryClass,
memory: vk::DeviceMemory,
buffer: vk::Buffer,
/// The device address of its first byte.
address: u64,
len: u64,
mapping: Option<Mapping>,
device_local: bool,
spilled: bool,
/// Bytes no block holds.
free: Ranges,
/// Its blocks handed out and not yet given back to the pool.
live: usize,
/// Their bytes.
placed: u64,
/// Its block out is a texture block ([`crate::places`]), which fills its allocation: a whole
/// unit, or a long allocation of its own length. False once it is idle.
texture: bool,
/// When it became idle, in nanoseconds of [`host_time`](crate::host_time): when it was made,
/// or when its last live block came back. The oldest idle allocation is freed first.
idle_since: u64,
/// What its free waits for, noted when it became idle.
wait: Wait,
/// The host time its make began and the make's length in nanoseconds
/// ([`AllocationInfo::make_start`]).
made: (u64, u64),
/// A spilled allocation whose last block came back: no take lands in it, its bytes are not
/// the class's free bytes, and the idle rule frees it next, so the next allocation tries
/// device-local memory again.
retiring: bool,
/// A long allocation, longer than [`ALLOCATION_LEN`], that holds a block of a take after the
/// one it was made for, until it is idle again
/// ([`Gauges::shared_long_reused_bytes`](crate::Gauges::shared_long_reused_bytes)).
reused: bool,
/// Made by the block-maker thread while its class held no wholly free unit ([`run`]), and no
/// take has landed in it yet: the first take that does is counted at its site
/// ([`SiteAllocations::takes_ahead`](crate::SiteAllocations::takes_ahead)).
ahead: bool,
}
impl Allocation {
/// Whether it is a unit allocation, of [`ALLOCATION_LEN`], which any take that fits shares,
/// where a long one, made for a take longer than the unit, serves only a take of its own
/// length.
fn unit(&self) -> bool {
self.len <= ALLOCATION_LEN
}
/// Whether a take of `len` bytes may land in it: a unit allocation for any take, a long one
/// for a take of exactly its length.
fn serves(&self, len: u64) -> bool {
!self.retiring && (self.unit() || self.len == len)
}
}
/// The pool: every allocation of each class, and the ranges of each that no block holds.
///
/// A take is the first free range of the class's allocations that fits it, in the order the
/// allocations were made; a long allocation serves only takes of its own length. Where none fits,
/// the take makes an allocation itself, of [`ALLOCATION_LEN`] or of its own length where that is
/// longer. A block given back is a free range of its allocation at once. The block-maker thread
/// ([`run`]) makes an allocation of a class while the class holds no wholly free unit
/// ([`State::whole`]); a take never waits for it.
///
/// The idle rule never frees the class's reserve, its last idle unit allocation in the order they
/// were made, so it does not free what the block-maker thread would make again. A long allocation
/// is never kept so.
///
/// The idle rule runs on the block-maker thread as a block is taken or given back, and again when
/// the oldest idle allocation reaches [`IDLE_AGE_NANOS`]: it frees each idle allocation of that
/// age, one at a time, once the work committed before it went idle completed ([`Wait`]); so wait
/// the frees of imported and exported blocks, and the objects released under work in flight
/// ([`Pool::release`]).
pub(crate) struct Pool {
state: Mutex<State>,
/// Signalled whenever the state changes, for the block-maker thread and for takers waiting
/// on it.
changed: Condvar,
/// The last commit's place in the device's commit order, which memory given back notes, and
/// the command buffers committed and not yet retired.
commits: AtomicU64,
in_flight: AtomicU64,
/// The completion thread's rounds of retirements, and whether the block-maker thread waits
/// for one to free ([`retired`](Self::retired)).
rounds: AtomicU64,
awaiting: AtomicBool,
mappings: Option<Arc<dyn BlockMappings>>,
/// The device's counters: the idle bytes, the allocations made and the long reuses.
counters: Counters,
/// A block's length and its first byte's offset are multiples of this: [`GRANULE`], or the
/// device's `bufferImageGranularity` where that is larger.
granule: u64,
/// The alignment of a buffer of the pool's usage, which Vulkan holds the same for every such
/// buffer.
alignment: u64,
/// For the device tests: each idle allocation the block-maker thread freed, as when it became
/// idle, when its free began and when it ended, in nanoseconds of
/// [`host_time`](crate::host_time).
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
frees: Mutex<Vec<(u64, u64, u64)>>,
}
/// What the block-maker thread makes for the device's other parts besides the pool's
/// allocations: the runtime's fixed-size internals, off every caller's thread. The device's making
/// asks it for the zero source, the parameter ring's first block and the staging region, its first
/// work once it starts, and the ring asks it for each later block ([`crate::ring`]). A call that
/// needs one while it is asked waits for its make ([`Pool::await_made`]), counted in
/// [`Metrics::maker_waits`](crate::Metrics::maker_waits); where the make failed, or the thread had
/// stopped, the call makes it on its own thread with the same function, counted at its [`Site`].
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Internal {
/// The zero source of the fills' and new textures' copies ([`crate::fill::Filler`]).
ZeroSource,
/// The parameter ring's next block ([`crate::ring`]).
RingBlock,
/// The 16 MiB staging region of the copies between textures and the CPU
/// ([`crate::staging`]).
StagingRegion,
}
/// The number of [`Internal`]s.
const INTERNALS: usize = 3;
impl Internal {
/// Every internal, in the order the block-maker thread makes those asked.
const ALL: [Internal; INTERNALS] = [
Internal::ZeroSource,
Internal::RingBlock,
Internal::StagingRegion,
];
fn index(self) -> usize {
self as usize
}
}
/// For the device tests: what the block-maker thread does with the internals asked of it
/// ([`DeviceOptions::maker`](crate::DeviceOptions)).
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
#[derive(Clone, Default)]
pub(crate) enum MakerTest {
/// Makes them.
#[default]
Make,
/// Meets the test at the barrier twice before its first work, once parked and once released;
/// then makes them.
Park(Arc<std::sync::Barrier>),
/// Every make of an internal fails, before the driver is called, so the calls that need them
/// make them.
Fail,
}
/// What the idle rule asks of a class.
enum Rule {
/// No idle allocation that the rule weighs.
Keep,
/// Idle allocations younger than [`IDLE_AGE_NANOS`]: the oldest reaches it at this
/// [`host_time`](crate::host_time).
KeepUntil(u64),
/// The allocation to free next: a retiring one, else the oldest idle one of
/// [`IDLE_AGE_NANOS`] or more.
Free(vk::DeviceMemory),
}
/// Memory of its own that an object of another thread held, given back for the block-maker thread
/// to free in the form of every free it makes ([`free`]), its gauges falling after the free.
pub(crate) enum Loose {
/// A texture's or a shadow's image-dedicated allocation (`crate::texture::Backing::Own`) of
/// `bytes`, `spilled` of them in a spill type.
Texture {
memory: vk::DeviceMemory,
bytes: u64,
spilled: u64,
},
/// A surface plane's import (`crate::texture::Backing::Import`) of `bytes`.
SurfaceImport {
memory: vk::DeviceMemory,
bytes: u64,
},
/// A copy's staging buffer of its own and its memory (`crate::staging::Staging`).
Staging {
buffer: vk::Buffer,
memory: vk::DeviceMemory,
},
/// An idle preprocess region of `bytes`, its buffer and memory (`crate::indirect::Region`).
Preprocess {
buffer: vk::Buffer,
memory: vk::DeviceMemory,
bytes: u64,
},
/// An idle parameter ring block, its buffer and memory (`crate::ring::Ring::due`).
RingBlock {
buffer: vk::Buffer,
memory: vk::DeviceMemory,
},
}
struct State {
/// Per class, its allocations in the order they were made.
allocations: [Vec<Allocation>; 2],
/// Imported and exported blocks given back, with the imports' releases, for the block-maker
/// thread to free, each with its [`Wait`].
owned: VecDeque<((Memory, Option<Release>), Wait)>,
/// Memory of its own other threads' objects held, given back for the block-maker thread to
/// free.
loose: VecDeque<Loose>,
/// Objects and indices released while work was in flight, each with its [`Wait`] and the
/// commits made when its mark was noted ([`Mark`]), in the order of those, for the
/// block-maker thread to hand to the reaper ([`Pool::release`]).
held: VecDeque<((Free, u64), Wait)>,
/// Per class, the block-maker thread could not make the last allocation it tried. It tries
/// again after the class's next take or give-back.
failed: [bool; 2],
/// Per class, when the block-maker thread's last free of an idle allocation of it ended, in
/// nanoseconds of [`host_time`](crate::host_time); 0 before one.
freed_at: [u64; 2],
/// The internals asked of the block-maker thread and not yet taken up, by [`Internal::index`]:
/// the zero source, the ring's first block and the staging region from the device's making,
/// and each later ring block a take asks for.
asked: [bool; INTERNALS],
/// The internal the block-maker thread took up and is making ([`run`]).
making: Option<Internal>,
stop: bool,
next_id: u32,
next_allocation: u32,
/// The allocations [`BlockMappings::map`] accepted whose unmap has not run.
mapped: HashSet<AllocationId>,
/// The unmap hook has run for every allocation mapped ([`Pool::unmap_all`]); no hook runs
/// again.
unmapped: bool,
/// Per class, the idle allocations' bytes the class's gauge shows.
idle_bytes: [u64; 2],
/// The bytes of the consumer's blocks in the Private units, the pool's share of
/// [`Gauges::private_unit_used_bytes`](crate::Gauges::private_unit_used_bytes).
unit_take_bytes: u64,
/// For the device tests: the blocks given back.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
returned: HashSet<BlockId>,
/// For the device tests: the calls in [`Pool::await_made`] waiting for an internal.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
waiting: usize,
}
thread_local! {
/// Where this thread makes memory ahead of need, its device's submit calls: on the block-maker
/// thread ([`run`]), its internals among what it makes, and in the device's making while it
/// makes the first allocations ([`stock`]). Each of its allocations and frees waits out the
/// submit calls its previous one met ([`maker_call`]), and its allocations' times count apart
/// from the consumer's.
static MAKER: RefCell<Option<Arc<SubmitCalls>>> = const { RefCell::new(None) };
}
/// Makes this thread one that makes memory ahead of need for the device whose submit calls are
/// `calls` ([`MAKER`]); `None` makes it one that does not.
pub(crate) fn make_ahead_here(calls: Option<Arc<SubmitCalls>>) {
MAKER.set(calls);
}
/// Whether this thread makes memory ahead of need ([`MAKER`]).
fn on_maker() -> bool {
MAKER.with_borrow(Option::is_some)
}
/// `call`, one `vkAllocateMemory`: on a thread that makes memory ahead of need, it starts once
/// every submit call of its device that was inside the driver as the thread's previous memory call
/// returned has returned, and the submit calls inside the driver as it returns are the ones the
/// thread's next memory call waits out ([`SubmitCalls::call`]). So a driver that holds a submit
/// through memory calls made back to back holds it through one of them alone. On every other
/// thread it is the call alone.
pub(crate) fn maker_call<R>(call: impl FnOnce() -> R) -> R {
MAKER.with_borrow(|maker| match maker {
Some(calls) => calls.call(call),
None => call(),
})
}
/// `vkFreeMemory` of `memory`, the form of every free the block-maker thread makes: there, as
/// [`maker_call`], and marked for the submissions made during it ([`SubmitCalls::freeing`]).
///
/// # Safety
///
/// As `vkFreeMemory`: `memory` is `device`'s, and nothing uses it.
pub(crate) unsafe fn free(device: &ash::Device, memory: vk::DeviceMemory) {
MAKER.with_borrow(|maker| match maker {
// SAFETY: the caller's.
Some(calls) => calls.free(|| unsafe { device.free_memory(memory, None) }),
// SAFETY: the caller's.
None => unsafe { device.free_memory(memory, None) },
});
}
/// What a block is: a range of an allocation.
#[derive(Clone, Copy)]
struct Carve {
allocation: AllocationId,
memory: vk::DeviceMemory,
buffer: vk::Buffer,
/// The device address of the range's first byte.
address: u64,
base: u64,
mapping: Option<Mapping>,
device_local: bool,
spilled: bool,
}
impl Carve {
/// Block `id` of `class` and `len` bytes, the range.
fn block(self, id: BlockId, class: MemoryClass, len: u64) -> Memory {
Memory {
id,
class,
len,
allocation: self.allocation,
memory: self.memory,
base: self.base,
buffer: self.buffer,
address: self.address,
mapping: self.mapping,
device_local: self.device_local,
spilled: self.spilled,
own: None,
}
}
}
impl Pool {
/// The alignment of a buffer of the pool's usage, which Vulkan's memory requirements gave at
/// the device's making ([`buffer_alignment`]).
pub(crate) fn alignment(&self) -> u64 {
self.alignment
}
/// The pool of a device whose `bufferImageGranularity` is `granularity`, and whose buffers of
/// the pool's usage have the alignment `alignment`.
pub(crate) fn new(
mappings: Option<Arc<dyn BlockMappings>>,
counters: Counters,
granularity: u64,
alignment: u64,
) -> Pool {
Pool {
state: Mutex::new(State {
allocations: [Vec::new(), Vec::new()],
owned: VecDeque::new(),
loose: VecDeque::new(),
held: VecDeque::new(),
failed: [false, false],
freed_at: [0, 0],
// The device's fixed-size internals: the block-maker thread's first work.
asked: [true; INTERNALS],
making: None,
stop: false,
next_id: 1,
next_allocation: 1,
mapped: HashSet::new(),
unmapped: false,
idle_bytes: [0, 0],
unit_take_bytes: 0,
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
returned: HashSet::new(),
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
waiting: 0,
}),
changed: Condvar::new(),
commits: AtomicU64::new(0),
in_flight: AtomicU64::new(0),
rounds: AtomicU64::new(0),
awaiting: AtomicBool::new(false),
mappings,
counters,
granule: GRANULE.max(granularity.next_power_of_two()),
alignment,
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
frees: Mutex::new(Vec::new()),
}
}
fn lock(&self) -> MutexGuard<'_, State> {
lock(&self.state)
}
/// A command buffer took place `seq` in the commit order.
pub(crate) fn committed(&self, seq: u64) {
self.in_flight.fetch_add(1, Ordering::SeqCst);
self.commits.fetch_max(seq, Ordering::SeqCst);
}
/// The command buffers committed and not yet retired.
pub(crate) fn in_flight(&self) -> u64 {
self.in_flight.load(Ordering::SeqCst)
}
/// A command buffer committed retired, completed or ended.
pub(crate) fn ended(&self) {
self.in_flight.fetch_sub(1, Ordering::SeqCst);
}
/// What memory given back now waits for: every command buffer committed so far, nothing where
/// none is in flight.
fn noted(&self) -> Wait {
match self.in_flight.load(Ordering::SeqCst) {
0 => Wait::Progress(0),
_ => Wait::Commits(self.commits.load(Ordering::SeqCst)),
}
}
/// What a release noted now waits for, with the commits made by now ([`Mark`]).
pub(crate) fn mark(&self) -> Mark {
let wait = self.noted();
let commits = match wait {
Wait::Commits(commits) => commits,
Wait::Progress(_) => self.commits.load(Ordering::SeqCst),
};
Mark { wait, commits }
}
/// The program let go of `object` (a texture's or a sampler state's index, views, image or
/// sampler), which work committed before now may reach with no handle of it, through set 1:
/// it goes once that work retired ([`Wait`]): an index on the block-maker thread, an object
/// on the reaper thread. Where none is in flight, it goes at once, an index on this thread,
/// so it is vacated before the program's release returns. Releases keep their order.
pub(crate) fn release(&self, core: &Core, object: Free) {
self.release_marked(core, object, None);
}
/// As [`release`](Self::release), for `object` the program let go of at `marked` where that
/// is Some, noted then ([`mark`](Self::mark)): an image whose last holder, which kept it for
/// its own work (an upload, a command buffer), let it go once that work retired. It waits for
/// the work committed before the program let go, not for later commits, which cannot reach
/// it, and goes among the releases waiting in the order of their marks' commits, after those
/// noted at as many.
pub(crate) fn release_marked(&self, core: &Core, object: Free, marked: Option<Mark>) {
let wait = marked.map_or_else(|| self.noted(), |mark| mark.wait);
let now = matches!(wait, Wait::Progress(0));
if now
&& matches!(
object,
Free::TextureIndex(..)
| Free::TexelIndex(..)
| Free::SamplerIndex(..)
| Free::LevelElements(_)
)
{
return crate::reaper::free(core, object);
}
let mut state = self.lock();
if now && state.held.is_empty() {
drop(state);
return core.reaper.give(Job::Free(object));
}
// Noted under the lock, or placed by the commits it was noted at, the marks rise along
// the queue, and a later mark waits for no less: the entries whose waits are over are its
// first, which the block-maker thread hands on in one round.
let mark = marked.unwrap_or_else(|| self.mark());
let at = (state.held).partition_point(|&((_, commits), _)| commits <= mark.commits);
state.held.insert(at, ((object, mark.commits), mark.wait));
self.counters.rise(Gauge::ReleasesWaiting);
self.changed.notify_all();
}
/// The completion thread ran a round of retirements: it wakes the block-maker thread where a
/// free waits for work to complete.
pub(crate) fn retired(&self) {
self.rounds.fetch_add(1, Ordering::SeqCst);
if self.awaiting.load(Ordering::SeqCst) {
let _state = self.lock();
self.changed.notify_all();
}
}
/// For the device tests: the completion thread's rounds of retirements so far
/// ([`retired`](Self::retired)).
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn rounds(&self) -> u64 {
self.rounds.load(Ordering::SeqCst)
}
fn wait<'a>(&self, state: MutexGuard<'a, State>) -> MutexGuard<'a, State> {
self.changed
.wait(state)
.unwrap_or_else(PoisonError::into_inner)
}
fn wait_for<'a>(
&self,
state: MutexGuard<'a, State>,
timeout: Duration,
) -> MutexGuard<'a, State> {
match self.changed.wait_timeout(state, timeout) {
Ok((state, _)) => state,
Err(poisoned) => poisoned.into_inner().0,
}
}
/// For the device tests: the last commit's place in the commit order, which a release made
/// now notes while work is in flight.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn commits(&self) -> u64 {
self.commits.load(Ordering::SeqCst)
}
/// For the device tests: whether the class holds a wholly free unit ([`State::whole`]), so
/// the block-maker thread makes nothing for it.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn stocked(&self, class: MemoryClass) -> bool {
self.lock().whole(class)
}
/// For the device tests: the allocations of both classes with a block in use, counted where
/// the pool keeps them. An allocation the block-maker thread is making or freeing is not
/// among them, where the `Blocks` gauge counts it: the gauge rises before the allocation joins
/// the pool and falls after it left.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn in_use(&self) -> usize {
let state = self.lock();
(state.allocations.iter().flatten())
.filter(|allocation| allocation.live > 0)
.count()
}
/// For the device tests: the class's allocations, as each one's number, length, live blocks
/// and free bytes, in the order they were made.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn allocations(&self, class: MemoryClass) -> Vec<(AllocationId, u64, usize, u64)> {
let state = self.lock();
(state.allocations[class.index()].iter())
.map(|a| {
let free = a.free.free().iter().map(|&(start, end)| end - start).sum();
(a.id, a.len, a.live, free)
})
.collect()
}
/// For the device tests: each idle allocation the block-maker thread freed so far, as when
/// it became idle, when its free began and when it ended ([`host_time`](crate::host_time)).
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn frees(&self) -> Vec<(u64, u64, u64)> {
lock(&self.frees).clone()
}
/// For the device tests: whether the pool has the block back.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn holds(&self, block: BlockId) -> bool {
self.lock().returned.contains(&block)
}
/// The length and alignment of a block for a take of `len` bytes: `len` rounded up to the
/// granule, at a multiple of that rounded up to a power of two (at most [`BASE_ALIGN`]), of
/// the alignment of the pool's buffers and of the granule. No Vulkan call.
///
/// Err: [`Limit::BlockLen`] past `max`, the device's longest allocation and buffer.
fn shape(&self, len: u64, max: u64) -> Result<(u64, u64), Error> {
let len = round(len, self.granule, max)?;
Ok((len, base_align(len, self.alignment, self.granule)))
}
/// Takes a block of `class` of at least `len` bytes for `core`: the first free range that
/// fits, in the order the allocations were made, a long allocation only for a take of its own
/// length; with no Vulkan call. Where none fits, a range of an allocation made here, of
/// [`ALLOCATION_LEN`] or the block's length where that is longer, which the consumer's
/// [`BlockMappings::map`] maps first, counted at `site`. A block's bytes are undefined.
///
/// Err: [`Limit::BlockLen`] past the device's longest allocation or buffer;
/// [`Limit::BlockIds`]; what [`make_allocation`] returns, [`Limit::AllocationAddress`] among
/// it; [`Limit::AllocationNotMapped`] where the consumer did not map the allocation the take
/// made; [`Error::Quiesced`] once the pool is stopped.
pub(crate) fn take(
&self,
core: &Core,
class: MemoryClass,
len: u64,
max: u64,
site: Site,
) -> Result<Memory, Error> {
let started = crate::host_time();
let (len, align) = self.shape(len, max)?;
let index = class.index();
let mut state = self.lock();
let locked = crate::host_time();
self.counters
.timed(Part::TakeLock, 0, locked.saturating_sub(started), false);
let took = |memory: Memory, made: bool| {
self.counters.rise_by(class.placed_gauge(), memory.len);
let nanos = crate::host_time() - started;
(self.counters).timed(Part::Take, memory.len, nanos, made);
memory
};
// A take is a pool event: the block-maker thread tries a failed allocation again.
state.failed[index] = false;
let id = number(&mut state)?;
if state.stop {
return Err(Error::Quiesced);
}
let texture = matches!(site, Site::TextureBlock);
if let Some((carve, idle_since, long, ahead)) = state.fit(class, len, align, texture) {
if ahead {
self.counters.site_take_ahead(site, len);
}
if let Some(since) = idle_since {
(self.counters).reused_idle(crate::host_time().saturating_sub(since));
}
if long {
(self.counters).long_reuse(class == MemoryClass::Shared);
}
self.counters.carved();
self.settle(&mut state, class);
// The block-maker thread runs the idle rule on this take, and makes an allocation
// where it took the class's last wholly free unit.
self.changed.notify_all();
return Ok(took(carve.block(id, class, len), false));
}
drop(state);
let memory = self.own(core, class, len, align, id, site)?;
let mut state = self.lock();
self.settle(&mut state, class);
self.changed.notify_all();
Ok(took(memory, true))
}
/// Makes block `id` of `len` bytes at a multiple of `align` at the start of an allocation of
/// its own, of [`ALLOCATION_LEN`] or `len` where that is longer, whose rest later takes share,
/// counted at `site`; the consumer maps the allocation first.
fn own(
&self,
core: &Core,
class: MemoryClass,
len: u64,
align: u64,
id: BlockId,
site: Site,
) -> Result<Memory, Error> {
let started = crate::host_time();
let mut allocation = make_allocation(core, class, len.max(ALLOCATION_LEN), Some(site))?;
let made = crate::host_time();
allocation.made = (started, made.saturating_sub(started));
let bytes = allocation.len;
(self.counters).timed(Part::TakeMake, bytes, made.saturating_sub(started), true);
let (mut allocation, mapped) = self.map_made(core, allocation, class, true);
if self.mappings.is_some() {
(self.counters).timed(Part::TakeMap, bytes, crate::host_time() - made, false);
}
if !mapped {
free_allocation(core, allocation);
return Err(Error::Limit(Limit::AllocationNotMapped { class, len }));
}
let base = allocation.free.take(len, align).unwrap_or(0);
let memory = allocation.carve(base).block(id, class, len);
allocation.live = 1;
allocation.placed = len;
allocation.texture = matches!(site, Site::TextureBlock);
self.lock().allocations[class.index()].push(allocation);
Ok(memory)
}
/// [`Pool::map`] of `allocation`, just made: the allocation, and whether the consumer mapped
/// it. Where the hook panics, the allocation is freed and the panic goes on: the thread that
/// ran the hook ends with it (the block-maker thread as [`Ends`] says), and no allocation is
/// left that no block or free range holds.
fn map_made(
&self,
core: &Core,
allocation: Allocation,
class: MemoryClass,
on_take: bool,
) -> (Allocation, bool) {
match panic::catch_unwind(AssertUnwindSafe(|| self.map(&allocation, class, on_take))) {
Ok(mapped) => (allocation, mapped),
Err(panicked) => {
free_allocation(core, allocation);
panic::resume_unwind(panicked)
}
}
}
/// The consumer's [`BlockMappings::map`] of `allocation`, made on a take's thread where
/// `on_take`, off the pool's lock: true where it mapped the allocation or has no hook. An
/// allocation mapped while a quiesce ran the unmaps has its own unmap at once.
fn map(&self, allocation: &Allocation, class: MemoryClass, on_take: bool) -> bool {
let Some(mappings) = &self.mappings else {
return true;
};
if self.lock().unmapped {
return true;
}
let info = allocation.info(class);
if !mappings.map(&info) {
self.counters.map_refused();
return false;
}
self.counters.mapped(on_take);
let mut state = self.lock();
if state.unmapped {
drop(state);
mappings.unmap(info.id);
} else {
state.mapped.insert(info.id);
}
true
}
/// Runs the consumer's unmap of `allocation` where it was mapped and its unmap has not run,
/// off the pool's lock.
fn unmap(&self, allocation: AllocationId) {
let mapped = self.lock().mapped.remove(&allocation);
if let Some(mappings) = self.mappings.as_ref().filter(|_| mapped) {
mappings.unmap(allocation);
}
}
/// In [`Device::quiesce`](crate::Device::quiesce) and at teardown, once the runtime's other
/// threads are stopped: calls the unmap hook for every allocation mapped whose unmap has not
/// run, on this thread. No hook runs again: an allocation freed later is freed without it.
pub(crate) fn unmap_all(&self) {
let mut mapped: Vec<AllocationId> = {
let mut state = self.lock();
state.unmapped = true;
state.mapped.drain().collect()
};
mapped.sort_unstable();
if let Some(mappings) = &self.mappings {
for allocation in mapped {
mappings.unmap(allocation);
}
}
}
/// Sets the class's idle gauges: its idle allocations' bytes, when the oldest idle one the
/// idle rule weighs became idle, its reserve's bytes, and the bytes of its long allocations
/// that hold a block of a later take.
fn settle(&self, state: &mut State, class: MemoryClass) {
let index = class.index();
let idle: u64 = (state.allocations[index].iter())
.filter(|allocation| allocation.live == 0)
.map(|allocation| allocation.len)
.sum();
let was = state.idle_bytes[index];
if idle > was {
self.counters.rise_by(class.idle_gauge(), idle - was);
} else {
self.counters.fall_by(class.idle_gauge(), was - idle);
}
state.idle_bytes[index] = idle;
let (weighed, reserve) = state.aging(class);
let oldest = weighed.first().map_or(0, |&(since, _)| since);
let shared = class == MemoryClass::Shared;
(self.counters).idle_since(shared, oldest);
(self.counters).reserve_bytes(shared, reserve);
let reused: u64 = (state.allocations[index].iter())
.filter(|allocation| allocation.reused)
.map(|allocation| allocation.len)
.sum();
(self.counters).long_reused_bytes(shared, reused);
// The consumer's Private allocations: those holding a block it took, which no texture
// block shares.
if class == MemoryClass::Private {
let (mut held, mut placed, mut sparse_bytes, mut sparse) = (0, 0, 0, 0);
// The units that hold a block, and the consumer's bytes in them: a texture block's
// places count in `crate::places`, as its block fills its unit.
let (mut units, mut unit_bytes) = (0, 0);
for allocation in (state.allocations[index].iter())
.filter(|allocation| allocation.live > 0 && allocation.unit())
{
units += 1;
if !allocation.texture {
unit_bytes += allocation.placed;
}
}
for allocation in (state.allocations[index].iter())
.filter(|allocation| allocation.live > 0 && !allocation.texture)
{
held += allocation.len;
placed += allocation.placed;
if allocation.placed < allocation.len / SPARSE {
sparse_bytes += allocation.len;
sparse += 1;
}
}
let counters = &self.counters;
counters.set(Gauge::PrivateTakeHeldBytes, held);
counters.set(Gauge::PrivateTakeBytes, placed);
counters.set(Gauge::PrivateTakeSparseBytes, sparse_bytes);
counters.set(Gauge::PrivateTakeSparseAllocations, sparse);
counters.set(Gauge::PrivateUnits, units);
let was = state.unit_take_bytes;
if unit_bytes > was {
counters.rise_by(Gauge::PrivateUnitUsedBytes, unit_bytes - was);
} else {
counters.fall_by(Gauge::PrivateUnitUsedBytes, was - unit_bytes);
}
state.unit_take_bytes = unit_bytes;
}
}
/// The idle rule for `class` at `now` ([`host_time`](crate::host_time)): its next allocation
/// to free is a retiring one, a spilled one that emptied, so the next allocation tries
/// device-local memory again; else the oldest idle one it weighs ([`State::aging`]), once it
/// has been idle for [`IDLE_AGE_NANOS`].
fn rule(&self, state: &State, class: MemoryClass, now: u64) -> Rule {
let allocations = &state.allocations[class.index()];
if let Some(retiring) = (allocations.iter()).find(|allocation| allocation.retiring) {
return Rule::Free(retiring.memory);
}
match state.aging(class).0.first() {
Some(&(since, memory)) => {
idle_due(since, now).map_or(Rule::Free(memory), Rule::KeepUntil)
}
None => Rule::Keep,
}
}
/// Takes back a block taken with [`take`](Self::take): its bytes are a free range of its
/// allocation at once.
pub(crate) fn give_back(&self, memory: Memory) {
let class = memory.class;
let wait = self.noted();
self.counters.fall_by(class.placed_gauge(), memory.len);
let mut state = self.lock();
state.failed[class.index()] = false;
if let Some(allocation) = state.find(class, memory.memory) {
allocation.free.give(memory.base, memory.len);
allocation.live -= 1;
allocation.placed -= memory.len;
if allocation.live == 0 {
allocation.texture = false;
// Its idle time starts now, however long ago the block was released.
allocation.idle_since = crate::host_time();
allocation.wait = wait;
allocation.retiring = allocation.spilled;
allocation.reused = false;
}
}
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
state.returned.insert(memory.id);
self.settle(&mut state, class);
self.changed.notify_all();
}
/// Takes back an imported or exported block, for the block-maker thread to free, and then to
/// run an import's release.
pub(crate) fn give_back_own(&self, memory: Memory, release: Option<Release>) {
// Noted under the lock, as a release's ([`release`](Self::release)).
let mut state = self.lock();
let wait = self.noted();
state.owned.push_back(((memory, release), wait));
drop(state);
self.changed.notify_all();
}
/// Takes back memory of its own that another thread's object held, for the block-maker
/// thread to free ([`Loose`]).
///
/// # Safety
///
/// Nothing uses the memory or the buffer, and nothing will: no GPU work that used them is
/// pending, and what was bound to the memory but the buffer is destroyed.
pub(crate) unsafe fn give_back_loose(&self, loose: Loose) {
self.lock().loose.push_back(loose);
self.changed.notify_all();
}
/// Wakes the block-maker thread to weigh what another part of the device made idle: a
/// preprocess region ([`crate::indirect`]), or a parameter ring block ([`crate::ring`]).
pub(crate) fn nudge(&self) {
drop(self.lock());
self.changed.notify_all();
}
/// An import's or an export's numbers: its block's, and its allocation's. Err:
/// [`Limit::BlockIds`].
pub(crate) fn own_numbers(&self) -> Result<(BlockId, AllocationId), Error> {
let mut state = self.lock();
Ok((number(&mut state)?, allocation_number(&mut state)?))
}
/// Stops the block-maker thread once it finishes what it is doing.
pub(crate) fn stop(&self) {
self.lock().stop = true;
self.changed.notify_all();
}
/// Asks the block-maker thread to make `internal`, which it makes before its other work. False
/// where the thread has stopped: the call that needs it makes it.
pub(crate) fn ask(&self, internal: Internal) -> bool {
let mut state = self.lock();
if state.stop {
return false;
}
state.asked[internal.index()] = true;
self.changed.notify_all();
true
}
/// Waits while `internal` is asked of the block-maker thread or being made there: until it has
/// made it, its make failed, or it stopped. A wait is counted in [`Metrics::maker_waits`] and
/// its time in [`Metrics::maker_wait_max_nanos`]; where it is neither, the call returns at
/// once.
///
/// [`Metrics::maker_waits`]: crate::Metrics::maker_waits
/// [`Metrics::maker_wait_max_nanos`]: crate::Metrics::maker_wait_max_nanos
pub(crate) fn await_made(&self, internal: Internal) {
let mut state = self.lock();
if !state.pending(internal) {
return;
}
let since = crate::host_time();
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
{
state.waiting += 1;
}
while state.pending(internal) {
state = self.wait(state);
}
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
{
state.waiting -= 1;
}
drop(state);
(self.counters).maker_wait(crate::host_time().saturating_sub(since));
}
/// For the device tests: the calls waiting in [`Pool::await_made`], each counted from the
/// moment its wait's time starts until it ends.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn waiting(&self) -> usize {
self.lock().waiting
}
/// For the device tests: whether the block-maker thread is told to stop.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn stopped(&self) -> bool {
self.lock().stop
}
/// For the device tests: waits until the block-maker thread has made, or failed to make,
/// every internal asked of it; fails where it has not within the tests' patience.
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
pub(crate) fn await_internals(&self) {
let patience = crate::tests::command::on_device::PATIENCE;
let deadline = std::time::Instant::now() + patience;
let mut state = self.lock();
while state.asked.contains(&true) || state.making.is_some() {
let left = deadline.saturating_duration_since(std::time::Instant::now());
assert!(
!left.is_zero(),
"timed out waiting for the block-maker thread's internals"
);
state = self.wait_for(state, left);
}
}
}
impl State {
/// Whether `internal` is asked of the block-maker thread or being made there.
fn pending(&self, internal: Internal) -> bool {
self.asked[internal.index()] || self.making == Some(internal)
}
/// Whether the class holds a wholly free unit: a unit allocation of [`ALLOCATION_LEN`], not
/// retiring, that no block holds, so a take of up to a whole unit has a free range with no
/// Vulkan call. The block-maker thread makes the class's next allocation while it holds none
/// ([`run`]), and the idle rule keeps the last such unit in the order made, the reserve
/// ([`aging`](Self::aging)): once the class is idle past [`IDLE_AGE_NANOS`], it holds that one
/// wholly free unit and no other. The trade: a small take that breaks the class's last whole
/// unit sets off an allocation beside the consumer's next calls, where a rule on the class's
/// free bytes would wait until they ran low; in return a take of up to a unit, the 16 MiB
/// ones among them, lands in memory made ahead once the block-maker thread has made it.
fn whole(&self, class: MemoryClass) -> bool {
(self.allocations[class.index()].iter())
.any(|allocation| allocation.unit() && !allocation.retiring && allocation.live == 0)
}
/// The allocation of `class` whose memory is `memory`.
fn find(&mut self, class: MemoryClass, memory: vk::DeviceMemory) -> Option<&mut Allocation> {
(self.allocations[class.index()].iter_mut()).find(|allocation| allocation.memory == memory)
}
/// Reserves `len` bytes at a multiple of `align` in the class's allocations that serve a take
/// of `len` ([`Allocation::serves`]), counting a block live there, a texture block where
/// `texture`: the first such free range, in the order the allocations were made. With the
/// range, when the allocation became idle where it was, whether it is a long allocation, and
/// whether it is the first take to land in a unit the block-maker thread made ahead
/// ([`Allocation::ahead`]).
fn fit(
&mut self,
class: MemoryClass,
len: u64,
align: u64,
texture: bool,
) -> Option<(Carve, Option<u64>, bool, bool)> {
let (allocation, base) = (self.allocations[class.index()].iter_mut())
.filter(|allocation| allocation.serves(len))
.find_map(|allocation| {
let base = allocation.free.take(len, align)?;
Some((allocation, base))
})?;
let idle_since = (allocation.live == 0).then_some(allocation.idle_since);
allocation.live += 1;
allocation.placed += len;
allocation.texture |= texture;
let long = !allocation.unit();
if long {
allocation.reused = true;
}
let ahead = allocation.ahead;
allocation.ahead = false;
Some((allocation.carve(base), idle_since, long, ahead))
}
/// The class's idle allocations the idle rule weighs, as when each became idle and its memory,
/// the oldest first, and the bytes of its reserve. The last idle unit allocation, of
/// [`ALLOCATION_LEN`], in the order the allocations were made is the reserve, the class's
/// wholly free unit ([`whole`](Self::whole)): the rule neither weighs nor frees it. Takes land
/// in the first allocation with room, so the last is the one they reach last, and a class whose
/// takes come and go within a unit's bytes keeps its reserve with no allocation made or freed.
/// A long allocation, which a take longer than the unit made for itself, is never the reserve:
/// the rule frees it at its idle age, so it is not held for the device's life.
fn aging(&self, class: MemoryClass) -> (Vec<(u64, vk::DeviceMemory)>, u64) {
let idle = (self.allocations[class.index()].iter())
.filter(|allocation| allocation.live == 0 && !allocation.retiring);
let reserve = idle.clone().rfind(|allocation| allocation.unit());
let mut weighed: Vec<(u64, vk::DeviceMemory)> = idle
.filter(|allocation| reserve.is_none_or(|reserve| reserve.memory != allocation.memory))
.map(|allocation| (allocation.idle_since, allocation.memory))
.collect();
weighed.sort_unstable_by_key(|&(since, _)| since);
(weighed, reserve.map_or(0, |reserve| reserve.len))
}
/// Takes the allocation of `class` whose memory is `memory` out of the pool, for the caller to
/// free.
fn take_out(&mut self, class: MemoryClass, memory: vk::DeviceMemory) -> Option<Allocation> {
let index = class.index();
let at =
(self.allocations[index].iter()).position(|allocation| allocation.memory == memory)?;
Some(self.allocations[index].remove(at))
}
}
impl Allocation {
/// What a block at `base`, a range taken from the free bytes, is.
fn carve(&self, base: u64) -> Carve {
Carve {
allocation: self.id,
memory: self.memory,
buffer: self.buffer,
address: self.address + base,
base,
// SAFETY: `base` is inside the allocation, which the mapping spans.
mapping: self
.mapping
.map(|mapping| Mapping(unsafe { mapping.at(base) })),
device_local: self.device_local,
spilled: self.spilled,
}
}
/// The allocation as the consumer's hooks are told of it.
fn info(&self, class: MemoryClass) -> AllocationInfo {
AllocationInfo {
id: self.id,
class,
len: self.len,
gpu_address: self.address,
contents: self.mapping.map(Mapping::get),
make_start: self.made.0,
make_nanos: self.made.1,
}
}
}
/// A block's length for `len` bytes: `len` (at least 1) rounded up to `granule`, a power of two.
///
/// Err: [`Limit::BlockLen`] past `max`, the device's longest allocation and buffer.
pub(crate) fn round(len: u64, granule: u64, max: u64) -> Result<u64, Error> {
match len.max(1).checked_next_multiple_of(granule) {
Some(rounded) if rounded <= max => Ok(rounded),
rounded => Err(Error::Limit(Limit::BlockLen {
len: rounded.unwrap_or(u64::MAX),
max,
})),
}
}
/// The alignment of the first byte of a block of `len` bytes in its allocation: `len` rounded up
/// to a power of two, at most [`BASE_ALIGN`], and at least its buffer's `alignment` and the
/// `granule`.
pub(crate) fn base_align(len: u64, alignment: u64, granule: u64) -> u64 {
(len.checked_next_power_of_two().unwrap_or(BASE_ALIGN))
.min(BASE_ALIGN)
.max(alignment)
.max(granule)
}
fn number(state: &mut State) -> Result<BlockId, Error> {
let id = state.next_id;
state.next_id = id.checked_add(1).ok_or(Error::Limit(Limit::BlockIds))?;
Ok(BlockId(id))
}
fn allocation_number(state: &mut State) -> Result<AllocationId, Error> {
let id = state.next_allocation;
state.next_allocation = id.checked_add(1).ok_or(Error::Limit(Limit::BlockIds))?;
Ok(AllocationId(id))
}
/// The alignment of a buffer of the pool's usage on `device`, the same for every such buffer.
pub(crate) fn buffer_alignment(device: &ash::Device) -> u64 {
let buffer = vk::BufferCreateInfo::default()
.size(GRANULE)
.usage(floor::BUFFER_USAGE)
.sharing_mode(vk::SharingMode::EXCLUSIVE);
let of = vk::DeviceBufferMemoryRequirements::default().create_info(&buffer);
let mut needs = vk::MemoryRequirements2::default();
// SAFETY: the device is live, and the create info valid.
unsafe { device.get_device_buffer_memory_requirements(&of, &mut needs) };
needs.memory_requirements.alignment
}
/// Makes an allocation ahead of need of `class`, and has the consumer map it: the allocation, or
/// `None` where it was not made or not mapped.
fn ahead(core: &Core, class: MemoryClass) -> Option<Allocation> {
let started = crate::host_time();
let mut allocation = make_allocation(core, class, ALLOCATION_LEN, None).ok()?;
allocation.made = (started, crate::host_time() - started);
(core.counters).timed(Part::MakerMake, allocation.len, allocation.made.1, true);
let (allocation, mapped) = core.pool.map_made(core, allocation, class, false);
if !mapped {
free_allocation(core, allocation);
return None;
}
Some(allocation)
}
/// In the device's making, before the block-maker thread starts: makes each class's first
/// allocation, and has the consumer map each, so the consumer's first
/// blocks of each class are ranges of it as the device is ready, and nothing is allocated beside
/// the consumer's first calls. Where one is not made, the block-maker thread tries again after
/// the class's next take or give-back.
pub(crate) fn stock(core: &Core) {
// Made ahead of need, as the block-maker thread's are: counted with them.
make_ahead_here(Some(Arc::clone(&core.submit_calls)));
for class in MemoryClass::ALL {
let made = ahead(core, class);
let mut state = core.pool.lock();
match made {
Some(allocation) => state.allocations[class.index()].push(allocation),
None => state.failed[class.index()] = true,
}
core.pool.settle(&mut state, class);
}
make_ahead_here(None);
}
/// The block-maker thread, until the device's teardown or a quiesce stops it. Off every caller's
/// path, in order: it takes up each internal asked of it and makes it ([`Internal`]), the
/// device's fixed-size ones first of all, so an ask made during a make is made after it; it
/// frees each imported or exported block given back, then runs an import's release; it makes an
/// allocation of a class that holds no wholly free unit ([`State::whole`]), having the consumer
/// map it before it joins the pool; and it frees each allocation idle for [`IDLE_AGE_NANOS`],
/// after the consumer's unmap of it, one at a time, and each parameter ring block the ring's idle
/// rule gives it ([`crate::ring::Ring::due`]). Each of its allocations and frees starts once
/// the submit calls inside the driver as its previous one returned have returned
/// ([`maker_call`]), and what it made is handed on as its call returns. It sleeps until
/// the next change, or until the oldest idle allocation reaches the idle age. Stopped, it makes
/// nothing more: what is asked of it and not made, the calls that need it make. It ends as a stop
/// however it ends, a panic of what it runs (one of the consumer's [`BlockMappings`] hooks) too
/// ([`Ends`]).
pub(crate) fn run(core: Arc<Core>) {
let _ends = Ends(&core);
make_ahead_here(Some(Arc::clone(&core.submit_calls)));
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
if let MakerTest::Park(park) = &core.maker_test {
park.wait();
park.wait();
}
let pool = &core.pool;
let mut state = pool.lock();
loop {
// A free whose wait is not over holds; a retirement round since this read wakes it.
let round = pool.rounds.load(Ordering::SeqCst);
let mut held = false;
if state.stop {
return;
}
if let Some(internal) = (Internal::ALL.into_iter()).find(|i| state.asked[i.index()]) {
// Taken up before the make: an ask made while it runs (the ring asks again once it has
// this make's block) is a new one, made next.
state.asked[internal.index()] = false;
state.making = Some(internal);
drop(state);
make_internal(&core, internal);
state = pool.lock();
state.making = None;
pool.changed.notify_all();
continue;
}
let own;
(state, own) = ripe(&core, state, |state| &mut state.owned, &mut held);
if let Some((memory, release)) = own {
drop(state);
free_own(&core, &memory, release);
state = pool.lock();
continue;
}
if let Some(loose) = state.loose.pop_front() {
drop(state);
// SAFETY: as given back.
unsafe { free_loose(&core, loose) };
state = pool.lock();
continue;
}
let object;
(state, object) = ripe(&core, state, |state| &mut state.held, &mut held);
if let Some((object, _)) = object {
drop(state);
// An index goes here, before the gauge falls, so every released index is vacated once
// nothing waits; an object goes to the reaper.
match object {
Free::TextureIndex(..)
| Free::TexelIndex(..)
| Free::SamplerIndex(..)
| Free::LevelElements(_) => crate::reaper::free(&core, object),
_ => core.reaper.give(Job::Free(object)),
}
core.counters.fall(Gauge::ReleasesWaiting);
state = pool.lock();
continue;
}
let wanted = (MemoryClass::ALL.into_iter())
.find(|&class| !state.failed[class.index()] && !state.whole(class));
if let Some(class) = wanted {
drop(state);
let made = ahead(&core, class);
state = pool.lock();
match made {
// Idle from now: the reserve, which the idle rule does not weigh.
Some(mut allocation) => {
allocation.ahead = true;
state.allocations[class.index()].push(allocation);
}
None => state.failed[class.index()] = true,
}
pool.settle(&mut state, class);
pool.changed.notify_all();
continue;
}
// The idle rule, both classes: the next allocation to free; first, a preprocess region
// idle for the idle age, and a ring block.
let now = crate::host_time();
let mut wake: Option<Duration> = None;
match (core.indirect.as_ref()).and_then(|indirect| indirect.due(now)) {
Some(Ok(region)) => {
drop(state);
// SAFETY: an idle region is in no pending work.
unsafe { free_loose(&core, region.into_loose()) };
state = pool.lock();
continue;
}
Some(Err(at)) => wake = Some(Duration::from_nanos(at.saturating_sub(now))),
None => {}
}
// Then a parameter ring block past the first, idle for the idle age.
match core.ring.due(now) {
Some(Ok(block)) => {
drop(state);
// SAFETY: every chunk of the block is free, so no pending work reads it.
unsafe { free_loose(&core, block) };
state = pool.lock();
continue;
}
Some(Err(at)) => {
let due = Duration::from_nanos(at.saturating_sub(now));
wake = Some(wake.map_or(due, |wake| wake.min(due)));
}
None => {}
}
let mut gone = None;
for class in MemoryClass::ALL {
let due = match pool.rule(&state, class, now) {
Rule::Free(memory) => {
let Some(mut wait) = state.find(class, memory).map(|a| a.wait) else {
continue;
};
pool.awaiting.store(true, Ordering::SeqCst);
drop(state);
let over = wait.over(&core);
state = pool.lock();
match state.find(class, memory) {
// Still idle: no take landed in it while the lock was dropped.
Some(allocation) if allocation.live == 0 && over => {}
Some(allocation) => {
allocation.wait = wait;
held |= allocation.live == 0;
continue;
}
None => continue,
}
gone = state.take_out(class, memory);
pool.settle(&mut state, class);
break;
}
Rule::KeepUntil(at) => Duration::from_nanos(at.saturating_sub(now)),
Rule::Keep => continue,
};
wake = Some(wake.map_or(due, |wake| wake.min(due)));
}
if let Some(allocation) = gone {
pool.changed.notify_all();
drop(state);
let class = allocation.class;
free_idle(&core, allocation);
state = pool.lock();
state.freed_at[class.index()] = crate::host_time();
continue;
}
if held && pool.rounds.load(Ordering::SeqCst) != round {
continue;
}
pool.awaiting.store(held, Ordering::SeqCst);
state = match wake {
Some(timeout) => pool.wait_for(state, timeout),
None => pool.wait(state),
};
}
}
/// The block-maker thread's end, however it comes: a stop, or a panic of what it runs, such as one
/// of the consumer's [`BlockMappings`] hooks. The pool is stopped, as a quiesce stops it, so a take
/// is [`Error::Quiesced`]; and nothing asked of the thread waits for it: an internal asked or being
/// made is the asking call's to make ([`Pool::ask`], [`Pool::await_made`]), and the ring's next
/// block the next take's ([`crate::ring::Ring::unasked`]).
struct Ends<'a>(&'a Core);
impl Drop for Ends<'_> {
fn drop(&mut self) {
let pool = &self.0.pool;
let mut state = pool.lock();
state.stop = true;
state.asked = [false; INTERNALS];
state.making = None;
pool.changed.notify_all();
drop(state);
self.0.ring.unasked();
}
}
/// The oldest entry of a queue of memory given back, taken out where its [`Wait`] is over; where
/// it is not, it stays first, and `held` is set. The pool's lock is dropped for the check. The
/// waits are in the order they were noted in, by their marks' commits for the releases
/// ([`Mark`]), and a later one waits for no less, so no entry behind one that holds is over: the
/// thread, taking one after another, hands on each that is in one round.
fn ripe<'a, T>(
core: &'a Core,
mut state: MutexGuard<'a, State>,
queue: fn(&mut State) -> &mut VecDeque<(T, Wait)>,
held: &mut bool,
) -> (MutexGuard<'a, State>, Option<T>) {
let Some((item, mut wait)) = queue(&mut state).pop_front() else {
return (state, None);
};
core.pool.awaiting.store(true, Ordering::SeqCst);
drop(state);
let over = wait.over(core);
let mut state = core.pool.lock();
if over {
return (state, Some(item));
}
queue(&mut state).push_front((item, wait));
*held = true;
(state, None)
}
/// On the block-maker thread: makes `internal` with the function a call that needs it makes it
/// with, into the part of the device that holds it. A make that fails leaves it unmade: the call
/// that needs it makes it on its own thread, where the allocation may succeed then.
fn make_internal(core: &Arc<Core>, internal: Internal) {
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
if let MakerTest::Fail = core.maker_test {
if internal == Internal::RingBlock {
core.ring.unasked();
core.ring.meet_park();
}
return;
}
let _ = match internal {
Internal::ZeroSource => crate::fill::make(core),
Internal::RingBlock => core.ring.make_ahead(core),
Internal::StagingRegion => core.work.make_staging_region(core),
};
}
/// [`allocate`] of `info`, and for Private memory, where the device-local type is out of device
/// memory, of each other type `bits` allows in the device's order until one is not: the spill
/// the plan decided, on the allocation's own error and never a budget poll. Returns the memory
/// and whether it spilled. Each call is counted at `site`, where it names one.
///
/// # Safety
///
/// As [`allocate`], for every type `bits` allows.
pub(crate) unsafe fn allocate_spilling(
core: &Core,
class: MemoryClass,
bits: u32,
info: &vk::MemoryAllocateInfo<'_>,
site: Option<Site>,
) -> Result<(vk::DeviceMemory, bool), Error> {
let out = |result: &Result<vk::DeviceMemory, Error>| {
result.as_ref().is_err_and(Error::is_out_of_device_memory)
};
let first = info.memory_type_index;
let mut result = match core.fails_private(class) {
true => Err(Error::out_of_device_memory()),
// SAFETY: the caller's.
false => unsafe {
allocate(
&core.device,
&core.counters,
core.max_allocations,
info,
site,
)
},
};
if class == MemoryClass::Shared {
return result.map(|memory| (memory, false));
}
for index in (0..u32::BITS).filter(|&index| index != first && bits & (1 << index) != 0) {
if !out(&result) {
break;
}
let mut next = *info;
next.memory_type_index = index;
// SAFETY: the caller's; `bits` allows the type.
result = unsafe {
allocate(
&core.device,
&core.counters,
core.max_allocations,
&next,
site,
)
};
if let Ok(memory) = result {
return Ok((memory, true));
}
}
result.map(|memory| (memory, false))
}
/// `vkAllocateMemory`, every allocation the runtime makes: one more live allocation in
/// `counters`' blocks gauge, refused with [`Limit::MemoryAllocations`] before the driver is called
/// where `max`, the device's `maxMemoryAllocationCount`, are live. Each free falls the gauge. The
/// call's time is counted on the block-maker thread or on the consumer's, and on the consumer's
/// at `site` too, where the call names one. On the block-maker thread it is a [`maker_call`].
///
/// # Safety
///
/// `info` is valid for `device`.
pub(crate) unsafe fn allocate(
device: &ash::Device,
counters: &Counters,
max: u32,
info: &vk::MemoryAllocateInfo<'_>,
site: Option<Site>,
) -> Result<vk::DeviceMemory, Error> {
if !counters.rise_below(Gauge::Blocks, u64::from(max)) {
return Err(Error::Limit(Limit::MemoryAllocations { max }));
}
let (allocated, nanos) = maker_call(|| {
let start = crate::host_time();
// SAFETY: the caller's.
let allocated = unsafe { device.allocate_memory(info, None) };
(allocated, crate::host_time().saturating_sub(start))
});
let maker = on_maker();
counters.allocation_time(nanos, maker);
if let (false, Some(site)) = (maker, site) {
counters.site_allocation(site, info.allocation_size, nanos);
}
allocated.map_err(|result| {
counters.fall(Gauge::Blocks);
Error::Vulkan {
call: "vkAllocateMemory",
result,
}
})
}
/// Makes an allocation of `len` bytes of `class` with device addresses, its number, the
/// `VkBuffer` with every buffer usage that spans it and that buffer's address, mapped whole if
/// Shared, its free ranges all of it. Nothing of the runtime writes its bytes, and a Shared
/// allocation's pages become resident as they are first written. Its bytes are undefined. Counted
/// at `site`, where it names one.
///
/// Err: what the allocation, the bind or the map returns; [`Limit::AllocationAddress`] where the
/// device placed the buffer's address off a multiple of [`BASE_ADDRESS`], the allocation freed.
fn make_allocation(
core: &Core,
class: MemoryClass,
len: u64,
site: Option<Site>,
) -> Result<Allocation, Error> {
let device = &core.device;
let (id, freed_at) = {
let mut state = core.pool.lock();
(
allocation_number(&mut state)?,
state.freed_at[class.index()],
)
};
let info = vk::BufferCreateInfo::default()
.size(len)
.usage(floor::BUFFER_USAGE)
.sharing_mode(vk::SharingMode::EXCLUSIVE);
// SAFETY: the device is live, and the create info valid: `len` is at most its
// `maxBufferSize`.
let buffer =
unsafe { device.create_buffer(&info, None) }.map_err(Error::vulkan("vkCreateBuffer"))?;
// SAFETY: the buffer is the device's.
let requirements = unsafe { device.get_buffer_memory_requirements(buffer) };
let mut flags =
vk::MemoryAllocateFlagsInfo::default().flags(vk::MemoryAllocateFlags::DEVICE_ADDRESS);
let allocate_info = vk::MemoryAllocateInfo::default()
.allocation_size(requirements.size.max(len))
.memory_type_index(core.memory_type(class))
.push_next(&mut flags);
let bits = requirements.memory_type_bits;
// SAFETY: the floor chose the memory type for buffers of this usage on this device, and
// `bufferDeviceAddress` is enabled; a spill takes a type the buffer's requirements allow.
let (memory, spilled) =
match unsafe { allocate_spilling(core, class, bits, &allocate_info, site) } {
Ok(made) => made,
Err(error) => {
// SAFETY: the buffer is unused.
unsafe { device.destroy_buffer(buffer, None) };
return Err(error);
}
};
let mut allocation = Allocation {
id,
class,
memory,
buffer,
address: 0,
len,
mapping: None,
device_local: !spilled && core.device_local & (1 << core.memory_type(class)) != 0,
spilled,
free: Ranges::new(len),
live: 0,
placed: 0,
texture: false,
idle_since: crate::host_time(),
made: (0, 0),
wait: Wait::Progress(0),
retiring: false,
reused: false,
ahead: false,
};
core.counters.count(Metric::BlocksMade);
core.counters.allocated(len);
if freed_at != 0 && remade(freed_at, crate::host_time()) {
(core.counters).remade_after_free(class == MemoryClass::Shared);
}
core.counters.rise_by(class.allocation_gauge(), len);
if spilled {
core.counters.rise_by(Gauge::SpilledBytes, len);
}
// SAFETY: the memory is of a type the buffer's requirements allow, at least their size, and
// nothing else is bound to the buffer.
if let Err(result) = unsafe { device.bind_buffer_memory(buffer, memory, 0) } {
free_allocation(core, allocation);
return Err(Error::Vulkan {
call: "vkBindBufferMemory",
result,
});
}
let of = vk::BufferDeviceAddressInfo::default().buffer(buffer);
// SAFETY: the buffer has device-address usage and is bound.
allocation.address = unsafe { device.get_buffer_device_address(&of) };
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
{
allocation.address += core.placed_past;
}
(core.spans).made(
buffer,
allocation.address,
len,
RangeKind::Allocation(id, class),
);
if !allocation.address.is_multiple_of(BASE_ADDRESS) {
let address = allocation.address;
free_allocation(core, allocation);
return Err(Error::Limit(Limit::AllocationAddress { class, address }));
}
if class == MemoryClass::Shared {
// SAFETY: the memory type is host-visible, and the memory is not mapped.
let mapped = unsafe {
device.map_memory(
allocation.memory,
0,
vk::WHOLE_SIZE,
vk::MemoryMapFlags::empty(),
)
};
match mapped {
Ok(pointer) => allocation.mapping = NonNull::new(pointer.cast::<u8>()).map(Mapping),
Err(result) => {
free_allocation(core, allocation);
return Err(Error::Vulkan {
call: "vkMapMemory",
result,
});
}
}
}
Ok(allocation)
}
/// The memory requirements of an import's buffer of `len` bytes for `handle`.
pub(crate) fn import_requirements(
core: &Core,
len: u64,
handle: vk::ExternalMemoryHandleTypeFlags,
) -> vk::MemoryRequirements {
let mut external = vk::ExternalMemoryBufferCreateInfo::default().handle_types(handle);
let buffer = vk::BufferCreateInfo::default()
.size(len)
.usage(floor::BUFFER_USAGE)
.sharing_mode(vk::SharingMode::EXCLUSIVE)
.push_next(&mut external);
let of = vk::DeviceBufferMemoryRequirements::default().create_info(&buffer);
let mut needs = vk::MemoryRequirements2::default();
// SAFETY: the device is live, and the create info valid: `len` is at most its
// `maxBufferSize`, and its buffers import `handle` ([`crate::import`]).
unsafe {
core.device
.get_device_buffer_memory_requirements(&of, &mut needs)
};
needs.memory_requirements
}
/// Why an import's block was not made.
pub(crate) enum ImportFailed {
/// Nothing was imported: the buffer, or `vkAllocateMemory` (or the count of allocations ahead
/// of it), failed. The handle is still the caller's.
Before(Error),
/// The import was made, then freed, with the handle it took: binding the buffer, or the
/// block's numbers, failed.
After(Error),
}
impl ImportFailed {
pub(crate) fn error(self) -> Error {
match self {
ImportFailed::Before(error) | ImportFailed::After(error) => error,
}
}
}
/// Makes an import's block: a buffer of `info`'s length for `handle`, the import `info` asks
/// for (counted against the device's allocations, [`allocate`]), bound to the buffer at its first
/// byte, the buffer's address, and its numbers: the block's, and its allocation's, which no hook
/// is told of. `mapping` is the host address of its bytes, where the consumer imported them by
/// address. `own` is [`Own::Import`] for the consumer's import, Shared as Metal's no-copy buffers
/// are, counted in the imports gauge; or [`Own::Surface`] for a dma-buf surface's buffer
/// ([`crate::Surface::buffer`]), Private as an export is, counted in the surface gauges.
///
/// # Safety
///
/// `info` is a valid import of `handle` into a type its buffers can use.
pub(crate) unsafe fn import(
core: &Core,
info: &vk::MemoryAllocateInfo<'_>,
handle: vk::ExternalMemoryHandleTypeFlags,
mapping: Option<Mapping>,
own: Own,
) -> Result<Memory, ImportFailed> {
let device = &core.device;
let len = info.allocation_size;
let mut external = vk::ExternalMemoryBufferCreateInfo::default().handle_types(handle);
let buffer = vk::BufferCreateInfo::default()
.size(len)
.usage(floor::BUFFER_USAGE)
.sharing_mode(vk::SharingMode::EXCLUSIVE)
.push_next(&mut external);
// SAFETY: as for `import_requirements`.
let buffer = unsafe { device.create_buffer(&buffer, None) }.map_err(|result| {
ImportFailed::Before(Error::Vulkan {
call: "vkCreateBuffer",
result,
})
})?;
// SAFETY: the caller's.
let memory = match unsafe { allocate(device, &core.counters, core.max_allocations, info, None) }
{
Ok(memory) => memory,
Err(error) => {
// SAFETY: `buffer` is unused.
unsafe { device.destroy_buffer(buffer, None) };
return Err(ImportFailed::Before(error));
}
};
let undo = |error: Error| {
// SAFETY: nothing uses the buffer or the memory.
unsafe {
device.destroy_buffer(buffer, None);
device.free_memory(memory, None);
}
core.counters.fall(Gauge::Blocks);
ImportFailed::After(error)
};
// SAFETY: the memory is of a type the buffer can use, and at least its size
// ([`crate::import`] checks both), and no other buffer is bound to it.
if let Err(result) = unsafe { device.bind_buffer_memory(buffer, memory, 0) } {
return Err(undo(Error::Vulkan {
call: "vkBindBufferMemory",
result,
}));
}
let (id, allocation) = core.pool.own_numbers().map_err(undo)?;
let address = vk::BufferDeviceAddressInfo::default().buffer(buffer);
// SAFETY: the buffer has device-address usage and is bound.
let address = unsafe { device.get_buffer_device_address(&address) };
let class = match own {
Own::Import { .. } => {
core.counters.imported(len);
MemoryClass::Shared
}
_ => {
core.counters.rise(Gauge::SurfaceBuffers);
#[cfg(unix)]
core.counters.surface_imported(len);
MemoryClass::Private
}
};
Ok(Memory {
id,
class,
len,
allocation,
memory,
base: 0,
buffer,
address,
mapping,
device_local: core.device_local & (1 << info.memory_type_index) != 0,
spilled: false,
own: Some(own),
})
}
/// Makes the sparse buffer of an import over several dma-bufs ([`crate::Device::import_dma_bufs`]):
/// `len` bytes of the floor's usage, for dma-bufs, made with `VK_BUFFER_CREATE_SPARSE_BINDING_BIT`,
/// bound to nothing yet; with its memory requirements.
///
/// Err: [`Error::Vulkan`] where the buffer cannot be made.
///
/// # Safety
///
/// The device was made with `sparseBinding` for such buffers ([`crate::import::binds_dma_bufs`]),
/// and `len` is at most its `maxBufferSize`.
#[cfg(unix)]
pub(crate) unsafe fn sparse_buffer(
core: &Core,
len: u64,
) -> Result<(vk::Buffer, vk::MemoryRequirements), Error> {
let device = &core.device;
let mut external = vk::ExternalMemoryBufferCreateInfo::default()
.handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT);
let info = vk::BufferCreateInfo::default()
.flags(vk::BufferCreateFlags::SPARSE_BINDING)
.size(len)
.usage(floor::BUFFER_USAGE)
.sharing_mode(vk::SharingMode::EXCLUSIVE)
.push_next(&mut external);
// SAFETY: the caller's.
let buffer =
unsafe { device.create_buffer(&info, None) }.map_err(Error::vulkan("vkCreateBuffer"))?;
// SAFETY: the buffer is live.
let needs = unsafe { device.get_buffer_memory_requirements(buffer) };
Ok((buffer, needs))
}
/// Imports one dma-buf of an import over several, `info`'s, with no buffer: counted against the
/// device's allocations ([`allocate`]) and in the imports gauge.
///
/// # Safety
///
/// `info` is a valid import of a dma-buf.
#[cfg(unix)]
pub(crate) unsafe fn import_chunk(
core: &Core,
info: &vk::MemoryAllocateInfo<'_>,
) -> Result<vk::DeviceMemory, Error> {
// SAFETY: the caller's.
let memory = unsafe {
allocate(
&core.device,
&core.counters,
core.max_allocations,
info,
None,
)
}?;
core.counters.imported(info.allocation_size);
Ok(memory)
}
/// Frees a chunk's import that no block holds: an import over several dma-bufs that failed past
/// it, before its bind.
///
/// # Safety
///
/// Nothing is bound to `memory`, an import of `bytes`, and nothing uses it.
#[cfg(unix)]
pub(crate) unsafe fn free_chunk(core: &Core, memory: vk::DeviceMemory, bytes: u64) {
// SAFETY: the caller's.
unsafe { core.device.free_memory(memory, None) };
core.counters.fall(Gauge::Blocks);
core.counters.import_freed(bytes);
}
/// What [`sparse_block`] makes a block of: the sparse buffer of `len` bytes, its chunks' imports
/// in order with their lengths, the memory type they are of, the bind's value, the sparse address
/// space its memory requirements name, and its numbers.
#[cfg(unix)]
pub(crate) struct SparseParts {
pub(crate) buffer: vk::Buffer,
pub(crate) len: u64,
pub(crate) chunks: Vec<(vk::DeviceMemory, u64)>,
pub(crate) index: u32,
pub(crate) bind: u64,
pub(crate) space: u64,
pub(crate) numbers: (BlockId, AllocationId),
}
/// The block of an import over several dma-bufs: Shared, as an import is, over the sparse buffer
/// from its first byte, at the buffer's device address, never mapped. Each chunk is a block of its
/// own under it ([`Block::_chunks`]), of the same numbers, its memory and no buffer, the last one
/// holding `release`.
#[cfg(unix)]
pub(crate) fn sparse_block(
core: &Arc<Core>,
parts: SparseParts,
release: Option<Release>,
) -> Block {
let SparseParts {
buffer,
len,
chunks,
index,
bind,
space,
numbers: (id, allocation),
} = parts;
let address = vk::BufferDeviceAddressInfo::default().buffer(buffer);
// SAFETY: the buffer has device-address usage, and it is sparse
// (`VUID-vkGetBufferDeviceAddress-bufferDeviceAddress-03324`). Its addresses run over its
// whole size (`VUID-VkDeviceAddress-size-11364`), across its chunks.
let address = unsafe { core.device.get_buffer_device_address(&address) };
let device_local = core.device_local & (1 << index) != 0;
let memory =
|memory: vk::DeviceMemory, len: u64, buffer: vk::Buffer, address: u64, own: Own| Memory {
id,
class: MemoryClass::Shared,
len,
allocation,
memory,
base: 0,
buffer,
address,
mapping: None,
device_local,
spilled: false,
own: Some(own),
};
let chunk = |(memory_of, chunk_len): (vk::DeviceMemory, u64), release: Option<Release>| {
let held = memory(memory_of, chunk_len, vk::Buffer::null(), 0, Own::Chunk);
Block::imported(held, Arc::clone(core), release)
};
let chunks = match chunks.split_last() {
Some((&last, before)) => (before.iter())
.map(|&before| chunk(before, None))
.chain(std::iter::once(chunk(last, release)))
.collect(),
None => Vec::new(),
};
let own = Own::Sparse { bind, space };
let memory = memory(vk::DeviceMemory::null(), len, buffer, address, own);
note(core, &memory);
Block {
memory,
core: Arc::clone(core),
release: Mutex::new(None),
_under: None,
_chunks: chunks,
addressed: None,
aliases: None,
}
}
/// Makes an export's block: a buffer of `len` bytes rounded up to 64 KiB, as a take's are, that
/// exports `handle`, its dedicated allocation in the Private memory type, exportable as `handle`
/// (counted against the device's allocations, [`allocate`]), bound at its first byte, the
/// buffer's address, and its numbers: the block's, and its allocation's, which no hook is told
/// of. Private; never mapped. With it, the allocation's size, which an import of
/// the handle names.
///
/// Err: [`Limit::BlockLen`] past the device's longest allocation or buffer; [`Limit::ExportType`]
/// where the buffer's memory requirements do not list the Private type;
/// [`Limit::MemoryAllocations`]; [`Limit::BlockIds`]; [`Error::Vulkan`] where the buffer, the
/// allocation or its binding fails.
///
/// # Safety
///
/// The device's buffers export `handle` with the floor's usage, its extensions enabled
/// ([`crate::export`]).
pub(crate) unsafe fn export(
core: &Core,
len: u64,
handle: crate::ExportHandle,
) -> Result<(Memory, u64), Error> {
let device = &core.device;
let handle_type = handle.vk();
let len = round(len, GRANULE, core.max_block_len)?;
let mut external = vk::ExternalMemoryBufferCreateInfo::default().handle_types(handle_type);
let info = vk::BufferCreateInfo::default()
.size(len)
.usage(floor::BUFFER_USAGE)
.sharing_mode(vk::SharingMode::EXCLUSIVE)
.push_next(&mut external);
// SAFETY: `len` is at most `maxBufferSize` (`round`), and the buffers export `handle` (the
// caller's).
let buffer =
unsafe { device.create_buffer(&info, None) }.map_err(Error::vulkan("vkCreateBuffer"))?;
let destroy = || {
// SAFETY: nothing uses the buffer.
unsafe { device.destroy_buffer(buffer, None) }
};
// SAFETY: the buffer is live.
let needs = unsafe { device.get_buffer_memory_requirements(buffer) };
let index = core.memory_type(MemoryClass::Private);
if needs.memory_type_bits & (1 << index) == 0 {
destroy();
return Err(Error::Limit(Limit::ExportType {
handle,
listed: Some(needs.memory_type_bits),
}));
}
let mut export = vk::ExportMemoryAllocateInfo::default().handle_types(handle_type);
let mut dedicated = vk::MemoryDedicatedAllocateInfo::default().buffer(buffer);
let mut flags =
vk::MemoryAllocateFlagsInfo::default().flags(vk::MemoryAllocateFlags::DEVICE_ADDRESS);
let info = vk::MemoryAllocateInfo::default()
.allocation_size(needs.size)
.memory_type_index(index)
.push_next(&mut export)
.push_next(&mut dedicated)
.push_next(&mut flags);
let started = crate::host_time();
// SAFETY: a dedicated allocation of the buffer's size in a type it lists, exportable as a
// handle its buffers export.
let memory =
match unsafe { allocate(device, &core.counters, core.max_allocations, &info, None) } {
Ok(memory) => memory,
Err(error) => {
destroy();
return Err(error);
}
};
let nanos = crate::host_time() - started;
(core.counters).timed(Part::ExportAllocation, needs.size, nanos, true);
let undo = |error: Error| {
// SAFETY: nothing uses the buffer or the memory.
unsafe {
device.destroy_buffer(buffer, None);
device.free_memory(memory, None);
}
core.counters.fall(Gauge::Blocks);
error
};
// SAFETY: the memory is the buffer's dedicated allocation, of its size and a type it lists.
if let Err(result) = unsafe { device.bind_buffer_memory(buffer, memory, 0) } {
return Err(undo(Error::Vulkan {
call: "vkBindBufferMemory",
result,
}));
}
let (id, allocation) = core.pool.own_numbers().map_err(undo)?;
let address = vk::BufferDeviceAddressInfo::default().buffer(buffer);
// SAFETY: the buffer has device-address usage and is bound.
let address = unsafe { device.get_buffer_device_address(&address) };
core.counters.exported(needs.size);
let memory = Memory {
id,
class: MemoryClass::Private,
len,
allocation,
memory,
base: 0,
buffer,
address,
mapping: None,
device_local: core.device_local & (1 << index) != 0,
spilled: false,
own: Some(Own::Export {
handle,
bytes: needs.size,
}),
};
Ok((memory, needs.size))
}
/// Makes an opaque handle's surface allocation in place ([`crate::surface`]): `len` bytes rounded
/// up to 64 KiB of memory type `index`, exportable as `handle`, dedicated to nothing and with
/// device addresses, counted against the device's allocations ([`allocate`]), and its numbers. It
/// holds no buffer: the surface's plane images are bound in it, and its block's buffer is null
/// and its address 0. The surface's buffer, where the consumer asks for one, is bound in it later
/// ([`surface_view`]). Counted as an export; its `vkAllocateMemory` timed in
/// [`Metrics::surface_allocations`](crate::Metrics::surface_allocations).
///
/// Err: [`Limit::BlockLen`] past the device's longest allocation; [`Limit::MemoryAllocations`];
/// [`Limit::BlockIds`]; [`Error::Vulkan`] where the allocation fails.
///
/// # Safety
///
/// Every image bound in it is made for `handle`, and none requires an allocation of its own.
pub(crate) unsafe fn export_surface(
core: &Core,
len: u64,
index: u32,
handle: crate::ExportHandle,
) -> Result<Memory, Error> {
let device = &core.device;
let len = round(len, GRANULE, core.max_block_len)?;
let mut export = vk::ExportMemoryAllocateInfo::default().handle_types(handle.vk());
// A buffer bound in it later takes a device address
// (`VUID-vkBindBufferMemory-bufferDeviceAddress-03339`).
let mut flags =
vk::MemoryAllocateFlagsInfo::default().flags(vk::MemoryAllocateFlags::DEVICE_ADDRESS);
let info = vk::MemoryAllocateInfo::default()
.allocation_size(len)
.memory_type_index(index)
.push_next(&mut export)
.push_next(&mut flags);
let started = crate::host_time();
// SAFETY: the caller's; the type is the images'.
let memory = unsafe { allocate(device, &core.counters, core.max_allocations, &info, None) }?;
let nanos = crate::host_time() - started;
(core.counters).timed(Part::SurfaceAllocation, len, nanos, true);
let (id, allocation) = match core.pool.own_numbers() {
Ok(numbers) => numbers,
Err(error) => {
// SAFETY: nothing is bound to the memory.
unsafe { device.free_memory(memory, None) };
core.counters.fall(Gauge::Blocks);
return Err(error);
}
};
core.counters.exported(len);
Ok(Memory {
id,
class: MemoryClass::Private,
len,
allocation,
memory,
base: 0,
buffer: vk::Buffer::null(),
address: 0,
mapping: None,
device_local: core.device_local & (1 << index) != 0,
spilled: false,
own: Some(Own::Export { handle, bytes: len }),
})
}
/// Makes an opaque surface's buffer ([`crate::Surface::buffer`]) on its allocation, which `under`
/// holds: a buffer of the allocation's length that names `handle`, bound at its first byte, its
/// address, and its numbers, which no hook is told of. Private and never mapped, as the
/// allocation is; it frees no memory, and holds `under` until it is given back, which then goes
/// back after it. Counted in the surface buffers gauge.
///
/// Err: [`Limit::ExportType`] where the buffer's memory requirements do not list the
/// allocation's type `index`, ask more than its length or require an allocation of its own;
/// [`Limit::BlockIds`]; [`Error::Vulkan`] where the buffer or its binding fails.
///
/// # Safety
///
/// `under` is an opaque surface's allocation ([`export_surface`]) of type `index`, exportable as
/// `handle`.
pub(crate) unsafe fn surface_view(
core: &Arc<Core>,
under: &Arc<Block>,
handle: crate::ExportHandle,
index: u32,
) -> Result<Block, Error> {
let device = &core.device;
let len = under.memory.len;
let mut external = vk::ExternalMemoryBufferCreateInfo::default().handle_types(handle.vk());
let info = vk::BufferCreateInfo::default()
.size(len)
.usage(floor::BUFFER_USAGE)
.sharing_mode(vk::SharingMode::EXCLUSIVE)
.push_next(&mut external);
// SAFETY: `len` is the allocation's, at most `maxBufferSize` (`export_surface` rounds it), and
// the device's buffers export `handle` (`crate::Device::new_exported_surface` checked it).
let buffer =
unsafe { device.create_buffer(&info, None) }.map_err(Error::vulkan("vkCreateBuffer"))?;
let destroy = || {
// SAFETY: nothing uses the buffer.
unsafe { device.destroy_buffer(buffer, None) }
};
let mut dedicated = vk::MemoryDedicatedRequirements::default();
let mut needs = vk::MemoryRequirements2::default().push_next(&mut dedicated);
let of = vk::BufferMemoryRequirementsInfo2::default().buffer(buffer);
// SAFETY: the buffer is live.
unsafe { device.get_buffer_memory_requirements2(&of, &mut needs) };
let needs = needs.memory_requirements;
// A buffer that requires an allocation of its own is bound in no other
// (`VUID-vkBindBufferMemory-buffer-01444`).
if needs.memory_type_bits & (1 << index) == 0
|| needs.size > len
|| dedicated.requires_dedicated_allocation != vk::FALSE
{
destroy();
return Err(Error::Limit(Limit::ExportType {
handle,
listed: Some(needs.memory_type_bits),
}));
}
// SAFETY: the allocation is of a type the buffer lists, at least its size, dedicated to
// nothing, made with device addresses and exportable as a handle the buffer names
// (`VUID-vkBindBufferMemory-memory-02726`); an offset of 0 meets every alignment.
if let Err(result) = unsafe { device.bind_buffer_memory(buffer, under.memory.memory, 0) } {
destroy();
return Err(Error::Vulkan {
call: "vkBindBufferMemory",
result,
});
}
let (id, allocation) = match core.pool.own_numbers() {
Ok(numbers) => numbers,
Err(error) => {
destroy();
return Err(error);
}
};
let address = vk::BufferDeviceAddressInfo::default().buffer(buffer);
// SAFETY: the buffer has device-address usage and is bound.
let address = unsafe { device.get_buffer_device_address(&address) };
core.counters.rise(Gauge::SurfaceBuffers);
let memory = Memory {
id,
class: MemoryClass::Private,
len,
allocation,
memory: under.memory.memory,
base: 0,
buffer,
address,
mapping: None,
device_local: under.memory.device_local,
spilled: false,
own: Some(Own::Surface {
import: false,
family: handle.family(),
}),
};
Ok(Block::over(memory, Arc::clone(core), Arc::clone(under)))
}
/// Frees an imported or exported block given back: its buffer, then its memory, then runs an
/// import's release, as the bytes are the consumer's alone again; of a surface's buffer on the
/// opaque handle's allocation, the buffer alone; of an import over several dma-bufs, the sparse
/// buffer alone, once its bind completed, and of each of its chunks, the memory alone, the last
/// one's release after it. The submissions made during the `vkFreeMemory` are counted.
fn free_own(core: &Core, memory: &Memory, release: Option<Release>) {
(core.spans).freed(memory.buffer, core.pool.in_flight(), None);
match memory.own {
// An import over several dma-bufs' buffer: its bind, the one use of it no command buffer
// held, completes before it goes (`VUID-vkDestroyBuffer-buffer-00922`), and its chunks,
// given back after it, free the memory.
#[cfg(unix)]
Some(Own::Sparse { bind, space }) => {
let sparse = core.imports.sparse.as_ref();
if let Some(sparse) = sparse {
sparse.wait(&core.device, bind);
}
// SAFETY: as below, and the bind completed.
unsafe { core.device.destroy_buffer(memory.buffer, None) };
if let Some(sparse) = sparse {
sparse.unreserve(space);
}
return;
}
// A chunk has no buffer.
#[cfg(unix)]
Some(Own::Chunk) => {}
// SAFETY: no GPU work that uses the block is pending (a command buffer or upload that
// uses it holds it until it retires), and no buffer of it is left.
_ => unsafe { core.device.destroy_buffer(memory.buffer, None) },
}
// A surface's buffer on the opaque handle's allocation frees no memory: the block under it,
// given back after it, does.
if matches!(memory.own, Some(Own::Surface { import: false, .. })) {
core.counters.fall(Gauge::SurfaceBuffers);
return;
}
// SAFETY: as above; nothing else is bound to the memory.
unsafe { free(&core.device, memory.memory) };
core.counters.fall(Gauge::Blocks);
match memory.own {
Some(Own::Export { bytes, .. }) => core.counters.export_freed(bytes),
Some(Own::Surface { .. }) => {
core.counters.fall(Gauge::SurfaceBuffers);
#[cfg(unix)]
core.counters.surface_import_freed(memory.len);
}
_ => core.counters.import_freed(memory.len),
}
if let Some(release) = release {
release();
}
}
/// Frees memory of its own another thread's object held ([`Loose`]): the buffer, then the memory,
/// then its gauges fall. On the block-maker thread, the free is in its form of every free
/// ([`free`]), and the submissions made during it are counted.
///
/// # Safety
///
/// As [`Pool::give_back_loose`].
unsafe fn free_loose(core: &Core, loose: Loose) {
let counters = &core.counters;
match loose {
Loose::Texture {
memory,
bytes,
spilled,
} => {
// SAFETY: the caller's.
unsafe { free(&core.device, memory) };
counters.fall(Gauge::Blocks);
counters.fall_by(Gauge::TextureOwnBytes, bytes);
counters.fall_by(Gauge::SpilledBytes, spilled);
}
Loose::SurfaceImport { memory, bytes } => {
// SAFETY: the caller's.
unsafe { free(&core.device, memory) };
counters.fall(Gauge::Blocks);
counters.surface_import_freed(bytes);
}
Loose::Staging { buffer, memory } => {
// SAFETY: the caller's; freeing the memory unmaps it.
unsafe {
core.device.destroy_buffer(buffer, None);
free(&core.device, memory);
}
counters.fall(Gauge::Blocks);
}
Loose::Preprocess {
buffer,
memory,
bytes,
} => {
(core.spans).freed(buffer, core.pool.in_flight(), None);
// SAFETY: the caller's.
unsafe {
core.device.destroy_buffer(buffer, None);
free(&core.device, memory);
}
counters.fall(Gauge::Blocks);
counters.fall_by(Gauge::PreprocessIdleBytes, bytes);
}
Loose::RingBlock { buffer, memory } => {
(core.spans).freed(buffer, core.pool.in_flight(), None);
// SAFETY: the caller's; freeing the memory unmaps it.
unsafe {
core.device.destroy_buffer(buffer, None);
free(&core.device, memory);
}
counters.fall(Gauge::Blocks);
}
}
}
/// On the block-maker thread: frees an allocation the idle rule took out, after the consumer's
/// unmap of it, and counts its idle time. The submissions made during its `vkFreeMemory` are
/// counted ([`free`]). Where the unmap panics, the consumer's mappings of the allocation may
/// stand, so it is not freed here: it goes back to the pool, stopped at once under the same lock
/// so that no take lands in it, and teardown frees it with no second unmap (it is no longer
/// mapped); then the panic goes on, and the thread ends as [`Ends`] says.
fn free_idle(core: &Core, allocation: Allocation) {
let id = allocation.id;
if let Err(panicked) = panic::catch_unwind(AssertUnwindSafe(|| core.pool.unmap(id))) {
let class = allocation.class;
let mut state = core.pool.lock();
state.stop = true;
state.allocations[class.index()].push(allocation);
core.pool.settle(&mut state, class);
drop(state);
panic::resume_unwind(panicked);
}
let since = allocation.idle_since;
let began = crate::host_time();
core.counters.freed_idle(began.saturating_sub(since));
free_allocation(core, allocation);
core.counters.count(Metric::AllocationsFreed);
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
lock(&core.pool.frees).push((since, began, crate::host_time()));
}
/// Frees an allocation and its buffer, whose blocks are all given back or gone with the device,
/// and whose unmap has run. Freeing the memory also unmaps the runtime's own mapping of it.
fn free_allocation(core: &Core, allocation: Allocation) {
let in_flight = core.pool.in_flight();
(core.spans).freed(allocation.buffer, in_flight, allocation.wait.progress());
// SAFETY: no GPU work uses the buffer or the memory: a command buffer or upload that uses a
// block holds it until it retires.
unsafe {
core.device.destroy_buffer(allocation.buffer, None);
free(&core.device, allocation.memory);
}
core.counters.fall(Gauge::Blocks);
(core.counters).fall_by(allocation.class.allocation_gauge(), allocation.len);
if allocation.spilled {
core.counters.fall_by(Gauge::SpilledBytes, allocation.len);
}
}
/// At teardown, after the block-maker thread is joined and every resource is freed: frees the
/// imported and exported blocks given back and runs the imports' releases, then every
/// allocation, each after the
/// consumer's unmap of it where it is mapped.
pub(crate) fn free_all(core: &mut Core) {
let mut allocations = Vec::new();
let mut idle = [0; 2];
// The GPU completed everything submitted: no wait holds. What was held goes first, as its
// memory and places come back here.
let held: Vec<Free> = {
let state = (core.pool.state.get_mut()).unwrap_or_else(PoisonError::into_inner);
state
.held
.drain(..)
.map(|((object, _), _)| object)
.collect()
};
for object in held {
core.counters.fall(Gauge::ReleasesWaiting);
crate::reaper::free(core, object);
}
let (owned, loose): (Vec<(Memory, Option<Release>)>, Vec<Loose>) = {
let state = (core.pool.state.get_mut()).unwrap_or_else(PoisonError::into_inner);
(
state.owned.drain(..).map(|(own, _)| own).collect(),
state.loose.drain(..).collect(),
)
};
for (memory, release) in owned {
free_own(core, &memory, release);
}
for loose in loose {
// SAFETY: as given back.
unsafe { free_loose(core, loose) };
}
{
let state = (core.pool.state.get_mut()).unwrap_or_else(PoisonError::into_inner);
for class in MemoryClass::ALL {
let index = class.index();
allocations.append(&mut state.allocations[index]);
idle[index] = state.idle_bytes[index];
state.idle_bytes[index] = 0;
}
state.unit_take_bytes = 0;
}
for class in MemoryClass::ALL {
let shared = class == MemoryClass::Shared;
core.counters
.fall_by(class.idle_gauge(), idle[class.index()]);
// The blocks still taken (the texture set's empty block kept for its next place among
// them) go with their allocations.
core.counters.set(class.placed_gauge(), 0);
(core.counters).idle_since(shared, 0);
(core.counters).reserve_bytes(shared, 0);
(core.counters).long_reused_bytes(shared, 0);
}
// The texture block kept for the next place and the blocks still taken go with their
// allocations.
for gauge in [
Gauge::TextureBlockHeldBytes,
Gauge::PrivateTakeHeldBytes,
Gauge::PrivateTakeBytes,
Gauge::PrivateTakeSparseBytes,
Gauge::PrivateTakeSparseAllocations,
Gauge::PrivateUnits,
Gauge::PrivateUnitUsedBytes,
] {
core.counters.set(gauge, 0);
}
for allocation in allocations {
core.pool.unmap(allocation.id);
free_allocation(core, allocation);
}
}