diff --git a/crates/stela-boot-core/src/lib.rs b/crates/stela-boot-core/src/lib.rs index 2beae0cfab..814ef137d3 100644 --- a/crates/stela-boot-core/src/lib.rs +++ b/crates/stela-boot-core/src/lib.rs @@ -106,6 +106,7 @@ pub mod ram; pub mod random; pub mod record; pub mod remapping; +pub mod repeated_lines; #[cfg(target_arch = "x86_64")] pub mod requester; pub mod reset_reason; diff --git a/crates/stela-boot-core/src/repeated_lines.rs b/crates/stela-boot-core/src/repeated_lines.rs new file mode 100644 index 0000000000..4fb8d3a8db --- /dev/null +++ b/crates/stela-boot-core/src/repeated_lines.rs @@ -0,0 +1,121 @@ +//! Thinning of a log's identical lines in a row: the first is written, each +//! power of two of its repeats leaves one count line, and the next different +//! line is preceded by the total, so a line Linux prints once per retry (an +//! amdgpu submission refused for memory, which RADV retries every +//! millisecond for a second) costs a few lines, not thousands, and the count +//! is in the log even if the boot stops while it repeats. +//! +//! Lines are compared by a hash of their text, which needs no buffer: the +//! sink may run where nothing can be allocated. +use core::fmt; + +/// What to write for a line. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum Verdict { + /// Write it, after a line saying the one before came `repeated` more + /// times than the count already written, if any. + Write { repeated: Option }, + /// The same as the one before: write nothing, or a line saying it has + /// come `count` more times so far. + Repeat { count: Option }, +} + +/// The last line's hash and its repeats. +#[derive(Debug, Default)] +pub struct Repeats { + last: Option, + count: u64, + written: u64, +} + +impl Repeats { + /// What to write for the line `text`. + pub fn line(&mut self, text: fmt::Arguments<'_>) -> Verdict { + self.hashed(hash(text)) + } + + fn hashed(&mut self, hash: u64) -> Verdict { + if self.last == Some(hash) { + self.count += 1; + if self.count.is_power_of_two() { + self.written = self.count; + return Verdict::Repeat { + count: Some(self.count), + }; + } + return Verdict::Repeat { count: None }; + } + let repeated = (self.count > self.written).then_some(self.count); + *self = Self { + last: Some(hash), + count: 0, + written: 0, + }; + Verdict::Write { repeated } + } +} + +/// FNV-1a over the formatted text. +fn hash(text: fmt::Arguments<'_>) -> u64 { + struct Fnv(u64); + impl fmt::Write for Fnv { + fn write_str(&mut self, s: &str) -> fmt::Result { + for byte in s.bytes() { + self.0 = (self.0 ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3); + } + Ok(()) + } + } + let mut fnv = Fnv(0xcbf2_9ce4_8422_2325); + let _ = fmt::write(&mut fnv, text); + fnv.0 +} + +#[cfg(test)] +mod tests { + use super::*; + use alloc::vec::Vec; + + #[test] + fn identical_lines_are_counted_at_powers_of_two_and_totalled() { + let mut repeats = Repeats::default(); + let refused = format_args!("<3> amdgpu: Not enough memory for command submission!"); + assert_eq!(repeats.line(refused), Verdict::Write { repeated: None }); + let counts: Vec<_> = (0..4265) + .filter_map(|_| match repeats.line(refused) { + Verdict::Repeat { count } => count, + Verdict::Write { .. } => panic!("a repeat written"), + }) + .collect(); + assert_eq!( + counts, + [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096] + ); + assert_eq!( + repeats.line(format_args!("<6> other")), + Verdict::Write { + repeated: Some(4265) + } + ); + assert_eq!( + repeats.line(format_args!("<6> {}", "other")), + Verdict::Repeat { count: Some(1) } + ); + // A total already written is not written again. + assert_eq!( + repeats.line(format_args!("<6> third")), + Verdict::Write { repeated: None } + ); + } + + #[test] + fn different_lines_are_all_written() { + let mut repeats = Repeats::default(); + for line in ["a", "b", "a", "b"] { + assert_eq!( + repeats.line(format_args!("{line}")), + Verdict::Write { repeated: None } + ); + } + } +} diff --git a/crates/stela-boot/src/engine/gpu.rs b/crates/stela-boot/src/engine/gpu.rs index b534d7d78e..643c36a94e 100644 --- a/crates/stela-boot/src/engine/gpu.rs +++ b/crates/stela-boot/src/engine/gpu.rs @@ -784,6 +784,9 @@ struct Host { lines: RefCell>, /// The unsupported operations already reported. reported: RefCell>, + /// Linux's last line and its repeats in a row, which are counted, not + /// written again ([`stela_boot_core::repeated_lines`]). + repeats: RefCell, /// The first error Linux reported, which a stop before the start /// finished names as the reason the GPU did not start. first_error: RefCell>, @@ -822,7 +825,28 @@ impl Host { } fn report(&self, line: core::fmt::Arguments<'_>) { - output::arguments(format_args!("LINUX {line}\n")); + use stela_boot_core::repeated_lines::Verdict; + // A report made while one is being counted is written as it is. + let verdict = self + .repeats + .try_borrow_mut() + .map_or(Verdict::Write { repeated: None }, |mut repeats| { + repeats.line(line) + }); + match verdict { + Verdict::Write { repeated } => { + if let Some(count) = repeated { + output::arguments(format_args!( + "LINUX the line before came {count} more times in all\n" + )); + } + output::arguments(format_args!("LINUX {line}\n")); + } + Verdict::Repeat { count: Some(count) } => output::arguments(format_args!( + "LINUX the line before came {count} more times so far\n" + )), + Verdict::Repeat { count: None } => {} + } } /// Whether Linux runs on a processor in the boot CPU's place, which @@ -2754,6 +2778,7 @@ pub(super) fn open(machine: Machine, claimed: Vec) -> Result<(), Error> devices: RefCell::new(BTreeMap::new()), lines: RefCell::new(lines), reported: RefCell::new(BTreeSet::new()), + repeats: RefCell::new(Default::default()), first_error: RefCell::new(None), gsp_start: RefCell::new(Default::default()), gpus: claimed