]> arthur.barton.de Git - netdata.git/blobdiff - plugins.d/loopsleepms.sh.inc
ab-debian 0.20170327.01-0ab1, upstream v1.6.0-42-gaa6b96fc
[netdata.git] / plugins.d / loopsleepms.sh.inc
index 40ace1fe949080f7b0ff9c4ca2f7011f85fb66fc..ef3db192d4a1bc9ea440748489739b83798d226e 100644 (file)
@@ -1,6 +1,6 @@
 # no need for shebang - this file is included from other scripts
 
-LOOPSLEEP_DATE="$(which date)"
+LOOPSLEEP_DATE="$(which date 2>/dev/null || command -v date 2>/dev/null)"
 if [ -z "$LOOPSLEEP_DATE" ]
     then
     echo >&2 "$0: ERROR: Cannot find the command 'date' in the system path."
@@ -13,6 +13,7 @@ fi
 now_ms=
 LOOPSLEEPMS_HIGHRES=1
 test "$($LOOPSLEEP_DATE +%N)" = "%N" && LOOPSLEEPMS_HIGHRES=0
+test -z "$($LOOPSLEEP_DATE +%N)"     && LOOPSLEEPMS_HIGHRES=0
 current_time_ms_from_date() {
     if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]
     then
@@ -27,9 +28,10 @@ current_time_ms_from_date() {
 
 current_time_ms_from_date
 current_time_ms_from_uptime_started="${now_ms}"
+current_time_ms_from_uptime_last="${now_ms}"
 current_time_ms_from_uptime_first=0
 current_time_ms_from_uptime() {
-    local up rest arr n
+    local up rest arr=() n
 
     read up rest </proc/uptime
     if [ $? -ne 0 ]
@@ -58,16 +60,19 @@ current_time_ms_from_uptime() {
 
     now_ms=$((current_time_ms_from_uptime_started - current_time_ms_from_uptime_first + n))
 
-    if [ "${now_ms}" -lt "${current_time_ms_from_uptime_started}" ]
+    if [ "${now_ms}" -lt "${current_time_ms_from_uptime_last}" ]
         then
-        echo >&2 "$0: Cannot use current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
+        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()."
         current_time_ms="current_time_ms_from_date"
         current_time_ms_from_date
         current_time_ms_accuracy=1
     fi
+
+    current_time_ms_from_uptime_last="${now_ms}"
 }
 current_time_ms_from_uptime
 current_time_ms_from_uptime_first="$((now_ms - current_time_ms_from_uptime_started))"
+current_time_ms_from_uptime_last="${current_time_ms_from_uptime_first}"
 current_time_ms="current_time_ms_from_uptime"
 current_time_ms_accuracy=10
 if [ "${current_time_ms_from_uptime_first}" -eq 0 ]
@@ -129,25 +134,19 @@ loopsleepms() {
         return
     fi
 
-    # get the current time, in ms
+    # get the current time, in ms in ${now_ms}
     ${current_time_ms}
 
-    # this is our first run
-    # just wait the requested time
-    if [ ${LOOPSLEEPMS_LASTRUN} -eq 0 ]
-        then
-        LOOPSLEEPMS_LASTWORK=0
-    else
-        # calculate ms since last run
-        LOOPSLEEPMS_LASTWORK=$((now_ms - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP))
-    fi
+    # calculate ms since last run
+    [ ${LOOPSLEEPMS_LASTRUN} -gt 0 ] && \
+        LOOPSLEEPMS_LASTWORK=$((now_ms - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP + current_time_ms_accuracy))
     # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
-
+    
     # remember this run
     LOOPSLEEPMS_LASTRUN=${now_ms}
 
-    # calculate the next run    
-    LOOPSLEEPMS_NEXTRUN=$(( ( now_ms - now_ms % ( t * 1000 ) ) + ( t * 1000 ) ))
+    # calculate the next run
+    LOOPSLEEPMS_NEXTRUN=$(( ( now_ms - ( now_ms % ( t * 1000 ) ) ) + ( t * 1000 ) ))
 
     # calculate ms to sleep
     mstosleep=$(( LOOPSLEEPMS_NEXTRUN - now_ms + current_time_ms_accuracy ))
@@ -156,8 +155,10 @@ loopsleepms() {
     # if we are too slow, sleep some time
     test ${mstosleep} -lt 200 && mstosleep=200
 
-    s=$((mstosleep / 1000))
-    m=$((mstosleep - (s * 1000)))
+    s=$(( mstosleep / 1000 ))
+    m=$(( mstosleep - (s * 1000) ))
+    [ "${m}" -lt 100 ] && m="0${m}"
+    [ "${m}" -lt 10  ] && m="0${m}"
 
     test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK ${LOOPSLEEPMS_LASTWORK} ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu ) >>> SLEEPING ${mstosleep} ms"
 
@@ -170,3 +171,19 @@ loopsleepms() {
     LOOPSLEEPMS_LASTSLEEP=$mstosleep
 }
 
+# test it
+#while [ 1 ]
+#do
+#    r=$(( (RANDOM * 2000 / 32767) ))
+#    s=$((r / 1000))
+#    m=$((r - (s * 1000)))
+#    [ "${m}" -lt 100 ] && m="0${m}"
+#    [ "${m}" -lt 10  ] && m="0${m}"
+#    echo "${r} = ${s}.${m}"
+#
+#    # the work
+#    ${mysleep} ${s}.${m}
+#
+#    # the alignment loop
+#    loopsleepms tellwork 1
+#done