Skip to content

Commit 7afdbd6

Browse files
emmertexclaude
andcommitted
upstream #3118: the automatic memory budget has no floor above a small cgroup limit
Partial port of upstream #3118's follow-ups (the fork already has the cgroup reader and mb=0 for automatic). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent d3c9c85 commit 7afdbd6

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

‎crates/engine/src/memory.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ const fn clamp_to_usize(bytes: u64) -> usize {
4343
/// The budget when it is unknown how much memory the computer has.
4444
const UNKNOWN_RAM_BUDGET: usize = 3 << 29; // 1.5 GiB
4545

46-
/// The automatic budget for a computer with `ram` bytes: a quarter of it, at least 256 MiB, at most
47-
/// [`AUTO_MAX`] (8 GB → 2 GB, 32 GB → 8 GB, 64 GB and more → 16 GB).
46+
/// The automatic budget for a computer with `ram` bytes: a quarter of it, at most [`AUTO_MAX`] (8 GB → 2 GB,
47+
/// 32 GB → 8 GB, 64 GB and more → 16 GB). No floor: under a small cgroup limit (128 MiB) a floor above the quarter
48+
/// would let the caches alone outgrow the limit, and the kernel kills the process (upstream #3072); at least a byte,
49+
/// since 0 means "not computed yet" to [`budget`].
4850
pub fn auto_budget(ram: Option<usize>) -> usize {
4951
match ram {
50-
Some(ram) => (ram / 4).clamp(256 << 20, AUTO_MAX),
52+
Some(ram) => (ram / 4).clamp(1, AUTO_MAX),
5153
None => UNKNOWN_RAM_BUDGET,
5254
}
5355
}
@@ -464,7 +466,9 @@ mod budget_tests {
464466
let gb = 1usize << 30;
465467
assert!(auto_budget(Some(32 * gb)) > auto_budget(Some(8 * gb)));
466468
assert_eq!(auto_budget(Some(512 * gb)), AUTO_MAX, "a safe ceiling");
467-
assert!(auto_budget(Some(512 << 20)) >= 256 << 20, "a floor");
469+
// no floor: under a small cgroup limit the caches must not outgrow the limit (upstream #3072)
470+
assert_eq!(auto_budget(Some(128 << 20)), 32 << 20, "a quarter of a 128 MiB limit");
471+
assert_eq!(auto_budget(Some(0)), 1, "never the 0 that means not computed");
468472
assert!(max_budget_of(Some(64 * gb)) < 64 * gb);
469473
assert!(max_budget_of(Some(64 * gb)) > auto_budget(Some(64 * gb)));
470474
assert!(max_budget_of(Some(usize::MAX)) < usize::MAX, "no overflow");

0 commit comments

Comments
 (0)