Skip to content

Commit

Permalink
Merge pull request #1039 from Lomanic/issue1037
Browse files Browse the repository at this point in the history
[cpu][linux] Fix #1037 only count logical cores where 2nd field is a number
  • Loading branch information
Lomanic committed Apr 13, 2021
2 parents 79048cc + 6589c5c commit d447f9f
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 3 deletions.
7 changes: 5 additions & 2 deletions cpu/cpu_linux.go
Expand Up @@ -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++
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions cpu/cpu_linux_test.go
Expand Up @@ -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)
}
}
91 changes: 91 additions & 0 deletions cpu/testdata/linux/1037/proc/cpuinfo
@@ -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
5 changes: 4 additions & 1 deletion v3/cpu/cpu_linux.go
Expand Up @@ -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++
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions v3/cpu/cpu_linux_test.go
Expand Up @@ -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)
}
}
91 changes: 91 additions & 0 deletions v3/cpu/testdata/linux/1037/proc/cpuinfo
@@ -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

0 comments on commit d447f9f

Please sign in to comment.