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