heartbeat now reports durations in realtime
Costa Tsaousis (ktsaou) committed
Jul 18, 2018 at 01:34 UTC
2d3d545828501055235d3c1e45ae240a648729c1
6 files changed
+40
-26
src/clocks.c
+32
-20
@@ -107,39 +107,51 @@ inline usec_t dt_usec(struct timeval *now, struct timeval *old) {
107
108
inline void heartbeat_init(heartbeat_t *hb)
109
{
110
- *hb = 0ULL;
110
+ hb->monotonic = hb->realtime = 0ULL;
111
}
112
113
-usec_t heartbeat_next(heartbeat_t *hb, usec_t tick)
114
-{
115
- heartbeat_t now = now_monotonic_usec();
116
- usec_t next = now - (now % tick) + tick;
113
+// waits for the next heartbeat
114
+// it waits using the monotonic clock
115
+// it returns the dt using the realtime clock
116
+
117
+usec_t heartbeat_next(heartbeat_t *hb, usec_t tick) {
118
+ heartbeat_t now;
119
+ now.monotonic = now_monotonic_usec();
120
+ now.realtime = now_realtime_usec();
121
118
- while(now < next) {
119
- sleep_usec(next - now);
120
- now = now_monotonic_usec();
122
+ usec_t next_monotonic = now.monotonic - (now.monotonic % tick) + tick;
123
+
124
+ while(now.monotonic < next_monotonic) {
125
+ sleep_usec(next_monotonic - now.monotonic);
126
+ now.monotonic = now_monotonic_usec();
127
+ now.realtime = now_realtime_usec();
128
}
129
123
- if(likely(*hb != 0ULL)) {
124
- usec_t dt = now - *hb;
125
- *hb = now;
130
+ if(likely(hb->realtime != 0ULL)) {
131
+ usec_t dt_monotonic = now.monotonic - hb->monotonic;
132
+ usec_t dt_realtime = now.realtime - hb->realtime;
133
+
134
+ hb->monotonic = now.monotonic;
135
+ hb->realtime = now.realtime;
136
127
- if(unlikely(dt >= tick + tick / 2)) {
137
+ if(unlikely(dt_monotonic >= tick + tick / 2)) {
138
errno = 0;
129
- error("heartbeat missed %llu microseconds", dt - tick);
139
+ error("heartbeat missed %llu monotonic microseconds", dt_monotonic - tick);
140
}
141
132
- return dt;
142
+ return dt_realtime;
143
}
144
else {
135
- *hb = now;
145
+ hb->monotonic = now.monotonic;
146
+ hb->realtime = now.realtime;
147
return 0ULL;
148
}
149
}
150
140
-inline usec_t heartbeat_dt_usec(heartbeat_t *hb)
141
-{
142
- if(!*hb)
143
- return 0ULL;
144
- return now_monotonic_usec() - *hb;
151
+// returned the elapsed time, since the last heartbeat
152
+// using the monotonic clock
153
+
154
+inline usec_t heartbeat_monotonic_dt_to_now_usec(heartbeat_t *hb) {
155
+ if(!hb || !hb->monotonic) return 0ULL;
156
+ return now_monotonic_usec() - hb->monotonic;
157
}
src/clocks.h
+5
-2
@@ -18,7 +18,10 @@ typedef unsigned long long msec_t;
18
typedef unsigned long long usec_t;
19
typedef long long susec_t;
20
21
-typedef usec_t heartbeat_t;
21
+typedef struct heartbeat {
22
+ usec_t monotonic;
23
+ usec_t realtime;
24
+} heartbeat_t;
25
26
/* Linux value is as good as any other */
27
#ifndef CLOCK_REALTIME
@@ -120,6 +123,6 @@ extern void heartbeat_init(heartbeat_t *hb);
123
extern usec_t heartbeat_next(heartbeat_t *hb, usec_t tick);
124
125
/* Returns elapsed time in microseconds since last heartbeat */
123
-extern usec_t heartbeat_dt_usec(heartbeat_t *hb);
126
+extern usec_t heartbeat_monotonic_dt_to_now_usec(heartbeat_t *hb);
127
128
#endif /* NETDATA_CLOCKS_H */
src/plugin_freebsd.c
+1
-1
@@ -114,7 +114,7 @@ void *freebsd_main(void *ptr) {
114
debug(D_PROCNETDEV_LOOP, "FREEBSD calling %s.", pm->name);
115
116
pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
117
- pm->duration = heartbeat_dt_usec(&hb) - duration;
117
+ pm->duration = heartbeat_monotonic_dt_to_now_usec(&hb) - duration;
118
duration += pm->duration;
119
120
if(unlikely(netdata_exit)) break;
src/plugin_nfacct.c
-1
@@ -789,7 +789,6 @@ void *nfacct_main(void *ptr) {
789
heartbeat_t hb;
790
heartbeat_init(&hb);
791
for(;;) {
792
- heartbeat_dt_usec(&hb);
792
heartbeat_next(&hb, step);
793
794
if(unlikely(netdata_exit)) break;
src/plugin_proc.c
+1
-1
@@ -109,7 +109,7 @@ void *proc_main(void *ptr) {
109
debug(D_PROCNETDEV_LOOP, "PROC calling %s.", pm->name);
110
111
pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
112
- pm->duration = heartbeat_dt_usec(&hb) - duration;
112
+ pm->duration = heartbeat_monotonic_dt_to_now_usec(&hb) - duration;
113
duration += pm->duration;
114
115
if(unlikely(netdata_exit)) break;
src/plugin_proc_diskspace.c
+1
-1
@@ -360,7 +360,7 @@ void *proc_diskspace_main(void *ptr) {
360
heartbeat_t hb;
361
heartbeat_init(&hb);
362
while(!netdata_exit) {
363
- duration = heartbeat_dt_usec(&hb);
363
+ duration = heartbeat_monotonic_dt_to_now_usec(&hb);
364
/* usec_t hb_dt = */ heartbeat_next(&hb, step);
365
366
if(unlikely(netdata_exit)) break;