Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial version of a flag-parser #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions archspec/cpu/microarchitecture.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,35 @@ 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 {
ma := 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),
}
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
Expand Down
52 changes: 52 additions & 0 deletions archspec/cpu/proc.go
Original file line number Diff line number Diff line change
@@ -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
}
101 changes: 101 additions & 0 deletions archspec/cpu/proc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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)
// 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) {
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
}
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ module github.com/archspec/archspec-go

go 1.19

require github.com/scylladb/go-set v1.0.2
require (
github.com/dominikbraun/graph v0.15.1
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
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
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=
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=