Skip to content

Commit 6e70c57

Browse files
mladedavhds
authored andcommitted
tracing-subscriber: count numbers of enters in Timings (#2944)
1 parent c01d4fd commit 6e70c57

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

tracing-subscriber/src/fmt/fmt_layer.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,12 @@ where
890890
let span = ctx.span(id).expect("Span not found, this is a bug");
891891
let mut extensions = span.extensions_mut();
892892
if let Some(timings) = extensions.get_mut::<Timings>() {
893-
let now = Instant::now();
894-
timings.idle += (now - timings.last).as_nanos() as u64;
895-
timings.last = now;
893+
if timings.entered_count == 0 {
894+
let now = Instant::now();
895+
timings.idle += (now - timings.last).as_nanos() as u64;
896+
timings.last = now;
897+
}
898+
timings.entered_count += 1;
896899
}
897900

898901
if self.fmt_span.trace_enter() {
@@ -910,9 +913,12 @@ where
910913
let span = ctx.span(id).expect("Span not found, this is a bug");
911914
let mut extensions = span.extensions_mut();
912915
if let Some(timings) = extensions.get_mut::<Timings>() {
913-
let now = Instant::now();
914-
timings.busy += (now - timings.last).as_nanos() as u64;
915-
timings.last = now;
916+
timings.entered_count -= 1;
917+
if timings.entered_count == 0 {
918+
let now = Instant::now();
919+
timings.busy += (now - timings.last).as_nanos() as u64;
920+
timings.last = now;
921+
}
916922
}
917923

918924
if self.fmt_span.trace_exit() {
@@ -934,7 +940,9 @@ where
934940
busy,
935941
mut idle,
936942
last,
943+
entered_count,
937944
} = *timing;
945+
debug_assert_eq!(entered_count, 0);
938946
idle += (Instant::now() - last).as_nanos() as u64;
939947

940948
let t_idle = field::display(TimingDisplay(idle));
@@ -1224,6 +1232,7 @@ struct Timings {
12241232
idle: u64,
12251233
busy: u64,
12261234
last: Instant,
1235+
entered_count: u64,
12271236
}
12281237

12291238
impl Timings {
@@ -1232,6 +1241,7 @@ impl Timings {
12321241
idle: 0,
12331242
busy: 0,
12341243
last: Instant::now(),
1244+
entered_count: 0,
12351245
}
12361246
}
12371247
}

0 commit comments

Comments
 (0)