Gentoo Archives: gentoo-dev

From: Andrew Savchenko <bircoph@g.o>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH 2/3] multiprocessing.eclass: Introduce get_nproc() to get no of CPUs
Date: Wed, 14 Dec 2016 12:27:41
Message-Id: 20161214152725.4858120ba6285a393d539cd1@gentoo.org
In Reply to: [gentoo-dev] [PATCH 2/3] multiprocessing.eclass: Introduce get_nproc() to get no of CPUs by "Michał Górny"
1 On Tue, 13 Dec 2016 10:36:15 +0100 Michał Górny wrote:
2 > Introduce get_nproc(), a portable 'nproc' wrapper. It uses either
3 > 'nproc' or a fallback Python multiprocessing module call to attempt to
4 > determine the number of available processing units.
5 >
6 > This can be used e.g. to determine a safe number of jobs to run when
7 > MAKEOPTS specifies unlimited --jobs and the build system in question
8 > does not support --load-average.
9 > ---
10 > eclass/multiprocessing.eclass | 25 +++++++++++++++++++++++++
11 > 1 file changed, 25 insertions(+)
12 >
13 > diff --git a/eclass/multiprocessing.eclass b/eclass/multiprocessing.eclass
14 > index 5a5fe9acb56a..0d241cdc15b6 100644
15 > --- a/eclass/multiprocessing.eclass
16 > +++ b/eclass/multiprocessing.eclass
17 > @@ -53,6 +53,31 @@ bashpid() {
18 > sh -c 'echo ${PPID}'
19 > }
20 >
21 > +# @FUNCTION: get_nproc
22 > +# @USAGE: [${fallback:-1}]
23 > +# @DESCRIPTION:
24 > +# Attempt to figure out the number of processing units available.
25 > +# If the value can not be determined, prints the provided fallback
26 > +# instead. If no fallback is provided, defaults to 1.
27 > +get_nproc() {
28 > + local nproc
29 > +
30 > + if type -P nproc &>/dev/null; then
31 > + # GNU
32 > + nproc=$(nproc)
33 > + elif type -P python &>/dev/null; then
34 > + # fallback to python2.6+
35 > + # note: this may fail (raise NotImplementedError)
36 > + nproc=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count());' 2>/dev/null)
37
38 This is not portable. E.g. paludis users can have python-less
39 system. Adding dev-lang/python to DEPEND will be also a bad idea,
40 since this is quite heavy dependency.
41
42 Since on Linux boxes nproc is from coreutils, which is in @system,
43 so looks like only *bsd setups are the problem. On FreeBSD
44 sysctl -a | egrep -i 'hw.ncpu'
45 should be sufficient solution.
46
47 > + fi
48 > +
49 > + if [[ -n ${nproc} ]]; then
50 > + echo "${nproc}"
51 > + else
52 > + echo "${1:-1}"
53 > + fi
54 > +}
55 > +
56 > # @FUNCTION: makeopts_jobs
57 > # @USAGE: [${MAKEOPTS}]
58 > # @DESCRIPTION:
59
60
61 Best regards,
62 Andrew Savchenko

Replies