Skip to contentSkip to conversation

perf: add Python 3 support

PATCH In review Jaroslav Škarvada 2 participants 3 messages Jan 19, 2018 – Jan 24, 2018 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: add Python 3 support

Added Python 3 support while keeping Python 2.7 compatibility.

---
 tools/perf/Makefile.perf                           |   4 +-
 .../perf/scripts/python/Perf-Trace-Util/Context.c  |  34 ++++-
 tools/perf/util/python.c                           |  95 ++++++++++---
 .../util/scripting-engines/trace-event-python.c    | 147 +++++++++++++++------
 tools/perf/util/setup.py                           |   6 +-
 5 files changed, 217 insertions(+), 69 deletions(-)
tools/perf/Makefile.perf +2 -2
index 68cf1360a..a341db473 100644
@@ -300,7 +300,7 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/
 PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/
 export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP
 
-python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf.so
+python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so
 
 PYTHON_EXT_SRCS := $(shell grep -v ^\# util/python-ext-sources)
 PYTHON_EXT_DEPS := util/python-ext-sources util/setup.py $(LIBTRACEEVENT) $(LIBAPI)
@@ -470,7 +470,7 @@ $(OUTPUT)python/perf.so: $(PYTHON_EXT_SRCS) $(PYTHON_EXT_DEPS) $(LIBTRACEEVENT_D
 	  $(PYTHON_WORD) util/setup.py \
 	  --quiet build_ext; \
 	mkdir -p $(OUTPUT)python && \
-	cp $(PYTHON_EXTBUILD_LIB)perf.so $(OUTPUT)python/
+	cp $(PYTHON_EXTBUILD_LIB)perf*.so $(OUTPUT)python/
 
 please_set_SHELL_PATH_to_a_more_modern_shell:
 	$(Q)$$(:)
tools/perf/scripts/python/Perf-Trace-Util/Context.c +31 -3
index fcd1dd667..1a0d27757 100644
@@ -23,7 +23,17 @@
 #include "../../../perf.h"
 #include "../../../util/trace-event.h"
 
+#if PY_MAJOR_VERSION < 3
+#define _PyCapsule_GetPointer(arg1, arg2) \
+  PyCObject_AsVoidPtr(arg1)
+
 PyMODINIT_FUNC initperf_trace_context(void);
+#else
+#define _PyCapsule_GetPointer(arg1, arg2) \
+  PyCapsule_GetPointer((arg1), (arg2))
+
+PyMODINIT_FUNC PyInit_perf_trace_context(void);
+#endif
 
 static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
 {
@@ -34,7 +44,7 @@ static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
 	if (!PyArg_ParseTuple(args, "O", &context))
 		return NULL;
 
-	scripting_context = PyCObject_AsVoidPtr(context);
+	scripting_context = _PyCapsule_GetPointer(context, NULL);
 	retval = common_pc(scripting_context);
 
 	return Py_BuildValue("i", retval);
@@ -50,7 +60,7 @@ static PyObject *perf_trace_context_common_flags(PyObject *obj,
 	if (!PyArg_ParseTuple(args, "O", &context))
 		return NULL;
 
-	scripting_context = PyCObject_AsVoidPtr(context);
+	scripting_context = _PyCapsule_GetPointer(context, NULL);
 	retval = common_flags(scripting_context);
 
 	return Py_BuildValue("i", retval);
@@ -66,7 +76,7 @@ static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
 	if (!PyArg_ParseTuple(args, "O", &context))
 		return NULL;
 
-	scripting_context = PyCObject_AsVoidPtr(context);
+	scripting_context = _PyCapsule_GetPointer(context, NULL);
 	retval = common_lock_depth(scripting_context);
 
 	return Py_BuildValue("i", retval);
@@ -82,7 +92,25 @@ static PyMethodDef ContextMethods[] = {
 	{ NULL, NULL, 0, NULL}
 };
 
+#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 = {
+		PyModuleDef_HEAD_INIT,
+		"perf_trace_context",	/* m_name */
+		"",			/* m_doc */
+		-1,			/* m_size */
+		ContextMethods,		/* m_methods */
+		NULL,			/* m_reload */
+		NULL,			/* m_traverse */
+		NULL,			/* m_clear */
+		NULL,			/* m_free */
+	};
+	return PyModule_Create(&moduledef);
+}
+#endif
tools/perf/util/python.c +77 -18

240 lines not on this page · raw message

tools/perf/util/scripting-engines/trace-event-python.c +104 -43

299 lines not on this page · raw message

tools/perf/util/setup.py +3 -3
index af415febb..6891635b5 100644
@@ -1,4 +1,4 @@
-#!/usr/bin/python2
+#!/usr/bin/python
 
 from os import getenv
 
@@ -35,11 +35,11 @@ build_tmp = getenv('PYTHON_EXTBUILD_TMP')
 libtraceevent = getenv('LIBTRACEEVENT')
 libapikfs = getenv('LIBAPI')
 
-ext_sources = [f.strip() for f in file('util/python-ext-sources')
+ext_sources = [f.strip() for f in open('util/python-ext-sources')
 				if len(f.strip()) > 0 and f[0] != '#']
 
 # use full paths with source files
-ext_sources = map(lambda x: '%s/%s' % (src_perf, x) , ext_sources)
+ext_sources = list(map(lambda x: '%s/%s' % (src_perf, x) , ext_sources))
 
 perf = Extension('perf',
 		  sources = ext_sources,
Signature · 1 line
2.13.6
#1 lore raw

This is an updated patch for kernel-4.15.0-0.rc8. It also applies to linux-stable HEAD. Our project Tuned is consumer of python-perf. We are trying to switch Tuned to Python 3 and the python-perf is the only blocker for us at the moment.

Downstream Fedora bugzilla containing the scratch build and Fedora Spec patch:
https://bugzilla.redhat.com/show_bug.cgi?id=1536656

The patch needs testing, but with our use case in Tuned it seems to work. Any comments or patch updates are welcome

thanks & regards

Jaroslav

#2 lore raw
In reply to Jaroslav Škarvada

Hi,

I tested the patch a bit and it seems to work fine. Some things I noticed:

Review PyMODINIT_FUNC initperf(void) · line 1280

Quoted 11 lines
(quoted diff) +4 -0
 	if (module == NULL ||
 	    pyrf_event__setup_types() < 0 ||
@@ -1221,7 +1273,11 @@ PyMODINIT_FUNC initperf(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

Wouldn't it make sense to clean up the partially loaded module and return NULL if module != NULL, but the .*setup_types() fail?

There also seems to be a bug, even without Jaroslav's patch, where e.g. event.prev_comm, event being a perf.sample_event, is _most of the time_ a bytestring. I think it should be, and was intended to be, a unicode string. I say most of the time, because if the string has the right length, it will in fact be returned as a unicode string.

The problem can be reproduced with the script perf/python/tracepoint.py, when run using python3. Most of the time, you will see something like the following in the output:

prev_comm=bytearray(b'python3\x000\x00\x00\x00\x00\x00\x00\x00')

But if the length of prev_comm is just right, you'll get:
prev_comm=aaaaaaaaaaaaaaa

The problem is in perf/util/python.c:tracepoint_field(), calling is_printable_array(data + offset, len). 'len' is the size of the whole array where the string is stored, not of the string itself. It could be resolved by passing the actual string length to is_printable_array() somehow, or maybe with the following patch (it was not tested, and might have unintended consequences):

tools/perf/util/print_binary.c +5 -1
index 23e367063446..f124127ace3b 100644
@@ -50,9 +50,13 @@ int is_printable_array(char *p, unsigned int len)
 
        len--;
 
-       for (i = 0; i < len; i++) {
+       for (i = 0; i < len && p[i] != 0; i++) {
                if (!isprint(p[i]) && !isspace(p[i]))
                        return 0;
        }
+       for (++i; i < len; ++i)  {
+               if (p[i] != 0)
+                       return 0;
+       }
        return 1;
 }

--

I'd prepare a proper patch myself, but unfortunatelly I don't have the time right now and I'm not familiar with the codebase.

Best regards
Ondřej Lysoněk