From 092ed41ffba4c6ea94cbc7d8fe89502571a45d71 Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Wed, 21 Oct 2026 13:03:00 +0200 Subject: [PATCH] perf: await systemd replies instead of blocking the screen Calls send their message and await the reply on the registered socket while the other streams keep drawing; signals that arrive meanwhile are queued for the next receive. Restoring systemd's status output moves to a guard that reconnects on exit, so it no longer depends on where the feed was suspended. Status modes are an enum instead of raw strings. --- src/console_session/watch_loop.rs | 19 +- src/systemd_link/boot_feed.rs | 204 ++++++++++++------- src/systemd_link/link_error.rs | 2 + src/systemd_link/systemd_bus.rs | 321 ++++++++++++++++++++---------- 4 files changed, 367 insertions(+), 179 deletions(-) diff --git a/src/console_session/watch_loop.rs b/src/console_session/watch_loop.rs index 14193ea..bdfe05c 100644 --- a/src/console_session/watch_loop.rs +++ b/src/console_session/watch_loop.rs @@ -17,7 +17,7 @@ use crate::{ boot_feed::{RootRequest, bus_inputs}, link_error::Error, switch_root::INITRD_RELEASE, - systemd_bus::{Scope, Session}, + systemd_bus::{Scope, Session, StatusOutput}, }, }; @@ -29,6 +29,20 @@ pub struct Settings { pub colors: Colors, } +struct ConfiguredStatusOnExit { + scope: Scope, +} + +impl Drop for ConfiguredStatusOnExit { + fn drop(&mut self) { + if self.scope == Scope::System + && let Ok(session) = Session::open(Scope::System) + { + session.set_show_status(StatusOutput::Configured).ok(); + } + } +} + fn boot_already_finished(settings: &Settings) -> bool { settings.scope == Scope::System && !settings.shutdown @@ -59,6 +73,9 @@ pub fn run(settings: &Settings) -> Result<(), Error> { } async fn watch_live_boot(settings: &Settings, phase: Phase) -> Result<(), Error> { + let _restores_status = ConfiguredStatusOnExit { + scope: settings.scope, + }; let requests = RequestWatch::open(); let desk = LiveDesk::new( Console::take(settings.scope == Scope::System), diff --git a/src/systemd_link/boot_feed.rs b/src/systemd_link/boot_feed.rs index a2cad6c..8e9c222 100644 --- a/src/systemd_link/boot_feed.rs +++ b/src/systemd_link/boot_feed.rs @@ -7,21 +7,21 @@ use std::{ use dbus::Path; use futures::{Stream, StreamExt, stream}; -use tokio::{io::unix::AsyncFd, time::sleep}; +use tokio::time::sleep; use crate::{ boot_model::{BootChange, BootState, HANDOFF_UNIT, JobKind, Phase}, console_session::{ - stream_sources::{ - CarriedItem, FRAME_INTERVAL, readable_or_timeout, readable_source, unfold_carrying, - }, + stream_sources::{CarriedItem, FRAME_INTERVAL, unfold_carrying}, watch_screen::ScreenInput, }, systemd_link::{ link_error::Error, monotonic_clock::monotonic_now, switch_root::Sysroot, - systemd_bus::{FetchedUnit, JobListing, Scope, Session, Signal}, + systemd_bus::{ + BusConnection, FetchedUnit, JobListing, Scope, Session, Signal, StatusOutput, + }, }, }; @@ -35,8 +35,6 @@ const SHUTDOWN_ROOTS: [&str; 5] = [ "soft-reboot.target", ]; const SHUTDOWN_FALLBACK_ROOT: &str = "shutdown.target"; -const SILENT_STATUS: &str = "no"; -const CONFIGURED_STATUS: &str = ""; #[derive(Clone, Debug, PartialEq, Eq)] pub enum RootRequest { @@ -45,7 +43,7 @@ pub enum RootRequest { } enum Link { - Connected(AsyncFd), + Connected(BusConnection), Connecting { deadline: Instant }, Finished, } @@ -79,6 +77,17 @@ struct BusFeed { gave_origin: bool, } +struct FeedProgress<'feed> { + feed: &'feed mut BusFeed, + connection: &'feed mut BusConnection, + changes: Vec, +} + +struct WatchedConnection { + connection: BusConnection, + changes: Vec, +} + fn boot_inputs(changes: Vec) -> Vec { if changes.is_empty() { return Vec::new(); @@ -104,19 +113,28 @@ impl BusFeed { } async fn next_inputs(&mut self) -> Option> { - if let Link::Connected(readiness) = &self.link { - readable_or_timeout(readiness, FRAME_INTERVAL).await; - } match mem::replace(&mut self.link, Link::Finished) { Link::Finished => None, Link::Connecting { deadline } => Some(self.connect(deadline).await), - Link::Connected(readiness) => Some(self.receive(readiness)), + Link::Connected(connection) => Some(self.receive(connection).await), } } + async fn open_watched(&mut self) -> Result { + let mut connection = BusConnection::register(Session::open(self.scope)?)?; + let changes = self.start_watching(&mut connection).await?; + Ok(WatchedConnection { + connection, + changes, + }) + } + async fn connect(&mut self, deadline: Instant) -> Vec { - match self.open_session() { - Ok(changes) => vec![ScreenInput::Boot(changes), ScreenInput::Reconnected], + match self.open_watched().await { + Ok(watched) => { + self.link = Link::Connected(watched.connection); + vec![ScreenInput::Boot(watched.changes), ScreenInput::Reconnected] + } Err(_) if Instant::now() < deadline => { sleep(FRAME_INTERVAL).await; self.link = Link::Connecting { deadline }; @@ -126,15 +144,26 @@ impl BusFeed { } } - fn receive(&mut self, readiness: AsyncFd) -> Vec { - let Ok(signals) = readiness.get_ref().receive() else { + async fn receive(&mut self, mut connection: BusConnection) -> Vec { + let Ok(signals) = connection.receive_signals(FRAME_INTERVAL).await else { return self.lose_connection(); }; - let changes = signals - .into_iter() - .flat_map(|signal| self.changes_for(readiness.get_ref(), signal)) - .collect(); - self.link = Link::Connected(readiness); + let progress = stream::iter(signals) + .fold( + FeedProgress { + feed: &mut *self, + connection: &mut connection, + changes: Vec::new(), + }, + |mut progress, signal| async move { + let changes = progress.feed.changes_for(progress.connection, signal).await; + progress.changes.extend(changes); + progress + }, + ) + .await; + let changes = progress.changes; + self.link = Link::Connected(connection); boot_inputs(changes) } @@ -156,30 +185,36 @@ impl BusFeed { boot_inputs(vec![BootChange::SwitchRoot { at }]) } - fn requested_root(&self, session: &Session, jobs: &[JobListing]) -> Result { + async fn requested_root( + &self, + connection: &mut BusConnection, + jobs: &[JobListing], + ) -> Result { let RootRequest::Named(target) = &self.target else { - return if self.phase == Phase::Shutdown { - Ok(SHUTDOWN_ROOTS - .into_iter() - .find(|root| { - jobs.iter() - .any(|job| job.unit == *root && job.kind == JobKind::BringsUp) - }) - .unwrap_or(SHUTDOWN_FALLBACK_ROOT) - .to_owned()) - } else { - session.default_target() - }; + if self.phase != Phase::Shutdown { + return connection.default_target().await; + } + return Ok(SHUTDOWN_ROOTS + .into_iter() + .find(|root| { + jobs.iter() + .any(|job| job.unit == *root && job.kind == JobKind::BringsUp) + }) + .unwrap_or(SHUTDOWN_FALLBACK_ROOT) + .to_owned()); }; Ok(target.clone()) } - fn origin_change(&mut self, session: &Session) -> Result, Error> { + async fn origin_change( + &mut self, + connection: &mut BusConnection, + ) -> Result, Error> { if self.gave_origin { return Ok(None); } self.gave_origin = true; - let origin = if session.boot_in_progress()? { + let origin = if connection.boot_in_progress().await? { Duration::ZERO } else { monotonic_now() @@ -187,48 +222,73 @@ impl BusFeed { Ok(Some(BootChange::SetOrigin(origin))) } - fn open_session(&mut self) -> Result, Error> { - let session = Session::open(self.scope)?; - session.subscribe()?; + async fn track_all( + &mut self, + connection: &mut BusConnection, + units: Vec, + ) -> Vec { + stream::iter(units) + .fold( + FeedProgress { + feed: &mut *self, + connection: &mut *connection, + changes: Vec::new(), + }, + |mut progress, unit| async move { + let tracked = progress.feed.track(progress.connection, &unit).await; + progress + .changes + .extend(tracked.and_then(|tracked| tracked.change)); + progress + }, + ) + .await + .changes + } + + async fn start_watching( + &mut self, + connection: &mut BusConnection, + ) -> Result, Error> { + connection.subscribe().await?; if self.scope == Scope::System { - session.set_show_status(SILENT_STATUS).ok(); + connection.set_show_status(StatusOutput::Silent).await.ok(); } let shutting_down = self.phase == Phase::Shutdown; - let jobs: Vec = session - .list_jobs()? + let jobs: Vec = connection + .list_jobs() + .await? .into_iter() .filter(|job| shutting_down || job.kind == JobKind::BringsUp) .collect(); - let requested_root = self.requested_root(&session, &jobs)?; - let origin_change = self.origin_change(&session)?; - let tracked_root = self.track(&session, &requested_root); + let requested_root = self.requested_root(connection, &jobs).await?; + let origin_change = self.origin_change(connection).await?; + let tracked_root = self.track(connection, &requested_root).await; let root = tracked_root .as_ref() .map_or(requested_root, |tracked| tracked.name.clone()); - let tracked_handoff = self.track(&session, HANDOFF_UNIT); + let tracked_handoff = self.track(connection, HANDOFF_UNIT).await; let handoff = tracked_handoff .as_ref() .map(|tracked| BootChange::SetHandoff(tracked.name.clone())); - let milestones = BootState::new(self.phase).milestone_targets; - let tracked_changes: Vec = jobs + let watched_units = jobs .iter() - .map(|job| job.unit.as_str()) - .chain(milestones.iter().map(String::as_str)) - .filter_map(|unit| self.track(&session, unit)) - .filter_map(|tracked| tracked.change) + .map(|job| job.unit.clone()) + .chain(BootState::new(self.phase).milestone_targets) .collect(); + let tracked_changes = self.track_all(connection, watched_units).await; eprintln!( "bootvine: watching {root} in {} as pid {} over {}", self.phase.label(), process::id(), - if session.peer_to_peer { + if connection.peer_to_peer() { "the private socket" } else { "the bus" }, ); let queued_at = monotonic_now(); - let changes = origin_change + Ok(origin_change .into_iter() .chain(tracked_root.and_then(|tracked| tracked.change)) .chain(tracked_handoff.and_then(|tracked| tracked.change)) @@ -241,12 +301,10 @@ impl BusFeed { kind: job.kind, at: queued_at, })) - .collect(); - self.link = Link::Connected(readable_source(session)?); - Ok(changes) + .collect()) } - fn track(&mut self, session: &Session, unit: &str) -> Option { + async fn track(&mut self, connection: &mut BusConnection, unit: &str) -> Option { if let Some(known) = self.unit_paths.values().find(|known| *known == unit) { return Some(TrackedUnit { name: known.clone(), @@ -257,7 +315,7 @@ impl BusFeed { path, facts, activity, - } = session.fetch_unit(unit).ok()?; + } = connection.fetch_unit(unit).await.ok()?; let name = facts.name.clone(); if self.unit_paths.values().any(|known| *known == name) { return Some(TrackedUnit { name, change: None }); @@ -269,10 +327,16 @@ impl BusFeed { }) } - fn changes_for(&mut self, session: &Session, signal: Signal) -> Vec { + async fn changes_for( + &mut self, + connection: &mut BusConnection, + signal: Signal, + ) -> Vec { let at = monotonic_now(); match signal { - Signal::JobNew { id, job, unit } => self.queue_new_job(session, id, &job, unit, at), + Signal::JobNew { id, job, unit } => { + self.queue_new_job(connection, id, &job, unit, at).await + } Signal::JobRemoved { id, result } => vec![BootChange::FinishJob { id, result, at }], Signal::UnitChanged { path, activity } => self .unit_paths @@ -286,19 +350,19 @@ impl BusFeed { } } - fn queue_new_job( + async fn queue_new_job( &mut self, - session: &Session, + connection: &mut BusConnection, id: u32, job: &Path<'static>, unit: String, at: Duration, ) -> Vec { - let kind = session.job_kind(job).unwrap_or(JobKind::Unknown); + let kind = connection.job_kind(job).await.unwrap_or(JobKind::Unknown); if kind == JobKind::TakesDown && self.phase != Phase::Shutdown { return Vec::new(); } - let tracked = self.track(session, &unit); + let tracked = self.track(connection, &unit).await; let unit = tracked .as_ref() .map_or(unit, |tracked| tracked.name.clone()); @@ -316,16 +380,6 @@ impl BusFeed { } } -impl Drop for BusFeed { - fn drop(&mut self) { - if let Link::Connected(readiness) = &self.link - && self.scope == Scope::System - { - readiness.get_ref().set_show_status(CONFIGURED_STATUS).ok(); - } - } -} - pub fn bus_inputs( phase: Phase, scope: Scope, diff --git a/src/systemd_link/link_error.rs b/src/systemd_link/link_error.rs index 56babd2..9afdd7f 100644 --- a/src/systemd_link/link_error.rs +++ b/src/systemd_link/link_error.rs @@ -13,6 +13,8 @@ pub enum Error { MalformedCall, #[error("the service manager closed the connection")] Disconnected, + #[error("the service manager did not answer in time")] + CallTimedOut, #[error("console: {0}")] Console(#[from] io::Error), #[error("entering the new root: {0}")] diff --git a/src/systemd_link/systemd_bus.rs b/src/systemd_link/systemd_bus.rs index f31dc95..5d65547 100644 --- a/src/systemd_link/systemd_bus.rs +++ b/src/systemd_link/systemd_bus.rs @@ -1,5 +1,6 @@ use std::{ - iter, + future::ready, + io, iter, mem, os::fd::{AsRawFd, RawFd}, time::Duration, }; @@ -9,9 +10,12 @@ use dbus::{ arg::{AppendAll, PropMap, RefArg, Variant}, channel::{BusType, Channel}, }; +use futures::{StreamExt, stream}; +use tokio::{io::unix::AsyncFd, time::timeout}; use crate::{ boot_model::{Activity, JobKind, JobResult, UnitFacts}, + console_session::stream_sources::{CarriedItem, readable_source, unfold_carrying}, systemd_link::link_error::Error, }; @@ -34,6 +38,21 @@ pub enum Scope { User, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum StatusOutput { + Silent, + Configured, +} + +impl StatusOutput { + fn bus_mode(self) -> &'static str { + match self { + Self::Silent => "no", + Self::Configured => "", + } + } +} + pub enum Signal { JobNew { id: u32, @@ -79,6 +98,49 @@ pub struct Session { pub peer_to_peer: bool, } +fn method_call( + destination: &str, + path: &str, + interface: &str, + member: &str, + arguments: impl AppendAll, +) -> Result { + let Ok(mut call) = Message::new_method_call(destination, path, interface, member) else { + return Err(Error::MalformedCall); + }; + call.append_all(arguments); + Ok(call) +} + +fn manager_call(member: &str, arguments: impl AppendAll) -> Result { + method_call( + DESTINATION, + MANAGER_PATH, + MANAGER_INTERFACE, + member, + arguments, + ) +} + +fn property_call(path: &str, interface: &str, property: &str) -> Result { + method_call( + DESTINATION, + path, + PROPERTIES_INTERFACE, + "Get", + (interface, property), + ) +} + +fn boot_in_progress_call() -> Result { + property_call(MANAGER_PATH, MANAGER_INTERFACE, "FinishTimestampMonotonic") +} + +fn read_boot_in_progress(reply: &Message) -> Result { + let finished: Variant = reply.read1()?; + Ok(finished.0 == 0) +} + impl Session { pub fn open(scope: Scope) -> Result { if scope == Scope::System @@ -92,13 +154,13 @@ impl Session { Scope::User => BusType::Session, }; let session = Self::watching(Channel::get_private(bus)?, false); - session.call( + session.blocking_call(method_call( BUS_DESTINATION, BUS_PATH, BUS_DESTINATION, "AddMatch", (SIGNAL_RULE,), - )?; + )?)?; Ok(session) } @@ -110,139 +172,192 @@ impl Session { } } - fn call( - &self, - destination: &str, - path: &str, - interface: &str, - member: &str, - arguments: impl AppendAll, - ) -> Result { - let Ok(mut call) = Message::new_method_call(destination, path, interface, member) else { - return Err(Error::MalformedCall); - }; - call.append_all(arguments); + fn blocking_call(&self, call: Message) -> Result { Ok(self.channel.send_with_reply_and_block(call, CALL_TIMEOUT)?) } - pub fn subscribe(&self) -> Result<(), Error> { - self.call( - DESTINATION, - MANAGER_PATH, - MANAGER_INTERFACE, - "Subscribe", - (), - )?; + pub fn set_show_status(&self, output: StatusOutput) -> Result<(), Error> { + self.blocking_call(manager_call("SetShowStatus", (output.bus_mode(),))?)?; Ok(()) } - pub fn set_show_status(&self, mode: &str) -> Result<(), Error> { - self.call( - DESTINATION, - MANAGER_PATH, - MANAGER_INTERFACE, - "SetShowStatus", - (mode,), - )?; - Ok(()) - } - - pub fn default_target(&self) -> Result { - let reply = self.call( - DESTINATION, - MANAGER_PATH, - MANAGER_INTERFACE, - "GetDefaultTarget", - (), - )?; - Ok(reply.read1()?) - } - pub fn boot_in_progress(&self) -> Result { - let reply = self.call( - DESTINATION, - MANAGER_PATH, - PROPERTIES_INTERFACE, - "Get", - (MANAGER_INTERFACE, "FinishTimestampMonotonic"), - )?; - let finished: Variant = reply.read1()?; - Ok(finished.0 == 0) + read_boot_in_progress(&self.blocking_call(boot_in_progress_call()?)?) + } +} + +pub struct BusConnection { + readiness: AsyncFd, + early_signals: Vec, +} + +fn into_results(batch: Result, Error>) -> Vec> { + match batch { + Ok(messages) => messages.into_iter().map(Ok).collect(), + Err(error) => vec![Err(error)], + } +} + +impl BusConnection { + pub fn register(session: Session) -> io::Result { + Ok(Self { + readiness: readable_source(session)?, + early_signals: Vec::new(), + }) } - pub fn list_jobs(&self) -> Result, Error> { - let reply = self.call(DESTINATION, MANAGER_PATH, MANAGER_INTERFACE, "ListJobs", ())?; + #[must_use] + pub fn peer_to_peer(&self) -> bool { + self.readiness.get_ref().peer_to_peer + } + + fn drain_replies(&mut self) -> Result, Error> { + let channel = &self.readiness.get_mut().channel; + if channel.read_write(Some(Duration::ZERO)).is_err() || !channel.is_connected() { + return Err(Error::Disconnected); + } + let (signals, replies): (Vec, Vec) = + iter::from_fn(|| channel.pop_message()) + .partition(|message| message.msg_type() == MessageType::Signal); + self.early_signals + .extend(signals.iter().filter_map(parse_signal)); + Ok(replies) + } + + async fn next_replies(&mut self) -> Result, Error> { + let drained = self.drain_replies()?; + if !drained.is_empty() { + return Ok(drained); + } + self.readiness.readable_mut().await?.clear_ready(); + self.drain_replies() + } + + async fn reply_to(&mut self, serial: u32) -> Result { + unfold_carrying(self, |connection| async move { + let replies = connection.next_replies().await; + Some(CarriedItem { + item: replies, + state: connection, + }) + }) + .flat_map(|replies| stream::iter(into_results(replies))) + .filter(|reply| { + ready( + reply + .as_ref() + .map_or(true, |message| message.get_reply_serial() == Some(serial)), + ) + }) + .take(1) + .fold(Err(Error::Disconnected), |_, reply| ready(reply)) + .await + } + + async fn call(&mut self, call: Message) -> Result { + let channel = &self.readiness.get_ref().channel; + let Ok(serial) = channel.send(call) else { + return Err(Error::Disconnected); + }; + channel.flush(); + let Ok(reply) = timeout(CALL_TIMEOUT, self.reply_to(serial)).await else { + return Err(Error::CallTimedOut); + }; + let mut reply = reply?; + reply.as_result()?; + Ok(reply) + } + + pub async fn subscribe(&mut self) -> Result<(), Error> { + self.call(manager_call("Subscribe", ())?).await?; + Ok(()) + } + + pub async fn set_show_status(&mut self, output: StatusOutput) -> Result<(), Error> { + self.call(manager_call("SetShowStatus", (output.bus_mode(),))?) + .await?; + Ok(()) + } + + pub async fn default_target(&mut self) -> Result { + let reply = self.call(manager_call("GetDefaultTarget", ())?).await?; + Ok(reply.read1()?) + } + + pub async fn boot_in_progress(&mut self) -> Result { + read_boot_in_progress(&self.call(boot_in_progress_call()?).await?) + } + + pub async fn list_jobs(&mut self) -> Result, Error> { + let reply = self.call(manager_call("ListJobs", ())?).await?; let listed: Vec = reply.read1()?; Ok(listed.into_iter().map(JobListing::from).collect()) } - pub fn job_kind(&self, job: &Path) -> Result { - let reply = self.call( - DESTINATION, - job, - PROPERTIES_INTERFACE, - "Get", - (JOB_INTERFACE, "JobType"), - )?; + pub async fn job_kind(&mut self, job: &Path<'_>) -> Result { + let reply = self + .call(property_call(job, JOB_INTERFACE, "JobType")?) + .await?; let kind: Variant = reply.read1()?; Ok(serde_plain::from_str(&kind.0).unwrap_or(JobKind::Unknown)) } - pub fn fetch_unit(&self, name: &str) -> Result { + pub async fn fetch_unit(&mut self, name: &str) -> Result { let path: Path<'static> = self - .call( - DESTINATION, - MANAGER_PATH, - MANAGER_INTERFACE, - "GetUnit", - (name,), - )? + .call(manager_call("GetUnit", (name,))?) + .await? .read1()?; let properties: PropMap = self - .call( + .call(method_call( DESTINATION, &path, PROPERTIES_INTERFACE, "GetAll", (UNIT_INTERFACE,), - )? + )?) + .await? .read1()?; - let text = |property: &str| { - properties - .get(property) - .and_then(|value| value.0.as_str()) - .map(ToOwned::to_owned) - }; - let names = |property: &str| { - properties - .get(property) - .and_then(|value| value.0.as_iter()) - .map(|items| { - items - .filter_map(|item| item.as_str().map(ToOwned::to_owned)) - .collect() - }) - .unwrap_or_default() - }; Ok(FetchedUnit { - facts: UnitFacts { - name: text("Id").unwrap_or_else(|| name.to_owned()), - description: text("Description").unwrap_or_default(), - after: names("After"), - before: names("Before"), - }, + facts: unit_facts_from(name, &properties), activity: activity_from(&properties), path, }) } - pub fn receive(&self) -> Result, Error> { - if self.channel.read_write(Some(Duration::ZERO)).is_err() || !self.channel.is_connected() { - return Err(Error::Disconnected); + pub async fn receive_signals(&mut self, patience: Duration) -> Result, Error> { + if self.early_signals.is_empty() + && let Ok(Ok(mut guard)) = timeout(patience, self.readiness.readable_mut()).await + { + guard.clear_ready(); } - Ok(iter::from_fn(|| self.channel.pop_message()) - .filter_map(|message| parse_signal(&message)) - .collect()) + self.drain_replies()?; + Ok(mem::take(&mut self.early_signals)) + } +} + +fn unit_facts_from(name: &str, properties: &PropMap) -> UnitFacts { + let text = |property: &str| { + properties + .get(property) + .and_then(|value| value.0.as_str()) + .map(ToOwned::to_owned) + }; + let names = |property: &str| { + properties + .get(property) + .and_then(|value| value.0.as_iter()) + .map(|items| { + items + .filter_map(|item| item.as_str().map(ToOwned::to_owned)) + .collect() + }) + .unwrap_or_default() + }; + UnitFacts { + name: text("Id").unwrap_or_else(|| name.to_owned()), + description: text("Description").unwrap_or_default(), + after: names("After"), + before: names("Before"), } }