use std::collections::VecDeque;
use std::io;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SystemClock {
Realtime,
Monotonic,
MonotonicRaw,
Tai,
}
pub struct HardwareClock {
inner: platform::HardwareClock,
}
impl HardwareClock {
pub fn open(index: u32) -> io::Result<Self> {
Ok(Self {
inner: platform::HardwareClock::open(index, false)?,
})
}
pub fn open_adjustable(index: u32) -> io::Result<Self> {
Ok(Self {
inner: platform::HardwareClock::open(index, true)?,
})
}
pub fn frequency_adjustment(&self) -> io::Result<f64> {
self.inner.frequency_adjustment()
}
pub fn set_frequency_adjustment(&self, ppb: f64) -> io::Result<()> {
self.inner.set_frequency_adjustment(ppb)
}
pub fn step(&self, by: i128) -> io::Result<()> {
self.inner.step(by)
}
pub fn now(&self) -> io::Result<Duration> {
self.inner.now()
}
pub fn offset_from(&self, other: &HardwareClock) -> io::Result<i128> {
self.reading_from(other).map(|reading| reading.offset)
}
pub fn reading_from(&self, other: &HardwareClock) -> io::Result<Reading> {
let ours = self.system_reading(SystemClock::Realtime)?;
let theirs = other.system_reading(SystemClock::Realtime)?;
Ok(Reading {
offset: ours.offset - theirs.offset,
span: ours.span.saturating_add(theirs.span),
})
}
fn system_reading(&self, system: SystemClock) -> io::Result<Reading> {
match self.inner.reading_from_system(system) {
Err(error) if error.kind() == io::ErrorKind::Unsupported => {
self.reading_against(system)
}
read => read,
}
}
pub fn offset_from_system_clock(&self, system: SystemClock) -> io::Result<i128> {
self.reading_from_system_clock(system)
.map(|reading| reading.offset)
}
pub fn reading_from_system_clock(&self, system: SystemClock) -> io::Result<Reading> {
self.system_reading(system)
}
pub fn offset_from_monotonic(&self) -> io::Result<i128> {
self.offset_read_against(SystemClock::Monotonic)
}
pub fn offset_read_against(&self, system: SystemClock) -> io::Result<i128> {
self.reading_against(system).map(|reading| reading.offset)
}
pub fn reading_against(&self, system: SystemClock) -> io::Result<Reading> {
let mut tries = [Reading { offset: 0, span: 0 }; 5];
for slot in &mut tries {
let before = system_now(system);
let clock = self.now()?;
let after = system_now(system);
let span = after.saturating_sub(before).as_nanos();
let middle = before + (after - before) / 2;
*slot = Reading {
offset: clock.as_nanos() as i128 - middle.as_nanos() as i128,
span: u64::try_from(span).unwrap_or(u64::MAX),
};
}
let mut sorted = tries;
sorted.sort_unstable_by_key(|reading| reading.offset);
let median = sorted[sorted.len() / 2];
Ok(tries
.into_iter()
.filter(|reading| (reading.offset - median.offset).abs() <= 10_000)
.min_by_key(|reading| reading.span)
.unwrap_or(median))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Reading {
pub offset: i128,
pub span: u64,
}
const SPAN: i128 = 2_000_000_000;
#[derive(Debug, Clone, Default)]
pub struct Comparison {
kept: VecDeque<(i128, i128)>,
}
impl Comparison {
pub fn update(&mut self, at: i128, offset: i128) {
if self.kept.back().is_some_and(|(last, _)| at <= *last) {
return;
}
self.kept.push_back((at, offset));
while self
.kept
.front()
.is_some_and(|(first, _)| at - first > SPAN)
&& self.kept.len() > 2
{
self.kept.pop_front();
}
}
pub fn offset_at(&self, at: i128) -> i128 {
let Some(&(last_at, last_offset)) = self.kept.back() else {
return 0;
};
let Some((mean_at, mean_offset, rate)) = self.fit() else {
return last_offset;
};
last_offset + (mean_offset + rate * ((at - last_at) as f64 - mean_at)).round() as i128
}
pub fn rate(&self) -> Option<f64> {
self.fit().map(|(_, _, rate)| rate)
}
fn fit(&self) -> Option<(f64, f64, f64)> {
let &(last_at, last_offset) = self.kept.back()?;
if self.kept.len() < 2 {
return None;
}
let count = self.kept.len() as f64;
let points = || {
self.kept
.iter()
.map(move |&(at, offset)| ((at - last_at) as f64, (offset - last_offset) as f64))
};
let mean_at = points().map(|(at, _)| at).sum::<f64>() / count;
let mean_offset = points().map(|(_, offset)| offset).sum::<f64>() / count;
let (spread, covariance) =
points().fold((0.0, 0.0), |(spread, covariance), (at, offset)| {
let at = at - mean_at;
(spread + at * at, covariance + at * (offset - mean_offset))
});
(spread > 0.0).then(|| (mean_at, mean_offset, covariance / spread))
}
pub fn is_empty(&self) -> bool {
self.kept.is_empty()
}
pub fn keep_last(&mut self, count: usize) {
while self.kept.len() > count {
self.kept.pop_front();
}
}
pub fn clear(&mut self) {
self.kept.clear();
}
}
pub fn monotonic_now() -> Duration {
platform::monotonic_now()
}
pub fn system_now(system: SystemClock) -> Duration {
platform::system_now(system)
}
pub fn suspended() -> Duration {
platform::suspended()
}
pub fn precise_sleeps() {
platform::precise_sleeps();
}
#[cfg(target_os = "linux")]
#[allow(unsafe_code)]
mod platform {
use super::{Reading, SystemClock};
use std::io;
use std::mem::size_of;
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::time::Duration;
pub struct HardwareClock {
_fd: OwnedFd,
id: libc::clockid_t,
}
fn read(id: libc::clockid_t) -> io::Result<Duration> {
let mut time = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
if unsafe { libc::clock_gettime(id, &raw mut time) } < 0 {
return Err(io::Error::last_os_error());
}
Ok(Duration::new(time.tv_sec as u64, time.tv_nsec as u32))
}
impl HardwareClock {
pub fn open(index: u32, adjustable: bool) -> io::Result<Self> {
let path = format!("/dev/ptp{index}\0");
let mode = if adjustable {
libc::O_RDWR
} else {
libc::O_RDONLY
};
let raw = unsafe { libc::open(path.as_ptr().cast(), mode | libc::O_CLOEXEC) };
if raw < 0 {
return Err(io::Error::last_os_error());
}
let fd = unsafe { OwnedFd::from_raw_fd(raw) };
let id = ((!fd.as_raw_fd()) << 3) | 3;
Ok(Self { _fd: fd, id })
}
pub fn now(&self) -> io::Result<Duration> {
read(self.id)
}
fn adjust(&self, modes: libc::c_uint, frequency: libc::c_long) -> io::Result<libc::timex> {
let mut time: libc::timex = unsafe { std::mem::zeroed() };
time.modes = modes;
time.freq = frequency;
if unsafe { libc::clock_adjtime(self.id, &raw mut time) } < 0 {
return Err(io::Error::last_os_error());
}
Ok(time)
}
const SCALED_PPB: f64 = 65.536;
pub fn frequency_adjustment(&self) -> io::Result<f64> {
Ok(self.adjust(0, 0)?.freq as f64 / Self::SCALED_PPB)
}
pub fn set_frequency_adjustment(&self, ppb: f64) -> io::Result<()> {
self.adjust(
libc::ADJ_FREQUENCY,
(ppb * Self::SCALED_PPB).round() as libc::c_long,
)
.map(|_| ())
}
pub fn step(&self, by: i128) -> io::Result<()> {
let mut time: libc::timex = unsafe { std::mem::zeroed() };
time.modes = libc::ADJ_SETOFFSET | libc::ADJ_NANO;
time.time.tv_sec = by.div_euclid(1_000_000_000) as libc::time_t;
time.time.tv_usec = by.rem_euclid(1_000_000_000) as libc::suseconds_t;
if unsafe { libc::clock_adjtime(self.id, &raw mut time) } < 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
pub fn reading_from_system(&self, system: SystemClock) -> io::Result<Reading> {
self.extended_reading(system).map_err(|error| {
if error.raw_os_error() == Some(libc::EOPNOTSUPP) {
io::Error::new(io::ErrorKind::Unsupported, error)
} else {
error
}
})
}
fn extended_reading(&self, system: SystemClock) -> io::Result<Reading> {
#[repr(C)]
#[derive(Clone, Copy, Default)]
struct Time {
seconds: i64,
nanoseconds: u32,
reserved: u32,
}
#[repr(C)]
struct Extended {
samples: u32,
clock: i32,
reserved: [u32; 2],
times: [[Time; 3]; 25],
}
const PTP_SYS_OFFSET_EXTENDED: libc::c_ulong =
0xc000_0000 | ((size_of::<Extended>() as libc::c_ulong) << 16) | (0x3d << 8) | 9;
let mut request = Extended {
samples: 9,
clock: id_of(system),
reserved: [0; 2],
times: [[Time::default(); 3]; 25],
};
if unsafe {
libc::ioctl(
self._fd.as_raw_fd(),
PTP_SYS_OFFSET_EXTENDED as _,
&raw mut request,
)
} < 0
{
return Err(io::Error::last_os_error());
}
let nanos = |time: Time| {
i128::from(time.seconds) * 1_000_000_000 + i128::from(time.nanoseconds)
};
request.times[..request.samples.min(25) as usize]
.iter()
.map(|[before, clock, after]| {
let (before, after) = (nanos(*before), nanos(*after));
Reading {
offset: nanos(*clock) - (before + after) / 2,
span: u64::try_from(after - before).unwrap_or(u64::MAX),
}
})
.min_by_key(|reading| reading.span)
.ok_or_else(|| io::Error::other("the clock gave no samples"))
}
}
pub fn monotonic_now() -> Duration {
read(libc::CLOCK_MONOTONIC).unwrap_or_default()
}
fn id_of(system: SystemClock) -> libc::clockid_t {
match system {
SystemClock::Realtime => libc::CLOCK_REALTIME,
SystemClock::Monotonic => libc::CLOCK_MONOTONIC,
SystemClock::MonotonicRaw => libc::CLOCK_MONOTONIC_RAW,
SystemClock::Tai => libc::CLOCK_TAI,
}
}
pub fn system_now(system: SystemClock) -> Duration {
read(id_of(system)).unwrap_or_default()
}
pub fn suspended() -> Duration {
let boot = read(libc::CLOCK_BOOTTIME).unwrap_or_default();
boot.saturating_sub(monotonic_now())
}
pub fn precise_sleeps() {
unsafe {
libc::prctl(libc::PR_SET_TIMERSLACK, 1 as libc::c_ulong);
}
}
}
#[cfg(not(target_os = "linux"))]
mod platform {
use std::io;
use std::time::Duration;
pub struct HardwareClock;
impl HardwareClock {
pub fn open(_index: u32, _adjustable: bool) -> io::Result<Self> {
Err(unsupported())
}
pub fn now(&self) -> io::Result<Duration> {
Ok(Duration::ZERO)
}
pub fn frequency_adjustment(&self) -> io::Result<f64> {
Err(unsupported())
}
pub fn set_frequency_adjustment(&self, _ppb: f64) -> io::Result<()> {
Err(unsupported())
}
pub fn step(&self, _by: i128) -> io::Result<()> {
Err(unsupported())
}
pub fn reading_from_system(
&self,
_system: super::SystemClock,
) -> io::Result<super::Reading> {
Err(unsupported())
}
}
fn unsupported() -> io::Error {
io::Error::new(
io::ErrorKind::Unsupported,
"hardware clocks are not supported on this system yet",
)
}
pub fn monotonic_now() -> Duration {
use std::sync::OnceLock;
static START: OnceLock<std::time::Instant> = OnceLock::new();
START.get_or_init(std::time::Instant::now).elapsed()
}
pub fn system_now(_system: super::SystemClock) -> Duration {
monotonic_now()
}
pub fn suspended() -> Duration {
Duration::ZERO
}
pub fn precise_sleeps() {}
}
#[cfg(test)]
mod comparison_tests {
use super::*;
#[test]
fn a_steady_drift_is_followed_between_comparisons() {
let mut comparison = Comparison::default();
for step in 0..30i128 {
comparison.update(step * 100_000_000, 1_000 + step * 600);
}
let last = 29 * 100_000_000;
assert_eq!(comparison.offset_at(last), 1_000 + 29 * 600);
assert_eq!(
comparison.offset_at(last + 50_000_000),
1_000 + 29 * 600 + 300
);
assert_eq!(
comparison.offset_at(last - 50_000_000),
1_000 + 29 * 600 - 300
);
assert!((comparison.rate().unwrap() - 6e-6).abs() < 1e-12);
}
#[test]
fn the_error_of_one_comparison_is_mostly_filtered_out() {
let mut comparison = Comparison::default();
for step in 0..20i128 {
let error = if step % 2 == 0 { 50 } else { -50 };
comparison.update(step * 100_000_000, step * 600 + error);
}
let last = 19 * 100_000_000;
let off = comparison.offset_at(last) - 19 * 600;
assert!(off.abs() <= 15, "off by {off} ns");
}
#[test]
fn one_comparison_is_taken_as_it_is() {
let mut comparison = Comparison::default();
assert!(comparison.is_empty());
assert_eq!(comparison.offset_at(5), 0);
comparison.update(10, 42);
assert_eq!(comparison.offset_at(1_000_000), 42);
assert_eq!(comparison.rate(), None);
comparison.clear();
assert!(comparison.is_empty());
}
}
#[cfg(all(test, target_os = "linux"))]
mod tests {
use super::*;
#[test]
fn a_hardware_clock_reads_when_there_is_one() {
let Ok(clock) = HardwareClock::open(0) else {
return;
};
let first = clock.offset_from_monotonic().unwrap();
std::thread::sleep(Duration::from_millis(20));
let second = clock.offset_from_monotonic().unwrap();
assert!((second - first).abs() < 100_000, "{first} then {second}");
}
#[test]
fn the_monotonic_clock_moves_on() {
let before = monotonic_now();
std::thread::sleep(Duration::from_millis(2));
assert!(monotonic_now() > before);
}
}