Skip to content

Commit

Permalink
Handle a weird corner case /proc/cpuinfo
Browse files Browse the repository at this point in the history
This one I got from my cell phone.
  • Loading branch information
walles committed Feb 21, 2017
1 parent dba0fbe commit f34ba51
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
14 changes: 9 additions & 5 deletions px/px_cpuinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,31 @@ def get_core_count_from_proc_cpuinfo(proc_cpuinfo="/proc/cpuinfo"):
PROCESSOR_NO_PREFIX = 'processor\t: '
CORE_ID_PREFIX = 'core id\t\t: '

core_ids = set()
max_processor_no = 0
try:
with open(proc_cpuinfo) as f:
core_ids = set()
max_processor_no = 0

for line in f:
if line.startswith(PROCESSOR_NO_PREFIX):
processor_no = int(line[len(PROCESSOR_NO_PREFIX):])
max_processor_no = max(processor_no, max_processor_no)
elif line.startswith(CORE_ID_PREFIX):
core_id = int(line[len(CORE_ID_PREFIX)])
core_ids.add(core_id)

return (len(core_ids), max_processor_no + 1)
except (IOError, OSError) as e:
if e.errno == errno.ENOENT:
# /proc/cpuinfo not found, we're probably not on Linux
return None

raise

physical = len(core_ids)
logical = max_processor_no + 1
if physical == 0:
# I get this on my cell phone
physical = logical
return (physical, logical)


def get_core_count_from_sysctl():
env = os.environ.copy()
Expand Down
17 changes: 17 additions & 0 deletions tests/proc-cpuinfo-8p8l
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Processor : AArch64 Processor rev 0 (aarch64)
processor : 0
processor : 1
processor : 2
processor : 3
processor : 4
processor : 5
processor : 6
processor : 7
Features : fp asimd aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: AArch64
CPU variant : 0x1
CPU part : 0xd07
CPU revision : 0

Hardware : SAMSUNG Exynos7420
7 changes: 7 additions & 0 deletions tests/px_cpuinfo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ def test_get_core_count_from_proc_cpuinfo():
assert physical == 1
assert logical == 1

# This one is from my cell phone, just to provide a weird corner case example of things
# we may have to handle.
physical, logical = px_cpuinfo.get_core_count_from_proc_cpuinfo(
os.path.join(my_dir, "proc-cpuinfo-8p8l"))
assert physical == 8
assert logical == 8

assert px_cpuinfo.get_core_count_from_proc_cpuinfo("/does/not/exist") is None


Expand Down

0 comments on commit f34ba51

Please sign in to comment.