All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] clock_settime/4-1: CLOCK_MONOTONIC for timer validation
@ 2026-06-16  8:57 Andrea Cervesato
  2026-06-16  9:06 ` Andrea Cervesato via ltp
  2026-06-16 10:09 ` [LTP] " linuxtestproject.agent
  0 siblings, 2 replies; 3+ messages in thread
From: Andrea Cervesato @ 2026-06-16  8:57 UTC (permalink / raw)
  To: Linux Test Project

From: Andrea Cervesato <andrea.cervesato@suse.com>

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@suse.com>
---
 .../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@suse.com>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [LTP] [PATCH] clock_settime/4-1: CLOCK_MONOTONIC for timer validation
  2026-06-16  8:57 [LTP] [PATCH] clock_settime/4-1: CLOCK_MONOTONIC for timer validation Andrea Cervesato
@ 2026-06-16  9:06 ` Andrea Cervesato via ltp
  2026-06-16 10:09 ` [LTP] " linuxtestproject.agent
  1 sibling, 0 replies; 3+ messages in thread
From: Andrea Cervesato via ltp @ 2026-06-16  9:06 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: Linux Test Project

Please ignore this patch. It's not correct.

Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [LTP] clock_settime/4-1: CLOCK_MONOTONIC for timer validation
  2026-06-16  8:57 [LTP] [PATCH] clock_settime/4-1: CLOCK_MONOTONIC for timer validation Andrea Cervesato
  2026-06-16  9:06 ` Andrea Cervesato via ltp
@ 2026-06-16 10:09 ` linuxtestproject.agent
  1 sibling, 0 replies; 3+ messages in thread
From: linuxtestproject.agent @ 2026-06-16 10:09 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hi Andrea,

On Tue, 16 Jun 2026 10:57:22 +0200, Andrea Cervesato wrote:
> 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".

> +		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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-06-16 10:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-16  8:57 [LTP] [PATCH] clock_settime/4-1: CLOCK_MONOTONIC for timer validation Andrea Cervesato
2026-06-16  9:06 ` Andrea Cervesato via ltp
2026-06-16 10:09 ` [LTP] " linuxtestproject.agent

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.