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