Skip to content

Commit afd68b2

Browse files
committed
fix GNR_X* microarchitecture detection
1 parent 244b484 commit afd68b2

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

internal/cpudb/cpu_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestGetCPU(t *testing.T) {
5656
t.Fatal(fmt.Errorf("Found the wrong CPU: %s", cpu.MicroArchitecture))
5757
}
5858

59-
cpu, err = cpudb.GetCPUExtended("6", "173", "", "", "2", "10") // GNR_X3
59+
cpu, err = cpudb.GetCPUExtended("6", "173", "", "", "10") // GNR_X3
6060
if err != nil {
6161
t.Fatal(err)
6262
}
@@ -67,7 +67,7 @@ func TestGetCPU(t *testing.T) {
6767
t.Fatal(fmt.Errorf("Incorrect channel count: %d", cpu.MemoryChannelCount))
6868
}
6969

70-
cpu, err = cpudb.GetCPUExtended("6", "173", "", "", "2", "8") // GNR_X2
70+
cpu, err = cpudb.GetCPUExtended("6", "173", "", "", "8") // GNR_X2
7171
if err != nil {
7272
t.Fatal(err)
7373
}
@@ -78,7 +78,7 @@ func TestGetCPU(t *testing.T) {
7878
t.Fatal(fmt.Errorf("Incorrect channel count: %d", cpu.MemoryChannelCount))
7979
}
8080

81-
cpu, err = cpudb.GetCPUExtended("6", "173", "", "", "2", "6") // GNR_X1
81+
cpu, err = cpudb.GetCPUExtended("6", "173", "", "", "6") // GNR_X1
8282
if err != nil {
8383
t.Fatal(err)
8484
}
@@ -97,7 +97,7 @@ func TestGetCPU(t *testing.T) {
9797
t.Fatal(fmt.Errorf("Found the wrong CPU: %s", cpu.MicroArchitecture))
9898
}
9999

100-
cpu, err = cpudb.GetCPUExtended("6", "207", "", "c0", "", "") // EMR XCC
100+
cpu, err = cpudb.GetCPUExtended("6", "207", "", "c0", "") // EMR XCC
101101
if err != nil {
102102
t.Fatal(err)
103103
}
@@ -108,7 +108,7 @@ func TestGetCPU(t *testing.T) {
108108
t.Fatal(fmt.Errorf("Incorrect channel count: %d", cpu.MemoryChannelCount))
109109
}
110110

111-
cpu, err = cpudb.GetCPUExtended("6", "207", "", "40", "", "") // EMR MCC
111+
cpu, err = cpudb.GetCPUExtended("6", "207", "", "40", "") // EMR MCC
112112
if err != nil {
113113
t.Fatal(err)
114114
}

