Author: David Schneider <[email protected]>
Branch:
Changeset: r44137:85b1bcec23c5
Date: 2011-05-13 17:30 +0200
http://bitbucket.org/pypy/pypy/changeset/85b1bcec23c5/
Log: detect the number of cpus on mac os
diff --git a/pypy/config/support.py b/pypy/config/support.py
--- a/pypy/config/support.py
+++ b/pypy/config/support.py
@@ -2,13 +2,15 @@
""" Some support code
"""
-import re, sys, os
+import re, sys, os, subprocess
def detect_number_of_processors(filename_or_file='/proc/cpuinfo'):
- if sys.platform != 'linux2':
- return 1 # implement me
if os.environ.get('MAKEFLAGS'):
return 1 # don't override MAKEFLAGS. This will call 'make' without
any '-j' option
+ if sys.platform == 'darwin':
+ return darwin_get_cpu_count()
+ elif sys.platform != 'linux2':
+ return 1 # implement me
try:
if isinstance(filename_or_file, str):
f = open(filename_or_file, "r")
@@ -23,3 +25,12 @@
return count
except:
return 1 # we really don't want to explode here, at worst we have 1
+
+def darwin_get_cpu_count(cmd = "/usr/sbin/sysctl hw.ncpu"):
+ try:
+ proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
+ # 'hw.ncpu: 20'
+ count = proc.communicate()[0].rstrip()[8:]
+ return int(count)
+ except (OSError, ValueError):
+ return 1
diff --git a/pypy/config/test/test_support.py b/pypy/config/test/test_support.py
--- a/pypy/config/test/test_support.py
+++ b/pypy/config/test/test_support.py
@@ -1,6 +1,6 @@
from cStringIO import StringIO
-from pypy.config.support import detect_number_of_processors
+from pypy.config import support
import os, sys, py
cpuinfo = """
@@ -39,15 +39,38 @@
assert varname == 'MAKEFLAGS'
return self._value
-def test_cpuinfo():
+def test_cpuinfo_linux():
if sys.platform != 'linux2':
py.test.skip("linux only")
saved = os.environ
try:
os.environ = FakeEnviron(None)
- assert detect_number_of_processors(StringIO(cpuinfo)) == 3
- assert detect_number_of_processors('random crap that does not exist')
== 1
+ assert support.detect_number_of_processors(StringIO(cpuinfo)) == 3
+ assert support.detect_number_of_processors('random crap that does not
exist') == 1
os.environ = FakeEnviron('-j2')
- assert detect_number_of_processors(StringIO(cpuinfo)) == 1
+ assert support.detect_number_of_processors(StringIO(cpuinfo)) == 1
finally:
os.environ = saved
+
+def test_cpuinfo_darwin():
+ if sys.platform != 'darwin':
+ py.test.skip('mac only')
+ saved_func = support.darwin_get_cpu_count
+ saved = os.environ
+ def count():
+ return 42
+ try:
+ support.darwin_get_cpu_count = count
+ os.environ = FakeEnviron(None)
+ assert support.detect_number_of_processors() == 42
+ os.environ = FakeEnviron('-j2')
+ assert support.detect_number_of_processors() == 1
+ finally:
+ os.environ = saved
+ support.darwin_get_cpu_count = saved_func
+
+def test_darwin_get_cpu_count():
+ if sys.platform != 'darwin':
+ py.test.skip('mac only')
+ assert support.darwin_get_cpu_count() > 0 # hopefully
+ assert support.darwin_get_cpu_count("false") == 1
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit