Skip to content

Commit d447f9f

Browse files
authored
Merge pull request #1039 from Lomanic/issue1037
[cpu][linux] Fix #1037 only count logical cores where 2nd field is a number
2 parents 79048cc + 6589c5c commit d447f9f

File tree

6 files changed

+221
-3
lines changed

6 files changed

+221
-3
lines changed

cpu/cpu_linux.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,11 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
286286
if err == nil {
287287
for _, line := range lines {
288288
line = strings.ToLower(line)
289-
if strings.HasPrefix(line, "processor") {
290-
ret++
289+
if strings.HasPrefix(line, "processor") {
290+
_, err = strconv.Atoi(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:]))
291+
if err == nil {
292+
ret++
293+
}
291294
}
292295
}
293296
}

cpu/cpu_linux_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,18 @@ func TestCPUCountsAgainstLscpu(t *testing.T) {
9191
t.Errorf("expected %v, got %v", expectedLogical, logical)
9292
}
9393
}
94+
95+
func TestCPUCountsLogicalAndroid_1037(t *testing.T) { // https://github.com/shirou/gopsutil/issues/1037
96+
orig := os.Getenv("HOST_PROC")
97+
os.Setenv("HOST_PROC", "testdata/linux/1037/proc")
98+
defer os.Setenv("HOST_PROC", orig)
99+
100+
count, err := Counts(true)
101+
if err != nil {
102+
t.Errorf("error %v", err)
103+
}
104+
expected := 8
105+
if count != expected {
106+
t.Errorf("expected %v, got %v", expected, count)
107+
}
108+
}

cpu/testdata/linux/1037/proc/cpuinfo

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
processor : 0
2+
Processor : ARMv7 Processor rev 4 (v7l)
3+
model name : ARMv7 Processor rev 4 (v7l)
4+
BogoMIPS : 42.43
5+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
6+
CPU implementer : 0x41
7+
CPU architecture: 7
8+
CPU variant : 0x0
9+
CPU part : 0xd03
10+
CPU revision : 4
11+
12+
processor : 1
13+
Processor : ARMv7 Processor rev 4 (v7l)
14+
model name : ARMv7 Processor rev 4 (v7l)
15+
BogoMIPS : 42.43
16+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
17+
CPU implementer : 0x41
18+
CPU architecture: 7
19+
CPU variant : 0x0
20+
CPU part : 0xd03
21+
CPU revision : 4
22+
23+
processor : 2
24+
Processor : ARMv7 Processor rev 4 (v7l)
25+
model name : ARMv7 Processor rev 4 (v7l)
26+
BogoMIPS : 42.43
27+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
28+
CPU implementer : 0x41
29+
CPU architecture: 7
30+
CPU variant : 0x0
31+
CPU part : 0xd03
32+
CPU revision : 4
33+
34+
processor : 3
35+
Processor : ARMv7 Processor rev 4 (v7l)
36+
model name : ARMv7 Processor rev 4 (v7l)
37+
BogoMIPS : 42.43
38+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
39+
CPU implementer : 0x41
40+
CPU architecture: 7
41+
CPU variant : 0x0
42+
CPU part : 0xd03
43+
CPU revision : 4
44+
45+
processor : 4
46+
Processor : ARMv7 Processor rev 2 (v7l)
47+
model name : ARMv7 Processor rev 2 (v7l)
48+
BogoMIPS : 29.52
49+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
50+
CPU implementer : 0x41
51+
CPU architecture: 7
52+
CPU variant : 0x0
53+
CPU part : 0xd09
54+
CPU revision : 2
55+
56+
processor : 5
57+
Processor : ARMv7 Processor rev 2 (v7l)
58+
model name : ARMv7 Processor rev 2 (v7l)
59+
BogoMIPS : 29.52
60+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
61+
CPU implementer : 0x41
62+
CPU architecture: 7
63+
CPU variant : 0x0
64+
CPU part : 0xd09
65+
CPU revision : 2
66+
67+
processor : 6
68+
Processor : ARMv7 Processor rev 2 (v7l)
69+
model name : ARMv7 Processor rev 2 (v7l)
70+
BogoMIPS : 29.52
71+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
72+
CPU implementer : 0x41
73+
CPU architecture: 7
74+
CPU variant : 0x0
75+
CPU part : 0xd09
76+
CPU revision : 2
77+
78+
processor : 7
79+
Processor : ARMv7 Processor rev 2 (v7l)
80+
model name : ARMv7 Processor rev 2 (v7l)
81+
BogoMIPS : 29.52
82+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
83+
CPU implementer : 0x41
84+
CPU architecture: 7
85+
CPU variant : 0x0
86+
CPU part : 0xd09
87+
CPU revision : 2
88+
89+
Hardware : MT8183
90+
Revision : 0000
91+
Serial : 29aa1cf5ba0159c3

