use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Default)]
pub(crate) struct Counter(AtomicU64);
impl Counter {
pub(crate) fn add(&self, count: u64) {
self.0.fetch_add(count, Ordering::Relaxed);
}
fn get(&self) -> u64 {
self.0.load(Ordering::Relaxed)
}
}
#[derive(Default)]
pub(crate) struct Counters {
pub rx_datagrams: Counter,
pub rx_receives: Counter,
pub rx_enobufs: Counter,
pub rx_exhausted: Counter,
pub tx_datagrams: Counter,
pub tx_sends: Counter,
pub tx_stalls: Counter,
pub submissions: Counter,
pub completions: Counter,
pub enters: Counter,
pub parks: Counter,
pub wakes: Counter,
pub timers_armed: Counter,
pub timers_fired: Counter,
pub timers_cancelled: Counter,
}
#[derive(Clone, Default)]
pub struct Metrics(Arc<Counters>);
impl Metrics {
pub(crate) fn counters(&self) -> &Arc<Counters> {
&self.0
}
pub(crate) fn from_counters(counters: Arc<Counters>) -> Self {
Self(counters)
}
pub fn snapshot(&self) -> Snapshot {
Snapshot {
rx_datagrams: self.0.rx_datagrams.get(),
rx_receives: self.0.rx_receives.get(),
rx_enobufs: self.0.rx_enobufs.get(),
rx_exhausted: self.0.rx_exhausted.get(),
tx_datagrams: self.0.tx_datagrams.get(),
tx_sends: self.0.tx_sends.get(),
tx_stalls: self.0.tx_stalls.get(),
submissions: self.0.submissions.get(),
completions: self.0.completions.get(),
enters: self.0.enters.get(),
parks: self.0.parks.get(),
wakes: self.0.wakes.get(),
timers_armed: self.0.timers_armed.get(),
timers_fired: self.0.timers_fired.get(),
timers_cancelled: self.0.timers_cancelled.get(),
}
}
}
impl std::fmt::Debug for Metrics {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.snapshot().fmt(f)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Snapshot {
pub rx_datagrams: u64,
pub rx_receives: u64,
pub rx_enobufs: u64,
pub rx_exhausted: u64,
pub tx_datagrams: u64,
pub tx_sends: u64,
pub tx_stalls: u64,
pub submissions: u64,
pub completions: u64,
pub enters: u64,
pub parks: u64,
pub wakes: u64,
pub timers_armed: u64,
pub timers_fired: u64,
pub timers_cancelled: u64,
}
impl Snapshot {
pub fn timers_active(&self) -> u64 {
self.timers_armed
.saturating_sub(self.timers_fired)
.saturating_sub(self.timers_cancelled)
}
}