diff options
author | Michał Górny <mgorny@gentoo.org> | 2016-12-13 10:08:22 +0100 |
---|---|---|
committer | Michał Górny <mgorny@gentoo.org> | 2016-12-18 14:46:42 +0100 |
commit | d9cde86b8e01a04b09c3cc7fb328d92428594828 (patch) | |
tree | 92ec1e06b3633a7f201e8262b143bd92b3291ef6 /eclass | |
parent | multiprocessing.eclass: Fix handling multiple short options (e.g. -kj) (diff) | |
download | gentoo-d9cde86b8e01a04b09c3cc7fb328d92428594828.tar.gz gentoo-d9cde86b8e01a04b09c3cc7fb328d92428594828.tar.bz2 gentoo-d9cde86b8e01a04b09c3cc7fb328d92428594828.zip |
multiprocessing.eclass: Introduce get_nproc() to get no of CPUs
Introduce get_nproc(), a portable 'nproc' wrapper. It uses either
'nproc' or a fallback Python multiprocessing module call to attempt to
determine the number of available processing units.
This can be used e.g. to determine a safe number of jobs to run when
MAKEOPTS specifies unlimited --jobs and the build system in question
does not support --load-average.
Diffstat (limited to 'eclass')
-rw-r--r-- | eclass/multiprocessing.eclass | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/eclass/multiprocessing.eclass b/eclass/multiprocessing.eclass index 5a5fe9acb56a..3c5dfff2d11c 100644 --- a/eclass/multiprocessing.eclass +++ b/eclass/multiprocessing.eclass @@ -53,6 +53,38 @@ bashpid() { sh -c 'echo ${PPID}' } +# @FUNCTION: get_nproc +# @USAGE: [${fallback:-1}] +# @DESCRIPTION: +# Attempt to figure out the number of processing units available. +# If the value can not be determined, prints the provided fallback +# instead. If no fallback is provided, defaults to 1. +get_nproc() { + local nproc + + # GNU + if type -P nproc &>/dev/null; then + nproc=$(nproc) + fi + + # BSD + if [[ -z ${nproc} ]] && type -P sysctl &>/dev/null; then + nproc=$(sysctl -n hw.ncpu 2>/dev/null) + fi + + # fallback to python2.6+ + # note: this may fail (raise NotImplementedError) + if [[ -z ${nproc} ]] && type -P python &>/dev/null; then + nproc=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count());' 2>/dev/null) + fi + + if [[ -n ${nproc} ]]; then + echo "${nproc}" + else + echo "${1:-1}" + fi +} + # @FUNCTION: makeopts_jobs # @USAGE: [${MAKEOPTS}] # @DESCRIPTION: |