Skip to content

Commit 8170828

Browse files
committed
Capture time spent blocked waiting on queries
This captures time spent blocked when a query is waiting for another query to finish executing in another thread.
1 parent ae044ee commit 8170828

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/librustc/ty/query/plumbing.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,15 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
124124
let job = match lock.active.entry((*key).clone()) {
125125
Entry::Occupied(entry) => {
126126
match *entry.get() {
127-
QueryResult::Started(ref job) => job.clone(),
127+
QueryResult::Started(ref job) => {
128+
//For parallel queries, we'll block and wait until the query running
129+
//in another thread has completed. Record how long we wait in the
130+
//self-profiler
131+
#[cfg(parallel_compiler)]
132+
tcx.sess.profiler(|p| p.query_blocked_start(Q::NAME, Q::CATEGORY));
133+
134+
job.clone()
135+
},
128136
QueryResult::Poisoned => FatalError.raise(),
129137
}
130138
}
@@ -160,7 +168,10 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
160168
// thread
161169
#[cfg(parallel_compiler)]
162170
{
163-
if let Err(cycle) = job.r#await(tcx, span) {
171+
let result = job.r#await(tcx, span);
172+
tcx.sess.profiler(|p| p.query_blocked_end(Q::NAME, Q::CATEGORY));
173+
174+
if let Err(cycle) = result {
164175
return TryGetJob::JobCompleted(Err(cycle));
165176
}
166177
}

src/librustc/util/profiling.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub enum ProfilerEvent {
2727
QueryCount { query_name: &'static str, category: ProfileCategory, count: usize },
2828
IncrementalLoadResultStart { query_name: &'static str, time: Instant },
2929
IncrementalLoadResultEnd { query_name: &'static str, time: Instant },
30+
QueryBlockedStart { query_name: &'static str, category: ProfileCategory, time: Instant },
31+
QueryBlockedEnd { query_name: &'static str, category: ProfileCategory, time: Instant },
3032
}
3133

3234
impl ProfilerEvent {
@@ -36,13 +38,15 @@ impl ProfilerEvent {
3638
match self {
3739
QueryStart { .. } |
3840
GenericActivityStart { .. } |
39-
IncrementalLoadResultStart { .. } => true,
41+
IncrementalLoadResultStart { .. } |
42+
QueryBlockedStart { .. } => true,
4043

4144
QueryEnd { .. } |
4245
GenericActivityEnd { .. } |
4346
QueryCacheHit { .. } |
4447
QueryCount { .. } |
45-
IncrementalLoadResultEnd { .. } => false,
48+
IncrementalLoadResultEnd { .. } |
49+
QueryBlockedEnd { .. } => false,
4650
}
4751
}
4852
}
@@ -249,6 +253,24 @@ impl SelfProfiler {
249253
})
250254
}
251255

256+
#[inline]
257+
pub fn query_blocked_start(&mut self, query_name: &'static str, category: ProfileCategory) {
258+
self.record(ProfilerEvent::QueryBlockedStart {
259+
query_name,
260+
category,
261+
time: Instant::now(),
262+
})
263+
}
264+
265+
#[inline]
266+
pub fn query_blocked_end(&mut self, query_name: &'static str, category: ProfileCategory) {
267+
self.record(ProfilerEvent::QueryBlockedEnd {
268+
query_name,
269+
category,
270+
time: Instant::now(),
271+
})
272+
}
273+
252274
#[inline]
253275
fn record(&mut self, event: ProfilerEvent) {
254276
let thread_id = std::thread::current().id();
@@ -343,6 +365,8 @@ impl SelfProfiler {
343365
},
344366
//we don't summarize incremental load result events in the simple output mode
345367
IncrementalLoadResultStart { .. } | IncrementalLoadResultEnd { .. } => { },
368+
//we don't summarize parallel query blocking in the simple output mode
369+
QueryBlockedStart { .. } | QueryBlockedEnd { .. } => { },
346370
}
347371
}
348372

0 commit comments

Comments
 (0)