2018-03-04 15:24:24 -05:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
2021-02-17 16:28:48 -05:00
|
|
|
// Copyright (C) 2018 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception.
|
2016-12-06 21:41:44 -05:00
|
|
|
|
|
|
|
//! Clock interface and implementations for testability.
|
|
|
|
|
2018-03-04 15:24:24 -05:00
|
|
|
use failure::Error;
|
2016-12-30 00:05:57 -05:00
|
|
|
use std::mem;
|
2022-09-29 01:19:35 -04:00
|
|
|
use std::sync::Mutex;
|
2021-02-17 01:15:54 -05:00
|
|
|
use std::sync::{mpsc, Arc};
|
2016-12-06 21:41:44 -05:00
|
|
|
use std::thread;
|
2018-03-23 18:16:43 -04:00
|
|
|
use std::time::Duration as StdDuration;
|
2016-12-30 00:05:57 -05:00
|
|
|
use time::{Duration, Timespec};
|
2023-02-16 02:14:54 -05:00
|
|
|
use tracing::warn;
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2021-09-23 18:55:53 -04:00
|
|
|
use crate::shutdown::ShutdownError;
|
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
/// Abstract interface to the system clocks. This is for testability.
|
2021-02-17 01:15:54 -05:00
|
|
|
pub trait Clocks: Send + Sync + 'static {
|
2016-12-30 00:05:57 -05:00
|
|
|
/// Gets the current time from `CLOCK_REALTIME`.
|
|
|
|
fn realtime(&self) -> Timespec;
|
|
|
|
|
2021-09-16 21:21:27 -04:00
|
|
|
/// Gets the current time from a monotonic clock.
|
|
|
|
///
|
|
|
|
/// On Linux, this uses `CLOCK_BOOTTIME`, which includes suspended time.
|
|
|
|
/// On other systems, it uses `CLOCK_MONOTONIC`.
|
2016-12-30 00:05:57 -05:00
|
|
|
fn monotonic(&self) -> Timespec;
|
2016-12-06 21:41:44 -05:00
|
|
|
|
|
|
|
/// Causes the current thread to sleep for the specified time.
|
2016-12-30 00:05:57 -05:00
|
|
|
fn sleep(&self, how_long: Duration);
|
2018-03-23 18:16:43 -04:00
|
|
|
|
|
|
|
/// Calls `rcv.recv_timeout` or substitutes a test implementation.
|
2021-02-17 01:15:54 -05:00
|
|
|
fn recv_timeout<T>(
|
|
|
|
&self,
|
|
|
|
rcv: &mpsc::Receiver<T>,
|
|
|
|
timeout: StdDuration,
|
|
|
|
) -> Result<T, mpsc::RecvTimeoutError>;
|
2018-03-09 20:41:53 -05:00
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2021-09-23 18:55:53 -04:00
|
|
|
pub fn retry<C, T, E>(
|
|
|
|
clocks: &C,
|
|
|
|
shutdown_rx: &crate::shutdown::Receiver,
|
|
|
|
f: &mut dyn FnMut() -> Result<T, E>,
|
|
|
|
) -> Result<T, ShutdownError>
|
2021-02-17 01:15:54 -05:00
|
|
|
where
|
|
|
|
C: Clocks,
|
|
|
|
E: Into<Error>,
|
|
|
|
{
|
2018-03-09 20:41:53 -05:00
|
|
|
loop {
|
|
|
|
let e = match f() {
|
2021-09-23 18:55:53 -04:00
|
|
|
Ok(t) => return Ok(t),
|
2018-03-09 20:41:53 -05:00
|
|
|
Err(e) => e.into(),
|
|
|
|
};
|
2021-09-23 18:55:53 -04:00
|
|
|
shutdown_rx.check()?;
|
2018-03-09 20:41:53 -05:00
|
|
|
let sleep_time = Duration::seconds(1);
|
2021-02-17 01:15:54 -05:00
|
|
|
warn!(
|
2023-02-16 02:14:54 -05:00
|
|
|
err = crate::error::prettify_failure(&e),
|
|
|
|
"sleeping for 1 s after error"
|
2021-02-17 01:15:54 -05:00
|
|
|
);
|
2018-03-09 20:41:53 -05:00
|
|
|
clocks.sleep(sleep_time);
|
2018-03-04 15:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
2016-12-30 00:05:57 -05:00
|
|
|
|
2018-03-23 16:31:23 -04:00
|
|
|
#[derive(Copy, Clone)]
|
2016-12-30 00:05:57 -05:00
|
|
|
pub struct RealClocks {}
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
impl RealClocks {
|
|
|
|
fn get(&self, clock: libc::clockid_t) -> Timespec {
|
|
|
|
unsafe {
|
2019-12-28 08:51:47 -05:00
|
|
|
let mut ts = mem::MaybeUninit::uninit();
|
|
|
|
assert_eq!(0, libc::clock_gettime(clock, ts.as_mut_ptr()));
|
|
|
|
let ts = ts.assume_init();
|
2023-01-28 14:59:21 -05:00
|
|
|
Timespec::new(ts.tv_sec, ts.tv_nsec as i32)
|
2016-12-30 00:05:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
impl Clocks for RealClocks {
|
2021-02-17 01:15:54 -05:00
|
|
|
fn realtime(&self) -> Timespec {
|
|
|
|
self.get(libc::CLOCK_REALTIME)
|
|
|
|
}
|
2021-09-16 21:21:27 -04:00
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn monotonic(&self) -> Timespec {
|
|
|
|
self.get(libc::CLOCK_BOOTTIME)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
2021-02-17 01:15:54 -05:00
|
|
|
fn monotonic(&self) -> Timespec {
|
|
|
|
self.get(libc::CLOCK_MONOTONIC)
|
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
fn sleep(&self, how_long: Duration) {
|
2016-12-06 21:41:44 -05:00
|
|
|
match how_long.to_std() {
|
|
|
|
Ok(d) => thread::sleep(d),
|
|
|
|
Err(e) => warn!("Invalid duration {:?}: {}", how_long, e),
|
|
|
|
};
|
|
|
|
}
|
2018-03-23 18:16:43 -04:00
|
|
|
|
2021-02-17 01:15:54 -05:00
|
|
|
fn recv_timeout<T>(
|
|
|
|
&self,
|
|
|
|
rcv: &mpsc::Receiver<T>,
|
|
|
|
timeout: StdDuration,
|
|
|
|
) -> Result<T, mpsc::RecvTimeoutError> {
|
2018-03-23 18:16:43 -04:00
|
|
|
rcv.recv_timeout(timeout)
|
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
}
|
|
|
|
|
2018-01-31 17:20:30 -05:00
|
|
|
/// Logs a warning if the TimerGuard lives "too long", using the label created by a supplied
|
|
|
|
/// function.
|
2018-03-09 20:41:53 -05:00
|
|
|
pub struct TimerGuard<'a, C: Clocks + ?Sized, S: AsRef<str>, F: FnOnce() -> S + 'a> {
|
2018-01-31 17:20:30 -05:00
|
|
|
clocks: &'a C,
|
|
|
|
label_f: Option<F>,
|
|
|
|
start: Timespec,
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:41:53 -05:00
|
|
|
impl<'a, C: Clocks + ?Sized, S: AsRef<str>, F: FnOnce() -> S + 'a> TimerGuard<'a, C, S, F> {
|
2018-01-31 17:20:30 -05:00
|
|
|
pub fn new(clocks: &'a C, label_f: F) -> Self {
|
|
|
|
TimerGuard {
|
|
|
|
clocks,
|
|
|
|
label_f: Some(label_f),
|
|
|
|
start: clocks.monotonic(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:41:53 -05:00
|
|
|
impl<'a, C, S, F> Drop for TimerGuard<'a, C, S, F>
|
2021-02-17 01:15:54 -05:00
|
|
|
where
|
|
|
|
C: Clocks + ?Sized,
|
|
|
|
S: AsRef<str>,
|
|
|
|
F: FnOnce() -> S + 'a,
|
|
|
|
{
|
2018-01-31 17:20:30 -05:00
|
|
|
fn drop(&mut self) {
|
|
|
|
let elapsed = self.clocks.monotonic() - self.start;
|
|
|
|
if elapsed.num_seconds() >= 1 {
|
|
|
|
let label_f = self.label_f.take().unwrap();
|
|
|
|
warn!("{} took {}!", label_f().as_ref(), elapsed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:41:44 -05:00
|
|
|
/// Simulated clock for testing.
|
2018-03-04 15:24:24 -05:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SimulatedClocks(Arc<SimulatedClocksInner>);
|
|
|
|
|
|
|
|
struct SimulatedClocksInner {
|
2016-12-30 00:05:57 -05:00
|
|
|
boot: Timespec,
|
|
|
|
uptime: Mutex<Duration>,
|
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
impl SimulatedClocks {
|
2018-03-04 15:24:24 -05:00
|
|
|
pub fn new(boot: Timespec) -> Self {
|
|
|
|
SimulatedClocks(Arc::new(SimulatedClocksInner {
|
2021-05-17 17:31:50 -04:00
|
|
|
boot,
|
2016-12-30 00:05:57 -05:00
|
|
|
uptime: Mutex::new(Duration::seconds(0)),
|
2018-03-04 15:24:24 -05:00
|
|
|
}))
|
2016-12-30 00:05:57 -05:00
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
}
|
|
|
|
|
2016-12-30 00:05:57 -05:00
|
|
|
impl Clocks for SimulatedClocks {
|
2021-02-17 01:15:54 -05:00
|
|
|
fn realtime(&self) -> Timespec {
|
2022-09-29 01:19:35 -04:00
|
|
|
self.0.boot + *self.0.uptime.lock().unwrap()
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
|
|
|
fn monotonic(&self) -> Timespec {
|
2022-09-29 01:19:35 -04:00
|
|
|
Timespec::new(0, 0) + *self.0.uptime.lock().unwrap()
|
2021-02-17 01:15:54 -05:00
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
|
|
|
|
/// Advances the clock by the specified amount without actually sleeping.
|
2016-12-30 00:05:57 -05:00
|
|
|
fn sleep(&self, how_long: Duration) {
|
2022-09-29 01:19:35 -04:00
|
|
|
let mut l = self.0.uptime.lock().unwrap();
|
2016-12-06 21:41:44 -05:00
|
|
|
*l = *l + how_long;
|
|
|
|
}
|
2018-03-23 18:16:43 -04:00
|
|
|
|
|
|
|
/// Advances the clock by the specified amount if data is not immediately available.
|
2021-02-17 01:15:54 -05:00
|
|
|
fn recv_timeout<T>(
|
|
|
|
&self,
|
|
|
|
rcv: &mpsc::Receiver<T>,
|
|
|
|
timeout: StdDuration,
|
|
|
|
) -> Result<T, mpsc::RecvTimeoutError> {
|
2018-03-23 18:16:43 -04:00
|
|
|
let r = rcv.recv_timeout(StdDuration::new(0, 0));
|
2021-05-17 17:31:50 -04:00
|
|
|
if r.is_err() {
|
2018-03-23 18:16:43 -04:00
|
|
|
self.sleep(Duration::from_std(timeout).unwrap());
|
|
|
|
}
|
|
|
|
r
|
|
|
|
}
|
2016-12-06 21:41:44 -05:00
|
|
|
}
|