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