Skip to content

Commit c9d3059

Browse files
committed
Add infrastructure for emitting timing sections
1 parent 7ad5248 commit c9d3059

File tree

9 files changed

+174
-11
lines changed

9 files changed

+174
-11
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::snippet::{
3434
Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString,
3535
};
3636
use crate::styled_buffer::StyledBuffer;
37+
use crate::timings::TimingRecord;
3738
use crate::translation::{Translate, to_fluent_args};
3839
use crate::{
3940
CodeSuggestion, DiagInner, DiagMessage, ErrCode, FluentBundle, LazyFallbackBundle, Level,
@@ -164,11 +165,16 @@ impl Margin {
164165
}
165166
}
166167

168+
pub enum TimingEvent {
169+
Start,
170+
End,
171+
}
172+
167173
const ANONYMIZED_LINE_NUM: &str = "LL";
168174

169175
pub type DynEmitter = dyn Emitter + DynSend;
170176

171-
/// Emitter trait for emitting errors.
177+
/// Emitter trait for emitting errors and other structured information.
172178
pub trait Emitter: Translate {
173179
/// Emit a structured diagnostic.
174180
fn emit_diagnostic(&mut self, diag: DiagInner, registry: &Registry);
@@ -177,6 +183,10 @@ pub trait Emitter: Translate {
177183
/// Currently only supported for the JSON format.
178184
fn emit_artifact_notification(&mut self, _path: &Path, _artifact_type: &str) {}
179185

186+
/// Emit a timestamp with start/end of a timing section.
187+
/// Currently only supported for the JSON format.
188+
fn emit_timing_section(&mut self, _record: TimingRecord, _event: TimingEvent) {}
189+
180190
/// Emit a report about future breakage.
181191
/// Currently only supported for the JSON format.
182192
fn emit_future_breakage_report(&mut self, _diags: Vec<DiagInner>, _registry: &Registry) {}

compiler/rustc_errors/src/json.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use termcolor::{ColorSpec, WriteColor};
2828
use crate::diagnostic::IsLint;
2929
use crate::emitter::{
3030
ColorConfig, Destination, Emitter, HumanEmitter, HumanReadableErrorType, OutputTheme,
31-
should_show_source_code,
31+
TimingEvent, should_show_source_code,
3232
};
3333
use crate::registry::Registry;
34+
use crate::timings::{TimingRecord, TimingSection};
3435
use crate::translation::{Translate, to_fluent_args};
3536
use crate::{
3637
CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, Subdiag, Suggestions,
@@ -104,6 +105,7 @@ impl JsonEmitter {
104105
enum EmitTyped<'a> {
105106
Diagnostic(Diagnostic),
106107
Artifact(ArtifactNotification<'a>),
108+
SectionTiming(SectionTimestamp<'a>),
107109
FutureIncompat(FutureIncompatReport<'a>),
108110
UnusedExtern(UnusedExterns<'a>),
109111
}
@@ -135,6 +137,21 @@ impl Emitter for JsonEmitter {
135137
}
136138
}
137139

140+
fn emit_timing_section(&mut self, record: TimingRecord, event: TimingEvent) {
141+
let event = match event {
142+
TimingEvent::Start => "start",
143+
TimingEvent::End => "end",
144+
};
145+
let name = match record.section {
146+
TimingSection::Linking => "link",
147+
};
148+
let data = SectionTimestamp { name, event, timestamp: record.timestamp };
149+
let result = self.emit(EmitTyped::SectionTiming(data));
150+
if let Err(e) = result {
151+
panic!("failed to print timing section: {e:?}");
152+
}
153+
}
154+
138155
fn emit_future_breakage_report(&mut self, diags: Vec<crate::DiagInner>, registry: &Registry) {
139156
let data: Vec<FutureBreakageItem<'_>> = diags
140157
.into_iter()
@@ -263,6 +280,16 @@ struct ArtifactNotification<'a> {
263280
emit: &'a str,
264281
}
265282

283+
#[derive(Serialize)]
284+
struct SectionTimestamp<'a> {
285+
/// Name of the section
286+
name: &'a str,
287+
/// Start/end of the section
288+
event: &'a str,
289+
/// Opaque timestamp.
290+
timestamp: u128,
291+
}
292+
266293
#[derive(Serialize)]
267294
struct FutureBreakageItem<'a> {
268295
// Always EmitTyped::Diagnostic, but we want to make sure it gets serialized

compiler/rustc_errors/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ pub use snippet::Style;
7474
pub use termcolor::{Color, ColorSpec, WriteColor};
7575
use tracing::debug;
7676

77+
use crate::emitter::TimingEvent;
7778
use crate::registry::Registry;
79+
use crate::timings::TimingRecord;
7880

7981
pub mod annotate_snippet_emitter_writer;
8082
pub mod codes;
@@ -90,6 +92,7 @@ mod snippet;
9092
mod styled_buffer;
9193
#[cfg(test)]
9294
mod tests;
95+
pub mod timings;
9396
pub mod translation;
9497

9598
pub type PResult<'a, T> = Result<T, Diag<'a>>;
@@ -1156,6 +1159,14 @@ impl<'a> DiagCtxtHandle<'a> {
11561159
self.inner.borrow_mut().emitter.emit_artifact_notification(path, artifact_type);
11571160
}
11581161

1162+
pub fn emit_timing_section_start(&self, record: TimingRecord) {
1163+
self.inner.borrow_mut().emitter.emit_timing_section(record, TimingEvent::Start);
1164+
}
1165+
1166+
pub fn emit_timing_section_end(&self, record: TimingRecord) {
1167+
self.inner.borrow_mut().emitter.emit_timing_section(record, TimingEvent::End);
1168+
}
1169+
11591170
pub fn emit_future_breakage_report(&self) {
11601171
let inner = &mut *self.inner.borrow_mut();
11611172
let diags = std::mem::take(&mut inner.future_breakage_diagnostics);

compiler/rustc_errors/src/timings.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::time::Instant;
2+
3+
use crate::DiagCtxtHandle;
4+
5+
/// A high-level section of the compilation process.
6+
#[derive(Copy, Clone, Debug)]
7+
pub enum TimingSection {
8+
/// Time spent linking.
9+
Linking,
10+
}
11+
12+
/// Section with attached timestamp
13+
#[derive(Copy, Clone, Debug)]
14+
pub struct TimingRecord {
15+
pub section: TimingSection,
16+
/// Microseconds elapsed since some predetermined point in time (~start of the rustc process).
17+
pub timestamp: u128,
18+
}
19+
20+
impl TimingRecord {
21+
fn from_origin(origin: Instant, section: TimingSection) -> Self {
22+
Self { section, timestamp: Instant::now().duration_since(origin).as_micros() }
23+
}
24+
25+
pub fn section(&self) -> TimingSection {
26+
self.section
27+
}
28+
29+
pub fn timestamp(&self) -> u128 {
30+
self.timestamp
31+
}
32+
}
33+
34+
/// Manages emission of start/end section timings, enabled through `--json=timings`.
35+
pub struct TimingSectionHandler {
36+
/// Time when the compilation session started.
37+
/// If `None`, timing is disabled.
38+
origin: Option<Instant>,
39+
}
40+
41+
impl TimingSectionHandler {
42+
pub fn new(enabled: bool) -> Self {
43+
let origin = if enabled { Some(Instant::now()) } else { None };
44+
Self { origin }
45+
}
46+
47+
/// Returns a RAII guard that will immediately emit a start the provided section, and then emit
48+
/// its end when it is dropped.
49+
pub fn start_section<'a>(
50+
&self,
51+
diag_ctxt: DiagCtxtHandle<'a>,
52+
section: TimingSection,
53+
) -> TimingSectionGuard<'a> {
54+
TimingSectionGuard::create(diag_ctxt, section, self.origin)
55+
}
56+
}
57+
58+
/// RAII wrapper for starting and ending section timings.
59+
pub struct TimingSectionGuard<'a> {
60+
dcx: DiagCtxtHandle<'a>,
61+
section: TimingSection,
62+
origin: Option<Instant>,
63+
}
64+
65+
impl<'a> TimingSectionGuard<'a> {
66+
fn create(dcx: DiagCtxtHandle<'a>, section: TimingSection, origin: Option<Instant>) -> Self {
67+
if let Some(origin) = origin {
68+
dcx.emit_timing_section_start(TimingRecord::from_origin(origin, section));
69+
}
70+
Self { dcx, section, origin }
71+
}
72+
}
73+
74+
impl<'a> Drop for TimingSectionGuard<'a> {
75+
fn drop(&mut self) {
76+
if let Some(origin) = self.origin {
77+
self.dcx.emit_timing_section_end(TimingRecord::from_origin(origin, self.section));
78+
}
79+
}
80+
}

compiler/rustc_session/src/config.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ impl Default for Options {
13661366
real_rust_source_base_dir: None,
13671367
edition: DEFAULT_EDITION,
13681368
json_artifact_notifications: false,
1369-
json_section_timings: false,
1369+
json_timings: false,
13701370
json_unused_externs: JsonUnusedExterns::No,
13711371
json_future_incompat: false,
13721372
pretty: None,
@@ -1883,7 +1883,7 @@ pub struct JsonConfig {
18831883
json_artifact_notifications: bool,
18841884
/// Output start and end timestamps of several high-level compilation sections
18851885
/// (frontend, backend, linker).
1886-
json_section_timings: bool,
1886+
json_timings: bool,
18871887
pub json_unused_externs: JsonUnusedExterns,
18881888
json_future_incompat: bool,
18891889
}
@@ -1925,7 +1925,7 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
19251925
let mut json_artifact_notifications = false;
19261926
let mut json_unused_externs = JsonUnusedExterns::No;
19271927
let mut json_future_incompat = false;
1928-
let mut json_section_timings = false;
1928+
let mut json_timings = false;
19291929
for option in matches.opt_strs("json") {
19301930
// For now conservatively forbid `--color` with `--json` since `--json`
19311931
// won't actually be emitting any colors and anything colorized is
@@ -1942,7 +1942,7 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
19421942
}
19431943
"diagnostic-rendered-ansi" => json_color = ColorConfig::Always,
19441944
"artifacts" => json_artifact_notifications = true,
1945-
"timings" => json_section_timings = true,
1945+
"timings" => json_timings = true,
19461946
"unused-externs" => json_unused_externs = JsonUnusedExterns::Loud,
19471947
"unused-externs-silent" => json_unused_externs = JsonUnusedExterns::Silent,
19481948
"future-incompat" => json_future_incompat = true,
@@ -1955,7 +1955,7 @@ pub fn parse_json(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Json
19551955
json_rendered,
19561956
json_color,
19571957
json_artifact_notifications,
1958-
json_section_timings,
1958+
json_timings,
19591959
json_unused_externs,
19601960
json_future_incompat,
19611961
}
@@ -2483,7 +2483,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
24832483
json_rendered,
24842484
json_color,
24852485
json_artifact_notifications,
2486-
json_section_timings,
2486+
json_timings,
24872487
json_unused_externs,
24882488
json_future_incompat,
24892489
} = parse_json(early_dcx, matches);
@@ -2505,7 +2505,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
25052505
let mut unstable_opts = UnstableOptions::build(early_dcx, matches, &mut target_modifiers);
25062506
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(early_dcx, matches);
25072507

2508-
if !unstable_opts.unstable_options && json_section_timings {
2508+
if !unstable_opts.unstable_options && json_timings {
25092509
early_dcx.early_fatal("--json=timings is unstable and requires using `-Zunstable-options`");
25102510
}
25112511

@@ -2786,7 +2786,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27862786
real_rust_source_base_dir,
27872787
edition,
27882788
json_artifact_notifications,
2789-
json_section_timings,
2789+
json_timings,
27902790
json_unused_externs,
27912791
json_future_incompat,
27922792
pretty,

compiler/rustc_session/src/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ top_level_options!(
412412

413413
/// `true` if we're emitting JSON timings with the start and end of
414414
/// high-level compilation sections
415-
json_section_timings: bool [UNTRACKED],
415+
json_timings: bool [UNTRACKED],
416416

417417
/// `true` if we're emitting a JSON blob containing the unused externs
418418
json_unused_externs: JsonUnusedExterns [UNTRACKED],

compiler/rustc_session/src/session.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_errors::emitter::{
1818
DynEmitter, HumanEmitter, HumanReadableErrorType, OutputTheme, stderr_destination,
1919
};
2020
use rustc_errors::json::JsonEmitter;
21+
use rustc_errors::timings::TimingSectionHandler;
2122
use rustc_errors::{
2223
Diag, DiagCtxt, DiagCtxtHandle, DiagMessage, Diagnostic, ErrorGuaranteed, FatalAbort,
2324
FluentBundle, LazyFallbackBundle, TerminalUrl, fallback_fluent_bundle,
@@ -156,6 +157,9 @@ pub struct Session {
156157
/// Used by `-Z self-profile`.
157158
pub prof: SelfProfilerRef,
158159

160+
/// Used to emit section timings events (enabled by `--json=timings`).
161+
pub timings: TimingSectionHandler,
162+
159163
/// Data about code being compiled, gathered during compilation.
160164
pub code_stats: CodeStats,
161165

@@ -1126,6 +1130,8 @@ pub fn build_session(
11261130
.as_ref()
11271131
.map(|_| rng().next_u32().to_base_fixed_len(CASE_INSENSITIVE).to_string());
11281132

1133+
let timings = TimingSectionHandler::new(sopts.json_timings);
1134+
11291135
let sess = Session {
11301136
target,
11311137
host,
@@ -1136,6 +1142,7 @@ pub fn build_session(
11361142
io,
11371143
incr_comp_session: RwLock::new(IncrCompSession::NotInitialized),
11381144
prof,
1145+
timings,
11391146
code_stats: Default::default(),
11401147
lint_store: None,
11411148
driver_lint_caps,

src/doc/rustc/src/command-line-arguments.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ to customize the output:
471471
- `future-incompat` - includes a JSON message that contains a report if the
472472
crate contains any code that may fail to compile in the future.
473473

474+
- `timings` - output a JSON message when a certain compilation "section"
475+
(such as frontend analysis, code generation, linking) begins or ends.
476+
474477
Note that it is invalid to combine the `--json` argument with the
475478
[`--color`](#option-color) argument, and it is required to combine `--json`
476479
with `--error-format=json`.

src/doc/rustc/src/json.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,31 @@ appropriately. (This is needed by Cargo which shares the same dependencies
298298
across multiple build targets, so it should only report an unused dependency if
299299
its not used by any of the targets.)
300300

301+
## Timings
302+
303+
**This setting is currently unstable and requires usage of `-Zunstable-options`.**
304+
305+
The `--timings` option will tell `rustc` to emit messages when a certain compilation
306+
section (such as code generation or linking) begins or ends. The messages will have
307+
the following format:
308+
309+
```json
310+
{
311+
"$message_type": "section_timing", /* Type of this message */
312+
"event": "start", /* Marks the "start" or "end" of the compilation section */
313+
"name": "link", /* The name of the compilation section */
314+
"time": 12345 /* Opaque timestamp when the message was emitted, in microseconds */
315+
}
316+
```
317+
318+
Compilation sections can be nested; for example, if you encounter the start of "foo",
319+
then the start of "bar", then the end of "bar" and then the end of "bar", it means that the
320+
"bar" section happened as a part of the "foo" section.
321+
322+
The timestamp should only be used for computing the duration of each section.
323+
324+
We currently do not guarantee any specific section names to be emitted.
325+
301326
[option-emit]: command-line-arguments.md#option-emit
302327
[option-error-format]: command-line-arguments.md#option-error-format
303328
[option-json]: command-line-arguments.md#option-json

0 commit comments

Comments
 (0)