From 398e38f951b9c0bf4cca56c07b13261afb3d68f2 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Tue, 1 Oct 2024 17:25:11 +0100 Subject: [PATCH 1/3] gh-124855: Don't allow the JIT and perf support to be active at the same time Signed-off-by: Pablo Galindo --- ...-10-01-17-31-32.gh-issue-124855.sdsv_H.rst | 2 ++ Python/pylifecycle.c | 21 ++++++++++++------- Python/sysmodule.c | 8 +++++++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-10-01-17-31-32.gh-issue-124855.sdsv_H.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-01-17-31-32.gh-issue-124855.sdsv_H.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-01-17-31-32.gh-issue-124855.sdsv_H.rst new file mode 100644 index 00000000000000..b65a5e6ac11c76 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-01-17-31-32.gh-issue-124855.sdsv_H.rst @@ -0,0 +1,2 @@ +Don't allow the JIT and perf support to be active at the same time. Patch by +Pablo Galindo diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 8aebbe5c405ffe..9be1a5dac2fc6e 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -1300,14 +1300,21 @@ init_interp_main(PyThreadState *tstate) enabled = *env != '0'; } if (enabled) { - PyObject *opt = _PyOptimizer_NewUOpOptimizer(); - if (opt == NULL) { - return _PyStatus_ERR("can't initialize optimizer"); - } - if (_Py_SetTier2Optimizer((_PyOptimizerObject *)opt)) { - return _PyStatus_ERR("can't install optimizer"); + if (config->perf_profiling > 0) { + (void)PyErr_WarnEx( + PyExc_RuntimeWarning, + "JIT deactivated as perf profiling support is active", + 0); + } else { + PyObject *opt = _PyOptimizer_NewUOpOptimizer(); + if (opt == NULL) { + return _PyStatus_ERR("can't initialize optimizer"); + } + if (_Py_SetTier2Optimizer((_PyOptimizerObject *)opt)) { + return _PyStatus_ERR("can't install optimizer"); + } + Py_DECREF(opt); } - Py_DECREF(opt); } } #endif diff --git a/Python/sysmodule.c b/Python/sysmodule.c index ac343a8048e008..1be9a70d2ba952 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2287,6 +2287,14 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend) /*[clinic end generated code: output=5783cdeb51874b43 input=a12df928758a82b4]*/ { #ifdef PY_HAVE_PERF_TRAMPOLINE +#ifdef _Py_TIER2 + _PyOptimizerObject* optimizer = _Py_GetOptimizer(); + if (optimizer != NULL) { + PyErr_SetString(PyExc_ValueError, "Cannot activate the perf trampiline if the JIT is active"); + return NULL; + } +#endif + if (strcmp(backend, "perf") == 0) { _PyPerf_Callbacks cur_cb; _PyPerfTrampoline_GetCallbacks(&cur_cb); From aeac29cafb7f05c83a1b0e31aafebb332c2526b9 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Tue, 1 Oct 2024 20:08:42 +0100 Subject: [PATCH 2/3] Update Python/sysmodule.c Co-authored-by: Ken Jin --- Python/sysmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 1be9a70d2ba952..e70c5d0df29d8f 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2290,7 +2290,7 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend) #ifdef _Py_TIER2 _PyOptimizerObject* optimizer = _Py_GetOptimizer(); if (optimizer != NULL) { - PyErr_SetString(PyExc_ValueError, "Cannot activate the perf trampiline if the JIT is active"); + PyErr_SetString(PyExc_ValueError, "Cannot activate the perf trampoline if the JIT is active"); return NULL; } #endif From d4b5386dab25545e68436ec1ae96577dd23b33d0 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Tue, 1 Oct 2024 20:09:00 +0100 Subject: [PATCH 3/3] Update Python/sysmodule.c Signed-off-by: Pablo Galindo --- Doc/library/sys.rst | 2 ++ Python/sysmodule.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Doc/library/sys.rst b/Doc/library/sys.rst index b0e40a4ea06946..ab16c36201e6db 100644 --- a/Doc/library/sys.rst +++ b/Doc/library/sys.rst @@ -1727,6 +1727,8 @@ always available. Activate the stack profiler trampoline *backend*. The only supported backend is ``"perf"``. + Stack trampolines cannot be activated if the JIT is active. + .. availability:: Linux. .. versionadded:: 3.12 diff --git a/Python/sysmodule.c b/Python/sysmodule.c index e70c5d0df29d8f..5bdfca549e08d1 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -2287,7 +2287,7 @@ sys_activate_stack_trampoline_impl(PyObject *module, const char *backend) /*[clinic end generated code: output=5783cdeb51874b43 input=a12df928758a82b4]*/ { #ifdef PY_HAVE_PERF_TRAMPOLINE -#ifdef _Py_TIER2 +#ifdef _Py_JIT _PyOptimizerObject* optimizer = _Py_GetOptimizer(); if (optimizer != NULL) { PyErr_SetString(PyExc_ValueError, "Cannot activate the perf trampoline if the JIT is active");