]> arthur.barton.de Git - netdata.git/blobdiff - plugins.d/loopsleepms.sh.inc
Merge pull request #17 from alonbl/build
[netdata.git] / plugins.d / loopsleepms.sh.inc
old mode 100755 (executable)
new mode 100644 (file)
index ad77008..fabd49a
@@ -1,5 +1,3 @@
-#!/bin/sh
-
 # this function is used to sleep a fraction of a second
 # it calculates the difference between every time is called
 # and tries to align the sleep time to give you exactly the
@@ -9,6 +7,31 @@ LOOPSLEEPMS_HIGHRES=2
 LOOPSLEEPMS_LASTRUN=0
 LOOPSLEEPMS_LASTSLEEP=0
 LOOPSLEEPMS_LASTWORK=0
+
+check_high_res() {
+       LOOPSLEEPMS_HIGHRES=1
+       test `date +%N` = "%N" && LOOPSLEEPMS_HIGHRES=0
+}
+
+now_ms=
+current_time_ms() {
+       # check if high resolution timer is supported
+       test $LOOPSLEEPMS_HIGHRES -eq 2 && check_high_res
+
+       # if high resolution is not supported
+       # just sleep the time requested, in seconds
+       if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]
+       then
+               local s="`date +'%s'`"
+               local m="000"
+       else
+               local d="`date +'%s.%N'`"
+               local s="`echo $d | cut -d '.' -f 1`"
+               local m="`echo $d | cut -d '.' -f 2 | cut -b 1-3`"
+       fi
+       now_ms="$s$m"
+}
+
 loopsleepms() {
        tellwork=0
        if [ "$1" = "tellwork" ]
@@ -21,11 +44,7 @@ loopsleepms() {
        local t=$1
 
        # check if high resolution timer is supported
-       if [ $LOOPSLEEPMS_HIGHRES -eq 2 ]
-       then
-               LOOPSLEEPMS_HIGHRES=1
-               test `date +%N` = "%N" && LOOPSLEEPMS_HIGHRES=0
-       fi
+       test $LOOPSLEEPMS_HIGHRES -eq 2 && check_high_res
 
        # if high resolution is not supported
        # just sleep the time requested, in seconds
@@ -36,9 +55,9 @@ loopsleepms() {
        fi
 
        # get the current time
-       local d=`date +'%s.%N'`
-       local s=`echo $d | cut -d '.' -f 1`
-       local m=`echo $d | cut -d '.' -f 2 | cut -b 1-3`
+       local d="`date +'%s.%N'`"
+       local s="`echo $d | cut -d '.' -f 1`"
+       local m="`echo $d | cut -d '.' -f 2 | cut -b 1-3`"
        local now="$s$m" # milliseconds since epoch (1-1-1970)
 
        # calculate required sleep in ms