diff --git a/komorebi/src/daemon.rs b/komorebi/src/daemon.rs index 3492bcd0..5b723e88 100644 --- a/komorebi/src/daemon.rs +++ b/komorebi/src/daemon.rs @@ -333,6 +333,7 @@ pub fn run() -> eyre::Result<()> { focus_manager::listen_for_notifications(wm.clone()); theme_manager::listen_for_notifications(); + crate::heartbeat::start(); listen_for_commands(wm.clone()); if let Some(port) = opts.tcp_port { diff --git a/komorebi/src/heartbeat.rs b/komorebi/src/heartbeat.rs new file mode 100644 index 00000000..d8039354 --- /dev/null +++ b/komorebi/src/heartbeat.rs @@ -0,0 +1,39 @@ +//! Evidence of a stall of the whole daemon. +//! +//! A thread that sleeps 100 ms in a loop and says so when it wakes up much later than that. If +//! it is late in the same moments in which every other thread of the daemon is silent, the +//! process (or the guest it runs in) was not scheduled; if only some threads are silent, one of +//! them holds something (see [`crate::lock_holder`]). + +use std::time::Duration; +use std::time::Instant; + +/// How long the heartbeat thread sleeps between two beats +const BEAT: Duration = Duration::from_millis(100); + +/// A beat later than this after the one before is logged +const LATE: Duration = Duration::from_millis(500); + +/// Starts the heartbeat thread +pub fn start() { + let _ = std::thread::Builder::new() + .name("heartbeat".to_string()) + .spawn(|| { + let mut last = Instant::now(); + + loop { + std::thread::sleep(BEAT); + + let now = Instant::now(); + let late = now.duration_since(last).saturating_sub(BEAT); + last = now; + + if late >= LATE { + tracing::warn!( + "heartbeat was {} ms late: the daemon was not scheduled for that long", + late.as_millis() + ); + } + } + }); +} diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index a93adca1..ef98074a 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -15,6 +15,7 @@ pub mod focus_manager; pub mod live_resize; pub use komorebi_collections::Lockable; pub use komorebi_collections::lockable_sequence; +pub mod heartbeat; pub mod lock_holder; pub mod monitor; pub mod monitor_reconciliator; diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index 2ee253f3..1ab0a946 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -114,6 +114,22 @@ struct WindowsConnectionPolicy; impl ConnectionPolicy for WindowsConnectionPolicy { fn on_accept(&self, stream: &UnixStream) { + // when connections come in: a request that waits in the backlog while the process is + // stalled is seen here late, next to the heartbeat + { + static LAST_ACCEPT: parking_lot::Mutex> = + parking_lot::Mutex::new(None); + let now = std::time::Instant::now(); + let since = LAST_ACCEPT + .lock() + .replace(now) + .map(|last| now.duration_since(last)); + tracing::debug!( + "accepted a command connection ({} ms since the previous one)", + since.map_or(-1, |d| d.as_millis() as i64) + ); + } + match stream.set_read_timeout(Some(Duration::from_secs(1))) { Ok(()) => {} Err(error) => tracing::error!("{}", error),