forge
Explore
Log in

Don't you dare go Hollow

The swap share of a runner's memory budget is swap again: a job without memory may hold the RAM part and swap up to the share, where before the whole budget was its RAM limit with swap off, so a build bigger than RAM was OOM-killed even with swap on the host

Browse files at this commit

a574fbf7bed590559c572b5ce37d6f7fe7941102Ahmet Kaan GümüşParent: ed4cd43
  • fix
    • the swap share of a runner's memory budget is swap again: a job without memory may hold the RAM part and swap up to the share, where before the whole budget was its RAM limit with swap off, so a build bigger than RAM was OOM-killed even with swap on the host
Show full message
🐛 fix: the swap share of a runner's memory budget is swap again: a job without memory may hold the RAM part and swap up to the share, where before the whole budget was its RAM limit with swap off, so a build bigger than RAM was OOM-killed even with swap on the host
crates/forge-runner/src/cli.rsmodified+1 -1
@@ -242,7 +242,7 @@ async fn run(cli: Cli) -> Result<ExitCode, Box<dyn std::error::Error>> {
242242 // use all of it, so an undeclared job's cgroup limit is the box budget.
243243 let pressure = match cfg.executor {
244244 ExecutorKind::Builtin => {
245- let budget = crate::memory::MemoryGate::for_host(&cfg.limits).budget();
245+ let budget = crate::memory::MemoryGate::for_host(&cfg.limits).ram_budget();
246246 let pressure = Pressure::start(budget, shutdown.clone()).await;
247247 if let Some(p) = &pressure {
248248 cfg.limits.memory = p.budget().to_string();
crates/forge-runner/src/config.rsmodified+9 -3
@@ -59,6 +59,10 @@ pub struct Limits {
5959 /// Percent of the host's swap that counts toward the `auto` budget. 0 means swap is ignored.
6060 #[serde(default)]
6161 pub budget_swap_percent: u32,
62+ /// Swap a job may use on top of `memory`, in bytes. Set per job by the runner, never read
63+ /// from `runner.toml`.
64+ #[serde(skip)]
65+ pub swap: u64,
6266 }
6367
6468 fn default_reserve_memory() -> String {
@@ -81,6 +85,7 @@ impl Default for Limits {
8185 memory_budget: None,
8286 memory_headroom: default_memory_headroom(),
8387 budget_swap_percent: 0,
88+ swap: 0,
8489 }
8590 }
8691 }
@@ -106,12 +111,13 @@ impl Limits {
106111 /// [`Limits::for_job`] for the builtin executor, where every job sits in the runner's box: a
107112 /// job without `memory` may use the whole budget (docs/ci.md, Memory), never less than the
108113 /// default reservation. The box and the pressure controller keep jobs together inside it.
109- pub fn for_builtin_job(&self, declared: Option<u64>, budget: Option<u64>) -> Limits {
114+ pub fn for_builtin_job(&self, declared: Option<u64>, budget: Option<u64>, swap: u64) -> Limits {
110115 match (declared, budget) {
111116 (None, Some(budget)) => {
112117 let reserve = forge_ci::size::parse_size(&self.reserve_memory).unwrap_or(0);
113118 let mut limits = self.clone();
114119 limits.memory = budget.max(reserve).max(1).to_string();
120+ limits.swap = swap;
115121 limits
116122 }
117123 _ => self.for_job(declared),
@@ -720,11 +726,11 @@ mod tests {
720726 assert_eq!(bad.for_job(None).memory, "lots");
721727 // Builtin: no `memory` means the whole budget; a declared one keeps its slack.
722728 assert_eq!(
723- limits.for_builtin_job(None, Some(6 * gib)).memory,
729+ limits.for_builtin_job(None, Some(6 * gib), 0).memory,
724730 (6 * gib).to_string()
725731 );
726732 assert_eq!(
727- limits.for_builtin_job(Some(gib), Some(6 * gib)).memory,
733+ limits.for_builtin_job(Some(gib), Some(6 * gib), 0).memory,
728734 limits.for_job(Some(gib)).memory
729735 );
730736 }
crates/forge-runner/src/executor.rsmodified+9 -3
@@ -59,9 +59,11 @@ pub struct ExecRequest<'a> {
5959 /// Where the executor leaves OOM kills it saw in the job's cgroup. Executors that cannot see
6060 /// them leave it alone.
6161 pub oom: OomSink,
62- /// The runner's memory budget now. The builtin executor gives a job without `memory` this as
63- /// its cgroup limit; `None` keeps the configured `limits.memory`.
62+ /// The RAM part of the runner's memory budget now. The builtin executor gives a job without
63+ /// `memory` this as its cgroup limit; `None` keeps the configured `limits.memory`.
6464 pub memory_budget: Option<u64>,
65+ /// The swap part of the budget, which a job without `memory` may use on top of it.
66+ pub memory_swap: u64,
6567 }
6668
6769 /// The executor's answer. The runner adds timeouts and artifact results on top of it.
@@ -651,8 +653,11 @@ impl<I: ImageSource> BuiltinExecutor<I> {
651653 cancel,
652654 oom: oom_sink,
653655 memory_budget,
656+ memory_swap,
654657 } = req;
655- let limits = self.limits.for_builtin_job(job.memory_bytes, memory_budget);
658+ let limits = self
659+ .limits
660+ .for_builtin_job(job.memory_bytes, memory_budget, memory_swap);
656661 let disk_gb = limits.job_disk_gb;
657662 if let Err(e) = validate_job_id(&job.job_id) {
658663 return error_result(e.to_string());
@@ -1181,6 +1186,7 @@ mod tests {
11811186 cancel: CancellationToken::new(),
11821187 oom: OomSink::default(),
11831188 memory_budget: None,
1189+ memory_swap: 0,
11841190 })
11851191 .await;
11861192 assert_eq!(result.outcome, Outcome::Success, "{result:?}");
crates/forge-runner/src/memory.rsmodified+44 -0
@@ -74,10 +74,24 @@ fn configured_budget(limits: &Limits, mem_total: u64, swap_total: u64) -> u64 {
7474
7575 struct State {
7676 budget: u64,
77+ swap: u64,
7778 reserve: u64,
7879 reserved: u64,
7980 }
8081
82+/// The part of the budget that is swap: `swap_percent` of the swap in `auto` mode, none with a set
83+/// budget. A lone job may use it as swap on top of the rest of the budget, which is RAM.
84+fn swap_share(limits: &Limits, swap_total: u64) -> u64 {
85+ let set = limits
86+ .memory_budget
87+ .as_deref()
88+ .is_some_and(|text| text != "auto" && parse_size(text).is_some());
89+ if set {
90+ return 0;
91+ }
92+ (u128::from(swap_total) * u128::from(limits.budget_swap_percent.min(100)) / 100) as u64
93+}
94+
8195 /// The runner's memory budget and the reservations against it. Cheap to share: clone the `Arc`.
8296 pub struct MemoryGate {
8397 limits: Limits,
@@ -122,6 +136,7 @@ impl MemoryGate {
122136 swap_total,
123137 state: Mutex::new(State {
124138 budget,
139+ swap: swap_share(limits, swap_total),
125140 reserve,
126141 reserved: 0,
127142 }),
@@ -136,6 +151,17 @@ impl MemoryGate {
136151 self.lock().budget
137152 }
138153
154+ /// The swap part of the budget, see [`swap_share`].
155+ pub fn swap(&self) -> u64 {
156+ self.lock().swap
157+ }
158+
159+ /// The RAM part of the budget: what the box and a lone job may hold in memory.
160+ pub fn ram_budget(&self) -> u64 {
161+ let state = self.lock();
162+ state.budget.saturating_sub(state.swap).max(MIN_BUDGET)
163+ }
164+
139165 pub fn default_reserve(&self) -> u64 {
140166 self.lock().reserve
141167 }
@@ -176,6 +202,7 @@ impl MemoryGate {
176202 }
177203 let new_budget = configured_budget(&limits, self.mem_total, self.swap_total);
178204 let new_reserve = parse_size(&limits.reserve_memory).unwrap_or(FALLBACK_RESERVE);
205+ let new_swap = swap_share(&limits, self.swap_total);
179206 let mut state = self.lock();
180207 if (state.budget, state.reserve) == (new_budget, new_reserve) {
181208 return None;
@@ -188,6 +215,7 @@ impl MemoryGate {
188215 format_size(new_reserve),
189216 );
190217 state.budget = new_budget;
218+ state.swap = new_swap;
191219 state.reserve = new_reserve;
192220 Some(line)
193221 }
@@ -206,6 +234,22 @@ mod tests {
206234
207235 const GIB: u64 = 1 << 30;
208236
237+ #[test]
238+ fn the_swap_share_is_kept_apart_from_ram() {
239+ let limits = Limits {
240+ budget_swap_percent: 50,
241+ ..Limits::default()
242+ };
243+ let gate = MemoryGate::new(&limits, 8 * GIB, 8 * GIB);
244+ assert_eq!(gate.swap(), 4 * GIB);
245+ assert_eq!(gate.ram_budget(), gate.budget() - 4 * GIB);
246+ let set = Limits {
247+ memory_budget: Some("6g".into()),
248+ ..limits
249+ };
250+ assert_eq!(MemoryGate::new(&set, 8 * GIB, 8 * GIB).swap(), 0);
251+ }
252+
209253 #[test]
210254 fn a_job_fits_when_the_sum_stays_within_the_budget() {
211255 // 6.5 GiB budget, one 4 GiB job running.
crates/forge-runner/src/runner.rsmodified+4 -3
@@ -318,10 +318,10 @@ impl Runner {
318318 reply.reserve_memory.as_deref(),
319319 );
320320 if let Some(pressure) = &self.pressure
321- && pressure.budget() != self.memory.budget()
321+ && pressure.budget() != self.memory.ram_budget()
322322 {
323323 let pressure = pressure.clone();
324- let budget = self.memory.budget();
324+ let budget = self.memory.ram_budget();
325325 let _ = tokio::task::spawn_blocking(move || pressure.resize(budget)).await;
326326 }
327327 self.restart.observe(reply.restart);
@@ -460,7 +460,8 @@ impl Runner {
460460 logs: tx.clone(),
461461 cancel: job_cancel.clone(),
462462 oom: oom.clone(),
463- memory_budget: Some(self.memory.budget()),
463+ memory_budget: Some(self.memory.ram_budget()),
464+ memory_swap: self.memory.swap(),
464465 })
465466 .await;
466467 workspace_dir = Some(dir);
crates/forge-runner/src/sandbox/spec.rsmodified+3 -3
@@ -282,9 +282,9 @@ fn build_resources(limits: &Limits) -> Result<oci_spec::runtime::LinuxResources,
282282 .memory(
283283 LinuxMemoryBuilder::default()
284284 .limit(memory)
285- // Memory plus swap equals memory: a job at its limit is OOM-killed inside its own
286- // cgroup instead of pushing the host into swap (memory.swap.max = 0).
287- .swap(memory)
285+ // Memory plus swap is memory plus the job's share of swap (0 unless the budget
286+ // counts swap): past both the job is OOM-killed inside its own cgroup.
287+ .swap(memory.saturating_add(i64::try_from(limits.swap).unwrap_or(i64::MAX)))
288288 .build()
289289 .map_err(spec_err)?,
290290 )
crates/forge-runner/tests/integration/builtin.rsmodified+1 -0
@@ -106,6 +106,7 @@ async fn run_job(
106106 cancel,
107107 oom: forge_runner::oom::OomSink::default(),
108108 memory_budget: None,
109+ memory_swap: 0,
109110 })
110111 .await;
111112 let logs = collector.await.expect("log collector");
docs/ci.mdmodified+5 -1
@@ -648,7 +648,11 @@ more RAM than the host has, which ends in the kernel's OOM killer picking a vict
648648 in the middle of a build. So a runner also has a memory budget. By default the budget is the
649649 host's `MemTotal` minus a headroom, the larger of `memory_headroom` and 10%: a 7.6 GiB host gets
650650 about 6.6 GiB. Swap does not count unless `budget_swap_percent` says so, because a build that
651-runs in swap is slower than one that waits. Each running job holds a reservation (its `memory`
651+runs in swap is slower than one that waits. The swap share is swap, not RAM: the box and a job
652+without `memory` may hold the rest of the budget in RAM, and that job may also swap up to the share.
653+So on a host where one build needs more than its RAM, such as forge's own on 8 GB, set
654+`budget_swap_percent` (for example 50) with zram or a swap file, and the build swaps instead of
655+being OOM-killed. Each running job holds a reservation (its `memory`
652656 from `ci.toml`, see [Memory](#memory), or `reserve_memory`), and a job starts only when the
653657 reservations of the running jobs plus its own fit the budget. `concurrency` stays as a second cap, so
654658 both must have room. The runner page shows the budget, what is reserved and what is free to admit.