Skip to content

Commit e252067

Browse files
authored
Add raw vendor string (#43)
Fixes #42
1 parent 7799af8 commit e252067

5 files changed

Lines changed: 62 additions & 34 deletions

File tree

cpuid.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const (
3434
XenHVM
3535
Bhyve
3636
Hygon
37+
SiS
38+
RDC
3739
)
3840

3941
const (
@@ -177,6 +179,7 @@ var flagNames = map[Flags]string{
177179
type CPUInfo struct {
178180
BrandName string // Brand name reported by the CPU
179181
VendorID Vendor // Comparable CPU vendor ID
182+
VendorString string // Raw vendor string.
180183
Features Flags // Features of the CPU
181184
PhysicalCores int // Number of physical processor cores in your CPU. Will be 0 if undetectable.
182185
ThreadsPerCore int // Number of threads per physical core. Will be 1 if undetectable.
@@ -231,7 +234,7 @@ func Detect() {
231234
CPU.ThreadsPerCore = threadsPerCore()
232235
CPU.LogicalCores = logicalCores()
233236
CPU.PhysicalCores = physicalCores()
234-
CPU.VendorID = vendorID()
237+
CPU.VendorID, CPU.VendorString = vendorID()
235238
CPU.Hz = hertz(CPU.BrandName)
236239
CPU.cacheSize()
237240
}
@@ -726,12 +729,14 @@ func brandName() string {
726729

727730
func threadsPerCore() int {
728731
mfi := maxFunctionID()
729-
if mfi < 0x4 || (vendorID() != Intel && vendorID() != AMD) {
732+
vend, _ := vendorID()
733+
734+
if mfi < 0x4 || (vend != Intel && vend != AMD) {
730735
return 1
731736
}
732737

733738
if mfi < 0xb {
734-
if vendorID() != Intel {
739+
if vend != Intel {
735740
return 1
736741
}
737742
_, b, _, d := cpuid(1)
@@ -758,7 +763,8 @@ func threadsPerCore() int {
758763

759764
func logicalCores() int {
760765
mfi := maxFunctionID()
761-
switch vendorID() {
766+
v, _ := vendorID()
767+
switch v {
762768
case Intel:
763769
// Use this on old Intel processors
764770
if mfi < 0xb {
@@ -793,7 +799,8 @@ func familyModel() (int, int) {
793799
}
794800

795801
func physicalCores() int {
796-
switch vendorID() {
802+
v, _ := vendorID()
803+
switch v {
797804
case Intel:
798805
return logicalCores() / threadsPerCore()
799806
case AMD, Hygon:
@@ -828,16 +835,20 @@ var vendorMapping = map[string]Vendor{
828835
"XenVMMXenVMM": XenHVM,
829836
"bhyve bhyve ": Bhyve,
830837
"HygonGenuine": Hygon,
838+
"Vortex86 SoC": SiS,
839+
"SiS SiS SiS ": SiS,
840+
"RiseRiseRise": SiS,
841+
"Genuine RDC": RDC,
831842
}
832843

833-
func vendorID() Vendor {
844+
func vendorID() (Vendor, string) {
834845
_, b, c, d := cpuid(0)
835-
v := valAsString(b, d, c)
836-
vend, ok := vendorMapping[string(v)]
846+
v := string(valAsString(b, d, c))
847+
vend, ok := vendorMapping[v]
837848
if !ok {
838-
return Other
849+
return Other, v
839850
}
840-
return vend
851+
return vend, v
841852
}
842853

843854
func cacheLine() int {
@@ -860,7 +871,7 @@ func (c *CPUInfo) cacheSize() {
860871
c.Cache.L1I = -1
861872
c.Cache.L2 = -1
862873
c.Cache.L3 = -1
863-
vendor := vendorID()
874+
vendor, _ := vendorID()
864875
switch vendor {
865876
case Intel:
866877
if maxFunctionID() < 4 {
@@ -1015,7 +1026,7 @@ func hasSGX(available, lc bool) (rval SGXSupport) {
10151026

10161027
func support() Flags {
10171028
mfi := maxFunctionID()
1018-
vend := vendorID()
1029+
vend, _ := vendorID()
10191030
if mfi < 0x1 {
10201031
return 0
10211032
}
@@ -1243,7 +1254,7 @@ func support() Flags {
12431254
AV_CPU_FLAG_SSE2 and AV_CPU_FLAG_SSE2SLOW are both set in this case
12441255
so that SSE2 is used unless explicitly disabled by checking
12451256
AV_CPU_FLAG_SSE2SLOW. */
1246-
if vendorID() != Intel &&
1257+
if vend != Intel &&
12471258
rval&SSE2 != 0 && (c&0x00000040) == 0 {
12481259
rval |= SSE2SLOW
12491260
}
@@ -1259,7 +1270,7 @@ func support() Flags {
12591270
}
12601271
}
12611272

1262-
if vendorID() == Intel {
1273+
if vend == Intel {
12631274
family, model := familyModel()
12641275
if family == 6 && (model == 9 || model == 13 || model == 14) {
12651276
/* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and

cpuid_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ import (
1111
// obviously differ on each machine.
1212
func TestCPUID(t *testing.T) {
1313
n := maxFunctionID()
14-
t.Logf("Max Function:0x%x\n", n)
14+
t.Logf("Max Function:0x%x", n)
1515
n = maxExtendedFunction()
16-
t.Logf("Max Extended Function:0x%x\n", n)
16+
t.Logf("Max Extended Function:0x%x", n)
17+
t.Log("VendorString:", CPU.VendorString)
18+
t.Log("VendorID:", CPU.VendorID)
1719
t.Log("Name:", CPU.BrandName)
1820
t.Log("PhysicalCores:", CPU.PhysicalCores)
1921
t.Log("ThreadsPerCore:", CPU.ThreadsPerCore)

mockcpu_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,11 @@ func TestMocks(t *testing.T) {
176176
Detect()
177177
t.Log("Name:", CPU.BrandName)
178178
n := maxFunctionID()
179-
t.Logf("Max Function:0x%x\n", n)
179+
t.Logf("Max Function:0x%x", n)
180180
n = maxExtendedFunction()
181-
t.Logf("Max Extended Function:0x%x\n", n)
181+
t.Logf("Max Extended Function:0x%x", n)
182+
t.Log("VendorString:", CPU.VendorString)
183+
t.Log("VendorID:", CPU.VendorID)
182184
t.Log("PhysicalCores:", CPU.PhysicalCores)
183185
t.Log("ThreadsPerCore:", CPU.ThreadsPerCore)
184186
t.Log("LogicalCores:", CPU.LogicalCores)

private/cpuid.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const (
2828
xenhvm
2929
bhyve
3030
hygon
31+
sis
32+
rdc
3133
)
3234

3335
const (
@@ -171,6 +173,7 @@ var flagNames = map[flags]string{
171173
type cpuInfo struct {
172174
brandname string // Brand name reported by the CPU
173175
vendorid vendor // Comparable CPU vendor ID
176+
vendorstring string // Raw vendor string.
174177
features flags // Features of the CPU
175178
physicalcores int // Number of physical processor cores in your CPU. Will be 0 if undetectable.
176179
threadspercore int // Number of threads per physical core. Will be 1 if undetectable.
@@ -225,7 +228,7 @@ func detect() {
225228
cpu.threadspercore = threadsPerCore()
226229
cpu.logicalcores = logicalCores()
227230
cpu.physicalcores = physicalCores()
228-
cpu.vendorid = vendorID()
231+
cpu.vendorid, cpu.vendorstring = vendorID()
229232
cpu.hz = hertz(cpu.brandname)
230233
cpu.cacheSize()
231234
}
@@ -720,12 +723,14 @@ func brandName() string {
720723

721724
func threadsPerCore() int {
722725
mfi := maxFunctionID()
723-
if mfi < 0x4 || (vendorID() != intel && vendorID() != amd) {
726+
vend, _ := vendorID()
727+
728+
if mfi < 0x4 || (vend != intel && vend != amd) {
724729
return 1
725730
}
726731

727732
if mfi < 0xb {
728-
if vendorID() != intel {
733+
if vend != intel {
729734
return 1
730735
}
731736
_, b, _, d := cpuid(1)
@@ -752,7 +757,8 @@ func threadsPerCore() int {
752757

753758
func logicalCores() int {
754759
mfi := maxFunctionID()
755-
switch vendorID() {
760+
v, _ := vendorID()
761+
switch v {
756762
case intel:
757763
// Use this on old Intel processors
758764
if mfi < 0xb {
@@ -787,7 +793,8 @@ func familyModel() (int, int) {
787793
}
788794

789795
func physicalCores() int {
790-
switch vendorID() {
796+
v, _ := vendorID()
797+
switch v {
791798
case intel:
792799
return logicalCores() / threadsPerCore()
793800
case amd, hygon:
@@ -822,16 +829,20 @@ var vendorMapping = map[string]vendor{
822829
"XenVMMXenVMM": xenhvm,
823830
"bhyve bhyve ": bhyve,
824831
"HygonGenuine": hygon,
832+
"Vortex86 SoC": sis,
833+
"SiS SiS SiS ": sis,
834+
"RiseRiseRise": sis,
835+
"Genuine RDC": rdc,
825836
}
826837

827-
func vendorID() vendor {
838+
func vendorID() (vendor, string) {
828839
_, b, c, d := cpuid(0)
829-
v := valAsString(b, d, c)
830-
vend, ok := vendorMapping[string(v)]
840+
v := string(valAsString(b, d, c))
841+
vend, ok := vendorMapping[v]
831842
if !ok {
832-
return other
843+
return other, v
833844
}
834-
return vend
845+
return vend, v
835846
}
836847

837848
func cacheLine() int {
@@ -854,7 +865,7 @@ func (c *cpuInfo) cacheSize() {
854865
c.cache.l1i = -1
855866
c.cache.l2 = -1
856867
c.cache.l3 = -1
857-
vendor := vendorID()
868+
vendor, _ := vendorID()
858869
switch vendor {
859870
case intel:
860871
if maxFunctionID() < 4 {
@@ -1009,7 +1020,7 @@ func hasSGX(available, lc bool) (rval sgxsupport) {
10091020

10101021
func support() flags {
10111022
mfi := maxFunctionID()
1012-
vend := vendorID()
1023+
vend, _ := vendorID()
10131024
if mfi < 0x1 {
10141025
return 0
10151026
}
@@ -1237,7 +1248,7 @@ func support() flags {
12371248
AV_CPU_FLAG_SSE2 and AV_CPU_FLAG_SSE2SLOW are both set in this case
12381249
so that SSE2 is used unless explicitly disabled by checking
12391250
AV_CPU_FLAG_SSE2SLOW. */
1240-
if vendorID() != intel &&
1251+
if vend != intel &&
12411252
rval&sse2 != 0 && (c&0x00000040) == 0 {
12421253
rval |= sse2slow
12431254
}
@@ -1253,7 +1264,7 @@ func support() flags {
12531264
}
12541265
}
12551266

1256-
if vendorID() == intel {
1267+
if vend == intel {
12571268
family, model := familyModel()
12581269
if family == 6 && (model == 9 || model == 13 || model == 14) {
12591270
/* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and

private/cpuid_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
// obviously differ on each machine.
1414
func TestCPUID(t *testing.T) {
1515
n := maxFunctionID()
16-
t.Logf("Max Function:0x%x\n", n)
16+
t.Logf("Max Function:0x%x", n)
1717
n = maxExtendedFunction()
18-
t.Logf("Max Extended Function:0x%x\n", n)
18+
t.Logf("Max Extended Function:0x%x", n)
19+
t.Log("VendorString:", cpu.vendorstring)
20+
t.Log("VendorID:", cpu.vendorid)
1921
t.Log("Name:", cpu.brandname)
2022
t.Log("PhysicalCores:", cpu.physicalcores)
2123
t.Log("ThreadsPerCore:", cpu.threadspercore)

0 commit comments

Comments
 (0)