Skip to content

Commit a62c040

Browse files
self-profile: Switch to new approach for event_id generation that enables query-invocation-specific event_ids.
1 parent 2d8d559 commit a62c040

File tree

9 files changed

+277
-108
lines changed

9 files changed

+277
-108
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,9 +1995,9 @@ dependencies = [
19951995

19961996
[[package]]
19971997
name = "measureme"
1998-
version = "0.5.0"
1998+
version = "0.6.0"
19991999
source = "registry+https://github.com/rust-lang/crates.io-index"
2000-
checksum = "c420bbc064623934620b5ab2dc0cf96451b34163329e82f95e7fa1b7b99a6ac8"
2000+
checksum = "36dcc09c1a633097649f7d48bde3d8a61d2a43c01ce75525e31fbbc82c0fccf4"
20012001
dependencies = [
20022002
"byteorder",
20032003
"memmap",

src/librustc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ parking_lot = "0.9"
3636
byteorder = { version = "1.3" }
3737
chalk-engine = { version = "0.9.0", default-features=false }
3838
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
39+
measureme = "0.6.0"
3940
rustc_error_codes = { path = "../librustc_error_codes" }
4041
rustc_session = { path = "../librustc_session" }

src/librustc/dep_graph/graph.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use rustc_data_structures::sync::{AtomicU32, AtomicU64, Lock, Lrc, Ordering};
88
use rustc_index::vec::{Idx, IndexVec};
99
use smallvec::SmallVec;
1010
use std::collections::hash_map::Entry;
11+
use rustc_data_structures::profiling::QueryInvocationId;
12+
use std::sync::atomic::Ordering::Relaxed;
1113
use std::env;
1214
use std::hash::Hash;
1315
use std::mem;
@@ -25,6 +27,12 @@ use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
2527
#[derive(Clone)]
2628
pub struct DepGraph {
2729
data: Option<Lrc<DepGraphData>>,
30+
31+
/// This field is used for assigning DepNodeIndices when running in
32+
/// non-incremental mode. Even in non-incremental mode we make sure that
33+
/// each task as a `DepNodeIndex` that uniquely identifies it. This unique
34+
/// ID is used for self-profiling.
35+
virtual_dep_node_index: Lrc<AtomicU32>,
2836
}
2937

3038
rustc_index::newtype_index! {
@@ -35,6 +43,13 @@ impl DepNodeIndex {
3543
pub const INVALID: DepNodeIndex = DepNodeIndex::MAX;
3644
}
3745

46+
impl std::convert::From<DepNodeIndex> for QueryInvocationId {
47+
#[inline]
48+
fn from(dep_node_index: DepNodeIndex) -> Self {
49+
QueryInvocationId(dep_node_index.as_u32())
50+
}
51+
}
52+
3853
#[derive(PartialEq)]
3954
pub enum DepNodeColor {
4055
Red,
@@ -105,11 +120,15 @@ impl DepGraph {
105120
previous: prev_graph,
106121
colors: DepNodeColorMap::new(prev_graph_node_count),
107122
})),
123+
virtual_dep_node_index: Lrc::new(AtomicU32::new(0)),
108124
}
109125
}
110126

111127
pub fn new_disabled() -> DepGraph {
112-
DepGraph { data: None }
128+
DepGraph {
129+
data: None,
130+
virtual_dep_node_index: Lrc::new(AtomicU32::new(0)),
131+
}
113132
}
114133

115134
/// Returns `true` if we are actually building the full dep-graph, and `false` otherwise.
@@ -322,7 +341,7 @@ impl DepGraph {
322341

323342
(result, dep_node_index)
324343
} else {
325-
(task(cx, arg), DepNodeIndex::INVALID)
344+
(task(cx, arg), self.next_virtual_depnode_index())
326345
}
327346
}
328347

@@ -352,7 +371,7 @@ impl DepGraph {
352371
let dep_node_index = data.current.complete_anon_task(dep_kind, task_deps);
353372
(result, dep_node_index)
354373
} else {
355-
(op(), DepNodeIndex::INVALID)
374+
(op(), self.next_virtual_depnode_index())
356375
}
357376
}
358377

@@ -877,6 +896,11 @@ impl DepGraph {
877896
}
878897
}
879898
}
899+
900+
fn next_virtual_depnode_index(&self) -> DepNodeIndex {
901+
let index = self.virtual_dep_node_index.fetch_add(1, Relaxed);
902+
DepNodeIndex::from_u32(index)
903+
}
880904
}
881905

882906
/// A "work product" is an intermediate result that we save into the

