@@ -59,14 +59,50 @@ pub fn time<T, U, F>(do_it: bool, what: &str, u: U, f: F) -> T where
59
59
const NANOS_PER_SEC : f64 = 1_000_000_000.0 ;
60
60
let secs = dur. secs ( ) as f64 ;
61
61
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) ;
64
72
65
73
DEPTH . with ( |slot| slot. set ( old) ) ;
66
74
67
75
rv
68
76
}
69
77
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
+
70
106
pub fn indent < R , F > ( op : F ) -> R where
71
107
R : Debug ,
72
108
F : FnOnce ( ) -> R ,
0 commit comments