Skip to content

Commit 32dd8af

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 74e35d2 commit 32dd8af

File tree

10 files changed

+66
-44
lines changed

10 files changed

+66
-44
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,6 +2736,7 @@ dependencies = [
27362736
"env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)",
27372737
"graphviz 0.0.0",
27382738
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
2739+
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
27392740
"rustc 0.0.0",
27402741
"rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
27412742
"rustc_allocator 0.0.0",

src/librustc/session/config.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,7 @@ mod tests {
26142614
};
26152615
let registry = errors::registry::Registry::new(&[]);
26162616
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
2617-
let sess = build_session(sessopts, None, registry);
2617+
let sess = build_session(sessopts, None, None, registry);
26182618
let cfg = build_configuration(&sess, cfg);
26192619
assert!(cfg.contains(&(Symbol::intern("test"), None)));
26202620
});
@@ -2632,7 +2632,7 @@ mod tests {
26322632
};
26332633
let registry = errors::registry::Registry::new(&[]);
26342634
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
2635-
let sess = build_session(sessopts, None, registry);
2635+
let sess = build_session(sessopts, None, None, registry);
26362636
let cfg = build_configuration(&sess, cfg);
26372637
let mut test_items = cfg.iter().filter(|&&(name, _)| name == "test");
26382638
assert!(test_items.next().is_some());
@@ -2646,7 +2646,7 @@ mod tests {
26462646
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
26472647
let registry = errors::registry::Registry::new(&[]);
26482648
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2649-
let sess = build_session(sessopts, None, registry);
2649+
let sess = build_session(sessopts, None, None, registry);
26502650
assert!(!sess.diagnostic().flags.can_emit_warnings);
26512651
});
26522652

@@ -2656,15 +2656,15 @@ mod tests {
26562656
.unwrap();
26572657
let registry = errors::registry::Registry::new(&[]);
26582658
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2659-
let sess = build_session(sessopts, None, registry);
2659+
let sess = build_session(sessopts, None, None, registry);
26602660
assert!(sess.diagnostic().flags.can_emit_warnings);
26612661
});
26622662

26632663
syntax::with_globals(|| {
26642664
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
26652665
let registry = errors::registry::Registry::new(&[]);
26662666
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2667-
let sess = build_session(sessopts, None, registry);
2667+
let sess = build_session(sessopts, None, None, registry);
26682668
assert!(sess.diagnostic().flags.can_emit_warnings);
26692669
});
26702670
}

src/librustc/session/mod.rs

