Fix some test races and other things found with go test -race (#711)
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user