Gentoo Archives: gentoo-portage-dev

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

Replies