loopsleepms.sh.inc 6.7 KB

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