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