Skip to content

Commit

Permalink
Wrap the self-profiler in an Arc<Mutex<>>
Browse files Browse the repository at this point in the history
This will allow us to send it across threads and measure things like
LLVM time.
  • Loading branch information
wesleywiser committed Feb 20, 2019
1 parent 74e35d2 commit 32dd8af
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 44 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2736,6 +2736,7 @@ dependencies = [
"env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)",
"graphviz 0.0.0",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc 0.0.0",
"rustc-rayon 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_allocator 0.0.0",
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2614,7 +2614,7 @@ mod tests {
};
let registry = errors::registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(sessopts, None, registry);
let sess = build_session(sessopts, None, None, registry);
let cfg = build_configuration(&sess, cfg);
assert!(cfg.contains(&(Symbol::intern("test"), None)));
});
Expand All @@ -2632,7 +2632,7 @@ mod tests {
};
let registry = errors::registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(sessopts, None, registry);
let sess = build_session(sessopts, None, None, registry);
let cfg = build_configuration(&sess, cfg);
let mut test_items = cfg.iter().filter(|&&(name, _)| name == "test");
assert!(test_items.next().is_some());
Expand All @@ -2646,7 +2646,7 @@ mod tests {
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
let registry = errors::registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, None, registry);
let sess = build_session(sessopts, None, None, registry);
assert!(!sess.diagnostic().flags.can_emit_warnings);
});

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

syntax::with_globals(|| {
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
let registry = errors::registry::Registry::new(&[]);
let (sessopts, _) = build_session_options_and_crate_config(&matches);
let sess = build_session(sessopts, None, registry);
let sess = build_session(sessopts, None, None, registry);
assert!(sess.diagnostic().flags.can_emit_warnings);
});
}
Expand Down
45 changes: 20 additions & 25 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ use std::fmt;
use std::io::Write;
use std::path::PathBuf;
use std::time::Duration;
use std::sync::mpsc;
use std::sync::{Arc, mpsc};

use parking_lot::Mutex as PlMutex;

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

/// Used by `-Z self-profile`.
pub self_profiling_active: bool,

/// Used by `-Z self-profile`.
pub self_profiling: Lock<SelfProfiler>,
/// Used by -Z self-profile
pub self_profiling: Option<Arc<PlMutex<SelfProfiler>>>,

/// Some measurements that are being gathered during compilation.
pub perf_stats: PerfStats,
Expand Down Expand Up @@ -833,27 +832,23 @@ impl Session {
#[inline(never)]
#[cold]
fn profiler_active<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
let mut profiler = self.self_profiling.borrow_mut();
f(&mut profiler);
match &self.self_profiling {
None => bug!("profiler_active() called but there was no profiler active"),
Some(profiler) => {
let mut p = profiler.lock();

f(&mut p);
}
}
}

#[inline(always)]
pub fn profiler<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
if unlikely!(self.self_profiling_active) {
if unlikely!(self.self_profiling.is_some()) {
self.profiler_active(f)
}
}

pub fn print_profiler_results(&self) {
let mut profiler = self.self_profiling.borrow_mut();
profiler.print_results(&self.opts);
}

pub fn save_json_results(&self) {
let profiler = self.self_profiling.borrow();
profiler.save_results(&self.opts);
}

