diff --git a/ntp_core.c b/ntp_core.c index 73a877a..4dca216 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -570,7 +570,10 @@ transmit_packet(NTP_Mode my_mode, /* The mode this machine wants to be */ version = NTP_VERSION; } - LCL_ReadCookedTime(&local_transmit, NULL); + /* This is accurate enough and cheaper than calling LCL_ReadCookedTime. + A more accurate time stamp will be taken later in this function. */ + SCH_GetLastEventTime(&local_transmit, NULL, NULL); + REF_GetReferenceParams(&local_transmit, &are_we_synchronised, &leap_status, &our_stratum, diff --git a/ntp_io.c b/ntp_io.c index 3dc4ace..803be91 100644 --- a/ntp_io.c +++ b/ntp_io.c @@ -296,7 +296,7 @@ read_from_socket(void *anything) ReceiveBuffer message; union sockaddr_in46 where_from; unsigned int flags = 0; - struct timeval now; + struct timeval now, now_raw; double now_err; NTP_Remote_Address remote_addr; char cmsgbuf[256]; @@ -306,7 +306,7 @@ read_from_socket(void *anything) assert(initialised); - SCH_GetLastEventTime(&now, &now_err, NULL); + SCH_GetLastEventTime(&now, &now_err, &now_raw); iov.iov_base = message.arbitrary; iov.iov_len = sizeof(message); @@ -376,7 +376,9 @@ read_from_socket(void *anything) struct timeval tv; memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv)); - LCL_CookTime(&tv, &now, &now_err); + + /* This should be more accurate than LCL_CookTime(&now_raw,...) */ + UTI_AddDiffToTimeval(&now, &now_raw, &tv, &now); } #endif } diff --git a/reference.c b/reference.c index 615a707..5368bc5 100644 --- a/reference.c +++ b/reference.c @@ -684,7 +684,7 @@ REF_SetReference(int stratum, double update_interval; double elapsed; double correction_rate; - struct timeval now, raw_now; + struct timeval now, raw_now, ev_now, ev_raw_now; assert(initialised); @@ -713,7 +713,10 @@ REF_SetReference(int stratum, } LCL_ReadRawTime(&raw_now); - LCL_CookTime(&raw_now, &now, NULL); + + /* This is cheaper than calling LCL_CookTime */ + SCH_GetLastEventTime(&ev_now, NULL, &ev_raw_now); + UTI_AddDiffToTimeval(&ev_now, &ev_raw_now, &raw_now, &now); UTI_DiffTimevalsToDouble(&elapsed, &now, ref_time); our_offset = offset + elapsed * frequency; diff --git a/sources.c b/sources.c index 2c8aa1e..6bbf319 100644 --- a/sources.c +++ b/sources.c @@ -44,6 +44,7 @@ #include "reports.h" #include "nameserv.h" #include "mkdirpp.h" +#include "sched.h" /* ================================================== */ /* Flag indicating that we are initialised */ @@ -448,7 +449,8 @@ SRC_SelectSource(uint32_t match_refid) return; } - LCL_ReadCookedTime(&now, NULL); + /* This is accurate enough and cheaper than calling LCL_ReadCookedTime */ + SCH_GetLastEventTime(&now, NULL, NULL); /* Step 1 - build intervals about each source */ n_endpoints = 0; diff --git a/util.c b/util.c index aedabeb..cb8c2a1 100644 --- a/util.c +++ b/util.c @@ -188,6 +188,18 @@ UTI_AverageDiffTimevals (struct timeval *earlier, /* ================================================== */ +void +UTI_AddDiffToTimeval(struct timeval *a, struct timeval *b, + struct timeval *c, struct timeval *result) +{ + double diff; + + UTI_DiffTimevalsToDouble(&diff, a, b); + UTI_AddDoubleToTimeval(c, diff, result); +} + +/* ================================================== */ + #define POOL_ENTRIES 16 #define BUFFER_LENGTH 64 static char buffer_pool[POOL_ENTRIES][BUFFER_LENGTH]; diff --git a/util.h b/util.h index 02fb6fb..774509c 100644 --- a/util.h +++ b/util.h @@ -63,6 +63,9 @@ extern void UTI_AddDoubleToTimeval(struct timeval *start, double increment, stru /* Calculate the average and difference (as a double) of two timevals */ extern void UTI_AverageDiffTimevals(struct timeval *earlier, struct timeval *later, struct timeval *average, double *diff); +/* Calculate result = a - b + c */ +extern void UTI_AddDiffToTimeval(struct timeval *a, struct timeval *b, struct timeval *c, struct timeval *result); + /* Convert a timeval into a temporary string, largely for diagnostic display */ extern char *UTI_TimevalToString(struct timeval *tv);