diff options
Diffstat (limited to 'tools/perf/util/python.c')
-rw-r--r-- | tools/perf/util/python.c | 110 |
1 files changed, 88 insertions, 22 deletions
diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index b1e999bd21ef..863b61478edd 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c | |||
@@ -12,6 +12,30 @@ | |||
12 | #include "print_binary.h" | 12 | #include "print_binary.h" |
13 | #include "thread_map.h" | 13 | #include "thread_map.h" |
14 | 14 | ||
15 | #if PY_MAJOR_VERSION < 3 | ||
16 | #define _PyUnicode_FromString(arg) \ | ||
17 | PyString_FromString(arg) | ||
18 | #define _PyUnicode_AsString(arg) \ | ||
19 | PyString_AsString(arg) | ||
20 | #define _PyUnicode_FromFormat(...) \ | ||
21 | PyString_FromFormat(__VA_ARGS__) | ||
22 | #define _PyLong_FromLong(arg) \ | ||
23 | PyInt_FromLong(arg) | ||
24 | |||
25 | #else | ||
26 | |||
27 | #define _PyUnicode_FromString(arg) \ | ||
28 | PyUnicode_FromString(arg) | ||
29 | #define _PyUnicode_FromFormat(...) \ | ||
30 | PyUnicode_FromFormat(__VA_ARGS__) | ||
31 | #define _PyLong_FromLong(arg) \ | ||
32 | PyLong_FromLong(arg) | ||
33 | #endif | ||
34 | |||
35 | #ifndef Py_TYPE | ||
36 | #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) | ||
37 | #endif | ||
38 | |||
15 | /* | 39 | /* |
16 | * Provide these two so that we don't have to link against callchain.c and | 40 | * Provide these two so that we don't have to link against callchain.c and |
17 | * start dragging hist.c, etc. | 41 | * start dragging hist.c, etc. |
@@ -49,7 +73,11 @@ int eprintf(int level, int var, const char *fmt, ...) | |||
49 | # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, | 73 | # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, |
50 | #endif | 74 | #endif |
51 | 75 | ||
76 | #if PY_MAJOR_VERSION < 3 | ||
52 | PyMODINIT_FUNC initperf(void); | 77 | PyMODINIT_FUNC initperf(void); |
78 | #else | ||
79 | PyMODINIT_FUNC PyInit_perf(void); | ||
80 | #endif | ||
53 | 81 | ||
54 | #define member_def(type, member, ptype, help) \ | 82 | #define member_def(type, member, ptype, help) \ |
55 | { #member, ptype, \ | 83 | { #member, ptype, \ |
@@ -107,7 +135,7 @@ static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent) | |||
107 | pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { | 135 | pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { |
108 | ret = PyErr_NoMemory(); | 136 | ret = PyErr_NoMemory(); |
109 | } else { | 137 | } else { |
110 | ret = PyString_FromString(s); | 138 | ret = _PyUnicode_FromString(s); |
111 | free(s); | 139 | free(s); |
112 | } | 140 | } |
113 | return ret; | 141 | return ret; |
@@ -138,7 +166,7 @@ static PyMemberDef pyrf_task_event__members[] = { | |||
138 | 166 | ||
139 | static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent) | 167 | static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent) |
140 | { | 168 | { |
141 | return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " | 169 | return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " |
142 | "ptid: %u, time: %" PRIu64 "}", | 170 | "ptid: %u, time: %" PRIu64 "}", |
143 | pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit", | 171 | pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit", |
144 | pevent->event.fork.pid, | 172 | pevent->event.fork.pid, |
@@ -171,7 +199,7 @@ static PyMemberDef pyrf_comm_event__members[] = { | |||
171 | 199 | ||
172 | static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent) | 200 | static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent) |
173 | { | 201 | { |
174 | return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", | 202 | return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", |
175 | pevent->event.comm.pid, | 203 | pevent->event.comm.pid, |
176 | pevent->event.comm.tid, | 204 | pevent->event.comm.tid, |
177 | pevent->event.comm.comm); | 205 | pevent->event.comm.comm); |
@@ -202,7 +230,7 @@ static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent) | |||
202 | { | 230 | { |
203 | struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1); | 231 | struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1); |
204 | 232 | ||
205 | return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64 | 233 | return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64 |
206 | ", stream_id: %" PRIu64 " }", | 234 | ", stream_id: %" PRIu64 " }", |
207 | pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un", | 235 | pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un", |
208 | te->time, te->id, te->stream_id); | 236 | te->time, te->id, te->stream_id); |
@@ -237,7 +265,7 @@ static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent) | |||
237 | pevent->event.lost.id, pevent->event.lost.lost) < 0) { | 265 | pevent->event.lost.id, pevent->event.lost.lost) < 0) { |
238 | ret = PyErr_NoMemory(); | 266 | ret = PyErr_NoMemory(); |
239 | } else { | 267 | } else { |
240 | ret = PyString_FromString(s); | 268 | ret = _PyUnicode_FromString(s); |
241 | free(s); | 269 | free(s); |
242 | } | 270 | } |
243 | return ret; | 271 | return ret; |
@@ -264,7 +292,7 @@ static PyMemberDef pyrf_read_event__members[] = { | |||
264 | 292 | ||
265 | static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent) | 293 | static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent) |
266 | { | 294 | { |
267 | return PyString_FromFormat("{ type: read, pid: %u, tid: %u }", | 295 | return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }", |
268 | pevent->event.read.pid, | 296 | pevent->event.read.pid, |
269 | pevent->event.read.tid); | 297 | pevent->event.read.tid); |
270 | /* | 298 | /* |
@@ -299,7 +327,7 @@ static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent) | |||
299 | if (asprintf(&s, "{ type: sample }") < 0) { | 327 | if (asprintf(&s, "{ type: sample }") < 0) { |
300 | ret = PyErr_NoMemory(); | 328 | ret = PyErr_NoMemory(); |
301 | } else { | 329 | } else { |
302 | ret = PyString_FromString(s); | 330 | ret = _PyUnicode_FromString(s); |
303 | free(s); | 331 | free(s); |
304 | } | 332 | } |
305 | return ret; | 333 | return ret; |
@@ -330,7 +358,7 @@ tracepoint_field(struct pyrf_event *pe, struct format_field *field) | |||
330 | } | 358 | } |
331 | if (field->flags & FIELD_IS_STRING && | 359 | if (field->flags & FIELD_IS_STRING && |
332 | is_printable_array(data + offset, len)) { | 360 | is_printable_array(data + offset, len)) { |
333 | ret = PyString_FromString((char *)data + offset); | 361 | ret = _PyUnicode_FromString((char *)data + offset); |
334 | } else { | 362 | } else { |
335 | ret = PyByteArray_FromStringAndSize((const char *) data + offset, len); | 363 | ret = PyByteArray_FromStringAndSize((const char *) data + offset, len); |
336 | field->flags &= ~FIELD_IS_STRING; | 364 | field->flags &= ~FIELD_IS_STRING; |
@@ -352,7 +380,7 @@ tracepoint_field(struct pyrf_event *pe, struct format_field *field) | |||
352 | static PyObject* | 380 | static PyObject* |
353 | get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) | 381 | get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) |
354 | { | 382 | { |
355 | const char *str = PyString_AsString(PyObject_Str(attr_name)); | 383 | const char *str = _PyUnicode_AsString(PyObject_Str(attr_name)); |
356 | struct perf_evsel *evsel = pevent->evsel; | 384 | struct perf_evsel *evsel = pevent->evsel; |
357 | struct format_field *field; | 385 | struct format_field *field; |
358 | 386 | ||
@@ -416,7 +444,7 @@ static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent) | |||
416 | !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) { | 444 | !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) { |
417 | ret = PyErr_NoMemory(); | 445 | ret = PyErr_NoMemory(); |
418 | } else { | 446 | } else { |
419 | ret = PyString_FromString(s); | 447 | ret = _PyUnicode_FromString(s); |
420 | free(s); | 448 | free(s); |
421 | } | 449 | } |
422 | return ret; | 450 | return ret; |
@@ -528,7 +556,7 @@ static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus, | |||
528 | static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus) | 556 | static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus) |
529 | { | 557 | { |
530 | cpu_map__put(pcpus->cpus); | 558 | cpu_map__put(pcpus->cpus); |
531 | pcpus->ob_type->tp_free((PyObject*)pcpus); | 559 | Py_TYPE(pcpus)->tp_free((PyObject*)pcpus); |
532 | } | 560 | } |
533 | 561 | ||
534 | static Py_ssize_t pyrf_cpu_map__length(PyObject *obj) | 562 | 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 | static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads) | 625 | static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads) |
598 | { | 626 | { |
599 | thread_map__put(pthreads->threads); | 627 | thread_map__put(pthreads->threads); |
600 | pthreads->ob_type->tp_free((PyObject*)pthreads); | 628 | Py_TYPE(pthreads)->tp_free((PyObject*)pthreads); |
601 | } | 629 | } |
602 | 630 | ||
603 | static Py_ssize_t pyrf_thread_map__length(PyObject *obj) | 631 | static Py_ssize_t pyrf_thread_map__length(PyObject *obj) |
@@ -759,7 +787,7 @@ static int pyrf_evsel__init(struct pyrf_evsel *pevsel, | |||
759 | static void pyrf_evsel__delete(struct pyrf_evsel *pevsel) | 787 | static void pyrf_evsel__delete(struct pyrf_evsel *pevsel) |
760 | { | 788 | { |
761 | perf_evsel__exit(&pevsel->evsel); | 789 | perf_evsel__exit(&pevsel->evsel); |
762 | pevsel->ob_type->tp_free((PyObject*)pevsel); | 790 | Py_TYPE(pevsel)->tp_free((PyObject*)pevsel); |
763 | } | 791 | } |
764 | 792 | ||
765 | static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, | 793 | static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, |
@@ -850,7 +878,7 @@ static int pyrf_evlist__init(struct pyrf_evlist *pevlist, | |||
850 | static void pyrf_evlist__delete(struct pyrf_evlist *pevlist) | 878 | static void pyrf_evlist__delete(struct pyrf_evlist *pevlist) |
851 | { | 879 | { |
852 | perf_evlist__exit(&pevlist->evlist); | 880 | perf_evlist__exit(&pevlist->evlist); |
853 | pevlist->ob_type->tp_free((PyObject*)pevlist); | 881 | Py_TYPE(pevlist)->tp_free((PyObject*)pevlist); |
854 | } | 882 | } |
855 | 883 | ||
856 | static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, | 884 | static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, |
@@ -902,12 +930,16 @@ static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist, | |||
902 | 930 | ||
903 | for (i = 0; i < evlist->pollfd.nr; ++i) { | 931 | for (i = 0; i < evlist->pollfd.nr; ++i) { |
904 | PyObject *file; | 932 | PyObject *file; |
933 | #if PY_MAJOR_VERSION < 3 | ||
905 | FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r"); | 934 | FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r"); |
906 | 935 | ||
907 | if (fp == NULL) | 936 | if (fp == NULL) |
908 | goto free_list; | 937 | goto free_list; |
909 | 938 | ||
910 | file = PyFile_FromFile(fp, "perf", "r", NULL); | 939 | file = PyFile_FromFile(fp, "perf", "r", NULL); |
940 | #else | ||
941 | file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1, NULL, NULL, NULL, 1); | ||
942 | #endif | ||
911 | if (file == NULL) | 943 | if (file == NULL) |
912 | goto free_list; | 944 | goto free_list; |
913 | 945 | ||
@@ -951,13 +983,18 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, | |||
951 | union perf_event *event; | 983 | union perf_event *event; |
952 | int sample_id_all = 1, cpu; | 984 | int sample_id_all = 1, cpu; |
953 | static char *kwlist[] = { "cpu", "sample_id_all", NULL }; | 985 | static char *kwlist[] = { "cpu", "sample_id_all", NULL }; |
986 | struct perf_mmap *md; | ||
954 | int err; | 987 | int err; |
955 | 988 | ||
956 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, | 989 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, |
957 | &cpu, &sample_id_all)) | 990 | &cpu, &sample_id_all)) |
958 | return NULL; | 991 | return NULL; |
959 | 992 | ||
960 | event = perf_evlist__mmap_read(evlist, cpu); | 993 | md = &evlist->mmap[cpu]; |
994 | if (perf_mmap__read_init(md) < 0) | ||
995 | goto end; | ||
996 | |||
997 | event = perf_mmap__read_event(md); | ||
961 | if (event != NULL) { | 998 | if (event != NULL) { |
962 | PyObject *pyevent = pyrf_event__new(event); | 999 | PyObject *pyevent = pyrf_event__new(event); |
963 | struct pyrf_event *pevent = (struct pyrf_event *)pyevent; | 1000 | struct pyrf_event *pevent = (struct pyrf_event *)pyevent; |
@@ -967,22 +1004,24 @@ static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, | |||
967 | return PyErr_NoMemory(); | 1004 | return PyErr_NoMemory(); |
968 | 1005 | ||
969 | evsel = perf_evlist__event2evsel(evlist, event); | 1006 | evsel = perf_evlist__event2evsel(evlist, event); |
970 | if (!evsel) | 1007 | if (!evsel) { |
1008 | Py_INCREF(Py_None); | ||
971 | return Py_None; | 1009 | return Py_None; |
1010 | } | ||
972 | 1011 | ||
973 | pevent->evsel = evsel; | 1012 | pevent->evsel = evsel; |
974 | 1013 | ||
975 | err = perf_evsel__parse_sample(evsel, event, &pevent->sample); | 1014 | err = perf_evsel__parse_sample(evsel, event, &pevent->sample); |
976 | 1015 | ||
977 | /* Consume the even only after we parsed it out. */ | 1016 | /* Consume the even only after we parsed it out. */ |
978 | perf_evlist__mmap_consume(evlist, cpu); | 1017 | perf_mmap__consume(md); |
979 | 1018 | ||
980 | if (err) | 1019 | if (err) |
981 | return PyErr_Format(PyExc_OSError, | 1020 | return PyErr_Format(PyExc_OSError, |
982 | "perf: can't parse sample, err=%d", err); | 1021 | "perf: can't parse sample, err=%d", err); |
983 | return pyevent; | 1022 | return pyevent; |
984 | } | 1023 | } |
985 | 1024 | end: | |
986 | Py_INCREF(Py_None); | 1025 | Py_INCREF(Py_None); |
987 | return Py_None; | 1026 | return Py_None; |
988 | } | 1027 | } |
@@ -1194,9 +1233,9 @@ static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel, | |||
1194 | 1233 | ||
1195 | tp_format = trace_event__tp_format(sys, name); | 1234 | tp_format = trace_event__tp_format(sys, name); |
1196 | if (IS_ERR(tp_format)) | 1235 | if (IS_ERR(tp_format)) |
1197 | return PyInt_FromLong(-1); | 1236 | return _PyLong_FromLong(-1); |
1198 | 1237 | ||
1199 | return PyInt_FromLong(tp_format->id); | 1238 | return _PyLong_FromLong(tp_format->id); |
1200 | } | 1239 | } |
1201 | 1240 | ||
1202 | static PyMethodDef perf__methods[] = { | 1241 | static PyMethodDef perf__methods[] = { |
@@ -1209,11 +1248,31 @@ static PyMethodDef perf__methods[] = { | |||
1209 | { .ml_name = NULL, } | 1248 | { .ml_name = NULL, } |
1210 | }; | 1249 | }; |
1211 | 1250 | ||
1251 | #if PY_MAJOR_VERSION < 3 | ||
1212 | PyMODINIT_FUNC initperf(void) | 1252 | PyMODINIT_FUNC initperf(void) |
1253 | #else | ||
1254 | PyMODINIT_FUNC PyInit_perf(void) | ||
1255 | #endif | ||
1213 | { | 1256 | { |
1214 | PyObject *obj; | 1257 | PyObject *obj; |
1215 | int i; | 1258 | int i; |
1216 | PyObject *dict, *module = Py_InitModule("perf", perf__methods); | 1259 | PyObject *dict; |
1260 | #if PY_MAJOR_VERSION < 3 | ||
1261 | PyObject *module = Py_InitModule("perf", perf__methods); | ||
1262 | #else | ||
1263 | static struct PyModuleDef moduledef = { | ||
1264 | PyModuleDef_HEAD_INIT, | ||
1265 | "perf", /* m_name */ | ||
1266 | "", /* m_doc */ | ||
1267 | -1, /* m_size */ | ||
1268 | perf__methods, /* m_methods */ | ||
1269 | NULL, /* m_reload */ | ||
1270 | NULL, /* m_traverse */ | ||
1271 | NULL, /* m_clear */ | ||
1272 | NULL, /* m_free */ | ||
1273 | }; | ||
1274 | PyObject *module = PyModule_Create(&moduledef); | ||
1275 | #endif | ||
1217 | 1276 | ||
1218 | if (module == NULL || | 1277 | if (module == NULL || |
1219 | pyrf_event__setup_types() < 0 || | 1278 | pyrf_event__setup_types() < 0 || |
@@ -1221,7 +1280,11 @@ PyMODINIT_FUNC initperf(void) | |||
1221 | pyrf_evsel__setup_types() < 0 || | 1280 | pyrf_evsel__setup_types() < 0 || |
1222 | pyrf_thread_map__setup_types() < 0 || | 1281 | pyrf_thread_map__setup_types() < 0 || |
1223 | pyrf_cpu_map__setup_types() < 0) | 1282 | pyrf_cpu_map__setup_types() < 0) |
1283 | #if PY_MAJOR_VERSION < 3 | ||
1224 | return; | 1284 | return; |
1285 | #else | ||
1286 | return module; | ||
1287 | #endif | ||
1225 | 1288 | ||
1226 | /* The page_size is placed in util object. */ | 1289 | /* The page_size is placed in util object. */ |
1227 | page_size = sysconf(_SC_PAGE_SIZE); | 1290 | page_size = sysconf(_SC_PAGE_SIZE); |
@@ -1270,7 +1333,7 @@ PyMODINIT_FUNC initperf(void) | |||
1270 | goto error; | 1333 | goto error; |
1271 | 1334 | ||
1272 | for (i = 0; perf__constants[i].name != NULL; i++) { | 1335 | for (i = 0; perf__constants[i].name != NULL; i++) { |
1273 | obj = PyInt_FromLong(perf__constants[i].value); | 1336 | obj = _PyLong_FromLong(perf__constants[i].value); |
1274 | if (obj == NULL) | 1337 | if (obj == NULL) |
1275 | goto error; | 1338 | goto error; |
1276 | PyDict_SetItemString(dict, perf__constants[i].name, obj); | 1339 | PyDict_SetItemString(dict, perf__constants[i].name, obj); |
@@ -1280,6 +1343,9 @@ PyMODINIT_FUNC initperf(void) | |||
1280 | error: | 1343 | error: |
1281 | if (PyErr_Occurred()) | 1344 | if (PyErr_Occurred()) |
1282 | PyErr_SetString(PyExc_ImportError, "perf: Init failed!"); | 1345 | PyErr_SetString(PyExc_ImportError, "perf: Init failed!"); |
1346 | #if PY_MAJOR_VERSION >= 3 | ||
1347 | return module; | ||
1348 | #endif | ||
1283 | } | 1349 | } |
1284 | 1350 | ||
1285 | /* | 1351 | /* |