Skip to content

Commit

Permalink
tracing-subscriber: count numbers of enters in Timings
Browse files Browse the repository at this point in the history
  • Loading branch information
mladedav committed May 3, 2024
1 parent 9adfa7b commit d3e6cfc
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions tracing-subscriber/src/fmt/fmt_subscriber.rs
Expand Up @@ -870,9 +870,12 @@ where
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();
if let Some(timings) = extensions.get_mut::<Timings>() {
let now = Instant::now();
timings.idle += (now - timings.last).as_nanos() as u64;
timings.last = now;
if timings.entered_count == 0 {
let now = Instant::now();
timings.idle += (now - timings.last).as_nanos() as u64;
timings.last = now;
}
timings.entered_count += 1;
}

if self.fmt_span.trace_enter() {
Expand All @@ -890,9 +893,12 @@ where
let span = ctx.span(id).expect("Span not found, this is a bug");
let mut extensions = span.extensions_mut();
if let Some(timings) = extensions.get_mut::<Timings>() {
let now = Instant::now();
timings.busy += (now - timings.last).as_nanos() as u64;
timings.last = now;
timings.entered_count -= 1;
if timings.entered_count == 0 {
let now = Instant::now();
timings.busy += (now - timings.last).as_nanos() as u64;
timings.last = now;
}
}

if self.fmt_span.trace_exit() {
Expand All @@ -914,7 +920,9 @@ where
busy,
mut idle,
last,
entered_count,
} = *timing;
debug_assert_eq!(entered_count, 0);
idle += (Instant::now() - last).as_nanos() as u64;

let t_idle = field::display(TimingDisplay(idle));
Expand Down Expand Up @@ -1204,6 +1212,7 @@ struct Timings {
idle: u64,
busy: u64,
last: Instant,
entered_count: u64,
}

impl Timings {
Expand All @@ -1212,6 +1221,7 @@ impl Timings {
idle: 0,
busy: 0,
last: Instant::now(),
entered_count: 0,
}
}
}
Expand Down

0 comments on commit d3e6cfc

Please sign in to comment.