Commit 66dfdff0 authored by Jaroslav Škarvada's avatar Jaroslav Škarvada Committed by Arnaldo Carvalho de Melo

perf tools: Add Python 3 support

Added Python 3 support while keeping Python 2.7 compatibility.

Committer notes:

This doesn't make it to auto detect python 3, one has to explicitely ask
it to build with python 3 devel files, here are the instructions
provided by Jaroslav:

 ---
  $ cp -a tools/perf tools/python3-perf
  $ make V=1 prefix=/usr -C tools/perf PYTHON=/usr/bin/python2 all
  $ make V=1 prefix=/usr -C tools/python3-perf PYTHON=/usr/bin/python3 all
  $ make V=1 prefix=/usr -C tools/python3-perf PYTHON=/usr/bin/python3 DESTDIR=%{buildroot} install-python_ext
  $ make V=1 prefix=/usr -C tools/perf PYTHON=/usr/bin/python2 DESTDIR=%{buildroot} install-python_ext
 ---

We need to make this automatic, just like the existing tests for checking if
the python2 devel files are in place, allowing the build with python3 if
available, fallbacking to python2 and then just disabling it if none are
available.

So, using the PYTHON variable to build it using O= we get:

Before this patch:

  $ rpm -q python3 python3-devel
  python3-3.6.4-7.fc27.x86_64
  python3-devel-3.6.4-7.fc27.x86_64
  $ rm -rf /tmp/build/perf/ ; mkdir -p /tmp/build/perf ; make O=/tmp/build/perf PYTHON=/usr/bin/python3 -C tools/perf install-bin
  make: Entering directory '/home/acme/git/linux/tools/perf'
  <SNIP>
  Makefile.config:670: Python 3 is not yet supported; please set
  Makefile.config:671: PYTHON and/or PYTHON_CONFIG appropriately.
  Makefile.config:672: If you also have Python 2 installed, then
  Makefile.config:673: try something like:
  Makefile.config:674:
  Makefile.config:675:   make PYTHON=python2
  Makefile.config:676:
  Makefile.config:677: Otherwise, disable Python support entirely:
  Makefile.config:678:
  Makefile.config:679:   make NO_LIBPYTHON=1
  Makefile.config:680:
  Makefile.config:681: *** .  Stop.
  make[1]: *** [Makefile.perf:212: sub-make] Error 2
  make: *** [Makefile:110: install-bin] Error 2
  make: Leaving directory '/home/acme/git/linux/tools/perf'
  $

After:

  $ make O=/tmp/build/perf PYTHON=python3 -C tools/perf install-bin
  $ ldd ~/bin/perf | grep python
	libpython3.6m.so.1.0 => /lib64/libpython3.6m.so.1.0 (0x00007f58a31e8000)
  $ rpm -qf /lib64/libpython3.6m.so.1.0
  python3-libs-3.6.4-7.fc27.x86_64
  $

Now verify that when using the binding the right ELF file is loaded,
using perf trace:

  $ perf trace -e open* perf test python
     0.051 ( 0.016 ms): perf/3927 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC           ) = 3
<SNIP>
  18: 'import perf' in python                               :
     8.849 ( 0.013 ms): sh/3929 openat(dfd: CWD, filename: /etc/ld.so.cache, flags: CLOEXEC           ) = 3
<SNIP>
    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
<SNIP>
 Ok
<SNIP>
  $

