1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00
shirou_gopsutil/cpu_freebsd.go

51 lines
963 B
Go
Raw Normal View History

2014-04-18 16:34:47 +09:00
// +build freebsd
2014-04-22 09:44:22 +09:00
package gopsutil
2014-04-18 16:34:47 +09:00
2014-04-23 11:37:00 +09: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-30 15:32:05 +09:00
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
2014-04-30 16:16:07 +09:00
var ret []CPU_TimesStat
2014-04-18 16:34:47 +09:00
2014-04-30 16:16:07 +09:00
cpuTime, err := doSysctrl("kern.cp_time")
2014-04-23 11:37:00 +09:00
if err != nil {
return ret, err
}
2014-04-30 16:16:07 +09:00
user, _ := strconv.ParseFloat(cpuTime[CP_USER], 32)
nice, _ := strconv.ParseFloat(cpuTime[CP_NICE], 32)
sys, _ := strconv.ParseFloat(cpuTime[CP_SYS], 32)
idle, _ := strconv.ParseFloat(cpuTime[CP_IDLE], 32)
intr, _ := strconv.ParseFloat(cpuTime[CP_INTR], 32)
2014-04-23 11:37:00 +09:00
2014-04-30 16:16:07 +09:00
c := CPUTimesStat{
2014-04-24 16:15:57 +09: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 11:37:00 +09:00
}
ret = append(ret, c)
2014-04-18 21:26:24 +09:00
return ret, nil
2014-04-18 16:34:47 +09:00
}