use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering::Relaxed};
use std::time::{Duration, Instant};
pub const LONG_YIELD: Duration = Duration::from_millis(2);
pub const WINDOW: Duration = Duration::from_millis(10);
static EPOCH: OnceLock<Instant> = OnceLock::new();
static LAST_LONG_YIELD_NS: AtomicU64 = AtomicU64::new(0);
static LONG_YIELDS: AtomicU64 = AtomicU64::new(0);
pub const YIELD_BUCKETS: usize = 16;
static YIELD_HISTOGRAM: [AtomicU64; YIELD_BUCKETS] = [const { AtomicU64::new(0) }; YIELD_BUCKETS];
fn bucket_of(took: Duration) -> usize {
let us = took.as_micros();
let bits = (u128::BITS - us.leading_zeros()) as usize;
bits.min(YIELD_BUCKETS - 1)
}
fn now_ns() -> u64 {
let since = EPOCH.get_or_init(Instant::now).elapsed().as_nanos();
since.min(u128::from(u64::MAX - 1)) as u64 + 1
}
pub(crate) fn timed_yield() -> Duration {
let started = Instant::now();
std::thread::yield_now();
let took = started.elapsed();
YIELD_HISTOGRAM[bucket_of(took)].fetch_add(1, Relaxed);
if took >= LONG_YIELD {
LONG_YIELDS.fetch_add(1, Relaxed);
LAST_LONG_YIELD_NS.store(now_ns(), Relaxed);
}
took
}
fn within_window(last_ns: u64, now_ns: u64) -> bool {
last_ns != 0 && now_ns.saturating_sub(last_ns) < WINDOW.as_nanos() as u64
}
pub(crate) fn recently() -> bool {
within_window(LAST_LONG_YIELD_NS.load(Relaxed), now_ns())
}
pub fn total_long_yields() -> u64 {
LONG_YIELDS.load(Relaxed)
}
pub fn yield_histogram() -> [u64; YIELD_BUCKETS] {
std::array::from_fn(|i| YIELD_HISTOGRAM[i].load(Relaxed))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_long_yield_is_never_recent() {
assert!(!within_window(0, 1));
assert!(!within_window(0, u64::MAX));
}
#[test]
fn a_long_yield_counts_for_the_window_and_then_stops() {
let window = WINDOW.as_nanos() as u64;
let at = 5_000_000_000u64;
assert!(within_window(at, at));
assert!(within_window(at, at + window - 1));
assert!(!within_window(at, at + window));
assert!(!within_window(at, at + 10 * window));
}
#[test]
fn a_yield_lands_in_the_bucket_of_its_length() {
assert_eq!(bucket_of(Duration::from_nanos(900)), 0);
assert_eq!(bucket_of(Duration::from_micros(1)), 1);
assert_eq!(bucket_of(Duration::from_micros(3)), 2);
assert_eq!(bucket_of(Duration::from_micros(4)), 3);
assert_eq!(bucket_of(Duration::from_micros(100)), 7);
assert_eq!(bucket_of(Duration::from_millis(8)), 13);
assert_eq!(bucket_of(Duration::from_millis(16)), 14);
assert_eq!(bucket_of(Duration::from_millis(17)), YIELD_BUCKETS - 1);
assert_eq!(bucket_of(Duration::from_secs(10)), YIELD_BUCKETS - 1);
}
#[test]
fn a_reading_from_before_the_last_one_still_counts() {
let at = 5_000_000_000u64;
assert!(within_window(at, at - 1));
}
#[test]
fn a_timed_yield_comes_back_and_says_how_long_it_took() {
let took = timed_yield();
assert!(took < Duration::from_secs(1), "a single yield took {took:?}");
}
}