Skip to content

Commit f50cf94

Browse files
committed
Wrap the self-profiler in an Arc<Mutex<>>
This will allow us to send it across threads and measure things like LLVM time.
1 parent 350674b commit f50cf94

File tree

7 files changed

+60
-32
lines changed

7 files changed

+60
-32
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,7 @@ dependencies = [
26242624
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
26252625
"memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
26262626
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
2627+
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
26272628
"rustc 0.0.0",
26282629
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
26292630
"rustc_allocator 0.0.0",

src/librustc/session/mod.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ use std::fmt;
4343
use std::io::Write;
4444
use std::path::PathBuf;
4545
use std::time::Duration;
46-
use std::sync::mpsc;
46+
use std::sync::{Arc, mpsc};
47+
48+
use parking_lot::Mutex as PlMutex;
4749

4850
mod code_stats;
4951
pub mod config;
@@ -126,11 +128,8 @@ pub struct Session {
126128
/// Used by `-Z profile-queries` in `util::common`.
127129
pub profile_channel: Lock<Option<mpsc::Sender<ProfileQueriesMsg>>>,
128130

129-
/// Used by `-Z self-profile`.
130-
pub self_profiling_active: bool,
131-
132-
/// Used by `-Z self-profile`.
133-
pub self_profiling: Lock<SelfProfiler>,
131+
/// Used by -Z self-profile
132+
pub self_profiling: Option<Arc<PlMutex<SelfProfiler>>>,
134133

135134
/// Some measurements that are being gathered during compilation.
136135
pub perf_stats: PerfStats,
@@ -833,27 +832,23 @@ impl Session {
833832
#[inline(never)]
834833
#[cold]
835834
fn profiler_active<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
836-
let mut profiler = self.self_profiling.borrow_mut();
837-
f(&mut profiler);
835+
match &self.self_profiling {
836+
None => bug!("profiler_active() called but there was no profiler active"),
837+
Some(profiler) => {
838+
let mut p = profiler.lock();
839+
840+
f(&mut p);
841+
}
842+
}
838843
}
839844

840845
#[inline(always)]
841846
pub fn profiler<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
842-
if unlikely!(self.self_profiling_active) {
847+
if unlikely!(self.self_profiling.is_some()) {
843848
self.profiler_active(f)
844849
}
845850
}
846851

847-
pub fn print_profiler_results(&self) {
848-
let mut profiler = self.self_profiling.borrow_mut();
849-
profiler.print_results(&self.opts);
850-
}
851-
852-
pub fn save_json_results(&self) {
853-
let profiler = self.self_profiling.borrow();
854-
profiler.save_results(&self.opts);
855-
}
856-
857852
pub fn print_perf_stats(&self) {
858853
println!(
859854
"Total time spent computing symbol hashes: {}",
@@ -1135,6 +1130,13 @@ pub fn build_session_(
11351130
source_map: Lrc<source_map::SourceMap>,
11361131
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
11371132
) -> Session {
1133+
let self_profiling_active = sopts.debugging_opts.self_profile ||
1134+
sopts.debugging_opts.profile_json;
1135+
1136+
let self_profiler =
1137+
if self_profiling_active { Some(Arc::new(PlMutex::new(SelfProfiler::new()))) }
1138+
else { None };
1139+
11381140
let host_triple = TargetTriple::from_triple(config::host_triple());
11391141
let host = Target::search(&host_triple).unwrap_or_else(|e|
11401142
span_diagnostic
@@ -1184,9 +1186,6 @@ pub fn build_session_(
11841186
CguReuseTracker::new_disabled()
11851187
};
11861188

1187-
let self_profiling_active = sopts.debugging_opts.self_profile ||
1188-
sopts.debugging_opts.profile_json;
1189-
11901189
let sess = Session {
11911190
target: target_cfg,
11921191
host,
@@ -1215,8 +1214,7 @@ pub fn build_session_(
12151214
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
12161215
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
12171216
cgu_reuse_tracker,
1218-
self_profiling_active,
1219-
self_profiling: Lock::new(SelfProfiler::new()),
1217+
self_profiling: self_profiler,
12201218
profile_channel: Lock::new(None),
12211219
perf_stats: PerfStats {
12221220
symbol_hash_time: Lock::new(Duration::from_secs(0)),

src/librustc_codegen_ssa/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ memmap = "0.6"
1919
log = "0.4.5"
2020
libc = "0.2.44"
2121
jobserver = "0.1.11"
22+
parking_lot = "0.7"
2223

2324
serialize = { path = "../libserialize" }
2425
syntax = { path = "../libsyntax" }

src/librustc_codegen_ssa/back/write.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc::util::time_graph::{self, TimeGraph, Timeline};
1919
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
2020
use rustc::ty::TyCtxt;
2121
use rustc::util::common::{time_depth, set_time_depth, print_time_passes_entry};
22+
use rustc::util::profiling::SelfProfiler;
2223
use rustc_fs_util::link_or_copy;
2324
use rustc_data_structures::svh::Svh;
2425
use rustc_errors::{Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId};
@@ -29,6 +30,7 @@ use syntax::ext::hygiene::Mark;
2930
use syntax_pos::MultiSpan;
3031
use syntax_pos::symbol::Symbol;
3132
use jobserver::{Client, Acquired};
33+
use parking_lot::Mutex as PlMutex;
3234

3335
use std::any::Any;
3436
use std::fs;
@@ -201,6 +203,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {
201203
// Resources needed when running LTO
202204
pub backend: B,
203205
pub time_passes: bool,
206+
pub profiler: Option<Arc<PlMutex<SelfProfiler>>>,
204207
pub lto: Lto,
205208
pub no_landing_pads: bool,
206209
pub save_temps: bool,
@@ -254,6 +257,26 @@ impl<B: WriteBackendMethods> CodegenContext<B> {
254257
ModuleKind::Allocator => &self.allocator_module_config,
255258
}
256259
}
260+
261+
#[inline(never)]
262+
#[cold]
263+
fn profiler_active<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
264+
match &self.profiler {
265+
None => bug!("profiler_active() called but there was no profiler active"),
266+
Some(profiler) => {
267+
let mut p = profiler.lock();
268+
269+
f(&mut p);
270+
}
271+
}
272+
}
273+
274+
#[inline(always)]
275+
pub fn profile<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
276+
if unlikely!(self.profiler.is_some()) {
277+
self.profiler_active(f)
278+
}
279+
}
257280
}
258281

259282
fn generate_lto_work<B: ExtraBackendMethods>(
@@ -1033,6 +1056,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
10331056
save_temps: sess.opts.cg.save_temps,
10341057
opts: Arc::new(sess.opts.clone()),
10351058
time_passes: sess.time_passes(),
1059+
profiler: sess.self_profiling.clone(),
10361060
exported_symbols,
10371061
plugin_passes: sess.plugin_llvm_passes.borrow().clone(),
10381062
remark: sess.opts.cg.remark.clone(),

src/librustc_codegen_ssa/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#![feature(box_patterns)]
44
#![feature(box_syntax)]
5+
#![feature(core_intrinsics)]
56
#![feature(custom_attribute)]
67
#![feature(libc)]
78
#![feature(rustc_diagnostic_macros)]
9+
#![feature(stmt_expr_attributes)]
810
#![feature(in_band_lifetimes)]
911
#![feature(nll)]
1012
#![allow(unused_attributes)]
@@ -20,6 +22,7 @@
2022
2123
#[macro_use] extern crate log;
2224
#[macro_use] extern crate rustc;
25+
#[macro_use] extern crate rustc_data_structures;
2326
#[macro_use] extern crate syntax;
2427

2528
use std::path::PathBuf;

src/librustc_driver/driver.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,6 @@ pub fn compile_input(
345345
sess.print_perf_stats();
346346
}
347347

348-
if sess.opts.debugging_opts.self_profile {
349-
sess.print_profiler_results();
350-
}
351-
352-
if sess.opts.debugging_opts.profile_json {
353-
sess.save_json_results();
354-
}
355-
356348
controller_entry_point!(
357349
compilation_done,
358350
sess,

src/librustc_driver/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,15 @@ fn run_compiler_with_pool<'a>(
276276
&control)
277277
};
278278

279+
280+
if sess.opts.debugging_opts.self_profile {
281+
sess.profiler(|p| p.print_results(&sess.opts));
282+
}
283+
284+
if sess.opts.debugging_opts.profile_json {
285+
sess.profiler(|p| p.save_results(&sess.opts));
286+
}
287+
279288
(result, Some(sess))
280289
}
281290

0 commit comments

Comments
 (0)