From ff9312b7b5708d1d1da6c58465e170bd0896556b Mon Sep 17 00:00:00 2001 From: =?utf8?q?R=C3=A9mi=20Lef=C3=A8vre?= Date: Tue, 6 Dec 2016 12:02:29 +0100 Subject: [PATCH] introduce new clocks API using `clock_gettime()` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fallbacks are provided for systems that do not support clock_gettime(), CLOCK_MONOTONIC or CLOCK_BOOTTIME. Signed-off-by: Rémi Lefèvre --- CMakeLists.txt | 2 ++ configure.ac | 3 ++ src/Makefile.am | 2 ++ src/clocks.c | 73 ++++++++++++++++++++++++++++++++++++++++ src/clocks.h | 88 +++++++++++++++++++++++++++++++++++++++++++++++++ src/common.c | 4 --- src/common.h | 2 +- 7 files changed, 169 insertions(+), 5 deletions(-) create mode 100644 src/clocks.c create mode 100644 src/clocks.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 79a3cc3f..d58e66cb 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,8 @@ set(NETDATA_SOURCE_FILES src/avl.h src/backends.c src/backends.h + src/clocks.c + src/clocks.h src/common.c src/common.h src/daemon.c diff --git a/configure.ac b/configure.ac index 9b26fcdd..f5dcf4df 100644 --- a/configure.ac +++ b/configure.ac @@ -35,6 +35,9 @@ AC_PROG_INSTALL PKG_PROG_PKG_CONFIG AC_USE_SYSTEM_EXTENSIONS AC_CHECK_FUNCS_ONCE(accept4) +AC_CHECK_TYPES([struct timespec, clockid_t], [], [], [[#include ]]) +AC_SEARCH_LIBS([clock_gettime], [rt posix4]) +AC_CHECK_FUNCS([clock_gettime]) # Check system type case "$host_os" in diff --git a/src/Makefile.am b/src/Makefile.am index 466c6993..f2ddef3d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -30,6 +30,7 @@ netdata_SOURCES = \ appconfig.c appconfig.h \ avl.c avl.h \ backends.c backends.h \ + clocks.c clocks.h \ common.c common.h \ daemon.c daemon.h \ dictionary.c dictionary.h \ @@ -100,6 +101,7 @@ netdata_LDADD = \ apps_plugin_SOURCES = \ apps_plugin.c \ avl.c avl.h \ + clocks.c clocks.h \ common.c common.h \ log.c log.h \ procfile.c procfile.h \ diff --git a/src/clocks.c b/src/clocks.c new file mode 100644 index 00000000..c90a07c2 --- /dev/null +++ b/src/clocks.c @@ -0,0 +1,73 @@ +#include "common.h" + +#ifndef HAVE_CLOCK_GETTIME +inline int clock_gettime(clockid_t clk_id, struct timespec *ts) { + struct timeval tv; + if(unlikely(gettimeofday(&tv, NULL) == -1)) + return -1; + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * NSEC_PER_USEC; + return 0; +} +#endif + +inline time_t now_realtime_sec(void) { + struct timespec ts; + if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1)) + return 0; + return ts.tv_sec; +} + +inline int now_realtime_timeval(struct timeval *tv) { + struct timespec ts; + if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1)) + return -1; + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC; + return 0; +} + +inline usec_t now_realtime_usec(void) { + struct timespec ts; + if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1)) + return 0; + return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC; +} + +inline time_t now_monotonic_sec(void) { + struct timespec ts; + if(unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) == -1)) + return 0; + return ts.tv_sec; +} + +inline usec_t now_monotonic_usec(void) { + struct timespec ts; + if(unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) == -1)) + return 0; + return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC; +} + +inline time_t now_boottime_sec(void) { + struct timespec ts; + if(unlikely(clock_gettime(CLOCK_BOOTTIME, &ts) == -1)) + return 0; + return ts.tv_sec; +} + +inline usec_t now_boottime_usec(void) { + struct timespec ts; + if(unlikely(clock_gettime(CLOCK_BOOTTIME, &ts) == -1)) + return 0; + return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC; +} + +inline usec_t timeval_usec(struct timeval *tv) { + return (usec_t)tv->tv_sec * USEC_PER_SEC + tv->tv_usec; +} + +inline usec_t dt_usec(struct timeval *now, struct timeval *old) { + usec_t ts1 = timeval_usec(now); + usec_t ts2 = timeval_usec(old); + return (ts1 > ts2) ? (ts1 - ts2) : (ts2 - ts1); +} diff --git a/src/clocks.h b/src/clocks.h new file mode 100644 index 00000000..0db52c2f --- /dev/null +++ b/src/clocks.h @@ -0,0 +1,88 @@ +#ifndef NETDATA_CLOCKS_H +#define NETDATA_CLOCKS_H 1 + +#ifndef HAVE_STRUCT_TIMESPEC +struct timespec { + time_t tv_sec; /* seconds */ + long tv_nsec; /* nanoseconds */ +} +#endif + +#ifndef HAVE_CLOCKID_T +typedef int clockid_t +#endif + +#ifndef HAVE_CLOCK_GETTIME +int clock_gettime(clockid_t clk_id, struct timespec *ts); +#endif + +/* Linux values are as good as any others */ +#ifndef CLOCK_REALTIME +#define CLOCK_REALTIME 0 +#endif + +#ifndef CLOCK_MONOTONIC +/* fallback to CLOCK_REALTIME if not available */ +#define CLOCK_MONOTONIC CLOCK_REALTIME +#endif + +#ifndef CLOCK_BOOTTIME +/* fallback to CLOCK_MONOTONIC if not available */ +#define CLOCK_BOOTTIME CLOCK_MONOTONIC +#endif + +typedef unsigned long long usec_t; + +#define NSEC_PER_SEC 1000000000ULL +#define NSEC_PER_MSEC 1000000ULL +#define NSEC_PER_USEC 1000ULL +#define USEC_PER_SEC 1000000ULL + +#ifndef HAVE_CLOCK_GETTIME +/* Fallback function for POSIX.1-2001 clock_gettime() function. + * + * We use a realtime clock from gettimeofday(), this will + * make systems without clock_gettime() support sensitive + * to time jumps or hibernation/suspend side effects. + */ +extern int clock_gettime(clockid_t clk_id, struct timespec *ts); +#endif + +/* Fills struct timeval with time since EPOCH from real-time clock (i.e. wall-clock). + * - Hibernation/suspend time is included + * - adjtime()/NTP adjustments affect this clock + * Return 0 on succes, -1 else with errno set appropriately. + */ +extern int now_realtime_timeval(struct timeval *tv); + +/* Returns time since EPOCH from real-time clock (i.e. wall-clock). + * - Hibernation/suspend time is included + * - adjtime()/NTP adjustments affect this clock + */ +extern time_t now_realtime_sec(void); +extern usec_t now_realtime_usec(void); + +/* Returns time from monotonic clock if available, real-time clock else. + * If monotonic clock is available: + * - hibernation/suspend time is not included + * - adjtime()/NTP adjusments affect this clock + * If monotonic clock is not available, this fallbacks to now_realtime(). + */ +extern time_t now_monotonic_sec(void); +extern usec_t now_monotonic_usec(void); + +/* Returns time from boottime clock if available, + * monotonic clock else if available, real-time clock else. + * If boottime clock is available: + * - hibernation/suspend time is included + * - adjtime()/NTP adjusments affect this clock + * If boottime clock is not available, this fallbacks to now_monotonic(). + * If monotonic clock is not available, this fallbacks to now_realtime(). + */ +extern time_t now_boottime_sec(void); +extern usec_t now_boottime_usec(void); + +extern usec_t timeval_usec(struct timeval *ts); +extern usec_t dt_usec(struct timeval *now, struct timeval *old); + +#endif /* NETDATA_CLOCKS_H */ diff --git a/src/common.c b/src/common.c index 98093b96..52e48fea 100644 --- a/src/common.c +++ b/src/common.c @@ -200,10 +200,6 @@ void freez(void *ptr) { // ---------------------------------------------------------------------------- // time functions -inline unsigned long long timeval_usec(struct timeval *tv) { - return tv->tv_sec * 1000000ULL + tv->tv_usec; -} - // time(NULL) in nanoseconds inline unsigned long long time_usec(void) { struct timeval now; diff --git a/src/common.h b/src/common.h index a6f185bc..582149fb 100644 --- a/src/common.h +++ b/src/common.h @@ -123,6 +123,7 @@ #include "plugin_tc.h" #include "plugins_d.h" +#include "clocks.h" #include "eval.h" #include "health.h" @@ -146,7 +147,6 @@ #define abs(x) ((x < 0)? -x : x) extern unsigned long long usec_dt(struct timeval *now, struct timeval *old); -extern unsigned long long timeval_usec(struct timeval *tv); // #define usec_dt(now, last) (((((now)->tv_sec * 1000000ULL) + (now)->tv_usec) - (((last)->tv_sec * 1000000ULL) + (last)->tv_usec))) -- 2.39.2