@@ -43,7 +43,7 @@ type cpuCollector struct {
4343 cpuPackageThrottle * prometheus.Desc
4444 cpuIsolated * prometheus.Desc
4545 logger log.Logger
46- cpuStats [ ]procfs.CPUStat
46+ cpuStats map [ int64 ]procfs.CPUStat
4747 cpuStatsMutex sync.Mutex
4848 isolatedCpus []uint16
4949
@@ -126,6 +126,7 @@ func NewCPUCollector(logger log.Logger) (Collector, error) {
126126 ),
127127 logger : logger ,
128128 isolatedCpus : isolcpus ,
129+ cpuStats : make (map [int64 ]procfs.CPUStat ),
129130 }
130131 err = c .compileIncludeFlags (flagsInclude , bugsInclude )
131132 if err != nil {
@@ -324,7 +325,7 @@ func (c *cpuCollector) updateStat(ch chan<- prometheus.Metric) error {
324325 c .cpuStatsMutex .Lock ()
325326 defer c .cpuStatsMutex .Unlock ()
326327 for cpuID , cpuStat := range c .cpuStats {
327- cpuNum := strconv .Itoa (cpuID )
328+ cpuNum := strconv .Itoa (int ( cpuID ) )
328329 ch <- prometheus .MustNewConstMetric (c .cpu , prometheus .CounterValue , cpuStat .User , cpuNum , "user" )
329330 ch <- prometheus .MustNewConstMetric (c .cpu , prometheus .CounterValue , cpuStat .Nice , cpuNum , "nice" )
330331 ch <- prometheus .MustNewConstMetric (c .cpu , prometheus .CounterValue , cpuStat .System , cpuNum , "system" )
@@ -345,82 +346,82 @@ func (c *cpuCollector) updateStat(ch chan<- prometheus.Metric) error {
345346}
346347
347348// updateCPUStats updates the internal cache of CPU stats.
348- func (c * cpuCollector ) updateCPUStats (newStats [ ]procfs.CPUStat ) {
349+ func (c * cpuCollector ) updateCPUStats (newStats map [ int64 ]procfs.CPUStat ) {
349350
350351 // Acquire a lock to update the stats.
351352 c .cpuStatsMutex .Lock ()
352353 defer c .cpuStatsMutex .Unlock ()
353354
354355 // Reset the cache if the list of CPUs has changed.
355- if len (c .cpuStats ) != len (newStats ) {
356- c .cpuStats = make ([]procfs.CPUStat , len (newStats ))
357- }
358-
359356 for i , n := range newStats {
357+ cpuStats := c .cpuStats [i ]
358+
360359 // If idle jumps backwards by more than X seconds, assume we had a hotplug event and reset the stats for this CPU.
361- if (c . cpuStats [ i ] .Idle - n .Idle ) >= jumpBackSeconds {
362- level .Debug (c .logger ).Log ("msg" , jumpBackDebugMessage , "cpu" , i , "old_value" , c . cpuStats [ i ] .Idle , "new_value" , n .Idle )
363- c . cpuStats [ i ] = procfs.CPUStat {}
360+ if (cpuStats .Idle - n .Idle ) >= jumpBackSeconds {
361+ level .Debug (c .logger ).Log ("msg" , jumpBackDebugMessage , "cpu" , i , "old_value" , cpuStats .Idle , "new_value" , n .Idle )
362+ cpuStats = procfs.CPUStat {}
364363 }
365364
366- if n .Idle >= c . cpuStats [ i ] .Idle {
367- c . cpuStats [ i ] .Idle = n .Idle
365+ if n .Idle >= cpuStats .Idle {
366+ cpuStats .Idle = n .Idle
368367 } else {
369- level .Debug (c .logger ).Log ("msg" , "CPU Idle counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .Idle , "new_value" , n .Idle )
368+ level .Debug (c .logger ).Log ("msg" , "CPU Idle counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .Idle , "new_value" , n .Idle )
370369 }
371370
372- if n .User >= c . cpuStats [ i ] .User {
373- c . cpuStats [ i ] .User = n .User
371+ if n .User >= cpuStats .User {
372+ cpuStats .User = n .User
374373 } else {
375- level .Debug (c .logger ).Log ("msg" , "CPU User counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .User , "new_value" , n .User )
374+ level .Debug (c .logger ).Log ("msg" , "CPU User counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .User , "new_value" , n .User )
376375 }
377376
378- if n .Nice >= c . cpuStats [ i ] .Nice {
379- c . cpuStats [ i ] .Nice = n .Nice
377+ if n .Nice >= cpuStats .Nice {
378+ cpuStats .Nice = n .Nice
380379 } else {
381- level .Debug (c .logger ).Log ("msg" , "CPU Nice counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .Nice , "new_value" , n .Nice )
380+ level .Debug (c .logger ).Log ("msg" , "CPU Nice counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .Nice , "new_value" , n .Nice )
382381 }
383382
384- if n .System >= c . cpuStats [ i ] .System {
385- c . cpuStats [ i ] .System = n .System
383+ if n .System >= cpuStats .System {
384+ cpuStats .System = n .System
386385 } else {
387- level .Debug (c .logger ).Log ("msg" , "CPU System counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .System , "new_value" , n .System )
386+ level .Debug (c .logger ).Log ("msg" , "CPU System counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .System , "new_value" , n .System )
388387 }
389388
390- if n .Iowait >= c . cpuStats [ i ] .Iowait {
391- c . cpuStats [ i ] .Iowait = n .Iowait
389+ if n .Iowait >= cpuStats .Iowait {
390+ cpuStats .Iowait = n .Iowait
392391 } else {
393- level .Debug (c .logger ).Log ("msg" , "CPU Iowait counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .Iowait , "new_value" , n .Iowait )
392+ level .Debug (c .logger ).Log ("msg" , "CPU Iowait counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .Iowait , "new_value" , n .Iowait )
394393 }
395394
396- if n .IRQ >= c . cpuStats [ i ] .IRQ {
397- c . cpuStats [ i ] .IRQ = n .IRQ
395+ if n .IRQ >= cpuStats .IRQ {
396+ cpuStats .IRQ = n .IRQ
398397 } else {
399- level .Debug (c .logger ).Log ("msg" , "CPU IRQ counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .IRQ , "new_value" , n .IRQ )
398+ level .Debug (c .logger ).Log ("msg" , "CPU IRQ counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .IRQ , "new_value" , n .IRQ )
400399 }
401400
402- if n .SoftIRQ >= c . cpuStats [ i ] .SoftIRQ {
403- c . cpuStats [ i ] .SoftIRQ = n .SoftIRQ
401+ if n .SoftIRQ >= cpuStats .SoftIRQ {
402+ cpuStats .SoftIRQ = n .SoftIRQ
404403 } else {
405- level .Debug (c .logger ).Log ("msg" , "CPU SoftIRQ counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .SoftIRQ , "new_value" , n .SoftIRQ )
404+ level .Debug (c .logger ).Log ("msg" , "CPU SoftIRQ counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .SoftIRQ , "new_value" , n .SoftIRQ )
406405 }
407406
408- if n .Steal >= c . cpuStats [ i ] .Steal {
409- c . cpuStats [ i ] .Steal = n .Steal
407+ if n .Steal >= cpuStats .Steal {
408+ cpuStats .Steal = n .Steal
410409 } else {
411- level .Debug (c .logger ).Log ("msg" , "CPU Steal counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .Steal , "new_value" , n .Steal )
410+ level .Debug (c .logger ).Log ("msg" , "CPU Steal counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .Steal , "new_value" , n .Steal )
412411 }
413412
414- if n .Guest >= c . cpuStats [ i ] .Guest {
415- c . cpuStats [ i ] .Guest = n .Guest
413+ if n .Guest >= cpuStats .Guest {
414+ cpuStats .Guest = n .Guest
416415 } else {
417- level .Debug (c .logger ).Log ("msg" , "CPU Guest counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .Guest , "new_value" , n .Guest )
416+ level .Debug (c .logger ).Log ("msg" , "CPU Guest counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .Guest , "new_value" , n .Guest )
418417 }
419418
420- if n .GuestNice >= c . cpuStats [ i ] .GuestNice {
421- c . cpuStats [ i ] .GuestNice = n .GuestNice
419+ if n .GuestNice >= cpuStats .GuestNice {
420+ cpuStats .GuestNice = n .GuestNice
422421 } else {
423- level .Debug (c .logger ).Log ("msg" , "CPU GuestNice counter jumped backwards" , "cpu" , i , "old_value" , c . cpuStats [ i ] .GuestNice , "new_value" , n .GuestNice )
422+ level .Debug (c .logger ).Log ("msg" , "CPU GuestNice counter jumped backwards" , "cpu" , i , "old_value" , cpuStats .GuestNice , "new_value" , n .GuestNice )
424423 }
424+
425+ c .cpuStats [i ] = cpuStats
425426 }
426427}
0 commit comments