@@ -13,6 +13,7 @@ use std::error::Report;
1313use std:: io:: { self , Write } ;
1414use std:: path:: Path ;
1515use std:: sync:: { Arc , Mutex } ;
16+ use std:: time:: Instant ;
1617use std:: vec;
1718
1819use derive_setters:: Setters ;
@@ -28,9 +29,10 @@ use termcolor::{ColorSpec, WriteColor};
2829use crate :: diagnostic:: IsLint ;
2930use crate :: emitter:: {
3031 ColorConfig , Destination , Emitter , HumanEmitter , HumanReadableErrorType , OutputTheme ,
31- should_show_source_code,
32+ TimingSectionKind , should_show_source_code,
3233} ;
3334use crate :: registry:: Registry ;
35+ use crate :: timings:: TimingSection ;
3436use crate :: translation:: { Translate , to_fluent_args} ;
3537use crate :: {
3638 CodeSuggestion , FluentBundle , LazyFallbackBundle , MultiSpan , SpanLabel , Subdiag , Suggestions ,
@@ -60,6 +62,8 @@ pub struct JsonEmitter {
6062 macro_backtrace : bool ,
6163 track_diagnostics : bool ,
6264 terminal_url : TerminalUrl ,
65+ #[ setters( skip) ]
66+ start_timestamp : Instant ,
6367}
6468
6569impl JsonEmitter {
@@ -85,6 +89,7 @@ impl JsonEmitter {
8589 macro_backtrace : false ,
8690 track_diagnostics : false ,
8791 terminal_url : TerminalUrl :: No ,
92+ start_timestamp : Instant :: now ( ) ,
8893 }
8994 }
9095
@@ -104,6 +109,7 @@ impl JsonEmitter {
104109enum EmitTyped < ' a > {
105110 Diagnostic ( Diagnostic ) ,
106111 Artifact ( ArtifactNotification < ' a > ) ,
112+ SectionTimestamp ( SectionTimestamp < ' a > ) ,
107113 FutureIncompat ( FutureIncompatReport < ' a > ) ,
108114 UnusedExtern ( UnusedExterns < ' a > ) ,
109115}
@@ -135,6 +141,26 @@ impl Emitter for JsonEmitter {
135141 }
136142 }
137143
144+ fn emit_timing_section ( & mut self , section : TimingSection , kind : TimingSectionKind ) {
145+ let kind = match kind {
146+ TimingSectionKind :: Start => "start" ,
147+ TimingSectionKind :: End => "end" ,
148+ } ;
149+ let name = match section {
150+ TimingSection :: Linking => "link" ,
151+ } ;
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) ) ;
159+ if let Err ( e) = result {
160+ panic ! ( "failed to print timing section: {e:?}" ) ;
161+ }
162+ }
163+
138164 fn emit_future_breakage_report ( & mut self , diags : Vec < crate :: DiagInner > , registry : & Registry ) {
139165 let data: Vec < FutureBreakageItem < ' _ > > = diags
140166 . into_iter ( )
@@ -263,6 +289,16 @@ struct ArtifactNotification<'a> {
263289 emit : & ' a str ,
264290}
265291
292+ #[ derive( Serialize ) ]
293+ struct SectionTimestamp < ' a > {
294+ /// Name of the section
295+ name : & ' a str ,
296+ /// Start/end of the section
297+ kind : & ' a str ,
298+ /// Microseconds elapsed since some predetermined point in time (~start of the rustc process).
299+ timestamp : u128 ,
300+ }
301+
266302#[ derive( Serialize ) ]
267303struct FutureBreakageItem < ' a > {
268304 // Always EmitTyped::Diagnostic, but we want to make sure it gets serialized
0 commit comments