Skip to content

Commit c8d44a3

Browse files
committed
tracing-subscriber: count numbers of enters in Timings
1 parent 908cc43 commit c8d44a3

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

tracing-subscriber/src/fmt/fmt_subscriber.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,12 @@ where
870870
let span = ctx.span(id).expect("Span not found, this is a bug");
871871
let mut extensions = span.extensions_mut();
872872
if let Some(timings) = extensions.get_mut::<Timings>() {
873-
let now = Instant::now();
874-
timings.idle += (now - timings.last).as_nanos() as u64;
875-
timings.last = now;
873+
if timings.entered_count == 0 {
874+
let now = Instant::now();
875+
timings.idle += (now - timings.last).as_nanos() as u64;
876+
timings.last = now;
877+
}
878+
timings.entered_count += 1;
876879
}
877880

878881
if self.fmt_span.trace_enter() {
@@ -890,9 +893,12 @@ where
890893
let span = ctx.span(id).expect("Span not found, this is a bug");
891894
let mut extensions = span.extensions_mut();
892895
if let Some(timings) = extensions.get_mut::<Timings>() {
893-
let now = Instant::now();
894-
timings.busy += (now - timings.last).as_nanos() as u64;
895-
timings.last = now;
896+
timings.entered_count -= 1;
897+
if timings.entered_count == 0 {
898+
let now = Instant::now();
899+
timings.busy += (now - timings.last).as_nanos() as u64;
900+
timings.last = now;
901+
}
896902
}
897903

898904
if self.fmt_span.trace_exit() {
@@ -914,7 +920,9 @@ where
914920
busy,
915921
mut idle,
916922
last,
923+
entered_count,
917924
} = *timing;
925+
debug_assert_eq!(entered_count, 0);
918926
idle += (Instant::now() - last).as_nanos() as u64;
919927

920928
let t_idle = field::display(TimingDisplay(idle));
@@ -1204,6 +1212,7 @@ struct Timings {
12041212
idle: u64,
12051213
busy: u64,
12061214
last: Instant,
1215+
entered_count: u64,
12071216
}
12081217

12091218
impl Timings {
@@ -1212,6 +1221,7 @@ impl Timings {
12121221
idle: 0,
12131222
busy: 0,
12141223
last: Instant::now(),
1224+
entered_count: 0,
12151225
}
12161226
}
12171227
}

0 commit comments

Comments
 (0)