2017-06-13 11:21:53 +02:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build !nocpu
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-09-07 23:24:18 +02:00
|
|
|
"io/ioutil"
|
2017-06-20 07:51:26 +02:00
|
|
|
"os"
|
2017-06-13 11:21:53 +02:00
|
|
|
"path/filepath"
|
2017-09-07 23:24:18 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2017-06-13 11:21:53 +02:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-06-20 07:51:26 +02:00
|
|
|
"github.com/prometheus/common/log"
|
2017-06-13 11:21:53 +02:00
|
|
|
"github.com/prometheus/procfs"
|
|
|
|
)
|
|
|
|
|
2017-09-07 23:24:18 +02:00
|
|
|
var (
|
|
|
|
digitRegexp = regexp.MustCompile("[0-9]+")
|
|
|
|
)
|
|
|
|
|
2017-06-13 11:21:53 +02:00
|
|
|
type cpuCollector struct {
|
|
|
|
cpu *prometheus.Desc
|
2017-11-23 15:04:47 +01:00
|
|
|
cpuGuest *prometheus.Desc
|
2017-06-13 11:21:53 +02:00
|
|
|
cpuFreq *prometheus.Desc
|
|
|
|
cpuFreqMin *prometheus.Desc
|
|
|
|
cpuFreqMax *prometheus.Desc
|
|
|
|
cpuCoreThrottle *prometheus.Desc
|
|
|
|
cpuPackageThrottle *prometheus.Desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("cpu", defaultEnabled, NewCPUCollector)
|
2017-06-13 11:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCPUCollector returns a new Collector exposing kernel/system statistics.
|
|
|
|
func NewCPUCollector() (Collector, error) {
|
|
|
|
return &cpuCollector{
|
2018-02-01 18:42:20 +01:00
|
|
|
cpu: nodeCpuSecondsDesc,
|
2017-11-23 15:04:47 +01:00
|
|
|
cpuGuest: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "guest_seconds_total"),
|
|
|
|
"Seconds the cpus spent in guests (VMs) for each mode.",
|
|
|
|
[]string{"cpu", "mode"}, nil,
|
|
|
|
),
|
2017-06-13 11:21:53 +02:00
|
|
|
cpuFreq: prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_hertz"),
|
2017-06-13 11:21:53 +02:00
|
|
|
"Current cpu thread frequency in hertz.",
|
|
|
|
[]string{"cpu"}, nil,
|
|
|
|
),
|
|
|
|
cpuFreqMin: prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_min_hertz"),
|
2017-06-13 11:21:53 +02:00
|
|
|
"Minimum cpu thread frequency in hertz.",
|
|
|
|
[]string{"cpu"}, nil,
|
|
|
|
),
|
|
|
|
cpuFreqMax: prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_max_hertz"),
|
2017-06-13 11:21:53 +02:00
|
|
|
"Maximum cpu thread frequency in hertz.",
|
|
|
|
[]string{"cpu"}, nil,
|
|
|
|
),
|
2017-09-07 23:24:18 +02:00
|
|
|
// FIXME: This should be a per core metric, not per cpu!
|
2017-06-13 11:21:53 +02:00
|
|
|
cpuCoreThrottle: prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "core_throttles_total"),
|
2017-06-13 11:21:53 +02:00
|
|
|
"Number of times this cpu core has been throttled.",
|
|
|
|
[]string{"cpu"}, nil,
|
|
|
|
),
|
|
|
|
cpuPackageThrottle: prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "package_throttles_total"),
|
2017-06-13 11:21:53 +02:00
|
|
|
"Number of times this cpu package has been throttled.",
|
2017-09-07 23:24:18 +02:00
|
|
|
[]string{"node"}, nil,
|
2017-06-13 11:21:53 +02:00
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update implements Collector and exposes cpu related metrics from /proc/stat and /sys/.../cpu/.
|
|
|
|
func (c *cpuCollector) Update(ch chan<- prometheus.Metric) error {
|
|
|
|
if err := c.updateStat(ch); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.updateCPUfreq(ch); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateCPUfreq reads /sys/bus/cpu/devices/cpu* and expose cpu frequency statistics.
|
|
|
|
func (c *cpuCollector) updateCPUfreq(ch chan<- prometheus.Metric) error {
|
|
|
|
cpus, err := filepath.Glob(sysFilePath("bus/cpu/devices/cpu[0-9]*"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var value uint64
|
|
|
|
|
2017-09-07 23:24:18 +02:00
|
|
|
// cpu loop
|
2017-06-13 11:21:53 +02:00
|
|
|
for _, cpu := range cpus {
|
2018-02-01 18:42:20 +01:00
|
|
|
_, cpuName := filepath.Split(cpu)
|
|
|
|
cpuNum := strings.TrimPrefix(cpuName, "cpu")
|
2017-06-13 11:21:53 +02:00
|
|
|
|
2017-06-20 07:51:26 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(cpu, "cpufreq")); os.IsNotExist(err) {
|
2017-11-10 15:31:26 +01:00
|
|
|
log.Debugf("CPU %v is missing cpufreq", cpu)
|
2017-06-20 07:51:26 +02:00
|
|
|
} else {
|
2017-06-27 11:25:06 +02:00
|
|
|
// 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
|
2017-09-07 23:24:18 +02:00
|
|
|
if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_cur_freq")); err != nil {
|
2017-06-20 07:51:26 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-02-01 18:42:20 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpuFreq, prometheus.GaugeValue, float64(value)*1000.0, cpuNum)
|
2017-06-20 07:51:26 +02:00
|
|
|
|
2017-09-07 23:24:18 +02:00
|
|
|
if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_min_freq")); err != nil {
|
2017-06-20 07:51:26 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-02-01 18:42:20 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpuFreqMin, prometheus.GaugeValue, float64(value)*1000.0, cpuNum)
|
2017-06-20 07:51:26 +02:00
|
|
|
|
2017-09-07 23:24:18 +02:00
|
|
|
if value, err = readUintFromFile(filepath.Join(cpu, "cpufreq", "scaling_max_freq")); err != nil {
|
2017-06-20 07:51:26 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-02-01 18:42:20 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpuFreqMax, prometheus.GaugeValue, float64(value)*1000.0, cpuNum)
|
2017-06-13 11:21:53 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:51:26 +02:00
|
|
|
if _, err := os.Stat(filepath.Join(cpu, "thermal_throttle")); os.IsNotExist(err) {
|
2017-11-10 15:31:26 +01:00
|
|
|
log.Debugf("CPU %v is missing thermal_throttle", cpu)
|
2017-09-07 23:24:18 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if value, err = readUintFromFile(filepath.Join(cpu, "thermal_throttle", "core_throttle_count")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-01 18:42:20 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpuCoreThrottle, prometheus.CounterValue, float64(value), cpuNum)
|
2017-09-07 23:24:18 +02:00
|
|
|
}
|
2017-06-20 07:51:26 +02:00
|
|
|
|
2017-11-10 15:31:26 +01:00
|
|
|
nodes, err := filepath.Glob(sysFilePath("bus/node/devices/node[0-9]*"))
|
2017-09-07 23:24:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-10 15:31:26 +01:00
|
|
|
// package / NUMA node loop
|
|
|
|
for _, node := range nodes {
|
|
|
|
if _, err := os.Stat(filepath.Join(node, "cpulist")); os.IsNotExist(err) {
|
|
|
|
log.Debugf("NUMA node %v is missing cpulist", node)
|
2017-09-07 23:24:18 +02:00
|
|
|
continue
|
|
|
|
}
|
2017-11-10 15:31:26 +01:00
|
|
|
cpulist, err := ioutil.ReadFile(filepath.Join(node, "cpulist"))
|
2017-09-07 23:24:18 +02:00
|
|
|
if err != nil {
|
2017-11-10 15:31:26 +01:00
|
|
|
log.Debugf("could not read cpulist of NUMA node %v", node)
|
2017-09-07 23:24:18 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// cpulist example of one package/node with HT: "0-11,24-35"
|
|
|
|
line := strings.Split(string(cpulist), "\n")[0]
|
2017-11-10 15:31:26 +01:00
|
|
|
if line == "" {
|
|
|
|
// Skip processor-less (memory-only) NUMA nodes.
|
|
|
|
// E.g. RAM expansion with Intel Optane Drive(s) using
|
|
|
|
// Intel Memory Drive Technology (IMDT).
|
|
|
|
log.Debugf("skipping processor-less (memory-only) NUMA node %v", node)
|
|
|
|
continue
|
|
|
|
}
|
2017-09-07 23:24:18 +02:00
|
|
|
firstCPU := strings.FieldsFunc(line, func(r rune) bool {
|
|
|
|
return r == '-' || r == ','
|
|
|
|
})[0]
|
2017-11-10 15:31:26 +01:00
|
|
|
if _, err := os.Stat(filepath.Join(node, "cpu"+firstCPU, "thermal_throttle", "package_throttle_count")); os.IsNotExist(err) {
|
|
|
|
log.Debugf("Node %v CPU %v is missing package_throttle", node, firstCPU)
|
2017-09-07 23:24:18 +02:00
|
|
|
continue
|
|
|
|
}
|
2017-11-10 15:31:26 +01:00
|
|
|
if value, err = readUintFromFile(filepath.Join(node, "cpu"+firstCPU, "thermal_throttle", "package_throttle_count")); err != nil {
|
2017-09-07 23:24:18 +02:00
|
|
|
return err
|
2017-06-13 11:21:53 +02:00
|
|
|
}
|
2017-11-10 15:31:26 +01:00
|
|
|
nodeno := digitRegexp.FindAllString(node, 1)[0]
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpuPackageThrottle, prometheus.CounterValue, float64(value), nodeno)
|
2017-06-13 11:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateStat reads /proc/stat through procfs and exports cpu related metrics.
|
|
|
|
func (c *cpuCollector) updateStat(ch chan<- prometheus.Metric) error {
|
|
|
|
fs, err := procfs.NewFS(*procPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open procfs: %v", err)
|
|
|
|
}
|
|
|
|
stats, err := fs.NewStat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for cpuID, cpuStat := range stats.CPU {
|
2017-11-23 15:04:47 +01:00
|
|
|
cpuNum := fmt.Sprintf("%d", cpuID)
|
2018-02-01 18:42:20 +01:00
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.User, cpuNum, "user")
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Nice, cpuNum, "nice")
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.System, cpuNum, "system")
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Idle, cpuNum, "idle")
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Iowait, cpuNum, "iowait")
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.IRQ, cpuNum, "irq")
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.SoftIRQ, cpuNum, "softirq")
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, cpuStat.Steal, cpuNum, "steal")
|
2017-11-23 15:04:47 +01:00
|
|
|
|
|
|
|
// Guest CPU is also accounted for in cpuStat.User and cpuStat.Nice, expose these as separate metrics.
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpuGuest, prometheus.CounterValue, cpuStat.Guest, cpuNum, "user")
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.cpuGuest, prometheus.CounterValue, cpuStat.GuestNice, cpuNum, "nice")
|
2017-06-13 11:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|