* lib/gettime-res.c, modules/gettime-res: New files.
* lib/timespec.h (gettime_res): New decl.
* m4/clock_time.m4 (gl_CLOCK_TIME): Also check for clock_getres.
* m4/gettime.m4 (gl_GETTIME_RES): New macro.
2021-12-28 Paul Eggert <eggert@cs.ucla.edu>
+ gettime-res: new module
+ * lib/gettime-res.c, modules/gettime-res: New files.
+ * lib/timespec.h (gettime_res): New decl.
+ * m4/clock_time.m4 (gl_CLOCK_TIME): Also check for clock_getres.
+ * m4/gettime.m4 (gl_GETTIME_RES): New macro.
+
gettime: port better to non-POSIX C2x
C2x requires a function timespec_get; prefer that to
gettimeofday, since it has better resolution.
This function is missing on some platforms:
Mac OS X 10.11, Minix 3.1.8, mingw, MSVC 14.
@end itemize
+
+The Gnulib module @code{gettime-res} is a partial substitute; it implements
+the @code{CLOCK_REALTIME} functionality of @code{clock_getres}.
--- /dev/null
+/* Get the system clock resolution.
+
+ Copyright 2021 Free Software Foundation, Inc.
+
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 3 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+/* Written by Paul Eggert. */
+
+#include <config.h>
+
+#include "timespec.h"
+
+static long int
+gcd (long int a, long int b)
+{
+ while (b != 0)
+ {
+ long int r = a % b;
+ a = b;
+ b = r;
+ }
+ return a;
+}
+
+/* Return the system time resolution in nanoseconds. */
+
+long int
+gettime_res (void)
+{
+ struct timespec res;
+#if defined CLOCK_REALTIME && HAVE_CLOCK_GETRES
+ clock_getres (CLOCK_REALTIME, &res);
+#elif defined HAVE_TIMESPEC_GETRES
+ timespec_getres (&res, TIME_UTC);
+#else
+ /* Guess high and let the later code deduce better. */
+ res.tv_sec = 1;
+ res.tv_nsec = 0;
+#endif
+
+ /* On all Gnulib platforms the following calculations do not overflow. */
+
+ long int hz = TIMESPEC_HZ;
+ long int r = hz * res.tv_sec + res.tv_nsec;
+
+ /* On some platforms, clock_getres (CLOCK_REALTIME, ...) yields a
+ too-large resolution, under the mistaken theory that it should
+ return the timer interval. For example, on AIX 7.2 POWER8
+ clock_getres yields 1 cs even though clock_gettime yields 1 µs
+ resolution. Work around the problem with high probability by
+ trying clock_gettime several times and observing the resulting
+ bounds on resolution. */
+ for (int i = 0; 1 < r && i < 32; i++)
+ {
+ struct timespec now = current_timespec ();
+ r = gcd (r, now.tv_nsec ? now.tv_nsec : hz * now.tv_sec);
+ }
+
+ return r;
+}
+
+/*
+ * Hey Emacs!
+ * Local Variables:
+ * coding: utf-8
+ * End:
+ */
return a.tv_sec + a.tv_nsec / 1e9;
}
+long int gettime_res (void);
struct timespec current_timespec (void);
void gettime (struct timespec *) _GL_ARG_NONNULL ((1));
int settime (struct timespec const *) _GL_ARG_NONNULL ((1));
-# clock_time.m4 serial 10
+# clock_time.m4 serial 11
dnl Copyright (C) 2002-2006, 2009-2021 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
-# Check for clock_gettime and clock_settime, and set LIB_CLOCK_GETTIME.
+# Check for clock_getres, clock_gettime and clock_settime,
+# and set LIB_CLOCK_GETTIME.
# For a program named, say foo, you should add a line like the following
# in the corresponding Makefile.am file:
# foo_LDADD = $(LDADD) $(LIB_CLOCK_GETTIME)
AC_SEARCH_LIBS([clock_gettime], [rt posix4],
[test "$ac_cv_search_clock_gettime" = "none required" ||
LIB_CLOCK_GETTIME=$ac_cv_search_clock_gettime])
- AC_CHECK_FUNCS([clock_gettime clock_settime])
+ AC_CHECK_FUNCS([clock_getres clock_gettime clock_settime])
LIBS=$gl_saved_libs
])
-# gettime.m4 serial 10
+# gettime.m4 serial 11
dnl Copyright (C) 2002, 2004-2006, 2009-2021 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
AC_REQUIRE([gl_TIMESPEC])
AC_CHECK_FUNCS_ONCE([timespec_get])
])
+
+AC_DEFUN([gl_GETTIME_RES],
+[
+ dnl Prerequisites of lib/gettime-res.c.
+ AC_REQUIRE([gl_CLOCK_TIME])
+ AC_REQUIRE([gl_TIMESPEC])
+ AC_CHECK_FUNCS_ONCE([timespec_getres])
+])
--- /dev/null
+Description:
+Return current time resolution
+
+Files:
+lib/gettime-res.c
+m4/gettime.m4
+
+Depends-on:
+clock-time
+gettime
+timespec
+extensions
+
+configure.ac:
+gl_GETTIME_RES
+
+Makefile.am:
+lib_SOURCES += gettime-res.c
+
+Include:
+"timespec.h"
+
+Link:
+$(LIB_CLOCK_GETTIME)
+
+License:
+LGPL
+
+Maintainer:
+Paul Eggert, Jim Meyering