CPUTimes percpu for Darwin

This commit is contained in:
KenjiTakahashi 2014-11-02 04:01:03 +01:00
parent 0725331571
commit d3206cbaf1
1 changed files with 47 additions and 30 deletions

View File

@ -23,32 +23,43 @@ const (
ClocksPerSec = 128
)
// TODO: get per cpus
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var ret []CPUTimesStat
cpuTime, err := doSysctrl("kern.cp_time")
var sysctlCall string
var ncpu int
if percpu {
sysctlCall = "kern.cp_times"
ncpu, _ = CPUCounts(true)
} else {
sysctlCall = "kern.cp_time"
ncpu = 1
}
cpuTimes, err := doSysctrl(sysctlCall)
if err != nil {
return ret, err
}
user, err := strconv.ParseFloat(cpuTime[CPUser], 32)
for i := 0; i < ncpu; i++ {
offset := CPUStates * i
user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 32)
if err != nil {
return ret, err
}
nice, err := strconv.ParseFloat(cpuTime[CPNice], 32)
nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 32)
if err != nil {
return ret, err
}
sys, err := strconv.ParseFloat(cpuTime[CPSys], 32)
sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 32)
if err != nil {
return ret, err
}
idle, err := strconv.ParseFloat(cpuTime[CPIdle], 32)
idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 32)
if err != nil {
return ret, err
}
intr, err := strconv.ParseFloat(cpuTime[CPIntr], 32)
intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 32)
if err != nil {
return ret, err
}
@ -60,8 +71,14 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
Idle: float32(idle / ClocksPerSec),
Irq: float32(intr / ClocksPerSec),
}
if !percpu {
c.CPU = "cpu-total"
} else {
c.CPU = fmt.Sprintf("cpu%d", i)
}
ret = append(ret, c)
}
return ret, nil
}