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