shirou_gopsutil/cpu/cpu_freebsd.go

186 lines
4.7 KiB
Go
Raw Normal View History

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"
2015-07-17 20:46:26 +08:00
"os/exec"
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"
2015-10-19 23:04:57 +08:00
"github.com/shirou/gopsutil/internal/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
)
2015-07-17 20:46:26 +08:00
var ClocksPerSec = float64(128)
var cpuMatch = regexp.MustCompile(`^CPU:`)
var originMatch = regexp.MustCompile(`Origin\s*=\s*"(.+)"\s+Id\s*=\s*(.+)\s+Family\s*=\s*(.+)\s+Model\s*=\s*(.+)\s+Stepping\s*=\s*(.+)`)
var featuresMatch = regexp.MustCompile(`Features=.+<(.+)>`)
var featuresMatch2 = regexp.MustCompile(`Features2=[a-f\dx]+<(.+)>`)
var cpuEnd = regexp.MustCompile(`^Trying to mount root`)
var cpuCores = regexp.MustCompile(`FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)`)
2015-07-17 20:46:26 +08:00
func init() {
getconf, err := exec.LookPath("/usr/bin/getconf")
if err != nil {
return
}
out, err := invoke.Command(getconf, "CLK_TCK")
2015-07-17 20:46:26 +08:00
// ignore errors
if err == nil {
2015-07-17 20:52:43 +08:00
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
2015-07-17 20:46:26 +08:00
if err == nil {
ClocksPerSec = float64(i)
}
}
}
2014-04-23 10:37:00 +08:00
func Times(percpu bool) ([]TimesStat, error) {
var ret []TimesStat
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, _ = Counts(true)
2014-11-02 08:14:38 +08:00
} 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
c := TimesStat{
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 InfoStat on FreeBSD. The information regarding core
// count, however is accurate and it is assumed that all InfoStat attributes
// are the same across CPUs.
func Info() ([]InfoStat, error) {
const dmesgBoot = "/var/run/dmesg.boot"
2014-05-16 17:39:17 +08:00
c, num, err := parseDmesgBoot(dmesgBoot)
if err != nil {
return nil, err
}
var vals []string
if vals, err = common.DoSysctrl("hw.clockrate"); err != nil {
return nil, err
}
if c.Mhz, err = strconv.ParseFloat(vals[0], 64); err != nil {
2017-03-15 21:43:20 +08:00
return nil, fmt.Errorf("unable to parse FreeBSD CPU clock rate: %v", err)
}
if vals, err = common.DoSysctrl("hw.ncpu"); err != nil {
return nil, err
}
var i64 int64
if i64, err = strconv.ParseInt(vals[0], 10, 32); err != nil {
2017-03-15 21:43:20 +08:00
return nil, fmt.Errorf("unable to parse FreeBSD cores: %v", err)
}
c.Cores = int32(i64)
if vals, err = common.DoSysctrl("hw.model"); err != nil {
return nil, err
}
2016-06-17 10:32:49 +08:00
c.ModelName = strings.Join(vals, " ")
ret := make([]InfoStat, num)
for i := 0; i < num; i++ {
ret[i] = c
}
return ret, nil
}
func parseDmesgBoot(fileName string) (InfoStat, int, error) {
c := InfoStat{}
lines, _ := common.ReadLines(fileName)
var cpuNum int
2014-05-16 17:39:17 +08:00
for _, line := range lines {
if matches := cpuEnd.FindStringSubmatch(line); matches != nil {
break
} else if matches := originMatch.FindStringSubmatch(line); matches != nil {
2014-05-16 17:39:17 +08:00
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 {
2017-03-15 21:43:20 +08:00
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU stepping information from %q: %v", line, err)
2014-09-19 23:15:35 +08:00
}
c.Stepping = int32(t)
} else if matches := featuresMatch.FindStringSubmatch(line); matches != nil {
2014-05-16 17:39:17 +08:00
for _, v := range strings.Split(matches[1], ",") {
c.Flags = append(c.Flags, strings.ToLower(v))
}
} else if matches := featuresMatch2.FindStringSubmatch(line); matches != nil {
2014-05-16 17:39:17 +08:00
for _, v := range strings.Split(matches[1], ",") {
c.Flags = append(c.Flags, strings.ToLower(v))
}
} else if matches := cpuCores.FindStringSubmatch(line); matches != nil {
t, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil {
2017-03-15 21:43:20 +08:00
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU Nums from %q: %v", line, err)
}
cpuNum = int(t)
t2, err := strconv.ParseInt(matches[2], 10, 32)
if err != nil {
2017-03-15 21:43:20 +08:00
return c, 0, fmt.Errorf("unable to parse FreeBSD CPU cores from %q: %v", line, err)
}
c.Cores = int32(t2)
2014-05-16 17:39:17 +08:00
}
}
return c, cpuNum, nil
2014-05-16 17:39:17 +08:00
}