2014-08-08 22:09:28 +08:00
|
|
|
// +build darwin
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package cpu
|
2014-08-08 22:09:28 +08:00
|
|
|
|
|
|
|
import (
|
2014-08-08 22:50:15 +08:00
|
|
|
"os/exec"
|
2014-08-08 22:09:28 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-08-28 16:01:43 +08:00
|
|
|
)
|
2014-11-27 09:18:15 +08:00
|
|
|
|
2015-08-28 16:01:43 +08:00
|
|
|
// sys/resource.h
|
|
|
|
const (
|
|
|
|
CPUser = 0
|
|
|
|
CPNice = 1
|
|
|
|
CPSys = 2
|
|
|
|
CPIntr = 3
|
|
|
|
CPIdle = 4
|
|
|
|
CPUStates = 5
|
2014-08-08 22:09:28 +08:00
|
|
|
)
|
|
|
|
|
2015-08-28 16:01:43 +08:00
|
|
|
// default value. from time.h
|
|
|
|
var ClocksPerSec = float64(128)
|
2014-08-08 22:09:28 +08:00
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func Times(percpu bool) ([]TimesStat, error) {
|
2015-08-28 16:01:43 +08:00
|
|
|
if percpu {
|
|
|
|
return perCPUTimes()
|
|
|
|
}
|
|
|
|
|
|
|
|
return allCPUTimes()
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:09:28 +08:00
|
|
|
// Returns only one CPUInfoStat on FreeBSD
|
2016-03-22 22:09:12 +08:00
|
|
|
func Info() ([]InfoStat, error) {
|
|
|
|
var ret []InfoStat
|
2016-04-01 21:13:05 +08:00
|
|
|
sysctl, err := exec.LookPath("/usr/sbin/sysctl")
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2016-05-20 16:59:41 +08:00
|
|
|
out, err := invoke.Command(sysctl, "machdep.cpu")
|
2014-08-08 22:50:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
c := InfoStat{}
|
2014-08-08 22:50:15 +08:00
|
|
|
for _, line := range strings.Split(string(out), "\n") {
|
|
|
|
values := strings.Fields(line)
|
2014-11-02 17:29:12 +08:00
|
|
|
if len(values) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-08 22:50:15 +08:00
|
|
|
|
2014-09-20 09:38:21 +08:00
|
|
|
t, err := strconv.ParseInt(values[1], 10, 64)
|
2014-11-02 17:29:12 +08:00
|
|
|
// err is not checked here because some value is string.
|
2014-08-08 22:50:15 +08:00
|
|
|
if strings.HasPrefix(line, "machdep.cpu.brand_string") {
|
|
|
|
c.ModelName = strings.Join(values[1:], " ")
|
|
|
|
} else if strings.HasPrefix(line, "machdep.cpu.family") {
|
|
|
|
c.Family = values[1]
|
|
|
|
} else if strings.HasPrefix(line, "machdep.cpu.model") {
|
|
|
|
c.Model = values[1]
|
|
|
|
} else if strings.HasPrefix(line, "machdep.cpu.stepping") {
|
2014-11-02 17:29:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2014-09-19 21:41:29 +08:00
|
|
|
c.Stepping = int32(t)
|
2014-08-08 22:50:15 +08:00
|
|
|
} else if strings.HasPrefix(line, "machdep.cpu.features") {
|
|
|
|
for _, v := range values[1:] {
|
|
|
|
c.Flags = append(c.Flags, strings.ToLower(v))
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(line, "machdep.cpu.leaf7_features") {
|
|
|
|
for _, v := range values[1:] {
|
2014-08-08 22:09:28 +08:00
|
|
|
c.Flags = append(c.Flags, strings.ToLower(v))
|
|
|
|
}
|
2014-08-08 22:50:15 +08:00
|
|
|
} else if strings.HasPrefix(line, "machdep.cpu.extfeatures") {
|
|
|
|
for _, v := range values[1:] {
|
2014-08-08 22:09:28 +08:00
|
|
|
c.Flags = append(c.Flags, strings.ToLower(v))
|
|
|
|
}
|
2014-08-08 22:50:15 +08:00
|
|
|
} else if strings.HasPrefix(line, "machdep.cpu.core_count") {
|
2014-11-02 17:29:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2014-09-20 09:17:00 +08:00
|
|
|
c.Cores = int32(t)
|
2014-08-08 22:55:18 +08:00
|
|
|
} else if strings.HasPrefix(line, "machdep.cpu.cache.size") {
|
2014-11-02 17:29:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2014-09-20 09:17:00 +08:00
|
|
|
c.CacheSize = int32(t)
|
2014-08-08 22:55:18 +08:00
|
|
|
} else if strings.HasPrefix(line, "machdep.cpu.vendor") {
|
|
|
|
c.VendorID = values[1]
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
2015-08-27 02:45:09 +08:00
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
|
2015-08-27 02:45:09 +08:00
|
|
|
// Use the rated frequency of the CPU. This is a static value and does not
|
|
|
|
// account for low power or Turbo Boost modes.
|
2016-05-20 16:59:41 +08:00
|
|
|
out, err = invoke.Command(sysctl, "hw.cpufrequency")
|
2015-08-27 02:45:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
values := strings.Fields(string(out))
|
2016-08-24 07:48:25 +08:00
|
|
|
hz, err := strconv.ParseFloat(values[1], 64)
|
2015-08-27 02:45:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
2016-08-24 07:48:25 +08:00
|
|
|
c.Mhz = hz / 1000000.0
|
2014-08-08 22:09:28 +08:00
|
|
|
|
|
|
|
return append(ret, c), nil
|
|
|
|
}
|