bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: speed up test-stat-time


From: Eric Blake
Subject: Re: speed up test-stat-time
Date: Tue, 13 Oct 2009 15:19:33 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

Eric Blake <ebb9 <at> byu.net> writes:

> +/* Sleep long enough to notice a timestamp difference on the file
> +   system in the current directory.  */
> +static void
> +nap (void)
> +{
...
> +        delay = 2000000;
> +    }
> +  usleep (delay);

Well I feel stupid.  POSIX 2008 withdrew usleep in favor of nanosleep because 
it is a better interface: for starters, nanosleep can reliably request 
intervals longer than one second while usleep cannot.  POSIX 2001 is clear that 
usleep (1000000) may fail with EINVAL, and modern mingw happens to be one of 
those systems (apparently, mingw has implemented usleep since the time that 
Bruno first documented in pastposix-functions/usleep.texi that it was missing; 
also, looking at the assembly, it looks like mingw fails to set errno to 
EINVAL, but just blindly returns without sleeping).  But using nanosleep is 
problematic in that it requires external libraries on some platforms, so usleep 
is still preferable in unit tests.  So, now that I've actually tested stat-time-
tests on mingw, I will be applying this.


From: Eric Blake <address@hidden>
Date: Tue, 13 Oct 2009 09:16:16 -0600
Subject: [PATCH] test-stat-time: port to mingw

* tests/test-stat-time.c (force_unlink): Return a value.
(test_ctime) [W32]: Fix compilation error.
(nap): Don't call usleep with too large an argument.  Use
force_unlink.
* doc/pastposix-functions/usleep.texi (usleep): Document the
portability issue.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog                           |   10 ++++++++++
 doc/pastposix-functions/usleep.texi |    6 +++++-
 tests/test-stat-time.c              |   20 ++++++++++++--------
 3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0ad6134..00c76c2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2009-10-13  Eric Blake  <address@hidden>

+       test-stat-time: port to mingw
+       * tests/test-stat-time.c (force_unlink): Return a value.
+       (test_ctime) [W32]: Fix compilation error.
+       (nap): Don't call usleep with too large an argument.  Use
+       force_unlink.
+       * doc/pastposix-functions/usleep.texi (usleep): Document the
+       portability issue.
+
+2009-10-13  Eric Blake  <address@hidden>
+
        utimensat: new module
        * modules/utimensat: New file.
        * lib/utimensat.c (utimensat): Likewise.
diff --git a/doc/pastposix-functions/usleep.texi b/doc/pastposix-
functions/usleep.texi
index 507395c..bb38152 100644
--- a/doc/pastposix-functions/usleep.texi
+++ b/doc/pastposix-functions/usleep.texi
@@ -14,9 +14,13 @@ usleep
 @itemize
 @item
 This function is missing on some platforms:
-IRIX 5.3, Solaris 2.4, mingw, BeOS.
+IRIX 5.3, Solaris 2.4, older mingw, BeOS.
 @item
 According to POSIX, the @code{usleep} function may interfere with the program's
 use of the @code{SIGALRM} signal.  On Linux, it doesn't; on other platforms,
 it may.
address@hidden
+On some systems, @code{usleep} rejects attempts to sleep longer than 1
+second, as allowed by POSIX:
+mingw.
 @end itemize
diff --git a/tests/test-stat-time.c b/tests/test-stat-time.c
index 97a719a..3a0aafc 100644
--- a/tests/test-stat-time.c
+++ b/tests/test-stat-time.c
@@ -41,13 +41,13 @@

 enum { NFILES = 4 };

-static void
+static int
 force_unlink (const char *filename)
 {
   /* This chmod is necessary on mingw, where unlink() of a read-only file
      fails with EPERM.  */
   chmod (filename, 0600);
-  unlink (filename);
+  return unlink (filename);
 }

 static void
@@ -110,11 +110,12 @@ nap (void)
          differ, repeat the test one more time (in case we crossed a
          quantization boundary on a file system with 1 second
          resolution).  If we can't observe a difference in only the
-         nanoseconds, then fall back to 2 seconds.  */
+         nanoseconds, then fall back to 2 seconds.  However, note that
+         usleep (2000000) is allowed to fail with EINVAL.  */
       struct stat st1;
       struct stat st2;
       ASSERT (stat ("t-stt-stamp1", &st1) == 0);
-      ASSERT (unlink ("t-stt-stamp1") == 0);
+      ASSERT (force_unlink ("t-stt-stamp1") == 0);
       delay = 15000;
       usleep (delay);
       create_file ("t-stt-stamp1");
@@ -123,7 +124,7 @@ nap (void)
         {
           /* Seconds differ, give it one more shot.  */
           st1 = st2;
-          ASSERT (unlink ("t-stt-stamp1") == 0);
+          ASSERT (force_unlink ("t-stt-stamp1") == 0);
           usleep (delay);
           create_file ("t-stt-stamp1");
           ASSERT (stat ("t-stt-stamp1", &st2) == 0);
@@ -132,7 +133,10 @@ nap (void)
              && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
         delay = 2000000;
     }
-  usleep (delay);
+  if (delay == 2000000)
+    sleep (2);
+  else
+    usleep (delay);
 #endif /* HAVE_USLEEP */
 }

@@ -204,7 +208,7 @@ test_mtime (const struct stat *statinfo, struct timespec 
*modtimes)
    st_ctime is either the same as st_mtime (plus or minus an offset)
    or set to the file _creation_ time, and is not influenced by rename
    or chmod.  */
-# define test_ctime ((void) 0)
+# define test_ctime(ignored) ((void) 0)
 #else
 static void
 test_ctime (const struct stat *statinfo)
@@ -229,7 +233,7 @@ test_birthtime (const struct stat *statinfo,
 {
   int i;

-  /* Collect the birth times.. */
+  /* Collect the birth times.  */
   for (i = 0; i < NFILES; ++i)
     {
       birthtimes[i] = get_stat_birthtime (&statinfo[i]);
-- 
1.6.4.2










reply via email to

[Prev in Thread] Current Thread [Next in Thread]