Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 2 | #include <Python.h> |
| 3 | #include <structmember.h> |
| 4 | #include <inttypes.h> |
| 5 | #include <poll.h> |
Jiri Olsa | 1075fbb | 2016-07-10 13:07:58 +0200 | [diff] [blame] | 6 | #include <linux/err.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 7 | #include <perf/cpumap.h> |
Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 8 | #include <traceevent/event-parse.h> |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 9 | #include "evlist.h" |
Arnaldo Carvalho de Melo | 56e2e05 | 2017-04-19 18:38:33 -0300 | [diff] [blame] | 10 | #include "callchain.h" |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 11 | #include "evsel.h" |
| 12 | #include "event.h" |
Arnaldo Carvalho de Melo | fea0139 | 2017-04-17 16:23:22 -0300 | [diff] [blame] | 13 | #include "print_binary.h" |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 14 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 15 | #include "trace-event.h" |
Jiri Olsa | 721f0df | 2018-08-17 13:45:56 +0200 | [diff] [blame] | 16 | #include "mmap.h" |
Jiri Olsa | 20f2be1 | 2019-08-06 15:25:25 +0200 | [diff] [blame] | 17 | #include <internal/lib.h> |
Arnaldo Carvalho de Melo | 91854f9 | 2019-08-29 14:59:50 -0300 | [diff] [blame] | 18 | #include "../perf-sys.h" |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 19 | |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 20 | #if PY_MAJOR_VERSION < 3 |
| 21 | #define _PyUnicode_FromString(arg) \ |
| 22 | PyString_FromString(arg) |
| 23 | #define _PyUnicode_AsString(arg) \ |
| 24 | PyString_AsString(arg) |
| 25 | #define _PyUnicode_FromFormat(...) \ |
| 26 | PyString_FromFormat(__VA_ARGS__) |
| 27 | #define _PyLong_FromLong(arg) \ |
| 28 | PyInt_FromLong(arg) |
| 29 | |
| 30 | #else |
| 31 | |
| 32 | #define _PyUnicode_FromString(arg) \ |
| 33 | PyUnicode_FromString(arg) |
| 34 | #define _PyUnicode_FromFormat(...) \ |
| 35 | PyUnicode_FromFormat(__VA_ARGS__) |
| 36 | #define _PyLong_FromLong(arg) \ |
| 37 | PyLong_FromLong(arg) |
| 38 | #endif |
| 39 | |
| 40 | #ifndef Py_TYPE |
| 41 | #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) |
| 42 | #endif |
| 43 | |
Adrian Hunter | 8afb4c0 | 2013-08-14 15:48:23 +0300 | [diff] [blame] | 44 | /* |
Arnaldo Carvalho de Melo | 56e2e05 | 2017-04-19 18:38:33 -0300 | [diff] [blame] | 45 | * Provide these two so that we don't have to link against callchain.c and |
| 46 | * start dragging hist.c, etc. |
| 47 | */ |
| 48 | struct callchain_param callchain_param; |
| 49 | |
| 50 | int parse_callchain_record(const char *arg __maybe_unused, |
| 51 | struct callchain_param *param __maybe_unused) |
| 52 | { |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /* |
Adrian Hunter | 8afb4c0 | 2013-08-14 15:48:23 +0300 | [diff] [blame] | 57 | * Support debug printing even though util/debug.c is not linked. That means |
| 58 | * implementing 'verbose' and 'eprintf'. |
| 59 | */ |
| 60 | int verbose; |
| 61 | |
Arnaldo Carvalho de Melo | 5939cac | 2019-09-11 12:54:33 +0100 | [diff] [blame] | 62 | int eprintf(int level, int var, const char *fmt, ...); |
| 63 | |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 64 | int eprintf(int level, int var, const char *fmt, ...) |
Adrian Hunter | 8afb4c0 | 2013-08-14 15:48:23 +0300 | [diff] [blame] | 65 | { |
| 66 | va_list args; |
| 67 | int ret = 0; |
| 68 | |
Jiri Olsa | c95688a | 2014-07-14 23:46:49 +0200 | [diff] [blame] | 69 | if (var >= level) { |
Adrian Hunter | 8afb4c0 | 2013-08-14 15:48:23 +0300 | [diff] [blame] | 70 | va_start(args, fmt); |
| 71 | ret = vfprintf(stderr, fmt, args); |
| 72 | va_end(args); |
| 73 | } |
| 74 | |
| 75 | return ret; |
| 76 | } |
| 77 | |
Frederic Weisbecker | cfff2d9 | 2011-02-25 21:30:16 +0100 | [diff] [blame] | 78 | /* Define PyVarObject_HEAD_INIT for python 2.5 */ |
| 79 | #ifndef PyVarObject_HEAD_INIT |
| 80 | # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, |
| 81 | #endif |
| 82 | |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 83 | #if PY_MAJOR_VERSION < 3 |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 84 | PyMODINIT_FUNC initperf(void); |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 85 | #else |
| 86 | PyMODINIT_FUNC PyInit_perf(void); |
| 87 | #endif |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 88 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 89 | #define member_def(type, member, ptype, help) \ |
| 90 | { #member, ptype, \ |
| 91 | offsetof(struct pyrf_event, event) + offsetof(struct type, member), \ |
| 92 | 0, help } |
| 93 | |
| 94 | #define sample_member_def(name, member, ptype, help) \ |
| 95 | { #name, ptype, \ |
| 96 | offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \ |
| 97 | 0, help } |
| 98 | |
| 99 | struct pyrf_event { |
| 100 | PyObject_HEAD |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 101 | struct evsel *evsel; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 102 | struct perf_sample sample; |
| 103 | union perf_event event; |
| 104 | }; |
| 105 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 106 | #define sample_members \ |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 107 | sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \ |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 108 | sample_member_def(sample_pid, pid, T_INT, "event pid"), \ |
| 109 | sample_member_def(sample_tid, tid, T_INT, "event tid"), \ |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 110 | sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \ |
| 111 | sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \ |
| 112 | sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \ |
| 113 | sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \ |
| 114 | sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \ |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 115 | sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"), |
| 116 | |
| 117 | static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object."); |
| 118 | |
| 119 | static PyMemberDef pyrf_mmap_event__members[] = { |
| 120 | sample_members |
| 121 | member_def(perf_event_header, type, T_UINT, "event type"), |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 122 | member_def(perf_event_header, misc, T_UINT, "event misc"), |
Arnaldo Carvalho de Melo | 69d81f0 | 2019-08-26 19:02:31 -0300 | [diff] [blame] | 123 | member_def(perf_record_mmap, pid, T_UINT, "event pid"), |
| 124 | member_def(perf_record_mmap, tid, T_UINT, "event tid"), |
| 125 | member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"), |
| 126 | member_def(perf_record_mmap, len, T_ULONGLONG, "map length"), |
| 127 | member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"), |
| 128 | member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"), |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 129 | { .name = NULL, }, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent) |
| 133 | { |
| 134 | PyObject *ret; |
| 135 | char *s; |
| 136 | |
Jiri Olsa | 1345e2e | 2019-08-25 20:17:41 +0200 | [diff] [blame] | 137 | if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", " |
| 138 | "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", " |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 139 | "filename: %s }", |
| 140 | pevent->event.mmap.pid, pevent->event.mmap.tid, |
| 141 | pevent->event.mmap.start, pevent->event.mmap.len, |
| 142 | pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { |
| 143 | ret = PyErr_NoMemory(); |
| 144 | } else { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 145 | ret = _PyUnicode_FromString(s); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 146 | free(s); |
| 147 | } |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static PyTypeObject pyrf_mmap_event__type = { |
| 152 | PyVarObject_HEAD_INIT(NULL, 0) |
| 153 | .tp_name = "perf.mmap_event", |
| 154 | .tp_basicsize = sizeof(struct pyrf_event), |
| 155 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 156 | .tp_doc = pyrf_mmap_event__doc, |
| 157 | .tp_members = pyrf_mmap_event__members, |
| 158 | .tp_repr = (reprfunc)pyrf_mmap_event__repr, |
| 159 | }; |
| 160 | |
| 161 | static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object."); |
| 162 | |
| 163 | static PyMemberDef pyrf_task_event__members[] = { |
| 164 | sample_members |
| 165 | member_def(perf_event_header, type, T_UINT, "event type"), |
Arnaldo Carvalho de Melo | 69d81f0 | 2019-08-26 19:02:31 -0300 | [diff] [blame] | 166 | member_def(perf_record_fork, pid, T_UINT, "event pid"), |
| 167 | member_def(perf_record_fork, ppid, T_UINT, "event ppid"), |
| 168 | member_def(perf_record_fork, tid, T_UINT, "event tid"), |
| 169 | member_def(perf_record_fork, ptid, T_UINT, "event ptid"), |
| 170 | member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"), |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 171 | { .name = NULL, }, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent) |
| 175 | { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 176 | return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " |
Jiri Olsa | bceb59b | 2019-08-25 20:17:45 +0200 | [diff] [blame] | 177 | "ptid: %u, time: %" PRI_lu64 "}", |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 178 | pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit", |
| 179 | pevent->event.fork.pid, |
| 180 | pevent->event.fork.ppid, |
| 181 | pevent->event.fork.tid, |
| 182 | pevent->event.fork.ptid, |
| 183 | pevent->event.fork.time); |
| 184 | } |
| 185 | |
| 186 | static PyTypeObject pyrf_task_event__type = { |
| 187 | PyVarObject_HEAD_INIT(NULL, 0) |
| 188 | .tp_name = "perf.task_event", |
| 189 | .tp_basicsize = sizeof(struct pyrf_event), |
| 190 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 191 | .tp_doc = pyrf_task_event__doc, |
| 192 | .tp_members = pyrf_task_event__members, |
| 193 | .tp_repr = (reprfunc)pyrf_task_event__repr, |
| 194 | }; |
| 195 | |
| 196 | static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object."); |
| 197 | |
| 198 | static PyMemberDef pyrf_comm_event__members[] = { |
| 199 | sample_members |
| 200 | member_def(perf_event_header, type, T_UINT, "event type"), |
Arnaldo Carvalho de Melo | 69d81f0 | 2019-08-26 19:02:31 -0300 | [diff] [blame] | 201 | member_def(perf_record_comm, pid, T_UINT, "event pid"), |
| 202 | member_def(perf_record_comm, tid, T_UINT, "event tid"), |
| 203 | member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"), |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 204 | { .name = NULL, }, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent) |
| 208 | { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 209 | return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 210 | pevent->event.comm.pid, |
| 211 | pevent->event.comm.tid, |
| 212 | pevent->event.comm.comm); |
| 213 | } |
| 214 | |
| 215 | static PyTypeObject pyrf_comm_event__type = { |
| 216 | PyVarObject_HEAD_INIT(NULL, 0) |
| 217 | .tp_name = "perf.comm_event", |
| 218 | .tp_basicsize = sizeof(struct pyrf_event), |
| 219 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 220 | .tp_doc = pyrf_comm_event__doc, |
| 221 | .tp_members = pyrf_comm_event__members, |
| 222 | .tp_repr = (reprfunc)pyrf_comm_event__repr, |
| 223 | }; |
| 224 | |
| 225 | static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object."); |
| 226 | |
| 227 | static PyMemberDef pyrf_throttle_event__members[] = { |
| 228 | sample_members |
| 229 | member_def(perf_event_header, type, T_UINT, "event type"), |
Arnaldo Carvalho de Melo | 69d81f0 | 2019-08-26 19:02:31 -0300 | [diff] [blame] | 230 | member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"), |
| 231 | member_def(perf_record_throttle, id, T_ULONGLONG, "event id"), |
| 232 | member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"), |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 233 | { .name = NULL, }, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent) |
| 237 | { |
Arnaldo Carvalho de Melo | 69d81f0 | 2019-08-26 19:02:31 -0300 | [diff] [blame] | 238 | struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 239 | |
Jiri Olsa | 003c66f | 2019-08-25 20:17:49 +0200 | [diff] [blame] | 240 | return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64 |
| 241 | ", stream_id: %" PRI_lu64 " }", |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 242 | pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un", |
| 243 | te->time, te->id, te->stream_id); |
| 244 | } |
| 245 | |
| 246 | static PyTypeObject pyrf_throttle_event__type = { |
| 247 | PyVarObject_HEAD_INIT(NULL, 0) |
| 248 | .tp_name = "perf.throttle_event", |
| 249 | .tp_basicsize = sizeof(struct pyrf_event), |
| 250 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 251 | .tp_doc = pyrf_throttle_event__doc, |
| 252 | .tp_members = pyrf_throttle_event__members, |
| 253 | .tp_repr = (reprfunc)pyrf_throttle_event__repr, |
| 254 | }; |
| 255 | |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 256 | static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object."); |
| 257 | |
| 258 | static PyMemberDef pyrf_lost_event__members[] = { |
| 259 | sample_members |
Arnaldo Carvalho de Melo | 69d81f0 | 2019-08-26 19:02:31 -0300 | [diff] [blame] | 260 | member_def(perf_record_lost, id, T_ULONGLONG, "event id"), |
| 261 | member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"), |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 262 | { .name = NULL, }, |
| 263 | }; |
| 264 | |
| 265 | static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent) |
| 266 | { |
| 267 | PyObject *ret; |
| 268 | char *s; |
| 269 | |
Jiri Olsa | 5290ed6 | 2019-08-25 20:17:46 +0200 | [diff] [blame] | 270 | if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", " |
| 271 | "lost: %#" PRI_lx64 " }", |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 272 | pevent->event.lost.id, pevent->event.lost.lost) < 0) { |
| 273 | ret = PyErr_NoMemory(); |
| 274 | } else { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 275 | ret = _PyUnicode_FromString(s); |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 276 | free(s); |
| 277 | } |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static PyTypeObject pyrf_lost_event__type = { |
| 282 | PyVarObject_HEAD_INIT(NULL, 0) |
| 283 | .tp_name = "perf.lost_event", |
| 284 | .tp_basicsize = sizeof(struct pyrf_event), |
| 285 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 286 | .tp_doc = pyrf_lost_event__doc, |
| 287 | .tp_members = pyrf_lost_event__members, |
| 288 | .tp_repr = (reprfunc)pyrf_lost_event__repr, |
| 289 | }; |
| 290 | |
| 291 | static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object."); |
| 292 | |
| 293 | static PyMemberDef pyrf_read_event__members[] = { |
| 294 | sample_members |
Arnaldo Carvalho de Melo | 69d81f0 | 2019-08-26 19:02:31 -0300 | [diff] [blame] | 295 | member_def(perf_record_read, pid, T_UINT, "event pid"), |
| 296 | member_def(perf_record_read, tid, T_UINT, "event tid"), |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 297 | { .name = NULL, }, |
| 298 | }; |
| 299 | |
| 300 | static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent) |
| 301 | { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 302 | return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }", |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 303 | pevent->event.read.pid, |
| 304 | pevent->event.read.tid); |
| 305 | /* |
| 306 | * FIXME: return the array of read values, |
| 307 | * making this method useful ;-) |
| 308 | */ |
| 309 | } |
| 310 | |
| 311 | static PyTypeObject pyrf_read_event__type = { |
| 312 | PyVarObject_HEAD_INIT(NULL, 0) |
| 313 | .tp_name = "perf.read_event", |
| 314 | .tp_basicsize = sizeof(struct pyrf_event), |
| 315 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 316 | .tp_doc = pyrf_read_event__doc, |
| 317 | .tp_members = pyrf_read_event__members, |
| 318 | .tp_repr = (reprfunc)pyrf_read_event__repr, |
| 319 | }; |
| 320 | |
| 321 | static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object."); |
| 322 | |
| 323 | static PyMemberDef pyrf_sample_event__members[] = { |
| 324 | sample_members |
| 325 | member_def(perf_event_header, type, T_UINT, "event type"), |
| 326 | { .name = NULL, }, |
| 327 | }; |
| 328 | |
| 329 | static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent) |
| 330 | { |
| 331 | PyObject *ret; |
| 332 | char *s; |
| 333 | |
| 334 | if (asprintf(&s, "{ type: sample }") < 0) { |
| 335 | ret = PyErr_NoMemory(); |
| 336 | } else { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 337 | ret = _PyUnicode_FromString(s); |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 338 | free(s); |
| 339 | } |
| 340 | return ret; |
| 341 | } |
| 342 | |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 343 | static bool is_tracepoint(struct pyrf_event *pevent) |
| 344 | { |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 345 | return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT; |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 348 | static PyObject* |
Tzvetomir Stoyanov (VMware) | 2c92f98 | 2018-09-19 14:56:45 -0400 | [diff] [blame] | 349 | tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field) |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 350 | { |
Tzvetomir Stoyanov | 69769ce | 2019-04-01 12:43:18 -0400 | [diff] [blame] | 351 | struct tep_handle *pevent = field->event->tep; |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 352 | void *data = pe->sample.raw_data; |
| 353 | PyObject *ret = NULL; |
| 354 | unsigned long long val; |
| 355 | unsigned int offset, len; |
| 356 | |
Tzvetomir Stoyanov (VMware) | bb39ccb | 2018-09-19 14:56:46 -0400 | [diff] [blame] | 357 | if (field->flags & TEP_FIELD_IS_ARRAY) { |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 358 | offset = field->offset; |
| 359 | len = field->size; |
Tzvetomir Stoyanov (VMware) | bb39ccb | 2018-09-19 14:56:46 -0400 | [diff] [blame] | 360 | if (field->flags & TEP_FIELD_IS_DYNAMIC) { |
Tzvetomir Stoyanov (VMware) | 59c1bae | 2018-08-08 14:02:53 -0400 | [diff] [blame] | 361 | val = tep_read_number(pevent, data + offset, len); |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 362 | offset = val; |
| 363 | len = offset >> 16; |
| 364 | offset &= 0xffff; |
| 365 | } |
Tzvetomir Stoyanov (VMware) | bb39ccb | 2018-09-19 14:56:46 -0400 | [diff] [blame] | 366 | if (field->flags & TEP_FIELD_IS_STRING && |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 367 | is_printable_array(data + offset, len)) { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 368 | ret = _PyUnicode_FromString((char *)data + offset); |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 369 | } else { |
| 370 | ret = PyByteArray_FromStringAndSize((const char *) data + offset, len); |
Tzvetomir Stoyanov (VMware) | bb39ccb | 2018-09-19 14:56:46 -0400 | [diff] [blame] | 371 | field->flags &= ~TEP_FIELD_IS_STRING; |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 372 | } |
| 373 | } else { |
Tzvetomir Stoyanov (VMware) | 59c1bae | 2018-08-08 14:02:53 -0400 | [diff] [blame] | 374 | val = tep_read_number(pevent, data + field->offset, |
| 375 | field->size); |
Tzvetomir Stoyanov (VMware) | bb39ccb | 2018-09-19 14:56:46 -0400 | [diff] [blame] | 376 | if (field->flags & TEP_FIELD_IS_POINTER) |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 377 | ret = PyLong_FromUnsignedLong((unsigned long) val); |
Tzvetomir Stoyanov (VMware) | bb39ccb | 2018-09-19 14:56:46 -0400 | [diff] [blame] | 378 | else if (field->flags & TEP_FIELD_IS_SIGNED) |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 379 | ret = PyLong_FromLong((long) val); |
| 380 | else |
| 381 | ret = PyLong_FromUnsignedLong((unsigned long) val); |
| 382 | } |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | static PyObject* |
| 388 | get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) |
| 389 | { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 390 | const char *str = _PyUnicode_AsString(PyObject_Str(attr_name)); |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 391 | struct evsel *evsel = pevent->evsel; |
Tzvetomir Stoyanov (VMware) | 2c92f98 | 2018-09-19 14:56:45 -0400 | [diff] [blame] | 392 | struct tep_format_field *field; |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 393 | |
| 394 | if (!evsel->tp_format) { |
Tzvetomir Stoyanov | 97fbf3f | 2018-11-30 10:44:07 -0500 | [diff] [blame] | 395 | struct tep_event *tp_format; |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 396 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 397 | tp_format = trace_event__tp_format_id(evsel->core.attr.config); |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 398 | if (!tp_format) |
| 399 | return NULL; |
| 400 | |
| 401 | evsel->tp_format = tp_format; |
| 402 | } |
| 403 | |
Tzvetomir Stoyanov (VMware) | af85cd1 | 2018-08-08 14:02:50 -0400 | [diff] [blame] | 404 | field = tep_find_any_field(evsel->tp_format, str); |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 405 | if (!field) |
| 406 | return NULL; |
| 407 | |
| 408 | return tracepoint_field(pevent, field); |
| 409 | } |
| 410 | |
| 411 | static PyObject* |
| 412 | pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name) |
| 413 | { |
| 414 | PyObject *obj = NULL; |
| 415 | |
| 416 | if (is_tracepoint(pevent)) |
| 417 | obj = get_tracepoint_field(pevent, attr_name); |
| 418 | |
| 419 | return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name); |
| 420 | } |
| 421 | |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 422 | static PyTypeObject pyrf_sample_event__type = { |
| 423 | PyVarObject_HEAD_INIT(NULL, 0) |
| 424 | .tp_name = "perf.sample_event", |
| 425 | .tp_basicsize = sizeof(struct pyrf_event), |
| 426 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 427 | .tp_doc = pyrf_sample_event__doc, |
| 428 | .tp_members = pyrf_sample_event__members, |
| 429 | .tp_repr = (reprfunc)pyrf_sample_event__repr, |
Jiri Olsa | bae57e3 | 2016-07-10 13:08:00 +0200 | [diff] [blame] | 430 | .tp_getattro = (getattrofunc) pyrf_sample_event__getattro, |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 431 | }; |
| 432 | |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 433 | static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object."); |
| 434 | |
| 435 | static PyMemberDef pyrf_context_switch_event__members[] = { |
| 436 | sample_members |
| 437 | member_def(perf_event_header, type, T_UINT, "event type"), |
Jiri Olsa | 7293237 | 2019-08-28 15:57:16 +0200 | [diff] [blame] | 438 | member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"), |
| 439 | member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"), |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 440 | { .name = NULL, }, |
| 441 | }; |
| 442 | |
| 443 | static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent) |
| 444 | { |
| 445 | PyObject *ret; |
| 446 | char *s; |
| 447 | |
| 448 | if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }", |
| 449 | pevent->event.context_switch.next_prev_pid, |
| 450 | pevent->event.context_switch.next_prev_tid, |
| 451 | !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) { |
| 452 | ret = PyErr_NoMemory(); |
| 453 | } else { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 454 | ret = _PyUnicode_FromString(s); |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 455 | free(s); |
| 456 | } |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | static PyTypeObject pyrf_context_switch_event__type = { |
| 461 | PyVarObject_HEAD_INIT(NULL, 0) |
| 462 | .tp_name = "perf.context_switch_event", |
| 463 | .tp_basicsize = sizeof(struct pyrf_event), |
| 464 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 465 | .tp_doc = pyrf_context_switch_event__doc, |
| 466 | .tp_members = pyrf_context_switch_event__members, |
| 467 | .tp_repr = (reprfunc)pyrf_context_switch_event__repr, |
| 468 | }; |
| 469 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 470 | static int pyrf_event__setup_types(void) |
| 471 | { |
| 472 | int err; |
| 473 | pyrf_mmap_event__type.tp_new = |
| 474 | pyrf_task_event__type.tp_new = |
| 475 | pyrf_comm_event__type.tp_new = |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 476 | pyrf_lost_event__type.tp_new = |
| 477 | pyrf_read_event__type.tp_new = |
| 478 | pyrf_sample_event__type.tp_new = |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 479 | pyrf_context_switch_event__type.tp_new = |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 480 | pyrf_throttle_event__type.tp_new = PyType_GenericNew; |
| 481 | err = PyType_Ready(&pyrf_mmap_event__type); |
| 482 | if (err < 0) |
| 483 | goto out; |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 484 | err = PyType_Ready(&pyrf_lost_event__type); |
| 485 | if (err < 0) |
| 486 | goto out; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 487 | err = PyType_Ready(&pyrf_task_event__type); |
| 488 | if (err < 0) |
| 489 | goto out; |
| 490 | err = PyType_Ready(&pyrf_comm_event__type); |
| 491 | if (err < 0) |
| 492 | goto out; |
| 493 | err = PyType_Ready(&pyrf_throttle_event__type); |
| 494 | if (err < 0) |
| 495 | goto out; |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 496 | err = PyType_Ready(&pyrf_read_event__type); |
| 497 | if (err < 0) |
| 498 | goto out; |
| 499 | err = PyType_Ready(&pyrf_sample_event__type); |
| 500 | if (err < 0) |
| 501 | goto out; |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 502 | err = PyType_Ready(&pyrf_context_switch_event__type); |
| 503 | if (err < 0) |
| 504 | goto out; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 505 | out: |
| 506 | return err; |
| 507 | } |
| 508 | |
| 509 | static PyTypeObject *pyrf_event__type[] = { |
| 510 | [PERF_RECORD_MMAP] = &pyrf_mmap_event__type, |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 511 | [PERF_RECORD_LOST] = &pyrf_lost_event__type, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 512 | [PERF_RECORD_COMM] = &pyrf_comm_event__type, |
| 513 | [PERF_RECORD_EXIT] = &pyrf_task_event__type, |
| 514 | [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type, |
| 515 | [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type, |
| 516 | [PERF_RECORD_FORK] = &pyrf_task_event__type, |
Arnaldo Carvalho de Melo | 3e9f45a7 | 2011-07-25 17:13:27 -0300 | [diff] [blame] | 517 | [PERF_RECORD_READ] = &pyrf_read_event__type, |
| 518 | [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type, |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 519 | [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type, |
| 520 | [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 521 | }; |
| 522 | |
| 523 | static PyObject *pyrf_event__new(union perf_event *event) |
| 524 | { |
| 525 | struct pyrf_event *pevent; |
| 526 | PyTypeObject *ptype; |
| 527 | |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 528 | if ((event->header.type < PERF_RECORD_MMAP || |
| 529 | event->header.type > PERF_RECORD_SAMPLE) && |
| 530 | !(event->header.type == PERF_RECORD_SWITCH || |
| 531 | event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 532 | return NULL; |
| 533 | |
| 534 | ptype = pyrf_event__type[event->header.type]; |
| 535 | pevent = PyObject_New(struct pyrf_event, ptype); |
| 536 | if (pevent != NULL) |
| 537 | memcpy(&pevent->event, event, event->header.size); |
| 538 | return (PyObject *)pevent; |
| 539 | } |
| 540 | |
| 541 | struct pyrf_cpu_map { |
| 542 | PyObject_HEAD |
| 543 | |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 544 | struct perf_cpu_map *cpus; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 545 | }; |
| 546 | |
| 547 | static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus, |
| 548 | PyObject *args, PyObject *kwargs) |
| 549 | { |
Frederic Weisbecker | 6434815 | 2011-03-31 18:27:43 +0200 | [diff] [blame] | 550 | static char *kwlist[] = { "cpustr", NULL }; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 551 | char *cpustr = NULL; |
| 552 | |
| 553 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", |
| 554 | kwlist, &cpustr)) |
| 555 | return -1; |
| 556 | |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 557 | pcpus->cpus = perf_cpu_map__new(cpustr); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 558 | if (pcpus->cpus == NULL) |
| 559 | return -1; |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus) |
| 564 | { |
Jiri Olsa | 38f01d8 | 2019-07-21 13:24:17 +0200 | [diff] [blame] | 565 | perf_cpu_map__put(pcpus->cpus); |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 566 | Py_TYPE(pcpus)->tp_free((PyObject*)pcpus); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static Py_ssize_t pyrf_cpu_map__length(PyObject *obj) |
| 570 | { |
| 571 | struct pyrf_cpu_map *pcpus = (void *)obj; |
| 572 | |
| 573 | return pcpus->cpus->nr; |
| 574 | } |
| 575 | |
| 576 | static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i) |
| 577 | { |
| 578 | struct pyrf_cpu_map *pcpus = (void *)obj; |
| 579 | |
| 580 | if (i >= pcpus->cpus->nr) |
| 581 | return NULL; |
| 582 | |
| 583 | return Py_BuildValue("i", pcpus->cpus->map[i]); |
| 584 | } |
| 585 | |
| 586 | static PySequenceMethods pyrf_cpu_map__sequence_methods = { |
| 587 | .sq_length = pyrf_cpu_map__length, |
| 588 | .sq_item = pyrf_cpu_map__item, |
| 589 | }; |
| 590 | |
| 591 | static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object."); |
| 592 | |
| 593 | static PyTypeObject pyrf_cpu_map__type = { |
| 594 | PyVarObject_HEAD_INIT(NULL, 0) |
| 595 | .tp_name = "perf.cpu_map", |
| 596 | .tp_basicsize = sizeof(struct pyrf_cpu_map), |
| 597 | .tp_dealloc = (destructor)pyrf_cpu_map__delete, |
| 598 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 599 | .tp_doc = pyrf_cpu_map__doc, |
| 600 | .tp_as_sequence = &pyrf_cpu_map__sequence_methods, |
| 601 | .tp_init = (initproc)pyrf_cpu_map__init, |
| 602 | }; |
| 603 | |
| 604 | static int pyrf_cpu_map__setup_types(void) |
| 605 | { |
| 606 | pyrf_cpu_map__type.tp_new = PyType_GenericNew; |
| 607 | return PyType_Ready(&pyrf_cpu_map__type); |
| 608 | } |
| 609 | |
| 610 | struct pyrf_thread_map { |
| 611 | PyObject_HEAD |
| 612 | |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 613 | struct perf_thread_map *threads; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 614 | }; |
| 615 | |
| 616 | static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads, |
| 617 | PyObject *args, PyObject *kwargs) |
| 618 | { |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 619 | static char *kwlist[] = { "pid", "tid", "uid", NULL }; |
| 620 | int pid = -1, tid = -1, uid = UINT_MAX; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 621 | |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 622 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii", |
| 623 | kwlist, &pid, &tid, &uid)) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 624 | return -1; |
| 625 | |
Arnaldo Carvalho de Melo | 0d37aa3 | 2012-01-19 14:08:15 -0200 | [diff] [blame] | 626 | pthreads->threads = thread_map__new(pid, tid, uid); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 627 | if (pthreads->threads == NULL) |
| 628 | return -1; |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads) |
| 633 | { |
Jiri Olsa | 7836e52 | 2019-07-21 13:24:20 +0200 | [diff] [blame] | 634 | perf_thread_map__put(pthreads->threads); |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 635 | Py_TYPE(pthreads)->tp_free((PyObject*)pthreads); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static Py_ssize_t pyrf_thread_map__length(PyObject *obj) |
| 639 | { |
| 640 | struct pyrf_thread_map *pthreads = (void *)obj; |
| 641 | |
| 642 | return pthreads->threads->nr; |
| 643 | } |
| 644 | |
| 645 | static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i) |
| 646 | { |
| 647 | struct pyrf_thread_map *pthreads = (void *)obj; |
| 648 | |
| 649 | if (i >= pthreads->threads->nr) |
| 650 | return NULL; |
| 651 | |
| 652 | return Py_BuildValue("i", pthreads->threads->map[i]); |
| 653 | } |
| 654 | |
| 655 | static PySequenceMethods pyrf_thread_map__sequence_methods = { |
| 656 | .sq_length = pyrf_thread_map__length, |
| 657 | .sq_item = pyrf_thread_map__item, |
| 658 | }; |
| 659 | |
| 660 | static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object."); |
| 661 | |
| 662 | static PyTypeObject pyrf_thread_map__type = { |
| 663 | PyVarObject_HEAD_INIT(NULL, 0) |
| 664 | .tp_name = "perf.thread_map", |
| 665 | .tp_basicsize = sizeof(struct pyrf_thread_map), |
| 666 | .tp_dealloc = (destructor)pyrf_thread_map__delete, |
| 667 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 668 | .tp_doc = pyrf_thread_map__doc, |
| 669 | .tp_as_sequence = &pyrf_thread_map__sequence_methods, |
| 670 | .tp_init = (initproc)pyrf_thread_map__init, |
| 671 | }; |
| 672 | |
| 673 | static int pyrf_thread_map__setup_types(void) |
| 674 | { |
| 675 | pyrf_thread_map__type.tp_new = PyType_GenericNew; |
| 676 | return PyType_Ready(&pyrf_thread_map__type); |
| 677 | } |
| 678 | |
| 679 | struct pyrf_evsel { |
| 680 | PyObject_HEAD |
| 681 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 682 | struct evsel evsel; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 683 | }; |
| 684 | |
| 685 | static int pyrf_evsel__init(struct pyrf_evsel *pevsel, |
| 686 | PyObject *args, PyObject *kwargs) |
| 687 | { |
| 688 | struct perf_event_attr attr = { |
| 689 | .type = PERF_TYPE_HARDWARE, |
| 690 | .config = PERF_COUNT_HW_CPU_CYCLES, |
| 691 | .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID, |
| 692 | }; |
| 693 | static char *kwlist[] = { |
| 694 | "type", |
| 695 | "config", |
| 696 | "sample_freq", |
| 697 | "sample_period", |
| 698 | "sample_type", |
| 699 | "read_format", |
| 700 | "disabled", |
| 701 | "inherit", |
| 702 | "pinned", |
| 703 | "exclusive", |
| 704 | "exclude_user", |
| 705 | "exclude_kernel", |
| 706 | "exclude_hv", |
| 707 | "exclude_idle", |
| 708 | "mmap", |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 709 | "context_switch", |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 710 | "comm", |
| 711 | "freq", |
| 712 | "inherit_stat", |
| 713 | "enable_on_exec", |
| 714 | "task", |
| 715 | "watermark", |
| 716 | "precise_ip", |
| 717 | "mmap_data", |
| 718 | "sample_id_all", |
| 719 | "wakeup_events", |
| 720 | "bp_type", |
| 721 | "bp_addr", |
Frederic Weisbecker | 6434815 | 2011-03-31 18:27:43 +0200 | [diff] [blame] | 722 | "bp_len", |
| 723 | NULL |
| 724 | }; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 725 | u64 sample_period = 0; |
| 726 | u32 disabled = 0, |
| 727 | inherit = 0, |
| 728 | pinned = 0, |
| 729 | exclusive = 0, |
| 730 | exclude_user = 0, |
| 731 | exclude_kernel = 0, |
| 732 | exclude_hv = 0, |
| 733 | exclude_idle = 0, |
| 734 | mmap = 0, |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 735 | context_switch = 0, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 736 | comm = 0, |
| 737 | freq = 1, |
| 738 | inherit_stat = 0, |
| 739 | enable_on_exec = 0, |
| 740 | task = 0, |
| 741 | watermark = 0, |
| 742 | precise_ip = 0, |
| 743 | mmap_data = 0, |
| 744 | sample_id_all = 1; |
| 745 | int idx = 0; |
| 746 | |
| 747 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 748 | "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 749 | &attr.type, &attr.config, &attr.sample_freq, |
| 750 | &sample_period, &attr.sample_type, |
| 751 | &attr.read_format, &disabled, &inherit, |
| 752 | &pinned, &exclusive, &exclude_user, |
| 753 | &exclude_kernel, &exclude_hv, &exclude_idle, |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 754 | &mmap, &context_switch, &comm, &freq, &inherit_stat, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 755 | &enable_on_exec, &task, &watermark, |
| 756 | &precise_ip, &mmap_data, &sample_id_all, |
| 757 | &attr.wakeup_events, &attr.bp_type, |
| 758 | &attr.bp_addr, &attr.bp_len, &idx)) |
| 759 | return -1; |
| 760 | |
| 761 | /* union... */ |
| 762 | if (sample_period != 0) { |
| 763 | if (attr.sample_freq != 0) |
| 764 | return -1; /* FIXME: throw right exception */ |
| 765 | attr.sample_period = sample_period; |
| 766 | } |
| 767 | |
| 768 | /* Bitfields */ |
| 769 | attr.disabled = disabled; |
| 770 | attr.inherit = inherit; |
| 771 | attr.pinned = pinned; |
| 772 | attr.exclusive = exclusive; |
| 773 | attr.exclude_user = exclude_user; |
| 774 | attr.exclude_kernel = exclude_kernel; |
| 775 | attr.exclude_hv = exclude_hv; |
| 776 | attr.exclude_idle = exclude_idle; |
| 777 | attr.mmap = mmap; |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 778 | attr.context_switch = context_switch; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 779 | attr.comm = comm; |
| 780 | attr.freq = freq; |
| 781 | attr.inherit_stat = inherit_stat; |
| 782 | attr.enable_on_exec = enable_on_exec; |
| 783 | attr.task = task; |
| 784 | attr.watermark = watermark; |
| 785 | attr.precise_ip = precise_ip; |
| 786 | attr.mmap_data = mmap_data; |
| 787 | attr.sample_id_all = sample_id_all; |
Jiri Olsa | ad4e3c04 | 2016-07-10 13:07:55 +0200 | [diff] [blame] | 788 | attr.size = sizeof(attr); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 789 | |
Jiri Olsa | b4b62ee | 2019-07-21 13:23:53 +0200 | [diff] [blame] | 790 | evsel__init(&pevsel->evsel, &attr, idx); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static void pyrf_evsel__delete(struct pyrf_evsel *pevsel) |
| 795 | { |
| 796 | perf_evsel__exit(&pevsel->evsel); |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 797 | Py_TYPE(pevsel)->tp_free((PyObject*)pevsel); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, |
| 801 | PyObject *args, PyObject *kwargs) |
| 802 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 803 | struct evsel *evsel = &pevsel->evsel; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 804 | struct perf_cpu_map *cpus = NULL; |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 805 | struct perf_thread_map *threads = NULL; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 806 | PyObject *pcpus = NULL, *pthreads = NULL; |
Arnaldo Carvalho de Melo | 5d2cd90 | 2011-04-14 11:20:14 -0300 | [diff] [blame] | 807 | int group = 0, inherit = 0; |
Frederic Weisbecker | 6434815 | 2011-03-31 18:27:43 +0200 | [diff] [blame] | 808 | static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL }; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 809 | |
| 810 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, |
Arnaldo Carvalho de Melo | 5d2cd90 | 2011-04-14 11:20:14 -0300 | [diff] [blame] | 811 | &pcpus, &pthreads, &group, &inherit)) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 812 | return NULL; |
| 813 | |
| 814 | if (pthreads != NULL) |
| 815 | threads = ((struct pyrf_thread_map *)pthreads)->threads; |
| 816 | |
| 817 | if (pcpus != NULL) |
| 818 | cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; |
| 819 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 820 | evsel->core.attr.inherit = inherit; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 821 | /* |
| 822 | * This will group just the fds for this single evsel, to group |
| 823 | * multiple events, use evlist.open(). |
| 824 | */ |
Jiri Olsa | 5972d1e | 2019-07-21 13:24:01 +0200 | [diff] [blame] | 825 | if (evsel__open(evsel, cpus, threads) < 0) { |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 826 | PyErr_SetFromErrno(PyExc_OSError); |
| 827 | return NULL; |
| 828 | } |
| 829 | |
| 830 | Py_INCREF(Py_None); |
| 831 | return Py_None; |
| 832 | } |
| 833 | |
| 834 | static PyMethodDef pyrf_evsel__methods[] = { |
| 835 | { |
| 836 | .ml_name = "open", |
| 837 | .ml_meth = (PyCFunction)pyrf_evsel__open, |
| 838 | .ml_flags = METH_VARARGS | METH_KEYWORDS, |
| 839 | .ml_doc = PyDoc_STR("open the event selector file descriptor table.") |
| 840 | }, |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 841 | { .ml_name = NULL, } |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 842 | }; |
| 843 | |
| 844 | static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object."); |
| 845 | |
| 846 | static PyTypeObject pyrf_evsel__type = { |
| 847 | PyVarObject_HEAD_INIT(NULL, 0) |
| 848 | .tp_name = "perf.evsel", |
| 849 | .tp_basicsize = sizeof(struct pyrf_evsel), |
| 850 | .tp_dealloc = (destructor)pyrf_evsel__delete, |
| 851 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 852 | .tp_doc = pyrf_evsel__doc, |
| 853 | .tp_methods = pyrf_evsel__methods, |
| 854 | .tp_init = (initproc)pyrf_evsel__init, |
| 855 | }; |
| 856 | |
| 857 | static int pyrf_evsel__setup_types(void) |
| 858 | { |
| 859 | pyrf_evsel__type.tp_new = PyType_GenericNew; |
| 860 | return PyType_Ready(&pyrf_evsel__type); |
| 861 | } |
| 862 | |
| 863 | struct pyrf_evlist { |
| 864 | PyObject_HEAD |
| 865 | |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 866 | struct evlist evlist; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 867 | }; |
| 868 | |
| 869 | static int pyrf_evlist__init(struct pyrf_evlist *pevlist, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 870 | PyObject *args, PyObject *kwargs __maybe_unused) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 871 | { |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 872 | PyObject *pcpus = NULL, *pthreads = NULL; |
Jiri Olsa | f854839 | 2019-07-21 13:23:49 +0200 | [diff] [blame] | 873 | struct perf_cpu_map *cpus; |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 874 | struct perf_thread_map *threads; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 875 | |
| 876 | if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads)) |
| 877 | return -1; |
| 878 | |
| 879 | threads = ((struct pyrf_thread_map *)pthreads)->threads; |
| 880 | cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; |
Jiri Olsa | 52c86bc | 2019-07-21 13:23:54 +0200 | [diff] [blame] | 881 | evlist__init(&pevlist->evlist, cpus, threads); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | static void pyrf_evlist__delete(struct pyrf_evlist *pevlist) |
| 886 | { |
Jiri Olsa | 470579b | 2019-09-02 14:34:52 +0200 | [diff] [blame] | 887 | evlist__exit(&pevlist->evlist); |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 888 | Py_TYPE(pevlist)->tp_free((PyObject*)pevlist); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, |
| 892 | PyObject *args, PyObject *kwargs) |
| 893 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 894 | struct evlist *evlist = &pevlist->evlist; |
Frederic Weisbecker | 6434815 | 2011-03-31 18:27:43 +0200 | [diff] [blame] | 895 | static char *kwlist[] = { "pages", "overwrite", NULL }; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 896 | int pages = 128, overwrite = false; |
| 897 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 898 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist, |
| 899 | &pages, &overwrite)) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 900 | return NULL; |
| 901 | |
Jiri Olsa | 9521b5f | 2019-07-28 12:45:35 +0200 | [diff] [blame] | 902 | if (evlist__mmap(evlist, pages) < 0) { |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 903 | PyErr_SetFromErrno(PyExc_OSError); |
| 904 | return NULL; |
| 905 | } |
| 906 | |
| 907 | Py_INCREF(Py_None); |
| 908 | return Py_None; |
| 909 | } |
| 910 | |
| 911 | static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist, |
| 912 | PyObject *args, PyObject *kwargs) |
| 913 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 914 | struct evlist *evlist = &pevlist->evlist; |
Frederic Weisbecker | 6434815 | 2011-03-31 18:27:43 +0200 | [diff] [blame] | 915 | static char *kwlist[] = { "timeout", NULL }; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 916 | int timeout = -1, n; |
| 917 | |
| 918 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout)) |
| 919 | return NULL; |
| 920 | |
Jiri Olsa | 80ab298 | 2019-08-31 22:48:33 +0200 | [diff] [blame^] | 921 | n = evlist__poll(evlist, timeout); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 922 | if (n < 0) { |
| 923 | PyErr_SetFromErrno(PyExc_OSError); |
| 924 | return NULL; |
| 925 | } |
| 926 | |
| 927 | return Py_BuildValue("i", n); |
| 928 | } |
| 929 | |
| 930 | static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 931 | PyObject *args __maybe_unused, |
| 932 | PyObject *kwargs __maybe_unused) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 933 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 934 | struct evlist *evlist = &pevlist->evlist; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 935 | PyObject *list = PyList_New(0); |
| 936 | int i; |
| 937 | |
Jiri Olsa | 40cb2d5 | 2019-08-06 11:28:02 +0200 | [diff] [blame] | 938 | for (i = 0; i < evlist->core.pollfd.nr; ++i) { |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 939 | PyObject *file; |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 940 | #if PY_MAJOR_VERSION < 3 |
Jiri Olsa | 40cb2d5 | 2019-08-06 11:28:02 +0200 | [diff] [blame] | 941 | FILE *fp = fdopen(evlist->core.pollfd.entries[i].fd, "r"); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 942 | |
| 943 | if (fp == NULL) |
| 944 | goto free_list; |
| 945 | |
| 946 | file = PyFile_FromFile(fp, "perf", "r", NULL); |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 947 | #else |
Jiri Olsa | 40cb2d5 | 2019-08-06 11:28:02 +0200 | [diff] [blame] | 948 | file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1, |
Jiri Olsa | a389aec | 2018-12-26 12:21:21 +0100 | [diff] [blame] | 949 | NULL, NULL, NULL, 0); |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 950 | #endif |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 951 | if (file == NULL) |
| 952 | goto free_list; |
| 953 | |
| 954 | if (PyList_Append(list, file) != 0) { |
| 955 | Py_DECREF(file); |
| 956 | goto free_list; |
| 957 | } |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 958 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 959 | Py_DECREF(file); |
| 960 | } |
| 961 | |
| 962 | return list; |
| 963 | free_list: |
| 964 | return PyErr_NoMemory(); |
| 965 | } |
| 966 | |
| 967 | |
| 968 | static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 969 | PyObject *args, |
| 970 | PyObject *kwargs __maybe_unused) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 971 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 972 | struct evlist *evlist = &pevlist->evlist; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 973 | PyObject *pevsel; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 974 | struct evsel *evsel; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 975 | |
| 976 | if (!PyArg_ParseTuple(args, "O", &pevsel)) |
| 977 | return NULL; |
| 978 | |
| 979 | Py_INCREF(pevsel); |
| 980 | evsel = &((struct pyrf_evsel *)pevsel)->evsel; |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 981 | evsel->idx = evlist->core.nr_entries; |
Jiri Olsa | a1cf3a7 | 2019-07-21 13:23:59 +0200 | [diff] [blame] | 982 | evlist__add(evlist, evsel); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 983 | |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 984 | return Py_BuildValue("i", evlist->core.nr_entries); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 985 | } |
| 986 | |
Jiri Olsa | a583053 | 2019-07-27 20:30:53 +0200 | [diff] [blame] | 987 | static struct mmap *get_md(struct evlist *evlist, int cpu) |
Jiri Olsa | 721f0df | 2018-08-17 13:45:56 +0200 | [diff] [blame] | 988 | { |
| 989 | int i; |
| 990 | |
Jiri Olsa | c976ee1 | 2019-07-30 13:04:59 +0200 | [diff] [blame] | 991 | for (i = 0; i < evlist->core.nr_mmaps; i++) { |
Jiri Olsa | a583053 | 2019-07-27 20:30:53 +0200 | [diff] [blame] | 992 | struct mmap *md = &evlist->mmap[i]; |
Jiri Olsa | 721f0df | 2018-08-17 13:45:56 +0200 | [diff] [blame] | 993 | |
Jiri Olsa | 56a9470 | 2019-07-27 22:33:20 +0200 | [diff] [blame] | 994 | if (md->core.cpu == cpu) |
Jiri Olsa | 721f0df | 2018-08-17 13:45:56 +0200 | [diff] [blame] | 995 | return md; |
| 996 | } |
| 997 | |
| 998 | return NULL; |
| 999 | } |
| 1000 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1001 | static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, |
| 1002 | PyObject *args, PyObject *kwargs) |
| 1003 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 1004 | struct evlist *evlist = &pevlist->evlist; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1005 | union perf_event *event; |
| 1006 | int sample_id_all = 1, cpu; |
Frederic Weisbecker | 6434815 | 2011-03-31 18:27:43 +0200 | [diff] [blame] | 1007 | static char *kwlist[] = { "cpu", "sample_id_all", NULL }; |
Jiri Olsa | a583053 | 2019-07-27 20:30:53 +0200 | [diff] [blame] | 1008 | struct mmap *md; |
Frederic Weisbecker | 5538bec | 2011-05-22 02:17:22 +0200 | [diff] [blame] | 1009 | int err; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1010 | |
| 1011 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, |
| 1012 | &cpu, &sample_id_all)) |
| 1013 | return NULL; |
| 1014 | |
Jiri Olsa | 721f0df | 2018-08-17 13:45:56 +0200 | [diff] [blame] | 1015 | md = get_md(evlist, cpu); |
| 1016 | if (!md) |
| 1017 | return NULL; |
| 1018 | |
Kan Liang | b9bae2c | 2018-03-06 10:36:07 -0500 | [diff] [blame] | 1019 | if (perf_mmap__read_init(md) < 0) |
Kan Liang | 35b7cdc | 2018-03-01 18:09:00 -0500 | [diff] [blame] | 1020 | goto end; |
| 1021 | |
Kan Liang | 0019dc87 | 2018-03-06 10:36:06 -0500 | [diff] [blame] | 1022 | event = perf_mmap__read_event(md); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1023 | if (event != NULL) { |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1024 | PyObject *pyevent = pyrf_event__new(event); |
| 1025 | struct pyrf_event *pevent = (struct pyrf_event *)pyevent; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1026 | struct evsel *evsel; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1027 | |
| 1028 | if (pyevent == NULL) |
| 1029 | return PyErr_NoMemory(); |
| 1030 | |
Jiri Olsa | 377f698 | 2016-07-10 13:07:59 +0200 | [diff] [blame] | 1031 | evsel = perf_evlist__event2evsel(evlist, event); |
Petr Machata | 83428f2 | 2018-03-22 00:57:32 +0100 | [diff] [blame] | 1032 | if (!evsel) { |
| 1033 | Py_INCREF(Py_None); |
Jiri Olsa | 377f698 | 2016-07-10 13:07:59 +0200 | [diff] [blame] | 1034 | return Py_None; |
Petr Machata | 83428f2 | 2018-03-22 00:57:32 +0100 | [diff] [blame] | 1035 | } |
Jiri Olsa | 377f698 | 2016-07-10 13:07:59 +0200 | [diff] [blame] | 1036 | |
| 1037 | pevent->evsel = evsel; |
| 1038 | |
| 1039 | err = perf_evsel__parse_sample(evsel, event, &pevent->sample); |
Jiri Olsa | e8968e6 | 2016-07-10 13:07:56 +0200 | [diff] [blame] | 1040 | |
| 1041 | /* Consume the even only after we parsed it out. */ |
Kan Liang | d6ace3d | 2018-03-06 10:36:05 -0500 | [diff] [blame] | 1042 | perf_mmap__consume(md); |
Jiri Olsa | e8968e6 | 2016-07-10 13:07:56 +0200 | [diff] [blame] | 1043 | |
Arnaldo Carvalho de Melo | 5c6970a | 2011-06-02 10:55:10 -0300 | [diff] [blame] | 1044 | if (err) |
| 1045 | return PyErr_Format(PyExc_OSError, |
| 1046 | "perf: can't parse sample, err=%d", err); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1047 | return pyevent; |
| 1048 | } |
Kan Liang | 35b7cdc | 2018-03-01 18:09:00 -0500 | [diff] [blame] | 1049 | end: |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1050 | Py_INCREF(Py_None); |
| 1051 | return Py_None; |
| 1052 | } |
| 1053 | |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1054 | static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist, |
| 1055 | PyObject *args, PyObject *kwargs) |
| 1056 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 1057 | struct evlist *evlist = &pevlist->evlist; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1058 | int group = 0; |
| 1059 | static char *kwlist[] = { "group", NULL }; |
| 1060 | |
| 1061 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group)) |
| 1062 | return NULL; |
| 1063 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1064 | if (group) |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 1065 | perf_evlist__set_leader(evlist); |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1066 | |
Jiri Olsa | 474ddc4 | 2019-07-21 13:24:06 +0200 | [diff] [blame] | 1067 | if (evlist__open(evlist) < 0) { |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1068 | PyErr_SetFromErrno(PyExc_OSError); |
| 1069 | return NULL; |
| 1070 | } |
| 1071 | |
| 1072 | Py_INCREF(Py_None); |
| 1073 | return Py_None; |
| 1074 | } |
| 1075 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1076 | static PyMethodDef pyrf_evlist__methods[] = { |
| 1077 | { |
| 1078 | .ml_name = "mmap", |
| 1079 | .ml_meth = (PyCFunction)pyrf_evlist__mmap, |
| 1080 | .ml_flags = METH_VARARGS | METH_KEYWORDS, |
| 1081 | .ml_doc = PyDoc_STR("mmap the file descriptor table.") |
| 1082 | }, |
| 1083 | { |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 1084 | .ml_name = "open", |
| 1085 | .ml_meth = (PyCFunction)pyrf_evlist__open, |
| 1086 | .ml_flags = METH_VARARGS | METH_KEYWORDS, |
| 1087 | .ml_doc = PyDoc_STR("open the file descriptors.") |
| 1088 | }, |
| 1089 | { |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1090 | .ml_name = "poll", |
| 1091 | .ml_meth = (PyCFunction)pyrf_evlist__poll, |
| 1092 | .ml_flags = METH_VARARGS | METH_KEYWORDS, |
| 1093 | .ml_doc = PyDoc_STR("poll the file descriptor table.") |
| 1094 | }, |
| 1095 | { |
| 1096 | .ml_name = "get_pollfd", |
| 1097 | .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd, |
| 1098 | .ml_flags = METH_VARARGS | METH_KEYWORDS, |
| 1099 | .ml_doc = PyDoc_STR("get the poll file descriptor table.") |
| 1100 | }, |
| 1101 | { |
| 1102 | .ml_name = "add", |
| 1103 | .ml_meth = (PyCFunction)pyrf_evlist__add, |
| 1104 | .ml_flags = METH_VARARGS | METH_KEYWORDS, |
| 1105 | .ml_doc = PyDoc_STR("adds an event selector to the list.") |
| 1106 | }, |
| 1107 | { |
| 1108 | .ml_name = "read_on_cpu", |
| 1109 | .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu, |
| 1110 | .ml_flags = METH_VARARGS | METH_KEYWORDS, |
| 1111 | .ml_doc = PyDoc_STR("reads an event.") |
| 1112 | }, |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 1113 | { .ml_name = NULL, } |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1114 | }; |
| 1115 | |
| 1116 | static Py_ssize_t pyrf_evlist__length(PyObject *obj) |
| 1117 | { |
| 1118 | struct pyrf_evlist *pevlist = (void *)obj; |
| 1119 | |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 1120 | return pevlist->evlist.core.nr_entries; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i) |
| 1124 | { |
| 1125 | struct pyrf_evlist *pevlist = (void *)obj; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1126 | struct evsel *pos; |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1127 | |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 1128 | if (i >= pevlist->evlist.core.nr_entries) |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1129 | return NULL; |
| 1130 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1131 | evlist__for_each_entry(&pevlist->evlist, pos) { |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1132 | if (i-- == 0) |
| 1133 | break; |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 1134 | } |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1135 | |
| 1136 | return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel)); |
| 1137 | } |
| 1138 | |
| 1139 | static PySequenceMethods pyrf_evlist__sequence_methods = { |
| 1140 | .sq_length = pyrf_evlist__length, |
| 1141 | .sq_item = pyrf_evlist__item, |
| 1142 | }; |
| 1143 | |
| 1144 | static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object."); |
| 1145 | |
| 1146 | static PyTypeObject pyrf_evlist__type = { |
| 1147 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1148 | .tp_name = "perf.evlist", |
| 1149 | .tp_basicsize = sizeof(struct pyrf_evlist), |
| 1150 | .tp_dealloc = (destructor)pyrf_evlist__delete, |
| 1151 | .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, |
| 1152 | .tp_as_sequence = &pyrf_evlist__sequence_methods, |
| 1153 | .tp_doc = pyrf_evlist__doc, |
| 1154 | .tp_methods = pyrf_evlist__methods, |
| 1155 | .tp_init = (initproc)pyrf_evlist__init, |
| 1156 | }; |
| 1157 | |
| 1158 | static int pyrf_evlist__setup_types(void) |
| 1159 | { |
| 1160 | pyrf_evlist__type.tp_new = PyType_GenericNew; |
| 1161 | return PyType_Ready(&pyrf_evlist__type); |
| 1162 | } |
| 1163 | |
Arnaldo Carvalho de Melo | 5865fe3 | 2015-07-24 13:00:03 -0300 | [diff] [blame] | 1164 | #define PERF_CONST(name) { #name, PERF_##name } |
| 1165 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1166 | static struct { |
| 1167 | const char *name; |
| 1168 | int value; |
| 1169 | } perf__constants[] = { |
Arnaldo Carvalho de Melo | 5865fe3 | 2015-07-24 13:00:03 -0300 | [diff] [blame] | 1170 | PERF_CONST(TYPE_HARDWARE), |
| 1171 | PERF_CONST(TYPE_SOFTWARE), |
| 1172 | PERF_CONST(TYPE_TRACEPOINT), |
| 1173 | PERF_CONST(TYPE_HW_CACHE), |
| 1174 | PERF_CONST(TYPE_RAW), |
| 1175 | PERF_CONST(TYPE_BREAKPOINT), |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1176 | |
Arnaldo Carvalho de Melo | 5865fe3 | 2015-07-24 13:00:03 -0300 | [diff] [blame] | 1177 | PERF_CONST(COUNT_HW_CPU_CYCLES), |
| 1178 | PERF_CONST(COUNT_HW_INSTRUCTIONS), |
| 1179 | PERF_CONST(COUNT_HW_CACHE_REFERENCES), |
| 1180 | PERF_CONST(COUNT_HW_CACHE_MISSES), |
| 1181 | PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS), |
| 1182 | PERF_CONST(COUNT_HW_BRANCH_MISSES), |
| 1183 | PERF_CONST(COUNT_HW_BUS_CYCLES), |
| 1184 | PERF_CONST(COUNT_HW_CACHE_L1D), |
| 1185 | PERF_CONST(COUNT_HW_CACHE_L1I), |
| 1186 | PERF_CONST(COUNT_HW_CACHE_LL), |
| 1187 | PERF_CONST(COUNT_HW_CACHE_DTLB), |
| 1188 | PERF_CONST(COUNT_HW_CACHE_ITLB), |
| 1189 | PERF_CONST(COUNT_HW_CACHE_BPU), |
| 1190 | PERF_CONST(COUNT_HW_CACHE_OP_READ), |
| 1191 | PERF_CONST(COUNT_HW_CACHE_OP_WRITE), |
| 1192 | PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH), |
| 1193 | PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS), |
| 1194 | PERF_CONST(COUNT_HW_CACHE_RESULT_MISS), |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1195 | |
Arnaldo Carvalho de Melo | 5865fe3 | 2015-07-24 13:00:03 -0300 | [diff] [blame] | 1196 | PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND), |
| 1197 | PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND), |
Ingo Molnar | 129c04c | 2011-04-29 14:41:28 +0200 | [diff] [blame] | 1198 | |
Arnaldo Carvalho de Melo | 5865fe3 | 2015-07-24 13:00:03 -0300 | [diff] [blame] | 1199 | PERF_CONST(COUNT_SW_CPU_CLOCK), |
| 1200 | PERF_CONST(COUNT_SW_TASK_CLOCK), |
| 1201 | PERF_CONST(COUNT_SW_PAGE_FAULTS), |
| 1202 | PERF_CONST(COUNT_SW_CONTEXT_SWITCHES), |
| 1203 | PERF_CONST(COUNT_SW_CPU_MIGRATIONS), |
| 1204 | PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN), |
| 1205 | PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ), |
| 1206 | PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS), |
| 1207 | PERF_CONST(COUNT_SW_EMULATION_FAULTS), |
| 1208 | PERF_CONST(COUNT_SW_DUMMY), |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1209 | |
Arnaldo Carvalho de Melo | 5865fe3 | 2015-07-24 13:00:03 -0300 | [diff] [blame] | 1210 | PERF_CONST(SAMPLE_IP), |
| 1211 | PERF_CONST(SAMPLE_TID), |
| 1212 | PERF_CONST(SAMPLE_TIME), |
| 1213 | PERF_CONST(SAMPLE_ADDR), |
| 1214 | PERF_CONST(SAMPLE_READ), |
| 1215 | PERF_CONST(SAMPLE_CALLCHAIN), |
| 1216 | PERF_CONST(SAMPLE_ID), |
| 1217 | PERF_CONST(SAMPLE_CPU), |
| 1218 | PERF_CONST(SAMPLE_PERIOD), |
| 1219 | PERF_CONST(SAMPLE_STREAM_ID), |
| 1220 | PERF_CONST(SAMPLE_RAW), |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1221 | |
Arnaldo Carvalho de Melo | 5865fe3 | 2015-07-24 13:00:03 -0300 | [diff] [blame] | 1222 | PERF_CONST(FORMAT_TOTAL_TIME_ENABLED), |
| 1223 | PERF_CONST(FORMAT_TOTAL_TIME_RUNNING), |
| 1224 | PERF_CONST(FORMAT_ID), |
| 1225 | PERF_CONST(FORMAT_GROUP), |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1226 | |
Arnaldo Carvalho de Melo | 5865fe3 | 2015-07-24 13:00:03 -0300 | [diff] [blame] | 1227 | PERF_CONST(RECORD_MMAP), |
| 1228 | PERF_CONST(RECORD_LOST), |
| 1229 | PERF_CONST(RECORD_COMM), |
| 1230 | PERF_CONST(RECORD_EXIT), |
| 1231 | PERF_CONST(RECORD_THROTTLE), |
| 1232 | PERF_CONST(RECORD_UNTHROTTLE), |
| 1233 | PERF_CONST(RECORD_FORK), |
| 1234 | PERF_CONST(RECORD_READ), |
| 1235 | PERF_CONST(RECORD_SAMPLE), |
Arnaldo Carvalho de Melo | 84576da | 2015-07-24 13:04:09 -0300 | [diff] [blame] | 1236 | PERF_CONST(RECORD_MMAP2), |
| 1237 | PERF_CONST(RECORD_AUX), |
| 1238 | PERF_CONST(RECORD_ITRACE_START), |
| 1239 | PERF_CONST(RECORD_LOST_SAMPLES), |
| 1240 | PERF_CONST(RECORD_SWITCH), |
| 1241 | PERF_CONST(RECORD_SWITCH_CPU_WIDE), |
Arnaldo Carvalho de Melo | ae93880 | 2015-10-06 17:46:46 -0300 | [diff] [blame] | 1242 | |
| 1243 | PERF_CONST(RECORD_MISC_SWITCH_OUT), |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 1244 | { .name = NULL, }, |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1245 | }; |
| 1246 | |
Jiri Olsa | 1075fbb | 2016-07-10 13:07:58 +0200 | [diff] [blame] | 1247 | static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel, |
| 1248 | PyObject *args, PyObject *kwargs) |
| 1249 | { |
Tzvetomir Stoyanov | 97fbf3f | 2018-11-30 10:44:07 -0500 | [diff] [blame] | 1250 | struct tep_event *tp_format; |
Jiri Olsa | 1075fbb | 2016-07-10 13:07:58 +0200 | [diff] [blame] | 1251 | static char *kwlist[] = { "sys", "name", NULL }; |
| 1252 | char *sys = NULL; |
| 1253 | char *name = NULL; |
| 1254 | |
| 1255 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist, |
| 1256 | &sys, &name)) |
| 1257 | return NULL; |
| 1258 | |
| 1259 | tp_format = trace_event__tp_format(sys, name); |
| 1260 | if (IS_ERR(tp_format)) |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 1261 | return _PyLong_FromLong(-1); |
Jiri Olsa | 1075fbb | 2016-07-10 13:07:58 +0200 | [diff] [blame] | 1262 | |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 1263 | return _PyLong_FromLong(tp_format->id); |
Jiri Olsa | 1075fbb | 2016-07-10 13:07:58 +0200 | [diff] [blame] | 1264 | } |
| 1265 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1266 | static PyMethodDef perf__methods[] = { |
Jiri Olsa | 1075fbb | 2016-07-10 13:07:58 +0200 | [diff] [blame] | 1267 | { |
| 1268 | .ml_name = "tracepoint", |
| 1269 | .ml_meth = (PyCFunction) pyrf__tracepoint, |
| 1270 | .ml_flags = METH_VARARGS | METH_KEYWORDS, |
| 1271 | .ml_doc = PyDoc_STR("Get tracepoint config.") |
| 1272 | }, |
Arnaldo Carvalho de Melo | f6bbc1d | 2011-01-31 20:56:27 -0200 | [diff] [blame] | 1273 | { .ml_name = NULL, } |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1274 | }; |
| 1275 | |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 1276 | #if PY_MAJOR_VERSION < 3 |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1277 | PyMODINIT_FUNC initperf(void) |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 1278 | #else |
| 1279 | PyMODINIT_FUNC PyInit_perf(void) |
| 1280 | #endif |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1281 | { |
| 1282 | PyObject *obj; |
| 1283 | int i; |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 1284 | PyObject *dict; |
| 1285 | #if PY_MAJOR_VERSION < 3 |
| 1286 | PyObject *module = Py_InitModule("perf", perf__methods); |
| 1287 | #else |
| 1288 | static struct PyModuleDef moduledef = { |
| 1289 | PyModuleDef_HEAD_INIT, |
| 1290 | "perf", /* m_name */ |
| 1291 | "", /* m_doc */ |
| 1292 | -1, /* m_size */ |
| 1293 | perf__methods, /* m_methods */ |
| 1294 | NULL, /* m_reload */ |
| 1295 | NULL, /* m_traverse */ |
| 1296 | NULL, /* m_clear */ |
| 1297 | NULL, /* m_free */ |
| 1298 | }; |
| 1299 | PyObject *module = PyModule_Create(&moduledef); |
| 1300 | #endif |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1301 | |
| 1302 | if (module == NULL || |
| 1303 | pyrf_event__setup_types() < 0 || |
| 1304 | pyrf_evlist__setup_types() < 0 || |
| 1305 | pyrf_evsel__setup_types() < 0 || |
| 1306 | pyrf_thread_map__setup_types() < 0 || |
| 1307 | pyrf_cpu_map__setup_types() < 0) |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 1308 | #if PY_MAJOR_VERSION < 3 |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1309 | return; |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 1310 | #else |
| 1311 | return module; |
| 1312 | #endif |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1313 | |
Jiri Olsa | 918512b | 2013-09-12 18:39:35 +0200 | [diff] [blame] | 1314 | /* The page_size is placed in util object. */ |
Arnaldo Carvalho de Melo | 0da2e9c | 2012-10-16 14:51:04 -0300 | [diff] [blame] | 1315 | page_size = sysconf(_SC_PAGE_SIZE); |
| 1316 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1317 | Py_INCREF(&pyrf_evlist__type); |
| 1318 | PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type); |
| 1319 | |
| 1320 | Py_INCREF(&pyrf_evsel__type); |
| 1321 | PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type); |
| 1322 | |
Jiri Olsa | 85e37de | 2016-07-10 13:07:57 +0200 | [diff] [blame] | 1323 | Py_INCREF(&pyrf_mmap_event__type); |
| 1324 | PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type); |
| 1325 | |
| 1326 | Py_INCREF(&pyrf_lost_event__type); |
| 1327 | PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type); |
| 1328 | |
| 1329 | Py_INCREF(&pyrf_comm_event__type); |
| 1330 | PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type); |
| 1331 | |
| 1332 | Py_INCREF(&pyrf_task_event__type); |
| 1333 | PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type); |
| 1334 | |
| 1335 | Py_INCREF(&pyrf_throttle_event__type); |
| 1336 | PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type); |
| 1337 | |
| 1338 | Py_INCREF(&pyrf_task_event__type); |
| 1339 | PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type); |
| 1340 | |
| 1341 | Py_INCREF(&pyrf_read_event__type); |
| 1342 | PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type); |
| 1343 | |
| 1344 | Py_INCREF(&pyrf_sample_event__type); |
| 1345 | PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type); |
| 1346 | |
| 1347 | Py_INCREF(&pyrf_context_switch_event__type); |
| 1348 | PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type); |
| 1349 | |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1350 | Py_INCREF(&pyrf_thread_map__type); |
| 1351 | PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type); |
| 1352 | |
| 1353 | Py_INCREF(&pyrf_cpu_map__type); |
| 1354 | PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type); |
| 1355 | |
| 1356 | dict = PyModule_GetDict(module); |
| 1357 | if (dict == NULL) |
| 1358 | goto error; |
| 1359 | |
| 1360 | for (i = 0; perf__constants[i].name != NULL; i++) { |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 1361 | obj = _PyLong_FromLong(perf__constants[i].value); |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1362 | if (obj == NULL) |
| 1363 | goto error; |
| 1364 | PyDict_SetItemString(dict, perf__constants[i].name, obj); |
| 1365 | Py_DECREF(obj); |
| 1366 | } |
| 1367 | |
| 1368 | error: |
| 1369 | if (PyErr_Occurred()) |
| 1370 | PyErr_SetString(PyExc_ImportError, "perf: Init failed!"); |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 1371 | #if PY_MAJOR_VERSION >= 3 |
| 1372 | return module; |
| 1373 | #endif |
Arnaldo Carvalho de Melo | 877108e | 2011-01-29 15:44:29 -0200 | [diff] [blame] | 1374 | } |
Arnaldo Carvalho de Melo | 0c6332e | 2012-12-13 16:43:04 -0300 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * Dummy, to avoid dragging all the test_attr infrastructure in the python |
| 1378 | * binding. |
| 1379 | */ |
| 1380 | void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu, |
| 1381 | int fd, int group_fd, unsigned long flags) |
| 1382 | { |
| 1383 | } |