pub const MACOS_MAX_HORIZON_S: f64 = 1024.0;
pub fn macos_adjtime_amount(freq_ppm: f64, drain_offset: f64, drain_rate_ppm: f64) -> f64 {
let horizon_s = if drain_rate_ppm > 0.0 {
(drain_offset.abs() / (drain_rate_ppm * 1e-6)).min(MACOS_MAX_HORIZON_S)
} else {
0.0
};
drain_offset + freq_ppm * 1e-6 * horizon_s
}
pub fn total_ppm(freq_ppm: f64, drain_offset: f64, drain_rate_ppm: f64, max_ppm: f64) -> f64 {
let drain = drain_rate_ppm.copysign(drain_offset);
(freq_ppm + drain).clamp(-max_ppm, max_ppm)
}
pub const LINUX_MAX_ADJ_FREQ_PPM: f64 = 500.0;
pub const LINUX_TICK_RANGE_FRACTION: f64 = 0.1;
pub fn linux_tick_and_freq(total_ppm: f64, nominal_tick_us: i64) -> (i64, f64) {
if nominal_tick_us <= 0 {
return (
nominal_tick_us,
total_ppm.clamp(-LINUX_MAX_ADJ_FREQ_PPM, LINUX_MAX_ADJ_FREQ_PPM),
);
}
let ppm_per_us = 1e6 / nominal_tick_us as f64;
let max_offset_us = (nominal_tick_us as f64 * LINUX_TICK_RANGE_FRACTION).floor() as i64;
let reachable = max_offset_us as f64 * ppm_per_us + LINUX_MAX_ADJ_FREQ_PPM;
let wanted = total_ppm.clamp(-reachable, reachable);
let mut offset_us = 0i64;
if wanted.abs() > LINUX_MAX_ADJ_FREQ_PPM {
offset_us = (wanted / ppm_per_us).round() as i64;
offset_us = offset_us.clamp(-max_offset_us, max_offset_us);
}
let residual = (wanted - offset_us as f64 * ppm_per_us)
.clamp(-LINUX_MAX_ADJ_FREQ_PPM, LINUX_MAX_ADJ_FREQ_PPM);
(nominal_tick_us + offset_us, residual)
}
pub fn linux_max_slew_ppm(nominal_tick_us: i64) -> f64 {
if nominal_tick_us <= 0 {
return LINUX_MAX_ADJ_FREQ_PPM;
}
let ppm_per_us = 1e6 / nominal_tick_us as f64;
(nominal_tick_us as f64 * LINUX_TICK_RANGE_FRACTION).floor() * ppm_per_us
+ LINUX_MAX_ADJ_FREQ_PPM
}
pub fn windows_adjustment(increment: u64, total_ppm: f64) -> u64 {
let scaled = increment as f64 * (1.0 + total_ppm * 1e-6);
if scaled <= 0.0 { 0 } else { scaled as u64 }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn drain_direction_sets_the_sign() {
assert!(total_ppm(0.0, 0.010, 50.0, 1e6) > 0.0);
assert!(total_ppm(0.0, -0.010, 50.0, 1e6) < 0.0);
assert!((total_ppm(10.0, 0.001, 5.0, 1e6) - 15.0).abs() < 1e-9);
}
#[test]
fn total_ppm_respects_the_platform_ceiling() {
assert_eq!(total_ppm(1e9, 1.0, 1e9, 500.0), 500.0);
assert_eq!(total_ppm(-1e9, -1.0, 1e9, 500.0), -500.0);
}
const TICK_100HZ: i64 = 10_000;
#[test]
fn small_corrections_leave_the_tick_alone() {
for ppm in [0.0, 1.0, -50.0, 499.0, -499.0] {
let (tick, residual) = linux_tick_and_freq(ppm, TICK_100HZ);
assert_eq!(tick, TICK_100HZ, "tick moved for {ppm} ppm");
assert!((residual - ppm).abs() < 1e-9);
}
}
#[test]
fn large_corrections_are_delivered_in_full() {
for ppm in [1_716.0, -1_716.0, 5_000.0, -20_000.0, 83_333.0] {
let (tick, residual) = linux_tick_and_freq(ppm, TICK_100HZ);
let delivered = (tick - TICK_100HZ) as f64 * (1e6 / TICK_100HZ as f64) + residual;
assert!(
(delivered - ppm).abs() < 1e-6,
"asked {ppm} ppm, driver would deliver {delivered} ppm"
);
}
}
#[test]
fn the_residual_always_fits_the_kernels_frequency_limit() {
for tick in [1_000i64, 10_000, 4_000] {
for ppm in [0.0, 600.0, -600.0, 1e5, -1e5, 1e9, -1e9] {
let (_, residual) = linux_tick_and_freq(ppm, tick);
assert!(
residual.abs() <= LINUX_MAX_ADJ_FREQ_PPM + 1e-9,
"tick {tick}, {ppm} ppm left residual {residual}"
);
}
}
}
#[test]
fn the_tick_stays_inside_what_the_kernel_will_accept() {
for tick in [1_000i64, 10_000, 4_000] {
for ppm in [1e9, -1e9, 250_000.0] {
let (out, _) = linux_tick_and_freq(ppm, tick);
assert!(
out >= 900_000 / (1_000_000 / tick) && out <= 1_100_000 / (1_000_000 / tick),
"tick {out} outside the kernel's window for nominal {tick}"
);
}
}
}
#[test]
fn the_advertised_ceiling_is_one_the_driver_can_reach() {
for tick in [1_000i64, 10_000, 4_000] {
let ceiling = linux_max_slew_ppm(tick);
let (out, residual) = linux_tick_and_freq(ceiling, tick);
let delivered = (out - tick) as f64 * (1e6 / tick as f64) + residual;
assert!(
(delivered - ceiling).abs() < 1e-6,
"advertised {ceiling} ppm but could only deliver {delivered}"
);
}
assert!(linux_max_slew_ppm(TICK_100HZ) > 99_000.0);
}
#[test]
fn windows_adjustment_scales_the_increment() {
let increment = 156_250u64; assert_eq!(windows_adjustment(increment, 0.0), increment);
let faster = windows_adjustment(increment, 1_000.0);
assert!((faster as f64 / increment as f64 - 1.001).abs() < 1e-4);
let slower = windows_adjustment(increment, -1_000.0);
assert!(slower < increment);
}
#[test]
fn windows_adjustment_never_wraps_negative() {
assert_eq!(windows_adjustment(1000, -2_000_000.0), 0);
}
#[test]
fn macos_amount_carries_the_offset_when_there_is_no_frequency_term() {
let amount = macos_adjtime_amount(0.0, 0.005, 100.0);
assert!((amount - 0.005).abs() < 1e-12);
}
#[test]
fn macos_amount_projects_frequency_over_the_drain_horizon() {
let amount = macos_adjtime_amount(20.0, 0.010, 100.0);
assert!(
(amount - 0.012).abs() < 1e-9,
"expected 12 ms, got {amount}"
);
}
#[test]
fn macos_horizon_is_bounded() {
let amount = macos_adjtime_amount(500.0, 1.0, 1e-9);
let worst = 1.0 + 500.0 * 1e-6 * MACOS_MAX_HORIZON_S;
assert!(
amount <= worst + 1e-9,
"amount {amount} exceeded the bounded horizon {worst}"
);
}
#[test]
fn macos_amount_is_zero_when_there_is_nothing_to_do() {
assert_eq!(macos_adjtime_amount(0.0, 0.0, 0.0), 0.0);
assert_eq!(macos_adjtime_amount(50.0, 0.0, 0.0), 0.0);
}
#[test]
fn macos_sign_follows_the_offset() {
assert!(macos_adjtime_amount(0.0, -0.005, 100.0) < 0.0);
assert!(macos_adjtime_amount(0.0, 0.005, 100.0) > 0.0);
}
}