about summary refs log tree commit diff
path: root/tvix/eval/src
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-04T17·06+0300
committertazjin <tazjin@tvl.su>2022-09-09T21·14+0000
commite03a729fa36287e236b5ac66f17d67d63a219d00 (patch)
tree17bae4e2ad03e6441bfc5841565adab2b713591f /tvix/eval/src
parent14ff889d607635083a030fc73d76b0263759be83 (diff)
feat(tvix/eval): implement TracingObserver for runtime tracing r/4779
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 <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src')
-rw-r--r--tvix/eval/src/observer.rs54
1 files changed, 54 insertions, 0 deletions
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<W: Write> Observer for DisassemblingObserver<W> {
         let _ = self.writer.flush();
     }
 }
+
+/// An observer that collects a textual representation of an entire
+/// runtime execution.
+pub struct TracingObserver<W: Write> {
+    writer: TabWriter<W>,
+}
+
+impl<W: Write> TracingObserver<W> {
+    pub fn new(writer: W) -> Self {
+        Self {
+            writer: TabWriter::new(writer),
+        }
+    }
+}
+
+impl<W: Write> Observer for TracingObserver<W> {
+    fn observe_enter_frame(&mut self, arg_count: usize, lambda: &Rc<Lambda>, 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<W: Write> Drop for TracingObserver<W> {
+    fn drop(&mut self) {
+        let _ = self.writer.flush();
+    }
+}