Skip to contentSkip to conversation

clock_settime/4-1: CLOCK_MONOTONIC for timer validation

PATCH In review Andrea Cervesato 3 participants 3 messages Jun 16

lore
Share & tools
lore
#0 lore raw

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

From: Andrea Cervesato <[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.

---
 .../conformance/interfaces/clock_settime/4-1.c     | 36 ++++++++++++++++------
 1 file changed, 26 insertions(+), 10 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c +26 -10
index ebd609963a3651212447ff2ab0885e20b19ba302..cd33ccde5030f54e2c60ee5e2c616dea6c375910 100644
@@ -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,

Signature · 5 lines
Andrea Cervesato <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Please ignore this patch. It's not correct.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Andrea Cervesato

Hi Andrea,

Quoted 5 lines · Andrea Cervesato

clock_settime/4-1: CLOCK_MONOTONIC for timer validation

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

Typo: "in order to the compute" should be "in order to compute".

testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED) · line 121

Quoted 5 lines
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c +4 -0
+		if (use_mono)
+			clock_gettime(CLOCK_MONOTONIC, &mono_before);
 
+		if (use_mono)
+			clock_gettime(CLOCK_MONOTONIC, &mono_after);

Both clock_gettime(CLOCK_MONOTONIC) calls are missing return-value checks. Every other POSIX call in this test checks for errors and returns PTS_UNRESOLVED on failure.

If clock_gettime() were to fail, mono_before / mono_after would remain uninitialized and the delta computation would use garbage values silently.

Verdict - Needs revision

Pre-existing: timer_create() allocates tid but timer_delete(tid) is never called on any exit path.

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp