use std::num::NonZeroU64;
use std::time::Instant;
#[derive(Copy, Clone)]
pub(crate) struct ScheduleLatencyInstant(Option<NonZeroU64>);
impl ScheduleLatencyInstant {
pub(crate) fn new(scheduler_start: Option<Instant>) -> Self {
Self(scheduler_start.map(|scheduler_start| {
NonZeroU64::new(scheduler_start.elapsed().as_nanos() as u64).unwrap_or(NonZeroU64::MIN)
}))
}
pub(crate) fn prepare(
self,
scheduler_start: Option<Instant>,
) -> Option<ScheduleLatencyContext> {
match (scheduler_start, self.0) {
(Some(scheduler_start), Some(scheduled_at_delta)) => Some(ScheduleLatencyContext {
scheduler_start,
scheduled_at_delta,
}),
_ => None,
}
}
}
pub(crate) struct ScheduleLatencyContext {
scheduler_start: Instant,
scheduled_at_delta: NonZeroU64,
}
impl ScheduleLatencyContext {
pub(crate) fn elapsed_nanos(&self, now: Instant) -> u64 {
let nanos_since_start = now
.saturating_duration_since(self.scheduler_start)
.as_nanos() as u64;
nanos_since_start.saturating_sub(self.scheduled_at_delta.get())
}
}