Subject: bug#12820: gnulib testsuite failure in latest master

From: Paul Eggert <[email-masked]>
Date: Wed, 07 Nov 2012 23:44:49 -0800

Ouch, I read your strace output incorrectly: it was napping
for 20 ms, not 2 s.  Still, there should be no need to nap
for that long; 1 ms should suffice on your platform.

I installed the following patch to cause the test
to take shorter naps.  This might affect your original
problem, and might not (hey! it's a timing problem)
but at least it should make the test run faster.

>From b1af052298f85650d85ea37d24f59ab99ff56db8 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Wed, 7 Nov 2012 23:36:43 -0800
Subject: [PATCH] test-utimens: speed up by taking shorter naps

* tests/nap.h (lt_mtime, get_mtime, nap_works, guess_delay):
New functions.
(nap): Use them, to do a better job of guessing the delay.
On Fedora 17 with ext4 atop md atop hard disks, this made
test-utimens run 10x faster, because the test napped for
1 ms at a time rather than 20 ms.  Reported by Stefano Lattarini in
<http://bugs.gnu.org/12820#11>.
---
 ChangeLog   |  11 +++++++
 tests/nap.h | 107 ++++++++++++++++++++++++++++++++++++++++--------------------
 2 files changed, 83 insertions(+), 35 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1a1e8b7..bd05f3c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2012-11-07  Paul Eggert  <[email-masked]>
+
+	test-utimens: speed up by taking shorter naps
+	* tests/nap.h (lt_mtime, get_mtime, nap_works, guess_delay):
+	New functions.
+	(nap): Use them, to do a better job of guessing the delay.
+	On Fedora 17 with ext4 atop md atop hard disks, this made
+	test-utimens run 10x faster, because the test napped for
+	1 ms at a time rather than 20 ms.  Reported by Stefano Lattarini in
+	<http://bugs.gnu.org/12820#11>.
+
 2012-11-07  Jim Meyering  <[email-masked]>
 
 	mountlist.c: fix a compilation failure
diff --git a/tests/nap.h b/tests/nap.h
index 6dfb0a0..99d47b6 100644
--- a/tests/nap.h
+++ b/tests/nap.h
@@ -19,48 +19,85 @@
 #ifndef GLTEST_NAP_H
 # define GLTEST_NAP_H
 
+static int
+lt_mtime (struct stat const *a, struct stat const *b)
+{
+  time_t as = a->st_mtime;
+  time_t bs = b->st_mtime;
+  int ans = get_stat_mtime_ns (a);
+  int bns = get_stat_mtime_ns (b);
+
+  return as < bs || (as == bs && ans < bns);
+}
+
+static void
+get_mtime (int fd, struct stat *st, int do_write)
+{
+  if (do_write)
+    ASSERT (write (fd, "\n", 1) == 1);
+  ASSERT (fstat (fd, st) == 0);
+}
+
+/* Given a file whose descriptor is FD, see whether delaying by DELAY
+   microseconds causes a change in a file's time stamp.  If the time
+   stamps differ, repeat the test one more time, in case we crossed a
+   quantization boundary on a file system with lower resolution.  *ST
+   is the file's status, recently gotten.  Update *ST to reflect the
+   latest status gotten.  */
+static int
+nap_works (int fd, int delay, struct stat *st)
+{
+  struct stat old_st;
+  int result = 0;
+  old_st = *st;
+  usleep (delay);
+  get_mtime (fd, st, 1);
+  if (! lt_mtime (&old_st, st))
+    return 0;
+  old_st = *st;
+  usleep (delay);
+  get_mtime (fd, st, 1);
+  return lt_mtime (&old_st, st);
+}
+
+static int
+guess_delay (void)
+{
+  /* Try a 1-microsecond sleep first, for speed.  If that doesn't
+     work, try a 1 ms sleep; that should work with ext.  If it doesn't
+     work, try a 20 ms sleep.  xfs has a quantization of about 10
+     milliseconds, even though it has a granularity of 1 nanosecond,
+     and NTFS has a default quantization of 15.25 milliseconds, even
+     though it has a granularity of 100 nanoseconds, so 20 ms is a
+     good quantization to try.  If that doesn't work, try 1 second.
+     The worst case is 2 seconds, needed for FAT.  */
+  static int const delaytab[] = {1, 1000, 20000, 1000000 };
+  int fd = creat (BASE "tmp", 0600);
+  int i;
+  int delay = 2000000;
+  struct stat st;
+  ASSERT (0 <= fd);
+  get_mtime (fd, &st, 0);
+  for (i = 0; i < sizeof delaytab / sizeof delaytab[0]; i++)
+    if (nap_works (fd, delaytab[i], &st))
+      {
+        delay = delaytab[i];
+        break;
+      }
+  ASSERT (close (fd) == 0);
+  ASSERT (unlink (BASE "tmp") == 0);
+  return delay;
+}
+
 /* Sleep long enough to notice a timestamp difference on the file
    system in the current directory.  Assumes that BASE is defined,
    and requires that the test module depends on usleep.  */
 static void
 nap (void)
 {
-  static long delay;
+  static int delay;
   if (!delay)
-    {
-      /* Initialize only once, by sleeping for 20 milliseconds (needed
-         since xfs has a quantization of about 10 milliseconds, even
-         though it has a granularity of 1 nanosecond, and since NTFS
-         has a default quantization of 15.25 milliseconds, even though
-         it has a granularity of 100 nanoseconds).  If the seconds
-         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 1 second if the time is odd,
-         and 2 seconds (needed for FAT) if time is even.  */
-      struct stat st1;
-      struct stat st2;
-      ASSERT (close (creat (BASE "tmp", 0600)) == 0);
-      ASSERT (stat (BASE "tmp", &st1) == 0);
-      ASSERT (unlink (BASE "tmp") == 0);
-      delay = 20000;
-      usleep (delay);
-      ASSERT (close (creat (BASE "tmp", 0600)) == 0);
-      ASSERT (stat (BASE "tmp", &st2) == 0);
-      ASSERT (unlink (BASE "tmp") == 0);
-      if (st1.st_mtime != st2.st_mtime)
-        {
-          /* Seconds differ, give it one more shot.  */
-          st1 = st2;
-          usleep (delay);
-          ASSERT (close (creat (BASE "tmp", 0600)) == 0);
-          ASSERT (stat (BASE "tmp", &st2) == 0);
-          ASSERT (unlink (BASE "tmp") == 0);
-        }
-      if (! (st1.st_mtime == st2.st_mtime
-             && get_stat_mtime_ns (&st1) < get_stat_mtime_ns (&st2)))
-        delay = (st1.st_mtime & 1) ? 1000000 : 2000000;
-    }
+    delay = guess_delay ();
   usleep (delay);
 }
 
-- 
1.7.11.7