aboutsummaryrefslogtreecommitdiff
path: root/collector/cpu_linux.go
diff options
context:
space:
mode:
authorBen Kochie <superq@gmail.com>2018-10-18 17:28:19 +0200
committerGitHub <noreply@github.com>2018-10-18 17:28:19 +0200
commita0a164defb80cfb1f66cd86ec18e377fe68b8bb5 (patch)
treec0568c4f30d27e2b0d2a5ee443fabd08442741fc /collector/cpu_linux.go
parentef7a02dfa82daa26ac94356ff0fb3afb091552da (diff)
downloadprometheus_node_collector-a0a164defb80cfb1f66cd86ec18e377fe68b8bb5.tar.bz2
prometheus_node_collector-a0a164defb80cfb1f66cd86ec18e377fe68b8bb5.tar.xz
prometheus_node_collector-a0a164defb80cfb1f66cd86ec18e377fe68b8bb5.zip
Update cpufreq metrics collector (#1117)
* Update Linux cpufreq collector to use new procfs library functions. * Split thermal throttle collection to a separate function. * Add new required fixtures and repack ttar file. Signed-off-by: Ben Kochie <superq@gmail.com>
Diffstat (limited to 'collector/cpu_linux.go')
-rw-r--r--collector/cpu_linux.go68
1 files changed, 41 insertions, 27 deletions
diff --git a/collector/cpu_linux.go b/collector/cpu_linux.go
index e2a3122..640873f 100644
--- a/collector/cpu_linux.go
+++ b/collector/cpu_linux.go
@@ -17,14 +17,13 @@ package collector
17 17
18import ( 18import (
19 "fmt" 19 "fmt"
20 "os"
21 "path/filepath" 20 "path/filepath"
22 "strconv" 21 "strconv"
23 "strings"
24 22
25 "github.com/prometheus/client_golang/prometheus" 23 "github.com/prometheus/client_golang/prometheus"
26 "github.com/prometheus/common/log" 24 "github.com/prometheus/common/log"
27 "github.com/prometheus/procfs" 25 "github.com/prometheus/procfs"
26 "github.com/prometheus/procfs/sysfs"
28) 27)
29 28
30type cpuCollector struct { 29type cpuCollector struct {
@@ -86,46 +85,61 @@ func (c *cpuCollector) Update(ch chan<- prometheus.Metric) error {
86 if err := c.updateCPUfreq(ch); err != nil { 85 if err := c.updateCPUfreq(ch); err != nil {
87 return err 86 return err
88 } 87 }
88 if err := c.updateThermalThrottle(ch); err != nil {
89 return err
90 }
89 return nil 91 return nil
90} 92}
91 93
92// updateCPUfreq reads /sys/devices/system/cpu/cpu* and expose cpu frequency statistics. 94// updateCPUfreq reads /sys/devices/system/cpu/cpu* and expose cpu frequency statistics.
93func (c *cpuCollector) updateCPUfreq(ch chan<- prometheus.Metric) error { 95func (c *cpuCollector) updateCPUfreq(ch chan<- prometheus.Metric) error {
96 fs, err := sysfs.NewFS(*sysPath)
97 if err != nil {
98 return fmt.Errorf("failed to open sysfs: %v", err)
99 }
100
101 cpuFreqs, err := fs.NewSystemCpufreq()
102 if err != nil {
103 return err
104 }
105
106 // sysfs cpufreq values are kHz, thus multiply by 1000 to export base units (hz).
107 // See https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt
108 for _, stats := range cpuFreqs {
109 ch <- prometheus.MustNewConstMetric(
110 c.cpuFreq,
111 prometheus.GaugeValue,
112 float64(stats.CurrentFrequency)*1000.0,
113 stats.Name,
114 )
115 ch <- prometheus.MustNewConstMetric(
116 c.cpuFreqMin,
117 prometheus.GaugeValue,
118 float64(stats.MinimumFrequency)*1000.0,
119 stats.Name,
120 )
121 ch <- prometheus.MustNewConstMetric(
122 c.cpuFreqMax,
123 prometheus.GaugeValue,
124 float64(stats.MaximumFrequency)*1000.0,
125 stats.Name,
126 )
127 }
128 return nil
129}
130
131// updateThermalThrottle reads /sys/devices/system/cpu/cpu* and expose thermal throttle statistics.
132func (c *cpuCollector) updateThermalThrottle(ch chan<- prometheus.Metric) error {
94 cpus, err := filepath.Glob(sysFilePath("devices/system/cpu/cpu[0-9]*")) 133 cpus, err := filepath.Glob(sysFilePath("devices/system/cpu/cpu[0-9]*"))
95 if err != nil { 134 if err != nil {
96 return err 135 return err
97 } 136 }
98 137
99 var value uint64
100 packageThrottles := make(map[uint64]uint64) 138 packageThrottles := make(map[uint64]uint64)
101 packageCoreThrottles := make(map[uint64]map[uint64]uint64) 139 packageCoreThrottles := make(map[uint64]map[uint64]uint64)
102 140
103 // cpu loop 141 // cpu loop
104 for _, cpu := range cpus { 142 for _, cpu := range cpus {
105 _, cpuName := filepath.Split(cpu)
106 cpuNum := strings.TrimPrefix(cpuName, "cpu")
107
108 if _, err := os.Stat(filepath.Join(cpu, "cpufreq")); os.IsNotExist(err) {
109 log.Debugf("CPU %v is missing cpufreq", cpu)
110 } else {
111 // sysfs cpufreq values are kHz, thus multiply by 1000 to export base units (hz).
112 // See https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt
113 if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_cur_freq")); err != nil {
114 return err
115 }
116 ch <- prometheus.MustNewConstMetric(c.cpuFreq, prometheus.GaugeValue, float64(value)*1000.0, cpuNum)
117
118 if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_min_freq")); err != nil {
119 return err
120 }
121 ch <- prometheus.MustNewConstMetric(c.cpuFreqMin, prometheus.GaugeValue, float64(value)*1000.0, cpuNum)
122
123 if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_max_freq")); err != nil {
124 return err
125 }
126 ch <- prometheus.MustNewConstMetric(c.cpuFreqMax, prometheus.GaugeValue, float64(value)*1000.0, cpuNum)
127 }
128
129 // See 143 // See
130 // https://www.kernel.org/doc/Documentation/x86/topology.txt 144 // https://www.kernel.org/doc/Documentation/x86/topology.txt
131 // https://www.kernel.org/doc/Documentation/cputopology.txt 145 // https://www.kernel.org/doc/Documentation/cputopology.txt