And using tools/perf/python/twatch.py, to show PERF_RECORD_ metaevents:

  $ python3 tools/perf/python/twatch.py
  cpu: 3, pid: 16060, tid: 16060 { type: fork, pid: 5207, ppid: 16060, tid: 5207, ptid: 16060, time: 10798513015459}
  cpu: 3, pid: 16060, tid: 16060 { type: fork, pid: 5208, ppid: 16060, tid: 5208, ptid: 16060, time: 10798513562503}
  cpu: 0, pid: 5208, tid: 5208 { type: comm, pid: 5208, tid: 5208, comm: grep }
  cpu: 2, pid: 5207, tid: 5207 { type: comm, pid: 5207, tid: 5207, comm: ps }
  cpu: 2, pid: 5207, tid: 5207 { type: exit, pid: 5207, ppid: 5207, tid: 5207, ptid: 5207, time: 10798551337484}
  cpu: 3, pid: 5208, tid: 5208 { type: exit, pid: 5208, ppid: 5208, tid: 5208, ptid: 5208, time: 10798551292153}
  cpu: 3, pid: 601, tid: 601 { type: fork, pid: 5209, ppid: 601, tid: 5209, ptid: 601, time: 10801779977324}
  ^CTraceback (most recent call last):
    File "tools/perf/python/twatch.py", line 68, in <module>
      main()
    File "tools/perf/python/twatch.py", line 40, in main
      evlist.poll(timeout = -1)
  KeyboardInterrupt
  $

  # ps ax|grep twatch
 5197 pts/8    S+     0:00 python3 tools/perf/python/twatch.py
  # ls -la /proc/5197/smaps
  -r--r--r--. 1 acme acme 0 Feb 19 13:14 /proc/5197/smaps
  # grep python /proc/5197/smaps
  558111307000-558111309000 r-xp 00000000 fd:00 3151710  /usr/bin/python3.6
  558111508000-558111509000 r--p 00001000 fd:00 3151710  /usr/bin/python3.6
  558111509000-55811150a000 rw-p 00002000 fd:00 3151710  /usr/bin/python3.6
  7ffad6fc1000-7ffad7008000 r-xp 00000000 00:2d 220196   /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so
  7ffad7008000-7ffad7207000 ---p 00047000 00:2d 220196   /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so
  7ffad7207000-7ffad7208000 r--p 00046000 00:2d 220196   /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so
  7ffad7208000-7ffad7215000 rw-p 00047000 00:2d 220196   /tmp/build/perf/python/perf.cpython-36m-x86_64-linux-gnu.so
  7ffadea77000-7ffaded3d000 r-xp 00000000 fd:00 3151795  /usr/lib64/libpython3.6m.so.1.0
  7ffaded3d000-7ffadef3c000 ---p 002c6000 fd:00 3151795  /usr/lib64/libpython3.6m.so.1.0
  7ffadef3c000-7ffadef42000 r--p 002c5000 fd:00 3151795  /usr/lib64/libpython3.6m.so.1.0
  7ffadef42000-7ffadefa5000 rw-p 002cb000 fd:00 3151795  /usr/lib64/libpython3.6m.so.1.0
  #

