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