summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChandan Singh <csingh43@bloomberg.net>2019-12-31 13:47:41 +0000
committerChandan Singh <csingh43@bloomberg.net>2019-12-31 14:10:35 +0000
commit51f72e9f30c0edf855471ae052bcde62394bc09b (patch)
treefddca2ae598db759f046954c27bcc9799da53e27
parentc8153bc7b7e9fd989ae2a6be3e2dea8aedad0343 (diff)
downloadbuildstream-51f72e9f30c0edf855471ae052bcde62394bc09b.tar.gz
_platform: Don't use psutil.Process.cpu_affinity on unsupported platformschandan/fix-cpu-count
`psutil.Process.cpu_affinity` is not available on many non-Linux platforms, like AIX and Solaris. We already have such a case with Darwin where we need to resort to using `os.cpu_count()`. Rather than adding more subclasses and overrides, add that logic as a fallback in the main `Process` class. `os.cpu_count()` should generally always be available, so hopefully we will only need to override this in future too much or at all. Fixes #1244.
-rw-r--r--src/buildstream/_platform/darwin.py8
-rw-r--r--src/buildstream/_platform/platform.py10
2 files changed, 9 insertions, 9 deletions
diff --git a/src/buildstream/_platform/darwin.py b/src/buildstream/_platform/darwin.py
index 06491e8b4..2e244557e 100644
--- a/src/buildstream/_platform/darwin.py
+++ b/src/buildstream/_platform/darwin.py
@@ -15,7 +15,6 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
-import os
import resource
from ..sandbox import SandboxDummy
@@ -28,13 +27,6 @@ class Darwin(Platform):
# This value comes from OPEN_MAX in syslimits.h
OPEN_MAX = 10240
- def get_cpu_count(self, cap=None):
- cpu_count = os.cpu_count()
- if cap is None:
- return cpu_count
- else:
- return min(cpu_count, cap)
-
def maximize_open_file_limit(self):
# Note that on Mac OSX, you may not be able to simply set the soft
# limit to the reported hard limit, as it may not be the only limit in
diff --git a/src/buildstream/_platform/platform.py b/src/buildstream/_platform/platform.py
index b58ae3e3d..c838ef6c2 100644
--- a/src/buildstream/_platform/platform.py
+++ b/src/buildstream/_platform/platform.py
@@ -122,7 +122,15 @@ class Platform:
return PlatformImpl(force_sandbox=force_sandbox)
def get_cpu_count(self, cap=None):
- cpu_count = len(psutil.Process().cpu_affinity())
+ # `psutil.Process.cpu_affinity()` is not available on all platforms.
+ # So, fallback to getting the total cpu count in cases where it is not
+ # available.
+ dummy_process = psutil.Process()
+ if hasattr(dummy_process, "cpu_affinity"):
+ cpu_count = len(dummy_process.cpu_affinity())
+ else:
+ cpu_count = os.cpu_count()
+
if cap is None:
return cpu_count
else: