1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#!/usr/bin/env python3
"""Compare the same controller's recorded instruction steps on C and debug V8."""
import argparse
import json
import math
import os
from pathlib import Path
import re
import select
import statistics
import subprocess
import time
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)
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)
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],
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):
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))
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', str(ROOT / 'gui/targets/jit.js')])]:
start = time.perf_counter_ns()
c = Controller(name, argv)
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
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))
print(name, json.dumps(results[name]), flush=True)
finally:
c.close()
if name == 'v8':
shown = subprocess.run([str(a.ztrace.resolve()), 'show', str(a.out / 'v8.ztrace'), '-1'],
check=True, capture_output=True, text=True)
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')
|