1use 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
27pub 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
37pub fn years_as_slots(years: f64, tick_duration: &Duration, ticks_per_slot: u64) -> f64 {
39 years *
41 SECONDS_PER_YEAR
43 * (1_000_000_000.0 / tick_duration.as_nanos() as f64)
45 / ticks_per_slot as f64
47}
48
49pub fn slot_duration_from_slots_per_year(slots_per_year: f64) -> Duration {
51 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 #[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 #[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 pub fn elapsed_ms(&self) -> u64 {
89 let now = timestamp();
90 let last = self.last_update.load(Ordering::Relaxed);
91 now.saturating_sub(last) }
93
94 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 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 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}