clock_settime: Detect external clock adjustments via CLOCK_MONOTONIC
PATCH v3 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 sporadic failures.
Add pts_mono_time_start()/pts_mono_time_check() helpers to detect external clock interference using CLOCK_MONOTONIC. When interference is detected, the test retries up to PTS_MONO_MAX_RETRIES times before reporting UNTESTED. Guard 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 v3:
- cycle multiple times before failing the tests
- add helpers functions for CLOCK_MONOTONIC checks
- Link to v2: https://lore.kernel.org/r/[email protected]
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 | 82 +++++++++------- .../conformance/interfaces/clock_settime/5-1.c | 67 ++++++++----- .../conformance/interfaces/clock_settime/5-2.c | 57 +++++++---- .../conformance/interfaces/clock_settime/7-1.c | 90 ++++++++++-------- .../conformance/interfaces/clock_settime/7-2.c | 105 ++++++++++++--------- .../conformance/interfaces/clock_settime/helpers.h | 47 +++++++++ .../conformance/interfaces/nanosleep/7-1.c | 50 +++++----- 7 files changed, 308 insertions(+), 190 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c +47 -35
@@ -47,7 +47,7 @@ int main(void)int delta;int sig;sigset_t set; -int flags =0 ; +int attempt;/* Check that we're root...can't call clock_settime with CLOCK_REALTIME otherwise */ if (geteuid () !=0 ) {
@@ -77,49 +77,61 @@ int main(void)return PTS_UNRESOLVED ; } -if (clock_gettime (CLOCK_REALTIME , &tpT0) !=0 ) { -perror ("clock_gettime() was not successful\n" ); -return PTS_UNRESOLVED ; - } -if (timer_create (CLOCK_REALTIME , &ev, &tid) !=0 ) {perror ("timer_create() did not return success\n" );return PTS_UNRESOLVED ; } - flags |=TIMER_ABSTIME ; - its.it_interval.tv_sec =0 ; - its.it_interval.tv_nsec =0 ; - its.it_value.tv_sec = tpT0.tv_sec +TIMEROFFSET ; - its.it_value.tv_nsec = tpT0.tv_nsec; -if (timer_settime (tid, flags, &its,NULL ) !=0 ) { -perror ("timer_settime() did not return success\n" ); -return PTS_UNRESOLVED ; - } - -sleep (SLEEPTIME ); -getBeforeTime (&tpreset); -if (clock_settime (CLOCK_REALTIME , &tpT0) !=0 ) { -perror ("clock_settime() was not successful" ); -return PTS_UNRESOLVED ; - } - -if (sigwait (&set, &sig) ==-1 ) { -perror ("sigwait() was not successful\n" ); -return PTS_UNRESOLVED ; +for (attempt =0 ; attempt <PTS_MONO_MAX_RETRIES ; attempt++) { +if (clock_gettime (CLOCK_REALTIME , &tpT0) !=0 ) { +perror ("clock_gettime() was not successful\n" ); +return PTS_UNRESOLVED ; + } + +pts_mono_time_start (); + + its.it_interval.tv_sec =0 ; + its.it_interval.tv_nsec =0 ; + its.it_value.tv_sec = tpT0.tv_sec +TIMEROFFSET ; + its.it_value.tv_nsec = tpT0.tv_nsec; +if (timer_settime (tid,TIMER_ABSTIME , &its,NULL ) !=0 ) { +perror ("timer_settime() did not return success\n" ); +return PTS_UNRESOLVED ; + } + +sleep (SLEEPTIME ); +getBeforeTime (&tpreset); +if (clock_settime (CLOCK_REALTIME , &tpT0) !=0 ) { +perror ("clock_settime() was not successful" ); +return PTS_UNRESOLVED ; + } + +if (sigwait (&set, &sig) ==-1 ) { +perror ("sigwait() was not successful\n" ); +return PTS_UNRESOLVED ; + } + +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; + +// add back time waited to reset value and reset time + tpreset.tv_sec += tpT2.tv_sec - tpT0.tv_sec; +setBackTime (tpreset); + +if (pts_mono_time_check (SLEEPTIME +TIMEROFFSET )) +break ; } -if (clock_gettime (CLOCK_REALTIME , &tpT2) !=0 ) { -printf ("clock_gettime() was not successful\n" ); -return PTS_UNRESOLVED ; +if (attempt ==PTS_MONO_MAX_RETRIES ) { +printf ("UNTESTED: persistent clock interference after %d attempts\n" , +PTS_MONO_MAX_RETRIES ); +return PTS_UNTESTED ; } - 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; -setBackTime (tpreset); -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 +43 -24
91 lines not on this page · raw message
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-2.c +39 -18
80 lines not on this page · raw message
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-1.c +49 -41
129 lines not on this page · raw message
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-2.c +62 -43
147 lines not on this page · raw message
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +47 -0
56 lines not on this page · raw message
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-1.c +21 -29
73 lines not on this page · raw message
---
base-commit: f992f6e25fce22dd37b37ec324aa1b1b26b3204f
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