use core::time::Duration;
use crate::time::{duration_add, instant_micros, micros_as_duration};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct StallMemorySnapshot {
pub peak: Duration,
pub age: Duration,
}
impl StallMemorySnapshot {
#[must_use]
pub const fn new(peak: Duration, age: Duration) -> Self {
Self { peak, age }
}
#[must_use]
pub fn aged(self, elapsed: Duration) -> Self {
Self {
peak: self.peak,
age: duration_add(self.age, elapsed),
}
}
}
pub(crate) fn decayed(peak: u64, age: u64, hold: u64, decay: u64) -> u64 {
if age <= hold {
return peak;
}
let into = age - hold;
if into >= decay {
return 0;
}
let v = u128::from(peak) * u128::from(decay - into) / u128::from(decay.max(1));
u64::try_from(v).unwrap_or(u64::MAX)
}
fn age_at(now: u64, at: i128) -> u64 {
u64::try_from((i128::from(now) - at).max(0)).unwrap_or(u64::MAX)
}
#[derive(Clone, Debug)]
pub(crate) struct MaxHold {
peak: u64,
at: Option<i128>,
pending_age: Option<u64>,
hold: u64,
decay: u64,
}
impl MaxHold {
pub(crate) fn new(hold: u64, decay: u64) -> Self {
Self {
peak: 0,
at: None,
pending_age: None,
hold,
decay,
}
}
pub(crate) fn anchor(&mut self, now: u64) {
if let Some(age) = self.pending_age.take() {
self.at = Some(i128::from(now) - i128::from(age));
}
}
pub(crate) fn value(&self, now: u64) -> u64 {
match (self.at, self.pending_age) {
(Some(at), _) => decayed(self.peak, age_at(now, at), self.hold, self.decay),
(None, Some(age)) => decayed(self.peak, age, self.hold, self.decay),
(None, None) => 0,
}
}
pub(crate) fn observe(&mut self, now: u64, v: u64) {
self.anchor(now);
if self.at.is_none() || v >= self.value(now) {
self.peak = v;
self.at = Some(i128::from(now));
}
}
pub(crate) fn snapshot(&self, now: u64) -> StallMemorySnapshot {
match (self.at, self.pending_age) {
(Some(at), _) => StallMemorySnapshot::new(
micros_as_duration(self.peak),
micros_as_duration(age_at(now, at)),
),
(None, Some(age)) => {
StallMemorySnapshot::new(micros_as_duration(self.peak), micros_as_duration(age))
}
(None, None) => StallMemorySnapshot::default(),
}
}
pub(crate) fn restore(&mut self, s: StallMemorySnapshot) {
self.peak = instant_micros(s.peak);
self.at = None;
self.pending_age = Some(instant_micros(s.age));
}
pub(crate) fn clear(&mut self) {
self.peak = 0;
self.at = None;
self.pending_age = None;
}
}