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