[Git][NTPsec/ntpsec][master] 3 commits: Create int ntp_adjtime_ns(). We will use this to hide the difference...
Eric S. Raymond
gitlab at mg.gitlab.com
Tue Oct 11 21:41:44 UTC 2016
Eric S. Raymond pushed to branch master at NTPsec / ntpsec
Commits:
429c2bea by Eric S. Raymond at 2016-10-11T17:41:31-04:00
Create int ntp_adjtime_ns(). We will use this to hide the difference...
...between systems with nanosecond precision and those without.
The assumptions about which fields need to be scaled are derived from
the Linux adjtimex(2) manual page and the legacy ntptime.c code.
- - - - -
8f3d5ad9 by Eric S. Raymond at 2016-10-11T17:41:31-04:00
Add explanatory comment.
- - - - -
9b8f2adc by Eric S. Raymond at 2016-10-11T17:41:31-04:00
Refactor ntptime to use ntp_adjtime_ns().
- - - - -
3 changed files:
- include/ntp_syscall.h
- libntp/machines.c
- ntptime/ntptime.c
Changes:
=====================================
include/ntp_syscall.h
=====================================
--- a/include/ntp_syscall.h
+++ b/include/ntp_syscall.h
@@ -39,7 +39,7 @@ int ntp_gettime(struct ntptimeval *);
#define ntp_error_in_seconds(n) ((n)/1.0e6)
# endif
-
+extern int ntp_adjtime_ns(struct timex *);
/* MUSL port shim */
#if !defined(HAVE_NTP_ADJTIME) && defined(HAVE_ADJTIMEX)
=====================================
libntp/machines.c
=====================================
--- a/libntp/machines.c
+++ b/libntp/machines.c
@@ -59,6 +59,31 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp)
}
#endif /* HAVE_CLOCK_GETTIME */
+/*
+ * ntp_adjtime at nanosecond precision. Hiding the units difference here
+ * helps prevent loss-of-precision bugs. We deliberately don't merge
+ * STA_NANO into the status flags if it's absent, however, this way
+ * callers can tell what accuracy they're actually getting.
+ *
+ * Problems: the Linux manual page for adjtimex(2) says the precision member
+ * is microseconds and doesn't mention STA_NANO, but the legacy ntptime code
+ * has a scaling expression in it that implies nanoseconds if that flash bit
+ * is on.
+ */
+int ntp_adjtime_ns(struct timex *ntx)
+{
+ int errval = ntp_adjtime(ntx);
+#ifdef STA_NANO
+ if (errval == 0 && !(ntx->status & STA_NANO)) {
+ ntx->time.tv_usec *= 1000;
+ ntx->offset *= 1000;
+ //ntx->precision *= 1000;
+ ntx->jitter *= 1000;
+ }
+#endif
+ return errval;
+}
+
#if !defined(HAVE_NTP_GETTIME) && defined(HAVE_NTP_ADJTIME)
int ntp_gettime(struct ntptimeval *ntv)
{
=====================================
ntptime/ntptime.c
=====================================
--- a/ntptime/ntptime.c
+++ b/ntptime/ntptime.c
@@ -46,6 +46,7 @@
TVUTOTSF((tv)->tv_usec, (ts)->l_uf); \
} while (false)
+#define NS_PER_MS_FLOAT 1000.0
/* MUSL port shim */
#ifndef HAVE_NTP_GETTIME
@@ -367,7 +368,7 @@ main(
printf(json ? jfmt6 : ofmt6);
#endif /* NTP_API */
}
- status = ntp_adjtime(&ntx);
+ status = ntp_adjtime_ns(&ntx);
if (status < 0) {
perror((errno == EPERM) ?
"Must be root to set kernel values\nntp_adjtime() call fails" :
@@ -399,11 +400,7 @@ main(
printf(json ? jfmt7 : ofmt7, status, timex_state(status));
printf(json ? jfmt8 : ofmt8,
snprintb(sizeof(binbuf), binbuf, ntx.modes, TIMEX_MOD_BITS));
- ftemp = (double)ntx.offset;
-#ifdef STA_NANO
- if (flash & STA_NANO)
- ftemp /= 1000.0;
-#endif
+ ftemp = (double)ntx.offset/NS_PER_MS_FLOAT;
printf(json ? jfmt9 : ofmt9, ftemp);
ftemp = (double)ntx.freq / SCALE_FREQ;
printf(json ? jfmt10 : ofmt10, ftemp, 1 << ntx.shift);
@@ -413,21 +410,20 @@ main(
snprintb(sizeof(binbuf), binbuf,
(u_int)ntx.status, TIMEX_STA_BITS));
ftemp = (double)ntx.tolerance / SCALE_FREQ;
+ /*
+ * Before the introduction of ntp_adjtime_ns() the
+ * ntptime code divided this by 1000 when the STA_NANO
+ * flash bit was on. This doesn't match the Linux
+ * documentation; might have been an error, or
+ * possibly some other systems behave differently.
+ */
gtemp = (double)ntx.precision;
-#ifdef STA_NANO
- if (flash & STA_NANO)
- gtemp /= 1000.0;
-#endif
printf(json ? jfmt13 : ofmt13,
(u_long)ntx.constant, gtemp, ftemp);
if (ntx.shift != 0) {
ftemp = (double)ntx.ppsfreq / SCALE_FREQ;
gtemp = (double)ntx.stabil / SCALE_FREQ;
- htemp = (double)ntx.jitter;
-#ifdef STA_NANO
- if (flash & STA_NANO)
- htemp /= 1000.0;
-#endif
+ htemp = (double)ntx.jitter/NS_PER_MS_FLOAT;
printf(json ? jfmt14 : ofmt14,
ftemp, gtemp, htemp);
printf(json ? jfmt15 : ofmt15,
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/3658a7c773299f969fd1e0ab9abc2315345a9d85...9b8f2adc030cbb93c775b38e331c17489695ecb3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ntpsec.org/pipermail/vc/attachments/20161011/09a63791/attachment.html>
More information about the vc
mailing list