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