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