Stop the runtime's own holds of a device at the program's last drop, and wait for them
The device's check of textures over host pages (in_place_check.rs) and its warm pass (warm/pass.rs) upgrade a weak handle on threads of their own and hold the device while they make objects on it: the check across a class's rounds (its device and its class's buffer), the pass across a record. A program that drops its last handle meanwhile leaves the holder the last share, so the device is torn down on the holder's thread after the program's drop returned. A consumer that adopted the VkDevice destroys it once its last drop returns, and Vulkan requires every object made from it destroyed first (VUID-vkDestroyDevice-device-05137). The same class as the queue-switch check's (de2554f1ac), whose thread could be made to hold no device handle; these two make buffers, textures and pipeline states that hold the device, so they cannot. What (device/holds.rs): - Every device handle is counted as the program's or as a holder's. WeakDevice::hold takes a holder's handle; a clone of one is a holder's too (the device of every object the holder makes); every other handle (the device's own, its objects', a runtime thread's upgrade) is the program's. The handle carries its kind (Share: the registry where it is a holder's), so a Device is 16 bytes, and its first field stays the Arc (the auto-trait walk that consumer_auto_traits holds to the default recursion limit keeps its depth). - The drop that leaves the program no handle stops the holds; on a thread of the program's it waits until the holders' handles went, each counted gone only once its share of the device went, so the device's last share is the program's and the teardown runs on its thread before the drop returns. On a runtime thread it waits for nothing (those threads serve the work the holds may wait for). - The holders read the stop between their steps (Device::holds_go_on): the check before each round, the warm pass before each record. The wait is at most the check's round in progress and its release of its buffer, or the record the pass is making; only at teardown. - A stopped device is gone: no holder takes it, and Device::system_default_with makes a new device rather than revive it (WeakDevice::revive). - exit_guard::on_runtime_thread tells the runtime's threads apart. - Device's rustdoc and the runtime README state the last-drop contract. Tests (tests/teardown_threads.rs, behind the crate's existing gated hold, freed_memory::held): - a_running_in_place_check_leaves_the_device_to_the_program_s_thread: the check held before a round while it holds the device and its buffer; the program's last drop tears the device down on its own thread before it returns, having waited for the check. Skipped, saying so, where the device takes no texture in place or its check makes no round. - a_running_warm_pass_leaves_the_device_to_the_program_s_thread: the same for a warm pass held before its first record. Not the queue-switch check's wait: its test (de2554f1ac) is unchanged here. Verified, at this tree (scratch 016ea26f28), --cfg m2v_gpu device tests: - Apple M3 Max, MoltenVK 1.4.2, macOS 27.0.1: tests::teardown_threads three times, 10 run, 10 passed each ("the last drop took 7.55ms, of which the wait for InPlaceCheck 991µs"; the warm pass's wait 22 to 113 µs). The runtime suite: 1636 run, 1636 passed, 5 skipped. cargo clippy plain and under --cfg m2v_gpu, and RUSTDOCFLAGS='-D warnings' cargo doc, clean. - RTX 5080 Laptop GPU, NVIDIA 615.71.09 (b5080, the hazard filter): tests::teardown_threads three times, both new tests passing in each (the wait for InPlaceCheck 3.98, 5.35 and 7.06 ms; for WarmPass 84, 208 and 239 µs); 10 of 10, 9 of 10 and 10 of 10, the one failure the unchanged queue-switch test's 16 ms bound (it waited 26.5 ms, beside the runtime suite; 13.4 and 14.3 ms in the other runs), followed up apart. The runtime suite: 1653 run, 1652 passed, 5 skipped, the one failure srgb_fetch_images::an_srgb_attachment_is_fetched_through_its_ unsigned_integer_view (an image plan's view formats at 4 samples; no hold is involved), attributed apart. - Negative control (the last drop waits for nothing: one line removed, built into a separate nextest archive): both new tests FAIL on the M3 and on the 5080, "the device was not torn down on this thread before its last drop returned" (left: Err(Empty)). (cherry picked from commit 4ca5040b55afcd816e22b4f2c9ddae6e675006b3)
This commit is contained in:
1 parent
f5d7b958a0
commit
bd779ba8c8
10 files changed
+410
-28
No files matched your search
@@ -197,7 +197,10 @@ the instance and its version, the physical device, the device, the queue family
|
||||
exactly what the device enabled (an `EnableSet`), plus what the runtime may use outside that
|
||||
vocabulary (calibrated timestamps, sparse features, view format reinterpretation, host image copy)
|
||||
and the same per-device choices as `DeviceOptions`. Dropping the runtime device destroys only what
|
||||
the runtime made.
|
||||
the runtime made. The program's last drop of the device and its objects, on a thread of the
|
||||
program's, returns once that is done: it stops the runtime's own background work on the device (its
|
||||
check of textures over host pages, its warm pass) and waits for it, at most a round of the check or
|
||||
the record the pass is making.
|
||||
|
||||
**The Vulkan objects underneath** (no Metal counterpart; each call is `unsafe`):
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
use super::instance_requirements::instance_requirements_met;
|
||||
use super::keeper::keep_drivers_loaded;
|
||||
use super::properties::{DeviceKind, PhysicalDeviceIdentity};
|
||||
use super::{device_error, lock, Device, DeviceShared, InstanceShared, Parts};
|
||||
use super::{device_error, lock, Device, InstanceShared, Parts, WeakDevice};
|
||||
use crate::degradation::{Degradation, DegradeNotice};
|
||||
use crate::enables::{host_image_copy_names, listed_reported, listed_structures, Enables};
|
||||
use crate::error::{vulkan, Error, InstanceApiVersion, LoaderNotFound};
|
||||
@@ -1900,7 +1900,7 @@ struct Made {
|
||||
/// The options' pipeline records' hook, which each of the group's devices holds.
|
||||
records: Option<std::sync::Weak<dyn Fn(crate::PipelineRecord) + Send + Sync>>,
|
||||
instance: Weak<InstanceShared>,
|
||||
devices: Vec<(vk::PhysicalDevice, Weak<DeviceShared>)>,
|
||||
devices: Vec<(vk::PhysicalDevice, WeakDevice)>,
|
||||
}
|
||||
|
||||
impl Made {
|
||||
@@ -1925,8 +1925,7 @@ impl Made {
|
||||
self.devices
|
||||
.iter()
|
||||
.find(|(of, _)| *of == physical)
|
||||
.and_then(|(_, device)| device.upgrade())
|
||||
.map(Device)
|
||||
.and_then(|(_, device)| device.revive())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1940,9 +1939,7 @@ pub(crate) fn live_devices_with(options: &DeviceOptions) -> usize {
|
||||
.find(|group| group.instance_for(options).is_some());
|
||||
of.map_or(0, |group| {
|
||||
let devices = group.devices.iter();
|
||||
devices
|
||||
.filter(|(_, device)| device.strong_count() > 0)
|
||||
.count()
|
||||
devices.filter(|(_, device)| device.lives()).count()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2041,8 +2038,8 @@ fn device_of(
|
||||
return Ok(device);
|
||||
}
|
||||
let device = create_on(instance, physical, options, cache)?;
|
||||
group.devices.retain(|(_, weak)| weak.strong_count() > 0);
|
||||
group.devices.push((physical, Arc::downgrade(&device.0)));
|
||||
group.devices.retain(|(_, weak)| weak.lives());
|
||||
group.devices.push((physical, device.downgrade()));
|
||||
Ok(device)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
//! The runtime's own holds of a device: its check of textures in place (`in_place_check.rs`) and
|
||||
//! its warm pass (`warm/pass.rs`), each on a thread of its own, which hold the device while they
|
||||
//! make objects on it and let it go between their steps.
|
||||
//!
|
||||
//! - **Two counts.** Every handle of the device is counted here, as the program's or as a
|
||||
//! holder's. A handle a holder takes ([`WeakDevice::hold`]) is a holder's, and so is each handle
|
||||
//! cloned from one (the device of every object the holder makes); every other is the program's
|
||||
//! (the device's own, its objects', and a runtime thread's upgrade of a weak handle).
|
||||
//! - **The program's last drop tears the device down.** The drop that leaves the program no
|
||||
//! handle stops the holds, and on a thread of the program's waits until the holders' handles
|
||||
//! went, so the device goes with it, on that thread, before it returns. A consumer that adopted
|
||||
//! the `VkDevice` destroys it once the last drop returned, and Vulkan requires every object
|
||||
//! made from it destroyed first. On one of the runtime's own threads (a completed handler's
|
||||
//! release of the program's last command buffer) it waits for nothing: those threads serve the
|
||||
//! work the holds may wait for, and the program's work there ends where it ends already.
|
||||
//! - **The wait is a step.** A holder reads the stop between its steps
|
||||
//! ([`Device::holds_go_on`]): the check between its rounds, the warm pass between its records.
|
||||
//! So the wait is at most the check's round in progress (and its release of its buffer), or the
|
||||
//! record the pass is making. It happens only at teardown.
|
||||
//! - **Exact.** A program handle's drop, a holder's take and a holder's handle's clone and drop
|
||||
//! count under the registry's lock; a holder's handle is counted gone only once its share of the
|
||||
//! device went ([`Share`]), so the program's last drop, waiting for that, holds the device's last
|
||||
//! share. A program handle is counted gone before its share goes: a program handle another
|
||||
//! thread drops at once may go last, inside that drop. A program handle's clone counts without
|
||||
//! the lock (it clones a handle that is counted), and so does a runtime thread's upgrade, which
|
||||
//! keeps the device as before: one made while the last drop waits goes on that thread.
|
||||
//! - **A stopped device is gone.** Once stopped, no holder takes the device, and a device made on
|
||||
//! the same physical device makes a new one (`create.rs`) rather than revive it.
|
||||
|
||||
use super::{lock, Device, DeviceShared, WeakDevice};
|
||||
use crate::exit_guard;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::sync::{Arc, Condvar, Mutex};
|
||||
|
||||
/// The runtime's holders of a device, each of whose steps reads the stop.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(crate) enum Holder {
|
||||
/// The check of textures in place, between its rounds.
|
||||
InPlaceCheck,
|
||||
/// The warm pass, between its records.
|
||||
WarmPass,
|
||||
}
|
||||
|
||||
/// A device's registry of its handles and the runtime's own holds (the module docs).
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Holds {
|
||||
/// The program's handles.
|
||||
program: AtomicUsize,
|
||||
/// The holders' handles, and whether the program's last drop stopped the holds.
|
||||
state: Mutex<State>,
|
||||
/// Signalled as the last holder's handle goes.
|
||||
released: Condvar,
|
||||
/// Whether the holds were stopped: read between a holder's steps without the lock.
|
||||
stopping: AtomicBool,
|
||||
/// How long the program's last drop waited for the holds, in nanoseconds: 0 where it waited
|
||||
/// for none.
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
waited: std::sync::atomic::AtomicU64,
|
||||
/// The crate's device tests' holds of each holder between its steps
|
||||
/// (`tests/teardown_threads.rs`).
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
held: [crate::freed_memory::held::Held; 2],
|
||||
}
|
||||
|
||||
/// The registry's count of the holders' handles, and its stop.
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
/// The holders' handles: taken by a holder or cloned from one, their share of the device not
|
||||
/// yet gone.
|
||||
holders: usize,
|
||||
/// The program's last drop stopped the holds.
|
||||
stopped: bool,
|
||||
}
|
||||
|
||||
/// A handle's place in the registry (the module docs): the registry where it is a holder's,
|
||||
/// `None` where it is the program's (whose drop reaches the registry through the device).
|
||||
pub(super) struct Share(Option<Arc<Holds>>);
|
||||
|
||||
impl Holds {
|
||||
/// Whether the program's last drop stopped the holds.
|
||||
pub(crate) fn stopping(&self) -> bool {
|
||||
self.stopping.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
/// How long the program's last drop waited for the holds; `None` where it waited for none.
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
pub(crate) fn waited(&self) -> Option<std::time::Duration> {
|
||||
match self.waited.load(Ordering::Acquire) {
|
||||
0 => None,
|
||||
nanos => Some(std::time::Duration::from_nanos(nanos)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The test hold of `holder`.
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
pub(crate) fn held(&self, holder: Holder) -> &crate::freed_memory::held::Held {
|
||||
&self.held[holder as usize]
|
||||
}
|
||||
|
||||
/// Let `holder`'s test hold go once the holds were stopped, or once it was let go already.
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
pub(crate) fn release_once_stopping(&self, holder: Holder) {
|
||||
self.held(holder).release_once(&self.stopping);
|
||||
}
|
||||
}
|
||||
|
||||
/// The program's handle of `shared`, counted.
|
||||
fn program(shared: Arc<DeviceShared>) -> Device {
|
||||
shared.holds.program.fetch_add(1, Ordering::SeqCst);
|
||||
Device(shared, Share(None))
|
||||
}
|
||||
|
||||
/// A new device's first handle, the program's.
|
||||
pub(super) fn first(shared: Arc<DeviceShared>) -> Device {
|
||||
program(shared)
|
||||
}
|
||||
|
||||
/// The registry a new device's handles share.
|
||||
pub(super) fn holds() -> Arc<Holds> {
|
||||
Arc::new(Holds::default())
|
||||
}
|
||||
|
||||
/// `weak`'s device for the program (a runtime thread's upgrade among them), counted, while it
|
||||
/// lives.
|
||||
pub(super) fn upgrade(weak: &WeakDevice) -> Option<Device> {
|
||||
weak.0.upgrade().map(program)
|
||||
}
|
||||
|
||||
impl WeakDevice {
|
||||
/// The device for a step of one of the runtime's holders, counted as the holder's (the module
|
||||
/// docs): `None` where it went, or the program's last drop stopped the holds.
|
||||
pub(crate) fn hold(&self) -> Option<Device> {
|
||||
let mut state = lock(&self.1.state);
|
||||
if state.stopped {
|
||||
return None;
|
||||
}
|
||||
let shared = self.0.upgrade()?;
|
||||
state.holders += 1;
|
||||
Some(Device(shared, Share(Some(Arc::clone(&self.1)))))
|
||||
}
|
||||
|
||||
/// The device for the program, as [`WeakDevice::upgrade`]: `None` where it went, or its
|
||||
/// holds were stopped, so that it goes.
|
||||
pub(super) fn revive(&self) -> Option<Device> {
|
||||
let state = lock(&self.1.state);
|
||||
if state.stopped {
|
||||
return None;
|
||||
}
|
||||
let shared = self.0.upgrade()?;
|
||||
drop(state);
|
||||
Some(program(shared))
|
||||
}
|
||||
}
|
||||
|
||||
impl Device {
|
||||
/// Whether `holder`, which holds the device through [`WeakDevice::hold`], goes on with its
|
||||
/// next step: `false` once the program's last drop stopped the holds.
|
||||
pub(crate) fn holds_go_on(&self, holder: Holder) -> bool {
|
||||
let holds = &self.0.holds;
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
holds.held(holder).pass();
|
||||
let _ = holder;
|
||||
!holds.stopping()
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Device {
|
||||
fn clone(&self) -> Device {
|
||||
match &(self.1).0 {
|
||||
Some(holds) => {
|
||||
let mut state = lock(&holds.state);
|
||||
state.holders += 1;
|
||||
Device(Arc::clone(&self.0), Share(Some(Arc::clone(holds))))
|
||||
}
|
||||
None => program(Arc::clone(&self.0)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Device {
|
||||
// A program handle's drop: the program's last stops the holds and waits for them. A holder's
|
||||
// handle is counted gone by its `Share`, once the device's share went before it.
|
||||
fn drop(&mut self) {
|
||||
if (self.1).0.is_some() {
|
||||
return;
|
||||
}
|
||||
let holds = &self.0.holds;
|
||||
let mut state = lock(&holds.state);
|
||||
if holds.program.fetch_sub(1, Ordering::SeqCst) > 1 {
|
||||
return;
|
||||
}
|
||||
state.stopped = true;
|
||||
holds.stopping.store(true, Ordering::SeqCst);
|
||||
if state.holders == 0 || exit_guard::on_runtime_thread() {
|
||||
return;
|
||||
}
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
let waiting = std::time::Instant::now();
|
||||
while state.holders > 0 {
|
||||
state = (holds.released.wait(state)).unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
}
|
||||
#[cfg(all(test, any(m2v_gpu, m2v_cpu_icd)))]
|
||||
{
|
||||
let nanos = u64::try_from(waiting.elapsed().as_nanos()).unwrap_or(u64::MAX);
|
||||
holds.waited.store(nanos.max(1), Ordering::Release);
|
||||
}
|
||||
// The device's share goes as this returns, the last: the teardown runs here.
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Share {
|
||||
// A holder's handle is counted gone here, after the device's share it held went (fields go
|
||||
// in their order): the program's last drop, which waits for this, then holds the last share.
|
||||
fn drop(&mut self) {
|
||||
let Some(holds) = &self.0 else {
|
||||
return;
|
||||
};
|
||||
let mut state = lock(&holds.state);
|
||||
state.holders -= 1;
|
||||
if state.holders == 0 {
|
||||
holds.released.notify_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ pub(super) mod create;
|
||||
pub(super) mod float_tininess;
|
||||
pub(super) mod functions;
|
||||
pub(crate) mod handles;
|
||||
pub(crate) mod holds;
|
||||
pub(super) mod instance_requirements;
|
||||
pub(super) mod keeper;
|
||||
pub(super) mod prime;
|
||||
@@ -98,17 +99,32 @@ use std::time::Instant;
|
||||
use submit::{Internal, OwnTimeline};
|
||||
|
||||
/// A Metal device (`MTLDevice`) on a Vulkan device. Cheap to clone (a retain); `Send + Sync`.
|
||||
#[derive(Clone)]
|
||||
pub struct Device(Arc<DeviceShared>);
|
||||
///
|
||||
/// The program's last drop of its handles (the device's own and its objects'), on a thread of the
|
||||
/// program's, stops the runtime's own background work on the device and waits for it: at most the
|
||||
/// round of its check of textures over host pages in progress, or the record its warm pass is
|
||||
/// making ([`Device::warm`]). So the device is torn down on that thread before the drop returns,
|
||||
/// and a consumer that adopted the `VkDevice` ([`Device::from_raw`]) may destroy it then.
|
||||
pub struct Device(
|
||||
Arc<DeviceShared>,
|
||||
/// The handle's place in the registry of the runtime's own holds (`holds.rs`).
|
||||
holds::Share,
|
||||
);
|
||||
|
||||
/// A handle on a device that does not keep it ([`Device::downgrade`]).
|
||||
/// A handle on a device that does not keep it ([`Device::downgrade`]), with the registry of its
|
||||
/// holds (`holds.rs`).
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct WeakDevice(Weak<DeviceShared>);
|
||||
pub(crate) struct WeakDevice(Weak<DeviceShared>, Arc<holds::Holds>);
|
||||
|
||||
impl WeakDevice {
|
||||
/// The device, while it lives.
|
||||
pub(crate) fn upgrade(&self) -> Option<Device> {
|
||||
self.0.upgrade().map(Device)
|
||||
holds::upgrade(self)
|
||||
}
|
||||
|
||||
/// Whether the device lives.
|
||||
pub(super) fn lives(&self) -> bool {
|
||||
self.0.strong_count() > 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -562,6 +578,9 @@ pub(super) struct DeviceShared {
|
||||
pub(crate) pipeline_recording: crate::warm::Recording,
|
||||
/// Its warm calls: the records queued, its warm thread, and the counts (`warm/pass.rs`).
|
||||
pub(crate) warmer: crate::warm::Warmer,
|
||||
/// The registry of the runtime's own holds of the device, which its handles share
|
||||
/// (`holds.rs`).
|
||||
pub(crate) holds: Arc<holds::Holds>,
|
||||
}
|
||||
|
||||
impl DeviceShared {
|
||||
@@ -1060,7 +1079,7 @@ impl Device {
|
||||
|
||||
/// A handle on the device that does not keep it.
|
||||
pub(crate) fn downgrade(&self) -> WeakDevice {
|
||||
WeakDevice(Arc::downgrade(&self.0))
|
||||
WeakDevice(Arc::downgrade(&self.0), Arc::clone(&self.0.holds))
|
||||
}
|
||||
|
||||
/// The compile-and-plan device this device compiles and plans for. No Metal counterpart.
|
||||
@@ -1926,6 +1945,7 @@ impl Device {
|
||||
views_epoch: AtomicU64::new(0),
|
||||
pipeline_recording: crate::warm::Recording::new(pipeline_records),
|
||||
warmer: crate::warm::Warmer::default(),
|
||||
holds: holds::holds(),
|
||||
device_address,
|
||||
upload_alignment,
|
||||
dynamic_buffers: [
|
||||
@@ -2073,7 +2093,7 @@ impl Device {
|
||||
shared.scheduler.take_stalls_idle(idle);
|
||||
}
|
||||
}
|
||||
let device = Device(shared);
|
||||
let device = holds::first(shared);
|
||||
// What the device allocated ahead goes back once its allocations come near the limit.
|
||||
device.give_back_ahead_near_the_limit();
|
||||
// The verdicts of its check of textures over host pages, where a record holds them.
|
||||
|
||||
@@ -639,6 +639,11 @@ thread_local! {
|
||||
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`]).
|
||||
|
||||
@@ -592,6 +592,11 @@ pub(crate) mod held {
|
||||
}
|
||||
}
|
||||
|
||||
/// How many wait at the hold now.
|
||||
pub(crate) fn waiting(&self) -> usize {
|
||||
lock(&self.state).1
|
||||
}
|
||||
|
||||
/// As a watch or release begins: wait while held, counted.
|
||||
pub(crate) fn pass(&self) {
|
||||
let mut state = lock(&self.state);
|
||||
|
||||
@@ -40,14 +40,18 @@
|
||||
//! device's life. A forced failure is kept nowhere.
|
||||
//! - **Its thread.** Each round's Vulkan work (the buffer, the texture, the submission and its
|
||||
//! wait, the releases) runs inside a section of the exit guard (`exit_guard.rs`), as every
|
||||
//! runtime thread's does; the check holds its device weakly between rounds, so a device the
|
||||
//! program lets go of goes, and the check ends unrecorded.
|
||||
//! runtime thread's does. It holds its device through the registry of the runtime's own holds
|
||||
//! for each class (`device/holds.rs`), weakly between classes, and reads the stop before each
|
||||
//! round: the program's last drop stops it there and waits for its round in progress and its
|
||||
//! release of its buffer, so the device goes on the program's thread, and the check ends
|
||||
//! unrecorded.
|
||||
//! - **Its dma-bufs.** The runtime runs no `udmabuf` code, so the dma-buf class's pages are an
|
||||
//! allocation of the device's own, host-visible and exported as a dma-buf, mapped: the device
|
||||
//! refuses their pointer (a driver that takes it leaves the class unchecked), and the check's
|
||||
//! source answers each request over them with that dma-buf. The image and its first move are
|
||||
//! the product's.
|
||||
|
||||
use crate::device::holds::Holder;
|
||||
use crate::exit_guard;
|
||||
use crate::host_memory::HostMemory;
|
||||
use crate::in_place::{encode, mark_checking, record_name, PagesClass, Verdict};
|
||||
@@ -150,21 +154,21 @@ fn run(weak: crate::device::WeakDevice) {
|
||||
mark_checking();
|
||||
let started = Instant::now();
|
||||
for class in PagesClass::ALL {
|
||||
let Some(device) = weak.upgrade() else {
|
||||
let Some(device) = weak.hold() else {
|
||||
return;
|
||||
};
|
||||
if device.shared().in_place.verdict(class) != Verdict::Pending {
|
||||
continue;
|
||||
}
|
||||
let Some(verdict) = check_class(&device, class) else {
|
||||
// The process is exiting: nothing is kept.
|
||||
// The process is exiting, or the program let the device go: nothing is kept.
|
||||
return;
|
||||
};
|
||||
let checks = &device.shared().in_place;
|
||||
checks.set(class, verdict);
|
||||
device.shared().counts.in_place_check_ran(verdict);
|
||||
}
|
||||
let Some(device) = weak.upgrade() else {
|
||||
let Some(device) = weak.hold() else {
|
||||
return;
|
||||
};
|
||||
let checks = &device.shared().in_place;
|
||||
@@ -183,7 +187,8 @@ fn run(weak: crate::device::WeakDevice) {
|
||||
}
|
||||
|
||||
/// The verdict of `class` on `device`: every shape's rounds, until one fails. `None` where the
|
||||
/// process is exiting.
|
||||
/// process is exiting, or the program's last drop stopped the device's holds before a round
|
||||
/// (`device/holds.rs`).
|
||||
fn check_class(device: &Device, class: PagesClass) -> Option<Verdict> {
|
||||
let page = host_page();
|
||||
let Some(pages) = ClassPages::make(device, class, BUFFER_BYTES) else {
|
||||
@@ -195,6 +200,9 @@ fn check_class(device: &Device, class: PagesClass) -> Option<Verdict> {
|
||||
continue;
|
||||
}
|
||||
for round in 0..ROUNDS {
|
||||
if !device.holds_go_on(Holder::InPlaceCheck) {
|
||||
return None;
|
||||
}
|
||||
let outcome = {
|
||||
let _busy = exit_guard::enter()?;
|
||||
check_round(&pages, shape, round, page)
|
||||
|
||||
@@ -49,6 +49,12 @@
|
||||
//! most one step and its release of its objects, within the 16 ms the owner allows a wait. A
|
||||
//! device that runs no check (one family of one `VkQueue`, or no calibrated timestamps) skips
|
||||
//! it, saying so.
|
||||
//! - **Not the runtime's own holders' threads**: the check of textures over host pages
|
||||
//! (`in_place_check.rs`), held before a round while it holds the device, and the warm pass
|
||||
//! (`warm/pass.rs`), held before a record while it holds it. The program's last drop stops the
|
||||
//! holds and waits for them (`device/holds.rs`), so the device is torn down on the program's
|
||||
//! thread before that drop returns. A device whose check makes no round (its policy takes no
|
||||
//! texture in place, or it makes no class's pages) skips the check's case, saying so.
|
||||
//!
|
||||
//! The teardown's end is seen through the device's message sink, whose last share the instance
|
||||
//! drops after `vkDestroyInstance`, the teardown's last step: the sink owns a value that, dropped,
|
||||
@@ -61,6 +67,7 @@
|
||||
use super::dispatch_pending as pending;
|
||||
use super::freed_memory::{unused, Host, Runs};
|
||||
use super::layer_claim::Watched;
|
||||
use crate::device::holds::Holder;
|
||||
use crate::device::lock;
|
||||
use crate::device::prime::SHADER;
|
||||
use crate::exit_guard;
|
||||
@@ -614,3 +621,110 @@ fn a_running_queue_switch_check_leaves_the_device_to_the_program_s_thread() {
|
||||
.watched
|
||||
.layer_reported_nothing("the teardown that stopped the check left no object and used none");
|
||||
}
|
||||
|
||||
/// The device whose holds `holder` takes, `holder` held at its next step while it holds the device
|
||||
/// (`device/holds.rs`); `start` sets it going. `None` where it ended without reaching the hold,
|
||||
/// which `ended` answers.
|
||||
fn held_at_a_step(
|
||||
device: &Device,
|
||||
holder: Holder,
|
||||
start: impl FnOnce(),
|
||||
ended: impl Fn() -> bool,
|
||||
) -> Option<Arc<crate::device::holds::Holds>> {
|
||||
let holds = Arc::clone(&device.shared().holds);
|
||||
let held = holds.held(holder);
|
||||
held.hold();
|
||||
start();
|
||||
let started = Instant::now();
|
||||
while held.waiting() == 0 {
|
||||
if ended() {
|
||||
held.release();
|
||||
return None;
|
||||
}
|
||||
assert!(
|
||||
started.elapsed() < WAIT,
|
||||
"{holder:?} never reached its hold"
|
||||
);
|
||||
std::thread::sleep(Duration::from_millis(1));
|
||||
}
|
||||
Some(holds)
|
||||
}
|
||||
|
||||
/// Drop `device`, the program's last handle, while `holder` holds it at its hold, letting the
|
||||
/// hold go once the drop stopped the holds: the device is torn down on this thread before the
|
||||
/// drop returns, having waited for the holder, and nothing failed meanwhile.
|
||||
fn dropped_while_held(
|
||||
device: Device,
|
||||
ending: &Ending,
|
||||
holds: &Arc<crate::device::holds::Holds>,
|
||||
holder: Holder,
|
||||
) {
|
||||
let releasing = std::thread::spawn({
|
||||
let holds = Arc::clone(holds);
|
||||
move || holds.release_once_stopping(holder)
|
||||
});
|
||||
let dropping = Instant::now();
|
||||
drop(device);
|
||||
let dropped = dropping.elapsed();
|
||||
let torn_down = ending.thread.try_recv();
|
||||
holds.held(holder).release();
|
||||
releasing.join().unwrap();
|
||||
assert_eq!(
|
||||
torn_down,
|
||||
Ok(program_thread()),
|
||||
"the device was not torn down on this thread before its last drop returned"
|
||||
);
|
||||
let waited = holds.waited().expect("the last drop waited for the holder");
|
||||
println!("the last drop took {dropped:?}, of which the wait for {holder:?} {waited:?}");
|
||||
ending.watched.layer_reported_nothing(&format!(
|
||||
"the teardown that stopped {holder:?} left no object and used none"
|
||||
));
|
||||
}
|
||||
|
||||
/// A device whose check of textures over host pages runs (`in_place_check.rs`), held before one
|
||||
/// of its rounds, while it holds the device and its class's buffer: the program's last drop stops
|
||||
/// the check there and waits for it to let them go, so the device is torn down on the program's
|
||||
/// thread before the drop returns. Without the wait the check's thread lets the device go last,
|
||||
/// after the drop returned.
|
||||
#[test]
|
||||
fn a_running_in_place_check_leaves_the_device_to_the_program_s_thread() {
|
||||
let scratch = Scratch::new("in-place");
|
||||
let (device, ending) = watched(&scratch.0);
|
||||
let policy = device.compile_device().policy().host_page_textures;
|
||||
if policy != metal2vulkan::device::HostPageTexturePolicy::InPlace {
|
||||
println!(
|
||||
"SKIPPED {}: the device takes no texture in place ({policy:?})",
|
||||
device.name()
|
||||
);
|
||||
return;
|
||||
}
|
||||
let checks = |device: &Device| device.shared().in_place.wait(Duration::ZERO);
|
||||
let start = || {
|
||||
device.settle_in_place_checks(Duration::ZERO);
|
||||
};
|
||||
let Some(holds) = held_at_a_step(&device, Holder::InPlaceCheck, start, || checks(&device))
|
||||
else {
|
||||
println!(
|
||||
"SKIPPED {}: the device's check of textures in place made no round ({:?})",
|
||||
device.name(),
|
||||
device.in_place_check_report()
|
||||
);
|
||||
return;
|
||||
};
|
||||
dropped_while_held(device, &ending, &holds, Holder::InPlaceCheck);
|
||||
}
|
||||
|
||||
/// A device's warm pass over records an earlier device made over the same directory, held before
|
||||
/// its first record while it holds the device (`warm/pass.rs`): the program's last drop stops the
|
||||
/// pass there and waits for it to let the device go, so the device is torn down on the program's
|
||||
/// thread before the drop returns.
|
||||
#[test]
|
||||
fn a_running_warm_pass_leaves_the_device_to_the_program_s_thread() {
|
||||
let scratch = Scratch::new("warm");
|
||||
let records = super::warm::recorded(&scratch.0);
|
||||
let (device, ending) = watched(&scratch.0);
|
||||
let start = || device.warm(records, u64::MAX);
|
||||
let holds = held_at_a_step(&device, Holder::WarmPass, start, || false)
|
||||
.expect("the warm pass reaches its hold");
|
||||
dropped_while_held(device, &ending, &holds, Holder::WarmPass);
|
||||
}
|
||||
@@ -161,7 +161,7 @@ fn make(
|
||||
|
||||
/// The records a device over `cache` hands over for [`make_states`]; the device is gone when
|
||||
/// this returns, so its cache thread wrote what it was handed.
|
||||
fn recorded(cache: &Path) -> Vec<PipelineRecord> {
|
||||
pub(super) fn recorded(cache: &Path) -> Vec<PipelineRecord> {
|
||||
let records = Arc::new(Mutex::new(Vec::new()));
|
||||
let device = device(cache, Some(&records), None);
|
||||
drop(make_states(&device));
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
//! the utility class (`exit_guard::lower_to_utility`); its pipeline makings rank below every
|
||||
//! other making of the process ([`Urgency::Warm`](crate::pipeline::budget::Urgency::Warm)),
|
||||
//! on its thread and in the pieces it hands the device's pool, which run as part of the pass
|
||||
//! ([`within`]). It holds its device only while it warms a record, so a device the program let
|
||||
//! go goes, and the pass with it.
|
||||
//! ([`within`]). It holds its device only while it warms a record, through the registry of the
|
||||
//! runtime's own holds (`device/holds.rs`), and reads the stop before each: the program's last
|
||||
//! drop stops the pass there and waits for the record being made, so the device goes on the
|
||||
//! program's thread, and the pass with it.
|
||||
//! - **The creation itself** (`pipeline::warm`). Each record is read back (its libraries from the
|
||||
//! shader cache directory, `shader_cache/libraries.rs`, unless it was made in this process) and
|
||||
//! created as the program created it, under the default options, inside one exit-guard section;
|
||||
@@ -41,6 +43,7 @@
|
||||
|
||||
use super::codec;
|
||||
use super::{PipelineRecord, WarmStatistics};
|
||||
use crate::device::holds::Holder;
|
||||
use crate::device::{lock, Device, WeakDevice};
|
||||
use crate::exit_guard;
|
||||
use metal2vulkan::cache::Hash256;
|
||||
@@ -261,7 +264,7 @@ impl Device {
|
||||
fn run(device: &WeakDevice) {
|
||||
exit_guard::lower_to_utility();
|
||||
loop {
|
||||
let Some(held) = device.upgrade() else {
|
||||
let Some(held) = device.hold() else {
|
||||
return;
|
||||
};
|
||||
let call = {
|
||||
@@ -289,9 +292,12 @@ fn warm_call(device: &WeakDevice, call: Call) {
|
||||
} = call;
|
||||
let total = records.len();
|
||||
for (at, record) in records.iter().enumerate() {
|
||||
let Some(held) = device.upgrade() else {
|
||||
let Some(held) = device.hold() else {
|
||||
return;
|
||||
};
|
||||
if !held.holds_go_on(Holder::WarmPass) {
|
||||
return;
|
||||
}
|
||||
let warmer = &held.shared().warmer;
|
||||
if pass.bytes.load(Ordering::Relaxed) >= byte_budget {
|
||||
warmer.not_run(total - at);
|
||||
|
||||
Reference in new issue
Block a user