@@ -88,7 +88,7 @@ pub type Label = &'static str;
8888pub fn profile ( label : Label ) -> Profiler {
8989 assert ! ( !label. is_empty( ) ) ;
9090 if !PROFILING_ENABLED . load ( Ordering :: Relaxed ) {
91- return Profiler { label : None } ;
91+ return Profiler { label : None , detail : None } ;
9292 }
9393
9494 PROFILE_STACK . with ( |stack| {
@@ -101,15 +101,15 @@ pub fn profile(label: Label) -> Profiler {
101101 } ;
102102 }
103103 if stack. starts . len ( ) > stack. filter_data . depth {
104- return Profiler { label : None } ;
104+ return Profiler { label : None , detail : None } ;
105105 }
106106 let allowed = & stack. filter_data . allowed ;
107107 if stack. starts . is_empty ( ) && !allowed. is_empty ( ) && !allowed. contains ( label) {
108- return Profiler { label : None } ;
108+ return Profiler { label : None , detail : None } ;
109109 }
110110
111111 stack. starts . push ( Instant :: now ( ) ) ;
112- Profiler { label : Some ( label) }
112+ Profiler { label : Some ( label) , detail : None }
113113 } )
114114}
115115
@@ -130,6 +130,16 @@ pub fn print_time(label: Label) -> impl Drop {
130130
131131pub struct Profiler {
132132 label : Option < Label > ,
133+ detail : Option < String > ,
134+ }
135+
136+ impl Profiler {
137+ pub fn detail ( mut self , detail : impl FnOnce ( ) -> String ) -> Profiler {
138+ if self . label . is_some ( ) {
139+ self . detail = Some ( detail ( ) )
140+ }
141+ self
142+ }
133143}
134144
135145pub struct Filter {
@@ -183,6 +193,7 @@ struct Message {
183193 level : usize ,
184194 duration : Duration ,
185195 label : Label ,
196+ detail : Option < String > ,
186197}
187198
188199impl ProfileStack {
@@ -208,13 +219,13 @@ thread_local!(static PROFILE_STACK: RefCell<ProfileStack> = RefCell::new(Profile
208219impl Drop for Profiler {
209220 fn drop ( & mut self ) {
210221 match self {
211- Profiler { label : Some ( label) } => {
222+ Profiler { label : Some ( label) , detail } => {
212223 PROFILE_STACK . with ( |stack| {
213224 let mut stack = stack. borrow_mut ( ) ;
214225 let start = stack. starts . pop ( ) . unwrap ( ) ;
215226 let duration = start. elapsed ( ) ;
216227 let level = stack. starts . len ( ) ;
217- stack. messages . push ( Message { level, duration, label } ) ;
228+ stack. messages . push ( Message { level, duration, label, detail : detail . take ( ) } ) ;
218229 if level == 0 {
219230 let stdout = stderr ( ) ;
220231 let longer_than = stack. filter_data . longer_than ;
@@ -228,7 +239,7 @@ impl Drop for Profiler {
228239 }
229240 } ) ;
230241 }
231- Profiler { label : None } => ( ) ,
242+ Profiler { label : None , .. } => ( ) ,
232243 }
233244 }
234245}
@@ -251,8 +262,16 @@ fn print_for_idx(
251262) {
252263 let current = & msgs[ current_idx] ;
253264 let current_indent = " " . repeat ( current. level ) ;
254- writeln ! ( out, "{}{:5}ms - {}" , current_indent, current. duration. as_millis( ) , current. label)
255- . expect ( "printing profiling info" ) ;
265+ let detail = current. detail . as_ref ( ) . map ( |it| format ! ( " @ {}" , it) ) . unwrap_or_default ( ) ;
266+ writeln ! (
267+ out,
268+ "{}{:5}ms - {}{}" ,
269+ current_indent,
270+ current. duration. as_millis( ) ,
271+ current. label,
272+ detail,
273+ )
274+ . expect ( "printing profiling info" ) ;
256275
257276 let longer_than_millis = longer_than. as_millis ( ) ;
258277 let children_indices = & children_map[ current_idx] ;
@@ -417,9 +436,9 @@ mod tests {
417436 fn test_longer_than ( ) {
418437 let mut result = vec ! [ ] ;
419438 let msgs = vec ! [
420- Message { level: 1 , duration: Duration :: from_nanos( 3 ) , label: "bar" } ,
421- Message { level: 1 , duration: Duration :: from_nanos( 2 ) , label: "bar" } ,
422- Message { level: 0 , duration: Duration :: from_millis( 1 ) , label: "foo" } ,
439+ Message { level: 1 , duration: Duration :: from_nanos( 3 ) , label: "bar" , detail : None } ,
440+ Message { level: 1 , duration: Duration :: from_nanos( 2 ) , label: "bar" , detail : None } ,
441+ Message { level: 0 , duration: Duration :: from_millis( 1 ) , label: "foo" , detail : None } ,
423442 ] ;
424443 print ( & msgs, Duration :: from_millis ( 0 ) , & mut result) ;
425444 // The calls to `bar` are so short that they'll be rounded to 0ms and should get collapsed
@@ -434,8 +453,8 @@ mod tests {
434453 fn test_unaccounted_for_topmost ( ) {
435454 let mut result = vec ! [ ] ;
436455 let msgs = vec ! [
437- Message { level: 1 , duration: Duration :: from_millis( 2 ) , label: "bar" } ,
438- Message { level: 0 , duration: Duration :: from_millis( 5 ) , label: "foo" } ,
456+ Message { level: 1 , duration: Duration :: from_millis( 2 ) , label: "bar" , detail : None } ,
457+ Message { level: 0 , duration: Duration :: from_millis( 5 ) , label: "foo" , detail : None } ,
439458 ] ;
440459 print ( & msgs, Duration :: from_millis ( 0 ) , & mut result) ;
441460 assert_eq ! (
@@ -453,11 +472,11 @@ mod tests {
453472 fn test_unaccounted_for_multiple_levels ( ) {
454473 let mut result = vec ! [ ] ;
455474 let msgs = vec ! [
456- Message { level: 2 , duration: Duration :: from_millis( 3 ) , label: "baz" } ,
457- Message { level: 1 , duration: Duration :: from_millis( 5 ) , label: "bar" } ,
458- Message { level: 2 , duration: Duration :: from_millis( 2 ) , label: "baz" } ,
459- Message { level: 1 , duration: Duration :: from_millis( 4 ) , label: "bar" } ,
460- Message { level: 0 , duration: Duration :: from_millis( 9 ) , label: "foo" } ,
475+ Message { level: 2 , duration: Duration :: from_millis( 3 ) , label: "baz" , detail : None } ,
476+ Message { level: 1 , duration: Duration :: from_millis( 5 ) , label: "bar" , detail : None } ,
477+ Message { level: 2 , duration: Duration :: from_millis( 2 ) , label: "baz" , detail : None } ,
478+ Message { level: 1 , duration: Duration :: from_millis( 4 ) , label: "bar" , detail : None } ,
479+ Message { level: 0 , duration: Duration :: from_millis( 9 ) , label: "foo" , detail : None } ,
461480 ] ;
462481 print ( & msgs, Duration :: from_millis ( 0 ) , & mut result) ;
463482 assert_eq ! (
0 commit comments