@cryptotaxi247 / netdata-1 / commits / ff9312b7b

introduce new clocks API using `clock_gettime()`

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>

Rémi Lefèvre committed Dec 6, 2016 at 12:02 UTC ff9312b7b5708d1d1da6c58465e170bd0896556b
7 files changed +169 -5
CMakeLists.txt
+2
@@ -16,6 +16,8 @@ set(NETDATA_SOURCE_FILES
16 src/avl.h
17 src/backends.c
18 src/backends.h
19 + src/clocks.c
20 + src/clocks.h
21 src/common.c
22 src/common.h
23 src/daemon.c
configure.ac
+3
@@ -35,6 +35,9 @@ AC_PROG_INSTALL
35 PKG_PROG_PKG_CONFIG
36 AC_USE_SYSTEM_EXTENSIONS
37 AC_CHECK_FUNCS_ONCE(accept4)
38 +AC_CHECK_TYPES([struct timespec, clockid_t], [], [], [[#include <time.h>]])
39 +AC_SEARCH_LIBS([clock_gettime], [rt posix4])
40 +AC_CHECK_FUNCS([clock_gettime])
41
42 # Check system type
43 case "$host_os" in
src/Makefile.am
+2
@@ -30,6 +30,7 @@ netdata_SOURCES = \
30 appconfig.c appconfig.h \
31 avl.c avl.h \
32 backends.c backends.h \
33 + clocks.c clocks.h \
34 common.c common.h \
35 daemon.c daemon.h \
36 dictionary.c dictionary.h \
@@ -100,6 +101,7 @@ netdata_LDADD = \
101 apps_plugin_SOURCES = \
102 apps_plugin.c \
103 avl.c avl.h \
104 + clocks.c clocks.h \
105 common.c common.h \
106 log.c log.h \
107 procfile.c procfile.h \
src/clocks.c new
+73
@@ -0,0 +1,73 @@
1 +#include "common.h"
2 +
3 +#ifndef HAVE_CLOCK_GETTIME
4 +inline int clock_gettime(clockid_t clk_id, struct timespec *ts) {
5 + struct timeval tv;
6 + if(unlikely(gettimeofday(&tv, NULL) == -1))
7 + return -1;
8 + ts->tv_sec = tv.tv_sec;
9 + ts->tv_nsec = tv.tv_usec * NSEC_PER_USEC;
10 + return 0;
11 +}
12 +#endif
13 +
14 +inline time_t now_realtime_sec(void) {
15 + struct timespec ts;
16 + if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1))
17 + return 0;
18 + return ts.tv_sec;
19 +}
20 +
21 +inline int now_realtime_timeval(struct timeval *tv) {
22 + struct timespec ts;
23 + if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1))
24 + return -1;
25 + tv->tv_sec = ts.tv_sec;
26 + tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
27 + return 0;
28 +}
29 +
30 +inline usec_t now_realtime_usec(void) {
31 + struct timespec ts;
32 + if(unlikely(clock_gettime(CLOCK_REALTIME, &ts) == -1))
33 + return 0;
34 + return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC;
35 +}
36 +
37 +inline time_t now_monotonic_sec(void) {
38 + struct timespec ts;
39 + if(unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) == -1))
40 + return 0;
41 + return ts.tv_sec;
42 +}
43 +
44 +inline usec_t now_monotonic_usec(void) {
45 + struct timespec ts;
46 + if(unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) == -1))
47 + return 0;
48 + return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC;
49 +}
50 +
51 +inline time_t now_boottime_sec(void) {
52 + struct timespec ts;
53 + if(unlikely(clock_gettime(CLOCK_BOOTTIME, &ts) == -1))
54 + return 0;
55 + return ts.tv_sec;
56 +}
57 +
58 +inline usec_t now_boottime_usec(void) {
59 + struct timespec ts;
60 + if(unlikely(clock_gettime(CLOCK_BOOTTIME, &ts) == -1))
61 + return 0;
62 + return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC;
63 +}
64 +
65 +inline usec_t timeval_usec(struct timeval *tv) {
66 + return (usec_t)tv->tv_sec * USEC_PER_SEC + tv->tv_usec;
67 +}
68 +
69 +inline usec_t dt_usec(struct timeval *now, struct timeval *old) {
70 + usec_t ts1 = timeval_usec(now);
71 + usec_t ts2 = timeval_usec(old);
72 + return (ts1 > ts2) ? (ts1 - ts2) : (ts2 - ts1);
73 +}
src/clocks.h new
+88
@@ -0,0 +1,88 @@
1 +#ifndef NETDATA_CLOCKS_H
2 +#define NETDATA_CLOCKS_H 1
3 +
4 +#ifndef HAVE_STRUCT_TIMESPEC
5 +struct timespec {
6 + time_t tv_sec; /* seconds */
7 + long tv_nsec; /* nanoseconds */
8 +}
9 +#endif
10 +
11 +#ifndef HAVE_CLOCKID_T
12 +typedef int clockid_t
13 +#endif
14 +
15 +#ifndef HAVE_CLOCK_GETTIME
16 +int clock_gettime(clockid_t clk_id, struct timespec *ts);
17 +#endif
18 +
19 +/* Linux values are as good as any others */
20 +#ifndef CLOCK_REALTIME
21 +#define CLOCK_REALTIME 0
22 +#endif
23 +
24 +#ifndef CLOCK_MONOTONIC
25 +/* fallback to CLOCK_REALTIME if not available */
26 +#define CLOCK_MONOTONIC CLOCK_REALTIME
27 +#endif
28 +
29 +#ifndef CLOCK_BOOTTIME
30 +/* fallback to CLOCK_MONOTONIC if not available */
31 +#define CLOCK_BOOTTIME CLOCK_MONOTONIC
32 +#endif
33 +
34 +typedef unsigned long long usec_t;
35 +
36 +#define NSEC_PER_SEC 1000000000ULL
37 +#define NSEC_PER_MSEC 1000000ULL
38 +#define NSEC_PER_USEC 1000ULL
39 +#define USEC_PER_SEC 1000000ULL
40 +
41 +#ifndef HAVE_CLOCK_GETTIME
42 +/* Fallback function for POSIX.1-2001 clock_gettime() function.
43 + *
44 + * We use a realtime clock from gettimeofday(), this will
45 + * make systems without clock_gettime() support sensitive
46 + * to time jumps or hibernation/suspend side effects.
47 + */
48 +extern int clock_gettime(clockid_t clk_id, struct timespec *ts);
49 +#endif
50 +
51 +/* Fills struct timeval with time since EPOCH from real-time clock (i.e. wall-clock).
52 + * - Hibernation/suspend time is included
53 + * - adjtime()/NTP adjustments affect this clock
54 + * Return 0 on succes, -1 else with errno set appropriately.
55 + */
56 +extern int now_realtime_timeval(struct timeval *tv);
57 +
58 +/* Returns time since EPOCH from real-time clock (i.e. wall-clock).
59 + * - Hibernation/suspend time is included
60 + * - adjtime()/NTP adjustments affect this clock
61 + */
62 +extern time_t now_realtime_sec(void);
63 +extern usec_t now_realtime_usec(void);
64 +
65 +/* Returns time from monotonic clock if available, real-time clock else.
66 + * If monotonic clock is available:
67 + * - hibernation/suspend time is not included
68 + * - adjtime()/NTP adjusments affect this clock
69 + * If monotonic clock is not available, this fallbacks to now_realtime().
70 + */
71 +extern time_t now_monotonic_sec(void);
72 +extern usec_t now_monotonic_usec(void);
73 +
74 +/* Returns time from boottime clock if available,
75 + * monotonic clock else if available, real-time clock else.
76 + * If boottime clock is available:
77 + * - hibernation/suspend time is included
78 + * - adjtime()/NTP adjusments affect this clock
79 + * If boottime clock is not available, this fallbacks to now_monotonic().
80 + * If monotonic clock is not available, this fallbacks to now_realtime().
81 + */
82 +extern time_t now_boottime_sec(void);
83 +extern usec_t now_boottime_usec(void);
84 +
85 +extern usec_t timeval_usec(struct timeval *ts);
86 +extern usec_t dt_usec(struct timeval *now, struct timeval *old);
87 +
88 +#endif /* NETDATA_CLOCKS_H */
src/common.c
-4
@@ -200,10 +200,6 @@ void freez(void *ptr) {
200 // ----------------------------------------------------------------------------
201 // time functions
202
203 -inline unsigned long long timeval_usec(struct timeval *tv) {
204 - return tv->tv_sec * 1000000ULL + tv->tv_usec;
205 -}
206 -
203 // time(NULL) in nanoseconds
204 inline unsigned long long time_usec(void) {
205 struct timeval now;
src/common.h
+1 -1
@@ -123,6 +123,7 @@
123 #include "plugin_tc.h"
124 #include "plugins_d.h"
125
126 +#include "clocks.h"
127 #include "eval.h"
128 #include "health.h"
129
@@ -146,7 +147,6 @@
147 #define abs(x) ((x < 0)? -x : x)
148
149 extern unsigned long long usec_dt(struct timeval *now, struct timeval *old);
149 -extern unsigned long long timeval_usec(struct timeval *tv);
150
151 // #define usec_dt(now, last) (((((now)->tv_sec * 1000000ULL) + (now)->tv_usec) - (((last)->tv_sec * 1000000ULL) + (last)->tv_usec)))
152