shirou_gopsutil/cpu_freebsd.go

51 lines
970 B
Go
Raw Normal View History

2014-04-18 15:34:47 +08:00
// +build freebsd
2014-04-22 08:44:22 +08:00
package gopsutil
2014-04-18 15:34:47 +08:00
2014-04-23 10:37:00 +08:00
import (
"strconv"
)
// sys/resource.h
const (
CP_USER = 0
CP_NICE = 1
CP_SYS = 2
CP_INTR = 3
CP_IDLE = 4
CPUSTATES = 5
)
// time.h
const (
CLOCKS_PER_SEC = 128
)
// TODO: get per cpus
2014-04-22 09:12:01 +08:00
func Cpu_times() ([]CPU_TimesStat, error) {
ret := make([]CPU_TimesStat, 0)
2014-04-18 15:34:47 +08:00
2014-04-23 10:37:00 +08:00
cpu_time, err := do_sysctrl("kern.cp_time")
if err != nil {
return ret, err
}
2014-04-24 15:15:57 +08:00
user, _ := strconv.ParseFloat(cpu_time[CP_USER], 32)
nice, _ := strconv.ParseFloat(cpu_time[CP_NICE], 32)
sys, _ := strconv.ParseFloat(cpu_time[CP_SYS], 32)
idle, _ := strconv.ParseFloat(cpu_time[CP_IDLE], 32)
intr, _ := strconv.ParseFloat(cpu_time[CP_INTR], 32)
2014-04-23 10:37:00 +08:00
c := CPU_TimesStat{
2014-04-24 15:15:57 +08:00
User: float32(user / CLOCKS_PER_SEC),
Nice: float32(nice / CLOCKS_PER_SEC),
System: float32(sys / CLOCKS_PER_SEC),
Idle: float32(idle / CLOCKS_PER_SEC),
Irq: float32(intr / CLOCKS_PER_SEC), // FIXME: correct?
2014-04-23 10:37:00 +08:00
}
ret = append(ret, c)
2014-04-18 20:26:24 +08:00
return ret, nil
2014-04-18 15:34:47 +08:00
}