Skip to content

Commit 87a4363

Browse files
committed
Auto merge of #58425 - wesleywiser:more_profiler_changes, r=michaelwoerister
[self-profiler] Make the profiler faster/more efficient Related to #58372 r? @michaelwoerister
2 parents 2cfd644 + f20ad70 commit 87a4363

File tree

9 files changed

+343
-372
lines changed

9 files changed

+343
-372
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/config.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1405,9 +1405,7 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14051405
crate_attr: Vec<String> = (Vec::new(), parse_string_push, [TRACKED],
14061406
"inject the given attribute in the crate"),
14071407
self_profile: bool = (false, parse_bool, [UNTRACKED],
1408-
"run the self profiler"),
1409-
profile_json: bool = (false, parse_bool, [UNTRACKED],
1410-
"output a json file with profiler results"),
1408+
"run the self profiler and output the raw event data"),
14111409
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
14121410
"emits a section containing stack size metadata"),
14131411
plt: Option<bool> = (None, parse_opt_bool, [TRACKED],

src/librustc/session/mod.rs

+19-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,10 @@ 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_profiler =
1135+
if sopts.debugging_opts.self_profile { Some(Arc::new(PlMutex::new(SelfProfiler::new()))) }
1136+
else { None };
1137+
11391138
let host_triple = TargetTriple::from_triple(config::host_triple());
11401139
let host = Target::search(&host_triple).unwrap_or_else(|e|
11411140
span_diagnostic
@@ -1185,9 +1184,6 @@ pub fn build_session_(
11851184
CguReuseTracker::new_disabled()
11861185
};
11871186

1188-
let self_profiling_active = sopts.debugging_opts.self_profile ||
1189-
sopts.debugging_opts.profile_json;
1190-
11911187
let sess = Session {
11921188
target: target_cfg,
11931189
host,
@@ -1216,8 +1212,7 @@ pub fn build_session_(
12161212
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
12171213
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
12181214
cgu_reuse_tracker,
1219-
self_profiling_active,
1220-
self_profiling: Lock::new(SelfProfiler::new()),
1215+
self_profiling: self_profiler,
12211216
profile_channel: Lock::new(None),
12221217
perf_stats: PerfStats {
12231218
symbol_hash_time: Lock::new(Duration::from_secs(0)),

0 commit comments

Comments
 (0)