1
0

cpuid_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // Copyright (c) 2015 Klaus Post, released under MIT License. See LICENSE file.
  2. package cpuid
  3. import (
  4. "fmt"
  5. "testing"
  6. )
  7. // There is no real way to test a CPU identifier, since results will
  8. // obviously differ on each machine.
  9. func TestCPUID(t *testing.T) {
  10. n := maxFunctionID()
  11. t.Logf("Max Function:0x%x\n", n)
  12. n = maxExtendedFunction()
  13. t.Logf("Max Extended Function:0x%x\n", n)
  14. t.Log("Name:", CPU.BrandName)
  15. t.Log("PhysicalCores:", CPU.PhysicalCores)
  16. t.Log("ThreadsPerCore:", CPU.ThreadsPerCore)
  17. t.Log("LogicalCores:", CPU.LogicalCores)
  18. t.Log("Family", CPU.Family, "Model:", CPU.Model)
  19. t.Log("Features:", CPU.Features)
  20. t.Log("Cacheline bytes:", CPU.CacheLine)
  21. t.Log("L1 Instruction Cache:", CPU.Cache.L1I, "bytes")
  22. t.Log("L1 Data Cache:", CPU.Cache.L1D, "bytes")
  23. t.Log("L2 Cache:", CPU.Cache.L2, "bytes")
  24. t.Log("L3 Cache:", CPU.Cache.L3, "bytes")
  25. if CPU.SSE2() {
  26. t.Log("We have SSE2")
  27. }
  28. }
  29. func TestDumpCPUID(t *testing.T) {
  30. n := int(maxFunctionID())
  31. for i := 0; i <= n; i++ {
  32. a, b, c, d := cpuidex(uint32(i), 0)
  33. t.Logf("CPUID %08x: %08x-%08x-%08x-%08x", i, a, b, c, d)
  34. ex := uint32(1)
  35. for {
  36. a2, b2, c2, d2 := cpuidex(uint32(i), ex)
  37. if a2 == a && b2 == b && d2 == d || ex > 50 || a2 == 0 {
  38. break
  39. }
  40. t.Logf("CPUID %08x: %08x-%08x-%08x-%08x", i, a2, b2, c2, d2)
  41. a, b, c, d = a2, b2, c2, d2
  42. ex++
  43. }
  44. }
  45. n2 := maxExtendedFunction()
  46. for i := uint32(0x80000000); i <= n2; i++ {
  47. a, b, c, d := cpuid(i)
  48. t.Logf("CPUID %08x: %08x-%08x-%08x-%08x", i, a, b, c, d)
  49. }
  50. }
  51. func Example() {
  52. // Print basic CPU information:
  53. fmt.Println("Name:", CPU.BrandName)
  54. fmt.Println("PhysicalCores:", CPU.PhysicalCores)
  55. fmt.Println("ThreadsPerCore:", CPU.ThreadsPerCore)
  56. fmt.Println("LogicalCores:", CPU.LogicalCores)
  57. fmt.Println("Family", CPU.Family, "Model:", CPU.Model)
  58. fmt.Println("Features:", CPU.Features)
  59. fmt.Println("Cacheline bytes:", CPU.CacheLine)
  60. // Test if we have a specific feature:
  61. if CPU.SSE() {
  62. fmt.Println("We have Streaming SIMD Extensions")
  63. }
  64. }
  65. func TestBrandNameZero(t *testing.T) {
  66. if len(CPU.BrandName) > 0 {
  67. // Cut out last byte
  68. last := []byte(CPU.BrandName[len(CPU.BrandName)-1:])
  69. if last[0] == 0 {
  70. t.Fatal("last byte was zero")
  71. } else if last[0] == 32 {
  72. t.Fatal("whitespace wasn't trimmed")
  73. }
  74. }
  75. }
  76. // Generated here: http://play.golang.org/p/mko-0tFt0Q
  77. // TestCmov tests Cmov() function
  78. func TestCmov(t *testing.T) {
  79. got := CPU.Cmov()
  80. expected := CPU.Features&CMOV == CMOV
  81. if got != expected {
  82. t.Fatalf("Cmov: expected %v, got %v", expected, got)
  83. }
  84. t.Log("CMOV Support:", got)
  85. }
  86. // TestAmd3dnow tests Amd3dnow() function
  87. func TestAmd3dnow(t *testing.T) {
  88. got := CPU.Amd3dnow()
  89. expected := CPU.Features&AMD3DNOW == AMD3DNOW
  90. if got != expected {
  91. t.Fatalf("Amd3dnow: expected %v, got %v", expected, got)
  92. }
  93. t.Log("AMD3DNOW Support:", got)
  94. }
  95. // TestAmd3dnowExt tests Amd3dnowExt() function
  96. func TestAmd3dnowExt(t *testing.T) {
  97. got := CPU.Amd3dnowExt()
  98. expected := CPU.Features&AMD3DNOWEXT == AMD3DNOWEXT
  99. if got != expected {
  100. t.Fatalf("Amd3dnowExt: expected %v, got %v", expected, got)
  101. }
  102. t.Log("AMD3DNOWEXT Support:", got)
  103. }
  104. // TestMMX tests MMX() function
  105. func TestMMX(t *testing.T) {
  106. got := CPU.MMX()
  107. expected := CPU.Features&MMX == MMX
  108. if got != expected {
  109. t.Fatalf("MMX: expected %v, got %v", expected, got)
  110. }
  111. t.Log("MMX Support:", got)
  112. }
  113. // TestMMXext tests MMXext() function
  114. func TestMMXext(t *testing.T) {
  115. got := CPU.MMXExt()
  116. expected := CPU.Features&MMXEXT == MMXEXT
  117. if got != expected {
  118. t.Fatalf("MMXExt: expected %v, got %v", expected, got)
  119. }
  120. t.Log("MMXEXT Support:", got)
  121. }
  122. // TestSSE tests SSE() function
  123. func TestSSE(t *testing.T) {
  124. got := CPU.SSE()
  125. expected := CPU.Features&SSE == SSE
  126. if got != expected {
  127. t.Fatalf("SSE: expected %v, got %v", expected, got)
  128. }
  129. t.Log("SSE Support:", got)
  130. }
  131. // TestSSE2 tests SSE2() function
  132. func TestSSE2(t *testing.T) {
  133. got := CPU.SSE2()
  134. expected := CPU.Features&SSE2 == SSE2
  135. if got != expected {
  136. t.Fatalf("SSE2: expected %v, got %v", expected, got)
  137. }
  138. t.Log("SSE2 Support:", got)
  139. }
  140. // TestSSE3 tests SSE3() function
  141. func TestSSE3(t *testing.T) {
  142. got := CPU.SSE3()
  143. expected := CPU.Features&SSE3 == SSE3
  144. if got != expected {
  145. t.Fatalf("SSE3: expected %v, got %v", expected, got)
  146. }
  147. t.Log("SSE3 Support:", got)
  148. }
  149. // TestSSSE3 tests SSSE3() function
  150. func TestSSSE3(t *testing.T) {
  151. got := CPU.SSSE3()
  152. expected := CPU.Features&SSSE3 == SSSE3
  153. if got != expected {
  154. t.Fatalf("SSSE3: expected %v, got %v", expected, got)
  155. }
  156. t.Log("SSSE3 Support:", got)
  157. }
  158. // TestSSE4 tests SSE4() function
  159. func TestSSE4(t *testing.T) {
  160. got := CPU.SSE4()
  161. expected := CPU.Features&SSE4 == SSE4
  162. if got != expected {
  163. t.Fatalf("SSE4: expected %v, got %v", expected, got)
  164. }
  165. t.Log("SSE4 Support:", got)
  166. }
  167. // TestSSE42 tests SSE42() function
  168. func TestSSE42(t *testing.T) {
  169. got := CPU.SSE42()
  170. expected := CPU.Features&SSE42 == SSE42
  171. if got != expected {
  172. t.Fatalf("SSE42: expected %v, got %v", expected, got)
  173. }
  174. t.Log("SSE42 Support:", got)
  175. }
  176. // TestAVX tests AVX() function
  177. func TestAVX(t *testing.T) {
  178. got := CPU.AVX()
  179. expected := CPU.Features&AVX == AVX
  180. if got != expected {
  181. t.Fatalf("AVX: expected %v, got %v", expected, got)
  182. }
  183. t.Log("AVX Support:", got)
  184. }
  185. // TestAVX2 tests AVX2() function
  186. func TestAVX2(t *testing.T) {
  187. got := CPU.AVX2()
  188. expected := CPU.Features&AVX2 == AVX2
  189. if got != expected {
  190. t.Fatalf("AVX2: expected %v, got %v", expected, got)
  191. }
  192. t.Log("AVX2 Support:", got)
  193. }
  194. // TestFMA3 tests FMA3() function
  195. func TestFMA3(t *testing.T) {
  196. got := CPU.FMA3()
  197. expected := CPU.Features&FMA3 == FMA3
  198. if got != expected {
  199. t.Fatalf("FMA3: expected %v, got %v", expected, got)
  200. }
  201. t.Log("FMA3 Support:", got)
  202. }
  203. // TestFMA4 tests FMA4() function
  204. func TestFMA4(t *testing.T) {
  205. got := CPU.FMA4()
  206. expected := CPU.Features&FMA4 == FMA4
  207. if got != expected {
  208. t.Fatalf("FMA4: expected %v, got %v", expected, got)
  209. }
  210. t.Log("FMA4 Support:", got)
  211. }
  212. // TestXOP tests XOP() function
  213. func TestXOP(t *testing.T) {
  214. got := CPU.XOP()
  215. expected := CPU.Features&XOP == XOP
  216. if got != expected {
  217. t.Fatalf("XOP: expected %v, got %v", expected, got)
  218. }
  219. t.Log("XOP Support:", got)
  220. }
  221. // TestF16C tests F16C() function
  222. func TestF16C(t *testing.T) {
  223. got := CPU.F16C()
  224. expected := CPU.Features&F16C == F16C
  225. if got != expected {
  226. t.Fatalf("F16C: expected %v, got %v", expected, got)
  227. }
  228. t.Log("F16C Support:", got)
  229. }
  230. // TestCX16 tests CX16() function
  231. func TestCX16(t *testing.T) {
  232. got := CPU.CX16()
  233. expected := CPU.Features&CX16 == CX16
  234. if got != expected {
  235. t.Fatalf("CX16: expected %v, got %v", expected, got)
  236. }
  237. t.Log("CX16 Support:", got)
  238. }
  239. // TestSGX tests SGX() function
  240. func TestSGX(t *testing.T) {
  241. got := CPU.SGX.Available
  242. expected := CPU.Features&SGX == SGX
  243. if got != expected {
  244. t.Fatalf("SGX: expected %v, got %v", expected, got)
  245. }
  246. t.Log("SGX Support:", got)
  247. }
  248. // TestBMI1 tests BMI1() function
  249. func TestBMI1(t *testing.T) {
  250. got := CPU.BMI1()
  251. expected := CPU.Features&BMI1 == BMI1
  252. if got != expected {
  253. t.Fatalf("BMI1: expected %v, got %v", expected, got)
  254. }
  255. t.Log("BMI1 Support:", got)
  256. }
  257. // TestBMI2 tests BMI2() function
  258. func TestBMI2(t *testing.T) {
  259. got := CPU.BMI2()
  260. expected := CPU.Features&BMI2 == BMI2
  261. if got != expected {
  262. t.Fatalf("BMI2: expected %v, got %v", expected, got)
  263. }
  264. t.Log("BMI2 Support:", got)
  265. }
  266. // TestTBM tests TBM() function
  267. func TestTBM(t *testing.T) {
  268. got := CPU.TBM()
  269. expected := CPU.Features&TBM == TBM
  270. if got != expected {
  271. t.Fatalf("TBM: expected %v, got %v", expected, got)
  272. }
  273. t.Log("TBM Support:", got)
  274. }
  275. // TestLzcnt tests Lzcnt() function
  276. func TestLzcnt(t *testing.T) {
  277. got := CPU.Lzcnt()
  278. expected := CPU.Features&LZCNT == LZCNT
  279. if got != expected {
  280. t.Fatalf("Lzcnt: expected %v, got %v", expected, got)
  281. }
  282. t.Log("LZCNT Support:", got)
  283. }
  284. // TestLzcnt tests Lzcnt() function
  285. func TestPopcnt(t *testing.T) {
  286. got := CPU.Popcnt()
  287. expected := CPU.Features&POPCNT == POPCNT
  288. if got != expected {
  289. t.Fatalf("Popcnt: expected %v, got %v", expected, got)
  290. }
  291. t.Log("POPCNT Support:", got)
  292. }
  293. // TestAesNi tests AesNi() function
  294. func TestAesNi(t *testing.T) {
  295. got := CPU.AesNi()
  296. expected := CPU.Features&AESNI == AESNI
  297. if got != expected {
  298. t.Fatalf("AesNi: expected %v, got %v", expected, got)
  299. }
  300. t.Log("AESNI Support:", got)
  301. }
  302. // TestHTT tests HTT() function
  303. func TestHTT(t *testing.T) {
  304. got := CPU.HTT()
  305. expected := CPU.Features&HTT == HTT
  306. if got != expected {
  307. t.Fatalf("HTT: expected %v, got %v", expected, got)
  308. }
  309. t.Log("HTT Support:", got)
  310. }
  311. // TestClmul tests Clmul() function
  312. func TestClmul(t *testing.T) {
  313. got := CPU.Clmul()
  314. expected := CPU.Features&CLMUL == CLMUL
  315. if got != expected {
  316. t.Fatalf("Clmul: expected %v, got %v", expected, got)
  317. }
  318. t.Log("CLMUL Support:", got)
  319. }
  320. // TestSSE2Slow tests SSE2Slow() function
  321. func TestSSE2Slow(t *testing.T) {
  322. got := CPU.SSE2Slow()
  323. expected := CPU.Features&SSE2SLOW == SSE2SLOW
  324. if got != expected {
  325. t.Fatalf("SSE2Slow: expected %v, got %v", expected, got)
  326. }
  327. t.Log("SSE2SLOW Support:", got)
  328. }
  329. // TestSSE3Slow tests SSE3slow() function
  330. func TestSSE3Slow(t *testing.T) {
  331. got := CPU.SSE3Slow()
  332. expected := CPU.Features&SSE3SLOW == SSE3SLOW
  333. if got != expected {
  334. t.Fatalf("SSE3slow: expected %v, got %v", expected, got)
  335. }
  336. t.Log("SSE3SLOW Support:", got)
  337. }
  338. // TestAtom tests Atom() function
  339. func TestAtom(t *testing.T) {
  340. got := CPU.Atom()
  341. expected := CPU.Features&ATOM == ATOM
  342. if got != expected {
  343. t.Fatalf("Atom: expected %v, got %v", expected, got)
  344. }
  345. t.Log("ATOM Support:", got)
  346. }
  347. // TestNX tests NX() function (NX (No-Execute) bit)
  348. func TestNX(t *testing.T) {
  349. got := CPU.NX()
  350. expected := CPU.Features&NX == NX
  351. if got != expected {
  352. t.Fatalf("NX: expected %v, got %v", expected, got)
  353. }
  354. t.Log("NX Support:", got)
  355. }
  356. // TestSSE4A tests SSE4A() function (AMD Barcelona microarchitecture SSE4a instructions)
  357. func TestSSE4A(t *testing.T) {
  358. got := CPU.SSE4A()
  359. expected := CPU.Features&SSE4A == SSE4A
  360. if got != expected {
  361. t.Fatalf("SSE4A: expected %v, got %v", expected, got)
  362. }
  363. t.Log("SSE4A Support:", got)
  364. }
  365. // TestHLE tests HLE() function (Hardware Lock Elision)
  366. func TestHLE(t *testing.T) {
  367. got := CPU.HLE()
  368. expected := CPU.Features&HLE == HLE
  369. if got != expected {
  370. t.Fatalf("HLE: expected %v, got %v", expected, got)
  371. }
  372. t.Log("HLE Support:", got)
  373. }
  374. // TestRTM tests RTM() function (Restricted Transactional Memory)
  375. func TestRTM(t *testing.T) {
  376. got := CPU.RTM()
  377. expected := CPU.Features&RTM == RTM
  378. if got != expected {
  379. t.Fatalf("RTM: expected %v, got %v", expected, got)
  380. }
  381. t.Log("RTM Support:", got)
  382. }
  383. // TestRdrand tests RDRAND() function (RDRAND instruction is available)
  384. func TestRdrand(t *testing.T) {
  385. got := CPU.Rdrand()
  386. expected := CPU.Features&RDRAND == RDRAND
  387. if got != expected {
  388. t.Fatalf("Rdrand: expected %v, got %v", expected, got)
  389. }
  390. t.Log("Rdrand Support:", got)
  391. }
  392. // TestRdseed tests RDSEED() function (RDSEED instruction is available)
  393. func TestRdseed(t *testing.T) {
  394. got := CPU.Rdseed()
  395. expected := CPU.Features&RDSEED == RDSEED
  396. if got != expected {
  397. t.Fatalf("Rdseed: expected %v, got %v", expected, got)
  398. }
  399. t.Log("Rdseed Support:", got)
  400. }
  401. // TestADX tests ADX() function (Intel ADX (Multi-Precision Add-Carry Instruction Extensions))
  402. func TestADX(t *testing.T) {
  403. got := CPU.ADX()
  404. expected := CPU.Features&ADX == ADX
  405. if got != expected {
  406. t.Fatalf("ADX: expected %v, got %v", expected, got)
  407. }
  408. t.Log("ADX Support:", got)
  409. }
  410. // TestSHA tests SHA() function (Intel SHA Extensions)
  411. func TestSHA(t *testing.T) {
  412. got := CPU.SHA()
  413. expected := CPU.Features&SHA == SHA
  414. if got != expected {
  415. t.Fatalf("SHA: expected %v, got %v", expected, got)
  416. }
  417. t.Log("SHA Support:", got)
  418. }
  419. // TestAVX512F tests AVX512F() function (AVX-512 Foundation)
  420. func TestAVX512F(t *testing.T) {
  421. got := CPU.AVX512F()
  422. expected := CPU.Features&AVX512F == AVX512F
  423. if got != expected {
  424. t.Fatalf("AVX512F: expected %v, got %v", expected, got)
  425. }
  426. t.Log("AVX512F Support:", got)
  427. }
  428. // TestAVX512DQ tests AVX512DQ() function (AVX-512 Doubleword and Quadword Instructions)
  429. func TestAVX512DQ(t *testing.T) {
  430. got := CPU.AVX512DQ()
  431. expected := CPU.Features&AVX512DQ == AVX512DQ
  432. if got != expected {
  433. t.Fatalf("AVX512DQ: expected %v, got %v", expected, got)
  434. }
  435. t.Log("AVX512DQ Support:", got)
  436. }
  437. // TestAVX512IFMA tests AVX512IFMA() function (AVX-512 Integer Fused Multiply-Add Instructions)
  438. func TestAVX512IFMA(t *testing.T) {
  439. got := CPU.AVX512IFMA()
  440. expected := CPU.Features&AVX512IFMA == AVX512IFMA
  441. if got != expected {
  442. t.Fatalf("AVX512IFMA: expected %v, got %v", expected, got)
  443. }
  444. t.Log("AVX512IFMA Support:", got)
  445. }
  446. // TestAVX512PF tests AVX512PF() function (AVX-512 Prefetch Instructions)
  447. func TestAVX512PF(t *testing.T) {
  448. got := CPU.AVX512PF()
  449. expected := CPU.Features&AVX512PF == AVX512PF
  450. if got != expected {
  451. t.Fatalf("AVX512PF: expected %v, got %v", expected, got)
  452. }
  453. t.Log("AVX512PF Support:", got)
  454. }
  455. // TestAVX512ER tests AVX512ER() function (AVX-512 Exponential and Reciprocal Instructions)
  456. func TestAVX512ER(t *testing.T) {
  457. got := CPU.AVX512ER()
  458. expected := CPU.Features&AVX512ER == AVX512ER
  459. if got != expected {
  460. t.Fatalf("AVX512ER: expected %v, got %v", expected, got)
  461. }
  462. t.Log("AVX512ER Support:", got)
  463. }
  464. // TestAVX512CD tests AVX512CD() function (AVX-512 Conflict Detection Instructions)
  465. func TestAVX512CD(t *testing.T) {
  466. got := CPU.AVX512CD()
  467. expected := CPU.Features&AVX512CD == AVX512CD
  468. if got != expected {
  469. t.Fatalf("AVX512CD: expected %v, got %v", expected, got)
  470. }
  471. t.Log("AVX512CD Support:", got)
  472. }
  473. // TestAVX512BW tests AVX512BW() function (AVX-512 Byte and Word Instructions)
  474. func TestAVX512BW(t *testing.T) {
  475. got := CPU.AVX512BW()
  476. expected := CPU.Features&AVX512BW == AVX512BW
  477. if got != expected {
  478. t.Fatalf("AVX512BW: expected %v, got %v", expected, got)
  479. }
  480. t.Log("AVX512BW Support:", got)
  481. }
  482. // TestAVX512VL tests AVX512VL() function (AVX-512 Vector Length Extensions)
  483. func TestAVX512VL(t *testing.T) {
  484. got := CPU.AVX512VL()
  485. expected := CPU.Features&AVX512VL == AVX512VL
  486. if got != expected {
  487. t.Fatalf("AVX512VL: expected %v, got %v", expected, got)
  488. }
  489. t.Log("AVX512VL Support:", got)
  490. }
  491. // TestAVX512VL tests AVX512VBMI() function (AVX-512 Vector Bit Manipulation Instructions)
  492. func TestAVX512VBMI(t *testing.T) {
  493. got := CPU.AVX512VBMI()
  494. expected := CPU.Features&AVX512VBMI == AVX512VBMI
  495. if got != expected {
  496. t.Fatalf("AVX512VBMI: expected %v, got %v", expected, got)
  497. }
  498. t.Log("AVX512VBMI Support:", got)
  499. }
  500. // TestMPX tests MPX() function (Intel MPX (Memory Protection Extensions))
  501. func TestMPX(t *testing.T) {
  502. got := CPU.MPX()
  503. expected := CPU.Features&MPX == MPX
  504. if got != expected {
  505. t.Fatalf("MPX: expected %v, got %v", expected, got)
  506. }
  507. t.Log("MPX Support:", got)
  508. }
  509. // TestERMS tests ERMS() function (Enhanced REP MOVSB/STOSB)
  510. func TestERMS(t *testing.T) {
  511. got := CPU.ERMS()
  512. expected := CPU.Features&ERMS == ERMS
  513. if got != expected {
  514. t.Fatalf("ERMS: expected %v, got %v", expected, got)
  515. }
  516. t.Log("ERMS Support:", got)
  517. }
  518. // TestVendor writes the detected vendor. Will be 0 if unknown
  519. func TestVendor(t *testing.T) {
  520. t.Log("Vendor ID:", CPU.VendorID)
  521. }
  522. // Intel returns true if vendor is recognized as Intel
  523. func TestIntel(t *testing.T) {
  524. got := CPU.Intel()
  525. expected := CPU.VendorID == Intel
  526. if got != expected {
  527. t.Fatalf("TestIntel: expected %v, got %v", expected, got)
  528. }
  529. t.Log("TestIntel:", got)
  530. }
  531. // AMD returns true if vendor is recognized as AMD
  532. func TestAMD(t *testing.T) {
  533. got := CPU.AMD()
  534. expected := CPU.VendorID == AMD
  535. if got != expected {
  536. t.Fatalf("TestAMD: expected %v, got %v", expected, got)
  537. }
  538. t.Log("TestAMD:", got)
  539. }
  540. // Transmeta returns true if vendor is recognized as Transmeta
  541. func TestTransmeta(t *testing.T) {
  542. got := CPU.Transmeta()
  543. expected := CPU.VendorID == Transmeta
  544. if got != expected {
  545. t.Fatalf("TestTransmeta: expected %v, got %v", expected, got)
  546. }
  547. t.Log("TestTransmeta:", got)
  548. }
  549. // NSC returns true if vendor is recognized as National Semiconductor
  550. func TestNSC(t *testing.T) {
  551. got := CPU.NSC()
  552. expected := CPU.VendorID == NSC
  553. if got != expected {
  554. t.Fatalf("TestNSC: expected %v, got %v", expected, got)
  555. }
  556. t.Log("TestNSC:", got)
  557. }
  558. // VIA returns true if vendor is recognized as VIA
  559. func TestVIA(t *testing.T) {
  560. got := CPU.VIA()
  561. expected := CPU.VendorID == VIA
  562. if got != expected {
  563. t.Fatalf("TestVIA: expected %v, got %v", expected, got)
  564. }
  565. t.Log("TestVIA:", got)
  566. }
  567. // Test VM function
  568. func TestVM(t *testing.T) {
  569. t.Log("Vendor ID:", CPU.VM())
  570. }
  571. // Test RTCounter function
  572. func TestRtCounter(t *testing.T) {
  573. a := CPU.RTCounter()
  574. b := CPU.RTCounter()
  575. t.Log("CPU Counter:", a, b, b-a)
  576. }
  577. // Prints the value of Ia32TscAux()
  578. func TestIa32TscAux(t *testing.T) {
  579. ecx := CPU.Ia32TscAux()
  580. t.Logf("Ia32TscAux:0x%x\n", ecx)
  581. if ecx != 0 {
  582. chip := (ecx & 0xFFF000) >> 12
  583. core := ecx & 0xFFF
  584. t.Log("Likely chip, core:", chip, core)
  585. }
  586. }
  587. func TestThreadsPerCoreNZ(t *testing.T) {
  588. if CPU.ThreadsPerCore == 0 {
  589. t.Fatal("threads per core is zero")
  590. }
  591. }
  592. // Prints the value of LogicalCPU()
  593. func TestLogicalCPU(t *testing.T) {
  594. t.Log("Currently executing on cpu:", CPU.LogicalCPU())
  595. }
  596. func TestMaxFunction(t *testing.T) {
  597. expect := maxFunctionID()
  598. if CPU.maxFunc != expect {
  599. t.Fatal("Max function does not match, expected", expect, "but got", CPU.maxFunc)
  600. }
  601. expect = maxExtendedFunction()
  602. if CPU.maxExFunc != expect {
  603. t.Fatal("Max Extended function does not match, expected", expect, "but got", CPU.maxFunc)
  604. }
  605. }
  606. // This example will calculate the chip/core number on Linux
  607. // Linux encodes numa id (<<12) and core id (8bit) into TSC_AUX.
  608. func ExampleCPUInfo_Ia32TscAux(t *testing.T) {
  609. ecx := CPU.Ia32TscAux()
  610. if ecx == 0 {
  611. fmt.Println("Unknown CPU ID")
  612. return
  613. }
  614. chip := (ecx & 0xFFF000) >> 12
  615. core := ecx & 0xFFF
  616. fmt.Println("Chip, Core:", chip, core)
  617. }
  618. /*
  619. func TestPhysical(t *testing.T) {
  620. var test16 = "CPUID 00000000: 0000000d-756e6547-6c65746e-49656e69 \nCPUID 00000001: 000206d7-03200800-1fbee3ff-bfebfbff \nCPUID 00000002: 76035a01-00f0b2ff-00000000-00ca0000 \nCPUID 00000003: 00000000-00000000-00000000-00000000 \nCPUID 00000004: 3c004121-01c0003f-0000003f-00000000 \nCPUID 00000004: 3c004122-01c0003f-0000003f-00000000 \nCPUID 00000004: 3c004143-01c0003f-000001ff-00000000 \nCPUID 00000004: 3c07c163-04c0003f-00003fff-00000006 \nCPUID 00000005: 00000040-00000040-00000003-00021120 \nCPUID 00000006: 00000075-00000002-00000009-00000000 \nCPUID 00000007: 00000000-00000000-00000000-00000000 \nCPUID 00000008: 00000000-00000000-00000000-00000000 \nCPUID 00000009: 00000001-00000000-00000000-00000000 \nCPUID 0000000a: 07300403-00000000-00000000-00000603 \nCPUID 0000000b: 00000000-00000000-00000003-00000003 \nCPUID 0000000b: 00000005-00000010-00000201-00000003 \nCPUID 0000000c: 00000000-00000000-00000000-00000000 \nCPUID 0000000d: 00000007-00000340-00000340-00000000 \nCPUID 0000000d: 00000001-00000000-00000000-00000000 \nCPUID 0000000d: 00000100-00000240-00000000-00000000 \nCPUID 80000000: 80000008-00000000-00000000-00000000 \nCPUID 80000001: 00000000-00000000-00000001-2c100800 \nCPUID 80000002: 20202020-49202020-6c65746e-20295228 \nCPUID 80000003: 6e6f6558-20295228-20555043-322d3545 \nCPUID 80000004: 20303636-20402030-30322e32-007a4847 \nCPUID 80000005: 00000000-00000000-00000000-00000000 \nCPUID 80000006: 00000000-00000000-01006040-00000000 \nCPUID 80000007: 00000000-00000000-00000000-00000100 \nCPUID 80000008: 0000302e-00000000-00000000-00000000"
  621. restore := mockCPU([]byte(test16))
  622. Detect()
  623. t.Log("Name:", CPU.BrandName)
  624. n := maxFunctionID()
  625. t.Logf("Max Function:0x%x\n", n)
  626. n = maxExtendedFunction()
  627. t.Logf("Max Extended Function:0x%x\n", n)
  628. t.Log("PhysicalCores:", CPU.PhysicalCores)
  629. t.Log("ThreadsPerCore:", CPU.ThreadsPerCore)
  630. t.Log("LogicalCores:", CPU.LogicalCores)
  631. t.Log("Family", CPU.Family, "Model:", CPU.Model)
  632. t.Log("Features:", CPU.Features)
  633. t.Log("Cacheline bytes:", CPU.CacheLine)
  634. t.Log("L1 Instruction Cache:", CPU.Cache.L1I, "bytes")
  635. t.Log("L1 Data Cache:", CPU.Cache.L1D, "bytes")
  636. t.Log("L2 Cache:", CPU.Cache.L2, "bytes")
  637. t.Log("L3 Cache:", CPU.Cache.L3, "bytes")
  638. if CPU.LogicalCores > 0 && CPU.PhysicalCores > 0 {
  639. if CPU.LogicalCores != CPU.PhysicalCores*CPU.ThreadsPerCore {
  640. t.Fatalf("Core count mismatch, LogicalCores (%d) != PhysicalCores (%d) * CPU.ThreadsPerCore (%d)",
  641. CPU.LogicalCores, CPU.PhysicalCores, CPU.ThreadsPerCore)
  642. }
  643. }
  644. if CPU.ThreadsPerCore > 1 && !CPU.HTT() {
  645. t.Fatalf("Hyperthreading not detected")
  646. }
  647. if CPU.ThreadsPerCore == 1 && CPU.HTT() {
  648. t.Fatalf("Hyperthreading detected, but only 1 Thread per core")
  649. }
  650. restore()
  651. Detect()
  652. TestCPUID(t)
  653. }
  654. */