From nobody Sat Nov 23 18:21:20 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 370256F2F2 for ; Tue, 19 Nov 2024 01:17:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731979026; cv=none; b=uKXDbaNK7YAhrLwWha928av20mtpxOwNpWZ6RU4pxGXSS+95AlwZ7LqmPIUDo9dDadjXQetXc65//FzcACKcMQwPznIM+k4G9XwVguqfNEmzMAJ7HQuzOUIQt8dNLPtSp+INPABGcURYzwfc6UwElNyPBbp8HyzyAf1+yeCef3s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731979026; c=relaxed/simple; bh=TRzdyrr83yFOHaaZp1XKONQ1u8411y5W18H7l2Lixew=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Cc:Content-Type; b=ZXi6JrJHIGdNiSjZVBN2oMbqCLoXfbC69/n9gbBA49c2upuK5yoLBswcK/UcFXpn33yjJMePXLpg3zetqhXFeysabL8wmOya21KgyBVziBefP7BdlkWnBjYBZk3RM7V2uuli5MbjRyAdwzLAs6zvcTP0xgzh3VQoU8/67oMmUQE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wpMvXUCi; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wpMvXUCi" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee426e3414so64352057b3.3 for ; Mon, 18 Nov 2024 17:17:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731979023; x=1732583823; darn=vger.kernel.org; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=e5R7N5m+a9zmfRArLdBFQ3oPGXQCBbIT9nqOcsc0Xic=; b=wpMvXUCikEG8xPXAfP6G/hg+WYfCfr9d/zAmn+SVUcIrHWi/Nz3s3okgYdbe2d6Kwe aTlN0sotq25bfUoxWoqU0AmgJzi5SKWbhOKd5znX/PvKPzsvYflLmDnka6YvlkgnsNkq eM25Lp9+6NENYmy7pfrs5cxNCdtvN+0Q15qmoajCNjpcOx+e1GdlwK03zq+0C/ulzSR0 43aUWpJPKJqbBtBqtowdu5yH9k+0l6sW5zJR2k3Nqq/S3Nxwoqp/JKLLj2i/oWKaa4x4 wJxm7r5KtMihnGBUmcrdDvwEEDxXVOgG1I6OlrlQKEIHvnS8nKfXvl7omff0Xhwf/ni8 DSDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731979023; x=1732583823; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=e5R7N5m+a9zmfRArLdBFQ3oPGXQCBbIT9nqOcsc0Xic=; b=gilccgfs5jzjnUMkILJ/GhigWv7mSdgy5aH+oJB4+F2mTREhxJrBJ3Dhd4YMzo1jTp MpIvtU6MdcMQn0hKa10uHOY8mQe47DiD1BHMYJj2AO6X1gmT9BjfRCe6eebn2LzItDBv WZJoDL37M6/wWzbS5TGX+gVx7PYKovYz2w4H1i17nj17Lf1zLZDa7hIN/ifAJI5f5ecq TKLEI2sKAry64lfrRMwZ6njMjoARgpBCv5Z4iWEOzsickHVrmXXaU892Ym3Zxz1Y+g/F qRXZDn/dTZyk7brLFXIFhb1qOJz7HNfCJd1EfchmEnZjOABvOYdHUVQ7P3ZXra2y4Df/ uw0Q== X-Forwarded-Encrypted: i=1; AJvYcCVDrlpeBPi+3m6z4VhhnbCTxJGgEOTrJ+r4aP2m7DKFQILRWc9k82msvM+GOvJpb0fJhIYBaGzYXuP+v4I=@vger.kernel.org X-Gm-Message-State: AOJu0YylHcayN25ZI37uFEboKou6N22tqxVSf6seYDpj2einUpe3Gc6a 3vCgjJrR42u7Q/LMZ/wbxv6Lq2Bx50R6yg2sG/s7WAmIkC1gWWPxElkWqI58qsU616OZf0T+HV6 Fl6FQeA== X-Google-Smtp-Source: AGHT+IEVB+QEv1B58NiPQwBfZyIOt1tj2t1pVEM3PPvhzdzHVO4jhLeP34Iu6kgscgaXruipXnyyh0ugXJnn X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:22a4:ded5:5c37:fcc7]) (user=irogers job=sendgmr) by 2002:a05:690c:8110:b0:6ee:3f5e:1c1b with SMTP id 00721157ae682-6ee558f3375mr446637b3.0.1731979023191; Mon, 18 Nov 2024 17:17:03 -0800 (PST) Date: Mon, 18 Nov 2024 17:16:23 -0800 In-Reply-To: <20241119011644.971342-1-irogers@google.com> Message-Id: <20241119011644.971342-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241119011644.971342-1-irogers@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Subject: [PATCH v7 01/22] perf python: Remove python 2 scripting support From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Howard Chu , Athira Jajeev , Michael Petlan , Veronika Molnarova , Dapeng Mi , Thomas Richter , Ilya Leoshkevich , Colin Ian King , Weilin Wang , Andi Kleen , Josh Poimboeuf , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Cc: Arnaldo Carvalho de Melo Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Python2 was deprecated 4 years ago, remove support and workarounds. Signed-off-by: Ian Rogers Acked-by: Arnaldo Carvalho de Melo --- .../scripts/python/Perf-Trace-Util/Context.c | 18 ----- tools/perf/util/python.c | 73 +++---------------- .../scripting-engines/trace-event-python.c | 63 +--------------- 3 files changed, 15 insertions(+), 139 deletions(-) diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Context.c b/tools/pe= rf/scripts/python/Perf-Trace-Util/Context.c index 01f54d6724a5..d742daaa5d5a 100644 --- a/tools/perf/scripts/python/Perf-Trace-Util/Context.c +++ b/tools/perf/scripts/python/Perf-Trace-Util/Context.c @@ -24,16 +24,6 @@ #include "../../../util/srcline.h" #include "../../../util/srccode.h" =20 -#if PY_MAJOR_VERSION < 3 -#define _PyCapsule_GetPointer(arg1, arg2) \ - PyCObject_AsVoidPtr(arg1) -#define _PyBytes_FromStringAndSize(arg1, arg2) \ - PyString_FromStringAndSize((arg1), (arg2)) -#define _PyUnicode_AsUTF8(arg) \ - PyString_AsString(arg) - -PyMODINIT_FUNC initperf_trace_context(void); -#else #define _PyCapsule_GetPointer(arg1, arg2) \ PyCapsule_GetPointer((arg1), (arg2)) #define _PyBytes_FromStringAndSize(arg1, arg2) \ @@ -42,7 +32,6 @@ PyMODINIT_FUNC initperf_trace_context(void); PyUnicode_AsUTF8(arg) =20 PyMODINIT_FUNC PyInit_perf_trace_context(void); -#endif =20 static struct scripting_context *get_args(PyObject *args, const char *name= , PyObject **arg2) { @@ -213,12 +202,6 @@ static PyMethodDef ContextMethods[] =3D { { NULL, NULL, 0, NULL} }; =20 -#if PY_MAJOR_VERSION < 3 -PyMODINIT_FUNC initperf_trace_context(void) -{ - (void) Py_InitModule("perf_trace_context", ContextMethods); -} -#else PyMODINIT_FUNC PyInit_perf_trace_context(void) { static struct PyModuleDef moduledef =3D { @@ -240,4 +223,3 @@ PyMODINIT_FUNC PyInit_perf_trace_context(void) =20 return mod; } -#endif diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 2096cdbaa53b..ea6dbe6e4317 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -26,40 +26,14 @@ #include #include "../builtin.h" =20 -#if PY_MAJOR_VERSION < 3 -#define _PyUnicode_FromString(arg) \ - PyString_FromString(arg) -#define _PyUnicode_AsString(arg) \ - PyString_AsString(arg) -#define _PyUnicode_FromFormat(...) \ - PyString_FromFormat(__VA_ARGS__) -#define _PyLong_FromLong(arg) \ - PyInt_FromLong(arg) - -#else - #define _PyUnicode_FromString(arg) \ PyUnicode_FromString(arg) #define _PyUnicode_FromFormat(...) \ PyUnicode_FromFormat(__VA_ARGS__) #define _PyLong_FromLong(arg) \ PyLong_FromLong(arg) -#endif =20 -#ifndef Py_TYPE -#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) -#endif - -/* Define PyVarObject_HEAD_INIT for python 2.5 */ -#ifndef PyVarObject_HEAD_INIT -# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, -#endif - -#if PY_MAJOR_VERSION < 3 -PyMODINIT_FUNC initperf(void); -#else PyMODINIT_FUNC PyInit_perf(void); -#endif =20 #define member_def(type, member, ptype, help) \ { #member, ptype, \ @@ -117,7 +91,7 @@ static PyObject *pyrf_mmap_event__repr(struct pyrf_event= *pevent) pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { ret =3D PyErr_NoMemory(); } else { - ret =3D _PyUnicode_FromString(s); + ret =3D PyUnicode_FromString(s); free(s); } return ret; @@ -148,7 +122,7 @@ static PyMemberDef pyrf_task_event__members[] =3D { =20 static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent) { - return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " + return PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " "ptid: %u, time: %" PRI_lu64 "}", pevent->event.header.type =3D=3D PERF_RECORD_FORK ? "fork" : "exit", pevent->event.fork.pid, @@ -181,7 +155,7 @@ static PyMemberDef pyrf_comm_event__members[] =3D { =20 static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent) { - return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", + return PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", pevent->event.comm.pid, pevent->event.comm.tid, pevent->event.comm.comm); @@ -212,7 +186,7 @@ static PyObject *pyrf_throttle_event__repr(struct pyrf_= event *pevent) { struct perf_record_throttle *te =3D (struct perf_record_throttle *)(&peve= nt->event.header + 1); =20 - return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id= : %" PRI_lu64 + return PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id:= %" PRI_lu64 ", stream_id: %" PRI_lu64 " }", pevent->event.header.type =3D=3D PERF_RECORD_THROTTLE ? "" : "un", te->time, te->id, te->stream_id); @@ -247,7 +221,7 @@ static PyObject *pyrf_lost_event__repr(struct pyrf_even= t *pevent) pevent->event.lost.id, pevent->event.lost.lost) < 0) { ret =3D PyErr_NoMemory(); } else { - ret =3D _PyUnicode_FromString(s); + ret =3D PyUnicode_FromString(s); free(s); } return ret; @@ -274,7 +248,7 @@ static PyMemberDef pyrf_read_event__members[] =3D { =20 static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent) { - return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }", + return PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }", pevent->event.read.pid, pevent->event.read.tid); /* @@ -309,7 +283,7 @@ static PyObject *pyrf_sample_event__repr(struct pyrf_ev= ent *pevent) if (asprintf(&s, "{ type: sample }") < 0) { ret =3D PyErr_NoMemory(); } else { - ret =3D _PyUnicode_FromString(s); + ret =3D PyUnicode_FromString(s); free(s); } return ret; @@ -343,7 +317,7 @@ tracepoint_field(struct pyrf_event *pe, struct tep_form= at_field *field) } if (field->flags & TEP_FIELD_IS_STRING && is_printable_array(data + offset, len)) { - ret =3D _PyUnicode_FromString((char *)data + offset); + ret =3D PyUnicode_FromString((char *)data + offset); } else { ret =3D PyByteArray_FromStringAndSize((const char *) data + offset, len= ); field->flags &=3D ~TEP_FIELD_IS_STRING; @@ -432,7 +406,7 @@ static PyObject *pyrf_context_switch_event__repr(struct= pyrf_event *pevent) !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) { ret =3D PyErr_NoMemory(); } else { - ret =3D _PyUnicode_FromString(s); + ret =3D PyUnicode_FromString(s); free(s); } return ret; @@ -918,17 +892,8 @@ static PyObject *pyrf_evlist__get_pollfd(struct pyrf_e= vlist *pevlist, =20 for (i =3D 0; i < evlist->core.pollfd.nr; ++i) { PyObject *file; -#if PY_MAJOR_VERSION < 3 - FILE *fp =3D fdopen(evlist->core.pollfd.entries[i].fd, "r"); - - if (fp =3D=3D NULL) - goto free_list; - - file =3D PyFile_FromFile(fp, "perf", "r", NULL); -#else file =3D PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -= 1, NULL, NULL, NULL, 0); -#endif if (file =3D=3D NULL) goto free_list; =20 @@ -1234,9 +1199,9 @@ static PyObject *pyrf__tracepoint(struct pyrf_evsel *= pevsel, =20 tp_format =3D trace_event__tp_format(sys, name); if (IS_ERR(tp_format)) - return _PyLong_FromLong(-1); + return PyLong_FromLong(-1); =20 - return _PyLong_FromLong(tp_format->id); + return PyLong_FromLong(tp_format->id); #endif // HAVE_LIBTRACEEVENT } =20 @@ -1250,18 +1215,11 @@ static PyMethodDef perf__methods[] =3D { { .ml_name =3D NULL, } }; =20 -#if PY_MAJOR_VERSION < 3 -PyMODINIT_FUNC initperf(void) -#else PyMODINIT_FUNC PyInit_perf(void) -#endif { PyObject *obj; int i; PyObject *dict; -#if PY_MAJOR_VERSION < 3 - PyObject *module =3D Py_InitModule("perf", perf__methods); -#else static struct PyModuleDef moduledef =3D { PyModuleDef_HEAD_INIT, "perf", /* m_name */ @@ -1274,7 +1232,6 @@ PyMODINIT_FUNC PyInit_perf(void) NULL, /* m_free */ }; PyObject *module =3D PyModule_Create(&moduledef); -#endif =20 if (module =3D=3D NULL || pyrf_event__setup_types() < 0 || @@ -1282,11 +1239,7 @@ PyMODINIT_FUNC PyInit_perf(void) pyrf_evsel__setup_types() < 0 || pyrf_thread_map__setup_types() < 0 || pyrf_cpu_map__setup_types() < 0) -#if PY_MAJOR_VERSION < 3 - return; -#else return module; -#endif =20 /* The page_size is placed in util object. */ page_size =3D sysconf(_SC_PAGE_SIZE); @@ -1335,7 +1288,7 @@ PyMODINIT_FUNC PyInit_perf(void) goto error; =20 for (i =3D 0; perf__constants[i].name !=3D NULL; i++) { - obj =3D _PyLong_FromLong(perf__constants[i].value); + obj =3D PyLong_FromLong(perf__constants[i].value); if (obj =3D=3D NULL) goto error; PyDict_SetItemString(dict, perf__constants[i].name, obj); @@ -1345,9 +1298,7 @@ PyMODINIT_FUNC PyInit_perf(void) error: if (PyErr_Occurred()) PyErr_SetString(PyExc_ImportError, "perf: Init failed!"); -#if PY_MAJOR_VERSION >=3D 3 return module; -#endif } =20 =20 diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools= /perf/util/scripting-engines/trace-event-python.c index 8bdae066e839..19eadce50ca1 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -58,22 +58,6 @@ #include "mem-events.h" #include "util/perf_regs.h" =20 -#if PY_MAJOR_VERSION < 3 -#define _PyUnicode_FromString(arg) \ - PyString_FromString(arg) -#define _PyUnicode_FromStringAndSize(arg1, arg2) \ - PyString_FromStringAndSize((arg1), (arg2)) -#define _PyBytes_FromStringAndSize(arg1, arg2) \ - PyString_FromStringAndSize((arg1), (arg2)) -#define _PyLong_FromLong(arg) \ - PyInt_FromLong(arg) -#define _PyLong_AsLong(arg) \ - PyInt_AsLong(arg) -#define _PyCapsule_New(arg1, arg2, arg3) \ - PyCObject_FromVoidPtr((arg1), (arg2)) - -PyMODINIT_FUNC initperf_trace_context(void); -#else #define _PyUnicode_FromString(arg) \ PyUnicode_FromString(arg) #define _PyUnicode_FromStringAndSize(arg1, arg2) \ @@ -88,7 +72,6 @@ PyMODINIT_FUNC initperf_trace_context(void); PyCapsule_New((arg1), (arg2), (arg3)) =20 PyMODINIT_FUNC PyInit_perf_trace_context(void); -#endif =20 #ifdef HAVE_LIBTRACEEVENT #define TRACE_EVENT_TYPE_MAX \ @@ -181,17 +164,7 @@ static int get_argument_count(PyObject *handler) { int arg_count =3D 0; =20 - /* - * The attribute for the code object is func_code in Python 2, - * whereas it is __code__ in Python 3.0+. - */ - PyObject *code_obj =3D PyObject_GetAttrString(handler, - "func_code"); - if (PyErr_Occurred()) { - PyErr_Clear(); - code_obj =3D PyObject_GetAttrString(handler, - "__code__"); - } + PyObject *code_obj =3D code_obj =3D PyObject_GetAttrString(handler, "__co= de__"); PyErr_Clear(); if (code_obj) { PyObject *arg_count_obj =3D PyObject_GetAttrString(code_obj, @@ -1902,12 +1875,6 @@ static void set_table_handlers(struct tables *tables) tables->synth_handler =3D get_handler("synth_data"); } =20 -#if PY_MAJOR_VERSION < 3 -static void _free_command_line(const char **command_line, int num) -{ - free(command_line); -} -#else static void _free_command_line(wchar_t **command_line, int num) { int i; @@ -1915,7 +1882,6 @@ static void _free_command_line(wchar_t **command_line= , int num) PyMem_RawFree(command_line[i]); free(command_line); } -#endif =20 =20 /* @@ -1925,30 +1891,12 @@ static int python_start_script(const char *script, = int argc, const char **argv, struct perf_session *session) { struct tables *tables =3D &tables_global; -#if PY_MAJOR_VERSION < 3 - const char **command_line; -#else wchar_t **command_line; -#endif - /* - * Use a non-const name variable to cope with python 2.6's - * PyImport_AppendInittab prototype - */ - char buf[PATH_MAX], name[19] =3D "perf_trace_context"; + char buf[PATH_MAX]; int i, err =3D 0; FILE *fp; =20 scripting_context->session =3D session; -#if PY_MAJOR_VERSION < 3 - command_line =3D malloc((argc + 1) * sizeof(const char *)); - if (!command_line) - return -1; - - command_line[0] =3D script; - for (i =3D 1; i < argc + 1; i++) - command_line[i] =3D argv[i - 1]; - PyImport_AppendInittab(name, initperf_trace_context); -#else command_line =3D malloc((argc + 1) * sizeof(wchar_t *)); if (!command_line) return -1; @@ -1956,15 +1904,10 @@ static int python_start_script(const char *script, = int argc, const char **argv, command_line[0] =3D Py_DecodeLocale(script, NULL); for (i =3D 1; i < argc + 1; i++) command_line[i] =3D Py_DecodeLocale(argv[i - 1], NULL); - PyImport_AppendInittab(name, PyInit_perf_trace_context); -#endif + PyImport_AppendInittab("perf_trace_context", PyInit_perf_trace_context); Py_Initialize(); =20 -#if PY_MAJOR_VERSION < 3 - PySys_SetArgv(argc + 1, (char **)command_line); -#else PySys_SetArgv(argc + 1, command_line); -#endif =20 fp =3D fopen(script, "r"); if (!fp) { --=20 2.47.0.338.g60cca15819-goog