Skip to content

Commit f913a4f

Browse files
committed
Auto merge of #86619 - rylev:incr-hashing-profiling, r=wesleywiser
Profile incremental compilation hashing fingerprints Adds profiling instrumentation for the hashing of incremental compilation fingerprints per query. This will eventually feed into the `measureme` and `rustc-perf` infrastructure for tracking if computing hashes changes over time. TODOs: * [x] Address the FIXME where we are including node interning in the hash timing. * [ ] Update measureme/summarize to handle this new data: rust-lang/measureme#166 * [ ] ~Update rustc-perf to handle the new data from measureme~ (will be done at a later time) r? `@ghost` cc `@michaelwoerister`
2 parents 7c89e38 + b5bec17 commit f913a4f

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

compiler/rustc_data_structures/src/profiling.rs

+49-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_id(&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,14 @@ impl SelfProfilerRef {
364390
}
365391
}
366392

393+
/// Gets a `StringId` for the given string. This method makes sure that
394+
/// any strings going through it will only be allocated once in the
395+
/// profiling data.
396+
/// Returns `None` if the self-profiling is not enabled.
397+
pub fn get_or_alloc_cached_string(&self, s: &str) -> Option<StringId> {
398+
self.profiler.as_ref().map(|p| p.get_or_alloc_cached_string(s))
399+
}
400+
367401
#[inline]
368402
pub fn enabled(&self) -> bool {
369403
self.profiler.is_some()
@@ -388,6 +422,7 @@ pub struct SelfProfiler {
388422
query_event_kind: StringId,
389423
generic_activity_event_kind: StringId,
390424
incremental_load_result_event_kind: StringId,
425+
incremental_result_hashing_event_kind: StringId,
391426
query_blocked_event_kind: StringId,
392427
query_cache_hit_event_kind: StringId,
393428
}
@@ -408,6 +443,8 @@ impl SelfProfiler {
408443
let query_event_kind = profiler.alloc_string("Query");
409444
let generic_activity_event_kind = profiler.alloc_string("GenericActivity");
410445
let incremental_load_result_event_kind = profiler.alloc_string("IncrementalLoadResult");
446+
let incremental_result_hashing_event_kind =
447+
profiler.alloc_string("IncrementalResultHashing");
411448
let query_blocked_event_kind = profiler.alloc_string("QueryBlocked");
412449
let query_cache_hit_event_kind = profiler.alloc_string("QueryCacheHit");
413450

@@ -451,6 +488,7 @@ impl SelfProfiler {
451488
query_event_kind,
452489
generic_activity_event_kind,
453490
incremental_load_result_event_kind,
491+
incremental_result_hashing_event_kind,
454492
query_blocked_event_kind,
455493
query_cache_hit_event_kind,
456494
})

compiler/rustc_query_system/src/dep_graph/graph.rs

+25-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,12 @@ 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+
/// This will be None if self-profiling is disabled.
43+
node_intern_event_id: Option<EventId>,
3944
}
4045

4146
rustc_index::newtype_index! {
@@ -130,6 +135,10 @@ impl<K: DepKind> DepGraph<K> {
130135
);
131136
debug_assert_eq!(_green_node_index, DepNodeIndex::SINGLETON_DEPENDENCYLESS_ANON_NODE);
132137

138+
let node_intern_event_id = profiler
139+
.get_or_alloc_cached_string("incr_comp_intern_dep_graph_node")
140+
.map(EventId::from_label);
141+
133142
DepGraph {
134143
data: Some(Lrc::new(DepGraphData {
135144
previous_work_products: prev_work_products,
@@ -141,11 +150,16 @@ impl<K: DepKind> DepGraph<K> {
141150
colors: DepNodeColorMap::new(prev_graph_node_count),
142151
})),
143152
virtual_dep_node_index: Lrc::new(AtomicU32::new(0)),
153+
node_intern_event_id,
144154
}
145155
}
146156

147157
pub fn new_disabled() -> DepGraph<K> {
148-
DepGraph { data: None, virtual_dep_node_index: Lrc::new(AtomicU32::new(0)) }
158+
DepGraph {
159+
data: None,
160+
virtual_dep_node_index: Lrc::new(AtomicU32::new(0)),
161+
node_intern_event_id: None,
162+
}
149163
}
150164

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

246260
let mut hcx = dcx.create_stable_hashing_context();
261+
let hashing_timer = dcx.profiler().incr_result_hashing();
247262
let current_fingerprint = hash_result(&mut hcx, &result);
248263

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

266+
// Get timer for profiling `DepNode` interning
267+
let node_intern_timer = self
268+
.node_intern_event_id
269+
.map(|eid| dcx.profiler().generic_activity_with_event_id(eid));
251270
// Intern the new `DepNode`.
252271
let (dep_node_index, prev_and_color) = data.current.intern_node(
253272
dcx.profiler(),
@@ -257,6 +276,9 @@ impl<K: DepKind> DepGraph<K> {
257276
current_fingerprint,
258277
print_status,
259278
);
279+
drop(node_intern_timer);
280+
281+
hashing_timer.finish_with_query_invocation_id(dep_node_index.into());
260282

261283
if let Some((prev_index, color)) = prev_and_color {
262284
debug_assert!(

compiler/rustc_session/src/options.rs

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

0 commit comments

Comments
 (0)