[cpu][linux] Fix #1037 only count logical cores where 2nd field is a number

pull/1039/head
Lomanic 4 years ago
parent a44e7585f9
commit 6589c5c274

@ -286,8 +286,11 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
if err == nil {
for _, line := range lines {
line = strings.ToLower(line)
if strings.HasPrefix(line, "processor") {
ret++
if strings.HasPrefix(line, "processor") {
_, err = strconv.Atoi(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:]))
if err == nil {
ret++
}
}
}
}

@ -91,3 +91,18 @@ func TestCPUCountsAgainstLscpu(t *testing.T) {
t.Errorf("expected %v, got %v", expectedLogical, logical)
}
}
func TestCPUCountsLogicalAndroid_1037(t *testing.T) { // https://github.com/shirou/gopsutil/issues/1037
orig := os.Getenv("HOST_PROC")
os.Setenv("HOST_PROC", "testdata/linux/1037/proc")
defer os.Setenv("HOST_PROC", orig)
count, err := Counts(true)
if err != nil {
t.Errorf("error %v", err)
}
expected := 8
if count != expected {
t.Errorf("expected %v, got %v", expected, count)
}
}

@ -0,0 +1,91 @@
processor : 0
Processor : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 42.43
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 1
Processor : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 42.43
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 2
Processor : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 42.43
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 3
Processor : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 42.43
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 4
Processor : ARMv7 Processor rev 2 (v7l)
model name : ARMv7 Processor rev 2 (v7l)
BogoMIPS : 29.52
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd09
CPU revision : 2
processor : 5
Processor : ARMv7 Processor rev 2 (v7l)
model name : ARMv7 Processor rev 2 (v7l)
BogoMIPS : 29.52
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd09
CPU revision : 2
processor : 6
Processor : ARMv7 Processor rev 2 (v7l)
model name : ARMv7 Processor rev 2 (v7l)
BogoMIPS : 29.52
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd09
CPU revision : 2
processor : 7
Processor : ARMv7 Processor rev 2 (v7l)
model name : ARMv7 Processor rev 2 (v7l)
BogoMIPS : 29.52
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd09
CPU revision : 2
Hardware : MT8183
Revision : 0000
Serial : 29aa1cf5ba0159c3

@ -287,7 +287,10 @@ func CountsWithContext(ctx context.Context, logical bool) (int, error) {
for _, line := range lines {
line = strings.ToLower(line)
if strings.HasPrefix(line, "processor") {
ret++
_, err = strconv.Atoi(strings.TrimSpace(line[strings.IndexByte(line, ':')+1:]))
if err == nil {
ret++
}
}
}
}

@ -91,3 +91,18 @@ func TestCPUCountsAgainstLscpu(t *testing.T) {
t.Errorf("expected %v, got %v", expectedLogical, logical)
}
}
func TestCPUCountsLogicalAndroid_1037(t *testing.T) { // https://github.com/shirou/gopsutil/issues/1037
orig := os.Getenv("HOST_PROC")
os.Setenv("HOST_PROC", "testdata/linux/1037/proc")
defer os.Setenv("HOST_PROC", orig)
count, err := Counts(true)
if err != nil {
t.Errorf("error %v", err)
}
expected := 8
if count != expected {
t.Errorf("expected %v, got %v", expected, count)
}
}

@ -0,0 +1,91 @@
processor : 0
Processor : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 42.43
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 1
Processor : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 42.43
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 2
Processor : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 42.43
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 3
Processor : ARMv7 Processor rev 4 (v7l)
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 42.43
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4
processor : 4
Processor : ARMv7 Processor rev 2 (v7l)
model name : ARMv7 Processor rev 2 (v7l)
BogoMIPS : 29.52
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd09
CPU revision : 2
processor : 5
Processor : ARMv7 Processor rev 2 (v7l)
model name : ARMv7 Processor rev 2 (v7l)
BogoMIPS : 29.52
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd09
CPU revision : 2
processor : 6
Processor : ARMv7 Processor rev 2 (v7l)
model name : ARMv7 Processor rev 2 (v7l)
BogoMIPS : 29.52
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd09
CPU revision : 2
processor : 7
Processor : ARMv7 Processor rev 2 (v7l)
model name : ARMv7 Processor rev 2 (v7l)
BogoMIPS : 29.52
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd09
CPU revision : 2
Hardware : MT8183
Revision : 0000
Serial : 29aa1cf5ba0159c3
Loading…
Cancel
Save