diff options
author | Lennart Weller <lhw@ring0.de> | 2017-01-24 15:21:09 +0000 |
---|---|---|
committer | Lennart Weller <lhw@ring0.de> | 2017-01-24 15:21:09 +0000 |
commit | 3ed3b02ed96ddab1c084811f3579b3a2aec83e04 (patch) | |
tree | 7a61ab288ae47800c4f11be5677d6ad8288dcd98 /src/clocks.c | |
parent | New upstream version 1.4.0+dfsg (diff) | |
download | netdata-3ed3b02ed96ddab1c084811f3579b3a2aec83e04.tar.xz netdata-3ed3b02ed96ddab1c084811f3579b3a2aec83e04.zip |
New upstream version 1.5.0+dfsgupstream/1.5.0+dfsg
Diffstat (limited to 'src/clocks.c')
-rw-r--r-- | src/clocks.c | 73 |
1 files changed, 73 insertions, 0 deletions
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); +} |