cpuid_test.go 20 KB

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