clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
PATCH 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(). Also add a missing #ifndef include guard and #include <unistd.h> so _POSIX_MONOTONIC_CLOCK is always defined.
--- .../conformance/interfaces/clock_settime/helpers.h | 76 +++++++++++++--------- 1 file changed, 46 insertions(+), 30 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +46 -30
@@ -13,7 +13,11 @@* by those tests. */ +#ifndef CLOCK_SETTIME_HELPERS_H +#define CLOCK_SETTIME_HELPERS_H +#include <stdlib.h> +#include <unistd.h> static int getBeforeTime (struct timespec *tpget) {
@@ -37,53 +41,65 @@ static int setBackTime(struct timespec tpset)#define PTS_MONO_MAX_RETRIES 3 +static struct timespec pts_mono_start; + +static inline int pts_mono_available (void ) +{#ifdef _POSIX_MONOTONIC_CLOCK -static struct timespec _pts_mono_start; +if (_POSIX_MONOTONIC_CLOCK >0 ) +return 1 ; + +if (!_POSIX_MONOTONIC_CLOCK &&sysconf (_SC_MONOTONIC_CLOCK) >0 ) +return 1 ; +#endif +return 0 ; +}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 */
---
base-commit: 620d596c79493af089ba6a1c4dc79efcc76a67c7
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
Hi Andrea,
Quoted 1 line · Andrea Cervesato
clock_settime: use POSIX runtime detection forCLOCK_MONOTONIC
---
Note:
Our agent completed the review of the patch. The full review can be found at: https://github.com/linux-test-project/ltp-agent/actions/runs/24513029369
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.
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 42 lines
From: Andrea Cervesato <[email protected]>
The
clock_settimehelpers.hused a compile-time #ifdef on_POSIX_MONOTONIC_CLOCKto select between real and stub implementations of pts_mono_time_start()/pts_mono_time_check().On Linux/glibc,
_POSIX_MONOTONIC_CLOCKis defined as 0, meaning support is optional and must be verified at runtime viasysconf(_SC_MONOTONIC_CLOCK). The #ifdef treated 0 the same asQuoted 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 viasysconf(). Also add a missing #ifndef include guard and #include <unistd.h> so_POSIX_MONOTONIC_CLOCKis always defined.Fixes 9ecb4a004b18 ("clock_settime: Detect external clock adjustments via CLOCK_MONOTONIC")Signed-off-by Andrea Cervesato <[email protected]>--- .../conformance/interfaces/clock_settime/helpers.h | 76 +++++++++++++--------- 1 file changed, 46 insertions(+), 30 deletions(-)testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +4
-0@@ -13,7 +13,11 @@* by those tests. */ +#ifndef CLOCK_SETTIME_HELPERS_H +#define CLOCK_SETTIME_HELPERS_H +#include <stdlib.h> +#include <unistd.h> static int getBeforeTime (struct timespec *tpget) {@@ -37,53 +41,65 @@ static int setBackTime(struct timespec tpset)#define PTS_MONO_MAX_RETRIES 3
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h static int setBackTime(struct timespec tpset) · line 52
Quoted 10 lines
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +8
-1+static struct timespec pts_mono_start; + +static inline int pts_mono_available (void ) +{#ifdef _POSIX_MONOTONIC_CLOCK -static struct timespec _pts_mono_start; +if (_POSIX_MONOTONIC_CLOCK >0 ) +return 1 ; + +if (!_POSIX_MONOTONIC_CLOCK &&sysconf (_SC_MONOTONIC_CLOCK) >0 )
This and the previous condition is the same as in patch for nanosleep. It's a simple code, but because it's still a bit specific, wouldn't it make sense to move the function from nanosleep/helpers.h [1]
to newly created file in testcases/open_posix_testsuite/include/clock.h? And both tests would use it?
Kind regards,
Petr
[1] https://patchwork.ozlabs.org/project/ltp/patch/[email protected]/
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h static int setBackTime(struct timespec tpset) · line 56
Quoted 4 lines
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +4
-0+return 1 ; +#endif +return 0 ; +}
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp