Skip to contentSkip to conversation

clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC

PATCH Superseded Andrea Cervesato 4 participants 7 messages Apr 28 – Apr 29

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC

From: Andrea Cervesato <[email protected]>

The clock_settime helpers.h used a compile-time #ifdef on _POSIX_MONOTONIC_CLOCK to select between real and stub implementations of pts_mono_time_start()/pts_mono_time_check().

On Linux/glibc, _POSIX_MONOTONIC_CLOCK is defined as 0, meaning support is optional and must be verified at runtime via sysconf(_SC_MONOTONIC_CLOCK). The #ifdef treated 0 the same as

Quoted 1 line

0 (always available), which is not POSIX-correct.

Replace the compile-time #ifdef/#else split with a runtime pts_mono_available() helper that performs proper POSIX detection via sysconf(). Extract pts_mono_available() into the shared include/clock.h so it can be reused by nanosleep helpers as well. Also add a missing #ifndef include guard and #include <unistd.h> so _POSIX_MONOTONIC_CLOCK is always defined.

Fixes 9ecb4a004b18 ("clock_settime: Detect external clock adjustments via CLOCK_MONOTONIC")

---
Changes in v2:
- add testcases/open_posix_testsuite/include/clock.h helper
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 .../conformance/interfaces/clock_settime/helpers.h | 67 ++++++++++++----------
 testcases/open_posix_testsuite/include/clock.h     | 23 ++++++++
 2 files changed, 59 insertions(+), 31 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +36 -31
index 37bf30926f039553e9e370751b7938ca5ea1d00a..2802b694133b90f478ee10073bcdfa0e34320f7d 100644
@@ -13,7 +13,12 @@
  * by those tests.
  */
 
+#ifndef CLOCK_SETTIME_HELPERS_H
+#define CLOCK_SETTIME_HELPERS_H
+
 #include <stdlib.h>
