Skip to content

Commit 30ce132

Browse files
committed
Auto merge of #59915 - michaelwoerister:sp-event-filters-1, r=<try>
[WIP] Implement event filtering for self-profiler. This is a first sketch for event filtering in the self-profiler, something that we'll want in order to keep profiling overhead low in the common case. The PR contains the commits from #59515 and, for the moment, is meant for performance testing. r? @wesleywiser
2 parents 99da733 + 10b43bb commit 30ce132

File tree

17 files changed

+252
-476
lines changed

17 files changed

+252
-476
lines changed

Cargo.lock

+12
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,16 @@ dependencies = [
14741474
"toml-query 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
14751475
]
14761476

1477+
[[package]]
1478+
name = "measureme"
1479+
version = "0.2.1"
1480+
source = "registry+https://github.com/rust-lang/crates.io-index"
1481+
dependencies = [
1482+
"byteorder 1.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
1483+
"memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
1484+
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
1485+
]
1486+
14771487
[[package]]
14781488
name = "memchr"
14791489
version = "2.1.1"
@@ -2326,6 +2336,7 @@ dependencies = [
23262336
"jobserver 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
23272337
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
23282338
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
2339+
"measureme 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
23292340
"num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
23302341
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
23312342
"polonius-engine 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -4106,6 +4117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
41064117
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
41074118
"checksum mdbook 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "90b5a8d7e341ceee5db3882a06078d42661ddcfa2b3687319cc5da76ec4e782f"
41084119
"checksum mdbook 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0ba0d44cb4089c741b9a91f3e5218298a40699c2f3a070a85014eed290c60819"
4120+
"checksum measureme 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "36bb2b263a6795d352035024d6b30ce465bb79a5e5280d74c3b5f8464c657bcc"
41094121
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
41104122
"checksum memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2ffa2c986de11a9df78620c01eeaaf27d94d3ff02bf81bfcca953102dd0c6ff"
41114123
"checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3"

src/librustc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ byteorder = { version = "1.1", features = ["i128"]}
3636
chalk-engine = { version = "0.9.0", default-features=false }
3737
rustc_fs_util = { path = "../librustc_fs_util" }
3838
smallvec = { version = "0.6.7", features = ["union", "may_dangle"] }
39+
measureme = "0.2.1"
3940

4041
# Note that these dependencies are a lie, they're just here to get linkage to
4142
# work.

src/librustc/session/config.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1431,8 +1431,10 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14311431
"don't interleave execution of lints; allows benchmarking individual lints"),
14321432
crate_attr: Vec<String> = (Vec::new(), parse_string_push, [TRACKED],
14331433
"inject the given attribute in the crate"),
1434-
self_profile: bool = (false, parse_bool, [UNTRACKED],
1434+
self_profile: bool = (true, parse_bool, [UNTRACKED],
14351435
"run the self profiler and output the raw event data"),
1436+
self_profile_event_filters: Option<Vec<String>> = (None, parse_opt_comma_list, [UNTRACKED],
1437+
"specifies which kinds of events get recorded by the self profiler"),
14361438
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
14371439
"emits a section containing stack size metadata"),
14381440
plt: Option<bool> = (None, parse_opt_bool, [TRACKED],

src/librustc/session/mod.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ use std::path::PathBuf;
4646
use std::time::Duration;
4747
use std::sync::{Arc, mpsc};
4848

49-
use parking_lot::Mutex as PlMutex;
50-
5149
mod code_stats;
5250
pub mod config;
5351
pub mod filesearch;
@@ -130,7 +128,7 @@ pub struct Session {
130128
pub profile_channel: Lock<Option<mpsc::Sender<ProfileQueriesMsg>>>,
131129

132130
/// Used by -Z self-profile
133-
pub self_profiling: Option<Arc<PlMutex<SelfProfiler>>>,
131+
pub self_profiling: Option<Arc<SelfProfiler>>,
134132

135133
/// Some measurements that are being gathered during compilation.
136134
pub perf_stats: PerfStats,
@@ -838,19 +836,17 @@ impl Session {
838836

839837
#[inline(never)]
840838
#[cold]
841-
fn profiler_active<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
839+
fn profiler_active<F: FnOnce(&SelfProfiler) -> ()>(&self, f: F) {
842840
match &self.self_profiling {
843841
None => bug!("profiler_active() called but there was no profiler active"),
844842
Some(profiler) => {
845-
let mut p = profiler.lock();
846-
847-
f(&mut p);
843+
f(&profiler);
848844
}
849845
}
850846
}
851847

852848
#[inline(always)]
853-
pub fn profiler<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
849+
pub fn profiler<F: FnOnce(&SelfProfiler) -> ()>(&self, f: F) {
854850
if unlikely!(self.self_profiling.is_some()) {
855851
self.profiler_active(f)
856852
}
@@ -1138,7 +1134,19 @@ fn build_session_(
11381134
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
11391135
) -> Session {
11401136
let self_profiler =
1141-
if sopts.debugging_opts.self_profile { Some(Arc::new(PlMutex::new(SelfProfiler::new()))) }
1137+
if sopts.debugging_opts.self_profile {
1138+
let profiler = SelfProfiler::new(&sopts.debugging_opts.self_profile_event_filters);
1139+
match profiler {
1140+
Ok(profiler) => {
1141+
crate::ty::query::QueryName::register_with_profiler(&profiler);
1142+
Some(Arc::new(profiler))
1143+
},
1144+
Err(e) => {
1145+
early_warn(sopts.error_format, &format!("failed to create profiler: {}", e));
1146+
None
1147+
}
1148+
}
1149+
}
11421150
else { None };
11431151

11441152
let host_triple = TargetTriple::from_triple(config::host_triple());

src/librustc/ty/query/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::dep_graph::DepNode;
33
use crate::hir::def_id::{CrateNum, DefId};
44
use crate::ty::TyCtxt;
55
use crate::ty::query::queries;
6-
use crate::ty::query::Query;
6+
use crate::ty::query::{Query, QueryName};
77
use crate::ty::query::QueryCache;
88
use crate::ty::query::plumbing::CycleError;
99
use crate::util::profiling::ProfileCategory;
@@ -18,7 +18,7 @@ use crate::ich::StableHashingContext;
1818
// Query configuration and description traits.
1919

2020
pub trait QueryConfig<'tcx> {
21-
const NAME: &'static str;
21+
const NAME: QueryName;
2222
const CATEGORY: ProfileCategory;
2323

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

src/librustc/ty/query/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use crate::ty::subst::SubstsRef;
4141
use crate::util::nodemap::{DefIdSet, DefIdMap, ItemLocalSet};
4242
use crate::util::common::{ErrorReported};
4343
use crate::util::profiling::ProfileCategory::*;
44-
use crate::session::Session;
4544

4645
use rustc_data_structures::svh::Svh;
4746
use rustc_data_structures::bit_set::BitSet;

src/librustc/ty/query/plumbing.rs

+40-27
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
114114
let mut lock = cache.borrow_mut();
115115
if let Some(value) = lock.results.get(key) {
116116
profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
117-
tcx.sess.profiler(|p| p.record_query_hit(Q::NAME, Q::CATEGORY));
117+
tcx.sess.profiler(|p| p.record_query_hit(Q::NAME));
118118
let result = (value.value.clone(), value.index);
119119
#[cfg(debug_assertions)]
120120
{
@@ -130,7 +130,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
130130
//in another thread has completed. Record how long we wait in the
131131
//self-profiler
132132
#[cfg(parallel_compiler)]
133-
tcx.sess.profiler(|p| p.query_blocked_start(Q::NAME, Q::CATEGORY));
133+
tcx.sess.profiler(|p| p.query_blocked_start(Q::NAME));
134134

135135
job.clone()
136136
},
@@ -172,7 +172,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
172172
#[cfg(parallel_compiler)]
173173
{
174174
let result = job.r#await(tcx, span);
175-
tcx.sess.profiler(|p| p.query_blocked_end(Q::NAME, Q::CATEGORY));
175+
tcx.sess.profiler(|p| p.query_blocked_end(Q::NAME));
176176

177177
if let Err(cycle) = result {
178178
return TryGetJob::Cycle(Q::handle_cycle_error(tcx, cycle));
@@ -358,14 +358,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
358358
key: Q::Key)
359359
-> Q::Value {
360360
debug!("ty::query::get_query<{}>(key={:?}, span={:?})",
361-
Q::NAME,
361+
Q::NAME.as_str(),
362362
key,
363363
span);
364364

365365
profq_msg!(self,
366366
ProfileQueriesMsg::QueryBegin(
367367
span.data(),
368-
profq_query_msg!(Q::NAME, self, key),
368+
profq_query_msg!(Q::NAME.as_str(), self, key),
369369
)
370370
);
371371

@@ -389,7 +389,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
389389

390390
if dep_node.kind.is_anon() {
391391
profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
392-
self.sess.profiler(|p| p.start_query(Q::NAME, Q::CATEGORY));
392+
self.sess.profiler(|p| p.start_query(Q::NAME));
393393

394394
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
395395
self.start_query(job.job.clone(), diagnostics, |tcx| {
@@ -399,7 +399,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
399399
})
400400
});
401401

402-
self.sess.profiler(|p| p.end_query(Q::NAME, Q::CATEGORY));
402+
self.sess.profiler(|p| p.end_query(Q::NAME));
403403
profq_msg!(self, ProfileQueriesMsg::ProviderEnd);
404404

405405
self.dep_graph.read_index(dep_node_index);
@@ -474,22 +474,22 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
474474

475475
let result = if let Some(result) = result {
476476
profq_msg!(self, ProfileQueriesMsg::CacheHit);
477-
self.sess.profiler(|p| p.record_query_hit(Q::NAME, Q::CATEGORY));
477+
self.sess.profiler(|p| p.record_query_hit(Q::NAME));
478478

479479
result
480480
} else {
481481
// We could not load a result from the on-disk cache, so
482482
// recompute.
483483

484-
self.sess.profiler(|p| p.start_query(Q::NAME, Q::CATEGORY));
484+
self.sess.profiler(|p| p.start_query(Q::NAME));
485485

486486
// The dep-graph for this computation is already in
487487
// place
488488
let result = self.dep_graph.with_ignore(|| {
489489
Q::compute(self, key)
490490
});
491491

492-
self.sess.profiler(|p| p.end_query(Q::NAME, Q::CATEGORY));
492+
self.sess.profiler(|p| p.end_query(Q::NAME));
493493
result
494494
};
495495

@@ -552,7 +552,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
552552
key, dep_node);
553553

554554
profq_msg!(self, ProfileQueriesMsg::ProviderBegin);
555-
self.sess.profiler(|p| p.start_query(Q::NAME, Q::CATEGORY));
555+
self.sess.profiler(|p| p.start_query(Q::NAME));
556556

557557
let ((result, dep_node_index), diagnostics) = with_diagnostics(|diagnostics| {
558558
self.start_query(job.job.clone(), diagnostics, |tcx| {
@@ -572,7 +572,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
572572
})
573573
});
574574

575-
self.sess.profiler(|p| p.end_query(Q::NAME, Q::CATEGORY));
575+
self.sess.profiler(|p| p.end_query(Q::NAME));
576576
profq_msg!(self, ProfileQueriesMsg::ProviderEnd);
577577

578578
if unlikely!(self.sess.opts.debugging_opts.query_dep_graph) {
@@ -619,7 +619,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
619619
let _ = self.get_query::<Q>(DUMMY_SP, key);
620620
} else {
621621
profq_msg!(self, ProfileQueriesMsg::CacheHit);
622-
self.sess.profiler(|p| p.record_query_hit(Q::NAME, Q::CATEGORY));
622+
self.sess.profiler(|p| p.record_query_hit(Q::NAME));
623623
}
624624
}
625625

@@ -632,7 +632,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
632632
) {
633633
profq_msg!(
634634
self,
635-
ProfileQueriesMsg::QueryBegin(span.data(), profq_query_msg!(Q::NAME, self, key))
635+
ProfileQueriesMsg::QueryBegin(span.data(),
636+
profq_query_msg!(Q::NAME.as_str(), self, key))
636637
);
637638

638639
// We may be concurrently trying both execute and force a query
@@ -725,18 +726,6 @@ macro_rules! define_queries_inner {
725726
}
726727
}
727728

728-
pub fn record_computed_queries(&self, sess: &Session) {
729-
sess.profiler(|p| {
730-
$(
731-
p.record_computed_queries(
732-
<queries::$name<'_> as QueryConfig<'_>>::NAME,
733-
<queries::$name<'_> as QueryConfig<'_>>::CATEGORY,
734-
self.$name.lock().results.len()
735-
);
736-
)*
737-
});
738-
}
739-
740729
#[cfg(parallel_compiler)]
741730
pub fn collect_active_jobs(&self) -> Vec<Lrc<QueryJob<$tcx>>> {
742731
let mut jobs = Vec::new();
@@ -854,6 +843,24 @@ macro_rules! define_queries_inner {
854843
}
855844
}
856845

846+
#[allow(nonstandard_style)]
847+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
848+
pub enum QueryName {
849+
$($name),*
850+
}
851+
852+
impl QueryName {
853+
pub fn register_with_profiler(profiler: &crate::util::profiling::SelfProfiler) {
854+
$(profiler.register_query_name(QueryName::$name);)*
855+
}
856+
857+
pub fn as_str(&self) -> &'static str {
858+
match self {
859+
$(QueryName::$name => stringify!($name),)*
860+
}
861+
}
862+
}
863+
857864
#[allow(nonstandard_style)]
858865
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
859866
pub enum Query<$tcx> {
@@ -894,6 +901,12 @@ macro_rules! define_queries_inner {
894901
$(Query::$name(key) => key.default_span(tcx),)*
895902
}
896903
}
904+
905+
pub fn query_name(&self) -> QueryName {
906+
match self {
907+
$(Query::$name(_) => QueryName::$name,)*
908+
}
909+
}
897910
}
898911

899912
impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
@@ -930,7 +943,7 @@ macro_rules! define_queries_inner {
930943
type Key = $K;
931944
type Value = $V;
932945

933-
const NAME: &'static str = stringify!($name);
946+
const NAME: QueryName = QueryName::$name;
934947
const CATEGORY: ProfileCategory = $category;
935948
}
936949

0 commit comments

Comments
 (0)