Skip to content

Commit 25b8c61

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 c0086b9 commit 25b8c61

File tree

7 files changed

+60
-32
lines changed

7 files changed

+60
-32
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,7 @@ dependencies = [
26252625
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
26262626
"memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
26272627
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
2628+
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
26282629
"rustc 0.0.0",
26292630
"rustc-demangle 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
26302631
"rustc_allocator 0.0.0",

src/librustc/session/mod.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ use std::fmt;
4444
use std::io::Write;
4545
use std::path::PathBuf;
4646
use std::time::Duration;
47-
use std::sync::mpsc;
47+
use std::sync::{Arc, mpsc};
48+
49+
use parking_lot::Mutex as PlMutex;
4850

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

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

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

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

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

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

src/librustc_codegen_ssa/Cargo.toml

+1
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

+24
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

+3
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

-8
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,6 @@ pub fn compile_input(
349349
sess.print_perf_stats();
350350
}
351351

352-
if sess.opts.debugging_opts.self_profile {
353-
sess.print_profiler_results();
354-
}
355-
356-
if sess.opts.debugging_opts.profile_json {
357-
sess.save_json_results();
358-
}
359-
360352
controller_entry_point!(
361353
compilation_done,
362354
sess,

src/librustc_driver/lib.rs

+9
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)