From 6dad2c24bf316c3e9ffe977a638334f3e3e8cde8 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 22 Aug 2017 13:13:45 +0200 Subject: [PATCH] hwclock: drop all samples on reset On some HW it seems it's possible to get an occasional bad reading of the PHC (with normal delay), or in a worse case the clock can step due to a HW/driver bug, which triggers reset of the HW clock instance. To avoid having a bad estimate of the frequency when the next (good) sample is accumulated, drop also the last sample which triggered the reset. --- hwclock.c | 10 +++++----- test/unit/hwclock.c | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hwclock.c b/hwclock.c index 84a130d..a6427c3 100644 --- a/hwclock.c +++ b/hwclock.c @@ -174,12 +174,12 @@ HCL_AccumulateSample(HCL_Instance clock, struct timespec *hw_ts, /* Drop unneeded samples */ clock->n_samples -= best_start; - /* If the fit doesn't cross the error interval of the last sample, throw away - all previous samples and keep only the frequency estimate */ + /* If the fit doesn't cross the error interval of the last sample, + throw away all samples and start again */ if (fabs(clock->offset) > err) { - DEBUG_LOG("HW clock reset offset=%e", clock->offset); - clock->offset = 0.0; - clock->n_samples = 1; + DEBUG_LOG("HW clock reset"); + clock->n_samples = 0; + clock->valid_coefs = 0; } DEBUG_LOG("HW clock samples=%d offset=%e freq=%.9e raw_freq=%.9e err=%e ref_diff=%e", diff --git a/test/unit/hwclock.c b/test/unit/hwclock.c index ed4bde1..804e600 100644 --- a/test/unit/hwclock.c +++ b/test/unit/hwclock.c @@ -65,7 +65,7 @@ test_unit(void) if (HCL_NeedsNewSample(clock, &local_ts)) HCL_AccumulateSample(clock, &hw_ts, &local_ts, 2.0 * jitter); - TEST_CHECK(j < 20 || clock->valid_coefs); + TEST_CHECK(clock->valid_coefs || clock->n_samples < 2); if (!clock->valid_coefs) continue;