+#include <unistd.h>
+#include "clock.h"
 
 static int getBeforeTime(struct timespec *tpget)
 {
@@ -37,53 +42,53 @@ static int setBackTime(struct timespec tpset)
 
 #define PTS_MONO_MAX_RETRIES 3
 
-#ifdef _POSIX_MONOTONIC_CLOCK
-static struct timespec _pts_mono_start;
+static struct timespec pts_mono_start;
 
 static inline int pts_mono_time_start(void)
 {
-	if (clock_gettime(CLOCK_MONOTONIC, &_pts_mono_start) != 0) {
+	if (!pts_mono_available()) {
+		static int warned;
+
+		if (!warned) {
+			printf("CLOCK_MONOTONIC unavailable, test may fail due to clock adjustment\n");
+			warned = 1;
+		}
+		return 0;
+	}
+
+#ifdef _POSIX_MONOTONIC_CLOCK
+	if (clock_gettime(CLOCK_MONOTONIC, &pts_mono_start) != 0) {
 		perror("clock_gettime(CLOCK_MONOTONIC) failed");
 		return -1;
 	}
+#endif
 	return 0;
 }
 
 static inline int pts_mono_time_check(unsigned int expected_secs)
 {
-	struct timespec now;
-	long elapsed;
-
-	if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
-		perror("clock_gettime(CLOCK_MONOTONIC) failed");
-		return -1;
-	}
+#ifdef _POSIX_MONOTONIC_CLOCK
+	if (pts_mono_available()) {
+		struct timespec now;
+		long elapsed;
 
-	elapsed = now.tv_sec - _pts_mono_start.tv_sec;
+		if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
+			perror("clock_gettime(CLOCK_MONOTONIC) failed");
+			return -1;
+		}
 
-	if (labs(elapsed - (long)expected_secs) > 1) {
-		printf("Clock adjustment detected (elapsed %lds, expected ~%us)\n",
-		       elapsed, expected_secs);
-		return 1;
-	}
-	return 0;
-}
-#else
-static inline int pts_mono_time_start(void)
-{
-	static int warned;
+		elapsed = now.tv_sec - pts_mono_start.tv_sec;
 
-	if (!warned) {
-		printf("CLOCK_MONOTONIC unavailable, test may fail due to clock adjustment\n");
-		warned = 1;
+		if (labs(elapsed - (long)expected_secs) > 1) {
+			printf("Clock adjustment detected (elapsed %lds, expected ~%us)\n",
+			       elapsed, expected_secs);
+			return 1;
+		}
+		return 0;
 	}
-	return 0;
-}
-
-static inline int pts_mono_time_check(unsigned int expected_secs)
-{
+#endif
 	(void)expected_secs;
 	return 0;
 }
-#endif
 
+#endif /* CLOCK_SETTIME_HELPERS_H */
testcases/open_posix_testsuite/include/clock.h new file +23 -0
new file mode 100644
index 0000000000000000000000000000000000000000..351064f988bd1af75aa16da03bf8a545990bbbfc
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2026, Linux Test Project
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef PTS_CLOCK_H
+#define PTS_CLOCK_H
+
+#include <unistd.h>
+
+static inline int pts_mono_available(void)
+{
+#ifdef _POSIX_MONOTONIC_CLOCK
+	if (_POSIX_MONOTONIC_CLOCK > 0)
+		return 1;
+
+	if (!_POSIX_MONOTONIC_CLOCK && sysconf(_SC_MONOTONIC_CLOCK) > 0)
+		return 1;
+#endif
+	return 0;
+}
+
+#endif /* PTS_CLOCK_H */

---
base-commit: 068617a55d86f94c21cc8cbaef1c8dd7b31531e3
change-id: 20260416-fix_clock_settime_helper-d47f0db0057e

Best regards,

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


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

Hi Andrea,

testcases/open_posix_testsuite/include/clock.h line 4

Quoted 8 lines · Andrea Cervesato

clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC

+++ b/testcases/open_posix_testsuite/include/clock.h

testcases/open_posix_testsuite/include/clock.h new file +4 -0
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2026, Linux Test Project
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */

New Open POSIX testsuite headers should carry a "Created by: name <email>" line and a brief description of the file's purpose in the copyright block, consistent with the rest of the tree (e.g. helpers.h). Please add both.

[...]

---
Note:

Our agent completed the review of the patch. The full review can be found at:
https://patchwork.ozlabs.org/project/ltp/list/?series=501837

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
#2 lore raw
Quoted 5 lines

New Open POSIX testsuite headers should carry a "Created by: name <email>" line and a brief description of the file's purpose in the copyright block, consistent with the rest of the tree (e.g. helpers.h). Please add both.

This is correct, I will fix it before merging it.
Ping @Petr

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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 8 lines
Quoted 4 lines

New Open POSIX testsuite headers should carry a "Created by: name <email>" line and a brief description of the file's purpose in the copyright block, consistent with the rest of the tree (e.g. helpers.h). Please add both.

This is correct, I will fix it before merging it.
Ping @Petr

Replied previously. BTW Agent removed "v2" from the title [1], therefore I did not reply here because I did not want my reply to endup in old patch version.

It'd be great if Agent just keep the title to not confuse humans (people usually trust the title instead of parsing In-Reply-To:).

Kind regards,
Petr

[1] https://lore.kernel.org/ltp/[email protected]/

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines

New Open POSIX testsuite headers should carry a "Created by: name <email>" line and a brief description of the file's purpose in the copyright block, consistent with the rest of the tree (e.g. helpers.h). Please add both.

Quoted 9 lines
Quoted 2 lines

This is correct, I will fix it before merging it.
Ping @Petr

Replied previously. BTW Agent removed "v2" from the title [1], therefore I did not reply here because I did not want my reply to endup in old patch version.

It'd be great if Agent just keep the title to not confuse humans (people usually trust the title instead of parsing In-Reply-To:).

I didn't find a way to do it yet. I will take a look

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

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 1/1
#5 lore raw
In reply to Andrea Cervesato

Hi Andrea,

Quoted 13 lines
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +3 -2
+#include <unistd.h>
+#include "clock.h"
 
 static int getBeforeTime(struct timespec *tpget)
 {
@@ -37,53 +42,53 @@ static int setBackTime(struct timespec tpset)
 
 #define PTS_MONO_MAX_RETRIES 3
 
-#ifdef _POSIX_MONOTONIC_CLOCK
-static struct timespec _pts_mono_start;
+static struct timespec pts_mono_start;
 

testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h static int setBackTime(struct timespec tpset) · line 55

Quoted 10 lines
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +7 -1
 static inline int pts_mono_time_start(void)
 {
-	if (clock_gettime(CLOCK_MONOTONIC, &_pts_mono_start) != 0) {
+	if (!pts_mono_available()) {
+		static int warned;
+
+		if (!warned) {
+			printf("CLOCK_MONOTONIC unavailable, test may fail due to clock adjustment\n");
+			warned = 1;
+		}

Function with this warning could be in include/clock.h as well, because that will be needed for nanosleep tests. But you can postpone it to nanosleep changes.

Kind regards,
Petr

Quoted 2 lines
(quoted diff) +2 -0
+		return 0;
+	}

...

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

Hi Petr,

Quoted 3 lines

Function with this warning could be in include/clock.h as well, because that will be needed for nanosleep tests. But you can postpone it to nanosleep changes.

I can do it here, no problem.

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

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