From fe4135e6ee906cd79b8834947aa9465f852d6f15 Mon Sep 17 00:00:00 2001 From: Christian Kniep Date: Mon, 6 Feb 2023 12:01:48 +0100 Subject: [PATCH 1/2] add initial version of a flag-parser --- archspec/cpu/microarchitecture.go | 15 ++++-- archspec/cpu/proc.go | 52 ++++++++++++++++++++ archspec/cpu/proc_test.go | 82 +++++++++++++++++++++++++++++++ go.mod | 11 ++++- go.sum | 17 +++++++ 5 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 archspec/cpu/proc.go create mode 100644 archspec/cpu/proc_test.go diff --git a/archspec/cpu/microarchitecture.go b/archspec/cpu/microarchitecture.go index 75f52ab..40dc792 100644 --- a/archspec/cpu/microarchitecture.go +++ b/archspec/cpu/microarchitecture.go @@ -64,13 +64,18 @@ func addItem(targets map[string]Microarchitecture, name string, value jMicroarch } parents = append(parents, targets[pname]) } + ma := MicroarchFromJMA(name, value) + ma.Parents = parents + targets[name] = ma +} - targets[name] = Microarchitecture{ +func MicroarchFromJMA(name string, jma jMicroarchitecture) Microarchitecture { + return Microarchitecture{ Name: name, - Vendor: value.Vendor, - Features: *strset.New(value.Features...), - Generation: value.Generation, - Parents: parents, + Vendor: jma.Vendor, + Features: *strset.New(jma.Features...), + Generation: jma.Generation, + Parents: make([]Microarchitecture, 0), } } diff --git a/archspec/cpu/proc.go b/archspec/cpu/proc.go new file mode 100644 index 0000000..937d6de --- /dev/null +++ b/archspec/cpu/proc.go @@ -0,0 +1,52 @@ +package cpu + +import ( + "errors" + "fmt" + "sort" + "strings" + + "github.com/scylladb/go-set/strset" +) + +var ErrKeyNotFound = errors.New("missing key in cpuinfo") + +type cMap struct { + Name string + Length int + MArch Microarchitecture +} + +func ParseCPUInfo(cpuinfo map[string]string) (march Microarchitecture, err error) { + candidateMap := []cMap{} + vndID, ok := cpuinfo["vendor_id"] + if !ok { + err = fmt.Errorf("%q: %w", "vendor_id", ErrKeyNotFound) + return + } + flags, ok := cpuinfo["flags"] + if !ok { + err = fmt.Errorf("%q: %w", "flags", ErrKeyNotFound) + return + } + + for name, value := range JSONData.Microarchitectures { + if value.Vendor != vndID { + continue + } + flagSlice := strings.Split(flags, " ") + flagSet := strset.New(flagSlice...) + entrySet := strset.New(value.Features...) + superS := flagSet.IsSubset(entrySet) + if superS { + candidateMap = append(candidateMap, cMap{Name: name, Length: len(entrySet.List()), MArch: MicroarchFromJMA(name, value)}) + } + } + // Sort by age preserving name order + sort.SliceStable(candidateMap, func(i, j int) bool { return candidateMap[i].Length > candidateMap[j].Length }) + if len(candidateMap) == 0 { + err = errors.New("no matching microarchitecture found") + } + march = candidateMap[0].MArch + return +} diff --git a/archspec/cpu/proc_test.go b/archspec/cpu/proc_test.go new file mode 100644 index 0000000..0c57401 --- /dev/null +++ b/archspec/cpu/proc_test.go @@ -0,0 +1,82 @@ +package cpu + +import ( + "bufio" + "errors" + "os" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + haswell = map[string]string{ + "vendor_id": "GenuineIntel", + "flags": "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb invpcid_single tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc ibpb ibrs stibp dtherm arat pln pts spec_ctrl intel_stibp", + } +) + +func TestProcInfo(t *testing.T) { + _, err := ParseCPUInfo(map[string]string{}) + if !errors.Is(err, ErrKeyNotFound) { + t.Errorf("wrong error: %v", err) + } + _, err = ParseCPUInfo(map[string]string{"vendor_id": "GenuineIntel"}) + if !errors.Is(err, ErrKeyNotFound) { + t.Errorf("wrong error: %v", err) + } + _, err = ParseCPUInfo(map[string]string{"flags": "sse"}) + if !errors.Is(err, ErrKeyNotFound) { + t.Errorf("wrong error: %v", err) + } + maHW, err := ParseCPUInfo(haswell) + assert.NoError(t, err) + assert.Equal(t, "haswell", maHW.Name) +} + +func TestProcInfoFiles(t *testing.T) { + cpuinfo, err := ParseTestFile("../json/tests/targets/linux-centos7-cascadelake") + assert.NoError(t, err) + maHW, err := ParseCPUInfo(cpuinfo) + assert.NoError(t, err) + assert.Equal(t, "cascadelake", maHW.Name) + cpuinfo, err = ParseTestFile("../json/tests/targets/linux-rhel7-haswell") + assert.NoError(t, err) + maHW, err = ParseCPUInfo(cpuinfo) + assert.NoError(t, err) + assert.Equal(t, "haswell", maHW.Name) + cpuinfo, err = ParseTestFile("../json/tests/targets/linux-rhel7-skylake_avx512") + assert.NoError(t, err) + maHW, err = ParseCPUInfo(cpuinfo) + assert.NoError(t, err) + assert.Equal(t, "skylake_avx512", maHW.Name) + cpuinfo, err = ParseTestFile("../json/tests/targets/linux-rhel7-zen") + assert.NoError(t, err) + maHW, err = ParseCPUInfo(cpuinfo) + assert.NoError(t, err) + assert.Equal(t, "zen", maHW.Name, "wrong arch: %v", maHW) + cpuinfo, err = ParseTestFile("../json/tests/targets/linux-ubuntu20.04-zen3") + assert.NoError(t, err) + maHW, err = ParseCPUInfo(cpuinfo) + assert.NoError(t, err) + assert.Equal(t, "zen3", maHW.Name) + +} + +func ParseTestFile(fname string) (map[string]string, error) { + cpuinfo := map[string]string{} + file, err := os.Open(fname) + if err != nil { + return nil, err + } + defer file.Close() + scanner := bufio.NewScanner(file) + for scanner.Scan() { + s := strings.Split(scanner.Text(), ":") + if len(s) == 2 { + cpuinfo[strings.TrimSpace(s[0])] = strings.TrimSpace(s[1]) + } + } + return cpuinfo, nil +} diff --git a/go.mod b/go.mod index 6cdfd34..d6552b5 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,13 @@ module github.com/archspec/archspec-go go 1.19 -require github.com/scylladb/go-set v1.0.2 +require ( + github.com/scylladb/go-set v1.0.2 + github.com/stretchr/testify v1.8.1 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index 9d82e32..e653ce7 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,21 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/set v0.2.1 h1:nn2CaJyknWE/6txyUDGwysr3G5QC6xWB/PtVjPBbeaA= github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/scylladb/go-set v1.0.2 h1:SkvlMCKhP0wyyct6j+0IHJkBkSZL+TDzZ4E7f7BCcRE= github.com/scylladb/go-set v1.0.2/go.mod h1:DkpGd78rljTxKAnTDPFqXSGxvETQnJyuSOQwsHycqfs= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From b15fc547d5a865b3558b5a0f0cdd7589cc704a45 Mon Sep 17 00:00:00 2001 From: Christian Kniep Date: Mon, 6 Feb 2023 13:31:03 +0100 Subject: [PATCH 2/2] add NewMAFromJSON --- archspec/cpu/microarchitecture.go | 18 +++++++++++++++++- archspec/cpu/proc_test.go | 19 +++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/archspec/cpu/microarchitecture.go b/archspec/cpu/microarchitecture.go index 40dc792..56324ed 100644 --- a/archspec/cpu/microarchitecture.go +++ b/archspec/cpu/microarchitecture.go @@ -70,13 +70,29 @@ func addItem(targets map[string]Microarchitecture, name string, value jMicroarch } func MicroarchFromJMA(name string, jma jMicroarchitecture) Microarchitecture { - return Microarchitecture{ + ma := Microarchitecture{ Name: name, Vendor: jma.Vendor, Features: *strset.New(jma.Features...), Generation: jma.Generation, Parents: make([]Microarchitecture, 0), } + switch jma.From.(type) { + case nil: + // Nothing to do, this is an architecture family like x86_64 or AArch64 + case string: + // The microarchitecture has a single parent + p := Microarchitecture{Name: jma.From.(string)} + ma.Parents = append(ma.Parents, p) + case []interface{}: + // The microarchitecture has a multiple parents (e.g. icelake) + for _, item := range jma.From.([]interface{}) { + p := Microarchitecture{Name: item.(string)} + ma.Parents = append(ma.Parents, p) + } + } + + return ma } // Ancestors returns the list of all the ancestors of the current microarchitecture diff --git a/archspec/cpu/proc_test.go b/archspec/cpu/proc_test.go index 0c57401..ffe6f68 100644 --- a/archspec/cpu/proc_test.go +++ b/archspec/cpu/proc_test.go @@ -56,12 +56,31 @@ func TestProcInfoFiles(t *testing.T) { maHW, err = ParseCPUInfo(cpuinfo) assert.NoError(t, err) assert.Equal(t, "zen", maHW.Name, "wrong arch: %v", maHW) + // Zen3 cpuinfo, err = ParseTestFile("../json/tests/targets/linux-ubuntu20.04-zen3") assert.NoError(t, err) maHW, err = ParseCPUInfo(cpuinfo) assert.NoError(t, err) assert.Equal(t, "zen3", maHW.Name) +} +func TestParse_Family(t *testing.T) { + cpuinfo, err := ParseTestFile("../json/tests/targets/linux-rhel7-zen") + assert.NoError(t, err) + maHW, err := ParseCPUInfo(cpuinfo) + assert.NoError(t, err) + assert.Equal(t, "zen", maHW.Name, "wrong arch: %v", maHW) + assert.Len(t, maHW.Parents, 1) + assert.Equal(t, "x86_64_v3", maHW.Parents[0].Name) + // Zen3 + cpuinfo, err = ParseTestFile("../json/tests/targets/linux-ubuntu20.04-zen3") + assert.NoError(t, err) + maHW, err = ParseCPUInfo(cpuinfo) + assert.NoError(t, err) + assert.Equal(t, "zen3", maHW.Name) + // TODO: How can I get the genric architecture from the cpuinfo? + // -> Iterate through the parents and find a generic one? + assert.Equal(t, "zen2", maHW.Parents[0].Name) } func ParseTestFile(fname string) (map[string]string, error) { diff --git a/go.mod b/go.mod index d6552b5..e9a7f36 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/archspec/archspec-go go 1.19 require ( + github.com/dominikbraun/graph v0.15.1 github.com/scylladb/go-set v1.0.2 github.com/stretchr/testify v1.8.1 ) diff --git a/go.sum b/go.sum index e653ce7..ce9e53e 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dominikbraun/graph v0.15.1 h1:qUz85rdSHdUtqKE4YcbAuKH5iDm9waGsv3CymeuAEQQ= +github.com/dominikbraun/graph v0.15.1/go.mod h1:yOjYyogZLY1LSG9E33JWZJiq5k83Qy2C6POAuiViluc= github.com/fatih/set v0.2.1 h1:nn2CaJyknWE/6txyUDGwysr3G5QC6xWB/PtVjPBbeaA= github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=