Skip to content

Commit 60e8253

Browse files
committed
Auto merge of #57118 - Zoxc:query-stats, r=wesleywiser
Add a command line flag to print some query stats r? @michaelwoerister
2 parents 3cda631 + 0257c5a commit 60e8253

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
12791279
"emit Retagging MIR statements, interpreted e.g., by miri; implies -Zmir-opt-level=0"),
12801280
perf_stats: bool = (false, parse_bool, [UNTRACKED],
12811281
"print some performance-related statistics"),
1282+
query_stats: bool = (false, parse_bool, [UNTRACKED],
1283+
"print some statistics about the query system"),
12821284
hir_stats: bool = (false, parse_bool, [UNTRACKED],
12831285
"print some statistics about AST and HIR"),
12841286
always_encode_mir: bool = (false, parse_bool, [TRACKED],

src/librustc/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ pub struct GlobalCtxt<'tcx> {
903903
/// as well as all upstream crates. Only populated in incremental mode.
904904
pub def_path_hash_to_def_id: Option<FxHashMap<DefPathHash, DefId>>,
905905

906-
pub(crate) queries: query::Queries<'tcx>,
906+
pub queries: query::Queries<'tcx>,
907907

908908
// Records the free variables referenced by every closure
909909
// expression. Do not track deps for this, just recompute it from

src/librustc/ty/query/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ use rustc_target::spec::PanicStrategy;
5353
use std::borrow::Cow;
5454
use std::ops::Deref;
5555
use std::sync::Arc;
56+
use std::intrinsics::type_name;
5657
use syntax_pos::{Span, DUMMY_SP};
5758
use syntax_pos::symbol::InternedString;
5859
use syntax::attr;

src/librustc/ty/query/plumbing.rs

+104-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use syntax::source_map::DUMMY_SP;
2727
pub struct QueryCache<'tcx, D: QueryConfig<'tcx> + ?Sized> {
2828
pub(super) results: FxHashMap<D::Key, QueryValue<D::Value>>,
2929
pub(super) active: FxHashMap<D::Key, QueryResult<'tcx>>,
30+
#[cfg(debug_assertions)]
31+
pub(super) cache_hits: usize,
3032
}
3133

3234
pub(super) struct QueryValue<T> {
@@ -50,6 +52,8 @@ impl<'tcx, M: QueryConfig<'tcx>> Default for QueryCache<'tcx, M> {
5052
QueryCache {
5153
results: FxHashMap::default(),
5254
active: FxHashMap::default(),
55+
#[cfg(debug_assertions)]
56+
cache_hits: 0,
5357
}
5458
}
5559
}
@@ -114,6 +118,10 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
114118
});
115119

116120
let result = Ok((value.value.clone(), value.index));
121+
#[cfg(debug_assertions)]
122+
{
123+
lock.cache_hits += 1;
124+
}
117125
return TryGetJob::JobCompleted(result);
118126
}
119127
let job = match lock.active.entry((*key).clone()) {
@@ -752,6 +760,101 @@ macro_rules! define_queries_inner {
752760

753761
jobs
754762
}
763+
764+
pub fn print_stats(&self) {
765+
let mut queries = Vec::new();
766+
767+
#[derive(Clone)]
768+
struct QueryStats {
769+
name: &'static str,
770+
cache_hits: usize,
771+
key_size: usize,
772+
key_type: &'static str,
773+
value_size: usize,
774+
value_type: &'static str,
775+
entry_count: usize,
776+
}
777+
778+
fn stats<'tcx, Q: QueryConfig<'tcx>>(
779+
name: &'static str,
780+
map: &QueryCache<'tcx, Q>
781+
) -> QueryStats {
782+
QueryStats {
783+
name,
784+
#[cfg(debug_assertions)]
785+
cache_hits: map.cache_hits,
786+
#[cfg(not(debug_assertions))]
787+
cache_hits: 0,
788+
key_size: mem::size_of::<Q::Key>(),
789+
key_type: unsafe { type_name::<Q::Key>() },
790+
value_size: mem::size_of::<Q::Value>(),
791+
value_type: unsafe { type_name::<Q::Value>() },
792+
entry_count: map.results.len(),
793+
}
794+
}
795+
796+
$(
797+
queries.push(stats::<queries::$name<'_>>(
798+
stringify!($name),
799+
&*self.$name.lock()
800+
));
801+
)*
802+
803+
if cfg!(debug_assertions) {
804+
let hits: usize = queries.iter().map(|s| s.cache_hits).sum();
805+
let results: usize = queries.iter().map(|s| s.entry_count).sum();
806+
println!("\nQuery cache hit rate: {}", hits as f64 / (hits + results) as f64);
807+
}
808+
809+
let mut query_key_sizes = queries.clone();
810+
query_key_sizes.sort_by_key(|q| q.key_size);
811+
println!("\nLarge query keys:");
812+
for q in query_key_sizes.iter().rev()
813+
.filter(|q| q.key_size > 8) {
814+
println!(
815+
" {} - {} x {} - {}",
816+
q.name,
817+
q.key_size,
818+
q.entry_count,
819+
q.key_type
820+
);
821+
}
822+
823+
let mut query_value_sizes = queries.clone();
824+
query_value_sizes.sort_by_key(|q| q.value_size);
825+
println!("\nLarge query values:");
826+
for q in query_value_sizes.iter().rev()
827+
.filter(|q| q.value_size > 8) {
828+
println!(
829+
" {} - {} x {} - {}",
830+
q.name,
831+
q.value_size,
832+
q.entry_count,
833+
q.value_type
834+
);
835+
}
836+
837+
if cfg!(debug_assertions) {
838+
let mut query_cache_hits = queries.clone();
839+
query_cache_hits.sort_by_key(|q| q.cache_hits);
840+
println!("\nQuery cache hits:");
841+
for q in query_cache_hits.iter().rev() {
842+
println!(
843+
" {} - {} ({}%)",
844+
q.name,
845+
q.cache_hits,
846+
q.cache_hits as f64 / (q.cache_hits + q.entry_count) as f64
847+
);
848+
}
849+
}
850+
851+
let mut query_value_count = queries.clone();
852+
query_value_count.sort_by_key(|q| q.entry_count);
853+
println!("\nQuery value count:");
854+
for q in query_value_count.iter().rev() {
855+
println!(" {} - {}", q.name, q.entry_count);
856+
}
857+
}
755858
}
756859

757860
#[allow(nonstandard_style)]
@@ -940,7 +1043,7 @@ macro_rules! define_queries_inner {
9401043
macro_rules! define_queries_struct {
9411044
(tcx: $tcx:tt,
9421045
input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
943-
pub(crate) struct Queries<$tcx> {
1046+
pub struct Queries<$tcx> {
9441047
/// This provides access to the incr. comp. on-disk cache for query results.
9451048
/// Do not access this directly. It is only meant to be used by
9461049
/// `DepGraph::try_mark_green()` and the query infrastructure.

src/librustc_driver/driver.rs

+4
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ pub fn compile_input(
328328
}
329329
}
330330

331+
if tcx.sess.opts.debugging_opts.query_stats {
332+
tcx.queries.print_stats();
333+
}
334+
331335
Ok((outputs.clone(), ongoing_codegen, tcx.dep_graph.clone()))
332336
},
333337
)??

0 commit comments

Comments
 (0)