[Git][NTPsec/ntpsec][master] Many CLOCK_REALTIME=>CLOCK_MONOTONIC

Hal Murray (@hal.murray) gitlab at mg.gitlab.com
Sun Oct 22 06:16:31 UTC 2023



Hal Murray pushed to branch master at NTPsec / ntpsec


Commits:
9b1c1cbc by Hal Murray at 2023-10-21T23:11:01-07:00
Many CLOCK_REALTIME=>CLOCK_MONOTONIC

This will get the right answer for timing things if the clock
is adjusted while our target is running.

It's also good documentation.

- - - - -


7 changed files:

- attic/clocks.c
- attic/random.c
- ntpd/ntp_monitor.c
- ntpd/ntp_proto.c
- ntpd/ntp_timer.c
- ntpd/nts_client.c
- ntpd/nts_server.c


Changes:

=====================================
attic/clocks.c
=====================================
@@ -26,9 +26,7 @@ struct table clocks [] = {
 #ifdef CLOCK_REALTIME_COARSE
 	{CLOCK_REALTIME_COARSE, "CLOCK_REALTIME_COARSE"},
 #endif
-#ifdef CLOCK_MONOTONIC
 	{CLOCK_MONOTONIC, "CLOCK_MONOTONIC"},
-#endif
 #ifdef CLOCK_MONOTONIC_RAW
 	{CLOCK_MONOTONIC_RAW, "CLOCK_MONOTONIC_RAW"},
 #endif
@@ -46,17 +44,17 @@ static int getpid_average(void) {
         struct timespec start, stop;
         uint64_t sec, nanos;
 
-        err = clock_gettime(CLOCK_REALTIME, &start);
+        err = clock_gettime(CLOCK_MONOTONIC, &start);
         if (-1 == err) {
-                printf("clock_gettime(CLOCK_REALTIME) didn't work, err %d\n", errno);
+                printf("clock_gettime(CLOCK_MONOTONIC) didn't work, err %d\n", errno);
                 return -1;
         }
 
-        clock_gettime(CLOCK_REALTIME, &start);
+        clock_gettime(CLOCK_MONOTONIC, &start);
         for (int i = 0; i < BATCHSIZE; i++) {
                 getpid();
         }
-        clock_gettime(CLOCK_REALTIME, &stop);
+        clock_gettime(CLOCK_MONOTONIC, &stop);
 
         /* Beware of overflowing 32 bits. */
         sec = (stop.tv_sec-start.tv_sec);
@@ -72,17 +70,17 @@ static int gettimeofday_average(void) {
         uint64_t sec, nanos;
 	struct timeval now;
 
-        err = clock_gettime(CLOCK_REALTIME, &start);
+        err = clock_gettime(CLOCK_MONOTONIC, &start);
         if (-1 == err) {
-                printf("clock_gettime(CLOCK_REALTIME) didn't work, err %d\n", errno);
+                printf("clock_gettime(CLOCK_MONOTONIC) didn't work, err %d\n", errno);
                 return -1;
         }
 
-        clock_gettime(CLOCK_REALTIME, &start);
+        clock_gettime(CLOCK_MONOTONIC, &start);
         for (int i = 0; i < BATCHSIZE; i++) {
                 gettimeofday(&now, NULL);
         }
-        clock_gettime(CLOCK_REALTIME, &stop);
+        clock_gettime(CLOCK_MONOTONIC, &stop);
 
         /* Beware of overflowing 32 bits. */
         sec = (stop.tv_sec-start.tv_sec);
@@ -119,11 +117,11 @@ static int do_average(int type, const char* name) {
 		return -1;
 	}
 
-	clock_gettime(CLOCK_REALTIME, &start);
+	clock_gettime(CLOCK_MONOTONIC, &start);
 	for (int i = 0; i < BATCHSIZE; i++) {
 		clock_gettime(type, &stop);
 	}
-	clock_gettime(CLOCK_REALTIME, &stop);
+	clock_gettime(CLOCK_MONOTONIC, &stop);
 
 	/* Beware of overflowing 32 bits. */
 	sec = (stop.tv_sec-start.tv_sec);


=====================================
attic/random.c
=====================================
@@ -24,17 +24,17 @@ static int getpid_average(void) {
         struct timespec start, stop;
         uint64_t sec, nanos;
 
-        err = clock_gettime(CLOCK_REALTIME, &start);
+        err = clock_gettime(CLOCK_MONOTONIC, &start);
         if (-1 == err) {
-                printf("clock_gettime(CLOCK_REALTIME) didn't work, err %d\n", errno);
+                printf("clock_gettime(CLOCK_MONOTONIC) didn't work, err %d\n", errno);
                 return -1;
         }
 
-        clock_gettime(CLOCK_REALTIME, &start);
+        clock_gettime(CLOCK_MONOTONIC, &start);
         for (int i = 0; i < BATCHSIZE; i++) {
                 getpid();
         }
-        clock_gettime(CLOCK_REALTIME, &stop);
+        clock_gettime(CLOCK_MONOTONIC, &stop);
 
         /* Beware of overflowing 32 bits. */
         sec = (stop.tv_sec-start.tv_sec);
@@ -48,17 +48,17 @@ static int clock_average(void) {
         struct timespec start, stop;
         uint64_t sec, nanos;
 
-        err = clock_gettime(CLOCK_REALTIME, &start);
+        err = clock_gettime(CLOCK_MONOTONIC, &start);
         if (-1 == err) {
-                printf("clock_gettime(CLOCK_REALTIME) didn't work, err %d\n", errno);
+                printf("clock_gettime(CLOCK_MONOTONIC) didn't work, err %d\n", errno);
                 return -1;
         }
 
-        clock_gettime(CLOCK_REALTIME, &start);
+        clock_gettime(CLOCK_MONOTONIC, &start);
         for (int i = 0; i < BATCHSIZE; i++) {
                 clock_gettime(CLOCK_REALTIME, &stop);
         }
-        clock_gettime(CLOCK_REALTIME, &stop);
+        clock_gettime(CLOCK_MONOTONIC, &stop);
 
         /* Beware of overflowing 32 bits. */
         sec = (stop.tv_sec-start.tv_sec);
@@ -72,11 +72,11 @@ static int do_average(void) {
 	struct timespec start, stop;
 	uint64_t sec, nanos;
 
-	clock_gettime(CLOCK_REALTIME, &start);
+	clock_gettime(CLOCK_MONOTONIC, &start);
 	for (int i = 0; i < BATCHSIZE; i++) {
 		sum += random();
 	}
-	clock_gettime(CLOCK_REALTIME, &stop);
+	clock_gettime(CLOCK_MONOTONIC, &stop);
 	(void)sum;	/* Squash unused warning */
 
 	/* Beware of overflowing 32 bits. */
@@ -96,11 +96,11 @@ static int do_avg_bytes(unsigned int bytes) {
 		exit(1);
 	}
 
-	clock_gettime(CLOCK_REALTIME, &start);
+	clock_gettime(CLOCK_MONOTONIC, &start);
 	for (int i = 0; i < BATCHSIZE; i++) {
             err += RAND_bytes((unsigned char *)&rnd, bytes);
 	}
-	clock_gettime(CLOCK_REALTIME, &stop);
+	clock_gettime(CLOCK_MONOTONIC, &stop);
 
 	if (BATCHSIZE != err) {
 		printf("## do_avg_bytes: troubles from RAND_bytes, %d\n",
@@ -126,7 +126,7 @@ static int do_avg_priv(unsigned int bytes) {
 		exit(1);
 	}
 
-	clock_gettime(CLOCK_REALTIME, &start);
+	clock_gettime(CLOCK_MONOTONIC, &start);
 	for (int i = 0; i < BATCHSIZE; i++) {
 #ifdef LIBRESSL_VERSION_NUMBER
             err += RAND_bytes((unsigned char *)&rnd, bytes);
@@ -134,7 +134,7 @@ static int do_avg_priv(unsigned int bytes) {
             err += RAND_priv_bytes((unsigned char *)&rnd, bytes);
 #endif
 	}
-	clock_gettime(CLOCK_REALTIME, &stop);
+	clock_gettime(CLOCK_MONOTONIC, &stop);
 
 	if (BATCHSIZE != err) {
 		printf("## do_avg_priv: troubles from RAND_bytes, %d\n",
@@ -155,10 +155,10 @@ static int do_fastest(void) {
 	uint64_t sec, nanos, fastest = 999999999;
 
 	for (int i = 0; i < BATCHSIZE; i++) {
-                clock_gettime(CLOCK_REALTIME, &start);
+                clock_gettime(CLOCK_MONOTONIC, &start);
 		/* coverity[DC.WEAK_CRYPTO] */
 		sum += random();
-		clock_gettime(CLOCK_REALTIME, &stop);
+		clock_gettime(CLOCK_MONOTONIC, &stop);
 		sec = (stop.tv_sec-start.tv_sec);
 		nanos = sec*BILLION + (stop.tv_nsec-start.tv_nsec);
 		if (nanos < fastest) {
@@ -182,9 +182,9 @@ static int do_fast_bytes(unsigned bytes) {
 	}
 
 	for (int i = 0; i < BATCHSIZE; i++) {
-                clock_gettime(CLOCK_REALTIME, &start);
+                clock_gettime(CLOCK_MONOTONIC, &start);
                 err += RAND_bytes((unsigned char *)&rnd, bytes);
-		clock_gettime(CLOCK_REALTIME, &stop);
+		clock_gettime(CLOCK_MONOTONIC, &stop);
 		sec = (stop.tv_sec-start.tv_sec);
 		nanos = sec*BILLION + (stop.tv_nsec-start.tv_nsec);
 		if (nanos < fastest) {
@@ -214,13 +214,13 @@ static int do_fast_priv(unsigned bytes) {
 	}
 
 	for (int i = 0; i < BATCHSIZE; i++) {
-                clock_gettime(CLOCK_REALTIME, &start);
+                clock_gettime(CLOCK_MONOTONIC, &start);
 #ifdef LIBRESSL_VERSION_NUMBER
                 err += RAND_bytes((unsigned char *)&rnd, bytes);
 #else
                 err += RAND_priv_bytes((unsigned char *)&rnd, bytes);
 #endif
-		clock_gettime(CLOCK_REALTIME, &stop);
+		clock_gettime(CLOCK_MONOTONIC, &stop);
 		sec = (stop.tv_sec-start.tv_sec);
 		nanos = sec*BILLION + (stop.tv_nsec-start.tv_nsec);
 		if (nanos < fastest) {


=====================================
ntpd/ntp_monitor.c
=====================================
@@ -506,7 +506,7 @@ void mon_timer(void) {
 	struct timespec start, finish;
 	float scan_time;
 
-	clock_gettime(CLOCK_REALTIME, &start);
+	clock_gettime(CLOCK_MONOTONIC, &start);
 	for (	mon = TAIL_DLIST(mon_data.mon_mru_list, mru);
 		mon != NULL;
 		mon = PREV_DLIST(mon_data.mon_mru_list, mon, mru)) {
@@ -538,7 +538,7 @@ void mon_timer(void) {
 	  }
 	  when = mon->last;
 	}
-	clock_gettime(CLOCK_REALTIME, &finish);
+	clock_gettime(CLOCK_MONOTONIC, &finish);
 	scan_time = tspec_to_d(sub_tspec(finish, start));
 	if (count == (long)mon_data.mru_entries)
 	    msyslog(LOG_INFO, "MON: Scanned %ld slots in %.3f",


=====================================
ntpd/ntp_proto.c
=====================================
@@ -2389,7 +2389,7 @@ fast_xmit(
          *  3) none
 	 */
 	sendlen = LEN_PKT_NOMAC;
-	clock_gettime(CLOCK_REALTIME, &start);
+	clock_gettime(CLOCK_MONOTONIC, &start);
 	if (rbufp->ntspacket.valid) {
 #ifndef DISABLE_NTS
 	  sendlen += extens_server_send(&rbufp->ntspacket, &xpkt);
@@ -2406,7 +2406,7 @@ fast_xmit(
 	  return;
 	}
 	sendpkt(&rbufp->recv_srcadr, rbufp->dstadr, &xpkt, (int)sendlen);
-	clock_gettime(CLOCK_REALTIME, &finish);
+	clock_gettime(CLOCK_MONOTONIC, &finish);
 	sys_authdelay = tspec_intv_to_lfp(sub_tspec(finish, start));
 	/* Previous versions of this code had separate DPRINT-s so it
 	 * could print the key on the auth case.  That requires separate


=====================================
ntpd/ntp_timer.c
=====================================
@@ -149,7 +149,7 @@ init_timer(void)
 	 * seconds.
 	 */
 #ifdef HAVE_TIMER_CREATE
-	if (TC_ERR == timer_create(CLOCK_REALTIME, NULL, &timer_id)) {
+	if (TC_ERR == timer_create(CLOCK_MONOTONIC, NULL, &timer_id)) {
 		msyslog(LOG_ERR, "ERR: timer_create failed, %s", strerror(errno));
 		exit(1);
 	}


=====================================
ntpd/nts_client.c
=====================================
@@ -78,7 +78,7 @@ bool nts_probe(struct peer * peer) {
 		return false;
 
 	addrOK = false;
-	clock_gettime(CLOCK_REALTIME, &start);
+	clock_gettime(CLOCK_MONOTONIC, &start);
 
 	if (NULL == hostname) {
 		/* IP Address case */
@@ -179,7 +179,7 @@ bool nts_probe(struct peer * peer) {
 	SSL_free(ssl);
 	close(server);
 
-	clock_gettime(CLOCK_REALTIME, &finish);
+	clock_gettime(CLOCK_MONOTONIC, &finish);
 	finish = sub_tspec(finish, start);
 	msyslog(LOG_INFO, "NTSc: NTS-KE req to %s took %.3f sec, %s",
 		hostname, tspec_to_d(finish),
@@ -272,14 +272,14 @@ int open_TCP_socket(struct peer *peer, const char *hostname) {
 	hints.ai_protocol = IPPROTO_TCP;
 	hints.ai_socktype = SOCK_STREAM;
 	hints.ai_family = AF(&peer->srcadr);  /* -4, -6 switch */
-	clock_gettime(CLOCK_REALTIME, &start);
+	clock_gettime(CLOCK_MONOTONIC, &start);
 	gai_rc = getaddrinfo(host, port, &hints, &answer);
 	if (0 != gai_rc) {
 		msyslog(LOG_INFO, "NTSc: open_TCP_socket: DNS error trying to contact %s: %d, %s",
 			hostname, gai_rc, gai_strerror(gai_rc));
 		return -1;
 	}
-	clock_gettime(CLOCK_REALTIME, &finish);
+	clock_gettime(CLOCK_MONOTONIC, &finish);
 	finish = sub_tspec(finish, start);
 	msyslog(LOG_INFO, "NTSc: DNS lookup of %s took %.3f sec",
 		hostname, tspec_to_d(finish));


=====================================
ntpd/nts_server.c
=====================================
@@ -238,7 +238,7 @@ void* nts_ke_listener(void* arg) {
 			sleep(1);		/* avoid log clutter on bug */
 			continue;
 		}
-		clock_gettime(CLOCK_REALTIME, &start);
+		clock_gettime(CLOCK_MONOTONIC, &start);
 		sockporttoa_r(&addr, addrbuf, sizeof(addrbuf));
 
 /* This is disabled in order to reduce clutter in the log file.
@@ -275,7 +275,7 @@ void* nts_ke_listener(void* arg) {
 		SSL_set_fd(ssl, client);
 
 		if (SSL_accept(ssl) <= 0) {
-			clock_gettime(CLOCK_REALTIME, &finish);
+			clock_gettime(CLOCK_MONOTONIC, &finish);
 			wall = tspec_intv_to_lfp(sub_tspec(finish, start));
 			nts_ke_accept_fail(addrbuf, lfptox(wall));
 			SSL_free(ssl);
@@ -314,7 +314,7 @@ void* nts_ke_listener(void* arg) {
 		SSL_free(ssl);
 		close(client);
 
-		clock_gettime(CLOCK_REALTIME, &finish);
+		clock_gettime(CLOCK_MONOTONIC, &finish);
 		wall = tspec_intv_to_lfp(sub_tspec(finish, start));
 		if (worked) {
 			ntske_cnt.serves_good++;



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/9b1c1cbcbd6c291eec7f512392610a0f767ef5bc

-- 
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/-/commit/9b1c1cbcbd6c291eec7f512392610a0f767ef5bc
You're receiving this email because of your account on gitlab.com.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20231022/9e9234fd/attachment-0001.htm>


More information about the vc mailing list