* [LTP] [PATCH] nanosleep: Use CLOCK_MONOTONIC for elapsed time measurement
@ 2026-03-31 11:48 Andrea Cervesato
2026-03-31 12:14 ` Cyril Hrubis
0 siblings, 1 reply; 5+ messages in thread
From: Andrea Cervesato @ 2026-03-31 11:48 UTC (permalink / raw)
To: Linux Test Project
From: Andrea Cervesato <andrea.cervesato@suse.com>
All nanosleep POSIX conformance tests used CLOCK_REALTIME to measure
elapsed sleep duration. CLOCK_REALTIME can jump due to NTP corrections,
VM time sync, or other wall-clock adjustments, causing sporadic test
failures where measured time far exceeds expected sleep duration.
Switch to CLOCK_MONOTONIC which is immune to wall-clock changes and is
the correct clock for measuring elapsed durations.
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c | 4 ++--
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c | 4 ++--
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c | 4 ++--
.../open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c | 4 ++--
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c | 4 ++--
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c | 4 ++--
testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c | 4 ++--
7 files changed, 14 insertions(+), 14 deletions(-)
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c
index 687d01e288d914fbbdc18287746f71296c7a4327..d76451e3438e15d44ab2c2b8d0b349e666daec8c 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c
@@ -18,7 +18,7 @@ int main(void)
int sleepnsec = 3;
int slepts = 0, sleptns = 0;
- if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -30,7 +30,7 @@ int main(void)
return PTS_UNRESOLVED;
}
- if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c
index 76f31982508dc87a4f400022cd46fe766071f27d..f72cbcfbbe06a0059bbc17ad617ca76c65651a93 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c
@@ -21,7 +21,7 @@ int main(void)
int sleepsec = 30;
int pid;
- if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -46,7 +46,7 @@ int main(void)
perror("Error waiting for child to exit\n");
return PTS_UNRESOLVED;
}
- if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c
index 31ac00858018c3741758d0b5f2bf28f87aa6c1bf..07ce767123443cea2754c8b5ff2a20ed536cbfec 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c
@@ -28,7 +28,7 @@ int main(void)
int pid;
struct sigaction act;
- if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -64,7 +64,7 @@ int main(void)
perror("Error waiting for child to exit\n");
return PTS_UNRESOLVED;
}
- if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c
index 883885df434fb4ba3bb555dd565880f14623a984..8bd372f590314faa8f15fccbe5325e9fa31a6f95 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c
@@ -56,13 +56,13 @@ int main(void)
tssleepfor.tv_nsec = sleepvalid[i][1];
printf("sleep %d sec %d nsec\n",
(int)tssleepfor.tv_sec, (int)tssleepfor.tv_nsec);
- if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
if (nanosleep(&tssleepfor, &tsstorage) == 0) {
- if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c
index cce3941e7be7212ea286647ec601d3ecbb9bb85a..f44d0f938a586defa3ef244371f1850865801afa 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c
@@ -25,7 +25,7 @@ int main(void)
int failure = 0;
int slepts, sleptns;
- if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -38,7 +38,7 @@ int main(void)
return PTS_UNRESOLVED;
}
- if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c
index bfc271edf25f9cd0f343eef11b335d5da20eb646..201ab72a5d4a5a345839fb87f4f1621c70a5bb36 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c
@@ -26,7 +26,7 @@ int main(void)
int pid, slepts;
struct timespec tsbefore, tsafter;
- if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsbefore) != 0) {
perror("clock_gettime() did not return success\n");
return PTS_UNRESOLVED;
}
@@ -74,7 +74,7 @@ int main(void)
return PTS_FAIL;
}
- if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c
index caf4470e4a26b27ec79deed27c147e2feceffdf8..eab419591ab0c27a5d2dfb556cf2f13f7cc25e6e 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c
@@ -34,7 +34,7 @@ int main(void)
int pid;
struct sigaction act;
- if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -60,7 +60,7 @@ int main(void)
return CHILDFAILURE;
}
- if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return CHILDFAILURE;
}
---
base-commit: 4688c20c01eece869b59e05ca3dd68c43e0d6af7
change-id: 20260331-nanosleep_posix_fix-66162acca1ed
Best regards,
--
Andrea Cervesato <andrea.cervesato@suse.com>
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [LTP] [PATCH] nanosleep: Use CLOCK_MONOTONIC for elapsed time measurement
2026-03-31 11:48 [LTP] [PATCH] nanosleep: Use CLOCK_MONOTONIC for elapsed time measurement Andrea Cervesato
@ 2026-03-31 12:14 ` Cyril Hrubis
2026-03-31 12:20 ` Andrea Cervesato via ltp
0 siblings, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2026-03-31 12:14 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: Linux Test Project
Hi!
> All nanosleep POSIX conformance tests used CLOCK_REALTIME to measure
> elapsed sleep duration. CLOCK_REALTIME can jump due to NTP corrections,
> VM time sync, or other wall-clock adjustments, causing sporadic test
> failures where measured time far exceeds expected sleep duration.
>
> Switch to CLOCK_MONOTONIC which is immune to wall-clock changes and is
> the correct clock for measuring elapsed durations.
Unfortunately CLOCK_MONOTONIC is optional in POSIX so we have to check
if it's present at least at compile time with something as:
#if _POSIX_MONOTONIC_CLOCK
# define TEST_CLOCK CLOCK_MONOTONIC
#else
# define TEST_CLOCK CLOCK_REALTIME
#endif
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH] nanosleep: Use CLOCK_MONOTONIC for elapsed time measurement
2026-03-31 12:14 ` Cyril Hrubis
@ 2026-03-31 12:20 ` Andrea Cervesato via ltp
2026-03-31 12:23 ` Cyril Hrubis
0 siblings, 1 reply; 5+ messages in thread
From: Andrea Cervesato via ltp @ 2026-03-31 12:20 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: Linux Test Project
Hi Cyril,
> Hi!
> > All nanosleep POSIX conformance tests used CLOCK_REALTIME to measure
> > elapsed sleep duration. CLOCK_REALTIME can jump due to NTP corrections,
> > VM time sync, or other wall-clock adjustments, causing sporadic test
> > failures where measured time far exceeds expected sleep duration.
> >
> > Switch to CLOCK_MONOTONIC which is immune to wall-clock changes and is
> > the correct clock for measuring elapsed durations.
>
> Unfortunately CLOCK_MONOTONIC is optional in POSIX so we have to check
> if it's present at least at compile time with something as:
>
> #if _POSIX_MONOTONIC_CLOCK
> # define TEST_CLOCK CLOCK_MONOTONIC
> #else
> # define TEST_CLOCK CLOCK_REALTIME
> #endif
good point. If it was a non-posix project it was called non-openposix :-)
Do you think that check is enough in this case? Or we should just mark
this test (if we really want to keep it 100% posix) as XFAIL ? I really
didn't see any option but introducing CLOCK_MONOTONIC checks.
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH] nanosleep: Use CLOCK_MONOTONIC for elapsed time measurement
2026-03-31 12:20 ` Andrea Cervesato via ltp
@ 2026-03-31 12:23 ` Cyril Hrubis
2026-03-31 12:28 ` Andrea Cervesato via ltp
0 siblings, 1 reply; 5+ messages in thread
From: Cyril Hrubis @ 2026-03-31 12:23 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: Linux Test Project
Hi!
> > #if _POSIX_MONOTONIC_CLOCK
> > # define TEST_CLOCK CLOCK_MONOTONIC
> > #else
> > # define TEST_CLOCK CLOCK_REALTIME
> > #endif
>
> good point. If it was a non-posix project it was called non-openposix :-)
>
> Do you think that check is enough in this case? Or we should just mark
> this test (if we really want to keep it 100% posix) as XFAIL ? I really
> didn't see any option but introducing CLOCK_MONOTONIC checks.
Maybe we can can print a hint that we are falling back to CLOCK_REALTIME
in case that CLOCK_MONOTNIC is not there and proceed with the test.
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [LTP] [PATCH] nanosleep: Use CLOCK_MONOTONIC for elapsed time measurement
2026-03-31 12:23 ` Cyril Hrubis
@ 2026-03-31 12:28 ` Andrea Cervesato via ltp
0 siblings, 0 replies; 5+ messages in thread
From: Andrea Cervesato via ltp @ 2026-03-31 12:28 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: Linux Test Project
> Maybe we can can print a hint that we are falling back to CLOCK_REALTIME
> in case that CLOCK_MONOTNIC is not there and proceed with the test.
Ok, I will also gate the CLOCK_MONOTONIC part in the patch for
clock_settime since it's having the same issue.
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-03-31 12:29 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-31 11:48 [LTP] [PATCH] nanosleep: Use CLOCK_MONOTONIC for elapsed time measurement Andrea Cervesato
2026-03-31 12:14 ` Cyril Hrubis
2026-03-31 12:20 ` Andrea Cervesato via ltp
2026-03-31 12:23 ` Cyril Hrubis
2026-03-31 12:28 ` Andrea Cervesato via ltp
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox