-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ariel Ben-Yehuda
committed
Nov 14, 2024
1 parent
772e0ca
commit f26e43e
Showing
3 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#![allow(unknown_lints, unexpected_cfgs)] | ||
#![cfg(all( | ||
tokio_unstable, | ||
tokio_taskdump, | ||
target_os = "linux", | ||
any(target_arch = "aarch64", target_arch = "x86", target_arch = "x86_64") | ||
))] | ||
|
||
use std::{ | ||
future::Future, | ||
mem, | ||
pin::Pin, | ||
sync::{Arc, Mutex}, | ||
task::{Context, Poll}, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use tokio::runtime::dump::{Root, Trace}; | ||
|
||
pin_project_lite::pin_project! { pub struct PrettyFuture<F: Future> { | ||
#[pin] | ||
f: Root<F>, | ||
t_last: Option<Instant>, | ||
logs: Arc<Mutex<Vec<Trace>>>, | ||
} | ||
} | ||
|
||
impl<F: Future> PrettyFuture<F> { | ||
pub fn pretty(f: F, logs: Arc<Mutex<Vec<Trace>>>) -> Self { | ||
PrettyFuture { | ||
f: Trace::root(f), | ||
t_last: None, | ||
logs, | ||
} | ||
} | ||
} | ||
|
||
impl<F: Future> Future for PrettyFuture<F> { | ||
type Output = F::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<F::Output> { | ||
let mut this = self.project(); | ||
let now = Instant::now(); | ||
let t_last = mem::replace(this.t_last, Some(now)); | ||
if t_last.is_some_and(|t_last| now.duration_since(t_last) > Duration::from_millis(10)) { | ||
let (res, trace) = tokio::runtime::dump::Trace::capture(|| this.f.as_mut().poll(cx)); | ||
this.logs.lock().unwrap().push(trace); | ||
return res; | ||
} | ||
this.f.as_mut().poll(cx) | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn task_trace_self() { | ||
let log = Arc::new(Mutex::new(vec![])); | ||
let log2 = Arc::new(Mutex::new(vec![])); | ||
let mut good_line = vec![]; | ||
let mut bad_line = vec![]; | ||
PrettyFuture::pretty( | ||
PrettyFuture::pretty( | ||
async { | ||
bad_line.push(line!() + 1); | ||
tokio::task::yield_now().await; | ||
bad_line.push(line!() + 1); | ||
tokio::time::sleep(Duration::from_millis(1)).await; | ||
good_line.push(line!() + 1); | ||
tokio::time::sleep(Duration::from_millis(1000)).await; | ||
}, | ||
log.clone(), | ||
), | ||
log2.clone(), | ||
) | ||
.await; | ||
for line in good_line { | ||
let s = format!("{}:{}:", file!(), line); | ||
assert!(log.lock().unwrap().iter().any(|x| { | ||
eprintln!("{}", x); | ||
format!("{}", x).contains(&s) | ||
})); | ||
} | ||
for line in bad_line { | ||
let s = format!("{}:{}:", file!(), line); | ||
assert!(!log | ||
.lock() | ||
.unwrap() | ||
.iter() | ||
.any(|x| format!("{}", x).contains(&s))); | ||
} | ||
} |