diff --git a/cpuinfo.go b/cpuinfo.go index bb3be99ff..1931de65c 100644 --- a/cpuinfo.go +++ b/cpuinfo.go @@ -190,15 +190,26 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) { scanner := bufio.NewScanner(bytes.NewReader(info)) firstLine := firstNonEmptyLine(scanner) - if !strings.HasPrefix(firstLine, "Processor") || !strings.Contains(firstLine, ":") { + match, _ := regexp.MatchString("^[Pp]rocessor", firstLine) + if !match || !strings.Contains(firstLine, ":") { return nil, errors.New("invalid cpuinfo file: " + firstLine) } field := strings.SplitN(firstLine, ": ", 2) - commonCPUInfo := CPUInfo{VendorID: field[1]} - cpuinfo := []CPUInfo{} - i := -1 featuresLine := "" + commonCPUInfo := CPUInfo{} + i := 0 + if field[0] == "Processor " { + commonCPUInfo = CPUInfo{VendorID: field[1]} + i = -1 + } else { + v, err := strconv.ParseUint(field[1], 0, 32) + if err != nil { + return nil, err + } + firstcpu := CPUInfo{Processor: uint(v)} + cpuinfo = []CPUInfo{firstcpu} + } for scanner.Scan() { line := scanner.Text() @@ -223,6 +234,8 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) { cpuinfo[i].BogoMips = v case "Features": featuresLine = line + case "model name": + cpuinfo[i].ModelName = field[1] } } fields := strings.SplitN(featuresLine, ": ", 2) @@ -230,6 +243,7 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) { cpuinfo[i].Flags = strings.Fields(fields[1]) } return cpuinfo, nil + } func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) { diff --git a/cpuinfo_test.go b/cpuinfo_test.go index b6869acee..d800749e0 100644 --- a/cpuinfo_test.go +++ b/cpuinfo_test.go @@ -18,7 +18,7 @@ package procfs import "testing" const ( - cpuinfoArm7 = ` + cpuinfoArm7Legacy = ` Processor : ARMv7 Processor rev 5 (v7l) processor : 0 BogoMIPS : 2400.00 @@ -37,6 +37,51 @@ Hardware : sun8i Revision : 0000 Serial : 5400503583203c3c040e` + cpuinfoArm7 = ` +processor : 0 +model name : ARMv7 Processor rev 3 (v7l) +BogoMIPS : 108.00 +Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 +CPU implementer : 0x41 +CPU architecture: 7 +CPU variant : 0x0 +CPU part : 0xd08 +CPU revision : 3 + +processor : 1 +model name : ARMv7 Processor rev 3 (v7l) +BogoMIPS : 108.00 +Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 +CPU implementer : 0x41 +CPU architecture: 7 +CPU variant : 0x0 +CPU part : 0xd08 +CPU revision : 3 + +processor : 2 +model name : ARMv7 Processor rev 3 (v7l) +BogoMIPS : 108.00 +Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 +CPU implementer : 0x41 +CPU architecture: 7 +CPU variant : 0x0 +CPU part : 0xd08 +CPU revision : 3 + +processor : 3 +model name : ARMv7 Processor rev 3 (v7l) +BogoMIPS : 108.00 +Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32 +CPU implementer : 0x41 +CPU architecture: 7 +CPU variant : 0x0 +CPU part : 0xd08 +CPU revision : 3 + +Hardware : BCM2835 +Revision : c03111 +` + cpuinfoS390x = ` vendor_id : IBM/S390 # processors : 4 @@ -153,8 +198,8 @@ func TestCPUInfoX86(t *testing.T) { } } -func TestCPUInfoParseARM(t *testing.T) { - cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7)) +func TestCPUInfoParseARMLegacy(t *testing.T) { + cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7Legacy)) if err != nil || cpuinfo == nil { t.Fatalf("unable to parse arm cpu info: %v", err) } @@ -169,6 +214,22 @@ func TestCPUInfoParseARM(t *testing.T) { } } +func TestCPUInfoParseARM(t *testing.T) { + cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7)) + if err != nil || cpuinfo == nil { + t.Fatalf("unable to parse arm cpu info: %v", err) + } + if want, have := 4, len(cpuinfo); want != have { + t.Errorf("want number of processors %v, have %v", want, have) + } + if want, have := "ARMv7 Processor rev 3 (v7l)", cpuinfo[0].ModelName; want != have { + t.Errorf("want vendor %v, have %v", want, have) + } + if want, have := "thumb", cpuinfo[1].Flags[1]; want != have { + t.Errorf("want flag %v, have %v", want, have) + } +} + func TestCPUInfoParseS390X(t *testing.T) { cpuinfo, err := parseCPUInfoS390X([]byte(cpuinfoS390x)) if err != nil || cpuinfo == nil {