From 63743d8807d1684570983b845666b3a5712caf65 Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Sun, 11 Oct 2026 13:54:23 +0200 Subject: [PATCH] boot-tree: libdbus feed thread, dedicated VT, stage sections, bead timeline, demo example --- .envrc | 1 + README.md | 48 +++ demo/test.nix | 2 +- examples/demo.rs | 291 ++++++++++++++ src/clock.rs | 3 +- src/feed.rs | 2 +- src/lib.rs | 2 + src/main.rs | 2 - src/model.rs | 10 + src/run.rs | 6 +- src/systemd.rs | 6 +- src/view.rs | 999 ++++++++++++++++++++++++++++------------------- 12 files changed, 968 insertions(+), 404 deletions(-) create mode 100644 .envrc create mode 100644 README.md create mode 100644 examples/demo.rs create mode 100644 src/lib.rs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/README.md b/README.md new file mode 100644 index 0000000..74969dc --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# boot-tree + +A live dependency tree of systemd jobs on the boot console, in the spirit of +nix-output-monitor. It replaces the scrolling kernel and systemd lines with a +tree of pending units and a timeline bar per row. + +## How it works + +- Starts as an initrd unit (needs `boot.initrd.systemd.enable`), about a second + after the kernel. +- Talks to PID 1 over the private socket `/run/systemd/private` with libdbus: + `Subscribe`, `ListJobs`, unit properties, and the `JobNew`/`JobRemoved`/ + `PropertiesChanged` signals. +- Survives the switch to the real root: its `argv[0]` starts with `@`, the unit + ignores isolate, and it chroots into `/sysroot` before PID 1 re-executes, then + reconnects. Plymouth does the same with `plymouth update-root-fs`. +- Draws on its own virtual terminal (tty12 by default) and switches back to the + previous one when the display manager starts or the default target is + reached, so gettys and greeters on tty1 cannot draw over it. + +## Usage + +```nix +inputs.boot-tree.url = "git+file:/home/wvhulle/Code/boot-tree?ref=refs/heads/main"; +# in a host's modules: +boot-tree.nixosModules.default +{ services.boot-tree.enable = true; } +``` + +Options: `tty`, `holdMs`, `quietConsole` (silences kernel/systemd console +output and loads a Terminus font in the initrd), `consoleFont`. + +## Testing + +Commands assume the dev shell is active (`direnv allow` once; `.envrc` runs +`use flake`). + +| What | Command | +| --- | --- | +| Unit and render snapshot tests | `cargo test` | +| Scripted boot animation, no systemd needed | `cargo run --example demo` (`q` quits) | +| Live against your user manager | `nix run .#demo-units -- start`, then `nix run . -- --user --target boot-tree-demo.target --hold-ms 3000` | +| Boot a demo VM in a window | `env -u LD_LIBRARY_PATH nix run .#vm` | +| Automated boot test with OCR | `nix build .#checks.x86_64-linux.boot` (screenshots in `result/`) | +| Same test, interactively | `nix run .#checks.x86_64-linux.boot.driverInteractive`, then `test_script()` | + +`env -u LD_LIBRARY_PATH` is only needed when the shell exports libraries that +clash with QEMU's. diff --git a/demo/test.nix b/demo/test.nix index cd19c1d..9c30160 100644 --- a/demo/test.nix +++ b/demo/test.nix @@ -20,7 +20,7 @@ machine.wait_for_text("initrd") machine.screenshot("initrd") - machine.wait_for_text("demo-slow") + machine.wait_for_text("stage 2") machine.screenshot("stage-2") machine.wait_for_unit("greetd.service") diff --git a/examples/demo.rs b/examples/demo.rs new file mode 100644 index 0000000..0a5f4a7 --- /dev/null +++ b/examples/demo.rs @@ -0,0 +1,291 @@ +use std::{ + collections::{HashMap, HashSet}, + io, + time::{Duration, Instant}, +}; + +use boot_tree::{ + model::{ActiveState, Activity, BootState, JobResult, Micros, Phase, UnitFacts}, + view::BootView, +}; +use ratatui::{ + DefaultTerminal, + crossterm::event::{self, Event, KeyCode}, +}; + +const FRAME_INTERVAL: Duration = Duration::from_millis(100); +const FINAL_FRAME: Duration = Duration::from_secs(3); + +struct Step { + unit: &'static str, + after: &'static [&'static str], + work_ms: u64, + fails: bool, +} + +const fn step(unit: &'static str, after: &'static [&'static str], work_ms: u64) -> Step { + Step { + unit, + after, + work_ms, + fails: false, + } +} + +const fn failing(unit: &'static str, after: &'static [&'static str], work_ms: u64) -> Step { + Step { + unit, + after, + work_ms, + fails: true, + } +} + +const INITRD: &[Step] = &[ + step( + "initrd.target", + &["initrd-fs.target", "initrd-root-device.target"], + 0, + ), + step("initrd-fs.target", &["sysroot.mount"], 0), + step( + "initrd-root-device.target", + &["dev-disk-by\\x2dlabel-nixos.device"], + 0, + ), + step("sysroot.mount", &["systemd-fsck-root.service"], 300), + step( + "systemd-fsck-root.service", + &["dev-disk-by\\x2dlabel-nixos.device"], + 500, + ), + step("dev-disk-by\\x2dlabel-nixos.device", &[], 1200), + step("systemd-udevd.service", &[], 400), + step("demo-initrd-probe.service", &[], 2500), +]; + +const HOST: &[Step] = &[ + step( + "graphical.target", + &["multi-user.target", "display-manager.service"], + 0, + ), + step("display-manager.service", &["multi-user.target"], 400), + step( + "multi-user.target", + &[ + "basic.target", + "NetworkManager-wait-online.service", + "docker.service", + "tailscaled.service", + "nvidia-powerd.service", + "systemd-rfkill.service", + ], + 0, + ), + step("basic.target", &["sysinit.target"], 0), + step( + "sysinit.target", + &[ + "systemd-tmpfiles-setup.service", + "systemd-udev-trigger.service", + ], + 0, + ), + step("systemd-tmpfiles-setup.service", &[], 600), + step("systemd-udev-trigger.service", &[], 900), + step("NetworkManager.service", &["basic.target"], 1500), + step( + "NetworkManager-wait-online.service", + &["NetworkManager.service"], + 3500, + ), + step( + "network-online.target", + &["NetworkManager-wait-online.service"], + 0, + ), + step("tailscaled.service", &["network-online.target"], 1200), + step("containerd.service", &["basic.target"], 800), + step( + "docker.service", + &["containerd.service", "network-online.target"], + 1500, + ), + failing("nvidia-powerd.service", &["basic.target"], 700), + step("sys-devices-virtual-misc-rfkill.device", &[], 4000), + step( + "systemd-rfkill.service", + &["sys-devices-virtual-misc-rfkill.device"], + 300, + ), +]; + +struct ScriptedJob { + id: u32, + step: &'static Step, +} + +impl ScriptedJob { + fn facts(&self) -> UnitFacts { + UnitFacts { + name: self.step.unit.to_owned(), + description: self.step.unit.to_owned(), + after: self.step.after.iter().map(ToString::to_string).collect(), + } + } + + fn outcome(&self) -> JobResult { + if self.step.fails { + JobResult::Failed + } else { + JobResult::Done + } + } + + fn settled_state(&self) -> ActiveState { + if self.step.fails { + ActiveState::Failed + } else { + ActiveState::Active + } + } +} + +struct Script { + jobs: Vec, + started: HashMap<&'static str, Micros>, + finished: HashSet<&'static str>, +} + +impl Script { + fn begin(steps: &'static [Step], first_job: u32, state: &mut BootState, now: Micros) -> Self { + let jobs: Vec = steps + .iter() + .zip(first_job..) + .map(|(step, id)| ScriptedJob { id, step }) + .collect(); + state.set_root(steps[0].unit.to_owned()); + jobs.iter() + .for_each(|job| state.track_unit(job.facts(), Activity::default())); + jobs.iter() + .for_each(|job| state.queue_job(job.id, job.step.unit, now)); + Self { + jobs, + started: HashMap::new(), + finished: HashSet::new(), + } + } + + fn is_finished(&self) -> bool { + self.finished.len() == self.jobs.len() + } + + fn is_ready(&self, job: &ScriptedJob) -> bool { + !self.started.contains_key(job.step.unit) + && job + .step + .after + .iter() + .all(|dependency| self.finished.contains(dependency)) + } + + fn is_due(&self, job: &ScriptedJob, now: Micros) -> bool { + !self.finished.contains(job.step.unit) + && self + .started + .get(job.step.unit) + .is_some_and(|started| now >= started + job.step.work_ms * 1_000) + } + + fn advance(&mut self, state: &mut BootState, now: Micros) { + let starting: Vec<&'static str> = self + .jobs + .iter() + .filter(|job| self.is_ready(job)) + .map(|job| job.step.unit) + .collect(); + starting.iter().for_each(|unit| { + self.started.insert(unit, now); + state.update_activity( + unit, + Activity { + state: Some(ActiveState::Activating), + inactive_exit: Some(now), + active_enter: None, + }, + ); + }); + let due: HashSet<&'static str> = self + .jobs + .iter() + .filter(|job| self.is_due(job, now)) + .map(|job| job.step.unit) + .collect(); + self.jobs + .iter() + .filter(|job| due.contains(job.step.unit)) + .for_each(|job| { + state.update_activity( + job.step.unit, + Activity { + state: Some(job.settled_state()), + inactive_exit: None, + active_enter: Some(now), + }, + ); + state.finish_job(job.id, job.outcome(), now); + }); + self.finished.extend(due); + } +} + +fn wants_to_quit() -> io::Result { + if !event::poll(FRAME_INTERVAL)? { + return Ok(false); + } + Ok( + matches!(event::read()?, Event::Key(key) if matches!(key.code, KeyCode::Char('q') | KeyCode::Esc)), + ) +} + +fn play(terminal: &mut DefaultTerminal) -> io::Result<()> { + let clock = Instant::now(); + let now = || clock.elapsed().as_micros() as Micros; + let mut state = BootState::new(Phase::Initrd); + let mut script = Script::begin(INITRD, 1, &mut state, now()); + let mut frame = 0; + let mut completed_at = None; + loop { + script.advance(&mut state, now()); + if state.phase() == Phase::Initrd && script.is_finished() { + state.switch_root(now()); + script = Script::begin(HOST, 1000, &mut state, now()); + } + terminal.draw(|canvas| { + canvas.render_widget( + BootView { + state: &state, + now: now(), + host: "demo", + frame, + }, + canvas.area(), + ); + })?; + if state.is_complete() { + let completed = *completed_at.get_or_insert_with(Instant::now); + if completed.elapsed() >= FINAL_FRAME { + return Ok(()); + } + } + if wants_to_quit()? { + return Ok(()); + } + frame += 1; + } +} + +fn main() -> io::Result<()> { + ratatui::run(play) +} diff --git a/src/clock.rs b/src/clock.rs index fa8ef44..6a30314 100644 --- a/src/clock.rs +++ b/src/clock.rs @@ -1,10 +1,9 @@ +use boot_tree::model::Micros; use rustix::{ system::uname, time::{ClockId, clock_gettime}, }; -use crate::model::Micros; - pub fn monotonic_now() -> Micros { let now = clock_gettime(ClockId::Monotonic); now.tv_sec as Micros * 1_000_000 + now.tv_nsec as Micros / 1_000 diff --git a/src/feed.rs b/src/feed.rs index ae8050e..ed4ac30 100644 --- a/src/feed.rs +++ b/src/feed.rs @@ -6,12 +6,12 @@ use std::{ time::{Duration, Instant}, }; +use boot_tree::model::{Activity, HANDOFF_UNIT, HOST_MILESTONES, JobResult, Micros, UnitFacts}; use dbus::Path; use crate::{ clock::monotonic_now, error::Error, - model::{Activity, HANDOFF_UNIT, HOST_MILESTONES, JobResult, Micros, UnitFacts}, sysroot::{Sysroot, running_in_initrd}, systemd::{Scope, Session, Signal, brings_unit_up}, }; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..47c7b81 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod model; +pub mod view; diff --git a/src/main.rs b/src/main.rs index 7742e51..e5b3397 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,9 @@ mod clock; mod console; mod error; mod feed; -mod model; mod run; mod sysroot; mod systemd; -mod view; use std::{process::ExitCode, time::Duration}; diff --git a/src/model.rs b/src/model.rs index 93f6dc2..e21aded 100644 --- a/src/model.rs +++ b/src/model.rs @@ -415,6 +415,16 @@ impl BootState { } } + pub fn finished_stages(&self) -> Vec { + self.frozen.iter().map(FrozenMilestone::row).collect() + } + + pub fn current_stage_started_at(&self) -> Micros { + self.frozen + .last() + .map_or(self.origin, |frozen| frozen.reached_at) + } + pub fn milestones(&self) -> Vec { let mut finished: Vec = self .units diff --git a/src/run.rs b/src/run.rs index 32c8c9b..eb1701f 100644 --- a/src/run.rs +++ b/src/run.rs @@ -8,6 +8,10 @@ use std::{ time::Duration, }; +use boot_tree::{ + model::{BootState, Phase}, + view::BootView, +}; use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers}; use ratatui::{ DefaultTerminal, @@ -20,10 +24,8 @@ use crate::{ console::ForegroundVt, error::Error, feed::{self, FeedOptions, JobKind, Update}, - model::{BootState, Phase}, sysroot::running_in_initrd, systemd::Scope, - view::BootView, }; const FRAME_INTERVAL: Duration = Duration::from_millis(100); diff --git a/src/systemd.rs b/src/systemd.rs index df83008..7534b5b 100644 --- a/src/systemd.rs +++ b/src/systemd.rs @@ -1,15 +1,13 @@ use std::{iter, time::Duration}; +use boot_tree::model::{ActiveState, Activity, JobResult, Micros, UnitFacts}; use dbus::{ Message, MessageType, Path, arg::{AppendAll, PropMap, RefArg, Variant}, channel::{BusType, Channel}, }; -use crate::{ - error::Error, - model::{ActiveState, Activity, JobResult, Micros, UnitFacts}, -}; +use crate::error::Error; const DESTINATION: &str = "org.freedesktop.systemd1"; const MANAGER_PATH: &str = "/org/freedesktop/systemd1"; diff --git a/src/view.rs b/src/view.rs index bacd11c..1124756 100644 --- a/src/view.rs +++ b/src/view.rs @@ -1,10 +1,11 @@ -use std::iter::repeat_n; +use std::iter::{once, repeat_n}; use ratatui::{ buffer::Buffer, - layout::Rect, - style::{Color, Modifier, Style}, - widgets::Widget, + layout::{Constraint, Layout, Rect}, + style::{Style, Stylize}, + text::{Line, Span}, + widgets::{Block, Borders, Paragraph, Row, Table, Widget}, }; use crate::model::{BootState, Micros, MilestoneRow, PendingRow, RowStatus, Tally}; @@ -12,14 +13,20 @@ use crate::model::{BootState, Micros, MilestoneRow, PendingRow, RowStatus, Tally const SPINNER: [char; 4] = ['|', '/', '-', '\\']; const DONE: char = '√'; const FAILED: char = '×'; -const RUNNING_BAR: char = '█'; -const WAITING_BAR: char = '░'; +const BEAD: char = '■'; +const WAITING_BEAD: char = '·'; const STILL_GOING: char = '▶'; -const ELLIPSIS: char = '…'; -const PHASE_ARROW: char = '→'; +const RUNNING_MARK: char = '►'; +const ROOT_SWITCH_MARK: char = '│'; +const RULE: char = '─'; +const DURATION_WIDTH: u16 = 8; +const TIMELINE_LABEL_WIDTH: u16 = 28; const MIN_BAR_WIDTH: u16 = 12; -const DURATION_WIDTH: usize = 7; +const MIN_HEIGHT_FOR_TIMELINE: u16 = 12; +const TIMELINE_CHROME_HEIGHT: u16 = 3; +const HEADER_HEIGHT: u16 = 2; +const FOOTER_HEIGHT: u16 = 2; const MAX_FAILED_ROWS: usize = 3; const AXIS_STEPS_SECONDS: [u64; 12] = [1, 2, 5, 10, 15, 30, 60, 120, 300, 600, 1800, 3600]; const AXIS_LABEL_SPACING: u64 = 8; @@ -31,32 +38,318 @@ pub struct BootView<'a> { pub frame: u64, } -struct Columns { - left: u16, - bar_x: u16, - bar_width: u16, +impl Widget for BootView<'_> { + fn render(self, area: Rect, buf: &mut Buffer) { + let rows = self.state.pending_rows(); + let milestones: Vec = self + .state + .milestones() + .into_iter() + .filter(|row| row.reached_at.is_some()) + .collect(); + let units = timeline_units(&rows); + let failures = failure_lines(self.state.failed_units()); + let stages = self.stage_lines(area.width); + let fixed = HEADER_HEIGHT + stages.len() as u16 + failures.len() as u16 + FOOTER_HEIGHT; + let heights = Heights::share( + area.height.saturating_sub(fixed), + rows.len() as u16, + timeline_need(area, milestones.len() + units.len()), + ); + + let [header, _, stage_area, tree, failed, _, timeline, footer] = Layout::vertical([ + Constraint::Length(1), + Constraint::Length(HEADER_HEIGHT - 1), + Constraint::Length(stages.len() as u16), + Constraint::Length(heights.tree), + Constraint::Length(failures.len() as u16), + Constraint::Fill(1), + Constraint::Length(heights.timeline), + Constraint::Length(FOOTER_HEIGHT), + ]) + .areas(area); + + self.header().render(header, buf); + Paragraph::new(stages).render(stage_area, buf); + self.tree(&rows, tree.height).render(tree, buf); + Paragraph::new(failures).render(failed, buf); + if heights.timeline > 0 { + self.timeline(&milestones, &units, timeline.width) + .render(timeline, buf); + } + let root_switched = !self.state.finished_stages().is_empty(); + footer_paragraph(self.state.tally(), footer.width, root_switched).render(footer, buf); + } } -impl Columns { - fn for_width(width: u16) -> Self { - let left = (width * 11 / 20).clamp(40.min(width), 72); - let bar_x = left + 1; - let bar_width = width.saturating_sub(bar_x); - if bar_width < MIN_BAR_WIDTH { +struct Heights { + tree: u16, + timeline: u16, +} + +impl Heights { + fn share(free: u16, tree_need: u16, timeline_need: u16) -> Self { + if tree_need + timeline_need <= free { return Self { - left: width, - bar_x: width, - bar_width: 0, + tree: tree_need, + timeline: timeline_need, }; } + let timeline = timeline_need.min((free / 2).max(free.saturating_sub(tree_need))); Self { - left, - bar_x, - bar_width, + tree: free - timeline, + timeline, } } } +fn rule_line(width: u16, left: Vec>, right: Vec>) -> Line<'static> { + let used = Line::from(left.clone()).width() + Line::from(right.clone()).width(); + let fill = Span::from(rule(usize::from(width).saturating_sub(used))).dark_gray(); + Line::from( + left.into_iter() + .chain(once(fill)) + .chain(right) + .collect::>(), + ) +} + +fn rule(length: usize) -> String { + repeat_n(RULE, length).collect() +} + +impl BootView<'_> { + fn header(&self) -> Table<'static> { + let elapsed = format_duration(self.now.saturating_sub(self.state.origin())); + Table::new( + [Row::new([ + Line::from(format!("boot-tree {}", self.host)), + Line::from(elapsed).right_aligned(), + ])], + [Constraint::Fill(1), Constraint::Length(DURATION_WIDTH)], + ) + .bold() + } + + fn stage_lines(&self, width: u16) -> Vec> { + let finished = self.state.finished_stages().into_iter().map(|stage| { + let took = stage.reached_at.unwrap_or(stage.started_at) - stage.started_at; + rule_line( + width, + vec![ + Span::from(format!("{} ", rule(2))).dark_gray(), + Span::from(stage.label).dark_gray(), + Span::from(format!(" {} ", rule(2))).dark_gray(), + Span::from(format!("{BEAD} done in {}", format_duration(took))).green(), + Span::from(format!(" · {} ", unit_count(stage.finished_units))).dark_gray(), + ], + Vec::new(), + ) + }); + let running_for = self + .now + .saturating_sub(self.state.current_stage_started_at()); + let current = rule_line( + width, + vec![ + Span::from(format!("{} ", rule(2))).dark_gray(), + Span::from(self.state.phase().label()).bold(), + Span::raw(" "), + ], + vec![ + Span::raw(" "), + Span::from(format_duration(running_for)).bold(), + Span::from(format!(" {}", rule(2))).dark_gray(), + ], + ); + finished.chain(once(current)).collect() + } + + fn tree<'a>(&self, rows: &'a [PendingRow], height: u16) -> Table<'a> { + let capacity = usize::from(height); + let shown = if rows.len() > capacity { + capacity.saturating_sub(1) + } else { + rows.len() + }; + let hidden = rows.len() - shown; + let overflow = + (hidden > 0).then(|| Row::new([Line::from(format!("… {hidden} more")).dark_gray()])); + Table::new( + rows.iter() + .take(shown) + .map(|row| self.tree_row(row)) + .chain(overflow), + [Constraint::Fill(1), Constraint::Length(DURATION_WIDTH)], + ) + } + + fn tree_row<'a>(&self, row: &'a PendingRow) -> Row<'a> { + let since = row.running_since.unwrap_or(row.queued_at); + Row::new([ + Line::from(vec![ + Span::from(tree_prefix(row)).dark_gray(), + status_marker(row, self.frame), + Span::styled(row.label.as_str(), label_style(row)), + ]), + Line::from(format_duration(self.now.saturating_sub(since))) + .style(label_style(row)) + .right_aligned(), + ]) + } + + fn timeline<'a>( + &self, + milestones: &'a [MilestoneRow], + units: &[&'a PendingRow], + width: u16, + ) -> Table<'a> { + let axis = TimeAxis::new( + self.state.origin(), + self.now, + width.saturating_sub(TIMELINE_LABEL_WIDTH + 1), + ); + let root_switches: Vec = self + .state + .finished_stages() + .iter() + .filter_map(|stage| stage.reached_at) + .collect(); + let axis_rows = [ + Row::new([Line::default(), axis.labels()]), + Row::new([Line::default(), axis.ticks_line(&root_switches)]), + ]; + let milestone_rows = milestones.iter().map(|row| { + Row::new([ + Line::from(format!( + "{DONE} {} ({})", + row.label, + unit_count(row.finished_units) + )) + .green(), + axis.bar(&milestone_segments(row), None, &root_switches), + ]) + }); + let unit_rows = units.iter().map(|row| { + Row::new([ + Line::from(vec![ + timeline_marker(row), + Span::styled(row.label.as_str(), label_style(row)), + ]), + axis.bar( + &unit_segments(row, self.now), + Some(self.now), + &root_switches, + ), + ]) + }); + Table::new( + axis_rows.into_iter().chain(milestone_rows).chain(unit_rows), + [ + Constraint::Length(TIMELINE_LABEL_WIDTH), + Constraint::Fill(1), + ], + ) + .block( + Block::new() + .borders(Borders::TOP) + .border_style(Style::new().dark_gray()) + .title(Line::from(" timeline ").dark_gray()), + ) + } +} + +fn timeline_need(screen: Rect, entries: usize) -> u16 { + let fits = screen.width >= TIMELINE_LABEL_WIDTH + 1 + MIN_BAR_WIDTH + && screen.height >= MIN_HEIGHT_FOR_TIMELINE + && entries > 0; + if fits { + TIMELINE_CHROME_HEIGHT + entries as u16 + } else { + 0 + } +} + +fn failure_lines(units: &[String]) -> Vec> { + let hidden = units.len().saturating_sub(MAX_FAILED_ROWS); + units + .iter() + .take(MAX_FAILED_ROWS) + .map(|unit| format!("{FAILED} {unit} failed")) + .chain((hidden > 0).then(|| format!("{FAILED} {hidden} more failed"))) + .map(|text| Line::from(text).red().bold()) + .collect() +} + +fn footer_paragraph(tally: Tally, width: u16, root_switched: bool) -> Paragraph<'static> { + let counts = format!( + "{DONE} {} done {} running {} waiting {FAILED} {} failed", + tally.done, tally.running, tally.waiting, tally.failed + ); + let switch_legend = if root_switched { + format!(" {ROOT_SWITCH_MARK} root switch") + } else { + String::new() + }; + let legend = format!("{BEAD} running {WAITING_BEAD} waiting{switch_legend}"); + let gap = usize::from(width).saturating_sub(counts.chars().count() + legend.chars().count()); + let counts_style = if tally.failed > 0 { + Style::new().red().bold() + } else { + Style::new() + }; + let line = if gap >= 2 { + Line::from(vec![ + Span::styled(counts, counts_style), + Span::raw(" ".repeat(gap)), + Span::from(legend).dark_gray(), + ]) + } else { + Line::from(Span::styled(counts, counts_style)) + }; + Paragraph::new(line).block( + Block::new() + .borders(Borders::TOP) + .border_style(Style::new().dark_gray()), + ) +} + +struct Segment { + from: Micros, + to: Micros, + symbol: char, + style: Style, +} + +fn milestone_segments(row: &MilestoneRow) -> Vec { + row.reached_at + .map(|reached_at| Segment { + from: row.started_at, + to: reached_at, + symbol: BEAD, + style: Style::new().green(), + }) + .into_iter() + .collect() +} + +fn unit_segments(row: &PendingRow, now: Micros) -> Vec { + let waiting_until = row.running_since.unwrap_or(now); + let waiting = (waiting_until > row.queued_at).then(|| Segment { + from: row.queued_at, + to: waiting_until, + symbol: WAITING_BEAD, + style: Style::new().dark_gray(), + }); + let running = row.running_since.map(|since| Segment { + from: since, + to: now, + symbol: BEAD, + style: Style::new().yellow(), + }); + waiting.into_iter().chain(running).collect() +} + struct TimeAxis { origin: Micros, end: Micros, @@ -64,6 +357,62 @@ struct TimeAxis { width: u16, } +struct Stroke { + column: u16, + glyphs: String, + style: Style, +} + +impl Stroke { + fn run(column: u16, length: u16, symbol: char, style: Style) -> Self { + Self { + column, + glyphs: repeat_n(symbol, usize::from(length)).collect(), + style, + } + } +} + +#[derive(Clone, Copy, PartialEq)] +struct Glyph { + symbol: char, + style: Style, +} + +const BLANK: Glyph = Glyph { + symbol: ' ', + style: Style::new(), +}; + +fn canvas_line(width: u16, strokes: impl IntoIterator) -> Line<'static> { + let cells = strokes + .into_iter() + .fold(vec![BLANK; usize::from(width)], |mut cells, stroke| { + cells + .iter_mut() + .skip(usize::from(stroke.column)) + .zip(stroke.glyphs.chars()) + .for_each(|(cell, symbol)| { + *cell = Glyph { + symbol, + style: stroke.style, + }; + }); + cells + }); + Line::from( + cells + .chunk_by(|left, right| left.style == right.style) + .map(|run| { + Span::styled( + run.iter().map(|glyph| glyph.symbol).collect::(), + run[0].style, + ) + }) + .collect::>(), + ) +} + impl TimeAxis { fn new(origin: Micros, now: Micros, width: u16) -> Self { let span_seconds = now.saturating_sub(origin).div_ceil(1_000_000).max(1); @@ -71,7 +420,7 @@ impl TimeAxis { let step_seconds = AXIS_STEPS_SECONDS .into_iter() .find(|step| span_seconds.div_ceil(*step) <= max_labels) - .unwrap_or(*AXIS_STEPS_SECONDS.last().expect("axis steps are not empty")); + .unwrap_or(AXIS_STEPS_SECONDS[AXIS_STEPS_SECONDS.len() - 1]); let end_seconds = span_seconds.div_ceil(step_seconds).max(1) * step_seconds; Self { origin, @@ -87,32 +436,145 @@ impl TimeAxis { ((fraction * f64::from(self.width)) as u16).min(self.width.saturating_sub(1)) } - fn ticks(&self) -> impl Iterator + '_ { + fn tick_offsets(&self) -> impl Iterator + '_ { (0..) .map(|index| index * self.step) .take_while(|offset| self.origin + offset <= self.end) - .map(|offset| AxisTick { - column: self.column(self.origin + offset), - offset, + } + + fn root_switch_strokes(&self, root_switches: &[Micros]) -> Vec { + root_switches + .iter() + .map(|at| { + Stroke::run( + self.column(*at), + 1, + ROOT_SWITCH_MARK, + Style::new().dark_gray(), + ) }) + .collect() + } + + fn labels(&self) -> Line<'static> { + canvas_line( + self.width, + self.tick_offsets().map(|offset| { + let glyphs = format_axis_label(offset); + Stroke { + column: self + .column(self.origin + offset) + .min(self.width.saturating_sub(glyphs.len() as u16)), + glyphs, + style: Style::new().dark_gray(), + } + }), + ) + } + + fn ticks_line(&self, root_switches: &[Micros]) -> Line<'static> { + canvas_line( + self.width, + self.root_switch_strokes(root_switches) + .into_iter() + .chain(self.tick_offsets().map(|offset| { + Stroke::run( + self.column(self.origin + offset), + 1, + '|', + Style::new().dark_gray(), + ) + })), + ) + } + + fn bar( + &self, + segments: &[Segment], + still_going_at: Option, + root_switches: &[Micros], + ) -> Line<'static> { + let bars = segments.iter().map(|segment| { + let start = self.column(segment.from); + let end = self.column(segment.to).max(start + 1); + Stroke::run(start, end - start, segment.symbol, segment.style) + }); + let still_going = still_going_at + .map(|now| Stroke::run(self.column(now), 1, STILL_GOING, Style::new().dark_gray())); + canvas_line( + self.width, + self.root_switch_strokes(root_switches) + .into_iter() + .chain(bars) + .chain(still_going), + ) } } -struct AxisTick { - column: u16, - offset: Micros, +fn timeline_units(rows: &[PendingRow]) -> Vec<&PendingRow> { + let mut units: Vec<&PendingRow> = rows.iter().filter(|row| !is_target(row)).collect(); + units.sort_by_key(|row| row.running_since.unwrap_or(Micros::MAX)); + units +} + +fn is_target(row: &PendingRow) -> bool { + row.label.ends_with(".target") +} + +fn status_marker(row: &PendingRow, frame: u64) -> Span<'static> { + match (is_target(row), row.status) { + (true, _) => Span::raw(""), + (false, RowStatus::Running) => { + Span::from(format!("[{}] ", SPINNER[(frame % 4) as usize])).yellow() + } + (false, RowStatus::Waiting) => Span::from("[ ] ").dark_gray(), + } +} + +fn timeline_marker(row: &PendingRow) -> Span<'static> { + match row.status { + RowStatus::Running => Span::from(format!("{RUNNING_MARK} ")).yellow(), + RowStatus::Waiting => Span::raw(" "), + } +} + +fn label_style(row: &PendingRow) -> Style { + match (is_target(row), row.status) { + (true, _) => Style::new().cyan(), + (false, RowStatus::Running) => Style::new(), + (false, RowStatus::Waiting) => Style::new().dark_gray(), + } +} + +fn tree_prefix(row: &PendingRow) -> String { + let ancestors = row + .is_last_sibling + .iter() + .skip(1) + .take(row.depth.saturating_sub(1)) + .map(|ancestor_is_last| if *ancestor_is_last { " " } else { "│ " }); + let connector = (row.depth > 0).then(|| match row.is_last_sibling.last() { + Some(false) => "├─ ", + _ => "└─ ", + }); + ancestors.chain(connector).chain(once("")).collect() +} + +fn unit_count(count: usize) -> String { + match count { + 1 => "1 unit".to_owned(), + count => format!("{count} units"), + } } fn format_duration(micros: Micros) -> String { let tenths = micros / 100_000; - if tenths < 600 { - return format!("{}.{}s", tenths / 10, tenths % 10); - } let seconds = micros / 1_000_000; - if seconds < 3600 { - return format!("{}m{:02}s", seconds / 60, seconds % 60); + match seconds { + 0..60 => format!("{}.{}s", tenths / 10, tenths % 10), + 60..3600 => format!("{}m{:02}s", seconds / 60, seconds % 60), + _ => format!("{}h{:02}m", seconds / 3600, seconds % 3600 / 60), } - format!("{}h{:02}m", seconds / 3600, seconds % 3600 / 60) } fn format_axis_label(offset: Micros) -> String { @@ -124,309 +586,6 @@ fn format_axis_label(offset: Micros) -> String { } } -fn truncate(text: &str, width: usize) -> String { - if text.chars().count() <= width { - return text.to_owned(); - } - if width == 0 { - return String::new(); - } - let mut kept: String = text.chars().take(width - 1).collect(); - kept.push(ELLIPSIS); - kept -} - -fn tree_prefix(row: &PendingRow) -> String { - let mut prefix: String = row - .is_last_sibling - .iter() - .skip(1) - .take(row.depth.saturating_sub(1)) - .map(|ancestor_is_last| if *ancestor_is_last { " " } else { "│ " }) - .collect(); - if row.depth > 0 { - let is_last = row.is_last_sibling.last().copied().unwrap_or(true); - prefix.push_str(if is_last { "└─ " } else { "├─ " }); - } - prefix -} - -struct Palette; - -impl Palette { - fn header() -> Style { - Style::new().add_modifier(Modifier::BOLD) - } - - fn dim() -> Style { - Style::new().fg(Color::DarkGray) - } - - fn running() -> Style { - Style::new().fg(Color::Yellow) - } - - fn waiting() -> Style { - Style::new().fg(Color::DarkGray) - } - - fn done() -> Style { - Style::new().fg(Color::Green) - } - - fn failed() -> Style { - Style::new().fg(Color::Red).add_modifier(Modifier::BOLD) - } - - fn target() -> Style { - Style::new().fg(Color::Cyan) - } -} - -struct Painter<'b> { - buf: &'b mut Buffer, - area: Rect, -} - -impl Painter<'_> { - fn text(&mut self, x: u16, y: u16, text: &str, style: Style) { - if y >= self.area.height || x >= self.area.width { - return; - } - let room = usize::from(self.area.width - x); - self.buf - .set_stringn(self.area.x + x, self.area.y + y, text, room, style); - } - - fn right_aligned(&mut self, right_edge: u16, y: u16, text: &str, style: Style) { - let width = text.chars().count() as u16; - self.text(right_edge.saturating_sub(width), y, text, style); - } - - fn fill(&mut self, x: u16, y: u16, length: u16, symbol: char, style: Style) { - let run: String = repeat_n(symbol, usize::from(length)).collect(); - self.text(x, y, &run, style); - } -} - -impl BootView<'_> { - fn draw_header(&self, painter: &mut Painter, columns: &Columns, axis: Option<&TimeAxis>) { - let title = format!( - "boot-tree {} {PHASE_ARROW} {}", - self.host, - self.state.phase().label() - ); - painter.text(0, 0, &title, Palette::header()); - let elapsed = format_duration(self.now.saturating_sub(self.state.origin())); - painter.right_aligned(columns.left, 0, &elapsed, Palette::header()); - let Some(axis) = axis else { - return; - }; - axis.ticks().for_each(|tick| { - let label = format_axis_label(tick.offset); - let label_width = label.chars().count() as u16; - let x = columns.bar_x + tick.column.min(axis.width.saturating_sub(label_width)); - painter.text(x, 0, &label, Palette::dim()); - painter.text(columns.bar_x + tick.column, 1, "|", Palette::dim()); - }); - } - - fn draw_pending( - &self, - painter: &mut Painter, - columns: &Columns, - axis: Option<&TimeAxis>, - y: u16, - row: &PendingRow, - ) { - let is_target = row.label.ends_with(".target"); - let prefix = tree_prefix(row); - let marker = match (is_target, row.status) { - (true, _) => String::new(), - (false, RowStatus::Running) => format!("[{}] ", SPINNER[(self.frame % 4) as usize]), - (false, RowStatus::Waiting) => "[ ] ".to_owned(), - }; - let label_style = match (is_target, row.status) { - (true, _) => Palette::target(), - (false, RowStatus::Running) => Style::new(), - (false, RowStatus::Waiting) => Palette::dim(), - }; - let since = row.running_since.unwrap_or(row.queued_at); - let duration = format_duration(self.now.saturating_sub(since)); - let fixed = prefix.chars().count() + marker.chars().count() + DURATION_WIDTH + 1; - let label_room = usize::from(columns.left).saturating_sub(fixed); - let mut x = 0; - painter.text(x, y, &prefix, Palette::dim()); - x += prefix.chars().count() as u16; - let marker_style = if row.status == RowStatus::Running { - Palette::running() - } else { - Palette::dim() - }; - painter.text(x, y, &marker, marker_style); - x += marker.chars().count() as u16; - painter.text(x, y, &truncate(&row.label, label_room), label_style); - painter.right_aligned(columns.left, y, &duration, label_style); - let Some(axis) = axis else { - return; - }; - let queued = axis.column(row.queued_at); - let now = axis.column(self.now); - match row.running_since { - Some(running_since) => { - let started = axis.column(running_since); - if started > queued { - painter.fill( - columns.bar_x + queued, - y, - started - queued, - WAITING_BAR, - Palette::waiting(), - ); - } - painter.fill( - columns.bar_x + started, - y, - now.saturating_sub(started).max(1), - RUNNING_BAR, - Palette::running(), - ); - } - None => { - painter.fill( - columns.bar_x + queued, - y, - now.saturating_sub(queued).max(1), - WAITING_BAR, - Palette::waiting(), - ); - } - } - let arrow_x = (now + 1).min(axis.width.saturating_sub(1)); - painter.text( - columns.bar_x + arrow_x, - y, - &STILL_GOING.to_string(), - Palette::dim(), - ); - } -} - -impl Painter<'_> { - fn milestone( - &mut self, - columns: &Columns, - axis: Option<&TimeAxis>, - y: u16, - row: &MilestoneRow, - ) { - let units = match row.finished_units { - 1 => "1 unit".to_owned(), - count => format!("{count} units"), - }; - let label = truncate(&row.label, 18); - let Some(reached_at) = row.reached_at else { - self.text(0, y, &format!(" {label:<18} {units:>10}"), Palette::dim()); - return; - }; - self.text( - 0, - y, - &format!("{DONE} {label:<18} {units:>10}"), - Palette::done(), - ); - let duration = format_duration(reached_at.saturating_sub(row.started_at)); - self.right_aligned(columns.left, y, &duration, Palette::done()); - if let Some(axis) = axis { - let start = axis.column(row.started_at); - let end = axis.column(reached_at); - self.fill( - columns.bar_x + start, - y, - end.saturating_sub(start).max(1), - RUNNING_BAR, - Palette::done(), - ); - } - } - - fn footer(&mut self, tally: Tally, y: u16) { - self.fill(0, y, self.area.width, '─', Palette::dim()); - let counts = format!( - "{DONE} {} done {} running {} waiting {FAILED} {} failed", - tally.done, tally.running, tally.waiting, tally.failed - ); - let style = if tally.failed > 0 { - Palette::failed() - } else { - Style::new() - }; - self.text(0, y + 1, &counts, style); - let legend = format!("{RUNNING_BAR} running {WAITING_BAR} waiting"); - let room_left = usize::from(self.area.width).saturating_sub(counts.chars().count()); - if room_left > legend.chars().count() + 2 { - self.right_aligned(self.area.width, y + 1, &legend, Palette::dim()); - } - } -} - -impl Widget for BootView<'_> { - fn render(self, area: Rect, buf: &mut Buffer) { - let mut painter = Painter { buf, area }; - let columns = Columns::for_width(area.width); - let axis = (columns.bar_width > 0) - .then(|| TimeAxis::new(self.state.origin(), self.now, columns.bar_width)); - self.draw_header(&mut painter, &columns, axis.as_ref()); - - let milestones = self.state.milestones(); - let failed = self.state.failed_units(); - let shown_failed = failed.len().min(MAX_FAILED_ROWS); - let failed_lines = shown_failed + usize::from(failed.len() > MAX_FAILED_ROWS); - let footer_height = 2; - let bottom_height = (milestones.len() + failed_lines + footer_height) as u16; - let tree_top = 2; - let tree_bottom = area.height.saturating_sub(bottom_height + 1).max(tree_top); - - let rows = self.state.pending_rows(); - let capacity = usize::from(tree_bottom - tree_top); - let overflow = rows.len() > capacity; - let visible = if overflow { - capacity.saturating_sub(1) - } else { - rows.len() - }; - rows.iter() - .take(visible) - .zip(tree_top..) - .for_each(|(row, y)| self.draw_pending(&mut painter, &columns, axis.as_ref(), y, row)); - if overflow { - let hidden = rows.len() - visible; - painter.text( - 0, - tree_top + visible as u16, - &format!("{ELLIPSIS} {hidden} more"), - Palette::dim(), - ); - } - - let milestones_top = area.height.saturating_sub(bottom_height); - milestones - .iter() - .zip(milestones_top..) - .for_each(|(milestone, y)| painter.milestone(&columns, axis.as_ref(), y, milestone)); - let failed_top = milestones_top + milestones.len() as u16; - let hidden_failures = failed.len().saturating_sub(MAX_FAILED_ROWS); - failed - .iter() - .take(shown_failed) - .map(|unit| format!("{FAILED} {unit} failed")) - .chain((hidden_failures > 0).then(|| format!("{FAILED} {hidden_failures} more failed"))) - .zip(failed_top..) - .for_each(|(line, y)| painter.text(0, y, &line, Palette::failed())); - painter.footer(self.state.tally(), failed_top + failed_lines as u16); - } -} - #[cfg(test)] mod tests { use ratatui::{Terminal, backend::TestBackend}; @@ -468,88 +627,144 @@ mod tests { .collect() } + fn running_since(since: Micros) -> Activity { + Activity { + state: Some(ActiveState::Activating), + inactive_exit: Some(since), + active_enter: None, + } + } + fn boot_in_progress() -> BootState { let mut state = BootState::new(Phase::Host); state.set_root("graphical.target".to_owned()); - state.track_unit( - facts("graphical.target", &["multi-user.target"]), - Activity::default(), - ); - state.track_unit( - facts( - "multi-user.target", - &["docker.service", "systemd-rfkill.service"], - ), - Activity::default(), - ); - state.track_unit( - facts("docker.service", &[]), - Activity { - state: Some(ActiveState::Activating), - inactive_exit: Some(9_000_000), - active_enter: None, - }, - ); - state.track_unit( - facts("systemd-rfkill.service", &[]), - Activity { - state: Some(ActiveState::Activating), - inactive_exit: Some(6_000_000), - active_enter: None, - }, - ); - state.track_unit(facts("sysinit.target", &[]), Activity::default()); - state.track_unit(facts("nvidia-powerd.service", &[]), Activity::default()); [ - "graphical.target", - "multi-user.target", - "docker.service", - "systemd-rfkill.service", - "sysinit.target", - "nvidia-powerd.service", + ( + facts("graphical.target", &["multi-user.target"]), + Activity::default(), + ), + ( + facts( + "multi-user.target", + &["docker.service", "systemd-rfkill.service"], + ), + Activity::default(), + ), + (facts("docker.service", &[]), running_since(9_000_000)), + ( + facts("systemd-rfkill.service", &[]), + running_since(6_000_000), + ), + (facts("sysinit.target", &[]), Activity::default()), + (facts("nvidia-powerd.service", &[]), Activity::default()), ] .into_iter() .zip(1..) - .for_each(|(unit, id)| state.queue_job(id, unit, 1_000_000)); + .for_each(|((unit, activity), id)| { + let name = unit.name.clone(); + state.track_unit(unit, activity); + state.queue_job(id, &name, 1_000_000); + }); state.finish_job(5, JobResult::Done, 4_000_000); state.finish_job(6, JobResult::Failed, 5_000_000); state } #[test] - fn renders_tree_bars_milestones_and_footer() { - let lines = render(&boot_in_progress(), 12_400_000, 110, 14); + fn tree_on_top_timeline_below() { + let lines = render(&boot_in_progress(), 12_400_000, 110, 24); let screen = lines.join("\n"); + assert!(lines[0].starts_with("boot-tree ryzen"), "{screen}"); + assert!(lines[0].ends_with("12.4s"), "{screen}"); + assert!(lines[2].starts_with("── stage 2 ──"), "{screen}"); + assert!(lines[2].ends_with("12.4s ──"), "{screen}"); + assert!(lines[3].starts_with("graphical.target"), "{screen}"); + assert!(lines[4].starts_with("└─ multi-user.target"), "{screen}"); + assert!(lines[5].starts_with(" ├─ [/] systemd-rfkill"), "{screen}"); + assert!(lines[6].starts_with(" └─ [/] docker"), "{screen}"); assert!( - lines[0].starts_with("boot-tree ryzen → stage 2"), + lines[7].starts_with("× nvidia-powerd.service failed"), "{screen}" ); - assert!(lines[0].contains("12.4s"), "{screen}"); - assert!(lines[2].starts_with("graphical.target"), "{screen}"); - assert!(lines[3].starts_with("└─ multi-user.target"), "{screen}"); - assert!(lines[4].starts_with(" ├─ [/] systemd-rfkill"), "{screen}"); - assert!(lines[5].starts_with(" └─ [/] docker"), "{screen}"); - assert!(lines[4].contains('█') && lines[4].contains('▶'), "{screen}"); - assert!(screen.contains("√ sysinit.target"), "{screen}"); + let timeline = lines + .iter() + .position(|line| line.contains(" timeline ")) + .expect("timeline panel"); + assert_eq!(timeline, 24 - 2 - 3 - 3, "{screen}"); + assert!(lines[timeline + 1].contains("0s"), "{screen}"); + assert!( + lines[timeline + 3].starts_with("√ sysinit.target (1 unit)"), + "{screen}" + ); + assert!(lines[timeline + 3].contains('■'), "{screen}"); + assert!( + lines[timeline + 4].starts_with("► systemd-rfkill"), + "{screen}" + ); + assert!( + lines[timeline + 4].contains("·■") && lines[timeline + 4].ends_with('▶'), + "{screen}" + ); + assert!(lines[timeline + 5].starts_with("► docker"), "{screen}"); + assert!(!screen.contains("root switch"), "{screen}"); assert!( screen.contains("× nvidia-powerd.service failed"), "{screen}" ); assert!( - screen.contains("√ 1 done 2 running 2 waiting × 1 failed"), + lines[23].starts_with("√ 1 done 2 running 2 waiting × 1 failed"), "{screen}" ); } #[test] fn narrow_screens_drop_the_timeline() { - let lines = render(&boot_in_progress(), 12_400_000, 50, 14); + let lines = render(&boot_in_progress(), 12_400_000, 36, 24); assert!( lines.iter().all(|line| !line.contains('█')), "{}", lines.join("\n") ); - assert!(lines[4].contains("systemd-rfkill")); + assert!(lines[5].contains("systemd-rfkill")); + } + + #[test] + fn finished_stages_collapse_into_a_summary_rule() { + let mut state = BootState::new(Phase::Initrd); + state.set_root("initrd.target".to_owned()); + state.track_unit(facts("initrd.target", &[]), Activity::default()); + state.queue_job(1, "initrd.target", 0); + state.finish_job(1, JobResult::Done, 6_000_000); + state.switch_root(7_200_000); + state.set_root("graphical.target".to_owned()); + state.track_unit(facts("graphical.target", &[]), Activity::default()); + state.queue_job(2, "graphical.target", 7_300_000); + let lines = render(&state, 17_300_000, 110, 24); + let screen = lines.join("\n"); + assert!( + lines[2].starts_with("── initrd ── ■ done in 7.2s · 1 unit ──"), + "{screen}" + ); + assert!(lines[3].starts_with("── stage 2 ──"), "{screen}"); + assert!(lines[3].ends_with("10.1s ──"), "{screen}"); + assert!(lines[4].starts_with("graphical.target"), "{screen}"); + let timeline = lines + .iter() + .position(|line| line.contains(" timeline ")) + .expect("timeline panel"); + let marker_column = |line: &str| line.chars().position(|symbol| symbol == '│'); + let switch_column = marker_column(&lines[timeline + 2]).expect("root switch marker"); + assert!( + lines[timeline + 3].starts_with("√ initrd (1 unit)"), + "{screen}" + ); + assert_eq!( + marker_column(&lines[timeline + 3]), + Some(switch_column), + "{screen}" + ); + assert!(lines[timeline + 3].contains("■│"), "{screen}"); + assert!(screen.contains("│ root switch"), "{screen}"); } #[test] @@ -560,7 +775,7 @@ mod tests { state.track_unit(facts(&name, &[]), Activity::default()); state.queue_job(id, &name, 0); }); - let lines = render(&state, 1_000_000, 110, 12); + let lines = render(&state, 1_000_000, 110, 20); assert!( lines.iter().any(|line| line.starts_with("… ")), "{}",