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