Skip to content

Commit

Permalink
Capture time spent blocked waiting on queries
Browse files Browse the repository at this point in the history
This captures time spent blocked when a query is waiting for another
query to finish executing in another thread.
  • Loading branch information
wesleywiser committed Feb 11, 2019
1 parent ae044ee commit 8170828
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/librustc/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,15 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
let job = match lock.active.entry((*key).clone()) {
Entry::Occupied(entry) => {
match *entry.get() {
QueryResult::Started(ref job) => job.clone(),
QueryResult::Started(ref job) => {
//For parallel queries, we'll block and wait until the query running
//in another thread has completed. Record how long we wait in the
//self-profiler
#[cfg(parallel_compiler)]
tcx.sess.profiler(|p| p.query_blocked_start(Q::NAME, Q::CATEGORY));

job.clone()
},
QueryResult::Poisoned => FatalError.raise(),
}
}
Expand Down Expand Up @@ -160,7 +168,10 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
// thread
#[cfg(parallel_compiler)]
{
if let Err(cycle) = job.r#await(tcx, span) {
let result = job.r#await(tcx, span);
tcx.sess.profiler(|p| p.query_blocked_end(Q::NAME, Q::CATEGORY));

if let Err(cycle) = result {
return TryGetJob::JobCompleted(Err(cycle));
}
}
Expand Down
28 changes: 26 additions & 2 deletions src/librustc/util/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ pub enum ProfilerEvent {
QueryCount { query_name: &'static str, category: ProfileCategory, count: usize },
IncrementalLoadResultStart { query_name: &'static str, time: Instant },
IncrementalLoadResultEnd { query_name: &'static str, time: Instant },
QueryBlockedStart { query_name: &'static str, category: ProfileCategory, time: Instant },
QueryBlockedEnd { query_name: &'static str, category: ProfileCategory, time: Instant },
}

impl ProfilerEvent {
Expand All @@ -36,13 +38,15 @@ impl ProfilerEvent {
match self {
QueryStart { .. } |
GenericActivityStart { .. } |
IncrementalLoadResultStart { .. } => true,
IncrementalLoadResultStart { .. } |
QueryBlockedStart { .. } => true,

QueryEnd { .. } |
GenericActivityEnd { .. } |
QueryCacheHit { .. } |
QueryCount { .. } |
IncrementalLoadResultEnd { .. } => false,
IncrementalLoadResultEnd { .. } |
QueryBlockedEnd { .. } => false,
}
}
}
Expand Down Expand Up @@ -249,6 +253,24 @@ impl SelfProfiler {
})
}

#[inline]
pub fn query_blocked_start(&mut self, query_name: &'static str, category: ProfileCategory) {
self.record(ProfilerEvent::QueryBlockedStart {
query_name,
category,
time: Instant::now(),
})
}

#[inline]
pub fn query_blocked_end(&mut self, query_name: &'static str, category: ProfileCategory) {
self.record(ProfilerEvent::QueryBlockedEnd {
query_name,
category,
time: Instant::now(),
})
}

#[inline]
fn record(&mut self, event: ProfilerEvent) {
let thread_id = std::thread::current().id();
Expand Down Expand Up @@ -343,6 +365,8 @@ impl SelfProfiler {
},
//we don't summarize incremental load result events in the simple output mode
IncrementalLoadResultStart { .. } | IncrementalLoadResultEnd { .. } => { },
//we don't summarize parallel query blocking in the simple output mode
QueryBlockedStart { .. } | QueryBlockedEnd { .. } => { },
}
}

Expand Down

0 comments on commit 8170828

Please sign in to comment.