Fix some test races and other things found with go test -race (#711)

This commit is contained in:
Raja Subramanian
2022-05-24 10:16:36 +05:30
committed by GitHub
parent a1e264bf34
commit 33032f6c4b
5 changed files with 57 additions and 16 deletions
+5
View File
@@ -4,9 +4,12 @@
package prometheus
import (
"sync"
"github.com/mackerelio/go-osstat/cpu"
)
var cpuStatsLock sync.RWMutex
var lastCPUTotal, lastCPUIdle uint64
func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) {
@@ -15,12 +18,14 @@ func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) {
return
}
cpuStatsLock.Lock()
if lastCPUTotal > 0 && lastCPUTotal < cpuInfo.Total {
cpuLoad = 1 - float32(cpuInfo.Idle-lastCPUIdle)/float32(cpuInfo.Total-lastCPUTotal)
}
lastCPUTotal = cpuInfo.Total
lastCPUIdle = cpuInfo.Idle // + cpu.Iowait
cpuStatsLock.Unlock()
numCPUs = uint32(cpuInfo.CPUCount)