Skip to content

Commit d9ab992

Browse files
committed
Make frame spans appear on a separate trace line
This was done by making the tracing_chrome tracing layer check if "tracing_separate_line" was in the arguments of a span, and act accordingly.
1 parent 7f2065a commit d9ab992

File tree

2 files changed

+57
-22
lines changed

2 files changed

+57
-22
lines changed

compiler/rustc_const_eval/src/interpret/stack.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
1212
use rustc_middle::{bug, mir};
1313
use rustc_mir_dataflow::impls::always_storage_live_locals;
1414
use rustc_span::Span;
15+
use tracing::field::Empty;
1516
use tracing::{info_span, instrument, trace};
1617

1718
use super::{
@@ -396,7 +397,11 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
396397
// Finish things up.
397398
M::after_stack_push(self)?;
398399
self.frame_mut().loc = Left(mir::Location::START);
399-
let span = info_span!("frame", "{}", instance);
400+
// `tracing_separate_line` is used to instruct the chrome_tracing [tracing::Layer] in Miri
401+
// to put the "frame" span on a separate trace line than other spans, to make the
402+
// visualization in https://ui.perfetto.dev easier to interpret. It is set to a value of
403+
// [tracing::field::Empty] so that other tracing layers (e.g. the logger) will ignore it.
404+
let span = info_span!("frame", tracing_separate_line = Empty, "{}", instance);
400405
self.frame_mut().tracing_span.enter(span);
401406

402407
interp_ok(())

src/tools/miri/src/bin/log/tracing_chrome.rs

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
// SPDX-License-Identifier: MIT
22
// SPDX-FileCopyrightText: Copyright (c) 2020 Thoren Paulson
3-
//! This file is taken unmodified from the following link, except for file attributes and
4-
//! `extern crate` at the top.
5-
//! https://github.com/thoren-d/tracing-chrome/blob/7e2625ab4aeeef2f0ef9bde9d6258dd181c04472/src/lib.rs
3+
//! This file was initially taken from the following link:
4+
//! <https://github.com/thoren-d/tracing-chrome/blob/7e2625ab4aeeef2f0ef9bde9d6258dd181c04472/src/lib.rs>
5+
//!
6+
//! The precise changes that were made to the original file can be found in git history
7+
//! (`git log -- path/to/tracing_chrome.rs`), but in summary:
8+
//! - the file attributes were changed and `extern crate` was added at the top
9+
//! - if a tracing span has a field called "tracing_separate_line", it will be given a separate span
10+
//! ID even in [TraceStyle::Threaded] mode, to make it appear on a separate line when viewing the
11+
//! trace in <https://ui.perfetto.dev>. This is the syntax to trigger this behavior:
12+
//! ```rust
13+
//! tracing::info_span!("my_span", tracing_separate_line = tracing::field::Empty, /* ... */)
14+
//! ```
15+
//!
616
//! Depending on the tracing-chrome crate from crates.io is unfortunately not possible, since it
717
//! depends on `tracing_core` which conflicts with rustc_private's `tracing_core` (meaning it would
818
//! not be possible to use the [ChromeLayer] in a context that expects a [Layer] from
@@ -79,14 +89,26 @@ where
7989
}
8090

8191
/// Decides how traces will be recorded.
92+
/// Also see <https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.jh64i9l3vwa1>
8293
#[derive(Default)]
8394
pub enum TraceStyle {
84-
/// Traces will be recorded as a group of threads.
95+
/// Traces will be recorded as a group of threads, and all spans on the same thread will appear
96+
/// on a single trace line in <https://ui.perfetto.dev>.
8597
/// In this style, spans should be entered and exited on the same thread.
98+
///
99+
/// If a tracing span has a field called "tracing_separate_line", it will be given a separate
100+
/// span ID even in this mode, to make it appear on a separate line when viewing the trace in
101+
/// <https://ui.perfetto.dev>. This is the syntax to trigger this behavior:
102+
/// ```rust
103+
/// tracing::info_span!("my_span", tracing_separate_line = tracing::field::Empty, /* ... */)
104+
/// ```
105+
/// [tracing::field::Empty] is used so that other tracing layers (e.g. the logger) will ignore
106+
/// the "tracing_separate_line" argument and not print out anything for it.
86107
#[default]
87108
Threaded,
88109

89-
/// Traces will recorded as a group of asynchronous operations.
110+
/// Traces will recorded as a group of asynchronous operations. All spans will be given separate
111+
/// span IDs and will appear on separate trace lines in <https://ui.perfetto.dev>.
90112
Async,
91113
}
92114

@@ -497,31 +519,39 @@ where
497519
}
498520
}
499521

500-
fn get_root_id(span: SpanRef<S>) -> u64 {
501-
span.scope()
502-
.from_root()
503-
.take(1)
504-
.next()
505-
.unwrap_or(span)
506-
.id()
507-
.into_u64()
522+
fn get_root_id(&self, span: SpanRef<S>) -> Option<u64> {
523+
match self.trace_style {
524+
TraceStyle::Threaded => {
525+
if span.fields().field("tracing_separate_line").is_some() {
526+
// put spans with argument "tracing_separate_line" on a separate trace line
527+
// by setting their "id", see
528+
// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.jh64i9l3vwa1
529+
Some(span.id().into_u64())
530+
} else {
531+
None
532+
}
533+
},
534+
TraceStyle::Async => Some(
535+
span.scope()
536+
.from_root()
537+
.take(1)
538+
.next()
539+
.unwrap_or(span)
540+
.id()
541+
.into_u64()
542+
),
543+
}
508544
}
509545

510546
fn enter_span(&self, span: SpanRef<S>, ts: f64) {
511547
let callsite = self.get_callsite(EventOrSpan::Span(&span));
512-
let root_id = match self.trace_style {
513-
TraceStyle::Async => Some(ChromeLayer::get_root_id(span)),
514-
_ => None,
515-
};
548+
let root_id = self.get_root_id(span);
516549
self.send_message(Message::Enter(ts, callsite, root_id));
517550
}
518551

519552
fn exit_span(&self, span: SpanRef<S>, ts: f64) {
520553
let callsite = self.get_callsite(EventOrSpan::Span(&span));
521-
let root_id = match self.trace_style {
522-
TraceStyle::Async => Some(ChromeLayer::get_root_id(span)),
523-
_ => None,
524-
};
554+
let root_id = self.get_root_id(span);
525555
self.send_message(Message::Exit(ts, callsite, root_id));
526556
}
527557

0 commit comments

Comments
 (0)