boot-tree: single-threaded feed, Duration timestamps, fields over getters, jiff formatting

This commit is contained in:
Willem Vanhulle committed 2026-10-11 15:54:28 +02:00
1 parent 63743d8807
commit 2b1447965a
11 files changed
+714 -757

No files matched your search

+30 -90
View File
@@ -1,8 +1,8 @@
use std::{
path::Path,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
mpsc::{Receiver, channel},
},
thread::sleep,
time::Duration,
@@ -17,14 +17,11 @@ use ratatui::{
DefaultTerminal,
backend::{Backend, ClearType},
};
use rustix::system::uname;
use signal_hook::{consts::SIGTERM, flag};
use crate::{
clock::{host_name, monotonic_now},
console::ForegroundVt,
error::Error,
feed::{self, FeedOptions, JobKind, Update},
sysroot::running_in_initrd,
clock::monotonic_now, console::ForegroundVt, error::Error, feed::Feed, sysroot::INITRD_RELEASE,
systemd::Scope,
};
@@ -40,92 +37,26 @@ pub struct Options {
pub fn run(options: Options) -> Result<(), Error> {
let terminate = Arc::new(AtomicBool::new(false));
flag::register(SIGTERM, Arc::clone(&terminate))?;
let (sender, updates) = channel();
let mut state = BootState::new(initial_phase(options.scope));
feed::spawn(
FeedOptions {
scope: options.scope,
target: options.target,
},
sender,
);
let foreground = ForegroundVt::take();
let phase = match options.scope {
Scope::User => Phase::User,
Scope::System if Path::new(INITRD_RELEASE).exists() => Phase::Initrd,
Scope::System => Phase::Host,
};
let mut state = BootState::new(phase);
let mut feed = Feed::new(options.scope, options.target);
let _foreground = ForegroundVt::take();
let mut terminal = ratatui::try_init()?;
let watched = watch(
&mut state,
&updates,
&mut feed,
&mut terminal,
&terminate,
options.hold,
);
ratatui::restore();
if let Some(foreground) = foreground {
foreground.give_back();
}
watched
}
fn initial_phase(scope: Scope) -> Phase {
match scope {
Scope::User => Phase::User,
Scope::System if running_in_initrd() => Phase::Initrd,
Scope::System => Phase::Host,
}
}
fn absorb(state: &mut BootState, update: Update) -> Option<Error> {
match update {
Update::Root(root) => state.set_root(root),
Update::Handoff(unit) => state.set_handoff(unit),
Update::Origin(origin) => state.set_origin(origin),
Update::Unit { facts, activity } => state.track_unit(facts, activity),
Update::JobQueued {
id,
unit,
at,
kind: JobKind::BringsUp,
} => state.queue_job(id, &unit, at),
Update::JobQueued {
id,
unit,
at,
kind: JobKind::Unknown,
} => state.queue_job_of_unknown_kind(id, &unit, at),
Update::JobFinished { id, result, at } => state.finish_job(id, result, at),
Update::Activity { unit, activity } => state.update_activity(&unit, activity),
Update::RootSwitched { at } => state.switch_root(at),
Update::Resubscribed => state.forget_jobs(),
Update::Ended(error) => return Some(error),
}
None
}
fn draw(
terminal: &mut DefaultTerminal,
state: &BootState,
host: &str,
frame: u64,
) -> Result<(), Error> {
terminal.draw(|canvas| {
canvas.render_widget(
BootView {
state,
now: monotonic_now(),
host,
frame,
},
canvas.area(),
);
})?;
Ok(())
}
fn repaint_fully(terminal: &mut DefaultTerminal) -> Result<(), Error> {
terminal.backend_mut().clear_region(ClearType::All)?;
terminal.swap_buffers();
Ok(())
}
fn asks_to_quit(event: &Event) -> bool {
let Event::Key(KeyEvent {
code, modifiers, ..
@@ -139,25 +70,34 @@ fn asks_to_quit(event: &Event) -> bool {
fn watch(
state: &mut BootState,
updates: &Receiver<Update>,
feed: &mut Feed,
terminal: &mut DefaultTerminal,
terminate: &AtomicBool,
hold: Duration,
) -> Result<(), Error> {
let host = host_name();
let host = uname().nodename().to_string_lossy().into_owned();
let mut frame: u64 = 0;
loop {
if let Some(error) = updates.try_iter().find_map(|update| absorb(state, update)) {
return Err(error);
}
feed.pump(state, FRAME_INTERVAL)?;
if frame.is_multiple_of(FRAMES_PER_FULL_REPAINT) {
repaint_fully(terminal)?;
terminal.backend_mut().clear_region(ClearType::All)?;
terminal.swap_buffers();
}
draw(terminal, state, &host, frame)?;
terminal.draw(|canvas| {
canvas.render_widget(
BootView {
state,
now: monotonic_now(),
host: &host,
frame,
},
canvas.area(),
);
})?;
if state.is_complete() {
eprintln!(
"boot-tree: {} reached, handing the console over",
state.root().unwrap_or("boot")
state.root.as_deref().unwrap_or("boot")
);
sleep(hold);
return Ok(());
@@ -165,7 +105,7 @@ fn watch(
if terminate.load(Ordering::Relaxed) {
return Ok(());
}
if event::poll(FRAME_INTERVAL)? && asks_to_quit(&event::read()?) {
if event::poll(Duration::ZERO)? && asks_to_quit(&event::read()?) {
return Ok(());
}
frame += 1;