forked from shirou/gopsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_freebsd.go
87 lines (72 loc) · 2.24 KB
/
cpu_freebsd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// +build freebsd
package gopsutil
import (
"regexp"
"strconv"
"strings"
)
// sys/resource.h
const (
CPUser = 0
CPNice = 1
CPSys = 2
CPIntr = 3
CPIdle = 4
CPUStates = 5
)
// time.h
const (
ClocksPerSec = 128
)
// TODO: get per cpus
func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
var ret []CPUTimesStat
cpuTime, err := doSysctrl("kern.cp_time")
if err != nil {
return ret, err
}
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)
c := CPUTimesStat{
User: float32(user / ClocksPerSec),
Nice: float32(nice / ClocksPerSec),
System: float32(sys / ClocksPerSec),
Idle: float32(idle / ClocksPerSec),
Irq: float32(intr / ClocksPerSec),
}
ret = append(ret, c)
return ret, nil
}
// 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])
} 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])
} 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])
}
}
return append(ret, c), nil
}