Skip to content

Add tracing to step.rs and friends #144708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2025
Merged

Conversation

Stypox
Copy link
Contributor

@Stypox Stypox commented Jul 30, 2025

Adds tracing calls to functions in step.rs (01717ff), to friend functions related to evaluation and stepping (cbfa7c4), and adds a new trait method EnteredTraceSpan::or_if_tracing_disabled (f0d0d1f).

Adding EnteredTraceSpan::or_if_tracing_disabled is optional and is only useful to avoid having both tracing::info!() calls (that existed before) and enter_trace_span!() calls (that this PR adds) that would be redundant and would slow down the collection of traces. I say it is optional because it adds some cognitive complexity around EnteredTraceSpan, which is possibly not worth the reduced redundancy. Let me know if I should revert that commit.

The tracing calls added in this PR are meant to make it easier to understand what was being executing at a particular point when looking at a trace. But they are likely not useful for the purpose of understanding which components are fast/slow, hence why I used tracing_separate_thread for them. After opening a trace generated using the code in this PR in https://ui.perfetto.dev, and after executing the following query and then pressing on "Show debug track", you will see something like the following image in the timeline:

select slices.id, ts, dur, track_id, category, args.string_value as name, depth, stack_id, parent_stack_id, parent_id, slices.arg_set_id, thread_ts, thread_instruction_count, thread_instruction_delta, cat, slice_id from slices inner join args USING (arg_set_id) where args.key = "args." || slices.name and name = "step"
image

r? @RalfJung

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 30, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 30, 2025

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@Stypox Stypox force-pushed the add-tracing-to-step branch from 01740a8 to f221d7c Compare July 31, 2025 09:28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eval_statement and eval_terminator make a lot of sense -- I'm a bit surprised by the others, is that really helpful and worth the overhead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do make it easier to understand what is happening at a particular point in my opinion, instead of having to look at the statement/terminator being executed and then guessing which path the execution followed. When I was looking for functions to trace, I found it quite helpful to notice "this init_fn_call took a lot of time, but most of it was outside init_stack_frame" or instead "this init_fn_call spent most of its time in init_stack_frame".

But yeah they do add quite some overhead (the number of spans is ~3x what it would be with only eval_statement and eval_terminator). If we want to prune some of the less useful ones, I'd remove eval_place and eval_place_to_op (they are very short lived, see the table), eval_rvalue_into_place (it's usually just a direct child of eval_statement with nothing else happening around it). This would leave us with just an 1.5x overhead over just eval_statement and eval_terminator. What do you think?

image

Copy link
Member

@RalfJung RalfJung Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to the ones in this file only, i.e. eval_rvalue_into_place, eval_fn_call_argument, eval_callee_and_args. My first inclination would be to remove them.

eval_place and eval_place_to_op and eval_operand form a nice group that makes sense together.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eval_rvalue_into_place, eval_fn_call_argument, eval_callee_and_args

Oh ok, I don't have strong opinions about those so we can as well remove them.

