diff --git a/memory-constraints.macros b/memory-constraints.macros index 8cbd51e..5f6ec13 100644 --- a/memory-constraints.macros +++ b/memory-constraints.macros @@ -4,3 +4,23 @@ # crazy magic in order to make sure it is most of the time building # rather than just giving you OOM # + +# m: memory limit in MBs per core; default is 1000 +%limit_build(m:) \ + _threads="`/usr/bin/getconf _NPROCESSORS_ONLN`" + _core_memory="%{-m:%{-m*}}%{!-m:1000}" + echo "Available memory:" \ + cat /proc/meminfo \ + echo "System limits:" \ + ulimit -a \ + echo "System jobs: $_threads" \ + if test "$_threads" -gt 1 ; then \ + mem_per_process="$_core_memory" \ + max_mem=$(awk '/MemTotal/ { print $2 }' /proc/meminfo) \ + max_jobs="$(($max_mem / $mem_per_process))" + test "$_threads" -gt "$max_jobs" && $_threads="$max_jobs" && echo "Warning: Reducing number of jobs to $max_jobs because of memory limits" \ + test "$_threads" -le 0 && $_threads=1 && echo "Warning: Do not use the parallel build at all becuse of memory limits" \ + fi \ + %global jobs $_threads + %global _smp_mflags -j$_threads + %{nil} diff --git a/memory-constraints.spec b/memory-constraints.spec index 45ea297..5638594 100644 --- a/memory-constraints.spec +++ b/memory-constraints.spec @@ -25,6 +25,7 @@ Group: Development/Tools/Building URL: https://www.opensuse.org/ Source0: %{name}.macros BuildRequires: rpm +Requires: awk BuildArch: noarch %description