convert backends to heartbeat API
As heartbeats are based on a monotonic clock that is different on each system, the previously used random delay to avoid all netdata servers sending their data simultaneously is not needed anymore. Also compute backend latency using monotonic clock. Signed-off-by: Rémi Lefèvre <remi.lefevre@parrot.com>
Rémi Lefèvre committed
Dec 21, 2016 at 12:50 UTC
28ff79260eab04c79a24b17a0be134375200d78b
1 file changed
+10
-21
src/backends.c
+10
-21
@@ -292,30 +292,19 @@ void *backends_main(void *ptr) {
292
info("BACKEND configured ('%s' on '%s' sending '%s' data, every %d seconds, as host '%s', with prefix '%s')", type, destination, source, frequency, hostname, prefix);
293
294
usec_t step_ut = frequency * USEC_PER_SEC;
295
- usec_t random_ut = now_realtime_usec() % (step_ut / 2);
296
- time_t before = (time_t)((now_realtime_usec() - step_ut) / USEC_PER_SEC);
297
- time_t after = before;
295
+ time_t after = now_realtime_sec();
296
int failures = 0;
297
+ heartbeat_t hb;
298
+ heartbeat_init(&hb);
299
300
for(;;) {
301
// ------------------------------------------------------------------------
302
- // wait for the next iteration point
303
-
304
- usec_t now_ut = now_realtime_usec();
305
- usec_t next_ut = now_ut - (now_ut % step_ut) + step_ut;
306
- before = (time_t)(next_ut / USEC_PER_SEC);
307
-
308
- // add a little delay (1/4 of the step) plus some randomness
309
- next_ut += (step_ut / 4) + random_ut;
310
-
311
- while(now_ut < next_ut) {
312
- sleep_usec(next_ut - now_ut);
313
- now_ut = now_realtime_usec();
314
- }
302
+ // Wait for the next iteration point.
303
+ heartbeat_next(&hb, step_ut);
304
+ time_t before = now_realtime_sec();
305
306
// ------------------------------------------------------------------------
307
// add to the buffer the data we need to send to the backend
318
-
308
RRDSET *st;
309
int pthreadoldcancelstate;
310
@@ -399,7 +388,7 @@ void *backends_main(void *ptr) {
388
// if we are not connected, connect to a backend server
389
390
if(unlikely(sock == -1)) {
402
- usec_t start_ut = now_realtime_usec();
391
+ usec_t start_ut = now_monotonic_usec();
392
const char *s = destination;
393
while(*s) {
394
const char *e = s;
@@ -420,7 +409,7 @@ void *backends_main(void *ptr) {
409
if(sock != -1) break;
410
s = e;
411
}
423
- chart_backend_latency += now_realtime_usec() - start_ut;
412
+ chart_backend_latency += now_monotonic_usec() - start_ut;
413
}
414
415
if(unlikely(netdata_exit)) break;
@@ -430,14 +419,14 @@ void *backends_main(void *ptr) {
419
420
if(likely(sock != -1)) {
421
size_t len = buffer_strlen(b);
433
- usec_t start_ut = now_realtime_usec();
422
+ usec_t start_ut = now_monotonic_usec();
423
int flags = 0;
424
#ifdef MSG_NOSIGNAL
425
flags += MSG_NOSIGNAL;
426
#endif
427
428
ssize_t written = send(sock, buffer_tostring(b), len, flags);
440
- chart_backend_latency += now_realtime_usec() - start_ut;
429
+ chart_backend_latency += now_monotonic_usec() - start_ut;
430
if(written != -1 && (size_t)written == len) {
431
// we sent the data successfully
432
chart_transmission_successes++;