summaryrefslogtreecommitdiff
path: root/gui/bench/steps.py
blob: 29067597577e1eb51574d90a4cfee9811049fd68 (plain) (blame)
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
#!/usr/bin/env python3
"""Compare source-enabled C/V8 steps; verify a natural wrong-map deoptimization separately."""
import argparse
import json
import math
from pathlib import Path
import statistics
import subprocess
import time
from control import ROOT, Controller, natural, deoptimization


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 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))


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, js, elf, bounds, instructions, stop = natural(a.ztrace, a.d8, a.out, a.source)
    try:
        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()
        elf.unlink(missing_ok=True)


if __name__ == '__main__':
    main()