atlas_time_utils/
lib.rs

1//! `std::time` utility functions.
2#![cfg_attr(docsrs, feature(doc_cfg))]
3use std::{
4    sync::atomic::{AtomicU64, Ordering},
5    time::{Duration, SystemTime, UNIX_EPOCH},
6};
7
8/// return timestamp as ms
9pub fn timestamp() -> u64 {
10    SystemTime::now()
11        .duration_since(UNIX_EPOCH)
12        .expect("create timestamp in timing")
13        .as_millis() as u64
14}
15
16pub const SECONDS_PER_YEAR: f64 = 365.242_199 * 24.0 * 60.0 * 60.0;
17
18/// from years to slots
19pub fn years_as_slots(years: f64, tick_duration: &Duration, ticks_per_slot: u64) -> f64 {
20    // slots is  years * slots/year
21    years       *
22    //  slots/year  is  seconds/year ...
23        SECONDS_PER_YEAR
24    //  * (ns/s)/(ns/tick) / ticks/slot = 1/s/1/tick = ticks/s
25        * (1_000_000_000.0 / tick_duration.as_nanos() as f64)
26    //  / ticks/slot
27        / ticks_per_slot as f64
28}
29
30/// From slots per year to slot duration
31pub fn slot_duration_from_slots_per_year(slots_per_year: f64) -> Duration {
32    // Recently, rust changed from infinity as usize being zero to 2^64-1; ensure it's zero here
33    let slot_in_ns = if slots_per_year != 0.0 {
34        (SECONDS_PER_YEAR * 1_000_000_000.0) / slots_per_year
35    } else {
36        0.0
37    };
38    Duration::from_nanos(slot_in_ns as u64)
39}
40
41#[derive(Debug, Default)]
42pub struct AtomicInterval {
43    last_update: AtomicU64,
44}
45
46impl AtomicInterval {
47    /// true if 'interval_time_ms' has elapsed since last time we returned true as long as it has been 'interval_time_ms' since this struct was created
48    #[inline(always)]
49    pub fn should_update(&self, interval_time_ms: u64) -> bool {
50        self.should_update_ext(interval_time_ms, true)
51    }
52
53    /// a primary use case is periodic metric reporting, potentially from different threads
54    /// true if 'interval_time_ms' has elapsed since last time we returned true
55    /// except, if skip_first=false, false until 'interval_time_ms' has elapsed since this struct was created
56    #[inline(always)]
57    pub fn should_update_ext(&self, interval_time_ms: u64, skip_first: bool) -> bool {
58        let now = timestamp();
59        let last = self.last_update.load(Ordering::Relaxed);
60        now.saturating_sub(last) > interval_time_ms
61            && self
62                .last_update
63                .compare_exchange(last, now, Ordering::Relaxed, Ordering::Relaxed)
64                == Ok(last)
65            && !(skip_first && last == 0)
66    }
67
68    /// return ms elapsed since the last time the time was set
69    pub fn elapsed_ms(&self) -> u64 {
70        let now = timestamp();
71        let last = self.last_update.load(Ordering::Relaxed);
72        now.saturating_sub(last) // wrapping somehow?
73    }
74
75    /// return ms until the interval_time will have elapsed
76    pub fn remaining_until_next_interval(&self, interval_time: u64) -> u64 {
77        interval_time.saturating_sub(self.elapsed_ms())
78    }
79}
80
81#[cfg(test)]
82mod test {
83    use super::*;
84
85    #[test]
86    fn test_interval_update() {
87        let i = AtomicInterval::default();
88        assert!(!i.should_update(1000));
89
90        let i = AtomicInterval::default();
91        assert!(i.should_update_ext(1000, false));
92
93        std::thread::sleep(Duration::from_millis(10));
94        assert!(i.elapsed_ms() > 9 && i.elapsed_ms() < 1000);
95        assert!(
96            i.remaining_until_next_interval(1000) > 9
97                && i.remaining_until_next_interval(1000) < 991
98        );
99        assert!(i.should_update(9));
100        assert!(!i.should_update(100));
101    }
102
103    #[test]
104    fn test_years_as_slots() {
105        let tick_duration = Duration::from_micros(1000 * 1000 / 160);
106
107        // interestingly large numbers with 160 ticks/second
108        assert_eq!(years_as_slots(0.0, &tick_duration, 4) as u64, 0);
109        assert_eq!(
110            years_as_slots(1.0 / 12f64, &tick_duration, 4) as u64,
111            105_189_753
112        );
113        assert_eq!(years_as_slots(1.0, &tick_duration, 4) as u64, 1_262_277_039);
114
115        let tick_duration = Duration::from_micros(1000 * 1000);
116        // one second in years with one tick per second + one tick per slot
117        assert_eq!(
118            years_as_slots(1.0 / SECONDS_PER_YEAR, &tick_duration, 1),
119            1.0
120        );
121    }
122
123    #[test]
124    fn test_slot_duration_from_slots_per_year() {
125        let slots_per_year = 1_262_277_039.0;
126        let ticks_per_slot = 4;
127
128        assert_eq!(
129            slot_duration_from_slots_per_year(slots_per_year),
130            Duration::from_micros(1000 * 1000 / 160) * ticks_per_slot
131        );
132        assert_eq!(
133            slot_duration_from_slots_per_year(0.0),
134            Duration::from_micros(0) * ticks_per_slot
135        );
136
137        let slots_per_year = SECONDS_PER_YEAR;
138        let ticks_per_slot = 1;
139        assert_eq!(
140            slot_duration_from_slots_per_year(slots_per_year),
141            Duration::from_millis(1000) * ticks_per_slot
142        );
143    }
144}