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
This commit is contained in:
1 parent
f0793fcc60
commit
00a0215928
4 files changed
+144
-19
No files matched your search
@@ -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<State>,
|
||||
/// 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> {
|
||||
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),
|
||||
|
||||
@@ -20,8 +20,6 @@ fn capture(heap: &Heap, layout: Layout) -> Option<Report> {
|
||||
{
|
||||
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<Report> {
|
||||
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<FailedHeap>,
|
||||
}
|
||||
|
||||
/// 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<Failure> {
|
||||
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
|
||||
|
||||
@@ -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::<u8>()).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
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -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<stela_boot_core::heap::Failure>);
|
||||
|
||||
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(()),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user