cpu_darwin.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // +build darwin
  2. package cpu
  3. import (
  4. "context"
  5. "strconv"
  6. "strings"
  7. "github.com/tklauser/go-sysconf"
  8. "golang.org/x/sys/unix"
  9. )
  10. // sys/resource.h
  11. const (
  12. CPUser = 0
  13. CPNice = 1
  14. CPSys = 2
  15. CPIntr = 3
  16. CPIdle = 4
  17. CPUStates = 5
  18. )
  19. // default value. from time.h
  20. var ClocksPerSec = float64(128)
  21. func init() {
  22. clkTck, err := sysconf.Sysconf(sysconf.SC_CLK_TCK)
  23. // ignore errors
  24. if err == nil {
  25. ClocksPerSec = float64(clkTck)
  26. }
  27. }
  28. func Times(percpu bool) ([]TimesStat, error) {
  29. return TimesWithContext(context.Background(), percpu)
  30. }
  31. func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
  32. if percpu {
  33. return perCPUTimes()
  34. }
  35. return allCPUTimes()
  36. }
  37. // Returns only one CPUInfoStat on FreeBSD
  38. func Info() ([]InfoStat, error) {
  39. return InfoWithContext(context.Background())
  40. }
  41. func InfoWithContext(ctx context.Context) ([]InfoStat, error) {
  42. var ret []InfoStat
  43. c := InfoStat{}
  44. c.ModelName, _ = unix.Sysctl("machdep.cpu.brand_string")
  45. family, _ := unix.SysctlUint32("machdep.cpu.family")
  46. c.Family = strconv.FormatUint(uint64(family), 10)
  47. model, _ := unix.SysctlUint32("machdep.cpu.model")
  48. c.Model = strconv.FormatUint(uint64(model), 10)
  49. stepping, _ := unix.SysctlUint32("machdep.cpu.stepping")
  50. c.Stepping = int32(stepping)
  51. features, err := unix.Sysctl("machdep.cpu.features")
  52. if err == nil {
  53. for _, v := range strings.Fields(features) {
  54. c.Flags = append(c.Flags, strings.ToLower(v))
  55. }
  56. }
  57. leaf7Features, err := unix.Sysctl("machdep.cpu.leaf7_features")
  58. if err == nil {
  59. for _, v := range strings.Fields(leaf7Features) {
  60. c.Flags = append(c.Flags, strings.ToLower(v))
  61. }
  62. }
  63. extfeatures, err := unix.Sysctl("machdep.cpu.extfeatures")
  64. if err == nil {
  65. for _, v := range strings.Fields(extfeatures) {
  66. c.Flags = append(c.Flags, strings.ToLower(v))
  67. }
  68. }
  69. cores, _ := unix.SysctlUint32("machdep.cpu.core_count")
  70. c.Cores = int32(cores)
  71. cacheSize, _ := unix.SysctlUint32("machdep.cpu.cache.size")
  72. c.CacheSize = int32(cacheSize)
  73. c.VendorID, _ = unix.Sysctl("machdep.cpu.vendor")
  74. // Use the rated frequency of the CPU. This is a static value and does not
  75. // account for low power or Turbo Boost modes.
  76. cpuFrequency, err := unix.SysctlUint64("hw.cpufrequency")
  77. if err != nil {
  78. return ret, err
  79. }
  80. c.Mhz = float64(cpuFrequency) / 1000000.0
  81. return append(ret, c), nil
  82. }
  83. func CountsWithContext(ctx context.Context, logical bool) (int, error) {
  84. var cpuArgument string
  85. if logical {
  86. cpuArgument = "hw.logicalcpu"
  87. } else {
  88. cpuArgument = "hw.physicalcpu"
  89. }
  90. count, err := unix.SysctlUint32(cpuArgument)
  91. if err != nil {
  92. return 0, err
  93. }
  94. return int(count), nil
  95. }