From 20d898d1829d3fcdbd0d748e514d93f36f95970c Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 10 Feb 2010 15:59:40 +0100 Subject: [PATCH] Prepare for handling offset correction error --- acquire.c | 5 ++--- broadcast.c | 5 ++--- cmdmon.c | 15 +++++-------- local.c | 60 ++++++++++++++++++++++----------------------------- local.h | 10 +++++---- localp.h | 2 +- manual.c | 6 ++---- ntp_core.c | 5 ++--- ntp_io.c | 12 +++-------- refclock.c | 8 +++---- reference.c | 5 ++--- rtc_linux.c | 5 ++--- sched.c | 8 ++++--- sched.h | 2 +- sources.c | 5 ++--- sys_linux.c | 3 ++- sys_netbsd.c | 3 ++- sys_solaris.c | 3 ++- sys_sunos.c | 3 ++- 19 files changed, 73 insertions(+), 92 deletions(-) diff --git a/acquire.c b/acquire.c index 3f702b6..ad2047d 100644 --- a/acquire.c +++ b/acquire.c @@ -248,7 +248,6 @@ probe_source(SourceRecord *src) int version = 3; NTP_Mode my_mode = MODE_CLIENT; struct timeval cooked; - double local_time_err; union sockaddr_in46 his_addr; int sock_fd; socklen_t addrlen; @@ -300,7 +299,7 @@ probe_source(SourceRecord *src) } - LCL_ReadCookedTime(&cooked, &local_time_err); + LCL_ReadCookedTime(&cooked, NULL); UTI_TimevalToInt64(&cooked, &pkt.transmit_ts); if (sendto(sock_fd, (void *) &pkt, NTP_NORMAL_PACKET_SIZE, @@ -449,7 +448,7 @@ read_from_socket(void *anything) his_addr_len = sizeof(his_addr); /* Get timestamp */ - SCH_GetFileReadyTime(&now); + SCH_GetFileReadyTime(&now, NULL); sock_fd = (long)anything; status = recvfrom (sock_fd, (char *)&msg, message_length, flags, diff --git a/broadcast.c b/broadcast.c index 6cd0a48..4005ba6 100644 --- a/broadcast.c +++ b/broadcast.c @@ -78,12 +78,11 @@ timeout_handler(void *arbitrary) unsigned long our_ref_id; struct timeval our_ref_time; double our_root_delay, our_root_dispersion; - double local_time_err; struct timeval local_transmit; version = 3; - LCL_ReadCookedTime(&local_transmit, &local_time_err); + LCL_ReadCookedTime(&local_transmit, NULL); REF_GetReferenceParams(&local_transmit, &are_we_synchronised, &leap_status, &our_stratum, @@ -116,7 +115,7 @@ timeout_handler(void *arbitrary) message.receive_ts.hi = 0UL; message.receive_ts.lo = 0UL; - LCL_ReadCookedTime(&local_transmit, &local_time_err); + LCL_ReadCookedTime(&local_transmit, NULL); UTI_TimevalToInt64(&local_transmit, &message.transmit_ts); NIO_SendNormalPacket(&message, &d->addr); diff --git a/cmdmon.c b/cmdmon.c index 2ccc544..05ebfc5 100644 --- a/cmdmon.c +++ b/cmdmon.c @@ -991,10 +991,9 @@ handle_source_data(CMD_Request *rx_message, CMD_Reply *tx_message) { RPT_SourceReport report; struct timeval now_corr; - double local_clock_err; /* Get data */ - LCL_ReadCookedTime(&now_corr, &local_clock_err); + LCL_ReadCookedTime(&now_corr, NULL); if (SRC_ReportSource(ntohl(rx_message->data.source_data.index), &report, &now_corr)) { switch (SRC_GetType(ntohl(rx_message->data.source_data.index))) { case SRC_NTP: @@ -1394,9 +1393,8 @@ handle_sourcestats(CMD_Request *rx_message, CMD_Reply *tx_message) int status; RPT_SourcestatsReport report; struct timeval now_corr; - double local_clock_err; - LCL_ReadCookedTime(&now_corr, &local_clock_err); + LCL_ReadCookedTime(&now_corr, NULL); status = SRC_ReportSourcestats(ntohl(rx_message->data.sourcestats.index), &report, &now_corr); @@ -1531,9 +1529,8 @@ handle_client_accesses(CMD_Request *rx_message, CMD_Reply *tx_message) IPAddr ip; int i; struct timeval now; - double local_time_error; - LCL_ReadCookedTime(&now, &local_time_error); + LCL_ReadCookedTime(&now, NULL); nc = ntohl(rx_message->data.client_accesses.n_clients); tx_message->status = htons(STT_SUCCESS); @@ -1585,9 +1582,8 @@ handle_client_accesses_by_index(CMD_Request *rx_message, CMD_Reply *tx_message) unsigned long first_index, n_indices, last_index, n_indices_in_table; int i, j; struct timeval now; - double local_time_error; - LCL_ReadCookedTime(&now, &local_time_error); + LCL_ReadCookedTime(&now, NULL); first_index = ntohl(rx_message->data.client_accesses_by_index.first_index); n_indices = ntohl(rx_message->data.client_accesses_by_index.n_indices); @@ -1742,7 +1738,6 @@ read_from_cmd_socket(void *anything) unsigned long rx_attempt; struct timeval now; struct timeval cooked_now; - double local_clock_err; flags = 0; rx_message_length = sizeof(rx_message); @@ -1762,7 +1757,7 @@ read_from_cmd_socket(void *anything) expected_length = PKL_CommandLength(&rx_message); LCL_ReadRawTime(&now); - LCL_ReadCookedTime(&cooked_now, &local_clock_err); + LCL_CookTime(&now, &cooked_now, NULL); tx_message.version = PROTO_VERSION_NUMBER; tx_message.pkt_type = PKT_TYPE_CMD_REPLY; diff --git a/local.c b/local.c index 5d832c3..1be6117 100644 --- a/local.c +++ b/local.c @@ -316,8 +316,6 @@ LCL_ReadRawTime(struct timeval *result) if (!(gettimeofday(result, &tz) >= 0)) { CROAK("Could not get time of day"); } - return; - } /* ================================================== */ @@ -326,30 +324,34 @@ void LCL_ReadCookedTime(struct timeval *result, double *err) { struct timeval raw; - double correction; LCL_ReadRawTime(&raw); - - /* For now, cheat and set the error to zero in all cases. - */ - - *err = 0.0; - - /* Call system specific driver to get correction */ - (*drv_offset_convert)(&raw, &correction); - UTI_AddDoubleToTimeval(&raw, correction, result); - - return; + LCL_CookTime(&raw, result, err); } /* ================================================== */ -double -LCL_GetOffsetCorrection(struct timeval *raw) +void +LCL_CookTime(struct timeval *raw, struct timeval *cooked, double *err) { double correction; - (*drv_offset_convert)(raw, &correction); - return correction; + + LCL_GetOffsetCorrection(raw, &correction, err); + UTI_AddDoubleToTimeval(raw, correction, cooked); +} + +/* ================================================== */ + +void +LCL_GetOffsetCorrection(struct timeval *raw, double *correction, double *err) +{ + double e; + + /* Call system specific driver to get correction */ + (*drv_offset_convert)(raw, correction, &e); + + if (err) + *err = e; } /* ================================================== */ @@ -370,7 +372,6 @@ LCL_SetAbsoluteFrequency(double afreq_ppm) { ChangeListEntry *ptr; struct timeval raw, cooked; - double correction; double dfreq; /* Call the system-specific driver for setting the frequency */ @@ -380,8 +381,7 @@ LCL_SetAbsoluteFrequency(double afreq_ppm) dfreq = 1.0e-6 * (afreq_ppm - current_freq_ppm) / (1.0 - 1.0e-6 * current_freq_ppm); LCL_ReadRawTime(&raw); - (drv_offset_convert)(&raw, &correction); - UTI_AddDoubleToTimeval(&raw, correction, &cooked); + LCL_CookTime(&raw, &cooked, NULL); /* Dispatch to all handlers */ for (ptr = change_list.next; ptr != &change_list; ptr = ptr->next) { @@ -399,7 +399,6 @@ LCL_AccumulateDeltaFrequency(double dfreq) { ChangeListEntry *ptr; struct timeval raw, cooked; - double correction; /* Work out new absolute frequency. Note that absolute frequencies are handled in units of ppm, whereas the 'dfreq' argument is in @@ -412,8 +411,7 @@ LCL_AccumulateDeltaFrequency(double dfreq) (*drv_set_freq)(current_freq_ppm); LCL_ReadRawTime(&raw); - (drv_offset_convert)(&raw, &correction); - UTI_AddDoubleToTimeval(&raw, correction, &cooked); + LCL_CookTime(&raw, &cooked, NULL); /* Dispatch to all handlers */ for (ptr = change_list.next; ptr != &change_list; ptr = ptr->next) { @@ -429,14 +427,12 @@ LCL_AccumulateOffset(double offset) { ChangeListEntry *ptr; struct timeval raw, cooked; - double correction; /* In this case, the cooked time to be passed to the notify clients has to be the cooked time BEFORE the change was made */ LCL_ReadRawTime(&raw); - (drv_offset_convert)(&raw, &correction); - UTI_AddDoubleToTimeval(&raw, correction, &cooked); + LCL_CookTime(&raw, &cooked, NULL); (*drv_accrue_offset)(offset); @@ -454,14 +450,12 @@ LCL_ApplyStepOffset(double offset) { ChangeListEntry *ptr; struct timeval raw, cooked; - double correction; /* In this case, the cooked time to be passed to the notify clients has to be the cooked time BEFORE the change was made */ LCL_ReadRawTime(&raw); - (drv_offset_convert)(&raw, &correction); - UTI_AddDoubleToTimeval(&raw, correction, &cooked); + LCL_CookTime(&raw, &cooked, NULL); (*drv_apply_step_offset)(offset); @@ -479,14 +473,12 @@ LCL_AccumulateFrequencyAndOffset(double dfreq, double doffset) { ChangeListEntry *ptr; struct timeval raw, cooked; - double correction; double old_freq_ppm; LCL_ReadRawTime(&raw); - (drv_offset_convert)(&raw, &correction); /* Due to modifying the offset, this has to be the cooked time prior to the change we are about to make */ - UTI_AddDoubleToTimeval(&raw, correction, &cooked); + LCL_CookTime(&raw, &cooked, NULL); old_freq_ppm = current_freq_ppm; @@ -563,7 +555,7 @@ LCL_MakeStep(double threshold) double correction; LCL_ReadRawTime(&raw); - correction = LCL_GetOffsetCorrection(&raw); + LCL_GetOffsetCorrection(&raw, &correction, NULL); if (fabs(correction) <= threshold) return 0; diff --git a/local.h b/local.h index 98899a9..bef1a3e 100644 --- a/local.h +++ b/local.h @@ -50,11 +50,13 @@ extern void LCL_ReadRawTime(struct timeval *); extern void LCL_ReadCookedTime(struct timeval *t, double *err); -/* Read the current offset between the system clock and true time - (i.e. 'cooked' - 'raw') (in seconds). Only intended for use in - status reporting, really. */ +/* Convert raw time to cooked. */ +extern void LCL_CookTime(struct timeval *raw, struct timeval *cooked, double *err); -extern double LCL_GetOffsetCorrection(struct timeval *raw); +/* Read the current offset between the system clock and true time + (i.e. 'cooked' - 'raw') (in seconds). */ + +extern void LCL_GetOffsetCorrection(struct timeval *raw, double *correction, double *err); /* Type of routines that may be invoked as callbacks when there is a change to the frequency or offset. diff --git a/localp.h b/localp.h index 47d7eb1..6f78d1d 100644 --- a/localp.h +++ b/localp.h @@ -54,7 +54,7 @@ typedef void (*lcl_ApplyStepOffsetDriver)(double offset); /* System driver to convert a raw time to an adjusted (cooked) time. The number of seconds returned in 'corr' have to be added to the raw time to get the corrected time */ -typedef void (*lcl_OffsetCorrectionDriver)(struct timeval *raw, double *corr); +typedef void (*lcl_OffsetCorrectionDriver)(struct timeval *raw, double *corr, double *err); /* System driver to schedule leap second */ typedef void (*lcl_SetLeapDriver)(int leap); diff --git a/manual.c b/manual.c index 34b73ae..f3e98e1 100644 --- a/manual.c +++ b/manual.c @@ -188,14 +188,13 @@ int MNL_AcceptTimestamp(struct timeval *ts, long *offset_cs, double *dfreq_ppm, double *new_afreq_ppm) { struct timeval now; - double local_clock_err; double offset; int i; if (enabled) { /* Check whether timestamp is within margin of old one */ - LCL_ReadCookedTime(&now, &local_clock_err); + LCL_ReadCookedTime(&now, NULL); UTI_DiffTimevalsToDouble(&offset, &now, ts); @@ -303,7 +302,6 @@ MNL_DeleteSample(int index) { int i; struct timeval now; - double local_clock_err; if ((index < 0) || (index >= n_samples)) { return 0; @@ -319,7 +317,7 @@ MNL_DeleteSample(int index) /* Now re-estimate. NULLs because we don't want the parameters back in this case. */ - LCL_ReadCookedTime(&now, &local_clock_err); + LCL_ReadCookedTime(&now, NULL); estimate_and_set_system(&now, 0, 0.0, NULL, NULL, NULL); return 1; diff --git a/ntp_core.c b/ntp_core.c index 84a47e3..7b2d717 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -523,7 +523,6 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ NTP_Packet message; int version; int leap; - double local_time_err; struct timeval local_transmit; /* Parameters read from reference module */ @@ -535,7 +534,7 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ version = 3; - LCL_ReadCookedTime(&local_transmit, &local_time_err); + LCL_ReadCookedTime(&local_transmit, NULL); REF_GetReferenceParams(&local_transmit, &are_we_synchronised, &leap_status, &our_stratum, @@ -582,7 +581,7 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ /* Transmit - this our local time right now! Also, we might need to store this for our own use later, next time we receive a message from the source we're sending to now. */ - LCL_ReadCookedTime(&local_transmit, &local_time_err); + LCL_ReadCookedTime(&local_transmit, NULL); /* Authenticate */ if (do_auth) { diff --git a/ntp_io.c b/ntp_io.c index 48802e4..1841b9c 100644 --- a/ntp_io.c +++ b/ntp_io.c @@ -281,6 +281,7 @@ read_from_socket(void *anything) union sockaddr_in46 where_from; unsigned int flags = 0; struct timeval now; + double now_err; NTP_Remote_Address remote_addr; char cmsgbuf[256]; struct msghdr msg; @@ -289,7 +290,7 @@ read_from_socket(void *anything) assert(initialised); - SCH_GetFileReadyTime(&now); + SCH_GetFileReadyTime(&now, &now_err); iov.iov_base = message.arbitrary; iov.iov_len = sizeof(message); @@ -346,16 +347,9 @@ read_from_socket(void *anything) #ifdef SO_TIMESTAMP if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SO_TIMESTAMP) { struct timeval tv; - double correction; memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv)); - correction = LCL_GetOffsetCorrection(&tv); - UTI_AddDoubleToTimeval(&tv, correction, &tv); -#if 0 - UTI_DiffTimevalsToDouble(&correction, &now, &tv); - LOG(LOGS_INFO, LOGF_NtpIO, "timestamp diff: %f", correction); -#endif - now = tv; + LCL_CookTime(&tv, &now, &now_err); } #endif } diff --git a/refclock.c b/refclock.c index a419800..f4e42a1 100644 --- a/refclock.c +++ b/refclock.c @@ -317,10 +317,10 @@ RCL_GetDriverOption(RCL_Instance instance, char *name) int RCL_AddSample(RCL_Instance instance, struct timeval *sample_time, double offset, NTP_Leap leap_status) { - double correction; + double correction, err; struct timeval cooked_time; - correction = LCL_GetOffsetCorrection(sample_time); + LCL_GetOffsetCorrection(sample_time, &correction, &err); UTI_AddDoubleToTimeval(sample_time, correction, &cooked_time); if (!valid_sample_time(instance, sample_time)) @@ -342,7 +342,7 @@ RCL_AddSample(RCL_Instance instance, struct timeval *sample_time, double offset, int RCL_AddPulse(RCL_Instance instance, struct timeval *pulse_time, double second) { - double correction, offset; + double correction, err, offset; struct timeval cooked_time; int rate; @@ -352,7 +352,7 @@ RCL_AddPulse(RCL_Instance instance, struct timeval *pulse_time, double second) NTP_Leap leap; unsigned long ref_id; - correction = LCL_GetOffsetCorrection(pulse_time); + LCL_GetOffsetCorrection(pulse_time, &correction, &err); UTI_AddDoubleToTimeval(pulse_time, correction, &cooked_time); if (!valid_sample_time(instance, pulse_time)) diff --git a/reference.c b/reference.c index af8baa1..3e85e35 100644 --- a/reference.c +++ b/reference.c @@ -573,11 +573,10 @@ REF_SetUnsynchronised(void) { /* Variables required for logging to statistics log */ struct timeval now; - double local_clock_err; assert(initialised); - LCL_ReadCookedTime(&now, &local_clock_err); + LCL_ReadCookedTime(&now, NULL); write_log(&now, "0.0.0.0", @@ -725,7 +724,7 @@ REF_GetTrackingReport(RPT_TrackingReport *rep) double correction; LCL_ReadRawTime(&now_raw); - correction = LCL_GetOffsetCorrection(&now_raw); + LCL_GetOffsetCorrection(&now_raw, &correction, NULL); UTI_AddDoubleToTimeval(&now_raw, correction, &now_cooked); if (are_we_synchronised) { diff --git a/rtc_linux.c b/rtc_linux.c index 582a8b5..786e7f8 100644 --- a/rtc_linux.c +++ b/rtc_linux.c @@ -911,7 +911,7 @@ read_from_device(void *any) /* Read RTC time, sandwiched between two polls of the system clock so we can bound any error. */ - SCH_GetFileReadyTime(&sys_time); + SCH_GetFileReadyTime(&sys_time, NULL); status = ioctl(fd, RTC_RD_TIME, &rtc_raw); if (status < 0) { @@ -1139,7 +1139,6 @@ int RTC_Linux_Trim(void) { struct timeval now; - double local_clock_err; /* Remember the slope coefficient - we won't be able to determine a @@ -1158,7 +1157,7 @@ RTC_Linux_Trim(void) want |E| <= 0.5, which implies R <= S <= R+1, i.e. R is just the rounded down part of S, i.e. the seconds part. */ - LCL_ReadCookedTime(&now, &local_clock_err); + LCL_ReadCookedTime(&now, NULL); set_rtc(now.tv_sec); diff --git a/sched.c b/sched.c index 6412f0a..c2d44b9 100644 --- a/sched.c +++ b/sched.c @@ -73,6 +73,7 @@ static FileHandlerEntry file_handlers[FD_SET_SIZE]; /* Last timestamp when a file descriptor became readable */ static struct timeval last_fdready; +static double last_fdready_err; /* ================================================== */ @@ -229,9 +230,11 @@ SCH_RemoveInputFileHandler(int fd) /* ================================================== */ void -SCH_GetFileReadyTime(struct timeval *tv) +SCH_GetFileReadyTime(struct timeval *tv, double *err) { *tv = last_fdready; + if (err) + *err = last_fdready_err; } /* ================================================== */ @@ -525,7 +528,6 @@ SCH_MainLoop(void) int status; struct timeval tv, *ptv; struct timeval now; - double err; if (!initialised) { CROAK("Should be initialised"); @@ -570,7 +572,7 @@ SCH_MainLoop(void) } else if (status > 0) { /* A file descriptor is ready to read */ - LCL_ReadCookedTime(&last_fdready, &err); + LCL_ReadCookedTime(&last_fdready, &last_fdready_err); dispatch_filehandlers(status, &rd); } else { diff --git a/sched.h b/sched.h index 0aea7ff..143c4c3 100644 --- a/sched.h +++ b/sched.h @@ -62,7 +62,7 @@ extern void SCH_RemoveInputFileHandler(int fd); /* Get the time (cooked) when file descriptor became ready, intended for use in file handlers */ -extern void SCH_GetFileReadyTime(struct timeval *tv); +extern void SCH_GetFileReadyTime(struct timeval *tv, double *err); /* This queues a timeout to elapse at a given (raw) local time */ extern SCH_TimeoutID SCH_AddTimeout(struct timeval *tv, SCH_TimeoutHandler, SCH_ArbitraryArgument); diff --git a/sources.c b/sources.c index 03b6fb6..f27583b 100644 --- a/sources.c +++ b/sources.c @@ -385,7 +385,6 @@ SRC_SelectSource(unsigned long match_addr) { int i, j, index; struct timeval now; - double local_clock_err; int src_select_ok; double src_offset, src_offset_sd, src_frequency, src_skew; double src_accrued_dispersion; @@ -412,7 +411,7 @@ SRC_SelectSource(unsigned long match_addr) return; } - LCL_ReadCookedTime(&now, &local_clock_err); + LCL_ReadCookedTime(&now, NULL); /* Step 1 - build intervals about each source */ n_endpoints = 0; @@ -704,7 +703,7 @@ SRC_SelectSource(unsigned long match_addr) /* Now just use the statistics of the selected source for trimming the local clock */ - LCL_ReadCookedTime(&now, &local_clock_err); + LCL_ReadCookedTime(&now, NULL); SST_GetTrackingData(sources[selected_source_index]->stats, &now, &src_offset, &src_offset_sd, diff --git a/sys_linux.c b/sys_linux.c index f46b055..6c6a64a 100644 --- a/sys_linux.c +++ b/sys_linux.c @@ -538,7 +538,7 @@ read_frequency(void) static void get_offset_correction(struct timeval *raw, - double *corr) + double *corr, double *err) { /* Correction is given by these things : @@ -602,6 +602,7 @@ again: } *corr = - (offset_register + fast_slew_remaining) + adjtime_left; + *err = 0.0; return; } diff --git a/sys_netbsd.c b/sys_netbsd.c index 4641ee4..4b921e7 100644 --- a/sys_netbsd.c +++ b/sys_netbsd.c @@ -264,11 +264,12 @@ read_frequency(void) static void get_offset_correction(struct timeval *raw, - double *corr) + double *corr, double *err) { stop_adjust(); *corr = -offset_register; start_adjust(); + *err = 0.0; } /* ================================================== */ diff --git a/sys_solaris.c b/sys_solaris.c index 6c65705..40a863d 100644 --- a/sys_solaris.c +++ b/sys_solaris.c @@ -293,11 +293,12 @@ read_frequency(void) static void get_offset_correction(struct timeval *raw, - double *corr) + double *corr, double *err) { stop_adjust(); *corr = -offset_register; start_adjust(); + *err = 0.0; return; } diff --git a/sys_sunos.c b/sys_sunos.c index 13bb262..6a98f51 100644 --- a/sys_sunos.c +++ b/sys_sunos.c @@ -280,11 +280,12 @@ read_frequency(void) static void get_offset_correction(struct timeval *raw, - double *corr) + double *corr, double *err) { stop_adjust(); *corr = -offset_register; start_adjust(); + *err = 0.0; return; }