2016-02-12 18:02:16 +00:00
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
package cpu
|
|
|
|
|
|
|
|
import (
|
2018-03-06 00:52:43 +00:00
|
|
|
"context"
|
2020-06-03 20:09:27 +00:00
|
|
|
"os/exec"
|
2016-02-12 18:02:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-03-15 08:36:59 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// sys/resource.h
|
|
|
|
const (
|
|
|
|
CPUser = 0
|
|
|
|
CPNice = 1
|
|
|
|
CPSys = 2
|
|
|
|
CPIntr = 3
|
|
|
|
CPIdle = 4
|
|
|
|
CPUStates = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
// default value. from time.h
|
|
|
|
var ClocksPerSec = float64(128)
|
|
|
|
|
2020-06-03 20:09:27 +00:00
|
|
|
func init() {
|
|
|
|
getconf, err := exec.LookPath("getconf")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out, err := invoke.Command(getconf, "CLK_TCK")
|
|
|
|
// ignore errors
|
|
|
|
if err == nil {
|
|
|
|
i, err := strconv.ParseFloat(strings.TrimSpace(string(out)), 64)
|
|
|
|
if err == nil {
|
|
|
|
ClocksPerSec = float64(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
func Times(percpu bool) ([]TimesStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return TimesWithContext(context.Background(), percpu)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
|
2016-02-12 18:02:16 +00:00
|
|
|
if percpu {
|
|
|
|
return perCPUTimes()
|
|
|
|
}
|
|
|
|
|
|
|
|
return allCPUTimes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns only one CPUInfoStat on FreeBSD
|
2016-05-09 15:19:04 +00:00
|
|
|
func Info() ([]InfoStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return InfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
2016-05-09 15:19:04 +00:00
|
|
|
var ret []InfoStat
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2016-05-09 15:19:04 +00:00
|
|
|
c := InfoStat{}
|
2020-03-15 08:36:59 +00:00
|
|
|
c.ModelName, _ = unix.Sysctl("machdep.cpu.brand_string")
|
|
|
|
family, _ := unix.SysctlUint32("machdep.cpu.family")
|
|
|
|
c.Family = strconv.FormatUint(uint64(family), 10)
|
|
|
|
model, _ := unix.SysctlUint32("machdep.cpu.model")
|
|
|
|
c.Model = strconv.FormatUint(uint64(model), 10)
|
|
|
|
stepping, _ := unix.SysctlUint32("machdep.cpu.stepping")
|
|
|
|
c.Stepping = int32(stepping)
|
|
|
|
features, err := unix.Sysctl("machdep.cpu.features")
|
|
|
|
if err == nil {
|
|
|
|
for _, v := range strings.Fields(features) {
|
|
|
|
c.Flags = append(c.Flags, strings.ToLower(v))
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
2020-03-15 08:36:59 +00:00
|
|
|
}
|
|
|
|
leaf7Features, err := unix.Sysctl("machdep.cpu.leaf7_features")
|
|
|
|
if err == nil {
|
|
|
|
for _, v := range strings.Fields(leaf7Features) {
|
|
|
|
c.Flags = append(c.Flags, strings.ToLower(v))
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 08:36:59 +00:00
|
|
|
extfeatures, err := unix.Sysctl("machdep.cpu.extfeatures")
|
|
|
|
if err == nil {
|
|
|
|
for _, v := range strings.Fields(extfeatures) {
|
|
|
|
c.Flags = append(c.Flags, strings.ToLower(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cores, _ := unix.SysctlUint32("machdep.cpu.core_count")
|
|
|
|
c.Cores = int32(cores)
|
|
|
|
cacheSize, _ := unix.SysctlUint32("machdep.cpu.cache.size")
|
|
|
|
c.CacheSize = int32(cacheSize)
|
|
|
|
c.VendorID, _ = unix.Sysctl("machdep.cpu.vendor")
|
2016-02-12 18:02:16 +00:00
|
|
|
|
|
|
|
// Use the rated frequency of the CPU. This is a static value and does not
|
|
|
|
// account for low power or Turbo Boost modes.
|
2020-03-15 08:36:59 +00:00
|
|
|
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2020-03-15 08:36:59 +00:00
|
|
|
c.Mhz = float64(cpuFrequency) / 1000000.0
|
|
|
|
|
|
|
|
return append(ret, c), nil
|
|
|
|
}
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2020-03-15 08:36:59 +00:00
|
|
|
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
|
|
|
var cpuArgument string
|
|
|
|
if logical {
|
|
|
|
cpuArgument = "hw.logicalcpu"
|
|
|
|
} else {
|
|
|
|
cpuArgument = "hw.physicalcpu"
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := unix.SysctlUint32(cpuArgument)
|
2016-02-12 18:02:16 +00:00
|
|
|
if err != nil {
|
2020-03-15 08:36:59 +00:00
|
|
|
return 0, err
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 08:36:59 +00:00
|
|
|
return int(count), nil
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|