Re: gettime-res failure on Solaris 11/SPARC

2024-03-26 Thread Bruno Haible
Paul Eggert wrote:
> > It doesn't work because it takes only 32 samples of current_timespec ().
> > In order to work reliably it would need 10 million samples.
> 
> Thanks for the fix. My puzzlement was more why one needs 10 million 
> samples. Perhaps it's possible to alter the test somewhat so that 32 
> samples would be enough, if we'd take them more cleverly.

Possibly. But even then, we would probably need to wait ca. 1 sec until
we get the "right" samples. And waiting such a long time is not OK
in a runtime function (as opposed to a configure test).

Bruno







Re: gettime-res failure on Solaris 11/SPARC

2024-03-26 Thread Paul Eggert

On 3/26/24 05:16, Bruno Haible wrote:

It doesn't work because it takes only 32 samples of current_timespec ().
In order to work reliably it would need 10 million samples.


Thanks for the fix. My puzzlement was more why one needs 10 million 
samples. Perhaps it's possible to alter the test somewhat so that 32 
samples would be enough, if we'd take them more cleverly. However, if 
this problem occurs only on Solaris it's probably not worth the effort 
and your fix is fine.




Re: gettime-res failure on Solaris 11/SPARC

2024-03-26 Thread Bruno Haible
Paul Eggert wrote:
> > What shall we do with this one? Override gettime_res() to return 1 ns
> > instead of 5 ns? Or adapt the unit test?
> 
> Perhaps overriding is best. I'm a bit puzzled why the current code 
> doesn't work, though.

It doesn't work because it takes only 32 samples of current_timespec ().
In order to work reliably it would need 10 million samples. Which is,
of course, not feasible :-)

I'm committing this override:


2024-03-26  Bruno Haible  

gettime-res: Fix test failure on Solaris 11.4/SPARC.
* lib/gettime-res.c (gettime_res): On Solaris/SPARC, just return 1 ns.

diff --git a/lib/gettime-res.c b/lib/gettime-res.c
index afc828a5fb..8bde4122a7 100644
--- a/lib/gettime-res.c
+++ b/lib/gettime-res.c
@@ -38,16 +38,28 @@ gcd (long int a, long int b)
 long int
 gettime_res (void)
 {
+#if defined __sun && defined __sparc
+  /* On Solaris 11.4/SPARC (with a SPARC-M8 CPU)
+ clock_getres (CLOCK_REALTIME, ...) returns a resolution of 100 ns,
+ and clock_gettime (CLOCK_REALTIME) returns a multiple of 5 ns with a
+ probability of ca. 1 - 1/30.  Thus the heuristic below with the
+ 32 samples guesses a resolution of 5 ns with a probability of ca.
+ 1 - 1/1.  But occasionally clock_gettime (CLOCK_REALTIME) returns
+ only a multiple of 1 ns.
+ Simple guesswork is not enough to cope with this irregular behaviour.
+ Therefore, here is an override.  */
+  return 1;
+#else
   struct timespec res;
-#if defined CLOCK_REALTIME && HAVE_CLOCK_GETRES
+# if defined CLOCK_REALTIME && HAVE_CLOCK_GETRES
   clock_getres (CLOCK_REALTIME, );
-#elif defined HAVE_TIMESPEC_GETRES
+# elif defined HAVE_TIMESPEC_GETRES
   timespec_getres (, TIME_UTC);
-#else
+# else
   /* Guess high and let the later code deduce better.  */
   res.tv_sec = 1;
   res.tv_nsec = 0;
-#endif
+# endif
 
   /* On all Gnulib platforms the following calculations do not overflow.  */
 
@@ -83,6 +95,7 @@ gettime_res (void)
 }
 
   return r;
+#endif
 }
 
 /*






Re: gettime-res failure on Solaris 11/SPARC

2024-03-25 Thread Paul Eggert

On 3/25/24 18:10, Bruno Haible wrote:

What shall we do with this one? Override gettime_res() to return 1 ns
instead of 5 ns? Or adapt the unit test?


Perhaps overriding is best. I'm a bit puzzled why the current code 
doesn't work, though.