solana_time_utils/
lib.rs

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