Skip to content

Commit a0b321e

Browse files
committed
Added support for CPUInfo for ARM on Linux kernels 3.8+
Fixes issue #294 Signed-off-by: kadern0 <kaderno@gmail.com>
1 parent c6ff04b commit a0b321e

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

cpuinfo.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,26 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) {
190190
scanner := bufio.NewScanner(bytes.NewReader(info))
191191

192192
firstLine := firstNonEmptyLine(scanner)
193-
if !strings.HasPrefix(firstLine, "Processor") || !strings.Contains(firstLine, ":") {
193+
match, _ := regexp.MatchString("^[Pp]rocessor", firstLine)
194+
if !match || !strings.Contains(firstLine, ":") {
194195
return nil, errors.New("invalid cpuinfo file: " + firstLine)
195196
}
196197
field := strings.SplitN(firstLine, ": ", 2)
197-
commonCPUInfo := CPUInfo{VendorID: field[1]}
198-
199198
cpuinfo := []CPUInfo{}
200-
i := -1
201199
featuresLine := ""
200+
commonCPUInfo := CPUInfo{}
201+
i := 0
202+
if field[0] == "Processor " {
203+
commonCPUInfo = CPUInfo{VendorID: field[1]}
204+
i = -1
205+
} else {
206+
v, err := strconv.ParseUint(field[1], 0, 32)
207+
if err != nil {
208+
return nil, err
209+
}
210+
firstcpu := CPUInfo{Processor: uint(v)}
211+
cpuinfo = []CPUInfo{firstcpu}
212+
}
202213

203214
for scanner.Scan() {
204215
line := scanner.Text()
@@ -223,13 +234,16 @@ func parseCPUInfoARM(info []byte) ([]CPUInfo, error) {
223234
cpuinfo[i].BogoMips = v
224235
case "Features":
225236
featuresLine = line
237+
case "model name":
238+
cpuinfo[i].ModelName = field[1]
226239
}
227240
}
228241
fields := strings.SplitN(featuresLine, ": ", 2)
229242
for i := range cpuinfo {
230243
cpuinfo[i].Flags = strings.Fields(fields[1])
231244
}
232245
return cpuinfo, nil
246+
233247
}
234248

235249
func parseCPUInfoS390X(info []byte) ([]CPUInfo, error) {

cpuinfo_test.go

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package procfs
1818
import "testing"
1919

2020
const (
21-
cpuinfoArm7 = `
21+
cpuinfoArm7Legacy = `
2222
Processor : ARMv7 Processor rev 5 (v7l)
2323
processor : 0
2424
BogoMIPS : 2400.00
@@ -37,6 +37,51 @@ Hardware : sun8i
3737
Revision : 0000
3838
Serial : 5400503583203c3c040e`
3939

40+
cpuinfoArm7 = `
41+
processor : 0
42+
model name : ARMv7 Processor rev 3 (v7l)
43+
BogoMIPS : 108.00
44+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
45+
CPU implementer : 0x41
46+
CPU architecture: 7
47+
CPU variant : 0x0
48+
CPU part : 0xd08
49+
CPU revision : 3
50+
51+
processor : 1
52+
model name : ARMv7 Processor rev 3 (v7l)
53+
BogoMIPS : 108.00
54+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
55+
CPU implementer : 0x41
56+
CPU architecture: 7
57+
CPU variant : 0x0
58+
CPU part : 0xd08
59+
CPU revision : 3
60+
61+
processor : 2
62+
model name : ARMv7 Processor rev 3 (v7l)
63+
BogoMIPS : 108.00
64+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
65+
CPU implementer : 0x41
66+
CPU architecture: 7
67+
CPU variant : 0x0
68+
CPU part : 0xd08
69+
CPU revision : 3
70+
71+
processor : 3
72+
model name : ARMv7 Processor rev 3 (v7l)
73+
BogoMIPS : 108.00
74+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
75+
CPU implementer : 0x41
76+
CPU architecture: 7
77+
CPU variant : 0x0
78+
CPU part : 0xd08
79+
CPU revision : 3
80+
81+
Hardware : BCM2835
82+
Revision : c03111
83+
`
84+
4085
cpuinfoS390x = `
4186
vendor_id : IBM/S390
4287
# processors : 4
@@ -153,8 +198,8 @@ func TestCPUInfoX86(t *testing.T) {
153198
}
154199
}
155200

156-
func TestCPUInfoParseARM(t *testing.T) {
157-
cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7))
201+
func TestCPUInfoParseARMLegacy(t *testing.T) {
202+
cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7Legacy))
158203
if err != nil || cpuinfo == nil {
159204
t.Fatalf("unable to parse arm cpu info: %v", err)
160205
}
@@ -169,6 +214,22 @@ func TestCPUInfoParseARM(t *testing.T) {
169214
}
170215
}
171216

217+
func TestCPUInfoParseARM(t *testing.T) {
218+
cpuinfo, err := parseCPUInfoARM([]byte(cpuinfoArm7))
219+
if err != nil || cpuinfo == nil {
220+
t.Fatalf("unable to parse arm cpu info: %v", err)
221+
}
222+
if want, have := 4, len(cpuinfo); want != have {
223+
t.Errorf("want number of processors %v, have %v", want, have)
224+
}
225+
if want, have := "ARMv7 Processor rev 3 (v7l)", cpuinfo[0].ModelName; want != have {
226+
t.Errorf("want vendor %v, have %v", want, have)
227+
}
228+
if want, have := "thumb", cpuinfo[1].Flags[1]; want != have {
229+
t.Errorf("want flag %v, have %v", want, have)
230+
}
231+
}
232+
172233
func TestCPUInfoParseS390X(t *testing.T) {
173234
cpuinfo, err := parseCPUInfoS390X([]byte(cpuinfoS390x))
174235
if err != nil || cpuinfo == nil {

0 commit comments

Comments
 (0)