| 1 | use std::sync::mpsc::{self, RecvTimeoutError}; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use futures::channel::oneshot; |
| 5 | use scheduler::Instant; |
| 6 | |
| 7 | use super::{HangDetector, HangIncident}; |
| 8 | |
| 9 | /// Detection thresholds and polling cadence for |
| 10 | /// [`crate::App::start_hang_monitor`]. |
| 11 | #[derive(Debug, Clone, Copy)] |
| 12 | pub struct HangMonitorConfig { |
| 13 | /// Duration at which a single piece of foreground work counts as a hang. |
| 14 | pub threshold: Duration, |
| 15 | /// Total foreground spend within one interval that counts as a hang. |
| 16 | pub frame_budget: Duration, |
| 17 | /// How often the monitor thread drains the journal. |
| 18 | pub interval: Duration, |
| 19 | } |
| 20 | |
| 21 | /// Why [`crate::App::start_hang_monitor`] failed. |
| 22 | #[derive(Debug, thiserror::Error)] |
| 23 | pub enum HangMonitorError { |
| 24 | /// The monitor was already started for this app. |
| 25 | #[error("the hang monitor was already started")] |
| 26 | AlreadyStarted, |
| 27 | /// The monitor thread couldn't be spawned. |
| 28 | #[error("failed to spawn the hang monitor thread: {0}")] |
| 29 | Spawn(#[source] std::io::Error), |
| 30 | } |
| 31 | |
| 32 | /// Why a [`HangMonitor`] polled its detector. |
| 33 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 34 | pub enum HangMonitorPollReason { |
| 35 | /// The monitor's regular interval elapsed. |
| 36 | Interval, |
| 37 | /// A poll was requested before the application quits. Consumers should |
| 38 | /// deliver any batched results now. |
| 39 | Flush, |
| 40 | } |
| 41 | |
| 42 | /// The incidents a [`HangMonitor`] collected in one poll. |
| 43 | pub struct HangMonitorPoll { |
| 44 | /// Incidents sealed since the previous poll, possibly none. |
| 45 | pub incidents: Vec<HangIncident>, |
| 46 | /// See [`HangDetector::first_present_at`]. |
| 47 | pub first_present_at: Option<Instant>, |
| 48 | /// Why this poll happened. |
| 49 | pub reason: HangMonitorPollReason, |
| 50 | } |
| 51 | |
| 52 | enum Request { |
| 53 | Flush { done: oneshot::Sender<()> }, |
| 54 | } |
| 55 | |
| 56 | /// Polls a [`HangDetector`] on a dedicated OS thread. |
| 57 | /// |
| 58 | /// Detection runs off the foreground thread so incidents are still collected |
| 59 | /// and reported while GPUI's executors are stalled. The thread owns the |
| 60 | /// detector and the callback; other threads only send it requests. Dropping |
| 61 | /// the monitor stops the thread. |
| 62 | pub(crate) struct HangMonitor { |
| 63 | requests: mpsc::Sender<Request>, |
| 64 | } |
| 65 | |
| 66 | impl HangMonitor { |
| 67 | /// Starts a thread that polls `detector` every `interval` and passes each |
| 68 | /// poll's result, including empty ones, to `on_poll` on that thread. |
| 69 | /// |
| 70 | /// # Errors |
| 71 | /// |
| 72 | /// Returns an error when the thread can't be spawned. |
| 73 | pub(crate) fn spawn<F>( |
| 74 | mut detector: HangDetector, |
| 75 | interval: Duration, |
| 76 | mut on_poll: F, |
| 77 | ) -> std::io::Result<Self> |
| 78 | where |
| 79 | F: FnMut(HangMonitorPoll) + Send + 'static, |
| 80 | { |
| 81 | let (requests, receiver) = mpsc::channel(); |
| 82 | std::thread::Builder::new() |
| 83 | .name("HangDetection".to_string()) |
| 84 | .spawn(move || { |
| 85 | loop { |
| 86 | let (reason, done) = match receiver.recv_timeout(interval) { |
| 87 | Ok(Request::Flush { done }) => (HangMonitorPollReason::Flush, Some(done)), |
| 88 | Err(RecvTimeoutError::Timeout) => (HangMonitorPollReason::Interval, None), |
| 89 | Err(RecvTimeoutError::Disconnected) => break, |
| 90 | }; |
| 91 | let incidents = detector.poll(); |
| 92 | on_poll(HangMonitorPoll { |
| 93 | incidents, |
| 94 | first_present_at: detector.first_present_at(), |
| 95 | reason, |
| 96 | }); |
| 97 | if let Some(done) = done { |
| 98 | done.send(()).ok(); |
| 99 | } |
| 100 | } |
| 101 | })?; |
| 102 | Ok(Self { requests }) |
| 103 | } |
| 104 | |
| 105 | /// Asks the monitor thread to poll now with [`HangMonitorPollReason::Flush`] |
| 106 | /// without waiting for it. The returned receiver completes once the |
| 107 | /// callback has returned. Returns `None` if the thread has exited. |
| 108 | pub(crate) fn request_flush(&self) -> Option<oneshot::Receiver<()>> { |
| 109 | let (done, finished) = oneshot::channel(); |
| 110 | self.requests.send(Request::Flush { done }).ok()?; |
| 111 | Some(finished) |
| 112 | } |
| 113 | } |