cpu_darwin.go 2.6 KB

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