1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(non_camel_case_types)]
13 use std::cell::{RefCell, Cell};
14 use std::collections::HashMap;
15 use std::ffi::CString;
17 use std::hash::{Hash, BuildHasher};
18 use std::iter::repeat;
20 use std::time::{Duration, Instant};
22 // The name of the associated type for `Fn` return types
23 pub const FN_OUTPUT_NAME: &'static str = "Output";
25 // Useful type to use with `Result<>` indicate that an error has already
26 // been reported to the user, so no need to continue checking.
27 #[derive(Clone, Copy, Debug)]
28 pub struct ErrorReported;
30 thread_local!(static TIME_DEPTH: Cell<usize> = Cell::new(0));
32 /// Read the current depth of `time()` calls. This is used to
33 /// encourage indentation across threads.
34 pub fn time_depth() -> usize {
35 TIME_DEPTH.with(|slot| slot.get())
38 /// Set the current depth of `time()` calls. The idea is to call
39 /// `set_time_depth()` with the result from `time_depth()` in the
41 pub fn set_time_depth(depth: usize) {
42 TIME_DEPTH.with(|slot| slot.set(depth));
45 pub fn time<T, F>(do_it: bool, what: &str, f: F) -> T where
48 if !do_it { return f(); }
50 let old = TIME_DEPTH.with(|slot| {
56 let start = Instant::now();
58 let dur = start.elapsed();
60 let mem_string = match get_resident() {
62 let mb = n as f64 / 1_000_000.0;
63 format!("; rss: {}MB", mb.round() as usize)
65 None => "".to_owned(),
67 println!("{}time: {}{}\t{}",
68 repeat(" ").take(old).collect::<String>(),
69 duration_to_secs_str(dur),
73 TIME_DEPTH.with(|slot| slot.set(old));
78 // Hack up our own formatting for the duration to make it easier for scripts
79 // to parse (always use the same number of decimal places and the same unit).
80 pub fn duration_to_secs_str(dur: Duration) -> String {
81 const NANOS_PER_SEC: f64 = 1_000_000_000.0;
82 let secs = dur.as_secs() as f64 +
83 dur.subsec_nanos() as f64 / NANOS_PER_SEC;
85 format!("{:.3}", secs)
88 pub fn to_readable_str(mut val: usize) -> String {
89 let mut groups = vec![];
91 let group = val % 1000;
96 groups.push(format!("{}", group));
99 groups.push(format!("{:03}", group));
108 pub fn record_time<T, F>(accu: &Cell<Duration>, f: F) -> T where
111 let start = Instant::now();
113 let duration = start.elapsed();
114 accu.set(duration + accu.get());
118 // Like std::macros::try!, but for Option<>.
119 macro_rules! option_try(
120 ($e:expr) => (match $e { Some(e) => e, None => return None })
125 fn get_resident() -> Option<usize> {
130 let mut f = option_try!(File::open("/proc/self/statm").ok());
131 let mut contents = String::new();
132 option_try!(f.read_to_string(&mut contents).ok());
133 let s = option_try!(contents.split_whitespace().nth(field));
134 let npages = option_try!(s.parse::<usize>().ok());
139 fn get_resident() -> Option<usize> {
142 type HANDLE = *mut u8;
146 #[allow(non_snake_case)]
147 struct PROCESS_MEMORY_COUNTERS {
149 PageFaultCount: DWORD,
150 PeakWorkingSetSize: size_t,
151 WorkingSetSize: size_t,
152 QuotaPeakPagedPoolUsage: size_t,
153 QuotaPagedPoolUsage: size_t,
154 QuotaPeakNonPagedPoolUsage: size_t,
155 QuotaNonPagedPoolUsage: size_t,
156 PagefileUsage: size_t,
157 PeakPagefileUsage: size_t,
159 type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
160 #[link(name = "psapi")]
162 fn GetCurrentProcess() -> HANDLE;
163 fn GetProcessMemoryInfo(Process: HANDLE,
164 ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
167 let mut pmc: PROCESS_MEMORY_COUNTERS = unsafe { mem::zeroed() };
168 pmc.cb = mem::size_of_val(&pmc) as DWORD;
169 match unsafe { GetProcessMemoryInfo(GetCurrentProcess(), &mut pmc, pmc.cb) } {
171 _ => Some(pmc.WorkingSetSize as usize),
175 pub fn indent<R, F>(op: F) -> R where
179 // Use in conjunction with the log post-processor like `src/etc/indenter`
180 // to make debug output more readable.
183 debug!("<< (Result = {:?})", r);
187 pub struct Indenter {
188 _cannot_construct_outside_of_this_module: (),
191 impl Drop for Indenter {
192 fn drop(&mut self) { debug!("<<"); }
195 pub fn indenter() -> Indenter {
197 Indenter { _cannot_construct_outside_of_this_module: () }
200 pub trait MemoizationMap {
204 /// If `key` is present in the map, return the valuee,
205 /// otherwise invoke `op` and store the value in the map.
207 /// NB: if the receiver is a `DepTrackingMap`, special care is
208 /// needed in the `op` to ensure that the correct edges are
209 /// added into the dep graph. See the `DepTrackingMap` impl for
211 fn memoize<OP>(&self, key: Self::Key, op: OP) -> Self::Value
212 where OP: FnOnce() -> Self::Value;
215 impl<K, V, S> MemoizationMap for RefCell<HashMap<K,V,S>>
216 where K: Hash+Eq+Clone, V: Clone, S: BuildHasher
221 fn memoize<OP>(&self, key: K, op: OP) -> V
222 where OP: FnOnce() -> V
224 let result = self.borrow().get(&key).cloned();
226 Some(result) => result,
229 self.borrow_mut().insert(key, result.clone());
237 pub fn path2cstr(p: &Path) -> CString {
238 use std::os::unix::prelude::*;
240 let p: &OsStr = p.as_ref();
241 CString::new(p.as_bytes()).unwrap()
244 pub fn path2cstr(p: &Path) -> CString {
245 CString::new(p.to_str().unwrap()).unwrap()
250 fn test_to_readable_str() {
251 assert_eq!("0", to_readable_str(0));
252 assert_eq!("1", to_readable_str(1));
253 assert_eq!("99", to_readable_str(99));
254 assert_eq!("999", to_readable_str(999));
255 assert_eq!("1_000", to_readable_str(1_000));
256 assert_eq!("1_001", to_readable_str(1_001));
257 assert_eq!("999_999", to_readable_str(999_999));
258 assert_eq!("1_000_000", to_readable_str(1_000_000));
259 assert_eq!("1_234_567", to_readable_str(1_234_567));