]> arthur.barton.de Git - netdata.git/blob - plugins.d/loopsleepms.sh.inc
tc-qos-helper.sh now uses the the forkless time function
[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_first=0
31 current_time_ms_from_uptime() {
32     local up rest arr n
33
34     read up rest </proc/uptime
35     if [ $? -ne 0 ]
36         then
37         echo >&2 "$0: Cannot read /proc/uptime - falling back to current_time_ms_from_date()."
38         current_time_ms="current_time_ms_from_date"
39         current_time_ms_from_date
40         current_time_ms_accuracy=1
41         return
42     fi
43
44     arr=(${up//./ })
45
46     if [ ${#arr[1]} -lt 1 ]
47         then
48         n="${arr[0]}000"
49     elif [ ${#arr[1]} -lt 2 ]
50         then
51         n="${arr[0]}${arr[1]}00"
52     elif [ ${#arr[1]} -lt 3 ]
53         then
54         n="${arr[0]}${arr[1]}0"
55     else
56         n="${arr[0]}${arr[1]}"
57     fi
58
59     now_ms=$((current_time_ms_from_uptime_started - current_time_ms_from_uptime_first + n))
60
61     if [ "${now_ms}" -lt "${current_time_ms_from_uptime_started}" ]
62         then
63         echo >&2 "$0: Cannot use current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
64         current_time_ms="current_time_ms_from_date"
65         current_time_ms_from_date
66         current_time_ms_accuracy=1
67     fi
68 }
69 current_time_ms_from_uptime
70 current_time_ms_from_uptime_first="$((now_ms - current_time_ms_from_uptime_started))"
71 current_time_ms="current_time_ms_from_uptime"
72 current_time_ms_accuracy=10
73 if [ "${current_time_ms_from_uptime_first}" -eq 0 ]
74     then
75     echo >&2 "$0: Invalid setup for current_time_ms_from_uptime() - falling back to current_time_ms_from_date()."
76     current_time_ms="current_time_ms_from_date"
77     current_time_ms_accuracy=1
78 fi
79
80 # -----------------------------------------------------------------------------
81 # use read with timeout for sleep
82
83 mysleep="mysleep_read"
84
85 mysleep_fifo="${NETDATA_CACHE_DIR-/tmp}/.netdata_bash_sleep_timer_fifo"
86 [ ! -e "${mysleep_fifo}" ] && mkfifo "${mysleep_fifo}"
87 [ ! -e "${mysleep_fifo}" ] && mysleep="sleep"
88
89 mysleep_read() {
90     read -t "${1}" <>"${mysleep_fifo}"
91     ret=$?
92     if [ $ret -le 128 ]
93         then
94         echo >&2 "$0: Cannot use read for sleeping (return code ${ret})."
95         mysleep="sleep"
96         ${mysleep} "${1}"
97     fi
98 }
99
100
101 # -----------------------------------------------------------------------------
102 # this function is used to sleep a fraction of a second
103 # it calculates the difference between every time is called
104 # and tries to align the sleep time to give you exactly the
105 # loop you need.
106
107 LOOPSLEEPMS_LASTRUN=0
108 LOOPSLEEPMS_NEXTRUN=0
109 LOOPSLEEPMS_LASTSLEEP=0
110 LOOPSLEEPMS_LASTWORK=0
111
112 loopsleepms() {
113     local tellwork=0 t="${1}" div s m now mstosleep
114
115     if [ "${t}" = "tellwork" ]
116     then
117         tellwork=1
118         shift
119         t="${1}"
120     fi
121
122     # $t = the time in seconds to wait
123
124     # if high resolution is not supported
125     # just sleep the time requested, in seconds
126     if [ ${LOOPSLEEPMS_HIGHRES} -eq 0 ]
127     then
128         sleep ${t}
129         return
130     fi
131
132     # get the current time, in ms
133     ${current_time_ms}
134
135     # this is our first run
136     # just wait the requested time
137     if [ ${LOOPSLEEPMS_LASTRUN} -eq 0 ]
138         then
139         LOOPSLEEPMS_LASTWORK=0
140     else
141         # calculate ms since last run
142         LOOPSLEEPMS_LASTWORK=$((now_ms - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP))
143     fi
144     # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
145
146     # remember this run
147     LOOPSLEEPMS_LASTRUN=${now_ms}
148
149     # calculate the next run    
150     LOOPSLEEPMS_NEXTRUN=$(( ( now_ms - now_ms % ( t * 1000 ) ) + ( t * 1000 ) ))
151
152     # calculate ms to sleep
153     mstosleep=$(( LOOPSLEEPMS_NEXTRUN - now_ms + current_time_ms_accuracy ))
154     # echo "# mstosleep is $mstosleep ms"
155
156     # if we are too slow, sleep some time
157     test ${mstosleep} -lt 200 && mstosleep=200
158
159     s=$((mstosleep / 1000))
160     m=$((mstosleep - (s * 1000)))
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