Skip to content

Commit 8f5f92a

Browse files
authored
Rollup merge of rust-lang#64840 - michaelwoerister:self-profiling-raii-refactor, r=wesleywiser
SelfProfiler API refactoring and part one of event review This PR refactors the `SelfProfiler` a little bit so that most profiling methods are RAII-based. The codegen backend code already had something similar, this refactoring pulls this functionality up into `SelfProfiler` itself, for general use. The second commit of this PR is a review and update of the existing events we are already recording. Names have been made more consistent. CGU names have been removed from event names. They will be added back in when function parameter recording is implemented. There is still some work to be done for adding new events, especially around trait resolution and the incremental system. r? @wesleywiser
2 parents 0e88e56 + d942622 commit 8f5f92a

File tree

14 files changed

+366
-268
lines changed

14 files changed

+366
-268
lines changed

src/librustc/session/mod.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use syntax::source_map;
3232
use syntax::parse::{self, ParseSess};
3333
use syntax::symbol::Symbol;
3434
use syntax_pos::{MultiSpan, Span};
35-
use crate::util::profiling::SelfProfiler;
35+
use crate::util::profiling::{SelfProfiler, SelfProfilerRef};
3636

3737
use rustc_target::spec::{PanicStrategy, RelroLevel, Target, TargetTriple};
3838
use rustc_data_structures::flock;
@@ -129,7 +129,7 @@ pub struct Session {
129129
pub profile_channel: Lock<Option<mpsc::Sender<ProfileQueriesMsg>>>,
130130

131131
/// Used by `-Z self-profile`.
132-
pub self_profiling: Option<Arc<SelfProfiler>>,
132+
pub prof: SelfProfilerRef,
133133

134134
/// Some measurements that are being gathered during compilation.
135135
pub perf_stats: PerfStats,
@@ -835,24 +835,6 @@ impl Session {
835835
}
836836
}
837837

