diff --git a/Cargo.toml b/Cargo.toml index 1fde31a..45938a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ regex = "1.7.3" rumqttc = "0.20.0" serde = { version = "1.0.160", features = ["derive"] } time = "0.3.20" -tokio = { version = "1.27.0", features = ["rt-multi-thread", "macros", "io-util"] } +tokio = { version = "1.27.0", features = ["rt", "macros", "io-util"] } # rt-multi-thread tokio-stream = "0.1.12" toml = "0.7.3" validator = "0.16.0" diff --git a/src/main.rs b/src/main.rs index 9a2b01d..a3a1db9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,10 +38,12 @@ mod reboot; mod rtsp; mod statusled; mod talk; +mod timed_poll; mod utils; use cmdline::{Command, Opt}; use config::Config; +pub(crate) use timed_poll::TimedPoll; #[tokio::main] async fn main() -> Result<()> { diff --git a/src/rtsp/gst/factory.rs b/src/rtsp/gst/factory.rs index 88b7af9..513b056 100644 --- a/src/rtsp/gst/factory.rs +++ b/src/rtsp/gst/factory.rs @@ -143,7 +143,7 @@ impl Default for NeoMediaFactoryImpl { ReceiverStream::new(datarx), ReceiverStream::new(rx_clientsender), ); - threads.spawn(async move { + threads.spawn(crate::TimedPoll::new("MediaSend", async move { loop { tokio::task::yield_now().await; match sender.run().await { @@ -157,7 +157,7 @@ impl Default for NeoMediaFactoryImpl { } unreachable!(); // Ok(()) - }); + })); Self { sender: datasender, diff --git a/src/rtsp/gst/sender.rs b/src/rtsp/gst/sender.rs index fb98e89..d17b5b6 100644 --- a/src/rtsp/gst/sender.rs +++ b/src/rtsp/gst/sender.rs @@ -314,13 +314,13 @@ impl NeoMediaSenders { } pub(super) async fn run(&mut self) -> AnyResult<()> { - let mut interval = tokio::time::interval(Duration::from_micros(LATENCY as u64 / 3)); + // let mut interval = tokio::time::interval(Duration::from_micros(LATENCY as u64 / 3)); loop { tokio::task::yield_now().await; tokio::select! { - _ = interval.tick() => { - self.handle_buffer().await - }, + // _ = interval.tick() => { + // self.handle_buffer().await + // }, Some(v) = self.data_source.next() => { self.handle_new_data(v).await }, @@ -643,12 +643,15 @@ impl NeoMediaSender { } // debug!("Buffer pushed"); let thread_appsrc = appsrc.clone(); // GObjects are refcounted - let res = tokio::task::spawn_blocking(move || { - thread_appsrc - .push_buffer(gst_buf.copy()) - .map(|_| ()) - .map_err(|_| anyhow!("Could not push buffer to appsrc")) - }) + let res = crate::TimedPoll::new( + "SendBuffer", + tokio::task::spawn_blocking(move || { + thread_appsrc + .push_buffer(gst_buf.copy()) + .map(|_| ()) + .map_err(|_| anyhow!("Could not push buffer to appsrc")) + }), + ) .await; match &res { Err(e) => { diff --git a/src/rtsp/gst/server.rs b/src/rtsp/gst/server.rs index e9b849d..37d135c 100644 --- a/src/rtsp/gst/server.rs +++ b/src/rtsp/gst/server.rs @@ -96,24 +96,30 @@ impl NeoRtspServer { main_loop_thread.run(); AnyResult::Ok(()) }); - self.imp() - .threads - .write() + crate::TimedPoll::new("ServerThreadsLock", self.imp().threads.write()) .await - .spawn(async move { handle.await? }); - self.imp().main_loop.write().await.replace(main_loop); + .spawn(crate::TimedPoll::new( + "ServerRun", + async move { handle.await? }, + )); + crate::TimedPoll::new("ServerMainLoop", self.imp().main_loop.write()) + .await + .replace(main_loop); Ok(()) } pub(crate) async fn quit(&self) -> AnyResult<()> { - if let Some(main_loop) = self.imp().main_loop.read().await.as_ref() { + if let Some(main_loop) = crate::TimedPoll::new("MainLoopQuit", self.imp().main_loop.read()) + .await + .as_ref() + { main_loop.quit(); } Ok(()) } pub(crate) async fn join(&self) -> AnyResult<()> { - let mut threads = self.imp().threads.write().await; + let mut threads = crate::TimedPoll::new("MainLoopJoin", self.imp().threads.write()).await; while let Some(thread) = threads.join_next().await { thread??; } @@ -251,7 +257,9 @@ impl NeoRtspServerImpl { self.threads .write() .await - .spawn(async move { thread_media.join().await }); + .spawn(crate::TimedPoll::new("RtspJoin", async move { + thread_media.join().await + })); vac.insert(FactoryData { factory: media, paths: Default::default(), diff --git a/src/rtsp/mod.rs b/src/rtsp/mod.rs index dbd7018..198a8b3 100644 --- a/src/rtsp/mod.rs +++ b/src/rtsp/mod.rs @@ -92,7 +92,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> { let mut set = tokio::task::JoinSet::new(); for mut camera in cameras.drain(..) { // Spawn each camera controller in it's own thread - set.spawn(async move { + set.spawn(crate::TimedPoll::new("CameraMain", async move { let shared = camera.shared.clone(); let name = camera.get_name(); let mut backoff = Duration::from_micros(125); @@ -122,7 +122,7 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> { } } Ok(()) - }); + })); } info!( "Starting RTSP Server at {}:{}", @@ -133,7 +133,9 @@ pub(crate) async fn main(_opt: Opt, mut config: Config) -> Result<()> { let bind_port = config.bind_port; rtsp.run(&bind_addr, bind_port).await?; let thread_rtsp = rtsp.clone(); - set.spawn(async move { thread_rtsp.join().await }); + set.spawn(crate::TimedPoll::new("RTSP Join", async move { + thread_rtsp.join().await + })); while let Some(joined) = set.join_next().await { match &joined { diff --git a/src/timed_poll.rs b/src/timed_poll.rs new file mode 100644 index 0000000..9d82edc --- /dev/null +++ b/src/timed_poll.rs @@ -0,0 +1,37 @@ +//! For debugging polls +//! +//! https://users.rust-lang.org/t/how-do-i-debug-a-tokio-hang-tokio-0-3-3/51235/3 +//! +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + +pub(crate) struct TimedPoll { + name: String, + future: F, +} + +impl TimedPoll { + pub fn new>(name: T, future: F) -> Self { + Self { + name: name.into(), + future, + } + } +} + +impl Future for TimedPoll { + type Output = F::Output; + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let name = self.name.clone(); + // log::debug!("{}: Start poll", name); + let before = std::time::Instant::now(); + let future = unsafe { Pin::map_unchecked_mut(self, |me| &mut me.future) }; + let res = future.poll(cx); + let elapsed = before.elapsed(); + if elapsed > std::time::Duration::from_micros(500) { + log::debug!("{}: Stop poll: {:?}", name, before.elapsed()); + } + res + } +}