123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- use core::{
- fmt,
- intrinsics::unlikely,
- ops::{self, Sub},
- };
- use crate::arch::CurrentTimeArch;
- use self::timekeep::ktime_get_real_ns;
- pub mod clocksource;
- pub mod jiffies;
- pub mod sleep;
- pub mod syscall;
- pub mod timeconv;
- pub mod timekeep;
- pub mod timekeeping;
- pub mod timer;
- /* Time structures. (Partitially taken from smoltcp)
- The `time` module contains structures used to represent both
- absolute and relative time.
- - [Instant] is used to represent absolute time.
- - [Duration] is used to represent relative time.
- [Instant]: struct.Instant.html
- [Duration]: struct.Duration.html
- */
- #[allow(dead_code)]
- pub const MSEC_PER_SEC: u32 = 1000;
- #[allow(dead_code)]
- pub const USEC_PER_MSEC: u32 = 1000;
- #[allow(dead_code)]
- pub const NSEC_PER_USEC: u32 = 1000;
- #[allow(dead_code)]
- pub const NSEC_PER_MSEC: u32 = 1000000;
- #[allow(dead_code)]
- pub const USEC_PER_SEC: u32 = 1000000;
- #[allow(dead_code)]
- pub const NSEC_PER_SEC: u32 = 1000000000;
- #[allow(dead_code)]
- pub const FSEC_PER_SEC: u64 = 1000000000000000;
- /// 表示时间的结构体,符合POSIX标准。
- #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
- #[repr(C)]
- pub struct TimeSpec {
- pub tv_sec: i64,
- pub tv_nsec: i64,
- }
- impl TimeSpec {
- #[allow(dead_code)]
- pub fn new(sec: i64, nsec: i64) -> TimeSpec {
- return TimeSpec {
- tv_sec: sec,
- tv_nsec: nsec,
- };
- }
- /// 获取当前时间
- pub fn now() -> Self {
- #[cfg(target_arch = "x86_64")]
- {
- use crate::arch::driver::tsc::TSCManager;
- let khz = TSCManager::cpu_khz();
- if unlikely(khz == 0) {
- return TimeSpec::default();
- } else {
- return Self::from(Duration::from_millis(
- CurrentTimeArch::get_cycles() as u64 / khz,
- ));
- }
- }
- #[cfg(target_arch = "riscv64")]
- {
- unimplemented!("TimeSpec::now()")
- }
- }
- }
- impl Sub for TimeSpec {
- type Output = Duration;
- fn sub(self, rhs: Self) -> Self::Output {
- let sec = self.tv_sec.checked_sub(rhs.tv_sec).unwrap_or(0);
- let nsec = self.tv_nsec.checked_sub(rhs.tv_nsec).unwrap_or(0);
- Duration::from_micros((sec * 1000000 + nsec / 1000) as u64)
- }
- }
- impl From<Duration> for TimeSpec {
- fn from(dur: Duration) -> Self {
- TimeSpec {
- tv_sec: dur.total_micros() as i64 / 1000000,
- tv_nsec: (dur.total_micros() as i64 % 1000000) * 1000,
- }
- }
- }
- impl Into<Duration> for TimeSpec {
- fn into(self) -> Duration {
- Duration::from_micros(self.tv_sec as u64 * 1000000 + self.tv_nsec as u64 / 1000)
- }
- }
- /// A representation of an absolute time value.
- ///
- /// The `Instant` type is a wrapper around a `i64` value that
- /// represents a number of microseconds, monotonically increasing
- /// since an arbitrary moment in time, such as system startup.
- ///
- /// * A value of `0` is inherently arbitrary.
- /// * A value less than `0` indicates a time before the starting
- /// point.
- #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
- #[cfg_attr(feature = "defmt", derive(defmt::Format))]
- pub struct Instant {
- micros: i64,
- }
- #[allow(dead_code)]
- impl Instant {
- pub const ZERO: Instant = Instant::from_micros_const(0);
- /// Create a new `Instant` from a number of microseconds.
- pub fn from_micros<T: Into<i64>>(micros: T) -> Instant {
- Instant {
- micros: micros.into(),
- }
- }
- pub const fn from_micros_const(micros: i64) -> Instant {
- Instant { micros }
- }
- /// Create a new `Instant` from a number of milliseconds.
- pub fn from_millis<T: Into<i64>>(millis: T) -> Instant {
- Instant {
- micros: millis.into() * 1000,
- }
- }
- /// Create a new `Instant` from a number of milliseconds.
- pub const fn from_millis_const(millis: i64) -> Instant {
- Instant {
- micros: millis * 1000,
- }
- }
- /// Create a new `Instant` from a number of seconds.
- pub fn from_secs<T: Into<i64>>(secs: T) -> Instant {
- Instant {
- micros: secs.into() * 1000000,
- }
- }
- /// Create a new `Instant` from the current time
- pub fn now() -> Instant {
- Self::from_micros(ktime_get_real_ns() / 1000)
- }
- /// The fractional number of milliseconds that have passed
- /// since the beginning of time.
- pub const fn millis(&self) -> i64 {
- self.micros % 1000000 / 1000
- }
- /// The fractional number of microseconds that have passed
- /// since the beginning of time.
- pub const fn micros(&self) -> i64 {
- self.micros % 1000000
- }
- /// The number of whole seconds that have passed since the
- /// beginning of time.
- pub const fn secs(&self) -> i64 {
- self.micros / 1000000
- }
- /// The total number of milliseconds that have passed since
- /// the beginning of time.
- pub const fn total_millis(&self) -> i64 {
- self.micros / 1000
- }
- /// The total number of milliseconds that have passed since
- /// the beginning of time.
- pub const fn total_micros(&self) -> i64 {
- self.micros
- }
- }
- impl fmt::Display for Instant {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}.{:0>3}s", self.secs(), self.millis())
- }
- }
- impl ops::Add<Duration> for Instant {
- type Output = Instant;
- fn add(self, rhs: Duration) -> Instant {
- Instant::from_micros(self.micros + rhs.total_micros() as i64)
- }
- }
- impl ops::AddAssign<Duration> for Instant {
- fn add_assign(&mut self, rhs: Duration) {
- self.micros += rhs.total_micros() as i64;
- }
- }
- impl ops::Sub<Duration> for Instant {
- type Output = Instant;
- fn sub(self, rhs: Duration) -> Instant {
- Instant::from_micros(self.micros - rhs.total_micros() as i64)
- }
- }
- impl ops::SubAssign<Duration> for Instant {
- fn sub_assign(&mut self, rhs: Duration) {
- self.micros -= rhs.total_micros() as i64;
- }
- }
- impl ops::Sub<Instant> for Instant {
- type Output = Duration;
- fn sub(self, rhs: Instant) -> Duration {
- Duration::from_micros((self.micros - rhs.micros).unsigned_abs())
- }
- }
- /// A relative amount of time.
- #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
- #[cfg_attr(feature = "defmt", derive(defmt::Format))]
- pub struct Duration {
- micros: u64,
- }
- impl Duration {
- pub const ZERO: Duration = Duration::from_micros(0);
- /// Create a new `Duration` from a number of microseconds.
- pub const fn from_micros(micros: u64) -> Duration {
- Duration { micros }
- }
- /// Create a new `Duration` from a number of milliseconds.
- pub const fn from_millis(millis: u64) -> Duration {
- Duration {
- micros: millis * 1000,
- }
- }
- /// Create a new `Instant` from a number of seconds.
- pub const fn from_secs(secs: u64) -> Duration {
- Duration {
- micros: secs * 1000000,
- }
- }
- /// The fractional number of milliseconds in this `Duration`.
- pub const fn millis(&self) -> u64 {
- self.micros / 1000 % 1000
- }
- /// The fractional number of milliseconds in this `Duration`.
- pub const fn micros(&self) -> u64 {
- self.micros % 1000000
- }
- /// The number of whole seconds in this `Duration`.
- pub const fn secs(&self) -> u64 {
- self.micros / 1000000
- }
- /// The total number of milliseconds in this `Duration`.
- pub const fn total_millis(&self) -> u64 {
- self.micros / 1000
- }
- /// The total number of microseconds in this `Duration`.
- pub const fn total_micros(&self) -> u64 {
- self.micros
- }
- }
- impl fmt::Display for Duration {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}.{:03}s", self.secs(), self.millis())
- }
- }
- impl ops::Add<Duration> for Duration {
- type Output = Duration;
- fn add(self, rhs: Duration) -> Duration {
- Duration::from_micros(self.micros + rhs.total_micros())
- }
- }
- impl ops::AddAssign<Duration> for Duration {
- fn add_assign(&mut self, rhs: Duration) {
- self.micros += rhs.total_micros();
- }
- }
- impl ops::Sub<Duration> for Duration {
- type Output = Duration;
- fn sub(self, rhs: Duration) -> Duration {
- Duration::from_micros(
- self.micros
- .checked_sub(rhs.total_micros())
- .expect("overflow when subtracting durations"),
- )
- }
- }
- impl ops::SubAssign<Duration> for Duration {
- fn sub_assign(&mut self, rhs: Duration) {
- self.micros = self
- .micros
- .checked_sub(rhs.total_micros())
- .expect("overflow when subtracting durations");
- }
- }
- impl ops::Mul<u32> for Duration {
- type Output = Duration;
- fn mul(self, rhs: u32) -> Duration {
- Duration::from_micros(self.micros * rhs as u64)
- }
- }
- impl ops::MulAssign<u32> for Duration {
- fn mul_assign(&mut self, rhs: u32) {
- self.micros *= rhs as u64;
- }
- }
- impl ops::Div<u32> for Duration {
- type Output = Duration;
- fn div(self, rhs: u32) -> Duration {
- Duration::from_micros(self.micros / rhs as u64)
- }
- }
- impl ops::DivAssign<u32> for Duration {
- fn div_assign(&mut self, rhs: u32) {
- self.micros /= rhs as u64;
- }
- }
- impl ops::Shl<u32> for Duration {
- type Output = Duration;
- fn shl(self, rhs: u32) -> Duration {
- Duration::from_micros(self.micros << rhs)
- }
- }
- impl ops::ShlAssign<u32> for Duration {
- fn shl_assign(&mut self, rhs: u32) {
- self.micros <<= rhs;
- }
- }
- impl ops::Shr<u32> for Duration {
- type Output = Duration;
- fn shr(self, rhs: u32) -> Duration {
- Duration::from_micros(self.micros >> rhs)
- }
- }
- impl ops::ShrAssign<u32> for Duration {
- fn shr_assign(&mut self, rhs: u32) {
- self.micros >>= rhs;
- }
- }
- impl From<::core::time::Duration> for Duration {
- fn from(other: ::core::time::Duration) -> Duration {
- Duration::from_micros(other.as_secs() * 1000000 + other.subsec_micros() as u64)
- }
- }
- impl From<Duration> for ::core::time::Duration {
- fn from(val: Duration) -> Self {
- ::core::time::Duration::from_micros(val.total_micros())
- }
- }
- /// 支持与smoltcp的时间转换
- impl From<smoltcp::time::Instant> for Instant {
- fn from(val: smoltcp::time::Instant) -> Self {
- Instant::from_micros(val.micros())
- }
- }
- impl Into<smoltcp::time::Instant> for Instant {
- fn into(self) -> smoltcp::time::Instant {
- smoltcp::time::Instant::from_millis(self.millis())
- }
- }
- /// 支持与smoltcp的时间转换
- impl From<smoltcp::time::Duration> for Duration {
- fn from(val: smoltcp::time::Duration) -> Self {
- Duration::from_micros(val.micros())
- }
- }
- impl Into<smoltcp::time::Duration> for Duration {
- fn into(self) -> smoltcp::time::Duration {
- smoltcp::time::Duration::from_millis(self.millis())
- }
- }
- pub trait TimeArch {
- /// Get CPU cycles (Read from register)
- fn get_cycles() -> usize;
- }
|