| 359 | |
| 360 | |
| 361 | def run_perf(cwd, *args, use_jit=False, **env_vars): |
| 362 | env = os.environ.copy() |
| 363 | if env_vars: |
| 364 | env.update(env_vars) |
| 365 | env["PYTHON_JIT"] = "0" |
| 366 | output_file = cwd + "/perf_output.perf" |
| 367 | if not use_jit: |
| 368 | base_cmd = ( |
| 369 | "perf", |
| 370 | "record", |
| 371 | "--no-buildid", |
| 372 | "--no-buildid-cache", |
| 373 | "-g", |
| 374 | "--call-graph=fp", |
| 375 | "-o", output_file, |
| 376 | "--" |
| 377 | ) |
| 378 | else: |
| 379 | base_cmd = ( |
| 380 | "perf", |
| 381 | "record", |
| 382 | "--no-buildid", |
| 383 | "--no-buildid-cache", |
| 384 | "-g", |
| 385 | "--call-graph=dwarf,65528", |
| 386 | "-F99", |
| 387 | "-k1", |
| 388 | "-o", |
| 389 | output_file, |
| 390 | "--", |
| 391 | ) |
| 392 | proc = subprocess.run( |
| 393 | base_cmd + args, |
| 394 | stdout=subprocess.PIPE, |
| 395 | stderr=subprocess.PIPE, |
| 396 | env=env, |
| 397 | text=True, |
| 398 | ) |
| 399 | if proc.returncode: |
| 400 | print(proc.stderr, file=sys.stderr) |
| 401 | raise ValueError(f"Perf failed with return code {proc.returncode}") |
| 402 | |
| 403 | if use_jit: |
| 404 | jit_output_file = cwd + "/jit_output.dump" |
| 405 | command = ("perf", "inject", "-j", "-i", output_file, "-o", jit_output_file) |
| 406 | proc = subprocess.run( |
| 407 | command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, env=env, text=True |
| 408 | ) |
| 409 | if proc.returncode: |
| 410 | print(proc.stderr, file=sys.stderr) |
| 411 | raise ValueError(f"Perf failed with return code {proc.returncode}") |
| 412 | # Copy the jit_output_file to the output_file |
| 413 | os.rename(jit_output_file, output_file) |
| 414 | |
| 415 | base_cmd = ("perf", "script") |
| 416 | proc = subprocess.run( |
| 417 | ("perf", "script", "-i", output_file), |
| 418 | stdout=subprocess.PIPE, |