Browse Source

Use proper algorithm for median computation; use fabs() for computing

an absolute value and fix poll loop to first generate messages and
then compute poll flags the write cases. This makes the timeout
workaround for constraints unneeded.  ok reyk@ tb@
OPENBSD_6_6
otto 5 years ago
parent
commit
11942d45dc
2 changed files with 21 additions and 22 deletions
  1. +15
    -13
      src/usr.sbin/ntpd/constraint.c
  2. +6
    -9
      src/usr.sbin/ntpd/ntp.c

+ 15
- 13
src/usr.sbin/ntpd/constraint.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: constraint.c,v 1.43 2019/05/28 06:49:46 otto Exp $ */
/* $OpenBSD: constraint.c,v 1.44 2019/05/30 13:42:19 otto Exp $ */
/*
* Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
@ -41,6 +41,7 @@
#include <ctype.h>
#include <tls.h>
#include <pwd.h>
#include <math.h>
#include "ntpd.h"
@ -773,7 +774,7 @@ constraint_update(void)
{
struct constraint *cstr;
int cnt, i;
time_t *sum;
time_t *values;
time_t now;
now = getmonotime();
@ -784,29 +785,31 @@ constraint_update(void)
continue;
cnt++;
}
if (cnt == 0)
return;
if ((sum = calloc(cnt, sizeof(time_t))) == NULL)
if ((values = calloc(cnt, sizeof(time_t))) == NULL)
fatal("calloc");
i = 0;
TAILQ_FOREACH(cstr, &conf->constraints, entry) {
if (cstr->state != STATE_REPLY_RECEIVED)
continue;
sum[i++] = cstr->constraint + (now - cstr->last);
values[i++] = cstr->constraint + (now - cstr->last);
}
qsort(sum, cnt, sizeof(time_t), constraint_cmp);
qsort(values, cnt, sizeof(time_t), constraint_cmp);
/* calculate median */
i = cnt / 2;
if (cnt % 2 == 0)
if (sum[i - 1] < sum[i])
i -= 1;
conf->constraint_median = (values[i - 1] + values[i]) / 2;
else
conf->constraint_median = values[i];
conf->constraint_last = now;
conf->constraint_median = sum[i];
free(sum);
free(values);
}
void
@ -826,7 +829,7 @@ int
constraint_check(double val)
{
struct timeval tv;
double constraint;
double diff;
time_t now;
if (conf->constraint_median == 0)
@ -836,10 +839,9 @@ constraint_check(double val)
now = getmonotime();
tv.tv_sec = conf->constraint_median + (now - conf->constraint_last);
tv.tv_usec = 0;
constraint = gettime_from_timeval(&tv);
diff = fabs(val - gettime_from_timeval(&tv));
if (((val - constraint) > CONSTRAINT_MARGIN) ||
((constraint - val) > CONSTRAINT_MARGIN)) {
if (diff > CONSTRAINT_MARGIN) {
/* XXX get new constraint if too many errors happened */
if (conf->constraint_errors++ >
(CONSTRAINT_ERROR_MARGIN * peer_cnt)) {


+ 6
- 9
src/usr.sbin/ntpd/ntp.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: ntp.c,v 1.151 2019/05/29 18:48:33 otto Exp $ */
/* $OpenBSD: ntp.c,v 1.152 2019/05/30 13:42:19 otto Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@ -316,6 +316,11 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv)
(peer_cnt == 0 && sensors_cnt == 0)))
priv_settime(0); /* no good peers, don't wait */
TAILQ_FOREACH(cstr, &conf->constraints, entry) {
if (constraint_query(cstr) == -1)
continue;
}
if (ibuf_main->w.queued > 0)
pfd[PFD_PIPE_MAIN].events |= POLLOUT;
if (ibuf_dns->w.queued > 0)
@ -330,15 +335,7 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv)
}
ctls = i;
TAILQ_FOREACH(cstr, &conf->constraints, entry) {
if (constraint_query(cstr) == -1)
continue;
}
now = getmonotime();
if (constraint_cnt)
nextaction = now + 1;
timeout = nextaction - now;
if (timeout < 0)
timeout = 0;


Loading…
Cancel
Save