[PATCH] tests: posix: use cpu clock for sleep

Yury Khrustalev yury.khrustalev@arm.com
Mon Feb 2 17:41:49 GMT 2026


On Mon, Feb 02, 2026 at 04:42:03PM +0100, Florian Weimer wrote:
> * 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.

Right, good point, I should amend this code to make it more accurate in
terms of nanoseconds.

> How accurate is CLOCK_PROCESS_CPUTIME_ID on emulated targets?

I thought that accuracy was not very important for this test as long as 
the delay is substantial for stat to see time difference.

> Should we
> busy-wait and also wait for the wall clock time to increase?

Calling clock_gettime() is a sort of busy-waiting. This functions is
supposed to work very much like sleep() on any normal system in terms
of observable time delays (save for nanosecond inaccuracy).

Thanks,
Yury



More information about the Libc-alpha mailing list