@@ -555,13 +555,16 @@ impl<'a> TimingGuard<'a> {
555555
556556#[ must_use]
557557pub struct VerboseTimingGuard < ' a > {
558- start_and_message : Option < ( Instant , String ) > ,
558+ start_and_message : Option < ( Instant , Option < usize > , String ) > ,
559559 _guard : TimingGuard < ' a > ,
560560}
561561
562562impl < ' a > VerboseTimingGuard < ' a > {
563563 pub fn start ( message : Option < String > , _guard : TimingGuard < ' a > ) -> Self {
564- VerboseTimingGuard { _guard, start_and_message : message. map ( |msg| ( Instant :: now ( ) , msg) ) }
564+ VerboseTimingGuard {
565+ _guard,
566+ start_and_message : message. map ( |msg| ( Instant :: now ( ) , get_resident_set_size ( ) , msg) ) ,
567+ }
565568 }
566569
567570 #[ inline( always) ]
@@ -573,25 +576,42 @@ impl<'a> VerboseTimingGuard<'a> {
573576
574577impl Drop for VerboseTimingGuard < ' _ > {
575578 fn drop ( & mut self ) {
576- if let Some ( ( start, ref message) ) = self . start_and_message {
577- print_time_passes_entry ( true , & message[ ..] , start. elapsed ( ) ) ;
579+ if let Some ( ( start_time, start_rss, ref message) ) = self . start_and_message {
580+ let end_rss = get_resident_set_size ( ) ;
581+ print_time_passes_entry ( & message[ ..] , start_time. elapsed ( ) , start_rss, end_rss) ;
578582 }
579583 }
580584}
581585
582- pub fn print_time_passes_entry ( do_it : bool , what : & str , dur : Duration ) {
583- if !do_it {
584- return ;
585- }
586-
587- let mem_string = match get_resident ( ) {
588- Some ( n) => {
589- let mb = n as f64 / 1_000_000.0 ;
590- format ! ( "; rss: {}MB" , mb. round( ) as usize )
586+ pub fn print_time_passes_entry (
587+ what : & str ,
588+ dur : Duration ,
589+ start_rss : Option < usize > ,
590+ end_rss : Option < usize > ,
591+ ) {
592+ let rss_to_mb = |rss| ( rss as f64 / 1_000_000.0 ) . round ( ) as usize ;
593+
594+ let mem_string = match ( start_rss, end_rss) {
595+ ( Some ( start_rss) , Some ( end_rss) ) => {
596+ // It's tempting to add the change in RSS from start to end, but its somewhat confusing
597+ // and misleading when looking at time-passes output. Consider two adjacent entries:
598+ //
599+ // time: 10.000; rss start: 1000MB, end: 1000MB, change: 0MB pass1
600+ // time: 5.000; rss start: 2000MB, end: 2000MB, change: 0MB pass2
601+ //
602+ // If you're looking for jumps in RSS based on the change column, you miss the fact
603+ // that a 1GB jump happened between pass1 and pass2 (supposing pass1 and pass2 actually
604+ // occur sequentially and pass1 isn't just nested within pass2). It's easy to imagine
605+ // someone missing this or being confused by the fact that the change is zero.
606+
607+ format ! ( "; rss: {:>5}MB -> {:>5}MB" , rss_to_mb( start_rss) , rss_to_mb( end_rss) )
591608 }
592- None => String :: new ( ) ,
609+ ( Some ( start_rss) , None ) => format ! ( "; rss start: {:>5}MB" , rss_to_mb( start_rss) ) ,
610+ ( None , Some ( end_rss) ) => format ! ( "; rss end: {:5>}MB" , rss_to_mb( end_rss) ) ,
611+ ( None , None ) => String :: new ( ) ,
593612 } ;
594- println ! ( "time: {}{}\t {}" , duration_to_secs_str( dur) , mem_string, what) ;
613+
614+ println ! ( "time: {:>7}{}\t {}" , duration_to_secs_str( dur) , mem_string, what) ;
595615}
596616
597617// Hack up our own formatting for the duration to make it easier for scripts
@@ -603,7 +623,7 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String {
603623// Memory reporting
604624cfg_if ! {
605625 if #[ cfg( windows) ] {
606- fn get_resident ( ) -> Option <usize > {
626+ pub fn get_resident_set_size ( ) -> Option <usize > {
607627 use std:: mem:: { self , MaybeUninit } ;
608628 use winapi:: shared:: minwindef:: DWORD ;
609629 use winapi:: um:: processthreadsapi:: GetCurrentProcess ;
@@ -621,7 +641,7 @@ cfg_if! {
621641 }
622642 }
623643 } else if #[ cfg( unix) ] {
624- fn get_resident ( ) -> Option <usize > {
644+ pub fn get_resident_set_size ( ) -> Option <usize > {
625645 let field = 1 ;
626646 let contents = fs:: read( "/proc/self/statm" ) . ok( ) ?;
627647 let contents = String :: from_utf8( contents) . ok( ) ?;
@@ -630,7 +650,7 @@ cfg_if! {
630650 Some ( npages * 4096 )
631651 }
632652 } else {
633- fn get_resident ( ) -> Option <usize > {
653+ pub fn get_resident_set_size ( ) -> Option <usize > {
634654 None
635655 }
636656 }
0 commit comments