[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openbsd-tech
Subject:    Re: use monotime for internal intervals and stuff in ntpd
From:       Peter Hessler <phessler () theapt ! org>
Date:       2006-10-25 21:37:53
Message-ID: 20061025213752.GA5791 () gir ! theapt ! org
[Download RAW message or body]

Been running with this for a few days, no noticeable change in behaviour. 
i386 and amd64 both running -current from last week.



On 2006 Oct 21 (Sat) at 10:04:13 +0200 (+0200), Henning Brauer wrote:
:this diff makes ntpd use monotonic time for its intrnal interval 
:calculations, like, when to send the next uery to an ntp peer and the 
:like. this has the advantage that changes to the clock do not interfere 
:with the intervals. for example, when we start on machines without an 
:RTC and the initial settime (-s) kicks in, intervals were strange.
:
:diff needs testing, no behaviour change expected (except the cases 
:named before, when system time jumps)
:
:Index: client.c
:===================================================================
:RCS file: /cvs/src/usr.sbin/ntpd/client.c,v
:retrieving revision 1.73
:diff -u -p -r1.73 client.c
:--- client.c	21 Oct 2006 07:32:46 -0000	1.73
:+++ client.c	21 Oct 2006 07:50:43 -0000
:@@ -32,14 +32,14 @@ void	set_deadline(struct ntp_peer *, tim
: void
: set_next(struct ntp_peer *p, time_t t)
: {
:-	p->next = time(NULL) + t;
:+	p->next = getmonotime() + t;
: 	p->deadline = 0;
: }
: 
: void
: set_deadline(struct ntp_peer *p, time_t t)
: {
:-	p->deadline = time(NULL) + t;
:+	p->deadline = getmonotime() + t;
: 	p->next = 0;
: }
: 
:@@ -247,7 +247,7 @@ client_dispatch(struct ntp_peer *p, u_in
: 		return (0);
: 	}
: 	p->reply[p->shift].error = (T2 - T1) - (T3 - T4);
:-	p->reply[p->shift].rcvd = time(NULL);
:+	p->reply[p->shift].rcvd = getmonotime();
: 	p->reply[p->shift].good = 1;
: 
: 	p->reply[p->shift].status.leap = (msg.status & LIMASK);
:Index: ntp.c
:===================================================================
:RCS file: /cvs/src/usr.sbin/ntpd/ntp.c,v
:retrieving revision 1.92
:diff -u -p -r1.92 ntp.c
:--- ntp.c	21 Oct 2006 07:30:58 -0000	1.92
:+++ ntp.c	21 Oct 2006 07:50:44 -0000
:@@ -202,7 +202,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_c
: 
: 		bzero(pfd, sizeof(struct pollfd) * pfd_elms);
: 		bzero(idx2peer, sizeof(void *) * idx2peer_elms);
:-		nextaction = time(NULL) + 3600;
:+		nextaction = getmonotime() + 3600;
: 		pfd[PFD_PIPE_MAIN].fd = ibuf_main->fd;
: 		pfd[PFD_PIPE_MAIN].events = POLLIN;
: 		pfd[PFD_HOTPLUG].fd = hotplugfd;
:@@ -218,7 +218,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_c
: 		idx_peers = i;
: 		sent_cnt = trial_cnt = 0;
: 		TAILQ_FOREACH(p, &conf->ntp_peers, entry) {
:-			if (p->next > 0 && p->next <= time(NULL)) {
:+			if (p->next > 0 && p->next <= getmonotime()) {
: 				if (p->state > STATE_DNS_INPROGRESS)
: 					trial_cnt++;
: 				if (client_query(p) == 0)
:@@ -229,7 +229,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_c
: 
: 			if (p->deadline > 0 && p->deadline < nextaction)
: 				nextaction = p->deadline;
:-			if (p->deadline > 0 && p->deadline <= time(NULL)) {
:+			if (p->deadline > 0 && p->deadline <= getmonotime()) {
: 				timeout = error_interval();
: 				log_debug("no reply from %s received in time, "
: 				    "next query %ds", log_sockaddr(
:@@ -252,9 +252,9 @@ ntp_main(int pipe_prnt[2], struct ntpd_c
: 			}
: 		}
: 
:-		if (last_sensor_scan + SENSOR_SCAN_INTERVAL < time(NULL)) {
:+		if (last_sensor_scan + SENSOR_SCAN_INTERVAL < getmonotime()) {
: 			sensor_scan();
:-			last_sensor_scan = time(NULL);
:+			last_sensor_scan = getmonotime();
: 		}
: 		sensors_cnt = 0;
: 		TAILQ_FOREACH(s, &conf->ntp_sensors, entry) {
:@@ -271,7 +271,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_c
: 		if (ibuf_main->w.queued > 0)
: 			pfd[PFD_PIPE_MAIN].events |= POLLOUT;
: 
:-		timeout = nextaction - time(NULL);
:+		timeout = nextaction - getmonotime();
: 		if (timeout < 0)
: 			timeout = 0;
: 
:@@ -316,7 +316,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_c
: 		for (s = TAILQ_FIRST(&conf->ntp_sensors); s != NULL;
: 		    s = next_s) {
: 			next_s = TAILQ_NEXT(s, entry);
:-			if (s->next <= time(NULL))
:+			if (s->next <= getmonotime())
: 				sensor_query(s);
: 		}
: 	}
:Index: ntpd.h
:===================================================================
:RCS file: /cvs/src/usr.sbin/ntpd/ntpd.h,v
:retrieving revision 1.76
:diff -u -p -r1.76 ntpd.h
:--- ntpd.h	30 Jun 2006 16:52:13 -0000	1.76
:+++ ntpd.h	21 Oct 2006 07:50:45 -0000
:@@ -293,6 +293,7 @@ void	set_next(struct ntp_peer *, time_t)
: double			gettime_corrected(void);
: double			getoffset(void);
: double			gettime(void);
:+time_t			getmonotime(void);
: void			d_to_tv(double, struct timeval *);
: double			lfp_to_d(struct l_fixedpt);
: struct l_fixedpt	d_to_lfp(double);
:Index: sensors.c
:===================================================================
:RCS file: /cvs/src/usr.sbin/ntpd/sensors.c,v
:retrieving revision 1.23
:diff -u -p -r1.23 sensors.c
:--- sensors.c	12 Oct 2006 10:41:51 -0000	1.23
:+++ sensors.c	21 Oct 2006 07:50:45 -0000
:@@ -100,7 +100,7 @@ sensor_add(struct sensor *sensor)
: 	if ((s = calloc(1, sizeof(*s))) == NULL)
: 		fatal("sensor_add calloc");
: 
:-	s->next = time(NULL);
:+	s->next = getmonotime();
: 	s->weight = cs->weight;
: 	if ((s->device = strdup(sensor->device)) == NULL)
: 		fatal("sensor_add strdup");
:@@ -127,7 +127,9 @@ sensor_query(struct ntp_sensor *s)
: 	int		 mib[3];
: 	size_t		 len;
: 
:-	s->next = time(NULL) + SENSOR_QUERY_INTERVAL;
:+	s->next = getmonotime() + SENSOR_QUERY_INTERVAL;
:+
:+	/* rcvd is walltime here, monotime in client.c. not used elsewhere */
: 	if (s->update.rcvd < time(NULL) - SENSOR_DATA_MAXAGE)
: 		s->update.good = 0;
: 
:Index: util.c
:===================================================================
:RCS file: /cvs/src/usr.sbin/ntpd/util.c,v
:retrieving revision 1.11
:diff -u -p -r1.11 util.c
:--- util.c	7 Jun 2006 06:29:03 -0000	1.11
:+++ util.c	21 Oct 2006 07:50:45 -0000
:@@ -47,6 +47,17 @@ gettime(void)
: 	return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec);
: }
: 
:+time_t
:+getmonotime(void)
:+{
:+	struct timespec	ts;
:+
:+	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
:+		fatal("clock_gettime");
:+
:+	return (ts.tv_sec);
:+}
:+
: 
: void
: d_to_tv(double d, struct timeval *tv)
:


--
Government lies, and newspapers lie, but in a democracy they are
different lies.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic