Skip to content

Commit 7f23917

Browse files
michaelwoeristerwesleywiser
authored andcommitted
[WIP] Implement event filtering for self-profiler.
1 parent 56e434d commit 7f23917

File tree

3 files changed

+70
-28
lines changed

3 files changed

+70
-28
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14331433
"inject the given attribute in the crate"),
14341434
self_profile: bool = (false, 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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ fn build_session_(
11351135
) -> Session {
11361136
let self_profiler =
11371137
if sopts.debugging_opts.self_profile {
1138-
let profiler = SelfProfiler::new();
1138+
let profiler = SelfProfiler::new(&sopts.debugging_opts.self_profile_event_filters);
11391139
match profiler {
11401140
Ok(profiler) => {
11411141
crate::ty::query::QueryName::register_with_profiler(&profiler);

src/librustc/util/profiling.rs

+67-27
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@ 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 = 0b00000001;
33+
const QUERY_PROVIDERS = 0b00000010;
34+
const QUERY_CACHE_HITS = 0b00000100;
35+
const QUERY_BLOCKED = 0b00001000;
36+
const INCR_CACHE_LOADS = 0b00010000;
37+
38+
const DEFAULT = Self::GENERIC_ACTIVITIES.bits |
39+
Self::QUERY_PROVIDERS.bits;
40+
}
4241
}
4342

4443
fn thread_id_to_u64(tid: ThreadId) -> u64 {
@@ -47,6 +46,7 @@ fn thread_id_to_u64(tid: ThreadId) -> u64 {
4746

4847
pub struct SelfProfiler {
4948
profiler: Profiler,
49+
event_filter_mask: EventFilter,
5050
query_event_kind: StringId,
5151
generic_activity_event_kind: StringId,
5252
incremental_load_result_event_kind: StringId,
@@ -55,7 +55,7 @@ pub struct SelfProfiler {
5555
}
5656

5757
impl SelfProfiler {
58-
pub fn new() -> Result<SelfProfiler, Box<dyn Error>> {
58+
pub fn new(event_filters: &Option<Vec<String>>) -> Result<SelfProfiler, Box<dyn Error>> {
5959
let filename = format!("pid-{}.rustc_profile", process::id());
6060
let path = std::path::Path::new(&filename);
6161
let profiler = Profiler::new(path)?;
@@ -66,8 +66,31 @@ impl SelfProfiler {
6666
let query_blocked_event_kind = profiler.alloc_string("QueryBlocked");
6767
let query_cache_hit_event_kind = profiler.alloc_string("QueryCacheHit");
6868

69+
let mut event_filter_mask = EventFilter::empty();
70+
71+
if let &Some(ref event_filters) = event_filters {
72+
for item in event_filters {
73+
event_filter_mask |= match &item[..] {
74+
"none" => EventFilter::empty(),
75+
"all" => EventFilter::all(),
76+
"generic-activities" => EventFilter::GENERIC_ACTIVITIES,
77+
"queries" => EventFilter::QUERY_PROVIDERS,
78+
"query-cache-hits" => EventFilter::QUERY_CACHE_HITS,
79+
"query-blocked" => EventFilter::QUERY_BLOCKED,
80+
"incr-cache-loads" => EventFilter::INCR_CACHE_LOADS,
81+
other => {
82+
warn!("unknown self-profiler event filter: `{}`", other);
83+
EventFilter::empty()
84+
}
85+
}
86+
}
87+
} else {
88+
event_filter_mask = EventFilter::DEFAULT;
89+
}
90+
6991
Ok(SelfProfiler {
7092
profiler,
93+
event_filter_mask,
7194
query_event_kind,
7295
generic_activity_event_kind,
7396
incremental_load_result_event_kind,
@@ -86,7 +109,6 @@ impl SelfProfiler {
86109

87110
pub fn register_query_name(&self, query_name: QueryName) {
88111
let id = SelfProfiler::get_query_name_string_id(query_name);
89-
90112
self.profiler.alloc_string_with_reserved_id(id, query_name.as_str());
91113
}
92114

@@ -95,54 +117,72 @@ impl SelfProfiler {
95117
&self,
96118
label: impl Into<Cow<'static, str>>,
97119
) {
98-
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::Start);
120+
if self.event_filter_mask.contains(EventFilter::GENERIC_ACTIVITIES) {
121+
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::Start);
122+
}
99123
}
100124

101125
#[inline]
102126
pub fn end_activity(
103127
&self,
104128
label: impl Into<Cow<'static, str>>,
105129
) {
106-
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::End);
130+
if self.event_filter_mask.contains(EventFilter::GENERIC_ACTIVITIES) {
131+
self.record(&label.into(), self.generic_activity_event_kind, TimestampKind::End);
132+
}
107133
}
108134

109135
#[inline]
110136
pub fn record_query_hit(&self, query_name: QueryName) {
111-
self.record_query(query_name, self.query_cache_hit_event_kind, TimestampKind::Instant);
137+
if self.event_filter_mask.contains(EventFilter::QUERY_CACHE_HITS) {
138+
self.record_query(query_name, self.query_cache_hit_event_kind, TimestampKind::Instant);
139+
}
112140
}
113141

114142
#[inline]
115143
pub fn start_query(&self, query_name: QueryName) {
116-
self.record_query(query_name, self.query_event_kind, TimestampKind::Start);
144+
if self.event_filter_mask.contains(EventFilter::QUERY_PROVIDERS) {
145+
self.record_query(query_name, self.query_event_kind, TimestampKind::Start);
146+
}
117147
}
118148

119149
#[inline]
120150
pub fn end_query(&self, query_name: QueryName) {
121-
self.record_query(query_name, self.query_event_kind, TimestampKind::End);
151+
if self.event_filter_mask.contains(EventFilter::QUERY_PROVIDERS) {
152+
self.record_query(query_name, self.query_event_kind, TimestampKind::End);
153+
}
122154
}
123155

124156
#[inline]
125157
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-
);
158+
if self.event_filter_mask.contains(EventFilter::INCR_CACHE_LOADS) {
159+
self.record_query(
160+
query_name,
161+
self.incremental_load_result_event_kind,
162+
TimestampKind::Start
163+
);
164+
}
131165
}
132166

133167
#[inline]
134168
pub fn incremental_load_result_end(&self, query_name: QueryName) {
135-
self.record_query(query_name, self.incremental_load_result_event_kind, TimestampKind::End);
169+
if self.event_filter_mask.contains(EventFilter::INCR_CACHE_LOADS) {
170+
self.record_query(query_name, self.incremental_load_result_event_kind, TimestampKind::End);
171+
}
136172
}
137173

138174
#[inline]
139175
pub fn query_blocked_start(&self, query_name: QueryName) {
140-
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::Start);
176+
if self.event_filter_mask.contains(EventFilter::QUERY_BLOCKED) {
177+
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::Start);
178+
}
141179
}
142180

143181
#[inline]
144182
pub fn query_blocked_end(&self, query_name: QueryName) {
145-
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::End);
183+
if self.event_filter_mask.contains(EventFilter::QUERY_BLOCKED) {
184+
self.record_query(query_name, self.query_blocked_event_kind, TimestampKind::End);
185+
}
146186
}
147187

148188
#[inline]

0 commit comments

Comments
 (0)