1 #![allow(non_camel_case_types)]
3 use rustc_data_structures::sync::Lock;
7 use std::time::{Duration, Instant};
9 use crate::session::Session;
10 use rustc_span::symbol::{sym, Symbol};
15 // The name of the associated type for `Fn` return types.
16 pub const FN_OUTPUT_NAME: Symbol = sym::Output;
18 pub use errors::ErrorReported;
20 thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0));
22 #[allow(nonstandard_style)]
23 #[derive(Clone, Debug, PartialEq, Eq)]
25 pub query: &'static str,
26 pub msg: Option<String>,
29 /// Read the current depth of `time()` calls. This is used to
30 /// encourage indentation across threads.
31 pub fn time_depth() -> usize {
32 TIME_DEPTH.with(|slot| slot.get())
35 /// Sets the current depth of `time()` calls. The idea is to call
36 /// `set_time_depth()` with the result from `time_depth()` in the
38 pub fn set_time_depth(depth: usize) {
39 TIME_DEPTH.with(|slot| slot.set(depth));
42 pub fn time<T, F>(sess: &Session, what: &str, f: F) -> T
46 time_ext(sess.time_passes(), what, f)
49 pub fn time_ext<T, F>(do_it: bool, what: &str, f: F) -> T
57 let old = TIME_DEPTH.with(|slot| {
63 let start = Instant::now();
65 let dur = start.elapsed();
67 print_time_passes_entry(true, what, dur);
69 TIME_DEPTH.with(|slot| slot.set(old));
74 pub fn print_time_passes_entry(do_it: bool, what: &str, dur: Duration) {
79 let indentation = TIME_DEPTH.with(|slot| slot.get());
81 let mem_string = match get_resident() {
83 let mb = n as f64 / 1_000_000.0;
84 format!("; rss: {}MB", mb.round() as usize)
86 None => String::new(),
90 " ".repeat(indentation),
91 duration_to_secs_str(dur),
97 pub use rustc_session::utils::duration_to_secs_str;
99 pub fn to_readable_str(mut val: usize) -> String {
100 let mut groups = vec![];
102 let group = val % 1000;
107 groups.push(group.to_string());
110 groups.push(format!("{:03}", group));
119 pub fn record_time<T, F>(accu: &Lock<Duration>, f: F) -> T
123 let start = Instant::now();
125 let duration = start.elapsed();
126 let mut accu = accu.lock();
127 *accu = *accu + duration;
133 fn get_resident() -> Option<usize> {
137 let contents = fs::read("/proc/self/statm").ok()?;
138 let contents = String::from_utf8(contents).ok()?;
139 let s = contents.split_whitespace().nth(field)?;
140 let npages = s.parse::<usize>().ok()?;
145 fn get_resident() -> Option<usize> {
148 type HANDLE = *mut u8;
152 #[allow(non_snake_case)]
153 struct PROCESS_MEMORY_COUNTERS {
155 PageFaultCount: DWORD,
156 PeakWorkingSetSize: size_t,
157 WorkingSetSize: size_t,
158 QuotaPeakPagedPoolUsage: size_t,
159 QuotaPagedPoolUsage: size_t,
160 QuotaPeakNonPagedPoolUsage: size_t,
161 QuotaNonPagedPoolUsage: size_t,
162 PagefileUsage: size_t,
163 PeakPagefileUsage: size_t,
165 type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
166 #[link(name = "psapi")]
168 fn GetCurrentProcess() -> HANDLE;
169 fn GetProcessMemoryInfo(
171 ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
175 let mut pmc: PROCESS_MEMORY_COUNTERS = unsafe { mem::zeroed() };
176 pmc.cb = mem::size_of_val(&pmc) as DWORD;
177 match unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb) } {
179 _ => Some(pmc.WorkingSetSize as usize),
183 pub fn indent<R, F>(op: F) -> R
188 // Use in conjunction with the log post-processor like `src/etc/indenter`
189 // to make debug output more readable.
192 debug!("<< (Result = {:?})", r);
196 pub struct Indenter {
197 _cannot_construct_outside_of_this_module: (),
200 impl Drop for Indenter {
206 pub fn indenter() -> Indenter {
208 Indenter { _cannot_construct_outside_of_this_module: () }