Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2002:
[Freeciv-Dev] (PR#2305) Add check to test for a working gettimeofday
Home

[Freeciv-Dev] (PR#2305) Add check to test for a working gettimeofday

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#2305) Add check to test for a working gettimeofday
From: "Raimar Falke via RT" <rt@xxxxxxxxxxxxxx>
Date: Mon, 11 Nov 2002 10:54:44 -0800
Reply-to: rt@xxxxxxxxxxxxxx


To ensure basic sanity we need to test the gettimeofday a bit
harder. So at Thomas's system it prints out:

after 0.157977s: going backward by 188us
after 0.245779s: going backward by 190us
after 0.007114s: going forward by 4295s
after 0.106756s: going backward by 189us
after 0.137305s: going backward by 185us
after 0.097206s: going backward by 190us

we want to detect such systems.

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 checking for the vaidity of the Maxwell laws on this machine... ok
 checking if e=mc^2... ok
 checking if we can safely swap on /dev/fd0... yes
    -- kvirc 2.0.0's configure 

diff -Nurd -X clean/diff_ignore clean/acconfig.old a/acconfig.old
--- clean/acconfig.old  Mon Nov 11 19:47:04 2002
+++ a/acconfig.old      Mon Nov 11 19:38:28 2002
@@ -63,6 +63,7 @@
 #undef ESD
 #undef SDL
 #undef WINMM
+#undef HAVE_GETTIMEOFDAY
 @BOTTOM@
 
 #endif /* FC_CONFIG_H */
diff -Nurd -X clean/diff_ignore clean/configure.ac a/configure.ac
--- clean/configure.ac  Wed Oct 16 21:11:29 2002
+++ a/configure.ac      Mon Nov 11 19:48:36 2002
@@ -567,9 +567,12 @@
 AC_FUNC_VPRINTF
 AC_FUNC_VSNPRINTF
 
-AC_CHECK_FUNCS(fileno gethostname getpwuid gettimeofday inet_aton \
+AC_CHECK_FUNCS(fileno gethostname getpwuid inet_aton \
                select snooze strerror strcasecmp strncasecmp \
                strlcat strlcpy strstr usleep vsnprintf uname)
+
+AC_MSG_CHECKING(for working gettimeofday)
+  FC_CHECK_GETTIMEOFDAY_RUNTIME(,AC_DEFINE(HAVE_GETTIMEOFDAY),)
 
 dnl The use of both AC_FUNC_VSNPRINTF and AC_CHECK_FUNCS(vsnprintf) is
 dnl deliberate.
diff -Nurd -X clean/diff_ignore clean/configure.in a/configure.in
--- clean/configure.in  Mon Nov 11 19:47:04 2002
+++ a/configure.in      Mon Nov 11 19:44:15 2002
@@ -561,9 +561,11 @@
 AC_FUNC_VPRINTF
 AC_FUNC_VSNPRINTF
 
-AC_CHECK_FUNCS(fileno gethostname getpwuid gettimeofday inet_aton \
+AC_CHECK_FUNCS(fileno gethostname getpwuid inet_aton \
                select snooze strerror strcasecmp strncasecmp \
                strlcat strlcpy strstr usleep vsnprintf uname)
+AC_MSG_CHECKING(for working gettimeofday)
+  FC_CHECK_GETTIMEOFDAY_RUNTIME(,AC_DEFINE(HAVE_GETTIMEOFDAY),)
 
 dnl The use of both AC_FUNC_VSNPRINTF and AC_CHECK_FUNCS(vsnprintf) is
 dnl deliberate.
diff -Nurd -X clean/diff_ignore clean/m4/gettimeofday.m4 a/m4/gettimeofday.m4
--- clean/m4/gettimeofday.m4    Thu Jan  1 01:00:00 1970
+++ a/m4/gettimeofday.m4        Mon Nov 11 19:48:03 2002
@@ -0,0 +1,112 @@
+dnl FC_CHECK_GETTIMEOFDAY_RUNTIME(EXTRA-LIBS, ACTION-IF-FOUND, 
ACTION-IF-NOT-FOUND)
+dnl
+dnl This tests whether gettimeofday works at runtime.  Here, "works"
+dnl means: time doesn't go backward and time doesn't jump forward by
+dnl a huge amount. It seems that glibc 2.3.1 is broken in this respect.
+
+AC_DEFUN(FC_CHECK_GETTIMEOFDAY_RUNTIME,
+[
+templibs="$LIBS"
+LIBS="$1 $LIBS"
+AC_TRY_RUN([
+#include <sys/time.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <time.h>
+#include <stdlib.h>
+
+#define VERBOSE 0
+#define SECONDS        3
+
+int main(int argc, char **argv)
+{
+  struct timeval tv[2], start, end;
+  int calls;
+
+  if (gettimeofday(&start, NULL) == -1) {
+    return 1;
+  }
+  end = start;
+  end.tv_sec += SECONDS;
+
+  tv[0] = start;
+  tv[1] = start;
+
+  for (calls = 0;; calls++) {
+    time_t sec;
+
+    if (gettimeofday(&tv[0], NULL) == -1) {
+      return 1;
+    }
+
+    if (tv[0].tv_sec < tv[1].tv_sec) {
+#if VERBOSE
+      double diff =
+         (tv[1].tv_sec - start.tv_sec) +
+         ((tv[1].tv_usec - start.tv_usec) / 1e6);
+      printf("after %fs: going backward by %lds\n", diff,
+            tv[1].tv_sec - tv[0].tv_sec);
+#endif
+      return 1;
+    }
+
+    if (tv[0].tv_sec == tv[1].tv_sec && tv[0].tv_usec < tv[1].tv_usec) {
+#if VERBOSE
+      double diff =
+         (tv[1].tv_sec - start.tv_sec) +
+         ((tv[1].tv_usec - start.tv_usec) / 1e6);
+      printf("after %fs: going backward by %ldus\n", diff,
+            tv[1].tv_usec - tv[0].tv_usec);
+#endif
+      return 1;
+    }
+
+    if (tv[0].tv_sec > tv[1].tv_sec + 1) {
+#if VERBOSE
+      double diff =
+         (tv[1].tv_sec - start.tv_sec) +
+         ((tv[1].tv_usec - start.tv_usec) / 1e6);
+      printf("after %fs: going forward by %lds\n", diff,
+            tv[0].tv_sec - tv[1].tv_sec);
+#endif
+      return 1;
+    }
+
+    sec = time(NULL);
+
+    if (abs(sec - tv[0].tv_sec) > 1) {
+#if VERBOSE
+      double diff =
+         (tv[1].tv_sec - start.tv_sec) +
+         ((tv[1].tv_usec - start.tv_usec) / 1e6);
+      printf("after %fs: time() = %ld, gettimeofday = %ld, diff = %ld\n", diff,
+            (long)sec, (long)tv[0].tv_sec, sec - (long)tv[0].tv_sec);
+#endif
+      return 1;
+    }
+
+    if (timercmp(&tv[0], &end, >)) {
+      break;
+    }
+    tv[1] = tv[0];
+  }
+
+#if VERBOSE
+  {
+    double diff =
+       (tv[1].tv_sec - start.tv_sec) +
+       ((tv[1].tv_usec - start.tv_usec) / 1e6);
+    printf("%d calls in %fs = %fus/call\n", calls, diff, 1e6 * diff / calls);
+  }
+#endif
+  return 0;
+}
+],
+[AC_MSG_RESULT(yes)
+  [$2]],
+[AC_MSG_RESULT(no)
+  [$3]],
+[AC_MSG_RESULT(unknown: cross-compiling)
+  [$2]])
+LIBS="$templibs"
+])

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