shirou_gopsutil/cpu/cpu_freebsd.go

135 lines
3.0 KiB
Go
Raw Normal View History

2014-04-18 15:34:47 +08:00
// +build freebsd
2014-12-30 21:09:05 +08:00
package cpu
2014-04-18 15:34:47 +08:00
2014-04-23 10:37:00 +08:00
import (
2014-11-02 08:14:38 +08:00
"fmt"
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"
common "github.com/shirou/gopsutil/common"
2014-04-23 10:37:00 +08:00
)
// sys/resource.h
const (
2014-08-16 02:05:26 +08:00
CPUser = 0
CPNice = 1
CPSys = 2
CPIntr = 3
CPIdle = 4
CPUStates = 5
2014-04-23 10:37:00 +08:00
)
// time.h
const (
2014-08-16 02:05:26 +08:00
ClocksPerSec = 128
2014-04-23 10:37:00 +08:00
)
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-11-02 08:14:38 +08:00
var sysctlCall string
var ncpu int
if percpu {
sysctlCall = "kern.cp_times"
ncpu, _ = CPUCounts(true)
} else {
sysctlCall = "kern.cp_time"
ncpu = 1
2014-04-23 10:37:00 +08:00
}
cpuTimes, err := common.DoSysctrl(sysctlCall)
2014-09-20 09:22:41 +08:00
if err != nil {
return ret, err
}
2014-04-23 10:37:00 +08:00
2014-11-02 08:14:38 +08:00
for i := 0; i < ncpu; i++ {
offset := CPUStates * i
user, err := strconv.ParseFloat(cpuTimes[CPUser+offset], 64)
2014-11-02 08:14:38 +08:00
if err != nil {
return ret, err
}
nice, err := strconv.ParseFloat(cpuTimes[CPNice+offset], 64)
2014-11-02 08:14:38 +08:00
if err != nil {
return ret, err
}
sys, err := strconv.ParseFloat(cpuTimes[CPSys+offset], 64)
2014-11-02 08:14:38 +08:00
if err != nil {
return ret, err
}
idle, err := strconv.ParseFloat(cpuTimes[CPIdle+offset], 64)
2014-11-02 08:14:38 +08:00
if err != nil {
return ret, err
}
intr, err := strconv.ParseFloat(cpuTimes[CPIntr+offset], 64)
2014-11-02 08:14:38 +08:00
if err != nil {
return ret, err
}
2014-04-23 10:37:00 +08:00
2014-11-02 08:14:38 +08:00
c := CPUTimesStat{
2015-02-15 19:48:29 +08:00
User: float64(user / ClocksPerSec),
Nice: float64(nice / ClocksPerSec),
System: float64(sys / ClocksPerSec),
Idle: float64(idle / ClocksPerSec),
Irq: float64(intr / ClocksPerSec),
2014-11-02 08:14:38 +08:00
}
if !percpu {
c.CPU = "cpu-total"
} else {
c.CPU = fmt.Sprintf("cpu%d", i)
}
ret = append(ret, c)
}
2014-04-23 10:37:00 +08:00
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"
2014-11-27 09:25:14 +08:00
lines, _ := common.ReadLines(filename)
2014-05-16 17:39:17 +08:00
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-19 23:15:35 +08:00
t, err := strconv.ParseFloat(matches[2], 64)
if err != nil {
return ret, nil
}
c.Mhz = t
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]
2014-09-19 23:15:35 +08:00
t, err := strconv.ParseInt(matches[5], 10, 32)
if err != nil {
return ret, nil
}
c.Stepping = int32(t)
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?
2014-09-19 23:15:35 +08:00
t, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil {
return ret, nil
}
c.Cores = int32(t)
2014-05-16 17:39:17 +08:00
}
}
return append(ret, c), nil
}