use core::marker::PhantomData;
#[cfg(all(feature = "probe", not(target_family = "wasm")))]
mod imp {
use std::cell::RefCell;
use std::time::Instant;
thread_local! {
static EVENTS: RefCell<Vec<super::Event>> = const { RefCell::new(Vec::new()) };
}
pub struct Span {
pub(crate) name: &'static str,
pub(crate) start: Instant,
}
impl Drop for Span {
fn drop(&mut self) {
let dur_ns = self.start.elapsed().as_nanos() as u64;
let _ = EVENTS.try_with(|cell| {
cell.borrow_mut().push(super::Event {
name: self.name,
kind: super::EventKind::Span { dur_ns },
});
});
}
}
pub(super) fn open(name: &'static str) -> Span {
Span { name, start: Instant::now() }
}
pub(super) fn sample_rss(label: &'static str, bytes: u64) {
let _ = EVENTS.try_with(|cell| {
cell.borrow_mut().push(super::Event {
name: label,
kind: super::EventKind::Rss { bytes },
});
});
}
pub(super) fn drain() -> Vec<super::Event> {
EVENTS
.try_with(|cell| core::mem::take(&mut *cell.borrow_mut()))
.unwrap_or_default()
}
pub(super) fn drop_events() {
let _ = EVENTS.try_with(|cell| cell.borrow_mut().clear());
}
pub(super) fn peek_len() -> usize {
EVENTS.try_with(|cell| cell.borrow().len()).unwrap_or(0)
}
pub(super) fn enabled() -> bool {
true
}
}
#[cfg(any(not(feature = "probe"), target_family = "wasm"))]
mod imp {
pub struct Span;
impl Drop for Span {
#[inline(always)]
fn drop(&mut self) {}
}
#[inline(always)]
pub(super) fn open(_name: &'static str) -> Span {
Span
}
#[inline(always)]
pub(super) fn sample_rss(_label: &'static str, _bytes: u64) {}
#[inline(always)]
pub(super) fn drain() -> Vec<super::Event> {
Vec::new()
}
#[inline(always)]
pub(super) fn drop_events() {}
#[inline(always)]
pub(super) fn peek_len() -> usize { 0 }
#[inline(always)]
pub(super) fn enabled() -> bool {
false
}
}
#[derive(Debug, Clone)]
pub struct Event {
pub name: &'static str,
pub kind: EventKind,
}
#[derive(Debug, Clone)]
pub enum EventKind {
Span { dur_ns: u64 },
Rss { bytes: u64 },
}
pub use imp::Span;
pub struct Probe {
_no_construct: PhantomData<()>,
}
impl Probe {
#[inline(always)]
pub fn span(name: &'static str) -> Span {
imp::open(name)
}
#[inline(always)]
pub fn sample_rss(label: &'static str, bytes: u64) {
imp::sample_rss(label, bytes);
}
#[inline(always)]
pub fn drain() -> Vec<Event> {
imp::drain()
}
#[inline(always)]
pub fn drop_events() {
imp::drop_events();
}
#[inline(always)]
pub fn peek_len() -> usize {
imp::peek_len()
}
#[inline(always)]
pub fn enabled() -> bool {
imp::enabled()
}
}
#[inline]
pub fn monotonic_now_nanos() -> u64 {
use std::sync::OnceLock;
use std::time::Instant;
static LAUNCH: OnceLock<Instant> = OnceLock::new();
let start = LAUNCH.get_or_init(Instant::now);
start.elapsed().as_nanos() as u64
}
pub fn print_drained_events(label: &str, events: &[Event]) {
use std::collections::BTreeMap;
if events.is_empty() {
if !Probe::enabled() {
eprintln!(
"[CPU] {label}: probe unavailable on this target (timings = ???)"
);
} else {
eprintln!("[CPU] {label}: no events recorded this pass");
}
return;
}
let mut spans: BTreeMap<&'static str, Vec<u64>> = BTreeMap::new();
let mut rss_marks: Vec<(&'static str, u64)> = Vec::new();
for ev in events {
match ev.kind {
EventKind::Span { dur_ns } => spans.entry(ev.name).or_default().push(dur_ns),
EventKind::Rss { bytes } => rss_marks.push((ev.name, bytes)),
}
}
let mut rows: Vec<(&'static str, usize, u64, u64, u64, u64)> = spans
.into_iter()
.map(|(name, mut ns)| {
ns.sort_unstable();
let n = ns.len();
let total: u128 = ns.iter().map(|&x| x as u128).sum();
let avg = (total / n.max(1) as u128) as u64;
let p99 = ns[(n.saturating_sub(1) * 99) / 100];
let max = *ns.last().unwrap();
(name, n, total as u64, avg, p99, max)
})
.collect();
rows.sort_by(|a, b| b.2.cmp(&a.2));
eprintln!("[CPU] === {label} ({} phases) ===", rows.len());
eprintln!(
"[CPU] {:<28} {:>5} {:>10} {:>9} {:>9} {:>9}",
"phase", "n", "total(µs)", "avg(µs)", "p99(µs)", "max(µs)"
);
for (name, n, total, avg, p99, max) in &rows {
eprintln!(
"[CPU] {:<28} {:>5} {:>10.1} {:>9.2} {:>9.2} {:>9.2}",
name,
n,
(*total as f64) / 1_000.0,
(*avg as f64) / 1_000.0,
(*p99 as f64) / 1_000.0,
(*max as f64) / 1_000.0,
);
}
if !rss_marks.is_empty() {
eprintln!("[CPU] -- RSS checkpoints (wall-clock order) --");
let mut prev: Option<u64> = None;
for (lbl, bytes) in &rss_marks {
let delta = prev
.map(|p| {
let diff = *bytes as i128 - p as i128;
if diff >= 0 {
format!(" (Δ +{:.2} MiB)", diff as f64 / 1048576.0)
} else {
format!(" (Δ -{:.2} MiB)", -diff as f64 / 1048576.0)
}
})
.unwrap_or_default();
eprintln!(
"[CPU] {:<28} {:.2} MiB{}",
lbl,
*bytes as f64 / 1048576.0,
delta
);
prev = Some(*bytes);
}
}
}
#[inline]
pub fn sample_peak_rss(label: &'static str) {
#[cfg(feature = "probe")]
{
let (current, _virt) = current_rss_bytes();
let bytes = if current != 0 { current } else { peak_rss_bytes_self() };
Probe::sample_rss(label, bytes);
}
#[cfg(not(feature = "probe"))]
let _ = label;
}
#[cfg(feature = "probe")]
pub fn peak_rss_bytes_pub() -> u64 { peak_rss_bytes_self() }
#[cfg(feature = "probe")]
fn peak_rss_bytes_self() -> u64 {
#[cfg(unix)]
unsafe {
let mut ru: libc::rusage = core::mem::zeroed();
if libc::getrusage(libc::RUSAGE_SELF, &mut ru) != 0 {
return 0;
}
let raw = ru.ru_maxrss as u64;
if cfg!(target_os = "macos") { raw } else { raw.saturating_mul(1024) }
}
#[cfg(not(unix))]
{
0
}
}
#[inline]
pub fn hint_purge_allocator() {
#[cfg(feature = "allocator_mimalloc")]
{
unsafe {
libmimalloc_sys::mi_collect(true);
}
static PURGE_TRACE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if *PURGE_TRACE.get_or_init(azul_core::profile::memory_enabled) {
let (rss, _) = current_rss_bytes();
eprintln!("[PURGE] mi_collect(true) called — current rss={:.2} MiB", rss as f64 / 1048576.0);
}
return;
}
#[cfg(feature = "allocator_jemalloc")]
{
unsafe {
let _ = tikv_jemalloc_sys::mallctl(
b"arena.4096.purge\0".as_ptr() as *const _,
core::ptr::null_mut(),
core::ptr::null_mut(),
core::ptr::null_mut(),
0,
);
}
return;
}
#[cfg(all(target_os = "macos", not(any(feature = "allocator_mimalloc", feature = "allocator_jemalloc"))))]
{
extern "C" {
fn malloc_zone_pressure_relief(zone: *mut core::ffi::c_void, goal: usize) -> usize;
}
unsafe {
malloc_zone_pressure_relief(core::ptr::null_mut(), 0);
}
}
}
#[cfg(feature = "probe")]
pub fn current_rss_bytes() -> (u64, u64) {
#[cfg(target_os = "macos")]
{
let pf = phys_footprint_bytes();
#[repr(C)]
struct MachTaskBasicInfo {
virtual_size: u64,
resident_size: u64,
resident_size_max: u64,
user_time: [u32; 2],
system_time: [u32; 2],
policy: i32,
suspend_count: i32,
}
const MACH_TASK_BASIC_INFO: u32 = 20;
extern "C" {
fn mach_task_self() -> u32;
fn task_info(
target: u32, flavor: u32,
info: *mut core::ffi::c_void, count: *mut u32,
) -> i32;
}
unsafe {
let mut info: MachTaskBasicInfo = core::mem::zeroed();
let mut count = (core::mem::size_of::<MachTaskBasicInfo>() / 4) as u32;
let kr = task_info(
mach_task_self(),
MACH_TASK_BASIC_INFO,
&mut info as *mut _ as *mut core::ffi::c_void,
&mut count,
);
if kr == 0 {
let rss = if pf != 0 { pf } else { info.resident_size };
(rss, info.virtual_size)
} else {
(pf, 0)
}
}
}
#[cfg(not(target_os = "macos"))]
{ (0, 0) }
}
#[cfg(feature = "probe")]
pub fn malloc_heap_bytes() -> u64 {
#[cfg(target_os = "macos")]
{
#[repr(C)]
struct Mstats {
bytes_total: usize,
chunks_used: usize,
bytes_used: usize,
chunks_free: usize,
bytes_free: usize,
}
extern "C" {
fn mstats() -> Mstats;
}
unsafe { mstats().bytes_used as u64 }
}
#[cfg(not(target_os = "macos"))]
{ 0 }
}
#[cfg(feature = "probe")]
pub fn phys_footprint_bytes() -> u64 {
#[cfg(target_os = "macos")]
{
#[repr(C)]
struct TaskVmInfo {
virtual_size: u64,
region_count: u32,
page_size: u32,
resident_size: u64,
resident_size_peak: u64,
device: u64,
device_peak: u64,
internal: u64,
internal_peak: u64,
external: u64,
external_peak: u64,
reusable: u64,
reusable_peak: u64,
purgeable_volatile_pmap: u64,
purgeable_volatile_resident: u64,
purgeable_volatile_virtual: u64,
compressed: u64,
compressed_peak: u64,
compressed_lifetime: u64,
phys_footprint: u64,
_rest: [u64; 12],
}
const TASK_VM_INFO: u32 = 22;
extern "C" {
fn mach_task_self() -> u32;
fn task_info(
target: u32, flavor: u32,
info: *mut core::ffi::c_void, count: *mut u32,
) -> i32;
}
unsafe {
let mut info: TaskVmInfo = core::mem::zeroed();
let mut count = (core::mem::size_of::<TaskVmInfo>() / 4) as u32;
let kr = task_info(
mach_task_self(),
TASK_VM_INFO,
&mut info as *mut _ as *mut core::ffi::c_void,
&mut count,
);
if kr == 0 { info.phys_footprint } else { 0 }
}
}
#[cfg(not(target_os = "macos"))]
{ 0 }
}
#[cfg(feature = "probe")]
pub fn start_peak_sampler() {
#[cfg(target_os = "macos")]
{
use std::sync::atomic::Ordering;
static STARTED: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
if STARTED.swap(true, Ordering::AcqRel) {
return;
}
std::thread::Builder::new()
.name("azul-peak-sampler".to_string())
.spawn(|| loop {
let now = phys_footprint_bytes();
let prev = PEAK_PHYS_FOOTPRINT.load(Ordering::Relaxed);
if now > prev {
PEAK_PHYS_FOOTPRINT.store(now, Ordering::Relaxed);
}
std::thread::sleep(std::time::Duration::from_micros(250));
})
.ok();
}
}
#[cfg(feature = "probe")]
static PEAK_PHYS_FOOTPRINT: std::sync::atomic::AtomicU64 =
std::sync::atomic::AtomicU64::new(0);
#[cfg(feature = "probe")]
pub fn peak_phys_footprint_seen() -> u64 {
PEAK_PHYS_FOOTPRINT.load(std::sync::atomic::Ordering::Relaxed)
}
#[cfg(feature = "probe")]
pub fn reset_peak() {
let now = phys_footprint_bytes();
PEAK_PHYS_FOOTPRINT.store(now, std::sync::atomic::Ordering::Relaxed);
}
#[cfg(feature = "probe")]
#[inline]
pub fn sample_phase_peak(label: &'static str) {
let peak = PEAK_PHYS_FOOTPRINT.load(std::sync::atomic::Ordering::Relaxed);
Probe::sample_rss(label, peak);
}
#[cfg(not(feature = "probe"))]
#[inline(always)]
pub fn reset_peak() {}
#[cfg(not(feature = "probe"))]
#[inline(always)]
pub fn sample_phase_peak(_label: &'static str) {}
#[cfg(not(feature = "probe"))]
#[inline(always)]
pub fn malloc_heap_bytes() -> u64 { 0 }
#[cfg(feature = "probe")]
pub fn emit_phase_heap(label: &str) {
use std::io::Write;
if !heap_jsonl_enabled() { return; }
let Some(p) = azul_core::profile::out_path() else { return };
static CALL_ID: std::sync::atomic::AtomicU64 =
std::sync::atomic::AtomicU64::new(0);
static CURRENT_CALL: std::sync::atomic::AtomicU64 =
std::sync::atomic::AtomicU64::new(0);
let call_id = if label == "start" {
let next = CALL_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
CURRENT_CALL.store(next, std::sync::atomic::Ordering::Relaxed);
next
} else {
CURRENT_CALL.load(std::sync::atomic::Ordering::Relaxed)
};
let heap = malloc_heap_bytes();
if let Ok(mut f) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(p)
{
let _ = writeln!(
f,
r#"{{"ev":"phase","call":{},"label":"{}","heap":{}}}"#,
call_id, label, heap
);
}
}
#[cfg(not(feature = "probe"))]
#[inline(always)]
pub fn emit_phase_heap(_label: &str) {}
#[cfg(feature = "probe")]
pub fn emit_phase_heap_extra(label: &str, extra: u64) {
use std::io::Write;
if !heap_jsonl_enabled() { return; }
if !azul_core::profile::detail_enabled() { return; }
let Some(p) = azul_core::profile::out_path() else { return };
let heap = malloc_heap_bytes();
if let Ok(mut f) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(p)
{
let _ = writeln!(
f,
r#"{{"ev":"phase","call":0,"label":"{}","heap":{},"extra":{}}}"#,
label, heap, extra
);
}
}
#[cfg(not(feature = "probe"))]
#[inline(always)]
pub fn emit_phase_heap_extra(_label: &str, _extra: u64) {}
#[cfg(feature = "probe")]
#[inline]
fn heap_jsonl_enabled() -> bool {
let f = azul_core::profile::flags();
f.heap && f.jsonl
}
#[cfg(feature = "probe")]
#[inline]
pub fn detail_enabled() -> bool {
azul_core::profile::detail_enabled()
}
#[cfg(not(feature = "probe"))]
#[inline(always)]
pub fn detail_enabled() -> bool { false }