Skip to content

Commit 6e33dce

Browse files
committed
Profile incremental hashing
1 parent c5e344f commit 6e33dce

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

compiler/rustc_data_structures/src/profiling.rs

+45-11
Original file line numberDiff line numberDiff line change
@@ -94,31 +94,34 @@ use std::process;
9494
use std::sync::Arc;
9595
use std::time::{Duration, Instant};
9696

97-
use measureme::{EventId, EventIdBuilder, Profiler, SerializableString, StringId};
97+
pub use measureme::EventId;
98+
use measureme::{EventIdBuilder, Profiler, SerializableString, StringId};
9899
use parking_lot::RwLock;
99100

100101
bitflags::bitflags! {
101102
struct EventFilter: u32 {
102-
const GENERIC_ACTIVITIES = 1 << 0;
103-
const QUERY_PROVIDERS = 1 << 1;
104-
const QUERY_CACHE_HITS = 1 << 2;
105-
const QUERY_BLOCKED = 1 << 3;
106-
const INCR_CACHE_LOADS = 1 << 4;
103+
const GENERIC_ACTIVITIES = 1 << 0;
104+
const QUERY_PROVIDERS = 1 << 1;
105+
const QUERY_CACHE_HITS = 1 << 2;
106+
const QUERY_BLOCKED = 1 << 3;
107+
const INCR_CACHE_LOADS = 1 << 4;
107108

108-
const QUERY_KEYS = 1 << 5;
109-
const FUNCTION_ARGS = 1 << 6;
110-
const LLVM = 1 << 7;
109+
const QUERY_KEYS = 1 << 5;
110+
const FUNCTION_ARGS = 1 << 6;
111+
const LLVM = 1 << 7;
112+
const INCR_RESULT_HASHING = 1 << 8;
111113

112114
const DEFAULT = Self::GENERIC_ACTIVITIES.bits |
113115
Self::QUERY_PROVIDERS.bits |
114116
Self::QUERY_BLOCKED.bits |
115-
Self::INCR_CACHE_LOADS.bits;
117+
Self::INCR_CACHE_LOADS.bits |
118+
Self::INCR_RESULT_HASHING.bits;
116119

117120
const ARGS = Self::QUERY_KEYS.bits | Self::FUNCTION_ARGS.bits;
118121
}
119122
}
120123

121-
// keep this in sync with the `-Z self-profile-events` help message in librustc_session/options.rs
124+
// keep this in sync with the `-Z self-profile-events` help message in rustc_session/options.rs
122125
const EVENT_FILTERS_BY_NAME: &[(&str, EventFilter)] = &[
123126
("none", EventFilter::empty()),
124127
("all", EventFilter::all()),
@@ -132,6 +135,7 @@ const EVENT_FILTERS_BY_NAME: &[(&str, EventFilter)] = &[
132135
("function-args", EventFilter::FUNCTION_ARGS),
133136
("args", EventFilter::ARGS),
134137
("llvm", EventFilter::LLVM),
138+
("incr-result-hashing", EventFilter::INCR_RESULT_HASHING),
135139
];
136140

137141
/// Something that uniquely identifies a query invocation.
@@ -248,6 +252,15 @@ impl SelfProfilerRef {
248252
})
249253
}
250254

255+
/// Start profiling with some event filter for a given event. Profiling continues until the
256+
/// TimingGuard returned from this call is dropped.
257+
#[inline(always)]
258+
pub fn generic_activity_with_event(&self, event_id: EventId) -> TimingGuard<'_> {
259+
self.exec(EventFilter::GENERIC_ACTIVITIES, |profiler| {
260+
TimingGuard::start(profiler, profiler.generic_activity_event_kind, event_id)
261+
})
262+
}
263+
251264
/// Start profiling a generic activity. Profiling continues until the
252265
/// TimingGuard returned from this call is dropped.
253266
#[inline(always)]
@@ -337,6 +350,19 @@ impl SelfProfilerRef {
337350
})
338351
}
339352

353+
/// Start profiling how long it takes to hash query results for incremental compilation.
354+
/// Profiling continues until the TimingGuard returned from this call is dropped.
355+
#[inline(always)]
356+
pub fn incr_result_hashing(&self) -> TimingGuard<'_> {
357+
self.exec(EventFilter::INCR_RESULT_HASHING, |profiler| {
358+
TimingGuard::start(
359+
profiler,
360+
profiler.incremental_result_hashing_event_kind,
361+
EventId::INVALID,
362+
)
363+
})
364+
}
365+
340366
#[inline(always)]
341367
fn instant_query_event(
342368
&self,
@@ -364,6 +390,10 @@ impl SelfProfilerRef {
364390
}
365391
}
366392

