Timed polling

This commit is contained in:
QuantumEntangledAndy committed 2023-05-03 13:19:59 +07:00
1 parent ae3f9365b1
commit 29b6ff8c24
7 files changed
+76 -24

No files matched your search

+1 -1
View File
@@ -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"
+2
View File
@@ -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<()> {
+2 -2
View File
@@ -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,
+13 -10
View File
@@ -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) => {
+16 -8
View File
@@ -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(),
+5 -3
View File
@@ -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 {
+37
View File
@@ -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<F> {
name: String,
future: F,
}
impl<F: Future> TimedPoll<F> {
pub fn new<T: Into<String>>(name: T, future: F) -> Self {
Self {
name: name.into(),
future,
}
}
}
impl<F: Future> Future for TimedPoll<F> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<F::Output> {
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
}
}