use std::sync::atomic::{AtomicU64, Ordering}; /// What is counted. #[derive(Clone, Copy)] pub(crate) enum Kind { /// Slices run. Slices, /// Slices run on another thread than the actor's previous one. Moved, /// Slices taken from a thread's next slot. Next, /// Slices stolen from another thread's queues. Stolen, /// Wake-ups queued on another thread (the actor's home). Sent, /// Times a thread parked. Parks, } const KINDS: usize = 6; /// What each [`Kind`] is called in the report. pub(crate) const NAMES: [&str; KINDS] = [ "slices", "slices on another thread than the last", "slices from the next slot", "slices stolen", "wake-ups queued on another thread", "parks", ]; static TOTALS: [AtomicU64; KINDS] = [const { AtomicU64::new(0) }; KINDS]; thread_local! { // Read and written only on the scheduler thread's own stack. static MINE: std::cell::Cell<[u64; KINDS]> = const { std::cell::Cell::new([0; KINDS]) }; } pub(crate) fn count(kind: Kind) { let mut mine = MINE.get(); mine[kind as usize] += 1; MINE.set(mine); } /// Add this thread's counts to the totals. pub(super) fn flush() { for (i, n) in MINE.replace([0; KINDS]).into_iter().enumerate() { TOTALS[i].fetch_add(n, Ordering::Relaxed); } } /// The totals, once the scheduler threads stopped. pub(crate) fn totals() -> [u64; KINDS] { std::array::from_fn(|i| TOTALS[i].load(Ordering::Relaxed)) }