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