Author: Catalin Gabriel Manciu <[email protected]>
Branch: detect_cpu_count
Changeset: r83669:91e8f98d883a
Date: 2016-04-14 13:47 +0300
http://bitbucket.org/pypy/pypy/changeset/91e8f98d883a/

Log:    (catalinm) detect_number_of_processors now uses
        multiprocessing.cpu_count as default behavior, the initial
        implementation is now just a fallback. Modified test_cpuinfo_linux
        to properly check its functionality. Modified the cpuinfo string to
        check regex for more than 10 cpus.

diff --git a/rpython/config/support.py b/rpython/config/support.py
--- a/rpython/config/support.py
+++ b/rpython/config/support.py
@@ -4,18 +4,12 @@
 
 import re, sys, os, subprocess
 
-def detect_number_of_processors(filename_or_file='/proc/cpuinfo'):
-    if os.environ.get('MAKEFLAGS'):
-        return 1    # don't override MAKEFLAGS.  This will call 'make' without 
any '-j' option
+def detect_number_of_processors_fallback(filename_or_file):
     if sys.platform == 'darwin':
         return sysctl_get_cpu_count('/usr/sbin/sysctl')
     elif sys.platform.startswith('freebsd'):
         return sysctl_get_cpu_count('/sbin/sysctl')
     elif not sys.platform.startswith('linux'):
-        try:
-            import multiprocessing
-            return multiprocessing.cpu_count()
-        except:
             return 1    # try to use cpu_count on other platforms or fallback 
to 1
     try:
         if isinstance(filename_or_file, str):
@@ -28,6 +22,15 @@
     except:
         return 1    # we really don't want to explode here, at worst we have 1
 
+def detect_number_of_processors(filename_or_file='/proc/cpuinfo'):
+    if os.environ.get('MAKEFLAGS'):
+        return 1    # don't override MAKEFLAGS.  This will call 'make' without 
any '-j' option
+    try:
+        import multiprocessing
+        return multiprocessing.cpu_count()
+    except:
+        return detect_number_of_processors_fallback(filename_or_file)
+
 def sysctl_get_cpu_count(cmd, name='hw.ncpu'):
     try:
         proc = subprocess.Popen([cmd, '-n', name], stdout=subprocess.PIPE)
diff --git a/rpython/config/test/test_support.py 
b/rpython/config/test/test_support.py
--- a/rpython/config/test/test_support.py
+++ b/rpython/config/test/test_support.py
@@ -30,6 +30,13 @@
 cache size\t: 4096 KB
 physical id\t: 0
 siblings\t: 4
+
+processor\t: 10
+vendor_id\t: GenuineIntel
+cpu family\t: 6
+model\t\t: 37
+model name\t: Intel(R) Core(TM) i7 CPU       L 620  @ 2.00GHz
+stepping\t: 2
 """
 
 class FakeEnviron:
@@ -43,14 +50,35 @@
     if not sys.platform.startswith('linux'):
         py.test.skip("linux only")
     saved = os.environ
+    # old_cpu_count will be multiprocessing.cpu_count if multiprocessing 
module is available or None if the import fails
     try:
+        import multiprocessing
+        old_cpu_count = multiprocessing.cpu_count
+    except:
+        old_cpu_count = None
+    if old_cpu_count != None: # if multiprocessing module is available
+        # test common behavior
+        assert support.detect_number_of_processors() == 
multiprocessing.cpu_count()
+        # test common behaviour when MAKEFLAGS is set
+        os.environ = FakeEnviron('-j2')
+        assert support.detect_number_of_processors() == 1
+        # create an override for cpu_count that throws an exception in order 
to test the fallback behavior of
+        # support.detect_number_of_processors()
+        def fail_cpu_count():
+            raise Exception("Failure")
+        multiprocessing.cpu_count = fail_cpu_count
+    try:
+        # test fallback behavior (multiprocessing.cpu_count() throwing an 
exception or multiprocessing module
+        # not available)
         os.environ = FakeEnviron(None)
-        assert support.detect_number_of_processors(StringIO(cpuinfo)) == 4
+        assert support.detect_number_of_processors(StringIO(cpuinfo)) == 11
         assert support.detect_number_of_processors('random crap that does not 
exist') == 1
         os.environ = FakeEnviron('-j2')
         assert support.detect_number_of_processors(StringIO(cpuinfo)) == 1
     finally:
         os.environ = saved
+        if old_cpu_count != None:
+            multiprocessing.cpu_count = old_cpu_count
 
 def test_cpuinfo_sysctl():
     if sys.platform != 'darwin' and not sys.platform.startswith('freebsd'):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to