Skip to content

Commit

Permalink
Update cpufreq metrics collector (prometheus#1117)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
SuperQ authored Oct 18, 2018
1 parent ef7a02d commit a0a164d
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 32 deletions.
68 changes: 41 additions & 27 deletions collector/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ package collector

import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
"github.com/prometheus/procfs"
"github.com/prometheus/procfs/sysfs"
)

type cpuCollector struct {
Expand Down Expand Up @@ -86,46 +85,61 @@ func (c *cpuCollector) Update(ch chan<- prometheus.Metric) error {
if err := c.updateCPUfreq(ch); err != nil {
return err
}
if err := c.updateThermalThrottle(ch); err != nil {
return err
}
return nil
}

// updateCPUfreq reads /sys/devices/system/cpu/cpu* and expose cpu frequency statistics.
func (c *cpuCollector) updateCPUfreq(ch chan<- prometheus.Metric) error {
fs, err := sysfs.NewFS(*sysPath)
if err != nil {
return fmt.Errorf("failed to open sysfs: %v", err)
}

cpuFreqs, err := fs.NewSystemCpufreq()
if err != nil {
return err
}

// sysfs cpufreq values are kHz, thus multiply by 1000 to export base units (hz).
// See https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt
for _, stats := range cpuFreqs {
ch <- prometheus.MustNewConstMetric(
c.cpuFreq,
prometheus.GaugeValue,
float64(stats.CurrentFrequency)*1000.0,
stats.Name,
)
ch <- prometheus.MustNewConstMetric(
c.cpuFreqMin,
prometheus.GaugeValue,
float64(stats.MinimumFrequency)*1000.0,
stats.Name,
)
ch <- prometheus.MustNewConstMetric(
c.cpuFreqMax,
prometheus.GaugeValue,
float64(stats.MaximumFrequency)*1000.0,
stats.Name,
)
}
return nil
}

// updateThermalThrottle reads /sys/devices/system/cpu/cpu* and expose thermal throttle statistics.
func (c *cpuCollector) updateThermalThrottle(ch chan<- prometheus.Metric) error {
cpus, err := filepath.Glob(sysFilePath("devices/system/cpu/cpu[0-9]*"))
if err != nil {
return err
}

var value uint64
packageThrottles := make(map[uint64]uint64)
packageCoreThrottles := make(map[uint64]map[uint64]uint64)

// cpu loop
for _, cpu := range cpus {
_, cpuName := filepath.Split(cpu)
cpuNum := strings.TrimPrefix(cpuName, "cpu")

if _, err := os.Stat(filepath.Join(cpu, "cpufreq")); os.IsNotExist(err) {
log.Debugf("CPU %v is missing cpufreq", cpu)
} else {
// sysfs cpufreq values are kHz, thus multiply by 1000 to export base units (hz).
// See https://www.kernel.org/doc/Documentation/cpu-freq/user-guide.txt
if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_cur_freq")); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(c.cpuFreq, prometheus.GaugeValue, float64(value)*1000.0, cpuNum)

if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_min_freq")); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(c.cpuFreqMin, prometheus.GaugeValue, float64(value)*1000.0, cpuNum)

if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_max_freq")); err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(c.cpuFreqMax, prometheus.GaugeValue, float64(value)*1000.0, cpuNum)
}

// See
// https://www.kernel.org/doc/Documentation/x86/topology.txt
// https://www.kernel.org/doc/Documentation/cputopology.txt
Expand Down
130 changes: 125 additions & 5 deletions collector/fixtures/sys.ttar
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ Mode: 755
Directory: sys/class/infiniband/i40iw0/ports/1/counters
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/class/infiniband/i40iw0/ports/1/counters/VL15_dropped
Lines: 1
N/A (no PMA)
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/class/infiniband/i40iw0/ports/1/counters/excessive_buffer_overrun_errors
Lines: 1
N/A (no PMA)
Expand Down Expand Up @@ -201,11 +206,6 @@ Lines: 1
N/A (no PMA)
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/class/infiniband/i40iw0/ports/1/counters/VL15_dropped
Lines: 1
N/A (no PMA)
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: sys/class/infiniband/mlx4_0
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -1871,11 +1871,36 @@ Mode: 755
Directory: sys/devices/system/cpu/cpu0/cpufreq
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_transition_latency
Lines: 1
0
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu0/cpufreq/related_cpus
Lines: 1
0
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
Lines: 1
performance powersave
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
Lines: 1
1699981
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_driver
Lines: 1
intel_pstate
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
Lines: 1
powersave
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq
Lines: 1
3700000
Expand All @@ -1886,6 +1911,11 @@ Lines: 1
800000
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed
Lines: 1
<unsupported>
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: sys/devices/system/cpu/cpu0/thermal_throttle
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -1918,11 +1948,36 @@ Mode: 755
Directory: sys/devices/system/cpu/cpu1/cpufreq
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu1/cpufreq/cpuinfo_transition_latency
Lines: 1
0
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu1/cpufreq/related_cpus
Lines: 1
0
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_available_governors
Lines: 1
performance powersave
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq
Lines: 1
1699981
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_driver
Lines: 1
intel_pstate
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
Lines: 1
powersave
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_max_freq
Lines: 1
3700000
Expand All @@ -1933,6 +1988,11 @@ Lines: 1
800000
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_setspeed
Lines: 1
<unsupported>
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: sys/devices/system/cpu/cpu1/thermal_throttle
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -1965,11 +2025,36 @@ Mode: 755
Directory: sys/devices/system/cpu/cpu2/cpufreq
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu2/cpufreq/cpuinfo_transition_latency
Lines: 1
0
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu2/cpufreq/related_cpus
Lines: 1
0
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_available_governors
Lines: 1
performance powersave
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq
Lines: 1
8000
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_driver
Lines: 1
intel_pstate
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
Lines: 1
powersave
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_max_freq
Lines: 1
4200000
Expand All @@ -1980,6 +2065,11 @@ Lines: 1
1000
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_setspeed
Lines: 1
<unsupported>
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: sys/devices/system/cpu/cpu2/thermal_throttle
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -2012,11 +2102,36 @@ Mode: 755
Directory: sys/devices/system/cpu/cpu3/cpufreq
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu3/cpufreq/cpuinfo_transition_latency
Lines: 1
0
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu3/cpufreq/related_cpus
Lines: 1
0
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_available_governors
Lines: 1
performance powersave
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq
Lines: 1
8000
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_driver
Lines: 1
intel_pstate
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_governor
Lines: 1
powersave
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_max_freq
Lines: 1
4200000
Expand All @@ -2027,6 +2142,11 @@ Lines: 1
1000
Mode: 644
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_setspeed
Lines: 1
<unsupported>
Mode: 664
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Directory: sys/devices/system/cpu/cpu3/thermal_throttle
Mode: 755
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down

0 comments on commit a0a164d

Please sign in to comment.