Files
metal2vulkan/crates/metal2vulkan-runtime/src/exit_guard.rs
T
Anees Iqbal 45182b7aeb Devices: prime only a device that registers the exit hook
What. A device's making asks the exit guard, before priming it, whether it will register the
hook (`exit_guard::before_priming`, now given the device's identity, answers
`Devices::admits`: the process's first device, the first after a load of a shared object, or
one of an identity new since). Only such a device is primed. A device made again on what stays
loaded, after a device of its identity registered, registers nothing and is no longer primed:
its calls are not the first, an earlier device's priming made each. The device's functions and
identity are made before the priming for the question; nothing else moves. A device that
registers unprimed after all (a load or another device between the question and its
registration) forgets the calls primed before, so each section's first call registers the hook
again. `ExitRegistrations` (hidden, for the lanes) counts the primings.

Why. Bug 487/676: on NVIDIA a device's making spent 120-160 ms of its own beside
vkCreateDevice, nearly all in priming's vkCreateIndirectCommandsLayoutEXT (112-144 ms on the
RTX 3090, NVIDIA 620.70.06, scratch timers on steelbrain/triage2-scratch 2edaaa5afa; m3-max-17899).
On that GPU every device after the first was made on what stayed loaded (the loads count stayed
at 67 over 8 makings and at_devices at 1, e6e5c1a34b, m3-max-17904), so each re-made device
paid priming for nothing. Priming exists only so that a device's registration of the hook comes
after the statics its calls make.

Tests: tests::exit_guard::whether_a_device_registers_is_answered_before_its_priming (the
answer agrees with the registration and notes nothing),
a_device_registered_unprimed_forgets_the_primed_calls (fork: 42), the two priming fork tests on
the new signatures, and tests::primed_devices (device cfgs): the first device primes, each made
again with no load neither primes nor registers.
2026-11-11 17:09:19 -08:00

2097 lines
106 KiB
Rust

