2015-03-11 22:06:00 +08:00
|
|
|
// +build linux freebsd darwin
|
2015-03-11 22:00:06 +08:00
|
|
|
|
|
|
|
package cpu
|
|
|
|
|
2015-12-16 04:22:30 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
2015-03-11 22:00:06 +08:00
|
|
|
|
2016-06-02 05:21:58 +08:00
|
|
|
func getAllBusy(t TimesStat) (float64, float64) {
|
|
|
|
busy := t.User + t.System + t.Nice + t.Iowait + t.Irq +
|
|
|
|
t.Softirq + t.Steal + t.Guest + t.GuestNice + t.Stolen
|
|
|
|
return busy + t.Idle, busy
|
|
|
|
}
|
|
|
|
|
|
|
|
func calculateBusy(t1, t2 TimesStat) float64 {
|
|
|
|
t1All, t1Busy := getAllBusy(t1)
|
|
|
|
t2All, t2Busy := getAllBusy(t2)
|
|
|
|
|
|
|
|
if t2Busy <= t1Busy {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if t2All <= t1All {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return (t2Busy - t1Busy) / (t2All - t1All) * 100
|
|
|
|
}
|
|
|
|
|
2016-06-02 05:53:54 +08:00
|
|
|
func calculateALLBusy(t1, t2 []TimesStat) ([]float64, error) {
|
2016-06-02 05:21:58 +08:00
|
|
|
// Make sure the CPU measurements have the same length.
|
|
|
|
if len(t1) != len(t2) {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"received two CPU counts: %d != %d",
|
|
|
|
len(t1), len(t2),
|
|
|
|
)
|
2015-03-11 22:00:06 +08:00
|
|
|
}
|
|
|
|
|
2016-06-02 05:21:58 +08:00
|
|
|
ret := make([]float64, len(t1))
|
|
|
|
for i, t := range t2 {
|
|
|
|
ret[i] = calculateBusy(t1[i], t)
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|
2015-03-11 22:00:06 +08:00
|
|
|
|
2016-06-02 05:21:58 +08:00
|
|
|
//Percent calculates the percentage of cpu used either per CPU or combined.
|
|
|
|
//If an interval of 0 is given it will compare the current cpu times against the last call.
|
|
|
|
func Percent(interval time.Duration, percpu bool) ([]float64, error) {
|
|
|
|
if interval <= 0 {
|
|
|
|
return percentUsedFromLastCall(percpu)
|
2015-03-11 22:00:06 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 04:22:30 +08:00
|
|
|
// Get CPU usage at the start of the interval.
|
2016-03-22 22:09:12 +08:00
|
|
|
cpuTimes1, err := Times(percpu)
|
2015-03-11 22:00:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-02 05:21:58 +08:00
|
|
|
time.Sleep(interval)
|
2015-03-11 22:00:06 +08:00
|
|
|
|
2015-12-16 04:22:30 +08:00
|
|
|
// And at the end of the interval.
|
2016-03-22 22:09:12 +08:00
|
|
|
cpuTimes2, err := Times(percpu)
|
2015-12-16 04:22:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-02 05:53:54 +08:00
|
|
|
return calculateALLBusy(cpuTimes1, cpuTimes2)
|
2016-06-02 05:21:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func percentUsedFromLastCall(percpu bool) ([]float64, error) {
|
|
|
|
cpuTimes, err := Times(percpu)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lastCPUPercent.Lock()
|
|
|
|
defer lastCPUPercent.Unlock()
|
|
|
|
var lastTimes []TimesStat
|
|
|
|
if percpu {
|
|
|
|
lastTimes = lastCPUPercent.lastPerCPUTimes
|
|
|
|
lastCPUPercent.lastPerCPUTimes = cpuTimes
|
|
|
|
} else {
|
|
|
|
lastTimes = lastCPUPercent.lastCPUTimes
|
|
|
|
lastCPUPercent.lastCPUTimes = cpuTimes
|
2015-12-16 04:22:30 +08:00
|
|
|
}
|
|
|
|
|
2016-06-02 05:21:58 +08:00
|
|
|
if lastTimes == nil {
|
|
|
|
return nil, fmt.Errorf("Error getting times for cpu percent. LastTimes was nil")
|
2015-03-11 22:00:06 +08:00
|
|
|
}
|
2016-06-02 05:53:54 +08:00
|
|
|
return calculateALLBusy(lastTimes, cpuTimes)
|
2016-06-02 05:21:58 +08:00
|
|
|
|
2015-03-11 22:00:06 +08:00
|
|
|
}
|