charts.d.plugin and tc-qos-helper.sh optimization to avoid frequent forks due to date and sleep
Costa Tsaousis (ktsaou) committed
Sep 12, 2016 at 23:47 UTC
4bd921279f0e2b9fe30de07529af87787bf5cd65
4 files changed
+129
-79
charts.d/example.chart.sh
+2
-2
@@ -48,8 +48,8 @@ example_get() {
48
else
49
example_count=$((1 + (RANDOM * 5 / 32767) ))
50
51
- [ $example_last -gt 16383 -a $example_value4 -gt 16383 ] && example_value4=$((value4 - 16383))
52
- [ $example_last -le 16383 -a $example_value4 -lt 16383 ] && example_value4=$((value4 + 16383))
51
+ [ $example_last -gt 16383 -a $example_value4 -gt 16383 ] && example_value4=$((example_value4 - 16383))
52
+ [ $example_last -le 16383 -a $example_value4 -lt 16383 ] && example_value4=$((example_value4 + 16383))
53
fi
54
example_last=$example_value4
55
plugins.d/charts.d.plugin
+44
-55
@@ -48,10 +48,6 @@ require_cmd awk || exit 1
48
# insternal defaults
49
# netdata exposes a few environment variables for us
50
51
-pause_method="sleep" # use either "suspend" or "sleep"
52
- # DO NOT USE SUSPEND - LINUX WILL SUSPEND NETDATA TOO
53
- # THE WHOLE PROCESS GROUP - NOT JUST THE SHELL
54
-
51
pluginsd="${NETDATA_PLUGINS_DIR}"
52
[ -z "$pluginsd" ] && pluginsd="$( dirname $PROGRAM_FILE )"
53
@@ -153,6 +149,24 @@ do
149
done
150
151
152
+# -----------------------------------------------------------------------------
153
+# loop control
154
+
155
+# default sleep function
156
+LOOPSLEEPMS_HIGHRES=0
157
+now_ms=
158
+current_time_ms_default() {
159
+ now_ms="$(date +'%s')000"
160
+}
161
+current_time_ms="current_time_ms_default"
162
+current_time_ms_accuracy=1
163
+mysleep="sleep"
164
+
165
+# if found and included, this file overwrites loopsleepms()
166
+# and current_time_ms() with a high resolution timer function
167
+# for precise looping.
168
+. "$pluginsd/loopsleepms.sh.inc"
169
+
170
# -----------------------------------------------------------------------------
171
# load my configuration
172
@@ -172,13 +186,6 @@ else
186
echo >&2 "$PROGRAM_NAME: configuration file '$myconfig' not found. Using defaults."
187
fi
188
175
-if [ "$pause_method" = "suspend" ]
176
-then
177
- # enable bash job control
178
- # this is required for suspend to work
179
- set -m
180
-fi
181
-
189
# we check for the timeout command, after we load our
190
# configuration, so that the user may overwrite the
191
# timeout command we use, providing a function that
@@ -204,27 +211,6 @@ if [ ! -d "$chartsd" ]
211
fi
212
213
207
-# -----------------------------------------------------------------------------
208
-# loop control
209
-
210
-# default sleep function
211
-LOOPSLEEPMS_HIGHRES=0
212
-loopsleepms() {
213
- [ "$1" = "tellwork" ] && shift
214
- sleep $1
215
-}
216
-
217
-now_ms=
218
-current_time_ms() {
219
- now_ms="$(date +'%s')000"
220
-}
221
-
222
-# if found and included, this file overwrites loopsleepms()
223
-# and current_time_ms() with a high resolution timer function
224
-# for precise looping.
225
-. "$pluginsd/loopsleepms.sh.inc"
226
-
227
-
214
# -----------------------------------------------------------------------------
215
# library functions
216
@@ -537,7 +523,7 @@ global_update() {
523
next_ms x seconds millis
524
525
# return the current time in ms in $now_ms
540
- current_time_ms
526
+ ${current_time_ms}
527
528
exit_at=$(( now_ms + (restart_timeout * 1000) ))
529
@@ -562,16 +548,16 @@ global_update() {
548
next_charts=()
549
550
# return the current time in ms in $now_ms
565
- current_time_ms
551
+ ${current_time_ms}
552
553
for chart in "${now_charts[@]}"
554
do
569
- # echo >&2 "DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}"
555
+ #echo >&2 " DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}"
556
if [ ${now_ms} -ge ${charts_next_update[$chart]} ]
557
then
558
last_ms=${charts_last_update[$chart]}
559
dt=$(( (now_ms - last_ms) ))
574
- # echo >&2 "DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}, dt: ${dt}"
560
+ #echo >&2 " DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}, dt: ${dt}"
561
562
charts_last_update[$chart]=${now_ms}
563
@@ -590,11 +576,12 @@ global_update() {
576
fi
577
578
exec_start_ms=$now_ms
579
+ #echo >&2 " EXEC: $chart$charts_update $dt"
580
$chart$charts_update $dt
581
ret=$?
582
583
# return the current time in ms in $now_ms
597
- current_time_ms; exec_end_ms=$now_ms
584
+ ${current_time_ms}; exec_end_ms=$now_ms
585
586
echo "BEGIN netdata.plugin_chartsd_$chart $dt"
587
echo "SET run_time = $(( exec_end_ms - exec_start_ms ))"
@@ -620,27 +607,29 @@ global_update() {
607
fi
608
done
609
623
- if [ "$pause_method" = "suspend" ]
624
- then
625
- echo "STOPPING_WAKE_ME_UP_PLEASE"
626
- suspend || ( echo >&2 "$PROGRAM_NAME: suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every $time_divisor)
627
- else
628
- # wait the time you are required to
629
- next_ms=$((now_ms + (update_every * 1000 * 100) ))
630
- for x in "${charts_next_update[@]}"; do [ ${x} -lt ${next_ms} ] && next_ms=${x}; done
631
- next_ms=$((next_ms - now_ms))
610
+ # wait the time you are required to
611
+ next_ms=$((now_ms + (update_every * 1000 * 100) ))
612
+ for x in "${charts_next_update[@]}"; do [ ${x} -lt ${next_ms} ] && next_ms=${x}; done
613
+ next_ms=$((next_ms - now_ms))
614
633
- if [ ${LOOPSLEEPMS_HIGHRES} -eq 1 -a ${next_ms} -gt 0 ]
615
+ if [ ${LOOPSLEEPMS_HIGHRES} -eq 1 -a ${next_ms} -gt 0 ]
616
+ then
617
+ next_ms=$(( next_ms + current_time_ms_accuracy ))
618
+ seconds=$(( next_ms / 1000 ))
619
+ millis=$(( next_ms % 1000 ))
620
+ if [ ${millis} -lt 10 ]
621
then
635
- seconds=$(( next_ms / 1000 ))
636
- millis=$(( next_ms % 1000 ))
637
- [ ${millis} -lt 10 ] && millis="0${millis}"
638
- [ ${millis} -lt 100 ] && millis="0${millis}"
639
- [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: sleeping for ${seconds}.${millis} seconds."
640
- sleep ${seconds}.${millis}
641
- else
642
- sleep $update_every
622
+ millis="00${millis}"
623
+ elif [ ${millis} -lt 100 ]
624
+ then
625
+ millis="0${millis}"
626
fi
627
+
628
+ [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: sleeping for ${seconds}.${millis} seconds."
629
+ ${mysleep} ${seconds}.${millis}
630
+ else
631
+ [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: sleeping for ${update_every} seconds."
632
+ ${mysleep} $update_every
633
fi
634
635
test ${now_ms} -ge ${exit_at} && exit 0
plugins.d/loopsleepms.sh.inc
+83
-15
@@ -1,10 +1,5 @@
1
# no need for shebang - this file is included from other scripts
2
3
-# this function is used to sleep a fraction of a second
4
-# it calculates the difference between every time is called
5
-# and tries to align the sleep time to give you exactly the
6
-# loop you need.
7
-
3
LOOPSLEEP_DATE="$(which date)"
4
if [ -z "$LOOPSLEEP_DATE" ]
5
then
@@ -12,17 +7,13 @@ if [ -z "$LOOPSLEEP_DATE" ]
7
exit 1
8
fi
9
15
-LOOPSLEEPMS_LASTRUN=0
16
-LOOPSLEEPMS_LASTSLEEP=0
17
-LOOPSLEEPMS_LASTWORK=0
10
+# -----------------------------------------------------------------------------
11
+# use the date command as a high resolution timer
12
13
+now_ms=
14
LOOPSLEEPMS_HIGHRES=1
15
test "$($LOOPSLEEP_DATE +%N)" = "%N" && LOOPSLEEPMS_HIGHRES=0
21
-
22
-now_ms=
23
-current_time_ms() {
24
- # if high resolution is not supported
25
- # just sleep the time requested, in seconds
16
+current_time_ms_from_date() {
17
if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]
18
then
19
now_ms="$($LOOPSLEEP_DATE +'%s')000"
@@ -31,6 +22,83 @@ current_time_ms() {
22
fi
23
}
24
25
+# -----------------------------------------------------------------------------
26
+# use /proc/uptime as a high resolution timer
27
+
28
+current_time_ms_from_date
29
+current_time_ms_from_uptime_started="${now_ms}"
30
+current_time_ms_from_uptime_first=0
31
+current_time_ms_from_uptime() {
32
+ local up rest arr n
33
+
34
+ read up rest </proc/uptime
35
+ arr=(${up//./ })
36
+
37
+ if [ ${#arr[1]} -lt 1 ]
38
+ then
39
+ n="${arr[0]}000"
40
+ elif [ ${#arr[1]} -lt 2 ]
41
+ then
42
+ n="${arr[0]}${arr[1]}00"
43
+ elif [ ${#arr[1]} -lt 3 ]
44
+ then
45
+ n="${arr[0]}${arr[1]}0"
46
+ else
47
+ n="${arr[0]}${arr[1]}"
48
+ fi
49
+
50
+ now_ms=$((current_time_ms_from_uptime_started - current_time_ms_from_uptime_first + n))
51
+
52
+ if [ "${now_ms}" -lt "${current_time_ms_from_uptime_started}" ]
53
+ then
54
+ echo >&2 "$0: Cannot use current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
55
+ current_time_ms="current_time_ms_from_date"
56
+ current_time_ms_from_date
57
+ current_time_ms_accuracy=1
58
+ fi
59
+}
60
+current_time_ms_from_uptime
61
+current_time_ms_from_uptime_first="$((now_ms - current_time_ms_from_uptime_started))"
62
+current_time_ms="current_time_ms_from_uptime"
63
+current_time_ms_accuracy=10
64
+if [ "${current_time_ms_from_uptime_first}" -eq 0 ]
65
+ then
66
+ echo >&2 "$0: Invalid setup for current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
67
+ current_time_ms="current_time_ms_from_date"
68
+ current_time_ms_accuracy=1
69
+fi
70
+
71
+# -----------------------------------------------------------------------------
72
+# use read with timeout for sleep
73
+
74
+mysleep="mysleep_read"
75
+
76
+mysleep_fifo="${NETDATA_CACHE_DIR-/tmp}/.netdata_bash_sleep_timer_fifo"
77
+[ -e "${mysleep_fifo}" ] && rm "${mysleep_fifo}"
78
+mkfifo "${mysleep_fifo}" || mysleep="sleep"
79
+
80
+mysleep_read() {
81
+ read -t "${1}" <>"${mysleep_fifo}"
82
+ ret=$?
83
+ if [ $ret -le 128 ]
84
+ then
85
+ echo >&2 "$0: Cannot use read for sleeping (return code ${ret})."
86
+ mysleep="sleep"
87
+ ${mysleep} "${1}"
88
+ fi
89
+}
90
+
91
+
92
+# -----------------------------------------------------------------------------
93
+# this function is used to sleep a fraction of a second
94
+# it calculates the difference between every time is called
95
+# and tries to align the sleep time to give you exactly the
96
+# loop you need.
97
+
98
+LOOPSLEEPMS_LASTRUN=0
99
+LOOPSLEEPMS_LASTSLEEP=0
100
+LOOPSLEEPMS_LASTWORK=0
101
+
102
loopsleepms() {
103
local tellwork=0 t="$1" div s m now mstosleep
104
@@ -68,7 +136,7 @@ loopsleepms() {
136
# echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
137
138
# calculate ms to sleep
71
- mstosleep=$(( t - LOOPSLEEPMS_LASTWORK ))
139
+ mstosleep=$(( t - LOOPSLEEPMS_LASTWORK + current_time_ms_accuracy ))
140
# echo "# mstosleep is $mstosleep ms"
141
142
# if we are too slow, sleep some time
@@ -81,7 +149,7 @@ loopsleepms() {
149
150
# echo "# sleeping $s.$m"
151
# echo
84
- sleep $s.$m
152
+ ${mysleep} $s.$m
153
154
# keep the values we need
155
# for our next run
plugins.d/tc-qos-helper.sh
-7
@@ -27,16 +27,9 @@ if [ -f "${config_dir}/tc-qos-helper.conf" ]
27
source "${config_dir}/tc-qos-helper.conf"
28
fi
29
30
-# default time function
31
-now_ms=
32
-current_time_ms() {
33
- now_ms="$(date +'%s')000"
34
-}
35
-
30
# default sleep function
31
LOOPSLEEPMS_LASTWORK=0
32
loopsleepms() {
39
- [ "$1" = "tellwork" ] && shift
33
sleep $1
34
}
35