]> git.lizzy.rs Git - rust.git/blob - src/librustc_data_structures/profiling.rs
Auto merge of #66716 - derekdreery:debug_non_exhaustive, r=dtolnay
[rust.git] / src / librustc_data_structures / profiling.rs
1 //! # Rust Compiler Self-Profiling
2 //!
3 //! This module implements the basic framework for the compiler's self-
4 //! profiling support. It provides the `SelfProfiler` type which enables
5 //! recording "events". An event is something that starts and ends at a given
6 //! point in time and has an ID and a kind attached to it. This allows for
7 //! tracing the compiler's activity.
8 //!
9 //! Internally this module uses the custom tailored [measureme][mm] crate for
10 //! efficiently recording events to disk in a compact format that can be
11 //! post-processed and analyzed by the suite of tools in the `measureme`
12 //! project. The highest priority for the tracing framework is on incurring as
13 //! little overhead as possible.
14 //!
15 //!
16 //! ## Event Overview
17 //!
18 //! Events have a few properties:
19 //!
20 //! - The `event_kind` designates the broad category of an event (e.g. does it
21 //!   correspond to the execution of a query provider or to loading something
22 //!   from the incr. comp. on-disk cache, etc).
23 //! - The `event_id` designates the query invocation or function call it
24 //!   corresponds to, possibly including the query key or function arguments.
25 //! - Each event stores the ID of the thread it was recorded on.
26 //! - The timestamp stores beginning and end of the event, or the single point
27 //!   in time it occurred at for "instant" events.
28 //!
29 //!
30 //! ## Event Filtering
31 //!
32 //! Event generation can be filtered by event kind. Recording all possible
33 //! events generates a lot of data, much of which is not needed for most kinds
34 //! of analysis. So, in order to keep overhead as low as possible for a given
35 //! use case, the `SelfProfiler` will only record the kinds of events that
36 //! pass the filter specified as a command line argument to the compiler.
37 //!
38 //!
39 //! ## `event_id` Assignment
40 //!
41 //! As far as `measureme` is concerned, `event_id`s are just strings. However,
42 //! it would incur too much overhead to generate and persist each `event_id`
43 //! string at the point where the event is recorded. In order to make this more
44 //! efficient `measureme` has two features:
45 //!
46 //! - Strings can share their content, so that re-occurring parts don't have to
47 //!   be copied over and over again. One allocates a string in `measureme` and
48 //!   gets back a `StringId`. This `StringId` is then used to refer to that
49 //!   string. `measureme` strings are actually DAGs of string components so that
50 //!   arbitrary sharing of substrings can be done efficiently. This is useful
51 //!   because `event_id`s contain lots of redundant text like query names or
52 //!   def-path components.
53 //!
54 //! - `StringId`s can be "virtual" which means that the client picks a numeric
55 //!   ID according to some application-specific scheme and can later make that
56 //!   ID be mapped to an actual string. This is used to cheaply generate
57 //!   `event_id`s while the events actually occur, causing little timing
58 //!   distortion, and then later map those `StringId`s, in bulk, to actual
59 //!   `event_id` strings. This way the largest part of the tracing overhead is
60 //!   localized to one contiguous chunk of time.
61 //!
62 //! How are these `event_id`s generated in the compiler? For things that occur
63 //! infrequently (e.g. "generic activities"), we just allocate the string the
64 //! first time it is used and then keep the `StringId` in a hash table. This
65 //! is implemented in `SelfProfiler::get_or_alloc_cached_string()`.
66 //!
67 //! For queries it gets more interesting: First we need a unique numeric ID for
68 //! each query invocation (the `QueryInvocationId`). This ID is used as the
69 //! virtual `StringId` we use as `event_id` for a given event. This ID has to
70 //! be available both when the query is executed and later, together with the
71 //! query key, when we allocate the actual `event_id` strings in bulk.
72 //!
73 //! We could make the compiler generate and keep track of such an ID for each
74 //! query invocation but luckily we already have something that fits all the
75 //! the requirements: the query's `DepNodeIndex`. So we use the numeric value
76 //! of the `DepNodeIndex` as `event_id` when recording the event and then,
77 //! just before the query context is dropped, we walk the entire query cache
78 //! (which stores the `DepNodeIndex` along with the query key for each
79 //! invocation) and allocate the corresponding strings together with a mapping
80 //! for `DepNodeIndex as StringId`.
81 //!
82 //! [mm]: https://github.com/rust-lang/measureme/
83
84 use crate::fx::FxHashMap;
85
86 use std::error::Error;
87 use std::fs;
88 use std::path::Path;
89 use std::process;
90 use std::sync::Arc;
91 use std::thread::ThreadId;
92 use std::time::{Duration, Instant};
93 use std::u32;
94
95 use measureme::{EventId, EventIdBuilder, SerializableString, StringId};
96 use parking_lot::RwLock;
97
98 /// MmapSerializatioSink is faster on macOS and Linux
99 /// but FileSerializationSink is faster on Windows
100 #[cfg(not(windows))]
101 type SerializationSink = measureme::MmapSerializationSink;
102 #[cfg(windows)]
103 type SerializationSink = measureme::FileSerializationSink;
104
105 type Profiler = measureme::Profiler<SerializationSink>;
106
107 #[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)]
108 pub enum ProfileCategory {
109     Parsing,
110     Expansion,
111     TypeChecking,
112     BorrowChecking,
113     Codegen,
114     Linking,
115     Other,
116 }
117
118 bitflags::bitflags! {
119     struct EventFilter: u32 {
120         const GENERIC_ACTIVITIES = 1 << 0;
121         const QUERY_PROVIDERS    = 1 << 1;
122         const QUERY_CACHE_HITS   = 1 << 2;
123         const QUERY_BLOCKED      = 1 << 3;
124         const INCR_CACHE_LOADS   = 1 << 4;
125
126         const QUERY_KEYS         = 1 << 5;
127
128         const DEFAULT = Self::GENERIC_ACTIVITIES.bits |
129                         Self::QUERY_PROVIDERS.bits |
130                         Self::QUERY_BLOCKED.bits |
131                         Self::INCR_CACHE_LOADS.bits;
132
133         // empty() and none() aren't const-fns unfortunately
134         const NONE = 0;
135         const ALL  = !Self::NONE.bits;
136     }
137 }
138
139 const EVENT_FILTERS_BY_NAME: &[(&str, EventFilter)] = &[
140     ("none", EventFilter::NONE),
141     ("all", EventFilter::ALL),
142     ("generic-activity", EventFilter::GENERIC_ACTIVITIES),
143     ("query-provider", EventFilter::QUERY_PROVIDERS),
144     ("query-cache-hit", EventFilter::QUERY_CACHE_HITS),
145     ("query-blocked", EventFilter::QUERY_BLOCKED),
146     ("incr-cache-load", EventFilter::INCR_CACHE_LOADS),
147     ("query-keys", EventFilter::QUERY_KEYS),
148 ];
149
150 fn thread_id_to_u32(tid: ThreadId) -> u32 {
151     unsafe { std::mem::transmute::<ThreadId, u64>(tid) as u32 }
152 }
153
154 /// Something that uniquely identifies a query invocation.
155 pub struct QueryInvocationId(pub u32);
156
157 /// A reference to the SelfProfiler. It can be cloned and sent across thread
158 /// boundaries at will.
159 #[derive(Clone)]
160 pub struct SelfProfilerRef {
161     // This field is `None` if self-profiling is disabled for the current
162     // compilation session.
163     profiler: Option<Arc<SelfProfiler>>,
164
165     // We store the filter mask directly in the reference because that doesn't
166     // cost anything and allows for filtering with checking if the profiler is
167     // actually enabled.
168     event_filter_mask: EventFilter,
169
170     // Print verbose generic activities to stdout
171     print_verbose_generic_activities: bool,
172
173     // Print extra verbose generic activities to stdout
174     print_extra_verbose_generic_activities: bool,
175 }
176
177 impl SelfProfilerRef {
178     pub fn new(
179         profiler: Option<Arc<SelfProfiler>>,
180         print_verbose_generic_activities: bool,
181         print_extra_verbose_generic_activities: bool,
182     ) -> SelfProfilerRef {
183         // If there is no SelfProfiler then the filter mask is set to NONE,
184         // ensuring that nothing ever tries to actually access it.
185         let event_filter_mask =
186             profiler.as_ref().map(|p| p.event_filter_mask).unwrap_or(EventFilter::NONE);
187
188         SelfProfilerRef {
189             profiler,
190             event_filter_mask,
191             print_verbose_generic_activities,
192             print_extra_verbose_generic_activities,
193         }
194     }
195
196     // This shim makes sure that calls only get executed if the filter mask
197     // lets them pass. It also contains some trickery to make sure that
198     // code is optimized for non-profiling compilation sessions, i.e. anything
199     // past the filter check is never inlined so it doesn't clutter the fast
200     // path.
201     #[inline(always)]
202     fn exec<F>(&self, event_filter: EventFilter, f: F) -> TimingGuard<'_>
203     where
204         F: for<'a> FnOnce(&'a SelfProfiler) -> TimingGuard<'a>,
205     {
206         #[inline(never)]
207         fn cold_call<F>(profiler_ref: &SelfProfilerRef, f: F) -> TimingGuard<'_>
208         where
209             F: for<'a> FnOnce(&'a SelfProfiler) -> TimingGuard<'a>,
210         {
211             let profiler = profiler_ref.profiler.as_ref().unwrap();
212             f(&**profiler)
213         }
214
215         if unlikely!(self.event_filter_mask.contains(event_filter)) {
216             cold_call(self, f)
217         } else {
218             TimingGuard::none()
219         }
220     }
221
222     /// Start profiling a verbose generic activity. Profiling continues until the
223     /// VerboseTimingGuard returned from this call is dropped. In addition to recording
224     /// a measureme event, "verbose" generic activities also print a timing entry to
225     /// stdout if the compiler is invoked with -Ztime or -Ztime-passes.
226     #[inline(always)]
227     pub fn verbose_generic_activity<'a>(
228         &'a self,
229         event_id: &'static str,
230     ) -> VerboseTimingGuard<'a> {
231         VerboseTimingGuard::start(
232             event_id,
233             self.print_verbose_generic_activities,
234             self.generic_activity(event_id),
235         )
236     }
237
238     /// Start profiling a extra verbose generic activity. Profiling continues until the
239     /// VerboseTimingGuard returned from this call is dropped. In addition to recording
240     /// a measureme event, "extra verbose" generic activities also print a timing entry to
241     /// stdout if the compiler is invoked with -Ztime-passes.
242     #[inline(always)]
243     pub fn extra_verbose_generic_activity<'a>(
244         &'a self,
245         event_id: &'a str,
246     ) -> VerboseTimingGuard<'a> {
247         // FIXME: This does not yet emit a measureme event
248         // because callers encode arguments into `event_id`.
249         VerboseTimingGuard::start(
250             event_id,
251             self.print_extra_verbose_generic_activities,
252             TimingGuard::none(),
253         )
254     }
255
256     /// Start profiling a generic activity. Profiling continues until the
257     /// TimingGuard returned from this call is dropped.
258     #[inline(always)]
259     pub fn generic_activity(&self, event_id: &'static str) -> TimingGuard<'_> {
260         self.exec(EventFilter::GENERIC_ACTIVITIES, |profiler| {
261             let event_id = profiler.get_or_alloc_cached_string(event_id);
262             let event_id = EventId::from_label(event_id);
263             TimingGuard::start(profiler, profiler.generic_activity_event_kind, event_id)
264         })
265     }
266
267     /// Start profiling a query provider. Profiling continues until the
268     /// TimingGuard returned from this call is dropped.
269     #[inline(always)]
270     pub fn query_provider(&self) -> TimingGuard<'_> {
271         self.exec(EventFilter::QUERY_PROVIDERS, |profiler| {
272             TimingGuard::start(profiler, profiler.query_event_kind, EventId::INVALID)
273         })
274     }
275
276     /// Record a query in-memory cache hit.
277     #[inline(always)]
278     pub fn query_cache_hit(&self, query_invocation_id: QueryInvocationId) {
279         self.instant_query_event(
280             |profiler| profiler.query_cache_hit_event_kind,
281             query_invocation_id,
282             EventFilter::QUERY_CACHE_HITS,
283         );
284     }
285
286     /// Start profiling a query being blocked on a concurrent execution.
287     /// Profiling continues until the TimingGuard returned from this call is
288     /// dropped.
289     #[inline(always)]
290     pub fn query_blocked(&self) -> TimingGuard<'_> {
291         self.exec(EventFilter::QUERY_BLOCKED, |profiler| {
292             TimingGuard::start(profiler, profiler.query_blocked_event_kind, EventId::INVALID)
293         })
294     }
295
296     /// Start profiling how long it takes to load a query result from the
297     /// incremental compilation on-disk cache. Profiling continues until the
298     /// TimingGuard returned from this call is dropped.
299     #[inline(always)]
300     pub fn incr_cache_loading(&self) -> TimingGuard<'_> {
301         self.exec(EventFilter::INCR_CACHE_LOADS, |profiler| {
302             TimingGuard::start(
303                 profiler,
304                 profiler.incremental_load_result_event_kind,
305                 EventId::INVALID,
306             )
307         })
308     }
309
310     #[inline(always)]
311     fn instant_query_event(
312         &self,
313         event_kind: fn(&SelfProfiler) -> StringId,
314         query_invocation_id: QueryInvocationId,
315         event_filter: EventFilter,
316     ) {
317         drop(self.exec(event_filter, |profiler| {
318             let event_id = StringId::new_virtual(query_invocation_id.0);
319             let thread_id = thread_id_to_u32(std::thread::current().id());
320
321             profiler.profiler.record_instant_event(
322                 event_kind(profiler),
323                 EventId::from_virtual(event_id),
324                 thread_id,
325             );
326
327             TimingGuard::none()
328         }));
329     }
330
331     pub fn with_profiler(&self, f: impl FnOnce(&SelfProfiler)) {
332         if let Some(profiler) = &self.profiler {
333             f(&profiler)
334         }
335     }
336
337     #[inline]
338     pub fn enabled(&self) -> bool {
339         self.profiler.is_some()
340     }
341 }
342
343 pub struct SelfProfiler {
344     profiler: Profiler,
345     event_filter_mask: EventFilter,
346
347     string_cache: RwLock<FxHashMap<&'static str, StringId>>,
348
349     query_event_kind: StringId,
350     generic_activity_event_kind: StringId,
351     incremental_load_result_event_kind: StringId,
352     query_blocked_event_kind: StringId,
353     query_cache_hit_event_kind: StringId,
354 }
355
356 impl SelfProfiler {
357     pub fn new(
358         output_directory: &Path,
359         crate_name: Option<&str>,
360         event_filters: &Option<Vec<String>>,
361     ) -> Result<SelfProfiler, Box<dyn Error>> {
362         fs::create_dir_all(output_directory)?;
363
364         let crate_name = crate_name.unwrap_or("unknown-crate");
365         let filename = format!("{}-{}.rustc_profile", crate_name, process::id());
366         let path = output_directory.join(&filename);
367         let profiler = Profiler::new(&path)?;
368
369         let query_event_kind = profiler.alloc_string("Query");
370         let generic_activity_event_kind = profiler.alloc_string("GenericActivity");
371         let incremental_load_result_event_kind = profiler.alloc_string("IncrementalLoadResult");
372         let query_blocked_event_kind = profiler.alloc_string("QueryBlocked");
373         let query_cache_hit_event_kind = profiler.alloc_string("QueryCacheHit");
374
375         let mut event_filter_mask = EventFilter::empty();
376
377         if let Some(ref event_filters) = *event_filters {
378             let mut unknown_events = vec![];
379             for item in event_filters {
380                 if let Some(&(_, mask)) =
381                     EVENT_FILTERS_BY_NAME.iter().find(|&(name, _)| name == item)
382                 {
383                     event_filter_mask |= mask;
384                 } else {
385                     unknown_events.push(item.clone());
386                 }
387             }
388
389             // Warn about any unknown event names
390             if unknown_events.len() > 0 {
391                 unknown_events.sort();
392                 unknown_events.dedup();
393
394                 warn!(
395                     "Unknown self-profiler events specified: {}. Available options are: {}.",
396                     unknown_events.join(", "),
397                     EVENT_FILTERS_BY_NAME
398                         .iter()
399                         .map(|&(name, _)| name.to_string())
400                         .collect::<Vec<_>>()
401                         .join(", ")
402                 );
403             }
404         } else {
405             event_filter_mask = EventFilter::DEFAULT;
406         }
407
408         Ok(SelfProfiler {
409             profiler,
410             event_filter_mask,
411             string_cache: RwLock::new(FxHashMap::default()),
412             query_event_kind,
413             generic_activity_event_kind,
414             incremental_load_result_event_kind,
415             query_blocked_event_kind,
416             query_cache_hit_event_kind,
417         })
418     }
419
420     /// Allocates a new string in the profiling data. Does not do any caching
421     /// or deduplication.
422     pub fn alloc_string<STR: SerializableString + ?Sized>(&self, s: &STR) -> StringId {
423         self.profiler.alloc_string(s)
424     }
425
426     /// Gets a `StringId` for the given string. This method makes sure that
427     /// any strings going through it will only be allocated once in the
428     /// profiling data.
429     pub fn get_or_alloc_cached_string(&self, s: &'static str) -> StringId {
430         // Only acquire a read-lock first since we assume that the string is
431         // already present in the common case.
432         {
433             let string_cache = self.string_cache.read();
434
435             if let Some(&id) = string_cache.get(s) {
436                 return id;
437             }
438         }
439
440         let mut string_cache = self.string_cache.write();
441         // Check if the string has already been added in the small time window
442         // between dropping the read lock and acquiring the write lock.
443         *string_cache.entry(s).or_insert_with(|| self.profiler.alloc_string(s))
444     }
445
446     pub fn map_query_invocation_id_to_string(&self, from: QueryInvocationId, to: StringId) {
447         let from = StringId::new_virtual(from.0);
448         self.profiler.map_virtual_to_concrete_string(from, to);
449     }
450
451     pub fn bulk_map_query_invocation_id_to_single_string<I>(&self, from: I, to: StringId)
452     where
453         I: Iterator<Item = QueryInvocationId> + ExactSizeIterator,
454     {
455         let from = from.map(|qid| StringId::new_virtual(qid.0));
456         self.profiler.bulk_map_virtual_to_single_concrete_string(from, to);
457     }
458
459     pub fn query_key_recording_enabled(&self) -> bool {
460         self.event_filter_mask.contains(EventFilter::QUERY_KEYS)
461     }
462
463     pub fn event_id_builder(&self) -> EventIdBuilder<'_, SerializationSink> {
464         EventIdBuilder::new(&self.profiler)
465     }
466 }
467
468 #[must_use]
469 pub struct TimingGuard<'a>(Option<measureme::TimingGuard<'a, SerializationSink>>);
470
471 impl<'a> TimingGuard<'a> {
472     #[inline]
473     pub fn start(
474         profiler: &'a SelfProfiler,
475         event_kind: StringId,
476         event_id: EventId,
477     ) -> TimingGuard<'a> {
478         let thread_id = thread_id_to_u32(std::thread::current().id());
479         let raw_profiler = &profiler.profiler;
480         let timing_guard =
481             raw_profiler.start_recording_interval_event(event_kind, event_id, thread_id);
482         TimingGuard(Some(timing_guard))
483     }
484
485     #[inline]
486     pub fn finish_with_query_invocation_id(self, query_invocation_id: QueryInvocationId) {
487         if let Some(guard) = self.0 {
488             let event_id = StringId::new_virtual(query_invocation_id.0);
489             let event_id = EventId::from_virtual(event_id);
490             guard.finish_with_override_event_id(event_id);
491         }
492     }
493
494     #[inline]
495     pub fn none() -> TimingGuard<'a> {
496         TimingGuard(None)
497     }
498
499     #[inline(always)]
500     pub fn run<R>(self, f: impl FnOnce() -> R) -> R {
501         let _timer = self;
502         f()
503     }
504 }
505
506 #[must_use]
507 pub struct VerboseTimingGuard<'a> {
508     event_id: &'a str,
509     start: Option<Instant>,
510     _guard: TimingGuard<'a>,
511 }
512
513 impl<'a> VerboseTimingGuard<'a> {
514     pub fn start(event_id: &'a str, verbose: bool, _guard: TimingGuard<'a>) -> Self {
515         VerboseTimingGuard {
516             event_id,
517             _guard,
518             start: if unlikely!(verbose) { Some(Instant::now()) } else { None },
519         }
520     }
521
522     #[inline(always)]
523     pub fn run<R>(self, f: impl FnOnce() -> R) -> R {
524         let _timer = self;
525         f()
526     }
527 }
528
529 impl Drop for VerboseTimingGuard<'_> {
530     fn drop(&mut self) {
531         self.start.map(|start| print_time_passes_entry(true, self.event_id, start.elapsed()));
532     }
533 }
534
535 pub fn print_time_passes_entry(do_it: bool, what: &str, dur: Duration) {
536     if !do_it {
537         return;
538     }
539
540     let mem_string = match get_resident() {
541         Some(n) => {
542             let mb = n as f64 / 1_000_000.0;
543             format!("; rss: {}MB", mb.round() as usize)
544         }
545         None => String::new(),
546     };
547     println!("time: {}{}\t{}", duration_to_secs_str(dur), mem_string, what);
548 }
549
550 // Hack up our own formatting for the duration to make it easier for scripts
551 // to parse (always use the same number of decimal places and the same unit).
552 pub fn duration_to_secs_str(dur: std::time::Duration) -> String {
553     const NANOS_PER_SEC: f64 = 1_000_000_000.0;
554     let secs = dur.as_secs() as f64 + dur.subsec_nanos() as f64 / NANOS_PER_SEC;
555
556     format!("{:.3}", secs)
557 }
558
559 // Memory reporting
560 #[cfg(unix)]
561 fn get_resident() -> Option<usize> {
562     let field = 1;
563     let contents = fs::read("/proc/self/statm").ok()?;
564     let contents = String::from_utf8(contents).ok()?;
565     let s = contents.split_whitespace().nth(field)?;
566     let npages = s.parse::<usize>().ok()?;
567     Some(npages * 4096)
568 }
569
570 #[cfg(windows)]
571 fn get_resident() -> Option<usize> {
572     use std::mem::{self, MaybeUninit};
573     use winapi::shared::minwindef::DWORD;
574     use winapi::um::processthreadsapi::GetCurrentProcess;
575     use winapi::um::psapi::{GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS};
576
577     let mut pmc = MaybeUninit::<PROCESS_MEMORY_COUNTERS>::uninit();
578     match unsafe {
579         GetProcessMemoryInfo(GetCurrentProcess(), pmc.as_mut_ptr(), mem::size_of_val(&pmc) as DWORD)
580     } {
581         0 => None,
582         _ => {
583             let pmc = unsafe { pmc.assume_init() };
584             Some(pmc.WorkingSetSize as usize)
585         }
586     }
587 }