Skip to content

Commit 87b103d

Browse files
committed
Add a "total" measurement to -Ztime-passes.
This is useful for getting the total compilation time at the end. To do this, the patch changes `print_time_passes_entry` to not increment the depth, which means that `print_time_passes_entry_internal` is no longer needed.
1 parent 90419d3 commit 87b103d

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

src/librustc/util/common.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub fn time_ext<T, F>(do_it: bool, sess: Option<&Session>, what: &str, f: F) ->
170170
}
171171
}
172172

173-
print_time_passes_entry_internal(what, dur);
173+
print_time_passes_entry(true, what, dur);
174174

175175
TIME_DEPTH.with(|slot| slot.set(old));
176176

@@ -182,18 +182,6 @@ pub fn print_time_passes_entry(do_it: bool, what: &str, dur: Duration) {
182182
return
183183
}
184184

185-
let old = TIME_DEPTH.with(|slot| {
186-
let r = slot.get();
187-
slot.set(r + 1);
188-
r
189-
});
190-
191-
print_time_passes_entry_internal(what, dur);
192-
193-
TIME_DEPTH.with(|slot| slot.set(old));
194-
}
195-
196-
fn print_time_passes_entry_internal(what: &str, dur: Duration) {
197185
let indentation = TIME_DEPTH.with(|slot| slot.get());
198186

199187
let mem_string = match get_resident() {

src/librustc_codegen_ssa/back/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
15541554
let total_llvm_time = Instant::now().duration_since(llvm_start_time);
15551555
// This is the top-level timing for all of LLVM, set the time-depth
15561556
// to zero.
1557-
set_time_depth(0);
1557+
set_time_depth(1);
15581558
print_time_passes_entry(cgcx.time_passes,
15591559
"LLVM passes",
15601560
total_llvm_time);

src/librustc_codegen_ssa/base.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc::ty::{self, Ty, TyCtxt, Instance};
2525
use rustc::ty::layout::{self, Align, TyLayout, LayoutOf, VariantIdx, HasTyCtxt};
2626
use rustc::ty::query::Providers;
2727
use rustc::middle::cstore::{self, LinkagePreference};
28-
use rustc::util::common::{time, print_time_passes_entry};
28+
use rustc::util::common::{time, print_time_passes_entry, set_time_depth, time_depth};
2929
use rustc::session::config::{self, EntryFnType, Lto};
3030
use rustc::session::Session;
3131
use rustc::util::nodemap::FxHashMap;
@@ -639,9 +639,12 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
639639

640640
// Since the main thread is sometimes blocked during codegen, we keep track
641641
// -Ztime-passes output manually.
642+
let time_depth = time_depth();
643+
set_time_depth(time_depth + 1);
642644
print_time_passes_entry(tcx.sess.time_passes(),
643645
"codegen to LLVM IR",
644646
total_codegen_time);
647+
set_time_depth(time_depth);
645648

646649
::rustc_incremental::assert_module_sources::assert_module_sources(tcx);
647650

src/librustc_driver/lib.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ use rustc::session::{early_error, early_warn};
3838
use rustc::lint::Lint;
3939
use rustc::lint;
4040
use rustc::hir::def_id::LOCAL_CRATE;
41-
use rustc::util::common::{time, ErrorReported, install_panic_hook};
41+
use rustc::util::common::{ErrorReported, install_panic_hook, print_time_passes_entry};
42+
use rustc::util::common::{set_time_depth, time};
4243
use rustc_metadata::locator;
4344
use rustc_metadata::cstore::CStore;
4445
use rustc_codegen_utils::codegen_backend::CodegenBackend;
@@ -54,11 +55,12 @@ use std::default::Default;
5455
use std::env;
5556
use std::ffi::OsString;
5657
use std::io::{self, Read, Write};
58+
use std::mem;
5759
use std::panic::{self, catch_unwind};
5860
use std::path::PathBuf;
5961
use std::process::{self, Command, Stdio};
6062
use std::str;
61-
use std::mem;
63+
use std::time::Instant;
6264

6365
use syntax::ast;
6466
use syntax::source_map::FileLoader;
@@ -72,7 +74,7 @@ pub mod pretty;
7274
/// Exit status code used for successful compilation and help output.
7375
pub const EXIT_SUCCESS: i32 = 0;
7476

75-
/// Exit status code used for compilation failures and invalid flags.
77+
/// Exit status code used for compilation failures and invalid flags.
7678
pub const EXIT_FAILURE: i32 = 1;
7779

7880
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.\
@@ -118,6 +120,18 @@ pub struct DefaultCallbacks;
118120

119121
impl Callbacks for DefaultCallbacks {}
120122

123+
#[derive(Default)]
124+
pub struct TimePassesCallbacks {
125+
time_passes: bool,
126+
}
127+
128+
impl Callbacks for TimePassesCallbacks {
129+
fn config(&mut self, config: &mut interface::Config) {
130+
self.time_passes =
131+
config.opts.debugging_opts.time_passes || config.opts.debugging_opts.time;
132+
}
133+
}
134+
121135
// Parse args and run the compiler. This is the primary entry point for rustc.
122136
// See comments on CompilerCalls below for details about the callbacks argument.
123137
// The FileLoader provides a way to load files from sources other than the file system.
@@ -1169,18 +1183,24 @@ pub fn init_rustc_env_logger() {
11691183
}
11701184

11711185
pub fn main() {
1186+
let start = Instant::now();
11721187
init_rustc_env_logger();
1188+
let mut callbacks = TimePassesCallbacks::default();
11731189
let result = report_ices_to_stderr_if_any(|| {
11741190
let args = env::args_os().enumerate()
11751191
.map(|(i, arg)| arg.into_string().unwrap_or_else(|arg| {
11761192
early_error(ErrorOutputType::default(),
11771193
&format!("Argument {} is not valid Unicode: {:?}", i, arg))
11781194
}))
11791195
.collect::<Vec<_>>();
1180-
run_compiler(&args, &mut DefaultCallbacks, None, None)
1196+
run_compiler(&args, &mut callbacks, None, None)
11811197
}).and_then(|result| result);
1182-
process::exit(match result {
1198+
let exit_code = match result {
11831199
Ok(_) => EXIT_SUCCESS,
11841200
Err(_) => EXIT_FAILURE,
1185-
});
1201+
};
1202+
// The extra `\t` is necessary to align this label with the others.
1203+
set_time_depth(0);
1204+
print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed());
1205+
process::exit(exit_code);
11861206
}

0 commit comments

Comments
 (0)