]> arthur.barton.de Git - netdata.git/blob - charts.d/cpufreq.chart.sh
Merge pull request #2021 from ktsaou/master
[netdata.git] / charts.d / cpufreq.chart.sh
1 # no need for shebang - this file is loaded from charts.d.plugin
2
3 # netdata
4 # real-time performance and health monitoring, done right!
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
6 # GPL v3+
7 #
8
9 # if this chart is called X.chart.sh, then all functions and global variables
10 # must start with X_
11
12 cpufreq_sys_dir="${NETDATA_HOST_PREFIX}/sys/devices"
13 cpufreq_sys_depth=10
14 cpufreq_source_update=1
15
16 # _update_every is a special variable - it holds the number of seconds
17 # between the calls of the _update() function
18 cpufreq_update_every=
19 cpufreq_priority=10000
20
21 cpufreq_find_all_files() {
22         find $1 -maxdepth $cpufreq_sys_depth -name scaling_cur_freq 2>/dev/null
23 }
24
25 # _check is called once, to find out if this chart should be enabled or not
26 cpufreq_check() {
27
28         # this should return:
29         #  - 0 to enable the chart
30         #  - 1 to disable the chart
31
32         [ -z "$( cpufreq_find_all_files $cpufreq_sys_dir )" ] && return 1
33         return 0
34 }
35
36 # _create is called once, to create the charts
37 cpufreq_create() {
38         local dir= file= id= i=
39
40         # we create a script with the source of the
41         # cpufreq_update() function
42         # - the highest speed we can achieve -
43         [ $cpufreq_source_update -eq 1 ] && echo >$TMP_DIR/cpufreq.sh "cpufreq_update() {"
44
45         echo "CHART cpu.cpufreq '' 'CPU Clock' 'MHz' 'cpufreq' '' line $((cpufreq_priority + 1)) $cpufreq_update_every"
46         echo >>$TMP_DIR/cpufreq.sh "echo \"BEGIN cpu.cpufreq \$1\""
47
48         i=0
49         for file in $( cpufreq_find_all_files $cpufreq_sys_dir | sort -u )
50         do
51                 i=$(( i + 1 ))
52                 dir=$( dirname $file )
53                 cpu=
54
55                 [ -f $dir/affected_cpus ] && cpu=$( cat $dir/affected_cpus )
56                 [ -z "$cpu" ] && cpu="$i.a"
57
58                 id="$( fixid "cpu$cpu" )"
59
60                 debug "file='$file', dir='$dir', cpu='$cpu', id='$id'"
61
62                 echo "DIMENSION $id '$id' absolute 1 1000"
63                 echo >>$TMP_DIR/cpufreq.sh "echo \"SET $id = \"\$(< $file )"
64         done
65         echo >>$TMP_DIR/cpufreq.sh "echo END"
66
67         [ $cpufreq_source_update -eq 1 ] && echo >>$TMP_DIR/cpufreq.sh "}"
68
69         # ok, load the function cpufreq_update() we created
70         [ $cpufreq_source_update -eq 1 ] && . $TMP_DIR/cpufreq.sh
71
72         return 0
73 }
74
75 # _update is called continiously, to collect the values
76 cpufreq_update() {
77         # the first argument to this function is the microseconds since last update
78         # pass this parameter to the BEGIN statement (see bellow).
79
80         # do all the work to collect / calculate the values
81         # for each dimension
82         # remember: KEEP IT SIMPLE AND SHORT
83
84         [ $cpufreq_source_update -eq 0 ] && . $TMP_DIR/cpufreq.sh $1
85
86         return 0
87 }
88