stackler 0.1.0

A lightweight, always-on stack telemetry library that requires no instrumentation.
Documentation
//! Runtime configuration, kept in relaxed atomics so that reading it on a
//! measurement path costs a single load.

use core::sync::atomic::Ordering;

use portable_atomic::AtomicUsize;

/// The number of bytes [`crate::attach`] paints below the stack pointer unless
/// configured otherwise.
///
/// Deep enough to cover the stack use of most threads, small enough that
/// committing it per thread stays unremarkable. Raise it if peaks come back
#[cfg_attr(feature = "watermark", doc = "[saturated](crate::Peak::AtLeast).")]
#[cfg_attr(not(feature = "watermark"), doc = "saturated.")]
pub const DEFAULT_PAINT_DEPTH: usize = 256 * 1024;

/// The headroom below which an observation is considered to be near the limit,
/// unless configured otherwise.
pub const DEFAULT_RESERVE: usize = 64 * 1024;

static PAINT_DEPTH: AtomicUsize = AtomicUsize::new(DEFAULT_PAINT_DEPTH);
static RESERVE: AtomicUsize = AtomicUsize::new(DEFAULT_RESERVE);

#[cfg(feature = "watermark")]
#[inline]
pub(crate) fn paint_depth() -> usize {
    PAINT_DEPTH.load(Ordering::Relaxed)
}

#[inline]
pub(crate) fn reserve() -> usize {
    RESERVE.load(Ordering::Relaxed)
}

pub(crate) fn set_paint_depth(bytes: usize) {
    PAINT_DEPTH.store(bytes, Ordering::Relaxed);
}

pub(crate) fn set_reserve(bytes: usize) {
    RESERVE.store(bytes, Ordering::Relaxed);
}