aboutsummaryrefslogtreecommitdiff
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
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>
-rw-r--r--collector/cpu_linux.go68
-rw-r--r--collector/fixtures/sys.ttar130
2 files changed, 166 insertions, 32 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
diff --git a/collector/fixtures/sys.ttar b/collector/fixtures/sys.ttar
index b8e4546..f739de2 100644
--- a/collector/fixtures/sys.ttar
+++ b/collector/fixtures/sys.ttar
@@ -121,6 +121,11 @@ Mode: 755
121Directory: sys/class/infiniband/i40iw0/ports/1/counters 121Directory: sys/class/infiniband/i40iw0/ports/1/counters
122Mode: 755 122Mode: 755
123# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 123# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124Path: sys/class/infiniband/i40iw0/ports/1/counters/VL15_dropped
125Lines: 1
126N/A (no PMA)
127Mode: 644
128# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124Path: sys/class/infiniband/i40iw0/ports/1/counters/excessive_buffer_overrun_errors 129Path: sys/class/infiniband/i40iw0/ports/1/counters/excessive_buffer_overrun_errors
125Lines: 1 130Lines: 1
126N/A (no PMA) 131N/A (no PMA)
@@ -201,11 +206,6 @@ Lines: 1
201N/A (no PMA) 206N/A (no PMA)
202Mode: 644 207Mode: 644
203# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 208# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
204Path: sys/class/infiniband/i40iw0/ports/1/counters/VL15_dropped
205Lines: 1
206N/A (no PMA)
207Mode: 644
208# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
209Directory: sys/class/infiniband/mlx4_0 209Directory: sys/class/infiniband/mlx4_0
210Mode: 755 210Mode: 755
211# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 211# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -1871,11 +1871,36 @@ Mode: 755
1871Directory: sys/devices/system/cpu/cpu0/cpufreq 1871Directory: sys/devices/system/cpu/cpu0/cpufreq
1872Mode: 755 1872Mode: 755
1873# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1873# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1874Path: sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_transition_latency
1875Lines: 1
18760
1877Mode: 664
1878# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1879Path: sys/devices/system/cpu/cpu0/cpufreq/related_cpus
1880Lines: 1
18810
1882Mode: 664
1883# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1884Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
1885Lines: 1
1886performance powersave
1887Mode: 664
1888# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1874Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq 1889Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
1875Lines: 1 1890Lines: 1
18761699981 18911699981
1877Mode: 644 1892Mode: 644
1878# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1893# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1894Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_driver
1895Lines: 1
1896intel_pstate
1897Mode: 664
1898# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1899Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
1900Lines: 1
1901powersave
1902Mode: 664
1903# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1879Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq 1904Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
1880Lines: 1 1905Lines: 1
18813700000 19063700000
@@ -1886,6 +1911,11 @@ Lines: 1
1886800000 1911800000
1887Mode: 644 1912Mode: 644
1888# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1913# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1914Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
1915Lines: 1
1916<unsupported>
1917Mode: 664
1918# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1889Directory: sys/devices/system/cpu/cpu0/thermal_throttle 1919Directory: sys/devices/system/cpu/cpu0/thermal_throttle
1890Mode: 755 1920Mode: 755
1891# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1921# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -1918,11 +1948,36 @@ Mode: 755
1918Directory: sys/devices/system/cpu/cpu1/cpufreq 1948Directory: sys/devices/system/cpu/cpu1/cpufreq
1919Mode: 755 1949Mode: 755
1920# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1950# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1951Path: sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_transition_latency
1952Lines: 1
19530
1954Mode: 664
1955# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1956Path: sys/devices/system/cpu/cpu1/cpufreq/related_cpus
1957Lines: 1
19580
1959Mode: 664
1960# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1961Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_available_governors
1962Lines: 1
1963performance powersave
1964Mode: 664
1965# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1921Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq 1966Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq
1922Lines: 1 1967Lines: 1
19231699981 19681699981
1924Mode: 644 1969Mode: 644
1925# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1970# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1971Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_driver
1972Lines: 1
1973intel_pstate
1974Mode: 664
1975# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1976Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
1977Lines: 1
1978powersave
1979Mode: 664
1980# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1926Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq 1981Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq
1927Lines: 1 1982Lines: 1
19283700000 19833700000
@@ -1933,6 +1988,11 @@ Lines: 1
1933800000 1988800000
1934Mode: 644 1989Mode: 644
1935# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1990# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1991Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_setspeed
1992Lines: 1
1993<unsupported>
1994Mode: 664
1995# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1936Directory: sys/devices/system/cpu/cpu1/thermal_throttle 1996Directory: sys/devices/system/cpu/cpu1/thermal_throttle
1937Mode: 755 1997Mode: 755
1938# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1998# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -1965,11 +2025,36 @@ Mode: 755
1965Directory: sys/devices/system/cpu/cpu2/cpufreq 2025Directory: sys/devices/system/cpu/cpu2/cpufreq
1966Mode: 755 2026Mode: 755
1967# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2027# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2028Path: sys/devices/system/cpu/cpu2/cpufreq/cpuinfo_transition_latency
2029Lines: 1
20300
2031Mode: 664
2032# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2033Path: sys/devices/system/cpu/cpu2/cpufreq/related_cpus
2034Lines: 1
20350
2036Mode: 664
2037# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2038Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_available_governors
2039Lines: 1
2040performance powersave
2041Mode: 664
2042# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1968Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq 2043Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq
1969Lines: 1 2044Lines: 1
19708000 20458000
1971Mode: 644 2046Mode: 644
1972# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2047# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2048Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_driver
2049Lines: 1
2050intel_pstate
2051Mode: 664
2052# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2053Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
2054Lines: 1
2055powersave
2056Mode: 664
2057# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1973Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq 2058Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq
1974Lines: 1 2059Lines: 1
19754200000 20604200000
@@ -1980,6 +2065,11 @@ Lines: 1
19801000 20651000
1981Mode: 644 2066Mode: 644
1982# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2067# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2068Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_setspeed
2069Lines: 1
2070<unsupported>
2071Mode: 664
2072# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1983Directory: sys/devices/system/cpu/cpu2/thermal_throttle 2073Directory: sys/devices/system/cpu/cpu2/thermal_throttle
1984Mode: 755 2074Mode: 755
1985# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2075# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -2012,11 +2102,36 @@ Mode: 755
2012Directory: sys/devices/system/cpu/cpu3/cpufreq 2102Directory: sys/devices/system/cpu/cpu3/cpufreq
2013Mode: 755 2103Mode: 755
2014# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2104# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2105Path: sys/devices/system/cpu/cpu3/cpufreq/cpuinfo_transition_latency
2106Lines: 1
21070
2108Mode: 664
2109# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2110Path: sys/devices/system/cpu/cpu3/cpufreq/related_cpus
2111Lines: 1
21120
2113Mode: 664
2114# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2115Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_available_governors
2116Lines: 1
2117performance powersave
2118Mode: 664
2119# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2015Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq 2120Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq
2016Lines: 1 2121Lines: 1
20178000 21228000
2018Mode: 644 2123Mode: 644
2019# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2124# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2125Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_driver
2126Lines: 1
2127intel_pstate
2128Mode: 664
2129# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2130Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_governor
2131Lines: 1
2132powersave
2133Mode: 664
2134# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2020Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq 2135Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq
2021Lines: 1 2136Lines: 1
20224200000 21374200000
@@ -2027,6 +2142,11 @@ Lines: 1
20271000 21421000
2028Mode: 644 2143Mode: 644
2029# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2144# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2145Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_setspeed
2146Lines: 1
2147<unsupported>
2148Mode: 664
2149# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2030Directory: sys/devices/system/cpu/cpu3/thermal_throttle 2150Directory: sys/devices/system/cpu/cpu3/thermal_throttle
2031Mode: 755 2151Mode: 755
2032# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2152# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -