Skip to content

Commit 010d902

Browse files
bors[bot]michalt
andauthored
Merge #2726
2726: Improve profiling output when duration filter is specified r=matklad a=michalt In particular: - Use strict inequality for comparisons, since that's what the filter syntax supports. - Convert to millis for comparisons, since that's the unit used both for the filter and when printing. Now something like `RA_PROFILE='*>0'` will only print things that took at least 1ms (when rounded to millis). Signed-off-by: Michal Terepeta <michal.terepeta@gmail.com> Co-authored-by: Michal Terepeta <michal.terepeta@gmail.com>
2 parents b86ab52 + bc91120 commit 010d902

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

crates/ra_prof/src/lib.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ impl Drop for Profiler {
197197
if level == 0 {
198198
let stdout = stderr();
199199
let longer_than = stack.filter_data.longer_than;
200-
if duration >= longer_than {
200+
// Convert to millis for comparison to avoid problems with rounding
201+
// (otherwise we could print `0ms` despite user's `>0` filter when
202+
// `duration` is just a few nanos).
203+
if duration.as_millis() > longer_than.as_millis() {
201204
print(0, &stack.messages, &mut stdout.lock(), longer_than, None);
202205
}
203206
stack.messages.clear();
@@ -226,7 +229,7 @@ fn print(
226229
continue;
227230
}
228231
accounted_for += duration;
229-
if duration >= longer_than {
232+
if duration.as_millis() > longer_than.as_millis() {
230233
writeln!(out, "{}{:5}ms - {}", indent, duration.as_millis(), msg)
231234
.expect("printing profiling info to stdout");
232235

@@ -251,8 +254,9 @@ fn print(
251254

252255
if let Some(total) = total {
253256
if let Some(unaccounted) = total.checked_sub(accounted_for) {
254-
if unaccounted >= longer_than && last > 0 {
255-
writeln!(out, "{}{:5}ms - ???", indent, unaccounted.as_millis())
257+
let unaccounted_millis = unaccounted.as_millis();
258+
if unaccounted_millis > longer_than.as_millis() && unaccounted_millis > 0 && last > 0 {
259+
writeln!(out, "{}{:5}ms - ???", indent, unaccounted_millis)
256260
.expect("printing profiling info to stdout");
257261
}
258262
}
@@ -356,4 +360,21 @@ mod tests {
356360
fn profiling_function2() {
357361
let _p = profile("profile2");
358362
}
363+
364+
#[test]
365+
fn test_longer_than() {
366+
let mut result = vec![];
367+
let msgs = vec![
368+
Message { level: 1, duration: Duration::from_nanos(3), message: "bar".to_owned() },
369+
Message { level: 1, duration: Duration::from_nanos(2), message: "bar".to_owned() },
370+
Message { level: 0, duration: Duration::from_millis(1), message: "foo".to_owned() },
371+
];
372+
print(0, &msgs, &mut result, Duration::from_millis(0), Some(Duration::from_millis(1)));
373+
// The calls to `bar` are so short that they'll be rounded to 0ms and should get collapsed
374+
// when printing.
375+
assert_eq!(
376+
std::str::from_utf8(&result).unwrap(),
377+
" 1ms - foo\n 0ms - bar (2 calls)\n"
378+
);
379+
}
359380
}

0 commit comments

Comments
 (0)