#!/bin/sh12# Source this to set CPUTHREADS (the number of apparent cores) and RAMTHREADS3# (free RAM divided by the maximum amount needed per thread typically)4# From this this script infers reasonable defaults for SAGE_NUM_THREADS and5# MAKEFLAGS.67# We do exactly the same for CPUTHREADS_DOCBUILD, RAMTHREADS_DOCBUILD,8# SAGE_NUM_THREADS_DOCBUILD, MAKEFLAGS_DOCBUILD. As the docbuild needs9# substantially more RAM as of May 2018.1011# ****************************************************************************12# Copyright (C) 2018 Julian Rüth <[email protected]>13#14# This program is free software: you can redistribute it and/or modify15# it under the terms of the GNU General Public License as published by16# the Free Software Foundation, either version 2 of the License, or17# (at your option) any later version.18# http://www.gnu.org/licenses/19# ****************************************************************************2021set -ex2223if [ -z "$CPUTHREADS" ]; then24# Determine the number of threads that can run simultaneously on this system25# (we might not have nproc available.)26# Note that this value is incorrect for some CI providers (notably CircleCI:27# https://circleci.com/docs/2.0/configuration-reference/#resource_class) which28# provision fewer vCPUs than shown in /proc/cpuinfo. So it is probably better29# to set CPUTHREADS manuall in your CI configuration.30CPUTHREADS=`docker run docker cat /proc/cpuinfo | grep -E '^processor' | wc -l`31fi32if [ -z "$CPUTHREADS_DOCBUILD" ]; then33CPUTHREADS_DOCBUILD=$CPUTHREADS34fi3536if [ -z "$RAMTHREADS" ]; then37RAMTHREADS=$(( `docker run docker cat /proc/meminfo | grep MemTotal | awk '{ print $2 }'` / 1048576 ))38if [ $RAMTHREADS = 0 ];then39RAMTHREADS=1;40fi41fi42if [ -z "$RAMTHREADS_DOCBUILD" ]; then43RAMTHREADS_DOCBUILD=$(( `docker run docker cat /proc/meminfo | grep MemTotal | awk '{ print $2 }'` / 2097152 ))44if [ $RAMTHREADS_DOCBUILD = 0 ];then45RAMTHREADS_DOCBUILD=1;46fi47fi4849# On CI machines with their virtual CPUs, it seems to be quite beneficial to50# overcommit on CPU usage. We only need to make sure that we do not exceed RAM51# (as there is no swap.)52if [ $CPUTHREADS -lt $RAMTHREADS ]; then53export SAGE_NUM_THREADS=$((CPUTHREADS + 1))54else55export SAGE_NUM_THREADS=$RAMTHREADS56fi57if [ $CPUTHREADS_DOCBUILD -lt $RAMTHREADS_DOCBUILD ]; then58export SAGE_NUM_THREADS_DOCBUILD=$((CPUTHREADS_DOCBUILD + 1))59else60export SAGE_NUM_THREADS_DOCBUILD=$RAMTHREADS_DOCBUILD61fi62# Set -j and -l for make (though -l is probably ignored by Sage)63export MAKEFLAGS="-j $SAGE_NUM_THREADS -l $((CPUTHREADS - 1)).8"64export MAKEFLAGS_DOCBUILD="-j $SAGE_NUM_THREADS_DOCBUILD -l $((CPUTHREADS_DOCBUILD - 1)).8"656667