Move self-profile infrastructure to data structures

The single dependency on queries (QueryName) can be fairly easily
abstracted via a trait and this further decouples Session from librustc
(the primary goal).
This commit is contained in:
Mark Rousskov 2019-11-11 17:15:36 -05:00
parent 5dda3ee931
commit f696b21c5f
12 changed files with 35 additions and 21 deletions

View file

@ -3120,7 +3120,6 @@ dependencies = [
"graphviz",
"jobserver",
"log",
"measureme",
"num_cpus",
"parking_lot 0.9.0",
"polonius-engine",
@ -3470,6 +3469,7 @@ dependencies = [
name = "rustc_data_structures"
version = "0.0.0"
dependencies = [
"bitflags",
"cfg-if",
"crossbeam-utils 0.6.5",
"ena",
@ -3478,6 +3478,7 @@ dependencies = [
"jobserver",
"lazy_static 1.3.0",
"log",
"measureme",
"parking_lot 0.9.0",
"rustc-hash",
"rustc-rayon 0.3.0",

View file

@ -40,4 +40,3 @@ 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.4"

View file

@ -126,7 +126,6 @@ pub mod util {
pub mod captures;
pub mod common;
pub mod nodemap;
pub mod profiling;
pub mod bug;
}

View file

@ -29,11 +29,11 @@ use syntax::source_map;
use syntax::sess::{ParseSess, ProcessCfgMod};
use syntax::symbol::Symbol;
use syntax_pos::{MultiSpan, Span};
use crate::util::profiling::{SelfProfiler, SelfProfilerRef};
use rustc_target::spec::{PanicStrategy, RelroLevel, Target, TargetTriple};
use rustc_data_structures::flock;
use rustc_data_structures::jobserver;
use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
use ::jobserver::Client;
use std;

View file

@ -46,11 +46,11 @@ use crate::ty::CanonicalPolyFnSig;
use crate::util::common::ErrorReported;
use crate::util::nodemap::{DefIdMap, DefIdSet, ItemLocalMap, ItemLocalSet, NodeMap};
use crate::util::nodemap::{FxHashMap, FxHashSet};
use crate::util::profiling::SelfProfilerRef;
use errors::DiagnosticBuilder;
use arena::SyncDroplessArena;
use smallvec::SmallVec;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::stable_hasher::{
HashStable, StableHasher, StableVec, hash_stable_hashmap,
};

View file

@ -6,7 +6,7 @@ use crate::ty::query::queries;
use crate::ty::query::{Query, QueryName};
use crate::ty::query::QueryCache;
use crate::ty::query::plumbing::CycleError;
use crate::util::profiling::ProfileCategory;
use rustc_data_structures::profiling::ProfileCategory;
use std::borrow::Cow;
use std::hash::Hash;

View file

@ -39,7 +39,7 @@ use crate::ty::util::NeedsDrop;
use crate::ty::subst::SubstsRef;
use crate::util::nodemap::{DefIdSet, DefIdMap};
use crate::util::common::ErrorReported;
use crate::util::profiling::ProfileCategory::*;
use rustc_data_structures::profiling::ProfileCategory::*;
use rustc_data_structures::svh::Svh;
use rustc_index::vec::IndexVec;

View file

@ -672,7 +672,7 @@ macro_rules! define_queries_inner {
rustc_data_structures::stable_hasher::StableHasher,
ich::StableHashingContext
};
use crate::util::profiling::ProfileCategory;
use rustc_data_structures::profiling::ProfileCategory;
define_queries_struct! {
tcx: $tcx,
@ -816,8 +816,18 @@ macro_rules! define_queries_inner {
$($name),*
}
impl rustc_data_structures::profiling::QueryName for QueryName {
fn discriminant(self) -> std::mem::Discriminant<QueryName> {
std::mem::discriminant(&self)
}
fn as_str(self) -> &'static str {
QueryName::as_str(&self)
}
}
impl QueryName {
pub fn register_with_profiler(profiler: &crate::util::profiling::SelfProfiler) {
pub fn register_with_profiler(profiler: &rustc_data_structures::profiling::SelfProfiler) {
$(profiler.register_query_name(QueryName::$name);)*
}

View file

@ -19,7 +19,7 @@ use rustc::util::nodemap::FxHashMap;
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc::ty::TyCtxt;
use rustc::util::common::{time_depth, set_time_depth, print_time_passes_entry};
use rustc::util::profiling::SelfProfilerRef;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_fs_util::link_or_copy;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::Lrc;

View file

@ -25,6 +25,8 @@ rayon-core = { version = "0.3.0", package = "rustc-rayon-core" }
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.4"
[dependencies.parking_lot]
version = "0.9"

View file

@ -94,6 +94,7 @@ pub use ena::unify;
pub mod vec_linked_list;
pub mod work_queue;
pub mod fingerprint;
pub mod profiling;
pub struct OnDrop<F: Fn()>(pub F);

View file

@ -7,8 +7,6 @@ use std::sync::Arc;
use std::thread::ThreadId;
use std::u32;
use crate::ty::query::QueryName;
use measureme::{StringId, TimestampKind};
/// MmapSerializatioSink is faster on macOS and Linux
@ -20,6 +18,10 @@ type SerializationSink = measureme::FileSerializationSink;
type Profiler = measureme::Profiler<SerializationSink>;
pub trait QueryName: Sized + Copy {
fn discriminant(self) -> Discriminant<Self>;
fn as_str(self) -> &'static str;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub enum ProfileCategory {
@ -32,7 +34,7 @@ pub enum ProfileCategory {
Other,
}
bitflags! {
bitflags::bitflags! {
struct EventFilter: u32 {
const GENERIC_ACTIVITIES = 1 << 0;
const QUERY_PROVIDERS = 1 << 1;
@ -137,7 +139,7 @@ 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: QueryName) -> TimingGuard<'_> {
pub fn query_provider(&self, query_name: impl QueryName) -> 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)
@ -146,7 +148,7 @@ impl SelfProfilerRef {
/// Record a query in-memory cache hit.
#[inline(always)]
pub fn query_cache_hit(&self, query_name: QueryName) {
pub fn query_cache_hit(&self, query_name: impl QueryName) {
self.non_guard_query_event(
|profiler| profiler.query_cache_hit_event_kind,
query_name,
@ -159,7 +161,7 @@ impl SelfProfilerRef {
/// Profiling continues until the TimingGuard returned from this call is
/// dropped.
#[inline(always)]
pub fn query_blocked(&self, query_name: QueryName) -> TimingGuard<'_> {
pub fn query_blocked(&self, query_name: impl QueryName) -> 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)
@ -170,7 +172,7 @@ 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: QueryName) -> TimingGuard<'_> {
pub fn incr_cache_loading(&self, query_name: impl QueryName) -> TimingGuard<'_> {
self.exec(EventFilter::INCR_CACHE_LOADS, |profiler| {
let event_id = SelfProfiler::get_query_name_string_id(query_name);
TimingGuard::start(
@ -185,7 +187,7 @@ impl SelfProfilerRef {
fn non_guard_query_event(
&self,
event_kind: fn(&SelfProfiler) -> StringId,
query_name: QueryName,
query_name: impl QueryName,
event_filter: EventFilter,
timestamp_kind: TimestampKind
) {
@ -274,15 +276,15 @@ impl SelfProfiler {
})
}
fn get_query_name_string_id(query_name: QueryName) -> StringId {
fn get_query_name_string_id(query_name: impl QueryName) -> StringId {
let discriminant = unsafe {
mem::transmute::<Discriminant<QueryName>, u64>(mem::discriminant(&query_name))
mem::transmute::<Discriminant<_>, u64>(query_name.discriminant())
};
StringId::reserved(discriminant as u32)
}
pub fn register_query_name(&self, query_name: QueryName) {
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());
}