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 #ifdef HAVE_LIBTRACEEVENT 9 #include <event-parse.h> 10 #endif 11 #include <perf/mmap.h> 12 #include "evlist.h" 13 #include "evsel.h" 14 #include "event.h" 15 #include "print_binary.h" 16 #include "thread_map.h" 17 #include "trace-event.h" 18 #include "mmap.h" 19 #include "util/kwork.h" 20 #include "util/sample.h" 21 #include <internal/lib.h> 22 #include "../builtin.h" 23 24 #define _PyUnicode_FromString(arg) \ 25 PyUnicode_FromString(arg) 26 #define _PyUnicode_FromFormat(...) \ 27 PyUnicode_FromFormat(__VA_ARGS__) 28 #define _PyLong_FromLong(arg) \ 29 PyLong_FromLong(arg) 30 31 PyMODINIT_FUNC PyInit_perf(void); 32 33 #define member_def(type, member, ptype, help) \ 34 { #member, ptype, \ 35 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \ 36 0, help } 37 38 #define sample_member_def(name, member, ptype, help) \ 39 { #name, ptype, \ 40 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \ 41 0, help } 42 43 struct pyrf_event { 44 PyObject_HEAD 45 struct evsel *evsel; 46 struct perf_sample sample; 47 union perf_event event; 48 }; 49 50 #define sample_members \ 51 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \ 52 sample_member_def(sample_pid, pid, T_INT, "event pid"), \ 53 sample_member_def(sample_tid, tid, T_INT, "event tid"), \ 54 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \ 55 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \ 56 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \ 57 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \ 58 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \ 59 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"), 60 61 static const char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object."); 62 63 static PyMemberDef pyrf_mmap_event__members[] = { 64 sample_members 65 member_def(perf_event_header, type, T_UINT, "event type"), 66 member_def(perf_event_header, misc, T_UINT, "event misc"), 67 member_def(perf_record_mmap, pid, T_UINT, "event pid"), 68 member_def(perf_record_mmap, tid, T_UINT, "event tid"), 69 member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"), 70 member_def(perf_record_mmap, len, T_ULONGLONG, "map length"), 71 member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"), 72 member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"), 73 { .name = NULL, }, 74 }; 75 76 static PyObject *pyrf_mmap_event__repr(const struct pyrf_event *pevent) 77 { 78 PyObject *ret; 79 char *s; 80 81 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", " 82 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", " 83 "filename: %s }", 84 pevent->event.mmap.pid, pevent->event.mmap.tid, 85 pevent->event.mmap.start, pevent->event.mmap.len, 86 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { 87 ret = PyErr_NoMemory(); 88 } else { 89 ret = PyUnicode_FromString(s); 90 free(s); 91 } 92 return ret; 93 } 94 95 static PyTypeObject pyrf_mmap_event__type = { 96 PyVarObject_HEAD_INIT(NULL, 0) 97 .tp_name = "perf.mmap_event", 98 .tp_basicsize = sizeof(struct pyrf_event), 99 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 100 .tp_doc = pyrf_mmap_event__doc, 101 .tp_members = pyrf_mmap_event__members, 102 .tp_repr = (reprfunc)pyrf_mmap_event__repr, 103 }; 104 105 static const char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object."); 106 107 static PyMemberDef pyrf_task_event__members[] = { 108 sample_members 109 member_def(perf_event_header, type, T_UINT, "event type"), 110 member_def(perf_record_fork, pid, T_UINT, "event pid"), 111 member_def(perf_record_fork, ppid, T_UINT, "event ppid"), 112 member_def(perf_record_fork, tid, T_UINT, "event tid"), 113 member_def(perf_record_fork, ptid, T_UINT, "event ptid"), 114 member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"), 115 { .name = NULL, }, 116 }; 117 118 static PyObject *pyrf_task_event__repr(const struct pyrf_event *pevent) 119 { 120 return PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " 121 "ptid: %u, time: %" PRI_lu64 "}", 122 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit", 123 pevent->event.fork.pid, 124 pevent->event.fork.ppid, 125 pevent->event.fork.tid, 126 pevent->event.fork.ptid, 127 pevent->event.fork.time); 128 } 129 130 static PyTypeObject pyrf_task_event__type = { 131 PyVarObject_HEAD_INIT(NULL, 0) 132 .tp_name = "perf.task_event", 133 .tp_basicsize = sizeof(struct pyrf_event), 134 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 135 .tp_doc = pyrf_task_event__doc, 136 .tp_members = pyrf_task_event__members, 137 .tp_repr = (reprfunc)pyrf_task_event__repr, 138 }; 139 140 static const char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object."); 141 142 static PyMemberDef pyrf_comm_event__members[] = { 143 sample_members 144 member_def(perf_event_header, type, T_UINT, "event type"), 145 member_def(perf_record_comm, pid, T_UINT, "event pid"), 146 member_def(perf_record_comm, tid, T_UINT, "event tid"), 147 member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"), 148 { .name = NULL, }, 149 }; 150 151 static PyObject *pyrf_comm_event__repr(const struct pyrf_event *pevent) 152 { 153 return PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", 154 pevent->event.comm.pid, 155 pevent->event.comm.tid, 156 pevent->event.comm.comm); 157 } 158 159 static PyTypeObject pyrf_comm_event__type = { 160 PyVarObject_HEAD_INIT(NULL, 0) 161 .tp_name = "perf.comm_event", 162 .tp_basicsize = sizeof(struct pyrf_event), 163 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 164 .tp_doc = pyrf_comm_event__doc, 165 .tp_members = pyrf_comm_event__members, 166 .tp_repr = (reprfunc)pyrf_comm_event__repr, 167 }; 168 169 static const char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object."); 170 171 static PyMemberDef pyrf_throttle_event__members[] = { 172 sample_members 173 member_def(perf_event_header, type, T_UINT, "event type"), 174 member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"), 175 member_def(perf_record_throttle, id, T_ULONGLONG, "event id"), 176 member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"), 177 { .name = NULL, }, 178 }; 179 180 static PyObject *pyrf_throttle_event__repr(const struct pyrf_event *pevent) 181 { 182 const struct perf_record_throttle *te = (const struct perf_record_throttle *) 183 (&pevent->event.header + 1); 184 185 return PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64 186 ", stream_id: %" PRI_lu64 " }", 187 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un", 188 te->time, te->id, te->stream_id); 189 } 190 191 static PyTypeObject pyrf_throttle_event__type = { 192 PyVarObject_HEAD_INIT(NULL, 0) 193 .tp_name = "perf.throttle_event", 194 .tp_basicsize = sizeof(struct pyrf_event), 195 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 196 .tp_doc = pyrf_throttle_event__doc, 197 .tp_members = pyrf_throttle_event__members, 198 .tp_repr = (reprfunc)pyrf_throttle_event__repr, 199 }; 200 201 static const char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object."); 202 203 static PyMemberDef pyrf_lost_event__members[] = { 204 sample_members 205 member_def(perf_record_lost, id, T_ULONGLONG, "event id"), 206 member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"), 207 { .name = NULL, }, 208 }; 209 210 static PyObject *pyrf_lost_event__repr(const struct pyrf_event *pevent) 211 { 212 PyObject *ret; 213 char *s; 214 215 if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", " 216 "lost: %#" PRI_lx64 " }", 217 pevent->event.lost.id, pevent->event.lost.lost) < 0) { 218 ret = PyErr_NoMemory(); 219 } else { 220 ret = PyUnicode_FromString(s); 221 free(s); 222 } 223 return ret; 224 } 225 226 static PyTypeObject pyrf_lost_event__type = { 227 PyVarObject_HEAD_INIT(NULL, 0) 228 .tp_name = "perf.lost_event", 229 .tp_basicsize = sizeof(struct pyrf_event), 230 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 231 .tp_doc = pyrf_lost_event__doc, 232 .tp_members = pyrf_lost_event__members, 233 .tp_repr = (reprfunc)pyrf_lost_event__repr, 234 }; 235 236 static const char pyrf_read_event__doc[] = PyDoc_STR("perf read event object."); 237 238 static PyMemberDef pyrf_read_event__members[] = { 239 sample_members 240 member_def(perf_record_read, pid, T_UINT, "event pid"), 241 member_def(perf_record_read, tid, T_UINT, "event tid"), 242 { .name = NULL, }, 243 }; 244 245 static PyObject *pyrf_read_event__repr(const struct pyrf_event *pevent) 246 { 247 return PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }", 248 pevent->event.read.pid, 249 pevent->event.read.tid); 250 /* 251 * FIXME: return the array of read values, 252 * making this method useful ;-) 253 */ 254 } 255 256 static PyTypeObject pyrf_read_event__type = { 257 PyVarObject_HEAD_INIT(NULL, 0) 258 .tp_name = "perf.read_event", 259 .tp_basicsize = sizeof(struct pyrf_event), 260 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 261 .tp_doc = pyrf_read_event__doc, 262 .tp_members = pyrf_read_event__members, 263 .tp_repr = (reprfunc)pyrf_read_event__repr, 264 }; 265 266 static const char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object."); 267 268 static PyMemberDef pyrf_sample_event__members[] = { 269 sample_members 270 member_def(perf_event_header, type, T_UINT, "event type"), 271 { .name = NULL, }, 272 }; 273 274 static PyObject *pyrf_sample_event__repr(const struct pyrf_event *pevent) 275 { 276 PyObject *ret; 277 char *s; 278 279 if (asprintf(&s, "{ type: sample }") < 0) { 280 ret = PyErr_NoMemory(); 281 } else { 282 ret = PyUnicode_FromString(s); 283 free(s); 284 } 285 return ret; 286 } 287 288 #ifdef HAVE_LIBTRACEEVENT 289 static bool is_tracepoint(const struct pyrf_event *pevent) 290 { 291 return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT; 292 } 293 294 static PyObject* 295 tracepoint_field(const struct pyrf_event *pe, struct tep_format_field *field) 296 { 297 struct tep_handle *pevent = field->event->tep; 298 void *data = pe->sample.raw_data; 299 PyObject *ret = NULL; 300 unsigned long long val; 301 unsigned int offset, len; 302 303 if (field->flags & TEP_FIELD_IS_ARRAY) { 304 offset = field->offset; 305 len = field->size; 306 if (field->flags & TEP_FIELD_IS_DYNAMIC) { 307 val = tep_read_number(pevent, data + offset, len); 308 offset = val; 309 len = offset >> 16; 310 offset &= 0xffff; 311 if (tep_field_is_relative(field->flags)) 312 offset += field->offset + field->size; 313 } 314 if (field->flags & TEP_FIELD_IS_STRING && 315 is_printable_array(data + offset, len)) { 316 ret = PyUnicode_FromString((char *)data + offset); 317 } else { 318 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len); 319 field->flags &= ~TEP_FIELD_IS_STRING; 320 } 321 } else { 322 val = tep_read_number(pevent, data + field->offset, 323 field->size); 324 if (field->flags & TEP_FIELD_IS_POINTER) 325 ret = PyLong_FromUnsignedLong((unsigned long) val); 326 else if (field->flags & TEP_FIELD_IS_SIGNED) 327 ret = PyLong_FromLong((long) val); 328 else 329 ret = PyLong_FromUnsignedLong((unsigned long) val); 330 } 331 332 return ret; 333 } 334 335 static PyObject* 336 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) 337 { 338 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name)); 339 struct evsel *evsel = pevent->evsel; 340 struct tep_format_field *field; 341 342 if (!evsel->tp_format) { 343 struct tep_event *tp_format; 344 345 tp_format = trace_event__tp_format_id(evsel->core.attr.config); 346 if (IS_ERR_OR_NULL(tp_format)) 347 return NULL; 348 349 evsel->tp_format = tp_format; 350 } 351 352 field = tep_find_any_field(evsel->tp_format, str); 353 if (!field) 354 return NULL; 355 356 return tracepoint_field(pevent, field); 357 } 358 #endif /* HAVE_LIBTRACEEVENT */ 359 360 static PyObject* 361 pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name) 362 { 363 PyObject *obj = NULL; 364 365 #ifdef HAVE_LIBTRACEEVENT 366 if (is_tracepoint(pevent)) 367 obj = get_tracepoint_field(pevent, attr_name); 368 #endif 369 370 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name); 371 } 372 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, 381 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro, 382 }; 383 384 static const 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(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"), 390 member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"), 391 { .name = NULL, }, 392 }; 393 394 static PyObject *pyrf_context_switch_event__repr(const 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 = PyUnicode_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 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 = 427 pyrf_lost_event__type.tp_new = 428 pyrf_read_event__type.tp_new = 429 pyrf_sample_event__type.tp_new = 430 pyrf_context_switch_event__type.tp_new = 431 pyrf_throttle_event__type.tp_new = PyType_GenericNew; 432 err = PyType_Ready(&pyrf_mmap_event__type); 433 if (err < 0) 434 goto out; 435 err = PyType_Ready(&pyrf_lost_event__type); 436 if (err < 0) 437 goto out; 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; 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; 453 err = PyType_Ready(&pyrf_context_switch_event__type); 454 if (err < 0) 455 goto out; 456 out: 457 return err; 458 } 459 460 static PyTypeObject *pyrf_event__type[] = { 461 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type, 462 [PERF_RECORD_LOST] = &pyrf_lost_event__type, 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, 468 [PERF_RECORD_READ] = &pyrf_read_event__type, 469 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type, 470 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type, 471 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type, 472 }; 473 474 static PyObject *pyrf_event__new(const union perf_event *event) 475 { 476 struct pyrf_event *pevent; 477 PyTypeObject *ptype; 478 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)) 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 perf_cpu_map *cpus; 496 }; 497 498 static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus, 499 PyObject *args, PyObject *kwargs) 500 { 501 static char *kwlist[] = { "cpustr", NULL }; 502 char *cpustr = NULL; 503 504 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", 505 kwlist, &cpustr)) 506 return -1; 507 508 pcpus->cpus = perf_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 { 516 perf_cpu_map__put(pcpus->cpus); 517 Py_TYPE(pcpus)->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 perf_cpu_map__nr(pcpus->cpus); 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 >= perf_cpu_map__nr(pcpus->cpus)) 532 return NULL; 533 534 return Py_BuildValue("i", perf_cpu_map__cpu(pcpus->cpus, i).cpu); 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 const 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 perf_thread_map *threads; 565 }; 566 567 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads, 568 PyObject *args, PyObject *kwargs) 569 { 570 static char *kwlist[] = { "pid", "tid", "uid", NULL }; 571 int pid = -1, tid = -1, uid = UINT_MAX; 572 573 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii", 574 kwlist, &pid, &tid, &uid)) 575 return -1; 576 577 pthreads->threads = thread_map__new(pid, tid, uid); 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 { 585 perf_thread_map__put(pthreads->threads); 586 Py_TYPE(pthreads)->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 perf_thread_map__nr(pthreads->threads); 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 >= perf_thread_map__nr(pthreads->threads)) 601 return NULL; 602 603 return Py_BuildValue("i", perf_thread_map__pid(pthreads->threads, 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 const 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 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", 660 "context_switch", 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", 673 "bp_len", 674 NULL 675 }; 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, 686 context_switch = 0, 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, 699 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist, 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, 705 &mmap, &context_switch, &comm, &freq, &inherit_stat, 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; 729 attr.context_switch = context_switch; 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; 739 attr.size = sizeof(attr); 740 741 evsel__init(&pevsel->evsel, &attr, idx); 742 return 0; 743 } 744 745 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel) 746 { 747 evsel__exit(&pevsel->evsel); 748 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel); 749 } 750 751 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, 752 PyObject *args, PyObject *kwargs) 753 { 754 struct evsel *evsel = &pevsel->evsel; 755 struct perf_cpu_map *cpus = NULL; 756 struct perf_thread_map *threads = NULL; 757 PyObject *pcpus = NULL, *pthreads = NULL; 758 int group = 0, inherit = 0; 759 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL }; 760 761 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, 762 &pcpus, &pthreads, &group, &inherit)) 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 771 evsel->core.attr.inherit = inherit; 772 /* 773 * This will group just the fds for this single evsel, to group 774 * multiple events, use evlist.open(). 775 */ 776 if (evsel__open(evsel, cpus, threads) < 0) { 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 }, 792 { .ml_name = NULL, } 793 }; 794 795 static const 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 evlist evlist; 818 }; 819 820 static int pyrf_evlist__init(struct pyrf_evlist *pevlist, 821 PyObject *args, PyObject *kwargs __maybe_unused) 822 { 823 PyObject *pcpus = NULL, *pthreads = NULL; 824 struct perf_cpu_map *cpus; 825 struct perf_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 evlist__init(&pevlist->evlist, cpus, threads); 833 return 0; 834 } 835 836 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist) 837 { 838 evlist__exit(&pevlist->evlist); 839 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist); 840 } 841 842 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, 843 PyObject *args, PyObject *kwargs) 844 { 845 struct evlist *evlist = &pevlist->evlist; 846 static char *kwlist[] = { "pages", "overwrite", NULL }; 847 int pages = 128, overwrite = false; 848 849 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist, 850 &pages, &overwrite)) 851 return NULL; 852 853 if (evlist__mmap(evlist, pages) < 0) { 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 evlist *evlist = &pevlist->evlist; 866 static char *kwlist[] = { "timeout", NULL }; 867 int timeout = -1, n; 868 869 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout)) 870 return NULL; 871 872 n = evlist__poll(evlist, timeout); 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, 882 PyObject *args __maybe_unused, 883 PyObject *kwargs __maybe_unused) 884 { 885 struct evlist *evlist = &pevlist->evlist; 886 PyObject *list = PyList_New(0); 887 int i; 888 889 for (i = 0; i < evlist->core.pollfd.nr; ++i) { 890 PyObject *file; 891 file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1, 892 NULL, NULL, NULL, 0); 893 if (file == NULL) 894 goto free_list; 895 896 if (PyList_Append(list, file) != 0) { 897 Py_DECREF(file); 898 goto free_list; 899 } 900 901 Py_DECREF(file); 902 } 903 904 return list; 905 free_list: 906 return PyErr_NoMemory(); 907 } 908 909 910 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist, 911 PyObject *args, 912 PyObject *kwargs __maybe_unused) 913 { 914 struct evlist *evlist = &pevlist->evlist; 915 PyObject *pevsel; 916 struct evsel *evsel; 917 918 if (!PyArg_ParseTuple(args, "O", &pevsel)) 919 return NULL; 920 921 Py_INCREF(pevsel); 922 evsel = &((struct pyrf_evsel *)pevsel)->evsel; 923 evsel->core.idx = evlist->core.nr_entries; 924 evlist__add(evlist, evsel); 925 926 return Py_BuildValue("i", evlist->core.nr_entries); 927 } 928 929 static struct mmap *get_md(struct evlist *evlist, int cpu) 930 { 931 int i; 932 933 for (i = 0; i < evlist->core.nr_mmaps; i++) { 934 struct mmap *md = &evlist->mmap[i]; 935 936 if (md->core.cpu.cpu == cpu) 937 return md; 938 } 939 940 return NULL; 941 } 942 943 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, 944 PyObject *args, PyObject *kwargs) 945 { 946 struct evlist *evlist = &pevlist->evlist; 947 union perf_event *event; 948 int sample_id_all = 1, cpu; 949 static char *kwlist[] = { "cpu", "sample_id_all", NULL }; 950 struct mmap *md; 951 int err; 952 953 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, 954 &cpu, &sample_id_all)) 955 return NULL; 956 957 md = get_md(evlist, cpu); 958 if (!md) 959 return NULL; 960 961 if (perf_mmap__read_init(&md->core) < 0) 962 goto end; 963 964 event = perf_mmap__read_event(&md->core); 965 if (event != NULL) { 966 PyObject *pyevent = pyrf_event__new(event); 967 struct pyrf_event *pevent = (struct pyrf_event *)pyevent; 968 struct evsel *evsel; 969 970 if (pyevent == NULL) 971 return PyErr_NoMemory(); 972 973 evsel = evlist__event2evsel(evlist, event); 974 if (!evsel) { 975 Py_INCREF(Py_None); 976 return Py_None; 977 } 978 979 pevent->evsel = evsel; 980 981 err = evsel__parse_sample(evsel, event, &pevent->sample); 982 983 /* Consume the even only after we parsed it out. */ 984 perf_mmap__consume(&md->core); 985 986 if (err) 987 return PyErr_Format(PyExc_OSError, 988 "perf: can't parse sample, err=%d", err); 989 return pyevent; 990 } 991 end: 992 Py_INCREF(Py_None); 993 return Py_None; 994 } 995 996 static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist, 997 PyObject *args, PyObject *kwargs) 998 { 999 struct evlist *evlist = &pevlist->evlist; 1000 1001 if (evlist__open(evlist) < 0) { 1002 PyErr_SetFromErrno(PyExc_OSError); 1003 return NULL; 1004 } 1005 1006 Py_INCREF(Py_None); 1007 return Py_None; 1008 } 1009 1010 static PyMethodDef pyrf_evlist__methods[] = { 1011 { 1012 .ml_name = "mmap", 1013 .ml_meth = (PyCFunction)pyrf_evlist__mmap, 1014 .ml_flags = METH_VARARGS | METH_KEYWORDS, 1015 .ml_doc = PyDoc_STR("mmap the file descriptor table.") 1016 }, 1017 { 1018 .ml_name = "open", 1019 .ml_meth = (PyCFunction)pyrf_evlist__open, 1020 .ml_flags = METH_VARARGS | METH_KEYWORDS, 1021 .ml_doc = PyDoc_STR("open the file descriptors.") 1022 }, 1023 { 1024 .ml_name = "poll", 1025 .ml_meth = (PyCFunction)pyrf_evlist__poll, 1026 .ml_flags = METH_VARARGS | METH_KEYWORDS, 1027 .ml_doc = PyDoc_STR("poll the file descriptor table.") 1028 }, 1029 { 1030 .ml_name = "get_pollfd", 1031 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd, 1032 .ml_flags = METH_VARARGS | METH_KEYWORDS, 1033 .ml_doc = PyDoc_STR("get the poll file descriptor table.") 1034 }, 1035 { 1036 .ml_name = "add", 1037 .ml_meth = (PyCFunction)pyrf_evlist__add, 1038 .ml_flags = METH_VARARGS | METH_KEYWORDS, 1039 .ml_doc = PyDoc_STR("adds an event selector to the list.") 1040 }, 1041 { 1042 .ml_name = "read_on_cpu", 1043 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu, 1044 .ml_flags = METH_VARARGS | METH_KEYWORDS, 1045 .ml_doc = PyDoc_STR("reads an event.") 1046 }, 1047 { .ml_name = NULL, } 1048 }; 1049 1050 static Py_ssize_t pyrf_evlist__length(PyObject *obj) 1051 { 1052 struct pyrf_evlist *pevlist = (void *)obj; 1053 1054 return pevlist->evlist.core.nr_entries; 1055 } 1056 1057 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i) 1058 { 1059 struct pyrf_evlist *pevlist = (void *)obj; 1060 struct evsel *pos; 1061 1062 if (i >= pevlist->evlist.core.nr_entries) 1063 return NULL; 1064 1065 evlist__for_each_entry(&pevlist->evlist, pos) { 1066 if (i-- == 0) 1067 break; 1068 } 1069 1070 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel)); 1071 } 1072 1073 static PySequenceMethods pyrf_evlist__sequence_methods = { 1074 .sq_length = pyrf_evlist__length, 1075 .sq_item = pyrf_evlist__item, 1076 }; 1077 1078 static const char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object."); 1079 1080 static PyTypeObject pyrf_evlist__type = { 1081 PyVarObject_HEAD_INIT(NULL, 0) 1082 .tp_name = "perf.evlist", 1083 .tp_basicsize = sizeof(struct pyrf_evlist), 1084 .tp_dealloc = (destructor)pyrf_evlist__delete, 1085 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 1086 .tp_as_sequence = &pyrf_evlist__sequence_methods, 1087 .tp_doc = pyrf_evlist__doc, 1088 .tp_methods = pyrf_evlist__methods, 1089 .tp_init = (initproc)pyrf_evlist__init, 1090 }; 1091 1092 static int pyrf_evlist__setup_types(void) 1093 { 1094 pyrf_evlist__type.tp_new = PyType_GenericNew; 1095 return PyType_Ready(&pyrf_evlist__type); 1096 } 1097 1098 #define PERF_CONST(name) { #name, PERF_##name } 1099 1100 struct perf_constant { 1101 const char *name; 1102 int value; 1103 }; 1104 1105 static const struct perf_constant perf__constants[] = { 1106 PERF_CONST(TYPE_HARDWARE), 1107 PERF_CONST(TYPE_SOFTWARE), 1108 PERF_CONST(TYPE_TRACEPOINT), 1109 PERF_CONST(TYPE_HW_CACHE), 1110 PERF_CONST(TYPE_RAW), 1111 PERF_CONST(TYPE_BREAKPOINT), 1112 1113 PERF_CONST(COUNT_HW_CPU_CYCLES), 1114 PERF_CONST(COUNT_HW_INSTRUCTIONS), 1115 PERF_CONST(COUNT_HW_CACHE_REFERENCES), 1116 PERF_CONST(COUNT_HW_CACHE_MISSES), 1117 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS), 1118 PERF_CONST(COUNT_HW_BRANCH_MISSES), 1119 PERF_CONST(COUNT_HW_BUS_CYCLES), 1120 PERF_CONST(COUNT_HW_CACHE_L1D), 1121 PERF_CONST(COUNT_HW_CACHE_L1I), 1122 PERF_CONST(COUNT_HW_CACHE_LL), 1123 PERF_CONST(COUNT_HW_CACHE_DTLB), 1124 PERF_CONST(COUNT_HW_CACHE_ITLB), 1125 PERF_CONST(COUNT_HW_CACHE_BPU), 1126 PERF_CONST(COUNT_HW_CACHE_OP_READ), 1127 PERF_CONST(COUNT_HW_CACHE_OP_WRITE), 1128 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH), 1129 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS), 1130 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS), 1131 1132 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND), 1133 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND), 1134 1135 PERF_CONST(COUNT_SW_CPU_CLOCK), 1136 PERF_CONST(COUNT_SW_TASK_CLOCK), 1137 PERF_CONST(COUNT_SW_PAGE_FAULTS), 1138 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES), 1139 PERF_CONST(COUNT_SW_CPU_MIGRATIONS), 1140 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN), 1141 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ), 1142 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS), 1143 PERF_CONST(COUNT_SW_EMULATION_FAULTS), 1144 PERF_CONST(COUNT_SW_DUMMY), 1145 1146 PERF_CONST(SAMPLE_IP), 1147 PERF_CONST(SAMPLE_TID), 1148 PERF_CONST(SAMPLE_TIME), 1149 PERF_CONST(SAMPLE_ADDR), 1150 PERF_CONST(SAMPLE_READ), 1151 PERF_CONST(SAMPLE_CALLCHAIN), 1152 PERF_CONST(SAMPLE_ID), 1153 PERF_CONST(SAMPLE_CPU), 1154 PERF_CONST(SAMPLE_PERIOD), 1155 PERF_CONST(SAMPLE_STREAM_ID), 1156 PERF_CONST(SAMPLE_RAW), 1157 1158 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED), 1159 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING), 1160 PERF_CONST(FORMAT_ID), 1161 PERF_CONST(FORMAT_GROUP), 1162 1163 PERF_CONST(RECORD_MMAP), 1164 PERF_CONST(RECORD_LOST), 1165 PERF_CONST(RECORD_COMM), 1166 PERF_CONST(RECORD_EXIT), 1167 PERF_CONST(RECORD_THROTTLE), 1168 PERF_CONST(RECORD_UNTHROTTLE), 1169 PERF_CONST(RECORD_FORK), 1170 PERF_CONST(RECORD_READ), 1171 PERF_CONST(RECORD_SAMPLE), 1172 PERF_CONST(RECORD_MMAP2), 1173 PERF_CONST(RECORD_AUX), 1174 PERF_CONST(RECORD_ITRACE_START), 1175 PERF_CONST(RECORD_LOST_SAMPLES), 1176 PERF_CONST(RECORD_SWITCH), 1177 PERF_CONST(RECORD_SWITCH_CPU_WIDE), 1178 1179 PERF_CONST(RECORD_MISC_SWITCH_OUT), 1180 { .name = NULL, }, 1181 }; 1182 1183 static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel, 1184 PyObject *args, PyObject *kwargs) 1185 { 1186 #ifndef HAVE_LIBTRACEEVENT 1187 return NULL; 1188 #else 1189 struct tep_event *tp_format; 1190 static char *kwlist[] = { "sys", "name", NULL }; 1191 char *sys = NULL; 1192 char *name = NULL; 1193 1194 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist, 1195 &sys, &name)) 1196 return NULL; 1197 1198 tp_format = trace_event__tp_format(sys, name); 1199 if (IS_ERR(tp_format)) 1200 return PyLong_FromLong(-1); 1201 1202 return PyLong_FromLong(tp_format->id); 1203 #endif // HAVE_LIBTRACEEVENT 1204 } 1205 1206 static PyMethodDef perf__methods[] = { 1207 { 1208 .ml_name = "tracepoint", 1209 .ml_meth = (PyCFunction) pyrf__tracepoint, 1210 .ml_flags = METH_VARARGS | METH_KEYWORDS, 1211 .ml_doc = PyDoc_STR("Get tracepoint config.") 1212 }, 1213 { .ml_name = NULL, } 1214 }; 1215 1216 PyMODINIT_FUNC PyInit_perf(void) 1217 { 1218 PyObject *obj; 1219 int i; 1220 PyObject *dict; 1221 static struct PyModuleDef moduledef = { 1222 PyModuleDef_HEAD_INIT, 1223 "perf", /* m_name */ 1224 "", /* m_doc */ 1225 -1, /* m_size */ 1226 perf__methods, /* m_methods */ 1227 NULL, /* m_reload */ 1228 NULL, /* m_traverse */ 1229 NULL, /* m_clear */ 1230 NULL, /* m_free */ 1231 }; 1232 PyObject *module = PyModule_Create(&moduledef); 1233 1234 if (module == NULL || 1235 pyrf_event__setup_types() < 0 || 1236 pyrf_evlist__setup_types() < 0 || 1237 pyrf_evsel__setup_types() < 0 || 1238 pyrf_thread_map__setup_types() < 0 || 1239 pyrf_cpu_map__setup_types() < 0) 1240 return module; 1241 1242 /* The page_size is placed in util object. */ 1243 page_size = sysconf(_SC_PAGE_SIZE); 1244 1245 Py_INCREF(&pyrf_evlist__type); 1246 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type); 1247 1248 Py_INCREF(&pyrf_evsel__type); 1249 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type); 1250 1251 Py_INCREF(&pyrf_mmap_event__type); 1252 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type); 1253 1254 Py_INCREF(&pyrf_lost_event__type); 1255 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type); 1256 1257 Py_INCREF(&pyrf_comm_event__type); 1258 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type); 1259 1260 Py_INCREF(&pyrf_task_event__type); 1261 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type); 1262 1263 Py_INCREF(&pyrf_throttle_event__type); 1264 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type); 1265 1266 Py_INCREF(&pyrf_task_event__type); 1267 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type); 1268 1269 Py_INCREF(&pyrf_read_event__type); 1270 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type); 1271 1272 Py_INCREF(&pyrf_sample_event__type); 1273 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type); 1274 1275 Py_INCREF(&pyrf_context_switch_event__type); 1276 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type); 1277 1278 Py_INCREF(&pyrf_thread_map__type); 1279 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type); 1280 1281 Py_INCREF(&pyrf_cpu_map__type); 1282 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type); 1283 1284 dict = PyModule_GetDict(module); 1285 if (dict == NULL) 1286 goto error; 1287 1288 for (i = 0; perf__constants[i].name != NULL; i++) { 1289 obj = PyLong_FromLong(perf__constants[i].value); 1290 if (obj == NULL) 1291 goto error; 1292 PyDict_SetItemString(dict, perf__constants[i].name, obj); 1293 Py_DECREF(obj); 1294 } 1295 1296 error: 1297 if (PyErr_Occurred()) 1298 PyErr_SetString(PyExc_ImportError, "perf: Init failed!"); 1299 return module; 1300 } 1301 1302 1303 /* The following are stubs to avoid dragging in builtin-* objects. */ 1304 /* TODO: move the code out of the builtin-* file into util. */ 1305 1306 struct kwork_work *perf_kwork_add_work(struct perf_kwork *kwork __maybe_unused, 1307 struct kwork_class *class __maybe_unused, 1308 struct kwork_work *key __maybe_unused) 1309 { 1310 return NULL; 1311 } 1312 1313 int cmd_inject(int argc __maybe_unused, const char *argv[] __maybe_unused) 1314 { 1315 return -1; 1316 } 1317