generate.py 2.59 KB
Newer Older
1
#!/usr/bin/env python3
2 3 4 5 6 7 8

# 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
9
from pathlib import Path
10 11 12 13 14

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`.'
)
15
parser.add_argument(
16
    'v8_target_cpu', help='target cpu to build the profile for: x64 or arm64')
17 18 19
parser.add_argument(
    '--target-cpu',
    default=None,
20
    help='target cpu for V8 binary (for simulator builds), by default it\'s equal to `v8_target_cpu`'
21
)
22 23
parser.add_argument(
    'benchmark_path',
24 25
    help='path to benchmark runner .js file, usually JetStream2\'s `cli.js`',
    type=Path)
26 27
parser.add_argument(
    '--out-path',
28 29 30
    default=Path("out"),
    help='directory to be used for building V8, by default `./out`',
    type=Path)
31 32 33

args = parser.parse_args()

34
if args.target_cpu == None:
35 36 37 38 39 40
  args.target_cpu = args.v8_target_cpu


def run(cmd, **kwargs):
  print(f"# CMD: {cmd} {kwargs}")
  return subprocess.run(cmd, **kwargs)
41

42 43

def try_start_goma():
44
  res = run(["goma_ctl", "ensure_start"])
45 46 47 48 49 50 51 52
  print(res.returncode)
  has_goma = res.returncode == 0
  print("Detected Goma:", has_goma)
  return has_goma



def build_d8(path, gn_args):
53
  if not path.exists():
54
    path.mkdir(parents=True, exist_ok=True)
55 56 57 58 59
  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()
60 61


62 63
tools_pgo_dir = Path(__file__).parent
v8_path = tools_pgo_dir.parent.parent
64

65 66 67
if not args.benchmark_path.is_file() or args.benchmark_path.suffix != ".js":
  print(f"Invalid benchmark argument: {args.benchmark_path}")
  exit(1)
68

69
has_goma_str = "true" if try_start_goma() else "false"
70

71
GN_ARGS_TEMPLATE = f"""\
72
is_debug = false
73 74 75
target_cpu = "{args.target_cpu}"
v8_target_cpu = "{args.v8_target_cpu}"
use_goma = {has_goma_str}
76
v8_enable_builtins_profiling = true
77 78 79 80
"""

for arch, gn_args in [(args.v8_target_cpu, GN_ARGS_TEMPLATE)]:
  build_dir = args.out_path / f"{arch}.release.generate_builtin_pgo_profile"
81
  d8_path = build_d8(build_dir, gn_args)
82 83 84 85 86 87 88 89
  benchmark_dir = args.benchmark_path.parent
  benchmark_file = args.benchmark_path.name
  log_path = build_dir / "v8.builtins.pgo"
  run([d8_path, f"--turbo-profiling-output={log_path}", benchmark_file],
      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])