From 1746898cefcb17f58b5cf27b4dad3d28236f1152 Mon Sep 17 00:00:00 2001 From: Lennart Weller Date: Mon, 5 Sep 2016 10:27:21 +0200 Subject: Imported Upstream version 1.3.0+dfsg --- plugins.d/loopsleepms.sh.inc | 108 +++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 54 deletions(-) (limited to 'plugins.d/loopsleepms.sh.inc') diff --git a/plugins.d/loopsleepms.sh.inc b/plugins.d/loopsleepms.sh.inc index 2e22de3d8..02ab694d2 100644 --- a/plugins.d/loopsleepms.sh.inc +++ b/plugins.d/loopsleepms.sh.inc @@ -1,4 +1,4 @@ -#!/bin/bash +# no need for shebang - this file is included from other scripts # this function is used to sleep a fraction of a second # it calculates the difference between every time is called @@ -7,9 +7,9 @@ LOOPSLEEP_DATE="$(which date)" if [ -z "$LOOPSLEEP_DATE" ] - then - echo >&2 "$0: ERROR: Cannot find the command 'date' in the system path." - exit 1 + then + echo >&2 "$0: ERROR: Cannot find the command 'date' in the system path." + exit 1 fi LOOPSLEEPMS_LASTRUN=0 @@ -21,70 +21,70 @@ test "$($LOOPSLEEP_DATE +%N)" = "%N" && LOOPSLEEPMS_HIGHRES=0 now_ms= current_time_ms() { - # if high resolution is not supported - # just sleep the time requested, in seconds - if [ $LOOPSLEEPMS_HIGHRES -eq 0 ] - then - now_ms="$($LOOPSLEEP_DATE +'%s')000" - else - now_ms="$(( $( $LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000' ) ))" - fi + # if high resolution is not supported + # just sleep the time requested, in seconds + if [ $LOOPSLEEPMS_HIGHRES -eq 0 ] + then + now_ms="$($LOOPSLEEP_DATE +'%s')000" + else + now_ms="$(( $( $LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000' ) ))" + fi } loopsleepms() { - local tellwork=0 t="$1" div s m now mstosleep + local tellwork=0 t="$1" div s m now mstosleep - if [ "$t" = "tellwork" ] - then - tellwork=1 - shift - t="$1" - fi - div="${2-100}" + if [ "$t" = "tellwork" ] + then + tellwork=1 + shift + t="$1" + fi + div="${2-100}" - # $t = the time in seconds to wait + # $t = the time in seconds to wait - # if high resolution is not supported - # just sleep the time requested, in seconds - if [ $LOOPSLEEPMS_HIGHRES -eq 0 ] - then - sleep $t - return - fi + # if high resolution is not supported + # just sleep the time requested, in seconds + if [ $LOOPSLEEPMS_HIGHRES -eq 0 ] + then + sleep $t + return + fi - # get the current time, in ms - # milliseconds since epoch (1-1-1970) - now="$(( $( $LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000' ) ))" + # get the current time, in ms + # milliseconds since epoch (1-1-1970) + now="$(( $( $LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000' ) ))" - # calculate required sleep in ms - t=$((t * 1000 * div / 100)) + # calculate required sleep in ms + t=$((t * 1000 * div / 100)) - # this is our first run - # just wait the requested time - test $LOOPSLEEPMS_LASTRUN -eq 0 && LOOPSLEEPMS_LASTRUN=$now + # this is our first run + # just wait the requested time + test $LOOPSLEEPMS_LASTRUN -eq 0 && LOOPSLEEPMS_LASTRUN=$now - # calculate ms since last run - LOOPSLEEPMS_LASTWORK=$((now - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP)) - # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms" + # calculate ms since last run + LOOPSLEEPMS_LASTWORK=$((now - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP)) + # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms" - # calculate ms to sleep - mstosleep=$(( t - LOOPSLEEPMS_LASTWORK )) - # echo "# mstosleep is $mstosleep ms" + # calculate ms to sleep + mstosleep=$(( t - LOOPSLEEPMS_LASTWORK )) + # echo "# mstosleep is $mstosleep ms" - # if we are too slow, sleep some time - test $mstosleep -lt 200 && mstosleep=200 + # 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))) - test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK $LOOPSLEEPMS_LASTWORK ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu ) >>> SLEEPING $mstosleep ms" + test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK $LOOPSLEEPMS_LASTWORK ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu ) >>> SLEEPING $mstosleep ms" - # echo "# sleeping $s.$m" - # echo - sleep $s.$m + # echo "# sleeping $s.$m" + # echo + sleep $s.$m - # keep the values we need - # for our next run - LOOPSLEEPMS_LASTRUN=$now - LOOPSLEEPMS_LASTSLEEP=$mstosleep + # keep the values we need + # for our next run + LOOPSLEEPMS_LASTRUN=$now + LOOPSLEEPMS_LASTSLEEP=$mstosleep } -- cgit v1.2.3