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