• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

cmd/cpuid/H17-Mar-2021-5640

testdata/H03-May-2022-7868

.gitignoreH A D17-Mar-2021266 2519

.goreleaser.ymlH A D17-Mar-20211.4 KiB7571

.travis.ymlH A D17-Mar-20211.5 KiB6860

LICENSEH A D17-Mar-20211.1 KiB2317

README.mdH A D17-Mar-20214 KiB138100

cpuid.goH A D17-Mar-202129.5 KiB1,018810

cpuid_386.sH A D17-Mar-2021960 4331

cpuid_amd64.sH A D17-Mar-2021967 4331

cpuid_arm64.sH A D17-Mar-2021855 2714

cpuid_test.goH A D17-Mar-20216.4 KiB244206

detect_arm64.goH A D17-Mar-20219.7 KiB24789

detect_ref.goH A D17-Mar-2021459 158

detect_x86.goH A D17-Mar-2021958 3426

featureid_string.goH A D17-Mar-20214.8 KiB174159

go.modH A D17-Mar-202146 42

mockcpu_test.goH A D17-Mar-20215.3 KiB214191

os_darwin_arm64.goH A D17-Mar-2021684 209

os_linux_arm64.goH A D17-Mar-20213.9 KiB13197

os_other_arm64.goH A D17-Mar-2021349 188

os_safe_linux_arm64.goH A D17-Mar-2021130 82

os_unsafe_linux_arm64.goH A D17-Mar-2021215 113

test-architectures.shH A D17-Mar-2021445 1612

README.md

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