use core::{cmp, ops::Sub};
#[cfg(feature = "histograms")]
use crate::Histogram;
use crate::registry;
#[derive(Debug, Default, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[non_exhaustive]
pub struct ThreadStats {
pub slot: usize,
pub id: u64,
pub base: usize,
pub limit: usize,
pub size: usize,
pub depth_last: usize,
pub peak: usize,
#[cfg(feature = "watermark")]
pub saturated: bool,
pub sample_count: u64,
pub sample_sum: u64,
pub near_limit_count: u64,
pub guard_trip_count: u64,
}
impl ThreadStats {
pub const fn stack(&self) -> Option<crate::Stack> {
crate::Stack::from_bounds(self.limit, self.base)
}
pub fn utilization(&self) -> Option<f64> {
if self.size == 0 {
return None;
}
Some(self.peak as f64 / self.size as f64)
}
}
#[cfg_attr(feature = "watermark", doc = " , [`Stats::saturated_count`])")]
#[cfg_attr(not(feature = "watermark"), doc = " )")]
#[derive(Debug, Default, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[non_exhaustive]
pub struct Stats {
pub threads_live: u64,
pub threads_seen: u64,
pub untracked: u64,
pub reserved_sum: u64,
pub reserved_max: usize,
pub peak_sum: u64,
pub peak_avg: Option<usize>,
pub peak_max: usize,
pub sample_count: u64,
pub sample_sum: u64,
pub sample_avg: Option<usize>,
#[cfg(feature = "histograms")]
pub depth_histogram: Histogram,
#[cfg(feature = "watermark")]
pub saturated_count: u64,
pub near_limit_count: u64,
pub guard_trip_count: u64,
}
impl Stats {
pub const fn waste(&self) -> u64 {
self.reserved_sum.saturating_sub(self.peak_sum)
}
pub fn utilization(&self) -> Option<f64> {
if self.reserved_sum == 0 {
return None;
}
Some(self.peak_sum as f64 / self.reserved_sum as f64)
}
}
pub fn stats() -> Stats {
let mut threads_live = 0u64;
let mut reserved_sum = 0u64;
let mut reserved_max = 0usize;
let mut peak_sum = 0u64;
let mut peak_max = 0usize;
let mut sample_count = 0u64;
let mut sample_sum = 0u64;
let mut near_limit_count = 0u64;
let mut guard_trip_count = 0u64;
#[cfg(feature = "watermark")]
let mut saturated_count = 0u64;
registry::for_each_thread(|thread| {
threads_live += 1;
reserved_sum += thread.size as u64;
reserved_max = cmp::max(reserved_max, thread.size);
peak_sum += thread.peak as u64;
peak_max = cmp::max(peak_max, thread.peak);
sample_count += thread.sample_count;
sample_sum += thread.sample_sum;
near_limit_count += thread.near_limit_count;
guard_trip_count += thread.guard_trip_count;
#[cfg(feature = "watermark")]
if thread.saturated {
saturated_count += 1;
}
});
let retired = registry::retired_totals();
peak_max = cmp::max(peak_max, retired.peak_max);
sample_count += retired.sample_count;
sample_sum += retired.sample_sum;
near_limit_count += retired.near_limit_count;
guard_trip_count += retired.guard_trip_count;
#[cfg(feature = "watermark")]
{
saturated_count += retired.saturated;
}
Stats {
threads_live,
threads_seen: registry::threads_seen(),
untracked: registry::untracked(),
reserved_sum,
reserved_max,
peak_sum,
peak_avg: peak_sum.checked_div(threads_live).map(|avg| avg as usize),
peak_max,
sample_count,
sample_sum,
sample_avg: sample_sum.checked_div(sample_count).map(|avg| avg as usize),
#[cfg(feature = "histograms")]
depth_histogram: registry::depth_histogram(),
#[cfg(feature = "watermark")]
saturated_count,
near_limit_count,
guard_trip_count,
}
}
impl Sub<&Stats> for &Stats {
type Output = Stats;
fn sub(self, old: &Stats) -> Stats {
let sample_count = self.sample_count.saturating_sub(old.sample_count);
let sample_sum = self.sample_sum.saturating_sub(old.sample_sum);
Stats {
threads_live: self.threads_live,
reserved_sum: self.reserved_sum,
reserved_max: self.reserved_max,
peak_sum: self.peak_sum,
peak_avg: self.peak_avg,
#[cfg(feature = "watermark")]
saturated_count: self.saturated_count,
threads_seen: self.threads_seen.saturating_sub(old.threads_seen),
untracked: self.untracked.saturating_sub(old.untracked),
peak_max: self.peak_max.saturating_sub(old.peak_max),
sample_count,
sample_sum,
sample_avg: sample_sum.checked_div(sample_count).map(|avg| avg as usize),
#[cfg(feature = "histograms")]
depth_histogram: self.depth_histogram - old.depth_histogram,
near_limit_count: self.near_limit_count.saturating_sub(old.near_limit_count),
guard_trip_count: self.guard_trip_count.saturating_sub(old.guard_trip_count),
}
}
}
impl Sub<Stats> for Stats {
type Output = Stats;
fn sub(self, old: Stats) -> Stats {
&self - &old
}
}
impl Sub<&Stats> for Stats {
type Output = Stats;
fn sub(self, old: &Stats) -> Stats {
&self - old
}
}
impl Sub<Stats> for &Stats {
type Output = Stats;
fn sub(self, old: Stats) -> Stats {
self - &old
}
}