//! The exit guard: no Vulkan call of the runtime overlaps the destructors a process runs as it
//! exits.
//!
//! A process exits (`exit`, or the end of `main`) on one thread, which runs the exit handlers and
//! the loaded libraries' destructors in the reverse order of their registration; every other
//! thread keeps running meanwhile. A driver's or a layer's destructors free what a later Vulkan
//! call needs: RADV ended a process with `SIGSEGV` when a completing thread destroyed a device
//! beside the validation layer's destructors.
//!
//! - **Sections.** Each Vulkan call the runtime makes on its own threads (the scheduler's
//! submissions, the queues' waiters' polls, the lowering of render encoders' calls on the
//! device's lowering thread), each release of a
//! Vulkan object (a drop, on whatever thread it runs), and each read of an event's counter runs
//! inside a section ([`enter`]), which counts itself. A section runs none of the program's code
//! (handlers, deallocators, notifications) except a device's message sink, which Vulkan calls
//! inside it, and publishes nothing it did (a status, a handler's turn, an event's value, a
//! batch to poll for) before it ended. A section started runs to its end, the sections inside
//! it included, even when the guard closes meanwhile.
//! - **Coverage, by construction and by test.** Four doors, which new code cannot go around:
//! - every release (`vkDestroy*`, `vkFree*`) goes through `release.rs`, which runs it inside a
//! section and marks its entry point; a new kind of object implements its `Released` there.
//! A source test fails when another file calls such an entry point;
//! - every thread the runtime starts is started by [`spawn`], and every call of the program's
//! code (handlers, notifications, deallocators, the message sink) goes through [`program`];
//! source tests fail when a thread is started or a panic caught elsewhere. A release of the
//! program's (a deallocator, a keep-alive, a range token, a dma-buf source) goes through
//! [`program_release`], which in builds with debug assertions ends the process on a thread
//! command buffers' completion depends on;
//! - the device's functions are reached only through checked accessors (`DeviceShared::vk`,
//! private field), which in builds with debug assertions end the process
//! when one of the runtime's threads reaches them outside a section and outside the
//! program's code ([`check`]). Every lane runs that way, so a Vulkan call a runtime thread
//! makes outside a section fails the lane that reaches it;
//! - the entry points sections call are one table ([`Call`]): in those builds a section reaches
//! the table's alone, every other one ending the process (`device/functions.rs`), and each
//! device's priming makes every one it can and checks that it did (`device/prime.rs`). A
//! new entry point in a section goes into the table and the priming, or fails the lane.
//!
//! The program's own calls on its own threads (making objects, encoding) take no section:
//! their race with an exit is the program's.
//! - **A host step makes what it needs**: an image, a render pass, a framebuffer or a neutral no
//! earlier settle made, as the program's thread or the device's lowering thread
//! (`render/lowering_worker.rs`) would, through their one door (`creation.rs`; `neutral.rs` for
//! the neutrals). Their entry points are in the table and among the calls a host step makes
//! ([`HOST_STEP`]); a source test fails when another file makes such an object.
//! - **The hook.** A device made or adopted registers an exit handler ([`register_device`]), once
//! its driver and layers are loaded and primed (`device/prime.rs`: the first call of each entry
//! point of the table, made on the creating thread), so it runs before their destructors, those
//! of statics made at those first calls included: the process's first device, one whose driver
//! and functions are new since the process last loaded a shared object, and the first after such
//! a load ([`loaded`]). A device made again on what stays loaded registers nothing. Each
//! registration carries what it follows ([`After`], the C library's argument to the handler), and
//! its run ends what the destructors registered just before it may break, before they run:
//! - **after a making** (below, "Registered again"), the destructors are the driver's compiler's,
//! which a making reaches: the run ends the makings ([`Guard::end_makings`]), waiting for those
//! in progress in sections at most `MAKING_BOUND`; none starts after (a pool's piece, a host
//! step's, or the program's own on its thread answers that the process is exiting,
//! [`driver_making`]). The guard stays open: the exit handlers the program registered before
//! that registration (once its device was made, before its later makings) run next with the
//! runtime's threads, and work they commit is submitted and completes, as Metal's exit runs
//! it (the exits lane's `handler_work_exit`). Where a device whose queues run in the process
//! lives (a CPU device, below), the run is a device's: its queues compile as they run their
//! work, and the run closes the guard, so such a handler finds the process exiting;
//! - **after a device, an instance or a first call** (below), the destructors are a driver's or a
//! layer's, which any Vulkan call may reach: the run closes the guard.
//!
//! Closing the guard, the hook lets no section start and waits for the sections in progress to
//! end, never for the exiting thread's own (an exit from a message sink). A later run (an
//! earlier registration)
//! finds nothing to wait for and makes no Vulkan call: it follows the destructors registered
//! after that registration. The first run waits for the sections in progress at most `BOUND`,
//! and while a driver's making of a `VkPipeline` is in progress in a section (a pool's piece's
//! `vkCreate*Pipelines`, [`driver_making`]) at most `MAKING_BOUND`, saying so on standard error
//! as that wait starts: a making outlasts `BOUND`, and the program need not wait for the command
//! buffer whose making it is before it exits. Seen: with two seconds, two lane processes on ANV
//! ended with `SIGSEGV` and `SIGABRT` inside the validation layer 3 and 20 s after their test
//! passed, where a bicubic kernel's making took 6.6 s under the lane's load; a making in
//! progress then is inferred from the timing. Once that wait passed half its bound, it says on
//! standard error, once, where the sections still in progress began: each thread's outermost
//! section keeps its caller's place while it lasts (`Location::caller`, through the
//! `#[track_caller]` doors, so a release names its caller), and an exit that waits that long
//! names the calls it waits for. Then, within what is left of the bound, it waits
//! for a CPU device's work (below). Once they ended it returns, and the exit handlers registered
//! before it (the program's: a VMM's re-attach of host USB devices to their drivers, a
//! terminal's restore) run, as Metal's exit runs them. Past the bound, what the hook does depends
//! on whether it learns the exit's status:
//! - **Linux, with the GNU C library**, where it does (`on_exit(3)`, [`exit_handler`]): it ends
//! the process at once with the exit's status (`end_at_once`): one line on standard error
//! (`AT_ONCE_NOTICE`), the C library's output streams flushed, `_exit`, so that no destructor
//! runs beside the driver's work. The exit handlers registered before the hook's last
//! registration then do not run, where Metal's exit runs them (docs/API.md, accepted
//! differences). Seen past the bound: under the GPU-assisted validation layer 1.4.357 two
//! completion tests' processes hung at exit, the layer deadlocked inside a submission, where
//! the hook returned after its wait.
//! - **Elsewhere** (macOS, where `atexit` gives no status): it returns, and the exit goes on.
//!
//! What runs once the hook returned makes no Vulkan call of the runtime's: a section ended
//! published its calls' results, so none of the runtime's calls is in flight past its section's
//! end, and no section starts once the guard closed (the scheduler, the queues' waiters and the
//! pool's threads end at their next section; a release skips its calls, below). What may still
//! call the driver is the program's own threads (making objects, encoding: the program's race,
//! above) and the driver's own threads (Mesa's thread queues, which its own exit handler, made
//! before the hook's first registration, joins after the hook returns).
//! - **Registered again.** Drivers and layers also register destructors later, when a static is
//! made at its first use. Measured: with the hook registered only at the first device, polls
//! and device waits crashed inside the validation layer's synchronization code at exit while
//! the hook still waited for them. So a section that ends after the first call of a Vulkan
//! entry point the runtime calls in sections ([`Call`], a fixed set) registers the hook again,
//! after anything that call registered: at most one per entry point of the table at each load.
//! The calls a device's priming made before its registration count as made ([`primed`]), so a
//! section registers the hook for an entry point the priming did not call.
//! Such a registration after an exit handler the program registered runs the hook before that
//! handler, closing the guard, so the handler finds its work left pending, where Metal runs it
//! (the exits lane's `cpu_work_exit`); so no section registers the hook for its count alone,
//! and a static a later call makes with new parameters is in the hole below. Each
//! `vkCreate*Pipelines` the runtime
//! makes on a device (`device/compiles.rs`, their one door: a pool's piece, a host step's, the
//! warm pass's, a self-check's, or the program's own on its thread) registers it again as the
//! driver's call returns, while the guard is open and the makings go on ([`pipelines_made`]): a
//! making runs the driver's compiler, which makes its statics at the first use of each of its
//! paths (a driver may load its compiler as a shared object only at its first compile), at any
//! making of the process's life, and a making takes milliseconds to seconds, beside which one
//! more entry in the C library's list of exit handlers costs nothing. So every destructor a
//! making that returned registered runs after the hook, which waits for the makings in progress
//! before it. While a making is in progress in a section (a pool's piece's, a host step's), a
//! thread of the runtime's ([`register_while_making`], started at the process's first such
//! making and parked while none is in progress) registers the hook again every
//! [`MAKING_INTERVAL`] (2 ms), at most [`MAKING_REGISTRATIONS`] (16384, 512 KiB of the C
//! library's list) times in the process, so a destructor the compiler registered in a making
//! still in progress runs after the hook too once the interval passed: the hook runs first and
//! waits for the making. Measured on lavapipe (Mesa 26.2.4, LLVM 23): with a pool's making in
//! progress, three destructors LLVM registered on a thread of the driver's 1 to 2 ms before the
//! exit ran before the hook, that thread still compiling (it registered three more once the exit
//! began), in 1 of 3 exits. Seen before: on an RTX 5080 (NVIDIA's driver), a consumer's test
//! process ended
//! with `SIGSEGV` at `0x18` inside `libnvidia-glvkspirv.so`, the driver's SPIR-V compiler, on a
//! thread the test's had started, during its exit after its test passed: one test of a whole
//! run under load, which passed alone. The same fault ends an exit with no making in progress
//! too, which no hook orders: on an RTX 3090 (620.70.06), the exit handler of `libEGL_nvidia.so`
//! tore the compiler down while a worker thread of the driver's (started from the thread that
//! made the device, whose name it carries) still ran in it, in 6 of 80 exits right after the
//! device's in-place check ended, and none with the device dropped first (docs/FAST-PATHS.md,
//! known gaps). A load is where the first call counts
//! from: the Vulkan loader unloads the layers with the last instance and loads them again with
//! the next, which make their statics again at each entry point's first call after the load;
//! so the device that registers at the load forgets the entry points called before as its
//! priming begins ([`before_priming`], [`forget_calls`]), and the priming's calls, before its
//! registration, are the first since. Measured: before, a teardown lane whose six devices each reloaded the
//! validation layer ended with `SIGSEGV` at exit, a pool thread's `vkDestroyRenderPass` (its
//! making's pass) calling through a static of the layer's destroyed first, while the hook waited
//! for that section (in the core, that pool thread, and the main thread asleep as the hook's wait
//! polls). A layer also registers an exit handler at every `vkCreateInstance`, loaded again or
//! not: the validation layer 1.4.357's (`ApplicationAtExit`) forgets every device the layer
//! knows, and its next call for any of them ends the process ("The VkDevice dispatch handle was
//! not found"). So every instance the runtime creates goes through one door (`device/create.rs`,
//! which a source test holds), and the hook registers again after it ([`instance_made`]).
//! Measured: before, on the M3 Max, a pool's making for a lane's third device, made on an
//! instance of its own with the layer loaded since the first, called `vkGetPipelineCacheData` or
//! `vkDestroyPipelineCache` at exit after the layer's handler ran and before the hook did, and
//! the layer ended the process (`SIGABRT`). The instance of a device the runtime adopts was the
//! program's, so the adoption registers the hook again too, and the program reports an instance
//! it makes itself ([`program_instance_made`]), which does the same. Measured: before, on the
//! M3 Max (the validation layer from Homebrew, MoltenVK 1.4.2), a child process whose own
//! instance was made after its device, while a loop without bound ran on
//! (`command_buffer/rerun.rs`), ended with `SIGABRT` inside the layer in 2 and 3 exits of 20
//! (its lookup of the device, at the loop's next `vkResetFences`, `vkWaitForFences` or
//! `vkDestroyCommandPool`), and 0 of 20 without that instance; on lavapipe, a lane run in one
//! process ended with `SIGSEGV` inside the layer's `vkWaitForFences` on two such loops' threads,
//! while the hook waited for their sections. The hook then runs before every destructor
//! registered before its last registration. Since a section publishes nothing before it ended,
//! an exit the program makes once it learned of a section's work (from a handler, a wait, a
//! status) comes after that section's registration. Measured: while the scheduler posted a
//! command buffer's scheduling inside its section, an exit from a scheduled handler crashed the
//! completion's poll on MoltenVK in 10 runs of 10. The hole left: a static first made by a
//! making in progress in the last interval before the exit, or once the registrar reached its
//! bound, or by a making on the program's own thread (in no section, which the hook does not
//! wait for), or by another call (not a making) after the hook's last registration, when the
//! process exits before the next registration. The program's own calls but its makings
//! register nothing, so one of those may make it
//! too: on the M3 Max (the validation layer 1.4.357.0 from Homebrew), the layer's descriptor set
//! layouts' empty lists
//! (`DescriptorSetLayoutDef::GetMutableTypes` and `GetImmutableSamplerCreateInfosFromIndex`),
//! first made on the program's thread 3 to 11 ms before an exit in each of 20 runs; no section
//! was seen to read them at exit. So may an instance the program creates itself and does not
//! report ([`program_instance_made`]), and is not that of a device the runtime adopts: a layer's
//! exit handler registered there runs before the hook's, until the next registration. Not the
//! guard's, at exit or before: under a layer on MoltenVK
//! 1.4.1, a message that names the device leaves `ICD_LOADER_MAGIC` in its loader word, and the
//! layer, which finds its state by that word, ends the process at its next call for the device
//! (the validation layer: "The VkDevice dispatch handle was not found";
//! `Device::with_loader_word`).
//! - **Once closed.** The scheduler and the queues' waiters end at their next section, so what
//! is pending stays pending, as Metal's does. A release skips its Vulkan calls (the process's
//! end frees what they would), and a no-copy buffer released then keeps its deallocator unrun,
//! with its pages imported; a release inside a section in progress (a whole drop's, a pool
//! piece's) runs, within the hook's wait. A device's teardown in progress, one section, leaves
//! the device to the process's end at its next step once the guard closed: before its wait for
//! idle, before its pipeline caches and memory go, and before `vkDestroyDevice`, which on the
//! RTX 5080 Laptop GPU took 0.3 to 2.6 s beside other processes' work on the GPU (the exits
//! lane's `handler_learns_exit`, whose teardown runs on a handler thread as the program exits).
//! The handler threads, listeners and the shader cache's thread go on. A driver's making then
//! calls the driver for its pipelines alone (`shader_cache/driver.rs`): one in progress (a pool
//! piece's or a host step's section runs on past the close) neither reads its cache for a
//! record nor releases it, and one begun after makes no cache. Once the makings ended instead
//! (a run after a making), none begins, on any thread, and the rest goes on.
//! - **A CPU device's work.** A device whose queues run in this process (`DeviceKind::Cpu`:
//! lavapipe, SwiftShader) runs what was submitted to it on the process's own threads, so the
//! destructors would run under its execution. Once the sections ended, the hook's first run waits
//! for the work submitted to each such device's queues ([`InProcess`], [`runs_in_process`]),
//! within what is left of its bound (above), and takes the devices off the list;
//! what is not submitted stays pending. Measured: while every run waited, lavapipe processes
//! exiting with a live device under the validation layer 1.4.357 ended with `SIGABRT` (`SIGSEGV`
//! with GPU-assisted validation) inside the layer's `vkWaitSemaphores`, in the run of the hook's
//! first registration, after a destructor the layer registered on the program's thread between
//! that registration and the next. A GPU's work runs on the GPU, and the hook does not wait for
//! it, as Metal's exit does not. Measured on lavapipe (Mesa 26.2.4, x86_64): with work in flight
//! at exit, its queue thread compiled a dispatch's shader (LLVM's `MCJIT::finalizeObject`) while
//! the destructors ran, and the process ended in `free()`'s heap checks, in 5 of 300 exits while
//! a host step waited and 1 of 300 with a plain dispatch in flight.
//! - **No exit waits for a thread the guard ended.** Those two threads end between sections,
//! holding no lock. Two things wait for them:
//! - joins, made only by a device's teardown, which is one section: once the guard closed none
//! begins, and one already begun returns, since the threads end;
//! - waits for their work: a command buffer's scheduling and completion, and an event's value
//! the GPU raises. Each such wait rechecks the guard at least every [`WAKE`] and then answers
//! at once: `wait_until_completed` and `wait_until_scheduled` with `WaitWouldDeadlock` of
//! `DeadlockCause::Exiting` (which [`process_exiting`] answers is so),
//! `wait_until_completed_for` with `TimedOut`, `wait_until_signaled_value` with the value
//! known. A handler's wait for another queue is one of them.
//!
//! The hook itself waits only for sections, and a section waits for nothing the exiting thread
//! holds but through a message sink, which the hook's bound covers. So nothing run before the
//! exit returns (later exit handlers and destructors, or a thread one of them joins) waits for a
//! thread the guard ended.
//! - **Forks.** The hook acts only in the process it was registered in: a child forked without
//! exec, which has none of its parent's threads, runs it as a no-op, and registers its own with
//! its first device.
use super::loaded;
use std::cell::Cell;
use std::marker::PhantomData;
use std::panic::Location;
use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicU32, AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, PoisonError, Weak};
use std::time::{Duration, Instant};
/// The longest the hook waits for the sections in progress, and then for a CPU device's
/// submitted work ([`InProcess`]). A section is one submission, one poll (at most 100 ms), one
/// counter read, one save's read, or one object's release (a device's: its threads' stops, then,
/// unless the guard closed meanwhile, `vkDeviceWaitIdle` and its destruction); it outlasts this
/// only when a driver or a message sink blocks.
pub(crate) const BOUND: Duration = Duration::from_secs(2);
/// The longest the hook waits while a driver's making of a `VkPipeline` is in progress in a
/// section ([`driver_making`]), for the sections in progress: a making takes 20 to 50 s on NVIDIA
/// (`pipeline/piece_holds.rs`), and Metal's process waited for all of it at the state's creation,
/// before its exit.
pub(crate) const MAKING_BOUND: Duration = Duration::from_secs(60);
/// The longest a blocked wait goes without looking at the guard.
pub(crate) const WAKE: Duration = Duration::from_millis(50);
/// How often the hook looks at the sections in progress.
const POLL: Duration = Duration::from_millis(1);
/// The outermost sections in progress whose places a guard keeps at once ([`Guard::places`]): one
/// that begins while every slot is taken goes unnamed.
const PLACES: usize = 64;
/// The sections in progress, and whether the guard closed. The process has one ([`GUARD`]).
pub(crate) struct Guard {
active: AtomicUsize,
closed: AtomicBool,
/// The sections in progress that make a `VkPipeline` ([`Guard::enter_making`]).
making: AtomicUsize,
/// Whether the makings ended ([`Guard::end_makings`]): a run of the hook registered after a
/// making ran, and no making starts since.
makings_ended: AtomicBool,
/// Where each outermost section in progress began (the caller of [`enter`] or
/// [`Guard::enter`], through every `#[track_caller]` function between), a slot each while it
/// lasts, null where free: what [`Guard::close`] names once its wait passed half its bound.
places: [AtomicPtr<Location<'static>>; PLACES],
}
/// The process's guard.
static GUARD: Guard = Guard::new();
/// The process's devices whose queues run in it.
static IN_PROCESS: InProcessDevices = InProcessDevices::new();
/// A device whose queues run in this process (a CPU device): the work submitted to them runs on
/// the process's own threads, which the exit's destructors must not run under.
pub(crate) trait InProcess: Send + Sync {
/// Wait until the work submitted to its queues completed, at most until `deadline` (`None`:
/// no deadline): whether
/// it did. Called by the hook once the sections ended, so nothing is submitted meanwhile.
fn drain(&self, deadline: Option<Instant>) -> bool;
}
/// The devices whose queues run in this process, each with the process it was made in (a forked
/// child has none of its parent's threads, its devices' included).
pub(crate) struct InProcessDevices {
devices: Mutex<Vec<(u32, Weak<dyn InProcess>)>>,
}
impl InProcessDevices {
/// None.
pub(crate) const fn new() -> InProcessDevices {
InProcessDevices {
devices: Mutex::new(Vec::new()),
}
}
/// Add `device`, made in this process; those gone are dropped from the list.
pub(crate) fn add(&self, device: Weak<dyn InProcess>) {
let mut devices = self.devices.lock().unwrap_or_else(PoisonError::into_inner);
devices.retain(|(_, device)| device.strong_count() > 0);
devices.push((std::process::id(), device));
}
/// Whether a device made in process `pid` is on the list and lives.
pub(crate) fn any(&self, pid: u32) -> bool {
let devices = self.devices.lock().unwrap_or_else(PoisonError::into_inner);
(devices.iter()).any(|(made, device)| *made == pid && device.strong_count() > 0)
}
/// Take the devices made in process `pid` off the list: the live ones. A second call finds
/// none.
pub(crate) fn take(&self, pid: u32) -> Vec<Arc<dyn InProcess>> {
self.devices
.lock()
.unwrap_or_else(PoisonError::into_inner)
.extract_if(.., |(made, _)| *made == pid)
.filter_map(|(_, device)| device.upgrade())
.collect()
}
}
/// Wait for the submitted work of each of `devices`, at most until `deadline`: whether every one
/// completed.
fn drain(devices: &[Arc<dyn InProcess>], deadline: Option<Instant>) -> bool {
// Every device is waited for, whatever an earlier one answered.
let mut drained = true;
for device in devices {
drained &= device.drain(deadline);
}
drained
}
/// Wait at exit for the work submitted to `device`, whose queues run in this process.
pub(crate) fn runs_in_process(device: Weak<dyn InProcess>) {
IN_PROCESS.add(device);
}
/// The process the hook is registered in; 0 before the first device.
static REGISTERED: AtomicU32 = AtomicU32::new(0);
/// The hook's registrations after makings ([`driver_making`]).
static AT_MAKINGS: AtomicU64 = AtomicU64::new(0);
/// How often the registrar registers the hook again while a driver's making is in progress in a
/// section ([`register_while_making`]): a destructor the making's compiler registers runs after the
/// hook once this much time passed, so the exit's wait for the making comes first.
pub(crate) const MAKING_INTERVAL: Duration = Duration::from_millis(2);
/// The most registrations the registrar makes in a process: 512 KiB of the C library's list of
/// exit handlers (32 bytes each), and 33 s of makings at [`MAKING_INTERVAL`]. A process's first
/// makings are those whose compilers make their statics.
pub(crate) const MAKING_REGISTRATIONS: u64 = 16_384;
/// The hook's registrations while makings were in progress ([`register_while_making`]).
static DURING_MAKINGS: AtomicU64 = AtomicU64::new(0);
/// The process whose registrar ([`register_while_making`]) was started; 0 before.
static REGISTRAR_PID: AtomicU32 = AtomicU32::new(0);
/// The registrar's thread, to wake as a making begins: set by the thread itself as it starts, in
/// the process [`REGISTRAR_PID`] names; null before (and in a forked child, until its own starts).
static REGISTRAR: AtomicPtr<std::thread::Thread> = AtomicPtr::new(std::ptr::null_mut());
/// The entry points ([`Call`]) called in sections that ended since the hook was last registered
/// at a device after a load of a shared object ([`register_device`], they were the process's), one
/// bit each.
static CALLED: CalledWords = CalledWords([const { AtomicU64::new(0) }; CALL_WORDS]);
/// A set of [`Calls`] as atomic words, one bit per entry point of the table ([`Call::bit`]).
struct CalledWords([AtomicU64; CALL_WORDS]);
impl CalledWords {
/// Set `calls`; the bits set before.
fn fetch_or(&self, calls: Calls) -> Calls {
Calls(std::array::from_fn(|at| {
self.0[at].fetch_or(calls.0[at], Ordering::Relaxed)
}))
}
/// Clear every bit: a load of a shared object since (a driver or layer loaded again makes
/// its statics again at each entry point's first call).
fn clear(&self) {
for word in &self.0 {
word.store(0, Ordering::Relaxed);
}
}
/// The bits set.
#[cfg(test)]
fn load(&self) -> Calls {
Calls(std::array::from_fn(|at| self.0[at].load(Ordering::Relaxed)))
}
}
/// The words of 64 bits a set of the table's entry points takes ([`Calls`]): as many as the
/// table needs, so it grows with the table.
const CALL_WORDS: usize = Call::ALL.len().div_ceil(64);
/// A set of the table's entry points, one bit each ([`Call::bit`]).
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) struct Calls([u64; CALL_WORDS]);
impl Calls {
/// No entry point.
pub(crate) const NONE: Calls = Calls([0; CALL_WORDS]);
/// It and `call`.
pub(crate) fn with(self, call: Call) -> Calls {
let (at, bit) = call.bit();
let mut words = self.0;
words[at] |= bit;
Calls(words)
}
/// Whether it holds `call`.
pub(crate) fn holds(self, call: Call) -> bool {
let (at, bit) = call.bit();
self.0[at] & bit != 0
}
/// It without `call`.
#[cfg(test)]
pub(crate) fn without(self, call: Call) -> Calls {
let (at, bit) = call.bit();
let mut words = self.0;
words[at] &= !bit;
Calls(words)
}
/// Whether it holds an entry point `other` does not.
fn beyond(self, other: Calls) -> bool {
self.0
.iter()
.zip(other.0)
.any(|(&own, other)| own & !other != 0)
}
}
/// The table of the Vulkan entry points the runtime calls inside its sections, each with its
/// name: [`Call`], [`Call::ALL`] and [`Call::name`] come from it alone. Three things read it:
/// a section that ends after the first call of one registers the hook again ([`called`]);
/// inside a section the device's functions are the table's alone (`device/functions.rs`); and
/// each device's priming makes the first call of each before the hook is registered for it
/// (`device/prime.rs`), which checks that it made them all.
macro_rules! calls {
($($call:ident => $name:literal,)*) => {
/// A Vulkan entry point the runtime calls inside its sections (the table above).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Call {
$($call,)*
}
impl Call {
/// Every entry point, in order.
pub(crate) const ALL: &'static [Call] = &[$(Call::$call,)*];
/// Its Vulkan name.
pub(crate) fn name(self) -> &'static std::ffi::CStr {
match self {
$(Call::$call => $name,)*
}
}
}
};
}
calls! {
QueueSubmit => c"vkQueueSubmit",
// A batch's sparse binding (`sparse/bind.rs`), on a device that binds sparse memory alone
// ([`SPARSE_CALLS`](crate::device::prime::SPARSE_CALLS)).
QueueBindSparse => c"vkQueueBindSparse",
WaitSemaphores => c"vkWaitSemaphores",
GetSemaphoreCounterValue => c"vkGetSemaphoreCounterValue",
// A waker's nudge of the scheduler's thread where it waits for the GPU (`scheduler/mod.rs`).
SignalSemaphore => c"vkSignalSemaphore",
GetPipelineCacheData => c"vkGetPipelineCacheData",
// A making's own pipeline cache (`shader_cache/driver.rs`), made in a host step's or a pool
// piece's making as in the program's.
CreatePipelineCache => c"vkCreatePipelineCache",
DeviceWaitIdle => c"vkDeviceWaitIdle",
CreateCommandPool => c"vkCreateCommandPool",
AllocateCommandBuffers => c"vkAllocateCommandBuffers",
// A spare command pool's command buffers past its recent uses, or an idle spare's, freed as a
// command buffer gives a pool back, and the pool trimmed (`spares.rs`).
FreeCommandBuffers => c"vkFreeCommandBuffers",
TrimCommandPool => c"vkTrimCommandPool",
BeginCommandBuffer => c"vkBeginCommandBuffer",
CmdPipelineBarrier => c"vkCmdPipelineBarrier",
CmdResetQueryPool => c"vkCmdResetQueryPool",
// A timestamp slot's reset as a command buffer takes it (`gpu_time.rs`), on a device that
// enables `hostQueryReset`
// ([`HOST_QUERY_RESET_CALLS`](crate::device::prime::HOST_QUERY_RESET_CALLS)).
ResetQueryPool => c"vkResetQueryPool",
CmdWriteTimestamp => c"vkCmdWriteTimestamp",
GetQueryPoolResults => c"vkGetQueryPoolResults",
// A completion turn's calibration reading (`gpu_time.rs`), on a device that calibrates its
// timestamps ([`CALIBRATION_CALLS`](crate::device::prime::CALIBRATION_CALLS)), through the
// entry point the device gave (`vkGetCalibratedTimestampsEXT` where it enables that form).
GetCalibratedTimestamps => c"vkGetCalibratedTimestampsKHR",
EndCommandBuffer => c"vkEndCommandBuffer",
CreateFence => c"vkCreateFence",
// The timeline of the device's internal submissions (`device/submit.rs`), made at the first,
// which an indirect sparse update's read in a host step can be.
CreateSemaphore => c"vkCreateSemaphore",
WaitForFences => c"vkWaitForFences",
// A completion turn's drain of a device's queues after a failed wait
// (`Device::drain_queues`).
QueueWaitIdle => c"vkQueueWaitIdle",
ResetFences => c"vkResetFences",
DestroyCommandPool => c"vkDestroyCommandPool",
DestroyFence => c"vkDestroyFence",
DestroySemaphore => c"vkDestroySemaphore",
DestroySampler => c"vkDestroySampler",
// A YCbCr sample kernel's conversion (`ycbcr_device.rs`), released with the kernel.
DestroySamplerYcbcrConversion => c"vkDestroySamplerYcbcrConversion",
DestroyBuffer => c"vkDestroyBuffer",
DestroyBufferView => c"vkDestroyBufferView",
DestroyImage => c"vkDestroyImage",
DestroyImageView => c"vkDestroyImageView",
FreeMemory => c"vkFreeMemory",
DestroyPipeline => c"vkDestroyPipeline",
DestroyPipelineLayout => c"vkDestroyPipelineLayout",
DestroyDescriptorSetLayout => c"vkDestroyDescriptorSetLayout",
DestroyDescriptorPool => c"vkDestroyDescriptorPool",
DestroyShaderModule => c"vkDestroyShaderModule",
DestroyRenderPass => c"vkDestroyRenderPass",
DestroyFramebuffer => c"vkDestroyFramebuffer",
DestroyPipelineCache => c"vkDestroyPipelineCache",
DestroyQueryPool => c"vkDestroyQueryPool",
// A `VK_EXT_device_generated_commands` object's (`generated_commands.rs`).
DestroyIndirectExecutionSet => c"vkDestroyIndirectExecutionSetEXT",
DestroyIndirectCommandsLayout => c"vkDestroyIndirectCommandsLayoutEXT",
DestroyDevice => c"vkDestroyDevice",
DestroyDebugUtilsMessenger => c"vkDestroyDebugUtilsMessengerEXT",
DestroyInstance => c"vkDestroyInstance",
// A host step's ([`HOST_STEP`]), and a translation's on a device without device addresses.
CmdBindPipeline => c"vkCmdBindPipeline",
CmdBindDescriptorSets => c"vkCmdBindDescriptorSets",
// A host step's set 0 pushed (`pipeline::objects::pushes_set_zero`), on a device that has the
// push command ([`PUSH_DESCRIPTOR_CALLS`](crate::device::prime::PUSH_DESCRIPTOR_CALLS)).
CmdPushDescriptorSet => c"vkCmdPushDescriptorSetKHR",
CmdPushConstants => c"vkCmdPushConstants",
CmdDispatch => c"vkCmdDispatch",
CmdCopyBuffer => c"vkCmdCopyBuffer",
CmdCopyBufferToImage => c"vkCmdCopyBufferToImage",
CmdCopyImageToBuffer => c"vkCmdCopyImageToBuffer",
// A sparse batch's copy of a move's texels (`sparse/emulated.rs`).
CmdCopyImage => c"vkCmdCopyImage",
CreateDescriptorPool => c"vkCreateDescriptorPool",
// The reset of a predicated loop's run-again arena once the run completed
// (`command_buffer/rerun.rs`, `DescriptorArena::reset`).
ResetDescriptorPool => c"vkResetDescriptorPool",
AllocateDescriptorSets => c"vkAllocateDescriptorSets",
UpdateDescriptorSets => c"vkUpdateDescriptorSets",
CreateBuffer => c"vkCreateBuffer",
GetBufferMemoryRequirements => c"vkGetBufferMemoryRequirements",
AllocateMemory => c"vkAllocateMemory",
BindBufferMemory => c"vkBindBufferMemory",
MapMemory => c"vkMapMemory",
GetBufferDeviceAddress => c"vkGetBufferDeviceAddress",
CreateImageView => c"vkCreateImageView",
CreateBufferView => c"vkCreateBufferView",
CreateSampler => c"vkCreateSampler",
CreateShaderModule => c"vkCreateShaderModule",
CreateDescriptorSetLayout => c"vkCreateDescriptorSetLayout",
// Asked before a heap's set layout past `maxPerSetDescriptors` is made
// (`pipeline::objects::layout_support`).
GetDescriptorSetLayoutSupport => c"vkGetDescriptorSetLayoutSupport",
CreatePipelineLayout => c"vkCreatePipelineLayout",
CreateComputePipelines => c"vkCreateComputePipelines",
// A render host step's ([`HOST_STEP`]): an executed render command's draw into a secondary
// command buffer, and the pass that executes the secondaries.
CmdBeginRenderPass => c"vkCmdBeginRenderPass",
CmdEndRenderPass => c"vkCmdEndRenderPass",
CmdExecuteCommands => c"vkCmdExecuteCommands",
CmdBindVertexBuffers => c"vkCmdBindVertexBuffers",
CmdDraw => c"vkCmdDraw",
CmdSetViewport => c"vkCmdSetViewport",
CmdSetScissor => c"vkCmdSetScissor",
CmdSetDepthBias => c"vkCmdSetDepthBias",
CmdSetBlendConstants => c"vkCmdSetBlendConstants",
CmdSetStencilCompareMask => c"vkCmdSetStencilCompareMask",
CmdSetStencilWriteMask => c"vkCmdSetStencilWriteMask",
CmdSetStencilReference => c"vkCmdSetStencilReference",
CreateGraphicsPipelines => c"vkCreateGraphicsPipelines",
// A graphics pipeline's debug name, where the device's instance reports messages
// ([`NAMING_CALLS`](crate::device::prime::NAMING_CALLS), `pipeline/graphics.rs`).
SetDebugUtilsObjectName => c"vkSetDebugUtilsObjectNameEXT",
// A render host step's execution under a visibility result mode: the query lent to it, begun
// in its secondary before its first command and ended after its last.
CmdBeginQuery => c"vkCmdBeginQuery",
CmdEndQuery => c"vkCmdEndQuery",
// A render host step's emulated mesh draw whose launch a scan counts: its mesh dispatch and
// its draw, each from the scan's arguments (`render/mesh.rs`).
CmdDispatchIndirect => c"vkCmdDispatchIndirect",
CmdDrawIndirect => c"vkCmdDrawIndirect",
// A render host step's draw whose pipeline was being made as it was encoded
// (`render/pending.rs`): an indexed draw, direct or from its arguments.
CmdBindIndexBuffer => c"vkCmdBindIndexBuffer",
CmdDrawIndexed => c"vkCmdDrawIndexed",
CmdDrawIndexedIndirect => c"vkCmdDrawIndexedIndirect",
// A render host step's pass steps (`render/pass_steps.rs`): the clear of a sample plane
// (`sample_planes.rs`) or of a shader-side stencil's shadow (`shadow_stencil.rs`).
CmdClearColorImage => c"vkCmdClearColorImage",
// A batch's first work: the clear of a neutral depth image, which may be multisampled
// (`texture/first_move.rs`, `neutral.rs`).
CmdClearDepthStencilImage => c"vkCmdClearDepthStencilImage",
// An encoder's commands that no host step records, primed with the rest.
CmdBlitImage => c"vkCmdBlitImage",
CmdUpdateBuffer => c"vkCmdUpdateBuffer",
CmdFillBuffer => c"vkCmdFillBuffer",
CmdClearAttachments => c"vkCmdClearAttachments",
CmdCopyQueryPoolResults => c"vkCmdCopyQueryPoolResults",
// A render encoder's extended dynamic states, on a device that has them
// ([`EXTENDED_DYNAMIC_STATE_CALLS`](crate::device::prime::EXTENDED_DYNAMIC_STATE_CALLS),
// [`PRIMITIVE_RESTART_CALLS`](crate::device::prime::PRIMITIVE_RESTART_CALLS)), through the
// entry points the device gave (`dynamic_state.rs`: Vulkan 1.3's, else the extensions').
CmdSetCullMode => c"vkCmdSetCullMode",
CmdSetFrontFace => c"vkCmdSetFrontFace",
CmdSetPrimitiveTopology => c"vkCmdSetPrimitiveTopology",
CmdBindVertexBuffers2 => c"vkCmdBindVertexBuffers2",
CmdSetDepthTestEnable => c"vkCmdSetDepthTestEnable",
CmdSetDepthWriteEnable => c"vkCmdSetDepthWriteEnable",
CmdSetDepthCompareOp => c"vkCmdSetDepthCompareOp",
CmdSetStencilTestEnable => c"vkCmdSetStencilTestEnable",
CmdSetStencilOp => c"vkCmdSetStencilOp",
CmdSetPrimitiveRestartEnable => c"vkCmdSetPrimitiveRestartEnable",
// A render encoder's extension commands, each on a device that has it
// ([`COLOR_WRITE_MASK_CALLS`](crate::device::prime::COLOR_WRITE_MASK_CALLS),
// [`SIZED_INDEX_BIND_CALLS`](crate::device::prime::SIZED_INDEX_BIND_CALLS)), through the
// entry point the device gave (`vkCmdBindIndexBuffer2KHR` where it enables that form).
CmdSetColorWriteMask => c"vkCmdSetColorWriteMaskEXT",
CmdBindIndexBuffer2 => c"vkCmdBindIndexBuffer2",
// The objects a render encoder's lowering makes as it draws (its pass's render pass and
// framebuffer, stand-in and scratch images, `creation.rs`), on the device's lowering thread
// (`render/lowering_worker.rs`), and a host step's ([`HOST_STEP`]).
CreateImage => c"vkCreateImage",
GetImageMemoryRequirements => c"vkGetImageMemoryRequirements",
BindImageMemory => c"vkBindImageMemory",
// A texture's image in place over host pages, made by the device's check of its first move on
// the check's thread as the program's thread makes it (`in_place_check.rs`,
// `texture/in_place_image.rs`): its requirements, and its linear layout, which priming does
// not make ([`UNPRIMED`](crate::device::prime::UNPRIMED)).
GetImageMemoryRequirements2 => c"vkGetImageMemoryRequirements2",
GetImageSubresourceLayout => c"vkGetImageSubresourceLayout",
// An exportable buffer's requirements with its dedicated ones, made by the device's check of
// textures over exported memory on the check's thread as the program's thread makes one
// (`in_place_check.rs`, `exported_memory.rs`).
GetBufferMemoryRequirements2 => c"vkGetBufferMemoryRequirements2",
CreateRenderPass => c"vkCreateRenderPass",
CreateRenderPass2 => c"vkCreateRenderPass2",
CreateFramebuffer => c"vkCreateFramebuffer",
// The query pool of a commit's timestamp slots (`gpu_time.rs`), on the lowering thread where a
// commit's own work waited for a render encoder's end (`render/queued_commits.rs`).
CreateQueryPool => c"vkCreateQueryPool",
// A mesh draw's, and an execution of commands the GPU generates: never primed
// ([`UNPRIMED`](crate::device::prime::UNPRIMED)).
CmdDrawMeshTasks => c"vkCmdDrawMeshTasksEXT",
CmdDrawMeshTasksIndirect => c"vkCmdDrawMeshTasksIndirectEXT",
CmdDrawMeshTasksIndirectCount => c"vkCmdDrawMeshTasksIndirectCountEXT",
CmdExecuteGeneratedCommands => c"vkCmdExecuteGeneratedCommandsEXT",
CmdPreprocessGeneratedCommands => c"vkCmdPreprocessGeneratedCommandsEXT",
}
/// The entry points a host step may call (`CommandBufferShared::host_step`): what a dispatch
/// recorded on the program's thread calls (a command buffer of its pool, the barriers, binds, push
/// constants and dispatch, the shadows' copies, descriptor pools and sets, upload chunks, the views
/// and samplers of bound textures, a `VkPipeline` from a compiled variant), on a device without
/// device addresses a variant's translation, an indirect sparse update's read of its regions, and
/// the objects a render pass's recording makes (`creation.rs`, `neutral.rs`).
/// Its section marks them all as called as it ends ([`called`]), a superset of what one step calls,
/// so the process's first step registers the hook again after it.
pub(crate) const HOST_STEP: &[Call] = &[
Call::CreateCommandPool,
Call::AllocateCommandBuffers,
Call::BeginCommandBuffer,
Call::CmdPipelineBarrier,
Call::EndCommandBuffer,
Call::CmdBindPipeline,
Call::CmdBindDescriptorSets,
Call::CmdPushDescriptorSet,
Call::CmdPushConstants,
Call::CmdDispatch,
Call::CmdCopyBuffer,
Call::CmdCopyBufferToImage,
Call::CmdCopyImageToBuffer,
Call::CreateDescriptorPool,
Call::AllocateDescriptorSets,
Call::UpdateDescriptorSets,
Call::CreateBuffer,
Call::GetBufferMemoryRequirements,
Call::AllocateMemory,
Call::BindBufferMemory,
Call::MapMemory,
Call::GetBufferDeviceAddress,
Call::CreateImageView,
Call::CreateBufferView,
Call::CreateSampler,
Call::CreateShaderModule,
Call::CreateDescriptorSetLayout,
Call::GetDescriptorSetLayoutSupport,
Call::CreatePipelineLayout,
Call::CreateComputePipelines,
// A making's own pipeline cache, seeded from its record and read for its save
// (`shader_cache/driver.rs`).
Call::CreatePipelineCache,
Call::GetPipelineCacheData,
// An indirect sparse update's read of a `Private` buffer (`sparse/indirect.rs`): a copy through
// the device's internal command buffer, waited for, which makes that buffer's fence and
// timeline where it is the device's first internal submission.
Call::CreateFence,
Call::CreateSemaphore,
Call::ResetFences,
Call::QueueSubmit,
Call::WaitForFences,
// A render pass whose render commands a host step records (`render/executions.rs`): each
// command's draw into a secondary command buffer, from a compiled variant's `VkPipeline`, and
// the pass's instances executing the secondaries.
Call::CmdBeginRenderPass,
Call::CmdEndRenderPass,
Call::CmdExecuteCommands,
Call::CmdBindVertexBuffers,
Call::CmdDraw,
Call::CmdSetViewport,
Call::CmdSetScissor,
Call::CmdSetDepthBias,
Call::CmdSetBlendConstants,
Call::CmdSetStencilCompareMask,
Call::CmdSetStencilWriteMask,
Call::CmdSetStencilReference,
Call::CreateGraphicsPipelines,
// The pipeline's debug name, where the instance reports messages (`pipeline/graphics.rs`).
Call::SetDebugUtilsObjectName,
// An execution recorded under a visibility result mode: its lent query.
Call::CmdBeginQuery,
Call::CmdEndQuery,
// An emulated mesh command whose launch a scan counts: its mesh dispatch and its draw from the
// scan's arguments.
Call::CmdDispatchIndirect,
Call::CmdDrawIndirect,
// A draw whose pipeline was being made as it was encoded (`render/pending.rs`).
Call::CmdBindIndexBuffer,
Call::CmdDrawIndexed,
Call::CmdDrawIndexedIndirect,
// A render pass's steps around its instances (`render/pass_steps.rs`), which a pass of
// such draws records in the host step too: a sample plane's or a stencil shadow's clear.
Call::CmdClearColorImage,
// What the step makes that no earlier settle made (`creation.rs`): a neutral image, an
// attachment's stand-in, scratch, sample planes or memoryless backing, a stencil shadow, a
// fragment imageblock's storage, and a run's render pass and framebuffer.
Call::CreateImage,
Call::GetImageMemoryRequirements,
Call::BindImageMemory,
Call::CreateRenderPass,
Call::CreateRenderPass2,
Call::CreateFramebuffer,
];
impl Call {
/// Its word of a set ([`Calls`]) and its bit in that word.
fn bit(self) -> (usize, u64) {
let at = self as usize;
(at / 64, 1 << (at % 64))
}
}
thread_local! {
/// The sections in progress on this thread (of every guard).
static OWN: Cell<usize> = const { Cell::new(0) };
/// The entry points ([`Call`]) this thread called since its last section ended.
static PENDING: Cell<Calls> = const { Cell::new(Calls::NONE) };
/// This thread is one of the runtime's own ([`spawn`]).
static RUNTIME: Cell<bool> = const { Cell::new(false) };
/// The program's code this thread is running ([`program`]).
static PROGRAM: Cell<usize> = const { Cell::new(0) };
/// The sections in progress on this thread when it began running the program's code it
/// runs now: those started since are the runtime's again ([`in_section`]).
static BASE: Cell<usize> = const { Cell::new(0) };
/// The host steps this thread is recording now ([`host_step`]).
static HOST_STEPS: Cell<usize> = const { Cell::new(0) };
/// Command buffers' completion depends on this thread ([`serves_completion`]).
static SERVES_COMPLETION: Cell<bool> = const { Cell::new(false) };
/// The runs of the program's calls this thread lowers now, made earlier on a thread of the
/// program's ([`lowering_program_calls`]).
static LOWERING: Cell<usize> = const { Cell::new(0) };
}
/// Whether this thread is one of the runtime's own ([`spawn`]), whatever code it runs now.
pub(crate) fn on_runtime_thread() -> bool {
RUNTIME.with(Cell::get)
}
/// Whether this thread runs inside a call of the program's: one of the program's own threads,
/// a runtime thread running the program's code ([`program`]), which may call the runtime, or one
/// lowering the program's calls that returned ([`lowering_program_calls`]).
pub(crate) fn in_program_call() -> bool {
!RUNTIME.with(Cell::get) || PROGRAM.with(Cell::get) > 0 || LOWERING.with(Cell::get) > 0
}
/// Run `f`, which lowers calls the program made on a thread of its own and that returned (a render
/// encoder's logged calls, on the device's lowering thread, `render/deferral.rs`), as those calls:
/// what it does is the program's calls' ([`in_program_call`]), in the order the program made
/// them. It runs inside a section all the same, which an exit waits for, and calls only the
/// table's entry points there ([`Call`]).
pub(crate) fn lowering_program_calls<T>(f: impl FnOnce() -> T) -> T {
/// Ends the mark as `f` returns or unwinds.
struct Mark;
impl Drop for Mark {
fn drop(&mut self) {
LOWERING.with(|lowering| lowering.set(lowering.get() - 1));
}
}
LOWERING.with(|lowering| lowering.set(lowering.get() + 1));
let _mark = Mark;
f()
}
/// Run `f`, a command buffer's host step, marked as one ([`in_host_step`]): a serial point of the
/// command buffers' stream, where the work before the batch completed and the batch is not yet
/// submitted.
pub(crate) fn host_step<T>(f: impl FnOnce() -> T) -> T {
/// Ends the mark as the step returns or unwinds.
struct Mark;
impl Drop for Mark {
fn drop(&mut self) {
HOST_STEPS.with(|steps| steps.set(steps.get() - 1));
}
}
HOST_STEPS.with(|steps| steps.set(steps.get() + 1));
let _mark = Mark;
f()
}
/// Whether this thread is recording a host step now ([`host_step`]).
pub(crate) fn in_host_step() -> bool {
HOST_STEPS.with(Cell::get) > 0
}
/// A section in progress: it ends when dropped, on the thread it started on.
pub(crate) struct Busy<'a> {
guard: &'a Guard,
/// A section of the process's guard ([`GUARD`]), whose first calls register the hook again
/// as it ends.
counted: bool,
/// It makes a `VkPipeline`, counted in its guard's makings ([`Guard::enter_making`]).
making: bool,
/// Its slot among its guard's places ([`Guard::places`]), where it is this thread's outermost
/// section and a slot was free.
place: Option<usize>,
/// Counted on this thread ([`OWN`]), so neither `Send` nor `Sync`.
_thread: PhantomData<*const ()>,
}
impl Guard {
/// An open guard.
pub(crate) const fn new() -> Guard {
Guard {
active: AtomicUsize::new(0),
closed: AtomicBool::new(false),
making: AtomicUsize::new(0),
makings_ended: AtomicBool::new(false),
places: [const { AtomicPtr::new(std::ptr::null_mut()) }; PLACES],
}
}
/// Start a section; `None` once the guard closed, unless this thread is in a section
/// already: a section that started runs to its end, the sections inside it included.
///
/// The count rises before the guard is read and [`Guard::close`] closes it before it reads
/// the count, both sequentially consistent: a section either sees the guard closed and
/// starts not, or is counted by the time the closing thread reads the count. A section
/// inside another is counted while the closing thread waits for the outer one. The
/// thread's outermost section keeps its caller's place ([`Guard::places`]).
#[track_caller]
pub(crate) fn enter(&self) -> Option<Busy<'_>> {
self.active.fetch_add(1, Ordering::SeqCst);
let own = OWN.with(Cell::get);
if self.closed.load(Ordering::SeqCst) && own == 0 {
self.active.fetch_sub(1, Ordering::SeqCst);
return None;
}
OWN.with(|count| count.set(own + 1));
let place = match own {
0 => self.take_place(Location::caller()),
_ => None,
};
Some(Busy {
guard: self,
counted: false,
making: false,
place,
_thread: PhantomData,
})
}
/// Keep `place` in a free slot of [`places`](Self::places): the slot, `None` where every one
/// is taken.
fn take_place(&self, place: &'static Location<'static>) -> Option<usize> {
let place = std::ptr::from_ref(place).cast_mut();
let free = std::ptr::null_mut();
(self.places.iter()).position(|slot| {
slot.load(Ordering::Relaxed).is_null()
&& (slot.compare_exchange(free, place, Ordering::Relaxed, Ordering::Relaxed))
.is_ok()
})
}
/// Where the outermost sections in progress began, as `file:line` each, in no order; those
/// begun while every slot was taken are not among them ([`PLACES`]).
pub(crate) fn places_in_progress(&self) -> Vec<String> {
(self.places.iter())
.map(|slot| slot.load(Ordering::Relaxed))
.filter(|place| !place.is_null())
.map(|place| {
// SAFETY: a slot holds null or a `&'static Location` (`take_place`).
let place = unsafe { &*place };
format!("{}:{}", place.file(), place.line())
})
.collect()
}
/// [`enter`](Self::enter) a section around a driver's making of a `VkPipeline`: while one is
/// in progress, [`close`](Self::close) and [`end_makings`](Self::end_makings) wait up to its
/// making's bound. `None` once the makings ended.
///
/// The count of makings rises before the makings' end is read, and `end_makings` marks the
/// end before it reads the count, both sequentially consistent: a making either sees the end
/// and starts not, or is counted by the time the ending thread reads the count.
#[track_caller]
pub(crate) fn enter_making(&self) -> Option<Busy<'_>> {
let mut busy = self.enter()?;
self.making.fetch_add(1, Ordering::SeqCst);
if self.makings_ended() {
self.making.fetch_sub(1, Ordering::SeqCst);
return None;
}
busy.making = true;
Some(busy)
}
/// Whether the guard closed.
pub(crate) fn is_closed(&self) -> bool {
self.closed.load(Ordering::SeqCst)
}
/// Whether the makings ended ([`end_makings`](Self::end_makings)).
pub(crate) fn makings_ended(&self) -> bool {
self.makings_ended.load(Ordering::SeqCst)
}
/// End the makings: none starts from now on ([`enter_making`](Self::enter_making), and on a
/// program's thread [`driver_making`]); then wait until the sections making a `VkPipeline`
/// ended, at most `making`: whether they did. Other sections go on, and start, as before.
/// Once its wait passed `bound`, it says so on standard error, once, as
/// [`close`](Self::close) does.
pub(crate) fn end_makings(&self, bound: Duration, making: Duration) -> bool {
self.makings_ended.store(true, Ordering::SeqCst);
let start = Instant::now();
let mut said = false;
loop {
let makings = self.making.load(Ordering::SeqCst);
if makings == 0 {
return true;
}
let waited = start.elapsed();
if waited >= making {
return false;
}
if waited >= bound && !said {
said = true;
eprintln!(
"metal2vulkan: the exit waits for {makings} pipeline making(s) in the driver, \
at most {} s more",
making.saturating_sub(waited).as_secs()
);
}
std::thread::sleep(POLL);
}
}
/// Close the guard, then wait until no more sections are in progress than `own` (the
/// closing thread's), at most `bound`, or while a section that makes a `VkPipeline` is in
/// progress, at most `making` (no more than `bound` where `making` is not longer): whether
/// they ended.
///
/// The count of sections and the count of makings are read one after the other, so a making
/// may end between the two reads: counted as in progress by the first, gone from the second.
/// The wait gives up only on a count read after the makings', which a section's end lowers
/// before its making's ([`Busy`]'s drop): a making that ended is never taken for a section
/// still in progress.
///
/// Once its wait passed half `bound`, it says on standard error, once, where the sections
/// still in progress began ([`places_in_progress`](Self::places_in_progress)): an exit that
/// waits that long waits for a driver's call or a message sink, which the place names.
pub(crate) fn close(&self, own: usize, bound: Duration, making: Duration) -> bool {
self.closed.store(true, Ordering::SeqCst);
let start = Instant::now();
let (mut said, mut named) = (false, false);
loop {
let active = self.active.load(Ordering::SeqCst);
if active <= own {
return true;
}
between_counts();
let waited = start.elapsed();
if waited >= bound / 2 && !named {
named = true;
eprintln!(
"metal2vulkan: the exit has waited {} ms for {} section(s) of the runtime's \
Vulkan work, begun at: {}",
waited.as_millis(),
active - own,
self.places_in_progress().join(", ")
);
}
let makings = self.making.load(Ordering::SeqCst);
if waited >= bound && (makings == 0 || waited >= making) {
return self.active.load(Ordering::SeqCst) <= own;
}
if waited >= bound && !said {
said = true;
eprintln!(
"metal2vulkan: the exit waits for {makings} pipeline making(s) in the driver, \
at most {} s more",
making.saturating_sub(waited).as_secs()
);
}
std::thread::sleep(POLL);
}
}
}
impl Drop for Busy<'_> {
fn drop(&mut self) {
OWN.with(|own| own.set(own.get() - 1));
// The section ends before its making: a closing thread that reads the makings ended has
// the section ended too ([`Guard::close`]).
self.guard.active.fetch_sub(1, Ordering::SeqCst);
between_counts();
if self.making {
self.guard.making.fetch_sub(1, Ordering::SeqCst);
}
if let Some(slot) = self.place {
self.guard.places[slot].store(std::ptr::null_mut(), Ordering::Relaxed);
}
if self.counted {
// After the section's Vulkan calls, which may have registered a destructor.
let calls = PENDING.with(|pending| {
let calls = pending.get();
pending.set(Calls::NONE);
calls
});
let first = calls.beyond(CALLED.fetch_or(calls));
if first && !self.guard.is_closed() {
register_again();
}
}
}
}
/// Start a section of the process's guard; `None` once the process is exiting.
#[track_caller]
pub(crate) fn enter() -> Option<Busy<'static>> {
let mut busy = GUARD.enter()?;
busy.counted = true;
Some(busy)
}
/// Run `make`, a driver's making of `VkPipeline`s, inside a section of its own where this thread
/// is in a section (a pool's piece), which the hook waits for up to its making's bound
/// ([`Guard::enter_making`], the module docs); on the program's own thread, as it is. A section
/// inside one runs even once the guard closed, so the making is always counted. `None`, `make`
/// unrun, once the makings ended ([`Guard::end_makings`]): the destructors of the driver's
/// compiler may have run.
#[track_caller]
pub(crate) fn driver_making<T>(make: impl FnOnce() -> T) -> Option<T> {
// Held until `make` returned.
let making = match in_section() {
true => Some(GUARD.enter_making()?),
false if GUARD.makings_ended() => return None,
false => None,
};
if making.is_some() {
making_began();
}
Some(make())
}
/// A making in a section began ([`driver_making`]): wake the registrar where the hook is
/// registered in this process, starting it at the process's first making.
fn making_began() {
let pid = std::process::id();
if REGISTERED.load(Ordering::SeqCst) != pid {
return;
}
let started = REGISTRAR_PID.load(Ordering::SeqCst);
if started != pid {
// The thread looks at the makings in progress as it starts, so no wake is due.
let builder = std::thread::Builder::new().name("metal2vulkan-exit-registrar".into());
if REGISTRAR_PID
.compare_exchange(started, pid, Ordering::SeqCst, Ordering::SeqCst)
.is_ok()
{
// A forked child's: the parent's thread is not in this process.
REGISTRAR.store(std::ptr::null_mut(), Ordering::SeqCst);
// Where no thread starts, the makings register the hook as they return alone.
let _ = spawn(builder, register_while_making);
}
return;
}
let thread = REGISTRAR.load(Ordering::SeqCst);
if !thread.is_null() {
// SAFETY: the registrar's own handle, leaked as it started in this process, never freed.
unsafe { (*thread).unpark() };
}
}
/// The registrar, one thread of the runtime's in a process: while a driver's making is in
/// progress in a section, register the hook again every [`MAKING_INTERVAL`], at most
/// [`MAKING_REGISTRATIONS`] times in the process, until the guard closes or the makings end;
/// parked while none is. A destructor the making's compiler registered (a static made at its first
/// use of a path, or the constructors of a compiler it loaded only then) then runs after the hook,
/// which waits for the making, once the interval passed. It makes no Vulkan call.
fn register_while_making() {
let current = Box::into_raw(Box::new(std::thread::current()));
REGISTRAR.store(current, Ordering::SeqCst);
let open = || {
!GUARD.is_closed()
&& !GUARD.makings_ended()
&& DURING_MAKINGS.load(Ordering::Relaxed) < MAKING_REGISTRATIONS
};
while open() {
if GUARD.making.load(Ordering::SeqCst) == 0 {
// A making begun since the load unparks it, and then the park returns at once.
std::thread::park();
continue;
}
std::thread::sleep(MAKING_INTERVAL);
if GUARD.making.load(Ordering::SeqCst) > 0 && open() {
install(After::Making);
// Counted once registered: a count read higher follows the registration.
DURING_MAKINGS.fetch_add(1, Ordering::SeqCst);
}
}
}
/// A driver's `vkCreate*Pipelines` returned, on this thread, whichever it is (`device/compiles.rs`,
/// the door of every one the runtime makes): register the hook again where it is registered in
/// this process and neither the guard closed nor the makings ended, after every destructor the
/// driver's compiler registered in the call (the module docs, "Registered again").
pub(crate) fn pipelines_made() {
if !GUARD.is_closed()
&& !GUARD.makings_ended()
&& REGISTERED.load(Ordering::SeqCst) == std::process::id()
{
AT_MAKINGS.fetch_add(1, Ordering::Relaxed);
install(After::Making);
}
}
/// This thread called `call`; the section it is in registers the hook again as it ends if no
/// section that ended had called it.
pub(crate) fn called(call: Call) {
PENDING.with(|pending| pending.set(pending.get().with(call)));
}
/// Whether the process is exiting: its guard closed.
pub(crate) fn closed() -> bool {
GUARD.is_closed()
}
/// Whether the process's makings ended ([`Guard::end_makings`]).
#[cfg(test)]
pub(crate) fn makings_ended() -> bool {
GUARD.makings_ended()
}
/// Whether this process is exiting, as the runtime sees it: the exit handler a device made or
/// adopted registers ran (`exit`, or the end of `main`), so the runtime's threads that schedule
/// and finish command buffers have ended and it makes no further Vulkan call. Once true it stays
/// true for the process; before any device was made or adopted it is false, since no handler is
/// registered. A command buffer wait then answers `WaitWouldDeadlock` of
/// [`DeadlockCause::Exiting`](crate::DeadlockCause::Exiting), and the bounded wait `TimedOut`.
///
/// No Metal counterpart: Metal's exit runs no such handler, and its waits for work that never
/// finishes hang. A consumer that copies that (a thread parked for good) asks this to tell an
/// exit from a deadlock the runtime refused.
pub fn process_exiting() -> bool {
closed()
}
/// Start one of the runtime's own threads, running `f`: every thread the runtime starts is
/// started here, so it is known to [`may_call`].
///
/// On macOS it starts at the default quality of service, whatever the class of the thread that
/// started it: `std::thread` makes it with `pthread_create` and no class in its attributes, and
/// such a thread starts at `QOS_CLASS_DEFAULT` (`<sys/qos.h>`). So none of the runtime's threads
/// runs below that class for having been started from a program's thread at the utility or
/// background class, which matters for one that serves the whole program for the device's life
/// (the pipeline reaper, the shader cache's worker): on an Apple M4 a pipeline making on a
/// background thread took 6.2 times as long through MoltenVK as at the default class. Some go
/// higher once started: the pipeline pool's threads and a compile's own thread run at the
/// user-initiated class ([`raise_to_user_initiated`]), and the threads that take command buffers
/// to their handlers at the user-interactive class ([`spawn_user_interactive`]). A clamp on the
/// whole process (`taskpolicy -c`, darwin background, App Nap) lowers every thread's priority
/// whatever class the thread asks for: there a new thread still reads the default class, and
/// asking for it again changes nothing (measured on macOS 27.0.1).
pub(crate) fn spawn<F, T>(
builder: std::thread::Builder,
f: F,
) -> std::io::Result<std::thread::JoinHandle<T>>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
builder.spawn(move || {
RUNTIME.with(|runtime| runtime.set(true));
f()
})
}
/// Start one of the runtime's own threads in `scope`, running `f`, as [`spawn`] does: a thread
/// that borrows its caller's arguments and is joined before its caller returns (a compile's own
/// thread, `device/compiles.rs`).
pub(crate) fn spawn_scoped<'scope, F, T>(
scope: &'scope std::thread::Scope<'scope, '_>,
builder: std::thread::Builder,
f: F,
) -> std::io::Result<std::thread::ScopedJoinHandle<'scope, T>>
where
F: FnOnce() -> T + Send + 'scope,
T: Send + 'scope,
{
builder.spawn_scoped(scope, move || {
RUNTIME.with(|runtime| runtime.set(true));
f()
})
}
/// Start one of the runtime's threads that take a command buffer from its commit to its handlers,
/// running `f`, as [`spawn`] does, but at the user-interactive quality of service on macOS, and
/// with the utilization floor at its top on Linux, from its start ([`run_at_user_interactive`]):
/// the scheduler's (`scheduler/`), each `VkQueue`'s waiter, which makes the completion turns
/// (`command_buffer/completion.rs`), every thread of the handler
/// pool (`contexts/`), the device's lowering thread (`render/lowering_worker.rs`), and a predicated loop's check's (`command_buffer/rerun.rs`),
/// whose work the program's command buffers wait for. A thread one of them starts (a program's
/// thread made in a handler) does not take either: it starts at the default class on macOS
/// ([`spawn`]), and with no floor on Linux.
pub(crate) fn spawn_user_interactive<F, T>(
builder: std::thread::Builder,
f: F,
) -> std::io::Result<std::thread::JoinHandle<T>>
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
#[cfg(test)]
let on_start = ON_START.with(|on_start| on_start.borrow().clone());
spawn(builder, move || {
run_at_user_interactive();
#[cfg(test)]
if let Some(on_start) = on_start {
on_start();
}
f()
})
}
/// A test's step between the guard's two counts, run once: between a section's end and its
/// making's ([`Busy`]'s drop), or between [`Guard::close`]'s read of the sections and its read of
/// the makings, whichever this thread reaches first (`tests::exit_guard`). None once this thread's
/// thread-locals are destroyed: `exit` destroys the exiting thread's before it runs the exit
/// handlers (`this_thread.rs`), the hook's [`Guard::close`] among them, where a read of a
/// destroyed one panics, which in a handler that cannot unwind aborts the process.
#[cfg(test)]
fn between_counts() {
let step = BETWEEN_COUNTS.try_with(|step| step.borrow_mut().take());
if let Ok(Some(step)) = step {
step();
}
}
/// Nothing outside tests.
#[cfg(not(test))]
fn between_counts() {}
#[cfg(test)]
thread_local! {
/// The step [`between_counts`] runs once, on this thread.
pub(crate) static BETWEEN_COUNTS: std::cell::RefCell<Option<Box<dyn FnOnce()>>> =
const { std::cell::RefCell::new(None) };
/// What each thread [`spawn_user_interactive`] starts from this thread runs once its class is
/// set, before `f`: a test's reading of the class from inside the thread.
pub(crate) static ON_START: std::cell::RefCell<Option<std::sync::Arc<dyn Fn() + Send + Sync>>> =
const { std::cell::RefCell::new(None) };
}
/// Raise this thread's quality of service to the user-initiated class where it is lower or
/// unspecified, and leave the user-interactive class as it is: a thread of the runtime's own
/// that issues the driver's compiles (the pool's, `pipeline/pool.rs`; a compile's own,
/// `device/compiles.rs`). MoltenVK runs each
/// compile on a dispatch queue that takes the class of the thread asking, so the class here is the
/// compile's class in Metal's compiler service.
#[cfg(target_os = "macos")]
pub(crate) fn raise_to_user_initiated() {
if !at_least_user_initiated() {
// SAFETY: sets the calling thread's own class; no pointer.
unsafe {
libc::pthread_set_qos_class_self_np(libc::qos_class_t::QOS_CLASS_USER_INITIATED, 0)
};
}
}
/// Nothing: no quality of service class here ([`raise_to_user_initiated`] on macOS).
#[cfg(not(target_os = "macos"))]
pub(crate) fn raise_to_user_initiated() {}
/// Lower this thread to the utility quality of service: a warm pass's thread (`warm/pass.rs`),
/// whose translations nothing waits for. Its pipeline makings still compile at the
/// user-initiated class, on a thread of their own (`device/compiles.rs`), and a thread it starts
/// starts at the default class ([`spawn`]).
#[cfg(target_os = "macos")]
pub(crate) fn lower_to_utility() {
// SAFETY: sets the calling thread's own class; no pointer.
unsafe { libc::pthread_set_qos_class_self_np(libc::qos_class_t::QOS_CLASS_UTILITY, 0) };
}
/// Nothing elsewhere: a Linux thread's nice value passes to the threads it starts, and the warm
/// pass starts the pipeline pool's, which serve the program ([`lower_to_utility`] on macOS).
#[cfg(not(target_os = "macos"))]
pub(crate) fn lower_to_utility() {}
/// Run this thread at the user-interactive quality of service, whatever its class: a thread of
/// the runtime's own that takes command buffers to their handlers ([`spawn_user_interactive`]).
#[cfg(target_os = "macos")]
fn run_at_user_interactive() {
// Metal runs every command buffer's scheduled and completed handlers at the user-interactive
// class, whatever the class of the thread that committed it: measured on an M3 Max, macOS
// 27.0.1, committing from threads at each class from background to user-interactive. So the
// threads that bring a command buffer to its handlers, and run them, run there too. It is a
// request of the documented `pthread_set_qos_class_self_np` (`<pthread/qos.h>`): the system
// still places the thread, which is bound to no CPU, and no option or variable turns it off.
// SAFETY: sets the calling thread's own class; no pointer.
unsafe {
libc::pthread_set_qos_class_self_np(libc::qos_class_t::QOS_CLASS_USER_INTERACTIVE, 0)
};
}
/// Ask the scheduler to treat this thread as fully busy whenever it runs, and no thread it starts:
/// a thread of the runtime's own that takes command buffers to their handlers
/// ([`spawn_user_interactive`]), as macOS runs them at the user-interactive class.
#[cfg(target_os = "linux")]
fn run_at_user_interactive() {
// `SCHED_FLAG_RESET_ON_FORK`, `SCHED_FLAG_KEEP_PARAMS` and `SCHED_FLAG_UTIL_CLAMP_MIN`
// (`<linux/sched.h>`).
const FLAGS: u64 = 0x01 | 0x10 | 0x20;
// The thread's policy is read and passed back as it is: `SCHED_OTHER`, `SCHED_BATCH`,
// `SCHED_IDLE` (where a daemon such as ananicy-cpp put the program's session there, it stays
// there) or a real-time one. `SCHED_FLAG_KEEP_POLICY` would keep it with no read, but the
// kernel then keeps the thread's reset-on-fork setting too, ignoring `SCHED_FLAG_RESET_ON_FORK`
// (`__sched_setscheduler`). `SCHED_FLAG_KEEP_PARAMS` keeps the nice value, the real-time
// priority and the slice, which the kernel reads for itself. Only the floor changes, and the
// reset-on-fork setting. Should another process change the thread's policy between the read
// and the call, the call puts the read one back where the thread may, and fails otherwise.
let Some(mut attr) = SchedAttr::own() else {
return;
};
attr.sched_flags = FLAGS;
// The top of the scale (`SCHED_CAPACITY_SCALE`, Documentation/scheduler/sched-util-clamp):
// where the kernel uses the floor (the schedutil governor's clocks, task placement on CPUs of
// different capacity) the thread is placed and clocked as if fully busy. No CPU affinity: the
// system still places it, and no option or variable turns this off. An unprivileged thread
// may raise its own floor; a kernel without the clamps refuses it (`EOPNOTSUPP`, or `EINVAL`
// and `E2BIG` before 5.3, without the flags or the struct's size), and the thread runs as
// before.
//
// Reset on fork: a thread this one starts begins with no floor (`uclamp_fork`). Without it,
// every thread started from one of these would keep the floor for its life: the notices
// thread, a pipeline pool's or a compile's thread started by a program's call in a handler, a
// program's own thread made there. The kernel's reset also starts such a thread at
// `SCHED_OTHER` where this one runs at a real-time policy, and at nice 0 where this one's nice
// is negative (`sched_fork`); `SCHED_BATCH`, `SCHED_IDLE` and a nice of 0 or more carry over as
// before. A thread without `CAP_SYS_NICE` cannot clear the setting again, the program's code
// in a handler included.
//
// Host-wide: the first call with a utilization clamp since the host booted turns the kernel's
// clamp accounting on (`static_branch_enable(&sched_uclamp_used)` in `uclamp_validate`) for
// every task of the host until it reboots, a little more work at each enqueue and dequeue,
// even where the floor acts on nothing (a governor other than schedutil, such as
// `intel_pstate` or `amd-pstate` in active mode, on CPUs of one capacity).
attr.sched_util_min = 1024;
attr.set_own();
}
/// `struct sched_attr` of `sched_setattr(2)` and `sched_getattr(2)` (`<linux/sched/types.h>`), its
/// 56-byte version, the first with the utilization clamps.
#[cfg(target_os = "linux")]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default)]
pub(crate) struct SchedAttr {
pub(crate) size: u32,
pub(crate) sched_policy: u32,
pub(crate) sched_flags: u64,
pub(crate) sched_nice: i32,
pub(crate) sched_priority: u32,
pub(crate) sched_runtime: u64,
pub(crate) sched_deadline: u64,
pub(crate) sched_period: u64,
pub(crate) sched_util_min: u32,
pub(crate) sched_util_max: u32,
}
#[cfg(target_os = "linux")]
impl SchedAttr {
/// Its size, which `size` states.
const SIZE: u32 = std::mem::size_of::<SchedAttr>() as u32;
/// The calling thread's attributes (`sched_getattr(2)`), `size` its own; `None` where the
/// kernel refuses.
pub(crate) fn own() -> Option<SchedAttr> {
let mut attr = SchedAttr {
size: Self::SIZE,
..SchedAttr::default()
};
// SAFETY: `sched_getattr(0, &mut attr, size, 0)` writes at most `size` bytes, the calling
// thread's attributes, into `attr`, which lives for the call.
let read = unsafe {
libc::syscall(
libc::SYS_sched_getattr,
0,
&mut attr as *mut SchedAttr,
Self::SIZE,
0,
)
};
// A kernel's struct may be shorter (48 bytes before 5.3): what it wrote is what it has.
attr.size = Self::SIZE;
(read == 0).then_some(attr)
}
/// Set the calling thread's attributes to these (`sched_setattr(2)`); whether the kernel
/// took them.
pub(crate) fn set_own(mut self) -> bool {
// SAFETY: `sched_setattr(0, &mut attr, 0)` reads `attr`, whose `size` is its own, and sets
// the calling thread's attributes from it. A kernel whose struct is shorter, and that
// finds a field past its end set, writes its own size into `attr.size` before it refuses
// (`E2BIG`, `sched_copy_attr`): `attr` is mutable, and lives for the call.
let set =
unsafe { libc::syscall(libc::SYS_sched_setattr, 0, &mut self as *mut SchedAttr, 0) };
set == 0
}
}
/// Nothing: no such hint here ([`run_at_user_interactive`] on macOS and Linux).
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn run_at_user_interactive() {}
/// Whether this thread runs at the user-initiated quality of service or above: its class is
/// user-initiated or user-interactive. Always where there is no such class.
#[cfg(target_os = "macos")]
pub(crate) fn at_least_user_initiated() -> bool {
use libc::qos_class_t as Q;
let mut class = Q::QOS_CLASS_UNSPECIFIED;
let mut relative = 0;
// SAFETY: the calling thread's own handle, and two out-parameters that live for the call.
let read =
unsafe { libc::pthread_get_qos_class_np(libc::pthread_self(), &mut class, &mut relative) };
read == 0
&& matches!(
class,
Q::QOS_CLASS_USER_INITIATED | Q::QOS_CLASS_USER_INTERACTIVE
)
}
/// Always: no quality of service class here ([`at_least_user_initiated`] on macOS).
#[cfg(not(target_os = "macos"))]
pub(crate) fn at_least_user_initiated() -> bool {
true
}
/// Run the program's code `f` (a handler, a notification, a deallocator, a message sink) on
/// this thread, catching a panic: what it calls is the program's, as on the program's own
/// threads, and no command buffer's work of the runtime's (`degradation::NoticesFor::none`) but
/// where it calls an encoder. Every call of the program's code goes through here. In builds with
/// debug assertions (every test run), end the process where this thread is inside a passing hold
/// of a device's resources, which a making on another thread may wait for (`resource_ids.rs`):
/// `f` may wait for that making.
pub(crate) fn program(f: impl FnOnce()) {
if cfg!(debug_assertions) && crate::resource_ids::inside_passing_hold() {
let name = std::thread::current().name().map(str::to_owned);
eprintln!(
"metal2vulkan: thread {} ran the program's code inside a passing hold of a device's \
resources, which a making on another thread may wait for",
name.as_deref().unwrap_or("(unnamed)")
);
std::process::abort();
}
PROGRAM.with(|program| program.set(program.get() + 1));
let base = BASE.with(|base| base.replace(OWN.with(Cell::get)));
let program = crate::degradation::NoticesFor::none();
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
drop(program);
BASE.with(|outer| outer.set(base));
PROGRAM.with(|program| program.set(program.get() - 1));
}
/// Whether this thread is in one of the runtime's sections now, not in the program's code a
/// section called (a message sink): the device's functions are then the table's alone
/// ([`Call`], `device/functions.rs`).
pub(crate) fn in_section() -> bool {
OWN.with(Cell::get) > BASE.with(Cell::get)
}
/// Mark this thread, one of the runtime's own, as one that command buffers' completion depends on:
/// the scheduler's, which submits them and holds each batch's command buffer until its pass ends,
/// so its gate stays shut meanwhile (`command_buffer/gate.rs`); the queues' waiters, which see
/// them complete and finish them. No release of the program's may run on it
/// ([`program_release`]).
pub(crate) fn serves_completion() {
SERVES_COMPLETION.with(|serves| serves.set(true));
}
/// Whether this thread is one command buffers' completion depends on ([`serves_completion`]): the
/// scheduler's or a queue's waiter, which never blocks in a wait for a command buffer, so a
/// command buffer's completion-seen handlers may run on it
/// (`CommandBufferShared::finish_here`).
pub(crate) fn serving_completion() -> bool {
SERVES_COMPLETION.with(Cell::get)
}
/// Run `f`, the program's code a release runs (a no-copy buffer's deallocator, the drop of its
/// keep-alive, of a range token or of a dma-buf source), as [`program`] runs it. Inside a passing
/// hold of a device's resources it runs once the thread's outermost hold ended instead, on a
/// handler thread where this thread is one command buffers' completion depends on
/// (`resource_ids.rs`, **Passing holds**): a making on another thread may wait for the hold. In
/// builds with debug assertions (every test run), end the process where this thread is one
/// command buffers' completion depends on ([`serves_completion`]) and runs none of the program's
/// code already (a submission handler's own drops are the program's): `f` may wait for such a
/// command buffer, which would then never complete or never open its gate. What a command
/// buffer's commands and encoders took goes in its release, on its queue's handler thread
/// (`CommandBufferShared::complete`).
pub(crate) fn program_release(f: impl FnOnce() + Send + 'static) {
let Some(f) = crate::resource_ids::released_past_passing_hold(f) else {
return;
};
if cfg!(debug_assertions) && SERVES_COMPLETION.with(Cell::get) && PROGRAM.with(Cell::get) == 0 {
let name = std::thread::current().name().map(str::to_owned);
eprintln!(
"metal2vulkan: the runtime's thread {} ran a release of the program's (a deallocator, \
a keep-alive, a range token or a dma-buf source), on which command buffers' \
completion depends",
name.as_deref().unwrap_or("(unnamed)")
);
std::process::abort();
}
program(f);
}
/// Whether this thread may reach the device's functions now: the program's threads always (the
/// program's calls are its own race with an exit), the runtime's own only inside a section or
/// while they run the program's code.
pub(crate) fn may_call() -> bool {
!RUNTIME.with(Cell::get) || OWN.with(Cell::get) > 0 || PROGRAM.with(Cell::get) > 0
}
/// In builds with debug assertions (every test run), end the process when this thread may not
/// reach the device's functions now ([`may_call`]): a Vulkan call of a runtime thread outside a
/// section, which an exit would not wait for.
pub(crate) fn check() {
if cfg!(debug_assertions) && !may_call() {
let name = std::thread::current().name().map(str::to_owned);
eprintln!(
"metal2vulkan: the runtime's thread {} reached the device outside an exit-guard \
section",
name.as_deref().unwrap_or("(unnamed)")
);
std::process::abort();
}
}
/// The devices the hook was registered at since the process last loaded a shared object: that
/// count of loads ([`loaded::count`]), and the devices' identities.
pub(crate) struct Devices {
loads: Option<u64>,
identities: Vec<Vec<u64>>,
}
impl Devices {
/// None yet.
pub(crate) const fn new() -> Devices {
Devices {
loads: None,
identities: Vec::new(),
}
}
/// Whether a device made now registers the hook as the first after a load
/// ([`Admitted::Load`]), the process having loaded `loads` shared objects, and the hook being
/// registered in this process (`registered`): the process's first device (a forked child's
/// included), the first since the count of loads moved, and any where the host keeps none.
pub(crate) fn at_load(&self, registered: bool, loads: Option<u64>) -> bool {
!(registered && loads.is_some() && self.loads == loads)
}
/// Whether a device of `identity` would register the hook now ([`Devices::admit`]), noting
/// nothing: the first after a load ([`Devices::at_load`]), or one of an identity no device
/// registered at since.
pub(crate) fn admits(&self, registered: bool, loads: Option<u64>, identity: &[u64]) -> bool {
self.at_load(registered, loads) || !self.identities.iter().any(|kept| kept == identity)
}
/// Whether a device of `identity` registers the hook ([`register_device`]), the process
/// having loaded `loads` shared objects, and the hook being registered in this process
/// (`registered`), and why; noted when it does. The identities kept are those registered
/// since the last load, so they stay as many as the distinct devices of one load.
pub(crate) fn admit(
&mut self,
registered: bool,
loads: Option<u64>,
identity: Vec<u64>,
) -> Option<Admitted> {
let current = !self.at_load(registered, loads);
if !self.admits(registered, loads, &identity) {
return None;
}
if !current {
self.identities.clear();
}
self.loads = loads;
self.identities.push(identity);
Some(match current {
true => Admitted::Identity,
false => Admitted::Load,
})
}
/// The identities kept.
#[cfg(test)]
pub(crate) fn kept(&self) -> usize {
self.identities.len()
}
}
/// Why a device registers the hook ([`Devices::admit`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Admitted {
/// The process's first device (a forked child's included), the first after a load of a
/// shared object, or any where the host keeps no count of loads: what was loaded since makes
/// its statics at each entry point's first call again, so the entry points called before its
/// priming are forgotten ([`before_priming`]).
Load,
/// A device of an identity new since the last load.
Identity,
}
/// The process's [`Devices`].
static DEVICES: Mutex<Devices> = Mutex::new(Devices::new());
/// The hook's registrations in this process: at devices ([`register_device`]), after instances
/// ([`instance_made`]), and in all.
static AT_DEVICES: AtomicU64 = AtomicU64::new(0);
static AT_INSTANCES: AtomicU64 = AtomicU64::new(0);
static INSTALLED: AtomicU64 = AtomicU64::new(0);
/// Register the hook at a device made or adopted, once its driver and layers are loaded and
/// primed (`device/prime.rs`), when the device can have made statics after the hook's last
/// registration: the process's first device (a forked child's included); a device whose
/// `identity` (its driver's and device's UUIDs and the entry points of the table its functions
/// reach, the topmost layer's where layers run) no device registered at since the last load of
/// a shared object; and the first device after such a load, since a driver or layer loaded again
/// makes its statics again. Devices made again on what stays loaded register nothing, so a
/// process that makes devices over and over adds a registration per load, not per device. Where
/// the host keeps no count of loads, every device registers.
///
/// `primed`: whether the device was primed ([`before_priming`] answered that it registers). One
/// that registers unprimed (a shared object loaded, or a device of its identity registered,
/// between the two calls) made no first calls since: the entry points called so far are
/// forgotten ([`forget_calls`]), so each section's first call of one registers the hook again.
pub(crate) fn register_device(identity: Vec<u64>, primed: bool) {
let loads = loaded::count();
let mut devices = DEVICES.lock().unwrap_or_else(PoisonError::into_inner);
let registered = REGISTERED.load(Ordering::SeqCst) == std::process::id();
if devices.admit(registered, loads, identity).is_none() {
return;
}
if !primed {
forget_calls();
}
AT_DEVICES.fetch_add(1, Ordering::Relaxed);
register();
}
/// Before a device of `identity`, made or adopted, is primed (`device/prime.rs`): whether it
/// registers the hook ([`register_device`]), and so is primed; a device that registers nothing,
/// made on what stays loaded after a device of its identity registered, makes no call first,
/// the earlier device's priming having made each. Where it registers the hook as the first after
/// a load of a shared object ([`Admitted::Load`]), forget the entry points called so far
/// ([`forget_calls`]). A layer the loader unloaded with the last instance and loaded again with
/// this one makes its statics again at the first call of each entry point: the priming's, which
/// come before the device's registration, and count as the first calls since, so a later section
/// that calls one of them registers nothing again (after an exit handler the program registered
/// once its device was made, such a registration would close the guard before it); one the
/// priming did not call registers the hook again as its section ends.
pub(crate) fn before_priming(identity: &[u64]) -> bool {
let loads = loaded::count();
let devices = DEVICES.lock().unwrap_or_else(PoisonError::into_inner);
let registered = REGISTERED.load(Ordering::SeqCst) == std::process::id();
if devices.at_load(registered, loads) {
forget_calls();
}
devices.admits(registered, loads, identity)
}
/// A `vkCreateInstance` was called, whatever it answered: by this thread (the runtime's one door,
/// `device/create.rs`), or by the program before (the instance of a device the runtime adopted,
/// `device/adopt.rs`, or one it reported, [`program_instance_made`]). A layer registers an exit
/// handler at each call, as it begins (the validation layer 1.4.357's `ApplicationAtExit`, which
/// forgets every device the layer knows), so the hook registers again after it, where it is
/// registered in this process. Before the process's first device the hook is not registered yet:
/// that device registers it, after its instance.
pub(crate) fn instance_made() {
if REGISTERED.load(Ordering::SeqCst) == std::process::id() {
AT_INSTANCES.fetch_add(1, Ordering::Relaxed);
install(After::Calls);
}
}
/// The program made a `VkInstance` itself, outside the runtime: call this right after its
/// `vkCreateInstance`, whatever that answered. A layer registers an exit handler as an instance is
/// created (the validation layer's `ApplicationAtExit` forgets every device it knows), and a
/// process's exit handlers run in the reverse order of their registration: without this, that
/// handler runs before the runtime's, and a call of the runtime's still in progress on its own
/// threads, or begun before the runtime's handler runs, ends the process inside the layer
/// (`SIGABRT`, "The VkDevice dispatch handle was not found", or `SIGSEGV`). This registers the
/// runtime's handler again, after the layer's; before the process's first device it does nothing,
/// as that device registers it after its own instance. The instances the runtime creates, and those
/// of the devices it adopts ([`Device::from_raw`](crate::Device::from_raw)), need no call.
///
/// No Metal counterpart: Metal has no Vulkan instance.
pub fn program_instance_made() {
instance_made();
}
/// A device's priming (`device/prime.rs`) made the first call of each of `calls` since the last
/// load of a shared object ([`before_priming`]), on its creating thread, before the device
/// registers the hook: the statics those calls made have their destructors run after the hook, so
/// a section's later call of one of them registers nothing again ([`CALLED`]).
pub(crate) fn primed(calls: Calls) {
CALLED.fetch_or(calls);
PRIMINGS.fetch_add(1, Ordering::Relaxed);
}
/// The devices primed in this process ([`primed`]).
static PRIMINGS: AtomicU64 = AtomicU64::new(0);
/// Forget the entry points called so far ([`CALLED`]): the next section that calls each
/// registers the hook again as it ends.
pub(crate) fn forget_calls() {
CALLED.clear();
LOADS.fetch_add(1, Ordering::Relaxed);
}
/// The times [`forget_calls`] ran (the loads devices were made after): each lets the sections
/// register the hook once per entry point of the table.
static LOADS: AtomicU64 = AtomicU64::new(0);
/// How many times this process registered the exit guard's hook, for the lanes: not part of the
/// API.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Registrations {
/// At devices made or adopted.
pub at_devices: u64,
/// After instances the runtime created.
pub at_instances: u64,
/// After makings of `VkPipeline`s, one per making.
pub at_makings: u64,
/// While makings in sections were in progress, one every `MAKING_INTERVAL` (2 ms), at most
/// `MAKING_REGISTRATIONS` (16384) in the process.
pub during_makings: u64,
/// In all: at devices, after instances, after makings, while makings were in progress, and at
/// the end of sections (after the first call of an entry point of the table).
pub total: u64,
/// The most the sections can make: one per entry point of the table at each load a device
/// was made after, the process's first device's included.
pub section_bound: u64,
/// The shared objects the process has loaded so far, unloaded ones included, as the rule
/// that registers the hook at a device after a load counts them; `None` where the host keeps
/// no count, and every device registers.
pub loads: Option<u64>,
/// The devices primed (`device/prime.rs`): each that registers the hook at its making, and
/// none made again on what stays loaded.
pub primings: u64,
}
/// This process's [`Registrations`] so far.
pub fn registrations() -> Registrations {
Registrations {
at_devices: AT_DEVICES.load(Ordering::Relaxed),
at_instances: AT_INSTANCES.load(Ordering::Relaxed),
at_makings: AT_MAKINGS.load(Ordering::Relaxed),
during_makings: DURING_MAKINGS.load(Ordering::Relaxed),
total: INSTALLED.load(Ordering::Relaxed),
section_bound: Call::ALL.len() as u64 * LOADS.load(Ordering::Relaxed).max(1),
loads: loaded::count(),
primings: PRIMINGS.load(Ordering::Relaxed),
}
}
/// Register the hook in this process. A child forked from a process that registered registers
/// its own, and counts none of its parent's sections, whose threads it does not have.
pub(crate) fn register() {
let pid = std::process::id();
let seen = REGISTERED.load(Ordering::SeqCst);
if seen == pid {
install(After::Calls);
return;
}
if REGISTERED
.compare_exchange(seen, pid, Ordering::SeqCst, Ordering::SeqCst)
.is_err()
{
register_again();
return;
}
if seen != 0 {
GUARD.active.store(OWN.with(Cell::get), Ordering::SeqCst);
}
install(After::Calls);
}
/// Register the hook again in the process it is registered in: after the destructors registered
/// since. A forked child that has not registered its own does nothing.
fn register_again() {
if REGISTERED.load(Ordering::SeqCst) == std::process::id() {
install(After::Calls);
}
}
/// What a registration of the hook follows, and so what the destructors registered just before it
/// are: its run ([`exit_handler`]) ends what those may break, before they run (the module docs,
/// "The hook").
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum After {
/// A device, an instance, or a section's first call of an entry point of the table: a
/// driver's or a layer's destructors, which any Vulkan call may reach. Its run closes the
/// guard ([`hook`]).
Calls,
/// A driver's making of `VkPipeline`s, returned or in progress: its compiler's destructors,
/// which a making reaches. Its run ends the makings ([`hook_after_making`]), and the guard
/// stays open, so the exit handlers the program registered before it still have the
/// runtime's threads.
Making,
}
impl After {
/// Its argument to the C library's exit handler: a tag, never dereferenced.
fn tag(self) -> *mut std::ffi::c_void {
std::ptr::without_provenance_mut(match self {
After::Calls => 1,
After::Making => 2,
})
}
/// The kind `tag` stands for ([`tag`](Self::tag)); any other tag (a test's null) a device's.
fn of_tag(tag: *mut std::ffi::c_void) -> After {
match tag.addr() {
2 => After::Making,
_ => After::Calls,
}
}
}
/// Register [`exit_handler`] with the C library's exit handlers, after `after`: through
/// `on_exit(3)`, which shares `atexit`'s order and passes the exit's status, and the kind's tag.
#[cfg(all(target_os = "linux", target_env = "gnu"))]
fn install(after: After) {
INSTALLED.fetch_add(1, Ordering::Relaxed);
// SAFETY: `exit_handler` takes the status and the tag, which it does not dereference, and
// never unwinds. Registration fails only when the C library is out of memory; the process
// then exits unguarded, as before.
unsafe { on_exit(exit_handler, after.tag()) };
}
/// Register [`exit_handler`] with the C library's exit handlers, after `after`: through
/// `__cxa_atexit`, which shares `atexit`'s order and passes the kind's tag.
#[cfg(all(unix, not(all(target_os = "linux", target_env = "gnu"))))]
fn install(after: After) {
INSTALLED.fetch_add(1, Ordering::Relaxed);
// SAFETY: `exit_handler` takes the tag, which it does not dereference, and never unwinds; no
// shared object is named, so only the process's exit runs it. Registration fails only when
// the C library is out of memory; the process then exits unguarded, as before.
unsafe { __cxa_atexit(exit_handler, after.tag(), std::ptr::null_mut()) };
}
/// Nothing to register: the runtime is built for unix hosts, whose exit order the hook relies on.
#[cfg(not(unix))]
fn install(_: After) {}
// The C++ ABI's `__cxa_atexit`, which the C library of every unix host but the GNU one's (where
// `on_exit` is used) exports and `libc` does not declare for all of them: `function` is called with
// `argument` as the process exits (no shared object named), among the handlers `atexit` registers,
// in the reverse order of their registration.
#[cfg(all(unix, not(all(target_os = "linux", target_env = "gnu"))))]
unsafe extern "C" {
fn __cxa_atexit(
function: extern "C" fn(*mut std::ffi::c_void),
argument: *mut std::ffi::c_void,
shared_object: *mut std::ffi::c_void,
) -> std::ffi::c_int;
}
// The GNU C library's `on_exit(3)` (`<stdlib.h>`), which `libc` does not declare: `function` is
// called with `exit`'s status and `argument`, among the handlers `atexit` registers, in the
// reverse order of their registration.
#[cfg(all(target_os = "linux", target_env = "gnu"))]
unsafe extern "C" {
fn on_exit(
function: extern "C" fn(std::ffi::c_int, *mut std::ffi::c_void),
argument: *mut std::ffi::c_void,
) -> std::ffi::c_int;
}
/// The hook's run for a registration after `after`, in this process: [`hook`] after calls,
/// [`hook_after_making`] after a making. `None` (nothing done) in another process than the one
/// registered; else whether what it waits for ended.
fn run(after: After) -> Option<bool> {
let (registered, pid) = (REGISTERED.load(Ordering::SeqCst), std::process::id());
match after {
After::Calls => hook(
&GUARD,
&IN_PROCESS,
registered,
pid,
OWN.with(Cell::get),
BOUND,
MAKING_BOUND,
),
After::Making => hook_after_making(
&GUARD,
&IN_PROCESS,
registered,
pid,
OWN.with(Cell::get),
BOUND,
MAKING_BOUND,
),
}
}
/// The exit handler, where it learns the exit's status: the hook's run for the registration
/// `tag` stands for ([`After`]) waits at most [`BOUND`], or [`MAKING_BOUND`] while a making is in
/// progress, and returns once what it waits for ended; where the driver still runs what the
/// runtime gave it past that, the process ends at once with that status ([`end_at_once`]).
#[cfg(all(target_os = "linux", target_env = "gnu"))]
extern "C" fn exit_handler(status: std::ffi::c_int, tag: *mut std::ffi::c_void) {
if run(After::of_tag(tag)) == Some(false) {
end_at_once(status);
}
}
/// The exit handler, where it learns no status (`__cxa_atexit`): the hook's run for the
/// registration `tag` stands for ([`After`]) waits at most [`BOUND`], or [`MAKING_BOUND`] while a
/// making is in progress, and returns.
#[cfg(all(unix, not(all(target_os = "linux", target_env = "gnu"))))]
extern "C" fn exit_handler(tag: *mut std::ffi::c_void) {
run(After::of_tag(tag));
}
/// The exit handler [`install`] registers after `after`, run alone, as the C library's `exit` runs
/// it for `status` (where it learns the status): a forked child's test runs it without the other
/// handlers the child inherits.
#[cfg(all(test, unix))]
pub(crate) fn run_on_exit(status: std::ffi::c_int, after: After) {
#[cfg(all(target_os = "linux", target_env = "gnu"))]
exit_handler(status, after.tag());
#[cfg(not(all(target_os = "linux", target_env = "gnu")))]
{
let _ = status;
exit_handler(after.tag());
}
}
/// What [`end_at_once`] writes on standard error, one line in a degrade notice's form, naming
/// [`BOUND`] and [`MAKING_BOUND`] (a test holds it to them).
#[cfg(all(target_os = "linux", target_env = "gnu"))]
pub(crate) const AT_ONCE_NOTICE: &str = "metal2vulkan: the process's exit: the Vulkan driver \
still runs the runtime's work (a call, a pipeline's making, or a CPU device's commands) after \
the exit's wait of 2 s for it (60 s while a pipeline's making is in progress); the process \
ends at once with its status, and the exit handlers registered before the runtime's (a \
terminal's restore, say) do not run\n";
/// End the process at once with `status`, the exit's, where the driver still runs what the runtime
/// gave it once the hook's wait reached its bound: a section of another thread, a making, or a CPU
/// device's work. One line on standard error ([`AT_ONCE_NOTICE`]), the C library's output streams
/// flushed (as `exit` flushes them after its handlers), then `_exit`: no later exit handler or
/// destructor runs, beside the driver's call or after it.
#[cfg(all(target_os = "linux", target_env = "gnu"))]
fn end_at_once(status: std::ffi::c_int) -> ! {
// SAFETY: writes the notice's bytes, live for the call, to standard error; a failed write
// is ignored, as the exit goes on.
unsafe { libc::write(2, AT_ONCE_NOTICE.as_ptr().cast(), AT_ONCE_NOTICE.len()) };
// SAFETY: flushes every open output stream of the C library.
unsafe { libc::fflush(std::ptr::null_mut()) };
// SAFETY: ends the process with the exit's status.
unsafe { libc::_exit(status) }
}
/// The hook's work in process `pid`, registered in process `registered`, on a thread with `own`
/// sections in progress: close `guard` and wait for the other sections, then for the work submitted
/// to `in_process`'s devices, which the first run takes whether it waits or not, at most `bound` in
/// all; where a section that makes a `VkPipeline` held the sections' wait past `bound` (at most
/// `making`, [`Guard::close`]), the work's wait gets `bound` from there. `None` (nothing done) in
/// another process than the one registered; else whether the other sections ended and the work
/// completed.
pub(crate) fn hook(
guard: &Guard,
in_process: &InProcessDevices,
registered: u32,
pid: u32,
own: usize,
bound: Duration,
making: Duration,
) -> Option<bool> {
(registered == pid).then(|| {
// A bound past what the host's clock holds is no deadline.
let deadline = Instant::now().checked_add(bound);
let ended = guard.close(own, bound, making);
// A making's section held the close past the bound: the work gets the bound from there.
let now = Instant::now();
let deadline = match deadline {
Some(deadline) if now > deadline => now.checked_add(bound),
deadline => deadline,
};
// A later run (an earlier registration) follows the destructors registered after its
// registration: it finds no device, so makes no Vulkan call.
let devices = in_process.take(pid);
ended && drain(&devices, deadline)
})
}
/// The hook's run for a registration after a making ([`After::Making`]), in process `pid`,
/// registered in process `registered`: end `guard`'s makings and wait for those in progress in
/// sections, at most `making` ([`Guard::end_makings`]), so the destructors the driver's compiler
/// registered before that registration run next with no making in progress, and none begins
/// after them. The guard stays open: the exit handlers the program registered before the
/// registration run next with the runtime's threads, as Metal's exit runs them. Where a device
/// whose queues run in this process lives (`in_process`, a CPU device, whose queues compile as
/// they run their work, on the process's own threads), it is [`hook`]'s run, with `own` and
/// `bound`. `None` (nothing done) in another process than the one registered; else whether the
/// makings ended.
pub(crate) fn hook_after_making(
guard: &Guard,
in_process: &InProcessDevices,
registered: u32,
pid: u32,
own: usize,
bound: Duration,
making: Duration,
) -> Option<bool> {
if registered != pid {
return None;
}
if in_process.any(pid) {
return hook(guard, in_process, registered, pid, own, bound, making);
}
Some(guard.end_makings(bound, making))
}
/// The sections in progress on this thread.
#[cfg(test)]
pub(crate) fn own() -> usize {
OWN.with(Cell::get)
}
/// Whether a section that ended called `call`.
#[cfg(test)]
pub(crate) fn was_called(call: Call) -> bool {
CALLED.load().holds(call)
}