master
inc 227 lines 6.7 KB
Raw
1 # no need for shebang - this file is included from other scripts
2 # SPDX-License-Identifier: GPL-3.0-or-later
3
4 LOOPSLEEP_DATE="$(which date 2>/dev/null || command -v date 2>/dev/null)"
5 if [ -z "$LOOPSLEEP_DATE" ]; 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 # macOS 'date' doesn't support '%N' precision
14 # echo $(/bin/date +"%N") is "N"
15 if [ "$($LOOPSLEEP_DATE +"%N")" = "N" ]; then
16 LOOPSLEEP_DATE_FORMAT="%s * 1000"
17 else
18 LOOPSLEEP_DATE_FORMAT="%s * 1000 + 10#%-N / 1000000"
19 fi
20
21 now_ms=
22 LOOPSLEEPMS_HIGHRES=1
23 test "$($LOOPSLEEP_DATE +%N)" = "%N" && LOOPSLEEPMS_HIGHRES=0
24 test -z "$($LOOPSLEEP_DATE +%N)" && LOOPSLEEPMS_HIGHRES=0
25 current_time_ms_from_date() {
26 if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]; then
27 now_ms="$($LOOPSLEEP_DATE +'%s')000"
28 else
29 now_ms="$(($($LOOPSLEEP_DATE +"$LOOPSLEEP_DATE_FORMAT")))"
30 fi
31 }
32
33 # -----------------------------------------------------------------------------
34 # use /proc/uptime as a high resolution timer
35
36 current_time_ms_from_date
37 current_time_ms_from_uptime_started="${now_ms}"
38 current_time_ms_from_uptime_last="${now_ms}"
39 current_time_ms_from_uptime_first=0
40 current_time_ms_from_uptime() {
41 local up rest arr=() n
42
43 read up rest </proc/uptime
44 if [ $? -ne 0 ]; then
45 echo >&2 "$0: Cannot read /proc/uptime - falling back to current_time_ms_from_date()."
46 current_time_ms="current_time_ms_from_date"
47 current_time_ms_from_date
48 current_time_ms_accuracy=1
49 return
50 fi
51
52 arr=(${up//./ })
53
54 if [ ${#arr[1]} -lt 1 ]; then
55 n="${arr[0]}000"
56 elif [ ${#arr[1]} -lt 2 ]; then
57 n="${arr[0]}${arr[1]}00"
58 elif [ ${#arr[1]} -lt 3 ]; then
59 n="${arr[0]}${arr[1]}0"
60 else
61 n="${arr[0]}${arr[1]}"
62 fi
63
64 now_ms=$((current_time_ms_from_uptime_started - current_time_ms_from_uptime_first + n))
65
66 if [ "${now_ms}" -lt "${current_time_ms_from_uptime_last}" ]; then
67 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()."
68 current_time_ms="current_time_ms_from_date"
69 current_time_ms_from_date
70 current_time_ms_accuracy=1
71 fi
72
73 current_time_ms_from_uptime_last="${now_ms}"
74 }
75 current_time_ms_from_uptime
76 current_time_ms_from_uptime_first="$((now_ms - current_time_ms_from_uptime_started))"
77 current_time_ms_from_uptime_last="${current_time_ms_from_uptime_first}"
78 current_time_ms="current_time_ms_from_uptime"
79 current_time_ms_accuracy=10
80 if [ "${current_time_ms_from_uptime_first}" -eq 0 ]; then
81 echo >&2 "$0: Invalid setup for current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
82 current_time_ms="current_time_ms_from_date"
83 current_time_ms_accuracy=1
84 fi
85
86 # -----------------------------------------------------------------------------
87 # use read with timeout for sleep
88
89 mysleep=""
90
91 mysleep_fifo="${NETDATA_CACHE_DIR-/tmp}/.netdata_bash_sleep_timer_fifo"
92 [ -f "${mysleep_fifo}" ] && rm "${mysleep_fifo}"
93 [ ! -p "${mysleep_fifo}" ] && mkfifo "${mysleep_fifo}"
94 [ -p "${mysleep_fifo}" ] && mysleep="mysleep_read"
95
96 mysleep_read() {
97 read -t "${1}" <>"${mysleep_fifo}"
98 ret=$?
99 if [ $ret -le 128 ]; then
100 echo >&2 "$0: Cannot use read for sleeping (return code ${ret})."
101 mysleep="sleep"
102 ${mysleep} "${1}"
103 fi
104 }
105
106 # -----------------------------------------------------------------------------
107 # use bash loadable module for sleep
108
109 mysleep_builtin() {
110 builtin sleep "${1}"
111 ret=$?
112 if [ $ret -ne 0 ]; then
113 echo >&2 "$0: Cannot use builtin sleep for sleeping (return code ${ret})."
114 mysleep="sleep"
115 ${mysleep} "${1}"
116 fi
117 }
118
119 if [ -z "${mysleep}" -a "$((BASH_VERSINFO[0] + 0))" -ge 3 -a "${NETDATA_BASH_LOADABLES}" != "DISABLE" ]; then
120 # enable modules only for bash version 3+
121
122 for bash_modules_path in ${BASH_LOADABLES_PATH//:/ } "$(pkg-config bash --variable=loadablesdir 2>/dev/null)" "/usr/lib/bash" "/lib/bash" "/lib64/bash" "/usr/local/lib/bash" "/usr/local/lib64/bash"; do
123 [ -z "${bash_modules_path}" -o ! -d "${bash_modules_path}" ] && continue
124
125 # check for sleep
126 for bash_module_sleep in "sleep" "sleep.so"; do
127 if [ -f "${bash_modules_path}/${bash_module_sleep}" ]; then
128 if enable -f "${bash_modules_path}/${bash_module_sleep}" sleep 2>/dev/null; then
129 mysleep="mysleep_builtin"
130 # echo >&2 "$0: Using bash loadable ${bash_modules_path}/${bash_module_sleep} for sleep"
131 break
132 fi
133 fi
134
135 done
136
137 [ ! -z "${mysleep}" ] && break
138 done
139 fi
140
141 # -----------------------------------------------------------------------------
142 # fallback to external sleep
143
144 [ -z "${mysleep}" ] && mysleep="sleep"
145
146 # -----------------------------------------------------------------------------
147 # this function is used to sleep a fraction of a second
148 # it calculates the difference between every time is called
149 # and tries to align the sleep time to give you exactly the
150 # loop you need.
151
152 LOOPSLEEPMS_LASTRUN=0
153 LOOPSLEEPMS_NEXTRUN=0
154 LOOPSLEEPMS_LASTSLEEP=0
155 LOOPSLEEPMS_LASTWORK=0
156
157 loopsleepms() {
158 local tellwork=0 t="${1}" div s m now mstosleep
159
160 if [ "${t}" = "tellwork" ]; then
161 tellwork=1
162 shift
163 t="${1}"
164 fi
165
166 # $t = the time in seconds to wait
167
168 # if high resolution is not supported
169 # just sleep the time requested, in seconds
170 if [ ${LOOPSLEEPMS_HIGHRES} -eq 0 ]; then
171 sleep ${t}
172 return
173 fi
174
175 # get the current time, in ms in ${now_ms}
176 ${current_time_ms}
177
178 # calculate ms since last run
179 [ ${LOOPSLEEPMS_LASTRUN} -gt 0 ] &&
180 LOOPSLEEPMS_LASTWORK=$((now_ms - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP + current_time_ms_accuracy))
181 # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
182
183 # remember this run
184 LOOPSLEEPMS_LASTRUN=${now_ms}
185
186 # calculate the next run
187 LOOPSLEEPMS_NEXTRUN=$(((now_ms - (now_ms % (t * 1000))) + (t * 1000)))
188
189 # calculate ms to sleep
190 mstosleep=$((LOOPSLEEPMS_NEXTRUN - now_ms + current_time_ms_accuracy))
191 # echo "# mstosleep is $mstosleep ms"
192
193 # if we are too slow, sleep some time
194 test ${mstosleep} -lt 200 && mstosleep=200
195
196 s=$((mstosleep / 1000))
197 m=$((mstosleep - (s * 1000)))
198 [ "${m}" -lt 100 ] && m="0${m}"
199 [ "${m}" -lt 10 ] && m="0${m}"
200
201 test $tellwork -eq 1 && echo >&2 " >>> PERFORMANCE >>> WORK TOOK ${LOOPSLEEPMS_LASTWORK} ms ( $((LOOPSLEEPMS_LASTWORK * 100 / 1000)).$((LOOPSLEEPMS_LASTWORK % 10))% cpu ) >>> SLEEPING ${mstosleep} ms"
202
203 # echo "# sleeping ${s}.${m}"
204 # echo
205 ${mysleep} ${s}.${m}
206
207 # keep the values we need
208 # for our next run
209 LOOPSLEEPMS_LASTSLEEP=$mstosleep
210 }
211
212 # test it
213 #while [ 1 ]
214 #do
215 # r=$(( (RANDOM * 2000 / 32767) ))
216 # s=$((r / 1000))
217 # m=$((r - (s * 1000)))
218 # [ "${m}" -lt 100 ] && m="0${m}"
219 # [ "${m}" -lt 10 ] && m="0${m}"
220 # echo "${r} = ${s}.${m}"
221 #
222 # # the work
223 # ${mysleep} ${s}.${m}
224 #
225 # # the alignment loop
226 # loopsleepms tellwork 1
227 #done