123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727 |
- package cpuid
- import (
- "fmt"
- "testing"
- )
- func TestCPUID(t *testing.T) {
- n := maxFunctionID()
- t.Logf("Max Function:0x%x\n", n)
- n = maxExtendedFunction()
- t.Logf("Max Extended Function:0x%x\n", n)
- t.Log("Name:", CPU.BrandName)
- t.Log("PhysicalCores:", CPU.PhysicalCores)
- t.Log("ThreadsPerCore:", CPU.ThreadsPerCore)
- t.Log("LogicalCores:", CPU.LogicalCores)
- t.Log("Family", CPU.Family, "Model:", CPU.Model)
- t.Log("Features:", CPU.Features)
- t.Log("Cacheline bytes:", CPU.CacheLine)
- t.Log("L1 Instruction Cache:", CPU.Cache.L1I, "bytes")
- t.Log("L1 Data Cache:", CPU.Cache.L1D, "bytes")
- t.Log("L2 Cache:", CPU.Cache.L2, "bytes")
- t.Log("L3 Cache:", CPU.Cache.L3, "bytes")
- if CPU.SSE2() {
- t.Log("We have SSE2")
- }
- }
- func TestDumpCPUID(t *testing.T) {
- n := int(maxFunctionID())
- for i := 0; i <= n; i++ {
- a, b, c, d := cpuidex(uint32(i), 0)
- t.Logf("CPUID %08x: %08x-%08x-%08x-%08x", i, a, b, c, d)
- ex := uint32(1)
- for {
- a2, b2, c2, d2 := cpuidex(uint32(i), ex)
- if a2 == a && b2 == b && d2 == d || ex > 50 || a2 == 0 {
- break
- }
- t.Logf("CPUID %08x: %08x-%08x-%08x-%08x", i, a2, b2, c2, d2)
- a, b, c, d = a2, b2, c2, d2
- ex++
- }
- }
- n2 := maxExtendedFunction()
- for i := uint32(0x80000000); i <= n2; i++ {
- a, b, c, d := cpuid(i)
- t.Logf("CPUID %08x: %08x-%08x-%08x-%08x", i, a, b, c, d)
- }
- }
- func Example() {
-
- 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)
- fmt.Println("Features:", CPU.Features)
- fmt.Println("Cacheline bytes:", CPU.CacheLine)
-
- if CPU.SSE() {
- fmt.Println("We have Streaming SIMD Extensions")
- }
- }
- func TestBrandNameZero(t *testing.T) {
- if len(CPU.BrandName) > 0 {
-
- last := []byte(CPU.BrandName[len(CPU.BrandName)-1:])
- if last[0] == 0 {
- t.Fatal("last byte was zero")
- } else if last[0] == 32 {
- t.Fatal("whitespace wasn't trimmed")
- }
- }
- }
- func TestCmov(t *testing.T) {
- got := CPU.Cmov()
- expected := CPU.Features&CMOV == CMOV
- if got != expected {
- t.Fatalf("Cmov: expected %v, got %v", expected, got)
- }
- t.Log("CMOV Support:", got)
- }
- func TestAmd3dnow(t *testing.T) {
- got := CPU.Amd3dnow()
- expected := CPU.Features&AMD3DNOW == AMD3DNOW
- if got != expected {
- t.Fatalf("Amd3dnow: expected %v, got %v", expected, got)
- }
- t.Log("AMD3DNOW Support:", got)
- }
- func TestAmd3dnowExt(t *testing.T) {
- got := CPU.Amd3dnowExt()
- expected := CPU.Features&AMD3DNOWEXT == AMD3DNOWEXT
- if got != expected {
- t.Fatalf("Amd3dnowExt: expected %v, got %v", expected, got)
- }
- t.Log("AMD3DNOWEXT Support:", got)
- }
- func TestMMX(t *testing.T) {
- got := CPU.MMX()
- expected := CPU.Features&MMX == MMX
- if got != expected {
- t.Fatalf("MMX: expected %v, got %v", expected, got)
- }
- t.Log("MMX Support:", got)
- }
- func TestMMXext(t *testing.T) {
- got := CPU.MMXExt()
- expected := CPU.Features&MMXEXT == MMXEXT
- if got != expected {
- t.Fatalf("MMXExt: expected %v, got %v", expected, got)
- }
- t.Log("MMXEXT Support:", got)
- }
- func TestSSE(t *testing.T) {
- got := CPU.SSE()
- expected := CPU.Features&SSE == SSE
- if got != expected {
- t.Fatalf("SSE: expected %v, got %v", expected, got)
- }
- t.Log("SSE Support:", got)
- }
- func TestSSE2(t *testing.T) {
- got := CPU.SSE2()
- expected := CPU.Features&SSE2 == SSE2
- if got != expected {
- t.Fatalf("SSE2: expected %v, got %v", expected, got)
- }
- t.Log("SSE2 Support:", got)
- }
- func TestSSE3(t *testing.T) {
- got := CPU.SSE3()
- expected := CPU.Features&SSE3 == SSE3
- if got != expected {
- t.Fatalf("SSE3: expected %v, got %v", expected, got)
- }
- t.Log("SSE3 Support:", got)
- }
- func TestSSSE3(t *testing.T) {
- got := CPU.SSSE3()
- expected := CPU.Features&SSSE3 == SSSE3
- if got != expected {
- t.Fatalf("SSSE3: expected %v, got %v", expected, got)
- }
- t.Log("SSSE3 Support:", got)
- }
- func TestSSE4(t *testing.T) {
- got := CPU.SSE4()
- expected := CPU.Features&SSE4 == SSE4
- if got != expected {
- t.Fatalf("SSE4: expected %v, got %v", expected, got)
- }
- t.Log("SSE4 Support:", got)
- }
- func TestSSE42(t *testing.T) {
- got := CPU.SSE42()
- expected := CPU.Features&SSE42 == SSE42
- if got != expected {
- t.Fatalf("SSE42: expected %v, got %v", expected, got)
- }
- t.Log("SSE42 Support:", got)
- }
- func TestAVX(t *testing.T) {
- got := CPU.AVX()
- expected := CPU.Features&AVX == AVX
- if got != expected {
- t.Fatalf("AVX: expected %v, got %v", expected, got)
- }
- t.Log("AVX Support:", got)
- }
- func TestAVX2(t *testing.T) {
- got := CPU.AVX2()
- expected := CPU.Features&AVX2 == AVX2
- if got != expected {
- t.Fatalf("AVX2: expected %v, got %v", expected, got)
- }
- t.Log("AVX2 Support:", got)
- }
- func TestFMA3(t *testing.T) {
- got := CPU.FMA3()
- expected := CPU.Features&FMA3 == FMA3
- if got != expected {
- t.Fatalf("FMA3: expected %v, got %v", expected, got)
- }
- t.Log("FMA3 Support:", got)
- }
- func TestFMA4(t *testing.T) {
- got := CPU.FMA4()
- expected := CPU.Features&FMA4 == FMA4
- if got != expected {
- t.Fatalf("FMA4: expected %v, got %v", expected, got)
- }
- t.Log("FMA4 Support:", got)
- }
- func TestXOP(t *testing.T) {
- got := CPU.XOP()
- expected := CPU.Features&XOP == XOP
- if got != expected {
- t.Fatalf("XOP: expected %v, got %v", expected, got)
- }
- t.Log("XOP Support:", got)
- }
- func TestF16C(t *testing.T) {
- got := CPU.F16C()
- expected := CPU.Features&F16C == F16C
- if got != expected {
- t.Fatalf("F16C: expected %v, got %v", expected, got)
- }
- t.Log("F16C Support:", got)
- }
- func TestCX16(t *testing.T) {
- got := CPU.CX16()
- expected := CPU.Features&CX16 == CX16
- if got != expected {
- t.Fatalf("CX16: expected %v, got %v", expected, got)
- }
- t.Log("CX16 Support:", got)
- }
- func TestSGX(t *testing.T) {
- got := CPU.SGX.Available
- expected := CPU.Features&SGX == SGX
- if got != expected {
- t.Fatalf("SGX: expected %v, got %v", expected, got)
- }
- t.Log("SGX Support:", got)
- }
- func TestBMI1(t *testing.T) {
- got := CPU.BMI1()
- expected := CPU.Features&BMI1 == BMI1
- if got != expected {
- t.Fatalf("BMI1: expected %v, got %v", expected, got)
- }
- t.Log("BMI1 Support:", got)
- }
- func TestBMI2(t *testing.T) {
- got := CPU.BMI2()
- expected := CPU.Features&BMI2 == BMI2
- if got != expected {
- t.Fatalf("BMI2: expected %v, got %v", expected, got)
- }
- t.Log("BMI2 Support:", got)
- }
- func TestTBM(t *testing.T) {
- got := CPU.TBM()
- expected := CPU.Features&TBM == TBM
- if got != expected {
- t.Fatalf("TBM: expected %v, got %v", expected, got)
- }
- t.Log("TBM Support:", got)
- }
- func TestLzcnt(t *testing.T) {
- got := CPU.Lzcnt()
- expected := CPU.Features&LZCNT == LZCNT
- if got != expected {
- t.Fatalf("Lzcnt: expected %v, got %v", expected, got)
- }
- t.Log("LZCNT Support:", got)
- }
- func TestPopcnt(t *testing.T) {
- got := CPU.Popcnt()
- expected := CPU.Features&POPCNT == POPCNT
- if got != expected {
- t.Fatalf("Popcnt: expected %v, got %v", expected, got)
- }
- t.Log("POPCNT Support:", got)
- }
- func TestAesNi(t *testing.T) {
- got := CPU.AesNi()
- expected := CPU.Features&AESNI == AESNI
- if got != expected {
- t.Fatalf("AesNi: expected %v, got %v", expected, got)
- }
- t.Log("AESNI Support:", got)
- }
- func TestHTT(t *testing.T) {
- got := CPU.HTT()
- expected := CPU.Features&HTT == HTT
- if got != expected {
- t.Fatalf("HTT: expected %v, got %v", expected, got)
- }
- t.Log("HTT Support:", got)
- }
- func TestClmul(t *testing.T) {
- got := CPU.Clmul()
- expected := CPU.Features&CLMUL == CLMUL
- if got != expected {
- t.Fatalf("Clmul: expected %v, got %v", expected, got)
- }
- t.Log("CLMUL Support:", got)
- }
- func TestSSE2Slow(t *testing.T) {
- got := CPU.SSE2Slow()
- expected := CPU.Features&SSE2SLOW == SSE2SLOW
- if got != expected {
- t.Fatalf("SSE2Slow: expected %v, got %v", expected, got)
- }
- t.Log("SSE2SLOW Support:", got)
- }
- func TestSSE3Slow(t *testing.T) {
- got := CPU.SSE3Slow()
- expected := CPU.Features&SSE3SLOW == SSE3SLOW
- if got != expected {
- t.Fatalf("SSE3slow: expected %v, got %v", expected, got)
- }
- t.Log("SSE3SLOW Support:", got)
- }
- func TestAtom(t *testing.T) {
- got := CPU.Atom()
- expected := CPU.Features&ATOM == ATOM
- if got != expected {
- t.Fatalf("Atom: expected %v, got %v", expected, got)
- }
- t.Log("ATOM Support:", got)
- }
- func TestNX(t *testing.T) {
- got := CPU.NX()
- expected := CPU.Features&NX == NX
- if got != expected {
- t.Fatalf("NX: expected %v, got %v", expected, got)
- }
- t.Log("NX Support:", got)
- }
- func TestSSE4A(t *testing.T) {
- got := CPU.SSE4A()
- expected := CPU.Features&SSE4A == SSE4A
- if got != expected {
- t.Fatalf("SSE4A: expected %v, got %v", expected, got)
- }
- t.Log("SSE4A Support:", got)
- }
- func TestHLE(t *testing.T) {
- got := CPU.HLE()
- expected := CPU.Features&HLE == HLE
- if got != expected {
- t.Fatalf("HLE: expected %v, got %v", expected, got)
- }
- t.Log("HLE Support:", got)
- }
- func TestRTM(t *testing.T) {
- got := CPU.RTM()
- expected := CPU.Features&RTM == RTM
- if got != expected {
- t.Fatalf("RTM: expected %v, got %v", expected, got)
- }
- t.Log("RTM Support:", got)
- }
- func TestRdrand(t *testing.T) {
- got := CPU.Rdrand()
- expected := CPU.Features&RDRAND == RDRAND
- if got != expected {
- t.Fatalf("Rdrand: expected %v, got %v", expected, got)
- }
- t.Log("Rdrand Support:", got)
- }
- func TestRdseed(t *testing.T) {
- got := CPU.Rdseed()
- expected := CPU.Features&RDSEED == RDSEED
- if got != expected {
- t.Fatalf("Rdseed: expected %v, got %v", expected, got)
- }
- t.Log("Rdseed Support:", got)
- }
- func TestADX(t *testing.T) {
- got := CPU.ADX()
- expected := CPU.Features&ADX == ADX
- if got != expected {
- t.Fatalf("ADX: expected %v, got %v", expected, got)
- }
- t.Log("ADX Support:", got)
- }
- func TestSHA(t *testing.T) {
- got := CPU.SHA()
- expected := CPU.Features&SHA == SHA
- if got != expected {
- t.Fatalf("SHA: expected %v, got %v", expected, got)
- }
- t.Log("SHA Support:", got)
- }
- func TestAVX512F(t *testing.T) {
- got := CPU.AVX512F()
- expected := CPU.Features&AVX512F == AVX512F
- if got != expected {
- t.Fatalf("AVX512F: expected %v, got %v", expected, got)
- }
- t.Log("AVX512F Support:", got)
- }
- func TestAVX512DQ(t *testing.T) {
- got := CPU.AVX512DQ()
- expected := CPU.Features&AVX512DQ == AVX512DQ
- if got != expected {
- t.Fatalf("AVX512DQ: expected %v, got %v", expected, got)
- }
- t.Log("AVX512DQ Support:", got)
- }
- func TestAVX512IFMA(t *testing.T) {
- got := CPU.AVX512IFMA()
- expected := CPU.Features&AVX512IFMA == AVX512IFMA
- if got != expected {
- t.Fatalf("AVX512IFMA: expected %v, got %v", expected, got)
- }
- t.Log("AVX512IFMA Support:", got)
- }
- func TestAVX512PF(t *testing.T) {
- got := CPU.AVX512PF()
- expected := CPU.Features&AVX512PF == AVX512PF
- if got != expected {
- t.Fatalf("AVX512PF: expected %v, got %v", expected, got)
- }
- t.Log("AVX512PF Support:", got)
- }
- func TestAVX512ER(t *testing.T) {
- got := CPU.AVX512ER()
- expected := CPU.Features&AVX512ER == AVX512ER
- if got != expected {
- t.Fatalf("AVX512ER: expected %v, got %v", expected, got)
- }
- t.Log("AVX512ER Support:", got)
- }
- func TestAVX512CD(t *testing.T) {
- got := CPU.AVX512CD()
- expected := CPU.Features&AVX512CD == AVX512CD
- if got != expected {
- t.Fatalf("AVX512CD: expected %v, got %v", expected, got)
- }
- t.Log("AVX512CD Support:", got)
- }
- func TestAVX512BW(t *testing.T) {
- got := CPU.AVX512BW()
- expected := CPU.Features&AVX512BW == AVX512BW
- if got != expected {
- t.Fatalf("AVX512BW: expected %v, got %v", expected, got)
- }
- t.Log("AVX512BW Support:", got)
- }
- func TestAVX512VL(t *testing.T) {
- got := CPU.AVX512VL()
- expected := CPU.Features&AVX512VL == AVX512VL
- if got != expected {
- t.Fatalf("AVX512VL: expected %v, got %v", expected, got)
- }
- t.Log("AVX512VL Support:", got)
- }
- func TestAVX512VBMI(t *testing.T) {
- got := CPU.AVX512VBMI()
- expected := CPU.Features&AVX512VBMI == AVX512VBMI
- if got != expected {
- t.Fatalf("AVX512VBMI: expected %v, got %v", expected, got)
- }
- t.Log("AVX512VBMI Support:", got)
- }
- func TestMPX(t *testing.T) {
- got := CPU.MPX()
- expected := CPU.Features&MPX == MPX
- if got != expected {
- t.Fatalf("MPX: expected %v, got %v", expected, got)
- }
- t.Log("MPX Support:", got)
- }
- func TestERMS(t *testing.T) {
- got := CPU.ERMS()
- expected := CPU.Features&ERMS == ERMS
- if got != expected {
- t.Fatalf("ERMS: expected %v, got %v", expected, got)
- }
- t.Log("ERMS Support:", got)
- }
- func TestVendor(t *testing.T) {
- t.Log("Vendor ID:", CPU.VendorID)
- }
- func TestIntel(t *testing.T) {
- got := CPU.Intel()
- expected := CPU.VendorID == Intel
- if got != expected {
- t.Fatalf("TestIntel: expected %v, got %v", expected, got)
- }
- t.Log("TestIntel:", got)
- }
- func TestAMD(t *testing.T) {
- got := CPU.AMD()
- expected := CPU.VendorID == AMD
- if got != expected {
- t.Fatalf("TestAMD: expected %v, got %v", expected, got)
- }
- t.Log("TestAMD:", got)
- }
- func TestTransmeta(t *testing.T) {
- got := CPU.Transmeta()
- expected := CPU.VendorID == Transmeta
- if got != expected {
- t.Fatalf("TestTransmeta: expected %v, got %v", expected, got)
- }
- t.Log("TestTransmeta:", got)
- }
- func TestNSC(t *testing.T) {
- got := CPU.NSC()
- expected := CPU.VendorID == NSC
- if got != expected {
- t.Fatalf("TestNSC: expected %v, got %v", expected, got)
- }
- t.Log("TestNSC:", got)
- }
- func TestVIA(t *testing.T) {
- got := CPU.VIA()
- expected := CPU.VendorID == VIA
- if got != expected {
- t.Fatalf("TestVIA: expected %v, got %v", expected, got)
- }
- t.Log("TestVIA:", got)
- }
- func TestVM(t *testing.T) {
- t.Log("Vendor ID:", CPU.VM())
- }
- func TestRtCounter(t *testing.T) {
- a := CPU.RTCounter()
- b := CPU.RTCounter()
- t.Log("CPU Counter:", a, b, b-a)
- }
- func TestIa32TscAux(t *testing.T) {
- ecx := CPU.Ia32TscAux()
- t.Logf("Ia32TscAux:0x%x\n", ecx)
- if ecx != 0 {
- chip := (ecx & 0xFFF000) >> 12
- core := ecx & 0xFFF
- t.Log("Likely chip, core:", chip, core)
- }
- }
- func TestThreadsPerCoreNZ(t *testing.T) {
- if CPU.ThreadsPerCore == 0 {
- t.Fatal("threads per core is zero")
- }
- }
- func TestLogicalCPU(t *testing.T) {
- t.Log("Currently executing on cpu:", CPU.LogicalCPU())
- }
- func TestMaxFunction(t *testing.T) {
- expect := maxFunctionID()
- if CPU.maxFunc != expect {
- t.Fatal("Max function does not match, expected", expect, "but got", CPU.maxFunc)
- }
- expect = maxExtendedFunction()
- if CPU.maxExFunc != expect {
- t.Fatal("Max Extended function does not match, expected", expect, "but got", CPU.maxFunc)
- }
- }
- func ExampleCPUInfo_Ia32TscAux(t *testing.T) {
- ecx := CPU.Ia32TscAux()
- if ecx == 0 {
- fmt.Println("Unknown CPU ID")
- return
- }
- chip := (ecx & 0xFFF000) >> 12
- core := ecx & 0xFFF
- fmt.Println("Chip, Core:", chip, core)
- }
|