And with this patch, but building normally, without specifying the
PYTHON=python3 part, which will make it use python2 if its devel files are
available, like in this test:

  $ make O=/tmp/build/perf -C tools/perf install-bin
  $ ldd ~/bin/perf | grep python
	libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007f6a44410000)
  $ ldd /tmp/build/perf/python_ext_build/lib/perf.so  | grep python
	libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007fed28a2c000)
  $

  [acme@jouet perf]$ tools/perf/python/twatch.py
  cpu: 0, pid: 2817, tid: 2817 { type: fork, pid: 2817, ppid: 2817, tid: 8910, ptid: 2817, time: 11126454335306}
  cpu: 0, pid: 2817, tid: 2817 { type: comm, pid: 2817, tid: 8910, comm: worker }
  $ ps ax | grep twatch.py
   8909 pts/8    S+     0:00 /usr/bin/python tools/perf/python/twatch.py
  $ grep python /proc/8909/smaps
  5579de658000-5579de659000 r-xp 00000000 fd:00 3156044  /usr/bin/python2.7
  5579de858000-5579de859000 r--p 00000000 fd:00 3156044  /usr/bin/python2.7
  5579de859000-5579de85a000 rw-p 00001000 fd:00 3156044  /usr/bin/python2.7
  7f0de01f7000-7f0de023e000 r-xp 00000000 00:2d 230695   /tmp/build/perf/python/perf.so
  7f0de023e000-7f0de043d000 ---p 00047000 00:2d 230695   /tmp/build/perf/python/perf.so
  7f0de043d000-7f0de043e000 r--p 00046000 00:2d 230695   /tmp/build/perf/python/perf.so
  7f0de043e000-7f0de044b000 rw-p 00047000 00:2d 230695   /tmp/build/perf/python/perf.so
  7f0de6f0f000-7f0de6f13000 r-xp 00000000 fd:00 134975   /usr/lib64/python2.7/lib-dynload/_localemodule.so
  7f0de6f13000-7f0de7113000 ---p 00004000 fd:00 134975   /usr/lib64/python2.7/lib-dynload/_localemodule.so
  7f0de7113000-7f0de7114000 r--p 00004000 fd:00 134975   /usr/lib64/python2.7/lib-dynload/_localemodule.so
  7f0de7114000-7f0de7115000 rw-p 00005000 fd:00 134975   /usr/lib64/python2.7/lib-dynload/_localemodule.so
  7f0de7e73000-7f0de8052000 r-xp 00000000 fd:00 3173292  /usr/lib64/libpython2.7.so.1.0
  7f0de8052000-7f0de8251000 ---p 001df000 fd:00 3173292  /usr/lib64/libpython2.7.so.1.0
  7f0de8251000-7f0de8255000 r--p 001de000 fd:00 3173292  /usr/lib64/libpython2.7.so.1.0
  7f0de8255000-7f0de8291000 rw-p 001e2000 fd:00 3173292  /usr/lib64/libpython2.7.so.1.0
  $