src/librustc/ty/query/config.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use crate::dep_graph::SerializedDepNodeIndex;
22
use crate::dep_graph::{DepKind, DepNode};
33
use crate::ty::query::plumbing::CycleError;
44
use crate::ty::query::queries;
5-
use crate::ty::query::QueryCache;
6-
use crate::ty::query::{Query, QueryName};
5+
use crate::ty::query::{Query, QueryCache};
76
use crate::ty::TyCtxt;
87
use rustc_data_structures::profiling::ProfileCategory;
98
use rustc_hir::def_id::{CrateNum, DefId};
@@ -20,7 +19,7 @@ use std::hash::Hash;
2019
// FIXME(eddyb) false positive, the lifetime parameter is used for `Key`/`Value`.
2120
#[allow(unused_lifetimes)]
2221
pub trait QueryConfig<'tcx> {
23-
const NAME: QueryName;
22+
const NAME: &'static str;
2423
const CATEGORY: ProfileCategory;
2524

2625
type Key: Eq + Hash + Clone + Debug;

src/librustc/ty/query/plumbing.rs

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
9595
if let Some((_, value)) =
9696
lock.results.raw_entry().from_key_hashed_nocheck(key_hash, key)
9797
{
98-
tcx.prof.query_cache_hit(Q::NAME);
98+
tcx.prof.query_cache_hit(value.index.into());
9999
let result = (value.value.clone(), value.index);
100100
#[cfg(debug_assertions)]
101101
{
@@ -347,7 +347,7 @@ impl<'tcx> TyCtxt<'tcx> {
347347

348348
#[inline(never)]
349349
pub(super) fn get_query<Q: QueryDescription<'tcx>>(self, span: Span, key: Q::Key) -> Q::Value {
350-
debug!("ty::query::get_query<{}>(key={:?}, span={:?})", Q::NAME.as_str(), key, span);
350+
debug!("ty::query::get_query<{}>(key={:?}, span={:?})", Q::NAME, key, span);
351351

352352
let job = match JobOwner::try_get(self, span, &key) {
353353
TryGetJob::NotYetStarted(job) => job,
@@ -366,15 +366,15 @@ impl<'tcx> TyCtxt<'tcx> {
366366
}
367367

368368
if Q::ANON {
369-
let prof_timer = self.prof.query_provider(Q::NAME);
369+
let prof_timer = self.prof.query_provider();
370370

371371
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
372372
self.start_query(job.job.clone(), diagnostics, |tcx| {
373373
tcx.dep_graph.with_anon_task(Q::dep_kind(), || Q::compute(tcx, key))
374374
})
375375
});
376376

377-
drop(prof_timer);
377+
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
378378

379379
self.dep_graph.read_index(dep_node_index);
380380

@@ -436,8 +436,9 @@ impl<'tcx> TyCtxt<'tcx> {
436436
let result = if Q::cache_on_disk(self, key.clone(), None)
437437
&& self.sess.opts.debugging_opts.incremental_queries
438438
{
439-
let _prof_timer = self.prof.incr_cache_loading(Q::NAME);
439+
let prof_timer = self.prof.incr_cache_loading();
440440
let result = Q::try_load_from_disk(self, prev_dep_node_index);
441+
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
441442

442443
// We always expect to find a cached result for things that
443444
// can be forced from `DepNode`.
@@ -457,11 +458,13 @@ impl<'tcx> TyCtxt<'tcx> {
457458
} else {
458459
// We could not load a result from the on-disk cache, so
459460
// recompute.
460-
let _prof_timer = self.prof.query_provider(Q::NAME);
461+
let prof_timer = self.prof.query_provider();
461462

462463
// The dep-graph for this computation is already in-place.
463464
let result = self.dep_graph.with_ignore(|| Q::compute(self, key));
464465

466+
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
467+
465468
result
466469
};
467470

@@ -523,7 +526,7 @@ impl<'tcx> TyCtxt<'tcx> {
523526
dep_node
524527
);
525528

526-
let prof_timer = self.prof.query_provider(Q::NAME);
529+
let prof_timer = self.prof.query_provider();
527530

528531
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
529532
self.start_query(job.job.clone(), diagnostics, |tcx| {
@@ -541,7 +544,7 @@ impl<'tcx> TyCtxt<'tcx> {
541544
})
542545
});
543546

544-
drop(prof_timer);
547+
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
545548

546549
if unlikely!(!diagnostics.is_empty()) {
547550
if dep_node.kind != crate::dep_graph::DepKind::Null {
@@ -572,17 +575,19 @@ impl<'tcx> TyCtxt<'tcx> {
572575

573576
let dep_node = Q::to_dep_node(self, &key);
574577

575-
if self.dep_graph.try_mark_green_and_read(self, &dep_node).is_none() {
576-
// A None return from `try_mark_green_and_read` means that this is either
577-
// a new dep node or that the dep node has already been marked red.
578-
// Either way, we can't call `dep_graph.read()` as we don't have the
579-
// DepNodeIndex. We must invoke the query itself. The performance cost
580-
// this introduces should be negligible as we'll immediately hit the
581-
// in-memory cache, or another query down the line will.
582-
583-
let _ = self.get_query::<Q>(DUMMY_SP, key);
584-
} else {
585-
self.prof.query_cache_hit(Q::NAME);
578+
match self.dep_graph.try_mark_green_and_read(self, &dep_node) {
579+
None => {
580+
// A None return from `try_mark_green_and_read` means that this is either
581+
// a new dep node or that the dep node has already been marked red.
582+
// Either way, we can't call `dep_graph.read()` as we don't have the
583+
// DepNodeIndex. We must invoke the query itself. The performance cost
584+
// this introduces should be negligible as we'll immediately hit the
585+
// in-memory cache, or another query down the line will.
586+
let _ = self.get_query::<Q>(DUMMY_SP, key);
587+
}
588+
Some((_, dep_node_index)) => {
589+
self.prof.query_cache_hit(dep_node_index.into());
590+
}
586591
}
587592
}
588593

@@ -696,6 +701,42 @@ macro_rules! define_queries_inner {
696701
}
697702
}
698703

704+
/// All self-profiling events generated by the query engine use a
705+
/// virtual `StringId`s for their `event_id`. This method makes all
706+
/// those virtual `StringId`s point to actual strings.
707+
///
708+
/// If we are recording only summary data, the ids will point to
709+
/// just the query names. If we are recording query keys too, we
710+
/// allocate the corresponding strings here. (The latter is not yet
711+
/// implemented.)
712+
pub fn allocate_self_profile_query_strings(
713+
&self,
714+
profiler: &rustc_data_structures::profiling::SelfProfiler
715+
) {
716+
// Walk the entire query cache and allocate the appropriate
717+
// string representation. Each cache entry is uniquely
718+
// identified by its dep_node_index.
719+
$({
720+
let query_name_string_id =
721+
profiler.get_or_alloc_cached_string(stringify!($name));
722+
723+
let result_cache = self.$name.lock_shards();
724+
725+
for shard in result_cache.iter() {
726+
let query_invocation_ids = shard
727+
.results
728+
.values()
729+
.map(|v| v.index)
730+
.map(|dep_node_index| dep_node_index.into());
731+
732+
profiler.bulk_map_query_invocation_id_to_single_string(
733+
query_invocation_ids,
734+
query_name_string_id
735+
);
736+
}
737+
})*
738+
}
739+
699740
#[cfg(parallel_compiler)]
700741
pub fn collect_active_jobs(&self) -> Vec<Lrc<QueryJob<$tcx>>> {
701742
let mut jobs = Vec::new();
@@ -813,36 +854,6 @@ macro_rules! define_queries_inner {
813854
}
814855
}
815856

816-
#[allow(nonstandard_style)]
817-
#[derive(Clone, Copy)]
818-
pub enum QueryName {
819-
$($name),*
820-
}
821-
822-
impl rustc_data_structures::profiling::QueryName for QueryName {
823-
fn discriminant(self) -> std::mem::Discriminant<QueryName> {
824-
std::mem::discriminant(&self)
825-
}
826-
827-
fn as_str(self) -> &'static str {
828-
QueryName::as_str(&self)
829-
}
830-
}
831-
832-
impl QueryName {
833-
pub fn register_with_profiler(
834-
profiler: &rustc_data_structures::profiling::SelfProfiler,
835-
) {
836-
$(profiler.register_query_name(QueryName::$name);)*
837-
}
838-
839-
pub fn as_str(&self) -> &'static str {
840-
match self {
841-
$(QueryName::$name => stringify!($name),)*
842-
}
843-
}
844-
}
845-
846857
#[allow(nonstandard_style)]
847858
#[derive(Clone, Debug)]
848859
pub enum Query<$tcx> {
@@ -883,12 +894,6 @@ macro_rules! define_queries_inner {
883894
$(Query::$name(key) => key.default_span(tcx),)*
884895
}
885896
}
886-
887-
pub fn query_name(&self) -> QueryName {
888-
match self {
889-
$(Query::$name(_) => QueryName::$name,)*
890-
}
891-
}
892897
}
893898

894899
impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
@@ -923,7 +928,7 @@ macro_rules! define_queries_inner {
923928
type Key = $K;
924929
type Value = $V;
925930

926-
const NAME: QueryName = QueryName::$name;
931+
const NAME: &'static str = stringify!($name);
927932
const CATEGORY: ProfileCategory = $category;
928933
}
929934

0 commit comments

Comments
 (0)