]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/time.rs
Rollup merge of #105482 - wesleywiser:fix_debuginfo_ub, r=tmiasko
[rust.git] / library / std / src / sys / unix / time.rs
1 use crate::fmt;
2 use crate::time::Duration;
3
4 pub use self::inner::Instant;
5
6 const NSEC_PER_SEC: u64 = 1_000_000_000;
7 pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() };
8
9 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10 #[repr(transparent)]
11 #[rustc_layout_scalar_valid_range_start(0)]
12 #[rustc_layout_scalar_valid_range_end(999_999_999)]
13 struct Nanoseconds(u32);
14
15 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16 pub struct SystemTime {
17     pub(in crate::sys::unix) t: Timespec,
18 }
19
20 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
21 pub(in crate::sys::unix) struct Timespec {
22     tv_sec: i64,
23     tv_nsec: Nanoseconds,
24 }
25
26 impl SystemTime {
27     #[cfg_attr(target_os = "horizon", allow(unused))]
28     pub fn new(tv_sec: i64, tv_nsec: i64) -> SystemTime {
29         SystemTime { t: Timespec::new(tv_sec, tv_nsec) }
30     }
31
32     pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
33         self.t.sub_timespec(&other.t)
34     }
35
36     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
37         Some(SystemTime { t: self.t.checked_add_duration(other)? })
38     }
39
40     pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
41         Some(SystemTime { t: self.t.checked_sub_duration(other)? })
42     }
43 }
44
45 impl From<libc::timespec> for SystemTime {
46     fn from(t: libc::timespec) -> SystemTime {
47         SystemTime { t: Timespec::from(t) }
48     }
49 }
50
51 impl fmt::Debug for SystemTime {
52     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53         f.debug_struct("SystemTime")
54             .field("tv_sec", &self.t.tv_sec)
55             .field("tv_nsec", &self.t.tv_nsec.0)
56             .finish()
57     }
58 }
59
60 impl Timespec {
61     pub const fn zero() -> Timespec {
62         Timespec::new(0, 0)
63     }
64
65     const fn new(tv_sec: i64, tv_nsec: i64) -> Timespec {
66         assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC as i64);
67         // SAFETY: The assert above checks tv_nsec is within the valid range
68         Timespec { tv_sec, tv_nsec: unsafe { Nanoseconds(tv_nsec as u32) } }
69     }
70
71     pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
72         if self >= other {
73             // NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
74             // to optimize it into a branchless form (see also #75545):
75             //
76             // 1. `self.tv_sec - other.tv_sec` shows up as a common expression
77             //    in both branches, i.e. the `else` must have its `- 1`
78             //    subtraction after the common one, not interleaved with it
79             //    (it used to be `self.tv_sec - 1 - other.tv_sec`)
80             //
81             // 2. the `Duration::new` call (or any other additional complexity)
82             //    is outside of the `if`-`else`, not duplicated in both branches
83             //
84             // Ideally this code could be rearranged such that it more
85             // directly expresses the lower-cost behavior we want from it.
86             let (secs, nsec) = if self.tv_nsec.0 >= other.tv_nsec.0 {
87                 ((self.tv_sec - other.tv_sec) as u64, self.tv_nsec.0 - other.tv_nsec.0)
88             } else {
89                 (
90                     (self.tv_sec - other.tv_sec - 1) as u64,
91                     self.tv_nsec.0 + (NSEC_PER_SEC as u32) - other.tv_nsec.0,
92                 )
93             };
94
95             Ok(Duration::new(secs, nsec))
96         } else {
97             match other.sub_timespec(self) {
98                 Ok(d) => Err(d),
99                 Err(d) => Ok(d),
100             }
101         }
102     }
103
104     pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
105         let mut secs = other
106             .as_secs()
107             .try_into() // <- target type would be `i64`
108             .ok()
109             .and_then(|secs| self.tv_sec.checked_add(secs))?;
110
111         // Nano calculations can't overflow because nanos are <1B which fit
112         // in a u32.
113         let mut nsec = other.subsec_nanos() + self.tv_nsec.0;
114         if nsec >= NSEC_PER_SEC as u32 {
115             nsec -= NSEC_PER_SEC as u32;
116             secs = secs.checked_add(1)?;
117         }
118         Some(Timespec::new(secs, nsec as i64))
119     }
120
121     pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
122         let mut secs = other
123             .as_secs()
124             .try_into() // <- target type would be `i64`
125             .ok()
126             .and_then(|secs| self.tv_sec.checked_sub(secs))?;
127
128         // Similar to above, nanos can't overflow.
129         let mut nsec = self.tv_nsec.0 as i32 - other.subsec_nanos() as i32;
130         if nsec < 0 {
131             nsec += NSEC_PER_SEC as i32;
132             secs = secs.checked_sub(1)?;
133         }
134         Some(Timespec::new(secs, nsec as i64))
135     }
136
137     #[allow(dead_code)]
138     pub fn to_timespec(&self) -> Option<libc::timespec> {
139         Some(libc::timespec {
140             tv_sec: self.tv_sec.try_into().ok()?,
141             tv_nsec: self.tv_nsec.0.try_into().ok()?,
142         })
143     }
144 }
145
146 impl From<libc::timespec> for Timespec {
147     fn from(t: libc::timespec) -> Timespec {
148         Timespec::new(t.tv_sec as i64, t.tv_nsec as i64)
149     }
150 }
151
152 #[cfg(any(
153     all(target_os = "macos", any(not(target_arch = "aarch64"))),
154     target_os = "ios",
155     target_os = "watchos"
156 ))]
157 mod inner {
158     use crate::sync::atomic::{AtomicU64, Ordering};
159     use crate::sys::cvt;
160     use crate::sys_common::mul_div_u64;
161     use crate::time::Duration;
162
163     use super::{SystemTime, Timespec, NSEC_PER_SEC};
164
165     #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
166     pub struct Instant {
167         t: u64,
168     }
169
170     #[repr(C)]
171     #[derive(Copy, Clone)]
172     struct mach_timebase_info {
173         numer: u32,
174         denom: u32,
175     }
176     type mach_timebase_info_t = *mut mach_timebase_info;
177     type kern_return_t = libc::c_int;
178
179     impl Instant {
180         pub fn now() -> Instant {
181             extern "C" {
182                 fn mach_absolute_time() -> u64;
183             }
184             Instant { t: unsafe { mach_absolute_time() } }
185         }
186
187         pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
188             let diff = self.t.checked_sub(other.t)?;
189             let info = info();
190             let nanos = mul_div_u64(diff, info.numer as u64, info.denom as u64);
191             Some(Duration::new(nanos / NSEC_PER_SEC, (nanos % NSEC_PER_SEC) as u32))
192         }
193
194         pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
195             Some(Instant { t: self.t.checked_add(checked_dur2intervals(other)?)? })
196         }
197
198         pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
199             Some(Instant { t: self.t.checked_sub(checked_dur2intervals(other)?)? })
200         }
201     }
202
203     impl SystemTime {
204         pub fn now() -> SystemTime {
205             use crate::ptr;
206
207             let mut s = libc::timeval { tv_sec: 0, tv_usec: 0 };
208             cvt(unsafe { libc::gettimeofday(&mut s, ptr::null_mut()) }).unwrap();
209             return SystemTime::from(s);
210         }
211     }
212
213     impl From<libc::timeval> for Timespec {
214         fn from(t: libc::timeval) -> Timespec {
215             Timespec::new(t.tv_sec as i64, 1000 * t.tv_usec as i64)
216         }
217     }
218
219     impl From<libc::timeval> for SystemTime {
220         fn from(t: libc::timeval) -> SystemTime {
221             SystemTime { t: Timespec::from(t) }
222         }
223     }
224
225     fn checked_dur2intervals(dur: &Duration) -> Option<u64> {
226         let nanos =
227             dur.as_secs().checked_mul(NSEC_PER_SEC)?.checked_add(dur.subsec_nanos() as u64)?;
228         let info = info();
229         Some(mul_div_u64(nanos, info.denom as u64, info.numer as u64))
230     }
231
232     fn info() -> mach_timebase_info {
233         // INFO_BITS conceptually is an `Option<mach_timebase_info>`. We can do
234         // this in 64 bits because we know 0 is never a valid value for the
235         // `denom` field.
236         //
237         // Encoding this as a single `AtomicU64` allows us to use `Relaxed`
238         // operations, as we are only interested in the effects on a single
239         // memory location.
240         static INFO_BITS: AtomicU64 = AtomicU64::new(0);
241
242         // If a previous thread has initialized `INFO_BITS`, use it.
243         let info_bits = INFO_BITS.load(Ordering::Relaxed);
244         if info_bits != 0 {
245             return info_from_bits(info_bits);
246         }
247
248         // ... otherwise learn for ourselves ...
249         extern "C" {
250             fn mach_timebase_info(info: mach_timebase_info_t) -> kern_return_t;
251         }
252
253         let mut info = info_from_bits(0);
254         unsafe {
255             mach_timebase_info(&mut info);
256         }
257         INFO_BITS.store(info_to_bits(info), Ordering::Relaxed);
258         info
259     }
260
261     #[inline]
262     fn info_to_bits(info: mach_timebase_info) -> u64 {
263         ((info.denom as u64) << 32) | (info.numer as u64)
264     }
265
266     #[inline]
267     fn info_from_bits(bits: u64) -> mach_timebase_info {
268         mach_timebase_info { numer: bits as u32, denom: (bits >> 32) as u32 }
269     }
270 }
271
272 #[cfg(not(any(
273     all(target_os = "macos", any(not(target_arch = "aarch64"))),
274     target_os = "ios",
275     target_os = "watchos"
276 )))]
277 mod inner {
278     use crate::fmt;
279     use crate::mem::MaybeUninit;
280     use crate::sys::cvt;
281     use crate::time::Duration;
282
283     use super::{SystemTime, Timespec};
284
285     #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
286     pub struct Instant {
287         t: Timespec,
288     }
289
290     impl Instant {
291         pub fn now() -> Instant {
292             #[cfg(target_os = "macos")]
293             const clock_id: libc::clockid_t = libc::CLOCK_UPTIME_RAW;
294             #[cfg(not(target_os = "macos"))]
295             const clock_id: libc::clockid_t = libc::CLOCK_MONOTONIC;
296             Instant { t: Timespec::now(clock_id) }
297         }
298
299         pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
300             self.t.sub_timespec(&other.t).ok()
301         }
302
303         pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
304             Some(Instant { t: self.t.checked_add_duration(other)? })
305         }
306
307         pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
308             Some(Instant { t: self.t.checked_sub_duration(other)? })
309         }
310     }
311
312     impl fmt::Debug for Instant {
313         fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314             f.debug_struct("Instant")
315                 .field("tv_sec", &self.t.tv_sec)
316                 .field("tv_nsec", &self.t.tv_nsec.0)
317                 .finish()
318         }
319     }
320
321     impl SystemTime {
322         pub fn now() -> SystemTime {
323             SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
324         }
325     }
326
327     impl Timespec {
328         pub fn now(clock: libc::clockid_t) -> Timespec {
329             // Try to use 64-bit time in preparation for Y2038.
330             #[cfg(all(target_os = "linux", target_env = "gnu", target_pointer_width = "32"))]
331             {
332                 use crate::sys::weak::weak;
333
334                 // __clock_gettime64 was added to 32-bit arches in glibc 2.34,
335                 // and it handles both vDSO calls and ENOSYS fallbacks itself.
336                 weak!(fn __clock_gettime64(libc::clockid_t, *mut __timespec64) -> libc::c_int);
337
338                 #[repr(C)]
339                 struct __timespec64 {
340                     tv_sec: i64,
341                     #[cfg(target_endian = "big")]
342                     _padding: i32,
343                     tv_nsec: i32,
344                     #[cfg(target_endian = "little")]
345                     _padding: i32,
346                 }
347
348                 if let Some(clock_gettime64) = __clock_gettime64.get() {
349                     let mut t = MaybeUninit::uninit();
350                     cvt(unsafe { clock_gettime64(clock, t.as_mut_ptr()) }).unwrap();
351                     let t = unsafe { t.assume_init() };
352                     return Timespec::new(t.tv_sec, t.tv_nsec as i64);
353                 }
354             }
355
356             let mut t = MaybeUninit::uninit();
357             cvt(unsafe { libc::clock_gettime(clock, t.as_mut_ptr()) }).unwrap();
358             Timespec::from(unsafe { t.assume_init() })
359         }
360     }
361 }