diff --git a/Cargo.lock b/Cargo.lock index 1af0442dde7fc..51ee05a98e26a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2021,9 +2021,9 @@ dependencies = [ [[package]] name = "measureme" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c420bbc064623934620b5ab2dc0cf96451b34163329e82f95e7fa1b7b99a6ac8" +checksum = "36dcc09c1a633097649f7d48bde3d8a61d2a43c01ce75525e31fbbc82c0fccf4" dependencies = [ "byteorder", "memmap", diff --git a/src/librustc/Cargo.toml b/src/librustc/Cargo.toml index a4536bb6c41f0..e6afe723f0fc5 100644 --- a/src/librustc/Cargo.toml +++ b/src/librustc/Cargo.toml @@ -37,6 +37,6 @@ byteorder = { version = "1.3" } chalk-engine = { version = "0.9.0", default-features=false } rustc_fs_util = { path = "../librustc_fs_util" } smallvec = { version = "1.0", features = ["union", "may_dangle"] } -measureme = "0.5" +measureme = "0.6.0" rustc_error_codes = { path = "../librustc_error_codes" } rustc_session = { path = "../librustc_session" } diff --git a/src/librustc/dep_graph/graph.rs b/src/librustc/dep_graph/graph.rs index d952bf7ab9e25..9625fcb840de5 100644 --- a/src/librustc/dep_graph/graph.rs +++ b/src/librustc/dep_graph/graph.rs @@ -3,9 +3,10 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_index::vec::{Idx, IndexVec}; use smallvec::SmallVec; +use rustc_data_structures::profiling::QueryInvocationId; use rustc_data_structures::sync::{Lrc, Lock, AtomicU32, AtomicU64, Ordering}; use rustc_data_structures::sharded::{self, Sharded}; -use std::sync::atomic::Ordering::SeqCst; +use std::sync::atomic::Ordering::Relaxed; use std::env; use std::hash::Hash; use std::collections::hash_map::Entry; @@ -25,6 +26,12 @@ use super::prev::PreviousDepGraph; #[derive(Clone)] pub struct DepGraph { data: Option>, + + /// This field is used for assigning DepNodeIndices when running in + /// non-incremental mode. Even in non-incremental mode we make sure that + /// each task as a `DepNodeIndex` that uniquely identifies it. This unique + /// ID is used for self-profiling. + virtual_dep_node_index: Lrc, } rustc_index::newtype_index! { @@ -35,6 +42,13 @@ impl DepNodeIndex { pub const INVALID: DepNodeIndex = DepNodeIndex::MAX; } +impl std::convert::From for QueryInvocationId { + #[inline] + fn from(dep_node_index: DepNodeIndex) -> Self { + QueryInvocationId(dep_node_index.as_u32()) + } +} + #[derive(PartialEq)] pub enum DepNodeColor { Red, @@ -103,12 +117,14 @@ impl DepGraph { previous: prev_graph, colors: DepNodeColorMap::new(prev_graph_node_count), })), + virtual_dep_node_index: Lrc::new(AtomicU32::new(0)), } } pub fn new_disabled() -> DepGraph { DepGraph { data: None, + virtual_dep_node_index: Lrc::new(AtomicU32::new(0)), } } @@ -319,7 +335,7 @@ impl DepGraph { (result, dep_node_index) } else { - (task(cx, arg), DepNodeIndex::INVALID) + (task(cx, arg), self.next_virtual_depnode_index()) } } @@ -354,7 +370,7 @@ impl DepGraph { .complete_anon_task(dep_kind, task_deps); (result, dep_node_index) } else { - (op(), DepNodeIndex::INVALID) + (op(), self.next_virtual_depnode_index()) } } @@ -877,6 +893,11 @@ impl DepGraph { } } } + + fn next_virtual_depnode_index(&self) -> DepNodeIndex { + let index = self.virtual_dep_node_index.fetch_add(1, Relaxed); + DepNodeIndex::from_u32(index) + } } /// A "work product" is an intermediate result that we save into the diff --git a/src/librustc/ty/query/config.rs b/src/librustc/ty/query/config.rs index 7e126459dcc73..66ee09552531d 100644 --- a/src/librustc/ty/query/config.rs +++ b/src/librustc/ty/query/config.rs @@ -3,7 +3,7 @@ use crate::dep_graph::{DepKind, DepNode}; use crate::hir::def_id::{CrateNum, DefId}; use crate::ty::TyCtxt; use crate::ty::query::queries; -use crate::ty::query::{Query, QueryName}; +use crate::ty::query::{Query}; use crate::ty::query::QueryCache; use crate::ty::query::plumbing::CycleError; use rustc_data_structures::profiling::ProfileCategory; @@ -20,7 +20,7 @@ use crate::ich::StableHashingContext; // FIXME(eddyb) false positive, the lifetime parameter is used for `Key`/`Value`. #[allow(unused_lifetimes)] pub trait QueryConfig<'tcx> { - const NAME: QueryName; + const NAME: &'static str; const CATEGORY: ProfileCategory; type Key: Eq + Hash + Clone + Debug; diff --git a/src/librustc/ty/query/plumbing.rs b/src/librustc/ty/query/plumbing.rs index fc55b665c1d0e..5e385c009a01a 100644 --- a/src/librustc/ty/query/plumbing.rs +++ b/src/librustc/ty/query/plumbing.rs @@ -104,7 +104,7 @@ 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(Q::NAME); + tcx.prof.query_cache_hit(value.index.into()); let result = (value.value.clone(), value.index); #[cfg(debug_assertions)] { @@ -356,7 +356,7 @@ impl<'tcx> TyCtxt<'tcx> { #[inline(never)] pub(super) fn get_query>(self, span: Span, key: Q::Key) -> Q::Value { debug!("ty::query::get_query<{}>(key={:?}, span={:?})", - Q::NAME.as_str(), + Q::NAME, key, span); @@ -378,7 +378,7 @@ impl<'tcx> TyCtxt<'tcx> { if Q::ANON { - let prof_timer = self.prof.query_provider(Q::NAME); + let prof_timer = self.prof.query_provider(); let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| { self.start_query(job.job.clone(), diagnostics, |tcx| { @@ -388,7 +388,7 @@ impl<'tcx> TyCtxt<'tcx> { }) }); - drop(prof_timer); + prof_timer.finish_with_query_invocation_id(dep_node_index.into()); self.dep_graph.read_index(dep_node_index); @@ -445,8 +445,9 @@ impl<'tcx> TyCtxt<'tcx> { // First we try to load the result from the on-disk cache. let result = if Q::cache_on_disk(self, key.clone(), None) && self.sess.opts.debugging_opts.incremental_queries { - let _prof_timer = self.prof.incr_cache_loading(Q::NAME); + let prof_timer = self.prof.incr_cache_loading(); let result = Q::try_load_from_disk(self, prev_dep_node_index); + prof_timer.finish_with_query_invocation_id(dep_node_index.into()); // We always expect to find a cached result for things that // can be forced from `DepNode`. @@ -465,13 +466,15 @@ impl<'tcx> TyCtxt<'tcx> { } else { // We could not load a result from the on-disk cache, so // recompute. - let _prof_timer = self.prof.query_provider(Q::NAME); + let prof_timer = self.prof.query_provider(); // The dep-graph for this computation is already in-place. let result = self.dep_graph.with_ignore(|| { Q::compute(self, key) }); + prof_timer.finish_with_query_invocation_id(dep_node_index.into()); + result }; @@ -534,7 +537,7 @@ impl<'tcx> TyCtxt<'tcx> { - dep-node: {:?}", key, dep_node); - let prof_timer = self.prof.query_provider(Q::NAME); + let prof_timer = self.prof.query_provider(); let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| { self.start_query(job.job.clone(), diagnostics, |tcx| { @@ -554,7 +557,7 @@ impl<'tcx> TyCtxt<'tcx> { }) }); - drop(prof_timer); + prof_timer.finish_with_query_invocation_id(dep_node_index.into()); if unlikely!(!diagnostics.is_empty()) { if dep_node.kind != crate::dep_graph::DepKind::Null { @@ -586,17 +589,19 @@ impl<'tcx> TyCtxt<'tcx> { let dep_node = Q::to_dep_node(self, &key); - if self.dep_graph.try_mark_green_and_read(self, &dep_node).is_none() { - // A None return from `try_mark_green_and_read` means that this is either - // a new dep node or that the dep node has already been marked red. - // Either way, we can't call `dep_graph.read()` as we don't have the - // DepNodeIndex. We must invoke the query itself. The performance cost - // this introduces should be negligible as we'll immediately hit the - // in-memory cache, or another query down the line will. - - let _ = self.get_query::(DUMMY_SP, key); - } else { - self.prof.query_cache_hit(Q::NAME); + match self.dep_graph.try_mark_green_and_read(self, &dep_node) { + None => { + // A None return from `try_mark_green_and_read` means that this is either + // a new dep node or that the dep node has already been marked red. + // Either way, we can't call `dep_graph.read()` as we don't have the + // DepNodeIndex. We must invoke the query itself. The performance cost + // this introduces should be negligible as we'll immediately hit the + // in-memory cache, or another query down the line will. + let _ = self.get_query::(DUMMY_SP, key); + } + Some((_, dep_node_index)) => { + self.prof.query_cache_hit(dep_node_index.into()); + } } } @@ -713,6 +718,42 @@ macro_rules! define_queries_inner { } } + /// All self-profiling events generated by the query engine use a + /// 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. (The latter is not yet + /// implemented.) + pub fn allocate_self_profile_query_strings( + &self, + profiler: &rustc_data_structures::profiling::SelfProfiler + ) { + // Walk the entire query cache and allocate the appropriate + // string representation. Each cache entry is uniquely + // identified by its dep_node_index. + $({ + let query_name_string_id = + profiler.get_or_alloc_cached_string(stringify!($name)); + + let result_cache = self.$name.lock_shards(); + + for shard in result_cache.iter() { + let query_invocation_ids = shard + .results + .values() + .map(|v| v.index) + .map(|dep_node_index| dep_node_index.into()); + + profiler.bulk_map_query_invocation_id_to_single_string( + query_invocation_ids, + query_name_string_id + ); + } + })* + } + #[cfg(parallel_compiler)] pub fn collect_active_jobs(&self) -> Vec>> { let mut jobs = Vec::new(); @@ -830,36 +871,6 @@ macro_rules! define_queries_inner { } } - #[allow(nonstandard_style)] - #[derive(Clone, Copy)] - pub enum QueryName { - $($name),* - } - - impl rustc_data_structures::profiling::QueryName for QueryName { - fn discriminant(self) -> std::mem::Discriminant { - std::mem::discriminant(&self) - } - - fn as_str(self) -> &'static str { - QueryName::as_str(&self) - } - } - - impl QueryName { - pub fn register_with_profiler( - profiler: &rustc_data_structures::profiling::SelfProfiler, - ) { - $(profiler.register_query_name(QueryName::$name);)* - } - - pub fn as_str(&self) -> &'static str { - match self { - $(QueryName::$name => stringify!($name),)* - } - } - } - #[allow(nonstandard_style)] #[derive(Clone, Debug)] pub enum Query<$tcx> { @@ -900,12 +911,6 @@ macro_rules! define_queries_inner { $(Query::$name(key) => key.default_span(tcx),)* } } - - pub fn query_name(&self) -> QueryName { - match self { - $(Query::$name(_) => QueryName::$name,)* - } - } } impl<'a, $tcx> HashStable> for Query<$tcx> { @@ -940,7 +945,7 @@ macro_rules! define_queries_inner { type Key = $K; type Value = $V; - const NAME: QueryName = QueryName::$name; + const NAME: &'static str = stringify!($name); const CATEGORY: ProfileCategory = $category; } diff --git a/src/librustc_codegen_ssa/base.rs b/src/librustc_codegen_ssa/base.rs index f6725e66f03e6..51168d59fdb60 100644 --- a/src/librustc_codegen_ssa/base.rs +++ b/src/librustc_codegen_ssa/base.rs @@ -502,7 +502,7 @@ pub fn codegen_crate( ongoing_codegen.codegen_finished(tcx); - assert_and_save_dep_graph(tcx); + finalize_tcx(tcx); ongoing_codegen.check_for_errors(tcx.sess); @@ -647,7 +647,8 @@ pub fn codegen_crate( ongoing_codegen.check_for_errors(tcx.sess); - assert_and_save_dep_graph(tcx); + finalize_tcx(tcx); + ongoing_codegen.into_inner() } @@ -698,7 +699,7 @@ impl Drop for AbortCodegenOnDrop { } } -fn assert_and_save_dep_graph(tcx: TyCtxt<'_>) { +fn finalize_tcx(tcx: TyCtxt<'_>) { time(tcx.sess, "assert dep graph", || ::rustc_incremental::assert_dep_graph(tcx)); @@ -706,6 +707,14 @@ fn assert_and_save_dep_graph(tcx: TyCtxt<'_>) { time(tcx.sess, "serialize dep graph", || ::rustc_incremental::save_dep_graph(tcx)); + + // We assume that no queries are run past here. If there are new queries + // after this point, they'll show up as "" in self-profiling data. + tcx.prof.with_profiler(|profiler| { + let _prof_timer = + tcx.prof.generic_activity("self_profile_alloc_query_strings"); + tcx.queries.allocate_self_profile_query_strings(profiler); + }); } impl CrateInfo { diff --git a/src/librustc_data_structures/Cargo.toml b/src/librustc_data_structures/Cargo.toml index 7fa40b8a86905..9c42f3633f2c0 100644 --- a/src/librustc_data_structures/Cargo.toml +++ b/src/librustc_data_structures/Cargo.toml @@ -26,7 +26,7 @@ rustc-hash = "1.0.1" smallvec = { version = "1.0", features = ["union", "may_dangle"] } rustc_index = { path = "../librustc_index", package = "rustc_index" } bitflags = "1.2.1" -measureme = "0.5" +measureme = "0.6.0" [dependencies.parking_lot] version = "0.9" diff --git a/src/librustc_data_structures/profiling.rs b/src/librustc_data_structures/profiling.rs index f9bfe5a1652ae..38aaf25e75fd7 100644 --- a/src/librustc_data_structures/profiling.rs +++ b/src/librustc_data_structures/profiling.rs @@ -1,13 +1,98 @@ +//! # Rust Compiler Self-Profiling +//! +//! This module implements the basic framework for the compiler's self- +//! profiling support. It provides the `SelfProfiler` type which enables +//! recording "events". An event is something that starts and ends at a given +//! point in time and has an ID and a kind attached to it. This allows for +//! tracing the compiler's activity. +//! +//! Internally this module uses the custom tailored [measureme][mm] crate for +//! efficiently recording events to disk in a compact format that can be +//! post-processed and analyzed by the suite of tools in the `measureme` +//! project. The highest priority for the tracing framework is on incurring as +//! little overhead as possible. +//! +//! +//! ## Event Overview +//! +//! Events have a few properties: +//! +//! - The `event_kind` designates the broad category of an event (e.g. does it +//! correspond to the execution of a query provider or to loading something +//! from the incr. comp. on-disk cache, etc). +//! - The `event_id` designates the query invocation or function call it +//! corresponds to, possibly including the query key or function arguments. +//! - Each event stores the ID of the thread it was recorded on. +//! - The timestamp stores beginning and end of the event, or the single point +//! in time it occurred at for "instant" events. +//! +//! +//! ## Event Filtering +//! +//! Event generation can be filtered by event kind. Recording all possible +//! events generates a lot of data, much of which is not needed for most kinds +//! of analysis. So, in order to keep overhead as low as possible for a given +//! use case, the `SelfProfiler` will only record the kinds of events that +//! pass the filter specified as a command line argument to the compiler. +//! +//! +//! ## `event_id` Assignment +//! +//! As far as `measureme` is concerned, `event_id`s are just strings. However, +//! it would incur way too much overhead to generate and persist each `event_id` +//! string at the point where the event is recorded. In order to make this more +//! efficient `measureme` has two features: +//! +//! - Strings can share their content, so that re-occurring parts don't have to +//! be copied over and over again. One allocates a string in `measureme` and +//! gets back a `StringId`. This `StringId` is then used to refer to that +//! string. `measureme` strings are actually DAGs of string components so that +//! arbitrary sharing of substrings can be done efficiently. This is useful +//! because `event_id`s contain lots of redundant text like query names or +//! def-path components. +//! +//! - `StringId`s can be "virtual" which means that the client picks a numeric +//! ID according to some application-specific scheme and can later make that +//! ID be mapped to an actual string. This is used to cheaply generate +//! `event_id`s while the events actually occur, causing little timing +//! distortion, and then later map those `StringId`s, in bulk, to actual +//! `event_id` strings. This way the largest part of tracing overhead is +//! localized to one contiguous chunk of time. +//! +//! How are these `event_id`s generated in the compiler? For things that occur +//! infrequently (e.g. "generic activities"), we just allocate the string the +//! first time it is used and then keep the `StringId` in a hash table. This +//! is implemented in `SelfProfiler::get_or_alloc_cached_string()`. +//! +//! For queries it gets more interesting: First we need a unique numeric ID for +//! each query invocation (the `QueryInvocationId`). This ID is used as the +//! virtual `StringId` we use as `event_id` for a given event. This ID has to +//! be available both when the query is executed and later, together with the +//! query key, when we allocate the actual `event_id` strings in bulk. +//! +//! We could make the compiler generate and keep track of such an ID for each +//! query invocation but luckily we already have something that fits all the +//! the requirements: the query's `DepNodeIndex`. So we use the numeric value +//! of the `DepNodeIndex` as `event_id` when recording the event and then, +//! just before the query context is dropped, we walk the entire query cache +//! (which stores the `DepNodeIndex` along with the query key for each +//! invocation) and allocate the corresponding strings together with a mapping +//! for `DepNodeIndex as StringId`. +//! +//! [mm]: https://github.com/rust-lang/measureme/ + +use crate::fx::FxHashMap; + use std::error::Error; use std::fs; -use std::mem::{self, Discriminant}; use std::path::Path; use std::process; use std::sync::Arc; use std::thread::ThreadId; use std::u32; -use measureme::{StringId}; +use measureme::StringId; +use parking_lot::RwLock; /// MmapSerializatioSink is faster on macOS and Linux /// but FileSerializationSink is faster on Windows @@ -18,11 +103,6 @@ type SerializationSink = measureme::FileSerializationSink; type Profiler = measureme::Profiler; -pub trait QueryName: Sized + Copy { - fn discriminant(self) -> Discriminant; - fn as_str(self) -> &'static str; -} - #[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)] pub enum ProfileCategory { Parsing, @@ -64,9 +144,11 @@ const EVENT_FILTERS_BY_NAME: &[(&str, EventFilter)] = &[ ]; fn thread_id_to_u32(tid: ThreadId) -> u32 { - unsafe { mem::transmute::(tid) as u32 } + unsafe { std::mem::transmute::(tid) as u32 } } +/// Something that uniquely identifies a query invocation. +pub struct QueryInvocationId(pub u32); /// A reference to the SelfProfiler. It can be cloned and sent across thread /// boundaries at will. @@ -125,9 +207,9 @@ impl SelfProfilerRef { /// Start profiling a generic activity. Profiling continues until the /// TimingGuard returned from this call is dropped. #[inline(always)] - pub fn generic_activity(&self, event_id: &str) -> TimingGuard<'_> { + pub fn generic_activity(&self, event_id: &'static str) -> TimingGuard<'_> { self.exec(EventFilter::GENERIC_ACTIVITIES, |profiler| { - let event_id = profiler.profiler.alloc_string(event_id); + let event_id = profiler.get_or_alloc_cached_string(event_id); TimingGuard::start( profiler, profiler.generic_activity_event_kind, @@ -139,19 +221,18 @@ impl SelfProfilerRef { /// Start profiling a query provider. Profiling continues until the /// TimingGuard returned from this call is dropped. #[inline(always)] - pub fn query_provider(&self, query_name: impl QueryName) -> TimingGuard<'_> { + pub fn query_provider(&self) -> TimingGuard<'_> { self.exec(EventFilter::QUERY_PROVIDERS, |profiler| { - let event_id = SelfProfiler::get_query_name_string_id(query_name); - TimingGuard::start(profiler, profiler.query_event_kind, event_id) + TimingGuard::start(profiler, profiler.query_event_kind, StringId::INVALID) }) } /// Record a query in-memory cache hit. #[inline(always)] - pub fn query_cache_hit(&self, query_name: impl QueryName) { + pub fn query_cache_hit(&self, query_invocation_id: QueryInvocationId) { self.instant_query_event( |profiler| profiler.query_cache_hit_event_kind, - query_name, + query_invocation_id, EventFilter::QUERY_CACHE_HITS, ); } @@ -160,10 +241,13 @@ impl SelfProfilerRef { /// Profiling continues until the TimingGuard returned from this call is /// dropped. #[inline(always)] - pub fn query_blocked(&self, query_name: impl QueryName) -> TimingGuard<'_> { + pub fn query_blocked(&self) -> TimingGuard<'_> { self.exec(EventFilter::QUERY_BLOCKED, |profiler| { - let event_id = SelfProfiler::get_query_name_string_id(query_name); - TimingGuard::start(profiler, profiler.query_blocked_event_kind, event_id) + TimingGuard::start( + profiler, + profiler.query_blocked_event_kind, + StringId::INVALID, + ) }) } @@ -171,13 +255,12 @@ impl SelfProfilerRef { /// incremental compilation on-disk cache. Profiling continues until the /// TimingGuard returned from this call is dropped. #[inline(always)] - pub fn incr_cache_loading(&self, query_name: impl QueryName) -> TimingGuard<'_> { + pub fn incr_cache_loading(&self) -> TimingGuard<'_> { self.exec(EventFilter::INCR_CACHE_LOADS, |profiler| { - let event_id = SelfProfiler::get_query_name_string_id(query_name); TimingGuard::start( profiler, profiler.incremental_load_result_event_kind, - event_id + StringId::INVALID, ) }) } @@ -186,11 +269,11 @@ impl SelfProfilerRef { fn instant_query_event( &self, event_kind: fn(&SelfProfiler) -> StringId, - query_name: impl QueryName, + query_invocation_id: QueryInvocationId, event_filter: EventFilter, ) { drop(self.exec(event_filter, |profiler| { - let event_id = SelfProfiler::get_query_name_string_id(query_name); + let event_id = StringId::new_virtual(query_invocation_id.0); let thread_id = thread_id_to_u32(std::thread::current().id()); profiler.profiler.record_instant_event( @@ -203,7 +286,7 @@ impl SelfProfilerRef { })); } - pub fn register_queries(&self, f: impl FnOnce(&SelfProfiler)) { + pub fn with_profiler(&self, f: impl FnOnce(&SelfProfiler)) { if let Some(profiler) = &self.profiler { f(&profiler) } @@ -213,6 +296,9 @@ impl SelfProfilerRef { pub struct SelfProfiler { profiler: Profiler, event_filter_mask: EventFilter, + + string_cache: RwLock>, + query_event_kind: StringId, generic_activity_event_kind: StringId, incremental_load_result_event_kind: StringId, @@ -271,6 +357,7 @@ impl SelfProfiler { Ok(SelfProfiler { profiler, event_filter_mask, + string_cache: RwLock::new(FxHashMap::default()), query_event_kind, generic_activity_event_kind, incremental_load_result_event_kind, @@ -279,17 +366,41 @@ impl SelfProfiler { }) } - fn get_query_name_string_id(query_name: impl QueryName) -> StringId { - let discriminant = unsafe { - mem::transmute::, u64>(query_name.discriminant()) - }; + pub fn get_or_alloc_cached_string(&self, s: &'static str) -> StringId { + // Only acquire a read-lock first since we assume that the string is + // already present in the common case. + { + let string_cache = self.string_cache.read(); - StringId::reserved(discriminant as u32) + if let Some(&id) = string_cache.get(s) { + return id + } + } + + let mut string_cache = self.string_cache.write(); + // Check if the string has already been added in the small time window + // between dropping the read lock and acquiring the write lock. + *string_cache.entry(s).or_insert_with(|| self.profiler.alloc_string(s)) } - pub fn register_query_name(&self, query_name: impl QueryName) { - let id = SelfProfiler::get_query_name_string_id(query_name); - self.profiler.alloc_string_with_reserved_id(id, query_name.as_str()); + pub fn map_query_invocation_id_to_string( + &self, + from: QueryInvocationId, + to: StringId + ) { + let from = StringId::new_virtual(from.0); + self.profiler.map_virtual_to_concrete_string(from, to); + } + + pub fn bulk_map_query_invocation_id_to_single_string( + &self, + from: I, + to: StringId + ) + where I: Iterator + ExactSizeIterator + { + let from = from.map(|qid| StringId::new_virtual(qid.0)); + self.profiler.bulk_map_virtual_to_single_concrete_string(from, to); } } @@ -311,6 +422,14 @@ impl<'a> TimingGuard<'a> { TimingGuard(Some(timing_guard)) } + #[inline] + pub fn finish_with_query_invocation_id(self, query_invocation_id: QueryInvocationId) { + if let Some(guard) = self.0 { + let event_id = StringId::new_virtual(query_invocation_id.0); + guard.finish_with_override_event_id(event_id); + } + } + #[inline] pub fn none() -> TimingGuard<'a> { TimingGuard(None) diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index c78b3ee07676e..200344006ddeb 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -80,10 +80,6 @@ pub fn create_session( lint_caps, ); - sess.prof.register_queries(|profiler| { - rustc::ty::query::QueryName::register_with_profiler(&profiler); - }); - let codegen_backend = get_codegen_backend(&sess); let mut cfg = config::build_configuration(&sess, config::to_crate_config(cfg));