summaryrefslogtreecommitdiffstats
path: root/libnetdata/clocks/clocks.c
diff options
context:
space:
mode:
Diffstat (limited to 'libnetdata/clocks/clocks.c')
-rw-r--r--libnetdata/clocks/clocks.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/libnetdata/clocks/clocks.c b/libnetdata/clocks/clocks.c
index f303ccddc..f7d217178 100644
--- a/libnetdata/clocks/clocks.c
+++ b/libnetdata/clocks/clocks.c
@@ -2,6 +2,8 @@
#include "../libnetdata.h"
+static int clock_boottime_valid = 1;
+
#ifndef HAVE_CLOCK_GETTIME
inline int clock_gettime(clockid_t clk_id, struct timespec *ts) {
struct timeval tv;
@@ -15,6 +17,12 @@ inline int clock_gettime(clockid_t clk_id, struct timespec *ts) {
}
#endif
+void test_clock_boottime(void) {
+ struct timespec ts;
+ if(clock_gettime(CLOCK_BOOTTIME, &ts) == -1 && errno == EINVAL)
+ clock_boottime_valid = 0;
+}
+
static inline time_t now_sec(clockid_t clk_id) {
struct timespec ts;
if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
@@ -73,15 +81,15 @@ inline int now_monotonic_timeval(struct timeval *tv) {
}
inline time_t now_boottime_sec(void) {
- return now_sec(CLOCK_BOOTTIME);
+ return now_sec(likely(clock_boottime_valid) ? CLOCK_BOOTTIME : CLOCK_MONOTONIC);
}
inline usec_t now_boottime_usec(void) {
- return now_usec(CLOCK_BOOTTIME);
+ return now_usec(likely(clock_boottime_valid) ? CLOCK_BOOTTIME : CLOCK_MONOTONIC);
}
inline int now_boottime_timeval(struct timeval *tv) {
- return now_timeval(CLOCK_BOOTTIME, tv);
+ return now_timeval(likely(clock_boottime_valid) ? CLOCK_BOOTTIME : CLOCK_MONOTONIC, tv);
}
inline usec_t timeval_usec(struct timeval *tv) {