-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: With the perf trampoline writing to the perf-map files, we want to have a C-API to unify writing to the perf-map files to avoid file corruption from simultaneous writes. We are trying to upstream the API here python/cpython#103546. More details about the motivation is in the PR. In addition to introducing the C-API, we also change JIT to utilize the new C-API. Reviewed By: czardoz Differential Revision: D45421966 fbshipit-source-id: d270cc753a245f93cbfe3d723d0880595fef45f2
- Loading branch information
1 parent
a88f497
commit c475629
Showing
12 changed files
with
359 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
|
||
.. highlight:: c | ||
|
||
.. _perfmaps: | ||
|
||
Support for Perf Maps | ||
---------------------- | ||
|
||
On supported platforms (as of this writing, only Linux), the runtime can take | ||
advantage of *perf map files* to make Python functions visible to an external | ||
profiling tool (such as `perf <https://perf.wiki.kernel.org/index.php/Main_Page>`_). | ||
A running process may create a file in the `/tmp` directory, which contains entries | ||
that can map a section of executable code to a name. This interface is described in the | ||
`documentation of the Linux Perf tool <https://git.kernel.org/pub/scm/linux/ | ||
kernel/git/torvalds/linux.git/tree/tools/perf/Documentation/jit-interface.txt>`_. | ||
|
||
In Python, these helper APIs can be used by libraries and features that rely | ||
on generating machine code on the fly. | ||
|
||
.. c:function:: int PyUnstable_PerfMapState_Init(void) | ||
Open the `/tmp/perf-$pid.map` file, unless it's already opened, and create | ||
a lock to ensure thread-safe writes to the file (provided the writes are | ||
done through :c:func:`PyUnstable_WritePerfMapEntry`). Normally, there's no need | ||
to call this explicitly, and it is safe to directly use :c:func:`PyUnstable_WritePerfMapEntry` | ||
in your code. If the state isn't already initialized, it will be created on | ||
the first call. | ||
.. c:function:: int PyUnstable_WritePerfMapEntry(const void *code_addr, unsigned int code_size, const char *entry_name) | ||
Write one single entry to the `/tmp/perf-$pid.map` file. This function is | ||
thread safe. Here is what an example entry looks like:: | ||
# address size name | ||
0x7f3529fcf759 b py::bar:/run/t.py | ||
Extensions are encouraged to directly call this API when needed, instead of | ||
separately initializing the state by calling :c:func:`PyUnstable_PerfMapState_Init`. | ||
.. c:function:: int PyUnstable_PerfMapState_Fini(void) | ||
Close the perf map file, which was opened in `PyUnstable_PerfMapState_Init`. This | ||
API is called by the runtime itself, during interpreter shut-down. In general, | ||
there shouldn't be a reason to explicitly call this, except to handle specific | ||
scenarios such as forking. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import os | ||
import sys | ||
import unittest | ||
|
||
from _testinternalcapi import perf_map_state_teardown, write_perf_map_entry | ||
|
||
if sys.platform != 'linux': | ||
raise unittest.SkipTest('Linux only') | ||
|
||
|
||
class TestPerfMapWriting(unittest.TestCase): | ||
def test_write_perf_map_entry(self): | ||
self.assertEqual(write_perf_map_entry(0x1234, 5678, "entry1"), 0) | ||
self.assertEqual(write_perf_map_entry(0x2345, 6789, "entry2"), 0) | ||
with open(f"/tmp/perf-{os.getpid()}.map") as f: | ||
perf_file_contents = f.read() | ||
self.assertIn("1234 162e entry1", perf_file_contents) | ||
self.assertIn("2345 1a85 entry2", perf_file_contents) | ||
perf_map_state_teardown() |
5 changes: 5 additions & 0 deletions
5
Misc/NEWS.d/next/C API/2023-04-14-23-05-52.gh-issue-103295.GRHY1Z.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Introduced :c:func:`PyUnstable_WritePerfMapEntry`, :c:func:`PyUnstable_PerfMapState_Init` and | ||
:c:func:`PyUnstable_PerfMapState_Fini`. These allow extension modules (JIT compilers in | ||
particular) to write to perf-map files in a thread safe manner. The | ||
:doc:`../howto/perf_profiling` also uses these APIs to write | ||
entries in the perf-map file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.