new synchronization mechanism to align data collected to fixed steps
Costa Tsaousis (ktsaou) committed
Jan 15, 2016 at 00:41 UTC
b28cdfe02aecb58f98a29a377ea11de124527314
1 file changed
+37
-13
plugins.d/charts.d.plugin
+37
-13
@@ -205,6 +205,7 @@ fi
205
# loop control
206
207
# default sleep function
208
+LOOPSLEEPMS_HIGHRES=0
209
loopsleepms() {
210
[ "$1" = "tellwork" ] && shift
211
sleep $1
@@ -436,22 +437,26 @@ if [ -z "$run_charts" ]
437
exit 1
438
fi
439
439
-declare -A charts_last_update=() charts_min_dt=()
440
+declare -A charts_last_update=() charts_update_every=() charts_next_update=() chart_run_counter=()
441
global_update() {
441
- local exit_after=$((restart_timeout / update_every)) \
442
+ local exit_at \
443
c=0 dt ret exec_start_ms exec_end_ms \
444
chart now_charts=() next_charts=($run_charts)
445
446
# return the current time in ms in $now_ms
447
current_time_ms
448
449
+ exit_at=$(( now_ms + (restart_timeout * 1000) ))
450
+
451
for chart in $run_charts
452
do
450
- eval "charts_min_dt[$chart]=\$$chart$suffix_update_every"
451
- test -z "${charts_min_dt[$chart]}" && charts_min_dt[$charts]=$update_every
452
- charts_last_update[$chart]=$((now_ms - (charts_min_dt[$chart] * 1000) ))
453
+ eval "charts_update_every[$chart]=\$$chart$suffix_update_every"
454
+ test -z "${charts_update_every[$chart]}" && charts_update_every[$charts]=$update_every
455
+ charts_last_update[$chart]=$((now_ms - (now_ms % (charts_update_every[$chart] * 1000) ) ))
456
+ charts_next_update[$chart]=$(( charts_last_update[$chart] + (charts_update_every[$chart] * 1000) ))
457
+ charts_run_counter[$chart]=0
458
454
- echo "CHART netdata.plugin_$chart '' 'Execution time for $chart plugin' 'milliseconds / run' netdata netdata area 90000 ${charts_min_dt[$chart]}"
459
+ echo "CHART netdata.plugin_$chart '' 'Execution time for $chart plugin' 'milliseconds / run' netdata netdata area 90000 ${charts_update_every[$chart]}"
460
echo "DIMENSION run_time 'run time' absolute 1 1"
461
done
462
@@ -462,22 +467,35 @@ global_update() {
467
now_charts=("${next_charts[@]}")
468
next_charts=()
469
470
+ # return the current time in ms in $now_ms
471
+ current_time_ms
472
+
473
for chart in "${now_charts[@]}"
474
do
467
- # return the current time in ms in $now_ms
468
- current_time_ms
469
- dt=$(( (now_ms - charts_last_update[$chart]) * 1000 ))
470
- if [ $dt -ge $(( charts_min_dt[$chart] * 1000000 )) ]
475
+ # echo >&2 "DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}"
476
+ if [ ${now_ms} -ge ${charts_next_update[$chart]} ]
477
then
478
+ dt=$(( (now_ms - charts_last_update[$chart]) ))
479
+ #echo >&2 "DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}, dt: ${dt}"
480
+
481
charts_last_update[$chart]=$now_ms
482
483
+ while [ ${charts_next_update[$chart]} -lt ${now_ms} ]
484
+ do
485
+ charts_next_update[$chart]=$(( charts_next_update[$chart] + (charts_update_every[$chart] * 1000) ))
486
+ done
487
+
488
# the first call should not give a duration
489
# so that netdata calibrates to current time
476
- test $c -eq 1 && dt=
490
+ charts_run_counter[$chart]=$(( charts_run_counter[$chart] + 1 ))
491
+ test ${charts_run_counter[$chart]} -eq 1 && dt=
492
+ dt=$(( dt * 1000 ))
493
494
exec_start_ms=$now_ms
495
$chart$charts_update $dt
496
ret=$?
497
+
498
+ # return the current time in ms in $now_ms
499
current_time_ms; exec_end_ms=$now_ms
500
501
echo "BEGIN netdata.plugin_$chart $dt"
@@ -501,10 +519,16 @@ global_update() {
519
suspend || ( echo >&2 "$PROGRAM_NAME: suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every $time_divisor)
520
else
521
# wait the time you are required to
504
- loopsleepms $debug_time $update_every $time_divisor
522
+ #loopsleepms $debug_time $update_every $time_divisor
523
+ if [ ${LOOPSLEEPMS_HIGHRES} -eq 1 ]
524
+ then
525
+ sleep 0.2
526
+ else
527
+ sleep 1
528
+ fi
529
fi
530
507
- test $c -gt $exit_after && exit 0
531
+ test ${now_ms} -ge ${exit_at} && exit 0
532
done
533
}
534