rust/compiler/rustc_query_impl/src/stats.rs

113 lines
3.3 KiB
Rust
Raw Normal View History

2020-02-15 09:44:27 +00:00
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_middle::ty::query::query_storage;
use rustc_middle::ty::TyCtxt;
use rustc_query_system::query::{QueryCache, QueryCacheStore};
2020-02-15 08:48:10 +00:00
use std::any::type_name;
use std::mem;
2020-02-15 09:44:27 +00:00
trait KeyStats {
fn key_stats(&self, stats: &mut QueryStats);
}
impl<T> KeyStats for T {
default fn key_stats(&self, _: &mut QueryStats) {}
}
impl KeyStats for DefId {
fn key_stats(&self, stats: &mut QueryStats) {
if self.krate == LOCAL_CRATE {
stats.local_def_id_keys = Some(stats.local_def_id_keys.unwrap_or(0) + 1);
}
}
}
2020-02-15 08:48:10 +00:00
#[derive(Clone)]
struct QueryStats {
name: &'static str,
key_size: usize,
key_type: &'static str,
value_size: usize,
value_type: &'static str,
entry_count: usize,
2020-02-15 09:44:27 +00:00
local_def_id_keys: Option<usize>,
2020-02-15 08:48:10 +00:00
}
fn stats<C>(name: &'static str, map: &QueryCacheStore<C>) -> QueryStats
where
C: QueryCache,
{
2020-02-15 09:44:27 +00:00
let mut stats = QueryStats {
2020-02-15 08:48:10 +00:00
name,
key_size: mem::size_of::<C::Key>(),
key_type: type_name::<C::Key>(),
value_size: mem::size_of::<C::Value>(),
value_type: type_name::<C::Value>(),
entry_count: 0,
2020-02-15 09:44:27 +00:00
local_def_id_keys: None,
};
map.iter_results(&mut |key, _, _| {
stats.entry_count += 1;
key.key_stats(&mut stats)
2020-02-15 09:44:27 +00:00
});
stats
2020-02-15 08:48:10 +00:00
}
pub fn print_stats(tcx: TyCtxt<'_>) {
let queries = query_stats(tcx);
let mut query_key_sizes = queries.clone();
query_key_sizes.sort_by_key(|q| q.key_size);
eprintln!("\nLarge query keys:");
2020-02-15 08:48:10 +00:00
for q in query_key_sizes.iter().rev().filter(|q| q.key_size > 8) {
eprintln!(" {} - {} x {} - {}", q.name, q.key_size, q.entry_count, q.key_type);
2020-02-15 08:48:10 +00:00
}
let mut query_value_sizes = queries.clone();
query_value_sizes.sort_by_key(|q| q.value_size);
eprintln!("\nLarge query values:");
2020-02-15 08:48:10 +00:00
for q in query_value_sizes.iter().rev().filter(|q| q.value_size > 8) {
eprintln!(" {} - {} x {} - {}", q.name, q.value_size, q.entry_count, q.value_type);
2020-02-15 08:48:10 +00:00
}
let mut query_value_count = queries.clone();
query_value_count.sort_by_key(|q| q.entry_count);
eprintln!("\nQuery value count:");
2020-02-15 08:48:10 +00:00
for q in query_value_count.iter().rev() {
eprintln!(" {} - {}", q.name, q.entry_count);
2020-02-15 08:48:10 +00:00
}
2020-02-15 09:44:27 +00:00
let mut def_id_density: Vec<_> =
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());
eprintln!("\nLocal DefId density:");
let total = tcx.resolutions(()).definitions.def_index_count() as f64;
2020-02-15 09:44:27 +00:00
for q in def_id_density.iter().rev() {
2020-02-15 11:00:31 +00:00
let local = q.local_def_id_keys.unwrap();
eprintln!(" {} - {} = ({}%)", q.name, local, (local as f64 * 100.0) / total);
2020-02-15 09:44:27 +00:00
}
2020-02-15 08:48:10 +00:00
}
macro_rules! print_stats {
2020-10-11 18:44:24 +00:00
(<$tcx:tt>
2020-10-11 18:46:46 +00:00
$($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,)*
2020-10-11 18:44:24 +00:00
) => {
2020-02-15 08:48:10 +00:00
fn query_stats(tcx: TyCtxt<'_>) -> Vec<QueryStats> {
let mut queries = Vec::new();
2020-10-11 18:44:24 +00:00
$(
2020-03-06 19:33:13 +00:00
queries.push(stats::<
query_storage::$name<'_>,
2020-03-06 19:33:13 +00:00
>(
2020-02-15 08:48:10 +00:00
stringify!($name),
&tcx.query_caches.$name,
2020-02-15 08:48:10 +00:00
));
2020-10-11 18:44:24 +00:00
)*
2020-02-15 08:48:10 +00:00
queries
}
}
}
rustc_query_append! { [print_stats!][<'tcx>] }