From ddd9bef8652987dd28ce3502b063e0e097573419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Mon, 23 Sep 2024 20:19:08 +0200 Subject: [PATCH 1/3] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- pkg/collector/cpu/cpu.go | 67 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/pkg/collector/cpu/cpu.go b/pkg/collector/cpu/cpu.go index cacd6593d..15569cf78 100644 --- a/pkg/collector/cpu/cpu.go +++ b/pkg/collector/cpu/cpu.go @@ -27,6 +27,9 @@ type Collector struct { perfDataCollector *perfdata.Collector + processorRTCValues map[string]cpuCounter + processorMPerfValues map[string]cpuCounter + logicalProcessors *prometheus.Desc cStateSecondsTotal *prometheus.Desc timeTotal *prometheus.Desc @@ -43,6 +46,11 @@ type Collector struct { processorPrivilegedUtility *prometheus.Desc } +type cpuCounter struct { + lastValue uint32 + totalValue float64 +} + func New(config *Config) *Collector { if config == nil { config = &ConfigDefaults @@ -222,6 +230,9 @@ func (c *Collector) Build(_ *slog.Logger, _ *wmi.Client) error { nil, ) + c.processorRTCValues = map[string]cpuCounter{} + c.processorMPerfValues = map[string]cpuCounter{} + return nil } @@ -281,6 +292,30 @@ func (c *Collector) collectFull(ctx *types.ScrapeContext, logger *slog.Logger, c core := cpu.Name + if val, ok := c.processorRTCValues[core]; ok { + c.processorRTCValues[core] = cpuCounter{ + uint32(cpu.ProcessorRTC), + val.totalValue + float64(uint32(cpu.ProcessorRTC)-val.lastValue), + } + } else { + c.processorRTCValues[core] = cpuCounter{ + uint32(cpu.ProcessorRTC), + cpu.ProcessorRTC, + } + } + + if val, ok := c.processorMPerfValues[core]; ok { + c.processorMPerfValues[core] = cpuCounter{ + uint32(cpu.ProcessorMPerf), + val.totalValue + float64(uint32(cpu.ProcessorMPerf)-val.lastValue), + } + } else { + c.processorMPerfValues[core] = cpuCounter{ + uint32(cpu.ProcessorMPerf), + cpu.ProcessorMPerf, + } + } + coreCount++ ch <- prometheus.MustNewConstMetric( @@ -380,13 +415,13 @@ func (c *Collector) collectFull(ctx *types.ScrapeContext, logger *slog.Logger, c ch <- prometheus.MustNewConstMetric( c.processorMPerf, prometheus.CounterValue, - cpu.ProcessorMPerf, + c.processorMPerfValues[core].totalValue, core, ) ch <- prometheus.MustNewConstMetric( c.processorRTC, prometheus.CounterValue, - cpu.ProcessorRTC, + c.processorRTCValues[core].totalValue, core, ) ch <- prometheus.MustNewConstMetric( @@ -423,6 +458,30 @@ func (c *Collector) collectPDH(ch chan<- prometheus.Metric) error { for core, coreData := range data { coreCount++ + if val, ok := c.processorRTCValues[core]; ok { + c.processorRTCValues[core] = cpuCounter{ + uint32(coreData[PrivilegedUtilitySeconds].SecondValue), + val.totalValue + float64(uint32(coreData[PrivilegedUtilitySeconds].SecondValue)-val.lastValue), + } + } else { + c.processorRTCValues[core] = cpuCounter{ + uint32(coreData[PrivilegedUtilitySeconds].SecondValue), + coreData[PrivilegedUtilitySeconds].SecondValue, + } + } + + if val, ok := c.processorMPerfValues[core]; ok { + c.processorMPerfValues[core] = cpuCounter{ + uint32(coreData[ProcessorPerformance].SecondValue), + val.totalValue + float64(uint32(coreData[ProcessorPerformance].SecondValue)-val.lastValue), + } + } else { + c.processorMPerfValues[core] = cpuCounter{ + uint32(coreData[ProcessorPerformance].SecondValue), + coreData[ProcessorPerformance].SecondValue, + } + } + ch <- prometheus.MustNewConstMetric( c.cStateSecondsTotal, prometheus.CounterValue, @@ -520,13 +579,13 @@ func (c *Collector) collectPDH(ch chan<- prometheus.Metric) error { ch <- prometheus.MustNewConstMetric( c.processorMPerf, prometheus.CounterValue, - coreData[ProcessorPerformance].SecondValue, + c.processorMPerfValues[core].totalValue, core, ) ch <- prometheus.MustNewConstMetric( c.processorRTC, prometheus.CounterValue, - coreData[ProcessorUtilityRate].SecondValue, + c.processorRTCValues[core].totalValue, core, ) ch <- prometheus.MustNewConstMetric( From abaa1629f39336c226df6bca8e64fb30be2d359f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Sat, 28 Sep 2024 11:36:17 +0200 Subject: [PATCH 2/3] reset counter at zero MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- pkg/collector/cpu/cpu.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/collector/cpu/cpu.go b/pkg/collector/cpu/cpu.go index 15569cf78..88c11837c 100644 --- a/pkg/collector/cpu/cpu.go +++ b/pkg/collector/cpu/cpu.go @@ -466,7 +466,7 @@ func (c *Collector) collectPDH(ch chan<- prometheus.Metric) error { } else { c.processorRTCValues[core] = cpuCounter{ uint32(coreData[PrivilegedUtilitySeconds].SecondValue), - coreData[PrivilegedUtilitySeconds].SecondValue, + 0, } } @@ -478,7 +478,7 @@ func (c *Collector) collectPDH(ch chan<- prometheus.Metric) error { } else { c.processorMPerfValues[core] = cpuCounter{ uint32(coreData[ProcessorPerformance].SecondValue), - coreData[ProcessorPerformance].SecondValue, + 0, } } From 3a28ddf9f66771605e5fd2605b947536c83f686a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Mon, 30 Sep 2024 09:06:10 +0200 Subject: [PATCH 3/3] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- pkg/collector/cpu/cpu.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/collector/cpu/cpu.go b/pkg/collector/cpu/cpu.go index c0c09c07f..6805cc0d2 100644 --- a/pkg/collector/cpu/cpu.go +++ b/pkg/collector/cpu/cpu.go @@ -270,7 +270,7 @@ func (c *Collector) collectFull(ctx *types.ScrapeContext, logger *slog.Logger, c } else { c.processorRTCValues[core] = cpuCounter{ uint32(cpu.ProcessorRTC), - cpu.ProcessorRTC, + 0, } } @@ -282,7 +282,7 @@ func (c *Collector) collectFull(ctx *types.ScrapeContext, logger *slog.Logger, c } else { c.processorMPerfValues[core] = cpuCounter{ uint32(cpu.ProcessorMPerf), - cpu.ProcessorMPerf, + 0, } }