internal/cpudb/cpudb.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ func NewCPUDB() *CPUDB {
2222

2323
// GetCPU retrieves the CPU structure that matches the provided args
2424
func (c *CPUDB) GetCPU(family, model, stepping string) (cpu CPU, err error) {
25-
return c.GetCPUExtended(family, model, stepping, "", "", "")
25+
return c.GetCPUExtended(family, model, stepping, "", "")
2626
}
2727

2828
// GetCPUExtended retrieves the CPU structure that matches the provided args
2929
// capid4 needed to differentiate EMR MCC from EMR XCC
3030
//
3131
// capid4: $ lspci -s $(lspci | grep 325b | awk 'NR==1{{print $1}}') -xxx | awk '$1 ~ /^90/{{print $9 $8 $7 $6; exit}}'
3232
//
33-
// sockets and devices (eventually devices per socket) needed to differentiate GNR X1/2/3
33+
// devices needed to differentiate GNR X1/2/3
3434
//
3535
// devices: $ lspci -d 8086:3258 | wc -l
36-
func (c *CPUDB) GetCPUExtended(family, model, stepping, capid4, sockets, devices string) (cpu CPU, err error) {
36+
func (c *CPUDB) GetCPUExtended(family, model, stepping, capid4, devices string) (cpu CPU, err error) {
3737
for _, info := range *c {
3838
// if family matches
3939
if info.Family == family {
@@ -59,7 +59,7 @@ func (c *CPUDB) GetCPUExtended(family, model, stepping, capid4, sockets, devices
5959
}
6060
cpu = info
6161
if cpu.Family == "6" && (cpu.Model == "143" || cpu.Model == "207" || cpu.Model == "173") { // SPR, EMR, GNR
62-
cpu, err = c.getSpecificCPU(family, model, capid4, sockets, devices)
62+
cpu, err = c.getSpecificCPU(family, model, capid4, devices)
6363
}
6464
return
6565
}
@@ -80,13 +80,13 @@ func (c *CPUDB) GetCPUByMicroArchitecture(uarch string) (cpu CPU, err error) {
8080
return
8181
}
8282

83-
func (c *CPUDB) getSpecificCPU(family, model, capid4, sockets, devices string) (cpu CPU, err error) {
83+
func (c *CPUDB) getSpecificCPU(family, model, capid4, devices string) (cpu CPU, err error) {
8484
if family == "6" && model == "143" { // SPR
8585
cpu, err = c.getSPRCPU(capid4)
8686
} else if family == "6" && model == "207" { // EMR
8787
cpu, err = c.getEMRCPU(capid4)
8888
} else if family == "6" && model == "173" { // GNR
89-
cpu, err = c.getGNRCPU(sockets, devices)
89+
cpu, err = c.getGNRCPU(devices)
9090
}
9191
return
9292
}
@@ -149,21 +149,17 @@ func (c *CPUDB) getEMRCPU(capid4 string) (cpu CPU, err error) {
149149
return
150150
}
151151

152-
func (c *CPUDB) getGNRCPU(sockets, devices string) (cpu CPU, err error) {
152+
func (c *CPUDB) getGNRCPU(devices string) (cpu CPU, err error) {
153153
var uarch string
154-
if sockets != "" && devices != "" {
155-
s, err := strconv.Atoi(sockets)
156-
if err == nil && s != 0 {
157-
d, err := strconv.Atoi(devices)
158-
if err == nil && d != 0 {
159-
devicesPerSocket := d / s
160-
if devicesPerSocket == 3 {
161-
uarch = "GNR_X1"
162-
} else if devicesPerSocket == 4 {
163-
uarch = "GNR_X2"
164-
} else if devicesPerSocket == 5 {
165-
uarch = "GNR_X3"
166-
}
154+
if devices != "" {
155+
d, err := strconv.Atoi(devices)
156+
if err == nil && d != 0 {
157+
if d%5 == 0 { // device count is multiple of 5
158+
uarch = "GNR_X3"
159+
} else if d%4 == 0 { // device count is multiple of 4
160+
uarch = "GNR_X2"
161+
} else if d%3 == 0 { // device count is multiple of 3
162+
uarch = "GNR_X1"
167163
}
168164
}
169165
}

internal/report/table_helpers.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,16 @@ func getDmiDecodeEntries(dmiDecodeOutput string, dmiType string) (entries [][]st
136136
return
137137
}
138138

139-
// uarchFromOutput returns the architecture of the CPU that matches family, model, stepping, sockets,
139+
// uarchFromOutput returns the architecture of the CPU that matches family, model, stepping,
140140
// capid4, and devices information from the output or an empty string, if no match is found.
141141
func uarchFromOutput(outputs map[string]script.ScriptOutput) string {
142142
family := valFromRegexSubmatch(outputs[script.LscpuScriptName].Stdout, `^CPU family:\s*(.+)$`)
143143
model := valFromRegexSubmatch(outputs[script.LscpuScriptName].Stdout, `^Model:\s*(.+)$`)
144144
stepping := valFromRegexSubmatch(outputs[script.LscpuScriptName].Stdout, `^Stepping:\s*(.+)$`)
145-
sockets := valFromRegexSubmatch(outputs[script.LscpuScriptName].Stdout, `^Socket\(s\):\s*(.+)$`)
146145
capid4 := valFromRegexSubmatch(outputs[script.LspciBitsScriptName].Stdout, `^([0-9a-fA-F]+)`)
147146
devices := valFromRegexSubmatch(outputs[script.LspciDevicesScriptName].Stdout, `^([0-9]+)`)
148147
CPUdb := cpudb.NewCPUDB()
149-
cpu, err := CPUdb.GetCPUExtended(family, model, stepping, capid4, sockets, devices)
148+
cpu, err := CPUdb.GetCPUExtended(family, model, stepping, capid4, devices)
150149
if err == nil {
151150
return cpu.MicroArchitecture
152151
}
@@ -305,7 +304,7 @@ func hyperthreadingFromOutput(outputs map[string]script.ScriptOutput) string {
305304
return ""
306305
}
307306
CPUdb := cpudb.NewCPUDB()
308-
cpu, err := CPUdb.GetCPUExtended(family, model, stepping, "", sockets, "")
307+
cpu, err := CPUdb.GetCPUExtended(family, model, stepping, "", "")
309308
if err != nil {
310309
return ""
311310
}
@@ -349,11 +348,10 @@ func channelsFromOutput(outputs map[string]script.ScriptOutput) string {
349348
family := valFromRegexSubmatch(outputs[script.LscpuScriptName].Stdout, `^CPU family:\s*(.+)$`)
350349
model := valFromRegexSubmatch(outputs[script.LscpuScriptName].Stdout, `^Model:\s*(.+)$`)
351350
stepping := valFromRegexSubmatch(outputs[script.LscpuScriptName].Stdout, `^Stepping:\s*(.+)$`)
352-
sockets := valFromRegexSubmatch(outputs[script.LscpuScriptName].Stdout, `^Socket\(.*:\s*(.+?)$`)
353351
capid4 := valFromRegexSubmatch(outputs[script.LspciBitsScriptName].Stdout, `^([0-9a-fA-F]+)`)
354352
devices := valFromRegexSubmatch(outputs[script.LspciDevicesScriptName].Stdout, `^([0-9]+)`)
355353
CPUdb := cpudb.NewCPUDB()
356-
cpu, err := CPUdb.GetCPUExtended(family, model, stepping, capid4, sockets, devices)
354+
cpu, err := CPUdb.GetCPUExtended(family, model, stepping, capid4, devices)
357355
if err != nil {
358356
slog.Error("error getting CPU from CPUdb", slog.String("error", err.Error()))
359357
return ""

0 commit comments

Comments
 (0)