File size: 1,391 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
use std::time::Instant;
/// Guard that emits a tracing event when dropped with the duration of the
/// lifetime of the guard.
pub struct DurationSpanGuard<F: FnOnce(u64)> {
start: Instant,
f: Option<F>,
}
impl<F: FnOnce(u64)> DurationSpanGuard<F> {
pub fn new(f: F) -> Self {
Self {
start: Instant::now(),
f: Some(f),
}
}
}
impl<F: FnOnce(u64)> Drop for DurationSpanGuard<F> {
fn drop(&mut self) {
if let Some(f) = self.f.take() {
f(self.start.elapsed().as_micros() as u64);
}
}
}
/// Creates a event-based span that traces a certain duration (lifetime of the
/// guard). It's not a real span, which means it can be used across threads.
///
/// It will trace a duration and not the time the cpu is doing actual work. This
/// way it can be used to trace non-cpu-time or time that is spend in other
/// processes.
#[macro_export]
macro_rules! duration_span {
($name:literal) => {
turbo_tasks::duration_span::DurationSpanGuard::new(|duration| {
turbo_tasks::macro_helpers::tracing::info!(name = $name, duration = duration);
})
};
($name:literal, $($arg:tt)+) => {
turbo_tasks::duration_span::DurationSpanGuard::new(|duration| {
turbo_tasks::macro_helpers::tracing::info!(name = $name, $($arg)+, duration = duration);
})
};
}
|