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 (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2014-11-02 08:14:38 +08:00
|
|
|
"fmt"
|
2015-07-17 20:46:26 +08:00
|
|
|
"os/exec"
|
2017-10-05 15:36:05 +08:00
|
|
|
"reflect"
|
2014-05-16 17:39:17 +08:00
|
|
|
"regexp"
|
2019-03-03 21:38:33 +08:00
|
|
|
"runtime"
|
2014-04-23 10:37:00 +08:00
|
|
|
"strconv"
|
2014-05-16 17:39:17 +08:00
|
|
|
"strings"
|
2017-10-05 15:36:05 +08:00
|
|
|
"unsafe"
|
2014-11-27 09:18:15 +08:00
|
|
|
|
2015-10-19 23:04:57 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2017-10-05 15:36:05 +08:00
|
|
|
"golang.org/x/sys/unix"
|
2014-04-23 10:37:00 +08:00
|
|
|
)
|
|
|
|
|
2015-07-17 20:46:26 +08:00
|
|
|
var ClocksPerSec = float64(128)
|
2017-01-22 21:31:05 +08:00
|
|
|
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`)
|
2017-01-29 00:23:21 +08:00
|
|
|
var cpuCores = regexp.MustCompile(`FreeBSD/SMP: (\d*) package\(s\) x (\d*) core\(s\)`)
|
2017-10-05 15:36:05 +08:00
|
|
|
var cpuTimesSize int
|
|
|
|
var emptyTimes cpuTimes
|
2015-07-17 20:46:26 +08:00
|
|
|
|
|
|
|
func init() {
|
2016-04-01 21:13:05 +08:00
|
|
|
getconf, err := exec.LookPath("/usr/bin/getconf")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-05-20 16:59:41 +08:00
|
|
|
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
|
|
|
|
2017-10-05 15:36:05 +08:00
|
|
|
func timeStat(name string, t *cpuTimes) *TimesStat {
|
|
|
|
return &TimesStat{
|
|
|
|
User: float64(t.User) / ClocksPerSec,
|
|
|
|
Nice: float64(t.Nice) / ClocksPerSec,
|
|
|
|
System: float64(t.Sys) / ClocksPerSec,
|
|
|
|
Idle: float64(t.Idle) / ClocksPerSec,
|
|
|
|
Irq: float64(t.Intr) / ClocksPerSec,
|
|
|
|
CPU: name,
|
2014-09-20 09:22:41 +08:00
|
|
|
}
|
2017-10-05 15:36:05 +08:00
|
|
|
}
|
2014-04-23 10:37:00 +08:00
|
|
|
|
2017-10-05 15:36:05 +08:00
|
|
|
func Times(percpu bool) ([]TimesStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return TimesWithContext(context.Background(), percpu)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
2017-10-05 15:36:05 +08:00
|
|
|
if percpu {
|
|
|
|
buf, err := unix.SysctlRaw("kern.cp_times")
|
2014-11-02 08:14:38 +08:00
|
|
|
if err != nil {
|
2017-10-05 15:36:05 +08:00
|
|
|
return nil, err
|
2014-11-02 08:14:38 +08:00
|
|
|
}
|
2014-04-23 10:37:00 +08:00
|
|
|
|
2017-10-05 15:36:05 +08:00
|
|
|
// We can't do this in init due to the conflict with cpu.init()
|
|
|
|
if cpuTimesSize == 0 {
|
|
|
|
cpuTimesSize = int(reflect.TypeOf(cpuTimes{}).Size())
|
2014-11-02 08:14:38 +08:00
|
|
|
}
|
2017-10-05 15:36:05 +08:00
|
|
|
|
|
|
|
ncpus := len(buf) / cpuTimesSize
|
|
|
|
ret := make([]TimesStat, 0, ncpus)
|
|
|
|
for i := 0; i < ncpus; i++ {
|
|
|
|
times := (*cpuTimes)(unsafe.Pointer(&buf[i*cpuTimesSize]))
|
|
|
|
if *times == emptyTimes {
|
|
|
|
// CPU not present
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, *timeStat(fmt.Sprintf("cpu%d", len(ret)), times))
|
2014-11-02 08:14:38 +08:00
|
|
|
}
|
2017-10-05 15:36:05 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
2014-11-02 08:14:38 +08:00
|
|
|
|
2017-10-05 15:36:05 +08:00
|
|
|
buf, err := unix.SysctlRaw("kern.cp_time")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-11-02 08:14:38 +08:00
|
|
|
}
|
2014-04-23 10:37:00 +08:00
|
|
|
|
2017-10-05 15:36:05 +08:00
|
|
|
times := (*cpuTimes)(unsafe.Pointer(&buf[0]))
|
|
|
|
return []TimesStat{*timeStat("cpu-total", times)}, nil
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
2014-05-16 17:39:17 +08:00
|
|
|
|
2016-06-17 10:25:27 +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.
|
2016-03-22 22:09:12 +08:00
|
|
|
func Info() ([]InfoStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return InfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
2016-06-17 10:25:27 +08:00
|
|
|
const dmesgBoot = "/var/run/dmesg.boot"
|
2014-05-16 17:39:17 +08:00
|
|
|
|
2017-01-29 00:23:21 +08:00
|
|
|
c, num, err := parseDmesgBoot(dmesgBoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-05 15:36:05 +08:00
|
|
|
|
|
|
|
var u32 uint32
|
|
|
|
if u32, err = unix.SysctlUint32("hw.clockrate"); err != nil {
|
2016-06-17 10:25:27 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-05 15:36:05 +08:00
|
|
|
c.Mhz = float64(u32)
|
2016-06-17 10:25:27 +08:00
|
|
|
|
2017-10-05 15:36:05 +08:00
|
|
|
if u32, err = unix.SysctlUint32("hw.ncpu"); err != nil {
|
2016-06-17 10:25:27 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-05 15:36:05 +08:00
|
|
|
c.Cores = int32(u32)
|
2016-06-17 10:25:27 +08:00
|
|
|
|
2017-10-05 15:36:05 +08:00
|
|
|
if c.ModelName, err = unix.Sysctl("hw.model"); err != nil {
|
2016-06-17 10:25:27 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-29 00:23:21 +08:00
|
|
|
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)
|
2017-05-10 10:47:26 +08:00
|
|
|
cpuNum := 1 // default cpu num is 1
|
2014-05-16 17:39:17 +08:00
|
|
|
for _, line := range lines {
|
2017-01-22 21:31:05 +08:00
|
|
|
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)
|
2017-01-22 21:31:05 +08:00
|
|
|
} 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))
|
|
|
|
}
|
2017-01-22 21:31:05 +08:00
|
|
|
} 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))
|
|
|
|
}
|
2017-01-29 00:23:21 +08:00
|
|
|
} 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)
|
2017-01-29 00:23:21 +08:00
|
|
|
}
|
|
|
|
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)
|
2017-01-29 00:23:21 +08:00
|
|
|
}
|
|
|
|
c.Cores = int32(t2)
|
2014-05-16 17:39:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 00:23:21 +08:00
|
|
|
return c, cpuNum, nil
|
2014-05-16 17:39:17 +08:00
|
|
|
}
|
2019-03-03 21:38:33 +08:00
|
|
|
|
|
|
|
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
|
|
|
return runtime.NumCPU(), nil
|
|
|
|
}
|