Signed-off-by: default avatarJaroslav Škarvada <jskarvad@redhat.com>
Tested-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
LPU-Reference: 20180119205641.24242-1-jskarvad@redhat.com
Link: https://lkml.kernel.org/n/tip-8d7dt9kqp83vsz25hagug8fu@git.kernel.org
[ Removed explicit check for python version, allowing it to really build with python3 ]
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent d2ed5d2b
...@@ -668,25 +668,10 @@ else ...@@ -668,25 +668,10 @@ else
ifneq ($(feature-libpython), 1) ifneq ($(feature-libpython), 1)
$(call disable-python,No 'Python.h' (for Python 2.x support) was found: disables Python support - please install python-devel/python-dev) $(call disable-python,No 'Python.h' (for Python 2.x support) was found: disables Python support - please install python-devel/python-dev)
else else
ifneq ($(feature-libpython-version), 1) LDFLAGS += $(PYTHON_EMBED_LDFLAGS)
$(warning Python 3 is not yet supported; please set) EXTLIBS += $(PYTHON_EMBED_LIBADD)
$(warning PYTHON and/or PYTHON_CONFIG appropriately.) LANG_BINDINGS += $(obj-perf)python/perf.so
$(warning If you also have Python 2 installed, then) $(call detected,CONFIG_LIBPYTHON)
$(warning try something like:)
$(warning $(and ,))
$(warning $(and ,) make PYTHON=python2)
$(warning $(and ,))
$(warning Otherwise, disable Python support entirely:)
$(warning $(and ,))
$(warning $(and ,) make NO_LIBPYTHON=1)
$(warning $(and ,))
$(error $(and ,))
else
LDFLAGS += $(PYTHON_EMBED_LDFLAGS)
EXTLIBS += $(PYTHON_EMBED_LIBADD)
LANG_BINDINGS += $(obj-perf)python/perf.so
$(call detected,CONFIG_LIBPYTHON)
endif
endif endif
endif endif
endif endif
......
...@@ -302,7 +302,7 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/ ...@@ -302,7 +302,7 @@ PYTHON_EXTBUILD_LIB := $(PYTHON_EXTBUILD)lib/
PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/ PYTHON_EXTBUILD_TMP := $(PYTHON_EXTBUILD)tmp/
export PYTHON_EXTBUILD_LIB 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_SRCS := $(shell grep -v ^\# util/python-ext-sources)
PYTHON_EXT_DEPS := util/python-ext-sources util/setup.py $(LIBTRACEEVENT) $(LIBAPI) PYTHON_EXT_DEPS := util/python-ext-sources util/setup.py $(LIBTRACEEVENT) $(LIBAPI)
...@@ -479,7 +479,7 @@ $(OUTPUT)python/perf.so: $(PYTHON_EXT_SRCS) $(PYTHON_EXT_DEPS) $(LIBTRACEEVENT_D ...@@ -479,7 +479,7 @@ $(OUTPUT)python/perf.so: $(PYTHON_EXT_SRCS) $(PYTHON_EXT_DEPS) $(LIBTRACEEVENT_D
$(PYTHON_WORD) util/setup.py \ $(PYTHON_WORD) util/setup.py \
--quiet build_ext; \ --quiet build_ext; \
mkdir -p $(OUTPUT)python && \ 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: please_set_SHELL_PATH_to_a_more_modern_shell:
$(Q)$$(:) $(Q)$$(:)
......
...@@ -23,7 +23,17 @@ ...@@ -23,7 +23,17 @@
#include "../../../perf.h" #include "../../../perf.h"
#include "../../../util/trace-event.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); 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) 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) ...@@ -34,7 +44,7 @@ static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
if (!PyArg_ParseTuple(args, "O", &context)) if (!PyArg_ParseTuple(args, "O", &context))
return NULL; return NULL;
scripting_context = PyCObject_AsVoidPtr(context); scripting_context = _PyCapsule_GetPointer(context, NULL);
retval = common_pc(scripting_context); retval = common_pc(scripting_context);
return Py_BuildValue("i", retval); return Py_BuildValue("i", retval);
...@@ -50,7 +60,7 @@ static PyObject *perf_trace_context_common_flags(PyObject *obj, ...@@ -50,7 +60,7 @@ static PyObject *perf_trace_context_common_flags(PyObject *obj,
if (!PyArg_ParseTuple(args, "O", &context)) if (!PyArg_ParseTuple(args, "O", &context))
return NULL; return NULL;
scripting_context = PyCObject_AsVoidPtr(context); scripting_context = _PyCapsule_GetPointer(context, NULL);
retval = common_flags(scripting_context); retval = common_flags(scripting_context);
return Py_BuildValue("i", retval); return Py_BuildValue("i", retval);
...@@ -66,7 +76,7 @@ static PyObject *perf_trace_context_common_lock_depth(PyObject *obj, ...@@ -66,7 +76,7 @@ static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
if (!PyArg_ParseTuple(args, "O", &context)) if (!PyArg_ParseTuple(args, "O", &context))
return NULL; return NULL;
scripting_context = PyCObject_AsVoidPtr(context); scripting_context = _PyCapsule_GetPointer(context, NULL);
retval = common_lock_depth(scripting_context); retval = common_lock_depth(scripting_context);
return Py_BuildValue("i", retval); return Py_BuildValue("i", retval);
...@@ -82,7 +92,25 @@ static PyMethodDef ContextMethods[] = { ...@@ -82,7 +92,25 @@ static PyMethodDef ContextMethods[] = {
{ NULL, NULL, 0, NULL} { NULL, NULL, 0, NULL}
}; };
#if PY_MAJOR_VERSION < 3
PyMODINIT_FUNC initperf_trace_context(void) PyMODINIT_FUNC initperf_trace_context(void)
{ {
(void) Py_InitModule("perf_trace_context", ContextMethods); (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
...@@ -12,6 +12,30 @@ ...@@ -12,6 +12,30 @@
#include "print_binary.h" #include "print_binary.h"
#include "thread_map.h" #include "thread_map.h"
#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
#ifndef Py_TYPE
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
#endif
/* /*
* Provide these two so that we don't have to link against callchain.c and * Provide these two so that we don't have to link against callchain.c and
* start dragging hist.c, etc. * start dragging hist.c, etc.
...@@ -49,7 +73,11 @@ int eprintf(int level, int var, const char *fmt, ...) ...@@ -49,7 +73,11 @@ int eprintf(int level, int var, const char *fmt, ...)
# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
#endif #endif
#if PY_MAJOR_VERSION < 3
PyMODINIT_FUNC initperf(void); PyMODINIT_FUNC initperf(void);
#else
PyMODINIT_FUNC PyInit_perf(void);
#endif
#define member_def(type, member, ptype, help) \ #define member_def(type, member, ptype, help) \
{ #member, ptype, \ { #member, ptype, \
...@@ -107,7 +135,7 @@ static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent) ...@@ -107,7 +135,7 @@ static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
ret = PyErr_NoMemory(); ret = PyErr_NoMemory();
} else { } else {
ret = PyString_FromString(s); ret = _PyUnicode_FromString(s);
free(s); free(s);
} }
return ret; return ret;
...@@ -138,7 +166,7 @@ static PyMemberDef pyrf_task_event__members[] = { ...@@ -138,7 +166,7 @@ static PyMemberDef pyrf_task_event__members[] = {
static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent) static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
{ {
return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
"ptid: %u, time: %" PRIu64 "}", "ptid: %u, time: %" PRIu64 "}",
pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit", pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
pevent->event.fork.pid, pevent->event.fork.pid,
...@@ -171,7 +199,7 @@ static PyMemberDef pyrf_comm_event__members[] = { ...@@ -171,7 +199,7 @@ static PyMemberDef pyrf_comm_event__members[] = {
static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent) static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
{ {
return PyString_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.pid,
pevent->event.comm.tid, pevent->event.comm.tid,
pevent->event.comm.comm); pevent->event.comm.comm);
...@@ -202,7 +230,7 @@ static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent) ...@@ -202,7 +230,7 @@ static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
{ {
struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1); struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
", stream_id: %" PRIu64 " }", ", stream_id: %" PRIu64 " }",
pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un", pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
te->time, te->id, te->stream_id); te->time, te->id, te->stream_id);
...@@ -237,7 +265,7 @@ static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent) ...@@ -237,7 +265,7 @@ static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
pevent->event.lost.id, pevent->event.lost.lost) < 0) { pevent->event.lost.id, pevent->event.lost.lost) < 0) {
ret = PyErr_NoMemory(); ret = PyErr_NoMemory();
} else { } else {
ret = PyString_FromString(s); ret = _PyUnicode_FromString(s);
free(s); free(s);
} }
return ret; return ret;
...@@ -264,7 +292,7 @@ static PyMemberDef pyrf_read_event__members[] = { ...@@ -264,7 +292,7 @@ static PyMemberDef pyrf_read_event__members[] = {
static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent) static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
{ {
return PyString_FromFormat("{ type: read, pid: %u, tid: %u }", return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
pevent->event.read.pid, pevent->event.read.pid,
pevent->event.read.tid); pevent->event.read.tid);
/* /*
...@@ -299,7 +327,7 @@ static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent) ...@@ -299,7 +327,7 @@ static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
if (asprintf(&s, "{ type: sample }") < 0) { if (asprintf(&s, "{ type: sample }") < 0) {
ret = PyErr_NoMemory(); ret = PyErr_NoMemory();
} else { } else {
ret = PyString_FromString(s); ret = _PyUnicode_FromString(s);
free(s); free(s);
} }
return ret; return ret;
...@@ -330,7 +358,7 @@ tracepoint_field(struct pyrf_event *pe, struct format_field *field) ...@@ -330,7 +358,7 @@ tracepoint_field(struct pyrf_event *pe, struct format_field *field)
} }
if (field->flags & FIELD_IS_STRING && if (field->flags & FIELD_IS_STRING &&
is_printable_array(data + offset, len)) { is_printable_array(data + offset, len)) {
ret = PyString_FromString((char *)data + offset); ret = _PyUnicode_FromString((char *)data + offset);
} else { } else {
ret = PyByteArray_FromStringAndSize((const char *) data + offset, len); ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
field->flags &= ~FIELD_IS_STRING; field->flags &= ~FIELD_IS_STRING;
...@@ -352,7 +380,7 @@ tracepoint_field(struct pyrf_event *pe, struct format_field *field) ...@@ -352,7 +380,7 @@ tracepoint_field(struct pyrf_event *pe, struct format_field *field)
static PyObject* static PyObject*
get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
{ {
const char *str = PyString_AsString(PyObject_Str(attr_name)); const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
struct perf_evsel *evsel = pevent->evsel; struct perf_evsel *evsel = pevent->evsel;
struct format_field *field; struct format_field *field;
...@@ -416,7 +444,7 @@ static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent) ...@@ -416,7 +444,7 @@ static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
!!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) { !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
ret = PyErr_NoMemory(); ret = PyErr_NoMemory();
} else { } else {
ret = PyString_FromString(s); ret = _PyUnicode_FromString(s);
free(s); free(s);
} }
return ret; return ret;
...@@ -528,7 +556,7 @@ static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus, ...@@ -528,7 +556,7 @@ static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus) static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
{ {
cpu_map__put(pcpus->cpus); cpu_map__put(pcpus->cpus);
pcpus->ob_type->tp_free((PyObject*)pcpus); Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
} }
static Py_ssize_t pyrf_cpu_map__length(PyObject *obj) static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
...@@ -597,7 +625,7 @@ static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads, ...@@ -597,7 +625,7 @@ static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads) static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
{ {
thread_map__put(pthreads->threads); thread_map__put(pthreads->threads);
pthreads->ob_type->tp_free((PyObject*)pthreads); Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
} }
static Py_ssize_t pyrf_thread_map__length(PyObject *obj) static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
...@@ -759,7 +787,7 @@ static int pyrf_evsel__init(struct pyrf_evsel *pevsel, ...@@ -759,7 +787,7 @@ static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
static void pyrf_evsel__delete(struct pyrf_evsel *pevsel) static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
{ {
perf_evsel__exit(&pevsel->evsel); perf_evsel__exit(&pevsel->evsel);
pevsel->ob_type->tp_free((PyObject*)pevsel); Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
} }
static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
...@@ -850,7 +878,7 @@ static int pyrf_evlist__init(struct pyrf_evlist *pevlist, ...@@ -850,7 +878,7 @@ static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
static void pyrf_evlist__delete(struct pyrf_evlist *pevlist) static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
{ {
perf_evlist__exit(&pevlist->evlist); perf_evlist__exit(&pevlist->evlist);
pevlist->ob_type->tp_free((PyObject*)pevlist); Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
} }
static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
...@@ -902,12 +930,16 @@ static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist, ...@@ -902,12 +930,16 @@ static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
for (i = 0; i < evlist->pollfd.nr; ++i) { for (i = 0; i < evlist->pollfd.nr; ++i) {
PyObject *file; PyObject *file;
#if PY_MAJOR_VERSION < 3
FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r"); FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
if (fp == NULL) if (fp == NULL)
goto free_list; goto free_list;
file = PyFile_FromFile(fp, "perf", "r", NULL); file = PyFile_FromFile(fp, "perf", "r", NULL);
#else
file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1, NULL, NULL, NULL, 1);
#endif
if (file == NULL) if (file == NULL)
goto free_list; goto free_list;
...@@ -1194,9 +1226,9 @@ static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel, ...@@ -1194,9 +1226,9 @@ static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
tp_format = trace_event__tp_format(sys, name); tp_format = trace_event__tp_format(sys, name);
if (IS_ERR(tp_format)) if (IS_ERR(tp_format))
return PyInt_FromLong(-1); return _PyLong_FromLong(-1);
return PyInt_FromLong(tp_format->id); return _PyLong_FromLong(tp_format->id);
} }
static PyMethodDef perf__methods[] = { static PyMethodDef perf__methods[] = {
...@@ -1209,11 +1241,31 @@ static PyMethodDef perf__methods[] = { ...@@ -1209,11 +1241,31 @@ static PyMethodDef perf__methods[] = {
{ .ml_name = NULL, } { .ml_name = NULL, }
}; };
#if PY_MAJOR_VERSION < 3
PyMODINIT_FUNC initperf(void) PyMODINIT_FUNC initperf(void)
#else
PyMODINIT_FUNC PyInit_perf(void)
#endif
{ {
PyObject *obj; PyObject *obj;
int i; int i;
PyObject *dict, *module = Py_InitModule("perf", perf__methods); PyObject *dict;
#if PY_MAJOR_VERSION < 3
PyObject *module = Py_InitModule("perf", perf__methods);
#else
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"perf", /* m_name */
"", /* m_doc */
-1, /* m_size */
perf__methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
PyObject *module = PyModule_Create(&moduledef);
#endif
if (module == NULL || if (module == NULL ||
pyrf_event__setup_types() < 0 || pyrf_event__setup_types() < 0 ||
...@@ -1221,7 +1273,11 @@ PyMODINIT_FUNC initperf(void) ...@@ -1221,7 +1273,11 @@ PyMODINIT_FUNC initperf(void)
pyrf_evsel__setup_types() < 0 || pyrf_evsel__setup_types() < 0 ||
pyrf_thread_map__setup_types() < 0 || pyrf_thread_map__setup_types() < 0 ||
pyrf_cpu_map__setup_types() < 0) pyrf_cpu_map__setup_types() < 0)
#if PY_MAJOR_VERSION < 3
return; return;
#else
return module;
#endif
/* The page_size is placed in util object. */ /* The page_size is placed in util object. */
page_size = sysconf(_SC_PAGE_SIZE); page_size = sysconf(_SC_PAGE_SIZE);
...@@ -1270,7 +1326,7 @@ PyMODINIT_FUNC initperf(void) ...@@ -1270,7 +1326,7 @@ PyMODINIT_FUNC initperf(void)
goto error; goto error;
for (i = 0; perf__constants[i].name != NULL; i++) { for (i = 0; perf__constants[i].name != NULL; i++) {
obj = PyInt_FromLong(perf__constants[i].value); obj = _PyLong_FromLong(perf__constants[i].value);
if (obj == NULL) if (obj == NULL)
goto error; goto error;
PyDict_SetItemString(dict, perf__constants[i].name, obj); PyDict_SetItemString(dict, perf__constants[i].name, obj);
...@@ -1280,6 +1336,9 @@ PyMODINIT_FUNC initperf(void) ...@@ -1280,6 +1336,9 @@ PyMODINIT_FUNC initperf(void)
error: error:
if (PyErr_Occurred()) if (PyErr_Occurred())
PyErr_SetString(PyExc_ImportError, "perf: Init failed!"); PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
#if PY_MAJOR_VERSION >= 3
return module;
#endif
} }
/* /*
......
#!/usr/bin/python2 #!/usr/bin/python
from os import getenv from os import getenv
...@@ -35,11 +35,11 @@ build_tmp = getenv('PYTHON_EXTBUILD_TMP') ...@@ -35,11 +35,11 @@ build_tmp = getenv('PYTHON_EXTBUILD_TMP')
libtraceevent = getenv('LIBTRACEEVENT') libtraceevent = getenv('LIBTRACEEVENT')
libapikfs = getenv('LIBAPI') 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] != '#'] if len(f.strip()) > 0 and f[0] != '#']
# use full paths with source files # 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', perf = Extension('perf',
sources = ext_sources, sources = ext_sources,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment