1 |
commit: 00354a2443c1a0f7f64d55daadd52324868b085f |
2 |
Author: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Feb 16 06:06:41 2019 +0000 |
4 |
Commit: Robin H. Johnson <robbat2 <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Feb 16 06:06:41 2019 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=00354a24 |
7 |
|
8 |
cpuinfo: use better available CPU calculation |
9 |
|
10 |
The existing portage.util.cpuinfo.get_cpu_count() behavior is wrong when |
11 |
run in any environment where the cpuset is a subset of online CPUs. |
12 |
|
13 |
The solution recommended by the 'os.cpu_count()' help is to use: |
14 |
len(os.sched_getaffinity(0)) |
15 |
|
16 |
This only works on line, so keep multiprocessing.cpu_count() as a |
17 |
fallback. In newer version of Python, multiprocessing.cpu_count() is a |
18 |
wrapper for os.cpu_count(). |
19 |
|
20 |
Reported-By: Daniel Robbins <drobbins <AT> funtoo.org> |
21 |
Fixes: https://bugs.funtoo.org/browse/FL-6227 |
22 |
Signed-off-by: Robin H. Johnson <robbat2 <AT> gentoo.org> |
23 |
|
24 |
lib/portage/util/cpuinfo.py | 33 +++++++++++++++++++++++++++++++-- |
25 |
1 file changed, 31 insertions(+), 2 deletions(-) |
26 |
|
27 |
diff --git a/lib/portage/util/cpuinfo.py b/lib/portage/util/cpuinfo.py |
28 |
index 669e707b5..9ab1c119d 100644 |
29 |
--- a/lib/portage/util/cpuinfo.py |
30 |
+++ b/lib/portage/util/cpuinfo.py |
31 |
@@ -1,15 +1,44 @@ |
32 |
-# Copyright 2015 Gentoo Foundation |
33 |
+# Copyright 2015-2019 Gentoo Authors |
34 |
# Distributed under the terms of the GNU General Public License v2 |
35 |
|
36 |
__all__ = ['get_cpu_count'] |
37 |
|
38 |
+# Before you set out to change this function, figure out what you're really |
39 |
+# asking: |
40 |
+# |
41 |
+# - How many CPUs exist in this system (e.g. that the kernel is aware of?) |
42 |
+# This is 'getconf _NPROCESSORS_CONF' / get_nprocs_conf(3) |
43 |
+# In modern Linux, implemented by counting CPUs in /sys/devices/system/cpu/ |
44 |
+# |
45 |
+# - How many CPUs in this system are ONLINE right now? |
46 |
+# This is 'getconf _NPROCESSORS_ONLN' / get_nprocs(3) |
47 |
+# In modern Linux, implemented by parsing /sys/devices/system/cpu/online |
48 |
+# |
49 |
+# - How many CPUs are available to this program? |
50 |
+# This is 'nproc' / sched_getaffinity(2), which is implemented in modern |
51 |
+# Linux kernels by querying the kernel scheduler; This might not be available |
52 |
+# in some non-Linux systems! |
53 |
+# |
54 |
+# - How many CPUs are available to this thread? |
55 |
+# This is pthread_getaffinity_np(3) |
56 |
+# |
57 |
+# As a further warning, the results returned by this function can differ |
58 |
+# between runs, if altered by the scheduler or other external factors. |
59 |
|
60 |
def get_cpu_count(): |
61 |
""" |
62 |
- Try to obtain the number of CPUs available. |
63 |
+ Try to obtain the number of CPUs available to this process. |
64 |
|
65 |
@return: Number of CPUs or None if unable to obtain. |
66 |
""" |
67 |
+ try: |
68 |
+ import os |
69 |
+ # This was introduced in Python 3.3 only, but exists in Linux |
70 |
+ # all the way back to the 2.5.8 kernel. |
71 |
+ # This NOT available in FreeBSD! |
72 |
+ return len(os.sched_getaffinity(0)) |
73 |
+ except (ImportError, NotImplementedError, AttributeError): |
74 |
+ pass |
75 |
|
76 |
try: |
77 |
import multiprocessing |