From 87b09d144ecbb5c9eea1bf604b9ec0d5a61875d0 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Mon, 12 Jul 2021 16:48:32 -0400 Subject: [PATCH] [core] use CLOCK_MONOTONIC_COARSE where available server.c uses monotonic clock to detect change in second, so CLOCK_MONOTONIC_COARSE clock resolution (often ~1ms) is more than sufficient. (Obtaining CLOCK_MONOTONIC_COARSE can be faster than CLOCK_MONOTONIC.) reference: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux_for_real_time/7/html/reference_guide/sect-posix_clocks --- src/log.c | 26 +++++++------------------- src/log.h | 2 +- src/server.c | 24 +++++++++++++++++++++++- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/log.c b/src/log.c index 75176f08..0fbf352b 100644 --- a/src/log.c +++ b/src/log.c @@ -24,13 +24,13 @@ unix_time64_t log_epoch_secs = 0; unix_time64_t log_monotonic_secs = 0; -int log_clock_gettime_realtime (unix_timespec64_t *ts) { +int log_clock_gettime (const int clockid, unix_timespec64_t * const ts) { #ifdef HAVE_CLOCK_GETTIME #if HAS_TIME_BITS64 - return clock_gettime(CLOCK_REALTIME, ts); + return clock_gettime(clockid, ts); #else struct timespec ts32; - int rc = clock_gettime(CLOCK_REALTIME, &ts32); + int rc = clock_gettime(clockid, &ts32); if (0 == rc) { /*(treat negative 32-bit tv.tv_sec as unsigned)*/ ts->tv_sec = TIME64_CAST(ts32.tv_sec); @@ -44,6 +44,7 @@ int log_clock_gettime_realtime (unix_timespec64_t *ts) { * && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 101200 */ struct timeval tv; gettimeofday(&tv, NULL); + UNUSED(clockid); #if HAS_TIME_BITS64 ts->tv_sec = tv.tv_sec; #else /*(treat negative 32-bit tv.tv_sec as unsigned)*/ @@ -54,24 +55,11 @@ int log_clock_gettime_realtime (unix_timespec64_t *ts) { #endif } -int log_clock_gettime_monotonic (unix_timespec64_t *ts) { +int log_clock_gettime_realtime (unix_timespec64_t *ts) { #ifdef HAVE_CLOCK_GETTIME - #if HAS_TIME_BITS64 - return clock_gettime(CLOCK_MONOTONIC, ts); - #else - struct timespec ts32; - int rc = clock_gettime(CLOCK_MONOTONIC, &ts32); - if (0 == rc) { - /*(treat negative 32-bit tv.tv_sec as unsigned)*/ - /*(negative 32-bit should not happen on monotonic clock - * unless system running continously for > 68 years)*/ - ts->tv_sec = TIME64_CAST(ts32.tv_sec); - ts->tv_nsec = ts32.tv_nsec; - } - return rc; - #endif + return log_clock_gettime(CLOCK_REALTIME, ts); #else - return log_clock_gettime_realtime(ts); /*(fallback)*/ + return log_clock_gettime(0, ts); #endif } diff --git a/src/log.h b/src/log.h index 4a4d9bbf..6d3e1aee 100644 --- a/src/log.h +++ b/src/log.h @@ -8,8 +8,8 @@ extern unix_time64_t log_epoch_secs; extern unix_time64_t log_monotonic_secs; +int log_clock_gettime(int clockid, unix_timespec64_t *ts); int log_clock_gettime_realtime (unix_timespec64_t *ts); -int log_clock_gettime_monotonic (unix_timespec64_t *ts); ssize_t write_all(int fd, const void* buf, size_t count); diff --git a/src/server.c b/src/server.c index ed9efb31..80a57d4b 100644 --- a/src/server.c +++ b/src/server.c @@ -222,11 +222,13 @@ static int daemonize(void) { } #endif +static int clockid_mono_coarse = 0; + static unix_time64_t server_monotonic_secs (void) { unix_timespec64_t ts; - return (0 == log_clock_gettime_monotonic(&ts)) + return (0 == log_clock_gettime(clockid_mono_coarse, &ts)) ? ts.tv_sec : log_monotonic_secs; } @@ -272,6 +274,26 @@ static server *server_init(void) { li_rand_reseed(); srv->startup_ts = log_epoch_secs = TIME64_CAST(time(NULL)); + #ifdef HAVE_CLOCK_GETTIME + unix_timespec64_t ts; + UNUSED(&ts); + #ifdef CLOCK_MONOTONIC_COARSE + if (0 == log_clock_gettime(CLOCK_MONOTONIC_COARSE, &ts)) + clockid_mono_coarse = CLOCK_MONOTONIC_COARSE; + else + #endif + #ifdef CLOCK_MONOTONIC_RAW_APPROX + if (0 == log_clock_gettime(CLOCK_MONOTONIC_RAW_APPROX, &ts)) + clockid_mono_coarse = CLOCK_MONOTONIC_RAW_APPROX; + else + #endif + #ifdef CLOCK_MONOTONIC_RAW + if (0 == log_clock_gettime(CLOCK_MONOTONIC_RAW, &ts)) + clockid_mono_coarse = CLOCK_MONOTONIC_RAW; + else + #endif + clockid_mono_coarse = CLOCK_MONOTONIC; + #endif log_monotonic_secs = server_monotonic_secs(); srv->errh = log_error_st_init();