From 8c25372709d256d83858be454987137dc202b725 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 18 Apr 2022 13:08:27 -0700 Subject: [PATCH 3/6] Add Gnulib gettime-res module * admin/merge-gnulib (GNULIB_MODULES): Add gettime-res. * lib/gettime-res.c: New file, copied from Gnulib. * lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate. --- admin/merge-gnulib | 3 +- lib/gettime-res.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++ lib/gnulib.mk.in | 9 ++++++ m4/gnulib-comp.m4 | 3 ++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 lib/gettime-res.c diff --git a/admin/merge-gnulib b/admin/merge-gnulib index ea3d23686f..0279d1f99d 100755 --- a/admin/merge-gnulib +++ b/admin/merge-gnulib @@ -37,7 +37,8 @@ GNULIB_MODULES= fchmodat fcntl fcntl-h fdopendir file-has-acl filemode filename filevercmp flexmember fpieee free-posix fstatat fsusage fsync futimens - getloadavg getopt-gnu getrandom gettime gettimeofday gitlog-to-changelog + getloadavg getopt-gnu getrandom + gettime gettime-res gettimeofday gitlog-to-changelog ieee754-h ignore-value intprops largefile libgmp lstat manywarnings memmem-simple mempcpy memrchr minmax mkostemp mktime nanosleep nproc nstrftime diff --git a/lib/gettime-res.c b/lib/gettime-res.c new file mode 100644 index 0000000000..611f83ad27 --- /dev/null +++ b/lib/gettime-res.c @@ -0,0 +1,78 @@ +/* Get the system clock resolution. + + Copyright 2021-2022 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 . */ + +/* Written by Paul Eggert. */ + +#include + +#include "timespec.h" + +static long int _GL_ATTRIBUTE_CONST +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 10 ms 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); + } + + return r; +} + +/* + * Hey Emacs! + * Local Variables: + * coding: utf-8 + * End: + */ diff --git a/lib/gnulib.mk.in b/lib/gnulib.mk.in index bbb05fdba5..fbc199faaf 100644 --- a/lib/gnulib.mk.in +++ b/lib/gnulib.mk.in @@ -114,6 +114,7 @@ # getopt-gnu \ # getrandom \ # gettime \ +# gettime-res \ # gettimeofday \ # gitlog-to-changelog \ # ieee754-h \ @@ -2188,6 +2189,14 @@ libgnu_a_SOURCES += gettime.c endif ## end gnulib module gettime +## begin gnulib module gettime-res +ifeq (,$(OMIT_GNULIB_MODULE_gettime-res)) + +libgnu_a_SOURCES += gettime-res.c + +endif +## end gnulib module gettime-res + ## begin gnulib module gettimeofday ifeq (,$(OMIT_GNULIB_MODULE_gettimeofday)) diff --git a/m4/gnulib-comp.m4 b/m4/gnulib-comp.m4 index fb5f1b52a4..a04ec44be9 100644 --- a/m4/gnulib-comp.m4 +++ b/m4/gnulib-comp.m4 @@ -112,6 +112,7 @@ AC_DEFUN # Code from module getrandom: # Code from module gettext-h: # Code from module gettime: + # Code from module gettime-res: # Code from module gettimeofday: # Code from module gitlog-to-changelog: # Code from module group-member: @@ -371,6 +372,7 @@ AC_DEFUN [test $HAVE_GETRANDOM = 0 || test $REPLACE_GETRANDOM = 1]) gl_SYS_RANDOM_MODULE_INDICATOR([getrandom]) gl_GETTIME + gl_GETTIME_RES gl_FUNC_GETTIMEOFDAY gl_CONDITIONAL([GL_COND_OBJ_GETTIMEOFDAY], [test $HAVE_GETTIMEOFDAY = 0 || test $REPLACE_GETTIMEOFDAY = 1]) @@ -1270,6 +1272,7 @@ AC_DEFUN lib/getopt_int.h lib/getrandom.c lib/gettext.h + lib/gettime-res.c lib/gettime.c lib/gettimeofday.c lib/gl_openssl.h -- 2.35.1