Skip to content

Commit d76573a

Browse files
committed
Integrate measureme's hardware performance counter support.
1 parent 083721a commit d76573a

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

compiler/rustc_data_structures/src/profiling.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,20 @@ impl SelfProfiler {
550550
pub fn new(
551551
output_directory: &Path,
552552
crate_name: Option<&str>,
553-
event_filters: &Option<Vec<String>>,
553+
event_filters: Option<&[String]>,
554+
counter_name: &str,
554555
) -> Result<SelfProfiler, Box<dyn Error + Send + Sync>> {
555556
fs::create_dir_all(output_directory)?;
556557

557558
let crate_name = crate_name.unwrap_or("unknown-crate");
558-
let filename = format!("{}-{}.rustc_profile", crate_name, process::id());
559+
// HACK(eddyb) we need to pad the PID, strange as it may seem, as its
560+
// length can behave as a source of entropy for heap addresses, when
561+
// ASLR is disabled and the heap is otherwise determinic.
562+
let pid: u32 = process::id();
563+
let filename = format!("{}-{:07}.rustc_profile", crate_name, pid);
559564
let path = output_directory.join(&filename);
560-
let profiler = Profiler::new(&path)?;
565+
let profiler =
566+
Profiler::with_counter(&path, measureme::counters::Counter::by_name(counter_name)?)?;
561567

562568
let query_event_kind = profiler.alloc_string("Query");
563569
let generic_activity_event_kind = profiler.alloc_string("GenericActivity");
@@ -570,7 +576,7 @@ impl SelfProfiler {
570576

571577
let mut event_filter_mask = EventFilter::empty();
572578

573-
if let Some(ref event_filters) = *event_filters {
579+
if let Some(event_filters) = event_filters {
574580
let mut unknown_events = vec![];
575581
for item in event_filters {
576582
if let Some(&(_, mask)) =

compiler/rustc_query_impl/src/profiling_support.rs

+3
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
275275
let query_name = profiler.get_or_alloc_cached_string(query_name);
276276
let event_id = event_id_builder.from_label(query_name).to_string_id();
277277

278+
// FIXME(eddyb) make this O(1) by using a pre-cached query name `EventId`,
279+
// instead of passing the `DepNodeIndex` to `finish_with_query_invocation_id`,
280+
// when recording the event in the first place.
278281
let mut query_invocation_ids = Vec::new();
279282
query_cache.iter(&mut |_, _, i| {
280283
query_invocation_ids.push(i.into());

compiler/rustc_session/src/options.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,12 @@ options! {
14731473
for example: `-Z self-profile-events=default,query-keys`
14741474
all options: none, all, default, generic-activity, query-provider, query-cache-hit
14751475
query-blocked, incr-cache-load, incr-result-hashing, query-keys, function-args, args, llvm, artifact-sizes"),
1476+
self_profile_counter: String = ("wall-time".to_string(), parse_string, [UNTRACKED],
1477+
"counter used by the self profiler (default: `wall-time`), one of:
1478+
`wall-time` (monotonic clock, i.e. `std::time::Instant`)
1479+
`instructions:u` (retired instructions, userspace-only)
1480+
`instructions-minus-irqs:u` (subtracting hardware interrupt counts for extra accuracy)"
1481+
),
14761482
share_generics: Option<bool> = (None, parse_opt_bool, [TRACKED],
14771483
"make the current crate share its generic instantiations"),
14781484
show_span: Option<String> = (None, parse_opt_string, [TRACKED],

compiler/rustc_session/src/session.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,8 @@ pub fn build_session(
12521252
let profiler = SelfProfiler::new(
12531253
directory,
12541254
sopts.crate_name.as_deref(),
1255-
&sopts.debugging_opts.self_profile_events,
1255+
sopts.debugging_opts.self_profile_events.as_ref().map(|xs| &xs[..]),
1256+
&sopts.debugging_opts.self_profile_counter,
12561257
);
12571258
match profiler {
12581259
Ok(profiler) => Some(Arc::new(profiler)),

0 commit comments

Comments
 (0)