393+
pub fn get_or_alloc_cached_string(&self, s: &'static str) -> Option<StringId> {
394+
self.profiler.as_ref().map(|p| p.get_or_alloc_cached_string(s))
395+
}
396+
367397
#[inline]
368398
pub fn enabled(&self) -> bool {
369399
self.profiler.is_some()
@@ -388,6 +418,7 @@ pub struct SelfProfiler {
388418
query_event_kind: StringId,
389419
generic_activity_event_kind: StringId,
390420
incremental_load_result_event_kind: StringId,
421+
incremental_result_hashing_event_kind: StringId,
391422
query_blocked_event_kind: StringId,
392423
query_cache_hit_event_kind: StringId,
393424
}
@@ -408,6 +439,8 @@ impl SelfProfiler {
408439
let query_event_kind = profiler.alloc_string("Query");
409440
let generic_activity_event_kind = profiler.alloc_string("GenericActivity");
410441
let incremental_load_result_event_kind = profiler.alloc_string("IncrementalLoadResult");
442+
let incremental_result_hashing_event_kind =
443+
profiler.alloc_string("IncrementalResultHashing");
411444
let query_blocked_event_kind = profiler.alloc_string("QueryBlocked");
412445
let query_cache_hit_event_kind = profiler.alloc_string("QueryCacheHit");
413446

@@ -451,6 +484,7 @@ impl SelfProfiler {
451484
query_event_kind,
452485
generic_activity_event_kind,
453486
incremental_load_result_event_kind,
487+
incremental_result_hashing_event_kind,
454488
query_blocked_event_kind,
455489
query_cache_hit_event_kind,
456490
})

compiler/rustc_query_system/src/dep_graph/graph.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use rustc_data_structures::fingerprint::Fingerprint;
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3-
use rustc_data_structures::profiling::QueryInvocationId;
4-
use rustc_data_structures::profiling::SelfProfilerRef;
3+
use rustc_data_structures::profiling::{EventId, QueryInvocationId, SelfProfilerRef};
54
use rustc_data_structures::sharded::{self, Sharded};
65
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
76
use rustc_data_structures::steal::Steal;
@@ -36,6 +35,11 @@ pub struct DepGraph<K: DepKind> {
3635
/// each task has a `DepNodeIndex` that uniquely identifies it. This unique
3736
/// ID is used for self-profiling.
3837
virtual_dep_node_index: Lrc<AtomicU32>,
38+
39+
/// The cached event id for profiling node interning. This saves us
40+
/// from having to look up the event id every time we intern a node
41+
/// which may incur too much overhead.
42+
node_intern_event_id: Option<EventId>,
3943
}
4044

4145
rustc_index::newtype_index! {
@@ -130,6 +134,10 @@ impl<K: DepKind> DepGraph<K> {
130134
);
131135
debug_assert_eq!(_green_node_index, DepNodeIndex::SINGLETON_DEPENDENCYLESS_ANON_NODE);
132136

137+
let node_intern_event_id = profiler
138+
.get_or_alloc_cached_string("incr_comp_intern_dep_graph_node")
139+
.map(EventId::from_label);
140+
133141
DepGraph {
134142
data: Some(Lrc::new(DepGraphData {
135143
previous_work_products: prev_work_products,
@@ -141,11 +149,16 @@ impl<K: DepKind> DepGraph<K> {
141149
colors: DepNodeColorMap::new(prev_graph_node_count),
142150
})),
143151
virtual_dep_node_index: Lrc::new(AtomicU32::new(0)),
152+
node_intern_event_id,
144153
}
145154
}
146155

147156
pub fn new_disabled() -> DepGraph<K> {
148-
DepGraph { data: None, virtual_dep_node_index: Lrc::new(AtomicU32::new(0)) }
157+
DepGraph {
158+
data: None,
159+
virtual_dep_node_index: Lrc::new(AtomicU32::new(0)),
160+
node_intern_event_id: None,
161+
}
149162
}
150163

151164
/// Returns `true` if we are actually building the full dep-graph, and `false` otherwise.
@@ -244,10 +257,15 @@ impl<K: DepKind> DepGraph<K> {
244257
let edges = task_deps.map_or_else(|| smallvec![], |lock| lock.into_inner().reads);
245258

246259
let mut hcx = dcx.create_stable_hashing_context();
260+
let hashing_timer = dcx.profiler().incr_result_hashing();
247261
let current_fingerprint = hash_result(&mut hcx, &result);
248262

249263
let print_status = cfg!(debug_assertions) && dcx.sess().opts.debugging_opts.dep_tasks;
250264

265+
// Get timer for profiling `DepNode` interning
266+
let node_intern_timer = self
267+
.node_intern_event_id
268+
.map(|eid| dcx.profiler().generic_activity_with_event(eid));
251269
// Intern the new `DepNode`.
252270
let (dep_node_index, prev_and_color) = data.current.intern_node(
253271
dcx.profiler(),
@@ -257,6 +275,9 @@ impl<K: DepKind> DepGraph<K> {
257275
current_fingerprint,
258276
print_status,
259277
);
278+
drop(node_intern_timer);
279+
280+
hashing_timer.finish_with_query_invocation_id(dep_node_index.into());
260281

261282
if let Some((prev_index, color)) = prev_and_color {
262283
debug_assert!(

compiler/rustc_session/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ options! {
12491249
"specify the events recorded by the self profiler;
12501250
for example: `-Z self-profile-events=default,query-keys`
12511251
all options: none, all, default, generic-activity, query-provider, query-cache-hit
1252-
query-blocked, incr-cache-load, query-keys, function-args, args, llvm"),
1252+
query-blocked, incr-cache-load, incr-result-hashing, query-keys, function-args, args, llvm"),
12531253
share_generics: Option<bool> = (None, parse_opt_bool, [TRACKED],
12541254
"make the current crate share its generic instantiations"),
12551255
show_span: Option<String> = (None, parse_opt_string, [TRACKED],

0 commit comments

Comments
 (0)