2021-12-23 05:54:41 +08:00
|
|
|
//go:build darwin
|
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 (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2014-08-08 22:09:28 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
[cpu][darwin] Remove calls to sysctl binary in cpu/cpu_darwin.go #639
Empirical benchmark (calling to cpu.Info):
Lomanics-iMac:~ lomanic$ time ./cpu_info
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci","fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm"],"microcode":""}
real 0m0.049s
user 0m0.023s
sys 0m0.041s
Lomanics-iMac:~ lomanic$ time ./cpu_info.fixed
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm","syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci"],"microcode":""}
real 0m0.010s
user 0m0.004s
sys 0m0.006s
2019-02-23 03:59:16 +08:00
|
|
|
|
2021-02-16 22:27:05 +08:00
|
|
|
"github.com/tklauser/go-sysconf"
|
[cpu][darwin] Remove calls to sysctl binary in cpu/cpu_darwin.go #639
Empirical benchmark (calling to cpu.Info):
Lomanics-iMac:~ lomanic$ time ./cpu_info
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci","fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm"],"microcode":""}
real 0m0.049s
user 0m0.023s
sys 0m0.041s
Lomanics-iMac:~ lomanic$ time ./cpu_info.fixed
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm","syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci"],"microcode":""}
real 0m0.010s
user 0m0.004s
sys 0m0.006s
2019-02-23 03:59:16 +08:00
|
|
|
"golang.org/x/sys/unix"
|
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
|
2021-11-06 17:53:56 +08:00
|
|
|
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
|
|
|
|
2020-05-12 19:36:13 +08:00
|
|
|
func init() {
|
2021-02-16 22:27:05 +08:00
|
|
|
clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
|
2020-05-12 19:36:13 +08:00
|
|
|
// ignore errors
|
|
|
|
if err == nil {
|
2021-02-16 22:27:05 +08:00
|
|
|
ClocksPerSec = float64(clkTck)
|
2020-05-12 19:36:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +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) {
|
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) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return InfoWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
|
2016-03-22 22:09:12 +08:00
|
|
|
var ret []InfoStat
|
2014-08-08 22:50:15 +08:00
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
c := InfoStat{}
|
[cpu][darwin] Remove calls to sysctl binary in cpu/cpu_darwin.go #639
Empirical benchmark (calling to cpu.Info):
Lomanics-iMac:~ lomanic$ time ./cpu_info
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci","fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm"],"microcode":""}
real 0m0.049s
user 0m0.023s
sys 0m0.041s
Lomanics-iMac:~ lomanic$ time ./cpu_info.fixed
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm","syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci"],"microcode":""}
real 0m0.010s
user 0m0.004s
sys 0m0.006s
2019-02-23 03:59:16 +08: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))
|
2014-11-02 17:29:12 +08:00
|
|
|
}
|
[cpu][darwin] Remove calls to sysctl binary in cpu/cpu_darwin.go #639
Empirical benchmark (calling to cpu.Info):
Lomanics-iMac:~ lomanic$ time ./cpu_info
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci","fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm"],"microcode":""}
real 0m0.049s
user 0m0.023s
sys 0m0.041s
Lomanics-iMac:~ lomanic$ time ./cpu_info.fixed
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm","syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci"],"microcode":""}
real 0m0.010s
user 0m0.004s
sys 0m0.006s
2019-02-23 03:59:16 +08: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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
extfeatures, err := unix.Sysctl("machdep.cpu.extfeatures")
|
|
|
|
if err == nil {
|
|
|
|
for _, v := range strings.Fields(extfeatures) {
|
|
|
|
c.Flags = append(c.Flags, strings.ToLower(v))
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
2015-08-27 02:45:09 +08:00
|
|
|
}
|
[cpu][darwin] Remove calls to sysctl binary in cpu/cpu_darwin.go #639
Empirical benchmark (calling to cpu.Info):
Lomanics-iMac:~ lomanic$ time ./cpu_info
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci","fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm"],"microcode":""}
real 0m0.049s
user 0m0.023s
sys 0m0.041s
Lomanics-iMac:~ lomanic$ time ./cpu_info.fixed
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm","syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci"],"microcode":""}
real 0m0.010s
user 0m0.004s
sys 0m0.006s
2019-02-23 03:59:16 +08:00
|
|
|
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")
|
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.
|
[cpu][darwin] Remove calls to sysctl binary in cpu/cpu_darwin.go #639
Empirical benchmark (calling to cpu.Info):
Lomanics-iMac:~ lomanic$ time ./cpu_info
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci","fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm"],"microcode":""}
real 0m0.049s
user 0m0.023s
sys 0m0.041s
Lomanics-iMac:~ lomanic$ time ./cpu_info.fixed
info 0: {"cpu":0,"vendorId":"GenuineIntel","family":"6","model":"30","stepping":5,"physicalId":"","coreId":"","cores":2,"modelName":"Intel(R) Core(TM) i5-6440HQ CPU @ 2.60GHz","mhz":2590,"cacheSize":256,"flags":["fpu","vme","de","pse","tsc","msr","pae","mce","cx8","apic","sep","mtrr","pge","mca","cmov","pat","pse36","clfsh","mmx","fxsr","sse","sse2","htt","sse3","ssse3","cx16","sse4.1","sse4.2","popcnt","vmm","syscall","xd","em64t","lahf","lzcnt","prefetchw","rdtscp","tsci"],"microcode":""}
real 0m0.010s
user 0m0.004s
sys 0m0.006s
2019-02-23 03:59:16 +08:00
|
|
|
cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
|
2021-12-04 12:23:33 +08:00
|
|
|
if err == nil {
|
|
|
|
c.Mhz = float64(cpuFrequency) / 1000000.0
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return append(ret, c), nil
|
|
|
|
}
|
2019-03-03 21:38:33 +08:00
|
|
|
|
|
|
|
func CountsWithContext(ctx context.Context, logical bool) (int, error) {
|
2019-03-17 22:18:29 +08:00
|
|
|
var cpuArgument string
|
|
|
|
if logical {
|
|
|
|
cpuArgument = "hw.logicalcpu"
|
|
|
|
} else {
|
|
|
|
cpuArgument = "hw.physicalcpu"
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := unix.SysctlUint32(cpuArgument)
|
|
|
|
if err != nil {
|
2019-03-21 22:52:34 +08:00
|
|
|
return 0, err
|
2019-03-17 22:18:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return int(count), nil
|
2019-03-03 21:38:33 +08:00
|
|
|
}
|