mod.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. use core::{
  2. fmt,
  3. intrinsics::unlikely,
  4. ops::{self, Sub},
  5. };
  6. use crate::arch::CurrentTimeArch;
  7. use self::timekeep::ktime_get_real_ns;
  8. pub mod clocksource;
  9. pub mod jiffies;
  10. pub mod sleep;
  11. pub mod syscall;
  12. pub mod timeconv;
  13. pub mod timekeep;
  14. pub mod timekeeping;
  15. pub mod timer;
  16. /* Time structures. (Partitially taken from smoltcp)
  17. The `time` module contains structures used to represent both
  18. absolute and relative time.
  19. - [Instant] is used to represent absolute time.
  20. - [Duration] is used to represent relative time.
  21. [Instant]: struct.Instant.html
  22. [Duration]: struct.Duration.html
  23. */
  24. #[allow(dead_code)]
  25. pub const MSEC_PER_SEC: u32 = 1000;
  26. #[allow(dead_code)]
  27. pub const USEC_PER_MSEC: u32 = 1000;
  28. #[allow(dead_code)]
  29. pub const NSEC_PER_USEC: u32 = 1000;
  30. #[allow(dead_code)]
  31. pub const NSEC_PER_MSEC: u32 = 1000000;
  32. #[allow(dead_code)]
  33. pub const USEC_PER_SEC: u32 = 1000000;
  34. #[allow(dead_code)]
  35. pub const NSEC_PER_SEC: u32 = 1000000000;
  36. #[allow(dead_code)]
  37. pub const FSEC_PER_SEC: u64 = 1000000000000000;
  38. /// 表示时间的结构体,符合POSIX标准。
  39. #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
  40. #[repr(C)]
  41. pub struct TimeSpec {
  42. pub tv_sec: i64,
  43. pub tv_nsec: i64,
  44. }
  45. impl TimeSpec {
  46. #[allow(dead_code)]
  47. pub fn new(sec: i64, nsec: i64) -> TimeSpec {
  48. return TimeSpec {
  49. tv_sec: sec,
  50. tv_nsec: nsec,
  51. };
  52. }
  53. /// 获取当前时间
  54. pub fn now() -> Self {
  55. #[cfg(target_arch = "x86_64")]
  56. {
  57. use crate::arch::driver::tsc::TSCManager;
  58. let khz = TSCManager::cpu_khz();
  59. if unlikely(khz == 0) {
  60. return TimeSpec::default();
  61. } else {
  62. return Self::from(Duration::from_millis(
  63. CurrentTimeArch::get_cycles() as u64 / khz,
  64. ));
  65. }
  66. }
  67. #[cfg(target_arch = "riscv64")]
  68. {
  69. unimplemented!("TimeSpec::now()")
  70. }
  71. }
  72. }
  73. impl Sub for TimeSpec {
  74. type Output = Duration;
  75. fn sub(self, rhs: Self) -> Self::Output {
  76. let sec = self.tv_sec.checked_sub(rhs.tv_sec).unwrap_or(0);
  77. let nsec = self.tv_nsec.checked_sub(rhs.tv_nsec).unwrap_or(0);
  78. Duration::from_micros((sec * 1000000 + nsec / 1000) as u64)
  79. }
  80. }
  81. impl From<Duration> for TimeSpec {
  82. fn from(dur: Duration) -> Self {
  83. TimeSpec {
  84. tv_sec: dur.total_micros() as i64 / 1000000,
  85. tv_nsec: (dur.total_micros() as i64 % 1000000) * 1000,
  86. }
  87. }
  88. }
  89. impl Into<Duration> for TimeSpec {
  90. fn into(self) -> Duration {
  91. Duration::from_micros(self.tv_sec as u64 * 1000000 + self.tv_nsec as u64 / 1000)
  92. }
  93. }
  94. /// A representation of an absolute time value.
  95. ///
  96. /// The `Instant` type is a wrapper around a `i64` value that
  97. /// represents a number of microseconds, monotonically increasing
  98. /// since an arbitrary moment in time, such as system startup.
  99. ///
  100. /// * A value of `0` is inherently arbitrary.
  101. /// * A value less than `0` indicates a time before the starting
  102. /// point.
  103. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
  104. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  105. pub struct Instant {
  106. micros: i64,
  107. }
  108. #[allow(dead_code)]
  109. impl Instant {
  110. pub const ZERO: Instant = Instant::from_micros_const(0);
  111. /// Create a new `Instant` from a number of microseconds.
  112. pub fn from_micros<T: Into<i64>>(micros: T) -> Instant {
  113. Instant {
  114. micros: micros.into(),
  115. }
  116. }
  117. pub const fn from_micros_const(micros: i64) -> Instant {
  118. Instant { micros }
  119. }
  120. /// Create a new `Instant` from a number of milliseconds.
  121. pub fn from_millis<T: Into<i64>>(millis: T) -> Instant {
  122. Instant {
  123. micros: millis.into() * 1000,
  124. }
  125. }
  126. /// Create a new `Instant` from a number of milliseconds.
  127. pub const fn from_millis_const(millis: i64) -> Instant {
  128. Instant {
  129. micros: millis * 1000,
  130. }
  131. }
  132. /// Create a new `Instant` from a number of seconds.
  133. pub fn from_secs<T: Into<i64>>(secs: T) -> Instant {
  134. Instant {
  135. micros: secs.into() * 1000000,
  136. }
  137. }
  138. /// Create a new `Instant` from the current time
  139. pub fn now() -> Instant {
  140. Self::from_micros(ktime_get_real_ns() / 1000)
  141. }
  142. /// The fractional number of milliseconds that have passed
  143. /// since the beginning of time.
  144. pub const fn millis(&self) -> i64 {
  145. self.micros % 1000000 / 1000
  146. }
  147. /// The fractional number of microseconds that have passed
  148. /// since the beginning of time.
  149. pub const fn micros(&self) -> i64 {
  150. self.micros % 1000000
  151. }
  152. /// The number of whole seconds that have passed since the
  153. /// beginning of time.
  154. pub const fn secs(&self) -> i64 {
  155. self.micros / 1000000
  156. }
  157. /// The total number of milliseconds that have passed since
  158. /// the beginning of time.
  159. pub const fn total_millis(&self) -> i64 {
  160. self.micros / 1000
  161. }
  162. /// The total number of milliseconds that have passed since
  163. /// the beginning of time.
  164. pub const fn total_micros(&self) -> i64 {
  165. self.micros
  166. }
  167. }
  168. impl fmt::Display for Instant {
  169. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  170. write!(f, "{}.{:0>3}s", self.secs(), self.millis())
  171. }
  172. }
  173. impl ops::Add<Duration> for Instant {
  174. type Output = Instant;
  175. fn add(self, rhs: Duration) -> Instant {
  176. Instant::from_micros(self.micros + rhs.total_micros() as i64)
  177. }
  178. }
  179. impl ops::AddAssign<Duration> for Instant {
  180. fn add_assign(&mut self, rhs: Duration) {
  181. self.micros += rhs.total_micros() as i64;
  182. }
  183. }
  184. impl ops::Sub<Duration> for Instant {
  185. type Output = Instant;
  186. fn sub(self, rhs: Duration) -> Instant {
  187. Instant::from_micros(self.micros - rhs.total_micros() as i64)
  188. }
  189. }
  190. impl ops::SubAssign<Duration> for Instant {
  191. fn sub_assign(&mut self, rhs: Duration) {
  192. self.micros -= rhs.total_micros() as i64;
  193. }
  194. }
  195. impl ops::Sub<Instant> for Instant {
  196. type Output = Duration;
  197. fn sub(self, rhs: Instant) -> Duration {
  198. Duration::from_micros((self.micros - rhs.micros).unsigned_abs())
  199. }
  200. }
  201. /// A relative amount of time.
  202. #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
  203. #[cfg_attr(feature = "defmt", derive(defmt::Format))]
  204. pub struct Duration {
  205. micros: u64,
  206. }
  207. impl Duration {
  208. pub const ZERO: Duration = Duration::from_micros(0);
  209. /// Create a new `Duration` from a number of microseconds.
  210. pub const fn from_micros(micros: u64) -> Duration {
  211. Duration { micros }
  212. }
  213. /// Create a new `Duration` from a number of milliseconds.
  214. pub const fn from_millis(millis: u64) -> Duration {
  215. Duration {
  216. micros: millis * 1000,
  217. }
  218. }
  219. /// Create a new `Instant` from a number of seconds.
  220. pub const fn from_secs(secs: u64) -> Duration {
  221. Duration {
  222. micros: secs * 1000000,
  223. }
  224. }
  225. /// The fractional number of milliseconds in this `Duration`.
  226. pub const fn millis(&self) -> u64 {
  227. self.micros / 1000 % 1000
  228. }
  229. /// The fractional number of milliseconds in this `Duration`.
  230. pub const fn micros(&self) -> u64 {
  231. self.micros % 1000000
  232. }
  233. /// The number of whole seconds in this `Duration`.
  234. pub const fn secs(&self) -> u64 {
  235. self.micros / 1000000
  236. }
  237. /// The total number of milliseconds in this `Duration`.
  238. pub const fn total_millis(&self) -> u64 {
  239. self.micros / 1000
  240. }
  241. /// The total number of microseconds in this `Duration`.
  242. pub const fn total_micros(&self) -> u64 {
  243. self.micros
  244. }
  245. }
  246. impl fmt::Display for Duration {
  247. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  248. write!(f, "{}.{:03}s", self.secs(), self.millis())
  249. }
  250. }
  251. impl ops::Add<Duration> for Duration {
  252. type Output = Duration;
  253. fn add(self, rhs: Duration) -> Duration {
  254. Duration::from_micros(self.micros + rhs.total_micros())
  255. }
  256. }
  257. impl ops::AddAssign<Duration> for Duration {
  258. fn add_assign(&mut self, rhs: Duration) {
  259. self.micros += rhs.total_micros();
  260. }
  261. }
  262. impl ops::Sub<Duration> for Duration {
  263. type Output = Duration;
  264. fn sub(self, rhs: Duration) -> Duration {
  265. Duration::from_micros(
  266. self.micros
  267. .checked_sub(rhs.total_micros())
  268. .expect("overflow when subtracting durations"),
  269. )
  270. }
  271. }
  272. impl ops::SubAssign<Duration> for Duration {
  273. fn sub_assign(&mut self, rhs: Duration) {
  274. self.micros = self
  275. .micros
  276. .checked_sub(rhs.total_micros())
  277. .expect("overflow when subtracting durations");
  278. }
  279. }
  280. impl ops::Mul<u32> for Duration {
  281. type Output = Duration;
  282. fn mul(self, rhs: u32) -> Duration {
  283. Duration::from_micros(self.micros * rhs as u64)
  284. }
  285. }
  286. impl ops::MulAssign<u32> for Duration {
  287. fn mul_assign(&mut self, rhs: u32) {
  288. self.micros *= rhs as u64;
  289. }
  290. }
  291. impl ops::Div<u32> for Duration {
  292. type Output = Duration;
  293. fn div(self, rhs: u32) -> Duration {
  294. Duration::from_micros(self.micros / rhs as u64)
  295. }
  296. }
  297. impl ops::DivAssign<u32> for Duration {
  298. fn div_assign(&mut self, rhs: u32) {
  299. self.micros /= rhs as u64;
  300. }
  301. }
  302. impl ops::Shl<u32> for Duration {
  303. type Output = Duration;
  304. fn shl(self, rhs: u32) -> Duration {
  305. Duration::from_micros(self.micros << rhs)
  306. }
  307. }
  308. impl ops::ShlAssign<u32> for Duration {
  309. fn shl_assign(&mut self, rhs: u32) {
  310. self.micros <<= rhs;
  311. }
  312. }
  313. impl ops::Shr<u32> for Duration {
  314. type Output = Duration;
  315. fn shr(self, rhs: u32) -> Duration {
  316. Duration::from_micros(self.micros >> rhs)
  317. }
  318. }
  319. impl ops::ShrAssign<u32> for Duration {
  320. fn shr_assign(&mut self, rhs: u32) {
  321. self.micros >>= rhs;
  322. }
  323. }
  324. impl From<::core::time::Duration> for Duration {
  325. fn from(other: ::core::time::Duration) -> Duration {
  326. Duration::from_micros(other.as_secs() * 1000000 + other.subsec_micros() as u64)
  327. }
  328. }
  329. impl From<Duration> for ::core::time::Duration {
  330. fn from(val: Duration) -> Self {
  331. ::core::time::Duration::from_micros(val.total_micros())
  332. }
  333. }
  334. /// 支持与smoltcp的时间转换
  335. impl From<smoltcp::time::Instant> for Instant {
  336. fn from(val: smoltcp::time::Instant) -> Self {
  337. Instant::from_micros(val.micros())
  338. }
  339. }
  340. impl Into<smoltcp::time::Instant> for Instant {
  341. fn into(self) -> smoltcp::time::Instant {
  342. smoltcp::time::Instant::from_millis(self.millis())
  343. }
  344. }
  345. /// 支持与smoltcp的时间转换
  346. impl From<smoltcp::time::Duration> for Duration {
  347. fn from(val: smoltcp::time::Duration) -> Self {
  348. Duration::from_micros(val.micros())
  349. }
  350. }
  351. impl Into<smoltcp::time::Duration> for Duration {
  352. fn into(self) -> smoltcp::time::Duration {
  353. smoltcp::time::Duration::from_millis(self.millis())
  354. }
  355. }
  356. pub trait TimeArch {
  357. /// Get CPU cycles (Read from register)
  358. fn get_cycles() -> usize;
  359. }