Skip to content

Commit f696b21

Browse files
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).
1 parent 5dda3ee commit f696b21

File tree

12 files changed

+35
-21
lines changed

12 files changed

+35
-21
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3120,7 +3120,6 @@ dependencies = [
31203120
"graphviz",
31213121
"jobserver",
31223122
"log",
3123-
"measureme",
31243123
"num_cpus",
31253124
"parking_lot 0.9.0",
31263125
"polonius-engine",
@@ -3470,6 +3469,7 @@ dependencies = [
34703469
name = "rustc_data_structures"
34713470
version = "0.0.0"
34723471
dependencies = [
3472+
"bitflags",
34733473
"cfg-if",
34743474
"crossbeam-utils 0.6.5",
34753475
"ena",
@@ -3478,6 +3478,7 @@ dependencies = [
34783478
"jobserver",
34793479
"lazy_static 1.3.0",
34803480
"log",
3481+
"measureme",
34813482
"parking_lot 0.9.0",
34823483
"rustc-hash",
34833484
"rustc-rayon 0.3.0",

src/librustc/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,3 @@ byteorder = { version = "1.3" }
4040
chalk-engine = { version = "0.9.0", default-features=false }
4141
rustc_fs_util = { path = "../librustc_fs_util" }
4242
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
43-
measureme = "0.4"

src/librustc/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ pub mod util {
126126
pub mod captures;
127127
pub mod common;
128128
pub mod nodemap;
129-
pub mod profiling;
130129
pub mod bug;
131130
}
132131

src/librustc/session/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ use syntax::source_map;
2929
use syntax::sess::{ParseSess, ProcessCfgMod};
3030
use syntax::symbol::Symbol;
3131
use syntax_pos::{MultiSpan, Span};
32-
use crate::util::profiling::{SelfProfiler, SelfProfilerRef};
3332

3433
use rustc_target::spec::{PanicStrategy, RelroLevel, Target, TargetTriple};
3534
use rustc_data_structures::flock;
3635
use rustc_data_structures::jobserver;
36+
use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
3737
use ::jobserver::Client;
3838

3939
use std;

src/librustc/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ use crate::ty::CanonicalPolyFnSig;
4646
use crate::util::common::ErrorReported;
4747
use crate::util::nodemap::{DefIdMap, DefIdSet, ItemLocalMap, ItemLocalSet, NodeMap};
4848
use crate::util::nodemap::{FxHashMap, FxHashSet};
49-
use crate::util::profiling::SelfProfilerRef;
5049

5150
use errors::DiagnosticBuilder;
5251
use arena::SyncDroplessArena;
5352
use smallvec::SmallVec;
53+
use rustc_data_structures::profiling::SelfProfilerRef;
5454
use rustc_data_structures::stable_hasher::{
5555
HashStable, StableHasher, StableVec, hash_stable_hashmap,
5656
};

src/librustc/ty/query/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::ty::query::queries;
66
use crate::ty::query::{Query, QueryName};
77
use crate::ty::query::QueryCache;
88
use crate::ty::query::plumbing::CycleError;
9-
use crate::util::profiling::ProfileCategory;
9+
use rustc_data_structures::profiling::ProfileCategory;
1010

1111
use std::borrow::Cow;
1212
use std::hash::Hash;

src/librustc/ty/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::ty::util::NeedsDrop;
3939
use crate::ty::subst::SubstsRef;
4040
use crate::util::nodemap::{DefIdSet, DefIdMap};
4141
use crate::util::common::ErrorReported;
42-
use crate::util::profiling::ProfileCategory::*;
42+
use rustc_data_structures::profiling::ProfileCategory::*;
4343

4444
use rustc_data_structures::svh::Svh;
4545
use rustc_index::vec::IndexVec;

src/librustc/ty/query/plumbing.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ macro_rules! define_queries_inner {
672672
rustc_data_structures::stable_hasher::StableHasher,
673673
ich::StableHashingContext
674674
};
675-
use crate::util::profiling::ProfileCategory;
675+
use rustc_data_structures::profiling::ProfileCategory;
676676

677677
define_queries_struct! {
678678
tcx: $tcx,
@@ -816,8 +816,18 @@ macro_rules! define_queries_inner {
816816
$($name),*
817817
}
818818

819+
impl rustc_data_structures::profiling::QueryName for QueryName {
820+
fn discriminant(self) -> std::mem::Discriminant<QueryName> {
821+
std::mem::discriminant(&self)
822+
}
823+
824+
fn as_str(self) -> &'static str {
825+
QueryName::as_str(&self)
826+
}
827+
}
828+
819829
impl QueryName {
820-
pub fn register_with_profiler(profiler: &crate::util::profiling::SelfProfiler) {
830+
pub fn register_with_profiler(profiler: &rustc_data_structures::profiling::SelfProfiler) {
821831
$(profiler.register_query_name(QueryName::$name);)*
822832
}
823833

src/librustc_codegen_ssa/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc::util::nodemap::FxHashMap;
1919
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
2020
use rustc::ty::TyCtxt;
2121
use rustc::util::common::{time_depth, set_time_depth, print_time_passes_entry};
22-
use rustc::util::profiling::SelfProfilerRef;
22+
use rustc_data_structures::profiling::SelfProfilerRef;
2323
use rustc_fs_util::link_or_copy;
2424
use rustc_data_structures::svh::Svh;
2525
use rustc_data_structures::sync::Lrc;

src/librustc_data_structures/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ rayon-core = { version = "0.3.0", package = "rustc-rayon-core" }
2525
rustc-hash = "1.0.1"
2626
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
2727
rustc_index = { path = "../librustc_index", package = "rustc_index" }
28+
bitflags = "1.2.1"
29+
measureme = "0.4"
2830

2931
[dependencies.parking_lot]
3032
version = "0.9"

src/librustc_data_structures/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub use ena::unify;
9494
pub mod vec_linked_list;
9595
pub mod work_queue;
9696
pub mod fingerprint;
97+
pub mod profiling;
9798

9899
pub struct OnDrop<F: Fn()>(pub F);
99100

src/librustc/util/profiling.rs renamed to src/librustc_data_structures/profiling.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use std::sync::Arc;
77
use std::thread::ThreadId;
88
use std::u32;
99

10-
use crate::ty::query::QueryName;
11-
1210
use measureme::{StringId, TimestampKind};
1311

1412
/// MmapSerializatioSink is faster on macOS and Linux
@@ -20,6 +18,10 @@ type SerializationSink = measureme::FileSerializationSink;
2018

2119
type Profiler = measureme::Profiler<SerializationSink>;
2220

21+
pub trait QueryName: Sized + Copy {
22+
fn discriminant(self) -> Discriminant<Self>;
23+
fn as_str(self) -> &'static str;
24+
}
2325

2426
#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)]
2527
pub enum ProfileCategory {
@@ -32,7 +34,7 @@ pub enum ProfileCategory {
3234
Other,
3335
}
3436

35-
bitflags! {
37+
bitflags::bitflags! {
3638
struct EventFilter: u32 {
3739
const GENERIC_ACTIVITIES = 1 << 0;
3840
const QUERY_PROVIDERS = 1 << 1;
@@ -137,7 +139,7 @@ impl SelfProfilerRef {
137139
/// Start profiling a query provider. Profiling continues until the
138140
/// TimingGuard returned from this call is dropped.
139141
#[inline(always)]
140-
pub fn query_provider(&self, query_name: QueryName) -> TimingGuard<'_> {
142+
pub fn query_provider(&self, query_name: impl QueryName) -> TimingGuard<'_> {
141143
self.exec(EventFilter::QUERY_PROVIDERS, |profiler| {
142144
let event_id = SelfProfiler::get_query_name_string_id(query_name);
143145
TimingGuard::start(profiler, profiler.query_event_kind, event_id)
@@ -146,7 +148,7 @@ impl SelfProfilerRef {
146148

147149
/// Record a query in-memory cache hit.
148150
#[inline(always)]
149-
pub fn query_cache_hit(&self, query_name: QueryName) {
151+
pub fn query_cache_hit(&self, query_name: impl QueryName) {
150152
self.non_guard_query_event(
151153
|profiler| profiler.query_cache_hit_event_kind,
152154
query_name,
@@ -159,7 +161,7 @@ impl SelfProfilerRef {
159161
/// Profiling continues until the TimingGuard returned from this call is
160162
/// dropped.
161163
#[inline(always)]
162-
pub fn query_blocked(&self, query_name: QueryName) -> TimingGuard<'_> {
164+
pub fn query_blocked(&self, query_name: impl QueryName) -> TimingGuard<'_> {
163165
self.exec(EventFilter::QUERY_BLOCKED, |profiler| {
164166
let event_id = SelfProfiler::get_query_name_string_id(query_name);
165167
TimingGuard::start(profiler, profiler.query_blocked_event_kind, event_id)
@@ -170,7 +172,7 @@ impl SelfProfilerRef {
170172
/// incremental compilation on-disk cache. Profiling continues until the
171173
/// TimingGuard returned from this call is dropped.
172174
#[inline(always)]
173-
pub fn incr_cache_loading(&self, query_name: QueryName) -> TimingGuard<'_> {
175+
pub fn incr_cache_loading(&self, query_name: impl QueryName) -> TimingGuard<'_> {
174176
self.exec(EventFilter::INCR_CACHE_LOADS, |profiler| {
175177
let event_id = SelfProfiler::get_query_name_string_id(query_name);
176178
TimingGuard::start(
@@ -185,7 +187,7 @@ impl SelfProfilerRef {
185187
fn non_guard_query_event(
186188
&self,
187189
event_kind: fn(&SelfProfiler) -> StringId,
188-
query_name: QueryName,
190+
query_name: impl QueryName,
189191
event_filter: EventFilter,
190192
timestamp_kind: TimestampKind
191193
) {
@@ -274,15 +276,15 @@ impl SelfProfiler {
274276
})
275277
}
276278

277-
fn get_query_name_string_id(query_name: QueryName) -> StringId {
279+
fn get_query_name_string_id(query_name: impl QueryName) -> StringId {
278280
let discriminant = unsafe {
279-
mem::transmute::<Discriminant<QueryName>, u64>(mem::discriminant(&query_name))
281+
mem::transmute::<Discriminant<_>, u64>(query_name.discriminant())
280282
};
281283

282284
StringId::reserved(discriminant as u32)
283285
}
284286

285-
pub fn register_query_name(&self, query_name: QueryName) {
287+
pub fn register_query_name(&self, query_name: impl QueryName) {
286288
let id = SelfProfiler::get_query_name_string_id(query_name);
287289
self.profiler.alloc_string_with_reserved_id(id, query_name.as_str());
288290
}

0 commit comments

Comments
 (0)