Gentoo Archives: gentoo-dev

From: Sam James <sam@g.o>
To: gentoo-dev@l.g.o
Cc: qa@g.o, Sam James <sam@g.o>
Subject: [gentoo-dev] [PATCH] check-reqs.eclass: clamp MAKEOPTS for memory/RAM usage
Date: Tue, 04 Jan 2022 22:58:45
Message-Id: 20220104225817.2341950-1-sam@gentoo.org
1 Crank down MAKEOPTS jobs if MAKEOPTS="-jN" is too high for the
2 amount of RAM available (uses amount declared as needed
3 in the ebuild). Typically should be ~2GB per job.
4
5 Bug: https://bugs.gentoo.org/570534
6 Signed-off-by: Sam James <sam@g.o>
7 ---
8 eclass/check-reqs.eclass | 42 +++++++++++++++++++++++++++++++++++++---
9 1 file changed, 39 insertions(+), 3 deletions(-)
10
11 diff --git a/eclass/check-reqs.eclass b/eclass/check-reqs.eclass
12 index 2130e2e349141..8c4adc8b4f121 100644
13 --- a/eclass/check-reqs.eclass
14 +++ b/eclass/check-reqs.eclass
15 @@ -43,6 +43,8 @@ case ${EAPI} in
16 *) die "${ECLASS}: EAPI=${EAPI:-0} is not supported" ;;
17 esac
18
19 +inherit multiprocessing
20 +
21 EXPORT_FUNCTIONS pkg_pretend pkg_setup
22
23 if [[ ! ${_CHECK_REQS_ECLASS} ]]; then
24 @@ -53,6 +55,13 @@ _CHECK_REQS_ECLASS=1
25 # @DESCRIPTION:
26 # How much RAM is needed? Eg.: CHECKREQS_MEMORY=15M
27
28 +# @ECLASS-VARIABLE: CHECKREQS_MEMORY_MANGLE_JOBS
29 +# @USER_VARIABLE
30 +# @DESCRIPTION:
31 +# Allow packages to reduce the number of multiprocessing (e.g. make, ninja) jobs
32 +# to lower memory usage.
33 +: ${CHECKREQS_MEMORY_MANGLE_JOBS=yes}
34 +
35 # @ECLASS-VARIABLE: CHECKREQS_DISK_BUILD
36 # @DEFAULT_UNSET
37 # @DESCRIPTION:
38 @@ -346,9 +355,36 @@ _check-reqs_memory() {
39 eend 0
40 else
41 eend 1
42 - _check-reqs_unsatisfied \
43 - ${size} \
44 - "RAM"
45 +
46 + # Has the user allowed us to mangle their MAKEOPTS?
47 + if [[ ${CHECKREQS_MEMORY_MANGLE_JOBS} == "yes" ]] ; then
48 + local jobs=$(makeopts_jobs)
49 +
50 + local estimated_max_memory=$((${actual_memory}/$(_check-reqs_get_kibibytes 1G)))
51 + if [[ $((jobs*2)) -gt ${estimated_max_memory} ]] ; then
52 + # Number of jobs exceeds RAM/2GB, so clamp it.
53 + local new_jobs=$(($(_check-reqs_get_number ${estimated_max_memory}G)*10/20))
54 +
55 + # This might _still_ be too big on small machines. Give up in such cases.
56 + # (Users can still set the do nothing variable which is independent of this.)
57 + if [[ $((new_jobs*2)) -gt ${estimated_max_memory} ]] ; then
58 + _check-reqs_unsatisfied \
59 + ${size} \
60 + "RAM"
61 + else
62 + # The clamped jobs seem to be enough to satisfy the check-reqs requirement from the ebuild.
63 + ewarn "Clamping MAKEOPTS jobs to -j${new_jobs} to reduce memory usage"
64 + ewarn "Compiler jobs may use around ~2GB each: https://wiki.gentoo.org/wiki/MAKEOPTS"
65 + ewarn "To disable this, set CHECKREQS_MEMORY_MANGLE_JOBS=no."
66 +
67 + MAKEOPTS+=" -j${new_jobs}"
68 + fi
69 + fi
70 + else
71 + _check-reqs_unsatisfied \
72 + ${size} \
73 + "RAM"
74 + fi
75 fi
76 else
77 eend 1
78 --
79 2.34.1

Replies