loopsleepms.sh.inc 7.1 KB

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