]> arthur.barton.de Git - netdata.git/blob - src/clocks.c
Merge pull request #1368 from rlefevre/time-improvements
[netdata.git] / src / clocks.c
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 static inline time_t now_sec(clockid_t clk_id) {
15     struct timespec ts;
16     if(unlikely(clock_gettime(clk_id, &ts) == -1))
17         return 0;
18     return ts.tv_sec;
19 }
20
21 static inline usec_t now_usec(clockid_t clk_id) {
22     struct timespec ts;
23     if(unlikely(clock_gettime(clk_id, &ts) == -1))
24         return 0;
25     return (usec_t)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC;
26 }
27
28 static inline int now_timeval(clockid_t clk_id, struct timeval *tv) {
29     struct timespec ts;
30     if(unlikely(clock_gettime(clk_id, &ts) == -1))
31         return -1;
32     tv->tv_sec = ts.tv_sec;
33     tv->tv_usec = ts.tv_nsec / NSEC_PER_USEC;
34     return 0;
35 }
36
37 inline time_t now_realtime_sec(void) {
38     return now_sec(CLOCK_REALTIME);
39 }
40
41 inline usec_t now_realtime_usec(void) {
42     return now_usec(CLOCK_REALTIME);
43 }
44
45 inline int now_realtime_timeval(struct timeval *tv) {
46     return now_timeval(CLOCK_REALTIME, tv);
47 }
48
49 inline time_t now_monotonic_sec(void) {
50     return now_sec(CLOCK_MONOTONIC);
51 }
52
53 inline usec_t now_monotonic_usec(void) {
54     return now_usec(CLOCK_MONOTONIC);
55 }
56
57 inline int now_monotonic_timeval(struct timeval *tv) {
58     return now_timeval(CLOCK_MONOTONIC, tv);
59 }
60
61 inline time_t now_boottime_sec(void) {
62     return now_sec(CLOCK_BOOTTIME);
63 }
64
65 inline usec_t now_boottime_usec(void) {
66     return now_usec(CLOCK_BOOTTIME);
67 }
68
69 inline int now_boottime_timeval(struct timeval *tv) {
70     return now_timeval(CLOCK_BOOTTIME, tv);
71 }
72
73 inline usec_t timeval_usec(struct timeval *tv) {
74     return (usec_t)tv->tv_sec * USEC_PER_SEC + tv->tv_usec;
75 }
76
77 inline usec_t dt_usec(struct timeval *now, struct timeval *old) {
78     usec_t ts1 = timeval_usec(now);
79     usec_t ts2 = timeval_usec(old);
80     return (ts1 > ts2) ? (ts1 - ts2) : (ts2 - ts1);
81 }
82
83 inline void heartbeat_init(heartbeat_t *hb)
84 {
85     *hb = 0ULL;
86 }
87
88 usec_t heartbeat_next(heartbeat_t *hb, usec_t tick)
89 {
90     heartbeat_t now = now_monotonic_usec();
91     usec_t next = now - (now % tick) + tick;
92
93     while(now < next) {
94         sleep_usec(next - now);
95         now = now_monotonic_usec();
96     }
97
98     if(likely(*hb != 0ULL)) {
99         usec_t dt = now - *hb;
100         *hb = now;
101         if(unlikely(dt / tick > 1))
102             error("heartbeat missed between %llu usec and %llu usec", *hb, now);
103         return dt;
104     }
105     else {
106         *hb = now;
107         return 0ULL;
108     }
109 }
110
111 inline usec_t heartbeat_dt_usec(heartbeat_t *hb)
112 {
113     if(!*hb)
114         return 0ULL;
115     return now_monotonic_usec() - *hb;
116 }