@@ -14,6 +14,8 @@ use std::time::{Duration, Instant, SystemTime};
14
14
15
15
pub struct Timings < ' a , ' cfg > {
16
16
config : & ' cfg Config ,
17
+ /// Whether or not timings should be captured.
18
+ enabled : bool ,
17
19
/// If true, saves an HTML report to disk.
18
20
report_html : bool ,
19
21
/// If true, reports unit completion to stderr.
@@ -80,6 +82,18 @@ struct Concurrency {
80
82
81
83
impl < ' a , ' cfg > Timings < ' a , ' cfg > {
82
84
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
+
83
97
let mut root_map: HashMap < PackageId , Vec < String > > = HashMap :: new ( ) ;
84
98
for unit in root_units {
85
99
let target_desc = unit. target . description_named ( ) ;
@@ -96,13 +110,6 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
96
110
} )
97
111
. collect ( ) ;
98
112
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
- } ;
106
113
let rustc_info = render_rustc_info ( bcx) ;
107
114
let profile = if bcx. build_config . release {
108
115
"release"
@@ -113,9 +120,10 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
113
120
114
121
Timings {
115
122
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,
119
127
start : bcx. config . creation_time ( ) ,
120
128
start_str,
121
129
rustc_info,
@@ -131,6 +139,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
131
139
132
140
/// Mark that a unit has started running.
133
141
pub fn unit_start ( & mut self , id : u32 , unit : Unit < ' a > ) {
142
+ if !self . enabled {
143
+ return ;
144
+ }
134
145
let mut target = if unit. target . is_lib ( ) && unit. mode == CompileMode :: Build {
135
146
// Special case for brevity, since most dependencies hit
136
147
// this path.
@@ -162,6 +173,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
162
173
163
174
/// Mark that the `.rmeta` file as generated.
164
175
pub fn unit_rmeta_finished ( & mut self , id : u32 , unlocked : Vec < & Unit < ' a > > ) {
176
+ if !self . enabled {
177
+ return ;
178
+ }
165
179
// `id` may not always be active. "fresh" units unconditionally
166
180
// generate `Message::Finish`, but this active map only tracks dirty
167
181
// units.
@@ -175,6 +189,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
175
189
176
190
/// Mark that a unit has finished running.
177
191
pub fn unit_finished ( & mut self , id : u32 , unlocked : Vec < & Unit < ' a > > ) {
192
+ if !self . enabled {
193
+ return ;
194
+ }
178
195
// See note above in `unit_rmeta_finished`, this may not always be active.
179
196
if let Some ( mut unit_time) = self . active . remove ( & id) {
180
197
let t = d_as_f64 ( self . start . elapsed ( ) ) ;
@@ -210,6 +227,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
210
227
211
228
/// This is called periodically to mark the concurrency of internal structures.
212
229
pub fn mark_concurrency ( & mut self , active : usize , waiting : usize , inactive : usize ) {
230
+ if !self . enabled {
231
+ return ;
232
+ }
213
233
let c = Concurrency {
214
234
t : d_as_f64 ( self . start . elapsed ( ) ) ,
215
235
active,
@@ -231,6 +251,9 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
231
251
232
252
/// Call this when all units are finished.
233
253
pub fn finished ( & mut self ) -> CargoResult < ( ) > {
254
+ if !self . enabled {
255
+ return Ok ( ( ) ) ;
256
+ }
234
257
self . mark_concurrency ( 0 , 0 , 0 ) ;
235
258
self . unit_times
236
259
. sort_unstable_by ( |a, b| a. start . partial_cmp ( & b. start ) . unwrap ( ) ) ;
@@ -241,7 +264,7 @@ impl<'a, 'cfg> Timings<'a, 'cfg> {
241
264
}
242
265
243
266
/// Save HTML report to disk.
244
- pub fn report_html ( & self ) -> CargoResult < ( ) > {
267
+ fn report_html ( & self ) -> CargoResult < ( ) > {
245
268
let duration = self . start . elapsed ( ) . as_secs ( ) as u32 + 1 ;
246
269
let timestamp = self . start_str . replace ( & [ '-' , ':' ] [ ..] , "" ) ;
247
270
let filename = format ! ( "cargo-timing-{}.html" , timestamp) ;
0 commit comments