Skip to content

Commit

Permalink
self-profile: Fix issue with handling query blocking.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Dec 19, 2019
1 parent fe2670a commit ff5a81d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
37 changes: 27 additions & 10 deletions src/librustc/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use rustc_data_structures::sharded::Sharded;
use rustc_data_structures::thin_vec::ThinVec;
#[cfg(not(parallel_compiler))]
use rustc_data_structures::cold_path;
#[cfg(parallel_compiler)]
use rustc_data_structures::profiling::TimingGuard;
use std::hash::{Hash, Hasher};
use std::mem;
use std::ptr;
Expand Down Expand Up @@ -91,6 +93,19 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
/// for some compile-time benchmarks.
#[inline(always)]
pub(super) fn try_get(tcx: TyCtxt<'tcx>, span: Span, key: &Q::Key) -> TryGetJob<'a, 'tcx, Q> {
// Handling the `query_blocked_prof_timer` is a bit weird because of the
// control flow in this function: Blocking is implemented by
// awaiting a running job and, once that is done, entering the loop below
// again from the top. In that second iteration we will hit the
// cache which provides us with the information we need for
// finishing the "query-blocked" event.
//
// We thus allocate `query_blocked_prof_timer` outside the loop,
// initialize it during the first iteration and finish it during the
// second iteration.
#[cfg(parallel_compiler)]
let mut query_blocked_prof_timer: Option<TimingGuard<'_>> = None;

let cache = Q::query_cache(tcx);
loop {
// We compute the key's hash once and then use it for both the
Expand All @@ -104,7 +119,17 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
if let Some((_, value)) =
lock.results.raw_entry().from_key_hashed_nocheck(key_hash, key)
{
tcx.prof.query_cache_hit(value.index.into());
if unlikely!(tcx.prof.enabled()) {
tcx.prof.query_cache_hit(value.index.into());

#[cfg(parallel_compiler)]
{
if let Some(prof_timer) = query_blocked_prof_timer.take() {
prof_timer.finish_with_query_invocation_id(value.index.into());
}
}
}

let result = (value.value.clone(), value.index);
#[cfg(debug_assertions)]
{
Expand All @@ -113,9 +138,6 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
return TryGetJob::JobCompleted(result);
}

#[cfg(parallel_compiler)]
let query_blocked_prof_timer;

let job = match lock.active.entry((*key).clone()) {
Entry::Occupied(entry) => {
match *entry.get() {
Expand All @@ -125,7 +147,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
// self-profiler.
#[cfg(parallel_compiler)]
{
query_blocked_prof_timer = tcx.prof.query_blocked(Q::NAME);
query_blocked_prof_timer = Some(tcx.prof.query_blocked());
}

job.clone()
Expand Down Expand Up @@ -169,11 +191,6 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
{
let result = job.r#await(tcx, span);

// This `drop()` is not strictly necessary as the binding
// would go out of scope anyway. But it's good to have an
// explicit marker of how far the measurement goes.
drop(query_blocked_prof_timer);

if let Err(cycle) = result {
return TryGetJob::Cycle(Q::handle_cycle_error(tcx, cycle));
}
Expand Down
1 change: 0 additions & 1 deletion src/librustc/ty/query/profiling_support.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use crate::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefId, DefIndex, LOCAL_CRATE};
use crate::hir::map::definitions::DefPathData;
use crate::ty::context::TyCtxt;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_data_structures/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ impl SelfProfilerRef {
}
}

#[inline]
pub fn enabled(&self) -> bool {
self.profiler.is_some()
}
Expand Down

0 comments on commit ff5a81d

Please sign in to comment.