Skip to content

Commit 806b8bf

Browse files
committed
Rename attribute, add documentation and simplify timestamp emission
1 parent 1221edb commit 806b8bf

File tree

6 files changed

+44
-22
lines changed

6 files changed

+44
-22
lines changed

compiler/rustc_errors/src/json.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::error::Report;
1313
use std::io::{self, Write};
1414
use std::path::Path;
1515
use std::sync::{Arc, Mutex};
16-
use std::time::Instant;
16+
use std::time::SystemTime;
1717
use std::vec;
1818

1919
use derive_setters::Setters;
@@ -62,8 +62,6 @@ pub struct JsonEmitter {
6262
macro_backtrace: bool,
6363
track_diagnostics: bool,
6464
terminal_url: TerminalUrl,
65-
#[setters(skip)]
66-
start_timestamp: Instant,
6765
}
6866

6967
impl JsonEmitter {
@@ -89,7 +87,6 @@ impl JsonEmitter {
8987
macro_backtrace: false,
9088
track_diagnostics: false,
9189
terminal_url: TerminalUrl::No,
92-
start_timestamp: Instant::now(),
9390
}
9491
}
9592

@@ -109,7 +106,7 @@ impl JsonEmitter {
109106
enum EmitTyped<'a> {
110107
Diagnostic(Diagnostic),
111108
Artifact(ArtifactNotification<'a>),
112-
SectionTimestamp(SectionTimestamp<'a>),
109+
SectionTiming(SectionTimestamp<'a>),
113110
FutureIncompat(FutureIncompatReport<'a>),
114111
UnusedExtern(UnusedExterns<'a>),
115112
}
@@ -149,13 +146,12 @@ impl Emitter for JsonEmitter {
149146
let name = match section {
150147
TimingSection::Linking => "link",
151148
};
152-
let time = Instant::now();
153-
let data = SectionTimestamp {
154-
name,
155-
kind,
156-
timestamp: time.duration_since(self.start_timestamp).as_micros(),
157-
};
158-
let result = self.emit(EmitTyped::SectionTimestamp(data));
149+
let timestamp = SystemTime::now()
150+
.duration_since(SystemTime::UNIX_EPOCH)
151+
.expect("system time should always be greater than the unix epoch")
152+
.as_micros();
153+
let data = SectionTimestamp { name, kind, timestamp };
154+
let result = self.emit(EmitTyped::SectionTiming(data));
159155
if let Err(e) = result {
160156
panic!("failed to print timing section: {e:?}");
161157
}

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

@@ -2777,7 +2777,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27772777
real_rust_source_base_dir,
27782778
edition,
27792779
json_artifact_notifications,
2780-
json_section_timings,
2780+
json_timings,
27812781
json_unused_externs,
27822782
json_future_incompat,
27832783
pretty,

compiler/rustc_session/src/options.rs

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

405405
/// `true` if we're emitting JSON timings with the start and end of
406406
/// high-level compilation sections
407-
json_section_timings: bool [UNTRACKED],
407+
json_timings: bool [UNTRACKED],
408408

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

compiler/rustc_session/src/session.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,7 @@ pub fn build_session(
11411141
.as_ref()
11421142
.map(|_| rng().next_u32().to_base_fixed_len(CASE_INSENSITIVE).to_string());
11431143

1144-
let timings = TimingSectionHandler::new(sopts.json_section_timings);
1144+
let timings = TimingSectionHandler::new(sopts.json_timings);
11451145

11461146
let sess = Session {
11471147
target,

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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,29 @@ 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+
"kind": "start", /* Marks the "start" or "end" of the compilation section */
313+
"name": "link", /* The name of the compilation section */
314+
"time": 12345 /* UNIX 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+
We currently do not guarantee any specific section names to be emitted.
323+
301324
[option-emit]: command-line-arguments.md#option-emit
302325
[option-error-format]: command-line-arguments.md#option-error-format
303326
[option-json]: command-line-arguments.md#option-json

0 commit comments

Comments
 (0)