@@ -27,26 +27,42 @@ pub enum ProfileCategory {
27
27
Other ,
28
28
}
29
29
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
+ }
42
47
}
43
48
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
+
44
59
fn thread_id_to_u64 ( tid : ThreadId ) -> u64 {
45
60
unsafe { mem:: transmute :: < ThreadId , u64 > ( tid) }
46
61
}
47
62
48
63
pub struct SelfProfiler {
49
64
profiler : Profiler ,
65
+ event_filter_mask : EventFilter ,
50
66
query_event_kind : StringId ,
51
67
generic_activity_event_kind : StringId ,
52
68
incremental_load_result_event_kind : StringId ,
@@ -55,7 +71,7 @@ pub struct SelfProfiler {
55
71
}
56
72
57
73
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 > > {
59
75
let filename = format ! ( "pid-{}.rustc_profile" , process:: id( ) ) ;
60
76
let path = std:: path:: Path :: new ( & filename) ;
61
77
let profiler = Profiler :: new ( path) ?;
@@ -66,8 +82,38 @@ impl SelfProfiler {
66
82
let query_blocked_event_kind = profiler. alloc_string ( "QueryBlocked" ) ;
67
83
let query_cache_hit_event_kind = profiler. alloc_string ( "QueryCacheHit" ) ;
68
84
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
+
69
114
Ok ( SelfProfiler {
70
115
profiler,
116
+ event_filter_mask,
71
117
query_event_kind,
72
118
generic_activity_event_kind,
73
119
incremental_load_result_event_kind,
@@ -86,7 +132,6 @@ impl SelfProfiler {
86
132
87
133
pub fn register_query_name ( & self , query_name : QueryName ) {
88
134
let id = SelfProfiler :: get_query_name_string_id ( query_name) ;
89
-
90
135
self . profiler . alloc_string_with_reserved_id ( id, query_name. as_str ( ) ) ;
91
136
}
92
137
@@ -95,54 +140,76 @@ impl SelfProfiler {
95
140
& self ,
96
141
label : impl Into < Cow < ' static , str > > ,
97
142
) {
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
+ }
99
146
}
100
147
101
148
#[ inline]
102
149
pub fn end_activity (
103
150
& self ,
104
151
label : impl Into < Cow < ' static , str > > ,
105
152
) {
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
+ }
107
156
}
108
157
109
158
#[ inline]
110
159
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
+ }
112
163
}
113
164
114
165
#[ inline]
115
166
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
+ }
117
170
}
118
171
119
172
#[ inline]
120
173
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
+ }
122
177
}
123
178
124
179
#[ inline]
125
180
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
+ }
131
188
}
132
189
133
190
#[ inline]
134
191
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
+ }
136
199
}
137
200
138
201
#[ inline]
139
202
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
+ }
141
206
}
142
207
143
208
#[ inline]
144
209
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
+ }
146
213
}
147
214
148
215
#[ inline]
0 commit comments