]> arthur.barton.de Git - netdata.git/blob - plugins.d/loopsleepms.sh.inc
ab-debian 0.20170327.01-0ab1, upstream v1.6.0-42-gaa6b96fc
[netdata.git] / plugins.d / loopsleepms.sh.inc
1 # no need for shebang - this file is included from other scripts
2
3 LOOPSLEEP_DATE="$(which date 2>/dev/null || command -v date 2>/dev/null)"
4 if [ -z "$LOOPSLEEP_DATE" ]
5     then
6     echo >&2 "$0: ERROR: Cannot find the command 'date' in the system path."
7     exit 1
8 fi
9
10 # -----------------------------------------------------------------------------
11 # use the date command as a high resolution timer
12
13 now_ms=
14 LOOPSLEEPMS_HIGHRES=1
15 test "$($LOOPSLEEP_DATE +%N)" = "%N" && LOOPSLEEPMS_HIGHRES=0
16 test -z "$($LOOPSLEEP_DATE +%N)"     && LOOPSLEEPMS_HIGHRES=0
17 current_time_ms_from_date() {
18     if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]
19     then
20         now_ms="$($LOOPSLEEP_DATE +'%s')000"
21     else
22         now_ms="$(( $( $LOOPSLEEP_DATE +'%s * 1000 + %-N / 1000000' ) ))"
23     fi
24 }
25
26 # -----------------------------------------------------------------------------
27 # use /proc/uptime as a high resolution timer
28
29 current_time_ms_from_date
30 current_time_ms_from_uptime_started="${now_ms}"
31 current_time_ms_from_uptime_last="${now_ms}"
32 current_time_ms_from_uptime_first=0
33 current_time_ms_from_uptime() {
34     local up rest arr=() n
35
36     read up rest </proc/uptime
37     if [ $? -ne 0 ]
38         then
39         echo >&2 "$0: Cannot read /proc/uptime - falling back to current_time_ms_from_date()."
40         current_time_ms="current_time_ms_from_date"
41         current_time_ms_from_date
42         current_time_ms_accuracy=1
43         return
44     fi
45
46     arr=(${up//./ })
47
48     if [ ${#arr[1]} -lt 1 ]
49         then
50         n="${arr[0]}000"
51     elif [ ${#arr[1]} -lt 2 ]
52         then
53         n="${arr[0]}${arr[1]}00"
54     elif [ ${#arr[1]} -lt 3 ]
55         then
56         n="${arr[0]}${arr[1]}0"
57     else
58         n="${arr[0]}${arr[1]}"
59     fi
60
61     now_ms=$((current_time_ms_from_uptime_started - current_time_ms_from_uptime_first + n))
62
63     if [ "${now_ms}" -lt "${current_time_ms_from_uptime_last}" ]
64         then
65         echo >&2 "$0: Cannot use current_time_ms_from_uptime() - new time ${now_ms} is older than the last ${current_time_ms_from_uptime_last} - falling back to current_time_ms_from_date()."
66         current_time_ms="current_time_ms_from_date"
67         current_time_ms_from_date
68         current_time_ms_accuracy=1
69     fi
70
71     current_time_ms_from_uptime_last="${now_ms}"
72 }
73 current_time_ms_from_uptime
74 current_time_ms_from_uptime_first="$((now_ms - current_time_ms_from_uptime_started))"
75 current_time_ms_from_uptime_last="${current_time_ms_from_uptime_first}"
76 current_time_ms="current_time_ms_from_uptime"
77 current_time_ms_accuracy=10
78 if [ "${current_time_ms_from_uptime_first}" -eq 0 ]
79     then
80     echo >&2 "$0: Invalid setup for current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
81     current_time_ms="current_time_ms_from_date"
82     current_time_ms_accuracy=1
83 fi
84
85 # -----------------------------------------------------------------------------
86 # use read with timeout for sleep
87
88 mysleep="mysleep_read"
89
90 mysleep_fifo="${NETDATA_CACHE_DIR-/tmp}/.netdata_bash_sleep_timer_fifo"
91 [ ! -e "${mysleep_fifo}" ] && mkfifo "${mysleep_fifo}"
92 [ ! -e "${mysleep_fifo}" ] && mysleep="sleep"
93
94 mysleep_read() {
95     read -t "${1}" <>"${mysleep_fifo}"
96     ret=$?
97     if [ $ret -le 128 ]
98         then
99         echo >&2 "$0: Cannot use read for sleeping (return code ${ret})."
100         mysleep="sleep"
101         ${mysleep} "${1}"
102     fi
103 }
104
105
106 # -----------------------------------------------------------------------------
107 # this function is used to sleep a fraction of a second
108 # it calculates the difference between every time is called
109 # and tries to align the sleep time to give you exactly the
110 # loop you need.
111
112 LOOPSLEEPMS_LASTRUN=0
113 LOOPSLEEPMS_NEXTRUN=0
114 LOOPSLEEPMS_LASTSLEEP=0
115 LOOPSLEEPMS_LASTWORK=0
116
117 loopsleepms() {
118     local tellwork=0 t="${1}" div s m now mstosleep
119
120     if [ "${t}" = "tellwork" ]
121     then
122         tellwork=1
123         shift
124         t="${1}"
125     fi
126
127     # $t = the time in seconds to wait
128
129     # if high resolution is not supported
130     # just sleep the time requested, in seconds
131     if [ ${LOOPSLEEPMS_HIGHRES} -eq 0 ]
132     then
133         sleep ${t}
134         return
135     fi
136
137     # get the current time, in ms in ${now_ms}
138     ${current_time_ms}
139
140     # calculate ms since last run
141     [ ${LOOPSLEEPMS_LASTRUN} -gt 0 ] && \
142         LOOPSLEEPMS_LASTWORK=$((now_ms - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP + current_time_ms_accuracy))
143     # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
144     
145     # remember this run
146     LOOPSLEEPMS_LASTRUN=${now_ms}
147
148     # calculate the next run
149     LOOPSLEEPMS_NEXTRUN=$(( ( now_ms - ( now_ms % ( t * 1000 ) ) ) + ( t * 1000 ) ))
150
151     # calculate ms to sleep
152     mstosleep=$(( LOOPSLEEPMS_NEXTRUN - now_ms + current_time_ms_accuracy ))
153     # echo "# mstosleep is $mstosleep ms"
154
155     # if we are too slow, sleep some time
156     test ${mstosleep} -lt 200 && mstosleep=200
157
158     s=$(( mstosleep / 1000 ))
159     m=$(( mstosleep - (s * 1000) ))
160     [ "${m}" -lt 100 ] && m="0${m}"
161     [ "${m}" -lt 10  ] && m="0${m}"
162
163     test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK ${LOOPSLEEPMS_LASTWORK} ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu ) >>> SLEEPING ${mstosleep} ms"
164
165     # echo "# sleeping ${s}.${m}"
166     # echo
167     ${mysleep} ${s}.${m}
168
169     # keep the values we need
170     # for our next run
171     LOOPSLEEPMS_LASTSLEEP=$mstosleep
172 }
173
174 # test it
175 #while [ 1 ]
176 #do
177 #    r=$(( (RANDOM * 2000 / 32767) ))
178 #    s=$((r / 1000))
179 #    m=$((r - (s * 1000)))
180 #    [ "${m}" -lt 100 ] && m="0${m}"
181 #    [ "${m}" -lt 10  ] && m="0${m}"
182 #    echo "${r} = ${s}.${m}"
183 #
184 #    # the work
185 #    ${mysleep} ${s}.${m}
186 #
187 #    # the alignment loop
188 #    loopsleepms tellwork 1
189 #done