1 #![allow(non_camel_case_types)]
3 use rustc_data_structures::{fx::FxHashMap, sync::Lock};
5 use std::cell::{RefCell, Cell};
10 use std::time::{Duration, Instant};
12 use std::sync::mpsc::{Sender};
13 use syntax_pos::{SpanData};
14 use syntax::symbol::{Symbol, sym};
15 use rustc_macros::HashStable;
16 use crate::ty::TyCtxt;
17 use crate::dep_graph::{DepNode};
19 use crate::session::Session;
21 // The name of the associated type for `Fn` return types
22 pub const FN_OUTPUT_NAME: Symbol = sym::Output;
24 // Useful type to use with `Result<>` indicate that an error has already
25 // been reported to the user, so no need to continue checking.
26 #[derive(Clone, Copy, Debug, RustcEncodable, RustcDecodable, HashStable)]
27 pub struct ErrorReported;
29 thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0));
32 static ref DEFAULT_HOOK: Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + 'static> = {
33 let hook = panic::take_hook();
34 panic::set_hook(Box::new(panic_hook));
39 fn panic_hook(info: &panic::PanicInfo<'_>) {
40 (*DEFAULT_HOOK)(info);
42 let backtrace = env::var_os("RUST_BACKTRACE").map(|x| &x != "0").unwrap_or(false);
45 TyCtxt::try_print_query_stack();
50 if env::var("RUSTC_BREAK_ON_ICE").is_ok() {
54 // Trigger a debugger if we crashed during bootstrap
60 pub fn install_panic_hook() {
61 lazy_static::initialize(&DEFAULT_HOOK);
64 /// Parameters to the `Dump` variant of type `ProfileQueriesMsg`.
65 #[derive(Clone,Debug)]
66 pub struct ProfQDumpParams {
67 /// A base path for the files we will dump.
69 /// To ensure that the compiler waits for us to finish our dumps.
71 /// Toggle dumping a log file with every `ProfileQueriesMsg`.
72 pub dump_profq_msg_log:bool,
75 #[allow(nonstandard_style)]
76 #[derive(Clone, Debug, PartialEq, Eq)]
78 pub query: &'static str,
79 pub msg: Option<String>,
82 /// A sequence of these messages induce a trace of query-based incremental compilation.
83 /// FIXME(matthewhammer): Determine whether we should include cycle detection here or not.
84 #[derive(Clone,Debug)]
85 pub enum ProfileQueriesMsg {
86 /// begin a timed pass
90 /// begin a task (see dep_graph::graph::with_task)
95 /// can't use `Span` because queries are sent to other thread
96 QueryBegin(SpanData, QueryMsg),
97 /// query is satisfied by using an already-known value for the given key
99 /// query requires running a provider; providers may nest, permitting queries to nest.
101 /// query is satisfied by a provider terminating with a value
103 /// dump a record of the queries to the given path
104 Dump(ProfQDumpParams),
105 /// halt the profiling/monitoring background thread
109 /// If enabled, send a message to the profile-queries thread
110 pub fn profq_msg(sess: &Session, msg: ProfileQueriesMsg) {
111 if let Some(s) = sess.profile_channel.borrow().as_ref() {
118 /// Set channel for profile queries channel
119 pub fn profq_set_chan(sess: &Session, s: Sender<ProfileQueriesMsg>) -> bool {
120 let mut channel = sess.profile_channel.borrow_mut();
121 if channel.is_none() {
129 /// Read the current depth of `time()` calls. This is used to
130 /// encourage indentation across threads.
131 pub fn time_depth() -> usize {
132 TIME_DEPTH.with(|slot| slot.get())
135 /// Sets the current depth of `time()` calls. The idea is to call
136 /// `set_time_depth()` with the result from `time_depth()` in the
138 pub fn set_time_depth(depth: usize) {
139 TIME_DEPTH.with(|slot| slot.set(depth));
142 pub fn time<T, F>(sess: &Session, what: &str, f: F) -> T where
145 time_ext(sess.time_passes(), Some(sess), what, f)
148 pub fn time_ext<T, F>(do_it: bool, sess: Option<&Session>, what: &str, f: F) -> T where
151 if !do_it { return f(); }
153 let old = TIME_DEPTH.with(|slot| {
159 if let Some(sess) = sess {
160 if cfg!(debug_assertions) {
161 profq_msg(sess, ProfileQueriesMsg::TimeBegin(what.to_string()))
164 let start = Instant::now();
166 let dur = start.elapsed();
167 if let Some(sess) = sess {
168 if cfg!(debug_assertions) {
169 profq_msg(sess, ProfileQueriesMsg::TimeEnd)
173 print_time_passes_entry_internal(what, dur);
175 TIME_DEPTH.with(|slot| slot.set(old));
180 pub fn print_time_passes_entry(do_it: bool, what: &str, dur: Duration) {
185 let old = TIME_DEPTH.with(|slot| {
191 print_time_passes_entry_internal(what, dur);
193 TIME_DEPTH.with(|slot| slot.set(old));
196 fn print_time_passes_entry_internal(what: &str, dur: Duration) {
197 let indentation = TIME_DEPTH.with(|slot| slot.get());
199 let mem_string = match get_resident() {
201 let mb = n as f64 / 1_000_000.0;
202 format!("; rss: {}MB", mb.round() as usize)
204 None => String::new(),
206 println!("{}time: {}{}\t{}",
207 " ".repeat(indentation),
208 duration_to_secs_str(dur),
213 // Hack up our own formatting for the duration to make it easier for scripts
214 // to parse (always use the same number of decimal places and the same unit).
215 pub fn duration_to_secs_str(dur: Duration) -> String {
216 const NANOS_PER_SEC: f64 = 1_000_000_000.0;
217 let secs = dur.as_secs() as f64 +
218 dur.subsec_nanos() as f64 / NANOS_PER_SEC;
220 format!("{:.3}", secs)
223 pub fn to_readable_str(mut val: usize) -> String {
224 let mut groups = vec![];
226 let group = val % 1000;
231 groups.push(group.to_string());
234 groups.push(format!("{:03}", group));
243 pub fn record_time<T, F>(accu: &Lock<Duration>, f: F) -> T where
246 let start = Instant::now();
248 let duration = start.elapsed();
249 let mut accu = accu.lock();
250 *accu = *accu + duration;
256 fn get_resident() -> Option<usize> {
260 let contents = fs::read("/proc/self/statm").ok()?;
261 let contents = String::from_utf8(contents).ok()?;
262 let s = contents.split_whitespace().nth(field)?;
263 let npages = s.parse::<usize>().ok()?;
268 fn get_resident() -> Option<usize> {
271 type HANDLE = *mut u8;
275 #[allow(non_snake_case)]
276 struct PROCESS_MEMORY_COUNTERS {
278 PageFaultCount: DWORD,
279 PeakWorkingSetSize: size_t,
280 WorkingSetSize: size_t,
281 QuotaPeakPagedPoolUsage: size_t,
282 QuotaPagedPoolUsage: size_t,
283 QuotaPeakNonPagedPoolUsage: size_t,
284 QuotaNonPagedPoolUsage: size_t,
285 PagefileUsage: size_t,
286 PeakPagefileUsage: size_t,
288 type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
289 #[link(name = "psapi")]
291 fn GetCurrentProcess() -> HANDLE;
292 fn GetProcessMemoryInfo(Process: HANDLE,
293 ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
296 let mut pmc: PROCESS_MEMORY_COUNTERS = unsafe { mem::zeroed() };
297 pmc.cb = mem::size_of_val(&pmc) as DWORD;
298 match unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb) } {
300 _ => Some(pmc.WorkingSetSize as usize),
304 pub fn indent<R, F>(op: F) -> R where
308 // Use in conjunction with the log post-processor like `src/etc/indenter`
309 // to make debug output more readable.
312 debug!("<< (Result = {:?})", r);
316 pub struct Indenter {
317 _cannot_construct_outside_of_this_module: (),
320 impl Drop for Indenter {
321 fn drop(&mut self) { debug!("<<"); }
324 pub fn indenter() -> Indenter {
326 Indenter { _cannot_construct_outside_of_this_module: () }
329 pub trait MemoizationMap {
333 /// If `key` is present in the map, return the value,
334 /// otherwise invoke `op` and store the value in the map.
336 /// N.B., if the receiver is a `DepTrackingMap`, special care is
337 /// needed in the `op` to ensure that the correct edges are
338 /// added into the dep graph. See the `DepTrackingMap` impl for
340 fn memoize<OP>(&self, key: Self::Key, op: OP) -> Self::Value
341 where OP: FnOnce() -> Self::Value;
344 impl<K, V> MemoizationMap for RefCell<FxHashMap<K,V>>
345 where K: Hash+Eq+Clone, V: Clone
350 fn memoize<OP>(&self, key: K, op: OP) -> V
351 where OP: FnOnce() -> V
353 let result = self.borrow().get(&key).cloned();
355 Some(result) => result,
358 self.borrow_mut().insert(key, result.clone());
366 fn test_to_readable_str() {
367 assert_eq!("0", to_readable_str(0));
368 assert_eq!("1", to_readable_str(1));
369 assert_eq!("99", to_readable_str(99));
370 assert_eq!("999", to_readable_str(999));
371 assert_eq!("1_000", to_readable_str(1_000));
372 assert_eq!("1_001", to_readable_str(1_001));
373 assert_eq!("999_999", to_readable_str(999_999));
374 assert_eq!("1_000_000", to_readable_str(1_000_000));
375 assert_eq!("1_234_567", to_readable_str(1_234_567));