Skip to content

Commit 1814db8

Browse files
committed
Uniform enter_trace_span! and add documentation
The macro was uniformed between rustc_const_eval and miri
1 parent e5e79f8 commit 1814db8

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

compiler/rustc_const_eval/src/interpret/stack.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
397397
// Finish things up.
398398
M::after_stack_push(self)?;
399399
self.frame_mut().loc = Left(mir::Location::START);
400-
// `tracing_separate_thread` is used to instruct the chrome_tracing [tracing::Layer] in Miri
400+
// `tracing_separate_thread` is used to instruct the tracing_chrome [tracing::Layer] in Miri
401401
// to put the "frame" span on a separate trace thread/line than other spans, to make the
402-
// visualization in https://ui.perfetto.dev easier to interpret. It is set to a value of
402+
// visualization in <https://ui.perfetto.dev> easier to interpret. It is set to a value of
403403
// [tracing::field::Empty] so that other tracing layers (e.g. the logger) will ignore it.
404404
let span = info_span!("frame", tracing_separate_thread = Empty, "{}", instance);
405405
self.frame_mut().tracing_span.enter(span);

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,63 @@ pub trait EnteredTraceSpan {}
5353
impl EnteredTraceSpan for () {}
5454
impl EnteredTraceSpan for tracing::span::EnteredSpan {}
5555

56-
/// Shortand for calling [crate::interpret::Machine::enter_trace_span] on a [tracing::info_span].
56+
/// Shortand for calling [crate::interpret::Machine::enter_trace_span] on a [tracing::info_span!].
5757
/// This is supposed to be compiled out when [crate::interpret::Machine::enter_trace_span] has the
5858
/// default implementation (i.e. when it does not actually enter the span but instead returns `()`).
59+
/// This macro takes a type implementing the [crate::interpret::Machine] trait as its first argument
60+
/// and otherwise accepts the same syntax as [tracing::span!] (see some tips below).
5961
/// Note: the result of this macro **must be used** because the span is exited when it's dropped.
62+
///
63+
/// ### Syntax accepted by this macro
64+
///
65+
/// The full documentation for the [tracing::span!] syntax can be found at [tracing] under "Using the
66+
/// Macros". A few possibly confusing syntaxes are listed here:
67+
/// ```rust
68+
/// // logs a span named "hello" with a field named "arg" of value 42 (works only because
69+
/// // 42 implements the tracing::Value trait, otherwise use one of the options below)
70+
/// let _span = enter_trace_span!(M, "hello", arg = 42);
71+
/// // logs a field called "my_display_var" using the Display implementation
72+
/// let _span = enter_trace_span!(M, "hello", %my_display_var);
73+
/// // logs a field called "my_debug_var" using the Debug implementation
74+
/// let _span = enter_trace_span!(M, "hello", ?my_debug_var);
75+
/// ```
76+
///
77+
/// ### `NAME::SUBNAME` syntax
78+
///
79+
/// In addition to the syntax accepted by [tracing::span!], this macro optionally allows passing
80+
/// the span name (i.e. the first macro argument) in the form `NAME::SUBNAME` (without quotes) to
81+
/// indicate that the span has name "NAME" (usually the name of the component) and has an additional
82+
/// more specific name "SUBNAME" (usually the function name). The latter is passed to the [tracing]
83+
/// infrastructure as a span field with the name "NAME". This allows not being distracted by
84+
/// subnames when looking at the trace in <https://ui.perfetto.dev>, but when deeper introspection
85+
/// is needed within a component, it's still possible to view the subnames directly in the UI by
86+
/// selecting a span, clicking on the "NAME" argument on the right, and clicking on "Visualize
87+
/// argument values".
88+
/// ```rust
89+
/// // for example, the first will expand to the second
90+
/// let _span = enter_trace_span!(M, borrow_tracker::on_stack_pop, /* ... */)
91+
/// let _span = enter_trace_span!(M, "borrow_tracker", borrow_tracker = "on_stack_pop", /* ... */)
92+
/// ```
93+
///
94+
/// ### `tracing_separate_thread` parameter
95+
///
96+
/// This macro was introduced to obtain better traces of Miri without impacting release performance.
97+
/// Miri saves traces using the the `tracing_chrome` [tracing::Layer] so that they can be visualized
98+
/// in <https://ui.perfetto.dev>. To instruct `tracing_chrome` to put some spans on a separate trace
99+
/// thread/line than other spans when viewed in <https://ui.perfetto.dev>, you can pass
100+
/// `tracing_separate_thread = tracing::field::Empty` to the tracing macros. This is useful to
101+
/// separate out spans which just indicate the current step or program frame being processed by the
102+
/// interpreter. You should use a value of [tracing::field::Empty] so that other tracing layers
103+
/// (e.g. the logger) will ignore the `tracing_separate_thread` field. For example:
104+
/// ```rust
105+
/// let _span = enter_trace_span!(M, step::eval_statement, tracing_separate_thread = tracing::field::Empty)
106+
/// ```
60107
#[macro_export]
61108
macro_rules! enter_trace_span {
109+
($machine:ident, $name:ident :: $subname:ident $($tt:tt)*) => {{
110+
$crate::enter_trace_span!($machine, stringify!($name), $name = %stringify!($subname) $($tt)*)
111+
}};
112+
62113
($machine:ident, $($tt:tt)*) => {
63114
$machine::enter_trace_span(|| tracing::info_span!($($tt)*))
64115
}

src/tools/miri/src/helpers.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,25 +1257,11 @@ pub struct MaybeEnteredTraceSpan {
12571257
/// This is like [rustc_const_eval::enter_trace_span] except that it does not depend on the
12581258
/// [Machine] trait to check if tracing is enabled, because from the Miri codebase we can directly
12591259
/// check whether the "tracing" feature is enabled, unlike from the rustc_const_eval codebase.
1260-
///
1261-
/// In addition to the syntax accepted by [tracing::span!], this macro optionally allows passing
1262-
/// the span name (i.e. the first macro argument) in the form `NAME::SUBNAME` (without quotes) to
1263-
/// indicate that the span has name "NAME" (usually the name of the component) and has an additional
1264-
/// more specific name "SUBNAME" (usually the function name). The latter is passed to the [tracing]
1265-
/// infrastructure as a span field with the name "NAME". This allows not being distracted by
1266-
/// subnames when looking at the trace in <https://ui.perfetto.dev>, but when deeper introspection
1267-
/// is needed within a component, it's still possible to view the subnames directly in the UI by
1268-
/// selecting a span, clicking on the "NAME" argument on the right, and clicking on "Visualize
1269-
/// argument values".
1270-
/// ```rust
1271-
/// // for example, the first will expand to the second
1272-
/// enter_trace_span!(borrow_tracker::on_stack_pop, /* ... */)
1273-
/// enter_trace_span!("borrow_tracker", borrow_tracker = "on_stack_pop", /* ... */)
1274-
/// ```
1260+
/// Look at [rustc_const_eval::enter_trace_span] for complete documentation, examples and tips.
12751261
#[macro_export]
12761262
macro_rules! enter_trace_span {
12771263
($name:ident :: $subname:ident $($tt:tt)*) => {{
1278-
enter_trace_span!(stringify!($name), $name = %stringify!($subname) $($tt)*)
1264+
$crate::enter_trace_span!(stringify!($name), $name = %stringify!($subname) $($tt)*)
12791265
}};
12801266

12811267
($($tt:tt)*) => {

0 commit comments

Comments
 (0)