xref: /DragonOS/kernel/src/time/mod.rs (revision 8cb2e9b344230227fe5f3ab3ebeb2522f1c5e289)
18612b6ceSLoGin use core::{
28612b6ceSLoGin     fmt,
38d72b68dSJomo     intrinsics::unlikely,
48612b6ceSLoGin     ops::{self, Sub},
58612b6ceSLoGin };
613776c11Slogin 
78d72b68dSJomo use crate::arch::CurrentTimeArch;
88d72b68dSJomo 
913776c11Slogin use self::timekeep::ktime_get_real_ns;
1013776c11Slogin 
1136fd0130Shoumkh pub mod clocksource;
1236fd0130Shoumkh pub mod jiffies;
13bacd691cSlogin pub mod sleep;
14ab5c8ca4Slogin pub mod syscall;
1536fd0130Shoumkh pub mod timeconv;
1601876902SGou Ngai pub mod timekeep;
1736fd0130Shoumkh pub mod timekeeping;
18bacd691cSlogin pub mod timer;
1913776c11Slogin /* Time structures. (Partitially taken from smoltcp)
2013776c11Slogin 
2113776c11Slogin The `time` module contains structures used to represent both
2213776c11Slogin absolute and relative time.
2313776c11Slogin 
2413776c11Slogin  - [Instant] is used to represent absolute time.
2513776c11Slogin  - [Duration] is used to represent relative time.
2613776c11Slogin 
2713776c11Slogin [Instant]: struct.Instant.html
2813776c11Slogin [Duration]: struct.Duration.html
2913776c11Slogin */
3036fd0130Shoumkh #[allow(dead_code)]
3136fd0130Shoumkh pub const MSEC_PER_SEC: u32 = 1000;
3236fd0130Shoumkh #[allow(dead_code)]
3336fd0130Shoumkh pub const USEC_PER_MSEC: u32 = 1000;
3436fd0130Shoumkh #[allow(dead_code)]
3536fd0130Shoumkh pub const NSEC_PER_USEC: u32 = 1000;
3636fd0130Shoumkh #[allow(dead_code)]
3736fd0130Shoumkh pub const NSEC_PER_MSEC: u32 = 1000000;
3836fd0130Shoumkh #[allow(dead_code)]
3936fd0130Shoumkh pub const USEC_PER_SEC: u32 = 1000000;
4036fd0130Shoumkh #[allow(dead_code)]
4136fd0130Shoumkh pub const NSEC_PER_SEC: u32 = 1000000000;
4236fd0130Shoumkh #[allow(dead_code)]
4336fd0130Shoumkh pub const FSEC_PER_SEC: u64 = 1000000000000000;
4413776c11Slogin 
45ab5c8ca4Slogin /// 表示时间的结构体,符合POSIX标准。
46004e86ffSlogin #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
47ab5c8ca4Slogin #[repr(C)]
48004e86ffSlogin pub struct TimeSpec {
49004e86ffSlogin     pub tv_sec: i64,
50004e86ffSlogin     pub tv_nsec: i64,
51004e86ffSlogin }
5220e3152eSlogin 
5320e3152eSlogin impl TimeSpec {
5413776c11Slogin     #[allow(dead_code)]
5520e3152eSlogin     pub fn new(sec: i64, nsec: i64) -> TimeSpec {
5620e3152eSlogin         return TimeSpec {
5720e3152eSlogin             tv_sec: sec,
5820e3152eSlogin             tv_nsec: nsec,
5920e3152eSlogin         };
6020e3152eSlogin     }
618d72b68dSJomo 
628d72b68dSJomo     /// 获取当前时间
638d72b68dSJomo     pub fn now() -> Self {
648d72b68dSJomo         #[cfg(target_arch = "x86_64")]
658d72b68dSJomo         {
668d72b68dSJomo             use crate::arch::driver::tsc::TSCManager;
678d72b68dSJomo             let khz = TSCManager::cpu_khz();
688d72b68dSJomo             if unlikely(khz == 0) {
698d72b68dSJomo                 return TimeSpec::default();
708d72b68dSJomo             } else {
718d72b68dSJomo                 return Self::from(Duration::from_millis(
728d72b68dSJomo                     CurrentTimeArch::get_cycles() as u64 / khz,
738d72b68dSJomo                 ));
748d72b68dSJomo             }
758d72b68dSJomo         }
768d72b68dSJomo 
778d72b68dSJomo         #[cfg(target_arch = "riscv64")]
788d72b68dSJomo         {
79453452ccSLoGin             return TimeSpec::new(0, 0);
808d72b68dSJomo         }
818d72b68dSJomo     }
8220e3152eSlogin }
8313776c11Slogin 
848612b6ceSLoGin impl Sub for TimeSpec {
858612b6ceSLoGin     type Output = Duration;
868612b6ceSLoGin     fn sub(self, rhs: Self) -> Self::Output {
878612b6ceSLoGin         let sec = self.tv_sec.checked_sub(rhs.tv_sec).unwrap_or(0);
888612b6ceSLoGin         let nsec = self.tv_nsec.checked_sub(rhs.tv_nsec).unwrap_or(0);
898612b6ceSLoGin         Duration::from_micros((sec * 1000000 + nsec / 1000) as u64)
908612b6ceSLoGin     }
918612b6ceSLoGin }
928612b6ceSLoGin 
938612b6ceSLoGin impl From<Duration> for TimeSpec {
948612b6ceSLoGin     fn from(dur: Duration) -> Self {
958612b6ceSLoGin         TimeSpec {
968612b6ceSLoGin             tv_sec: dur.total_micros() as i64 / 1000000,
978612b6ceSLoGin             tv_nsec: (dur.total_micros() as i64 % 1000000) * 1000,
988612b6ceSLoGin         }
998612b6ceSLoGin     }
1008612b6ceSLoGin }
1018612b6ceSLoGin 
102840045afSLoGin impl From<TimeSpec> for Duration {
103840045afSLoGin     fn from(val: TimeSpec) -> Self {
104840045afSLoGin         Duration::from_micros(val.tv_sec as u64 * 1000000 + val.tv_nsec as u64 / 1000)
1058612b6ceSLoGin     }
1068612b6ceSLoGin }
1078612b6ceSLoGin 
10813776c11Slogin /// A representation of an absolute time value.
10913776c11Slogin ///
11013776c11Slogin /// The `Instant` type is a wrapper around a `i64` value that
11113776c11Slogin /// represents a number of microseconds, monotonically increasing
11213776c11Slogin /// since an arbitrary moment in time, such as system startup.
11313776c11Slogin ///
11413776c11Slogin /// * A value of `0` is inherently arbitrary.
11513776c11Slogin /// * A value less than `0` indicates a time before the starting
11613776c11Slogin ///   point.
11713776c11Slogin #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
11813776c11Slogin #[cfg_attr(feature = "defmt", derive(defmt::Format))]
11913776c11Slogin pub struct Instant {
12013776c11Slogin     micros: i64,
12113776c11Slogin }
12213776c11Slogin 
12313776c11Slogin #[allow(dead_code)]
12413776c11Slogin impl Instant {
12513776c11Slogin     pub const ZERO: Instant = Instant::from_micros_const(0);
12613776c11Slogin 
12713776c11Slogin     /// Create a new `Instant` from a number of microseconds.
12813776c11Slogin     pub fn from_micros<T: Into<i64>>(micros: T) -> Instant {
12913776c11Slogin         Instant {
13013776c11Slogin             micros: micros.into(),
13113776c11Slogin         }
13213776c11Slogin     }
13313776c11Slogin 
13413776c11Slogin     pub const fn from_micros_const(micros: i64) -> Instant {
13513776c11Slogin         Instant { micros }
13613776c11Slogin     }
13713776c11Slogin 
13813776c11Slogin     /// Create a new `Instant` from a number of milliseconds.
13913776c11Slogin     pub fn from_millis<T: Into<i64>>(millis: T) -> Instant {
14013776c11Slogin         Instant {
14113776c11Slogin             micros: millis.into() * 1000,
14213776c11Slogin         }
14313776c11Slogin     }
14413776c11Slogin 
14513776c11Slogin     /// Create a new `Instant` from a number of milliseconds.
14613776c11Slogin     pub const fn from_millis_const(millis: i64) -> Instant {
14713776c11Slogin         Instant {
14813776c11Slogin             micros: millis * 1000,
14913776c11Slogin         }
15013776c11Slogin     }
15113776c11Slogin 
15213776c11Slogin     /// Create a new `Instant` from a number of seconds.
15313776c11Slogin     pub fn from_secs<T: Into<i64>>(secs: T) -> Instant {
15413776c11Slogin         Instant {
15513776c11Slogin             micros: secs.into() * 1000000,
15613776c11Slogin         }
15713776c11Slogin     }
15813776c11Slogin 
15913776c11Slogin     /// Create a new `Instant` from the current time
16013776c11Slogin     pub fn now() -> Instant {
16113776c11Slogin         Self::from_micros(ktime_get_real_ns() / 1000)
16213776c11Slogin     }
16313776c11Slogin 
16413776c11Slogin     /// The fractional number of milliseconds that have passed
16513776c11Slogin     /// since the beginning of time.
16613776c11Slogin     pub const fn millis(&self) -> i64 {
16713776c11Slogin         self.micros % 1000000 / 1000
16813776c11Slogin     }
16913776c11Slogin 
17013776c11Slogin     /// The fractional number of microseconds that have passed
17113776c11Slogin     /// since the beginning of time.
17213776c11Slogin     pub const fn micros(&self) -> i64 {
17313776c11Slogin         self.micros % 1000000
17413776c11Slogin     }
17513776c11Slogin 
17613776c11Slogin     /// The number of whole seconds that have passed since the
17713776c11Slogin     /// beginning of time.
17813776c11Slogin     pub const fn secs(&self) -> i64 {
17913776c11Slogin         self.micros / 1000000
18013776c11Slogin     }
18113776c11Slogin 
18213776c11Slogin     /// The total number of milliseconds that have passed since
18313776c11Slogin     /// the beginning of time.
18413776c11Slogin     pub const fn total_millis(&self) -> i64 {
18513776c11Slogin         self.micros / 1000
18613776c11Slogin     }
18713776c11Slogin     /// The total number of milliseconds that have passed since
18813776c11Slogin     /// the beginning of time.
18913776c11Slogin     pub const fn total_micros(&self) -> i64 {
19013776c11Slogin         self.micros
19113776c11Slogin     }
19213776c11Slogin }
19313776c11Slogin 
19413776c11Slogin impl fmt::Display for Instant {
19513776c11Slogin     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19613776c11Slogin         write!(f, "{}.{:0>3}s", self.secs(), self.millis())
19713776c11Slogin     }
19813776c11Slogin }
19913776c11Slogin 
20013776c11Slogin impl ops::Add<Duration> for Instant {
20113776c11Slogin     type Output = Instant;
20213776c11Slogin 
20313776c11Slogin     fn add(self, rhs: Duration) -> Instant {
20413776c11Slogin         Instant::from_micros(self.micros + rhs.total_micros() as i64)
20513776c11Slogin     }
20613776c11Slogin }
20713776c11Slogin 
20813776c11Slogin impl ops::AddAssign<Duration> for Instant {
20913776c11Slogin     fn add_assign(&mut self, rhs: Duration) {
21013776c11Slogin         self.micros += rhs.total_micros() as i64;
21113776c11Slogin     }
21213776c11Slogin }
21313776c11Slogin 
21413776c11Slogin impl ops::Sub<Duration> for Instant {
21513776c11Slogin     type Output = Instant;
21613776c11Slogin 
21713776c11Slogin     fn sub(self, rhs: Duration) -> Instant {
21813776c11Slogin         Instant::from_micros(self.micros - rhs.total_micros() as i64)
21913776c11Slogin     }
22013776c11Slogin }
22113776c11Slogin 
22213776c11Slogin impl ops::SubAssign<Duration> for Instant {
22313776c11Slogin     fn sub_assign(&mut self, rhs: Duration) {
22413776c11Slogin         self.micros -= rhs.total_micros() as i64;
22513776c11Slogin     }
22613776c11Slogin }
22713776c11Slogin 
22813776c11Slogin impl ops::Sub<Instant> for Instant {
22913776c11Slogin     type Output = Duration;
23013776c11Slogin 
23113776c11Slogin     fn sub(self, rhs: Instant) -> Duration {
23213776c11Slogin         Duration::from_micros((self.micros - rhs.micros).unsigned_abs())
23313776c11Slogin     }
23413776c11Slogin }
23513776c11Slogin 
23613776c11Slogin /// A relative amount of time.
23713776c11Slogin #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
23813776c11Slogin #[cfg_attr(feature = "defmt", derive(defmt::Format))]
23913776c11Slogin pub struct Duration {
24013776c11Slogin     micros: u64,
24113776c11Slogin }
24213776c11Slogin 
24313776c11Slogin impl Duration {
24413776c11Slogin     pub const ZERO: Duration = Duration::from_micros(0);
24513776c11Slogin     /// Create a new `Duration` from a number of microseconds.
24613776c11Slogin     pub const fn from_micros(micros: u64) -> Duration {
24713776c11Slogin         Duration { micros }
24813776c11Slogin     }
24913776c11Slogin 
25013776c11Slogin     /// Create a new `Duration` from a number of milliseconds.
25113776c11Slogin     pub const fn from_millis(millis: u64) -> Duration {
25213776c11Slogin         Duration {
25313776c11Slogin             micros: millis * 1000,
25413776c11Slogin         }
25513776c11Slogin     }
25613776c11Slogin 
25713776c11Slogin     /// Create a new `Instant` from a number of seconds.
25813776c11Slogin     pub const fn from_secs(secs: u64) -> Duration {
25913776c11Slogin         Duration {
26013776c11Slogin             micros: secs * 1000000,
26113776c11Slogin         }
26213776c11Slogin     }
26313776c11Slogin 
26413776c11Slogin     /// The fractional number of milliseconds in this `Duration`.
26513776c11Slogin     pub const fn millis(&self) -> u64 {
26613776c11Slogin         self.micros / 1000 % 1000
26713776c11Slogin     }
26813776c11Slogin 
26913776c11Slogin     /// The fractional number of milliseconds in this `Duration`.
27013776c11Slogin     pub const fn micros(&self) -> u64 {
27113776c11Slogin         self.micros % 1000000
27213776c11Slogin     }
27313776c11Slogin 
27413776c11Slogin     /// The number of whole seconds in this `Duration`.
27513776c11Slogin     pub const fn secs(&self) -> u64 {
27613776c11Slogin         self.micros / 1000000
27713776c11Slogin     }
27813776c11Slogin 
27913776c11Slogin     /// The total number of milliseconds in this `Duration`.
28013776c11Slogin     pub const fn total_millis(&self) -> u64 {
28113776c11Slogin         self.micros / 1000
28213776c11Slogin     }
28313776c11Slogin 
28413776c11Slogin     /// The total number of microseconds in this `Duration`.
28513776c11Slogin     pub const fn total_micros(&self) -> u64 {
28613776c11Slogin         self.micros
28713776c11Slogin     }
28813776c11Slogin }
28913776c11Slogin 
29013776c11Slogin impl fmt::Display for Duration {
29113776c11Slogin     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29213776c11Slogin         write!(f, "{}.{:03}s", self.secs(), self.millis())
29313776c11Slogin     }
29413776c11Slogin }
29513776c11Slogin 
29613776c11Slogin impl ops::Add<Duration> for Duration {
29713776c11Slogin     type Output = Duration;
29813776c11Slogin 
29913776c11Slogin     fn add(self, rhs: Duration) -> Duration {
30013776c11Slogin         Duration::from_micros(self.micros + rhs.total_micros())
30113776c11Slogin     }
30213776c11Slogin }
30313776c11Slogin 
30413776c11Slogin impl ops::AddAssign<Duration> for Duration {
30513776c11Slogin     fn add_assign(&mut self, rhs: Duration) {
30613776c11Slogin         self.micros += rhs.total_micros();
30713776c11Slogin     }
30813776c11Slogin }
30913776c11Slogin 
31013776c11Slogin impl ops::Sub<Duration> for Duration {
31113776c11Slogin     type Output = Duration;
31213776c11Slogin 
31313776c11Slogin     fn sub(self, rhs: Duration) -> Duration {
31413776c11Slogin         Duration::from_micros(
31513776c11Slogin             self.micros
31613776c11Slogin                 .checked_sub(rhs.total_micros())
31713776c11Slogin                 .expect("overflow when subtracting durations"),
31813776c11Slogin         )
31913776c11Slogin     }
32013776c11Slogin }
32113776c11Slogin 
32213776c11Slogin impl ops::SubAssign<Duration> for Duration {
32313776c11Slogin     fn sub_assign(&mut self, rhs: Duration) {
32413776c11Slogin         self.micros = self
32513776c11Slogin             .micros
32613776c11Slogin             .checked_sub(rhs.total_micros())
32713776c11Slogin             .expect("overflow when subtracting durations");
32813776c11Slogin     }
32913776c11Slogin }
33013776c11Slogin 
33113776c11Slogin impl ops::Mul<u32> for Duration {
33213776c11Slogin     type Output = Duration;
33313776c11Slogin 
33413776c11Slogin     fn mul(self, rhs: u32) -> Duration {
33513776c11Slogin         Duration::from_micros(self.micros * rhs as u64)
33613776c11Slogin     }
33713776c11Slogin }
33813776c11Slogin 
33913776c11Slogin impl ops::MulAssign<u32> for Duration {
34013776c11Slogin     fn mul_assign(&mut self, rhs: u32) {
34113776c11Slogin         self.micros *= rhs as u64;
34213776c11Slogin     }
34313776c11Slogin }
34413776c11Slogin 
34513776c11Slogin impl ops::Div<u32> for Duration {
34613776c11Slogin     type Output = Duration;
34713776c11Slogin 
34813776c11Slogin     fn div(self, rhs: u32) -> Duration {
34913776c11Slogin         Duration::from_micros(self.micros / rhs as u64)
35013776c11Slogin     }
35113776c11Slogin }
35213776c11Slogin 
35313776c11Slogin impl ops::DivAssign<u32> for Duration {
35413776c11Slogin     fn div_assign(&mut self, rhs: u32) {
35513776c11Slogin         self.micros /= rhs as u64;
35613776c11Slogin     }
35713776c11Slogin }
35813776c11Slogin 
35913776c11Slogin impl ops::Shl<u32> for Duration {
36013776c11Slogin     type Output = Duration;
36113776c11Slogin 
36213776c11Slogin     fn shl(self, rhs: u32) -> Duration {
36313776c11Slogin         Duration::from_micros(self.micros << rhs)
36413776c11Slogin     }
36513776c11Slogin }
36613776c11Slogin 
36713776c11Slogin impl ops::ShlAssign<u32> for Duration {
36813776c11Slogin     fn shl_assign(&mut self, rhs: u32) {
36913776c11Slogin         self.micros <<= rhs;
37013776c11Slogin     }
37113776c11Slogin }
37213776c11Slogin 
37313776c11Slogin impl ops::Shr<u32> for Duration {
37413776c11Slogin     type Output = Duration;
37513776c11Slogin 
37613776c11Slogin     fn shr(self, rhs: u32) -> Duration {
37713776c11Slogin         Duration::from_micros(self.micros >> rhs)
37813776c11Slogin     }
37913776c11Slogin }
38013776c11Slogin 
38113776c11Slogin impl ops::ShrAssign<u32> for Duration {
38213776c11Slogin     fn shr_assign(&mut self, rhs: u32) {
38313776c11Slogin         self.micros >>= rhs;
38413776c11Slogin     }
38513776c11Slogin }
38613776c11Slogin 
38713776c11Slogin impl From<::core::time::Duration> for Duration {
38813776c11Slogin     fn from(other: ::core::time::Duration) -> Duration {
38913776c11Slogin         Duration::from_micros(other.as_secs() * 1000000 + other.subsec_micros() as u64)
39013776c11Slogin     }
39113776c11Slogin }
39213776c11Slogin 
39313776c11Slogin impl From<Duration> for ::core::time::Duration {
39413776c11Slogin     fn from(val: Duration) -> Self {
39513776c11Slogin         ::core::time::Duration::from_micros(val.total_micros())
39613776c11Slogin     }
39713776c11Slogin }
39813776c11Slogin 
39913776c11Slogin /// 支持与smoltcp的时间转换
40013776c11Slogin impl From<smoltcp::time::Instant> for Instant {
40113776c11Slogin     fn from(val: smoltcp::time::Instant) -> Self {
40213776c11Slogin         Instant::from_micros(val.micros())
40313776c11Slogin     }
40413776c11Slogin }
40513776c11Slogin 
406840045afSLoGin impl From<Instant> for smoltcp::time::Instant {
407840045afSLoGin     fn from(val: Instant) -> Self {
408840045afSLoGin         smoltcp::time::Instant::from_millis(val.millis())
40913776c11Slogin     }
41013776c11Slogin }
41113776c11Slogin 
41213776c11Slogin /// 支持与smoltcp的时间转换
41313776c11Slogin impl From<smoltcp::time::Duration> for Duration {
41413776c11Slogin     fn from(val: smoltcp::time::Duration) -> Self {
41513776c11Slogin         Duration::from_micros(val.micros())
41613776c11Slogin     }
41713776c11Slogin }
41813776c11Slogin 
419840045afSLoGin impl From<Duration> for smoltcp::time::Duration {
420840045afSLoGin     fn from(val: Duration) -> Self {
421840045afSLoGin         smoltcp::time::Duration::from_millis(val.millis())
42213776c11Slogin     }
42313776c11Slogin }
424fbe6becdSLoGin 
425fbe6becdSLoGin pub trait TimeArch {
426fbe6becdSLoGin     /// Get CPU cycles (Read from register)
427fbe6becdSLoGin     fn get_cycles() -> usize;
428*8cb2e9b3SLoGin 
429*8cb2e9b3SLoGin     /// Calculate expire cycles
430*8cb2e9b3SLoGin     ///
431*8cb2e9b3SLoGin     /// # Arguments
432*8cb2e9b3SLoGin     ///
433*8cb2e9b3SLoGin     /// - `ns` - The time to expire in nanoseconds
434*8cb2e9b3SLoGin     ///
435*8cb2e9b3SLoGin     /// # Returns
436*8cb2e9b3SLoGin     ///
437*8cb2e9b3SLoGin     /// The expire cycles
438*8cb2e9b3SLoGin     fn cal_expire_cycles(ns: usize) -> usize;
439fbe6becdSLoGin }
440