tc-qos-helper.sh now uses the the forkless time function
Costa Tsaousis (ktsaou) committed
Sep 18, 2016 at 19:04 UTC
82a40232b32f6276c12940b093de67aa3246b825
1 file changed
+26
-21
plugins.d/loopsleepms.sh.inc
+26
-21
@@ -105,63 +105,68 @@ mysleep_read() {
105
# loop you need.
106
107
LOOPSLEEPMS_LASTRUN=0
108
+LOOPSLEEPMS_NEXTRUN=0
109
LOOPSLEEPMS_LASTSLEEP=0
110
LOOPSLEEPMS_LASTWORK=0
111
112
loopsleepms() {
112
- local tellwork=0 t="$1" div s m now mstosleep
113
+ local tellwork=0 t="${1}" div s m now mstosleep
114
114
- if [ "$t" = "tellwork" ]
115
+ if [ "${t}" = "tellwork" ]
116
then
117
tellwork=1
118
shift
118
- t="$1"
119
+ t="${1}"
120
fi
120
- div="${2-100}"
121
122
# $t = the time in seconds to wait
123
124
# if high resolution is not supported
125
# just sleep the time requested, in seconds
126
- if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]
126
+ if [ ${LOOPSLEEPMS_HIGHRES} -eq 0 ]
127
then
128
- sleep $t
128
+ sleep ${t}
129
return
130
fi
131
132
# get the current time, in ms
133
- # milliseconds since epoch (1-1-1970)
134
- now="$(( $( $LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000' ) ))"
135
-
136
- # calculate required sleep in ms
137
- t=$((t * 1000 * div / 100))
133
+ ${current_time_ms}
134
135
# this is our first run
136
# just wait the requested time
141
- test $LOOPSLEEPMS_LASTRUN -eq 0 && LOOPSLEEPMS_LASTRUN=$now
142
-
143
- # calculate ms since last run
144
- LOOPSLEEPMS_LASTWORK=$((now - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP))
137
+ if [ ${LOOPSLEEPMS_LASTRUN} -eq 0 ]
138
+ then
139
+ LOOPSLEEPMS_LASTWORK=0
140
+ else
141
+ # calculate ms since last run
142
+ LOOPSLEEPMS_LASTWORK=$((now_ms - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP))
143
+ fi
144
# echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
145
146
+ # remember this run
147
+ LOOPSLEEPMS_LASTRUN=${now_ms}
148
+
149
+ # calculate the next run
150
+ LOOPSLEEPMS_NEXTRUN=$(( ( now_ms - now_ms % ( t * 1000 ) ) + ( t * 1000 ) ))
151
+
152
# calculate ms to sleep
148
- mstosleep=$(( t - LOOPSLEEPMS_LASTWORK + current_time_ms_accuracy ))
153
+ mstosleep=$(( LOOPSLEEPMS_NEXTRUN - now_ms + current_time_ms_accuracy ))
154
# echo "# mstosleep is $mstosleep ms"
155
156
# if we are too slow, sleep some time
152
- test $mstosleep -lt 200 && mstosleep=200
157
+ test ${mstosleep} -lt 200 && mstosleep=200
158
159
s=$((mstosleep / 1000))
160
m=$((mstosleep - (s * 1000)))
161
157
- test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK $LOOPSLEEPMS_LASTWORK ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu ) >>> SLEEPING $mstosleep ms"
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
159
- # echo "# sleeping $s.$m"
164
+ # echo "# sleeping ${s}.${m}"
165
# echo
161
- ${mysleep} $s.$m
166
+ ${mysleep} ${s}.${m}
167
168
# keep the values we need
169
# for our next run
165
- LOOPSLEEPMS_LASTRUN=$now
170
LOOPSLEEPMS_LASTSLEEP=$mstosleep
171
}
172
+