pub fn print_perf_stats(&self) {
println!(
"Total time spent computing symbol hashes: {}",
Expand Down Expand Up @@ -1013,13 +1008,15 @@ impl Session {

pub fn build_session(
sopts: config::Options,
self_profiler: Option<Arc<PlMutex<SelfProfiler>>>,
local_crate_source_file: Option<PathBuf>,
registry: errors::registry::Registry,
) -> Session {
let file_path_mapping = sopts.file_path_mapping();

build_session_with_source_map(
sopts,
self_profiler,
local_crate_source_file,
registry,
Lrc::new(source_map::SourceMap::new(file_path_mapping)),
Expand All @@ -1029,6 +1026,7 @@ pub fn build_session(

pub fn build_session_with_source_map(
sopts: config::Options,
self_profiler: Option<Arc<PlMutex<SelfProfiler>>>,
local_crate_source_file: Option<PathBuf>,
registry: errors::registry::Registry,
source_map: Lrc<source_map::SourceMap>,
Expand Down Expand Up @@ -1103,11 +1101,12 @@ pub fn build_session_with_source_map(
},
);

build_session_(sopts, local_crate_source_file, diagnostic_handler, source_map)
build_session_(sopts, self_profiler, local_crate_source_file, diagnostic_handler, source_map)
}

pub fn build_session_(
sopts: config::Options,
self_profiler: Option<Arc<PlMutex<SelfProfiler>>>,
local_crate_source_file: Option<PathBuf>,
span_diagnostic: errors::Handler,
source_map: Lrc<source_map::SourceMap>,
Expand Down Expand Up @@ -1161,9 +1160,6 @@ pub fn build_session_(
CguReuseTracker::new_disabled()
};

let self_profiling_active = sopts.debugging_opts.self_profile ||
sopts.debugging_opts.profile_json;

let sess = Session {
target: target_cfg,
host,
Expand Down Expand Up @@ -1192,8 +1188,7 @@ pub fn build_session_(
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
cgu_reuse_tracker,
self_profiling_active,
self_profiling: Lock::new(SelfProfiler::new()),
self_profiling: self_profiler,
profile_channel: Lock::new(None),
perf_stats: PerfStats {
symbol_hash_time: Lock::new(Duration::from_secs(0)),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ arena = { path = "../libarena" }
graphviz = { path = "../libgraphviz" }
log = "0.4"
env_logger = { version = "0.5", default-features = false }
parking_lot = "0.6"
rustc-rayon = "0.1.1"
scoped-tls = { version = "0.1.1", features = ["nightly"] }
rustc = { path = "../librustc" }
Expand Down
8 changes: 0 additions & 8 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,6 @@ pub fn compile_input(
sess.print_perf_stats();
}

if sess.opts.debugging_opts.self_profile {
sess.print_profiler_results();
}

if sess.opts.debugging_opts.profile_json {
sess.save_json_results();
}

controller_entry_point!(
compilation_done,
sess,
Expand Down
36 changes: 34 additions & 2 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern crate graphviz;
extern crate env_logger;
#[cfg(unix)]
extern crate libc;
extern crate parking_lot;
extern crate rustc_rayon as rayon;
extern crate rustc;
extern crate rustc_allocator;
Expand Down Expand Up @@ -64,6 +65,7 @@ use rustc::session::config::{Input, PrintRequest, ErrorOutputType};
use rustc::session::config::nightly_options;
use rustc::session::filesearch;
use rustc::session::{early_error, early_warn};
use rustc::util::profiling::{SelfProfiler};
use rustc::lint::Lint;
use rustc::lint;
use rustc_metadata::locator;
Expand All @@ -90,7 +92,7 @@ use std::path::{PathBuf, Path};
use std::process::{self, Command, Stdio};
use std::str;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Once, ONCE_INIT};
use std::sync::{Arc, Once, ONCE_INIT};
use std::thread;

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

use parking_lot::Mutex as PlMutex;

#[cfg(test)]
mod test;

Expand Down Expand Up @@ -461,6 +465,13 @@ fn run_compiler_with_pool<'a>(
}
}}

let self_profiling_active = sopts.debugging_opts.self_profile ||
sopts.debugging_opts.profile_json;

let profiler =
if self_profiling_active { Some(Arc::new(PlMutex::new(SelfProfiler::new()))) }
else { None };

let descriptions = diagnostics_registry();

do_or_return!(callbacks.early_callback(&matches,
Expand All @@ -485,7 +496,12 @@ fn run_compiler_with_pool<'a>(
let loader = file_loader.unwrap_or(box RealFileLoader);
let source_map = Lrc::new(SourceMap::with_file_loader(loader, sopts.file_path_mapping()));
let mut sess = session::build_session_with_source_map(
sopts, input_file_path.clone(), descriptions, source_map, emitter_dest,
sopts,
profiler.clone(),
input_file_path.clone(),
descriptions,
source_map,
emitter_dest,
);

if let Some(err) = input_err {
Expand Down Expand Up @@ -531,6 +547,21 @@ fn run_compiler_with_pool<'a>(
&control)
};

match profiler {
None => { },
Some(profiler) => {
let mut profiler = profiler.lock();

if sess.opts.debugging_opts.self_profile {
profiler.print_results(&sess.opts);
}

if sess.opts.debugging_opts.profile_json {
profiler.save_results(&sess.opts);
}
}
}

(result, Some(sess))
}

Expand Down Expand Up @@ -810,6 +841,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
match matches.free.len() {
0 => {
let mut sess = build_session(sopts.clone(),
None,
None,
descriptions.clone());
if sopts.describe_lints {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ fn test_env_with_pool<F>(
let sess = session::build_session_(
options,
None,
None,
diagnostic_handler,
Lrc::new(SourceMap::new(FilePathMapping::empty())),
);
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
debugging_options.ui_testing);

let mut sess = session::build_session_(
sessopts, cpath, diagnostic_handler, source_map,
sessopts, None, cpath, diagnostic_handler, source_map,
);

lint::builtin::HardwiredLints.get_lints()
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn run(mut options: Options) -> isize {
Some(source_map.clone()));

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

let mut sess = session::build_session_(
sessopts, None, diagnostic_handler, source_map,
sessopts, None, None, diagnostic_handler, source_map,
);
let codegen_backend = rustc_driver::get_codegen_backend(&sess);
let cstore = CStore::new(codegen_backend.metadata_loader());
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make-fulldeps/issue-19371/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn main() {

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

0 comments on commit 32dd8af

Please sign in to comment.