catinello.eu/x/cpuid@v0.0.0-20231214173555-81a76c018636/README.md (about)

     1  # cpuid
     2  Package cpuid provides information about the CPU running the current program.
     3  
     4  CPU features are detected on startup, and kept for fast access through the life of the application.
     5  Currently 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  
     7  You can access the CPU information by accessing the shared CPU variable of the cpuid library.
     8  
     9  Package 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  
    21  Drop `v2` for others.
    22  
    23  ## example
    24  
    25  ```Go
    26  package main
    27  
    28  import (
    29  	"fmt"
    30  	"strings"
    31  
    32  	. "github.com/klauspost/cpuid/v2"
    33  )
    34  
    35  func 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:", 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.L1I, "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  
    57  Sample output:
    58  ```
    59  >go run main.go
    60  Name: AMD Ryzen 9 3950X 16-Core Processor
    61  PhysicalCores: 16
    62  ThreadsPerCore: 2
    63  LogicalCores: 32
    64  Family 23 Model: 113 Vendor ID: AMD
    65  Features: 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
    66  Cacheline bytes: 64
    67  L1 Data Cache: 32768 bytes
    68  L1 Instruction Cache: 32768 bytes
    69  L2 Cache: 524288 bytes
    70  L3 Cache: 16777216 bytes
    71  Frequency 0 hz
    72  We have Streaming SIMD 2 Extensions
    73  ```
    74  
    75  # usage
    76  
    77  The `cpuid.CPU` provides access to CPU features. Use `cpuid.CPU.Supports()` to check for CPU features.
    78  A faster `cpuid.CPU.Has()` is provided which will usually be inlined by the gc compiler.  
    79  
    80  Note 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  
    83  Note that hypervisors may not pass through all CPU features.
    84  
    85  ## arm64 feature detection
    86  
    87  Not all operating systems provide ARM features directly 
    88  and there is no safe way to do so for the rest.
    89  
    90  Currently `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  
    93  A `DetectARM()` can be used if you are able to control your deployment,
    94  it will detect CPU features, but may crash if the OS doesn't intercept the calls.
    95  A `-cpu.arm` flag for detecting unsafe ARM features can be added. See below.
    96   
    97  Note that currently only features are detected on ARM, 
    98  no additional information is currently available. 
    99  
   100  ## flags
   101  
   102  It is possible to add flags that affects cpu detection.
   103  
   104  For this the `Flags()` command is provided.
   105  
   106  This must be called *before* `flag.Parse()` AND after the flags have been parsed `Detect()` must be called.
   107  
   108  This means that any detection used in `init()` functions will not contain these flags.
   109  
   110  Example:
   111  
   112  ```Go
   113  package main
   114  
   115  import (
   116  	"flag"
   117  	"fmt"
   118  	"strings"
   119  
   120  	"github.com/klauspost/cpuid/v2"
   121  )
   122  
   123  func 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  ## commandline
   136  
   137  Download as binary from: https://github.com/klauspost/cpuid/releases
   138  
   139  Install from source:
   140  
   141  `go install github.com/klauspost/cpuid/v2/cmd/cpuid@latest`
   142  
   143  ### Example
   144  
   145  ```
   146  λ cpuid
   147  Name: AMD Ryzen 9 3950X 16-Core Processor
   148  Vendor String: AuthenticAMD
   149  Vendor ID: AMD
   150  PhysicalCores: 16
   151  Threads Per Core: 2
   152  Logical Cores: 32
   153  CPU Family 23 Model: 113
   154  Features: ADX,AESNI,AVX,AVX2,BMI1,BMI2,CLMUL,CLZERO,CMOV,CMPXCHG8,CPBOOST,CX16,F16C,FMA3,FXSR,FXSROPT,HTT,HYPERVISOR,LAHF,LZCNT,MCAOVERFLOW,MMX,MMXEXT,MOVBE,NX,OSXSAVE,POPCNT,RDRAND,RDSEED,RDTSCP,SCE,SHA,SSE,SSE2,SSE3,SSE4,SSE42,SSE4A,SSSE3,SUCCOR,X87,XSAVE
   155  Microarchitecture level: 3
   156  Cacheline bytes: 64
   157  L1 Instruction Cache: 32768 bytes
   158  L1 Data Cache: 32768 bytes
   159  L2 Cache: 524288 bytes
   160  L3 Cache: 16777216 bytes
   161  
   162  ```
   163  ### JSON Output:
   164  
   165  ```
   166  λ cpuid --json
   167  {
   168    "BrandName": "AMD Ryzen 9 3950X 16-Core Processor",
   169    "VendorID": 2,
   170    "VendorString": "AuthenticAMD",
   171    "PhysicalCores": 16,
   172    "ThreadsPerCore": 2,
   173    "LogicalCores": 32,
   174    "Family": 23,
   175    "Model": 113,
   176    "CacheLine": 64,
   177    "Hz": 0,
   178    "BoostFreq": 0,
   179    "Cache": {
   180      "L1I": 32768,
   181      "L1D": 32768,
   182      "L2": 524288,
   183      "L3": 16777216
   184    },
   185    "SGX": {
   186      "Available": false,
   187      "LaunchControl": false,
   188      "SGX1Supported": false,
   189      "SGX2Supported": false,
   190      "MaxEnclaveSizeNot64": 0,
   191      "MaxEnclaveSize64": 0,
   192      "EPCSections": null
   193    },
   194    "Features": [
   195      "ADX",
   196      "AESNI",
   197      "AVX",
   198      "AVX2",
   199      "BMI1",
   200      "BMI2",
   201      "CLMUL",
   202      "CLZERO",
   203      "CMOV",
   204      "CMPXCHG8",
   205      "CPBOOST",
   206      "CX16",
   207      "F16C",
   208      "FMA3",
   209      "FXSR",
   210      "FXSROPT",
   211      "HTT",
   212      "HYPERVISOR",
   213      "LAHF",
   214      "LZCNT",
   215      "MCAOVERFLOW",
   216      "MMX",
   217      "MMXEXT",
   218      "MOVBE",
   219      "NX",
   220      "OSXSAVE",
   221      "POPCNT",
   222      "RDRAND",
   223      "RDSEED",
   224      "RDTSCP",
   225      "SCE",
   226      "SHA",
   227      "SSE",
   228      "SSE2",
   229      "SSE3",
   230      "SSE4",
   231      "SSE42",
   232      "SSE4A",
   233      "SSSE3",
   234      "SUCCOR",
   235      "X87",
   236      "XSAVE"
   237    ],
   238    "X64Level": 3
   239  }
   240  ```
   241  
   242  ### Check CPU microarch level
   243  
   244  ```
   245  λ cpuid --check-level=3
   246  2022/03/18 17:04:40 AMD Ryzen 9 3950X 16-Core Processor
   247  2022/03/18 17:04:40 Microarchitecture level 3 is supported. Max level is 3.
   248  Exit Code 0
   249  
   250  λ cpuid --check-level=4
   251  2022/03/18 17:06:18 AMD Ryzen 9 3950X 16-Core Processor
   252  2022/03/18 17:06:18 Microarchitecture level 4 not supported. Max level is 3.
   253  Exit Code 1
   254  ```
   255  
   256  # license
   257  
   258  This code is published under an MIT license. See LICENSE file for more information.