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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
#!/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.
from datetime import datetime
from datetime import timedelta
import optparse
import os
from pathlib import Path
import shlex
import shutil
import signal
import subprocess
import sys
import tempfile
import time
import psutil
# ==============================================================================
usage = """Usage: %prog [OPTION]... $D8_BIN [D8_OPTION]... [FILE]
This script runs linux-perf with custom V8 logging to get support to resolve
JS function names.
The perf data is written to OUT_DIR separate by renderer process.
See https://v8.dev/docs/linux-perf for more detailed instructions.
See $D8_BIN --help for more flags/options
"""
parser = optparse.OptionParser(usage=usage)
# Stop parsing options after D8_BIN
parser.disable_interspersed_args()
parser.add_option(
'--perf-data-dir',
default=None,
metavar="OUT_DIR",
help=("Output directory for linux perf profile files "
"Defaults to './perf_profile_d8_%Y-%m-%d_%H%M%S'"))
parser.add_option("--timeout", type=float, help="Stop d8 after N seconds")
parser.add_option(
"--scope-to-mark-measure",
action="store_true",
default=False,
help="Scope perf recording to start at performance.mark events and stop at performance.measure events"
)
parser.add_option(
"--skip-pprof",
action="store_true",
default=False,
help="Skip pprof upload (relevant for Googlers only)")
d8_options = optparse.OptionGroup(
parser, "d8-forwarded Options",
"THese options are for a better script experience that are forward directly"
"to d8. Any other d8 option can be passed after the '--' arguments"
"separator.")
d8_options.add_option(
"--perf-prof-annotate-wasm",
help="Load wasm source map and provide annotate wasm code.",
action="store_true",
default=False)
d8_options.add_option(
"--no-interpreted-frames-native-stack",
help="For profiling v8 copies the interpreter entry trampoline for every "
"interpreted function. This makes interpreted functions identifiable on the "
"native stack at cost of a slight performance and memory overhead.",
action="store_true",
default=False)
parser.add_option_group(d8_options)
perf_options = optparse.OptionGroup(
parser, "perf-forward options", """
These options are forward directly to the `perf record` command and can be
used to manually tweak how profiling works.
See `perf record --help for more` informationn
""")
perf_options.add_option(
"--freq",
default="10000",
help="Sampling frequency, either 'max' or a number in herz. "
"Might be reduced depending on the platform. "
"Default is 10000.")
perf_options.add_option(
"--count", default=None, help="Event period to sample. Not set by default.")
perf_options.add_option("--call-graph", default="fp", help="Defaults tp 'fp'")
perf_options.add_option("--clockid", default="mono", help="Defaults to 'mono'")
perf_options.add_option("--event", default=None, help="Not set by default.")
perf_options.add_option(
"--raw-samples",
default=None,
help="Collect raw samples. Not set by default")
perf_options.add_option(
"--no-inherit",
action="store_true",
default=False,
help=" Child tasks do not inherit counters.")
parser.add_option_group(perf_options)
# ==============================================================================
def log(*args):
print("")
print("=" * 80)
print(*args)
print("=" * 80)
def main():
cleanup = []
try:
# ==========================================================================
(options, args) = parser.parse_args()
if options.freq and options.count:
parser.error("--freq and --count are mutually exclusive. "
"See `perf record --help' for more details.")
if len(args) == 0:
parser.error("No d8 binary provided")
raw_perf_args = []
d8_bin = None
while args:
additional_arg = args.pop(0)
maybe_d8_bin = Path(additional_arg).absolute()
if maybe_d8_bin.exists():
d8_bin = maybe_d8_bin
break
else:
raw_perf_args.append(additional_arg)
if not d8_bin:
parser.error(f"D8 '{d8_bin}' does not exist")
if options.perf_data_dir is None:
options.perf_data_dir = Path.cwd()
else:
options.perf_data_dir = Path(options.perf_data_dir).absolute()
options.perf_data_dir.mkdir(parents=True, exist_ok=True)
if not options.perf_data_dir.is_dir():
parser.error(f"--perf-data-dir={options.perf_data_dir} "
"is not an directory or does not exist.")
if options.timeout and options.timeout < 0:
parser.error("--timeout should be a positive number")
# ==========================================================================
cmd = [
str(d8_bin), "--perf-prof", "--perf-prof-path",
options.perf_data_dir.as_posix()
]
if not options.no_interpreted_frames_native_stack:
cmd += ["--interpreted-frames-native-stack"]
if options.perf_prof_annotate_wasm:
cmd += ["--perf-prof-annotate-wasm"]
if options.scope_to_mark_measure:
tmp_fifo_dir = tempfile.mkdtemp()
cleanup.append(lambda: shutil.rmtree(tmp_fifo_dir))
perf_ctl_fifo_path = os.path.join(tmp_fifo_dir, "perf_ctl.fifo")
perf_ack_fifo_path = os.path.join(tmp_fifo_dir, "perf_ack.fifo")
os.mkfifo(perf_ctl_fifo_path)
os.mkfifo(perf_ack_fifo_path)
perf_ctl_fd = os.open(perf_ctl_fifo_path, os.O_RDWR)
cleanup.append(lambda: os.close(perf_ctl_fd))
perf_ack_fd = os.open(perf_ack_fifo_path, os.O_RDWR)
cleanup.append(lambda: os.close(perf_ack_fd))
cmd += [
f"--perf-ctl-fd={perf_ctl_fd}", f"--perf-ack-fd={perf_ack_fd}",
"--scope-linux-perf-to-mark-measure"
]
pass_fds = [perf_ctl_fd, perf_ack_fd]
else:
pass_fds = []
cmd += args
log("D8 CMD: ", shlex.join(cmd))
datetime_str = datetime.now().strftime("%Y-%m-%d_%H%M%S")
perf_data_file = options.perf_data_dir / f"d8_{datetime_str}.perf.data"
perf_cmd = [
"perf", "record", f"--call-graph={options.call_graph}",
f"--clockid={options.clockid}", f"--output={perf_data_file}"
]
if options.freq:
perf_cmd += [f"--freq={options.freq}"]
if options.count:
perf_cmd += [f"--count={options.count}"]
if options.raw_samples:
perf_cmd += [f"--raw_samples={options.raw_samples}"]
if options.event:
perf_cmd += [f"--event={options.event}"]
if options.no_inherit:
perf_cmd += [f"--no-inherit"]
if raw_perf_args:
perf_cmd.extend(raw_perf_args)
if options.scope_to_mark_measure:
perf_cmd += [f"--control=fd:{perf_ctl_fd},{perf_ack_fd}", "--delay=-1"]
cmd = perf_cmd + ["--"] + cmd
log("LINUX PERF CMD: ", shlex.join(cmd))
def wait_for_process_timeout(process):
delta = timedelta(seconds=options.timeout)
start_time = datetime.now()
while True:
if (datetime.now() - start_time) >= delta:
return False
processHasStopped = process.poll() is not None
if processHasStopped:
return True
time.sleep(0.1)
return False
if options.timeout is None:
try:
subprocess.check_call(cmd, pass_fds=pass_fds)
log("Waiting for linux-perf to flush all perf data")
time.sleep(1)
except Exception as e:
log("ERROR running perf record: ", e)
else:
process = subprocess.Popen(cmd, pass_fds=pass_fds)
if not wait_for_process_timeout(process):
log(f"QUITING d8 processes after {options.timeout}s timeout")
current_process = psutil.Process()
children = current_process.children(recursive=True)
for child in children:
if "d8" in child.name():
print(f" quitting PID={child.pid}")
child.send_signal(signal.SIGQUIT)
log("Waiting for linux-perf to flush all perf data")
time.sleep(1)
return_status = process.poll()
if return_status is None:
log("Force quitting linux-perf")
process.send_signal(signal.SIGQUIT)
process.wait()
elif return_status != 0:
log("ERROR running perf record")
# ==========================================================================
log("POST PROCESSING: Injecting JS symbols")
def inject_v8_symbols(perf_dat_file):
output_file = perf_dat_file.with_suffix(".data.jitted")
cmd = [
"perf", "inject", "--jit", f"--input={perf_dat_file.absolute()}",
f"--output={output_file.absolute()}"
]
try:
subprocess.check_call(cmd)
print(f"Processed: {output_file}")
except:
print(shlex.join(cmd))
return None
return output_file
result = inject_v8_symbols(perf_data_file)
if result is None:
print(
"No perf files were successfully processed"
f" Check for errors or partial results in '{options.perf_data_dir}'")
return 1
log(f"RESULTS in '{options.perf_data_dir}'")
BYTES_TO_MIB = 1 / 1024 / 1024
print(f"{result.name:67}{(result.stat().st_size*BYTES_TO_MIB):10.2f}MiB")
# ==========================================================================
if not shutil.which('gcertstatus') or options.skip_pprof:
log("ANALYSIS")
print(f"perf report --input='{result}'")
print(f"pprof '{result}'")
return 0
log("PPROF")
has_gcert = False
try:
print("# Checking gcert status for googlers")
subprocess.check_call("gcertstatus >&/dev/null || gcert", shell=True)
has_gcert = True
cmd = [
"pprof", "-symbolize=local", "-flame",
f"-add_comment={shlex.join(sys.argv)}",
str(result.absolute())
]
print("# Processing and uploading to pprofresult")
url = subprocess.check_output(cmd).decode('utf-8').strip()
print(url)
except subprocess.CalledProcessError as e:
if has_gcert:
raise Exception("Could not generate pprof results") from e
print("# Please run `gcert` for generating pprof results")
print(f"pprof -symbolize=local -flame {result}")
except KeyboardInterrupt:
return 1
finally:
for cleanup_func in reversed(cleanup):
cleanup_func()
return 0
sys.exit(main())
|