Skip to content

Commit 7713e14

Browse files
committed
Report memory use in time-passes
Reports the resident set size after each pass (linux-only).
1 parent fdf219d commit 7713e14

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#![feature(vec_push_all)]
6363
#![feature(wrapping)]
6464
#![feature(cell_extras)]
65+
#![feature(page_size)]
6566
#![cfg_attr(test, feature(test))]
6667

6768
#![allow(trivial_casts)]

src/librustc/util/common.rs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,50 @@ pub fn time<T, U, F>(do_it: bool, what: &str, u: U, f: F) -> T where
5959
const NANOS_PER_SEC: f64 = 1_000_000_000.0;
6060
let secs = dur.secs() as f64;
6161
let secs = secs + dur.extra_nanos() as f64 / NANOS_PER_SEC;
62-
println!("{}time: {:.3} \t{}", repeat(" ").take(old).collect::<String>(),
63-
secs, what);
62+
63+
let mem_string = match get_resident() {
64+
Some(n) => {
65+
let mb = n as f64 / 1_000_000.0;
66+
format!("; rss: {}MB", mb.round() as usize)
67+
}
68+
None => "".to_owned(),
69+
};
70+
println!("{}time: {:.3}{}\t{}", repeat(" ").take(old).collect::<String>(),
71+
secs, mem_string, what);
6472

6573
DEPTH.with(|slot| slot.set(old));
6674

6775
rv
6876
}
6977

78+
// Memory reporting
79+
fn get_resident() -> Option<usize> {
80+
if cfg!(unix) {
81+
get_proc_self_statm_field(1)
82+
} else {
83+
None
84+
}
85+
}
86+
87+
// Like std::macros::try!, but for Option<>.
88+
macro_rules! option_try(
89+
($e:expr) => (match $e { Some(e) => e, None => return None })
90+
);
91+
92+
fn get_proc_self_statm_field(field: usize) -> Option<usize> {
93+
use std::fs::File;
94+
use std::io::Read;
95+
96+
assert!(cfg!(unix));
97+
98+
let mut f = option_try!(File::open("/proc/self/statm").ok());
99+
let mut contents = String::new();
100+
option_try!(f.read_to_string(&mut contents).ok());
101+
let s = option_try!(contents.split_whitespace().nth(field));
102+
let npages = option_try!(s.parse::<usize>().ok());
103+
Some(npages * ::std::env::page_size())
104+
}
105+
70106
pub fn indent<R, F>(op: F) -> R where
71107
R: Debug,
72108
F: FnOnce() -> R,

0 commit comments

Comments
 (0)