xref: /DragonOS/kernel/src/arch/x86_64/time.rs (revision 23ef2b33d1e3cfd2506eb7449a33df4ec42f11d3)
1 use crate::time::{clocksource::HZ, TimeArch};
2 
3 use super::driver::tsc::TSCManager;
4 
5 /// 这个是系统jiffies时钟源的固有频率(不是调频之后的)
6 pub const CLOCK_TICK_RATE: u32 = HZ as u32 * 1000000;
7 
time_init()8 pub fn time_init() {
9     // do nothing
10 }
11 
12 pub struct X86_64TimeArch;
13 
14 impl TimeArch for X86_64TimeArch {
15     #[inline(always)]
get_cycles() -> usize16     fn get_cycles() -> usize {
17         unsafe { x86::time::rdtsc() as usize }
18     }
19 
cal_expire_cycles(ns: usize) -> usize20     fn cal_expire_cycles(ns: usize) -> usize {
21         Self::get_cycles() + ns * TSCManager::cpu_khz() as usize / 1000000
22     }
23 
24     /// 将CPU的时钟周期数转换为纳秒
25     #[inline(always)]
cycles2ns(cycles: usize) -> usize26     fn cycles2ns(cycles: usize) -> usize {
27         cycles * 1000000 / TSCManager::cpu_khz() as usize
28     }
29 }
30