3 use std::mem::{self, Discriminant};
7 use std::thread::ThreadId;
10 use measureme::{StringId};
12 /// MmapSerializatioSink is faster on macOS and Linux
13 /// but FileSerializationSink is faster on Windows
15 type SerializationSink = measureme::MmapSerializationSink;
17 type SerializationSink = measureme::FileSerializationSink;
19 type Profiler = measureme::Profiler<SerializationSink>;
21 pub trait QueryName: Sized + Copy {
22 fn discriminant(self) -> Discriminant<Self>;
23 fn as_str(self) -> &'static str;
26 #[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)]
27 pub enum ProfileCategory {
38 struct EventFilter: u32 {
39 const GENERIC_ACTIVITIES = 1 << 0;
40 const QUERY_PROVIDERS = 1 << 1;
41 const QUERY_CACHE_HITS = 1 << 2;
42 const QUERY_BLOCKED = 1 << 3;
43 const INCR_CACHE_LOADS = 1 << 4;
45 const DEFAULT = Self::GENERIC_ACTIVITIES.bits |
46 Self::QUERY_PROVIDERS.bits |
47 Self::QUERY_BLOCKED.bits |
48 Self::INCR_CACHE_LOADS.bits;
50 // empty() and none() aren't const-fns unfortunately
52 const ALL = !Self::NONE.bits;
56 const EVENT_FILTERS_BY_NAME: &[(&str, EventFilter)] = &[
57 ("none", EventFilter::NONE),
58 ("all", EventFilter::ALL),
59 ("generic-activity", EventFilter::GENERIC_ACTIVITIES),
60 ("query-provider", EventFilter::QUERY_PROVIDERS),
61 ("query-cache-hit", EventFilter::QUERY_CACHE_HITS),
62 ("query-blocked" , EventFilter::QUERY_BLOCKED),
63 ("incr-cache-load", EventFilter::INCR_CACHE_LOADS),
66 fn thread_id_to_u32(tid: ThreadId) -> u32 {
67 unsafe { mem::transmute::<ThreadId, u64>(tid) as u32 }
71 /// A reference to the SelfProfiler. It can be cloned and sent across thread
72 /// boundaries at will.
74 pub struct SelfProfilerRef {
75 // This field is `None` if self-profiling is disabled for the current
76 // compilation session.
77 profiler: Option<Arc<SelfProfiler>>,
79 // We store the filter mask directly in the reference because that doesn't
80 // cost anything and allows for filtering with checking if the profiler is
82 event_filter_mask: EventFilter,
85 impl SelfProfilerRef {
87 pub fn new(profiler: Option<Arc<SelfProfiler>>) -> SelfProfilerRef {
88 // If there is no SelfProfiler then the filter mask is set to NONE,
89 // ensuring that nothing ever tries to actually access it.
90 let event_filter_mask = profiler
92 .map(|p| p.event_filter_mask)
93 .unwrap_or(EventFilter::NONE);
101 // This shim makes sure that calls only get executed if the filter mask
102 // lets them pass. It also contains some trickery to make sure that
103 // code is optimized for non-profiling compilation sessions, i.e. anything
104 // past the filter check is never inlined so it doesn't clutter the fast
107 fn exec<F>(&self, event_filter: EventFilter, f: F) -> TimingGuard<'_>
108 where F: for<'a> FnOnce(&'a SelfProfiler) -> TimingGuard<'a>
111 fn cold_call<F>(profiler_ref: &SelfProfilerRef, f: F) -> TimingGuard<'_>
112 where F: for<'a> FnOnce(&'a SelfProfiler) -> TimingGuard<'a>
114 let profiler = profiler_ref.profiler.as_ref().unwrap();
118 if unlikely!(self.event_filter_mask.contains(event_filter)) {
125 /// Start profiling a generic activity. Profiling continues until the
126 /// TimingGuard returned from this call is dropped.
128 pub fn generic_activity(&self, event_id: &str) -> TimingGuard<'_> {
129 self.exec(EventFilter::GENERIC_ACTIVITIES, |profiler| {
130 let event_id = profiler.profiler.alloc_string(event_id);
133 profiler.generic_activity_event_kind,
139 /// Start profiling a query provider. Profiling continues until the
140 /// TimingGuard returned from this call is dropped.
142 pub fn query_provider(&self, query_name: impl QueryName) -> TimingGuard<'_> {
143 self.exec(EventFilter::QUERY_PROVIDERS, |profiler| {
144 let event_id = SelfProfiler::get_query_name_string_id(query_name);
145 TimingGuard::start(profiler, profiler.query_event_kind, event_id)
149 /// Record a query in-memory cache hit.
151 pub fn query_cache_hit(&self, query_name: impl QueryName) {
152 self.instant_query_event(
153 |profiler| profiler.query_cache_hit_event_kind,
155 EventFilter::QUERY_CACHE_HITS,
159 /// Start profiling a query being blocked on a concurrent execution.
160 /// Profiling continues until the TimingGuard returned from this call is
163 pub fn query_blocked(&self, query_name: impl QueryName) -> TimingGuard<'_> {
164 self.exec(EventFilter::QUERY_BLOCKED, |profiler| {
165 let event_id = SelfProfiler::get_query_name_string_id(query_name);
166 TimingGuard::start(profiler, profiler.query_blocked_event_kind, event_id)
170 /// Start profiling how long it takes to load a query result from the
171 /// incremental compilation on-disk cache. Profiling continues until the
172 /// TimingGuard returned from this call is dropped.
174 pub fn incr_cache_loading(&self, query_name: impl QueryName) -> TimingGuard<'_> {
175 self.exec(EventFilter::INCR_CACHE_LOADS, |profiler| {
176 let event_id = SelfProfiler::get_query_name_string_id(query_name);
179 profiler.incremental_load_result_event_kind,
186 fn instant_query_event(
188 event_kind: fn(&SelfProfiler) -> StringId,
189 query_name: impl QueryName,
190 event_filter: EventFilter,
192 drop(self.exec(event_filter, |profiler| {
193 let event_id = SelfProfiler::get_query_name_string_id(query_name);
194 let thread_id = thread_id_to_u32(std::thread::current().id());
196 profiler.profiler.record_instant_event(
197 event_kind(profiler),
206 pub fn register_queries(&self, f: impl FnOnce(&SelfProfiler)) {
207 if let Some(profiler) = &self.profiler {
213 pub struct SelfProfiler {
215 event_filter_mask: EventFilter,
216 query_event_kind: StringId,
217 generic_activity_event_kind: StringId,
218 incremental_load_result_event_kind: StringId,
219 query_blocked_event_kind: StringId,
220 query_cache_hit_event_kind: StringId,
225 output_directory: &Path,
226 crate_name: Option<&str>,
227 event_filters: &Option<Vec<String>>
228 ) -> Result<SelfProfiler, Box<dyn Error>> {
229 fs::create_dir_all(output_directory)?;
231 let crate_name = crate_name.unwrap_or("unknown-crate");
232 let filename = format!("{}-{}.rustc_profile", crate_name, process::id());
233 let path = output_directory.join(&filename);
234 let profiler = Profiler::new(&path)?;
236 let query_event_kind = profiler.alloc_string("Query");
237 let generic_activity_event_kind = profiler.alloc_string("GenericActivity");
238 let incremental_load_result_event_kind = profiler.alloc_string("IncrementalLoadResult");
239 let query_blocked_event_kind = profiler.alloc_string("QueryBlocked");
240 let query_cache_hit_event_kind = profiler.alloc_string("QueryCacheHit");
242 let mut event_filter_mask = EventFilter::empty();
244 if let Some(ref event_filters) = *event_filters {
245 let mut unknown_events = vec![];
246 for item in event_filters {
247 if let Some(&(_, mask)) = EVENT_FILTERS_BY_NAME.iter()
248 .find(|&(name, _)| name == item) {
249 event_filter_mask |= mask;
251 unknown_events.push(item.clone());
255 // Warn about any unknown event names
256 if unknown_events.len() > 0 {
257 unknown_events.sort();
258 unknown_events.dedup();
260 warn!("Unknown self-profiler events specified: {}. Available options are: {}.",
261 unknown_events.join(", "),
262 EVENT_FILTERS_BY_NAME.iter()
263 .map(|&(name, _)| name.to_string())
268 event_filter_mask = EventFilter::DEFAULT;
275 generic_activity_event_kind,
276 incremental_load_result_event_kind,
277 query_blocked_event_kind,
278 query_cache_hit_event_kind,
282 fn get_query_name_string_id(query_name: impl QueryName) -> StringId {
283 let discriminant = unsafe {
284 mem::transmute::<Discriminant<_>, u64>(query_name.discriminant())
287 StringId::reserved(discriminant as u32)
290 pub fn register_query_name(&self, query_name: impl QueryName) {
291 let id = SelfProfiler::get_query_name_string_id(query_name);
292 self.profiler.alloc_string_with_reserved_id(id, query_name.as_str());
297 pub struct TimingGuard<'a>(Option<measureme::TimingGuard<'a, SerializationSink>>);
299 impl<'a> TimingGuard<'a> {
302 profiler: &'a SelfProfiler,
303 event_kind: StringId,
305 ) -> TimingGuard<'a> {
306 let thread_id = thread_id_to_u32(std::thread::current().id());
307 let raw_profiler = &profiler.profiler;
308 let timing_guard = raw_profiler.start_recording_interval_event(event_kind,
311 TimingGuard(Some(timing_guard))
315 pub fn none() -> TimingGuard<'a> {