1use struct_field_names_as_array::FieldNamesAsArray;
7use thiserror::Error;
8use tracing::error;
9
10use crate::units::SystemdUnitStats;
11
12#[derive(Error, Debug)]
13pub enum MonitordTimerError {
14 #[error("Timer D-Bus error: {0}")]
15 ZbusError(#[from] zbus::Error),
16}
17
18#[derive(
19 serde::Serialize, serde::Deserialize, Clone, Debug, Default, Eq, FieldNamesAsArray, PartialEq,
20)]
21
22pub struct TimerStats {
25 pub accuracy_usec: u64,
27 pub fixed_random_delay: bool,
29 pub last_trigger_usec: u64,
31 pub last_trigger_usec_monotonic: u64,
33 pub next_elapse_usec_monotonic: u64,
35 pub next_elapse_usec_realtime: u64,
37 pub persistent: bool,
39 pub randomized_delay_usec: u64,
41 pub remain_after_elapse: bool,
43 pub service_unit_last_state_change_usec: u64,
45 pub service_unit_last_state_change_usec_monotonic: u64,
47}
48
49pub const TIMER_STATS_FIELD_NAMES: &[&str] = &TimerStats::FIELD_NAMES_AS_ARRAY;
50
51pub async fn collect_timer_stats(
52 connection: &zbus::Connection,
53 stats: &mut SystemdUnitStats,
54 unit: &crate::units::ListedUnit,
55) -> Result<TimerStats, MonitordTimerError> {
56 let mut timer_stats = TimerStats::default();
57
58 let pt = crate::dbus::zbus_timer::TimerProxy::builder(connection)
59 .path(unit.unit_object_path.clone())?
60 .build()
61 .await?;
62 let service_unit = pt.unit().await?;
65 let mut service_unit_last_state_change_usec: Result<u64, zbus::Error> = Ok(0);
66 let mut service_unit_last_state_change_usec_monotonic: Result<u64, zbus::Error> = Ok(0);
67 if service_unit.is_empty() {
68 error!("{}: No service unit name found for timer.", unit.name);
69 } else {
70 let mp = crate::dbus::zbus_systemd::ManagerProxy::new(connection).await?;
72 let service_unit_path = mp.get_unit(&service_unit).await?;
73 let up = crate::dbus::zbus_unit::UnitProxy::builder(connection)
75 .path(service_unit_path)?
76 .build()
77 .await?;
78
79 (
80 service_unit_last_state_change_usec,
81 service_unit_last_state_change_usec_monotonic,
82 ) = tokio::join!(
83 up.state_change_timestamp(),
84 up.state_change_timestamp_monotonic(),
85 );
86 }
87 timer_stats.service_unit_last_state_change_usec = service_unit_last_state_change_usec?;
88 timer_stats.service_unit_last_state_change_usec_monotonic =
89 service_unit_last_state_change_usec_monotonic?;
90
91 let (
94 accuracy_usec,
95 fixed_random_delay,
96 last_trigger_usec,
97 last_trigger_usec_monotonic,
98 persistent,
99 next_elapse_usec_monotonic,
100 next_elapse_usec_realtime,
101 randomized_delay_usec,
102 remain_after_elapse,
103 ) = tokio::join!(
104 pt.accuracy_usec(),
105 pt.fixed_random_delay(),
106 pt.last_trigger_usec(),
107 pt.last_trigger_usec_monotonic(),
108 pt.persistent(),
109 pt.next_elapse_usec_monotonic(),
110 pt.next_elapse_usec_realtime(),
111 pt.randomized_delay_usec(),
112 pt.remain_after_elapse(),
113 );
114
115 timer_stats.accuracy_usec = accuracy_usec?;
116 timer_stats.fixed_random_delay = fixed_random_delay?;
117 timer_stats.last_trigger_usec = last_trigger_usec?;
118 timer_stats.last_trigger_usec_monotonic = last_trigger_usec_monotonic?;
119 timer_stats.persistent = persistent?;
120 timer_stats.next_elapse_usec_monotonic = next_elapse_usec_monotonic?;
121 timer_stats.next_elapse_usec_realtime = next_elapse_usec_realtime?;
122 timer_stats.randomized_delay_usec = randomized_delay_usec?;
123 timer_stats.remain_after_elapse = remain_after_elapse?;
124
125 if timer_stats.persistent {
126 stats.timer_persistent_units += 1;
127 }
128
129 if timer_stats.remain_after_elapse {
130 stats.timer_remain_after_elapse += 1;
131 }
132
133 Ok(timer_stats)
134}