From: Daniel Stenberg Date: Fri, 9 Jun 2023 18:42:51 +0000 (+0200) Subject: timeval: use CLOCK_MONOTONIC_RAW if available X-Git-Tag: curl-8_2_0~100 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c92b7228c575484f213a412a79f8340048c8c767;p=thirdparty%2Fcurl.git timeval: use CLOCK_MONOTONIC_RAW if available Reported-by: Harry Sintonen Ref: #11288 Closes #11291 --- diff --git a/acinclude.m4 b/acinclude.m4 index 5dbdd93fef..df2004937c 100644 --- a/acinclude.m4 +++ b/acinclude.m4 @@ -1072,6 +1072,38 @@ AC_DEFUN([CURL_CHECK_FUNC_CLOCK_GETTIME_MONOTONIC], [ dnl until library linking and run-time checks for clock_gettime succeed. ]) +dnl CURL_CHECK_FUNC_CLOCK_GETTIME_MONOTONIC_RAW +dnl ------------------------------------------------- +dnl Check if monotonic clock_gettime is available. + +AC_DEFUN([CURL_CHECK_FUNC_CLOCK_GETTIME_MONOTONIC_RAW], [ + AC_CHECK_HEADERS(sys/types.h sys/time.h) + AC_MSG_CHECKING([for raw monotonic clock_gettime]) + # + if test "x$dontwant_rt" = "xno" ; then + AC_COMPILE_IFELSE([ + AC_LANG_PROGRAM([[ +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#ifdef HAVE_SYS_TIME_H +#include +#endif +#include + ]],[[ + struct timespec ts; + (void)clock_gettime(CLOCK_MONOTONIC_RAW, &ts); + ]]) + ],[ + AC_MSG_RESULT([yes]) + AC_DEFINE_UNQUOTED(HAVE_CLOCK_GETTIME_MONOTONIC_RAW, 1, + [Define to 1 if you have the clock_gettime function and raw monotonic timer.]) + ],[ + AC_MSG_RESULT([no]) + ]) + fi +]) + dnl CURL_CHECK_LIBS_CLOCK_GETTIME_MONOTONIC dnl ------------------------------------------------- diff --git a/configure.ac b/configure.ac index 87fded66e7..373e2e0cef 100644 --- a/configure.ac +++ b/configure.ac @@ -1339,6 +1339,9 @@ dnl check for additional required libraries. dnl ********************************************************************** CURL_CHECK_LIBS_CLOCK_GETTIME_MONOTONIC +dnl Check for even better option +CURL_CHECK_FUNC_CLOCK_GETTIME_MONOTONIC_RAW + dnl ********************************************************************** dnl The preceding library checks are all potentially useful for test dnl servers and libtest cases which require networking and clock_gettime diff --git a/lib/timeval.c b/lib/timeval.c index dca1c6fe50..2de79be46d 100644 --- a/lib/timeval.c +++ b/lib/timeval.c @@ -58,7 +58,8 @@ struct curltime Curl_now(void) return now; } -#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC) +#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC) || \ + defined(HAVE_CLOCK_GETTIME_MONOTONIC_RAW) struct curltime Curl_now(void) { @@ -87,6 +88,19 @@ struct curltime Curl_now(void) have_clock_gettime = TRUE; #endif +#ifdef HAVE_CLOCK_GETTIME_MONOTONIC_RAW + if( +#if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \ + (HAVE_BUILTIN_AVAILABLE == 1) + have_clock_gettime && +#endif + (0 == clock_gettime(CLOCK_MONOTONIC_RAW, &tsnow))) { + cnow.tv_sec = tsnow.tv_sec; + cnow.tv_usec = (unsigned int)(tsnow.tv_nsec / 1000); + } + else +#endif + if( #if defined(__APPLE__) && defined(HAVE_BUILTIN_AVAILABLE) && \ (HAVE_BUILTIN_AVAILABLE == 1)