|
1 | 1 | use std::time::Instant;
|
2 | 2 |
|
| 3 | +use rustc_data_structures::fx::FxHashSet; |
| 4 | +use rustc_data_structures::sync::Lock; |
| 5 | + |
3 | 6 | use crate::DiagCtxtHandle;
|
4 | 7 |
|
5 | 8 | /// A high-level section of the compilation process.
|
6 |
| -#[derive(Copy, Clone, Debug)] |
| 9 | +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] |
7 | 10 | pub enum TimingSection {
|
| 11 | + /// Time spent doing codegen. |
| 12 | + Codegen, |
8 | 13 | /// Time spent linking.
|
9 | 14 | Linking,
|
10 | 15 | }
|
@@ -36,23 +41,59 @@ pub struct TimingSectionHandler {
|
36 | 41 | /// Time when the compilation session started.
|
37 | 42 | /// If `None`, timing is disabled.
|
38 | 43 | origin: Option<Instant>,
|
| 44 | + /// Sanity check to ensure that we open and close sections correctly. |
| 45 | + opened_sections: Lock<FxHashSet<TimingSection>>, |
39 | 46 | }
|
40 | 47 |
|
41 | 48 | impl TimingSectionHandler {
|
42 | 49 | pub fn new(enabled: bool) -> Self {
|
43 | 50 | let origin = if enabled { Some(Instant::now()) } else { None };
|
44 |
| - Self { origin } |
| 51 | + Self { origin, opened_sections: Lock::new(FxHashSet::default()) } |
45 | 52 | }
|
46 | 53 |
|
47 | 54 | /// Returns a RAII guard that will immediately emit a start the provided section, and then emit
|
48 | 55 | /// its end when it is dropped.
|
49 |
| - pub fn start_section<'a>( |
| 56 | + pub fn section_guard<'a>( |
50 | 57 | &self,
|
51 | 58 | diag_ctxt: DiagCtxtHandle<'a>,
|
52 | 59 | section: TimingSection,
|
53 | 60 | ) -> TimingSectionGuard<'a> {
|
| 61 | + if self.is_enabled() && self.opened_sections.borrow().contains(§ion) { |
| 62 | + diag_ctxt |
| 63 | + .bug(format!("Section `{section:?}` was started again before it was finished")); |
| 64 | + } |
| 65 | + |
54 | 66 | TimingSectionGuard::create(diag_ctxt, section, self.origin)
|
55 | 67 | }
|
| 68 | + |
| 69 | + /// Start the provided section. |
| 70 | + pub fn start_section(&self, diag_ctxt: DiagCtxtHandle<'_>, section: TimingSection) { |
| 71 | + if let Some(origin) = self.origin { |
| 72 | + let mut opened = self.opened_sections.borrow_mut(); |
| 73 | + if !opened.insert(section) { |
| 74 | + diag_ctxt |
| 75 | + .bug(format!("Section `{section:?}` was started again before it was finished")); |
| 76 | + } |
| 77 | + |
| 78 | + diag_ctxt.emit_timing_section_start(TimingRecord::from_origin(origin, section)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// End the provided section. |
| 83 | + pub fn end_section(&self, diag_ctxt: DiagCtxtHandle<'_>, section: TimingSection) { |
| 84 | + if let Some(origin) = self.origin { |
| 85 | + let mut opened = self.opened_sections.borrow_mut(); |
| 86 | + if !opened.remove(§ion) { |
| 87 | + diag_ctxt.bug(format!("Section `{section:?}` was ended before being started")); |
| 88 | + } |
| 89 | + |
| 90 | + diag_ctxt.emit_timing_section_end(TimingRecord::from_origin(origin, section)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + fn is_enabled(&self) -> bool { |
| 95 | + self.origin.is_some() |
| 96 | + } |
56 | 97 | }
|
57 | 98 |
|
58 | 99 | /// RAII wrapper for starting and ending section timings.
|
|
0 commit comments