diff options
Diffstat (limited to 'gui/bench')
| -rw-r--r-- | gui/bench/RESULTS.md | 210 | ||||
| -rw-r--r-- | gui/bench/control.py | 180 | ||||
| -rw-r--r-- | gui/bench/steps.py | 177 |
3 files changed, 337 insertions, 230 deletions
diff --git a/gui/bench/RESULTS.md b/gui/bench/RESULTS.md index 89f0d78..0426dce 100644 --- a/gui/bench/RESULTS.md +++ b/gui/bench/RESULTS.md @@ -1,134 +1,134 @@ -# Debug V8 versus a small C target +# Natural V8 deoptimization and stepping measurements -Measured locally on 2026-11-13, Linux 7.2.11-arch1-1, Intel i7-11700, -Zig 0.17.0, ReleaseFast controller. The targets were a `cc -g -O0` C loop -and **unoptimized debug V8**, not release d8. Both used the same controller: -512 stack bytes, 24 disassembled instructions and telescope depth 2, with a -recording flush at every stop. No syscall tracing and no continuous stepping -while the user has selected Continue. - -Final controller run: +Measured 2026-11-13 on Linux 7.2.11-arch1-1, Intel i7-11700, Zig 0.17.0, +ReleaseFast debugger; targets are `cc -g -O0` C and unoptimized debug V8. +Source is loaded for both. Capture settings are 512 stack bytes, 24 instructions, +telescope depth 2, mapping refresh and recording flush at every stop. Continue +runs normally between breakpoints; no continuous instruction tracing occurs. ```sh -python3 gui/bench/steps.py \ - --d8 /tmp/computeranim-v8-debug-remote/d8 \ - --ztrace zig-out/bin/ztrace \ - --out /tmp/ztrace-step-bench-ui +python3 gui/bench/steps.py --d8 /tmp/computeranim-v8-debug-remote/d8 \ + --source /tmp/computeranim-v8-source --out /tmp/ztrace-natural-bench-final ``` -| 1,000 instruction steps | C | Debug V8 / generated JS | +| 1,000 steady instruction steps | C | Debug V8 / TurboFan JS | | --- | ---: | ---: | -| Capture + flush, median | 37.48 µs | 38.43 µs | -| Capture + flush, p95 | 54.37 µs | 52.48 µs | -| Controller round trip, median | 49.49 µs | 49.48 µs | -| Controller round trip, p95 | 72.46 µs | 68.31 µs | -| Controller round trip, maximum | 553.21 µs | 364.10 µs | -| Launch through first requested breakpoint | 5.98 ms | 938.49 ms | - -This supports comparable steady instruction-step cost for these workloads. -It does not claim that V8 starts as quickly as a small program, or that all C++ -source/DWARF operations have this cost. The previous 1,000-step run measured -35.73 / 38.47 µs capture medians and 46.94 / 49.53 µs round-trip medians for -C / V8. Scheduling and cache state affect these microsecond measurements; no -CPU isolation or filesystem-cache dropping was used. GUI presentation is not -included in the controller timings. +| Capture + flush, median | 48.16 µs | 133.16 µs | +| Capture + flush, p95 | 75.98 µs | 153.33 µs | +| Entire command, median | 55.00 µs | 140.00 µs | +| Controller round trip, median | 68.88 µs | 154.44 µs | +| Controller round trip, p95 | 105.64 µs | 208.51 µs | +| Controller round trip, maximum | 567.93 µs | 539.92 µs | -The V8 fixture compiles `jitWork` with TurboFan, traps in the native runtime, -then installs an actual software breakpoint at `addl r8,r11` inside the -optimized loop. The harness asserts the returned RIP equals that address and -lies in an anonymous executable mapping. It then tests source-step fallback -without JIT source lines, records the instruction-step sample, and checks that -the recording no longer reports the previous native source line. The loop -address is read from V8's own output; the harness fails rather than guesses if -a different V8 compiler allocates different registers. +`capture_ns` covers reporting state plus recording flush. `elapsed_ns` covers the +whole command, including execution/wait and capture. Round trip additionally +includes pipes and the Python driver. Neither includes GUI presentation. +Launch through first generated stop and JIT source import took 1,288.73 ms. +No CPU isolation or filesystem-cache dropping was used. These are workload +measurements, not a release-V8 performance comparison. Per-stop mapping refresh +and natural tiering differ from the earlier forced-optimization demonstration; +its old figures are not directly comparable. -The raw records, logs and JSON results are in `/tmp/ztrace-step-bench-ui`. -The script is the durable reproducer; `/tmp` artifacts are not permanent. +## Verified transition -With JIT source information loaded, the final 1,000-step run remained comparable: +The ordinary fixture naturally compiled through Maglev and TurboFan. After +warming `jitWork` on `{value:7}`, `{extra:1,value:9}` hit its TurboFan wrong-map +guard. The generated exit's return PC matched the `rdx` origin argument at +`Deoptimizer::New`; unrelated outer-loop bailouts were ignored. Native source +resolved at both Deoptimizer::New (`deoptimizer.cc:290`) and +Runtime_NotifyDeoptimized (`runtime-compiler.cc`). The guard mapped to JS line 3, +`let sum = object.value`. Its shared exit stub mapped to a different JS line, so +origin context deliberately uses the guard. -| Source-enabled run | C | Debug V8 / generated JS | +| Transition stop | Capture + flush | Entire command | | --- | ---: | ---: | -| Capture + flush, median | 36.60 µs | 37.76 µs | -| Capture + flush, p95 | 46.78 µs | 46.35 µs | -| Controller round trip, median | 48.04 µs | 48.84 µs | -| Controller round trip, p95 | 60.97 µs | 63.83 µs | +| generated wrong-map exit | 372.09 µs | 1192.34 µs | +| Deoptimizer::New | 202.01 µs | 214.16 µs | +| Runtime_NotifyDeoptimized | 265.13 µs | 1086.31 µs | +| InterpreterEnterAtBytecode | 445.40 µs | 752.09 µs | +| Ignition property bytecode handler | 15163.99 µs | 15474.67 µs | -Reproduce with `--jit-source --out /tmp/ztrace-step-bench-source` added to the -command above. This run imports the real JIT ELF before sampling and checks the -final recording retains JavaScript source information. The initial import is -excluded from steady stepping, just as executable startup is excluded. +The immediate fallback was **Ignition**, confirmed by stopping at +`Builtins_InterpreterEnterAtBytecode` and stepping into +`Builtins_GetNamedPropertyHandler`. This is actual interpreter dispatch, +not an inference from the deoptimizer's frame-reconstruction log. The first +handler capture incurred additional symbol/function work, which is why transition +costs are reported separately from warmed stepping. The script completed with +`mismatch 499509` and `fallback 499507`. Default tiers remain enabled; this does +not imply that other V8 builds always resume Ignition. -## V8 provenance +Raw logs, recordings and JSON are in `/tmp/ztrace-natural-bench-final`; the +checked-in scripts are the durable reproducer. Addresses/PIDs in those files are +session-specific and `/tmp` artifacts are not permanent. -The user's remote Arch build host (`chaincli-box`) has the existing checkout at -`/home/goblin/v8-research/v8`. Ninja verified the compatible debug build up to date -(`ninja: no work to do`). No V8 compilation was performed on the workstation. -The complete runtime artifacts were copied to `/tmp/computeranim-v8-debug-remote` -and smoke-tested locally. +## Source and build provenance +- Remote: `chaincli-box`, `/home/goblin/v8-research/v8`. - Revision: `84703f702dca525edd469f3a98d784faa46a02f8`. -- d8 version: `15.2.0 (candidate)`. -- Output: `out/x64.debug-vuln` (historical directory name). -- Existing unrelated local `BUILD.gn` change to the `v8_fuzzer` source list was - preserved; the checkout was not reset or updated. +- d8: `15.2.0 (candidate)`, existing `out/x64.debug-vuln`. - `is_debug=true`, `v8_optimized_debug=false`, `symbol_level=2`, `v8_symbol_level=2`, `use_debug_fission=true`, `target_cpu="x64"`. - Disassembler, object printing, heap verification, slow DCHECKs and backtraces enabled; `use_remoteexec=false`, `use_siso=false`. -- Component build: d8 plus its shared libraries (including the large debug - libv8.so), snapshots and ICU data. Split `.dwo` files were not copied, so this - run is not a validation of complete C++ local-variable/source debugging. +- Existing remote `BUILD.gn` edits and untracked files were preserved. No engine + patch, rebuild, checkout reset or workstation V8 compilation was needed. + +The component runtime (d8, shared libraries, snapshots and ICU data) is at +`/tmp/computeranim-v8-debug-remote`. Matching `src/` and all 2,328 `.dwo` files +were copied from the same checkout to `/tmp/computeranim-v8-source` (about 1.9 GiB +combined). To retrieve again after verifying the same revision: + +```sh +rsync -a -e 'ssh -F ~/.ssh/config -o BatchMode=yes' \ + chaincli-box:/home/goblin/v8-research/v8/src/ /tmp/computeranim-v8-source/src/ +rsync -a --include='*/' --include='*.dwo' --exclude='*' \ + -e 'ssh -F ~/.ssh/config -o BatchMode=yes' \ + chaincli-box:/home/goblin/v8-research/v8/out/x64.debug-vuln/obj/ \ + /tmp/computeranim-v8-source/out/x64.debug-vuln/obj/ +``` -## Checks +This build's skeleton line tables use comp_dir `.` and stripped `../../src` +paths. `source-map ../../src /tmp/computeranim-v8-source/src` resolves them +through the shared debugger source-prefix interface. Native line resolution was +verified; full split-DWARF C++ local-variable inspection was not implemented. -The GUI's real-target script exercises launch, a named breakpoint through the -Vim command bar, continue, keyboard and mouse instruction stepping, source -stepping, pause during execution and browsing history back to live. It also -checks fractional scrolling, pane histories, Mouse4/5 through the actual -pointer handler, semantic right-clicks, minimap dragging and animated lenses. -Native-window checks cover keyboard stepping, pane focus, scrolling and command -entry. Raw hardware Mouse4/5 injection was not performed. +## Verification -`gui/test/inspection.py` checks bounded read-only code/memory inspection against -C and debug V8: RIP stays unchanged, invalid counts are rejected, and subsequent -stepping still works. With `--d8`, it imports V8's real GDB JIT ELF and asserts -that source line 6 of the JavaScript fixture appears in the recorded instruction -metadata. The line mapping comes from V8's DWARF line program. +`gui/test/inspection.py --d8 /tmp/computeranim-v8-debug-remote/d8` passes bounded +read-only inspection, unchanged RIP, invalid-count rejection, source-prefix +replacement, natural optimization, matching JIT ELF import, native deoptimization +stops and actual fallback dispatch. -The root suite had two failures before this work; they remain in the existing -loader/syscall formatting and watch/string assertions. The new debugger tests -pass (86/88 in the full root suite; 25/27 debugger tests, with those same two -failures). No assertion was -weakened to hide them. +GUI tests cover live stepping/pause/history, source stepping, semantic address +navigation, minimap dragging, changed-state animation and lenses. Adaptive tests +use 2240×1260, 1440×900 and 800×700 at 60%, 100% and 200%: full 4 GiB mapping +navigation with single-row precision, per-pane segment history, pinned refresh at +repeated same-PC stops, graph/dim controls and font reset. Independent visual +review additionally checks PC/source alignment, pointer hit targets, compact +minimaps, lens cycles/collapse and native interaction. -## Rich GUI verification +The shared renderer/gallery suite passes. The root suite has 87/89 passing tests; +the same two pre-existing loader/syscall-format and watch/string assertions fail +(25/27 debugger tests). No assertions were weakened. The canvas still lacks a +native accessibility tree, and raw hardware Mouse4/5 injection was not tested. -A separate UI agent compared rendered C and V8 frames against the existing -animation design, checked strike/highlight/settled frames, and approved the -final native window. The checked-in GUI test asserts that the stack highlight -changes between strike and settled screenshots, as well as exercising controls. +## Final interactive stop -At 2240×1260, an approximate headless redraw measurement was **3.55 ms/frame -for C and 4.28 ms/frame for V8**, including source/disassembly, registers, -stack, memory and cached content minimaps. `gui/bench/redraw.py` subtracts the -median elapsed time of 30-frame runs from 330-frame runs (three repeats each), -then divides by 300. Both opened their corresponding source-enabled 1,000-step recording; -this is a whole-view cost, not an equal-row renderer comparison. It excludes -live trace reopening and display presentation latency. Raw timings are in -`/tmp/ztrace-gui-redraw-source.json`. The previous, smaller 1920×1080 view measured -1.23 / 2.41 ms/frame for C / V8. +The native GUI remains open with d8 PID 2639255 in Linux tracing-stop state at +`Deoptimizer::New+4`, RIP `0x7fa19dbe3f74`, on native source line 290. +Its `rdx = 0x7fa1e0000ffa` matches the normal `jitWork` TurboFan wrong-map exit's +return PC exactly. The retained JS context is the guard at `0x7fa1e0000c14`, +`jit.js:3`, not the shared exit stub's line 5. -The complete animation gallery and shared renderer tests passed after the -library changes. Existing applications retain their organization: the GUI uses -shared source preparation/alignment, row drawing, address hit geometry and -minimap drawing instead of maintaining a second renderer. +GUI PID 2639214; recording +`/tmp/ztrace-gui-2639214-392301809642450.ztrace`, moment 8. +This session used ordinary default tiering and symbolic native breakpoints. +The complete fallback transition was verified separately by the benchmark above, +so the interactive target can remain at the requested deoptimization entry. -The final native window was independently approved with debug d8 PID 2536162 -in Linux tracing-stop state at breakpoint 1, RIP `0x7f6ee00001a2`: `jbe`, offset -`0x162` inside TurboFan-generated `jitWork`, mapped by V8's line table to the -body at `gui/targets/jit.js:6`. Both the source-to-PC and branch-to-target arrows -are visible. The GUI remains open. The recording is -`/tmp/ztrace-gui-2536121-364024192152453.ztrace`; the screenshot is -`/tmp/ztrace-v8-final-reviewed.png`. Addresses and PIDs identify this session -only. Other test windows were closed. +The independent native review passed after exercising graph/source toggling, +all four opacity settings, and Follow. It restored source/live/follow at 100% +without executing the target. Screenshot: +`/tmp/ztrace-ui-review/native-final-reviewed.jpg`. Native font-shortcut injection +was unavailable through the automation tool's punctuation grammar; the actual +headless key handler passed the nine size/zoom combinations. 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('<Q', data, 40)[0] + size, count = struct.unpack_from('<HH', data, 58) + return any((lambda sh: sh[2] & 4 and sh[3] <= start < sh[3] + sh[5])( + struct.unpack_from('<IIQQQQIIQQ', data, shoff + i * size)) for i in range(count)) + elf, = [path for path in candidates if owns_start(path)] + c.command('symbols ' + str(elf)) + c.command(f'inspect code {start} 128') + return c, js, elf, (start, end), instructions, stop + except BaseException: + c.close() + raise + + +def deoptimization(c, bounds, instructions, source): + transition = [] + wrong_return = int(re.search(r'^(0x[0-9a-f]+)\s+deopt reason\s+\(wrong map\)', c.output(), re.M)[1], 16) + exit_call = max(int(a, 16) for a, _ in instructions if int(a, 16) < wrong_return) + guard = next(int(a, 16) for a, text in instructions if re.match(r'j\w+ ' + hex(exit_call) + r'\b', text)) + c.command('break *' + hex(exit_call)) + for _ in range(30): + generated = c.command('continue') + if generated['pc'] == exit_call: + break + else: + raise AssertionError('wrong-map generated exit not reached') + transition.append(dict(stage='generated wrong-map exit', guard_pc=guard, **generated)) + c.command('clear 5') + # Deoptimizer::New receives the originating generated return PC in rdx. + for _ in range(30): + stop = c.command('continue') + if stop['reason'] == 'breakpoint 2': + regs = c.regs() + if bounds[0] <= regs['rdx'] < bounds[1]: + entry = stop + origin = regs['rdx'] + break + else: + raise AssertionError('no deoptimization originating in jitWork') + shown = c.show() + assert str(source / 'src/deoptimizer/deoptimizer.cc') in shown, shown[:1000] + assert (source / 'src/deoptimizer/deoptimizer.cc').exists() + transition.append(dict(stage='Deoptimizer::New', **entry)) + origin_instruction = guard + c.command(f'inspect code {origin_instruction} 16') + notify = c.command('continue') + assert notify['reason'] == 'breakpoint 3', notify + log = c.output() + wrong_maps = [int(pc, 16) for pc in re.findall(r'^(0x[0-9a-f]+)\s+deopt reason\s+\(wrong map\)', log, re.M)] + assert origin in wrong_maps, (hex(origin), [hex(pc) for pc in wrong_maps]) + assert str(source / 'src/runtime/runtime-compiler.cc') in c.show() + transition.append(dict(stage='Runtime_NotifyDeoptimized', **notify)) + c.command('break Builtins_InterpreterEnterAtBytecode') + fallback = c.command('continue') + assert fallback['reason'] == 'breakpoint 6', fallback + transition.append(dict(stage='InterpreterEnterAtBytecode', **fallback)) + # Prove dispatch into the bytecode handler, not just the frame-reconstruction log. + dispatch = None + for _ in range(300): + step = c.command('stepi') + shown = c.show() + rip = next(line for line in shown.splitlines() if line.startswith(' rip ')) + if 'Builtins_InterpreterEntryTrampoline' not in rip and 'Builtins_InterpreterEnterAtBytecode' not in rip and re.search(r'Builtins_(GetNamedProperty|LdaNamedProperty|GetNamedPropertyFromSuper)', rip): + dispatch = dict(stage='Ignition property bytecode handler', symbol=rip.strip(), **step) + break + assert dispatch, c.show()[:1600] + transition.append(dispatch) + for bp in (2, 3, 6): + c.command(f'clear {bp}') + c.command('continue', status='exited') + log = c.output() + assert re.search(r'reason: wrong map.*<JSFunction jitWork', log), log[-6000:] + assert 'mismatch 499509' in log and 'fallback 499507' in log + return dict(reason='wrong map', function='jitWork', optimized_tier='TurboFan', fallback_tier='Ignition', + origin_return_pc=origin, origin_instruction=origin_instruction, deopt_call=exit_call, stops=transition) 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() |
