Panu Matilainen 48ff1f
From fa70eaa852aab38857b3c8663386e8759f121bbe Mon Sep 17 00:00:00 2001
Panu Matilainen 48ff1f
Message-Id: <fa70eaa852aab38857b3c8663386e8759f121bbe.1566904946.git.pmatilai@redhat.com>
Panu Matilainen d678a6
From: Panu Matilainen <pmatilai@redhat.com>
Panu Matilainen d678a6
Date: Fri, 23 Aug 2019 11:17:21 +0300
Panu Matilainen 48ff1f
Subject: [PATCH] Cap number of threads on 32bit platforms, add tunables
Panu Matilainen d678a6
 (RhBug:1729382)
Panu Matilainen d678a6
Panu Matilainen d678a6
On 32bit plaforms, address space is easily exhausted with multiple
Panu Matilainen d678a6
threads, causing arbitrary builds failure regressions (RhBug:1729382).
Panu Matilainen d678a6
Simply cap the number of threads to maximum of four on any 32bit
Panu Matilainen d678a6
platform to play it safe. It's still three more than we were able to
Panu Matilainen d678a6
use on older releases...
Panu Matilainen d678a6
Panu Matilainen 48ff1f
In addition, introduce tunables for controlling the number of threads
Panu Matilainen 48ff1f
used similarly to what is available for number of CPUs: forced number
Panu Matilainen 48ff1f
and a separate ceiling.
Panu Matilainen 48ff1f
Panu Matilainen 48ff1f
The max logic gets surprisingly tricky with the mixed OMP and rpm
Panu Matilainen 48ff1f
tunables and different semantics. Notably, OMP does not have a nice
Panu Matilainen 48ff1f
way to set a maximum from inside a program (this is only possible
Panu Matilainen 48ff1f
from an environment variable so we can't use it here), so we need
Panu Matilainen 48ff1f
to jump through a lot of hoops to figure out whether an *unlimited*
Panu Matilainen 48ff1f
setting would go above the platform limitation. I'm not entirely
Panu Matilainen 48ff1f
convinced this is 100% bulletproof, but then it's all going to change
Panu Matilainen 48ff1f
and become even more complicated when we start taking memory limitations
Panu Matilainen 48ff1f
into account...
Panu Matilainen d678a6
---
Panu Matilainen 48ff1f
 build/parseSpec.c | 20 +++++++++++++++++---
Panu Matilainen 48ff1f
 platform.in       |  5 +++++
Panu Matilainen 48ff1f
 2 files changed, 22 insertions(+), 3 deletions(-)
Panu Matilainen d678a6
Panu Matilainen d678a6
diff --git a/build/parseSpec.c b/build/parseSpec.c
Panu Matilainen 48ff1f
index 737a1233c..ed5c3ef63 100644
Panu Matilainen d678a6
--- a/build/parseSpec.c
Panu Matilainen d678a6
+++ b/build/parseSpec.c
Panu Matilainen 48ff1f
@@ -1035,9 +1035,23 @@ static rpmSpec parseSpec(const char *specFile, rpmSpecFlags flags,
Panu Matilainen d678a6
 
Panu Matilainen d678a6
 #ifdef ENABLE_OPENMP
Panu Matilainen d678a6
     /* Set number of OMP threads centrally */
Panu Matilainen d678a6
-    int ncpus = rpmExpandNumeric("%{?_smp_build_ncpus}");
Panu Matilainen 48ff1f
-    if (ncpus > 0)
Panu Matilainen 48ff1f
-	omp_set_num_threads(ncpus);
Panu Matilainen 48ff1f
+    int nthreads = rpmExpandNumeric("%{?_smp_build_nthreads}");
Panu Matilainen 48ff1f
+    int nthreads_max = rpmExpandNumeric("%{?_smp_nthreads_max}");
Panu Matilainen 48ff1f
+    if (nthreads <= 0)
Panu Matilainen 48ff1f
+        nthreads = omp_get_max_threads();
Panu Matilainen 48ff1f
+    if (nthreads_max > 0 && nthreads > nthreads_max)
Panu Matilainen 48ff1f
+	nthreads = nthreads_max;
Panu Matilainen d678a6
+#if __WORDSIZE == 32
Panu Matilainen d678a6
+    /* On 32bit platforms, address space shortage is an issue. Play safe. */
Panu Matilainen 48ff1f
+    int platlimit = 4;
Panu Matilainen 48ff1f
+    if (nthreads > platlimit) {
Panu Matilainen 48ff1f
+	nthreads = platlimit;
Panu Matilainen d678a6
+	rpmlog(RPMLOG_DEBUG,
Panu Matilainen 48ff1f
+	    "limiting number of threads to %d due to platform\n", platlimit);
Panu Matilainen d678a6
+    }
Panu Matilainen d678a6
+#endif
Panu Matilainen 48ff1f
+    if (nthreads > 0)
Panu Matilainen 48ff1f
+	omp_set_num_threads(nthreads);
Panu Matilainen d678a6
 #endif
Panu Matilainen 48ff1f
 
Panu Matilainen 48ff1f
     if (spec->clean == NULL) {
Panu Matilainen d678a6
diff --git a/platform.in b/platform.in
Panu Matilainen 48ff1f
index db6d2382f..61f5e18a0 100644
Panu Matilainen d678a6
--- a/platform.in
Panu Matilainen d678a6
+++ b/platform.in
Panu Matilainen 48ff1f
@@ -59,6 +59,11 @@
Panu Matilainen d678a6
 
Panu Matilainen d678a6
 %_smp_mflags -j%{_smp_build_ncpus}
Panu Matilainen d678a6
 
Panu Matilainen 48ff1f
+# Maximum number of threads to use when building, 0 for unlimited
Panu Matilainen 48ff1f
+#%_smp_nthreads_max 0
Panu Matilainen 48ff1f
+
Panu Matilainen 48ff1f
+%_smp_build_nthreads %{_smp_build_ncpus}
Panu Matilainen d678a6
+
Panu Matilainen d678a6
 #==============================================================================
Panu Matilainen d678a6
 # ---- Build policy macros.
Panu Matilainen d678a6
 #
Panu Matilainen d678a6
-- 
Panu Matilainen d678a6
2.21.0
Panu Matilainen d678a6