]> arthur.barton.de Git - netdata.git/commitdiff
introduce new clocks API using `clock_gettime()`
authorRémi Lefèvre <remi.lefevre@parrot.com>
Tue, 6 Dec 2016 11:02:29 +0000 (12:02 +0100)
committerRémi Lefèvre <remi.lefevre@parrot.com>
Sun, 11 Dec 2016 10:51:53 +0000 (11:51 +0100)
Fallbacks are provided for systems that do not support
clock_gettime(), CLOCK_MONOTONIC or CLOCK_BOOTTIME.

Signed-off-by: Rémi Lefèvre <remi.lefevre@parrot.com>
CMakeLists.txt
configure.ac
src/Makefile.am
src/clocks.c [new file with mode: 0644]
src/clocks.h [new file with mode: 0644]
src/common.c
src/common.h

index 79a3cc3f17061beee76e5521456627374aff1c75..d58e66cbe0ee67dde922cb18f750fb6d4a0d8195 100755 (executable)
@@ -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
index 9b26fcddcc584bcb7f788e6a63672206fd45af6a..f5dcf4dfdc5f85f7e22a4cb45b1742b6ce5359b0 100644 (file)
@@ -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 <time.h>]])
+AC_SEARCH_LIBS([clock_gettime], [rt posix4])
+AC_CHECK_FUNCS([clock_gettime])
 
 # Check system type
 case "$host_os" in
index 466c699354313764e396b7367955df0b7c475aba..f2ddef3d11081e2253675dcc0a8c37b6f709595a 100644 (file)
@@ -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 (file)
index 0000000..c90a07c
--- /dev/null
@@ -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 (file)
index 0000000..0db52c2
--- /dev/null
@@ -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 */
index 98093b965cd4e70a8a62eb28a44abe4f473b20ee..52e48fea2b388b22a5aee2ef81b65adc38d39f8e 100644 (file)
@@ -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;
index a6f185bc0da80949584f20c8c49c32c66f2cd82c..582149fb9273dbfa4f459ae67fa0aafbcf241b5c 100644 (file)
 #include "plugin_tc.h"
 #include "plugins_d.h"
 
+#include "clocks.h"
 #include "eval.h"
 #include "health.h"
 
 #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)))