shirou_gopsutil/cpu_freebsd.go

88 lines
2.3 KiB
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 (
2014-05-16 17:39:17 +08:00
"regexp"
2014-04-23 10:37:00 +08:00
"strconv"
2014-05-16 17:39:17 +08:00
"strings"
2014-04-23 10:37:00 +08:00
)
// 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 14:32:05 +08:00
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
2014-04-30 15:19:39 +08:00
var ret []CPUTimesStat
2014-04-18 15:34:47 +08:00
2014-04-30 15:16:07 +08:00
cpuTime, err := doSysctrl("kern.cp_time")
2014-04-23 10:37:00 +08:00
if err != nil {
return ret, err
}
2014-04-30 15:16:07 +08: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 10:37:00 +08:00
2014-04-30 15:16:07 +08:00
c := CPUTimesStat{
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
}
2014-05-16 17:39:17 +08:00
// Returns only one CPUInfoStat on FreeBSD
func CPUInfo() ([]CPUInfoStat, error) {
filename := "/var/run/dmesg.boot"
lines, _ := readLines(filename)
var ret []CPUInfoStat
c := CPUInfoStat{}
for _, line := range lines {
if matches := regexp.MustCompile(`CPU:\s+(.+) \(([\d.]+).+\)`).FindStringSubmatch(line); matches != nil {
c.ModelName = matches[1]
c.Mhz = mustParseFloat64(matches[2])
2014-05-16 17:39:17 +08:00
} else if matches := regexp.MustCompile(`Origin = "(.+)" Id = (.+) Family = (.+) Model = (.+) Stepping = (.+)`).FindStringSubmatch(line); matches != nil {
c.VendorID = matches[1]
c.Family = matches[3]
c.Model = matches[4]
c.Stepping = mustParseInt32(matches[5])
2014-05-16 17:39:17 +08:00
} else if matches := regexp.MustCompile(`Features=.+<(.+)>`).FindStringSubmatch(line); matches != nil {
for _, v := range strings.Split(matches[1], ",") {
c.Flags = append(c.Flags, strings.ToLower(v))
}
} else if matches := regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`).FindStringSubmatch(line); matches != nil {
for _, v := range strings.Split(matches[1], ",") {
c.Flags = append(c.Flags, strings.ToLower(v))
}
} else if matches := regexp.MustCompile(`Logical CPUs per core: (\d+)`).FindStringSubmatch(line); matches != nil {
// FIXME: no this line?
c.Cores = mustParseInt32(matches[1])
2014-05-16 17:39:17 +08:00
}
}
return append(ret, c), nil
}