[PATCH] clock_settime/4-1: CLOCK_MONOTONIC for timer validation

Andrea Cervesato <[email protected]>
Newsgroups gmane.linux.ltp
Message-ID <[email protected]>
From: Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>

During test execution, any service which is operating on clock might
interfere with the final result. For this reason, we use CLOCK_MONOTONIC
(when available) in order to the compute the right delta between
clock_gettime() calls.

Signed-off-by: Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>
---
 .../conformance/interfaces/clock_settime/4-1.c     | 36 ++++++++++++++++------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c
index ebd609963a3651212447ff2ab0885e20b19ba302..cd33ccde5030f54e2c60ee5e2c616dea6c375910 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c
@@ -16,12 +16,10 @@
  * - set time back to T0
  * - wait for the timer to expire
  * - get time T2
- * - ensure that:  T2 >= T1 and (T2-T1) <= ACCEPTABLEDELTA
+ * - ensure the timer fired ~TIMEROFFSET seconds after clock_settime()
+ *   using CLOCK_MONOTONIC when available (immune to NTP adjustments)
  *
  * signal SIGTOTEST is used.
- *
- * adam.li: I think should check that (abs(T2-T1) <= ACCEPTABLEDELTA)
- * 2004-04-30
  */
 #include <stdio.h>
 #include <time.h>
@@ -43,12 +41,14 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
 {
 	struct sigevent ev;
 	struct timespec tpT0, tpT2, tpreset;
+	struct timespec mono_before, mono_after;
 	struct itimerspec its;
 	timer_t tid;
-	int delta;
+	long delta;
 	int sig;
 	sigset_t set;
 	int attempt, ret;
+	int use_mono = pts_mono_available();
 
 	/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */
 	if (geteuid() != 0) {
@@ -103,6 +103,10 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
 
 		sleep(SLEEPTIME);
 		getBeforeTime(&tpreset);
+
+		if (use_mono)
+			clock_gettime(CLOCK_MONOTONIC, &mono_before);
+
 		if (clock_settime(CLOCK_REALTIME, &tpT0) != 0) {
 			perror("clock_settime() was not successful");
 			return PTS_UNRESOLVED;
@@ -113,12 +117,24 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
 			return PTS_UNRESOLVED;
 		}
 
+		if (use_mono)
+			clock_gettime(CLOCK_MONOTONIC, &mono_after);
+
 		if (clock_gettime(CLOCK_REALTIME, &tpT2) != 0) {
 			printf("clock_gettime() was not successful\n");
 			return PTS_UNRESOLVED;
 		}
 
-		delta = tpT2.tv_sec - its.it_value.tv_sec;
+		/*
+		 * Use CLOCK_MONOTONIC to compute delta when available
+		 * since CLOCK_REALTIME may be adjusted by NTP between
+		 * clock_settime() and clock_gettime(). The timer
+		 * should fire TIMEROFFSET seconds after the reset.
+		 */
+		if (use_mono)
+			delta = (mono_after.tv_sec - mono_before.tv_sec) - TIMEROFFSET;
+		else
+			delta = tpT2.tv_sec - its.it_value.tv_sec;
 
 		// add back time waited to reset value and reset time
 		tpreset.tv_sec += tpT2.tv_sec - tpT0.tv_sec;
@@ -137,13 +153,13 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
 		return PTS_UNTESTED;
 	}
 
-	printf("delta: %d\n", delta);
-	if ((delta <= ACCEPTABLEDELTA) && (delta >= 0)) {
+	printf("delta: %ld\n", delta);
+	if (labs(delta) <= ACCEPTABLEDELTA) {
 		printf("Test PASSED\n");
 		return PTS_PASS;
 	}
 
-	printf("FAIL:  Ended %d, not %d\n",
-	       (int)tpT2.tv_sec, (int)its.it_value.tv_sec);
+	printf("FAIL:  Timer fired %ld seconds %s expected\n",
+	       labs(delta), delta > 0 ? "after" : "before");
 	return PTS_FAIL;
 }

---
base-commit: b4d31ea5d5c16628b9c45932fc98b929ca2ceece
change-id: 20260616-fix_clock_gettime_openposix-c6779c9f4d92

Best regards,
-- 
Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.