| 1 | use std::time::Duration; |
| 2 | |
| 3 | use itertools::Itertools; |
| 4 | use scheduler::Instant; |
| 5 | |
| 6 | #[cfg(feature = "profiler")] |
| 7 | use crate::action::Action; |
| 8 | |
| 9 | #[doc(hidden)] |
| 10 | #[derive(Clone)] |
| 11 | pub struct ActionStatistics { |
| 12 | runtime_to_beat: Duration, |
| 13 | |
| 14 | longest_runtimes: heapless::Vec<ActionTiming, 5>, |
| 15 | running: Option<(&'static str, Instant)>, |
| 16 | } |
| 17 | |
| 18 | impl std::fmt::Debug for ActionStatistics { |
| 19 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 20 | f.debug_struct("ActionStatistics") |
| 21 | .field("runtime_to_beat", &self.runtime_to_beat) |
| 22 | .field("longest_runtimes", &self.longest_runtimes) |
| 23 | .field( |
| 24 | "running", |
| 25 | &self.running.map(|(id, started)| (id, started.elapsed())), |
| 26 | ) |
| 27 | .finish() |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl std::fmt::Display for ActionStatistics { |
| 32 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 33 | f.write_str("Actions that blocked the longest\n")?; |
| 34 | for action in self |
| 35 | .longest_runtimes(true) |
| 36 | .sorted_by_key(|action| action.runtime()) |
| 37 | .rev() |
| 38 | { |
| 39 | f.write_fmt(format_args!( |
| 40 | "{:<20} - {}", |
| 41 | format!("{:?}", action.runtime()), // impl dbg does not support alignment |
| 42 | action.name |
| 43 | ))?; |
| 44 | writeln!(f)?; |
| 45 | } |
| 46 | Ok(()) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl Default for ActionStatistics { |
| 51 | fn default() -> Self { |
| 52 | Self::new() |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl ActionStatistics { |
| 57 | const fn new() -> Self { |
| 58 | Self { |
| 59 | // This keeps more calls on the fast path by only tracking |
| 60 | // problematic polls |
| 61 | runtime_to_beat: Duration::from_micros(100), |
| 62 | longest_runtimes: heapless::Vec::new(), |
| 63 | running: None, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | pub fn take(&mut self) -> Self { |
| 68 | let taken = std::mem::take(self); |
| 69 | self.running = taken.running; |
| 70 | taken |
| 71 | } |
| 72 | |
| 73 | pub fn is_empty(&self) -> bool { |
| 74 | self.longest_runtimes.is_empty() |
| 75 | } |
| 76 | |
| 77 | #[cfg(feature = "profiler")] |
| 78 | pub fn update_running_action(&mut self, action: &'static str, started: Instant) { |
| 79 | self.running = Some((action, started)); |
| 80 | } |
| 81 | #[cfg(not(feature = "profiler"))] |
| 82 | pub fn update_running_action(&mut self, _action: &'static str, _started: Instant) {} |
| 83 | |
| 84 | #[cfg(feature = "profiler")] |
| 85 | pub fn save_action_timing(&mut self) { |
| 86 | let now = Instant::now(); |
| 87 | |
| 88 | let Some((action, started)) = self.running.take() else { |
| 89 | // Actions are ran only on the foreground executor and therefore |
| 90 | // sequentially _except_ in tests where they can run concurrently. |
| 91 | // |
| 92 | // When ran sequentially self.running will always be Some. When ran |
| 93 | // concurrently that is no longer true. But that is fine, we do not |
| 94 | // need to track action timings in tests. |
| 95 | std::hint::cold_path(); |
| 96 | return; |
| 97 | }; |
| 98 | |
| 99 | let timing = ActionTiming { |
| 100 | name: action, |
| 101 | start: started, |
| 102 | end: now, |
| 103 | }; |
| 104 | |
| 105 | let runtime = now.duration_since(started); |
| 106 | if runtime >= self.runtime_to_beat { |
| 107 | std::hint::cold_path(); // most actions are not the worst, optimize for that |
| 108 | |
| 109 | if self.longest_runtimes.is_full() |
| 110 | && let Some(to_replace) = self |
| 111 | .longest_runtimes |
| 112 | .iter_mut() |
| 113 | .min_by_key(|action| runtime >= action.runtime()) |
| 114 | { |
| 115 | *to_replace = timing; |
| 116 | } else { |
| 117 | self.longest_runtimes |
| 118 | .push(timing) |
| 119 | .expect("just checked it is not full"); |
| 120 | }; |
| 121 | |
| 122 | self.runtime_to_beat = self |
| 123 | .longest_runtimes |
| 124 | .iter() |
| 125 | .map(|action| action.runtime()) |
| 126 | .min() |
| 127 | .expect("never empty"); |
| 128 | } |
| 129 | } |
| 130 | #[cfg(not(feature = "profiler"))] |
| 131 | pub fn save_action_timing(&mut self) {} |
| 132 | |
| 133 | pub fn longest_runtimes(&self, include_running: bool) -> impl Iterator<Item = ActionTiming> { |
| 134 | self.longest_runtimes.iter().copied().chain( |
| 135 | self.running |
| 136 | .into_iter() |
| 137 | .filter(move |_| include_running) |
| 138 | .map(|(name, start)| ActionTiming { |
| 139 | name, |
| 140 | start, |
| 141 | end: Instant::now(), |
| 142 | }), |
| 143 | ) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #[doc(hidden)] |
| 148 | /// UNSTABLE only for use in the profiler and zed-reliability |
| 149 | #[derive(Copy, Clone)] |
| 150 | pub struct ActionTiming { |
| 151 | pub name: &'static str, |
| 152 | pub start: Instant, |
| 153 | pub end: Instant, |
| 154 | } |
| 155 | |
| 156 | impl core::fmt::Debug for ActionTiming { |
| 157 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 158 | f.debug_struct("ActionTiming") |
| 159 | .field("name", &self.name) |
| 160 | .field("runtime", &self.runtime()) |
| 161 | .finish() |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | impl ActionTiming { |
| 166 | pub fn duration(&self) -> Duration { |
| 167 | self.end.saturating_duration_since(self.start) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl ActionTiming { |
| 172 | #[doc(hidden)] |
| 173 | pub fn runtime(&self) -> Duration { |
| 174 | self.end - self.start |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // The profiler is careful to never block when the lock is held, therefore a |
| 179 | // spinlock is optimal. |
| 180 | #[cfg(feature = "profiler")] |
| 181 | static ACTION_STATISTICS: spin::Mutex<ActionStatistics> = |
| 182 | const { spin::Mutex::new(ActionStatistics::new()) }; |
| 183 | |
| 184 | #[doc(hidden)] |
| 185 | #[cfg(feature = "profiler")] |
| 186 | pub(crate) fn update_running_action( |
| 187 | action: &(dyn Action + 'static), |
| 188 | cx: &mut crate::App, |
| 189 | ) -> &'static str { |
| 190 | let now = Instant::now(); |
| 191 | let action = action.type_id(); |
| 192 | let action = cx.actions.try_resolve_action(&action).unwrap_or("un-named"); |
| 193 | ACTION_STATISTICS.lock().update_running_action(action, now); |
| 194 | action |
| 195 | } |
| 196 | |
| 197 | #[doc(hidden)] |
| 198 | #[cfg(feature = "profiler")] |
| 199 | pub(crate) fn save_action_timing() { |
| 200 | ACTION_STATISTICS.lock().save_action_timing(); |
| 201 | } |
| 202 | |
| 203 | #[doc(hidden)] |
| 204 | #[cfg(feature = "profiler")] |
| 205 | pub fn take_action_stats() -> ActionStatistics { |
| 206 | ACTION_STATISTICS.lock().take() |
| 207 | } |
| 208 | |
| 209 | #[doc(hidden)] |
| 210 | #[cfg(not(feature = "profiler"))] |
| 211 | pub fn take_action_stats() -> ActionStatistics { |
| 212 | ActionStatistics::default() |
| 213 | } |