1 # Copyright 1999-2017 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: multiprocessing.eclass
6 # base-system@gentoo.org
8 # Brian Harring <ferringb@gentoo.org>
9 # Mike Frysinger <vapier@gentoo.org>
10 # @BLURB: multiprocessing helper functions
12 # The multiprocessing eclass contains a suite of utility functions
13 # that could be helpful to controlling parallel multiple job execution.
14 # The most common use is processing MAKEOPTS in order to obtain job
21 # # custom build system that does not support most of MAKEOPTS
22 # ./mybs -j$(makeopts_jobs)
26 if [[ -z ${_MULTIPROCESSING_ECLASS} ]]; then
27 _MULTIPROCESSING_ECLASS=1
29 # @FUNCTION: get_nproc
30 # @USAGE: [${fallback:-1}]
32 # Attempt to figure out the number of processing units available.
33 # If the value can not be determined, prints the provided fallback
34 # instead. If no fallback is provided, defaults to 1.
39 if type -P nproc &>/dev/null; then
44 if [[ -z ${nproc} ]] && type -P sysctl &>/dev/null; then
45 nproc=$(sysctl -n hw.ncpu 2>/dev/null)
48 # fallback to python2.6+
49 # note: this may fail (raise NotImplementedError)
50 if [[ -z ${nproc} ]] && type -P python &>/dev/null; then
51 nproc=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count());' 2>/dev/null)
54 if [[ -n ${nproc} ]]; then
61 # @FUNCTION: makeopts_jobs
62 # @USAGE: [${MAKEOPTS}] [${inf:-999}]
64 # Searches the arguments (defaults to ${MAKEOPTS}) and extracts the jobs number
65 # specified therein. Useful for running non-make tools in parallel too.
66 # i.e. if the user has MAKEOPTS=-j9, this will echo "9" -- we can't return the
67 # number as bash normalizes it to [0, 255]. If the flags haven't specified a
68 # -j flag, then "1" is shown as that is the default `make` uses. Since there's
69 # no way to represent infinity, we return ${inf} (defaults to 999) if the user
70 # has -j without a number.
72 [[ $# -eq 0 ]] && set -- "${MAKEOPTS}"
73 # This assumes the first .* will be more greedy than the second .*
74 # since POSIX doesn't specify a non-greedy match (i.e. ".*?").
75 local jobs=$(echo " $* " | sed -r -n \
76 -e 's:.*[[:space:]](-[a-z]*j|--jobs[=[:space:]])[[:space:]]*([0-9]+).*:\2:p' \
77 -e "s:.*[[:space:]](-[a-z]*j|--jobs)[[:space:]].*:${2:-999}:p")
81 # @FUNCTION: makeopts_loadavg
82 # @USAGE: [${MAKEOPTS}] [${inf:-999}]
84 # Searches the arguments (defaults to ${MAKEOPTS}) and extracts the value set
85 # for load-average. For make and ninja based builds this will mean new jobs are
86 # not only limited by the jobs-value, but also by the current load - which might
87 # get excessive due to I/O and not just due to CPU load.
88 # Be aware that the returned number might be a floating-point number. Test
89 # whether your software supports that.
90 # If no limit is specified or --load-average is used without a number, ${inf}
91 # (defaults to 999) is returned.
93 [[ $# -eq 0 ]] && set -- "${MAKEOPTS}"
94 # This assumes the first .* will be more greedy than the second .*
95 # since POSIX doesn't specify a non-greedy match (i.e. ".*?").
96 local lavg=$(echo " $* " | sed -r -n \
97 -e 's:.*[[:space:]](-[a-z]*l|--(load-average|max-load)[=[:space:]])[[:space:]]*([0-9]+(\.[0-9]+)?)[[:space:]].*:\3:p' \
98 -e "s:.*[[:space:]](-[a-z]*l|--(load-average|max-load))[[:space:]].*:${2:-999}:p")
99 # Default to ${inf} since the default is to not use a load limit.
100 echo ${lavg:-${2:-999}}