|
| 1 | +//! # Support for collecting simple statistics |
| 2 | +//! |
| 3 | +//! Statistics are useful for collecting metrics from optimization passes, like |
| 4 | +//! the number of simplifications performed. To avoid introducing overhead, the |
| 5 | +//! collection of statistics is enabled only when rustc is compiled with |
| 6 | +//! debug-assertions. |
| 7 | +//! |
| 8 | +//! Statistics are static variables defined in the module they are used, and |
| 9 | +//! lazy registered in the global collector on the first use. Once registered, |
| 10 | +//! the collector will obtain their values at the end of compilation process |
| 11 | +//! when requested with -Zmir-opt-stats option. |
| 12 | +
|
| 13 | +use parking_lot::{const_mutex, Mutex}; |
| 14 | +use std::io::{self, stdout, Write as _}; |
| 15 | +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; |
| 16 | + |
| 17 | +static COLLECTOR: Collector = Collector::new(); |
| 18 | + |
| 19 | +/// Enables the collection of statistics. |
| 20 | +/// To be effective it has to be called before the first use of a statistics. |
| 21 | +pub fn try_enable() -> Result<(), ()> { |
| 22 | + COLLECTOR.try_enable() |
| 23 | +} |
| 24 | + |
| 25 | +/// Prints all statistics collected so far. |
| 26 | +pub fn print() { |
| 27 | + COLLECTOR.print(); |
| 28 | +} |
| 29 | + |
| 30 | +pub struct Statistic { |
| 31 | + category: &'static str, |
| 32 | + name: &'static str, |
| 33 | + initialized: AtomicBool, |
| 34 | + value: AtomicUsize, |
| 35 | +} |
| 36 | + |
| 37 | +struct Collector(Mutex<State>); |
| 38 | + |
| 39 | +struct State { |
| 40 | + enabled: bool, |
| 41 | + stats: Vec<&'static Statistic>, |
| 42 | +} |
| 43 | + |
| 44 | +#[derive(Eq, PartialEq, Ord, PartialOrd)] |
| 45 | +struct Snapshot { |
| 46 | + category: &'static str, |
| 47 | + name: &'static str, |
| 48 | + value: usize, |
| 49 | +} |
| 50 | + |
| 51 | +impl Statistic { |
| 52 | + pub const fn new(category: &'static str, name: &'static str) -> Self { |
| 53 | + Statistic { |
| 54 | + category, |
| 55 | + name, |
| 56 | + initialized: AtomicBool::new(false), |
| 57 | + value: AtomicUsize::new(0), |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + pub fn name(&self) -> &'static str { |
| 62 | + self.name |
| 63 | + } |
| 64 | + |
| 65 | + pub fn category(&self) -> &'static str { |
| 66 | + self.category.rsplit("::").next().unwrap() |
| 67 | + } |
| 68 | + |
| 69 | + #[inline] |
| 70 | + pub fn register(&'static self) { |
| 71 | + if cfg!(debug_assertions) { |
| 72 | + if !self.initialized.load(Ordering::Acquire) { |
| 73 | + COLLECTOR.register(self); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + #[inline] |
| 79 | + pub fn increment(&'static self, value: usize) { |
| 80 | + if cfg!(debug_assertions) { |
| 81 | + self.value.fetch_add(value, Ordering::Relaxed); |
| 82 | + self.register(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + #[inline] |
| 87 | + pub fn update_max(&'static self, value: usize) { |
| 88 | + if cfg!(debug_assertions) { |
| 89 | + self.value.fetch_max(value, Ordering::Relaxed); |
| 90 | + self.register(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + fn snapshot(&'static self) -> Snapshot { |
| 95 | + Snapshot { |
| 96 | + name: self.name(), |
| 97 | + category: self.category(), |
| 98 | + value: self.value.load(Ordering::Relaxed), |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl Collector { |
| 104 | + const fn new() -> Self { |
| 105 | + Collector(const_mutex(State { enabled: false, stats: Vec::new() })) |
| 106 | + } |
| 107 | + |
| 108 | + fn try_enable(&self) -> Result<(), ()> { |
| 109 | + if cfg!(debug_assertions) { |
| 110 | + self.0.lock().enabled = true; |
| 111 | + Ok(()) |
| 112 | + } else { |
| 113 | + Err(()) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + fn snapshot(&self) -> Vec<Snapshot> { |
| 118 | + self.0.lock().stats.iter().copied().map(Statistic::snapshot).collect() |
| 119 | + } |
| 120 | + |
| 121 | + fn register(&self, s: &'static Statistic) { |
| 122 | + let mut state = self.0.lock(); |
| 123 | + if !s.initialized.load(Ordering::Relaxed) { |
| 124 | + if state.enabled { |
| 125 | + state.stats.push(s); |
| 126 | + } |
| 127 | + s.initialized.store(true, Ordering::Release); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + fn print(&self) { |
| 132 | + let mut stats = self.snapshot(); |
| 133 | + stats.sort(); |
| 134 | + match self.write(&stats) { |
| 135 | + Ok(_) => {} |
| 136 | + Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {} |
| 137 | + Err(e) => panic!(e), |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + fn write(&self, stats: &[Snapshot]) -> io::Result<()> { |
| 142 | + let mut cat_width = 0; |
| 143 | + let mut val_width = 0; |
| 144 | + |
| 145 | + for s in stats { |
| 146 | + cat_width = cat_width.max(s.category.len()); |
| 147 | + val_width = val_width.max(s.value.to_string().len()); |
| 148 | + } |
| 149 | + |
| 150 | + let mut out = Vec::new(); |
| 151 | + for s in stats { |
| 152 | + write!( |
| 153 | + &mut out, |
| 154 | + "{val:val_width$} {cat:cat_width$} {name}\n", |
| 155 | + val = s.value, |
| 156 | + val_width = val_width, |
| 157 | + cat = s.category, |
| 158 | + cat_width = cat_width, |
| 159 | + name = s.name, |
| 160 | + )?; |
| 161 | + } |
| 162 | + |
| 163 | + stdout().write_all(&out) |
| 164 | + } |
| 165 | +} |
0 commit comments