[PATCH] tests: posix: use cpu clock for sleep
Florian Weimer
fweimer@redhat.com
Mon Feb 2 15:42:03 GMT 2026
* Yury Khrustalev:
> On some emulated targets sleep may result in inconsistent wait
> times which will lead to the failure of the tst-chmod test.
>
> To account for this we use the CLOCK_PROCESS_CPUTIME_ID clock ID
> while also consuming CPU time by repeatedly calling clock_gettime.
>
> ---
> Passes regression on aarch64 and x86. OK for trunk?
> Base commit: 67494ceb5d
>
> ---
> posix/tst-chmod.c | 19 +++++++++++++++++--
> 1 file changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/posix/tst-chmod.c b/posix/tst-chmod.c
> index 80a69dddd4..620055080c 100644
> --- a/posix/tst-chmod.c
> +++ b/posix/tst-chmod.c
> @@ -26,6 +26,7 @@
> #include <string.h>
> #include <unistd.h>
> #include <sys/stat.h>
> +#include <time.h>
>
> #include <support/xunistd.h>
>
> @@ -37,6 +38,20 @@
> goto fail; \
> } while (0)
>
> +/* On some emulated targets sleep may result in inconsistent wait
> + times which will lead to the failure of this test. To account
> + for this we use the CLOCK_PROCESS_CPUTIME_ID clock ID while
> + also consuming CPU time by repeatedly calling clock_gettime. */
> +static void
> +test_sleep (unsigned long seconds)
> +{
> + struct timespec ts;
> + clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
> + unsigned long end = ts.tv_sec + seconds;
> + while (ts.tv_sec < end)
> + clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
> +}
This can wait a very short time only if tv_nsec is close to the maximum.
How accurate is CLOCK_PROCESS_CPUTIME_ID on emulated targets? Should we
busy-wait and also wait for the wall clock time to increase?
Thanks,
Florian
More information about the Libc-alpha
mailing list