gitea/vendor/github.com/klauspost/cpuid/v2
techknowlogick 7613f31c6b
Update vendor 20210707 (#16366)
* update gitea.com/go-chi/binding

* update github.com/blevesearch/bleve/v2

* update github.com/caddyserver/certmagic

* update github.com/go-git/go-git/v5

* update github.com/lafriks/xormstore

* update github.com/yuin/goldmark

* Revert "update gitea.com/go-chi/binding"

This reverts commit dea2f292b1.
2021-07-07 15:41:05 -04:00
..
.gitignore go1.16 (#14783) 2021-02-28 18:08:33 -05:00
.goreleaser.yml Vendor Update (#16121) 2021-06-10 16:44:25 +02:00
CONTRIBUTING.txt go1.16 (#14783) 2021-02-28 18:08:33 -05:00
cpuid_386.s Vendor Update (#16121) 2021-06-10 16:44:25 +02:00
cpuid_amd64.s Vendor Update (#16121) 2021-06-10 16:44:25 +02:00
cpuid_arm64.s go1.16 (#14783) 2021-02-28 18:08:33 -05:00
cpuid.go Update vendor 20210707 (#16366) 2021-07-07 15:41:05 -04:00
detect_arm64.go go1.16 (#14783) 2021-02-28 18:08:33 -05:00
detect_ref.go go1.16 (#14783) 2021-02-28 18:08:33 -05:00
detect_x86.go Update Vendor (#16325) 2021-07-04 04:06:10 +02:00
featureid_string.go Update Vendor (#16325) 2021-07-04 04:06:10 +02:00
go.mod go1.16 (#14783) 2021-02-28 18:08:33 -05:00
LICENSE go1.16 (#14783) 2021-02-28 18:08:33 -05:00
os_darwin_arm64.go Vendor Update (#16121) 2021-06-10 16:44:25 +02:00
os_linux_arm64.go Vendor Update (#16121) 2021-06-10 16:44:25 +02:00
os_other_arm64.go Vendor Update (#16121) 2021-06-10 16:44:25 +02:00
os_safe_linux_arm64.go Vendor Update (#16121) 2021-06-10 16:44:25 +02:00
os_unsafe_linux_arm64.go Vendor Update (#16121) 2021-06-10 16:44:25 +02:00
README.md go1.16 (#14783) 2021-02-28 18:08:33 -05:00
test-architectures.sh Vendor Update (#16121) 2021-06-10 16:44:25 +02:00

cpuid

Package cpuid provides information about the CPU running the current program.

CPU features are detected on startup, and kept for fast access through the life of the application.
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.

You can access the CPU information by accessing the shared CPU variable of the cpuid library.

Package home: https://github.com/klauspost/cpuid

PkgGoDev
Build Status

installing

go get -u github.com/klauspost/cpuid/v2 using modules.

Drop v2 for others.

example

package main

import (
	"fmt"
	"strings"

	. "github.com/klauspost/cpuid/v2"
)

func main() {
	// Print basic CPU information:
	fmt.Println("Name:", CPU.BrandName)
	fmt.Println("PhysicalCores:", CPU.PhysicalCores)
	fmt.Println("ThreadsPerCore:", CPU.ThreadsPerCore)
	fmt.Println("LogicalCores:", CPU.LogicalCores)
	fmt.Println("Family", CPU.Family, "Model:", CPU.Model, "Vendor ID:", CPU.VendorID)
	fmt.Println("Features:", fmt.Sprintf(strings.Join(CPU.FeatureSet(), ",")))
	fmt.Println("Cacheline bytes:", CPU.CacheLine)
	fmt.Println("L1 Data Cache:", CPU.Cache.L1D, "bytes")
	fmt.Println("L1 Instruction Cache:", CPU.Cache.L1D, "bytes")
	fmt.Println("L2 Cache:", CPU.Cache.L2, "bytes")
	fmt.Println("L3 Cache:", CPU.Cache.L3, "bytes")
	fmt.Println("Frequency", CPU.Hz, "hz")

	// Test if we have these specific features:
	if CPU.Supports(SSE, SSE2) {
		fmt.Println("We have Streaming SIMD 2 Extensions")
	}
}

Sample output:

>go run main.go
Name: AMD Ryzen 9 3950X 16-Core Processor
PhysicalCores: 16
ThreadsPerCore: 2
LogicalCores: 32
Family 23 Model: 113 Vendor ID: AMD
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
Cacheline bytes: 64
L1 Data Cache: 32768 bytes
L1 Instruction Cache: 32768 bytes
L2 Cache: 524288 bytes
L3 Cache: 16777216 bytes
Frequency 0 hz
We have Streaming SIMD 2 Extensions

usage

The cpuid.CPU provides access to CPU features. Use cpuid.CPU.Supports() to check for CPU features.
A faster cpuid.CPU.Has() is provided which will usually be inlined by the gc compiler.

Note that for some cpu/os combinations some features will not be detected.
amd64 has rather good support and should work reliably on all platforms.

Note that hypervisors may not pass through all CPU features.

arm64 feature detection

Not all operating systems provide ARM features directly
and there is no safe way to do so for the rest.

Currently arm64/linux and arm64/freebsd should be quite reliable.
arm64/darwin adds features expected from the M1 processor, but a lot remains undetected.

A DetectARM() can be used if you are able to control your deployment,
it will detect CPU features, but may crash if the OS doesn't intercept the calls.
A -cpu.arm flag for detecting unsafe ARM features can be added. See below.

Note that currently only features are detected on ARM,
no additional information is currently available.

flags

It is possible to add flags that affects cpu detection.

For this the Flags() command is provided.

This must be called before flag.Parse() AND after the flags have been parsed Detect() must be called.

This means that any detection used in init() functions will not contain these flags.

Example:

package main

import (
	"flag"
	"fmt"
	"strings"

	"github.com/klauspost/cpuid/v2"
)

func main() {
	cpuid.Flags()
	flag.Parse()
	cpuid.Detect()

	// Test if we have these specific features:
	if cpuid.CPU.Supports(cpuid.SSE, cpuid.SSE2) {
		fmt.Println("We have Streaming SIMD 2 Extensions")
	}
}

license

This code is published under an MIT license. See LICENSE file for more information.