clock_settime: Detect external clock adjustments via CLOCK_MONOTONIC
PATCH v2 1/1 clock_settime: Detect external clock adjustments via CLOCK_MONOTONIC
From: Andrea Cervesato <[email protected]>
These tests manipulate CLOCK_REALTIME to verify timer and clock_nanosleep behavior, but NTP or VM time sync can also adjust CLOCK_REALTIME during the test, causing timers to fire at wrong times and producing spurious failures.
Use CLOCK_MONOTONIC as a sideband check to detect external interference. If the monotonic elapsed time is anomalous, report UNTESTED instead of a false FAIL. Guard the checks with _POSIX_MONOTONIC_CLOCK since CLOCK_MONOTONIC is optional in POSIX.1-2001.
---
I used Claude to generate the right patches to fix sporadic issues in the clock_settime testing suite. We tried many attempt for fixing it, but the CLOCK_REALTIME usage seems to be the real issue in here. The idea is that
we use internal Openposix mechanisms to skip tests if CLOCK_MONOTONIC
usage spots a possible interference from external tools.
---
Changes in v2:
- print a message if CLOCK_MONOTONIC is not available
- always verify that CLOCK_MONOTONIC is available
- Link to v1: https://lore.kernel.org/r/[email protected]
--- .../conformance/interfaces/clock_settime/4-1.c | 36 ++++++++++++++++++ .../conformance/interfaces/clock_settime/5-1.c | 43 +++++++++++++++++++++- .../conformance/interfaces/clock_settime/5-2.c | 43 +++++++++++++++++++++- .../conformance/interfaces/clock_settime/7-1.c | 38 +++++++++++++++++++ .../conformance/interfaces/clock_settime/7-2.c | 41 ++++++++++++++++++++- 5 files changed, 198 insertions(+), 3 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c +36 -0
@@ -42,6 +42,9 @@ int main(void) {struct sigevent ev;struct timespec tpT0, tpT2, tpreset; +#ifdef _POSIX_MONOTONIC_CLOCK +struct timespec mono_before, mono_after; +#endif struct itimerspec its;timer_t tid;int delta;
@@ -55,6 +58,10 @@ int main(void)return PTS_UNTESTED ; } +#ifndef _POSIX_MONOTONIC_CLOCK +printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); +#endif +/* * set up sigevent for timer * set up signal set for sigwait
@@ -82,6 +89,13 @@ int main(void)return PTS_UNRESOLVED ; } +#ifdef _POSIX_MONOTONIC_CLOCK +if (clock_gettime (CLOCK_MONOTONIC , &mono_before) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } +#endif +if (timer_create (CLOCK_REALTIME , &ev, &tid) !=0 ) {perror ("timer_create() did not return success\n" );return PTS_UNRESOLVED ;
@@ -120,6 +134,28 @@ int main(void) tpreset.tv_sec += tpT2.tv_sec - tpT0.tv_sec;setBackTime (tpreset); +#ifdef _POSIX_MONOTONIC_CLOCK +if (clock_gettime (CLOCK_MONOTONIC , &mono_after) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } + +/* +* The expected monotonic elapsed time is SLEEPTIME + TIMEROFFSET +* since after resetting the clock, the timer must wait another +* full TIMEROFFSET. If monotonic time is much shorter, an external +* clock adjustment (NTP, VM sync) interfered with the test. +*/ +if (mono_after.tv_sec - mono_before.tv_sec < +SLEEPTIME +TIMEROFFSET -ACCEPTABLEDELTA ) { +printf ("UNTESTED: external clock adjustment detected " +"(monotonic elapsed %ds, expected ~%ds)\n" , + (int )(mono_after.tv_sec - mono_before.tv_sec), +SLEEPTIME +TIMEROFFSET ); +return PTS_UNTESTED ; + } +#endif +printf ("delta: %d\n" , delta);if ((delta <=ACCEPTABLEDELTA ) && (delta >=0 )) {printf ("Test PASSED\n" );
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-1.c +42 -1
70 lines not on this page · raw message
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-2.c +42 -1
70 lines not on this page · raw message
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-1.c +38 -0
@@ -36,6 +36,9 @@int main (void ) {struct timespec tsT0, tsT1; +#ifdef _POSIX_MONOTONIC_CLOCK +struct timespec mono_before, mono_after; +#endif int pid;/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */
@@ -44,11 +47,22 @@ int main(void)return PTS_UNTESTED ; } +#ifndef _POSIX_MONOTONIC_CLOCK +printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); +#endif +if (clock_gettime (CLOCK_REALTIME , &tsT0) !=0 ) {perror ("clock_gettime() did not return success\n" );return PTS_UNRESOLVED ; } +#ifdef _POSIX_MONOTONIC_CLOCK +if (clock_gettime (CLOCK_MONOTONIC , &mono_before) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } +#endif +if ((pid =fork ()) ==0 ) {/* child here */ int flags =0 ;
@@ -103,6 +117,30 @@ int main(void)getBeforeTime (&tsreset);// get current time tsreset.tv_sec +=SMALLTIME ;setBackTime (tsreset); + +#ifdef _POSIX_MONOTONIC_CLOCK +if (clock_gettime (CLOCK_MONOTONIC , &mono_after) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } + +/* +* Expected monotonic elapsed time is SMALLTIME + SLEEPOFFSET +* since after resetting the clock, the child must wait +* another full SLEEPOFFSET. If monotonic time is much +* shorter, an external clock adjustment (NTP, VM sync) +* interfered with the test. +*/ +if (mono_after.tv_sec - mono_before.tv_sec < +SMALLTIME +SLEEPOFFSET -ACCEPTABLEDELTA -1 ) { +printf ("UNTESTED: external clock adjustment detected " +"(monotonic elapsed %ds, expected ~%ds)\n" , + (int )(mono_after.tv_sec - mono_before.tv_sec), +SMALLTIME +SLEEPOFFSET ); +return PTS_UNTESTED ; + } +#endif +if (WIFEXITED (i) &&WEXITSTATUS (i)) {printf ("Test PASSED\n" );return PTS_PASS ;
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-2.c +40 -1
72 lines not on this page · raw message
---
base-commit: 4688c20c01eece869b59e05ca3dd68c43e0d6af7
change-id: 20260331-clock_settime_fix-8e4117dcb609
Best regards,
Signature · 5 lines
Andrea Cervesato <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 9 lines
These tests manipulate
CLOCK_REALTIMEto verify timer andclock_nanosleepbehavior, but NTP or VM time sync can also adjustCLOCK_REALTIMEduring the test, causing timers to fire at wrong times and producing spurious failures.Use
CLOCK_MONOTONICas a sideband check to detect external interference. If the monotonic elapsed time is anomalous, report UNTESTED instead of a false FAIL. Guard the checks with_POSIX_MONOTONIC_CLOCKsinceCLOCK_MONOTONICis optional in POSIX.1-2001.
Maybe we can retry a few times before we give up and report untested?
Quoted 75 lines
Co-authored-by Claude <[email protected]>Signed-off-by Andrea Cervesato <[email protected]>---
I used Claude to generate the right patches to fix sporadic issues in theclock_settimetesting suite. We tried many attempt for fixing it, but theCLOCK_REALTIMEusage seems to be the real issue in here. The idea is that
we use internal Openposix mechanisms to skip tests ifCLOCK_MONOTONIC
usage spots a possible interference from external tools.
---
Changes in v2:
- print a message ifCLOCK_MONOTONICis not available
- always verify thatCLOCK_MONOTONICis available
- Link to v1: https://lore.kernel.org/r/[email protected]--- .../conformance/interfaces/clock_settime/4-1.c | 36 ++++++++++++++++++ .../conformance/interfaces/clock_settime/5-1.c | 43 +++++++++++++++++++++- .../conformance/interfaces/clock_settime/5-2.c | 43 +++++++++++++++++++++- .../conformance/interfaces/clock_settime/7-1.c | 38 +++++++++++++++++++ .../conformance/interfaces/clock_settime/7-2.c | 41 ++++++++++++++++++++- 5 files changed, 198 insertions(+), 3 deletions(-)testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c +25
-0@@ -42,6 +42,9 @@ int main(void) {struct sigevent ev;struct timespec tpT0, tpT2, tpreset; +#ifdef _POSIX_MONOTONIC_CLOCK +struct timespec mono_before, mono_after; +#endif struct itimerspec its;timer_t tid;int delta;@@ -55,6 +58,10 @@ int main(void)return PTS_UNTESTED ; } +#ifndef _POSIX_MONOTONIC_CLOCK +printf ("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n" ); +#endif +/* * set up sigevent for timer * set up signal set for sigwait @@ -82,6 +89,13 @@ int main(void)return PTS_UNRESOLVED ; } +#ifdef _POSIX_MONOTONIC_CLOCK +if (clock_gettime (CLOCK_MONOTONIC , &mono_before) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } +#endif +if (timer_create (CLOCK_REALTIME , &ev, &tid) !=0 ) {perror ("timer_create() did not return success\n" );return PTS_UNRESOLVED ;@@ -120,6 +134,28 @@ int main(void) tpreset.tv_sec += tpT2.tv_sec - tpT0.tv_sec;setBackTime (tpreset); +#ifdef _POSIX_MONOTONIC_CLOCK +if (clock_gettime (CLOCK_MONOTONIC , &mono_after) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } + +/* +* The expected monotonic elapsed time is SLEEPTIME + TIMEROFFSET +* since after resetting the clock, the timer must wait another +* full TIMEROFFSET. If monotonic time is much shorter, an external +* clock adjustment (NTP, VM sync) interfered with the test.
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c int main(void) · line 157
Quoted 10 lines
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c +10
-0+*/ +if (mono_after.tv_sec - mono_before.tv_sec < +SLEEPTIME +TIMEROFFSET -ACCEPTABLEDELTA ) { +printf ("UNTESTED: external clock adjustment detected " +"(monotonic elapsed %ds, expected ~%ds)\n" , + (int )(mono_after.tv_sec - mono_before.tv_sec), +SLEEPTIME +TIMEROFFSET ); +return PTS_UNTESTED ; + } +#endif
This is a bit more than a line or two, maybe we can put this into a common header as a static inline functions instead?
Something as having a function to start measuring the time and to return true of false based on if the measured interval lenght is as expected?
e.g.:
static inline void pts_mono_time_start(void);
static inline bool pts_mono_time_end(unsigned int expected_delta);
Then we could loop the actual test a few times while pts_mon_time_end() returns false.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 6 lines
Hi!
Quoted 4 lines
These tests manipulate
CLOCK_REALTIMEto verify timer andclock_nanosleepbehavior, but NTP or VM time sync can also adjustCLOCK_REALTIMEduring the test, causing timers to fire at wrong times and producing spurious failures.
Quoted 6 lines
Quoted 4 lines
Use
CLOCK_MONOTONICas a sideband check to detect external interference. If the monotonic elapsed time is anomalous, report UNTESTED instead of a false FAIL. Guard the checks with_POSIX_MONOTONIC_CLOCKsinceCLOCK_MONOTONICis optional in POSIX.1-2001.Maybe we can retry a few times before we give up and report untested?
+1.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp