Skip to content

Commit 1fe4886

Browse files
authored
Rollup merge of #144688 - Stypox:better-enter-trace-span, r=RalfJung
Uniform `enter_trace_span!` and add documentation 1. The latest changes to `enter_trace_span!` were ported from Miri (see rust-lang/miri#4452 (comment)), so now both the `rustc_const_eval` and the Miri macro accept the same syntax. Furthermore, the Miri macro was changed to just call rustc_const_eval`'s, to avoid duplication. 2. I made the `layout_of` (& friends) calls use that new syntax, e.g. `enter_trace_span!(layouting::layout_of, ...)` 3. I made sure the macro specifies all types/traits/macros it refers to using `$crate::`, so the macro works anywhere independently of which types/traits/macros are imported in the context it is used in. 4. I added documentation, examples and tips to the macro's doc. To make the rustdoc compile I had to add some hidden lines (`#`), but now it acts as a compilation test which will avoid reintroducing issue 3. in the future. I will also create a documentation file with everything one needs to know about tracing at a later point, but I figured adding some of that info directly on the tracing macro makes it more discoverable. 5. In `stack.rs` I made it so that the `"frame"` span has a field named "frame" (instead of "message") with the data about the frame. This field used to be called "message" (tracing's default) since it was previously formatted using `"{}", instance`, and now I replaced it with `frame = %instance`.
2 parents 3fb1b53 + bb08a4d commit 1fe4886

File tree

5 files changed

+75
-47
lines changed

5 files changed

+75
-47
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
113113
/// See [LayoutOf::layout_of] for the original documentation.
114114
#[inline(always)]
115115
pub fn layout_of(&self, ty: Ty<'tcx>) -> <Self as LayoutOfHelpers<'tcx>>::LayoutOfResult {
116-
let _span = enter_trace_span!(M, "InterpCx::layout_of", ty = ?ty.kind());
116+
let _span = enter_trace_span!(M, layouting::layout_of, ty = ?ty.kind());
117117
LayoutOf::layout_of(self, ty)
118118
}
119119

@@ -126,7 +126,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
126126
sig: ty::PolyFnSig<'tcx>,
127127
extra_args: &'tcx ty::List<Ty<'tcx>>,
128128
) -> <Self as FnAbiOfHelpers<'tcx>>::FnAbiOfResult {
129-
let _span = enter_trace_span!(M, "InterpCx::fn_abi_of_fn_ptr", ?sig, ?extra_args);
129+
let _span = enter_trace_span!(M, layouting::fn_abi_of_fn_ptr, ?sig, ?extra_args);
130130
FnAbiOf::fn_abi_of_fn_ptr(self, sig, extra_args)
131131
}
132132

@@ -139,7 +139,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
139139
instance: ty::Instance<'tcx>,
140140
extra_args: &'tcx ty::List<Ty<'tcx>>,
141141
) -> <Self as FnAbiOfHelpers<'tcx>>::FnAbiOfResult {
142-
let _span = enter_trace_span!(M, "InterpCx::fn_abi_of_instance", ?instance, ?extra_args);
142+
let _span = enter_trace_span!(M, layouting::fn_abi_of_instance, ?instance, ?extra_args);
143143
FnAbiOf::fn_abi_of_instance(self, instance, extra_args)
144144
}
145145
}

compiler/rustc_const_eval/src/interpret/stack.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,11 @@ 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.
404-
let span = info_span!("frame", tracing_separate_thread = Empty, "{}", instance);
404+
let span = info_span!("frame", tracing_separate_thread = Empty, frame = %instance);
405405
self.frame_mut().tracing_span.enter(span);
406406

