From 5c0230ea5a77f85de77ad4354788cd6c27aa1ef6 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 13 Nov 2026 09:28:38 -0300 Subject: debugger: follow natural V8 deoptimization with adaptive live views --- gui/bench/control.py | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 gui/bench/control.py (limited to 'gui/bench/control.py') diff --git a/gui/bench/control.py b/gui/bench/control.py new file mode 100644 index 0000000..eb3ff92 --- /dev/null +++ b/gui/bench/control.py @@ -0,0 +1,180 @@ +"""Small driver for the live controller; no tracing between requested stops.""" +import json +import os +from pathlib import Path +import re +import select +import struct +import subprocess +import time + +ROOT = Path(__file__).resolve().parents[2] + + +class Controller: + def __init__(self, ztrace, out, name, argv): + self.ztrace = Path(ztrace).resolve() + self.recording = out / (name + '.ztrace') + self.log = (out / (name + '.log')).open('w+') + self.process = subprocess.Popen([str(self.ztrace), 'serve', str(self.recording), '--', *argv], + cwd=out, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=self.log, bufsize=0) + self.pending = b'' + + def read(self): + deadline = time.monotonic() + 60 + while b'\n' not in self.pending: + if not select.select([self.process.stdout], [], [], max(0, deadline - time.monotonic()))[0]: + raise TimeoutError('controller timeout: ' + self.log.name) + data = os.read(self.process.stdout.fileno(), 4096) + if not data: + raise RuntimeError('controller exited: ' + self.log.name) + self.pending += data + line, self.pending = self.pending.split(b'\n', 1) + return json.loads(line) + + def command(self, command, status='stopped'): + self.process.stdin.write((command + '\n').encode()) + while True: + reply = self.read() + if reply['status'] != 'running': + assert reply['status'] == status, (command, reply) + return reply + + def output(self): + self.log.seek(0) + return self.log.read() + + def show(self): + return subprocess.check_output([str(self.ztrace), 'show', str(self.recording), '-1'], text=True) + + def regs(self): + return {name: int(value, 16) for name, value in re.findall(r'^ (\w+)\s+([0-9a-f]+)', self.show(), re.M)} + + def close(self): + try: + if self.process.poll() is None: + self.process.stdin.write(b'quit\n') + self.process.wait(timeout=30) + finally: + if self.process.poll() is None: + self.process.kill() + self.process.wait() + self.process.stdin.close() + self.process.stdout.close() + self.log.close() + + +def natural(ztrace, d8, out, source): + js = out / f'natural-{os.getpid()}.js' + js.write_bytes((ROOT / 'gui/targets/jit.js').read_bytes()) + c = Controller(ztrace, out, 'v8', [str(Path(d8).resolve()), '--trace-opt', '--trace-deopt', + '--trace-deopt-verbose', '--print-opt-code', '--print-opt-code-filter=jitWork', + '--gdbjit', '--gdbjit-dump', '--gdbjit-dump-filter=jitWork', js.name]) + try: + assert c.read()['status'] == 'stopped' + c.command('break main') + c.command('continue') + c.command('clear 1') + c.command('source-map ../../src ' + str(source / 'src')) + c.command('break v8::internal::Deoptimizer::New') + c.command('break v8::internal::Runtime_NotifyDeoptimized') + # Stop on an unrelated outer-loop bailout after natural compilation. + for _ in range(30): + stop = c.command('continue') + code = c.output() + if 'kind = TURBOFAN_JS' in code and "deopt reason 'wrong map'" in code: + break + else: + raise AssertionError('jitWork did not naturally reach TurboFan') + assert 'target MAGLEV' in code and 'target TURBOFAN_JS' in code + # The optimized-code block belongs to jitWork, selected by V8's own filter. + block = next(b.split('--- End code ---')[0] for b in reversed(code.split('--- Optimized code ---')) if 'kind = TURBOFAN_JS' in b and "deopt reason 'wrong map'" in b) + instructions = re.findall(r'^(0x[0-9a-f]+)\s+[0-9a-f]+\s+[0-9a-f]+\s+(.+)$', block, re.M) + assert instructions + start = int(instructions[0][0], 16) + end = int(instructions[-1][0], 16) + 16 + c.command('break *' + hex(start)) + for _ in range(30): + stop = c.command('continue') + if stop['pc'] == start: + break + else: + raise AssertionError('optimized warm call was not reached') + c.command('clear 4') + candidates = list(Path('/tmp').glob('elfdumpjitWork ' + js.name + ':*.o')) + def owns_start(path): + data = path.read_bytes() + shoff = struct.unpack_from('