Skip to content

Commit d06f931

Browse files
authored
Rollup merge of rust-lang#59915 - michaelwoerister:sp-event-filters-1, r=wesleywiser
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 rust-lang#59515 and, for the moment, is meant for performance testing. r? @wesleywiser
2 parents d6f513e + 08efbac commit d06f931

File tree

3 files changed

+97
-28
lines changed

3 files changed

+97
-28
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14671467
"inject the given attribute in the crate"),
14681468
self_profile: bool = (false, parse_bool, [UNTRACKED],
14691469
"run the self profiler and output the raw event data"),
1470+
self_profile_events: Option<Vec<String>> = (None, parse_opt_comma_list, [UNTRACKED],
1471+
"specifies which kinds of events get recorded by the self profiler"),
14701472
emit_stack_sizes: bool = (false, parse_bool, [UNTRACKED],
14711473
"emits a section containing stack size metadata"),
14721474
plt: Option<bool> = (None, parse_opt_bool, [TRACKED],

src/librustc/session/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ fn build_session_(
11381138
) -> Session {
11391139
let self_profiler =
11401140
if sopts.debugging_opts.self_profile {
1141-
let profiler = SelfProfiler::new();
1141+
let profiler = SelfProfiler::new(&sopts.debugging_opts.self_profile_events);
11421142
match profiler {
11431143
Ok(profiler) => {
11441144
crate::ty::query::QueryName::register_with_profiler(&profiler);

src/librustc/util/profiling.rs

+94-27
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,42 @@ pub enum ProfileCategory {
2727
Other,
2828
}
2929

30-
#[derive(Clone, Debug, Eq, PartialEq)]
31-
pub enum ProfilerEvent {
32-
QueryStart { query_name: &'static str, category: ProfileCategory, time: u64 },
33-
QueryEnd { query_name: &'static str, category: ProfileCategory, time: u64 },
34-
GenericActivityStart { category: ProfileCategory, label: Cow<'static, str>, time: u64 },
35-
GenericActivityEnd { category: ProfileCategory, label: Cow<'static, str>, time: u64 },
36-
IncrementalLoadResultStart { query_name: &'static str, time: u64 },
37-
IncrementalLoadResultEnd { query_name: &'static str, time: u64 },
38-
QueryCacheHit { query_name: &'static str, category: ProfileCategory, time: u64 },
39-
QueryCount { query_name: &'static str, category: ProfileCategory, count: usize, time: u64 },
40-
QueryBlockedStart { query_name: &'static str, category: ProfileCategory, time: u64 },
41-
QueryBlockedEnd { query_name: &'static str, category: ProfileCategory, time: u64 },
30+
bitflags! {
31+
struct EventFilter: u32 {
32+
const GENERIC_ACTIVITIES = 1 << 0;
33+
const QUERY_PROVIDERS = 1 << 1;
34+
const QUERY_CACHE_HITS = 1 << 2;
35+
const QUERY_BLOCKED = 1 << 3;
36+
const INCR_CACHE_LOADS = 1 << 4;
37+
38+
const DEFAULT = Self::GENERIC_ACTIVITIES.bits |
39+
Self::QUERY_PROVIDERS.bits |
40+
Self::QUERY_BLOCKED.bits |
41+
Self::INCR_CACHE_LOADS.bits;
42+
43+
// empty() and none() aren't const-fns unfortunately
44+
const NONE = 0;
45+
const ALL = !Self::NONE.bits;
46+
}
4247
}
4348

49+
const EVENT_FILTERS_BY_NAME: &[(&str, EventFilter)] = &[
50+
("none", EventFilter::NONE),
51+
("all", EventFilter::ALL),
52+
("generic-activity", EventFilter::GENERIC_ACTIVITIES),
53+
("query-provider", EventFilter::QUERY_PROVIDERS),
54+
("query-cache-hit", EventFilter::QUERY_CACHE_HITS),
55+
("query-blocked" , EventFilter::QUERY_BLOCKED),
56+
("incr-cache-load", EventFilter::INCR_CACHE_LOADS),
57+
];
58+
4459
fn thread_id_to_u64(tid: ThreadId) -> u64 {
4560
unsafe { mem::transmute::<ThreadId, u64>(tid) }
4661
}
4762

4863
pub struct SelfProfiler {
4964
profiler: Profiler,
65+
event_filter_mask: EventFilter,
5066
query_event_kind: StringId,
5167
generic_activity_event_kind: StringId,
5268
incremental_load_result_event_kind: StringId,
@@ -55,7 +71,7 @@ pub struct SelfProfiler {
5571
}
5672

5773
impl SelfProfiler {
58-
pub fn new() -> Result<SelfProfiler, Box<dyn Error>> {
74+
pub fn new(event_filters: &Option<Vec<String>>) -> Result<SelfProfiler, Box<dyn Error>> {
5975
let filename = format!("pid-{}.rustc_profile", process::id());
6076
let path = std::path::Path::new(&filename);
6177
let profiler = Profiler::new(path)?;
@@ -66,8 +82,38 @@ impl SelfProfiler {
6682
let query_blocked_event_kind = profiler.alloc_string("QueryBlocked");
6783
let query_cache_hit_event_kind = profiler.alloc_string("QueryCacheHit");
6884

85+
let mut event_filter_mask = EventFilter::empty();
86+
87+
if let Some(ref event_filters) = *event_filters {
88+
let mut unknown_events = vec![];
89+
for item in event_filters {
90+
if let Some(&(_, mask)) = EVENT_FILTERS_BY_NAME.iter()
91+
.find(|&(name, _)| name == item) {
92+
event_filter_mask |= mask;
93+
} else {
94+
unknown_events.push(item.clone());
95+
}
96+
}
97+
98+
// Warn about any unknown event names
99+
if unknown_events.len() > 0 {
100+
unknown_events.sort();
101+
unknown_events.dedup();
102+
103+
warn!("Unknown self-profiler events specified: {}. Available options are: {}.",
104+
unknown_events.join(", "),
105+
EVENT_FILTERS_BY_NAME.iter()
106+
.map(|&(name, _)| name.to_string())
107+
.collect::<Vec<_>>()
108+
.join(", "));
109+
}
110+
} else {
111+
event_filter_mask = EventFilter::DEFAULT;
112+
}
113+
69114
Ok(SelfProfiler {
70115
profiler,
116+
event_filter_mask,
71117
query_event_kind,
72118
generic_activity_event_kind,
73119
incremental_load_result_event_kind,
@@ -86,7 +132,6 @@ impl SelfProfiler {
86132

87133
pub fn register_query_name(&self, query_name: QueryName) {
88134
let id = SelfProfiler::get_query_name_string_id(query_name);
89-
90135
self.profiler.alloc_string_with_reserved_id(id, query_name.as_str());
91136
}
92137

@@ -95,54 +140,76 @@ impl SelfProfiler {
95140
&self,
96141
label: impl Into<Cow<'static, str>>,
97142
) {
98-
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::Start);
143+
if self.event_filter_mask.contains(EventFilter::GENERIC_ACTIVITIES) {
144+
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::Start);
145+
}
99146
}
100147

101148
#[inline]
102149
pub fn end_activity(
103150
&self,
104151
label: impl Into<Cow<'static, str>>,
105152
) {
106-
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::End);
153+
if self.event_filter_mask.contains(EventFilter::GENERIC_ACTIVITIES) {
154+
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::End);
155+
}
107156
}
108157

109158
#[inline]
110159
pub fn record_query_hit(&self, query_name: QueryName) {
111-
self.record_query(query_name, self.query_cache_hit_event_kind, TimestampKind::Instant);
160+
if self.event_filter_mask.contains(EventFilter::QUERY_CACHE_HITS) {
161+
self.record_query(query_name, self.query_cache_hit_event_kind, TimestampKind::Instant);
162+
}
112163
}
113164

114165
#[inline]
115166
pub fn start_query(&self, query_name: QueryName) {
116-
self.record_query(query_name, self.query_event_kind, TimestampKind::Start);
167+
if self.event_filter_mask.contains(EventFilter::QUERY_PROVIDERS) {
168+
self.record_query(query_name, self.query_event_kind, TimestampKind::Start);
169+
}
117170
}
118171

119172
#[inline]
120173
pub fn end_query(&self, query_name: QueryName) {
121-
self.record_query(query_name, self.query_event_kind, TimestampKind::End);
174+
if self.event_filter_mask.contains(EventFilter::QUERY_PROVIDERS) {
175+
self.record_query(query_name, self.query_event_kind, TimestampKind::End);
176+
}
122177
}
123178

124179
#[inline]
125180
pub fn incremental_load_result_start(&self, query_name: QueryName) {
126-
self.record_query(
127-
query_name,
128-
self.incremental_load_result_event_kind,
129-
TimestampKind::Start
130-
);
181+
if self.event_filter_mask.contains(EventFilter::INCR_CACHE_LOADS) {
182+
self.record_query(
183+
query_name,
184+
self.incremental_load_result_event_kind,
185+
TimestampKind::Start
186+
);
187+
}
131188
}
132189

133190
#[inline]
134191
pub fn incremental_load_result_end(&self, query_name: QueryName) {
135-
self.record_query(query_name, self.incremental_load_result_event_kind, TimestampKind::End);
192+
if self.event_filter_mask.contains(EventFilter::INCR_CACHE_LOADS) {
193+
self.record_query(
194+
query_name,
195+
self.incremental_load_result_event_kind,
196+
TimestampKind::End
197+
);
198+
}
136199
}
137200

138201
#[inline]
139202
pub fn query_blocked_start(&self, query_name: QueryName) {
140-
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::Start);
203+
if self.event_filter_mask.contains(EventFilter::QUERY_BLOCKED) {
204+
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::Start);
205+
}
141206
}
142207

143208
#[inline]
144209
pub fn query_blocked_end(&self, query_name: QueryName) {
145-
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::End);
210+
if self.event_filter_mask.contains(EventFilter::QUERY_BLOCKED) {
211+
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::End);
212+
}
146213
}
147214

148215
#[inline]

0 commit comments

Comments
 (0)