From e03a729fa36287e236b5ac66f17d67d63a219d00 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 4 Sep 2022 20:06:27 +0300 Subject: feat(tvix/eval): implement TracingObserver for runtime tracing This produces similar output to the previous tracing feature, but can redirect the output somewhere else. Change-Id: I9493c260f480904f3932cb74809b622c24d7be96 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6453 Reviewed-by: sterni Tested-by: BuildkiteCI --- tvix/eval/src/observer.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tvix/eval/src/observer.rs b/tvix/eval/src/observer.rs index 427fc2c3996a..677c3f0811e7 100644 --- a/tvix/eval/src/observer.rs +++ b/tvix/eval/src/observer.rs @@ -112,3 +112,57 @@ impl Observer for DisassemblingObserver { let _ = self.writer.flush(); } } + +/// An observer that collects a textual representation of an entire +/// runtime execution. +pub struct TracingObserver { + writer: TabWriter, +} + +impl TracingObserver { + pub fn new(writer: W) -> Self { + Self { + writer: TabWriter::new(writer), + } + } +} + +impl Observer for TracingObserver { + fn observe_enter_frame(&mut self, arg_count: usize, lambda: &Rc, call_depth: usize) { + let _ = writeln!( + &mut self.writer, + "=== entering {} frame[{}] @ {:p} ===", + if arg_count == 0 { "thunk" } else { "closure" }, + call_depth, + lambda, + ); + } + + fn observe_exit_frame(&mut self, frame_at: usize) { + let _ = writeln!(&mut self.writer, "=== exiting frame {} ===", frame_at); + } + + fn observe_enter_builtin(&mut self, name: &'static str) { + let _ = writeln!(&mut self.writer, "=== entering builtin {} ===", name); + } + + fn observe_exit_builtin(&mut self, name: &'static str) { + let _ = writeln!(&mut self.writer, "=== exiting builtin {} ===", name); + } + + fn observe_execute_op(&mut self, ip: usize, op: &OpCode, stack: &[Value]) { + let _ = write!(&mut self.writer, "{:04} {:?}\t[ ", ip, op); + + for val in stack { + let _ = write!(&mut self.writer, "{} ", val); + } + + let _ = writeln!(&mut self.writer, "]"); + } +} + +impl Drop for TracingObserver { + fn drop(&mut self) { + let _ = self.writer.flush(); + } +} -- cgit 1.4.1