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
|
#!/usr/bin/env python3
# Copyright 2022 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can
# be found in the LICENSE file.
import subprocess
import argparse
from pathlib import Path
parser = argparse.ArgumentParser(
description='Generate builtin PGO profiles. ' +
'The script has to be run from the root of a V8 checkout and updates the profiles in `tools/builtins-pgo`.'
)
parser.add_argument(
'v8_target_cpu', help='target cpu to build the profile for: x64 or arm64')
parser.add_argument(
'--target-cpu',
default=None,
help='target cpu for V8 binary (for simulator builds), by default it\'s equal to `v8_target_cpu`'
)
parser.add_argument(
'--use-qemu',
default=False,
help='Use qemu for running cross-compiled V8 binary.',
action=argparse.BooleanOptionalAction)
parser.add_argument(
'benchmark_path',
help='path to benchmark runner .js file, usually JetStream2\'s `cli.js`',
type=Path)
parser.add_argument(
'--out-path',
default=Path("out"),
help='directory to be used for building V8, by default `./out`',
type=Path)
args = parser.parse_args()
if args.target_cpu == None:
args.target_cpu = args.v8_target_cpu
def run(cmd, **kwargs):
print(f"# CMD: {cmd} {kwargs}")
return subprocess.run(cmd, **kwargs)
def try_start_goma():
res = run(["goma_ctl", "ensure_start"])
print(res.returncode)
has_goma = res.returncode == 0
print("Detected Goma:", has_goma)
return has_goma
def build_d8(path, gn_args):
if not path.exists():
path.mkdir(parents=True, exist_ok=True)
with (path / "args.gn").open("w") as f:
f.write(gn_args)
run(["gn", "gen", path])
run(["autoninja", "-C", path, "d8"])
return (path / "d8").absolute()
tools_pgo_dir = Path(__file__).parent
v8_path = tools_pgo_dir.parent.parent
if not args.benchmark_path.is_file() or args.benchmark_path.suffix != ".js":
print(f"Invalid benchmark argument: {args.benchmark_path}")
exit(1)
has_goma_str = "true" if try_start_goma() else "false"
cmd_prefix = []
if args.use_qemu:
if args.v8_target_cpu == "arm":
cmd_prefix = ["qemu-arm", "-L", "/usr/arm-linux-gnueabihf/"]
elif args.v8_target_cpu == "arm64":
cmd_prefix = ["qemu-aarch64", "-L", "/usr/aarch64-linux-gnu/"]
else:
print(f"{args.v8_target_cpu} binaries can't be run with qemu")
exit(1)
GN_ARGS_TEMPLATE = f"""\
is_debug = false
target_cpu = "{args.target_cpu}"
v8_target_cpu = "{args.v8_target_cpu}"
use_goma = {has_goma_str}
v8_enable_builtins_profiling = true
"""
for arch, gn_args in [(args.v8_target_cpu, GN_ARGS_TEMPLATE)]:
build_dir = args.out_path / f"{arch}.release.generate_builtin_pgo_profile"
d8_path = build_d8(build_dir, gn_args)
benchmark_dir = args.benchmark_path.parent
benchmark_file = args.benchmark_path.name
log_path = (build_dir / "v8.builtins.pgo").absolute()
cmd = cmd_prefix + [
d8_path, f"--turbo-profiling-output={log_path}", benchmark_file
]
run(cmd, cwd=benchmark_dir)
get_hints_path = tools_pgo_dir / "get_hints.py"
profile_path = tools_pgo_dir / f"{arch}.profile"
run([get_hints_path, log_path, profile_path])
|