Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for profiling benchmarks using perf-record #214

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions pyperf/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

import abc
import importlib.metadata
import os.path
import shlex
import subprocess
import sys
import tempfile
import uuid


def get_hooks():
Expand Down Expand Up @@ -108,3 +113,58 @@ def __enter__(self):

def __exit__(self, _exc_type, _exc_value, _traceback):
sys._stats_off()


class perf_record(HookBase):
"""Profile the benchmark using perf-record.

Profile data is written to the current directory directory by default, or
to the value of the `PYPERF_PERF_RECORD_DATA_DIR` environment variable, if
it is provided.

Profile data files have a basename of the form `perf.data.<uuid>`

The value of the `PYPERF_PERF_RECORD_EXTRA_OPTS` environment variable is
appended to the command line of perf-record, if provided.
"""

def mkfifo(self, tmpdir, basename):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we move down mkfifo / exec_perf_cmd to under the dunder methods?

path = os.path.join(tmpdir, basename)
os.mkfifo(path)
return path

def exec_perf_cmd(self, cmd):
self.ctl_fd.write(f"{cmd}\n")
self.ctl_fd.flush()
res = self.ack_fd.readline()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
res = self.ack_fd.readline()
_ = self.ack_fd.readline()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's for passing the CI


def __init__(self):
self.tempdir = tempfile.TemporaryDirectory()
self.ctl_fifo = self.mkfifo(self.tempdir.name, "ctl_fifo")
self.ack_fifo = self.mkfifo(self.tempdir.name, "ack_fifo")
perf_data_dir = os.environ.get("PYPERF_PERF_RECORD_DATA_DIR", "")
perf_data_basename = f"perf.data.{uuid.uuid4()}"
cmd = ["perf", "record",
"--pid", str(os.getpid()),
"--output", os.path.join(perf_data_dir, perf_data_basename),
"--control", f"fifo:{self.ctl_fifo},{self.ack_fifo}"]
extra_opts = os.environ.get("PYPERF_PERF_RECORD_EXTRA_OPTS", "")
cmd += shlex.split(extra_opts)
self.perf = subprocess.Popen(
cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
self.ctl_fd = open(self.ctl_fifo, "w")
self.ack_fd = open(self.ack_fifo, "r")

def __enter__(self):
self.exec_perf_cmd("enable")

def __exit__(self, _exc_type, _exc_value, _traceback):
self.exec_perf_cmd("disable")

def teardown(self, metadata):
try:
self.exec_perf_cmd("stop")
self.perf.wait(timeout=120)
finally:
self.ctl_fd.close()
self.ack_fd.close()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dev = [
pyperf = "pyperf.__main__:main"

[project.entry-points."pyperf.hook"]
perf_record = "pyperf._hooks:perf_record"
pystats = "pyperf._hooks:pystats"
_test_hook = "pyperf._hooks:_test_hook"

Expand Down
Loading