diff --git a/collector/cpu_linux.go b/collector/cpu_linux.go index e2a31228..640873f4 100644 --- a/collector/cpu_linux.go +++ b/collector/cpu_linux.go @@ -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 { @@ -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 diff --git a/collector/fixtures/sys.ttar b/collector/fixtures/sys.ttar index b8e45464..f739de25 100644 --- a/collector/fixtures/sys.ttar +++ b/collector/fixtures/sys.ttar @@ -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) @@ -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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -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 @@ -1886,6 +1911,11 @@ Lines: 1 800000 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed +Lines: 1 + +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/devices/system/cpu/cpu0/thermal_throttle Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -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 @@ -1933,6 +1988,11 @@ Lines: 1 800000 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/devices/system/cpu/cpu1/cpufreq/scaling_setspeed +Lines: 1 + +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/devices/system/cpu/cpu1/thermal_throttle Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -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 @@ -1980,6 +2065,11 @@ Lines: 1 1000 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/devices/system/cpu/cpu2/cpufreq/scaling_setspeed +Lines: 1 + +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/devices/system/cpu/cpu2/thermal_throttle Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -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 @@ -2027,6 +2142,11 @@ Lines: 1 1000 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/devices/system/cpu/cpu3/cpufreq/scaling_setspeed +Lines: 1 + +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/devices/system/cpu/cpu3/thermal_throttle Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -