1# cpuid 2Package cpuid provides information about the CPU running the current program. 3 4CPU features are detected on startup, and kept for fast access through the life of the application. 5Currently x86 / x64 (AMD64/i386) and ARM (ARM64) is supported, and no external C (cgo) code is used, which should make the library very easy to use. 6 7You can access the CPU information by accessing the shared CPU variable of the cpuid library. 8 9Package home: https://github.com/klauspost/cpuid 10 11[![PkgGoDev](https://pkg.go.dev/badge/github.com/klauspost/cpuid)](https://pkg.go.dev/github.com/klauspost/cpuid/v2) 12[![Build Status][3]][4] 13 14[3]: https://travis-ci.org/klauspost/cpuid.svg?branch=master 15[4]: https://travis-ci.org/klauspost/cpuid 16 17## installing 18 19`go get -u github.com/klauspost/cpuid/v2` using modules. 20 21Drop `v2` for others. 22 23## example 24 25```Go 26package main 27 28import ( 29 "fmt" 30 "strings" 31 32 . "github.com/klauspost/cpuid/v2" 33) 34 35func main() { 36 // Print basic CPU information: 37 fmt.Println("Name:", CPU.BrandName) 38 fmt.Println("PhysicalCores:", CPU.PhysicalCores) 39 fmt.Println("ThreadsPerCore:", CPU.ThreadsPerCore) 40 fmt.Println("LogicalCores:", CPU.LogicalCores) 41 fmt.Println("Family", CPU.Family, "Model:", CPU.Model, "Vendor ID:", CPU.VendorID) 42 fmt.Println("Features:", fmt.Sprintf(strings.Join(CPU.FeatureSet(), ","))) 43 fmt.Println("Cacheline bytes:", CPU.CacheLine) 44 fmt.Println("L1 Data Cache:", CPU.Cache.L1D, "bytes") 45 fmt.Println("L1 Instruction Cache:", CPU.Cache.L1D, "bytes") 46 fmt.Println("L2 Cache:", CPU.Cache.L2, "bytes") 47 fmt.Println("L3 Cache:", CPU.Cache.L3, "bytes") 48 fmt.Println("Frequency", CPU.Hz, "hz") 49 50 // Test if we have these specific features: 51 if CPU.Supports(SSE, SSE2) { 52 fmt.Println("We have Streaming SIMD 2 Extensions") 53 } 54} 55``` 56 57Sample output: 58``` 59>go run main.go 60Name: AMD Ryzen 9 3950X 16-Core Processor 61PhysicalCores: 16 62ThreadsPerCore: 2 63LogicalCores: 32 64Family 23 Model: 113 Vendor ID: AMD 65Features: ADX,AESNI,AVX,AVX2,BMI1,BMI2,CLMUL,CMOV,CX16,F16C,FMA3,HTT,HYPERVISOR,LZCNT,MMX,MMXEXT,NX,POPCNT,RDRAND,RDSEED,RDTSCP,SHA,SSE,SSE2,SSE3,SSE4,SSE42,SSE4A,SSSE3 66Cacheline bytes: 64 67L1 Data Cache: 32768 bytes 68L1 Instruction Cache: 32768 bytes 69L2 Cache: 524288 bytes 70L3 Cache: 16777216 bytes 71Frequency 0 hz 72We have Streaming SIMD 2 Extensions 73``` 74 75# usage 76 77The `cpuid.CPU` provides access to CPU features. Use `cpuid.CPU.Supports()` to check for CPU features. 78A faster `cpuid.CPU.Has()` is provided which will usually be inlined by the gc compiler. 79 80Note that for some cpu/os combinations some features will not be detected. 81`amd64` has rather good support and should work reliably on all platforms. 82 83Note that hypervisors may not pass through all CPU features. 84 85## arm64 feature detection 86 87Not all operating systems provide ARM features directly 88and there is no safe way to do so for the rest. 89 90Currently `arm64/linux` and `arm64/freebsd` should be quite reliable. 91`arm64/darwin` adds features expected from the M1 processor, but a lot remains undetected. 92 93A `DetectARM()` can be used if you are able to control your deployment, 94it will detect CPU features, but may crash if the OS doesn't intercept the calls. 95A `-cpu.arm` flag for detecting unsafe ARM features can be added. See below. 96 97Note that currently only features are detected on ARM, 98no additional information is currently available. 99 100## flags 101 102It is possible to add flags that affects cpu detection. 103 104For this the `Flags()` command is provided. 105 106This must be called *before* `flag.Parse()` AND after the flags have been parsed `Detect()` must be called. 107 108This means that any detection used in `init()` functions will not contain these flags. 109 110Example: 111 112```Go 113package main 114 115import ( 116 "flag" 117 "fmt" 118 "strings" 119 120 "github.com/klauspost/cpuid/v2" 121) 122 123func main() { 124 cpuid.Flags() 125 flag.Parse() 126 cpuid.Detect() 127 128 // Test if we have these specific features: 129 if cpuid.CPU.Supports(cpuid.SSE, cpuid.SSE2) { 130 fmt.Println("We have Streaming SIMD 2 Extensions") 131 } 132} 133``` 134 135# license 136 137This code is published under an MIT license. See LICENSE file for more information. 138