407407
interp_ok(())

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,72 @@ 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+
/// # use rustc_const_eval::enter_trace_span;
69+
/// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
70+
/// # let my_display_var = String::new();
71+
/// # let my_debug_var = String::new();
72+
/// // logs a span named "hello" with a field named "arg" of value 42 (works only because
73+
/// // 42 implements the tracing::Value trait, otherwise use one of the options below)
74+
/// let _span = enter_trace_span!(M, "hello", arg = 42);
75+
/// // logs a field called "my_display_var" using the Display implementation
76+
/// let _span = enter_trace_span!(M, "hello", %my_display_var);
77+
/// // logs a field called "my_debug_var" using the Debug implementation
78+
/// let _span = enter_trace_span!(M, "hello", ?my_debug_var);
79+
/// ```
80+
///
81+
/// ### `NAME::SUBNAME` syntax
82+
///
83+
/// In addition to the syntax accepted by [tracing::span!], this macro optionally allows passing
84+
/// the span name (i.e. the first macro argument) in the form `NAME::SUBNAME` (without quotes) to
85+
/// indicate that the span has name "NAME" (usually the name of the component) and has an additional
86+
/// more specific name "SUBNAME" (usually the function name). The latter is passed to the [tracing]
87+
/// infrastructure as a span field with the name "NAME". This allows not being distracted by
88+
/// subnames when looking at the trace in <https://ui.perfetto.dev>, but when deeper introspection
89+
/// is needed within a component, it's still possible to view the subnames directly in the UI by
90+
/// selecting a span, clicking on the "NAME" argument on the right, and clicking on "Visualize
91+
/// argument values".
92+
/// ```rust
93+
/// # use rustc_const_eval::enter_trace_span;
94+
/// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
95+
/// // for example, the first will expand to the second
96+
/// let _span = enter_trace_span!(M, borrow_tracker::on_stack_pop, /* ... */);
97+
/// let _span = enter_trace_span!(M, "borrow_tracker", borrow_tracker = "on_stack_pop", /* ... */);
98+
/// ```
99+
///
100+
/// ### `tracing_separate_thread` parameter
101+
///
102+
/// This macro was introduced to obtain better traces of Miri without impacting release performance.
103+
/// Miri saves traces using the the `tracing_chrome` `tracing::Layer` so that they can be visualized
104+
/// in <https://ui.perfetto.dev>. To instruct `tracing_chrome` to put some spans on a separate trace
105+
/// thread/line than other spans when viewed in <https://ui.perfetto.dev>, you can pass
106+
/// `tracing_separate_thread = tracing::field::Empty` to the tracing macros. This is useful to
107+
/// separate out spans which just indicate the current step or program frame being processed by the
108+
/// interpreter. You should use a value of [tracing::field::Empty] so that other tracing layers
109+
/// (e.g. the logger) will ignore the `tracing_separate_thread` field. For example:
110+
/// ```rust
111+
/// # use rustc_const_eval::enter_trace_span;
112+
/// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
113+
/// let _span = enter_trace_span!(M, step::eval_statement, tracing_separate_thread = tracing::field::Empty);
114+
/// ```
60115
#[macro_export]
61116
macro_rules! enter_trace_span {
62-
($machine:ident, $($tt:tt)*) => {
63-
$machine::enter_trace_span(|| tracing::info_span!($($tt)*))
64-
}
117+
($machine:ty, $name:ident :: $subname:ident $($tt:tt)*) => {
118+
$crate::enter_trace_span!($machine, stringify!($name), $name = %stringify!($subname) $($tt)*)
119+
};
120+
121+
($machine:ty, $($tt:tt)*) => {
122+
<$machine as $crate::interpret::Machine>::enter_trace_span(|| tracing::info_span!($($tt)*))
123+
};
65124
}

src/tools/miri/src/helpers.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,43 +1245,14 @@ impl ToU64 for usize {
12451245
}
12461246
}
12471247

1248-
/// This struct is needed to enforce `#[must_use]` on values produced by [enter_trace_span] even
1249-
/// when the "tracing" feature is not enabled.
1250-
#[must_use]
1251-
pub struct MaybeEnteredTraceSpan {
1252-
#[cfg(feature = "tracing")]
1253-
pub _entered_span: tracing::span::EnteredSpan,
1254-
}
1255-
12561248
/// Enters a [tracing::info_span] only if the "tracing" feature is enabled, otherwise does nothing.
1257-
/// This is like [rustc_const_eval::enter_trace_span] except that it does not depend on the
1258-
/// [Machine] trait to check if tracing is enabled, because from the Miri codebase we can directly
1259-
/// 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-
/// ```
1249+
/// This calls [rustc_const_eval::enter_trace_span] with [MiriMachine] as the first argument, which
1250+
/// will in turn call [MiriMachine::enter_trace_span], which takes care of determining at compile
1251+
/// time whether to trace or not (and supposedly the call is compiled out if tracing is disabled).
1252+
/// Look at [rustc_const_eval::enter_trace_span] for complete documentation, examples and tips.
12751253
#[macro_export]
12761254
macro_rules! enter_trace_span {
1277-
($name:ident :: $subname:ident $($tt:tt)*) => {{
1278-
enter_trace_span!(stringify!($name), $name = %stringify!($subname) $($tt)*)
1279-
}};
1280-
12811255
($($tt:tt)*) => {
1282-
$crate::MaybeEnteredTraceSpan {
1283-
#[cfg(feature = "tracing")]
1284-
_entered_span: tracing::info_span!($($tt)*).entered()
1285-
}
1256+
rustc_const_eval::enter_trace_span!($crate::MiriMachine<'static>, $($tt)*)
12861257
};
12871258
}

src/tools/miri/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ pub use crate::eval::{
143143
AlignmentCheck, BacktraceStyle, IsolatedOp, MiriConfig, MiriEntryFnType, RejectOpWith,
144144
ValidationMode, create_ecx, eval_entry,
145145
};
146-
pub use crate::helpers::{
147-
AccessKind, EvalContextExt as _, MaybeEnteredTraceSpan, ToU64 as _, ToUsize as _,
148-
};
146+
pub use crate::helpers::{AccessKind, EvalContextExt as _, ToU64 as _, ToUsize as _};
149147
pub use crate::intrinsics::EvalContextExt as _;
150148
pub use crate::machine::{
151149
AllocExtra, DynMachineCallback, FrameExtra, MachineCallback, MemoryKind, MiriInterpCx,

0 commit comments

Comments
 (0)