Skip to main content

HardwareClock

Struct HardwareClock 

Source
pub struct HardwareClock { /* private fields */ }
Expand description

A PTP hardware clock.

Implementations§

Source§

impl HardwareClock

Source

pub fn open(index: u32) -> Result<Self>

Opens the clock with index, as an interface reports it in crate::Interface::hardware_clock.

Examples found in repository?
examples/phc_watch.rs (line 14)
10fn main() -> std::io::Result<()> {
11    let mut args = std::env::args().skip(1);
12    let index = args.next().and_then(|text| text.parse().ok()).unwrap_or(0);
13    let seconds = args.next().and_then(|text| text.parse().ok()).unwrap_or(60);
14    let clock = HardwareClock::open(index)?;
15    let until = Instant::now() + Duration::from_secs(seconds);
16    let mut last: Option<i128> = None;
17    let mut reads = 0u64;
18    let mut strays = 0u64;
19    while Instant::now() < until {
20        let before = monotonic_now().as_nanos() as i128;
21        let phc = clock.now()?.as_nanos() as i128;
22        let after = monotonic_now().as_nanos() as i128;
23        let offset = phc - (before + after) / 2;
24        reads += 1;
25        if let Some(previous) = last
26            && (offset - previous).abs() > 100_000
27        {
28            strays += 1;
29            println!(
30                "read {reads}: offset moved {} ns (read took {} ns)",
31                offset - previous,
32                after - before
33            );
34        }
35        last = Some(offset);
36        std::thread::sleep(Duration::from_micros(500));
37    }
38    println!("{reads} reads, {strays} strays");
39    Ok(())
40}
Source

pub fn open_adjustable(index: u32) -> Result<Self>

Opens the clock with index to adjust it as well as read it.

Source

pub fn frequency_adjustment(&self) -> Result<f64>

How far the clock’s frequency is adjusted from its oscillator’s, in parts per billion.

Source

pub fn set_frequency_adjustment(&self, ppb: f64) -> Result<()>

Adjusts the clock’s frequency to ppb parts per billion from its oscillator’s: 0 lets it run free. Needs the clock opened with open_adjustable.

Source

pub fn now(&self) -> Result<Duration>

The clock’s time, from the PTP epoch.

Examples found in repository?
examples/phc_watch.rs (line 21)
10fn main() -> std::io::Result<()> {
11    let mut args = std::env::args().skip(1);
12    let index = args.next().and_then(|text| text.parse().ok()).unwrap_or(0);
13    let seconds = args.next().and_then(|text| text.parse().ok()).unwrap_or(60);
14    let clock = HardwareClock::open(index)?;
15    let until = Instant::now() + Duration::from_secs(seconds);
16    let mut last: Option<i128> = None;
17    let mut reads = 0u64;
18    let mut strays = 0u64;
19    while Instant::now() < until {
20        let before = monotonic_now().as_nanos() as i128;
21        let phc = clock.now()?.as_nanos() as i128;
22        let after = monotonic_now().as_nanos() as i128;
23        let offset = phc - (before + after) / 2;
24        reads += 1;
25        if let Some(previous) = last
26            && (offset - previous).abs() > 100_000
27        {
28            strays += 1;
29            println!(
30                "read {reads}: offset moved {} ns (read took {} ns)",
31                offset - previous,
32                after - before
33            );
34        }
35        last = Some(offset);
36        std::thread::sleep(Duration::from_micros(500));
37    }
38    println!("{reads} reads, {strays} strays");
39    Ok(())
40}
Source

pub fn offset_from(&self, other: &HardwareClock) -> Result<i128>

How far this clock is ahead of other, in nanoseconds: each read by its driver between two readings of the system clock, the tightest of several pairs kept, so the system clock drops out. Linux only; elsewhere unsupported.

Source

pub fn reading_from(&self, other: &HardwareClock) -> Result<Reading>

offset_from with its span: the two clocks’ spans together.

Source

pub fn offset_from_system_clock(&self, system: SystemClock) -> Result<i128>

How far this clock is ahead of system, in nanoseconds, from its driver’s tightest of several readings between two of system’s (PTP_SYS_OFFSET_EXTENDED). A kernel too old to take a clock other than the wall clock for it refuses the others, and then offset_read_against serves.

Source

pub fn reading_from_system_clock(&self, system: SystemClock) -> Result<Reading>

offset_from_system_clock with the span of the reading kept.

Source

pub fn offset_from_monotonic(&self) -> Result<i128>

How far the clock is ahead of this computer’s monotonic clock, in nanoseconds, read between two readings of the monotonic clock: of a few tries, the closest-read of those near their median, as a clock can now and then be read wrong (a 2^32 ns tear has been seen on one card’s).

Source

pub fn offset_read_against(&self, system: SystemClock) -> Result<i128>

How far the clock is ahead of system, in nanoseconds, read as offset_from_monotonic reads it: looser than the driver’s readings, but on every kernel.

Source

pub fn reading_against(&self, system: SystemClock) -> Result<Reading>

offset_read_against with the span of the try kept. Where none stands near the median, the median’s.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.