On the Iris Xe (ANV, Mesa 26.2.6) two runtime tests aborted with "has overflowed its stack":
- tests::partial_threadgroups::a_heavy_kernel_s_ragged_grids_pay_one_creation, on a pipeline pool
thread (std's default 2 MiB stack). Under gdb (m3-max-33742) the thread held 25,972 frames and
2,090,624 bytes when it overflowed, 25,922 of them one function of libvulkan_intel.so, 80 bytes
each: ANV's compiler recurses about 80 bytes deep for each value of a chain of dependent
values. The kernel's chain is 40,000 values long, so its vkCreateComputePipelines takes about
3.2 MB. Our frames above the driver's entry point there are 13 KiB.
- tests::small_stack_device::a_device_is_made_used_and_released_on_a_thread_with_a_small_stack, on
a 256 KiB thread. Device creation's priming (prime.rs) compiles its kernel through
vkCreateComputePipelines on the caller's thread: 204 KiB of our frames in this debug build
(assemble 108,864 bytes, create_on 52,560, Primer::objects 24,688) and then ANV's compile, 52 KiB
of its frames (one of 49 KiB) when the stack ran out. vkCreateDevice itself took about 55 KiB,
and the fill, commit and wait at most 74 KiB of the thread (painted stack probe, 33742).
Metal compiles in a service of its own, so no program's stack takes its compiles. Every driver
call that may compile already goes through device/compiles.rs's at_user_initiated
(vkCreateDevice, vkCreateGraphicsPipelines, vkCreateComputePipelines, priming's included):
- this_thread::stack_room reads the room left on the calling thread's stack, where its stack ends
read once a thread: pthread_getattr_np + pthread_attr_getstack on Linux,
pthread_get_stackaddr_np - pthread_get_stacksize_np on macOS, GetCurrentThreadStackLimits on
Windows (gnullvm and msvc alike, windows-sys's Win32_System_Threading), none elsewhere.
- at_user_initiated runs the call on the asking thread only where it is at the user-initiated
class (macOS) and its stack has COMPILE_ROOM (4 MiB) left; else on its relay thread, which now
starts with COMPILE_STACK (8 MiB, glibc's default thread stack, which Mesa's own compile threads
use) on every OS. A Linux or macOS main thread still compiles in place; a Rust thread's 2 MiB,
a macOS secondary thread's 512 KiB and a Windows main thread's 1 MiB hand the call over, at one
thread start and join per call. The relay is the one macOS's class already used, unchanged: it
raises its class to user-initiated on macOS, makes the one call inside an exit-guard section
with the asking thread's measured diagnostics, holds the driver's messages for the asking
thread's sink, is joined before the call returns, resumes the call's panic on the asking
thread, and where no thread starts or the process is exiting the asking thread makes the call.
- The pipeline pool's threads start with COMPILE_STACK, so their pieces' makings run on them.
The stacks are address space; the system commits their pages as they are touched.
Tests: device::compiles::tests::a_pipeline_asked_for_on_a_small_stack_is_made_on_a_stack_with_the_room
(a fake driver reports the room and thread its call ran on: asked on 256 KiB and 2 MiB threads
it runs on the relay with at least 4 MiB left, on an 8 MiB thread on the asking thread; without
the room check it runs on the 256 KiB thread and fails), and
pipeline::pool::tests::a_piece_runs_on_a_stack_with_the_room_a_compile_takes. The existing tests'
asking thread now has the compile stack, so their in-place assertions still hold.
The relay test expects by the room the asking thread has: glibc reuses a cached stack of up to four times the size asked, so a 2 MiB thread may hold the 8 MiB a previous relay thread left and rightly compile in place; the 256 KiB thread must relay and the COMPILE_STACK thread must not.
Verified at a4d5ec0cdf: m3-max-33801 (msi) clippy -D warnings for metal2vulkan-runtime (all targets, all features) plain, x86_64-pc-windows-gnullvm, x86_64-pc-windows-msvc, gpucfg and cpuicd; device::compiles + pipeline::pool unit tests 14/14; the negative control (room check removed) fails at the 256 KiB thread ('made on the asking thread true, 251975 bytes of room'); lavapipe small_stack_device and the two non-heavy partial_threadgroups tests 3/3. Iris (m3-max-33783, the same stack code plus inert probe hooks): small_stack_device passes; the heavy kernel's vkCreateComputePipelines ran on an 8 MiB pool thread at 3,216,936 bytes deep (13,449 ours) without overflowing. The heavy test still takes ANV about 52 s a pipeline at 40,000 steps; its step count follows in its own commit.
140 lines
5.5 KiB
Rust
140 lines
5.5 KiB
Rust
//! The calling thread's identity, and the room left on its stack, read at any point of the
|
|
//! thread's life.
|
|
//!
|
|
//! A program's exit handlers run on the thread that called `exit`, after the C library destroyed
|
|
//! that thread's thread-locals (macOS's `exit` and the GNU C library's run their destructors
|
|
//! first): a thread-local that needs dropping is gone there, read with `with` it panics, and so
|
|
//! does `std::thread::current` once the thread's handle went with them. In an exit handler, which
|
|
//! cannot unwind, the panic ends the process. Each thread-local of the runtime's that a program's
|
|
//! call reaches is read with `try_with` or holds nothing to drop, and the calls ask this module,
|
|
//! not `std::thread::current`, which thread they run on.
|
|
|
|
use std::cell::Cell;
|
|
use std::thread::JoinHandle;
|
|
|
|
/// This thread's identity among the live threads: the address of a thread-local of nothing to
|
|
/// drop, which stays readable to the thread's end. No two live threads share one; a thread that
|
|
/// starts after another ended may take its address.
|
|
pub(crate) fn id() -> usize {
|
|
thread_local! {
|
|
static HERE: u8 = const { 0 };
|
|
}
|
|
HERE.with(|here| std::ptr::from_ref(here).addr())
|
|
}
|
|
|
|
/// The bytes of the calling thread's stack below the caller's frame: `None` where the system does
|
|
/// not say where the thread's stack ends, or once the thread's thread-locals are gone (an exit
|
|
/// handler). Where the stack ends is read once a thread, at its first ask.
|
|
pub(crate) fn stack_room() -> Option<usize> {
|
|
thread_local! {
|
|
/// The lowest address of this thread's stack, once read; nothing to drop.
|
|
static LOW: Cell<Option<Option<usize>>> = const { Cell::new(None) };
|
|
}
|
|
let low = LOW
|
|
.try_with(|low| {
|
|
let read = low.get().unwrap_or_else(stack_low);
|
|
low.set(Some(read));
|
|
read
|
|
})
|
|
.ok()??;
|
|
let here = 0u8;
|
|
std::ptr::from_ref(std::hint::black_box(&here))
|
|
.addr()
|
|
.checked_sub(low)
|
|
}
|
|
|
|
/// The lowest address of the calling thread's stack (`pthread_attr_getstack` of
|
|
/// `pthread_getattr_np`'s attributes); `None` where the system answers none.
|
|
#[cfg(any(target_os = "linux", target_os = "android"))]
|
|
fn stack_low() -> Option<usize> {
|
|
let mut attributes = std::mem::MaybeUninit::<libc::pthread_attr_t>::uninit();
|
|
// SAFETY: the calling thread's own attributes, read into `attributes`, which are destroyed
|
|
// once read.
|
|
unsafe {
|
|
if libc::pthread_getattr_np(libc::pthread_self(), attributes.as_mut_ptr()) != 0 {
|
|
return None;
|
|
}
|
|
let (mut low, mut size) = (std::ptr::null_mut(), 0);
|
|
let read = libc::pthread_attr_getstack(attributes.as_ptr(), &mut low, &mut size);
|
|
libc::pthread_attr_destroy(attributes.as_mut_ptr());
|
|
(read == 0).then(|| low.addr())
|
|
}
|
|
}
|
|
|
|
/// The lowest address of the calling thread's stack: its top
|
|
/// (`pthread_get_stackaddr_np`) less its size (`pthread_get_stacksize_np`).
|
|
#[cfg(target_vendor = "apple")]
|
|
fn stack_low() -> Option<usize> {
|
|
// SAFETY: reads of the calling thread's own stack's top and size; no pointer is followed.
|
|
let (top, size) = unsafe {
|
|
(
|
|
libc::pthread_get_stackaddr_np(libc::pthread_self()),
|
|
libc::pthread_get_stacksize_np(libc::pthread_self()),
|
|
)
|
|
};
|
|
top.addr().checked_sub(size)
|
|
}
|
|
|
|
/// The lowest address of the calling thread's stack (`GetCurrentThreadStackLimits`).
|
|
#[cfg(windows)]
|
|
fn stack_low() -> Option<usize> {
|
|
use windows_sys::Win32::System::Threading::GetCurrentThreadStackLimits;
|
|
let (mut low, mut high) = (0, 0);
|
|
// SAFETY: two out-parameters for the calling thread's stack limits.
|
|
unsafe { GetCurrentThreadStackLimits(&mut low, &mut high) };
|
|
(low != 0).then_some(low)
|
|
}
|
|
|
|
/// None: no answer here.
|
|
#[cfg(not(any(
|
|
target_os = "linux",
|
|
target_os = "android",
|
|
target_vendor = "apple",
|
|
windows
|
|
)))]
|
|
fn stack_low() -> Option<usize> {
|
|
None
|
|
}
|
|
|
|
/// Whether `handle`'s thread is this one: a thread of the runtime's own whose teardown it runs,
|
|
/// which it does not join.
|
|
pub(crate) fn runs<T>(handle: &JoinHandle<T>) -> bool {
|
|
#[cfg(unix)]
|
|
{
|
|
use std::os::unix::thread::JoinHandleExt;
|
|
// SAFETY: reads of two threads' identifiers; `handle`'s thread is neither joined nor
|
|
// detached while the handle lives.
|
|
unsafe { libc::pthread_equal(handle.as_pthread_t(), libc::pthread_self()) != 0 }
|
|
}
|
|
#[cfg(not(unix))]
|
|
{
|
|
handle.thread().id() == std::thread::current().id()
|
|
}
|
|
}
|
|
|
|
/// The calling thread's CPU time on Windows, in nanoseconds: its kernel and user times
|
|
/// (`GetThreadTimes`), each counted in 100 ns units; `None` where the system answers none.
|
|
#[cfg(windows)]
|
|
pub(crate) fn cpu_nanos() -> Option<u64> {
|
|
use windows_sys::Win32::Foundation::FILETIME;
|
|
use windows_sys::Win32::System::Threading::{GetCurrentThread, GetThreadTimes};
|
|
let (mut created, mut exited) = (FILETIME::default(), FILETIME::default());
|
|
let (mut kernel, mut user) = (FILETIME::default(), FILETIME::default());
|
|
// SAFETY: the pseudo-handle of the calling thread, and four valid `FILETIME`s to fill.
|
|
let read = unsafe {
|
|
GetThreadTimes(
|
|
GetCurrentThread(),
|
|
&mut created,
|
|
&mut exited,
|
|
&mut kernel,
|
|
&mut user,
|
|
)
|
|
};
|
|
if read == 0 {
|
|
return None;
|
|
}
|
|
let ticks =
|
|
|time: FILETIME| (u64::from(time.dwHighDateTime) << 32) | u64::from(time.dwLowDateTime);
|
|
ticks(kernel).checked_add(ticks(user))?.checked_mul(100)
|
|
}
|