@@ -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+
70106pub fn indent < R , F > ( op : F ) -> R where
71107 R : Debug ,
72108 F : FnOnce ( ) -> R ,
0 commit comments