xref: /DragonOS/kernel/src/time/timekeeping.rs (revision 840045af94ea3391f29e87e968db5d9c48316981)
1 use alloc::sync::Arc;
2 use core::sync::atomic::{compiler_fence, AtomicBool, AtomicI64, Ordering};
3 
4 use crate::{
5     arch::CurrentIrqArch,
6     exception::InterruptArch,
7     kdebug, kinfo,
8     libs::rwlock::RwLock,
9     time::{
10         jiffies::{clocksource_default_clock, jiffies_init},
11         timekeep::ktime_get_real_ns,
12         TimeSpec,
13     },
14 };
15 
16 use super::{
17     clocksource::{clocksource_cyc2ns, Clocksource, CycleNum, HZ},
18     syscall::PosixTimeval,
19     NSEC_PER_SEC, USEC_PER_SEC,
20 };
21 /// NTP周期频率
22 pub const NTP_INTERVAL_FREQ: u64 = HZ;
23 /// NTP周期长度
24 pub const NTP_INTERVAL_LENGTH: u64 = NSEC_PER_SEC as u64 / NTP_INTERVAL_FREQ;
25 /// NTP转换比例
26 pub const NTP_SCALE_SHIFT: u32 = 32;
27 
28 /// timekeeping休眠标志,false为未休眠
29 pub static TIMEKEEPING_SUSPENDED: AtomicBool = AtomicBool::new(false);
30 /// 已经递增的微秒数
31 static __ADDED_USEC: AtomicI64 = AtomicI64::new(0);
32 /// 已经递增的秒数
33 static __ADDED_SEC: AtomicI64 = AtomicI64::new(0);
34 /// timekeeper全局变量,用于管理timekeeper模块
35 static mut __TIMEKEEPER: Option<Timekeeper> = None;
36 
37 #[derive(Debug)]
38 pub struct Timekeeper(RwLock<TimekeeperData>);
39 
40 #[allow(dead_code)]
41 #[derive(Debug)]
42 pub struct TimekeeperData {
43     /// 用于计时的当前时钟源。
44     clock: Option<Arc<dyn Clocksource>>,
45     /// 当前时钟源的移位值。
46     shift: i32,
47     /// 一个NTP间隔中的时钟周期数。
48     cycle_interval: CycleNum,
49     /// 一个NTP间隔中时钟移位的纳秒数。
50     xtime_interval: u64,
51     ///
52     xtime_remainder: i64,
53     /// 每个NTP间隔累积的原始纳米秒
54     raw_interval: i64,
55     /// 时钟移位纳米秒余数
56     xtime_nsec: u64,
57     /// 积累时间和ntp时间在ntp位移纳秒量上的差距
58     ntp_error: i64,
59     /// 用于转换时钟偏移纳秒和ntp偏移纳秒的偏移量
60     ntp_error_shift: i32,
61     /// NTP调整时钟乘法器
62     mult: u32,
63     raw_time: TimeSpec,
64     wall_to_monotonic: TimeSpec,
65     total_sleep_time: TimeSpec,
66     xtime: TimeSpec,
67 }
68 impl TimekeeperData {
69     pub fn new() -> Self {
70         Self {
71             clock: None,
72             shift: Default::default(),
73             cycle_interval: CycleNum(0),
74             xtime_interval: Default::default(),
75             xtime_remainder: Default::default(),
76             raw_interval: Default::default(),
77             xtime_nsec: Default::default(),
78             ntp_error: Default::default(),
79             ntp_error_shift: Default::default(),
80             mult: Default::default(),
81             xtime: TimeSpec {
82                 tv_nsec: 0,
83                 tv_sec: 0,
84             },
85             wall_to_monotonic: TimeSpec {
86                 tv_nsec: 0,
87                 tv_sec: 0,
88             },
89             total_sleep_time: TimeSpec {
90                 tv_nsec: 0,
91                 tv_sec: 0,
92             },
93             raw_time: TimeSpec {
94                 tv_nsec: 0,
95                 tv_sec: 0,
96             },
97         }
98     }
99 }
100 impl Timekeeper {
101     /// # 设置timekeeper的参数
102     ///
103     /// ## 参数
104     ///
105     /// * 'clock' - 指定的时钟实际类型。初始为ClocksourceJiffies
106     pub fn timekeeper_setup_internals(&self, clock: Arc<dyn Clocksource>) {
107         let mut timekeeper = self.0.write_irqsave();
108         // 更新clock
109         let mut clock_data = clock.clocksource_data();
110         clock_data.watchdog_last = clock.read();
111         if clock.update_clocksource_data(clock_data).is_err() {
112             kdebug!("timekeeper_setup_internals:update_clocksource_data run failed");
113         }
114         timekeeper.clock.replace(clock.clone());
115 
116         let clock_data = clock.clocksource_data();
117         let mut temp = NTP_INTERVAL_LENGTH << clock_data.shift;
118         let ntpinterval = temp;
119         temp += (clock_data.mult / 2) as u64;
120         // do div
121 
122         timekeeper.cycle_interval = CycleNum(temp);
123         timekeeper.xtime_interval = temp * clock_data.mult as u64;
124         // 这里可能存在下界溢出问题,debug模式下会报错panic
125         timekeeper.xtime_remainder = (ntpinterval - timekeeper.xtime_interval) as i64;
126         timekeeper.raw_interval = (timekeeper.xtime_interval >> clock_data.shift) as i64;
127         timekeeper.xtime_nsec = 0;
128         timekeeper.shift = clock_data.shift as i32;
129 
130         timekeeper.ntp_error = 0;
131         timekeeper.ntp_error_shift = (NTP_SCALE_SHIFT - clock_data.shift) as i32;
132 
133         timekeeper.mult = clock_data.mult;
134     }
135 
136     /// # 获取当前时钟源距离上次检测走过的纳秒数
137     #[allow(dead_code)]
138     pub fn tk_get_ns(&self) -> u64 {
139         let timekeeper = self.0.read_irqsave();
140         let clock = timekeeper.clock.clone().unwrap();
141         drop(timekeeper);
142         let clock_now = clock.read();
143         let clcok_data = clock.clocksource_data();
144         let clock_delta = clock_now.div(clcok_data.watchdog_last).data() & clcok_data.mask.bits();
145         return clocksource_cyc2ns(CycleNum(clock_delta), clcok_data.mult, clcok_data.shift);
146     }
147 }
148 pub fn timekeeper() -> &'static Timekeeper {
149     let r = unsafe { __TIMEKEEPER.as_ref().unwrap() };
150 
151     return r;
152 }
153 
154 pub fn timekeeper_init() {
155     unsafe { __TIMEKEEPER = Some(Timekeeper(RwLock::new(TimekeeperData::new()))) };
156 }
157 
158 /// # 获取1970.1.1至今的UTC时间戳(最小单位:nsec)
159 ///
160 /// ## 返回值
161 ///
162 /// * 'TimeSpec' - 时间戳
163 pub fn getnstimeofday() -> TimeSpec {
164     // kdebug!("enter getnstimeofday");
165 
166     // let mut nsecs: u64 = 0;0
167     let mut _xtime = TimeSpec {
168         tv_nsec: 0,
169         tv_sec: 0,
170     };
171     loop {
172         match timekeeper().0.try_read_irqsave() {
173             None => continue,
174             Some(tk) => {
175                 _xtime = tk.xtime;
176                 drop(tk);
177                 // nsecs = timekeeper().tk_get_ns();
178                 // TODO 不同架构可能需要加上不同的偏移量
179                 break;
180             }
181         }
182     }
183     // xtime.tv_nsec += nsecs as i64;
184     let sec = __ADDED_SEC.load(Ordering::SeqCst);
185     _xtime.tv_sec += sec;
186     while _xtime.tv_nsec >= NSEC_PER_SEC.into() {
187         _xtime.tv_nsec -= NSEC_PER_SEC as i64;
188         _xtime.tv_sec += 1;
189     }
190 
191     // TODO 将xtime和当前时间源的时间相加
192 
193     return _xtime;
194 }
195 
196 /// # 获取1970.1.1至今的UTC时间戳(最小单位:usec)
197 ///
198 /// ## 返回值
199 ///
200 /// * 'PosixTimeval' - 时间戳
201 pub fn do_gettimeofday() -> PosixTimeval {
202     let tp = getnstimeofday();
203     return PosixTimeval {
204         tv_sec: tp.tv_sec,
205         tv_usec: (tp.tv_nsec / 1000) as i32,
206     };
207 }
208 
209 /// # 初始化timekeeping模块
210 #[inline(never)]
211 pub fn timekeeping_init() {
212     kinfo!("Initializing timekeeping module...");
213     let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
214     timekeeper_init();
215 
216     // TODO 有ntp模块后 在此初始化ntp模块
217 
218     let clock = clocksource_default_clock();
219     clock
220         .enable()
221         .expect("clocksource_default_clock enable failed");
222     timekeeper().timekeeper_setup_internals(clock);
223     // 暂时不支持其他架构平台对时间的设置 所以使用x86平台对应值初始化
224     let mut timekeeper = timekeeper().0.write_irqsave();
225     timekeeper.xtime.tv_nsec = ktime_get_real_ns();
226 
227     // 初始化wall time到monotonic的时间
228     let mut nsec = -timekeeper.xtime.tv_nsec;
229     let mut sec = -timekeeper.xtime.tv_sec;
230     // FIXME: 这里有个奇怪的奇怪的bug
231     let num = nsec % NSEC_PER_SEC as i64;
232     nsec += num * NSEC_PER_SEC as i64;
233     sec -= num;
234     timekeeper.wall_to_monotonic.tv_nsec = nsec;
235     timekeeper.wall_to_monotonic.tv_sec = sec;
236 
237     __ADDED_USEC.store(0, Ordering::SeqCst);
238     __ADDED_SEC.store(0, Ordering::SeqCst);
239 
240     drop(irq_guard);
241     jiffies_init();
242     kinfo!("timekeeping_init successfully");
243 }
244 
245 /// # 使用当前时钟源增加wall time
246 pub fn update_wall_time(delta_us: i64) {
247     // kdebug!("enter update_wall_time, stack_use = {:}",stack_use);
248     compiler_fence(Ordering::SeqCst);
249     let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
250     // 如果在休眠那就不更新
251     if TIMEKEEPING_SUSPENDED.load(Ordering::SeqCst) {
252         return;
253     }
254 
255     // ===== 请不要删除这些注释 =====
256     // let clock = timekeeper.clock.clone().unwrap();
257     // let clock_data = clock.clocksource_data();
258     // let offset = (clock.read().div(clock_data.watchdog_last).data()) & clock_data.mask.bits();
259 
260     // timekeeper.xtime_nsec = (timekeeper.xtime.tv_nsec as u64) << timekeeper.shift;
261     // // TODO 当有ntp模块之后 需要将timekeep与ntp进行同步并检查
262     // timekeeper.xtime.tv_nsec = ((timekeeper.xtime_nsec as i64) >> timekeeper.shift) + 1;
263     // timekeeper.xtime_nsec -= (timekeeper.xtime.tv_nsec as u64) << timekeeper.shift;
264 
265     // timekeeper.xtime.tv_nsec += offset as i64;
266     // while unlikely(timekeeper.xtime.tv_nsec >= NSEC_PER_SEC.into()) {
267     //     timekeeper.xtime.tv_nsec -= NSEC_PER_SEC as i64;
268     //     timekeeper.xtime.tv_sec += 1;
269     //     // TODO 需要处理闰秒
270     // }
271     // ================
272     compiler_fence(Ordering::SeqCst);
273 
274     __ADDED_USEC.fetch_add(delta_us, Ordering::SeqCst);
275     compiler_fence(Ordering::SeqCst);
276     let mut retry = 10;
277 
278     let usec = __ADDED_USEC.load(Ordering::SeqCst);
279     if usec % USEC_PER_SEC as i64 == 0 {
280         compiler_fence(Ordering::SeqCst);
281 
282         __ADDED_SEC.fetch_add(1, Ordering::SeqCst);
283         compiler_fence(Ordering::SeqCst);
284     }
285     // 一分钟同步一次
286     loop {
287         if (usec & !((1 << 26) - 1)) != 0 {
288             if __ADDED_USEC
289                 .compare_exchange(usec, 0, Ordering::SeqCst, Ordering::SeqCst)
290                 .is_ok()
291                 || retry == 0
292             {
293                 // 同步时间
294                 // 我感觉这里会出问题:多个读者不退出的话,写者就无法写入
295                 // 然后这里会超时,导致在中断返回之后,会不断的进入这个中断,最终爆栈。
296                 let mut timekeeper = timekeeper().0.write_irqsave();
297                 timekeeper.xtime.tv_nsec = ktime_get_real_ns();
298                 timekeeper.xtime.tv_sec = 0;
299                 __ADDED_SEC.store(0, Ordering::SeqCst);
300                 drop(timekeeper);
301                 break;
302             }
303             retry -= 1;
304         } else {
305             break;
306         }
307     }
308     // TODO 需要检查是否更新时间源
309     compiler_fence(Ordering::SeqCst);
310     drop(irq_guard);
311     compiler_fence(Ordering::SeqCst);
312 }
313 // TODO timekeeping_adjust
314 // TODO wall_to_monotic
315