From fe19fdd1e813357d3703a469f1e665483efed16f Mon Sep 17 00:00:00 2001 From: Lukas Coppens Date: Wed, 8 Mar 2023 13:18:50 +0100 Subject: [PATCH] feat: add support for cpu freq governor metrics Signed-off-by: Lukas Coppens --- collector/cpufreq_common.go | 11 ++++++++--- collector/cpufreq_linux.go | 16 ++++++++++++++++ collector/fixtures/e2e-64k-page-output.txt | 10 ++++++++++ collector/fixtures/e2e-output.txt | 10 ++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/collector/cpufreq_common.go b/collector/cpufreq_common.go index 8a6d7ce5..e1eda589 100644 --- a/collector/cpufreq_common.go +++ b/collector/cpufreq_common.go @@ -23,17 +23,17 @@ import ( var ( cpuFreqHertzDesc = prometheus.NewDesc( prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_hertz"), - "Current cpu thread frequency in hertz.", + "Current CPU thread frequency in hertz.", []string{"cpu"}, nil, ) cpuFreqMinDesc = prometheus.NewDesc( prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_min_hertz"), - "Minimum cpu thread frequency in hertz.", + "Minimum CPU thread frequency in hertz.", []string{"cpu"}, nil, ) cpuFreqMaxDesc = prometheus.NewDesc( prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "frequency_max_hertz"), - "Maximum cpu thread frequency in hertz.", + "Maximum CPU thread frequency in hertz.", []string{"cpu"}, nil, ) cpuFreqScalingFreqDesc = prometheus.NewDesc( @@ -51,4 +51,9 @@ var ( "Maximum scaled CPU thread frequency in hertz.", []string{"cpu"}, nil, ) + cpuFreqScalingGovernorDesc = prometheus.NewDesc( + prometheus.BuildFQName(namespace, cpuCollectorSubsystem, "scaling_governor"), + "Current enabled CPU frequency governor.", + []string{"cpu", "governor"}, nil, + ) ) diff --git a/collector/cpufreq_linux.go b/collector/cpufreq_linux.go index 41ebfe22..d3651e98 100644 --- a/collector/cpufreq_linux.go +++ b/collector/cpufreq_linux.go @@ -104,6 +104,22 @@ func (c *cpuFreqCollector) Update(ch chan<- prometheus.Metric) error { stats.Name, ) } + if stats.Governor != "" { + availableGovernors := strings.Split(stats.AvailableGovernors, " ") + for _, g := range availableGovernors { + state := 0 + if g == stats.Governor { + state = 1 + } + ch <- prometheus.MustNewConstMetric( + cpuFreqScalingGovernorDesc, + prometheus.GaugeValue, + float64(state), + stats.Name, + g, + ) + } + } } return nil } diff --git a/collector/fixtures/e2e-64k-page-output.txt b/collector/fixtures/e2e-64k-page-output.txt index 69b2bb85..8ea37007 100644 --- a/collector/fixtures/e2e-64k-page-output.txt +++ b/collector/fixtures/e2e-64k-page-output.txt @@ -326,6 +326,16 @@ node_cpu_scaling_frequency_min_hertz{cpu="0"} 8e+08 node_cpu_scaling_frequency_min_hertz{cpu="1"} 8e+08 node_cpu_scaling_frequency_min_hertz{cpu="2"} 1e+06 node_cpu_scaling_frequency_min_hertz{cpu="3"} 1e+06 +# HELP node_cpu_scaling_governor Current enabled CPU frequency governor. +# TYPE node_cpu_scaling_governor gauge +node_cpu_scaling_governor{cpu="0",governor="performance"} 0 +node_cpu_scaling_governor{cpu="0",governor="powersave"} 1 +node_cpu_scaling_governor{cpu="1",governor="performance"} 0 +node_cpu_scaling_governor{cpu="1",governor="powersave"} 1 +node_cpu_scaling_governor{cpu="2",governor="performance"} 0 +node_cpu_scaling_governor{cpu="2",governor="powersave"} 1 +node_cpu_scaling_governor{cpu="3",governor="performance"} 0 +node_cpu_scaling_governor{cpu="3",governor="powersave"} 1 # HELP node_cpu_seconds_total Seconds the CPUs spent in each mode. # TYPE node_cpu_seconds_total counter node_cpu_seconds_total{cpu="0",mode="idle"} 10870.69 diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt index 0ffdece0..d45671be 100644 --- a/collector/fixtures/e2e-output.txt +++ b/collector/fixtures/e2e-output.txt @@ -348,6 +348,16 @@ node_cpu_scaling_frequency_min_hertz{cpu="0"} 8e+08 node_cpu_scaling_frequency_min_hertz{cpu="1"} 8e+08 node_cpu_scaling_frequency_min_hertz{cpu="2"} 1e+06 node_cpu_scaling_frequency_min_hertz{cpu="3"} 1e+06 +# HELP node_cpu_scaling_governor Current enabled CPU frequency governor. +# TYPE node_cpu_scaling_governor gauge +node_cpu_scaling_governor{cpu="0",governor="performance"} 0 +node_cpu_scaling_governor{cpu="0",governor="powersave"} 1 +node_cpu_scaling_governor{cpu="1",governor="performance"} 0 +node_cpu_scaling_governor{cpu="1",governor="powersave"} 1 +node_cpu_scaling_governor{cpu="2",governor="performance"} 0 +node_cpu_scaling_governor{cpu="2",governor="powersave"} 1 +node_cpu_scaling_governor{cpu="3",governor="performance"} 0 +node_cpu_scaling_governor{cpu="3",governor="powersave"} 1 # HELP node_cpu_seconds_total Seconds the CPUs spent in each mode. # TYPE node_cpu_seconds_total counter node_cpu_seconds_total{cpu="0",mode="idle"} 10870.69