Skip to content

Commit

Permalink
Fix cache hit stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Feb 15, 2020
1 parent a82cc1f commit 0b31698
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/librustc/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use std::hash::{Hash, Hasher};
use std::mem;
use std::num::NonZeroU32;
use std::ptr;
#[cfg(debug_assertions)]
use std::sync::atomic::{AtomicUsize, Ordering};

pub(crate) struct QueryStateShard<'tcx, D: QueryAccessors<'tcx> + ?Sized> {
pub(super) cache: <<D as QueryAccessors<'tcx>>::Cache as QueryCache<D::Key, D::Value>>::Sharded,
Expand Down Expand Up @@ -51,7 +53,7 @@ pub(crate) struct QueryState<'tcx, D: QueryAccessors<'tcx> + ?Sized> {
pub(super) cache: D::Cache,
pub(super) shards: Sharded<QueryStateShard<'tcx, D>>,
#[cfg(debug_assertions)]
pub(super) cache_hits: usize,
pub(super) cache_hits: AtomicUsize,
}

impl<'tcx, Q: QueryAccessors<'tcx>> QueryState<'tcx, Q> {
Expand Down Expand Up @@ -100,7 +102,7 @@ impl<'tcx, M: QueryAccessors<'tcx>> Default for QueryState<'tcx, M> {
cache: M::Cache::default(),
shards: Default::default(),
#[cfg(debug_assertions)]
cache_hits: 0,
cache_hits: AtomicUsize::new(0),
}
}
}
Expand Down Expand Up @@ -439,6 +441,10 @@ impl<'tcx> TyCtxt<'tcx> {
if unlikely!(self.prof.enabled()) {
self.prof.query_cache_hit(index.into());
}
#[cfg(debug_assertions)]
{
state.cache_hits.fetch_add(1, Ordering::Relaxed);
}
on_hit(value, index)
},
on_miss,
Expand Down
8 changes: 6 additions & 2 deletions src/librustc/ty/query/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE};

use std::any::type_name;
use std::mem;
#[cfg(debug_assertions)]
use std::sync::atomic::Ordering;

trait KeyStats {
fn key_stats(&self, stats: &mut QueryStats);
Expand Down Expand Up @@ -42,7 +44,7 @@ fn stats<'tcx, Q: QueryAccessors<'tcx>>(
let mut stats = QueryStats {
name,
#[cfg(debug_assertions)]
cache_hits: map.cache_hits,
cache_hits: map.cache_hits.load(Ordering::Relaxed),
#[cfg(not(debug_assertions))]
cache_hits: 0,
key_size: mem::size_of::<Q::Key>(),
Expand Down Expand Up @@ -108,8 +110,10 @@ pub fn print_stats(tcx: TyCtxt<'_>) {
queries.iter().filter(|q| q.local_def_id_keys.is_some()).collect();
def_id_density.sort_by_key(|q| q.local_def_id_keys.unwrap());
println!("\nLocal DefId density:");
let total = tcx.hir().definitions().def_index_count() as f64;
for q in def_id_density.iter().rev() {
println!(" {} - {}", q.name, q.local_def_id_keys.unwrap());
let local = q.local_def_id_keys.unwrap();
println!(" {} - {} = ({}%)", q.name, local, (local as f64 * 100.0) / total);
}
}

Expand Down

0 comments on commit 0b31698

Please sign in to comment.