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