Fix some test races and other things found with go test -race (#711)
This commit is contained in:
@@ -5,10 +5,12 @@ package prometheus
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/mackerelio/go-osstat/cpu"
|
||||
)
|
||||
|
||||
var cpuStatsLock sync.RWMutex
|
||||
var lastCPUTotal, lastCPUIdle uint64
|
||||
|
||||
func getCPUStats() (cpuLoad float32, numCPUs uint32, err error) {
|
||||
@@ -17,12 +19,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
|
||||
cpuStatsLock.Unlock()
|
||||
|
||||
numCPUs = uint32(runtime.NumCPU())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user