Skip to content

Commit 695f06a

Browse files
committed
Auto merge of #58425 - wesleywiser:more_profiler_changes, r=<try>
[self-profiler] Make the profiler faster/more efficient Related to #58372 r? @michaelwoerister
2 parents 74e35d2 + 2ba18f3 commit 695f06a

File tree

11 files changed

+354
-384
lines changed

11 files changed

+354
-384
lines changed

Cargo.lock

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -1397,10 +1397,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
13971397
"don't interleave execution of lints; allows benchmarking individual lints"),
13981398
crate_attr: Vec<String> = (Vec::new(), parse_string_push, [TRACKED],
13991399
"inject the given attribute in the crate"),
1400-
self_profile: bool = (false, parse_bool, [UNTRACKED],
1401-
"run the self profiler"),
1402-
profile_json: bool = (false, parse_bool, [UNTRACKED],
1403-
"output a json file with profiler results"),
1400+
self_profile: bool = (true, parse_bool, [UNTRACKED],
1401+
"run the self profiler and output the raw event data"),
14041402
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
14051403
"emits a section containing stack size metadata"),
14061404
plt: Option<bool> = (None, parse_opt_bool, [TRACKED],
@@ -2614,7 +2612,7 @@ mod tests {
26142612
};
26152613
let registry = errors::registry::Registry::new(&[]);
26162614
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
2617-
let sess = build_session(sessopts, None, registry);
2615+
let sess = build_session(sessopts, None, None, registry);
26182616
let cfg = build_configuration(&sess, cfg);
26192617
assert!(cfg.contains(&(Symbol::intern("test"), None)));
26202618
});
@@ -2632,7 +2630,7 @@ mod tests {
26322630
};
26332631
let registry = errors::registry::Registry::new(&[]);
26342632
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
2635-
let sess = build_session(sessopts, None, registry);
2633+
let sess = build_session(sessopts, None, None, registry);
26362634
let cfg = build_configuration(&sess, cfg);
26372635
let mut test_items = cfg.iter().filter(|&&(name, _)| name == "test");
26382636
assert!(test_items.next().is_some());
@@ -2646,7 +2644,7 @@ mod tests {
26462644
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
26472645
let registry = errors::registry::Registry::new(&[]);
26482646
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2649-
let sess = build_session(sessopts, None, registry);
2647+
let sess = build_session(sessopts, None, None, registry);
26502648
assert!(!sess.diagnostic().flags.can_emit_warnings);
26512649
});
26522650

@@ -2656,15 +2654,15 @@ mod tests {
26562654
.unwrap();
26572655
let registry = errors::registry::Registry::new(&[]);
26582656
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2659-
let sess = build_session(sessopts, None, registry);
2657+
let sess = build_session(sessopts, None, None, registry);
26602658
assert!(sess.diagnostic().flags.can_emit_warnings);
26612659
});
26622660

26632661
syntax::with_globals(|| {
26642662
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
26652663
let registry = errors::registry::Registry::new(&[]);
26662664
let (sessopts, _) = build_session_options_and_crate_config(&matches);
2667-
let sess = build_session(sessopts, None, registry);
2665+
let sess = build_session(sessopts, None, None, registry);
26682666
assert!(sess.diagnostic().flags.can_emit_warnings);
26692667
});
26702668
}

src/librustc/session/mod.rs

+20-25
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)),

0 commit comments

Comments
 (0)