From 00a02159280d6ab85a51eb71d67041eae993d88c Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:40:53 -0500 Subject: [PATCH] feat(boot): name the heap's live, carved, largest free and total bytes in the allocation failure the screen and boot log keep, where only the serial port told fragmentation from a full heap --- crates/stela-boot-core/src/heap.rs | 19 ++--- crates/stela-boot-core/src/heap/failure.rs | 74 ++++++++++++++++++- .../stela-boot-core/src/heap/failure/tests.rs | 60 ++++++++++++++- crates/stela-boot/src/output/terminal.rs | 10 +-- 4 files changed, 144 insertions(+), 19 deletions(-) diff --git a/crates/stela-boot-core/src/heap.rs b/crates/stela-boot-core/src/heap.rs index 2458fa34c1..89601f7e37 100644 --- a/crates/stela-boot-core/src/heap.rs +++ b/crates/stela-boot-core/src/heap.rs @@ -35,6 +35,7 @@ use core::{ // Explicit, so the NVIDIA LUT fixture can compile this file by path. #[path = "heap/failure.rs"] mod failure; +pub use failure::{FailedHeap, Failure}; #[path = "heap/tally.rs"] pub mod tally; @@ -488,9 +489,11 @@ pub struct Heap { failures: AtomicU64, /// Where the first failed allocation is reported; it must not allocate. failure_sink: fn(core::fmt::Arguments<'_>), - /// The first failed allocation's bytes and alignment, for a report that - /// can no longer ask the heap ([`Heap::first_failure`]). - failed: [AtomicUsize; 2], + /// The first failed allocation's bytes and alignment, then the heap's + /// live, carved, largest free and total bytes when it failed (all zero + /// where the metadata was held), for a report that can no longer ask + /// the heap ([`Heap::first_failure`]). + failed: [AtomicUsize; 6], state: UnsafeCell, /// Each tagged core's cache and counts. cores: [Core; CPUS], @@ -528,11 +531,9 @@ impl Heap { } } - /// The bytes and alignment of the first allocation that found no - /// memory, once one has. - pub fn first_failure(&self) -> Option<(usize, usize)> { - let align = self.failed[1].load(Ordering::Acquire); - (align != 0).then(|| (self.failed[0].load(Ordering::Relaxed), align)) + /// The first allocation that found no memory, once one has. + pub fn first_failure(&self) -> Option { + failure::first(self) } pub const fn new() -> Self { @@ -541,7 +542,7 @@ impl Heap { failure_reported: AtomicBool::new(false), failures: AtomicU64::new(0), failure_sink: |_| {}, - failed: [const { AtomicUsize::new(0) }; 2], + failed: [const { AtomicUsize::new(0) }; 6], cores: [const { Core::new() }; CPUS], tagged: AtomicUsize::new(0), any_tagged: AtomicBool::new(false), diff --git a/crates/stela-boot-core/src/heap/failure.rs b/crates/stela-boot-core/src/heap/failure.rs index bffdf0ba06..e4bfc1502b 100644 --- a/crates/stela-boot-core/src/heap/failure.rs +++ b/crates/stela-boot-core/src/heap/failure.rs @@ -20,8 +20,6 @@ fn capture(heap: &Heap, layout: Layout) -> Option { { return None; } - heap.failed[0].store(layout.size(), Ordering::Relaxed); - heap.failed[1].store(layout.align(), Ordering::Release); let usage = heap.try_lock().and_then(|guard| { // SAFETY: the successful one-shot guard owns heap metadata and masks // local IRQ reentry. No state borrow or guard reaches UART formatting. @@ -30,6 +28,15 @@ fn capture(heap: &Heap, layout: Layout) -> Option { drop(guard); usage }); + let snapshot = usage.map_or([0; 4], |usage| { + [usage.live, usage.carved, usage.largest, usage.total] + }); + heap.failed[0].store(layout.size(), Ordering::Relaxed); + for (slot, value) in heap.failed[2..].iter().zip(snapshot) { + slot.store(value, Ordering::Relaxed); + } + // The alignment, never zero, publishes the rest. + heap.failed[1].store(layout.align(), Ordering::Release); let mut cores = [0; CPUS]; let mut tagged = 0; for (slot, own) in cores.iter_mut().zip(heap.owned()) { @@ -82,6 +89,69 @@ impl fmt::Display for Report { } } +/// The first allocation that found no memory, and the heap it found. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Failure { + pub bytes: usize, + pub align: usize, + /// None where another holder had the heap's metadata. + pub heap: Option, +} + +/// The heap's bytes when an allocation failed: a largest free block under +/// the request with much more free means fragmentation, both small means +/// the heap was full. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct FailedHeap { + pub live: usize, + pub carved: usize, + pub largest: usize, + pub total: usize, +} + +pub(super) fn first(heap: &Heap) -> Option { + let align = heap.failed[1].load(Ordering::Acquire); + if align == 0 { + return None; + } + let [live, carved, largest, total] = + core::array::from_fn(|index| heap.failed[2 + index].load(Ordering::Relaxed)); + Some(Failure { + bytes: heap.failed[0].load(Ordering::Relaxed), + align, + heap: (total != 0).then_some(FailedHeap { + live, + carved, + largest, + total, + }), + }) +} + +/// ` (the heap had no room for 17609526 bytes aligned to 1: heap=2619M/3072M +/// carved=2776M largest=16M)`, with BEAT's names, for the stop line the +/// screen and boot log keep. +impl fmt::Display for Failure { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + " (the heap had no room for {} bytes aligned to {}", + self.bytes, self.align + )?; + if let Some(heap) = self.heap { + write!( + f, + ": heap={}M/{}M carved={}M largest={}M", + heap.live >> 20, + heap.total >> 20, + heap.carved >> 20, + heap.largest >> 20 + )?; + } + f.write_str(")") + } +} + pub(super) fn report(heap: &Heap, layout: Layout) { if let Some(report) = capture(heap, layout) { // This formatter emits only fixed strings and integer fields; the diff --git a/crates/stela-boot-core/src/heap/failure/tests.rs b/crates/stela-boot-core/src/heap/failure/tests.rs index c18882d12b..0a68bc7304 100644 --- a/crates/stela-boot-core/src/heap/failure/tests.rs +++ b/crates/stela-boot-core/src/heap/failure/tests.rs @@ -56,11 +56,15 @@ fn first_exhaustion_reports_once_without_allocating() { assert!(reported.contains("live_estimate=")); assert!(reported.contains("cpu_counters=relaxed total=4096")); assert!(reported.contains("largest=0\nHEAP AT FAILURE large classes 16:0M")); - assert_eq!(heap.first_failure(), Some((16, 16))); + let first = heap.first_failure().unwrap(); + assert_eq!((first.bytes, first.align), (16, 16)); + let found = first.heap.unwrap(); + assert_eq!((found.total, found.largest), (4096, 0)); + assert!(found.live <= found.carved && found.carved <= found.total); // SAFETY: as above; this second failure must not report again, and the // first stays the one named. assert!(unsafe { heap.alloc(Layout::from_size_align(64, 8).unwrap()) }.is_null()); - assert_eq!(heap.first_failure(), Some((16, 16))); + assert_eq!(heap.first_failure(), Some(first)); REPORTED.with(|text| assert_eq!(text.borrow().as_str(), reported)); assert!(capture(&heap, layout).is_none()); } @@ -81,3 +85,55 @@ fn held_metadata_reports_unavailable_once() { assert!(capture(&heap, Layout::new::()).is_none()); drop(guard); } + +#[test] +fn the_stop_line_names_the_heap_the_failure_found() { + let failure = Failure { + bytes: 17_609_526, + align: 1, + heap: Some(FailedHeap { + live: 2619 << 20, + carved: 2776 << 20, + largest: 16 << 20, + total: 3072 << 20, + }), + }; + let mut text = Text::new(); + write!(&mut text, "{failure}").unwrap(); + assert_eq!( + text.as_str(), + " (the heap had no room for 17609526 bytes aligned to 1: heap=2619M/3072M \ + carved=2776M largest=16M)" + ); + let mut text = Text::new(); + write!( + &mut text, + "{}", + Failure { + heap: None, + ..failure + } + ) + .unwrap(); + assert_eq!( + text.as_str(), + " (the heap had no room for 17609526 bytes aligned to 1)" + ); +} + +#[test] +fn a_failure_under_held_metadata_keeps_no_heap() { + let heap = Heap::new(); + let guard = heap.lock(); + // The guard makes the failure's snapshot unavailable. + let _ = capture(&heap, Layout::from_size_align(32, 8).unwrap()); + drop(guard); + assert_eq!( + heap.first_failure(), + Some(Failure { + bytes: 32, + align: 8, + heap: None + }) + ); +} diff --git a/crates/stela-boot/src/output/terminal.rs b/crates/stela-boot/src/output/terminal.rs index 4d7ad28e01..11b46a064f 100644 --- a/crates/stela-boot/src/output/terminal.rs +++ b/crates/stela-boot/src/output/terminal.rs @@ -186,16 +186,14 @@ impl core::fmt::Display for Callers { } } -/// The heap's first failed allocation, as a failure message names it. -struct Failed(Option<(usize, usize)>); +/// The heap's first failed allocation and the heap it found, as a failure +/// message names it. +struct Failed(Option); impl core::fmt::Display for Failed { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self.0 { - Some((bytes, align)) => write!( - f, - " (the heap had no room for {bytes} bytes aligned to {align})" - ), + Some(failure) => write!(f, "{failure}"), None => Ok(()), } }