1+ /// Stats for a nested span in the execution segment that is tracked by the [`CycleTracker`].
2+ #[ derive( Clone , Debug , Default ) ]
3+ pub struct SpanInfo {
4+ /// The name of the span.
5+ tag : String ,
6+ /// The cycle count at which the span starts.
7+ start : usize ,
8+ }
9+
110#[ derive( Clone , Debug , Default ) ]
211pub struct CycleTracker {
312 /// Stack of span names, with most recent at the end
4- stack : Vec < String > ,
13+ stack : Vec < SpanInfo > ,
14+ /// Depth of the stack.
15+ depth : usize ,
516}
617
718impl CycleTracker {
@@ -11,23 +22,33 @@ impl CycleTracker {
1122
1223 /// Starts a new cycle tracker span for the given name.
1324 /// If a span already exists for the given name, it ends the existing span and pushes a new one to the vec.
14- pub fn start ( & mut self , mut name : String ) {
25+ pub fn start ( & mut self , mut name : String , cycles_count : usize ) {
1526 // hack to remove "CT-" prefix
1627 if name. starts_with ( "CT-" ) {
1728 name = name. split_off ( 3 ) ;
1829 }
19- self . stack . push ( name) ;
30+ self . stack . push ( SpanInfo {
31+ tag : name. clone ( ) ,
32+ start : cycles_count,
33+ } ) ;
34+ let padding = "│ " . repeat ( self . depth ) ;
35+ tracing:: info!( "{}┌╴{}" , padding, name) ;
36+ self . depth += 1 ;
2037 }
2138
2239 /// Ends the cycle tracker span for the given name.
2340 /// If no span exists for the given name, it panics.
24- pub fn end ( & mut self , mut name : String ) {
41+ pub fn end ( & mut self , mut name : String , cycles_count : usize ) {
2542 // hack to remove "CT-" prefix
2643 if name. starts_with ( "CT-" ) {
2744 name = name. split_off ( 3 ) ;
2845 }
29- let stack_top = self . stack . pop ( ) ;
30- assert_eq ! ( stack_top. unwrap( ) , name, "Stack top does not match name" ) ;
46+ let SpanInfo { tag, start } = self . stack . pop ( ) . unwrap ( ) ;
47+ assert_eq ! ( tag, name, "Stack top does not match name" ) ;
48+ self . depth -= 1 ;
49+ let padding = "│ " . repeat ( self . depth ) ;
50+ let span_cycles = cycles_count - start;
51+ tracing:: info!( "{}└╴{} cycles" , padding, span_cycles) ;
3152 }
3253
3354 /// Ends the current cycle tracker span.
@@ -37,7 +58,11 @@ impl CycleTracker {
3758
3859 /// Get full name of span with all parent names separated by ";" in flamegraph format
3960 pub fn get_full_name ( & self ) -> String {
40- self . stack . join ( ";" )
61+ self . stack
62+ . iter ( )
63+ . map ( |span_info| span_info. tag . clone ( ) )
64+ . collect :: < Vec < String > > ( )
65+ . join ( ";" )
4166 }
4267}
4368
0 commit comments