Skip to content

Commit 9d54812

Browse files
committed
Auto merge of #57095 - Zoxc:prof-fix, r=michaelwoerister
Fix and optimize query profiling r? @michaelwoerister cc @wesleywiser
2 parents 8e2063d + 23c742c commit 9d54812

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

src/librustc/ty/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12481248

12491249
sync::assert_send_val(&gcx);
12501250

1251-
tls::enter_global(gcx, f)
1251+
let r = tls::enter_global(gcx, f);
1252+
1253+
gcx.queries.record_computed_queries(s);
1254+
1255+
r
12521256
}
12531257

12541258
pub fn consider_optimizing<T: Fn() -> String>(&self, msg: T) -> bool {

src/librustc/ty/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use ty::subst::Substs;
4242
use util::nodemap::{DefIdSet, DefIdMap, ItemLocalSet};
4343
use util::common::{ErrorReported};
4444
use util::profiling::ProfileCategory::*;
45+
use session::Session;
4546

4647
use rustc_data_structures::bit_set::BitSet;
4748
use rustc_data_structures::indexed_vec::IndexVec;

src/librustc/ty/query/plumbing.rs

+25-22
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
112112
let mut lock = cache.borrow_mut();
113113
if let Some(value) = lock.results.get(key) {
114114
profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
115-
tcx.sess.profiler(|p| {
116-
p.record_query(Q::CATEGORY);
117-
p.record_query_hit(Q::CATEGORY);
118-
});
119-
115+
tcx.sess.profiler(|p| p.record_query_hit(Q::CATEGORY));
120116
let result = Ok((value.value.clone(), value.index));
121117
#[cfg(debug_assertions)]
122118
{
@@ -195,6 +191,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
195191
/// Executes a job by changing the ImplicitCtxt to point to the
196192
/// new query job while it executes. It returns the diagnostics
197193
/// captured during execution and the actual result.
194+
#[inline(always)]
198195
pub(super) fn start<'lcx, F, R>(
199196
&self,
200197
tcx: TyCtxt<'_, 'tcx, 'lcx>,
@@ -382,13 +379,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
382379
)
383380
);
384381

385-
self.sess.profiler(|p| p.record_query(Q::CATEGORY));
386-
387382
let job = match JobOwner::try_get(self, span, &key) {
388383
TryGetJob::NotYetStarted(job) => job,
389384
TryGetJob::JobCompleted(result) => {
390385
return result.map(|(v, index)| {
391-
self.sess.profiler(|p| p.record_query_hit(Q::CATEGORY));
392386
self.dep_graph.read_index(index);
393387
v
394388
})
@@ -430,9 +424,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
430424

431425
if !dep_node.kind.is_input() {
432426
if let Some(dep_node_index) = self.try_mark_green_and_read(&dep_node) {
433-
profq_msg!(self, ProfileQueriesMsg::CacheHit);
434-
self.sess.profiler(|p| p.record_query_hit(Q::CATEGORY));
435-
436427
return self.load_from_disk_and_cache_in_memory::<Q>(key,
437428
job,
438429
dep_node_index,
@@ -483,11 +474,16 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
483474
};
484475

485476
let result = if let Some(result) = result {
477+
profq_msg!(self, ProfileQueriesMsg::CacheHit);
478+
self.sess.profiler(|p| p.record_query_hit(Q::CATEGORY));
479+
486480
result
487481
} else {
488482
// We could not load a result from the on-disk cache, so
489483
// recompute.
490484

485+
self.sess.profiler(|p| p.start_activity(Q::CATEGORY));
486+
491487
// The diagnostics for this query have already been
492488
// promoted to the current session during
493489
// try_mark_green(), so we can ignore them here.
@@ -498,6 +494,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
498494
Q::compute(tcx, key)
499495
})
500496
});
497+
498+
self.sess.profiler(|p| p.end_activity(Q::CATEGORY));
501499
result
502500
};
503501

@@ -547,6 +545,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
547545
for {:?}", dep_node);
548546
}
549547

