implements cpu_times on freebsd.

This commit is contained in:
WAKAYAMA shirou 2014-04-23 11:37:00 +09:00
parent dcfdf6b13f
commit 6562d7f797
2 changed files with 42 additions and 1 deletions

View File

@ -56,7 +56,7 @@ Current Status
- done
- cpu_times (linux)
- cpu_times (linux, freebsd)
- cpu_count (linux, freebsd, windows)
- virtual_memory (linux, windows)
- swap_memory (linux)

View File

@ -2,8 +2,49 @@
package gopsutil
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
func Cpu_times() ([]CPU_TimesStat, error) {
ret := make([]CPU_TimesStat, 0)
cpu_time, err := do_sysctrl("kern.cp_time")
if err != nil {
return ret, err
}
user, _ := strconv.ParseInt(cpu_time[CP_USER], 10, 64)
nice, _ := strconv.ParseInt(cpu_time[CP_NICE], 10, 64)
sys, _ := strconv.ParseInt(cpu_time[CP_SYS], 10, 64)
idle, _ := strconv.ParseInt(cpu_time[CP_IDLE], 10, 64)
intr, _ := strconv.ParseInt(cpu_time[CP_INTR], 10, 64)
c := CPU_TimesStat{
User: uint64(user / CLOCKS_PER_SEC),
Nice: uint64(nice / CLOCKS_PER_SEC),
System: uint64(sys / CLOCKS_PER_SEC),
Idle: uint64(idle / CLOCKS_PER_SEC),
Irq: uint64(intr / CLOCKS_PER_SEC), // FIXME: correct?
}
ret = append(ret, c)
return ret, nil
}