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

100 lines
2.4 KiB
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 (
2014-05-16 18:39:17 +09:00
"regexp"
2014-04-23 11:37:00 +09:00
"strconv"
2014-05-16 18:39:17 +09:00
"strings"
2014-04-23 11:37:00 +09:00
)
// sys/resource.h
const (
2014-08-15 20:05:26 +02:00
CPUser = 0
CPNice = 1
CPSys = 2
CPIntr = 3
CPIdle = 4
CPUStates = 5
2014-04-23 11:37:00 +09:00
)
// time.h
const (
2014-08-15 20:05:26 +02:00
ClocksPerSec = 128
2014-04-23 11:37:00 +09:00
)
// TODO: get per cpus
2014-04-30 15:32:05 +09:00
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
2014-04-30 16:19:39 +09:00
var ret []CPUTimesStat
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-08-15 20:05:26 +02:00
user, _ := strconv.ParseFloat(cpuTime[CPUser], 32)
nice, _ := strconv.ParseFloat(cpuTime[CPNice], 32)
sys, _ := strconv.ParseFloat(cpuTime[CPSys], 32)
idle, _ := strconv.ParseFloat(cpuTime[CPIdle], 32)
intr, _ := strconv.ParseFloat(cpuTime[CPIntr], 32)
2014-04-23 11:37:00 +09:00
2014-04-30 16:16:07 +09:00
c := CPUTimesStat{
2014-08-15 20:05:26 +02:00
User: float32(user / ClocksPerSec),
Nice: float32(nice / ClocksPerSec),
System: float32(sys / ClocksPerSec),
Idle: float32(idle / ClocksPerSec),
Irq: float32(intr / ClocksPerSec),
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
}
2014-05-16 18:39:17 +09: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]
2014-09-20 00:15:35 +09:00
t, err := strconv.ParseFloat(matches[2], 64)
if err != nil {
return ret, nil
}
c.Mhz = t
2014-05-16 18:39:17 +09: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]
2014-09-20 00:15:35 +09:00
t, err := strconv.ParseInt(matches[5], 10, 32)
if err != nil {
return ret, nil
}
c.Stepping = int32(t)
2014-05-16 18:39:17 +09: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?
2014-09-20 00:15:35 +09:00
t, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil {
return ret, nil
}
c.Cores = int32(t)
2014-05-16 18:39:17 +09:00
}
}
return append(ret, c), nil
}