use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct BundleCounts {
pub decoded: u64,
pub discarded: u64,
pub redecoded: u64,
pub runs: u64,
pub runs_held: u64,
pub runs_peak: u64,
}
const _: () = crate::send_sync::<BundleCounts>();
#[derive(Debug, Default)]
pub(super) struct DecodeCounts {
decoded: AtomicU64,
discarded: AtomicU64,
redecoded: AtomicU64,
}
impl DecodeCounts {
pub(super) fn wrote(&self, bytes: u64, again: bool) {
add(&self.decoded, bytes);
if again {
add(&self.redecoded, bytes);
}
}
pub(super) fn dropped(&self, bytes: u64) {
add(&self.discarded, bytes);
}
pub(super) fn snapshot(&self, runs: u64, held: &RunBytes) -> BundleCounts {
BundleCounts {
decoded: self.decoded.load(Ordering::Relaxed),
discarded: self.discarded.load(Ordering::Relaxed),
redecoded: self.redecoded.load(Ordering::Relaxed),
runs,
runs_held: held.held,
runs_peak: held.peak,
}
}
}
#[derive(Debug, Default)]
pub(crate) struct RunBytes {
held: u64,
peak: u64,
}
impl RunBytes {
pub(crate) fn publish(&mut self, added: u64, dropped: u64) {
if cfg!(not(unityrs_no_counters)) {
self.held = self.held.saturating_add(added).saturating_sub(dropped);
self.peak = self.peak.max(self.held);
}
}
}
fn add(counter: &AtomicU64, n: u64) {
if cfg!(not(unityrs_no_counters)) {
counter.fetch_add(n, Ordering::Relaxed);
}
}