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 commita574fbf7bed590559c572b5ce37d6f7fe7941102Parent: 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
8 changed files +76 -14
- crates/forge-runner/src/cli.rs +1 -1
- crates/forge-runner/src/config.rs +9 -3
- crates/forge-runner/src/executor.rs +9 -3
- crates/forge-runner/src/memory.rs +44 -0
- crates/forge-runner/src/runner.rs +4 -3
- crates/forge-runner/src/sandbox/spec.rs +3 -3
- crates/forge-runner/tests/integration/builtin.rs +1 -0
- docs/ci.md +5 -1
crates/forge-runner/src/cli.rsmodified+1 -1
| @@ -242,7 +242,7 @@ async fn run(cli: Cli) -> Result<ExitCode, Box<dyn std::error::Error>> { | |||
| 242 | // use all of it, so an undeclared job's cgroup limit is the box budget. | 242 | // use all of it, so an undeclared job's cgroup limit is the box budget. |
| 243 | let pressure = match cfg.executor { | 243 | let pressure = match cfg.executor { |
| 244 | ExecutorKind::Builtin => { | 244 | 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(); |
| 246 | let pressure = Pressure::start(budget, shutdown.clone()).await; | 246 | let pressure = Pressure::start(budget, shutdown.clone()).await; |
| 247 | if let Some(p) = &pressure { | 247 | if let Some(p) = &pressure { |
| 248 | cfg.limits.memory = p.budget().to_string(); | 248 | cfg.limits.memory = p.budget().to_string(); |
crates/forge-runner/src/config.rsmodified+9 -3
| @@ -59,6 +59,10 @@ pub struct Limits { | |||
| 59 | /// Percent of the host's swap that counts toward the `auto` budget. 0 means swap is ignored. | 59 | /// Percent of the host's swap that counts toward the `auto` budget. 0 means swap is ignored. |
| 60 | #[serde(default)] | 60 | #[serde(default)] |
| 61 | pub budget_swap_percent: u32, | 61 | 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, | ||
| 62 | } | 66 | } |
| 63 | 67 | ||
| 64 | fn default_reserve_memory() -> String { | 68 | fn default_reserve_memory() -> String { |
| @@ -81,6 +85,7 @@ impl Default for Limits { | |||
| 81 | memory_budget: None, | 85 | memory_budget: None, |
| 82 | memory_headroom: default_memory_headroom(), | 86 | memory_headroom: default_memory_headroom(), |
| 83 | budget_swap_percent: 0, | 87 | budget_swap_percent: 0, |
| 88 | + swap: 0, | ||
| 84 | } | 89 | } |
| 85 | } | 90 | } |
| 86 | } | 91 | } |
| @@ -106,12 +111,13 @@ impl Limits { | |||
| 106 | /// [`Limits::for_job`] for the builtin executor, where every job sits in the runner's box: a | 111 | /// [`Limits::for_job`] for the builtin executor, where every job sits in the runner's box: a |
| 107 | /// job without `memory` may use the whole budget (docs/ci.md, Memory), never less than the | 112 | /// job without `memory` may use the whole budget (docs/ci.md, Memory), never less than the |
| 108 | /// default reservation. The box and the pressure controller keep jobs together inside it. | 113 | /// 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 { |
| 110 | match (declared, budget) { | 115 | match (declared, budget) { |
| 111 | (None, Some(budget)) => { | 116 | (None, Some(budget)) => { |
| 112 | let reserve = forge_ci::size::parse_size(&self.reserve_memory).unwrap_or(0); | 117 | let reserve = forge_ci::size::parse_size(&self.reserve_memory).unwrap_or(0); |
| 113 | let mut limits = self.clone(); | 118 | let mut limits = self.clone(); |
| 114 | limits.memory = budget.max(reserve).max(1).to_string(); | 119 | limits.memory = budget.max(reserve).max(1).to_string(); |
| 120 | + limits.swap = swap; | ||
| 115 | limits | 121 | limits |
| 116 | } | 122 | } |
| 117 | _ => self.for_job(declared), | 123 | _ => self.for_job(declared), |
| @@ -720,11 +726,11 @@ mod tests { | |||
| 720 | assert_eq!(bad.for_job(None).memory, "lots"); | 726 | assert_eq!(bad.for_job(None).memory, "lots"); |
| 721 | // Builtin: no `memory` means the whole budget; a declared one keeps its slack. | 727 | // Builtin: no `memory` means the whole budget; a declared one keeps its slack. |
| 722 | assert_eq!( | 728 | assert_eq!( |
| 723 | - limits.for_builtin_job(None, Some(6 * gib)).memory, | 729 | + limits.for_builtin_job(None, Some(6 * gib), 0).memory, |
| 724 | (6 * gib).to_string() | 730 | (6 * gib).to_string() |
| 725 | ); | 731 | ); |
| 726 | assert_eq!( | 732 | 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, |
| 728 | limits.for_job(Some(gib)).memory | 734 | limits.for_job(Some(gib)).memory |
| 729 | ); | 735 | ); |
| 730 | } | 736 | } |
crates/forge-runner/src/executor.rsmodified+9 -3
| @@ -59,9 +59,11 @@ pub struct ExecRequest<'a> { | |||
| 59 | /// Where the executor leaves OOM kills it saw in the job's cgroup. Executors that cannot see | 59 | /// Where the executor leaves OOM kills it saw in the job's cgroup. Executors that cannot see |
| 60 | /// them leave it alone. | 60 | /// them leave it alone. |
| 61 | pub oom: OomSink, | 61 | pub oom: OomSink, |
| 62 | - /// The runner's memory budget now. The builtin executor gives a job without `memory` this as | 62 | + /// The RAM part of the runner's memory budget now. The builtin executor gives a job without |
| 63 | - /// its cgroup limit; `None` keeps the configured `limits.memory`. | 63 | + /// `memory` this as its cgroup limit; `None` keeps the configured `limits.memory`. |
| 64 | pub memory_budget: Option<u64>, | 64 | 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, | ||
| 65 | } | 67 | } |
| 66 | 68 | ||
| 67 | /// The executor's answer. The runner adds timeouts and artifact results on top of it. | 69 | /// The executor's answer. The runner adds timeouts and artifact results on top of it. |
| @@ -651,8 +653,11 @@ impl<I: ImageSource> BuiltinExecutor<I> { | |||
| 651 | cancel, | 653 | cancel, |
| 652 | oom: oom_sink, | 654 | oom: oom_sink, |
| 653 | memory_budget, | 655 | memory_budget, |
| 656 | + memory_swap, | ||
| 654 | } = req; | 657 | } = 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); | ||
| 656 | let disk_gb = limits.job_disk_gb; | 661 | let disk_gb = limits.job_disk_gb; |
| 657 | if let Err(e) = validate_job_id(&job.job_id) { | 662 | if let Err(e) = validate_job_id(&job.job_id) { |
| 658 | return error_result(e.to_string()); | 663 | return error_result(e.to_string()); |
| @@ -1181,6 +1186,7 @@ mod tests { | |||
| 1181 | cancel: CancellationToken::new(), | 1186 | cancel: CancellationToken::new(), |
| 1182 | oom: OomSink::default(), | 1187 | oom: OomSink::default(), |
| 1183 | memory_budget: None, | 1188 | memory_budget: None, |
| 1189 | + memory_swap: 0, | ||
| 1184 | }) | 1190 | }) |
| 1185 | .await; | 1191 | .await; |
| 1186 | assert_eq!(result.outcome, Outcome::Success, "{result:?}"); | 1192 | 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 { | |||
| 74 | 74 | ||
| 75 | struct State { | 75 | struct State { |
| 76 | budget: u64, | 76 | budget: u64, |
| 77 | + swap: u64, | ||
| 77 | reserve: u64, | 78 | reserve: u64, |
| 78 | reserved: u64, | 79 | reserved: u64, |
| 79 | } | 80 | } |
| 80 | 81 | ||
| 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 | + | ||
| 81 | /// The runner's memory budget and the reservations against it. Cheap to share: clone the `Arc`. | 95 | /// The runner's memory budget and the reservations against it. Cheap to share: clone the `Arc`. |
| 82 | pub struct MemoryGate { | 96 | pub struct MemoryGate { |
| 83 | limits: Limits, | 97 | limits: Limits, |
| @@ -122,6 +136,7 @@ impl MemoryGate { | |||
| 122 | swap_total, | 136 | swap_total, |
| 123 | state: Mutex::new(State { | 137 | state: Mutex::new(State { |
| 124 | budget, | 138 | budget, |
| 139 | + swap: swap_share(limits, swap_total), | ||
| 125 | reserve, | 140 | reserve, |
| 126 | reserved: 0, | 141 | reserved: 0, |
| 127 | }), | 142 | }), |
| @@ -136,6 +151,17 @@ impl MemoryGate { | |||
| 136 | self.lock().budget | 151 | self.lock().budget |
| 137 | } | 152 | } |
| 138 | 153 | ||
| 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 | + | ||
| 139 | pub fn default_reserve(&self) -> u64 { | 165 | pub fn default_reserve(&self) -> u64 { |
| 140 | self.lock().reserve | 166 | self.lock().reserve |
| 141 | } | 167 | } |
| @@ -176,6 +202,7 @@ impl MemoryGate { | |||
| 176 | } | 202 | } |
| 177 | let new_budget = configured_budget(&limits, self.mem_total, self.swap_total); | 203 | let new_budget = configured_budget(&limits, self.mem_total, self.swap_total); |
| 178 | let new_reserve = parse_size(&limits.reserve_memory).unwrap_or(FALLBACK_RESERVE); | 204 | let new_reserve = parse_size(&limits.reserve_memory).unwrap_or(FALLBACK_RESERVE); |
| 205 | + let new_swap = swap_share(&limits, self.swap_total); | ||
| 179 | let mut state = self.lock(); | 206 | let mut state = self.lock(); |
| 180 | if (state.budget, state.reserve) == (new_budget, new_reserve) { | 207 | if (state.budget, state.reserve) == (new_budget, new_reserve) { |
| 181 | return None; | 208 | return None; |
| @@ -188,6 +215,7 @@ impl MemoryGate { | |||
| 188 | format_size(new_reserve), | 215 | format_size(new_reserve), |
| 189 | ); | 216 | ); |
| 190 | state.budget = new_budget; | 217 | state.budget = new_budget; |
| 218 | + state.swap = new_swap; | ||
| 191 | state.reserve = new_reserve; | 219 | state.reserve = new_reserve; |
| 192 | Some(line) | 220 | Some(line) |
| 193 | } | 221 | } |
| @@ -206,6 +234,22 @@ mod tests { | |||
| 206 | 234 | ||
| 207 | const GIB: u64 = 1 << 30; | 235 | const GIB: u64 = 1 << 30; |
| 208 | 236 | ||
| 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 | + | ||
| 209 | #[test] | 253 | #[test] |
| 210 | fn a_job_fits_when_the_sum_stays_within_the_budget() { | 254 | fn a_job_fits_when_the_sum_stays_within_the_budget() { |
| 211 | // 6.5 GiB budget, one 4 GiB job running. | 255 | // 6.5 GiB budget, one 4 GiB job running. |
crates/forge-runner/src/runner.rsmodified+4 -3
| @@ -318,10 +318,10 @@ impl Runner { | |||
| 318 | reply.reserve_memory.as_deref(), | 318 | reply.reserve_memory.as_deref(), |
| 319 | ); | 319 | ); |
| 320 | if let Some(pressure) = &self.pressure | 320 | if let Some(pressure) = &self.pressure |
| 321 | - && pressure.budget() != self.memory.budget() | 321 | + && pressure.budget() != self.memory.ram_budget() |
| 322 | { | 322 | { |
| 323 | let pressure = pressure.clone(); | 323 | let pressure = pressure.clone(); |
| 324 | - let budget = self.memory.budget(); | 324 | + let budget = self.memory.ram_budget(); |
| 325 | let _ = tokio::task::spawn_blocking(move || pressure.resize(budget)).await; | 325 | let _ = tokio::task::spawn_blocking(move || pressure.resize(budget)).await; |
| 326 | } | 326 | } |
| 327 | self.restart.observe(reply.restart); | 327 | self.restart.observe(reply.restart); |
| @@ -460,7 +460,8 @@ impl Runner { | |||
| 460 | logs: tx.clone(), | 460 | logs: tx.clone(), |
| 461 | cancel: job_cancel.clone(), | 461 | cancel: job_cancel.clone(), |
| 462 | oom: oom.clone(), | 462 | oom: oom.clone(), |
| 463 | - memory_budget: Some(self.memory.budget()), | 463 | + memory_budget: Some(self.memory.ram_budget()), |
| 464 | + memory_swap: self.memory.swap(), | ||
| 464 | }) | 465 | }) |
| 465 | .await; | 466 | .await; |
| 466 | workspace_dir = Some(dir); | 467 | 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, | |||
| 282 | .memory( | 282 | .memory( |
| 283 | LinuxMemoryBuilder::default() | 283 | LinuxMemoryBuilder::default() |
| 284 | .limit(memory) | 284 | .limit(memory) |
| 285 | - // Memory plus swap equals memory: a job at its limit is OOM-killed inside its own | 285 | + // Memory plus swap is memory plus the job's share of swap (0 unless the budget |
| 286 | - // cgroup instead of pushing the host into swap (memory.swap.max = 0). | 286 | + // counts swap): past both the job is OOM-killed inside its own cgroup. |
| 287 | - .swap(memory) | 287 | + .swap(memory.saturating_add(i64::try_from(limits.swap).unwrap_or(i64::MAX))) |
| 288 | .build() | 288 | .build() |
| 289 | .map_err(spec_err)?, | 289 | .map_err(spec_err)?, |
| 290 | ) | 290 | ) |
crates/forge-runner/tests/integration/builtin.rsmodified+1 -0
| @@ -106,6 +106,7 @@ async fn run_job( | |||
| 106 | cancel, | 106 | cancel, |
| 107 | oom: forge_runner::oom::OomSink::default(), | 107 | oom: forge_runner::oom::OomSink::default(), |
| 108 | memory_budget: None, | 108 | memory_budget: None, |
| 109 | + memory_swap: 0, | ||
| 109 | }) | 110 | }) |
| 110 | .await; | 111 | .await; |
| 111 | let logs = collector.await.expect("log collector"); | 112 | 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 | |||
| 648 | in the middle of a build. So a runner also has a memory budget. By default the budget is the | 648 | in the middle of a build. So a runner also has a memory budget. By default the budget is the |
| 649 | host's `MemTotal` minus a headroom, the larger of `memory_headroom` and 10%: a 7.6 GiB host gets | 649 | host's `MemTotal` minus a headroom, the larger of `memory_headroom` and 10%: a 7.6 GiB host gets |
| 650 | about 6.6 GiB. Swap does not count unless `budget_swap_percent` says so, because a build that | 650 | 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` | ||
| 652 | from `ci.toml`, see [Memory](#memory), or `reserve_memory`), and a job starts only when the | 656 | from `ci.toml`, see [Memory](#memory), or `reserve_memory`), and a job starts only when the |
| 653 | reservations of the running jobs plus its own fit the budget. `concurrency` stays as a second cap, so | 657 | reservations of the running jobs plus its own fit the budget. `concurrency` stays as a second cap, so |
| 654 | both must have room. The runner page shows the budget, what is reserved and what is free to admit. | 658 | both must have room. The runner page shows the budget, what is reserved and what is free to admit. |