diff mbox series

[1/1] utils: better estimate number of available cpus

Message ID 20240313100645.588421-1-felix.moessbauer@siemens.com
State Accepted, archived
Commit a029bfe96c6542f178720c72a772b7ede9898118
Headers show
Series [1/1] utils: better estimate number of available cpus | expand

Commit Message

Felix Moessbauer March 13, 2024, 10:06 a.m. UTC
When running in a cgroup which is limited to a subset of cpus (via
cpuset.cpus), cpu_count() should return the number of cpus that can be
used instead of the number of cpus the system has.

This also aligns the semantics with nproc.

Signed-off-by: Felix Moessbauer <felix.moessbauer@siemens.com>
---
For a more detailed explanation about the difference, please also see
https://github.com/python/cpython/issues/77167

Best regards,
Felix Moessbauer
Siemens AG

 lib/bb/utils.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index ad13d043..ebee65d3 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -1142,7 +1142,10 @@  def get_referenced_vars(start_expr, d):
 
 
 def cpu_count():
-    return multiprocessing.cpu_count()
+    try:
+        return len(os.sched_getaffinity(0))
+    except OSError:
+        return multiprocessing.cpu_count()
 
 def nonblockingfd(fd):
     fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)