@@ -74,7 +75,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
///
/// This does NOT move the statement counter forward, the caller has to do that!
pub fn eval_statement(&mut self, stmt: &mir::Statement<'tcx>) -> InterpResult<'tcx> {
info!("{:?}", stmt);
let _span = enter_trace_span!(M, step::eval_statement, stmt = ?stmt, tracing_separate_thread = Empty);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want to only print stmt.kind here as well to match the terminator case (I know that inconsistency is pre-existing but this is a good time to fix it).

Is it worth also adding span = ?stmt.source_info.span? Please check if that produces something useful (should be file, line and column information); if yes, please add it for both statements and terminators.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

span seems to work as expected:

image image
select slices.id, ts, dur, track_id, category, SUBSTRING(args.string_value, 45) as name, depth, stack_id, parent_stack_id, parent_id, slices.arg_set_id, thread_ts, thread_instruction_count, thread_instruction_delta, cat, slice_id from slices inner join args USING (arg_set_id) where args.key = "args.span" and name = "step"

I used SUBSTRING(args.string_value, 45) to make the path relative to the repository root

/// # use rustc_const_eval::interpret::EnteredTraceSpan;
/// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
/// let _span = enter_trace_span!(M, step::eval_statement)
/// .or_if_tracing_disabled(|| tracing::info!("eval_statement"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nice!

@Stypox Stypox force-pushed the add-tracing-to-step branch from b24607a to 120b899 Compare July 31, 2025 14:20
@Stypox
Copy link
Contributor Author

Stypox commented Jul 31, 2025

I pushed a commit adding span, reducing line length, and removing eval_rvalue_into_place, eval_fn_call_argument and eval_callee_and_args. I wonder why fmt check was not complaining about the 165-character long lines xD.

Should I add span also to the alternative info!() calls? I'd say no as it would probably clutter the logs.

@RalfJung
Copy link
Member

I wonder why fmt check was not complaining about the 165-character long lines xD.

rustfmt accepts such lines when it is unable to break them.

Should I add span also to the alternative info!() calls? I'd say no as it would probably clutter the logs.

Agreed, let's not have a span there.

@RalfJung
Copy link
Member

RalfJung commented Jul 31, 2025

This looks good now, thanks! Please squash at least the last commit so we don't have a back-and-forth in the history.

Then we have to wait for #144688 to land.

@Stypox Stypox force-pushed the add-tracing-to-step branch from 120b899 to 88c9a25 Compare July 31, 2025 19:53
@Stypox
Copy link
Contributor Author

Stypox commented Jul 31, 2025

Done, and I also rebased on master now that #144688 has been merged

@RalfJung
Copy link
Member

RalfJung commented Aug 1, 2025

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Aug 1, 2025

📌 Commit 88c9a25 has been approved by RalfJung

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 1, 2025
RalfJung added a commit to RalfJung/rust that referenced this pull request Aug 1, 2025
…Jung

Add tracing to step.rs and friends

Adds tracing calls to functions in `step.rs` (01717ff), to friend functions related to evaluation and stepping (cbfa7c4), and adds a new trait method `EnteredTraceSpan::or_if_tracing_disabled` (f0d0d1f).

Adding `EnteredTraceSpan::or_if_tracing_disabled` is optional and is only useful to avoid having both `tracing::info!()` calls (that existed before) and `enter_trace_span!()` calls (that this PR adds) that would be redundant and would slow down the collection of traces. I say it is optional because it adds some cognitive complexity around `EnteredTraceSpan`, which is possibly not worth the reduced redundancy. Let me know if I should revert that commit.

The tracing calls added in this PR are meant to make it easier to understand what was being executing at a particular point when looking at a trace. But they are likely not useful for the purpose of understanding which components are fast/slow, hence why I used `tracing_separate_thread` for them. After opening a trace generated using the code in this PR in https://ui.perfetto.dev, and after executing the following query and then pressing on "Show debug track", you will see something like the following image in the timeline:

```sql
select slices.id, ts, dur, track_id, category, args.string_value as name, depth, stack_id, parent_stack_id, parent_id, slices.arg_set_id, thread_ts, thread_instruction_count, thread_instruction_delta, cat, slice_id from slices inner join args USING (arg_set_id) where args.key = "args." || slices.name and name = "step"
```

<img width="739" height="87" alt="image" src="https://github.com/user-attachments/assets/74ad9619-9a1f-40e5-9ef4-3db31e33d6e1" />
bors added a commit that referenced this pull request Aug 1, 2025
Rollup of 6 pull requests

Successful merges:

 - #144397 (`tests/ui/issues/`: The Issues Strike Back [2/N])
 - #144410 (Make tier 3 musl targets link dynamically by default)
 - #144708 (Add tracing to step.rs and friends)
 - #144730 (Create a typed wrapper for codegen backends in bootstrap)
 - #144771 (Remove some noisy triagebot pings for myself)
 - #144772 (add unsupported_calling_conventions to lint list)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit d9f794a into rust-lang:master Aug 1, 2025
10 checks passed
@rustbot rustbot added this to the 1.90.0 milestone Aug 1, 2025
rust-timer added a commit that referenced this pull request Aug 1, 2025
Rollup merge of #144708 - Stypox:add-tracing-to-step, r=RalfJung

Add tracing to step.rs and friends

Adds tracing calls to functions in `step.rs` (01717ff), to friend functions related to evaluation and stepping (cbfa7c4), and adds a new trait method `EnteredTraceSpan::or_if_tracing_disabled` (f0d0d1f).

Adding `EnteredTraceSpan::or_if_tracing_disabled` is optional and is only useful to avoid having both `tracing::info!()` calls (that existed before) and `enter_trace_span!()` calls (that this PR adds) that would be redundant and would slow down the collection of traces. I say it is optional because it adds some cognitive complexity around `EnteredTraceSpan`, which is possibly not worth the reduced redundancy. Let me know if I should revert that commit.

The tracing calls added in this PR are meant to make it easier to understand what was being executing at a particular point when looking at a trace. But they are likely not useful for the purpose of understanding which components are fast/slow, hence why I used `tracing_separate_thread` for them. After opening a trace generated using the code in this PR in https://ui.perfetto.dev, and after executing the following query and then pressing on "Show debug track", you will see something like the following image in the timeline:

```sql
select slices.id, ts, dur, track_id, category, args.string_value as name, depth, stack_id, parent_stack_id, parent_id, slices.arg_set_id, thread_ts, thread_instruction_count, thread_instruction_delta, cat, slice_id from slices inner join args USING (arg_set_id) where args.key = "args." || slices.name and name = "step"
```

<img width="739" height="87" alt="image" src="https://github.com/user-attachments/assets/74ad9619-9a1f-40e5-9ef4-3db31e33d6e1" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants