pub struct HardwareClock { /* private fields */ }Expand description
A PTP hardware clock.
Implementations§
Source§impl HardwareClock
impl HardwareClock
Sourcepub fn open(index: u32) -> Result<Self>
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?
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}Sourcepub fn open_adjustable(index: u32) -> Result<Self>
pub fn open_adjustable(index: u32) -> Result<Self>
Opens the clock with index to adjust it as well as read it.
Sourcepub fn frequency_adjustment(&self) -> Result<f64>
pub fn frequency_adjustment(&self) -> Result<f64>
How far the clock’s frequency is adjusted from its oscillator’s, in parts per billion.
Sourcepub fn set_frequency_adjustment(&self, ppb: f64) -> Result<()>
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.
Sourcepub fn now(&self) -> Result<Duration>
pub fn now(&self) -> Result<Duration>
The clock’s time, from the PTP epoch.
Examples found in repository?
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}Sourcepub fn offset_from(&self, other: &HardwareClock) -> Result<i128>
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.
Sourcepub fn reading_from(&self, other: &HardwareClock) -> Result<Reading>
pub fn reading_from(&self, other: &HardwareClock) -> Result<Reading>
offset_from with its span: the two clocks’
spans together.
Sourcepub fn offset_from_system_clock(&self, system: SystemClock) -> Result<i128>
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.
Sourcepub fn reading_from_system_clock(&self, system: SystemClock) -> Result<Reading>
pub fn reading_from_system_clock(&self, system: SystemClock) -> Result<Reading>
offset_from_system_clock with
the span of the reading kept.
Sourcepub fn offset_from_monotonic(&self) -> Result<i128>
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).
Sourcepub fn offset_read_against(&self, system: SystemClock) -> Result<i128>
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.
Sourcepub fn reading_against(&self, system: SystemClock) -> Result<Reading>
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.