Index | Thread | Search

From:
Christian Ludwig <christian_ludwig@genua.de>
Subject:
regress/nanosleep: Use monotonic clock for measurements
To:
<tech@openbsd.org>
Date:
Fri, 2 Feb 2024 16:24:43 +0100

Download raw body.

Thread
  • Christian Ludwig:

    regress/nanosleep: Use monotonic clock for measurements

The nanosleep regression test uses gettimeofday(2). Switch it to a
monotonic clock to avoid accidential fails from a call to
settimeofday(2) somewhere in the system.
---
 regress/sys/kern/nanosleep/nanosleep.c | 30 +++++++++++++-------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/regress/sys/kern/nanosleep/nanosleep.c b/regress/sys/kern/nanosleep/nanosleep.c
index 5ccc0826235..451981b55f9 100644
--- a/regress/sys/kern/nanosleep/nanosleep.c
+++ b/regress/sys/kern/nanosleep/nanosleep.c
@@ -137,13 +137,13 @@ int
 time_elapsed(void)
 {
 	struct timespec ts;
-	struct timeval stv, etv;
+	struct timespec stv, etv;
 
 	ts.tv_sec = 0;
 	ts.tv_nsec = 500000000;
 
-	if (gettimeofday(&stv, NULL) < 0) {
-		warn("gettimeofday");
+	if (clock_gettime(CLOCK_MONOTONIC, &stv) < 0) {
+		warn("clock_gettime");
 		return 1;
 	}
 
@@ -152,14 +152,14 @@ time_elapsed(void)
 		return 1;
 	}
 
-	if (gettimeofday(&etv, NULL) < 0) {
-		warn("gettimeofday");
+	if (clock_gettime(CLOCK_MONOTONIC, &etv) < 0) {
+		warn("clock_gettime");
 		return 1;
 	}
 
-	timersub(&etv, &stv, &stv);
+	timespecsub(&etv, &stv, &stv);
 
-	if (stv.tv_sec == 0 && stv.tv_usec < 500000) {
+	if (stv.tv_sec == 0 && stv.tv_nsec < 500000000) {
 		warnx("slept less than 0.5 sec");
 		return 1;
 	}
@@ -171,7 +171,7 @@ int
 time_elapsed_with_signal(void)
 {
 	struct timespec ts, rts;
-	struct timeval stv, etv;
+	struct timespec stv, etv;
 	pid_t pid;
 	int status;
 
@@ -195,8 +195,8 @@ time_elapsed_with_signal(void)
 	rts.tv_sec = 0;
 	rts.tv_nsec = 0;
 
-	if (gettimeofday(&stv, NULL) < 0) {
-		warn("gettimeofday");
+	if (clock_gettime(CLOCK_MONOTONIC, &stv) < 0) {
+		warn("clock_gettime");
 		return 1;
 	}
 
@@ -205,17 +205,17 @@ time_elapsed_with_signal(void)
 		return 1;
 	}
 
-	if (gettimeofday(&etv, NULL) < 0) {
-		warn("gettimeofday");
+	if (clock_gettime(CLOCK_MONOTONIC, &etv) < 0) {
+		warn("clock_gettime");
 		return 1;
 	}
 
-	timersub(&etv, &stv, &stv);
+	timespecsub(&etv, &stv, &stv);
 
 	etv.tv_sec = rts.tv_sec;
-	etv.tv_usec = rts.tv_nsec / 1000 + 1; /* the '+ 1' is a "roundup" */
+	etv.tv_nsec = rts.tv_nsec;
 
-	timeradd(&etv, &stv, &stv);
+	timespecadd(&etv, &stv, &stv);
 
 	if (stv.tv_sec < 10) {
 		warnx("slept time + leftover time < 10 sec");
-- 
2.34.1