Skip to content

Commit

Permalink
Add raw vendor string (#43)
Browse files Browse the repository at this point in the history
Fixes #42
  • Loading branch information
klauspost committed May 26, 2020
1 parent 7799af8 commit e252067
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 34 deletions.
39 changes: 25 additions & 14 deletions cpuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const (
XenHVM
Bhyve
Hygon
SiS
RDC
)

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

func threadsPerCore() int {
mfi := maxFunctionID()
if mfi < 0x4 || (vendorID() != Intel && vendorID() != AMD) {
vend, _ := vendorID()

if mfi < 0x4 || (vend != Intel && vend != AMD) {
return 1
}

if mfi < 0xb {
if vendorID() != Intel {
if vend != Intel {
return 1
}
_, b, _, d := cpuid(1)
Expand All @@ -758,7 +763,8 @@ func threadsPerCore() int {

func logicalCores() int {
mfi := maxFunctionID()
switch vendorID() {
v, _ := vendorID()
switch v {
case Intel:
// Use this on old Intel processors
if mfi < 0xb {
Expand Down Expand Up @@ -793,7 +799,8 @@ func familyModel() (int, int) {
}

func physicalCores() int {
switch vendorID() {
v, _ := vendorID()
switch v {
case Intel:
return logicalCores() / threadsPerCore()
case AMD, Hygon:
Expand Down Expand Up @@ -828,16 +835,20 @@ var vendorMapping = map[string]Vendor{
"XenVMMXenVMM": XenHVM,
"bhyve bhyve ": Bhyve,
"HygonGenuine": Hygon,
"Vortex86 SoC": SiS,
"SiS SiS SiS ": SiS,
"RiseRiseRise": SiS,
"Genuine RDC": RDC,
}

func vendorID() Vendor {
func vendorID() (Vendor, string) {
_, b, c, d := cpuid(0)
v := valAsString(b, d, c)
vend, ok := vendorMapping[string(v)]
v := string(valAsString(b, d, c))
vend, ok := vendorMapping[v]
if !ok {
return Other
return Other, v
}
return vend
return vend, v
}

func cacheLine() int {
Expand All @@ -860,7 +871,7 @@ func (c *CPUInfo) cacheSize() {
c.Cache.L1I = -1
c.Cache.L2 = -1
c.Cache.L3 = -1
vendor := vendorID()
vendor, _ := vendorID()
switch vendor {
case Intel:
if maxFunctionID() < 4 {
Expand Down Expand Up @@ -1015,7 +1026,7 @@ func hasSGX(available, lc bool) (rval SGXSupport) {

func support() Flags {
mfi := maxFunctionID()
vend := vendorID()
vend, _ := vendorID()
if mfi < 0x1 {
return 0
}
Expand Down Expand Up @@ -1243,7 +1254,7 @@ func support() Flags {
AV_CPU_FLAG_SSE2 and AV_CPU_FLAG_SSE2SLOW are both set in this case
so that SSE2 is used unless explicitly disabled by checking
AV_CPU_FLAG_SSE2SLOW. */
if vendorID() != Intel &&
if vend != Intel &&
rval&SSE2 != 0 && (c&0x00000040) == 0 {
rval |= SSE2SLOW
}
Expand All @@ -1259,7 +1270,7 @@ func support() Flags {
}
}

if vendorID() == Intel {
if vend == Intel {
family, model := familyModel()
if family == 6 && (model == 9 || model == 13 || model == 14) {
/* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and
Expand Down
6 changes: 4 additions & 2 deletions cpuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
// obviously differ on each machine.
func TestCPUID(t *testing.T) {
n := maxFunctionID()
t.Logf("Max Function:0x%x\n", n)
t.Logf("Max Function:0x%x", n)
n = maxExtendedFunction()
t.Logf("Max Extended Function:0x%x\n", n)
t.Logf("Max Extended Function:0x%x", n)
t.Log("VendorString:", CPU.VendorString)
t.Log("VendorID:", CPU.VendorID)
t.Log("Name:", CPU.BrandName)
t.Log("PhysicalCores:", CPU.PhysicalCores)
t.Log("ThreadsPerCore:", CPU.ThreadsPerCore)
Expand Down
6 changes: 4 additions & 2 deletions mockcpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,11 @@ func TestMocks(t *testing.T) {
Detect()
t.Log("Name:", CPU.BrandName)
n := maxFunctionID()
t.Logf("Max Function:0x%x\n", n)
t.Logf("Max Function:0x%x", n)
n = maxExtendedFunction()
t.Logf("Max Extended Function:0x%x\n", n)
t.Logf("Max Extended Function:0x%x", n)
t.Log("VendorString:", CPU.VendorString)
t.Log("VendorID:", CPU.VendorID)
t.Log("PhysicalCores:", CPU.PhysicalCores)
t.Log("ThreadsPerCore:", CPU.ThreadsPerCore)
t.Log("LogicalCores:", CPU.LogicalCores)
Expand Down
39 changes: 25 additions & 14 deletions private/cpuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
xenhvm
bhyve
hygon
sis
rdc
)

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

func threadsPerCore() int {
mfi := maxFunctionID()
if mfi < 0x4 || (vendorID() != intel && vendorID() != amd) {
vend, _ := vendorID()

if mfi < 0x4 || (vend != intel && vend != amd) {
return 1
}

if mfi < 0xb {
if vendorID() != intel {
if vend != intel {
return 1
}
_, b, _, d := cpuid(1)
Expand All @@ -752,7 +757,8 @@ func threadsPerCore() int {

func logicalCores() int {
mfi := maxFunctionID()
switch vendorID() {
v, _ := vendorID()
switch v {
case intel:
// Use this on old Intel processors
if mfi < 0xb {
Expand Down Expand Up @@ -787,7 +793,8 @@ func familyModel() (int, int) {
}

func physicalCores() int {
switch vendorID() {
v, _ := vendorID()
switch v {
case intel:
return logicalCores() / threadsPerCore()
case amd, hygon:
Expand Down Expand Up @@ -822,16 +829,20 @@ var vendorMapping = map[string]vendor{
"XenVMMXenVMM": xenhvm,
"bhyve bhyve ": bhyve,
"HygonGenuine": hygon,
"Vortex86 SoC": sis,
"SiS SiS SiS ": sis,
"RiseRiseRise": sis,
"Genuine RDC": rdc,
}

func vendorID() vendor {
func vendorID() (vendor, string) {
_, b, c, d := cpuid(0)
v := valAsString(b, d, c)
vend, ok := vendorMapping[string(v)]
v := string(valAsString(b, d, c))
vend, ok := vendorMapping[v]
if !ok {
return other
return other, v
}
return vend
return vend, v
}

func cacheLine() int {
Expand All @@ -854,7 +865,7 @@ func (c *cpuInfo) cacheSize() {
c.cache.l1i = -1
c.cache.l2 = -1
c.cache.l3 = -1
vendor := vendorID()
vendor, _ := vendorID()
switch vendor {
case intel:
if maxFunctionID() < 4 {
Expand Down Expand Up @@ -1009,7 +1020,7 @@ func hasSGX(available, lc bool) (rval sgxsupport) {

func support() flags {
mfi := maxFunctionID()
vend := vendorID()
vend, _ := vendorID()
if mfi < 0x1 {
return 0
}
Expand Down Expand Up @@ -1237,7 +1248,7 @@ func support() flags {
AV_CPU_FLAG_SSE2 and AV_CPU_FLAG_SSE2SLOW are both set in this case
so that SSE2 is used unless explicitly disabled by checking
AV_CPU_FLAG_SSE2SLOW. */
if vendorID() != intel &&
if vend != intel &&
rval&sse2 != 0 && (c&0x00000040) == 0 {
rval |= sse2slow
}
Expand All @@ -1253,7 +1264,7 @@ func support() flags {
}
}

if vendorID() == intel {
if vend == intel {
family, model := familyModel()
if family == 6 && (model == 9 || model == 13 || model == 14) {
/* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and
Expand Down
6 changes: 4 additions & 2 deletions private/cpuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
// obviously differ on each machine.
func TestCPUID(t *testing.T) {
n := maxFunctionID()
t.Logf("Max Function:0x%x\n", n)
t.Logf("Max Function:0x%x", n)
n = maxExtendedFunction()
t.Logf("Max Extended Function:0x%x\n", n)
t.Logf("Max Extended Function:0x%x", n)
t.Log("VendorString:", cpu.vendorstring)
t.Log("VendorID:", cpu.vendorid)
t.Log("Name:", cpu.brandname)
t.Log("PhysicalCores:", cpu.physicalcores)
t.Log("ThreadsPerCore:", cpu.threadspercore)
Expand Down

0 comments on commit e252067

Please sign in to comment.