Skip to content

Commit

Permalink
Rollup merge of rust-lang#66335 - Mark-Simulacrum:self-profile-to-dat…
Browse files Browse the repository at this point in the history
…a, r=michaelwoerister

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 is intended as a precursor to moving Session out of librustc, but since that involves lots of smaller steps that move around code I'm splitting it up into separate PRs.
  • Loading branch information
JohnTitor committed Nov 13, 2019
2 parents f735cd2 + 2fd5454 commit fab583b
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3120,7 +3120,6 @@ dependencies = [
"graphviz",
"jobserver",
"log",
"measureme",
"num_cpus",
"parking_lot 0.9.0",
"polonius-engine",
Expand Down Expand Up @@ -3470,6 +3469,7 @@ dependencies = [
name = "rustc_data_structures"
version = "0.0.0"
dependencies = [
"bitflags",
"cfg-if",
"crossbeam-utils 0.6.5",
"ena",
Expand All @@ -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",
Expand Down
1 change: 0 additions & 1 deletion src/librustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 0 additions & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ pub mod util {
pub mod captures;
pub mod common;
pub mod nodemap;
pub mod profiling;
pub mod bug;
}

Expand Down
3 changes: 1 addition & 2 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,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;
Expand Down Expand Up @@ -1091,7 +1091,6 @@ fn build_session_(
);
match profiler {
Ok(profiler) => {
crate::ty::query::QueryName::register_with_profiler(&profiler);
Some(Arc::new(profiler))
},
Err(e) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/query/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 14 additions & 2 deletions src/librustc/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -816,8 +816,20 @@ 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);)*
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/librustc_data_structures/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -32,7 +34,7 @@ pub enum ProfileCategory {
Other,
}

bitflags! {
bitflags::bitflags! {
struct EventFilter: u32 {
const GENERIC_ACTIVITIES = 1 << 0;
const QUERY_PROVIDERS = 1 << 1;
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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
) {
Expand All @@ -203,6 +205,12 @@ impl SelfProfilerRef {
TimingGuard::none()
}));
}

pub fn register_queries(&self, f: impl FnOnce(&SelfProfiler)) {
if let Some(profiler) = &self.profiler {
f(&profiler)
}
}
}

pub struct SelfProfiler {
Expand Down Expand Up @@ -274,15 +282,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());
}
Expand Down
4 changes: 4 additions & 0 deletions src/librustc_interface/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ pub fn create_session(
process_configure_mod,
);

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));
Expand Down

0 comments on commit fab583b

Please sign in to comment.