From 9a85c5607f47a0085b6c344e962a6cb90fb3f61b Mon Sep 17 00:00:00 2001 From: Yash Date: Wed, 7 Jan 2026 00:17:10 +0530 Subject: [PATCH 1/6] first commit perfmaps --- Doc/c-api/perfmaps.rst | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Doc/c-api/perfmaps.rst b/Doc/c-api/perfmaps.rst index 76a1e9f528dc70..098b8c18ff2498 100644 --- a/Doc/c-api/perfmaps.rst +++ b/Doc/c-api/perfmaps.rst @@ -49,3 +49,34 @@ Note that holding an :term:`attached thread state` is not required for these API This 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. + + +.. c:function:: int PyUnstable_CopyPerfMapFile(const char *parent_filename) + + Open the ``/tmp/perf-$pid.map`` file and append the content of *parent_filename* + to it. + + :param parent_filename: The name of the file to copy. + :return: 0 on success, -1 on failure. + + .. versionadded:: 3.13 + + +.. c:function:: int PyUnstable_PerfTrampoline_CompileCode(PyCodeObject *code) + + Compile the given code object using the current perf trampoline. + + :param code: The code object to compile. + :return: 0 on success, -1 on failure. + + .. versionadded:: 3.13 + + +.. c:function:: int PyUnstable_PerfTrampoline_SetPersistAfterFork(int enable) + + Set whether the perf trampoline should persist after a fork. + + :param enable: 1 to enable, 0 to disable. + :return: 0 on success, -1 on failure. + + .. versionadded:: 3.13 From 5e050f1939d8551a381f84ba24f6f249805f1e23 Mon Sep 17 00:00:00 2001 From: Yash Date: Wed, 7 Jan 2026 00:44:01 +0530 Subject: [PATCH 2/6] fix 1 perfmaps --- Doc/c-api/perfmaps.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/c-api/perfmaps.rst b/Doc/c-api/perfmaps.rst index 098b8c18ff2498..dbce1c1e389658 100644 --- a/Doc/c-api/perfmaps.rst +++ b/Doc/c-api/perfmaps.rst @@ -56,8 +56,8 @@ Note that holding an :term:`attached thread state` is not required for these API Open the ``/tmp/perf-$pid.map`` file and append the content of *parent_filename* to it. - :param parent_filename: The name of the file to copy. - :return: 0 on success, -1 on failure. + This function is only available on platforms that support perf maps (currently + Linux). Return ``0`` on success, ``-1`` on failure. .. versionadded:: 3.13 From bd7eb99ee99bd129bb646384f1809c4426313175 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 8 Jan 2026 22:24:34 +0530 Subject: [PATCH 3/6] fix 2 perfmaps --- Doc/c-api/perfmaps.rst | 17 +++++++++++++++++ Tools/check-c-api-docs/ignored_c_api.txt | 4 ---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Doc/c-api/perfmaps.rst b/Doc/c-api/perfmaps.rst index dbce1c1e389658..d908ebebdfc74e 100644 --- a/Doc/c-api/perfmaps.rst +++ b/Doc/c-api/perfmaps.rst @@ -50,6 +50,11 @@ Note that holding an :term:`attached thread state` is not required for these API general, there shouldn't be a reason to explicitly call this, except to handle specific scenarios such as forking. +These unstable functions let you access and set perf map information +about the current frame from C code. + +Note: Appends the content of the parent frame to the current one in perf maps. +Just like in frameobject.h. .. c:function:: int PyUnstable_CopyPerfMapFile(const char *parent_filename) @@ -66,6 +71,11 @@ Note that holding an :term:`attached thread state` is not required for these API Compile the given code object using the current perf trampoline. + The "current" trampoline is the one set by the runtime or the most recent + :c:func:`PyUnstable_PerfTrampoline_SetPersistAfterFork` call. + + If no trampoline is set, falls back to normal compilation (no perf map entry). + :param code: The code object to compile. :return: 0 on success, -1 on failure. @@ -76,6 +86,13 @@ Note that holding an :term:`attached thread state` is not required for these API Set whether the perf trampoline should persist after a fork. + * If ``enable`` is true (non-zero): perf map file remains open/valid post-fork. + Child process inherits all existing perf map entries. + * If ``enable`` is false (zero): perf map closes post-fork. + Child process gets empty perf map. + + Default: false (clears on fork). + :param enable: 1 to enable, 0 to disable. :return: 0 on success, -1 on failure. diff --git a/Tools/check-c-api-docs/ignored_c_api.txt b/Tools/check-c-api-docs/ignored_c_api.txt index e81ffd51e193b2..6f79dd2e92e727 100644 --- a/Tools/check-c-api-docs/ignored_c_api.txt +++ b/Tools/check-c-api-docs/ignored_c_api.txt @@ -68,10 +68,6 @@ PyStdPrinter_Type Py_UniversalNewlineFgets # cpython/setobject.h PySet_MINSIZE -# cpython/ceval.h -PyUnstable_CopyPerfMapFile -PyUnstable_PerfTrampoline_CompileCode -PyUnstable_PerfTrampoline_SetPersistAfterFork # cpython/genobject.h PyAsyncGenASend_CheckExact # cpython/longintrepr.h From e5a2111a858257fff4210ba60fefc140b82551c2 Mon Sep 17 00:00:00 2001 From: Yash Date: Sat, 31 Jan 2026 23:22:35 +0530 Subject: [PATCH 4/6] perfmaps fix 3 --- Doc/c-api/perfmaps.rst | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Doc/c-api/perfmaps.rst b/Doc/c-api/perfmaps.rst index d908ebebdfc74e..4679cf4567a77c 100644 --- a/Doc/c-api/perfmaps.rst +++ b/Doc/c-api/perfmaps.rst @@ -50,12 +50,6 @@ Note that holding an :term:`attached thread state` is not required for these API general, there shouldn't be a reason to explicitly call this, except to handle specific scenarios such as forking. -These unstable functions let you access and set perf map information -about the current frame from C code. - -Note: Appends the content of the parent frame to the current one in perf maps. -Just like in frameobject.h. - .. c:function:: int PyUnstable_CopyPerfMapFile(const char *parent_filename) Open the ``/tmp/perf-$pid.map`` file and append the content of *parent_filename* @@ -66,7 +60,6 @@ Just like in frameobject.h. .. versionadded:: 3.13 - .. c:function:: int PyUnstable_PerfTrampoline_CompileCode(PyCodeObject *code) Compile the given code object using the current perf trampoline. @@ -81,7 +74,6 @@ Just like in frameobject.h. .. versionadded:: 3.13 - .. c:function:: int PyUnstable_PerfTrampoline_SetPersistAfterFork(int enable) Set whether the perf trampoline should persist after a fork. From 85b98cff27e94c0d989cd02d13d5f70fd42311da Mon Sep 17 00:00:00 2001 From: Yash Date: Sat, 31 Jan 2026 23:42:48 +0530 Subject: [PATCH 5/6] fix capi_ignored --- Tools/check-c-api-docs/ignored_c_api.txt | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/Tools/check-c-api-docs/ignored_c_api.txt b/Tools/check-c-api-docs/ignored_c_api.txt index d79299a2e25104..c1f8fd8cfd5141 100644 --- a/Tools/check-c-api-docs/ignored_c_api.txt +++ b/Tools/check-c-api-docs/ignored_c_api.txt @@ -71,20 +71,6 @@ PyDescr_TYPE PyWrapperFlag_KEYWORDS # cpython/fileobject.h Py_UniversalNewlineFgets -# cpython/setobject.h -PySet_MINSIZE -# cpython/genobject.h -PyAsyncGenASend_CheckExact -# cpython/longintrepr.h -PyLong_BASE -PyLong_MASK -PyLong_SHIFT -# cpython/pyerrors.h -PyException_HEAD -# cpython/ceval.h -PyUnstable_CopyPerfMapFile -PyUnstable_PerfTrampoline_CompileCode -PyUnstable_PerfTrampoline_SetPersistAfterFork # cpython/pyframe.h PyUnstable_EXECUTABLE_KINDS PyUnstable_EXECUTABLE_KIND_BUILTIN_FUNCTION From 5c89eea209a6193ff99b6e365dbc41ca2536b3fc Mon Sep 17 00:00:00 2001 From: Yash Date: Mon, 2 Feb 2026 21:39:22 +0530 Subject: [PATCH 6/6] perfmaps fix 4 --- Doc/c-api/perfmaps.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/c-api/perfmaps.rst b/Doc/c-api/perfmaps.rst index 4679cf4567a77c..98b88e2553847d 100644 --- a/Doc/c-api/perfmaps.rst +++ b/Doc/c-api/perfmaps.rst @@ -55,8 +55,8 @@ Note that holding an :term:`attached thread state` is not required for these API Open the ``/tmp/perf-$pid.map`` file and append the content of *parent_filename* to it. - This function is only available on platforms that support perf maps (currently - Linux). Return ``0`` on success, ``-1`` on failure. + This function is available on all platforms but only generates output on platforms + that support perf maps (currently only Linux). On other platforms, it does nothing. .. versionadded:: 3.13