v3/cpu/cpu_linux.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,10 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
287287
for _, line := range lines {
288288
line = strings.ToLower(line)
289289
if strings.HasPrefix(line, "processor") {
290-
ret++
290+
_, err = strconv.Atoi(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:]))
291+
if err == nil {
292+
ret++
293+
}
291294
}
292295
}
293296
}

v3/cpu/cpu_linux_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,18 @@ func TestCPUCountsAgainstLscpu(t *testing.T) {
9191
t.Errorf("expected %v, got %v", expectedLogical, logical)
9292
}
9393
}
94+
95+
func TestCPUCountsLogicalAndroid_1037(t *testing.T) { // https://github.com/shirou/gopsutil/issues/1037
96+
orig := os.Getenv("HOST_PROC")
97+
os.Setenv("HOST_PROC", "testdata/linux/1037/proc")
98+
defer os.Setenv("HOST_PROC", orig)
99+
100+
count, err := Counts(true)
101+
if err != nil {
102+
t.Errorf("error %v", err)
103+
}
104+
expected := 8
105+
if count != expected {
106+
t.Errorf("expected %v, got %v", expected, count)
107+
}
108+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
processor : 0
2+
Processor : ARMv7 Processor rev 4 (v7l)
3+
model name : ARMv7 Processor rev 4 (v7l)
4+
BogoMIPS : 42.43
5+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
6+
CPU implementer : 0x41
7+
CPU architecture: 7
8+
CPU variant : 0x0
9+
CPU part : 0xd03
10+
CPU revision : 4
11+
12+
processor : 1
13+
Processor : ARMv7 Processor rev 4 (v7l)
14+
model name : ARMv7 Processor rev 4 (v7l)
15+
BogoMIPS : 42.43
16+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
17+
CPU implementer : 0x41
18+
CPU architecture: 7
19+
CPU variant : 0x0
20+
CPU part : 0xd03
21+
CPU revision : 4
22+
23+
processor : 2
24+
Processor : ARMv7 Processor rev 4 (v7l)
25+
model name : ARMv7 Processor rev 4 (v7l)
26+
BogoMIPS : 42.43
27+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
28+
CPU implementer : 0x41
29+
CPU architecture: 7
30+
CPU variant : 0x0
31+
CPU part : 0xd03
32+
CPU revision : 4
33+
34+
processor : 3
35+
Processor : ARMv7 Processor rev 4 (v7l)
36+
model name : ARMv7 Processor rev 4 (v7l)
37+
BogoMIPS : 42.43
38+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
39+
CPU implementer : 0x41
40+
CPU architecture: 7
41+
CPU variant : 0x0
42+
CPU part : 0xd03
43+
CPU revision : 4
44+
45+
processor : 4
46+
Processor : ARMv7 Processor rev 2 (v7l)
47+
model name : ARMv7 Processor rev 2 (v7l)
48+
BogoMIPS : 29.52
49+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
50+
CPU implementer : 0x41
51+
CPU architecture: 7
52+
CPU variant : 0x0
53+
CPU part : 0xd09
54+
CPU revision : 2
55+
56+
processor : 5
57+
Processor : ARMv7 Processor rev 2 (v7l)
58+
model name : ARMv7 Processor rev 2 (v7l)
59+
BogoMIPS : 29.52
60+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
61+
CPU implementer : 0x41
62+
CPU architecture: 7
63+
CPU variant : 0x0
64+
CPU part : 0xd09
65+
CPU revision : 2
66+
67+
processor : 6
68+
Processor : ARMv7 Processor rev 2 (v7l)
69+
model name : ARMv7 Processor rev 2 (v7l)
70+
BogoMIPS : 29.52
71+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
72+
CPU implementer : 0x41
73+
CPU architecture: 7
74+
CPU variant : 0x0
75+
CPU part : 0xd09
76+
CPU revision : 2
77+
78+
processor : 7
79+
Processor : ARMv7 Processor rev 2 (v7l)
80+
model name : ARMv7 Processor rev 2 (v7l)
81+
BogoMIPS : 29.52
82+
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
83+
CPU implementer : 0x41
84+
CPU architecture: 7
85+
CPU variant : 0x0
86+
CPU part : 0xd09
87+
CPU revision : 2
88+
89+
Hardware : MT8183
90+
Revision : 0000
91+
Serial : 29aa1cf5ba0159c3

0 commit comments

Comments
 (0)