xref: /DragonOS/kernel/src/arch/x86_64/time.rs (revision b8ed38251dc255b0c525801b5dbf37d3b0d0d61e)
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 
8 pub struct X86_64TimeArch;
9 
10 impl TimeArch for X86_64TimeArch {
11     #[inline(always)]
12     fn get_cycles() -> usize {
13         unsafe { x86::time::rdtsc() as usize }
14     }
15 
16     fn cal_expire_cycles(ns: usize) -> usize {
17         Self::get_cycles() + ns * TSCManager::cpu_khz() as usize / 1000000
18     }
19 
20     /// 将CPU的时钟周期数转换为纳秒
21     #[inline(always)]
22     fn cycles2ns(cycles: usize) -> usize {
23         cycles * 1000000 / TSCManager::cpu_khz() as usize
24     }
25 }
26