548+
#[inline(always)]
550549
fn force_query_with_job<Q: QueryDescription<'gcx>>(
551550
self,
552551
key: Q::Key,
@@ -565,10 +564,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
565564
key, dep_node);
566565

567566
profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
568-
self.sess.profiler(|p| {
569-
p.start_activity(Q::CATEGORY);
570-
p.record_query(Q::CATEGORY);
571-
});
567+
self.sess.profiler(|p| p.start_activity(Q::CATEGORY));
572568

573569
let res = job.start(self, |tcx| {
574570
if dep_node.kind.is_eval_always() {
@@ -624,14 +620,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
624620
// this introduces should be negligible as we'll immediately hit the
625621
// in-memory cache, or another query down the line will.
626622

627-
self.sess.profiler(|p| {
628-
p.start_activity(Q::CATEGORY);
629-
p.record_query(Q::CATEGORY);
630-
});
631-
632623
let _ = self.get_query::<Q>(DUMMY_SP, key);
633-
634-
self.sess.profiler(|p| p.end_activity(Q::CATEGORY));
624+
} else {
625+
profq_msg!(self, ProfileQueriesMsg::CacheHit);
626+
self.sess.profiler(|p| p.record_query_hit(Q::CATEGORY));
635627
}
636628
}
637629

@@ -751,6 +743,17 @@ macro_rules! define_queries_inner {
751743
}
752744
}
753745

746+
pub fn record_computed_queries(&self, sess: &Session) {
747+
sess.profiler(|p| {
748+
$(
749+
p.record_computed_queries(
750+
<queries::$name<'_> as QueryConfig<'_>>::CATEGORY,
751+
self.$name.lock().results.len()
752+
);
753+
)*
754+
});
755+
}
756+
754757
#[cfg(parallel_queries)]
755758
pub fn collect_active_jobs(&self) -> Vec<Lrc<QueryJob<$tcx>>> {
756759
let mut jobs = Vec::new();

src/librustc/util/profiling.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ macro_rules! define_categories {
6262
let total_time = ($(self.times.$name + )* 0) as f32;
6363

6464
$(
65-
let (hits, total) = self.query_counts.$name;
65+
let (hits, computed) = self.query_counts.$name;
66+
let total = hits + computed;
6667
let (hits, total) = if total > 0 {
6768
(format!("{:.2}",
6869
(((hits as f32) / (total as f32)) * 100.0)), total.to_string())
@@ -86,7 +87,8 @@ macro_rules! define_categories {
8687
let mut json = String::from("[");
8788

8889
$(
89-
let (hits, total) = self.query_counts.$name;
90+
let (hits, computed) = self.query_counts.$name;
91+
let total = hits + computed;
9092

9193
//normalize hits to 0%
9294
let hit_percent =
@@ -168,14 +170,14 @@ impl SelfProfiler {
168170
self.timer_stack.push(category);
169171
}
170172

171-
pub fn record_query(&mut self, category: ProfileCategory) {
172-
let (hits, total) = *self.data.query_counts.get(category);
173-
self.data.query_counts.set(category, (hits, total + 1));
173+
pub fn record_computed_queries(&mut self, category: ProfileCategory, count: usize) {
174+
let (hits, computed) = *self.data.query_counts.get(category);
175+
self.data.query_counts.set(category, (hits, computed + count as u64));
174176
}
175177

176178
pub fn record_query_hit(&mut self, category: ProfileCategory) {
177-
let (hits, total) = *self.data.query_counts.get(category);
178-
self.data.query_counts.set(category, (hits + 1, total));
179+
let (hits, computed) = *self.data.query_counts.get(category);
180+
self.data.query_counts.set(category, (hits + 1, computed));
179181
}
180182

181183
pub fn end_activity(&mut self, category: ProfileCategory) {

0 commit comments

Comments
 (0)