@cryptotaxi247 / netdata-1 / commits / fdc86f483

fixed bug where loopsleepms was calculating the time to sleep in a faulty way. This affected the timings of tc-qos-helper.sh

Costa Tsaousis (ktsaou) committed Sep 22, 2016 at 23:48 UTC fdc86f4834831a2864cc65a2dc2c1bef51784d2b
1 file changed +33 -17
plugins.d/loopsleepms.sh.inc
+33 -17
@@ -27,9 +27,10 @@ current_time_ms_from_date() {
27
28 current_time_ms_from_date
29 current_time_ms_from_uptime_started="${now_ms}"
30 +current_time_ms_from_uptime_last="${now_ms}"
31 current_time_ms_from_uptime_first=0
32 current_time_ms_from_uptime() {
32 - local up rest arr n
33 + local up rest arr=() n
34
35 read up rest </proc/uptime
36 if [ $? -ne 0 ]
@@ -58,16 +59,19 @@ current_time_ms_from_uptime() {
59
60 now_ms=$((current_time_ms_from_uptime_started - current_time_ms_from_uptime_first + n))
61
61 - if [ "${now_ms}" -lt "${current_time_ms_from_uptime_started}" ]
62 + if [ "${now_ms}" -lt "${current_time_ms_from_uptime_last}" ]
63 then
63 - echo >&2 "$0: Cannot use current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
64 + echo >&2 "$0: Cannot use current_time_ms_from_uptime() - new time ${now_ms} is older than the last ${current_time_ms_from_uptime_last} - falling back to current_time_ms_from_date()."
65 current_time_ms="current_time_ms_from_date"
66 current_time_ms_from_date
67 current_time_ms_accuracy=1
68 fi
69 +
70 + current_time_ms_from_uptime_last="${now_ms}"
71 }
72 current_time_ms_from_uptime
73 current_time_ms_from_uptime_first="$((now_ms - current_time_ms_from_uptime_started))"
74 +current_time_ms_from_uptime_last="${current_time_ms_from_uptime_first}"
75 current_time_ms="current_time_ms_from_uptime"
76 current_time_ms_accuracy=10
77 if [ "${current_time_ms_from_uptime_first}" -eq 0 ]
@@ -129,25 +133,19 @@ loopsleepms() {
133 return
134 fi
135
132 - # get the current time, in ms
136 + # get the current time, in ms in ${now_ms}
137 ${current_time_ms}
138
135 - # this is our first run
136 - # just wait the requested time
137 - if [ ${LOOPSLEEPMS_LASTRUN} -eq 0 ]
138 - then
139 - LOOPSLEEPMS_LASTWORK=0
140 - else
141 - # calculate ms since last run
139 + # calculate ms since last run
140 + [ ${LOOPSLEEPMS_LASTRUN} -gt 0 ] && \
141 LOOPSLEEPMS_LASTWORK=$((now_ms - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP))
143 - fi
142 # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
145 -
143 +
144 # remember this run
145 LOOPSLEEPMS_LASTRUN=${now_ms}
146
149 - # calculate the next run
150 - LOOPSLEEPMS_NEXTRUN=$(( ( now_ms - now_ms % ( t * 1000 ) ) + ( t * 1000 ) ))
147 + # calculate the next run
148 + LOOPSLEEPMS_NEXTRUN=$(( ( now_ms - ( now_ms % ( t * 1000 ) ) ) + ( t * 1000 ) ))
149
150 # calculate ms to sleep
151 mstosleep=$(( LOOPSLEEPMS_NEXTRUN - now_ms + current_time_ms_accuracy ))
@@ -156,8 +154,10 @@ loopsleepms() {
154 # if we are too slow, sleep some time
155 test ${mstosleep} -lt 200 && mstosleep=200
156
159 - s=$((mstosleep / 1000))
160 - m=$((mstosleep - (s * 1000)))
157 + s=$(( mstosleep / 1000 ))
158 + m=$(( mstosleep - (s * 1000) ))
159 + [ "${m}" -lt 100 ] && m="0${m}"
160 + [ "${m}" -lt 10 ] && m="0${m}"
161
162 test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK ${LOOPSLEEPMS_LASTWORK} ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu ) >>> SLEEPING ${mstosleep} ms"
163
@@ -170,3 +170,19 @@ loopsleepms() {
170 LOOPSLEEPMS_LASTSLEEP=$mstosleep
171 }
172
173 +# test it
174 +#while [ 1 ]
175 +#do
176 +# r=$(( (RANDOM * 2000 / 32767) ))
177 +# s=$((r / 1000))
178 +# m=$((r - (s * 1000)))
179 +# [ "${m}" -lt 100 ] && m="0${m}"
180 +# [ "${m}" -lt 10 ] && m="0${m}"
181 +# echo "${r} = ${s}.${m}"
182 +#
183 +# # the work
184 +# ${mysleep} ${s}.${m}
185 +#
186 +# # the alignment loop
187 +# loopsleepms tellwork 1
188 +#done