30 lines
1.3 KiB
Bash
Executable File
30 lines
1.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Choose a high-throughput but memory-safe default. Callers can override with
|
|
# WEB_BUILD_JOBS; the per-job memory estimate keeps large Blender/Emscripten
|
|
# links from swapping while still using more than the historical -j8 default
|
|
# on hosts with enough RAM.
|
|
if [[ -n "${WEB_BUILD_JOBS:-}" ]]; then
|
|
printf '%s\n' "${WEB_BUILD_JOBS}"
|
|
exit 0
|
|
fi
|
|
|
|
cpu_count="$(getconf _NPROCESSORS_ONLN 2>/dev/null || printf '8')"
|
|
memory_kib="$(awk '/MemAvailable:/ { print $2; exit }' /proc/meminfo 2>/dev/null || printf '0')"
|
|
memory_per_job_gib="${WEB_BUILD_MEMORY_GIB_PER_JOB:-4}"
|
|
job_cap="${WEB_BUILD_JOBS_CAP:-16}"
|
|
|
|
if [[ ! "${cpu_count}" =~ ^[0-9]+$ ]] || ((cpu_count < 1)); then cpu_count=8; fi
|
|
if [[ ! "${memory_kib}" =~ ^[0-9]+$ ]] || ((memory_kib < 1)); then memory_kib=$((8 * 1024 * 1024)); fi
|
|
if [[ ! "${memory_per_job_gib}" =~ ^[0-9]+$ ]] || ((memory_per_job_gib < 1)); then memory_per_job_gib=4; fi
|
|
if [[ ! "${job_cap}" =~ ^[0-9]+$ ]] || ((job_cap < 1)); then job_cap=16; fi
|
|
|
|
memory_jobs=$((memory_kib / (memory_per_job_gib * 1024 * 1024)))
|
|
if ((memory_jobs < 2)); then memory_jobs=2; fi
|
|
jobs=$cpu_count
|
|
if ((jobs > memory_jobs)); then jobs=$memory_jobs; fi
|
|
if ((jobs > job_cap)); then jobs=$job_cap; fi
|
|
if ((jobs < 2)); then jobs=2; fi
|
|
printf '%s\n' "${jobs}"
|