@@ -23,8 +23,21 @@ use std::rc::Rc;
23
23
use std:: sync:: Mutex ;
24
24
use std:: time:: Duration ;
25
25
26
- use piet_common:: { kurbo , Color , RenderContext , Text , TextLayout , TextLayoutBuilder } ;
26
+ use piet_common:: { Color , RenderContext , Text , TextLayout , TextLayoutBuilder , kurbo } ;
27
27
28
+ fn read_u8_vec ( inf : & mut File ) -> Option < Vec < u8 > > {
29
+ let len = read_u64 ( inf) ?;
30
+ let mut vec = Vec :: with_capacity ( len as usize ) ;
31
+ for _ in 0 ..len {
32
+ vec. push ( read_u8 ( inf) ?) ;
33
+ }
34
+ Some ( vec)
35
+ }
36
+ fn read_u8 ( inf : & mut File ) -> Option < u8 > {
37
+ let mut bytes: [ u8 ; 1 ] = [ 0 ; 1 ] ;
38
+ inf. read_exact ( & mut bytes) . ok ( ) ?;
39
+ Some ( u8:: from_ne_bytes ( bytes) )
40
+ }
28
41
fn read_u128 ( inf : & mut File ) -> Result < u128 , std:: io:: Error > {
29
42
let mut bytes: [ u8 ; 16 ] = [ 0 ; 16 ] ;
30
43
inf. read_exact ( & mut bytes) ?;
@@ -92,6 +105,53 @@ fn dump_free(
92
105
Some ( ( ) )
93
106
}
94
107
108
+ fn dump_trace_record (
109
+ _state : & mut State ,
110
+ _rs : & mut ( ) ,
111
+ indent : & mut u64 ,
112
+ now : Duration ,
113
+ msg : Rc < [ u8 ] > ,
114
+ ) -> Option < ( ) > {
115
+ let msg = String :: from_utf8_lossy ( & msg) ;
116
+
117
+ // Pritty printing of trace records to show indentation based on the trace depth.
118
+ // Indentation is increased for messages starting with `>`, and decreased for messages starting
119
+ // with `<`.
120
+ // With the exception of `> halt`, which decreases the indent (because `> entrypoint` does not
121
+ // have a corresponding `< entrypoint`)
122
+ let msg = if msg. starts_with ( '>' ) {
123
+ if msg == "> halt" {
124
+ if * indent > 0 {
125
+ * indent -= 1 ;
126
+ }
127
+ }
128
+ let indent_str = " " . repeat ( * indent as usize ) ;
129
+ let msg = format ! ( "{}{}" , indent_str, & msg) ;
130
+ if msg != "> halt" {
131
+ // If the message is not `> halt`, increment the indent.
132
+ // This is to ensure that the next message is indented correctly.
133
+ * indent += 1 ;
134
+ }
135
+
136
+ msg
137
+ } else if msg. starts_with ( '<' ) {
138
+ if * indent > 0 {
139
+ * indent -= 1 ;
140
+ }
141
+ let indent_str = " " . repeat ( * indent as usize ) ;
142
+ let msg = format ! ( "{}{}" , indent_str, msg) ;
143
+
144
+ msg
145
+ } else {
146
+ let indent_str = " " . repeat ( * indent as usize ) ;
147
+ format ! ( "{}{}" , indent_str, msg)
148
+ } ;
149
+
150
+ println ! ( "\n [{:9?}] {}" , now, msg) ;
151
+
152
+ Some ( ( ) )
153
+ }
154
+
95
155
// todo: this should use something more reasonable than a hash table
96
156
// for each node. let's measure the out-degree and see if a small
97
157
// array is better, to start.
@@ -542,20 +602,33 @@ fn render_free(
542
602
Some ( ( ) )
543
603
}
544
604
545
- fn read_file < I , U , A , F , S > (
605
+ fn render_trace_record (
606
+ _state : & mut State ,
607
+ _rs : & mut RenderState ,
608
+ _indent : & mut u64 ,
609
+ _now : Duration ,
610
+ _msg : Rc < [ u8 ] > ,
611
+ ) -> Option < ( ) > {
612
+ Some ( ( ) )
613
+ }
614
+
615
+ fn read_file < I , U , A , F , T , S > (
546
616
state : & mut State ,
547
617
mut handle_state : S ,
548
618
handle_ident : I ,
549
619
handle_unwind : U ,
550
620
handle_alloc : A ,
551
621
handle_free : F ,
622
+ handle_trace_record : T ,
552
623
) -> Option < ( ) >
553
624
where
554
625
I : Fn ( & mut State , & mut S , Duration , blake3:: Hash ) -> Option < ( ) > ,
555
626
U : Fn ( & mut State , & mut S , Duration , Rc < [ u64 ] > ) -> Option < ( ) > ,
556
627
A : Fn ( & mut State , & mut S , Duration , u64 , u64 , Rc < [ u64 ] > ) -> Option < ( ) > ,
557
628
F : Fn ( & mut State , & mut S , Duration , u64 , u64 , Rc < [ u64 ] > ) -> Option < ( ) > ,
629
+ T : Fn ( & mut State , & mut S , & mut u64 , Duration , Rc < [ u8 ] > ) -> Option < ( ) > ,
558
630
{
631
+ let mut indent = 0 ;
559
632
loop {
560
633
let time = match read_u128 ( & mut state. inf ) {
561
634
Ok ( t) => t,
@@ -600,6 +673,9 @@ where
600
673
let trace = amt_trace. 1 . clone ( ) ;
601
674
state. total -= amt;
602
675
handle_free ( state, & mut handle_state, now, ptr, amt, trace) ?;
676
+ } else if frame_id == 4 {
677
+ let msg = read_u8_vec ( & mut state. inf ) ?. into ( ) ;
678
+ handle_trace_record ( state, & mut handle_state, & mut indent, now, msg) ?;
603
679
} else {
604
680
return None ;
605
681
}
@@ -699,6 +775,7 @@ fn spawn_render_thread(
699
775
render_unwind,
700
776
render_alloc,
701
777
render_free,
778
+ render_trace_record,
702
779
) ?;
703
780
bar_ffmpeg. wait ( ) . ok ( ) ?;
704
781
flame_ffmpeg. wait ( ) . ok ( ) ?;
@@ -755,6 +832,7 @@ fn dump_trace(mut state: State) {
755
832
dump_unwind,
756
833
dump_alloc,
757
834
dump_free,
835
+ dump_trace_record,
758
836
) ;
759
837
}
760
838
@@ -771,6 +849,7 @@ fn plot_mem(args: Vec<String>, mut state: State) {
771
849
|_, _, _, _| Some ( ( ) ) ,
772
850
|_, _, _, _, _, _| Some ( ( ) ) ,
773
851
|_, _, _, _, _, _| Some ( ( ) ) ,
852
+ |_, _, _, _, _| Some ( ( ) ) ,
774
853
) {
775
854
Some ( ( ) ) => ( ) ,
776
855
None => {
@@ -810,6 +889,7 @@ fn plot_mem(args: Vec<String>, mut state: State) {
810
889
|_, _, _, _| Some ( ( ) ) ,
811
890
count_frame,
812
891
count_frame,
892
+ |_, _, _, _, _| Some ( ( ) ) ,
813
893
) ;
814
894
if state. num_durations > 0 {
815
895
( * jobs. lock ( ) . unwrap ( ) ) . push ( ( * start_duration. lock ( ) . unwrap ( ) , state. max_duration ) ) ;
0 commit comments