Skip to content

Commit

Permalink
Added support for CPUInfo for ARM on Linux kernels 3.8+
Browse files Browse the repository at this point in the history
Fixes issue prometheus#294

Signed-off-by: kadern0 <kaderno@gmail.com>
  • Loading branch information
kadern0 committed May 18, 2020
1 parent c6ff04b commit a0b321e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 7 deletions.
22 changes: 18 additions & 4 deletions cpuinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -223,13 +234,16 @@ 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)
for i := range cpuinfo {
cpuinfo[i].Flags = strings.Fields(fields[1])
}
return cpuinfo, nil

}

func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {
Expand Down
67 changes: 64 additions & 3 deletions cpuinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package procfs
import "testing"

const (
cpuinfoArm7 = `
cpuinfoArm7Legacy = `
Processor : ARMv7 Processor rev 5 (v7l)
processor : 0
BogoMIPS : 2400.00
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit a0b321e

Please sign in to comment.