@@ -14,6 +14,8 @@ use std::time::{Duration, Instant, SystemTime};
1414
1515pub struct Timings < ' a , ' cfg > {
1616 config : & ' cfg Config ,
17+ /// Whether or not timings should be captured.
18+ enabled : bool ,
1719 /// If true, saves an HTML report to disk.
1820 report_html : bool ,
1921 /// If true, reports unit completion to stderr.
@@ -80,6 +82,18 @@ struct Concurrency {
8082
8183impl < ' a , ' cfg > Timings < ' a , ' cfg > {
8284 pub fn new ( bcx : & BuildContext < ' a , ' cfg > , root_units : & [ Unit < ' _ > ] ) -> Timings < ' a , ' cfg > {
85+ let has_report = |what| {
86+ bcx. config
87+ . cli_unstable ( )
88+ . timings
89+ . as_ref ( )
90+ . map_or ( false , |t| t. iter ( ) . any ( |opt| opt == what) )
91+ } ;
92+ let report_html = has_report ( "html" ) ;
93+ let report_info = has_report ( "info" ) ;
94+ let report_json = has_report ( "json" ) ;
95+ let enabled = report_html | report_info | report_json;
96+
8397 let mut root_map: HashMap < PackageId , Vec < String > > = HashMap :: new ( ) ;
8498 for unit in root_units {
8599 let target_desc = unit. target . description_named ( ) ;
@@ -96,13 +110,6 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
96110 } )
97111 . collect ( ) ;
98112 let start_str = humantime:: format_rfc3339_seconds ( SystemTime :: now ( ) ) . to_string ( ) ;
99- let has_report = |what| {
100- bcx. config
101- . cli_unstable ( )
102- . timings
103- . as_ref ( )
104- . map_or ( false , |t| t. iter ( ) . any ( |opt| opt == what) )
105- } ;
106113 let rustc_info = render_rustc_info ( bcx) ;
107114 let profile = if bcx. build_config . release {
108115 "release"
@@ -113,9 +120,10 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
113120
114121 Timings {
115122 config : bcx. config ,
116- report_html : has_report ( "html" ) ,
117- report_info : has_report ( "info" ) ,
118- report_json : has_report ( "json" ) ,
123+ enabled,
124+ report_html,
125+ report_info,
126+ report_json,
119127 start : bcx. config . creation_time ( ) ,
120128 start_str,
121129 rustc_info,
@@ -131,6 +139,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
131139
132140 /// Mark that a unit has started running.
133141 pub fn unit_start ( & mut self , id : u32 , unit : Unit < ' a > ) {
142+ if !self . enabled {
143+ return ;
144+ }
134145 let mut target = if unit. target . is_lib ( ) && unit. mode == CompileMode :: Build {
135146 // Special case for brevity, since most dependencies hit
136147 // this path.
@@ -162,6 +173,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
162173
163174 /// Mark that the `.rmeta` file as generated.
164175 pub fn unit_rmeta_finished ( & mut self , id : u32 , unlocked : Vec < & Unit < ' a > > ) {
176+ if !self . enabled {
177+ return ;
178+ }
165179 // `id` may not always be active. "fresh" units unconditionally
166180 // generate `Message::Finish`, but this active map only tracks dirty
167181 // units.
@@ -175,6 +189,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
175189
176190 /// Mark that a unit has finished running.
177191 pub fn unit_finished ( & mut self , id : u32 , unlocked : Vec < & Unit < ' a > > ) {
192+ if !self . enabled {
193+ return ;
194+ }
178195 // See note above in `unit_rmeta_finished`, this may not always be active.
179196 if let Some ( mut unit_time) = self . active . remove ( & id) {
180197 let t = d_as_f64 ( self . start . elapsed ( ) ) ;
@@ -210,6 +227,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
210227
211228 /// This is called periodically to mark the concurrency of internal structures.
212229 pub fn mark_concurrency ( & mut self , active : usize , waiting : usize , inactive : usize ) {
230+ if !self . enabled {
231+ return ;
232+ }
213233 let c = Concurrency {
214234 t : d_as_f64 ( self . start . elapsed ( ) ) ,
215235 active,
@@ -231,6 +251,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
231251
232252 /// Call this when all units are finished.
233253 pub fn finished ( & mut self ) -> CargoResult < ( ) > {
254+ if !self . enabled {
255+ return Ok ( ( ) ) ;
256+ }
234257 self . mark_concurrency ( 0 , 0 , 0 ) ;
235258 self . unit_times
236259 . sort_unstable_by ( |a, b| a. start . partial_cmp ( & b. start ) . unwrap ( ) ) ;
@@ -241,7 +264,7 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
241264 }
242265
243266 /// Save HTML report to disk.
244- pub fn report_html ( & self ) -> CargoResult < ( ) > {
267+ fn report_html ( & self ) -> CargoResult < ( ) > {
245268 let duration = self . start . elapsed ( ) . as_secs ( ) as u32 + 1 ;
246269 let timestamp = self . start_str . replace ( & [ '-' , ':' ] [ ..] , "" ) ;
247270 let filename = format ! ( "cargo-timing-{}.html" , timestamp) ;
0 commit comments