solana_sdk/
timing.rs

1//! The `timing` module provides 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        solana_logger::setup();
107        let i = AtomicInterval::default();
108        assert!(!i.should_update(1000));
109
110        let i = AtomicInterval::default();
111        assert!(i.should_update_ext(1000, false));
112
113        std::thread::sleep(Duration::from_millis(10));
114        assert!(i.elapsed_ms() > 9 && i.elapsed_ms() < 1000);
115        assert!(
116            i.remaining_until_next_interval(1000) > 9
117                && i.remaining_until_next_interval(1000) < 991
118        );
119        assert!(i.should_update(9));
120        assert!(!i.should_update(100));
121    }
122
123    #[test]
124    fn test_years_as_slots() {
125        let tick_duration = Duration::from_micros(1000 * 1000 / 160);
126
127        // interestingly large numbers with 160 ticks/second
128        assert_eq!(years_as_slots(0.0, &tick_duration, 4) as u64, 0);
129        assert_eq!(
130            years_as_slots(1.0 / 12f64, &tick_duration, 4) as u64,
131            105_189_753
132        );
133        assert_eq!(years_as_slots(1.0, &tick_duration, 4) as u64, 1_262_277_039);
134
135        let tick_duration = Duration::from_micros(1000 * 1000);
136        // one second in years with one tick per second + one tick per slot
137        assert_eq!(
138            years_as_slots(1.0 / SECONDS_PER_YEAR, &tick_duration, 1),
139            1.0
140        );
141    }
142
143    #[test]
144    fn test_slot_duration_from_slots_per_year() {
145        let slots_per_year = 1_262_277_039.0;
146        let ticks_per_slot = 4;
147
148        assert_eq!(
149            slot_duration_from_slots_per_year(slots_per_year),
150            Duration::from_micros(1000 * 1000 / 160) * ticks_per_slot
151        );
152        assert_eq!(
153            slot_duration_from_slots_per_year(0.0),
154            Duration::from_micros(0) * ticks_per_slot
155        );
156
157        let slots_per_year = SECONDS_PER_YEAR;
158        let ticks_per_slot = 1;
159        assert_eq!(
160            slot_duration_from_slots_per_year(slots_per_year),
161            Duration::from_millis(1000) * ticks_per_slot
162        );
163    }
164}