feat: show timeout timer

This commit is contained in:
Willem Vanhulle committed 2026-10-25 12:11:21 +01:00
1 parent 9fa6e26d52
commit c63c6f1d28
13 files changed
+375 -43

No files matched your search

+3 -2
View File
@@ -131,8 +131,9 @@ const HOST: &[Step] = &[
step(
"sys-devices-virtual-misc-rfkill.device",
&[],
Duration::from_millis(4000),
),
Duration::from_millis(7000),
)
.timing_out_after(Duration::from_secs(90)),
step(
"systemd-rfkill.service",
&["sys-devices-virtual-misc-rfkill.device"],
+2 -1
View File
@@ -40,7 +40,8 @@ const SHUTDOWN: &[Step] = &[
&["display-manager.service"],
Duration::from_millis(1600),
),
stopping("docker.service", &[], Duration::from_millis(3200)),
stopping("docker.service", &[], Duration::from_millis(6500))
.timing_out_after(Duration::from_secs(90)),
stopping(
"containerd.service",
&["docker.service"],
+1 -1
View File
@@ -12,7 +12,7 @@ pub use self::{
boot_change::BootChange,
boot_projection::BootProjection,
reached_milestones::MilestoneRow,
unit_facts::{ActiveState, Activity, JobKind, JobResult, UnitFacts, UnitKind},
unit_facts::{ActiveState, Activity, JobKind, JobResult, UnitFacts, UnitKind, UnitTimeouts},
waiting_tree::{RowStatus, TreeNode, TreeRow, flatten_forest},
};
+73 -4
View File
@@ -25,6 +25,26 @@ impl UnitRecord {
transitioning.then(|| self.transition_started(job.kind).unwrap_or(job.queued_at))
}
pub(super) fn deadline(&self) -> Option<Duration> {
let Progress::Pending(job) = self.progress else {
return None;
};
let since = self.running_since()?;
let timeouts = self.facts.timeouts;
let transition = match job.kind {
JobKind::TakesDown => timeouts.stop,
JobKind::BringsUp | JobKind::Unknown => timeouts.start,
};
[
transition.map(|limit| since + limit),
timeouts.job_running.map(|limit| since + limit),
timeouts.job.map(|limit| job.queued_at + limit),
]
.into_iter()
.flatten()
.min()
}
fn is_pending(&self) -> bool {
matches!(self.progress, Progress::Pending(_))
}
@@ -271,10 +291,10 @@ impl BootState {
mod tests {
use super::*;
use crate::{
boot_model::RowStatus,
boot_model::{RowStatus, UnitTimeouts},
demo_script::boot_test_fixtures::{
MS, activating, facts, nested_boot_sample as sample, queue_unit, queued_boot, waiting,
waiting_rows,
MS, activating, facts, nested_boot_sample as sample, queue_unit, queued_boot, running,
waiting, waiting_rows,
},
};
@@ -368,7 +388,8 @@ mod tests {
waiting_rows(&state)[0].status,
RowStatus::Running {
queued_at: 1000 * MS,
since: 1200 * MS
since: 1200 * MS,
deadline: None,
}
);
state.finish_job(1, JobResult::Done, 1700 * MS);
@@ -382,4 +403,52 @@ mod tests {
}]
);
}
fn deadline_of(state: &BootState) -> Option<Duration> {
match waiting_rows(state)[0].status {
RowStatus::Running { deadline, .. } => deadline,
_ => None,
}
}
#[test]
fn the_earliest_timer_sets_the_deadline() {
let state = queued_boot(
Phase::Host,
1000 * MS,
[
running("web.service", &[], 3000 * MS).timing_out(UnitTimeouts {
start: Some(90_000 * MS),
job: Some(10_000 * MS),
..UnitTimeouts::default()
}),
],
);
assert_eq!(deadline_of(&state), Some(11_000 * MS));
}
#[test]
fn stopping_units_run_against_the_stop_timeout() {
let mut state = queued_boot(
Phase::Shutdown,
Duration::ZERO,
[waiting("db.service", &[])
.stopping()
.timing_out(UnitTimeouts {
start: Some(5000 * MS),
stop: Some(90_000 * MS),
..UnitTimeouts::default()
})],
);
assert_eq!(deadline_of(&state), None);
state.update_activity(
"db.service",
Activity {
state: Some(ActiveState::Deactivating),
active_exit: Some(2000 * MS),
..Activity::default()
},
);
assert_eq!(deadline_of(&state), Some(92_000 * MS));
}
}
+9
View File
@@ -26,12 +26,21 @@ pub enum JobResult {
Failed,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct UnitTimeouts {
pub start: Option<Duration>,
pub stop: Option<Duration>,
pub job: Option<Duration>,
pub job_running: Option<Duration>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnitFacts {
pub name: String,
pub description: Option<String>,
pub after: Vec<String>,
pub before: Vec<String>,
pub timeouts: UnitTimeouts,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+2
View File
@@ -29,6 +29,7 @@ pub enum RowStatus {
Running {
queued_at: Duration,
since: Duration,
deadline: Option<Duration>,
},
Failed,
Done,
@@ -83,6 +84,7 @@ impl UnitRecord {
.map_or(RowStatus::Waiting, |since| RowStatus::Running {
queued_at: job.queued_at,
since,
deadline: self.deadline(),
})
}
Progress::Finished(Outcome::Failed) => RowStatus::Failed,
+7 -1
View File
@@ -59,6 +59,10 @@ const ELAPSED: SpanPrinter = SpanPrinter::new()
const RUNNING_ELAPSED: SpanPrinter = ELAPSED.precision(Some(2));
const WHOLE_UNITS: SpanPrinter = SpanPrinter::new()
.designator(Designator::Compact)
.spacing(Spacing::None);
pub struct BootView<'a> {
pub state: &'a BootState,
pub projection: &'a BootProjection,
@@ -76,7 +80,9 @@ impl BootView<'_> {
.iter()
.filter(|row| row.kind != UnitKind::Target)
.filter_map(|row| match row.status {
RowStatus::Running { queued_at, since } => Some(RunningRun {
RowStatus::Running {
queued_at, since, ..
} => Some(RunningRun {
label: &row.label,
queued_at,
since,
+2 -6
View File
@@ -1,6 +1,5 @@
use std::{cmp::Reverse, iter::once, time::Duration};
use jiff::fmt::friendly::{Designator, Spacing, SpanPrinter};
use ratatui::{
buffer::Buffer,
layout::{Constraint, Layout, Rect},
@@ -13,7 +12,7 @@ use ratatui::{
},
};
use super::{fit_width, rule, unit_count};
use super::{WHOLE_UNITS, fit_width, rule, unit_count};
use crate::{
boot_model::{FinishedRun, MilestoneRow},
screen_settings::{color_scheme::TimelineColors, font_glyphs::Glyphs},
@@ -39,9 +38,6 @@ const AXIS_STEPS: [Duration; 12] = [
Duration::from_mins(30),
Duration::from_hours(1),
];
const AXIS_LABEL: SpanPrinter = SpanPrinter::new()
.designator(Designator::Compact)
.spacing(Spacing::None);
pub struct Timeline<'a> {
pub origin: Duration,
@@ -313,7 +309,7 @@ impl TimeAxis {
if offset.is_zero() && self.origin.is_zero() {
KERNEL_ORIGIN_LABEL.to_owned()
} else {
AXIS_LABEL.unsigned_duration_to_string(&offset)
WHOLE_UNITS.unsigned_duration_to_string(&offset)
}
}
}
+67 -1
View File
@@ -4,7 +4,7 @@ use unicode_width::UnicodeWidthStr;
use super::screen_render_fixtures::{boot_in_progress, chain_boot, described_boot, render};
use crate::{
boot_model::{JobResult, Phase},
boot_model::{BootState, JobResult, Phase, UnitTimeouts},
demo_script::boot_test_fixtures::{MS, queue_unit, queued_boot, running, waiting},
};
@@ -73,6 +73,72 @@ fn only_running_units_carry_a_timer() {
assert!(lines[5].ends_with("web"), "{screen}");
}
fn timing_out_boot() -> BootState {
let start_timeout = UnitTimeouts {
start: Some(Duration::from_secs(90)),
..UnitTimeouts::default()
};
queued_boot(
Phase::Host,
Duration::ZERO,
[
waiting(
"multi-user.target",
&["slow.service", "quick.service", "unlimited.service"],
),
running("slow.service", &[], 1000 * MS).timing_out(start_timeout),
running("quick.service", &[], 5000 * MS).timing_out(start_timeout),
running("unlimited.service", &[], 1000 * MS),
],
)
}
fn line_with<'a>(lines: &'a [String], text: &str) -> &'a str {
lines
.iter()
.find(|line| line.contains(text))
.unwrap_or_else(|| panic!("{text} missing\n{}", lines.join("\n")))
}
#[test]
fn slow_runs_show_their_timeout_in_a_column() {
let lines = render(&timing_out_boot(), 7400 * MS, 110, 24);
let screen = lines.join("\n");
assert!(
line_with(&lines, "Running").ends_with("Timeout"),
"{screen}"
);
assert!(
line_with(&lines, "slow").ends_with("6.40s 1m30s"),
"{screen}"
);
assert!(line_with(&lines, "quick").ends_with("2.40s"), "{screen}");
assert!(
line_with(&lines, "unlimited").ends_with("6.40s"),
"{screen}"
);
}
#[test]
fn the_timeout_column_waits_for_a_slow_run() {
let lines = render(&timing_out_boot(), 4000 * MS, 110, 24);
assert!(
lines.iter().all(|line| !line.contains("Timeout")),
"{}",
lines.join("\n")
);
}
#[test]
fn narrow_screens_keep_the_timeout() {
let lines = render(&timing_out_boot(), 7400 * MS, 50, 24);
assert!(
line_with(&lines, "slow").ends_with("1m30s"),
"{}",
lines.join("\n")
);
}
#[test]
fn chains_stay_unfolded_while_they_fit() {
let lines = render(&chain_boot(), 5000 * MS, 110, 24);
+72 -6
View File
@@ -10,7 +10,8 @@ use ratatui::{
use unicode_width::UnicodeWidthStr;
use super::{
BRANCH, DURATION_WIDTH, ENDED_TRUNK, LAST_BRANCH, PASSING_TRUNK, RUNNING_ELAPSED, fit_width,
BRANCH, DURATION_WIDTH, ENDED_TRUNK, LAST_BRANCH, PASSING_TRUNK, RUNNING_ELAPSED, WHOLE_UNITS,
fit_width,
};
use crate::{
boot_model::{RowStatus, TreeRow, UnitKind},
@@ -23,6 +24,13 @@ const MIN_DESCRIPTION_WIDTH: u16 = 12;
const MIN_TAG_WIDTH: u16 = 6;
const MIN_LABEL_WIDTH: u16 = 24;
const HEADER_ROWS: u16 = 1;
const SHOW_TIMEOUT_AFTER: Duration = Duration::from_secs(5);
const TIMEOUT_HEADER: &str = "Timeout";
struct RunTimeout {
limit: Duration,
exceeded: bool,
}
pub struct UnitTree<'a> {
pub phase: Line<'static>,
@@ -38,6 +46,7 @@ struct Columns {
label: u16,
tag: u16,
description: u16,
timeout: u16,
}
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -86,9 +95,49 @@ impl UnitTree<'_> {
name("Kind"),
name("Description"),
name("Running").right_aligned(),
name(TIMEOUT_HEADER).right_aligned(),
]))
}
fn run_timeout(&self, row: &TreeRow) -> Option<RunTimeout> {
let RowStatus::Running {
since,
deadline: Some(deadline),
..
} = row.status
else {
return None;
};
let elapsed = self.now.saturating_sub(since);
let limit = deadline.saturating_sub(since);
(elapsed >= SHOW_TIMEOUT_AFTER).then_some(RunTimeout {
limit,
exceeded: elapsed >= limit,
})
}
fn timeout_width(&self) -> u16 {
self.rows
.iter()
.filter_map(|row| self.run_timeout(row))
.map(|timeout| width_of(&WHOLE_UNITS.unsigned_duration_to_string(&timeout.limit)))
.max()
.map_or(0, |widest| widest.max(width_of(TIMEOUT_HEADER)))
}
fn timeout_cell(&self, row: &TreeRow) -> Line<'static> {
self.run_timeout(row)
.map_or_else(Line::default, |timeout| {
let color = if timeout.exceeded {
self.colors.failed_unit
} else {
self.colors.columns
};
Line::from(WHOLE_UNITS.unsigned_duration_to_string(&timeout.limit)).fg(color)
})
.right_aligned()
}
fn stages(&self) -> impl Iterator<Item = Stage> + '_ {
let current = self
.rows
@@ -164,7 +213,9 @@ impl UnitTree<'_> {
}
fn columns(&self, width: u16) -> Columns {
let unannotated = width.saturating_sub(DURATION_WIDTH + 1);
let timeout = self.timeout_width();
let timeout_room = if timeout > 0 { timeout + 1 } else { 0 };
let unannotated = width.saturating_sub(DURATION_WIDTH + 1 + timeout_room);
let annotated: Vec<(&TreeRow, Stage)> = self
.rows
.iter()
@@ -186,11 +237,13 @@ impl UnitTree<'_> {
label: unannotated,
tag: 0,
description: 0,
timeout,
},
|label| Columns {
label,
tag,
description: content - label - tag,
timeout,
},
)
}
@@ -229,14 +282,21 @@ impl UnitTree<'_> {
))
.fg(self.colors.columns),
Line::from(timer).style(style).right_aligned(),
self.timeout_cell(row),
]),
)
}
}
impl Columns {
fn shown<T>(&self, cells: [T; 4]) -> impl Iterator<Item = T> {
let present = [true, self.tag > 0, self.description > 0, true];
fn shown<T>(&self, cells: [T; 5]) -> impl Iterator<Item = T> {
let present = [
true,
self.tag > 0,
self.description > 0,
true,
self.timeout > 0,
];
cells
.into_iter()
.zip(present)
@@ -244,8 +304,14 @@ impl Columns {
}
fn widths(&self) -> impl Iterator<Item = Constraint> {
self.shown([self.label, self.tag, self.description, DURATION_WIDTH])
.map(Constraint::Length)
self.shown([
self.label,
self.tag,
self.description,
DURATION_WIDTH,
self.timeout,
])
.map(Constraint::Length)
}
}
+8 -1
View File
@@ -1,7 +1,8 @@
use std::{iter::once, time::Duration};
use crate::boot_model::{
ActiveState, Activity, BootState, JobKind, Phase, TreeRow, UnitFacts, flatten_forest,
ActiveState, Activity, BootState, JobKind, Phase, TreeRow, UnitFacts, UnitTimeouts,
flatten_forest,
};
pub const MS: Duration = Duration::from_millis(1);
@@ -18,6 +19,7 @@ pub fn facts(name: &str, after: &[&str]) -> UnitFacts {
description: None,
after: after.iter().map(ToString::to_string).collect(),
before: Vec::new(),
timeouts: UnitTimeouts::default(),
}
}
@@ -57,6 +59,11 @@ impl QueuedUnit {
self
}
pub fn timing_out(mut self, timeouts: UnitTimeouts) -> Self {
self.facts.timeouts = timeouts;
self
}
pub fn described(mut self, description: &str) -> Self {
self.facts.description = (!description.is_empty()).then(|| description.to_owned());
self
+17 -1
View File
@@ -12,7 +12,7 @@ use zeroize::Zeroizing;
use crate::{
boot_model::{
ActiveState, Activity, BootState, JobKind, JobResult, Phase, UnitFacts,
ActiveState, Activity, BootState, JobKind, JobResult, Phase, UnitFacts, UnitTimeouts,
boot_origin::BootOrigin,
},
boot_view::PasswordPrompt,
@@ -38,6 +38,17 @@ pub struct Step {
fails: bool,
stops: bool,
asks: Option<&'static str>,
timeout: Option<Duration>,
}
impl Step {
#[must_use]
pub const fn timing_out_after(self, limit: Duration) -> Self {
Self {
timeout: Some(limit),
..self
}
}
}
#[must_use]
@@ -49,6 +60,7 @@ pub const fn step(unit: &'static str, after: &'static [&'static str], work: Dura
fails: false,
stops: false,
asks: None,
timeout: None,
}
}
@@ -103,6 +115,10 @@ impl ScriptedJob {
description: describe(self.step.unit).map(str::to_owned),
after: after.into_iter().map(ToString::to_string).collect(),
before: before.into_iter().map(ToString::to_string).collect(),
timeouts: UnitTimeouts {
job_running: self.step.timeout,
..UnitTimeouts::default()
},
}
}
+112 -19
View File
@@ -15,7 +15,7 @@ use rustix::event::{PollFd, PollFlags, Timespec, poll};
use tokio::{io::unix::AsyncFd, time::timeout};
use crate::{
boot_model::{Activity, JobKind, JobResult, UnitFacts},
boot_model::{Activity, JobKind, JobResult, UnitFacts, UnitKind, UnitTimeouts},
console_session::stream_sources::{CarriedItem, readable_source, unfold_carrying},
systemd_link::link_error::Error,
};
@@ -24,6 +24,10 @@ const DESTINATION: &str = "org.freedesktop.systemd1";
const MANAGER_PATH: &str = "/org/freedesktop/systemd1";
const MANAGER_INTERFACE: &str = "org.freedesktop.systemd1.Manager";
const UNIT_INTERFACE: &str = "org.freedesktop.systemd1.Unit";
const SERVICE_INTERFACE: &str = "org.freedesktop.systemd1.Service";
const MOUNT_INTERFACE: &str = "org.freedesktop.systemd1.Mount";
const SWAP_INTERFACE: &str = "org.freedesktop.systemd1.Swap";
const SOCKET_INTERFACE: &str = "org.freedesktop.systemd1.Socket";
const JOB_INTERFACE: &str = "org.freedesktop.systemd1.Job";
const PROPERTIES_INTERFACE: &str = "org.freedesktop.DBus.Properties";
const PEER_INTERFACE: &str = "org.freedesktop.DBus.Peer";
@@ -32,6 +36,7 @@ const BUS_PATH: &str = "/org/freedesktop/DBus";
const PRIVATE_SOCKET: &str = "unix:path=/run/systemd/private";
const SIGNAL_RULE: &str = "type='signal',path_namespace='/org/freedesktop/systemd1'";
const CALL_TIMEOUT: Duration = Duration::from_secs(1);
const UNLIMITED: Duration = Duration::from_micros(u64::MAX);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Scope {
@@ -328,23 +333,31 @@ impl BusConnection {
.call(manager_call("GetUnit", (name,))?)
.await?
.read1()?;
let properties: PropMap = self
.call(method_call(
DESTINATION,
&path,
PROPERTIES_INTERFACE,
"GetAll",
(UNIT_INTERFACE,),
)?)
.await?
.read1()?;
let properties = self.all_properties(&path, UNIT_INTERFACE).await?;
let kind_properties = match kind_interface(UnitKind::of(&unit_id(name, &properties))) {
Some(interface) => self.all_properties(&path, interface).await?,
None => PropMap::new(),
};
Ok(FetchedUnit {
facts: unit_facts_from(name, &properties),
facts: unit_facts_from(name, &properties, &kind_properties),
activity: activity_from(&properties),
path,
})
}
async fn all_properties(&mut self, path: &Path<'_>, interface: &str) -> Result<PropMap, Error> {
Ok(self
.call(method_call(
DESTINATION,
path,
PROPERTIES_INTERFACE,
"GetAll",
(interface,),
)?)
.await?
.read1()?)
}
pub async fn receive_signals(&mut self, patience: Duration) -> Result<Vec<Signal>, Error> {
if self.early_signals.is_empty()
&& let Ok(Ok(mut guard)) = timeout(patience, self.session.readable_mut()).await
@@ -356,13 +369,51 @@ impl BusConnection {
}
}
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)
fn text_property(properties: &PropMap, property: &str) -> Option<String> {
properties
.get(property)
.and_then(|value| value.0.as_str())
.map(ToOwned::to_owned)
}
fn unit_id(name: &str, properties: &PropMap) -> String {
text_property(properties, "Id").unwrap_or_else(|| name.to_owned())
}
fn kind_interface(kind: UnitKind) -> Option<&'static str> {
match kind {
UnitKind::Service => Some(SERVICE_INTERFACE),
UnitKind::Mount => Some(MOUNT_INTERFACE),
UnitKind::Swap => Some(SWAP_INTERFACE),
UnitKind::Socket => Some(SOCKET_INTERFACE),
_ => None,
}
}
fn time_limit(properties: &PropMap, property: &str) -> Option<Duration> {
properties
.get(property)
.and_then(|value| value.0.as_u64())
.map(Duration::from_micros)
.filter(|limit| !limit.is_zero() && *limit != UNLIMITED)
}
fn timeouts_from(kind: UnitKind, unit: &PropMap, kind_specific: &PropMap) -> UnitTimeouts {
let (start, stop) = if kind == UnitKind::Service {
("TimeoutStartUSec", "TimeoutStopUSec")
} else {
("TimeoutUSec", "TimeoutUSec")
};
UnitTimeouts {
start: time_limit(kind_specific, start),
stop: time_limit(kind_specific, stop),
job: time_limit(unit, "JobTimeoutUSec"),
job_running: time_limit(unit, "JobRunningTimeoutUSec"),
}
}
fn unit_facts_from(name: &str, properties: &PropMap, kind_properties: &PropMap) -> UnitFacts {
let text = |property: &str| text_property(properties, property);
let names = |property: &str| {
properties
.get(property)
@@ -374,8 +425,10 @@ fn unit_facts_from(name: &str, properties: &PropMap) -> UnitFacts {
})
.unwrap_or_default()
};
let id = unit_id(name, properties);
UnitFacts {
name: text("Id").unwrap_or_else(|| name.to_owned()),
timeouts: timeouts_from(UnitKind::of(&id), properties, kind_properties),
name: id,
description: text("Description").filter(|description| !description.is_empty()),
after: names("After"),
before: names("Before"),
@@ -488,4 +541,44 @@ mod tests {
Some(Signal::BootFinished)
));
}
fn limit_properties(entries: &[(&str, Duration)]) -> PropMap {
entries
.iter()
.map(|(name, limit)| {
let micros = u64::try_from(limit.as_micros()).unwrap_or(u64::MAX);
(
(*name).to_owned(),
Variant(Box::new(micros) as Box<dyn RefArg>),
)
})
.collect()
}
#[test]
fn unlimited_timeouts_are_absent() {
let unit = limit_properties(&[
("JobTimeoutUSec", UNLIMITED),
("JobRunningTimeoutUSec", Duration::ZERO),
]);
let service = limit_properties(&[
("TimeoutStartUSec", UNLIMITED),
("TimeoutStopUSec", Duration::from_secs(90)),
]);
assert_eq!(
timeouts_from(UnitKind::Service, &unit, &service),
UnitTimeouts {
stop: Some(Duration::from_secs(90)),
..UnitTimeouts::default()
}
);
}
#[test]
fn mounts_use_one_timeout_for_both_directions() {
let mount = limit_properties(&[("TimeoutUSec", Duration::from_secs(20))]);
let timeouts = timeouts_from(UnitKind::Mount, &PropMap::new(), &mount);
assert_eq!(timeouts.start, Some(Duration::from_secs(20)));
assert_eq!(timeouts.stop, Some(Duration::from_secs(20)));
}
}