Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: Andrew Savchenko <bircoph@g.o>
Cc: 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 16:04:55
Message-Id: 20161214170436.4385b488.mgorny@gentoo.org
In Reply to: Re: [gentoo-dev] [PATCH 2/3] multiprocessing.eclass: Introduce get_nproc() to get no of CPUs by Andrew Savchenko
1 On Wed, 14 Dec 2016 15:27:25 +0300
2 Andrew Savchenko <bircoph@g.o> wrote:
3
4 > On Tue, 13 Dec 2016 10:36:15 +0100 Michał Górny wrote:
5 > > Introduce get_nproc(), a portable 'nproc' wrapper. It uses either
6 > > 'nproc' or a fallback Python multiprocessing module call to attempt to
7 > > determine the number of available processing units.
8 > >
9 > > This can be used e.g. to determine a safe number of jobs to run when
10 > > MAKEOPTS specifies unlimited --jobs and the build system in question
11 > > does not support --load-average.
12 > > ---
13 > > eclass/multiprocessing.eclass | 25 +++++++++++++++++++++++++
14 > > 1 file changed, 25 insertions(+)
15 > >
16 > > diff --git a/eclass/multiprocessing.eclass b/eclass/multiprocessing.eclass
17 > > index 5a5fe9acb56a..0d241cdc15b6 100644
18 > > --- a/eclass/multiprocessing.eclass
19 > > +++ b/eclass/multiprocessing.eclass
20 > > @@ -53,6 +53,31 @@ bashpid() {
21 > > sh -c 'echo ${PPID}'
22 > > }
23 > >
24 > > +# @FUNCTION: get_nproc
25 > > +# @USAGE: [${fallback:-1}]
26 > > +# @DESCRIPTION:
27 > > +# Attempt to figure out the number of processing units available.
28 > > +# If the value can not be determined, prints the provided fallback
29 > > +# instead. If no fallback is provided, defaults to 1.
30 > > +get_nproc() {
31 > > + local nproc
32 > > +
33 > > + if type -P nproc &>/dev/null; then
34 > > + # GNU
35 > > + nproc=$(nproc)
36 > > + elif type -P python &>/dev/null; then
37 > > + # fallback to python2.6+
38 > > + # note: this may fail (raise NotImplementedError)
39 > > + nproc=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count());' 2>/dev/null)
40 >
41 > This is not portable. E.g. paludis users can have python-less
42 > system. Adding dev-lang/python to DEPEND will be also a bad idea,
43 > since this is quite heavy dependency.
44
45 You can bikeshed potential circumstances where it wouldn't work for
46 the next year. Which doesn't change that it would work quite reliably
47 for the most of Gentoo users.
48
49 > Since on Linux boxes nproc is from coreutils, which is in @system,
50 > so looks like only *bsd setups are the problem. On FreeBSD
51
52 ...and all other operating systems (see: Prefix).
53
54 > sysctl -a | egrep -i 'hw.ncpu'
55
56 I somehow doubt that would give me the expected number only, and I lack
57 a BSD install handy to test it.
58
59 > > + fi
60 > > +
61 > > + if [[ -n ${nproc} ]]; then
62 > > + echo "${nproc}"
63 > > + else
64 > > + echo "${1:-1}"
65 > > + fi
66 > > +}
67 > > +
68 > > # @FUNCTION: makeopts_jobs
69 > > # @USAGE: [${MAKEOPTS}]
70 > > # @DESCRIPTION:
71
72 --
73 Best regards,
74 Michał Górny
75 <http://dev.gentoo.org/~mgorny/>

Replies