838-
#[inline(never)]
839-
#[cold]
840-
fn profiler_active<F: FnOnce(&SelfProfiler) -> ()>(&self, f: F) {
841-
match &self.self_profiling {
842-
None => bug!("profiler_active() called but there was no profiler active"),
843-
Some(profiler) => {
844-
f(&profiler);
845-
}
846-
}
847-
}
848-
849-
#[inline(always)]
850-
pub fn profiler<F: FnOnce(&SelfProfiler) -> ()>(&self, f: F) {
851-
if unlikely!(self.self_profiling.is_some()) {
852-
self.profiler_active(f)
853-
}
854-
}
855-
856838
pub fn print_perf_stats(&self) {
857839
println!(
858840
"Total time spent computing symbol hashes: {}",
@@ -1251,7 +1233,7 @@ fn build_session_(
12511233
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
12521234
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
12531235
cgu_reuse_tracker,
1254-
self_profiling: self_profiler,
1236+
prof: SelfProfilerRef::new(self_profiler),
12551237
profile_channel: Lock::new(None),
12561238
perf_stats: PerfStats {
12571239
symbol_hash_time: Lock::new(Duration::from_secs(0)),

src/librustc/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use crate::ty::CanonicalPolyFnSig;
4545
use crate::util::common::ErrorReported;
4646
use crate::util::nodemap::{DefIdMap, DefIdSet, ItemLocalMap, ItemLocalSet};
4747
use crate::util::nodemap::{FxHashMap, FxHashSet};
48+
use crate::util::profiling::SelfProfilerRef;
4849

4950
use errors::DiagnosticBuilder;
5051
use arena::SyncDroplessArena;
@@ -1030,6 +1031,8 @@ pub struct GlobalCtxt<'tcx> {
10301031

10311032
pub dep_graph: DepGraph,
10321033

1034+
pub prof: SelfProfilerRef,
1035+
10331036
/// Common objects.
10341037
pub common: Common<'tcx>,
10351038

@@ -1260,6 +1263,7 @@ impl<'tcx> TyCtxt<'tcx> {
12601263
arena: WorkerLocal::new(|_| Arena::default()),
12611264
interners,
12621265
dep_graph,
1266+
prof: s.prof.clone(),
12631267
common,
12641268
types: common_types,
12651269
lifetimes: common_lifetimes,

src/librustc/ty/query/plumbing.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
112112
let mut lock = cache.get_shard_by_value(key).lock();
113113
if let Some(value) = lock.results.get(key) {
114114
profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
115-
tcx.sess.profiler(|p| p.record_query_hit(Q::NAME));
115+
tcx.prof.query_cache_hit(Q::NAME);
116116
let result = (value.value.clone(), value.index);
117117
#[cfg(debug_assertions)]
118118
{
@@ -128,7 +128,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
128128
// in another thread has completed. Record how long we wait in the
129129
// self-profiler.
130130
#[cfg(parallel_compiler)]
131-
tcx.sess.profiler(|p| p.query_blocked_start(Q::NAME));
131+
tcx.prof.query_blocked_start(Q::NAME);
132132

133133
job.clone()
134134
},
@@ -170,7 +170,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
170170
#[cfg(parallel_compiler)]
171171
{
172172
let result = job.r#await(tcx, span);
173-
tcx.sess.profiler(|p| p.query_blocked_end(Q::NAME));
173+
tcx.prof.query_blocked_end(Q::NAME);
174174

175175
if let Err(cycle) = result {
176176
return TryGetJob::Cycle(Q::handle_cycle_error(tcx, cycle));
@@ -382,8 +382,9 @@ impl<'tcx> TyCtxt<'tcx> {
382382
}
383383

384384
if Q::ANON {
385+
385386
profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
386-
self.sess.profiler(|p| p.start_query(Q::NAME));
387+
let prof_timer = self.prof.query_provider(Q::NAME);
387388

388389
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
389390
self.start_query(job.job.clone(), diagnostics, |tcx| {
@@ -393,7 +394,7 @@ impl<'tcx> TyCtxt<'tcx> {
393394
})
394395
});
395396

396-
self.sess.profiler(|p| p.end_query(Q::NAME));
397+
drop(prof_timer);
397398
profq_msg!(self, ProfileQueriesMsg::ProviderEnd);
398399

399400
self.dep_graph.read_index(dep_node_index);
@@ -451,9 +452,8 @@ impl<'tcx> TyCtxt<'tcx> {
451452
// First we try to load the result from the on-disk cache.
452453
let result = if Q::cache_on_disk(self, key.clone(), None) &&
453454
self.sess.opts.debugging_opts.incremental_queries {
454-
self.sess.profiler(|p| p.incremental_load_result_start(Q::NAME));
455+
let _prof_timer = self.prof.incr_cache_loading(Q::NAME);
455456
let result = Q::try_load_from_disk(self, prev_dep_node_index);
456-
self.sess.profiler(|p| p.incremental_load_result_end(Q::NAME));
457457

458458
// We always expect to find a cached result for things that
459459
// can be forced from `DepNode`.
@@ -469,21 +469,17 @@ impl<'tcx> TyCtxt<'tcx> {
469469

470470
let result = if let Some(result) = result {
471471
profq_msg!(self, ProfileQueriesMsg::CacheHit);
472-
self.sess.profiler(|p| p.record_query_hit(Q::NAME));
473-
474472
result
475473
} else {
476474
// We could not load a result from the on-disk cache, so
477475
// recompute.
478-
479-
self.sess.profiler(|p| p.start_query(Q::NAME));
476+
let _prof_timer = self.prof.query_provider(Q::NAME);
480477

481478
// The dep-graph for this computation is already in-place.
482479
let result = self.dep_graph.with_ignore(|| {
483480
Q::compute(self, key)
484481
});
485482

486-
self.sess.profiler(|p| p.end_query(Q::NAME));
487483
result
488484
};
489485

@@ -551,7 +547,7 @@ impl<'tcx> TyCtxt<'tcx> {
551547
key, dep_node);
552548

553549
profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
554-
self.sess.profiler(|p| p.start_query(Q::NAME));
550+
let prof_timer = self.prof.query_provider(Q::NAME);
555551

556552
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
557553
self.start_query(job.job.clone(), diagnostics, |tcx| {
@@ -571,7 +567,7 @@ impl<'tcx> TyCtxt<'tcx> {
571567
})
572568
});
573569

574-
self.sess.profiler(|p| p.end_query(Q::NAME));
570+
drop(prof_timer);
575571
profq_msg!(self, ProfileQueriesMsg::ProviderEnd);
576572

577573
if unlikely!(self.sess.opts.debugging_opts.query_dep_graph) {
@@ -619,7 +615,7 @@ impl<'tcx> TyCtxt<'tcx> {
619615
let _ = self.get_query::<Q>(DUMMY_SP, key);
620616
} else {
621617
profq_msg!(self, ProfileQueriesMsg::CacheHit);
622-
self.sess.profiler(|p| p.record_query_hit(Q::NAME));
618+
self.prof.query_cache_hit(Q::NAME);
623619
}
624620
}
625621

0 commit comments

Comments
 (0)