18612b6ceSLoGin use core::{ 28612b6ceSLoGin fmt, 38d72b68dSJomo intrinsics::unlikely, 48612b6ceSLoGin ops::{self, Sub}, 58612b6ceSLoGin }; 613776c11Slogin 78d72b68dSJomo use crate::arch::CurrentTimeArch; 86f189d27Slinfeng use crate::time::syscall::PosixTimeval; 98d72b68dSJomo 10881ff6f9Syuyi2439 use self::timekeeping::getnstimeofday; 1113776c11Slogin 1236fd0130Shoumkh pub mod clocksource; 1336fd0130Shoumkh pub mod jiffies; 14bacd691cSlogin pub mod sleep; 15ab5c8ca4Slogin pub mod syscall; 16*af097f9fS黄铭涛 pub mod tick_common; 1736fd0130Shoumkh pub mod timeconv; 1801876902SGou Ngai pub mod timekeep; 1936fd0130Shoumkh pub mod timekeeping; 20bacd691cSlogin pub mod timer; 21da152319SLoGin 2213776c11Slogin /* Time structures. (Partitially taken from smoltcp) 2313776c11Slogin 2413776c11Slogin The `time` module contains structures used to represent both 2513776c11Slogin absolute and relative time. 2613776c11Slogin 2713776c11Slogin - [Instant] is used to represent absolute time. 2813776c11Slogin - [Duration] is used to represent relative time. 2913776c11Slogin 3013776c11Slogin [Instant]: struct.Instant.html 3113776c11Slogin [Duration]: struct.Duration.html 3213776c11Slogin */ 3336fd0130Shoumkh #[allow(dead_code)] 3436fd0130Shoumkh pub const MSEC_PER_SEC: u32 = 1000; 3536fd0130Shoumkh #[allow(dead_code)] 3636fd0130Shoumkh pub const USEC_PER_MSEC: u32 = 1000; 3736fd0130Shoumkh #[allow(dead_code)] 3836fd0130Shoumkh pub const NSEC_PER_USEC: u32 = 1000; 3936fd0130Shoumkh #[allow(dead_code)] 4036fd0130Shoumkh pub const NSEC_PER_MSEC: u32 = 1000000; 4136fd0130Shoumkh #[allow(dead_code)] 4236fd0130Shoumkh pub const USEC_PER_SEC: u32 = 1000000; 4336fd0130Shoumkh #[allow(dead_code)] 4436fd0130Shoumkh pub const NSEC_PER_SEC: u32 = 1000000000; 4536fd0130Shoumkh #[allow(dead_code)] 4636fd0130Shoumkh pub const FSEC_PER_SEC: u64 = 1000000000000000; 4713776c11Slogin 48dd8e74efSMingtao Huang /// The clock frequency of the i8253/i8254 PIT 49dd8e74efSMingtao Huang pub const PIT_TICK_RATE: u64 = 1193182; 50dd8e74efSMingtao Huang 51ab5c8ca4Slogin /// 表示时间的结构体,符合POSIX标准。 52004e86ffSlogin #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] 53ab5c8ca4Slogin #[repr(C)] 546fc066acSJomo pub struct PosixTimeSpec { 55004e86ffSlogin pub tv_sec: i64, 56004e86ffSlogin pub tv_nsec: i64, 57004e86ffSlogin } 5820e3152eSlogin 596fc066acSJomo impl PosixTimeSpec { 6013776c11Slogin #[allow(dead_code)] new(sec: i64, nsec: i64) -> PosixTimeSpec616fc066acSJomo pub fn new(sec: i64, nsec: i64) -> PosixTimeSpec { 626fc066acSJomo return PosixTimeSpec { 6320e3152eSlogin tv_sec: sec, 6420e3152eSlogin tv_nsec: nsec, 6520e3152eSlogin }; 6620e3152eSlogin } 678d72b68dSJomo 688d72b68dSJomo /// 获取当前时间 696fc066acSJomo #[inline(always)] now() -> Self708d72b68dSJomo pub fn now() -> Self { 716fc066acSJomo getnstimeofday() 726fc066acSJomo } 736fc066acSJomo 746fc066acSJomo /// 获取当前CPU时间(使用CPU时钟周期计算,会存在回绕问题) now_cpu_time() -> Self756fc066acSJomo pub fn now_cpu_time() -> Self { 768d72b68dSJomo #[cfg(target_arch = "x86_64")] 778d72b68dSJomo { 788d72b68dSJomo use crate::arch::driver::tsc::TSCManager; 798d72b68dSJomo let khz = TSCManager::cpu_khz(); 808d72b68dSJomo if unlikely(khz == 0) { 816fc066acSJomo return PosixTimeSpec::default(); 828d72b68dSJomo } else { 838d72b68dSJomo return Self::from(Duration::from_millis( 848d72b68dSJomo CurrentTimeArch::get_cycles() as u64 / khz, 858d72b68dSJomo )); 868d72b68dSJomo } 878d72b68dSJomo } 888d72b68dSJomo 898d72b68dSJomo #[cfg(target_arch = "riscv64")] 908d72b68dSJomo { 916fc066acSJomo return PosixTimeSpec::new(0, 0); 928d72b68dSJomo } 9320e3152eSlogin } 9413776c11Slogin 956fc066acSJomo /// 换算成纳秒 total_nanos(&self) -> i64966fc066acSJomo pub fn total_nanos(&self) -> i64 { 976fc066acSJomo self.tv_sec * 1000000000 + self.tv_nsec 986fc066acSJomo } 996fc066acSJomo } 1006fc066acSJomo 1016fc066acSJomo impl Sub for PosixTimeSpec { 1028612b6ceSLoGin type Output = Duration; sub(self, rhs: Self) -> Self::Output1038612b6ceSLoGin fn sub(self, rhs: Self) -> Self::Output { 1048612b6ceSLoGin let sec = self.tv_sec.checked_sub(rhs.tv_sec).unwrap_or(0); 1058612b6ceSLoGin let nsec = self.tv_nsec.checked_sub(rhs.tv_nsec).unwrap_or(0); 1068612b6ceSLoGin Duration::from_micros((sec * 1000000 + nsec / 1000) as u64) 1078612b6ceSLoGin } 1088612b6ceSLoGin } 1098612b6ceSLoGin 1106fc066acSJomo impl From<Duration> for PosixTimeSpec { from(dur: Duration) -> Self1118612b6ceSLoGin fn from(dur: Duration) -> Self { 1126fc066acSJomo PosixTimeSpec { 1138612b6ceSLoGin tv_sec: dur.total_micros() as i64 / 1000000, 1148612b6ceSLoGin tv_nsec: (dur.total_micros() as i64 % 1000000) * 1000, 1158612b6ceSLoGin } 1168612b6ceSLoGin } 1178612b6ceSLoGin } 1188612b6ceSLoGin 1196f189d27Slinfeng impl From<PosixTimeval> for PosixTimeSpec { from(value: PosixTimeval) -> Self1206f189d27Slinfeng fn from(value: PosixTimeval) -> Self { 1216f189d27Slinfeng PosixTimeSpec { 1226f189d27Slinfeng tv_sec: value.tv_sec, 1236f189d27Slinfeng tv_nsec: value.tv_usec as i64 * 1000, 1246f189d27Slinfeng } 1256f189d27Slinfeng } 1266f189d27Slinfeng } 1276f189d27Slinfeng 1286fc066acSJomo impl From<PosixTimeSpec> for Duration { from(val: PosixTimeSpec) -> Self1296fc066acSJomo fn from(val: PosixTimeSpec) -> Self { 130840045afSLoGin Duration::from_micros(val.tv_sec as u64 * 1000000 + val.tv_nsec as u64 / 1000) 1318612b6ceSLoGin } 1328612b6ceSLoGin } 1338612b6ceSLoGin 13413776c11Slogin /// A representation of an absolute time value. 13513776c11Slogin /// 13613776c11Slogin /// The `Instant` type is a wrapper around a `i64` value that 13713776c11Slogin /// represents a number of microseconds, monotonically increasing 13813776c11Slogin /// since an arbitrary moment in time, such as system startup. 13913776c11Slogin /// 14013776c11Slogin /// * A value of `0` is inherently arbitrary. 14113776c11Slogin /// * A value less than `0` indicates a time before the starting 14213776c11Slogin /// point. 14313776c11Slogin #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] 14413776c11Slogin pub struct Instant { 14513776c11Slogin micros: i64, 14613776c11Slogin } 14713776c11Slogin 14813776c11Slogin #[allow(dead_code)] 14913776c11Slogin impl Instant { 15013776c11Slogin pub const ZERO: Instant = Instant::from_micros_const(0); 15113776c11Slogin 152da152319SLoGin /// mktime64 - 将日期转换为秒。 153da152319SLoGin /// 154da152319SLoGin /// ## 参数 155da152319SLoGin /// 156da152319SLoGin /// - year0: 要转换的年份 157da152319SLoGin /// - mon0: 要转换的月份 158da152319SLoGin /// - day: 要转换的天 159da152319SLoGin /// - hour: 要转换的小时 160da152319SLoGin /// - min: 要转换的分钟 161da152319SLoGin /// - sec: 要转换的秒 162da152319SLoGin /// 163da152319SLoGin /// 将公历日期转换为1970-01-01 00:00:00以来的秒数。 164da152319SLoGin /// 假设输入为正常的日期格式,即1980-12-31 23:59:59 => 年份=1980, 月=12, 日=31, 时=23, 分=59, 秒=59。 165da152319SLoGin /// 166da152319SLoGin /// [For the Julian calendar(俄罗斯在1917年之前使用,英国及其殖民地在大西洋1752年之前使用, 167da152319SLoGin /// 其他地方在1582年之前使用,某些社区仍然在使用)省略-year/100+year/400项, 168da152319SLoGin /// 并在结果上加10。] 169da152319SLoGin /// 170da152319SLoGin /// 这个算法最初由高斯(我认为是)发表。 171da152319SLoGin /// 172da152319SLoGin /// 要表示闰秒,可以通过将sec设为60(在ISO 8601允许)来调用此函数。 173da152319SLoGin /// 闰秒与随后的秒一样处理,因为它们在UNIX时间中不存在。 174da152319SLoGin /// 175da152319SLoGin /// 支持将午夜作为当日末尾的24:00:00编码 - 即明天的午夜(在ISO 8601允许)。 176da152319SLoGin /// 177da152319SLoGin /// ## 返回 178da152319SLoGin /// 179da152319SLoGin /// 返回:给定输入日期自1970-01-01 00:00:00以来的秒数 mktime64(year0: u32, mon0: u32, day: u32, hour: u32, min: u32, sec: u32) -> Self180da152319SLoGin pub fn mktime64(year0: u32, mon0: u32, day: u32, hour: u32, min: u32, sec: u32) -> Self { 181da152319SLoGin let mut mon: i64 = mon0.into(); 182da152319SLoGin let mut year: u64 = year0.into(); 183da152319SLoGin let day: u64 = day.into(); 184da152319SLoGin let hour: u64 = hour.into(); 185da152319SLoGin let min: u64 = min.into(); 186da152319SLoGin let sec: u64 = sec.into(); 187da152319SLoGin 188da152319SLoGin mon -= 2; 189da152319SLoGin /* 1..12 -> 11,12,1..10 */ 190da152319SLoGin if mon <= 0 { 191da152319SLoGin /* Puts Feb last since it has leap day */ 192da152319SLoGin mon += 12; 193da152319SLoGin year -= 1; 194da152319SLoGin } 195da152319SLoGin let mon = mon as u64; 196da152319SLoGin 197da152319SLoGin let secs = ((((year / 4 - year / 100 + year / 400 + 367 * mon / 12 + day) + year * 365 198da152319SLoGin - 719499) 199da152319SLoGin * 24 + hour) /* now have hours - midnight tomorrow handled here */ 200da152319SLoGin * 60 + min)/* now have minutes */ 201da152319SLoGin * 60 202da152319SLoGin + sec; /* finally seconds */ 203da152319SLoGin 204da152319SLoGin Self::from_secs(secs as i64) 205da152319SLoGin } 206da152319SLoGin 20713776c11Slogin /// Create a new `Instant` from a number of microseconds. from_micros<T: Into<i64>>(micros: T) -> Instant20813776c11Slogin pub fn from_micros<T: Into<i64>>(micros: T) -> Instant { 20913776c11Slogin Instant { 21013776c11Slogin micros: micros.into(), 21113776c11Slogin } 21213776c11Slogin } 21313776c11Slogin from_micros_const(micros: i64) -> Instant21413776c11Slogin pub const fn from_micros_const(micros: i64) -> Instant { 21513776c11Slogin Instant { micros } 21613776c11Slogin } 21713776c11Slogin 21813776c11Slogin /// Create a new `Instant` from a number of milliseconds. from_millis<T: Into<i64>>(millis: T) -> Instant21913776c11Slogin pub fn from_millis<T: Into<i64>>(millis: T) -> Instant { 22013776c11Slogin Instant { 22113776c11Slogin micros: millis.into() * 1000, 22213776c11Slogin } 22313776c11Slogin } 22413776c11Slogin 22513776c11Slogin /// Create a new `Instant` from a number of milliseconds. from_millis_const(millis: i64) -> Instant22613776c11Slogin pub const fn from_millis_const(millis: i64) -> Instant { 22713776c11Slogin Instant { 22813776c11Slogin micros: millis * 1000, 22913776c11Slogin } 23013776c11Slogin } 23113776c11Slogin 23213776c11Slogin /// Create a new `Instant` from a number of seconds. from_secs<T: Into<i64>>(secs: T) -> Instant23313776c11Slogin pub fn from_secs<T: Into<i64>>(secs: T) -> Instant { 23413776c11Slogin Instant { 23513776c11Slogin micros: secs.into() * 1000000, 23613776c11Slogin } 23713776c11Slogin } 23813776c11Slogin 23913776c11Slogin /// Create a new `Instant` from the current time now() -> Instant24013776c11Slogin pub fn now() -> Instant { 241881ff6f9Syuyi2439 let tm = getnstimeofday(); 242881ff6f9Syuyi2439 Self::from_micros(tm.tv_sec * 1000000 + tm.tv_nsec / 1000) 24313776c11Slogin } 24413776c11Slogin 24513776c11Slogin /// The fractional number of milliseconds that have passed 24613776c11Slogin /// since the beginning of time. millis(&self) -> i6424713776c11Slogin pub const fn millis(&self) -> i64 { 24813776c11Slogin self.micros % 1000000 / 1000 24913776c11Slogin } 25013776c11Slogin 25113776c11Slogin /// The fractional number of microseconds that have passed 25213776c11Slogin /// since the beginning of time. micros(&self) -> i6425313776c11Slogin pub const fn micros(&self) -> i64 { 25413776c11Slogin self.micros % 1000000 25513776c11Slogin } 25613776c11Slogin 25713776c11Slogin /// The number of whole seconds that have passed since the 25813776c11Slogin /// beginning of time. secs(&self) -> i6425913776c11Slogin pub const fn secs(&self) -> i64 { 26013776c11Slogin self.micros / 1000000 26113776c11Slogin } 26213776c11Slogin 26313776c11Slogin /// The total number of milliseconds that have passed since 26413776c11Slogin /// the beginning of time. total_millis(&self) -> i6426513776c11Slogin pub const fn total_millis(&self) -> i64 { 26613776c11Slogin self.micros / 1000 26713776c11Slogin } 26813776c11Slogin /// The total number of milliseconds that have passed since 26913776c11Slogin /// the beginning of time. total_micros(&self) -> i6427013776c11Slogin pub const fn total_micros(&self) -> i64 { 27113776c11Slogin self.micros 27213776c11Slogin } 27313776c11Slogin } 27413776c11Slogin 27513776c11Slogin impl fmt::Display for Instant { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result27613776c11Slogin fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 27713776c11Slogin write!(f, "{}.{:0>3}s", self.secs(), self.millis()) 27813776c11Slogin } 27913776c11Slogin } 28013776c11Slogin 28113776c11Slogin impl ops::Add<Duration> for Instant { 28213776c11Slogin type Output = Instant; 28313776c11Slogin add(self, rhs: Duration) -> Instant28413776c11Slogin fn add(self, rhs: Duration) -> Instant { 28513776c11Slogin Instant::from_micros(self.micros + rhs.total_micros() as i64) 28613776c11Slogin } 28713776c11Slogin } 28813776c11Slogin 28913776c11Slogin impl ops::AddAssign<Duration> for Instant { add_assign(&mut self, rhs: Duration)29013776c11Slogin fn add_assign(&mut self, rhs: Duration) { 29113776c11Slogin self.micros += rhs.total_micros() as i64; 29213776c11Slogin } 29313776c11Slogin } 29413776c11Slogin 29513776c11Slogin impl ops::Sub<Duration> for Instant { 29613776c11Slogin type Output = Instant; 29713776c11Slogin sub(self, rhs: Duration) -> Instant29813776c11Slogin fn sub(self, rhs: Duration) -> Instant { 29913776c11Slogin Instant::from_micros(self.micros - rhs.total_micros() as i64) 30013776c11Slogin } 30113776c11Slogin } 30213776c11Slogin 30313776c11Slogin impl ops::SubAssign<Duration> for Instant { sub_assign(&mut self, rhs: Duration)30413776c11Slogin fn sub_assign(&mut self, rhs: Duration) { 30513776c11Slogin self.micros -= rhs.total_micros() as i64; 30613776c11Slogin } 30713776c11Slogin } 30813776c11Slogin 30913776c11Slogin impl ops::Sub<Instant> for Instant { 31013776c11Slogin type Output = Duration; 31113776c11Slogin sub(self, rhs: Instant) -> Duration31213776c11Slogin fn sub(self, rhs: Instant) -> Duration { 31313776c11Slogin Duration::from_micros((self.micros - rhs.micros).unsigned_abs()) 31413776c11Slogin } 31513776c11Slogin } 31613776c11Slogin 31713776c11Slogin /// A relative amount of time. 31813776c11Slogin #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] 31913776c11Slogin pub struct Duration { 32013776c11Slogin micros: u64, 32113776c11Slogin } 32213776c11Slogin 32313776c11Slogin impl Duration { 32413776c11Slogin pub const ZERO: Duration = Duration::from_micros(0); 32513776c11Slogin /// Create a new `Duration` from a number of microseconds. from_micros(micros: u64) -> Duration32613776c11Slogin pub const fn from_micros(micros: u64) -> Duration { 32713776c11Slogin Duration { micros } 32813776c11Slogin } 32913776c11Slogin 33013776c11Slogin /// Create a new `Duration` from a number of milliseconds. from_millis(millis: u64) -> Duration33113776c11Slogin pub const fn from_millis(millis: u64) -> Duration { 33213776c11Slogin Duration { 33313776c11Slogin micros: millis * 1000, 33413776c11Slogin } 33513776c11Slogin } 33613776c11Slogin 33713776c11Slogin /// Create a new `Instant` from a number of seconds. from_secs(secs: u64) -> Duration33813776c11Slogin pub const fn from_secs(secs: u64) -> Duration { 33913776c11Slogin Duration { 34013776c11Slogin micros: secs * 1000000, 34113776c11Slogin } 34213776c11Slogin } 34313776c11Slogin 34413776c11Slogin /// The fractional number of milliseconds in this `Duration`. millis(&self) -> u6434513776c11Slogin pub const fn millis(&self) -> u64 { 34613776c11Slogin self.micros / 1000 % 1000 34713776c11Slogin } 34813776c11Slogin 34913776c11Slogin /// The fractional number of milliseconds in this `Duration`. micros(&self) -> u6435013776c11Slogin pub const fn micros(&self) -> u64 { 35113776c11Slogin self.micros % 1000000 35213776c11Slogin } 35313776c11Slogin 35413776c11Slogin /// The number of whole seconds in this `Duration`. secs(&self) -> u6435513776c11Slogin pub const fn secs(&self) -> u64 { 35613776c11Slogin self.micros / 1000000 35713776c11Slogin } 35813776c11Slogin 35913776c11Slogin /// The total number of milliseconds in this `Duration`. total_millis(&self) -> u6436013776c11Slogin pub const fn total_millis(&self) -> u64 { 36113776c11Slogin self.micros / 1000 36213776c11Slogin } 36313776c11Slogin 36413776c11Slogin /// The total number of microseconds in this `Duration`. total_micros(&self) -> u6436513776c11Slogin pub const fn total_micros(&self) -> u64 { 36613776c11Slogin self.micros 36713776c11Slogin } 36813776c11Slogin } 36913776c11Slogin 37013776c11Slogin impl fmt::Display for Duration { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result37113776c11Slogin fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 37213776c11Slogin write!(f, "{}.{:03}s", self.secs(), self.millis()) 37313776c11Slogin } 37413776c11Slogin } 37513776c11Slogin 37613776c11Slogin impl ops::Add<Duration> for Duration { 37713776c11Slogin type Output = Duration; 37813776c11Slogin add(self, rhs: Duration) -> Duration37913776c11Slogin fn add(self, rhs: Duration) -> Duration { 38013776c11Slogin Duration::from_micros(self.micros + rhs.total_micros()) 38113776c11Slogin } 38213776c11Slogin } 38313776c11Slogin 38413776c11Slogin impl ops::AddAssign<Duration> for Duration { add_assign(&mut self, rhs: Duration)38513776c11Slogin fn add_assign(&mut self, rhs: Duration) { 38613776c11Slogin self.micros += rhs.total_micros(); 38713776c11Slogin } 38813776c11Slogin } 38913776c11Slogin 39013776c11Slogin impl ops::Sub<Duration> for Duration { 39113776c11Slogin type Output = Duration; 39213776c11Slogin sub(self, rhs: Duration) -> Duration39313776c11Slogin fn sub(self, rhs: Duration) -> Duration { 39413776c11Slogin Duration::from_micros( 39513776c11Slogin self.micros 39613776c11Slogin .checked_sub(rhs.total_micros()) 39713776c11Slogin .expect("overflow when subtracting durations"), 39813776c11Slogin ) 39913776c11Slogin } 40013776c11Slogin } 40113776c11Slogin 40213776c11Slogin impl ops::SubAssign<Duration> for Duration { sub_assign(&mut self, rhs: Duration)40313776c11Slogin fn sub_assign(&mut self, rhs: Duration) { 40413776c11Slogin self.micros = self 40513776c11Slogin .micros 40613776c11Slogin .checked_sub(rhs.total_micros()) 40713776c11Slogin .expect("overflow when subtracting durations"); 40813776c11Slogin } 40913776c11Slogin } 41013776c11Slogin 41113776c11Slogin impl ops::Mul<u32> for Duration { 41213776c11Slogin type Output = Duration; 41313776c11Slogin mul(self, rhs: u32) -> Duration41413776c11Slogin fn mul(self, rhs: u32) -> Duration { 41513776c11Slogin Duration::from_micros(self.micros * rhs as u64) 41613776c11Slogin } 41713776c11Slogin } 41813776c11Slogin 41913776c11Slogin impl ops::MulAssign<u32> for Duration { mul_assign(&mut self, rhs: u32)42013776c11Slogin fn mul_assign(&mut self, rhs: u32) { 42113776c11Slogin self.micros *= rhs as u64; 42213776c11Slogin } 42313776c11Slogin } 42413776c11Slogin 42513776c11Slogin impl ops::Div<u32> for Duration { 42613776c11Slogin type Output = Duration; 42713776c11Slogin div(self, rhs: u32) -> Duration42813776c11Slogin fn div(self, rhs: u32) -> Duration { 42913776c11Slogin Duration::from_micros(self.micros / rhs as u64) 43013776c11Slogin } 43113776c11Slogin } 43213776c11Slogin 43313776c11Slogin impl ops::DivAssign<u32> for Duration { div_assign(&mut self, rhs: u32)43413776c11Slogin fn div_assign(&mut self, rhs: u32) { 43513776c11Slogin self.micros /= rhs as u64; 43613776c11Slogin } 43713776c11Slogin } 43813776c11Slogin 43913776c11Slogin impl ops::Shl<u32> for Duration { 44013776c11Slogin type Output = Duration; 44113776c11Slogin shl(self, rhs: u32) -> Duration44213776c11Slogin fn shl(self, rhs: u32) -> Duration { 44313776c11Slogin Duration::from_micros(self.micros << rhs) 44413776c11Slogin } 44513776c11Slogin } 44613776c11Slogin 44713776c11Slogin impl ops::ShlAssign<u32> for Duration { shl_assign(&mut self, rhs: u32)44813776c11Slogin fn shl_assign(&mut self, rhs: u32) { 44913776c11Slogin self.micros <<= rhs; 45013776c11Slogin } 45113776c11Slogin } 45213776c11Slogin 45313776c11Slogin impl ops::Shr<u32> for Duration { 45413776c11Slogin type Output = Duration; 45513776c11Slogin shr(self, rhs: u32) -> Duration45613776c11Slogin fn shr(self, rhs: u32) -> Duration { 45713776c11Slogin Duration::from_micros(self.micros >> rhs) 45813776c11Slogin } 45913776c11Slogin } 46013776c11Slogin 46113776c11Slogin impl ops::ShrAssign<u32> for Duration { shr_assign(&mut self, rhs: u32)46213776c11Slogin fn shr_assign(&mut self, rhs: u32) { 46313776c11Slogin self.micros >>= rhs; 46413776c11Slogin } 46513776c11Slogin } 46613776c11Slogin 46713776c11Slogin impl From<::core::time::Duration> for Duration { from(other: ::core::time::Duration) -> Duration46813776c11Slogin fn from(other: ::core::time::Duration) -> Duration { 46913776c11Slogin Duration::from_micros(other.as_secs() * 1000000 + other.subsec_micros() as u64) 47013776c11Slogin } 47113776c11Slogin } 47213776c11Slogin 47313776c11Slogin impl From<Duration> for ::core::time::Duration { from(val: Duration) -> Self47413776c11Slogin fn from(val: Duration) -> Self { 47513776c11Slogin ::core::time::Duration::from_micros(val.total_micros()) 47613776c11Slogin } 47713776c11Slogin } 47813776c11Slogin 47913776c11Slogin /// 支持与smoltcp的时间转换 48013776c11Slogin impl From<smoltcp::time::Instant> for Instant { from(val: smoltcp::time::Instant) -> Self48113776c11Slogin fn from(val: smoltcp::time::Instant) -> Self { 48213776c11Slogin Instant::from_micros(val.micros()) 48313776c11Slogin } 48413776c11Slogin } 48513776c11Slogin 486840045afSLoGin impl From<Instant> for smoltcp::time::Instant { from(val: Instant) -> Self487840045afSLoGin fn from(val: Instant) -> Self { 488840045afSLoGin smoltcp::time::Instant::from_millis(val.millis()) 48913776c11Slogin } 49013776c11Slogin } 49113776c11Slogin 49213776c11Slogin /// 支持与smoltcp的时间转换 49313776c11Slogin impl From<smoltcp::time::Duration> for Duration { from(val: smoltcp::time::Duration) -> Self49413776c11Slogin fn from(val: smoltcp::time::Duration) -> Self { 49513776c11Slogin Duration::from_micros(val.micros()) 49613776c11Slogin } 49713776c11Slogin } 49813776c11Slogin 499840045afSLoGin impl From<Duration> for smoltcp::time::Duration { from(val: Duration) -> Self500840045afSLoGin fn from(val: Duration) -> Self { 501840045afSLoGin smoltcp::time::Duration::from_millis(val.millis()) 50213776c11Slogin } 50313776c11Slogin } 504fbe6becdSLoGin 505fbe6becdSLoGin pub trait TimeArch { 506fbe6becdSLoGin /// Get CPU cycles (Read from register) get_cycles() -> usize507fbe6becdSLoGin fn get_cycles() -> usize; 5088cb2e9b3SLoGin 5098cb2e9b3SLoGin /// Calculate expire cycles 5108cb2e9b3SLoGin /// 5118cb2e9b3SLoGin /// # Arguments 5128cb2e9b3SLoGin /// 5138cb2e9b3SLoGin /// - `ns` - The time to expire in nanoseconds 5148cb2e9b3SLoGin /// 5158cb2e9b3SLoGin /// # Returns 5168cb2e9b3SLoGin /// 5178cb2e9b3SLoGin /// The expire cycles cal_expire_cycles(ns: usize) -> usize5188cb2e9b3SLoGin fn cal_expire_cycles(ns: usize) -> usize; 519b8ed3825SDonkey Kane 520b8ed3825SDonkey Kane /// 将CPU的时钟周期数转换为纳秒 cycles2ns(cycles: usize) -> usize521b8ed3825SDonkey Kane fn cycles2ns(cycles: usize) -> usize; 522fbe6becdSLoGin } 523