loopsleepms.sh.inc 6.5 KB

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