From 9800e397fbf5f7e43ad4217d803aed71a2e0e07c Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 19 Jan 2017 12:45:44 +0100 Subject: [PATCH] hwclock: make minimum sampling separation configurable --- hwclock.c | 13 +++++++------ hwclock.h | 2 +- ntp_io_linux.c | 2 +- test/unit/hwclock.c | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/hwclock.c b/hwclock.c index c44a795..e6f26fd 100644 --- a/hwclock.c +++ b/hwclock.c @@ -39,9 +39,6 @@ /* Maximum number of samples per clock */ #define MAX_SAMPLES 16 -/* Minimum interval between samples (in seconds) */ -#define MIN_SAMPLE_SEPARATION 1.0 - struct HCL_Instance_Record { /* HW and local reference timestamp */ struct timespec hw_ref; @@ -58,6 +55,9 @@ struct HCL_Instance_Record { /* Maximum error of the last sample */ double last_err; + /* Minimum interval between samples */ + double min_separation; + /* Flag indicating the offset and frequency values are valid */ int valid_coefs; @@ -86,7 +86,7 @@ handle_slew(struct timespec *raw, struct timespec *cooked, double dfreq, /* ================================================== */ HCL_Instance -HCL_CreateInstance(void) +HCL_CreateInstance(double min_separation) { HCL_Instance clock; @@ -95,6 +95,7 @@ HCL_CreateInstance(void) clock->y_data[MAX_SAMPLES - 1] = 0.0; clock->n_samples = 0; clock->valid_coefs = 0; + clock->min_separation = min_separation; LCL_AddParameterChangeHandler(handle_slew, clock); @@ -115,7 +116,7 @@ int HCL_NeedsNewSample(HCL_Instance clock, struct timespec *now) { if (!clock->n_samples || - fabs(UTI_DiffTimespecsToDouble(now, &clock->local_ref)) >= MIN_SAMPLE_SEPARATION) + fabs(UTI_DiffTimespecsToDouble(now, &clock->local_ref)) >= clock->min_separation) return 1; return 0; @@ -140,7 +141,7 @@ HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts, hw_delta = UTI_DiffTimespecsToDouble(hw_ts, &clock->hw_ref); local_delta = UTI_DiffTimespecsToDouble(local_ts, &clock->local_ref) / local_freq; - if (hw_delta <= 0.0 || local_delta < MIN_SAMPLE_SEPARATION / 2.0) { + if (hw_delta <= 0.0 || local_delta < clock->min_separation / 2.0) { clock->n_samples = 0; DEBUG_LOG(LOGF_HwClocks, "HW clock reset interval=%f", local_delta); } diff --git a/hwclock.h b/hwclock.h index 11a79b0..f80d09a 100644 --- a/hwclock.h +++ b/hwclock.h @@ -29,7 +29,7 @@ typedef struct HCL_Instance_Record *HCL_Instance; /* Create a new HW clock instance */ -extern HCL_Instance HCL_CreateInstance(void); +extern HCL_Instance HCL_CreateInstance(double min_separation); /* Destroy a HW clock instance */ extern void HCL_DestroyInstance(HCL_Instance clock); diff --git a/ntp_io_linux.c b/ntp_io_linux.c index 41a8903..3a87cad 100644 --- a/ntp_io_linux.c +++ b/ntp_io_linux.c @@ -179,7 +179,7 @@ add_interface(CNF_HwTsInterface *conf_iface) iface->tx_comp = conf_iface->tx_comp; iface->rx_comp = conf_iface->rx_comp; - iface->clock = HCL_CreateInstance(); + iface->clock = HCL_CreateInstance(1.0); DEBUG_LOG(LOGF_NtpIOLinux, "Enabled HW timestamping on %s", iface->name); diff --git a/test/unit/hwclock.c b/test/unit/hwclock.c index f5667ed..51e41ec 100644 --- a/test/unit/hwclock.c +++ b/test/unit/hwclock.c @@ -31,7 +31,7 @@ test_unit(void) LCL_Initialise(); - clock = HCL_CreateInstance(); + clock = HCL_CreateInstance(1.0); for (i = 0; i < 2000; i++) { UTI_ZeroTimespec(&start_hw_ts); @@ -43,7 +43,7 @@ test_unit(void) freq = TST_GetRandomDouble(0.9, 1.1); jitter = TST_GetRandomDouble(10.0e-9, 1000.0e-9); - interval = TST_GetRandomDouble(MIN_SAMPLE_SEPARATION / 10, MIN_SAMPLE_SEPARATION * 10.0); + interval = TST_GetRandomDouble(0.1, 10.0); clock->n_samples = 0; clock->valid_coefs = 0;