]> arthur.barton.de Git - netdata.git/blob - plugins.d/loopsleepms.sh.inc
fixed charts.d.plugin to get directories from netdata server; netdata now exports...
[netdata.git] / plugins.d / loopsleepms.sh.inc
1 #!/bin/bash
2
3 # this function is used to sleep a fraction of a second
4 # it calculates the difference between every time is called
5 # and tries to align the sleep time to give you exactly the
6 # loop you need.
7
8 LOOPSLEEP_DATE="$(which date)"
9 if [ -z "$LOOPSLEEP_DATE" ]
10         then
11         echo >&2 "$0: ERROR: Cannot find the command 'date' in the system path."
12         exit 1
13 fi
14
15 LOOPSLEEPMS_LASTRUN=0
16 LOOPSLEEPMS_LASTSLEEP=0
17 LOOPSLEEPMS_LASTWORK=0
18
19 LOOPSLEEPMS_HIGHRES=1
20 test "$($LOOPSLEEP_DATE +%N)" = "%N" && LOOPSLEEPMS_HIGHRES=0
21
22 now_ms=
23 current_time_ms() {
24         # if high resolution is not supported
25         # just sleep the time requested, in seconds
26         if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]
27         then
28                 now_ms="$($LOOPSLEEP_DATE +'%s')000"
29         else
30                 now_ms="$(( $( $LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000' ) ))"
31         fi
32 }
33
34 loopsleepms() {
35         local tellwork=0 t="$1" s m now mstosleep
36
37         if [ "$t" = "tellwork" ]
38         then
39                 tellwork=1
40                 shift
41                 t="$1"
42         fi
43
44         # $t = the time in seconds to wait
45
46         # if high resolution is not supported
47         # just sleep the time requested, in seconds
48         if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]
49         then
50                 sleep $t
51                 return
52         fi
53
54         # get the current time, in ms
55         # milliseconds since epoch (1-1-1970)
56         now="$(( $( $LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000' ) ))"
57
58         # calculate required sleep in ms
59         t=$((t * 1000))
60
61         # this is our first run
62         # just wait the requested time
63         test $LOOPSLEEPMS_LASTRUN -eq 0 && LOOPSLEEPMS_LASTRUN=$now
64
65         # calculate ms since last run
66         LOOPSLEEPMS_LASTWORK=$((now - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP))
67         # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
68
69         test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK $LOOPSLEEPMS_LASTWORK ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu )"
70
71         # calculate ms to sleep
72         mstosleep=$((t - LOOPSLEEPMS_LASTWORK))
73         # echo "# mstosleep is $mstosleep ms"
74         
75         # if we are too slow, sleep some time
76         test $mstosleep -lt $((t / 2)) && mstosleep=$((t / 2))
77
78         s=$((mstosleep / 1000))
79         m=$((mstosleep - (s * 1000)))
80
81         # echo "# sleeping $s.$m"
82         # echo
83         sleep $s.$m
84
85         # keep the values we need
86         # for our next run
87         LOOPSLEEPMS_LASTRUN=$now
88         LOOPSLEEPMS_LASTSLEEP=$mstosleep
89 }