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