| 1 | use std::{ |
| 2 | io::{Read, Write}, |
| 3 | mem::MaybeUninit, |
| 4 | ops::{Add, Sub}, |
| 5 | time::Duration, |
| 6 | }; |
| 7 | |
| 8 | /// A timestamp relative to `CLOCK_BOOTTIME`. |
| 9 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 10 | pub struct SystemTime { |
| 11 | secs: i64, |
| 12 | nsecs: i64, |
| 13 | } |
| 14 | |
| 15 | impl SystemTime { |
| 16 | pub(super) fn new(secs: i64, nsecs: i64) -> SystemTime { |
| 17 | SystemTime { |
| 18 | secs: secs + nsecs.div_euclid(1_000_000_000), |
| 19 | nsecs: nsecs.rem_euclid(1_000_000_000), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub fn now() -> std::io::Result<SystemTime> { |
| 24 | let mut spec = MaybeUninit::<libc::timespec>::uninit(); |
| 25 | // SAFETY: valid pointer is passed to clock_gettime |
| 26 | crate::cutils::cerr(unsafe { |
| 27 | libc::clock_gettime(libc::CLOCK_BOOTTIME, spec.as_mut_ptr()) |
| 28 | })?; |
| 29 | // SAFETY: The `libc::clock_gettime` will correctly initialize `spec`, |
| 30 | // otherwise it will return early with the `?` operator. |
| 31 | let spec = unsafe { spec.assume_init() }; |
| 32 | Ok(spec.into()) |
| 33 | } |
| 34 | |
| 35 | pub(super) fn encode(&self, target: &mut impl Write) -> std::io::Result<()> { |
| 36 | let secs = self.secs.to_ne_bytes(); |
| 37 | let nsecs = self.nsecs.to_ne_bytes(); |
| 38 | target.write_all(&secs)?; |
| 39 | target.write_all(&nsecs)?; |
| 40 | Ok(()) |
| 41 | } |
| 42 | |
| 43 | pub(super) fn decode(from: &mut impl Read) -> std::io::Result<SystemTime> { |
| 44 | let mut sec_bytes = [0; 8]; |
| 45 | let mut nsec_bytes = [0; 8]; |
| 46 | |
| 47 | from.read_exact(&mut sec_bytes)?; |
| 48 | from.read_exact(&mut nsec_bytes)?; |
| 49 | |
| 50 | Ok(SystemTime::new( |
| 51 | i64::from_ne_bytes(sec_bytes), |
| 52 | i64::from_ne_bytes(nsec_bytes), |
| 53 | )) |
| 54 | } |
| 55 | |
| 56 | #[inline] |
| 57 | fn checked_add(self, rhs: Duration) -> Option<SystemTime> { |
| 58 | let rhs_secs = rhs.as_secs().try_into().ok()?; |
| 59 | let rhs_nsecs = rhs.subsec_nanos().into(); |
| 60 | |
| 61 | let secs = self.secs.checked_add(rhs_secs)?; |
| 62 | let nsecs = self.nsecs.checked_add(rhs_nsecs)?; |
| 63 | |
| 64 | Some(SystemTime::new(secs, nsecs)) |
| 65 | } |
| 66 | |
| 67 | #[inline] |
| 68 | fn checked_sub(self, rhs: Duration) -> Option<SystemTime> { |
| 69 | let rhs_secs = rhs.as_secs().try_into().ok()?; |
| 70 | let rhs_nsecs = rhs.subsec_nanos().into(); |
| 71 | |
| 72 | let secs = self.secs.checked_sub(rhs_secs)?; |
| 73 | let nsecs = self.nsecs.checked_sub(rhs_nsecs)?; |
| 74 | |
| 75 | Some(SystemTime::new(secs, nsecs)) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl Add<Duration> for SystemTime { |
| 80 | type Output = SystemTime; |
| 81 | |
| 82 | #[inline] |
| 83 | fn add(self, rhs: Duration) -> Self::Output { |
| 84 | self.checked_add(rhs) |
| 85 | .expect("overflow when adding duration") |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | impl Sub<Duration> for SystemTime { |
| 90 | type Output = SystemTime; |
| 91 | |
| 92 | #[inline] |
| 93 | fn sub(self, rhs: Duration) -> Self::Output { |
| 94 | self.checked_sub(rhs) |
| 95 | .expect("overflow when subtracting duration") |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | impl From<libc::timespec> for SystemTime { |
| 100 | #[allow(clippy::useless_conversion)] |
| 101 | fn from(value: libc::timespec) -> Self { |
| 102 | SystemTime::new(value.tv_sec.into(), value.tv_nsec.into()) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /// A timestamp relative to `CLOCK_BOOTTIME` on Linux and relative to `CLOCK_REALTIME` on FreeBSD. |
| 107 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 108 | pub struct ProcessCreateTime { |
| 109 | secs: i64, |
| 110 | nsecs: i64, |
| 111 | } |
| 112 | |
| 113 | impl ProcessCreateTime { |
| 114 | pub fn new(secs: i64, nsecs: i64) -> ProcessCreateTime { |
| 115 | ProcessCreateTime { |
| 116 | secs: secs + nsecs.div_euclid(1_000_000_000), |
| 117 | nsecs: nsecs.rem_euclid(1_000_000_000), |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | pub(super) fn encode(&self, target: &mut impl Write) -> std::io::Result<()> { |
| 122 | let secs = self.secs.to_ne_bytes(); |
| 123 | let nsecs = self.nsecs.to_ne_bytes(); |
| 124 | target.write_all(&secs)?; |
| 125 | target.write_all(&nsecs)?; |
| 126 | Ok(()) |
| 127 | } |
| 128 | |
| 129 | pub(super) fn decode(from: &mut impl Read) -> std::io::Result<ProcessCreateTime> { |
| 130 | let mut sec_bytes = [0; 8]; |
| 131 | let mut nsec_bytes = [0; 8]; |
| 132 | |
| 133 | from.read_exact(&mut sec_bytes)?; |
| 134 | from.read_exact(&mut nsec_bytes)?; |
| 135 | |
| 136 | Ok(ProcessCreateTime::new( |
| 137 | i64::from_ne_bytes(sec_bytes), |
| 138 | i64::from_ne_bytes(nsec_bytes), |
| 139 | )) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #[cfg(test)] |
| 144 | mod tests { |
| 145 | use super::*; |
| 146 | |
| 147 | #[test] |
| 148 | fn test_new_system_time() { |
| 149 | assert_eq!(SystemTime::new(-1, 2_000_000_000), SystemTime::new(1, 0)); |
| 150 | assert_eq!( |
| 151 | SystemTime::new(2, -500_000_000), |
| 152 | SystemTime::new(1, 500_000_000) |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | #[test] |
| 157 | fn test_time_ops() { |
| 158 | assert_eq!( |
| 159 | SystemTime::new(0, 0) + Duration::from_secs(3), |
| 160 | SystemTime::new(3, 0) |
| 161 | ); |
| 162 | assert_eq!( |
| 163 | SystemTime::new(0, 500_000_000) + Duration::from_nanos(2_500_000_000), |
| 164 | SystemTime::new(3, 0) |
| 165 | ); |
| 166 | assert_eq!( |
| 167 | SystemTime::new(10, 0) - Duration::from_secs(4), |
| 168 | SystemTime::new(6, 0) |
| 169 | ); |
| 170 | assert_eq!( |
| 171 | SystemTime::new(10, 0) - Duration::from_nanos(3_500_000_000), |
| 172 | SystemTime::new(6, 500_000_000) |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn get_process_start_time() { |
| 178 | use crate::system::{Process, WithProcess}; |
| 179 | let time = Process::starting_time(WithProcess::Current).unwrap(); |
| 180 | |
| 181 | let now = { |
| 182 | let super::SystemTime { secs, nsecs } = super::SystemTime::now().unwrap(); |
| 183 | super::ProcessCreateTime { secs, nsecs } |
| 184 | }; |
| 185 | |
| 186 | assert!(time.secs > now.secs - 24 * 60 * 60); |
| 187 | assert!(time < now); |
| 188 | } |
| 189 | } |