From fbad51ac199308cb69f62ccea31c1750d16fa710 Mon Sep 17 00:00:00 2001 From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Mon, 10 May 2021 16:04:47 +0100 Subject: [PATCH] Check that the clock receives a positive adjustment every 10 seconds. If not, it probably has lost sync and will be resynced. --- nqptp-clock-sources.h | 7 +---- nqptp-message-handlers.c | 57 ++++++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/nqptp-clock-sources.h b/nqptp-clock-sources.h index 5f5f40f..6744476 100644 --- a/nqptp-clock-sources.h +++ b/nqptp-clock-sources.h @@ -45,13 +45,8 @@ typedef struct { uint64_t local_time; // the local time when the offset was calculated uint64_t local_to_source_time_offset; // add this to the local time to get source time uint32_t flags; - // uint16_t sequence_number; uint16_t in_use; - // uint64_t mm_count; // mickey mouse averaging - // uint64_t mm_average; // the mickey mouse average - // enum stage current_stage; - // uint64_t t1, t2, t3, previous_offset, previous_estimated_offset, previous_offset_time; - uint64_t previous_offset, previous_offset_time; + uint64_t previous_offset, previous_offset_time, last_sync_time; // for garbage collection uint64_t time_of_last_use; // will be taken out of use if not used for a while and not in the // timing peer group diff --git a/nqptp-message-handlers.c b/nqptp-message-handlers.c index 261f36a..db68924 100644 --- a/nqptp-message-handlers.c +++ b/nqptp-message-handlers.c @@ -236,29 +236,46 @@ void handle_follow_up(char *buf, __attribute__((unused)) ssize_t recv_len, uint64_t offset = preciseOriginTimestamp - reception_time; int64_t jitter = 0; - if (clock_private_info->previous_offset != 0) { - - // do acceptance checking - // if the new offset is greater, by any amount, than the old offset - // accept it - // if it is less than the new offset by up to what a reasonable drift divergence would allow - // accept it - // otherwise, reject it - // drift divergence of 1000 ppm (which is huge) would give 125 us per 125 ms. + if (clock_private_info->previous_offset == 0) { + clock_private_info->last_sync_time = reception_time; + } else { - jitter = offset - clock_private_info->previous_offset; + int64_t time_since_last_sync = reception_time - clock_private_info->last_sync_time; + int64_t sync_timeout = 10000000000; // nanoseconds + debug(2,"Sync interval: %f seconds.", 0.000000001 * time_since_last_sync); + if (time_since_last_sync < sync_timeout) { + // do acceptance checking + // if the new offset is greater, by any amount, than the old offset + // accept it + // if it is less than the new offset by up to what a reasonable drift divergence would allow + // accept it + // otherwise, reject it + // drift divergence of 1000 ppm (which is huge) would give 125 us per 125 ms. - uint64_t jitter_timing_interval = reception_time - clock_private_info->previous_offset_time; - long double jitterppm = 0.0; - if (jitter_timing_interval != 0) { - jitterppm = (0.001 * (jitter * 1000000000)) / jitter_timing_interval; - debug(2, "jitter: %" PRId64 " in: %" PRId64 " ns, %+f ppm ", jitter, jitter_timing_interval, - jitterppm); - } + jitter = offset - clock_private_info->previous_offset; - if (jitterppm <= -1000) { - jitter = -125 * 100; // i.e. 100 parts per million - offset = clock_private_info->previous_offset + jitter; + uint64_t jitter_timing_interval = reception_time - clock_private_info->previous_offset_time; + long double jitterppm = 0.0; + if (jitter_timing_interval != 0) { + jitterppm = (0.001 * (jitter * 1000000000)) / jitter_timing_interval; + debug(2, "jitter: %" PRId64 " in: %" PRId64 " ns, %+f ppm ", jitter, jitter_timing_interval, + jitterppm); + } + if (jitterppm >= -1000) { + // we take a positive or small negative jitter as a sync event + // as we have a new figure for the difference between the local clock and the + // remote clock which is almost the same or greater than our previous estimate + clock_private_info->last_sync_time = reception_time; + } else { + // let our previous estimate drop by some parts-per-million + //jitter = (-100 * jitter_timing_interval) / 1000000; + jitter = -10 * 1000; // this is nanoseconds in, supposedly, 125 milliseconds. 12.5 us / 125 ms is 100 ppm. + offset = clock_private_info->previous_offset + jitter; + } + } else { + debug(1,"NQPTP lost sync with clock %" PRIx64 " at %s. Resynchronising.", clock_private_info->clock_id, clock_private_info->ip); + // leave the offset as it was coming in and take it as a sync time + clock_private_info->last_sync_time = reception_time; } } -- 2.47.2