1use std::{
3 sync::atomic::{AtomicU64, Ordering},
4 time::{Duration, SystemTime, UNIX_EPOCH},
5};
6
7pub 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
17pub fn years_as_slots(years: f64, tick_duration: &Duration, ticks_per_slot: u64) -> f64 {
19 years *
21 SECONDS_PER_YEAR
23 * (1_000_000_000.0 / tick_duration.as_nanos() as f64)
25 / ticks_per_slot as f64
27}
28
29pub fn slot_duration_from_slots_per_year(slots_per_year: f64) -> Duration {
31 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 #[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 #[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 pub fn elapsed_ms(&self) -> u64 {
69 let now = timestamp();
70 let last = self.last_update.load(Ordering::Relaxed);
71 now.saturating_sub(last) }
73
74 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 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 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}