Skip to content

Commit ae7887d

Browse files
authored
Add Explicit TSX support (#16)
* Add Explicit TSX support See #15 Also updates test set and updates private package. * Remove Go 1.3 and 1.4 checks.
1 parent 09cded8 commit ae7887d

7 files changed

Lines changed: 236 additions & 144 deletions

File tree

.travis.yml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
language: go
22

3+
sudo: false
4+
5+
os:
6+
- linux
7+
- osx
38
go:
4-
- 1.3
5-
- 1.4
6-
- 1.5
7-
- 1.6
8-
- tip
9+
- 1.5.x
10+
- 1.6.x
11+
- 1.7.x
12+
- 1.8.x
13+
- master
14+
15+
script:
16+
- go vet ./...
17+
- go test -v ./...
18+
- go test -race ./...
19+
- diff <(gofmt -d .) <("")
20+
21+
matrix:
22+
allow_failures:
23+
- go: 'master'
24+
fast_finish: true

cpuid.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func Detect() {
192192
CPU.CacheLine = cacheLine()
193193
CPU.Family, CPU.Model = familyModel()
194194
CPU.Features = support()
195-
CPU.SGX = sgx(CPU.Features&SGX != 0)
195+
CPU.SGX = hasSGX(CPU.Features&SGX != 0)
196196
CPU.ThreadsPerCore = threadsPerCore()
197197
CPU.LogicalCores = logicalCores()
198198
CPU.PhysicalCores = physicalCores()
@@ -437,14 +437,22 @@ func (c CPUInfo) ERMS() bool {
437437
return c.Features&ERMS != 0
438438
}
439439

440+
// RDTSCP Instruction is available.
440441
func (c CPUInfo) RDTSCP() bool {
441442
return c.Features&RDTSCP != 0
442443
}
443444

445+
// CX16 indicates if CMPXCHG16B instruction is available.
444446
func (c CPUInfo) CX16() bool {
445447
return c.Features&CX16 != 0
446448
}
447449

450+
// TSX is split into HLE (Hardware Lock Elision) and RTM (Restricted Transactional Memory) detection.
451+
// So TSX simply checks that.
452+
func (c CPUInfo) TSX() bool {
453+
return c.Features&(MPX|RTM) == MPX|RTM
454+
}
455+
448456
// Atom indicates an Atom processor
449457
func (c CPUInfo) Atom() bool {
450458
return c.Features&ATOM != 0
@@ -757,7 +765,7 @@ type SGXSupport struct {
757765
MaxEnclaveSize64 int64
758766
}
759767

760-
func sgx(available bool) (rval SGXSupport) {
768+
func hasSGX(available bool) (rval SGXSupport) {
761769
rval.Available = available
762770

763771
if !available {

cpuid_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,16 @@ func TestVM(t *testing.T) {
634634
t.Log("Vendor ID:", CPU.VM())
635635
}
636636

637+
// NSC returns true if vendor is recognized as National Semiconductor
638+
func TestCPUInfo_TSX(t *testing.T) {
639+
got := CPU.TSX()
640+
expected := CPU.HLE() && CPU.RTM()
641+
if got != expected {
642+
t.Fatalf("TestNSC: expected %v, got %v", expected, got)
643+
}
644+
t.Log("TestNSC:", got)
645+
}
646+
637647
// Test RTCounter function
638648
func TestRtCounter(t *testing.T) {
639649
a := CPU.RTCounter()
@@ -676,7 +686,7 @@ func TestMaxFunction(t *testing.T) {
676686

677687
// This example will calculate the chip/core number on Linux
678688
// Linux encodes numa id (<<12) and core id (8bit) into TSC_AUX.
679-
func ExampleCPUInfo_Ia32TscAux(t *testing.T) {
689+
func ExampleCPUInfo_Ia32TscAux() {
680690
ecx := CPU.Ia32TscAux()
681691
if ecx == 0 {
682692
fmt.Println("Unknown CPU ID")

generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package cpuid
22

33
//go:generate go run private-gen.go
4+
//go:generate gofmt -w ./private

0 commit comments

Comments
 (0)