nanosleep: use POSIX runtime detection for CLOCK_MONOTONIC
PATCH v2 1/1 nanosleep: use POSIX runtime detection for CLOCK_MONOTONIC
From: Andrea Cervesato <[email protected]>
All nanosleep POSIX conformance tests used a compile-time #ifdef on _POSIX_MONOTONIC_CLOCK to select the clock for measuring elapsed sleep duration. This was incorrect for two reasons:
1. Three test files (1-1.c, 2-1.c, 10000-1.c) did not include <unistd.h>, so the macro was never defined and the tests silently fell back to CLOCK_REALTIME.
2. 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.
Using CLOCK_REALTIME caused sporadic test failures when NTP or VM time sync adjusted the wall clock during sleep, producing results like "slept 32s >> 10s".
Extract the clock selection logic into a shared helpers.h with a get_supported_clock() function that performs proper POSIX runtime detection via sysconf(), and use it across all affected tests.
---
Some nanosleep tests are not recognizing CLOCK_MONOTONIC at runtime even if it's available:
CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments sleep 0 sec 30000000 nsec
PASS - slept 0s30078623ns ~= 0s30000000ns
sleep 1 sec 0 nsec
PASS - slept 1s97618ns ~= 1s0ns
sleep 1 sec 30000000 nsec
PASS - slept 1s30109248ns ~= 1s30000000ns
sleep 2 sec 0 nsec
PASS - slept 2s147180ns ~= 2s0ns
sleep 10 sec 5000 nsec
FAIL - slept 32s630120452ns >> 10s5000ns
sleep 13 sec 5 nsec
PASS - slept 13s146848ns ~= 13s5ns
sleep -1 sec -1 nsec
sleep 0 sec -1 nsec
sleep 1 sec 1000000000 nsec
sleep 2 sec 1000000000 nsec
sleep -2147483647 sec -2147483647 nsec
sleep 1 sec 2147483647 nsec
sleep 0 sec 1075002478 nsec
At least one test FAILED
---
Changes in v2:
- add <unistd.h> to imports
- Link to v1: https://lore.kernel.org/r/[email protected]
--- .../conformance/interfaces/nanosleep/1-1.c | 16 ++++--------- .../conformance/interfaces/nanosleep/1-2.c | 16 ++++--------- .../conformance/interfaces/nanosleep/1-3.c | 16 ++++--------- .../conformance/interfaces/nanosleep/10000-1.c | 16 ++++--------- .../conformance/interfaces/nanosleep/2-1.c | 16 ++++--------- .../conformance/interfaces/nanosleep/3-2.c | 16 ++++--------- .../conformance/interfaces/nanosleep/7-2.c | 16 ++++--------- .../conformance/interfaces/nanosleep/helpers.h | 28 ++++++++++++++++++++++ 8 files changed, 56 insertions(+), 84 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c +4 -12
@@ -11,24 +11,16 @@#include <stdio.h> #include <time.h> #include "posixtest.h" - -#ifdef _POSIX_MONOTONIC_CLOCK -#define TEST_CLOCK CLOCK_MONOTONIC -#else -#define TEST_CLOCK CLOCK_REALTIME -#endif +#include "helpers.h" int main (void ) {struct timespec tssleepfor, tsstorage, tsbefore, tsafter;int sleepnsec =3 ;int slepts =0 , sleptns =0 ; +clockid_t test_clock =get_supported_clock (); -#ifndef _POSIX_MONOTONIC_CLOCK -printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); -#endif - -if (clock_gettime (TEST_CLOCK , &tsbefore) ==-1 ) { +if (clock_gettime (test_clock, &tsbefore) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
@@ -40,7 +32,7 @@ int main(void)return PTS_UNRESOLVED ; } -if (clock_gettime (TEST_CLOCK , &tsafter) ==-1 ) { +if (clock_gettime (test_clock, &tsafter) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c +4 -12
@@ -14,24 +14,16 @@#include <unistd.h> #include <sys/wait.h> #include "posixtest.h" - -#ifdef _POSIX_MONOTONIC_CLOCK -#define TEST_CLOCK CLOCK_MONOTONIC -#else -#define TEST_CLOCK CLOCK_REALTIME -#endif +#include "helpers.h" int main (void ) {struct timespec tssleepfor, tsstorage, tsbefore, tsafter;int sleepsec =30 ;int pid; +clockid_t test_clock =get_supported_clock (); -#ifndef _POSIX_MONOTONIC_CLOCK -printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); -#endif - -if (clock_gettime (TEST_CLOCK , &tsbefore) ==-1 ) { +if (clock_gettime (test_clock, &tsbefore) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
@@ -56,7 +48,7 @@ int main(void)perror ("Error waiting for child to exit\n" );return PTS_UNRESOLVED ; } -if (clock_gettime (TEST_CLOCK , &tsafter) ==-1 ) { +if (clock_gettime (test_clock, &tsafter) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c +4 -12
@@ -15,12 +15,7 @@#include <unistd.h> #include <sys/wait.h> #include "posixtest.h" - -#ifdef _POSIX_MONOTONIC_CLOCK -#define TEST_CLOCK CLOCK_MONOTONIC -#else -#define TEST_CLOCK CLOCK_REALTIME -#endif +#include "helpers.h" static void handler(int signo PTS_ATTRIBUTE_UNUSED) {
@@ -33,12 +28,9 @@ int main(void)int sleepsec =30 ;int pid;struct sigaction act; +clockid_t test_clock =get_supported_clock (); -#ifndef _POSIX_MONOTONIC_CLOCK -printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); -#endif - -if (clock_gettime (TEST_CLOCK , &tsbefore) ==-1 ) { +if (clock_gettime (test_clock, &tsbefore) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
@@ -74,7 +66,7 @@ int main(void)perror ("Error waiting for child to exit\n" );return PTS_UNRESOLVED ; } -if (clock_gettime (TEST_CLOCK , &tsafter) ==-1 ) { +if (clock_gettime (test_clock, &tsafter) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c +4 -12
@@ -18,12 +18,7 @@#include <time.h> #include <errno.h> #include "posixtest.h" - -#ifdef _POSIX_MONOTONIC_CLOCK -#define TEST_CLOCK CLOCK_MONOTONIC -#else -#define TEST_CLOCK CLOCK_REALTIME -#endif +#include "helpers.h" #define NUMVALID 6#define NUMINVALID 7
@@ -56,23 +51,20 @@ int main(void)int i;int failure =0 ;int slepts =0 , sleptns =0 ; - -#ifndef _POSIX_MONOTONIC_CLOCK -printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); -#endif +clockid_t test_clock =get_supported_clock ();for (i =0 ; i <NUMVALID ; i++) { tssleepfor.tv_sec = sleepvalid[i][0 ]; tssleepfor.tv_nsec = sleepvalid[i][1 ];printf ("sleep %d sec %d nsec\n" , (int )tssleepfor.tv_sec, (int )tssleepfor.tv_nsec); -if (clock_gettime (TEST_CLOCK , &tsbefore) ==-1 ) { +if (clock_gettime (test_clock, &tsbefore) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }if (nanosleep (&tssleepfor, &tsstorage) ==0 ) { -if (clock_gettime (TEST_CLOCK , &tsafter) ==-1 ) { +if (clock_gettime (test_clock, &tsafter) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c +4 -12
@@ -12,12 +12,7 @@#include <stdio.h> #include <time.h> #include "posixtest.h" - -#ifdef _POSIX_MONOTONIC_CLOCK -#define TEST_CLOCK CLOCK_MONOTONIC -#else -#define TEST_CLOCK CLOCK_REALTIME -#endif +#include "helpers.h" #define NUMINTERVALS 13int main (void )
@@ -30,12 +25,9 @@ int main(void)int i;int failure =0 ;int slepts, sleptns; +clockid_t test_clock =get_supported_clock (); -#ifndef _POSIX_MONOTONIC_CLOCK -printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); -#endif - -if (clock_gettime (TEST_CLOCK , &tsbefore) ==-1 ) { +if (clock_gettime (test_clock, &tsbefore) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
@@ -48,7 +40,7 @@ int main(void)return PTS_UNRESOLVED ; } -if (clock_gettime (TEST_CLOCK , &tsafter) ==-1 ) { +if (clock_gettime (test_clock, &tsafter) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c +4 -12
@@ -15,12 +15,7 @@#include <sys/wait.h> #include <stdlib.h> #include "posixtest.h" - -#ifdef _POSIX_MONOTONIC_CLOCK -#define TEST_CLOCK CLOCK_MONOTONIC -#else -#define TEST_CLOCK CLOCK_REALTIME -#endif +#include "helpers.h" #define SLEEPSEC 5
@@ -31,12 +26,9 @@ int main(void) {int pid, slepts;struct timespec tsbefore, tsafter; +clockid_t test_clock =get_supported_clock (); -#ifndef _POSIX_MONOTONIC_CLOCK -printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); -#endif - -if (clock_gettime (TEST_CLOCK , &tsbefore) !=0 ) { +if (clock_gettime (test_clock, &tsbefore) !=0 ) {perror ("clock_gettime() did not return success\n" );return PTS_UNRESOLVED ; }
@@ -84,7 +76,7 @@ int main(void)return PTS_FAIL ; } -if (clock_gettime (TEST_CLOCK , &tsafter) ==-1 ) { +if (clock_gettime (test_clock, &tsafter) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c +4 -12
@@ -16,12 +16,7 @@#include <sys/wait.h> #include <stdlib.h> #include "posixtest.h" - -#ifdef _POSIX_MONOTONIC_CLOCK -#define TEST_CLOCK CLOCK_MONOTONIC -#else -#define TEST_CLOCK CLOCK_REALTIME -#endif +#include "helpers.h" #define CHILDSUCCESS 1#define CHILDFAILURE 0
@@ -39,12 +34,9 @@ int main(void)int sleepsec =30 ;int pid;struct sigaction act; +clockid_t test_clock =get_supported_clock (); -#ifndef _POSIX_MONOTONIC_CLOCK -printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); -#endif - -if (clock_gettime (TEST_CLOCK , &tsbefore) ==-1 ) { +if (clock_gettime (test_clock, &tsbefore) ==-1 ) {perror ("Error in clock_gettime()\n" );return PTS_UNRESOLVED ; }
@@ -70,7 +62,7 @@ int main(void)return CHILDFAILURE ; } -if (clock_gettime (TEST_CLOCK , &tsafter) ==-1 ) { +if (clock_gettime (test_clock, &tsafter) ==-1 ) {perror ("Error in clock_gettime()\n" );return CHILDFAILURE ; }
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/helpers.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* Copyright (c) 2026, Linux Test Project +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef NANOSLEEP_HELPERS_H +#define NANOSLEEP_HELPERS_H + +#include <stdio.h> +#include <unistd.h> +#include <time.h> + +static inline clockid_t get_supported_clock (void ) +{ +#ifdef _POSIX_MONOTONIC_CLOCK +if (_POSIX_MONOTONIC_CLOCK >0 ) +return CLOCK_MONOTONIC ; + +if (!_POSIX_MONOTONIC_CLOCK &&sysconf (_SC_MONOTONIC_CLOCK) >0 ) +return CLOCK_MONOTONIC ; +#endif + +printf ("CLOCK_MONOTONIC unavailable, test may fail due to " +"external clock adjustments\n" ); +return CLOCK_REALTIME ; +} + +#endif /* NANOSLEEP_HELPERS_H */
---
base-commit: 620d596c79493af089ba6a1c4dc79efcc76a67c7
change-id: 20260416-fix_nanosleep_include-edcc8f01f0e0
Best regards,
Signature · 5 lines
Andrea Cervesato <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Andrea,
Quoted 6 lines · Andrea Cervesato
nanosleep: use POSIX runtime detection for
CLOCK_MONOTONICtestcases/open_posix_testsuite/conformance/interfaces/nanosleep/helpers.h new file +4
-0+static inline clockid_t get_supported_clock (void ) +{ +#ifdef _POSIX_MONOTONIC_CLOCK +if (_POSIX_MONOTONIC_CLOCK >0 )
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/helpers.h line 26
Quoted 10 lines
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/helpers.h new file +10
-0+return CLOCK_MONOTONIC ; + +if (!_POSIX_MONOTONIC_CLOCK &&sysconf (_SC_MONOTONIC_CLOCK) >0 ) +return CLOCK_MONOTONIC ; +#endif + +printf ("CLOCK_MONOTONIC unavailable, test may fail due to " +"external clock adjustments\n" ); +return CLOCK_REALTIME ; +}
Correct POSIX runtime detection — handles all three _POSIX_MONOTONIC_CLOCK states (>0, ==0, undefined) properly, and the unistd.h include in helpers.h closes the silent fallback that affected 1-1.c, 2-1.c, and 10000-1.c.
---
Note:
Our agent completed the review of the patch.
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
Hi Andrea,
...
Quoted 17 lines
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/helpers.h new file +14
-0@@ -0,0 +1,28 @@ +/* +* Copyright (c) 2026, Linux Test Project +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef NANOSLEEP_HELPERS_H +#define NANOSLEEP_HELPERS_H + +#include <stdio.h> +#include <unistd.h> +#include <time.h> + +static inline clockid_t get_supported_clock (void ) +{
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/helpers.h line 24
Quoted 10 lines
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/helpers.h new file +10
-0+#ifdef _POSIX_MONOTONIC_CLOCK +if (_POSIX_MONOTONIC_CLOCK >0 ) +return CLOCK_MONOTONIC ; + +if (!_POSIX_MONOTONIC_CLOCK &&sysconf (_SC_MONOTONIC_CLOCK) >0 ) +return CLOCK_MONOTONIC ; +#endif + +printf ("CLOCK_MONOTONIC unavailable, test may fail due to " +"external clock adjustments\n" );
+1 for warning. But please don't split string just due few chars over 100.
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/helpers.h line 25
Quoted 1 line
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/helpers.h new file +1
-0+return CLOCK_REALTIME ;
I've suggested [1] in clock_settime [2] to move this function to newly created testcases/open_posix_testsuite/include/clock.h, so that clock_settime/helpers.h can also use it.
Kind regards,
Petr
[1] https://lore.kernel.org/ltp/20260417125322.GA419619@pevik/
[2] https://patchwork.ozlabs.org/project/ltp/patch/[email protected]/
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp