From 08ac882ce0f24bdfb111dc8f799d5c04d28ebf3e Mon Sep 17 00:00:00 2001 From: henning <> Date: Sun, 18 Jul 2004 12:59:41 +0000 Subject: [PATCH] query interval scaling, episode II 1) base the interval calculation on the offset from the last reply, not from the last peer update. Allows us to send more queries again faster when the local clock diverges too much 2) every time we form a peer update (for which we need 8 replies) check wether we have a ready peer update for all peers that are currently trusted, and if so, calculate the total offset and call adjtime(). that means that adjtime is no longer called in fixed intervals but whenever we have enough data to reliably calculate the local clock offset. In practice, that means we call adjtime() less often, but with probably better data. 3) invalidate peer updates after beeing used. no point in re-using them - this resulted in calling adjtime() multiple times with the same offset, which doesn't make sense tested by many --- src/usr.sbin/ntpd/client.c | 26 +++++++++++--------------- src/usr.sbin/ntpd/ntp.c | 34 +++++++++++++--------------------- src/usr.sbin/ntpd/ntpd.h | 7 +++---- 3 files changed, 27 insertions(+), 40 deletions(-) diff --git a/src/usr.sbin/ntpd/client.c b/src/usr.sbin/ntpd/client.c index 3c5c9fc8..4ce0f19a 100644 --- a/src/usr.sbin/ntpd/client.c +++ b/src/usr.sbin/ntpd/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.25 2004/07/14 20:16:31 henning Exp $ */ +/* $OpenBSD: client.c,v 1.26 2004/07/18 12:59:41 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -182,19 +182,19 @@ client_dispatch(struct ntp_peer *p) else if (p->trustlevel < TRUSTLEVEL_AGRESSIVE) interval = INTERVAL_QUERY_AGRESSIVE; else { - if (p->update.offset < 0) - abs_offset = p->update.offset * -1; + if (p->reply[p->shift].offset < 0) + abs_offset = -p->reply[p->shift].offset; else - abs_offset = p->update.offset; + abs_offset = p->reply[p->shift].offset; - if (!p->update.good) - interval = INTERVAL_QUERY_NORMAL; - else if (abs_offset > QSCALE_OFF_MAX) + if (abs_offset > QSCALE_OFF_MAX) interval = INTERVAL_QUERY_NORMAL; else if (abs_offset < QSCALE_OFF_MIN) - interval = INTERVAL_QUERY_NORMAL * (1 / QSCALE_OFF_MIN); + interval = INTERVAL_QUERY_NORMAL * + (QSCALE_OFF_MAX / QSCALE_OFF_MIN); else - interval = INTERVAL_QUERY_NORMAL * (1 / abs_offset); + interval = INTERVAL_QUERY_NORMAL * + (QSCALE_OFF_MAX / abs_offset); } p->next = time(NULL) + interval; @@ -240,22 +240,18 @@ client_update(struct ntp_peer *p) best = i; } - for (; i < OFFSET_ARRAY_SIZE; i++) { - if (p->reply[i].good && - p->reply[i].rcvd + REPLY_MAXAGE < time(NULL)) - p->reply[i].good = 0; - + for (; i < OFFSET_ARRAY_SIZE; i++) if (p->reply[i].good) { good++; if (p->reply[i].delay < p->reply[best].delay) best = i; } - } if (good < 8) return (-1); memcpy(&p->update, &p->reply[best], sizeof(p->update)); + ntp_adjtime(); for (i = 0; i < OFFSET_ARRAY_SIZE; i++) if (p->reply[i].rcvd <= p->reply[best].rcvd) diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index efeddd56..5326c28f 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.24 2004/07/14 20:16:31 henning Exp $ */ +/* $OpenBSD: ntp.c,v 1.25 2004/07/18 12:59:41 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -34,10 +34,10 @@ volatile sig_atomic_t ntp_quit = 0; struct imsgbuf ibuf_main; struct l_fixedpt ref_ts; +struct ntpd_conf *conf; void ntp_sighdlr(int); int ntp_dispatch_imsg(void); -void ntp_adjtime(struct ntpd_conf *); void ntp_sighdlr(int sig) @@ -51,7 +51,7 @@ ntp_sighdlr(int sig) } pid_t -ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) +ntp_main(int pipe_prnt[2], struct ntpd_conf *nconf) { int nfds, i, j, idx_peers, timeout; u_int pfd_elms = 0, idx2peer_elms = 0; @@ -63,7 +63,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) struct listen_addr *la; struct ntp_peer *p; struct ntp_peer **idx2peer = NULL; - time_t nextaction, next_adjtime; + time_t nextaction; void *newp; switch (pid = fork()) { @@ -88,6 +88,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) setproctitle("ntp engine"); + conf = nconf; setup_listeners(se, conf, &listener_cnt); if (setgroups(1, &pw->pw_gid) || @@ -118,8 +119,6 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) TAILQ_FOREACH(p, &conf->ntp_peers, entry) peer_cnt++; - next_adjtime = time(NULL) + INTERVAL_ADJTIME; - while (ntp_quit == 0) { if (peer_cnt > idx2peer_elms || peer_cnt + IDX2PEER_RESERVE < idx2peer_elms) { @@ -152,7 +151,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) bzero(pfd, sizeof(struct pollfd) * pfd_elms); bzero(idx2peer, sizeof(void *) * idx2peer_elms); - nextaction = next_adjtime; + nextaction = time(NULL) + 3600; pfd[PFD_PIPE_MAIN].fd = ibuf_main.fd; pfd[PFD_PIPE_MAIN].events = POLLIN; @@ -193,11 +192,6 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) } } - if (next_adjtime <= time(NULL)) { - next_adjtime = time(NULL) + INTERVAL_ADJTIME; - ntp_adjtime(conf); - } - if (ibuf_main.w.queued > 0) pfd[PFD_PIPE_MAIN].events |= POLLOUT; @@ -277,24 +271,19 @@ ntp_dispatch_imsg(void) } void -ntp_adjtime(struct ntpd_conf *conf) +ntp_adjtime(void) { struct ntp_peer *p; double offset_median = 0; int offset_cnt = 0; TAILQ_FOREACH(p, &conf->ntp_peers, entry) { - if (!p->update.good) - continue; - - if (p->update.rcvd + REPLY_MAXAGE < time(NULL)) { - p->update.good = 0; - continue; - } - if (p->trustlevel < TRUSTLEVEL_BADPEER) continue; + if (!p->update.good) + return; + offset_median += p->update.offset; offset_cnt++; } @@ -307,4 +296,7 @@ ntp_adjtime(struct ntpd_conf *conf) conf->status.reftime = gettime(); conf->status.leap = LI_NOWARNING; /* XXX */ } + + TAILQ_FOREACH(p, &conf->ntp_peers, entry) + p->update.good = 0; } diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index 0508fdbf..7b05aa07 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.26 2004/07/14 20:16:31 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.27 2004/07/18 12:59:41 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -37,7 +37,6 @@ #define NTPD_OPT_VERBOSE 0x0001 #define NTPD_OPT_VERBOSE2 0x0002 -#define INTERVAL_ADJTIME 240 /* call adjtime every n secs */ #define INTERVAL_QUERY_NORMAL 30 /* sync to peers every n secs */ #define INTERVAL_QUERY_PATHETIC 60 #define INTERVAL_QUERY_AGRESSIVE 5 @@ -47,10 +46,9 @@ #define TRUSTLEVEL_AGRESSIVE 8 #define QSCALE_OFF_MIN 0.05 -#define QSCALE_OFF_MAX 1 +#define QSCALE_OFF_MAX 0.50 #define QUERYTIME_MAX 15 /* single query might take n secs max */ -#define REPLY_MAXAGE 720 #define OFFSET_ARRAY_SIZE 8 enum client_state { @@ -195,6 +193,7 @@ void imsg_free(struct imsg *); /* ntp.c */ pid_t ntp_main(int[2], struct ntpd_conf *); +void ntp_adjtime(void); /* parse.y */ int parse_config(char *, struct ntpd_conf *);