]> arthur.barton.de Git - netdata.git/blob - src/clocks.c
Merge branch 'master' into ab-debian
[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 % USEC_PER_SEC) * 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_SEC) / NSEC_PER_USEC;
26 }
27
28 static inline int now_timeval(clockid_t clk_id, struct timeval *tv) {
29     struct timespec ts;
30
31     if(unlikely(clock_gettime(clk_id, &ts) == -1)) {
32         tv->tv_sec = 0;
33         tv->tv_usec = 0;
34         return -1;
35     }
36
37     tv->tv_sec = ts.tv_sec;
38     tv->tv_usec = (suseconds_t)((ts.tv_nsec % NSEC_PER_SEC) / NSEC_PER_USEC);
39     return 0;
40 }
41
42 inline time_t now_realtime_sec(void) {
43     return now_sec(CLOCK_REALTIME);
44 }
45
46 inline usec_t now_realtime_usec(void) {
47     return now_usec(CLOCK_REALTIME);
48 }
49
50 inline int now_realtime_timeval(struct timeval *tv) {
51     return now_timeval(CLOCK_REALTIME, tv);
52 }
53
54 inline time_t now_monotonic_sec(void) {
55     return now_sec(CLOCK_MONOTONIC);
56 }
57
58 inline usec_t now_monotonic_usec(void) {
59     return now_usec(CLOCK_MONOTONIC);
60 }
61
62 inline int now_monotonic_timeval(struct timeval *tv) {
63     return now_timeval(CLOCK_MONOTONIC, tv);
64 }
65
66 inline time_t now_boottime_sec(void) {
67     return now_sec(CLOCK_BOOTTIME);
68 }
69
70 inline usec_t now_boottime_usec(void) {
71     return now_usec(CLOCK_BOOTTIME);
72 }
73
74 inline int now_boottime_timeval(struct timeval *tv) {
75     return now_timeval(CLOCK_BOOTTIME, tv);
76 }
77
78 inline usec_t timeval_usec(struct timeval *tv) {
79     return (usec_t)tv->tv_sec * USEC_PER_SEC + (tv->tv_usec % USEC_PER_SEC);
80 }
81
82 inline msec_t timeval_msec(struct timeval *tv) {
83     return (msec_t)tv->tv_sec * MSEC_PER_SEC + ((tv->tv_usec % USEC_PER_SEC) / MSEC_PER_SEC);
84 }
85
86 inline susec_t dt_usec_signed(struct timeval *now, struct timeval *old) {
87     usec_t ts1 = timeval_usec(now);
88     usec_t ts2 = timeval_usec(old);
89
90     if(likely(ts1 >= ts2)) return (susec_t)(ts1 - ts2);
91     return -((susec_t)(ts2 - ts1));
92 }
93
94 inline usec_t dt_usec(struct timeval *now, struct timeval *old) {
95     usec_t ts1 = timeval_usec(now);
96     usec_t ts2 = timeval_usec(old);
97     return (ts1 > ts2) ? (ts1 - ts2) : (ts2 - ts1);
98 }
99
100 inline void heartbeat_init(heartbeat_t *hb)
101 {
102     *hb = 0ULL;
103 }
104
105 usec_t heartbeat_next(heartbeat_t *hb, usec_t tick)
106 {
107     heartbeat_t now = now_monotonic_usec();
108     usec_t next = now - (now % tick) + tick;
109
110     while(now < next) {
111         sleep_usec(next - now);
112         now = now_monotonic_usec();
113     }
114
115     if(likely(*hb != 0ULL)) {
116         usec_t dt = now - *hb;
117         *hb = now;
118         if(unlikely(dt / tick > 1))
119             error("heartbeat missed between %llu usec and %llu usec", *hb, now);
120         return dt;
121     }
122     else {
123         *hb = now;
124         return 0ULL;
125     }
126 }
127
128 inline usec_t heartbeat_dt_usec(heartbeat_t *hb)
129 {
130     if(!*hb)
131         return 0ULL;
132     return now_monotonic_usec() - *hb;
133 }