Lines changed: 20 additions & 25 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: {}",
@@ -1013,13 +1008,15 @@ impl Session {
10131008

10141009
pub fn build_session(
10151010
sopts: config::Options,
1011+
self_profiler: Option<Arc<PlMutex<SelfProfiler>>>,
10161012
local_crate_source_file: Option<PathBuf>,
10171013
registry: errors::registry::Registry,
10181014
) -> Session {
10191015
let file_path_mapping = sopts.file_path_mapping();
10201016

10211017
build_session_with_source_map(
10221018
sopts,
1019+
self_profiler,
10231020
local_crate_source_file,
10241021
registry,
10251022
Lrc::new(source_map::SourceMap::new(file_path_mapping)),
@@ -1029,6 +1026,7 @@ pub fn build_session(
10291026

10301027
pub fn build_session_with_source_map(
10311028
sopts: config::Options,
1029+
self_profiler: Option<Arc<PlMutex<SelfProfiler>>>,
10321030
local_crate_source_file: Option<PathBuf>,
10331031
registry: errors::registry::Registry,
10341032
source_map: Lrc<source_map::SourceMap>,
@@ -1103,11 +1101,12 @@ pub fn build_session_with_source_map(
11031101
},
11041102
);
11051103

1106-
build_session_(sopts, local_crate_source_file, diagnostic_handler, source_map)
1104+
build_session_(sopts, self_profiler, local_crate_source_file, diagnostic_handler, source_map)
11071105
}
11081106

11091107
pub fn build_session_(
11101108
sopts: config::Options,
1109+
self_profiler: Option<Arc<PlMutex<SelfProfiler>>>,
11111110
local_crate_source_file: Option<PathBuf>,
11121111
span_diagnostic: errors::Handler,
11131112
source_map: Lrc<source_map::SourceMap>,
@@ -1161,9 +1160,6 @@ pub fn build_session_(
11611160
CguReuseTracker::new_disabled()
11621161
};
11631162

1164-
let self_profiling_active = sopts.debugging_opts.self_profile ||
1165-
sopts.debugging_opts.profile_json;
1166-
11671163
let sess = Session {
11681164
target: target_cfg,
11691165
host,
@@ -1192,8 +1188,7 @@ pub fn build_session_(
11921188
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
11931189
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
11941190
cgu_reuse_tracker,
1195-
self_profiling_active,
1196-
self_profiling: Lock::new(SelfProfiler::new()),
1191+
self_profiling: self_profiler,
11971192
profile_channel: Lock::new(None),
11981193
perf_stats: PerfStats {
11991194
symbol_hash_time: Lock::new(Duration::from_secs(0)),

src/librustc_driver/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ arena = { path = "../libarena" }
1313
graphviz = { path = "../libgraphviz" }
1414
log = "0.4"
1515
env_logger = { version = "0.5", default-features = false }
16+
parking_lot = "0.6"
1617
rustc-rayon = "0.1.1"
1718
scoped-tls = { version = "0.1.1", features = ["nightly"] }
1819
rustc = { path = "../librustc" }

src/librustc_driver/driver.rs

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

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

src/librustc_driver/lib.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern crate graphviz;
2222
extern crate env_logger;
2323
#[cfg(unix)]
2424
extern crate libc;
25+
extern crate parking_lot;
2526
extern crate rustc_rayon as rayon;
2627
extern crate rustc;
2728
extern crate rustc_allocator;
@@ -64,6 +65,7 @@ use rustc::session::config::{Input, PrintRequest, ErrorOutputType};
6465
use rustc::session::config::nightly_options;
6566
use rustc::session::filesearch;
6667
use rustc::session::{early_error, early_warn};
68+
use rustc::util::profiling::{SelfProfiler};
6769
use rustc::lint::Lint;
6870
use rustc::lint;
6971
use rustc_metadata::locator;
@@ -90,7 +92,7 @@ use std::path::{PathBuf, Path};
9092
use std::process::{self, Command, Stdio};
9193
use std::str;
9294
use std::sync::atomic::{AtomicBool, Ordering};
93-
use std::sync::{Once, ONCE_INIT};
95+
use std::sync::{Arc, Once, ONCE_INIT};
9496
use std::thread;
9597

9698
use syntax::ast;
@@ -99,6 +101,8 @@ use syntax::feature_gate::{GatedCfg, UnstableFeatures};
99101
use syntax::parse::{self, PResult};
100102
use syntax_pos::{DUMMY_SP, MultiSpan, FileName};
101103

104+
use parking_lot::Mutex as PlMutex;
105+
102106
#[cfg(test)]
103107
mod test;
104108

@@ -461,6 +465,13 @@ fn run_compiler_with_pool<'a>(
461465
}
462466
}}
463467

468+
let self_profiling_active = sopts.debugging_opts.self_profile ||
469+
sopts.debugging_opts.profile_json;
470+
471+
let profiler =
472+
if self_profiling_active { Some(Arc::new(PlMutex::new(SelfProfiler::new()))) }
473+
else { None };
474+
464475
let descriptions = diagnostics_registry();
465476

466477
do_or_return!(callbacks.early_callback(&matches,
@@ -485,7 +496,12 @@ fn run_compiler_with_pool<'a>(
485496
let loader = file_loader.unwrap_or(box RealFileLoader);
486497
let source_map = Lrc::new(SourceMap::with_file_loader(loader, sopts.file_path_mapping()));
487498
let mut sess = session::build_session_with_source_map(
488-
sopts, input_file_path.clone(), descriptions, source_map, emitter_dest,
499+
sopts,
500+
profiler.clone(),
501+
input_file_path.clone(),
502+
descriptions,
503+
source_map,
504+
emitter_dest,
489505
);
490506

491507
if let Some(err) = input_err {
@@ -531,6 +547,21 @@ fn run_compiler_with_pool<'a>(
531547
&control)
532548
};
533549

550+
match profiler {
551+
None => { },
552+
Some(profiler) => {
553+
let mut profiler = profiler.lock();
554+
555+
if sess.opts.debugging_opts.self_profile {
556+
profiler.print_results(&sess.opts);
557+
}
558+
559+
if sess.opts.debugging_opts.profile_json {
560+
profiler.save_results(&sess.opts);
561+
}
562+
}
563+
}
564+
534565
(result, Some(sess))
535566
}
536567

@@ -810,6 +841,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
810841
match matches.free.len() {
811842
0 => {
812843
let mut sess = build_session(sopts.clone(),
844+
None,
813845
None,
814846
descriptions.clone());
815847
if sopts.describe_lints {

src/librustc_driver/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ fn test_env_with_pool<F>(
109109
let sess = session::build_session_(
110110
options,
111111
None,
112+
None,
112113
diagnostic_handler,
113114
Lrc::new(SourceMap::new(FilePathMapping::empty())),
114115
);

src/librustdoc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
404404
debugging_options.ui_testing);
405405

406406
let mut sess = session::build_session_(
407-
sessopts, cpath, diagnostic_handler, source_map,
407+
sessopts, None, cpath, diagnostic_handler, source_map,
408408
);
409409

410410
lint::builtin::HardwiredLints.get_lints()

src/librustdoc/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn run(mut options: Options) -> isize {
7070
Some(source_map.clone()));
7171

7272
let mut sess = session::build_session_(
73-
sessopts, Some(options.input), handler, source_map.clone(),
73+
sessopts, None, Some(options.input), handler, source_map.clone(),
7474
);
7575
let codegen_backend = rustc_driver::get_codegen_backend(&sess);
7676
let cstore = CStore::new(codegen_backend.metadata_loader());
@@ -274,7 +274,7 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
274274
let diagnostic_handler = errors::Handler::with_emitter(true, false, box emitter);
275275

276276
let mut sess = session::build_session_(
277-
sessopts, None, diagnostic_handler, source_map,
277+
sessopts, None, None, diagnostic_handler, source_map,
278278
);
279279
let codegen_backend = rustc_driver::get_codegen_backend(&sess);
280280
let cstore = CStore::new(codegen_backend.metadata_loader());

src/test/run-make-fulldeps/issue-19371/foo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn main() {
4444

4545
fn basic_sess(opts: Options) -> (Session, Rc<CStore>, Box<CodegenBackend>) {
4646
let descriptions = Registry::new(&rustc::DIAGNOSTICS);
47-
let sess = build_session(opts, None, descriptions);
47+
let sess = build_session(opts, None, None, descriptions);
4848
let codegen_backend = rustc_driver::get_codegen_backend(&sess);
4949
let cstore = Rc::new(CStore::new(codegen_backend.metadata_loader()));
5050
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

0 commit comments

Comments
 (0)