summaryrefslogtreecommitdiffstats
path: root/recipes-kernel/linux/linux-intel/0001-perf-tools-Add-Python-3-support.patch
diff options
context:
space:
mode:
Diffstat (limited to 'recipes-kernel/linux/linux-intel/0001-perf-tools-Add-Python-3-support.patch')
-rw-r--r--recipes-kernel/linux/linux-intel/0001-perf-tools-Add-Python-3-support.patch917
1 files changed, 917 insertions, 0 deletions
diff --git a/recipes-kernel/linux/linux-intel/0001-perf-tools-Add-Python-3-support.patch b/recipes-kernel/linux/linux-intel/0001-perf-tools-Add-Python-3-support.patch
new file mode 100644
index 00000000..74ce61e5
--- /dev/null
+++ b/recipes-kernel/linux/linux-intel/0001-perf-tools-Add-Python-3-support.patch
@@ -0,0 +1,917 @@
1From 8804547988bb772a00cd4bb832fe4e1a79f552b7 Mon Sep 17 00:00:00 2001
2From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= <jskarvad@redhat.com>
3Date: Fri, 19 Jan 2018 21:56:41 +0100
4Subject: [PATCH] perf tools: Add Python 3 support
5MIME-Version: 1.0
6Content-Type: text/plain; charset=UTF-8
7Content-Transfer-Encoding: 8bit
8
9Added Python 3 support while keeping Python 2.7 compatibility.
10
11Committer notes:
12
13This doesn't make it to auto detect python 3, one has to explicitely ask
14it to build with python 3 devel files, here are the instructions
15provided by Jaroslav:
16
17 ---
18 $ cp -a tools/perf tools/python3-perf
19 $ make V=1 prefix=/usr -C tools/perf PYTHON=/usr/bin/python2 all
20 $ make V=1 prefix=/usr -C tools/python3-perf PYTHON=/usr/bin/python3 all
21 $ make V=1 prefix=/usr -C tools/python3-perf PYTHON=/usr/bin/python3 DESTDIR=%{buildroot} install-python_ext
22 $ make V=1 prefix=/usr -C tools/perf PYTHON=/usr/bin/python2 DESTDIR=%{buildroot} install-python_ext
23 ---
24
25We need to make this automatic, just like the existing tests for checking if
26the python2 devel files are in place, allowing the build with python3 if
27available, fallbacking to python2 and then just disabling it if none are
28available.
29
30So, using the PYTHON variable to build it using O= we get:
31
32Before this patch:
33
34 $ rpm -q python3 python3-devel
35 python3-3.6.4-7.fc27.x86_64
36 python3-devel-3.6.4-7.fc27.x86_64
37 $ rm -rf /tmp/build/perf/ ; mkdir -p /tmp/build/perf ; make O=/tmp/build/perf PYTHON=/usr/bin/python3 -C tools/perf install-bin
38 make: Entering directory '/home/acme/git/linux/tools/perf'
39 <SNIP>
40 Makefile.config:670: Python 3 is not yet supported; please set
41 Makefile.config:671: PYTHON and/or PYTHON_CONFIG appropriately.
42 Makefile.config:672: If you also have Python 2 installed, then
43 Makefile.config:673: try something like:
44 Makefile.config:674:
45 Makefile.config:675: make PYTHON=python2
46 Makefile.config:676:
47 Makefile.config:677: Otherwise, disable Python support entirely:
48 Makefile.config:678:
49 Makefile.config:679: make NO_LIBPYTHON=1
50 Makefile.config:680:
51 Makefile.config:681: *** . Stop.
52 make[1]: *** [Makefile.perf:212: sub-make] Error 2
53 make: *** [Makefile:110: install-bin] Error 2
54 make: Leaving directory '/home/acme/git/linux/tools/perf'
55 $
56
57After:
58
59 $ make O=/tmp/build/perf PYTHON=python3 -C tools/perf install-bin
60 $ ldd ~/bin/perf | grep python
61 libpython3.6m.so.1.0 => /lib64/libpython3.6m.so.1.0 (0x00007f58a31e8000)
62 $ rpm -qf /lib64/libpython3.6m.so.1.0
63 python3-libs-3.6.4-7.fc27.x86_64
64 $
65
66Now verify that when using the binding the right ELF file is loaded,
67using perf trace:
68
69 $ perf trace -e open* perf test python
70 0.051 ( 0.016 ms): perf/3927 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC ) = 3
71<SNIP>
72 18: 'import perf' in python :
73 8.849 ( 0.013 ms): sh/3929 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC ) = 3
74<SNIP>
75 25.572 ( 0.008 ms): python3/3931 openat(dfd: CWD, filename: /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so, flags: CLOEXEC) = 3
76<SNIP>
77 Ok
78<SNIP>
79 $
80
81And using tools/perf/python/twatch.py, to show PERF_RECORD_ metaevents:
82
83 $ python3 tools/perf/python/twatch.py
84 cpu: 3, pid: 16060, tid: 16060 { type: fork, pid: 5207, ppid: 16060, tid: 5207, ptid: 16060, time: 10798513015459}
85 cpu: 3, pid: 16060, tid: 16060 { type: fork, pid: 5208, ppid: 16060, tid: 5208, ptid: 16060, time: 10798513562503}
86 cpu: 0, pid: 5208, tid: 5208 { type: comm, pid: 5208, tid: 5208, comm: grep }
87 cpu: 2, pid: 5207, tid: 5207 { type: comm, pid: 5207, tid: 5207, comm: ps }
88 cpu: 2, pid: 5207, tid: 5207 { type: exit, pid: 5207, ppid: 5207, tid: 5207, ptid: 5207, time: 10798551337484}
89 cpu: 3, pid: 5208, tid: 5208 { type: exit, pid: 5208, ppid: 5208, tid: 5208, ptid: 5208, time: 10798551292153}
90 cpu: 3, pid: 601, tid: 601 { type: fork, pid: 5209, ppid: 601, tid: 5209, ptid: 601, time: 10801779977324}
91 ^CTraceback (most recent call last):
92 File "tools/perf/python/twatch.py", line 68, in <module>
93 main()
94 File "tools/perf/python/twatch.py", line 40, in main
95 evlist.poll(timeout = -1)
96 KeyboardInterrupt
97 $
98
99 # ps ax|grep twatch
100 5197 pts/8 S+ 0:00 python3 tools/perf/python/twatch.py
101 # ls -la /proc/5197/smaps
102 -r--r--r--. 1 acme acme 0 Feb 19 13:14 /proc/5197/smaps
103 # grep python /proc/5197/smaps
104 558111307000-558111309000 r-xp 00000000 fd:00 3151710 /usr/bin/python3.6
105 558111508000-558111509000 r--p 00001000 fd:00 3151710 /usr/bin/python3.6
106 558111509000-55811150a000 rw-p 00002000 fd:00 3151710 /usr/bin/python3.6
107 7ffad6fc1000-7ffad7008000 r-xp 00000000 00:2d 220196 /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so
108 7ffad7008000-7ffad7207000 ---p 00047000 00:2d 220196 /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so
109 7ffad7207000-7ffad7208000 r--p 00046000 00:2d 220196 /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so
110 7ffad7208000-7ffad7215000 rw-p 00047000 00:2d 220196 /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so
111 7ffadea77000-7ffaded3d000 r-xp 00000000 fd:00 3151795 /usr/lib64/libpython3.6m.so.1.0
112 7ffaded3d000-7ffadef3c000 ---p 002c6000 fd:00 3151795 /usr/lib64/libpython3.6m.so.1.0
113 7ffadef3c000-7ffadef42000 r--p 002c5000 fd:00 3151795 /usr/lib64/libpython3.6m.so.1.0
114 7ffadef42000-7ffadefa5000 rw-p 002cb000 fd:00 3151795 /usr/lib64/libpython3.6m.so.1.0
115 #
116
117And with this patch, but building normally, without specifying the
118PYTHON=python3 part, which will make it use python2 if its devel files are
119available, like in this test:
120
121 $ make O=/tmp/build/perf -C tools/perf install-bin
122 $ ldd ~/bin/perf | grep python
123 libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007f6a44410000)
124 $ ldd /tmp/build/perf/python_ext_build/lib/perf.so | grep python
125 libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007fed28a2c000)
126 $
127
128 [acme@jouet perf]$ tools/perf/python/twatch.py
129 cpu: 0, pid: 2817, tid: 2817 { type: fork, pid: 2817, ppid: 2817, tid: 8910, ptid: 2817, time: 11126454335306}
130 cpu: 0, pid: 2817, tid: 2817 { type: comm, pid: 2817, tid: 8910, comm: worker }
131 $ ps ax | grep twatch.py
132 8909 pts/8 S+ 0:00 /usr/bin/python tools/perf/python/twatch.py
133 $ grep python /proc/8909/smaps
134 5579de658000-5579de659000 r-xp 00000000 fd:00 3156044 /usr/bin/python2.7
135 5579de858000-5579de859000 r--p 00000000 fd:00 3156044 /usr/bin/python2.7
136 5579de859000-5579de85a000 rw-p 00001000 fd:00 3156044 /usr/bin/python2.7
137 7f0de01f7000-7f0de023e000 r-xp 00000000 00:2d 230695 /tmp/build/perf/python/perf.so
138 7f0de023e000-7f0de043d000 ---p 00047000 00:2d 230695 /tmp/build/perf/python/perf.so
139 7f0de043d000-7f0de043e000 r--p 00046000 00:2d 230695 /tmp/build/perf/python/perf.so
140 7f0de043e000-7f0de044b000 rw-p 00047000 00:2d 230695 /tmp/build/perf/python/perf.so
141 7f0de6f0f000-7f0de6f13000 r-xp 00000000 fd:00 134975 /usr/lib64/python2.7/lib-dynload/_localemodule.so
142 7f0de6f13000-7f0de7113000 ---p 00004000 fd:00 134975 /usr/lib64/python2.7/lib-dynload/_localemodule.so
143 7f0de7113000-7f0de7114000 r--p 00004000 fd:00 134975 /usr/lib64/python2.7/lib-dynload/_localemodule.so
144 7f0de7114000-7f0de7115000 rw-p 00005000 fd:00 134975 /usr/lib64/python2.7/lib-dynload/_localemodule.so
145 7f0de7e73000-7f0de8052000 r-xp 00000000 fd:00 3173292 /usr/lib64/libpython2.7.so.1.0
146 7f0de8052000-7f0de8251000 ---p 001df000 fd:00 3173292 /usr/lib64/libpython2.7.so.1.0
147 7f0de8251000-7f0de8255000 r--p 001de000 fd:00 3173292 /usr/lib64/libpython2.7.so.1.0
148 7f0de8255000-7f0de8291000 rw-p 001e2000 fd:00 3173292 /usr/lib64/libpython2.7.so.1.0
149 $
150
151Signed-off-by: Jaroslav Škarvada <jskarvad@redhat.com>
152Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
153Cc: Adrian Hunter <adrian.hunter@intel.com>
154Cc: David Ahern <dsahern@gmail.com>
155Cc: Jiri Olsa <jolsa@kernel.org>
156Cc: Namhyung Kim <namhyung@kernel.org>
157Cc: Wang Nan <wangnan0@huawei.com>
158LPU-Reference: 20180119205641.24242-1-jskarvad@redhat.com
159Link: https://lkml.kernel.org/n/tip-8d7dt9kqp83vsz25hagug8fu@git.kernel.org
160[ Removed explicit check for python version, allowing it to really build with python3 ]
161Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
162
163Upstream status: Backport [https://github.com/torvalds/linux/commit/66dfdff03d196e51322c6a85c0d8db8bb2bdd655]
164Signed-off-by: Naveen Saini <naveen.kumar.saini@intel.com>
165---
166 tools/perf/Makefile.config | 23 +--
167 tools/perf/Makefile.perf | 4 +-
168 .../scripts/python/Perf-Trace-Util/Context.c | 34 +++-
169 tools/perf/util/python.c | 95 ++++++++---
170 .../scripting-engines/trace-event-python.c | 147 +++++++++++++-----
171 tools/perf/util/setup.py | 6 +-
172 6 files changed, 221 insertions(+), 88 deletions(-)
173
174diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
175index f362ee46506a..ecdc3770d899 100644
176--- a/tools/perf/Makefile.config
177+++ b/tools/perf/Makefile.config
178@@ -622,25 +622,10 @@ else
179 ifneq ($(feature-libpython), 1)
180 $(call disable-python,No 'Python.h' (for Python 2.x support) was found: disables Python support - please install python-devel/python-dev)
181 else
182- ifneq ($(feature-libpython-version), 1)
183- $(warning Python 3 is not yet supported; please set)
184- $(warning PYTHON and/or PYTHON_CONFIG appropriately.)
185- $(warning If you also have Python 2 installed, then)
186- $(warning try something like:)
187- $(warning $(and ,))
188- $(warning $(and ,) make PYTHON=python2)
189- $(warning $(and ,))
190- $(warning Otherwise, disable Python support entirely:)
191- $(warning $(and ,))
192- $(warning $(and ,) make NO_LIBPYTHON=1)
193- $(warning $(and ,))
194- $(error $(and ,))
195- else
196- LDFLAGS += $(PYTHON_EMBED_LDFLAGS)
197- EXTLIBS += $(PYTHON_EMBED_LIBADD)
198- LANG_BINDINGS += $(obj-perf)python/perf.so
199- $(call detected,CONFIG_LIBPYTHON)
200- endif
201+ LDFLAGS += $(PYTHON_EMBED_LDFLAGS)
202+ EXTLIBS += $(PYTHON_EMBED_LIBADD)
203+ LANG_BINDINGS += $(obj-perf)python/perf.so
204+ $(call detected,CONFIG_LIBPYTHON)
205 endif
206 endif
207 endif
208diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
209index 349ea5133d83..6879321c612d 100644
210--- a/tools/perf/Makefile.perf
211+++ b/tools/perf/Makefile.perf
212@@ -294,7 +294,7 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/
213 PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/
214 export PYTHON_EXTBUILD_LIB PYTHON_EXTBUILD_TMP
215
216-python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf.so
217+python-clean := $(call QUIET_CLEAN, python) $(RM) -r $(PYTHON_EXTBUILD) $(OUTPUT)python/perf*.so
218
219 PYTHON_EXT_SRCS := $(shell grep -v ^\# util/python-ext-sources)
220 PYTHON_EXT_DEPS := util/python-ext-sources util/setup.py $(LIBTRACEEVENT) $(LIBAPI)
221@@ -444,7 +444,7 @@ $(OUTPUT)python/perf.so: $(PYTHON_EXT_SRCS) $(PYTHON_EXT_DEPS) $(LIBTRACEEVENT_D
222 $(PYTHON_WORD) util/setup.py \
223 --quiet build_ext; \
224 mkdir -p $(OUTPUT)python && \
225- cp $(PYTHON_EXTBUILD_LIB)perf.so $(OUTPUT)python/
226+ cp $(PYTHON_EXTBUILD_LIB)perf*.so $(OUTPUT)python/
227
228 please_set_SHELL_PATH_to_a_more_modern_shell:
229 $(Q)$$(:)
230diff --git a/tools/perf/scripts/python/Perf-Trace-Util/Context.c b/tools/perf/scripts/python/Perf-Trace-Util/Context.c
231index fcd1dd667906..1a0d27757eec 100644
232--- a/tools/perf/scripts/python/Perf-Trace-Util/Context.c
233+++ b/tools/perf/scripts/python/Perf-Trace-Util/Context.c
234@@ -23,7 +23,17 @@
235 #include "../../../perf.h"
236 #include "../../../util/trace-event.h"
237
238+#if PY_MAJOR_VERSION < 3
239+#define _PyCapsule_GetPointer(arg1, arg2) \
240+ PyCObject_AsVoidPtr(arg1)
241+
242 PyMODINIT_FUNC initperf_trace_context(void);
243+#else
244+#define _PyCapsule_GetPointer(arg1, arg2) \
245+ PyCapsule_GetPointer((arg1), (arg2))
246+
247+PyMODINIT_FUNC PyInit_perf_trace_context(void);
248+#endif
249
250 static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
251 {
252@@ -34,7 +44,7 @@ static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
253 if (!PyArg_ParseTuple(args, "O", &context))
254 return NULL;
255
256- scripting_context = PyCObject_AsVoidPtr(context);
257+ scripting_context = _PyCapsule_GetPointer(context, NULL);
258 retval = common_pc(scripting_context);
259
260 return Py_BuildValue("i", retval);
261@@ -50,7 +60,7 @@ static PyObject *perf_trace_context_common_flags(PyObject *obj,
262 if (!PyArg_ParseTuple(args, "O", &context))
263 return NULL;
264
265- scripting_context = PyCObject_AsVoidPtr(context);
266+ scripting_context = _PyCapsule_GetPointer(context, NULL);
267 retval = common_flags(scripting_context);
268
269 return Py_BuildValue("i", retval);
270@@ -66,7 +76,7 @@ static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
271 if (!PyArg_ParseTuple(args, "O", &context))
272 return NULL;
273
274- scripting_context = PyCObject_AsVoidPtr(context);
275+ scripting_context = _PyCapsule_GetPointer(context, NULL);
276 retval = common_lock_depth(scripting_context);
277
278 return Py_BuildValue("i", retval);
279@@ -82,7 +92,25 @@ static PyMethodDef ContextMethods[] = {
280 { NULL, NULL, 0, NULL}
281 };
282
283+#if PY_MAJOR_VERSION < 3
284 PyMODINIT_FUNC initperf_trace_context(void)
285 {
286 (void) Py_InitModule("perf_trace_context", ContextMethods);
287 }
288+#else
289+PyMODINIT_FUNC PyInit_perf_trace_context(void)
290+{
291+ static struct PyModuleDef moduledef = {
292+ PyModuleDef_HEAD_INIT,
293+ "perf_trace_context", /* m_name */
294+ "", /* m_doc */
295+ -1, /* m_size */
296+ ContextMethods, /* m_methods */
297+ NULL, /* m_reload */
298+ NULL, /* m_traverse */
299+ NULL, /* m_clear */
300+ NULL, /* m_free */
301+ };
302+ return PyModule_Create(&moduledef);
303+}
304+#endif
305diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
306index 8e49d9cafcfc..16620c37fd8c 100644
307--- a/tools/perf/util/python.c
308+++ b/tools/perf/util/python.c
309@@ -12,6 +12,30 @@
310 #include "print_binary.h"
311 #include "thread_map.h"
312
313+#if PY_MAJOR_VERSION < 3
314+#define _PyUnicode_FromString(arg) \
315+ PyString_FromString(arg)
316+#define _PyUnicode_AsString(arg) \
317+ PyString_AsString(arg)
318+#define _PyUnicode_FromFormat(...) \
319+ PyString_FromFormat(__VA_ARGS__)
320+#define _PyLong_FromLong(arg) \
321+ PyInt_FromLong(arg)
322+
323+#else
324+
325+#define _PyUnicode_FromString(arg) \
326+ PyUnicode_FromString(arg)
327+#define _PyUnicode_FromFormat(...) \
328+ PyUnicode_FromFormat(__VA_ARGS__)
329+#define _PyLong_FromLong(arg) \
330+ PyLong_FromLong(arg)
331+#endif
332+
333+#ifndef Py_TYPE
334+#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
335+#endif
336+
337 /*
338 * Provide these two so that we don't have to link against callchain.c and
339 * start dragging hist.c, etc.
340@@ -49,7 +73,11 @@ int eprintf(int level, int var, const char *fmt, ...)
341 # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
342 #endif
343
344+#if PY_MAJOR_VERSION < 3
345 PyMODINIT_FUNC initperf(void);
346+#else
347+PyMODINIT_FUNC PyInit_perf(void);
348+#endif
349
350 #define member_def(type, member, ptype, help) \
351 { #member, ptype, \
352@@ -107,7 +135,7 @@ static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
353 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
354 ret = PyErr_NoMemory();
355 } else {
356- ret = PyString_FromString(s);
357+ ret = _PyUnicode_FromString(s);
358 free(s);
359 }
360 return ret;
361@@ -138,7 +166,7 @@ static PyMemberDef pyrf_task_event__members[] = {
362
363 static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
364 {
365- return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
366+ return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
367 "ptid: %u, time: %" PRIu64 "}",
368 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
369 pevent->event.fork.pid,
370@@ -171,7 +199,7 @@ static PyMemberDef pyrf_comm_event__members[] = {
371
372 static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
373 {
374- return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
375+ return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
376 pevent->event.comm.pid,
377 pevent->event.comm.tid,
378 pevent->event.comm.comm);
379@@ -202,7 +230,7 @@ static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
380 {
381 struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
382
383- return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
384+ return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
385 ", stream_id: %" PRIu64 " }",
386 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
387 te->time, te->id, te->stream_id);
388@@ -237,7 +265,7 @@ static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
389 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
390 ret = PyErr_NoMemory();
391 } else {
392- ret = PyString_FromString(s);
393+ ret = _PyUnicode_FromString(s);
394 free(s);
395 }
396 return ret;
397@@ -264,7 +292,7 @@ static PyMemberDef pyrf_read_event__members[] = {
398
399 static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
400 {
401- return PyString_FromFormat("{ type: read, pid: %u, tid: %u }",
402+ return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
403 pevent->event.read.pid,
404 pevent->event.read.tid);
405 /*
406@@ -299,7 +327,7 @@ static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
407 if (asprintf(&s, "{ type: sample }") < 0) {
408 ret = PyErr_NoMemory();
409 } else {
410- ret = PyString_FromString(s);
411+ ret = _PyUnicode_FromString(s);
412 free(s);
413 }
414 return ret;
415@@ -330,7 +358,7 @@ tracepoint_field(struct pyrf_event *pe, struct format_field *field)
416 }
417 if (field->flags & FIELD_IS_STRING &&
418 is_printable_array(data + offset, len)) {
419- ret = PyString_FromString((char *)data + offset);
420+ ret = _PyUnicode_FromString((char *)data + offset);
421 } else {
422 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
423 field->flags &= ~FIELD_IS_STRING;
424@@ -352,7 +380,7 @@ tracepoint_field(struct pyrf_event *pe, struct format_field *field)
425 static PyObject*
426 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
427 {
428- const char *str = PyString_AsString(PyObject_Str(attr_name));
429+ const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
430 struct perf_evsel *evsel = pevent->evsel;
431 struct format_field *field;
432
433@@ -416,7 +444,7 @@ static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
434 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
435 ret = PyErr_NoMemory();
436 } else {
437- ret = PyString_FromString(s);
438+ ret = _PyUnicode_FromString(s);
439 free(s);
440 }
441 return ret;
442@@ -528,7 +556,7 @@ static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
443 static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
444 {
445 cpu_map__put(pcpus->cpus);
446- pcpus->ob_type->tp_free((PyObject*)pcpus);
447+ Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
448 }
449
450 static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
451@@ -597,7 +625,7 @@ static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
452 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
453 {
454 thread_map__put(pthreads->threads);
455- pthreads->ob_type->tp_free((PyObject*)pthreads);
456+ Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
457 }
458
459 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
460@@ -759,7 +787,7 @@ static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
461 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
462 {
463 perf_evsel__exit(&pevsel->evsel);
464- pevsel->ob_type->tp_free((PyObject*)pevsel);
465+ Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
466 }
467
468 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
469@@ -850,7 +878,7 @@ static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
470 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
471 {
472 perf_evlist__exit(&pevlist->evlist);
473- pevlist->ob_type->tp_free((PyObject*)pevlist);
474+ Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
475 }
476
477 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
478@@ -902,12 +930,16 @@ static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
479
480 for (i = 0; i < evlist->pollfd.nr; ++i) {
481 PyObject *file;
482+#if PY_MAJOR_VERSION < 3
483 FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
484
485 if (fp == NULL)
486 goto free_list;
487
488 file = PyFile_FromFile(fp, "perf", "r", NULL);
489+#else
490+ file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1, NULL, NULL, NULL, 1);
491+#endif
492 if (file == NULL)
493 goto free_list;
494
495@@ -1194,9 +1226,9 @@ static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
496
497 tp_format = trace_event__tp_format(sys, name);
498 if (IS_ERR(tp_format))
499- return PyInt_FromLong(-1);
500+ return _PyLong_FromLong(-1);
501
502- return PyInt_FromLong(tp_format->id);
503+ return _PyLong_FromLong(tp_format->id);
504 }
505
506 static PyMethodDef perf__methods[] = {
507@@ -1209,11 +1241,31 @@ static PyMethodDef perf__methods[] = {
508 { .ml_name = NULL, }
509 };
510
511+#if PY_MAJOR_VERSION < 3
512 PyMODINIT_FUNC initperf(void)
513+#else
514+PyMODINIT_FUNC PyInit_perf(void)
515+#endif
516 {
517 PyObject *obj;
518 int i;
519- PyObject *dict, *module = Py_InitModule("perf", perf__methods);
520+ PyObject *dict;
521+#if PY_MAJOR_VERSION < 3
522+ PyObject *module = Py_InitModule("perf", perf__methods);
523+#else
524+ static struct PyModuleDef moduledef = {
525+ PyModuleDef_HEAD_INIT,
526+ "perf", /* m_name */
527+ "", /* m_doc */
528+ -1, /* m_size */
529+ perf__methods, /* m_methods */
530+ NULL, /* m_reload */
531+ NULL, /* m_traverse */
532+ NULL, /* m_clear */
533+ NULL, /* m_free */
534+ };
535+ PyObject *module = PyModule_Create(&moduledef);
536+#endif
537
538 if (module == NULL ||
539 pyrf_event__setup_types() < 0 ||
540@@ -1221,7 +1273,11 @@ PyMODINIT_FUNC initperf(void)
541 pyrf_evsel__setup_types() < 0 ||
542 pyrf_thread_map__setup_types() < 0 ||
543 pyrf_cpu_map__setup_types() < 0)
544+#if PY_MAJOR_VERSION < 3
545 return;
546+#else
547+ return module;
548+#endif
549
550 /* The page_size is placed in util object. */
551 page_size = sysconf(_SC_PAGE_SIZE);
552@@ -1270,7 +1326,7 @@ PyMODINIT_FUNC initperf(void)
553 goto error;
554
555 for (i = 0; perf__constants[i].name != NULL; i++) {
556- obj = PyInt_FromLong(perf__constants[i].value);
557+ obj = _PyLong_FromLong(perf__constants[i].value);
558 if (obj == NULL)
559 goto error;
560 PyDict_SetItemString(dict, perf__constants[i].name, obj);
561@@ -1280,6 +1336,9 @@ PyMODINIT_FUNC initperf(void)
562 error:
563 if (PyErr_Occurred())
564 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
565+#if PY_MAJOR_VERSION >= 3
566+ return module;
567+#endif
568 }
569
570 /*
571diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
572index f03fa7a835a1..8db28d4ff4b6 100644
573--- a/tools/perf/util/scripting-engines/trace-event-python.c
574+++ b/tools/perf/util/scripting-engines/trace-event-python.c
575@@ -50,7 +50,37 @@
576 #include "print_binary.h"
577 #include "stat.h"
578
579+#if PY_MAJOR_VERSION < 3
580+#define _PyUnicode_FromString(arg) \
581+ PyString_FromString(arg)
582+#define _PyUnicode_FromStringAndSize(arg1, arg2) \
583+ PyString_FromStringAndSize((arg1), (arg2))
584+#define _PyBytes_FromStringAndSize(arg1, arg2) \
585+ PyString_FromStringAndSize((arg1), (arg2))
586+#define _PyLong_FromLong(arg) \
587+ PyInt_FromLong(arg)
588+#define _PyLong_AsLong(arg) \
589+ PyInt_AsLong(arg)
590+#define _PyCapsule_New(arg1, arg2, arg3) \
591+ PyCObject_FromVoidPtr((arg1), (arg2))
592+
593 PyMODINIT_FUNC initperf_trace_context(void);
594+#else
595+#define _PyUnicode_FromString(arg) \
596+ PyUnicode_FromString(arg)
597+#define _PyUnicode_FromStringAndSize(arg1, arg2) \
598+ PyUnicode_FromStringAndSize((arg1), (arg2))
599+#define _PyBytes_FromStringAndSize(arg1, arg2) \
600+ PyBytes_FromStringAndSize((arg1), (arg2))
601+#define _PyLong_FromLong(arg) \
602+ PyLong_FromLong(arg)
603+#define _PyLong_AsLong(arg) \
604+ PyLong_AsLong(arg)
605+#define _PyCapsule_New(arg1, arg2, arg3) \
606+ PyCapsule_New((arg1), (arg2), (arg3))
607+
608+PyMODINIT_FUNC PyInit_perf_trace_context(void);
609+#endif
610
611 #define TRACE_EVENT_TYPE_MAX \
612 ((1 << (sizeof(unsigned short) * 8)) - 1)
613@@ -136,7 +166,7 @@ static int get_argument_count(PyObject *handler)
614 PyObject *arg_count_obj = PyObject_GetAttrString(code_obj,
615 "co_argcount");
616 if (arg_count_obj) {
617- arg_count = (int) PyInt_AsLong(arg_count_obj);
618+ arg_count = (int) _PyLong_AsLong(arg_count_obj);
619 Py_DECREF(arg_count_obj);
620 }
621 Py_DECREF(code_obj);
622@@ -183,10 +213,10 @@ static void define_value(enum print_arg_type field_type,
623
624 value = eval_flag(field_value);
625
626- PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
627- PyTuple_SetItem(t, n++, PyString_FromString(field_name));
628- PyTuple_SetItem(t, n++, PyInt_FromLong(value));
629- PyTuple_SetItem(t, n++, PyString_FromString(field_str));
630+ PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
631+ PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
632+ PyTuple_SetItem(t, n++, _PyLong_FromLong(value));
633+ PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_str));
634
635 try_call_object(handler_name, t);
636
637@@ -224,10 +254,10 @@ static void define_field(enum print_arg_type field_type,
638 if (!t)
639 Py_FatalError("couldn't create Python tuple");
640
641- PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
642- PyTuple_SetItem(t, n++, PyString_FromString(field_name));
643+ PyTuple_SetItem(t, n++, _PyUnicode_FromString(ev_name));
644+ PyTuple_SetItem(t, n++, _PyUnicode_FromString(field_name));
645 if (field_type == PRINT_FLAGS)
646- PyTuple_SetItem(t, n++, PyString_FromString(delim));
647+ PyTuple_SetItem(t, n++, _PyUnicode_FromString(delim));
648
649 try_call_object(handler_name, t);
650
651@@ -326,12 +356,12 @@ static PyObject *get_field_numeric_entry(struct event_format *event,
652 if (field->flags & FIELD_IS_SIGNED) {
653 if ((long long)val >= LONG_MIN &&
654 (long long)val <= LONG_MAX)
655- obj = PyInt_FromLong(val);
656+ obj = _PyLong_FromLong(val);
657 else
658 obj = PyLong_FromLongLong(val);
659 } else {
660 if (val <= LONG_MAX)
661- obj = PyInt_FromLong(val);
662+ obj = _PyLong_FromLong(val);
663 else
664 obj = PyLong_FromUnsignedLongLong(val);
665 }
666@@ -390,9 +420,9 @@ static PyObject *python_process_callchain(struct perf_sample *sample,
667 pydict_set_item_string_decref(pysym, "end",
668 PyLong_FromUnsignedLongLong(node->sym->end));
669 pydict_set_item_string_decref(pysym, "binding",
670- PyInt_FromLong(node->sym->binding));
671+ _PyLong_FromLong(node->sym->binding));
672 pydict_set_item_string_decref(pysym, "name",
673- PyString_FromStringAndSize(node->sym->name,
674+ _PyUnicode_FromStringAndSize(node->sym->name,
675 node->sym->namelen));
676 pydict_set_item_string_decref(pyelem, "sym", pysym);
677 }
678@@ -407,7 +437,7 @@ static PyObject *python_process_callchain(struct perf_sample *sample,
679 dsoname = map->dso->name;
680 }
681 pydict_set_item_string_decref(pyelem, "dso",
682- PyString_FromString(dsoname));
683+ _PyUnicode_FromString(dsoname));
684 }
685
686 callchain_cursor_advance(&callchain_cursor);
687@@ -484,16 +514,16 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
688 if (!dict_sample)
689 Py_FatalError("couldn't create Python dictionary");
690
691- pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
692- pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
693+ pydict_set_item_string_decref(dict, "ev_name", _PyUnicode_FromString(perf_evsel__name(evsel)));
694+ pydict_set_item_string_decref(dict, "attr", _PyUnicode_FromStringAndSize(
695 (const char *)&evsel->attr, sizeof(evsel->attr)));
696
697 pydict_set_item_string_decref(dict_sample, "pid",
698- PyInt_FromLong(sample->pid));
699+ _PyLong_FromLong(sample->pid));
700 pydict_set_item_string_decref(dict_sample, "tid",
701- PyInt_FromLong(sample->tid));
702+ _PyLong_FromLong(sample->tid));
703 pydict_set_item_string_decref(dict_sample, "cpu",
704- PyInt_FromLong(sample->cpu));
705+ _PyLong_FromLong(sample->cpu));
706 pydict_set_item_string_decref(dict_sample, "ip",
707 PyLong_FromUnsignedLongLong(sample->ip));
708 pydict_set_item_string_decref(dict_sample, "time",
709@@ -503,17 +533,17 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
710 set_sample_read_in_dict(dict_sample, sample, evsel);
711 pydict_set_item_string_decref(dict, "sample", dict_sample);
712
713- pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
714+ pydict_set_item_string_decref(dict, "raw_buf", _PyBytes_FromStringAndSize(
715 (const char *)sample->raw_data, sample->raw_size));
716 pydict_set_item_string_decref(dict, "comm",
717- PyString_FromString(thread__comm_str(al->thread)));
718+ _PyUnicode_FromString(thread__comm_str(al->thread)));
719 if (al->map) {
720 pydict_set_item_string_decref(dict, "dso",
721- PyString_FromString(al->map->dso->name));
722+ _PyUnicode_FromString(al->map->dso->name));
723 }
724 if (al->sym) {
725 pydict_set_item_string_decref(dict, "symbol",
726- PyString_FromString(al->sym->name));
727+ _PyUnicode_FromString(al->sym->name));
728 }
729
730 pydict_set_item_string_decref(dict, "callchain", callchain);
731@@ -573,9 +603,9 @@ static void python_process_tracepoint(struct perf_sample *sample,
732 scripting_context->event_data = data;
733 scripting_context->pevent = evsel->tp_format->pevent;
734
735- context = PyCObject_FromVoidPtr(scripting_context, NULL);
736+ context = _PyCapsule_New(scripting_context, NULL, NULL);
737
738- PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
739+ PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name));
740 PyTuple_SetItem(t, n++, context);
741
742 /* ip unwinding */
743@@ -584,18 +614,18 @@ static void python_process_tracepoint(struct perf_sample *sample,
744 Py_INCREF(callchain);
745
746 if (!dict) {
747- PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
748- PyTuple_SetItem(t, n++, PyInt_FromLong(s));
749- PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
750- PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
751- PyTuple_SetItem(t, n++, PyString_FromString(comm));
752+ PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
753+ PyTuple_SetItem(t, n++, _PyLong_FromLong(s));
754+ PyTuple_SetItem(t, n++, _PyLong_FromLong(ns));
755+ PyTuple_SetItem(t, n++, _PyLong_FromLong(pid));
756+ PyTuple_SetItem(t, n++, _PyUnicode_FromString(comm));
757 PyTuple_SetItem(t, n++, callchain);
758 } else {
759- pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
760- pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
761- pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
762- pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
763- pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
764+ pydict_set_item_string_decref(dict, "common_cpu", _PyLong_FromLong(cpu));
765+ pydict_set_item_string_decref(dict, "common_s", _PyLong_FromLong(s));
766+ pydict_set_item_string_decref(dict, "common_ns", _PyLong_FromLong(ns));
767+ pydict_set_item_string_decref(dict, "common_pid", _PyLong_FromLong(pid));
768+ pydict_set_item_string_decref(dict, "common_comm", _PyUnicode_FromString(comm));
769 pydict_set_item_string_decref(dict, "common_callchain", callchain);
770 }
771 for (field = event->format.fields; field; field = field->next) {
772@@ -614,7 +644,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
773 }
774 if (field->flags & FIELD_IS_STRING &&
775 is_printable_array(data + offset, len)) {
776- obj = PyString_FromString((char *) data + offset);
777+ obj = _PyUnicode_FromString((char *) data + offset);
778 } else {
779 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
780 field->flags &= ~FIELD_IS_STRING;
781@@ -664,7 +694,7 @@ static PyObject *tuple_new(unsigned int sz)
782 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
783 {
784 #if BITS_PER_LONG == 64
785- return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
786+ return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
787 #endif
788 #if BITS_PER_LONG == 32
789 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
790@@ -673,12 +703,12 @@ static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
791
792 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
793 {
794- return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
795+ return PyTuple_SetItem(t, pos, _PyLong_FromLong(val));
796 }
797
798 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
799 {
800- return PyTuple_SetItem(t, pos, PyString_FromString(s));
801+ return PyTuple_SetItem(t, pos, _PyUnicode_FromString(s));
802 }
803
804 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
805@@ -1024,8 +1054,8 @@ process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
806 return;
807 }
808
809- PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
810- PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
811+ PyTuple_SetItem(t, n++, _PyLong_FromLong(cpu));
812+ PyTuple_SetItem(t, n++, _PyLong_FromLong(thread));
813
814 tuple_set_u64(t, n++, tstamp);
815 tuple_set_u64(t, n++, count->val);
816@@ -1207,27 +1237,58 @@ static void set_table_handlers(struct tables *tables)
817 SET_TABLE_HANDLER(call_return);
818 }
819
820+#if PY_MAJOR_VERSION < 3
821+static void _free_command_line(const char **command_line, int num)
822+{
823+ free(command_line);
824+}
825+#else
826+static void _free_command_line(wchar_t **command_line, int num)
827+{
828+ int i;
829+ for (i = 0; i < num; i++)
830+ PyMem_RawFree(command_line[i]);
831+ free(command_line);
832+}
833+#endif
834+
835+
836 /*
837 * Start trace script
838 */
839 static int python_start_script(const char *script, int argc, const char **argv)
840 {
841 struct tables *tables = &tables_global;
842+#if PY_MAJOR_VERSION < 3
843 const char **command_line;
844+#else
845+ wchar_t **command_line;
846+#endif
847 char buf[PATH_MAX];
848 int i, err = 0;
849 FILE *fp;
850
851+#if PY_MAJOR_VERSION < 3
852 command_line = malloc((argc + 1) * sizeof(const char *));
853 command_line[0] = script;
854 for (i = 1; i < argc + 1; i++)
855 command_line[i] = argv[i - 1];
856+#else
857+ command_line = malloc((argc + 1) * sizeof(wchar_t *));
858+ command_line[0] = Py_DecodeLocale(script, NULL);
859+ for (i = 1; i < argc + 1; i++)
860+ command_line[i] = Py_DecodeLocale(argv[i - 1], NULL);
861+#endif
862
863 Py_Initialize();
864
865+#if PY_MAJOR_VERSION < 3
866 initperf_trace_context();
867-
868 PySys_SetArgv(argc + 1, (char **)command_line);
869+#else
870+ PyInit_perf_trace_context();
871+ PySys_SetArgv(argc + 1, command_line);
872+#endif
873
874 fp = fopen(script, "r");
875 if (!fp) {
876@@ -1257,12 +1318,12 @@ static int python_start_script(const char *script, int argc, const char **argv)
877 goto error;
878 }
879
880- free(command_line);
881+ _free_command_line(command_line, argc + 1);
882
883 return err;
884 error:
885 Py_Finalize();
886- free(command_line);
887+ _free_command_line(command_line, argc + 1);
888
889 return err;
890 }
891diff --git a/tools/perf/util/setup.py b/tools/perf/util/setup.py
892index 23f1bf175179..a5f9e236cc71 100644
893--- a/tools/perf/util/setup.py
894+++ b/tools/perf/util/setup.py
895@@ -1,4 +1,4 @@
896-#!/usr/bin/python2
897+#!/usr/bin/python
898
899 from os import getenv
900
901@@ -37,11 +37,11 @@ build_tmp = getenv('PYTHON_EXTBUILD_TMP')
902 libtraceevent = getenv('LIBTRACEEVENT')
903 libapikfs = getenv('LIBAPI')
904
905-ext_sources = [f.strip() for f in file('util/python-ext-sources')
906+ext_sources = [f.strip() for f in open('util/python-ext-sources')
907 if len(f.strip()) > 0 and f[0] != '#']
908
909 # use full paths with source files
910-ext_sources = map(lambda x: '%s/%s' % (src_perf, x) , ext_sources)
911+ext_sources = list(map(lambda x: '%s/%s' % (src_perf, x) , ext_sources))
912
913 perf = Extension('perf',
914 sources = ext_sources,
915--
9162.17.1
917