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/steps.py | 177 ++++++++++++++++------------------------------------- 1 file changed, 52 insertions(+), 125 deletions(-) (limited to 'gui/bench/steps.py') diff --git a/gui/bench/steps.py b/gui/bench/steps.py index 166fa90..2906759 100644 --- a/gui/bench/steps.py +++ b/gui/bench/steps.py @@ -1,143 +1,70 @@ #!/usr/bin/env python3 -"""Compare the same controller's recorded instruction steps on C and debug V8.""" +"""Compare source-enabled C/V8 steps; verify a natural wrong-map deoptimization separately.""" import argparse import json import math -import os from pathlib import Path -import re -import select import statistics import subprocess import time +from control import ROOT, Controller, natural, deoptimization -ROOT = Path(__file__).resolve().parents[2] -p = argparse.ArgumentParser(description=__doc__) -p.add_argument('--d8', type=Path, required=True) -p.add_argument('--ztrace', type=Path, default=ROOT / 'gui/zig-out/bin/ztrace') -p.add_argument('--out', type=Path, default=Path('/tmp/ztrace-step-bench')) -p.add_argument('--steps', type=int, default=1000) -p.add_argument('--jit-source', action='store_true', help='Measure with V8 JIT symbols and source lines loaded') -a = p.parse_args() -assert a.steps > 0 -a.out.mkdir(parents=True, exist_ok=True) -a.out = a.out.resolve() -a.d8 = a.d8.resolve() -small = a.out / 'loop' -subprocess.run(['cc', '-g', '-O0', str(ROOT / 'gui/targets/loop.c'), '-o', str(small)], check=True) -js = a.out / f'bench-jit-{os.getpid()}.js' -if a.jit_source: - js.write_bytes((ROOT / 'gui/targets/jit.js').read_bytes()) - -class Controller: - def __init__(self, name, argv): - self.log = (a.out / (name + '.log')).open('w+') - self.process = subprocess.Popen( - [str(a.ztrace.resolve()), 'serve', str(a.out / (name + '.ztrace')), '--', *argv], - cwd=a.out, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=self.log, bufsize=0) - self.pending = b'' - - def read(self): - deadline = time.monotonic() + 30 - while b'\n' not in self.pending: - if not select.select([self.process.stdout], [], [], max(0, deadline - time.monotonic()))[0]: - raise TimeoutError('controller did not reply within 30 seconds') - data = os.read(self.process.stdout.fileno(), 4096) - if not data: - raise RuntimeError('controller exited; see ' + self.log.name) - self.pending += data - line, self.pending = self.pending.split(b'\n', 1) - return json.loads(line) - - def command(self, command): - self.process.stdin.write((command + '\n').encode()) - while True: - reply = self.read() - assert reply['status'] not in ('failed', 'exited'), reply - if reply['status'] == 'stopped': - return reply - - 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 summary(ns): + values = sorted(v / 1000 for v in ns) + return dict(median_us=statistics.median(values), p95_us=values[math.ceil(len(values) * .95) - 1], max_us=max(values)) -def summary(ns): - ordered = sorted(v / 1000 for v in ns) - return dict(median_us=statistics.median(ordered), p95_us=ordered[math.ceil(len(ns) * .95) - 1], max_us=max(ordered)) +def measure(c, count): + core, wall, command = [], [], [] + for _ in range(count): + started = time.perf_counter_ns() + reply = c.command('stepi') + wall.append(time.perf_counter_ns() - started) + core.append(reply['capture_ns']) + command.append(reply['elapsed_ns']) + assert reply['reason'] == 'step', reply + return dict(steps=count, capture=summary(core), command=summary(command), roundtrip=summary(wall)) -results = {} -for name, argv in [('c', [str(small)]), ('v8', [str(a.d8), '--allow-natives-syntax', - '--no-concurrent-recompilation', '--no-maglev', '--print-opt-code', - '--print-opt-code-filter=jitWork', *(['--gdbjit', '--gdbjit-dump', '--gdbjit-dump-filter=jitWork', js.name] - if a.jit_source else [str(ROOT / 'gui/targets/jit.js')])])]: +def main(): + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('--d8', type=Path, required=True) + p.add_argument('--source', type=Path, default=Path('/tmp/computeranim-v8-source')) + p.add_argument('--ztrace', type=Path, default=ROOT / 'gui/zig-out/bin/ztrace') + p.add_argument('--out', type=Path, default=Path('/tmp/ztrace-natural-bench')) + p.add_argument('--steps', type=int, default=1000) + p.add_argument('--jit-source', action='store_true', help='Compatibility: source is always enabled') + a = p.parse_args() + assert 0 < a.steps <= 4000, 'sample must stay in the warmed loop' + a.out = a.out.resolve() + a.source = a.source.resolve() + a.out.mkdir(parents=True, exist_ok=True) + small = a.out / 'loop' + subprocess.run(['cc', '-g', '-O0', str(ROOT / 'gui/targets/loop.c'), '-o', str(small)], check=True) + c = Controller(a.ztrace, a.out, 'c', [str(small)]) + results = {} + try: + c.read() + c.command('break work') + c.command('continue') + results['c'] = measure(c, a.steps) + finally: + c.close() start = time.perf_counter_ns() - c = Controller(name, argv) - elf = None + c, js, elf, bounds, instructions, stop = natural(a.ztrace, a.d8, a.out, a.source) try: - assert c.read()['status'] == 'stopped' - entry_ns = time.perf_counter_ns() - start - if name == 'c': - c.command('break work') - else: - stopped = c.command('continue') # Native %SystemBreak, before the final JS call. - assert stopped['reason'].startswith('signal TRAP'), stopped - c.log.seek(0) - assembly = c.log.read() - assert 'kind = TURBOFAN_JS' in assembly - # shortcut: this fixture's x64 loop instruction; fail loudly if V8 changes its register allocation. - match = re.search(r'^(0x[0-9a-f]+)\s+[0-9a-f]+\s+[0-9a-f]+\s+addl r8,r11', assembly, re.M) - assert match, 'could not find jitWork loop body; inspect ' + c.log.name - address = int(match[1], 16) - c.command('signal 0') - c.command('break *' + hex(address)) - hit = c.command('continue') - assert hit['reason'].startswith('breakpoint '), hit - if name == 'v8': - assert hit['pc'] == address, hit - maps = Path(f"/proc/{hit['pid']}/maps").read_text().splitlines() - mapping = next(line for line in maps if int(line.split('-')[0], 16) <= address < int(line.split('-')[1].split()[0], 16)) - assert 'x' in mapping.split()[1] and len(mapping.split()) == 5, mapping - first_break_ns = time.perf_counter_ns() - start - if name == 'v8': - stepped = c.command('step') - assert stepped['reason'] == 'step' and stepped['pc'] != address, stepped - if a.jit_source: - elf, = Path('/tmp').glob('elfdumpjitWork ' + js.name + ':*.o') - imported = c.command('symbols ' + str(elf)) - assert imported['pc'] == stepped['pc'], imported - core, wall = [], [] - for _ in range(a.steps): - start_step = time.perf_counter_ns() - reply = c.command('stepi') - wall.append(time.perf_counter_ns() - start_step) - core.append(reply['elapsed_ns']) - assert reply['reason'] == 'step', reply - results[name] = dict(steps=a.steps, entry_ms=entry_ns / 1e6, first_break_ms=first_break_ns / 1e6, - capture=summary(core), roundtrip=summary(wall)) - if name == 'v8': - results[name]['jit_source'] = a.jit_source - print(name, json.dumps(results[name]), flush=True) + results['v8'] = dict(first_generated_stop_ms=(time.perf_counter_ns() - start) / 1e6, **measure(c, a.steps)) + regs = c.regs() + assert bounds[0] <= regs['rip'] < bounds[1], 'steady sample left optimized jitWork' + assert str(js) + ':' in c.show(), 'generated source mapping missing' + results['transition'] = deoptimization(c, bounds, instructions, a.source) + (a.out / 'results.json').write_text(json.dumps(results, indent=2) + '\n') + print(json.dumps(results, indent=2)) finally: c.close() - if elf is not None: - elf.unlink(missing_ok=True) - if name == 'v8': - shown = subprocess.run([str(a.ztrace.resolve()), 'show', str(a.out / 'v8.ztrace'), '-1'], - check=True, capture_output=True, text=True) - if a.jit_source: - assert str(js) + ':' in shown.stdout, 'JIT source line missing' - else: - assert f"thread {reply['tid']}" in shown.stdout.splitlines(), 'JIT stop inherited a stale native source line' -(a.out / 'results.json').write_text(json.dumps(results, indent=2) + '\n') + elf.unlink(missing_ok=True) + + +if __name__ == '__main__': + main() -- cgit v1.3