@cryptotaxi247 / netdata-1 / commits / ad1c637e0

precise looping (at the ms level) for scripts

Costa Tsaousis (ktsaou) committed Apr 29, 2014 at 00:34 UTC ad1c637e0ce8f0301952c939f582ee289fed675e
3 files changed +176 -1
charts.d/loopsleepms.sh.inc new
+65
@@ -0,0 +1,65 @@
1 +#!/bin/sh
2 +
3 +# this function is used to sleep a fraction of a second
4 +# it calculates the difference between every time is called
5 +# and tries to align the sleep time to give you exactly the
6 +# loop you need.
7 +
8 +LOOPSLEEPMS_HIGHRES=2
9 +LOOPSLEEPMS_LASTRUN=0
10 +LOOPSLEEPMS_LASTSLEEP=0
11 +loopsleepms() {
12 + # the time in seconds to wait, as the first argument
13 + local t=$1
14 +
15 + # check if high resolution timer is supported
16 + if [ $LOOPSLEEPMS_HIGHRES -eq 2 ]
17 + then
18 + LOOPSLEEPMS_HIGHRES=1
19 + test `date +%N` = "%N" && LOOPSLEEPMS_HIGHRES=0
20 + fi
21 +
22 + # if high resolution is not supported
23 + # just sleep the time requested, in seconds
24 + if [ $LOOPSLEEPMS_HIGHRES -eq 0 ]
25 + then
26 + sleep $t
27 + return
28 + fi
29 +
30 + # get the current time
31 + local d=`date +'%s.%N'`
32 + local s=`echo $d | cut -d '.' -f 1`
33 + local m=`echo $d | cut -d '.' -f 2 | cut -b 1-3`
34 + local now="$s$m" # milliseconds since epoch (1-1-1970)
35 +
36 + # calculate required sleep in ms
37 + t=$((t * 1000))
38 +
39 + # this is our first run
40 + # just wait the requested time
41 + test $LOOPSLEEPMS_LASTRUN -eq 0 && LOOPSLEEPMS_LASTRUN=$now
42 +
43 + # calculate ms since last run
44 + local workms=$((now - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP))
45 + # echo "# last loop's work took $workms ms"
46 +
47 + # calculate ms to sleep
48 + local mstosleep=$((t - workms))
49 + # echo "# mstosleep is $mstosleep ms"
50 +
51 + # if we are too slow, sleep some time
52 + test $mstosleep -lt 100 && mstosleep=100
53 +
54 + local s=$((mstosleep / 1000))
55 + local m=$((mstosleep - (s * 1000)))
56 +
57 + # echo "# sleeping $s.$m"
58 + # echo
59 + sleep $s.$m
60 +
61 + # keep the values we need
62 + # for our next run
63 + LOOPSLEEPMS_LASTRUN=$now
64 + LOOPSLEEPMS_LASTSLEEP=$mstosleep
65 +}
charts.d/skeleton-chart.sh new
+100
@@ -0,0 +1,100 @@
1 +#!/bin/sh
2 +
3 +# default sleep function
4 +loopsleepms() {
5 + sleep $1
6 +}
7 +# if found and included, this file overwrites loopsleepms()
8 +# with a high resolution timer function for precise looping.
9 +. "`dirname $0`/loopsleepms.sh.inc"
10 +
11 +# netdata passes the requested update frequency as the first argument
12 +update_every=$1
13 +update_every=$(( update_every + 1 - 1)) # makes sure it is a number
14 +test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
15 +
16 +# report the PID back to netdata
17 +# this is required for netdata to kill this process when it exits
18 +echo "MYPID $$"
19 +
20 +# -----------------------------------------------------------------------------
21 +# create a new chart
22 +# > CHART family.chartname group[=type] homegroup[=family] charttype priority[=1000]
23 +# charttype = line or area or stacked
24 +# homegroup = any name or the word 'none' which hides the chart from the home web page
25 +#
26 +# set the chart title
27 +# > TITLE My Super Title
28 +#
29 +# set the units of measurement
30 +# > UNITS my wonderfull unit
31 +#
32 +# you can overwrite the update frequency as you need
33 +# > UPDATE EVERY $update_every
34 +#
35 +# create all the dimensions you need
36 +# > DIMENSION CREATE dimensionname1 algorithm signed|unsigned byte|char|int|long|long long"
37 +#
38 +# algorithms:
39 +# absolute
40 +# the value is to drawn as-is
41 +#
42 +# incremental
43 +# the value increases over time
44 +# the difference from the last value is drawn
45 +#
46 +# percentage-of-absolute-row
47 +# the % is drawn of this value compared to the total of all dimensions
48 +#
49 +# percentage-of-incremental-row
50 +# the % is drawn of this value compared to the differential total of
51 +# each dimension
52 +#
53 +# number sizes:
54 +# unsigned byte = 1 byte = 0 ... 255
55 +# unsigned int = 2 bytes = 0 ... 65.535
56 +# unsigned long = 4 bytes = 0 ... 4.294.967.295
57 +# unsigned long long = 8 bytes = 0 ... 18.446.744.073.709.551.615
58 +#
59 +# signed values are from - to + the half of the above
60 +#
61 +
62 +cat <<EOF
63 +CHART example.random ExampleGroup ExampleCategory stacked 1
64 +TITLE Random Numbers Example Chart
65 +UNITS random numbers
66 +UPDATE EVERY $update_every
67 +DIMENSION number1 absolute unsigned int
68 +DIMENSION number2 absolute unsigned int
69 +DIMENSION number3 absolute unsigned int
70 +EOF
71 +
72 +# You can create more charts if you like.
73 +# Just add more chart definitions.
74 +
75 +while [ 1 ]
76 +do
77 + # do all the work to calculate the numbers
78 +
79 + value1=$RANDOM
80 + value2=$RANDOM
81 + value3=$RANDOM
82 +
83 +
84 + # write the result of the work.
85 + # it is important to keep this short.
86 + # between BEGIN and END statements the clients are blocked access to the chart.
87 + cat <<VALUESEOF
88 +BEGIN example.random
89 +SET number1 = $value1
90 +SET number2 = $value2
91 +SET number3 = $value3
92 +END
93 +VALUESEOF
94 +
95 + # if you have more charts, add BEGIN->END statements here
96 +
97 + # wait the time you are required to
98 + loopsleepms $update_every
99 +done
100 +
tc-all.sh
+11 -1
@@ -1,5 +1,15 @@
1 #!/bin/sh
2
3 +# default sleep function
4 +loopsleepms() {
5 + sleep $1
6 +}
7 +# if found and included, this file overwrites loopsleepms()
8 +# with a high resolution timer function for precise looping.
9 +. "`dirname $0`/charts.d/loopsleepms.sh.inc"
10 +
11 +# let netdata know our PID
12 +# this is required to kill us when it exits
13 echo "MYPID $$"
14
15 # check if we have a valid number for interval
@@ -80,5 +90,5 @@ do
90
91 [ $gc -gt $exit_after ] && exit 0
92
83 - sleep $sleep_time
93 + loopsleepms $sleep_time
94 done