2020-01-03 04:49:14 -06:00
|
|
|
use measureme::{StringComponent, StringId};
|
2019-12-17 07:44:07 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use rustc_data_structures::profiling::SelfProfiler;
|
2022-04-15 12:27:53 -05:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
|
2020-09-01 13:34:46 -05:00
|
|
|
use rustc_hir::definitions::DefPathData;
|
2021-01-19 13:40:16 -06:00
|
|
|
use rustc_middle::ty::{TyCtxt, WithOptConstParam};
|
2022-02-19 21:44:19 -06:00
|
|
|
use rustc_query_system::query::QueryCache;
|
2019-12-17 07:44:07 -06:00
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::io::Write;
|
|
|
|
|
2021-01-17 07:57:34 -06:00
|
|
|
struct QueryKeyStringCache {
|
2019-12-17 07:44:07 -06:00
|
|
|
def_id_cache: FxHashMap<DefId, StringId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl QueryKeyStringCache {
|
2021-01-17 07:57:34 -06:00
|
|
|
fn new() -> QueryKeyStringCache {
|
2020-01-03 04:49:14 -06:00
|
|
|
QueryKeyStringCache { def_id_cache: Default::default() }
|
2019-12-17 07:44:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 07:57:34 -06:00
|
|
|
struct QueryKeyStringBuilder<'p, 'c, 'tcx> {
|
2019-12-17 07:44:07 -06:00
|
|
|
profiler: &'p SelfProfiler,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
string_cache: &'c mut QueryKeyStringCache,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'p, 'c, 'tcx> QueryKeyStringBuilder<'p, 'c, 'tcx> {
|
2021-01-17 07:57:34 -06:00
|
|
|
fn new(
|
2019-12-17 07:44:07 -06:00
|
|
|
profiler: &'p SelfProfiler,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
string_cache: &'c mut QueryKeyStringCache,
|
|
|
|
) -> QueryKeyStringBuilder<'p, 'c, 'tcx> {
|
2020-01-03 04:49:14 -06:00
|
|
|
QueryKeyStringBuilder { profiler, tcx, string_cache }
|
2019-12-17 07:44:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The current implementation is rather crude. In the future it might be a
|
|
|
|
// good idea to base this on `ty::print` in order to get nicer and more
|
|
|
|
// efficient query keys.
|
|
|
|
fn def_id_to_string_id(&mut self, def_id: DefId) -> StringId {
|
|
|
|
if let Some(&string_id) = self.string_cache.def_id_cache.get(&def_id) {
|
|
|
|
return string_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
let def_key = self.tcx.def_key(def_id);
|
|
|
|
|
|
|
|
let (parent_string_id, start_index) = match def_key.parent {
|
|
|
|
Some(parent_index) => {
|
2020-01-03 04:49:14 -06:00
|
|
|
let parent_def_id = DefId { index: parent_index, krate: def_id.krate };
|
2019-12-17 07:44:07 -06:00
|
|
|
|
|
|
|
(self.def_id_to_string_id(parent_def_id), 0)
|
|
|
|
}
|
2020-01-03 04:49:14 -06:00
|
|
|
None => (StringId::INVALID, 2),
|
2019-12-17 07:44:07 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let dis_buffer = &mut [0u8; 16];
|
2020-09-23 17:38:38 -05:00
|
|
|
let crate_name;
|
|
|
|
let other_name;
|
2019-12-17 07:44:07 -06:00
|
|
|
let name;
|
|
|
|
let dis;
|
|
|
|
let end_index;
|
|
|
|
|
|
|
|
match def_key.disambiguated_data.data {
|
|
|
|
DefPathData::CrateRoot => {
|
2021-12-14 15:32:21 -06:00
|
|
|
crate_name = self.tcx.crate_name(def_id.krate);
|
|
|
|
name = crate_name.as_str();
|
2019-12-17 07:44:07 -06:00
|
|
|
dis = "";
|
|
|
|
end_index = 3;
|
|
|
|
}
|
|
|
|
other => {
|
2020-09-23 17:38:38 -05:00
|
|
|
other_name = other.to_string();
|
|
|
|
name = other_name.as_str();
|
2020-09-01 13:34:46 -05:00
|
|
|
if def_key.disambiguated_data.disambiguator == 0 {
|
|
|
|
dis = "";
|
|
|
|
end_index = 3;
|
|
|
|
} else {
|
|
|
|
write!(&mut dis_buffer[..], "[{}]", def_key.disambiguated_data.disambiguator)
|
2020-01-03 04:49:14 -06:00
|
|
|
.unwrap();
|
2020-09-01 13:34:46 -05:00
|
|
|
let end_of_dis = dis_buffer.iter().position(|&c| c == b']').unwrap();
|
|
|
|
dis = std::str::from_utf8(&dis_buffer[..end_of_dis + 1]).unwrap();
|
|
|
|
end_index = 4;
|
|
|
|
}
|
2019-12-17 07:44:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let components = [
|
|
|
|
StringComponent::Ref(parent_string_id),
|
|
|
|
StringComponent::Value("::"),
|
2020-07-14 01:29:44 -05:00
|
|
|
StringComponent::Value(name),
|
2020-01-03 04:49:14 -06:00
|
|
|
StringComponent::Value(dis),
|
2019-12-17 07:44:07 -06:00
|
|
|
];
|
|
|
|
|
2020-01-03 04:49:14 -06:00
|
|
|
let string_id = self.profiler.alloc_string(&components[start_index..end_index]);
|
2019-12-17 07:44:07 -06:00
|
|
|
|
|
|
|
self.string_cache.def_id_cache.insert(def_id, string_id);
|
|
|
|
|
|
|
|
string_id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 07:57:34 -06:00
|
|
|
trait IntoSelfProfilingString {
|
2020-01-03 04:49:14 -06:00
|
|
|
fn to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_, '_>) -> StringId;
|
2019-12-17 07:44:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The default implementation of `IntoSelfProfilingString` just uses `Debug`
|
|
|
|
// which is slow and causes lots of duplication of string data.
|
|
|
|
// The specialized impls below take care of making the `DefId` case more
|
|
|
|
// efficient.
|
|
|
|
impl<T: Debug> IntoSelfProfilingString for T {
|
|
|
|
default fn to_self_profile_string(
|
|
|
|
&self,
|
2020-01-03 04:49:14 -06:00
|
|
|
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
|
2019-12-17 07:44:07 -06:00
|
|
|
) -> StringId {
|
|
|
|
let s = format!("{:?}", self);
|
|
|
|
builder.profiler.alloc_string(&s[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:58:18 -05:00
|
|
|
impl<T: SpecIntoSelfProfilingString> IntoSelfProfilingString for T {
|
2020-01-03 04:49:14 -06:00
|
|
|
fn to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_, '_>) -> StringId {
|
2020-06-01 12:58:18 -05:00
|
|
|
self.spec_to_self_profile_string(builder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rustc_specialization_trait]
|
2021-01-17 07:57:34 -06:00
|
|
|
trait SpecIntoSelfProfilingString: Debug {
|
2020-06-01 12:58:18 -05:00
|
|
|
fn spec_to_self_profile_string(
|
|
|
|
&self,
|
|
|
|
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
|
|
|
|
) -> StringId;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpecIntoSelfProfilingString for DefId {
|
|
|
|
fn spec_to_self_profile_string(
|
|
|
|
&self,
|
|
|
|
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
|
|
|
|
) -> StringId {
|
2019-12-17 07:44:07 -06:00
|
|
|
builder.def_id_to_string_id(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:58:18 -05:00
|
|
|
impl SpecIntoSelfProfilingString for CrateNum {
|
|
|
|
fn spec_to_self_profile_string(
|
|
|
|
&self,
|
|
|
|
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
|
|
|
|
) -> StringId {
|
2022-04-15 12:27:53 -05:00
|
|
|
builder.def_id_to_string_id(self.as_def_id())
|
2019-12-17 07:44:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:58:18 -05:00
|
|
|
impl SpecIntoSelfProfilingString for DefIndex {
|
|
|
|
fn spec_to_self_profile_string(
|
|
|
|
&self,
|
|
|
|
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
|
|
|
|
) -> StringId {
|
2020-01-03 04:49:14 -06:00
|
|
|
builder.def_id_to_string_id(DefId { krate: LOCAL_CRATE, index: *self })
|
2019-12-17 07:44:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 08:19:16 -05:00
|
|
|
impl SpecIntoSelfProfilingString for LocalDefId {
|
|
|
|
fn spec_to_self_profile_string(
|
|
|
|
&self,
|
|
|
|
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
|
|
|
|
) -> StringId {
|
|
|
|
builder.def_id_to_string_id(DefId { krate: LOCAL_CRATE, index: self.local_def_index })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: SpecIntoSelfProfilingString> SpecIntoSelfProfilingString for WithOptConstParam<T> {
|
|
|
|
fn spec_to_self_profile_string(
|
|
|
|
&self,
|
|
|
|
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
|
|
|
|
) -> StringId {
|
|
|
|
// We print `WithOptConstParam` values as tuples to make them shorter
|
|
|
|
// and more readable, without losing information:
|
|
|
|
//
|
|
|
|
// "WithOptConstParam { did: foo::bar, const_param_did: Some(foo::baz) }"
|
|
|
|
// becomes "(foo::bar, foo::baz)" and
|
|
|
|
// "WithOptConstParam { did: foo::bar, const_param_did: None }"
|
|
|
|
// becomes "(foo::bar, _)".
|
|
|
|
|
|
|
|
let did = StringComponent::Ref(self.did.to_self_profile_string(builder));
|
|
|
|
|
|
|
|
let const_param_did = if let Some(const_param_did) = self.const_param_did {
|
|
|
|
let const_param_did = builder.def_id_to_string_id(const_param_did);
|
|
|
|
StringComponent::Ref(const_param_did)
|
|
|
|
} else {
|
|
|
|
StringComponent::Value("_")
|
|
|
|
};
|
|
|
|
|
|
|
|
let components = [
|
|
|
|
StringComponent::Value("("),
|
|
|
|
did,
|
|
|
|
StringComponent::Value(", "),
|
|
|
|
const_param_did,
|
|
|
|
StringComponent::Value(")"),
|
|
|
|
];
|
|
|
|
|
|
|
|
builder.profiler.alloc_string(&components[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:58:18 -05:00
|
|
|
impl<T0, T1> SpecIntoSelfProfilingString for (T0, T1)
|
2020-01-03 04:49:14 -06:00
|
|
|
where
|
2020-06-01 12:58:18 -05:00
|
|
|
T0: SpecIntoSelfProfilingString,
|
|
|
|
T1: SpecIntoSelfProfilingString,
|
2019-12-17 07:44:07 -06:00
|
|
|
{
|
2020-06-01 12:58:18 -05:00
|
|
|
fn spec_to_self_profile_string(
|
2019-12-17 07:44:07 -06:00
|
|
|
&self,
|
2020-01-03 04:49:14 -06:00
|
|
|
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
|
2019-12-17 07:44:07 -06:00
|
|
|
) -> StringId {
|
|
|
|
let val0 = self.0.to_self_profile_string(builder);
|
|
|
|
let val1 = self.1.to_self_profile_string(builder);
|
|
|
|
|
|
|
|
let components = &[
|
|
|
|
StringComponent::Value("("),
|
|
|
|
StringComponent::Ref(val0),
|
|
|
|
StringComponent::Value(","),
|
|
|
|
StringComponent::Ref(val1),
|
|
|
|
StringComponent::Value(")"),
|
|
|
|
];
|
|
|
|
|
|
|
|
builder.profiler.alloc_string(components)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allocate the self-profiling query strings for a single query cache. This
|
|
|
|
/// method is called from `alloc_self_profile_query_strings` which knows all
|
|
|
|
/// the queries via macro magic.
|
2021-01-17 07:57:34 -06:00
|
|
|
fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
|
2019-12-17 07:44:07 -06:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
query_name: &'static str,
|
2022-02-19 21:44:19 -06:00
|
|
|
query_cache: &C,
|
2019-12-17 07:44:07 -06:00
|
|
|
string_cache: &mut QueryKeyStringCache,
|
2020-01-03 04:49:14 -06:00
|
|
|
) where
|
2020-03-24 17:46:47 -05:00
|
|
|
C: QueryCache,
|
2020-03-07 11:36:24 -06:00
|
|
|
C::Key: Debug + Clone,
|
2020-01-03 04:49:14 -06:00
|
|
|
{
|
2019-12-17 07:44:07 -06:00
|
|
|
tcx.prof.with_profiler(|profiler| {
|
|
|
|
let event_id_builder = profiler.event_id_builder();
|
|
|
|
|
|
|
|
// Walk the entire query cache and allocate the appropriate
|
|
|
|
// string representations. Each cache entry is uniquely
|
|
|
|
// identified by its dep_node_index.
|
|
|
|
if profiler.query_key_recording_enabled() {
|
2020-01-03 04:49:14 -06:00
|
|
|
let mut query_string_builder = QueryKeyStringBuilder::new(profiler, tcx, string_cache);
|
2019-12-17 07:44:07 -06:00
|
|
|
|
|
|
|
let query_name = profiler.get_or_alloc_cached_string(query_name);
|
|
|
|
|
|
|
|
// Since building the string representation of query keys might
|
|
|
|
// need to invoke queries itself, we cannot keep the query caches
|
|
|
|
// locked while doing so. Instead we copy out the
|
|
|
|
// `(query_key, dep_node_index)` pairs and release the lock again.
|
2021-04-29 09:23:17 -05:00
|
|
|
let mut query_keys_and_indices = Vec::new();
|
2022-02-19 21:44:19 -06:00
|
|
|
query_cache.iter(&mut |k, _, i| query_keys_and_indices.push((k.clone(), i)));
|
2019-12-17 07:44:07 -06:00
|
|
|
|
|
|
|
// Now actually allocate the strings. If allocating the strings
|
|
|
|
// generates new entries in the query cache, we'll miss them but
|
|
|
|
// we don't actually care.
|
|
|
|
for (query_key, dep_node_index) in query_keys_and_indices {
|
|
|
|
// Translate the DepNodeIndex into a QueryInvocationId
|
|
|
|
let query_invocation_id = dep_node_index.into();
|
|
|
|
|
|
|
|
// Create the string version of the query-key
|
|
|
|
let query_key = query_key.to_self_profile_string(&mut query_string_builder);
|
|
|
|
let event_id = event_id_builder.from_label_and_arg(query_name, query_key);
|
|
|
|
|
|
|
|
// Doing this in bulk might be a good idea:
|
|
|
|
profiler.map_query_invocation_id_to_string(
|
|
|
|
query_invocation_id,
|
|
|
|
event_id.to_string_id(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In this branch we don't allocate query keys
|
|
|
|
let query_name = profiler.get_or_alloc_cached_string(query_name);
|
|
|
|
let event_id = event_id_builder.from_label(query_name).to_string_id();
|
|
|
|
|
2021-04-29 09:23:17 -05:00
|
|
|
let mut query_invocation_ids = Vec::new();
|
2022-02-19 21:44:19 -06:00
|
|
|
query_cache.iter(&mut |_, _, i| {
|
2021-04-29 09:23:17 -05:00
|
|
|
query_invocation_ids.push(i.into());
|
2020-02-08 00:38:00 -06:00
|
|
|
});
|
2021-04-29 09:23:17 -05:00
|
|
|
|
|
|
|
profiler.bulk_map_query_invocation_id_to_single_string(
|
|
|
|
query_invocation_ids.into_iter(),
|
|
|
|
event_id,
|
|
|
|
);
|
2019-12-17 07:44:07 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-01-17 07:57:34 -06:00
|
|
|
|
|
|
|
/// All self-profiling events generated by the query engine use
|
|
|
|
/// virtual `StringId`s for their `event_id`. This method makes all
|
|
|
|
/// those virtual `StringId`s point to actual strings.
|
|
|
|
///
|
|
|
|
/// If we are recording only summary data, the ids will point to
|
|
|
|
/// just the query names. If we are recording query keys too, we
|
|
|
|
/// allocate the corresponding strings here.
|
2021-12-14 11:13:07 -06:00
|
|
|
pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) {
|
2021-01-17 07:57:34 -06:00
|
|
|
if !tcx.prof.enabled() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut string_cache = QueryKeyStringCache::new();
|
|
|
|
|
|
|
|
macro_rules! alloc_once {
|
|
|
|
(<$tcx:tt>
|
|
|
|
$($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,)*
|
|
|
|
) => {
|
|
|
|
$({
|
|
|
|
alloc_self_profile_query_strings_for_query_cache(
|
|
|
|
tcx,
|
|
|
|
stringify!($name),
|
|
|
|
&tcx.query_caches.$name,
|
|
|
|
&mut string_cache,
|
|
|
|
);
|
|
|
|
})*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rustc_query_append! { [alloc_once!][<'tcx>] }
|
|
|
|
}
|