Skip to content

Commit 9738dba

Browse files
committed
Update utility for dumping logs to support the traces
Signed-off-by: Doru Blânzeanu <dblnz@pm.me>
1 parent b2ff420 commit 9738dba

File tree

1 file changed

+82
-2
lines changed

1 file changed

+82
-2
lines changed

src/trace_dump/main.rs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,21 @@ use std::rc::Rc;
2323
use std::sync::Mutex;
2424
use std::time::Duration;
2525

26-
use piet_common::{kurbo, Color, RenderContext, Text, TextLayout, TextLayoutBuilder};
26+
use piet_common::{Color, RenderContext, Text, TextLayout, TextLayoutBuilder, kurbo};
2727

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+
}
2841
fn read_u128(inf: &mut File) -> Result<u128, std::io::Error> {
2942
let mut bytes: [u8; 16] = [0; 16];
3043
inf.read_exact(&mut bytes)?;
@@ -92,6 +105,53 @@ fn dump_free(
92105
Some(())
93106
}
94107

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+
95155
// todo: this should use something more reasonable than a hash table
96156
// for each node. let's measure the out-degree and see if a small
97157
// array is better, to start.
@@ -542,20 +602,33 @@ fn render_free(
542602
Some(())
543603
}
544604

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>(
546616
state: &mut State,
547617
mut handle_state: S,
548618
handle_ident: I,
549619
handle_unwind: U,
550620
handle_alloc: A,
551621
handle_free: F,
622+
handle_trace_record: T,
552623
) -> Option<()>
553624
where
554625
I: Fn(&mut State, &mut S, Duration, blake3::Hash) -> Option<()>,
555626
U: Fn(&mut State, &mut S, Duration, Rc<[u64]>) -> Option<()>,
556627
A: Fn(&mut State, &mut S, Duration, u64, u64, Rc<[u64]>) -> Option<()>,
557628
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<()>,
558630
{
631+
let mut indent = 0;
559632
loop {
560633
let time = match read_u128(&mut state.inf) {
561634
Ok(t) => t,
@@ -600,6 +673,9 @@ where
600673
let trace = amt_trace.1.clone();
601674
state.total -= amt;
602675
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)?;
603679
} else {
604680
return None;
605681
}
@@ -699,6 +775,7 @@ fn spawn_render_thread(
699775
render_unwind,
700776
render_alloc,
701777
render_free,
778+
render_trace_record,
702779
)?;
703780
bar_ffmpeg.wait().ok()?;
704781
flame_ffmpeg.wait().ok()?;
@@ -755,6 +832,7 @@ fn dump_trace(mut state: State) {
755832
dump_unwind,
756833
dump_alloc,
757834
dump_free,
835+
dump_trace_record,
758836
);
759837
}
760838

@@ -771,6 +849,7 @@ fn plot_mem(args: Vec<String>, mut state: State) {
771849
|_, _, _, _| Some(()),
772850
|_, _, _, _, _, _| Some(()),
773851
|_, _, _, _, _, _| Some(()),
852+
|_, _, _, _, _| Some(()),
774853
) {
775854
Some(()) => (),
776855
None => {
@@ -810,6 +889,7 @@ fn plot_mem(args: Vec<String>, mut state: State) {
810889
|_, _, _, _| Some(()),
811890
count_frame,
812891
count_frame,
892+
|_, _, _, _, _| Some(()),
813893
);
814894
if state.num_durations > 0 {
815895
(*jobs.lock().unwrap()).push((*start_duration.lock().unwrap(), state.max_duration));

0 commit comments

Comments
 (0)