xref: /linux-6.15/rust/kernel/time.rs (revision aa33de03)
182e17087SAlice Ryhl // SPDX-License-Identifier: GPL-2.0
282e17087SAlice Ryhl 
382e17087SAlice Ryhl //! Time related primitives.
482e17087SAlice Ryhl //!
582e17087SAlice Ryhl //! This module contains the kernel APIs related to time and timers that
682e17087SAlice Ryhl //! have been ported or wrapped for usage by Rust code in the kernel.
7ddd91209SBoqun Feng //!
8ddd91209SBoqun Feng //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h).
9ddd91209SBoqun Feng //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h).
1082e17087SAlice Ryhl 
118a8afe93SAndreas Hindborg pub mod hrtimer;
128a8afe93SAndreas Hindborg 
1348b7f4d2SAlice Ryhl /// The number of nanoseconds per millisecond.
1448b7f4d2SAlice Ryhl pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
1548b7f4d2SAlice Ryhl 
1682e17087SAlice Ryhl /// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
17d072acdaSGary Guo pub type Jiffies = crate::ffi::c_ulong;
1882e17087SAlice Ryhl 
1982e17087SAlice Ryhl /// The millisecond time unit.
20d072acdaSGary Guo pub type Msecs = crate::ffi::c_uint;
2182e17087SAlice Ryhl 
2282e17087SAlice Ryhl /// Converts milliseconds to jiffies.
2382e17087SAlice Ryhl #[inline]
msecs_to_jiffies(msecs: Msecs) -> Jiffies2482e17087SAlice Ryhl pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
2582e17087SAlice Ryhl     // SAFETY: The `__msecs_to_jiffies` function is always safe to call no
2682e17087SAlice Ryhl     // matter what the argument is.
2782e17087SAlice Ryhl     unsafe { bindings::__msecs_to_jiffies(msecs) }
2882e17087SAlice Ryhl }
2948b7f4d2SAlice Ryhl 
3048b7f4d2SAlice Ryhl /// A Rust wrapper around a `ktime_t`.
3148b7f4d2SAlice Ryhl #[repr(transparent)]
3248b7f4d2SAlice Ryhl #[derive(Copy, Clone)]
3348b7f4d2SAlice Ryhl pub struct Ktime {
3448b7f4d2SAlice Ryhl     inner: bindings::ktime_t,
3548b7f4d2SAlice Ryhl }
3648b7f4d2SAlice Ryhl 
3748b7f4d2SAlice Ryhl impl Ktime {
3848b7f4d2SAlice Ryhl     /// Create a `Ktime` from a raw `ktime_t`.
3948b7f4d2SAlice Ryhl     #[inline]
from_raw(inner: bindings::ktime_t) -> Self4048b7f4d2SAlice Ryhl     pub fn from_raw(inner: bindings::ktime_t) -> Self {
4148b7f4d2SAlice Ryhl         Self { inner }
4248b7f4d2SAlice Ryhl     }
4348b7f4d2SAlice Ryhl 
4448b7f4d2SAlice Ryhl     /// Get the current time using `CLOCK_MONOTONIC`.
4548b7f4d2SAlice Ryhl     #[inline]
ktime_get() -> Self4648b7f4d2SAlice Ryhl     pub fn ktime_get() -> Self {
4748b7f4d2SAlice Ryhl         // SAFETY: It is always safe to call `ktime_get` outside of NMI context.
4848b7f4d2SAlice Ryhl         Self::from_raw(unsafe { bindings::ktime_get() })
4948b7f4d2SAlice Ryhl     }
5048b7f4d2SAlice Ryhl 
5148b7f4d2SAlice Ryhl     /// Divide the number of nanoseconds by a compile-time constant.
5248b7f4d2SAlice Ryhl     #[inline]
divns_constant<const DIV: i64>(self) -> i645348b7f4d2SAlice Ryhl     fn divns_constant<const DIV: i64>(self) -> i64 {
5448b7f4d2SAlice Ryhl         self.to_ns() / DIV
5548b7f4d2SAlice Ryhl     }
5648b7f4d2SAlice Ryhl 
5748b7f4d2SAlice Ryhl     /// Returns the number of nanoseconds.
5848b7f4d2SAlice Ryhl     #[inline]
to_ns(self) -> i645948b7f4d2SAlice Ryhl     pub fn to_ns(self) -> i64 {
6048b7f4d2SAlice Ryhl         self.inner
6148b7f4d2SAlice Ryhl     }
6248b7f4d2SAlice Ryhl 
6348b7f4d2SAlice Ryhl     /// Returns the number of milliseconds.
6448b7f4d2SAlice Ryhl     #[inline]
to_ms(self) -> i646548b7f4d2SAlice Ryhl     pub fn to_ms(self) -> i64 {
6648b7f4d2SAlice Ryhl         self.divns_constant::<NSEC_PER_MSEC>()
6748b7f4d2SAlice Ryhl     }
6848b7f4d2SAlice Ryhl }
6948b7f4d2SAlice Ryhl 
7048b7f4d2SAlice Ryhl /// Returns the number of milliseconds between two ktimes.
7148b7f4d2SAlice Ryhl #[inline]
ktime_ms_delta(later: Ktime, earlier: Ktime) -> i647248b7f4d2SAlice Ryhl pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 {
7348b7f4d2SAlice Ryhl     (later - earlier).to_ms()
7448b7f4d2SAlice Ryhl }
7548b7f4d2SAlice Ryhl 
7648b7f4d2SAlice Ryhl impl core::ops::Sub for Ktime {
7748b7f4d2SAlice Ryhl     type Output = Ktime;
7848b7f4d2SAlice Ryhl 
7948b7f4d2SAlice Ryhl     #[inline]
sub(self, other: Ktime) -> Ktime8048b7f4d2SAlice Ryhl     fn sub(self, other: Ktime) -> Ktime {
8148b7f4d2SAlice Ryhl         Self {
8248b7f4d2SAlice Ryhl             inner: self.inner - other.inner,
8348b7f4d2SAlice Ryhl         }
8448b7f4d2SAlice Ryhl     }
8548b7f4d2SAlice Ryhl }
86*aa33de03SAndreas Hindborg 
87*aa33de03SAndreas Hindborg /// An identifier for a clock. Used when specifying clock sources.
88*aa33de03SAndreas Hindborg ///
89*aa33de03SAndreas Hindborg ///
90*aa33de03SAndreas Hindborg /// Selection of the clock depends on the use case. In some cases the usage of a
91*aa33de03SAndreas Hindborg /// particular clock is mandatory, e.g. in network protocols, filesystems.In other
92*aa33de03SAndreas Hindborg /// cases the user of the clock has to decide which clock is best suited for the
93*aa33de03SAndreas Hindborg /// purpose. In most scenarios clock [`ClockId::Monotonic`] is the best choice as it
94*aa33de03SAndreas Hindborg /// provides a accurate monotonic notion of time (leap second smearing ignored).
95*aa33de03SAndreas Hindborg #[derive(Clone, Copy, PartialEq, Eq, Debug)]
96*aa33de03SAndreas Hindborg #[repr(u32)]
97*aa33de03SAndreas Hindborg pub enum ClockId {
98*aa33de03SAndreas Hindborg     /// A settable system-wide clock that measures real (i.e., wall-clock) time.
99*aa33de03SAndreas Hindborg     ///
100*aa33de03SAndreas Hindborg     /// Setting this clock requires appropriate privileges. This clock is
101*aa33de03SAndreas Hindborg     /// affected by discontinuous jumps in the system time (e.g., if the system
102*aa33de03SAndreas Hindborg     /// administrator manually changes the clock), and by frequency adjustments
103*aa33de03SAndreas Hindborg     /// performed by NTP and similar applications via adjtime(3), adjtimex(2),
104*aa33de03SAndreas Hindborg     /// clock_adjtime(2), and ntp_adjtime(3). This clock normally counts the
105*aa33de03SAndreas Hindborg     /// number of seconds since 1970-01-01 00:00:00 Coordinated Universal Time
106*aa33de03SAndreas Hindborg     /// (UTC) except that it ignores leap seconds; near a leap second it may be
107*aa33de03SAndreas Hindborg     /// adjusted by leap second smearing to stay roughly in sync with UTC. Leap
108*aa33de03SAndreas Hindborg     /// second smearing applies frequency adjustments to the clock to speed up
109*aa33de03SAndreas Hindborg     /// or slow down the clock to account for the leap second without
110*aa33de03SAndreas Hindborg     /// discontinuities in the clock. If leap second smearing is not applied,
111*aa33de03SAndreas Hindborg     /// the clock will experience discontinuity around leap second adjustment.
112*aa33de03SAndreas Hindborg     RealTime = bindings::CLOCK_REALTIME,
113*aa33de03SAndreas Hindborg     /// A monotonically increasing clock.
114*aa33de03SAndreas Hindborg     ///
115*aa33de03SAndreas Hindborg     /// A nonsettable system-wide clock that represents monotonic time since—as
116*aa33de03SAndreas Hindborg     /// described by POSIX—"some unspecified point in the past". On Linux, that
117*aa33de03SAndreas Hindborg     /// point corresponds to the number of seconds that the system has been
118*aa33de03SAndreas Hindborg     /// running since it was booted.
119*aa33de03SAndreas Hindborg     ///
120*aa33de03SAndreas Hindborg     /// The CLOCK_MONOTONIC clock is not affected by discontinuous jumps in the
121*aa33de03SAndreas Hindborg     /// CLOCK_REAL (e.g., if the system administrator manually changes the
122*aa33de03SAndreas Hindborg     /// clock), but is affected by frequency adjustments. This clock does not
123*aa33de03SAndreas Hindborg     /// count time that the system is suspended.
124*aa33de03SAndreas Hindborg     Monotonic = bindings::CLOCK_MONOTONIC,
125*aa33de03SAndreas Hindborg     /// A monotonic that ticks while system is suspended.
126*aa33de03SAndreas Hindborg     ///
127*aa33de03SAndreas Hindborg     /// A nonsettable system-wide clock that is identical to CLOCK_MONOTONIC,
128*aa33de03SAndreas Hindborg     /// except that it also includes any time that the system is suspended. This
129*aa33de03SAndreas Hindborg     /// allows applications to get a suspend-aware monotonic clock without
130*aa33de03SAndreas Hindborg     /// having to deal with the complications of CLOCK_REALTIME, which may have
131*aa33de03SAndreas Hindborg     /// discontinuities if the time is changed using settimeofday(2) or similar.
132*aa33de03SAndreas Hindborg     BootTime = bindings::CLOCK_BOOTTIME,
133*aa33de03SAndreas Hindborg     /// International Atomic Time.
134*aa33de03SAndreas Hindborg     ///
135*aa33de03SAndreas Hindborg     /// A system-wide clock derived from wall-clock time but counting leap seconds.
136*aa33de03SAndreas Hindborg     ///
137*aa33de03SAndreas Hindborg     /// This clock is coupled to CLOCK_REALTIME and will be set when CLOCK_REALTIME is
138*aa33de03SAndreas Hindborg     /// set, or when the offset to CLOCK_REALTIME is changed via adjtimex(2). This
139*aa33de03SAndreas Hindborg     /// usually happens during boot and **should** not happen during normal operations.
140*aa33de03SAndreas Hindborg     /// However, if NTP or another application adjusts CLOCK_REALTIME by leap second
141*aa33de03SAndreas Hindborg     /// smearing, this clock will not be precise during leap second smearing.
142*aa33de03SAndreas Hindborg     ///
143*aa33de03SAndreas Hindborg     /// The acronym TAI refers to International Atomic Time.
144*aa33de03SAndreas Hindborg     TAI = bindings::CLOCK_TAI,
145*aa33de03SAndreas Hindborg }
146*aa33de03SAndreas Hindborg 
147*aa33de03SAndreas Hindborg impl ClockId {
into_c(self) -> bindings::clockid_t148*aa33de03SAndreas Hindborg     fn into_c(self) -> bindings::clockid_t {
149*aa33de03SAndreas Hindborg         self as bindings::clockid_t
150*aa33de03SAndreas Hindborg     }
151*aa33de03SAndreas Hindborg }
152