From 553f49eed27e9526f7e65856733a7f4ac0a807c3 Mon Sep 17 00:00:00 2001 From: Tully Foote Date: Wed, 28 Oct 2009 23:54:32 +0000 Subject: [PATCH] more robust cpu detection --- tools/rosmake/src/rosmake/parallel_build.py | 27 ++++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tools/rosmake/src/rosmake/parallel_build.py b/tools/rosmake/src/rosmake/parallel_build.py index 59803179..6a1c64e1 100644 --- a/tools/rosmake/src/rosmake/parallel_build.py +++ b/tools/rosmake/src/rosmake/parallel_build.py @@ -47,15 +47,24 @@ import threading def num_cpus(): - n = 0 - cpuinfo_file = "/proc/cpuinfo" - if os.path.exists(cpuinfo_file): - for line in open(cpuinfo_file): - if line.startswith("processor"): - n = n + 1 - else: - n = 1 - return n + """ + Detects the number of CPUs on a system. Cribbed from pp. + """ + # Linux, Unix and MacOS: + if hasattr(os, "sysconf"): + if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"): + # Linux & Unix: + ncpus = os.sysconf("SC_NPROCESSORS_ONLN") + if isinstance(ncpus, int) and ncpus > 0: + return ncpus + else: # OSX: + return int(os.popen2("sysctl -n hw.ncpu")[1].read()) + # Windows: + if os.environ.has_key("NUMBER_OF_PROCESSORS"): + ncpus = int(os.environ["NUMBER_OF_PROCESSORS"]); + if ncpus > 0: + return ncpus + return 1 # Default class DependencyTracker: """ Track dependencies between packages. This is basically a