From 2047aa1853a7924ebdb5b34d35b3bf00f27ec499 Mon Sep 17 00:00:00 2001 From: Anthony Ettinger Date: Mon, 30 Nov 2026 14:23:53 +0000 Subject: [PATCH 1/2] build: cap engine jobs by available RAM Cargo's NUM_JOBS is the CPU count (32 here); 32 concurrent cc1plus on the engine sources OOM-kills the compiler ("Terminated signal terminated program cc1plus"). Default to one job per 2 GB of MemAvailable, capped at NUM_JOBS; SK33P_JOBS still overrides. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +++- build.rs | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3c0b9a6..25650d7 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,9 @@ cd sk33p && cargo build --release `build.rs` runs the engine's own CMake superbuild (which fetches RapidJSON, RapidXML, pugixml and libzip), compiles the C ABI shim in `shim/`, and links it all statically. `patches/` holds two small engine fixes applied at build time. -The engine is a heavy C++ compile: set `SK33P_JOBS=1` on a small machine. +The engine is a heavy C++ compile: the build runs one job per 2 GB of available +RAM (capped at the CPU count) so cc1plus is not OOM-killed; set `SK33P_JOBS=N` +to override. ## License diff --git a/build.rs b/build.rs index 66412b1..02550af 100644 --- a/build.rs +++ b/build.rs @@ -14,6 +14,25 @@ fn run(cmd: &mut Command) { assert!(status.success(), "command failed: {cmd:?}"); } +/// Cargo's NUM_JOBS is the CPU count, but an engine translation unit can take +/// 1-2 GB in cc1plus, so 32 at once gets OOM-killed ("Terminated signal +/// terminated program cc1plus"). Cap at one job per 2 GB of MemAvailable. +fn default_jobs() -> usize { + let cpus: usize = env::var("NUM_JOBS").ok().and_then(|v| v.parse().ok()).unwrap_or(4); + let avail_kb: Option = std::fs::read_to_string("/proc/meminfo").ok().and_then(|m| { + m.lines() + .find(|l| l.starts_with("MemAvailable:")) + .and_then(|l| l.split_whitespace().nth(1)) + .and_then(|v| v.parse().ok()) + }); + let jobs = match avail_kb { + Some(kb) => cpus.min(kb / (2 * 1024 * 1024)).max(1), + None => cpus, + }; + println!("cargo:warning=sk33p: building engine with {jobs} jobs (set SK33P_JOBS to override)"); + jobs +} + fn main() { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); let engine = root.join("engine"); @@ -51,7 +70,7 @@ fn main() { // 2. engine superbuild. GCC 15 no longer pulls in transitively, // and the engine uses the 4-argument std::lower_bound without including it. - let jobs = env::var("SK33P_JOBS").unwrap_or_else(|_| env::var("NUM_JOBS").unwrap_or("4".into())); + let jobs = env::var("SK33P_JOBS").unwrap_or_else(|_| default_jobs().to_string()); let msvc = env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc"); let force = if msvc { "/FIalgorithm" } else { "-include algorithm" }; let build_dir = out.join("engine-build"); From 6da1f9afd930b8143a3e2ea8ab52f4fc90b300b4 Mon Sep 17 00:00:00 2001 From: Anthony Ettinger Date: Mon, 30 Nov 2026 14:27:23 +0000 Subject: [PATCH 2/2] build: one engine job per 4 GB available, at most 4 8 jobs still tripped earlyoom on a shared box (each cc1plus is ~350 MB, but other services spike). 4 jobs builds clean in 43s. Co-Authored-By: Claude Opus 5.5 --- README.md | 5 ++--- build.rs | 8 +++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 25650d7..a9242ea 100644 --- a/README.md +++ b/README.md @@ -55,9 +55,8 @@ cd sk33p && cargo build --release `build.rs` runs the engine's own CMake superbuild (which fetches RapidJSON, RapidXML, pugixml and libzip), compiles the C ABI shim in `shim/`, and links it all statically. `patches/` holds two small engine fixes applied at build time. -The engine is a heavy C++ compile: the build runs one job per 2 GB of available -RAM (capped at the CPU count) so cc1plus is not OOM-killed; set `SK33P_JOBS=N` -to override. +The engine is a heavy C++ compile: the build runs one job per 4 GB of available +RAM (at most 4) so cc1plus is not OOM-killed; set `SK33P_JOBS=N` to override. ## License diff --git a/build.rs b/build.rs index 02550af..4768e7c 100644 --- a/build.rs +++ b/build.rs @@ -16,7 +16,9 @@ fn run(cmd: &mut Command) { /// Cargo's NUM_JOBS is the CPU count, but an engine translation unit can take /// 1-2 GB in cc1plus, so 32 at once gets OOM-killed ("Terminated signal -/// terminated program cc1plus"). Cap at one job per 2 GB of MemAvailable. +/// terminated program cc1plus", the SIGTERM coming from earlyoom). Each job is +/// only ~350 MB, but on a shared box other services spike, so stay well clear: +/// one job per 4 GB of MemAvailable, at most 4. fn default_jobs() -> usize { let cpus: usize = env::var("NUM_JOBS").ok().and_then(|v| v.parse().ok()).unwrap_or(4); let avail_kb: Option = std::fs::read_to_string("/proc/meminfo").ok().and_then(|m| { @@ -26,8 +28,8 @@ fn default_jobs() -> usize { .and_then(|v| v.parse().ok()) }); let jobs = match avail_kb { - Some(kb) => cpus.min(kb / (2 * 1024 * 1024)).max(1), - None => cpus, + Some(kb) => cpus.min(4).min(kb / (4 * 1024 * 1024)).max(1), + None => cpus.min(4), }; println!("cargo:warning=sk33p: building engine with {jobs} jobs (set SK33P_JOBS to override)"); jobs