[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gentoo-dev
Subject:    [gentoo-dev] [PATCH v2] multiprocessing.eclass: Default makeopts_jobs to inf=nproc+1
From:       Michał Górny <mgorny () gentoo ! org>
Date:       2022-04-28 13:35:10
Message-ID: 20220428133510.17866-1-mgorny () gentoo ! org
[Download RAW message or body]

Change the default value for 'inf' argument to makeopts_jobs from 999
to $(get_nproc) + 1.  This means that if MAKEOPTS specifies a `-j`
argument without a specific value, nproc will be used rather than
infinity-ish number of jobs.

The old default made sense for ebuilds using both makeopts_jobs
and makeopts_loadavg.  However, these are very rare — only 4 packages
and 3 eclass at this time.  For the remaining ebuilds, they meant
uncontrollably using up to 999 jobs.

The new default is both safer and more correct for the vast majority
of Gentoo packages, removing the necessity of repeating:

    $(makeopts_jobs "${MAKEOPTS}" "$(get_nproc)")

The ebuilds and eclasses using makeopts_loadavg have been updated
to pass the old default.

Signed-off-by: Michał Górny <mgorny@gentoo.org>
---
 eclass/multiprocessing.eclass                 |  9 ++++-----
 eclass/tests/multiprocessing_makeopts_jobs.sh | 15 ++++++++++-----
 2 files changed, 14 insertions(+), 10 deletions(-)

Changes in v2:
- changed the default to nproc+1 as suggested by Flow, thanks!

diff --git a/eclass/multiprocessing.eclass b/eclass/multiprocessing.eclass
index c32bfaac2e6b..b235c74a5baa 100644
--- a/eclass/multiprocessing.eclass
+++ b/eclass/multiprocessing.eclass
@@ -65,22 +65,21 @@ get_nproc() {
 }
 
 # @FUNCTION: makeopts_jobs
-# @USAGE: [${MAKEOPTS}] [${inf:-999}]
+# @USAGE: [${MAKEOPTS}] [${inf:-$(( $(get_nproc) + 1 ))}]
 # @DESCRIPTION:
 # Searches the arguments (defaults to ${MAKEOPTS}) and extracts the jobs number
 # specified therein.  Useful for running non-make tools in parallel too.
 # i.e. if the user has MAKEOPTS=-j9, this will echo "9" -- we can't return the
 # number as bash normalizes it to [0, 255].  If the flags haven't specified a
-# -j flag, then "1" is shown as that is the default `make` uses.  Since there's
-# no way to represent infinity, we return ${inf} (defaults to 999) if the user
-# has -j without a number.
+# -j flag, then "1" is shown as that is the default `make` uses.  If the flags
+# specify -j without a number, ${inf} is returned (defaults to nproc).
 makeopts_jobs() {
 	[[ $# -eq 0 ]] && set -- "${MAKEOPTS}"
 	# This assumes the first .* will be more greedy than the second .*
 	# since POSIX doesn't specify a non-greedy match (i.e. ".*?").
 	local jobs=$(echo " $* " | sed -r -n \
 		-e 's:.*[[:space:]](-[a-z]*j|--jobs[=[:space:]])[[:space:]]*([0-9]+).*:\2:p' \
-		-e "s:.*[[:space:]](-[a-z]*j|--jobs)[[:space:]].*:${2:-999}:p")
+		-e "s:.*[[:space:]](-[a-z]*j|--jobs)[[:space:]].*:${2:-$(( $(get_nproc) + 1 \
))}:p")  echo ${jobs:-1}
 }
 
diff --git a/eclass/tests/multiprocessing_makeopts_jobs.sh \
b/eclass/tests/multiprocessing_makeopts_jobs.sh index 70a6085d5362..37d5a7257775 \
                100755
--- a/eclass/tests/multiprocessing_makeopts_jobs.sh
+++ b/eclass/tests/multiprocessing_makeopts_jobs.sh
@@ -16,14 +16,19 @@ test-makeopts_jobs() {
 		tend 1 "Mismatch between MAKEOPTS/cli: '${indirect}' != '${direct}'"
 	else
 		[[ ${direct} == "${exp}" ]]
-		tend $? "Got back: ${act}"
+		tend $? "Got back: ${direct}"
 	fi
 }
 
+# override to avoid relying on a specific value
+get_nproc() {
+	echo 41
+}
+
 tests=(
-	999 "-j"
-	999 "--jobs"
-	999 "-j -l9"
+	42 "-j"
+	42 "--jobs"
+	42 "-j -l9"
 	1 ""
 	1 "-l9 -w"
 	1 "-l9 -w-j4"
@@ -37,7 +42,7 @@ tests=(
 	7 "-l3 --jobs 7 -w"
 	4 "-j1 -j 2 --jobs 3 --jobs=4"
 	8 "     -j        			8     "
-	999 "-kj"
+	42 "-kj"
 	4 "-kj4"
 	5 "-kj 5"
 )
-- 
2.35.1


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic