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