1877108e4SArnaldo Carvalho de Melo #include <Python.h> 2877108e4SArnaldo Carvalho de Melo #include <structmember.h> 3877108e4SArnaldo Carvalho de Melo #include <inttypes.h> 4877108e4SArnaldo Carvalho de Melo #include <poll.h> 5877108e4SArnaldo Carvalho de Melo #include "evlist.h" 6877108e4SArnaldo Carvalho de Melo #include "evsel.h" 7877108e4SArnaldo Carvalho de Melo #include "event.h" 8877108e4SArnaldo Carvalho de Melo #include "cpumap.h" 9877108e4SArnaldo Carvalho de Melo #include "thread_map.h" 10877108e4SArnaldo Carvalho de Melo 118afb4c01SAdrian Hunter /* 128afb4c01SAdrian Hunter * Support debug printing even though util/debug.c is not linked. That means 138afb4c01SAdrian Hunter * implementing 'verbose' and 'eprintf'. 148afb4c01SAdrian Hunter */ 158afb4c01SAdrian Hunter int verbose; 168afb4c01SAdrian Hunter 178afb4c01SAdrian Hunter int eprintf(int level, const char *fmt, ...) 188afb4c01SAdrian Hunter { 198afb4c01SAdrian Hunter va_list args; 208afb4c01SAdrian Hunter int ret = 0; 218afb4c01SAdrian Hunter 228afb4c01SAdrian Hunter if (verbose >= level) { 238afb4c01SAdrian Hunter va_start(args, fmt); 248afb4c01SAdrian Hunter ret = vfprintf(stderr, fmt, args); 258afb4c01SAdrian Hunter va_end(args); 268afb4c01SAdrian Hunter } 278afb4c01SAdrian Hunter 288afb4c01SAdrian Hunter return ret; 298afb4c01SAdrian Hunter } 308afb4c01SAdrian Hunter 31cfff2d90SFrederic Weisbecker /* Define PyVarObject_HEAD_INIT for python 2.5 */ 32cfff2d90SFrederic Weisbecker #ifndef PyVarObject_HEAD_INIT 33cfff2d90SFrederic Weisbecker # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, 34cfff2d90SFrederic Weisbecker #endif 35cfff2d90SFrederic Weisbecker 36f6bbc1daSArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void); 37f6bbc1daSArnaldo Carvalho de Melo 38877108e4SArnaldo Carvalho de Melo #define member_def(type, member, ptype, help) \ 39877108e4SArnaldo Carvalho de Melo { #member, ptype, \ 40877108e4SArnaldo Carvalho de Melo offsetof(struct pyrf_event, event) + offsetof(struct type, member), \ 41877108e4SArnaldo Carvalho de Melo 0, help } 42877108e4SArnaldo Carvalho de Melo 43877108e4SArnaldo Carvalho de Melo #define sample_member_def(name, member, ptype, help) \ 44877108e4SArnaldo Carvalho de Melo { #name, ptype, \ 45877108e4SArnaldo Carvalho de Melo offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \ 46877108e4SArnaldo Carvalho de Melo 0, help } 47877108e4SArnaldo Carvalho de Melo 48877108e4SArnaldo Carvalho de Melo struct pyrf_event { 49877108e4SArnaldo Carvalho de Melo PyObject_HEAD 50877108e4SArnaldo Carvalho de Melo struct perf_sample sample; 51877108e4SArnaldo Carvalho de Melo union perf_event event; 52877108e4SArnaldo Carvalho de Melo }; 53877108e4SArnaldo Carvalho de Melo 54877108e4SArnaldo Carvalho de Melo #define sample_members \ 55f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \ 56877108e4SArnaldo Carvalho de Melo sample_member_def(sample_pid, pid, T_INT, "event pid"), \ 57877108e4SArnaldo Carvalho de Melo sample_member_def(sample_tid, tid, T_INT, "event tid"), \ 58f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \ 59f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \ 60f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \ 61f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \ 62f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \ 63877108e4SArnaldo Carvalho de Melo sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"), 64877108e4SArnaldo Carvalho de Melo 65877108e4SArnaldo Carvalho de Melo static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object."); 66877108e4SArnaldo Carvalho de Melo 67877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_mmap_event__members[] = { 68877108e4SArnaldo Carvalho de Melo sample_members 69877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 70877108e4SArnaldo Carvalho de Melo member_def(mmap_event, pid, T_UINT, "event pid"), 71877108e4SArnaldo Carvalho de Melo member_def(mmap_event, tid, T_UINT, "event tid"), 72f6bbc1daSArnaldo Carvalho de Melo member_def(mmap_event, start, T_ULONGLONG, "start of the map"), 73f6bbc1daSArnaldo Carvalho de Melo member_def(mmap_event, len, T_ULONGLONG, "map length"), 74f6bbc1daSArnaldo Carvalho de Melo member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"), 75877108e4SArnaldo Carvalho de Melo member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"), 76f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 77877108e4SArnaldo Carvalho de Melo }; 78877108e4SArnaldo Carvalho de Melo 79877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent) 80877108e4SArnaldo Carvalho de Melo { 81877108e4SArnaldo Carvalho de Melo PyObject *ret; 82877108e4SArnaldo Carvalho de Melo char *s; 83877108e4SArnaldo Carvalho de Melo 84877108e4SArnaldo Carvalho de Melo if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", " 85877108e4SArnaldo Carvalho de Melo "length: %#" PRIx64 ", offset: %#" PRIx64 ", " 86877108e4SArnaldo Carvalho de Melo "filename: %s }", 87877108e4SArnaldo Carvalho de Melo pevent->event.mmap.pid, pevent->event.mmap.tid, 88877108e4SArnaldo Carvalho de Melo pevent->event.mmap.start, pevent->event.mmap.len, 89877108e4SArnaldo Carvalho de Melo pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { 90877108e4SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 91877108e4SArnaldo Carvalho de Melo } else { 92877108e4SArnaldo Carvalho de Melo ret = PyString_FromString(s); 93877108e4SArnaldo Carvalho de Melo free(s); 94877108e4SArnaldo Carvalho de Melo } 95877108e4SArnaldo Carvalho de Melo return ret; 96877108e4SArnaldo Carvalho de Melo } 97877108e4SArnaldo Carvalho de Melo 98877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_mmap_event__type = { 99877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 100877108e4SArnaldo Carvalho de Melo .tp_name = "perf.mmap_event", 101877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 102877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 103877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_mmap_event__doc, 104877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_mmap_event__members, 105877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_mmap_event__repr, 106877108e4SArnaldo Carvalho de Melo }; 107877108e4SArnaldo Carvalho de Melo 108877108e4SArnaldo Carvalho de Melo static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object."); 109877108e4SArnaldo Carvalho de Melo 110877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_task_event__members[] = { 111877108e4SArnaldo Carvalho de Melo sample_members 112877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 113877108e4SArnaldo Carvalho de Melo member_def(fork_event, pid, T_UINT, "event pid"), 114877108e4SArnaldo Carvalho de Melo member_def(fork_event, ppid, T_UINT, "event ppid"), 115877108e4SArnaldo Carvalho de Melo member_def(fork_event, tid, T_UINT, "event tid"), 116877108e4SArnaldo Carvalho de Melo member_def(fork_event, ptid, T_UINT, "event ptid"), 117f6bbc1daSArnaldo Carvalho de Melo member_def(fork_event, time, T_ULONGLONG, "timestamp"), 118f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 119877108e4SArnaldo Carvalho de Melo }; 120877108e4SArnaldo Carvalho de Melo 121877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent) 122877108e4SArnaldo Carvalho de Melo { 123877108e4SArnaldo Carvalho de Melo return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " 124877108e4SArnaldo Carvalho de Melo "ptid: %u, time: %" PRIu64 "}", 125877108e4SArnaldo Carvalho de Melo pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit", 126877108e4SArnaldo Carvalho de Melo pevent->event.fork.pid, 127877108e4SArnaldo Carvalho de Melo pevent->event.fork.ppid, 128877108e4SArnaldo Carvalho de Melo pevent->event.fork.tid, 129877108e4SArnaldo Carvalho de Melo pevent->event.fork.ptid, 130877108e4SArnaldo Carvalho de Melo pevent->event.fork.time); 131877108e4SArnaldo Carvalho de Melo } 132877108e4SArnaldo Carvalho de Melo 133877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_task_event__type = { 134877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 135877108e4SArnaldo Carvalho de Melo .tp_name = "perf.task_event", 136877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 137877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 138877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_task_event__doc, 139877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_task_event__members, 140877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_task_event__repr, 141877108e4SArnaldo Carvalho de Melo }; 142877108e4SArnaldo Carvalho de Melo 143877108e4SArnaldo Carvalho de Melo static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object."); 144877108e4SArnaldo Carvalho de Melo 145877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_comm_event__members[] = { 146877108e4SArnaldo Carvalho de Melo sample_members 147877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 148877108e4SArnaldo Carvalho de Melo member_def(comm_event, pid, T_UINT, "event pid"), 149877108e4SArnaldo Carvalho de Melo member_def(comm_event, tid, T_UINT, "event tid"), 150877108e4SArnaldo Carvalho de Melo member_def(comm_event, comm, T_STRING_INPLACE, "process name"), 151f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 152877108e4SArnaldo Carvalho de Melo }; 153877108e4SArnaldo Carvalho de Melo 154877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent) 155877108e4SArnaldo Carvalho de Melo { 156877108e4SArnaldo Carvalho de Melo return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", 157877108e4SArnaldo Carvalho de Melo pevent->event.comm.pid, 158877108e4SArnaldo Carvalho de Melo pevent->event.comm.tid, 159877108e4SArnaldo Carvalho de Melo pevent->event.comm.comm); 160877108e4SArnaldo Carvalho de Melo } 161877108e4SArnaldo Carvalho de Melo 162877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_comm_event__type = { 163877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 164877108e4SArnaldo Carvalho de Melo .tp_name = "perf.comm_event", 165877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 166877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 167877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_comm_event__doc, 168877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_comm_event__members, 169877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_comm_event__repr, 170877108e4SArnaldo Carvalho de Melo }; 171877108e4SArnaldo Carvalho de Melo 172877108e4SArnaldo Carvalho de Melo static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object."); 173877108e4SArnaldo Carvalho de Melo 174877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_throttle_event__members[] = { 175877108e4SArnaldo Carvalho de Melo sample_members 176877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 177f6bbc1daSArnaldo Carvalho de Melo member_def(throttle_event, time, T_ULONGLONG, "timestamp"), 178f6bbc1daSArnaldo Carvalho de Melo member_def(throttle_event, id, T_ULONGLONG, "event id"), 179f6bbc1daSArnaldo Carvalho de Melo member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"), 180f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 181877108e4SArnaldo Carvalho de Melo }; 182877108e4SArnaldo Carvalho de Melo 183877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent) 184877108e4SArnaldo Carvalho de Melo { 185877108e4SArnaldo Carvalho de Melo struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1); 186877108e4SArnaldo Carvalho de Melo 187877108e4SArnaldo Carvalho de Melo return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64 188877108e4SArnaldo Carvalho de Melo ", stream_id: %" PRIu64 " }", 189877108e4SArnaldo Carvalho de Melo pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un", 190877108e4SArnaldo Carvalho de Melo te->time, te->id, te->stream_id); 191877108e4SArnaldo Carvalho de Melo } 192877108e4SArnaldo Carvalho de Melo 193877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_throttle_event__type = { 194877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 195877108e4SArnaldo Carvalho de Melo .tp_name = "perf.throttle_event", 196877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 197877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 198877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_throttle_event__doc, 199877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_throttle_event__members, 200877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_throttle_event__repr, 201877108e4SArnaldo Carvalho de Melo }; 202877108e4SArnaldo Carvalho de Melo 2033e9f45a7SArnaldo Carvalho de Melo static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object."); 2043e9f45a7SArnaldo Carvalho de Melo 2053e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_lost_event__members[] = { 2063e9f45a7SArnaldo Carvalho de Melo sample_members 2073e9f45a7SArnaldo Carvalho de Melo member_def(lost_event, id, T_ULONGLONG, "event id"), 2083e9f45a7SArnaldo Carvalho de Melo member_def(lost_event, lost, T_ULONGLONG, "number of lost events"), 2093e9f45a7SArnaldo Carvalho de Melo { .name = NULL, }, 2103e9f45a7SArnaldo Carvalho de Melo }; 2113e9f45a7SArnaldo Carvalho de Melo 2123e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent) 2133e9f45a7SArnaldo Carvalho de Melo { 2143e9f45a7SArnaldo Carvalho de Melo PyObject *ret; 2153e9f45a7SArnaldo Carvalho de Melo char *s; 2163e9f45a7SArnaldo Carvalho de Melo 2173e9f45a7SArnaldo Carvalho de Melo if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", " 2183e9f45a7SArnaldo Carvalho de Melo "lost: %#" PRIx64 " }", 2193e9f45a7SArnaldo Carvalho de Melo pevent->event.lost.id, pevent->event.lost.lost) < 0) { 2203e9f45a7SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 2213e9f45a7SArnaldo Carvalho de Melo } else { 2223e9f45a7SArnaldo Carvalho de Melo ret = PyString_FromString(s); 2233e9f45a7SArnaldo Carvalho de Melo free(s); 2243e9f45a7SArnaldo Carvalho de Melo } 2253e9f45a7SArnaldo Carvalho de Melo return ret; 2263e9f45a7SArnaldo Carvalho de Melo } 2273e9f45a7SArnaldo Carvalho de Melo 2283e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_lost_event__type = { 2293e9f45a7SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 2303e9f45a7SArnaldo Carvalho de Melo .tp_name = "perf.lost_event", 2313e9f45a7SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 2323e9f45a7SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 2333e9f45a7SArnaldo Carvalho de Melo .tp_doc = pyrf_lost_event__doc, 2343e9f45a7SArnaldo Carvalho de Melo .tp_members = pyrf_lost_event__members, 2353e9f45a7SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_lost_event__repr, 2363e9f45a7SArnaldo Carvalho de Melo }; 2373e9f45a7SArnaldo Carvalho de Melo 2383e9f45a7SArnaldo Carvalho de Melo static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object."); 2393e9f45a7SArnaldo Carvalho de Melo 2403e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_read_event__members[] = { 2413e9f45a7SArnaldo Carvalho de Melo sample_members 2423e9f45a7SArnaldo Carvalho de Melo member_def(read_event, pid, T_UINT, "event pid"), 2433e9f45a7SArnaldo Carvalho de Melo member_def(read_event, tid, T_UINT, "event tid"), 2443e9f45a7SArnaldo Carvalho de Melo { .name = NULL, }, 2453e9f45a7SArnaldo Carvalho de Melo }; 2463e9f45a7SArnaldo Carvalho de Melo 2473e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent) 2483e9f45a7SArnaldo Carvalho de Melo { 2493e9f45a7SArnaldo Carvalho de Melo return PyString_FromFormat("{ type: read, pid: %u, tid: %u }", 2503e9f45a7SArnaldo Carvalho de Melo pevent->event.read.pid, 2513e9f45a7SArnaldo Carvalho de Melo pevent->event.read.tid); 2523e9f45a7SArnaldo Carvalho de Melo /* 2533e9f45a7SArnaldo Carvalho de Melo * FIXME: return the array of read values, 2543e9f45a7SArnaldo Carvalho de Melo * making this method useful ;-) 2553e9f45a7SArnaldo Carvalho de Melo */ 2563e9f45a7SArnaldo Carvalho de Melo } 2573e9f45a7SArnaldo Carvalho de Melo 2583e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_read_event__type = { 2593e9f45a7SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 2603e9f45a7SArnaldo Carvalho de Melo .tp_name = "perf.read_event", 2613e9f45a7SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 2623e9f45a7SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 2633e9f45a7SArnaldo Carvalho de Melo .tp_doc = pyrf_read_event__doc, 2643e9f45a7SArnaldo Carvalho de Melo .tp_members = pyrf_read_event__members, 2653e9f45a7SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_read_event__repr, 2663e9f45a7SArnaldo Carvalho de Melo }; 2673e9f45a7SArnaldo Carvalho de Melo 2683e9f45a7SArnaldo Carvalho de Melo static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object."); 2693e9f45a7SArnaldo Carvalho de Melo 2703e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_sample_event__members[] = { 2713e9f45a7SArnaldo Carvalho de Melo sample_members 2723e9f45a7SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 2733e9f45a7SArnaldo Carvalho de Melo { .name = NULL, }, 2743e9f45a7SArnaldo Carvalho de Melo }; 2753e9f45a7SArnaldo Carvalho de Melo 2763e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent) 2773e9f45a7SArnaldo Carvalho de Melo { 2783e9f45a7SArnaldo Carvalho de Melo PyObject *ret; 2793e9f45a7SArnaldo Carvalho de Melo char *s; 2803e9f45a7SArnaldo Carvalho de Melo 2813e9f45a7SArnaldo Carvalho de Melo if (asprintf(&s, "{ type: sample }") < 0) { 2823e9f45a7SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 2833e9f45a7SArnaldo Carvalho de Melo } else { 2843e9f45a7SArnaldo Carvalho de Melo ret = PyString_FromString(s); 2853e9f45a7SArnaldo Carvalho de Melo free(s); 2863e9f45a7SArnaldo Carvalho de Melo } 2873e9f45a7SArnaldo Carvalho de Melo return ret; 2883e9f45a7SArnaldo Carvalho de Melo } 2893e9f45a7SArnaldo Carvalho de Melo 2903e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_sample_event__type = { 2913e9f45a7SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 2923e9f45a7SArnaldo Carvalho de Melo .tp_name = "perf.sample_event", 2933e9f45a7SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 2943e9f45a7SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 2953e9f45a7SArnaldo Carvalho de Melo .tp_doc = pyrf_sample_event__doc, 2963e9f45a7SArnaldo Carvalho de Melo .tp_members = pyrf_sample_event__members, 2973e9f45a7SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_sample_event__repr, 2983e9f45a7SArnaldo Carvalho de Melo }; 2993e9f45a7SArnaldo Carvalho de Melo 300877108e4SArnaldo Carvalho de Melo static int pyrf_event__setup_types(void) 301877108e4SArnaldo Carvalho de Melo { 302877108e4SArnaldo Carvalho de Melo int err; 303877108e4SArnaldo Carvalho de Melo pyrf_mmap_event__type.tp_new = 304877108e4SArnaldo Carvalho de Melo pyrf_task_event__type.tp_new = 305877108e4SArnaldo Carvalho de Melo pyrf_comm_event__type.tp_new = 3063e9f45a7SArnaldo Carvalho de Melo pyrf_lost_event__type.tp_new = 3073e9f45a7SArnaldo Carvalho de Melo pyrf_read_event__type.tp_new = 3083e9f45a7SArnaldo Carvalho de Melo pyrf_sample_event__type.tp_new = 309877108e4SArnaldo Carvalho de Melo pyrf_throttle_event__type.tp_new = PyType_GenericNew; 310877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_mmap_event__type); 311877108e4SArnaldo Carvalho de Melo if (err < 0) 312877108e4SArnaldo Carvalho de Melo goto out; 3133e9f45a7SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_lost_event__type); 3143e9f45a7SArnaldo Carvalho de Melo if (err < 0) 3153e9f45a7SArnaldo Carvalho de Melo goto out; 316877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_task_event__type); 317877108e4SArnaldo Carvalho de Melo if (err < 0) 318877108e4SArnaldo Carvalho de Melo goto out; 319877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_comm_event__type); 320877108e4SArnaldo Carvalho de Melo if (err < 0) 321877108e4SArnaldo Carvalho de Melo goto out; 322877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_throttle_event__type); 323877108e4SArnaldo Carvalho de Melo if (err < 0) 324877108e4SArnaldo Carvalho de Melo goto out; 3253e9f45a7SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_read_event__type); 3263e9f45a7SArnaldo Carvalho de Melo if (err < 0) 3273e9f45a7SArnaldo Carvalho de Melo goto out; 3283e9f45a7SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_sample_event__type); 3293e9f45a7SArnaldo Carvalho de Melo if (err < 0) 3303e9f45a7SArnaldo Carvalho de Melo goto out; 331877108e4SArnaldo Carvalho de Melo out: 332877108e4SArnaldo Carvalho de Melo return err; 333877108e4SArnaldo Carvalho de Melo } 334877108e4SArnaldo Carvalho de Melo 335877108e4SArnaldo Carvalho de Melo static PyTypeObject *pyrf_event__type[] = { 336877108e4SArnaldo Carvalho de Melo [PERF_RECORD_MMAP] = &pyrf_mmap_event__type, 3373e9f45a7SArnaldo Carvalho de Melo [PERF_RECORD_LOST] = &pyrf_lost_event__type, 338877108e4SArnaldo Carvalho de Melo [PERF_RECORD_COMM] = &pyrf_comm_event__type, 339877108e4SArnaldo Carvalho de Melo [PERF_RECORD_EXIT] = &pyrf_task_event__type, 340877108e4SArnaldo Carvalho de Melo [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type, 341877108e4SArnaldo Carvalho de Melo [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type, 342877108e4SArnaldo Carvalho de Melo [PERF_RECORD_FORK] = &pyrf_task_event__type, 3433e9f45a7SArnaldo Carvalho de Melo [PERF_RECORD_READ] = &pyrf_read_event__type, 3443e9f45a7SArnaldo Carvalho de Melo [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type, 345877108e4SArnaldo Carvalho de Melo }; 346877108e4SArnaldo Carvalho de Melo 347877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_event__new(union perf_event *event) 348877108e4SArnaldo Carvalho de Melo { 349877108e4SArnaldo Carvalho de Melo struct pyrf_event *pevent; 350877108e4SArnaldo Carvalho de Melo PyTypeObject *ptype; 351877108e4SArnaldo Carvalho de Melo 352877108e4SArnaldo Carvalho de Melo if (event->header.type < PERF_RECORD_MMAP || 353877108e4SArnaldo Carvalho de Melo event->header.type > PERF_RECORD_SAMPLE) 354877108e4SArnaldo Carvalho de Melo return NULL; 355877108e4SArnaldo Carvalho de Melo 356877108e4SArnaldo Carvalho de Melo ptype = pyrf_event__type[event->header.type]; 357877108e4SArnaldo Carvalho de Melo pevent = PyObject_New(struct pyrf_event, ptype); 358877108e4SArnaldo Carvalho de Melo if (pevent != NULL) 359877108e4SArnaldo Carvalho de Melo memcpy(&pevent->event, event, event->header.size); 360877108e4SArnaldo Carvalho de Melo return (PyObject *)pevent; 361877108e4SArnaldo Carvalho de Melo } 362877108e4SArnaldo Carvalho de Melo 363877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map { 364877108e4SArnaldo Carvalho de Melo PyObject_HEAD 365877108e4SArnaldo Carvalho de Melo 366877108e4SArnaldo Carvalho de Melo struct cpu_map *cpus; 367877108e4SArnaldo Carvalho de Melo }; 368877108e4SArnaldo Carvalho de Melo 369877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus, 370877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 371877108e4SArnaldo Carvalho de Melo { 37264348153SFrederic Weisbecker static char *kwlist[] = { "cpustr", NULL }; 373877108e4SArnaldo Carvalho de Melo char *cpustr = NULL; 374877108e4SArnaldo Carvalho de Melo 375877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", 376877108e4SArnaldo Carvalho de Melo kwlist, &cpustr)) 377877108e4SArnaldo Carvalho de Melo return -1; 378877108e4SArnaldo Carvalho de Melo 379877108e4SArnaldo Carvalho de Melo pcpus->cpus = cpu_map__new(cpustr); 380877108e4SArnaldo Carvalho de Melo if (pcpus->cpus == NULL) 381877108e4SArnaldo Carvalho de Melo return -1; 382877108e4SArnaldo Carvalho de Melo return 0; 383877108e4SArnaldo Carvalho de Melo } 384877108e4SArnaldo Carvalho de Melo 385877108e4SArnaldo Carvalho de Melo static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus) 386877108e4SArnaldo Carvalho de Melo { 387877108e4SArnaldo Carvalho de Melo cpu_map__delete(pcpus->cpus); 388877108e4SArnaldo Carvalho de Melo pcpus->ob_type->tp_free((PyObject*)pcpus); 389877108e4SArnaldo Carvalho de Melo } 390877108e4SArnaldo Carvalho de Melo 391877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_cpu_map__length(PyObject *obj) 392877108e4SArnaldo Carvalho de Melo { 393877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map *pcpus = (void *)obj; 394877108e4SArnaldo Carvalho de Melo 395877108e4SArnaldo Carvalho de Melo return pcpus->cpus->nr; 396877108e4SArnaldo Carvalho de Melo } 397877108e4SArnaldo Carvalho de Melo 398877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i) 399877108e4SArnaldo Carvalho de Melo { 400877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map *pcpus = (void *)obj; 401877108e4SArnaldo Carvalho de Melo 402877108e4SArnaldo Carvalho de Melo if (i >= pcpus->cpus->nr) 403877108e4SArnaldo Carvalho de Melo return NULL; 404877108e4SArnaldo Carvalho de Melo 405877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", pcpus->cpus->map[i]); 406877108e4SArnaldo Carvalho de Melo } 407877108e4SArnaldo Carvalho de Melo 408877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_cpu_map__sequence_methods = { 409877108e4SArnaldo Carvalho de Melo .sq_length = pyrf_cpu_map__length, 410877108e4SArnaldo Carvalho de Melo .sq_item = pyrf_cpu_map__item, 411877108e4SArnaldo Carvalho de Melo }; 412877108e4SArnaldo Carvalho de Melo 413877108e4SArnaldo Carvalho de Melo static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object."); 414877108e4SArnaldo Carvalho de Melo 415877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_cpu_map__type = { 416877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 417877108e4SArnaldo Carvalho de Melo .tp_name = "perf.cpu_map", 418877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_cpu_map), 419877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_cpu_map__delete, 420877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 421877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_cpu_map__doc, 422877108e4SArnaldo Carvalho de Melo .tp_as_sequence = &pyrf_cpu_map__sequence_methods, 423877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_cpu_map__init, 424877108e4SArnaldo Carvalho de Melo }; 425877108e4SArnaldo Carvalho de Melo 426877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__setup_types(void) 427877108e4SArnaldo Carvalho de Melo { 428877108e4SArnaldo Carvalho de Melo pyrf_cpu_map__type.tp_new = PyType_GenericNew; 429877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_cpu_map__type); 430877108e4SArnaldo Carvalho de Melo } 431877108e4SArnaldo Carvalho de Melo 432877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map { 433877108e4SArnaldo Carvalho de Melo PyObject_HEAD 434877108e4SArnaldo Carvalho de Melo 435877108e4SArnaldo Carvalho de Melo struct thread_map *threads; 436877108e4SArnaldo Carvalho de Melo }; 437877108e4SArnaldo Carvalho de Melo 438877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads, 439877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 440877108e4SArnaldo Carvalho de Melo { 4410d37aa34SArnaldo Carvalho de Melo static char *kwlist[] = { "pid", "tid", "uid", NULL }; 4420d37aa34SArnaldo Carvalho de Melo int pid = -1, tid = -1, uid = UINT_MAX; 443877108e4SArnaldo Carvalho de Melo 4440d37aa34SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii", 4450d37aa34SArnaldo Carvalho de Melo kwlist, &pid, &tid, &uid)) 446877108e4SArnaldo Carvalho de Melo return -1; 447877108e4SArnaldo Carvalho de Melo 4480d37aa34SArnaldo Carvalho de Melo pthreads->threads = thread_map__new(pid, tid, uid); 449877108e4SArnaldo Carvalho de Melo if (pthreads->threads == NULL) 450877108e4SArnaldo Carvalho de Melo return -1; 451877108e4SArnaldo Carvalho de Melo return 0; 452877108e4SArnaldo Carvalho de Melo } 453877108e4SArnaldo Carvalho de Melo 454877108e4SArnaldo Carvalho de Melo static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads) 455877108e4SArnaldo Carvalho de Melo { 456877108e4SArnaldo Carvalho de Melo thread_map__delete(pthreads->threads); 457877108e4SArnaldo Carvalho de Melo pthreads->ob_type->tp_free((PyObject*)pthreads); 458877108e4SArnaldo Carvalho de Melo } 459877108e4SArnaldo Carvalho de Melo 460877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_thread_map__length(PyObject *obj) 461877108e4SArnaldo Carvalho de Melo { 462877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map *pthreads = (void *)obj; 463877108e4SArnaldo Carvalho de Melo 464877108e4SArnaldo Carvalho de Melo return pthreads->threads->nr; 465877108e4SArnaldo Carvalho de Melo } 466877108e4SArnaldo Carvalho de Melo 467877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i) 468877108e4SArnaldo Carvalho de Melo { 469877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map *pthreads = (void *)obj; 470877108e4SArnaldo Carvalho de Melo 471877108e4SArnaldo Carvalho de Melo if (i >= pthreads->threads->nr) 472877108e4SArnaldo Carvalho de Melo return NULL; 473877108e4SArnaldo Carvalho de Melo 474877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", pthreads->threads->map[i]); 475877108e4SArnaldo Carvalho de Melo } 476877108e4SArnaldo Carvalho de Melo 477877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_thread_map__sequence_methods = { 478877108e4SArnaldo Carvalho de Melo .sq_length = pyrf_thread_map__length, 479877108e4SArnaldo Carvalho de Melo .sq_item = pyrf_thread_map__item, 480877108e4SArnaldo Carvalho de Melo }; 481877108e4SArnaldo Carvalho de Melo 482877108e4SArnaldo Carvalho de Melo static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object."); 483877108e4SArnaldo Carvalho de Melo 484877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_thread_map__type = { 485877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 486877108e4SArnaldo Carvalho de Melo .tp_name = "perf.thread_map", 487877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_thread_map), 488877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_thread_map__delete, 489877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 490877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_thread_map__doc, 491877108e4SArnaldo Carvalho de Melo .tp_as_sequence = &pyrf_thread_map__sequence_methods, 492877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_thread_map__init, 493877108e4SArnaldo Carvalho de Melo }; 494877108e4SArnaldo Carvalho de Melo 495877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__setup_types(void) 496877108e4SArnaldo Carvalho de Melo { 497877108e4SArnaldo Carvalho de Melo pyrf_thread_map__type.tp_new = PyType_GenericNew; 498877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_thread_map__type); 499877108e4SArnaldo Carvalho de Melo } 500877108e4SArnaldo Carvalho de Melo 501877108e4SArnaldo Carvalho de Melo struct pyrf_evsel { 502877108e4SArnaldo Carvalho de Melo PyObject_HEAD 503877108e4SArnaldo Carvalho de Melo 504877108e4SArnaldo Carvalho de Melo struct perf_evsel evsel; 505877108e4SArnaldo Carvalho de Melo }; 506877108e4SArnaldo Carvalho de Melo 507877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__init(struct pyrf_evsel *pevsel, 508877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 509877108e4SArnaldo Carvalho de Melo { 510877108e4SArnaldo Carvalho de Melo struct perf_event_attr attr = { 511877108e4SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 512877108e4SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 513877108e4SArnaldo Carvalho de Melo .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID, 514877108e4SArnaldo Carvalho de Melo }; 515877108e4SArnaldo Carvalho de Melo static char *kwlist[] = { 516877108e4SArnaldo Carvalho de Melo "type", 517877108e4SArnaldo Carvalho de Melo "config", 518877108e4SArnaldo Carvalho de Melo "sample_freq", 519877108e4SArnaldo Carvalho de Melo "sample_period", 520877108e4SArnaldo Carvalho de Melo "sample_type", 521877108e4SArnaldo Carvalho de Melo "read_format", 522877108e4SArnaldo Carvalho de Melo "disabled", 523877108e4SArnaldo Carvalho de Melo "inherit", 524877108e4SArnaldo Carvalho de Melo "pinned", 525877108e4SArnaldo Carvalho de Melo "exclusive", 526877108e4SArnaldo Carvalho de Melo "exclude_user", 527877108e4SArnaldo Carvalho de Melo "exclude_kernel", 528877108e4SArnaldo Carvalho de Melo "exclude_hv", 529877108e4SArnaldo Carvalho de Melo "exclude_idle", 530877108e4SArnaldo Carvalho de Melo "mmap", 531877108e4SArnaldo Carvalho de Melo "comm", 532877108e4SArnaldo Carvalho de Melo "freq", 533877108e4SArnaldo Carvalho de Melo "inherit_stat", 534877108e4SArnaldo Carvalho de Melo "enable_on_exec", 535877108e4SArnaldo Carvalho de Melo "task", 536877108e4SArnaldo Carvalho de Melo "watermark", 537877108e4SArnaldo Carvalho de Melo "precise_ip", 538877108e4SArnaldo Carvalho de Melo "mmap_data", 539877108e4SArnaldo Carvalho de Melo "sample_id_all", 540877108e4SArnaldo Carvalho de Melo "wakeup_events", 541877108e4SArnaldo Carvalho de Melo "bp_type", 542877108e4SArnaldo Carvalho de Melo "bp_addr", 54364348153SFrederic Weisbecker "bp_len", 54464348153SFrederic Weisbecker NULL 54564348153SFrederic Weisbecker }; 546877108e4SArnaldo Carvalho de Melo u64 sample_period = 0; 547877108e4SArnaldo Carvalho de Melo u32 disabled = 0, 548877108e4SArnaldo Carvalho de Melo inherit = 0, 549877108e4SArnaldo Carvalho de Melo pinned = 0, 550877108e4SArnaldo Carvalho de Melo exclusive = 0, 551877108e4SArnaldo Carvalho de Melo exclude_user = 0, 552877108e4SArnaldo Carvalho de Melo exclude_kernel = 0, 553877108e4SArnaldo Carvalho de Melo exclude_hv = 0, 554877108e4SArnaldo Carvalho de Melo exclude_idle = 0, 555877108e4SArnaldo Carvalho de Melo mmap = 0, 556877108e4SArnaldo Carvalho de Melo comm = 0, 557877108e4SArnaldo Carvalho de Melo freq = 1, 558877108e4SArnaldo Carvalho de Melo inherit_stat = 0, 559877108e4SArnaldo Carvalho de Melo enable_on_exec = 0, 560877108e4SArnaldo Carvalho de Melo task = 0, 561877108e4SArnaldo Carvalho de Melo watermark = 0, 562877108e4SArnaldo Carvalho de Melo precise_ip = 0, 563877108e4SArnaldo Carvalho de Melo mmap_data = 0, 564877108e4SArnaldo Carvalho de Melo sample_id_all = 1; 565877108e4SArnaldo Carvalho de Melo int idx = 0; 566877108e4SArnaldo Carvalho de Melo 567877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, 568877108e4SArnaldo Carvalho de Melo "|iKiKKiiiiiiiiiiiiiiiiiiiiiKK", kwlist, 569877108e4SArnaldo Carvalho de Melo &attr.type, &attr.config, &attr.sample_freq, 570877108e4SArnaldo Carvalho de Melo &sample_period, &attr.sample_type, 571877108e4SArnaldo Carvalho de Melo &attr.read_format, &disabled, &inherit, 572877108e4SArnaldo Carvalho de Melo &pinned, &exclusive, &exclude_user, 573877108e4SArnaldo Carvalho de Melo &exclude_kernel, &exclude_hv, &exclude_idle, 574877108e4SArnaldo Carvalho de Melo &mmap, &comm, &freq, &inherit_stat, 575877108e4SArnaldo Carvalho de Melo &enable_on_exec, &task, &watermark, 576877108e4SArnaldo Carvalho de Melo &precise_ip, &mmap_data, &sample_id_all, 577877108e4SArnaldo Carvalho de Melo &attr.wakeup_events, &attr.bp_type, 578877108e4SArnaldo Carvalho de Melo &attr.bp_addr, &attr.bp_len, &idx)) 579877108e4SArnaldo Carvalho de Melo return -1; 580877108e4SArnaldo Carvalho de Melo 581877108e4SArnaldo Carvalho de Melo /* union... */ 582877108e4SArnaldo Carvalho de Melo if (sample_period != 0) { 583877108e4SArnaldo Carvalho de Melo if (attr.sample_freq != 0) 584877108e4SArnaldo Carvalho de Melo return -1; /* FIXME: throw right exception */ 585877108e4SArnaldo Carvalho de Melo attr.sample_period = sample_period; 586877108e4SArnaldo Carvalho de Melo } 587877108e4SArnaldo Carvalho de Melo 588877108e4SArnaldo Carvalho de Melo /* Bitfields */ 589877108e4SArnaldo Carvalho de Melo attr.disabled = disabled; 590877108e4SArnaldo Carvalho de Melo attr.inherit = inherit; 591877108e4SArnaldo Carvalho de Melo attr.pinned = pinned; 592877108e4SArnaldo Carvalho de Melo attr.exclusive = exclusive; 593877108e4SArnaldo Carvalho de Melo attr.exclude_user = exclude_user; 594877108e4SArnaldo Carvalho de Melo attr.exclude_kernel = exclude_kernel; 595877108e4SArnaldo Carvalho de Melo attr.exclude_hv = exclude_hv; 596877108e4SArnaldo Carvalho de Melo attr.exclude_idle = exclude_idle; 597877108e4SArnaldo Carvalho de Melo attr.mmap = mmap; 598877108e4SArnaldo Carvalho de Melo attr.comm = comm; 599877108e4SArnaldo Carvalho de Melo attr.freq = freq; 600877108e4SArnaldo Carvalho de Melo attr.inherit_stat = inherit_stat; 601877108e4SArnaldo Carvalho de Melo attr.enable_on_exec = enable_on_exec; 602877108e4SArnaldo Carvalho de Melo attr.task = task; 603877108e4SArnaldo Carvalho de Melo attr.watermark = watermark; 604877108e4SArnaldo Carvalho de Melo attr.precise_ip = precise_ip; 605877108e4SArnaldo Carvalho de Melo attr.mmap_data = mmap_data; 606877108e4SArnaldo Carvalho de Melo attr.sample_id_all = sample_id_all; 607877108e4SArnaldo Carvalho de Melo 608877108e4SArnaldo Carvalho de Melo perf_evsel__init(&pevsel->evsel, &attr, idx); 609877108e4SArnaldo Carvalho de Melo return 0; 610877108e4SArnaldo Carvalho de Melo } 611877108e4SArnaldo Carvalho de Melo 612877108e4SArnaldo Carvalho de Melo static void pyrf_evsel__delete(struct pyrf_evsel *pevsel) 613877108e4SArnaldo Carvalho de Melo { 614877108e4SArnaldo Carvalho de Melo perf_evsel__exit(&pevsel->evsel); 615877108e4SArnaldo Carvalho de Melo pevsel->ob_type->tp_free((PyObject*)pevsel); 616877108e4SArnaldo Carvalho de Melo } 617877108e4SArnaldo Carvalho de Melo 618877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, 619877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 620877108e4SArnaldo Carvalho de Melo { 621877108e4SArnaldo Carvalho de Melo struct perf_evsel *evsel = &pevsel->evsel; 622877108e4SArnaldo Carvalho de Melo struct cpu_map *cpus = NULL; 623877108e4SArnaldo Carvalho de Melo struct thread_map *threads = NULL; 624877108e4SArnaldo Carvalho de Melo PyObject *pcpus = NULL, *pthreads = NULL; 6255d2cd909SArnaldo Carvalho de Melo int group = 0, inherit = 0; 62664348153SFrederic Weisbecker static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL }; 627877108e4SArnaldo Carvalho de Melo 628877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, 6295d2cd909SArnaldo Carvalho de Melo &pcpus, &pthreads, &group, &inherit)) 630877108e4SArnaldo Carvalho de Melo return NULL; 631877108e4SArnaldo Carvalho de Melo 632877108e4SArnaldo Carvalho de Melo if (pthreads != NULL) 633877108e4SArnaldo Carvalho de Melo threads = ((struct pyrf_thread_map *)pthreads)->threads; 634877108e4SArnaldo Carvalho de Melo 635877108e4SArnaldo Carvalho de Melo if (pcpus != NULL) 636877108e4SArnaldo Carvalho de Melo cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; 637877108e4SArnaldo Carvalho de Melo 6385d2cd909SArnaldo Carvalho de Melo evsel->attr.inherit = inherit; 639727ab04eSArnaldo Carvalho de Melo /* 640727ab04eSArnaldo Carvalho de Melo * This will group just the fds for this single evsel, to group 641727ab04eSArnaldo Carvalho de Melo * multiple events, use evlist.open(). 642727ab04eSArnaldo Carvalho de Melo */ 6436a4bb04cSJiri Olsa if (perf_evsel__open(evsel, cpus, threads) < 0) { 644877108e4SArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 645877108e4SArnaldo Carvalho de Melo return NULL; 646877108e4SArnaldo Carvalho de Melo } 647877108e4SArnaldo Carvalho de Melo 648877108e4SArnaldo Carvalho de Melo Py_INCREF(Py_None); 649877108e4SArnaldo Carvalho de Melo return Py_None; 650877108e4SArnaldo Carvalho de Melo } 651877108e4SArnaldo Carvalho de Melo 652877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evsel__methods[] = { 653877108e4SArnaldo Carvalho de Melo { 654877108e4SArnaldo Carvalho de Melo .ml_name = "open", 655877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evsel__open, 656877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 657877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("open the event selector file descriptor table.") 658877108e4SArnaldo Carvalho de Melo }, 659f6bbc1daSArnaldo Carvalho de Melo { .ml_name = NULL, } 660877108e4SArnaldo Carvalho de Melo }; 661877108e4SArnaldo Carvalho de Melo 662877108e4SArnaldo Carvalho de Melo static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object."); 663877108e4SArnaldo Carvalho de Melo 664877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evsel__type = { 665877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 666877108e4SArnaldo Carvalho de Melo .tp_name = "perf.evsel", 667877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_evsel), 668877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_evsel__delete, 669877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 670877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_evsel__doc, 671877108e4SArnaldo Carvalho de Melo .tp_methods = pyrf_evsel__methods, 672877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_evsel__init, 673877108e4SArnaldo Carvalho de Melo }; 674877108e4SArnaldo Carvalho de Melo 675877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__setup_types(void) 676877108e4SArnaldo Carvalho de Melo { 677877108e4SArnaldo Carvalho de Melo pyrf_evsel__type.tp_new = PyType_GenericNew; 678877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_evsel__type); 679877108e4SArnaldo Carvalho de Melo } 680877108e4SArnaldo Carvalho de Melo 681877108e4SArnaldo Carvalho de Melo struct pyrf_evlist { 682877108e4SArnaldo Carvalho de Melo PyObject_HEAD 683877108e4SArnaldo Carvalho de Melo 684877108e4SArnaldo Carvalho de Melo struct perf_evlist evlist; 685877108e4SArnaldo Carvalho de Melo }; 686877108e4SArnaldo Carvalho de Melo 687877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__init(struct pyrf_evlist *pevlist, 6881d037ca1SIrina Tirdea PyObject *args, PyObject *kwargs __maybe_unused) 689877108e4SArnaldo Carvalho de Melo { 6907e2ed097SArnaldo Carvalho de Melo PyObject *pcpus = NULL, *pthreads = NULL; 6917e2ed097SArnaldo Carvalho de Melo struct cpu_map *cpus; 6927e2ed097SArnaldo Carvalho de Melo struct thread_map *threads; 6937e2ed097SArnaldo Carvalho de Melo 6947e2ed097SArnaldo Carvalho de Melo if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads)) 6957e2ed097SArnaldo Carvalho de Melo return -1; 6967e2ed097SArnaldo Carvalho de Melo 6977e2ed097SArnaldo Carvalho de Melo threads = ((struct pyrf_thread_map *)pthreads)->threads; 6987e2ed097SArnaldo Carvalho de Melo cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; 6997e2ed097SArnaldo Carvalho de Melo perf_evlist__init(&pevlist->evlist, cpus, threads); 700877108e4SArnaldo Carvalho de Melo return 0; 701877108e4SArnaldo Carvalho de Melo } 702877108e4SArnaldo Carvalho de Melo 703877108e4SArnaldo Carvalho de Melo static void pyrf_evlist__delete(struct pyrf_evlist *pevlist) 704877108e4SArnaldo Carvalho de Melo { 705877108e4SArnaldo Carvalho de Melo perf_evlist__exit(&pevlist->evlist); 706877108e4SArnaldo Carvalho de Melo pevlist->ob_type->tp_free((PyObject*)pevlist); 707877108e4SArnaldo Carvalho de Melo } 708877108e4SArnaldo Carvalho de Melo 709877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, 710877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 711877108e4SArnaldo Carvalho de Melo { 712877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 71364348153SFrederic Weisbecker static char *kwlist[] = { "pages", "overwrite", NULL }; 714877108e4SArnaldo Carvalho de Melo int pages = 128, overwrite = false; 715877108e4SArnaldo Carvalho de Melo 7167e2ed097SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist, 7177e2ed097SArnaldo Carvalho de Melo &pages, &overwrite)) 718877108e4SArnaldo Carvalho de Melo return NULL; 719877108e4SArnaldo Carvalho de Melo 7207e2ed097SArnaldo Carvalho de Melo if (perf_evlist__mmap(evlist, pages, overwrite) < 0) { 721877108e4SArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 722877108e4SArnaldo Carvalho de Melo return NULL; 723877108e4SArnaldo Carvalho de Melo } 724877108e4SArnaldo Carvalho de Melo 725877108e4SArnaldo Carvalho de Melo Py_INCREF(Py_None); 726877108e4SArnaldo Carvalho de Melo return Py_None; 727877108e4SArnaldo Carvalho de Melo } 728877108e4SArnaldo Carvalho de Melo 729877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist, 730877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 731877108e4SArnaldo Carvalho de Melo { 732877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 73364348153SFrederic Weisbecker static char *kwlist[] = { "timeout", NULL }; 734877108e4SArnaldo Carvalho de Melo int timeout = -1, n; 735877108e4SArnaldo Carvalho de Melo 736877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout)) 737877108e4SArnaldo Carvalho de Melo return NULL; 738877108e4SArnaldo Carvalho de Melo 739877108e4SArnaldo Carvalho de Melo n = poll(evlist->pollfd, evlist->nr_fds, timeout); 740877108e4SArnaldo Carvalho de Melo if (n < 0) { 741877108e4SArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 742877108e4SArnaldo Carvalho de Melo return NULL; 743877108e4SArnaldo Carvalho de Melo } 744877108e4SArnaldo Carvalho de Melo 745877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", n); 746877108e4SArnaldo Carvalho de Melo } 747877108e4SArnaldo Carvalho de Melo 748877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist, 7491d037ca1SIrina Tirdea PyObject *args __maybe_unused, 7501d037ca1SIrina Tirdea PyObject *kwargs __maybe_unused) 751877108e4SArnaldo Carvalho de Melo { 752877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 753877108e4SArnaldo Carvalho de Melo PyObject *list = PyList_New(0); 754877108e4SArnaldo Carvalho de Melo int i; 755877108e4SArnaldo Carvalho de Melo 756877108e4SArnaldo Carvalho de Melo for (i = 0; i < evlist->nr_fds; ++i) { 757877108e4SArnaldo Carvalho de Melo PyObject *file; 758877108e4SArnaldo Carvalho de Melo FILE *fp = fdopen(evlist->pollfd[i].fd, "r"); 759877108e4SArnaldo Carvalho de Melo 760877108e4SArnaldo Carvalho de Melo if (fp == NULL) 761877108e4SArnaldo Carvalho de Melo goto free_list; 762877108e4SArnaldo Carvalho de Melo 763877108e4SArnaldo Carvalho de Melo file = PyFile_FromFile(fp, "perf", "r", NULL); 764877108e4SArnaldo Carvalho de Melo if (file == NULL) 765877108e4SArnaldo Carvalho de Melo goto free_list; 766877108e4SArnaldo Carvalho de Melo 767877108e4SArnaldo Carvalho de Melo if (PyList_Append(list, file) != 0) { 768877108e4SArnaldo Carvalho de Melo Py_DECREF(file); 769877108e4SArnaldo Carvalho de Melo goto free_list; 770877108e4SArnaldo Carvalho de Melo } 771877108e4SArnaldo Carvalho de Melo 772877108e4SArnaldo Carvalho de Melo Py_DECREF(file); 773877108e4SArnaldo Carvalho de Melo } 774877108e4SArnaldo Carvalho de Melo 775877108e4SArnaldo Carvalho de Melo return list; 776877108e4SArnaldo Carvalho de Melo free_list: 777877108e4SArnaldo Carvalho de Melo return PyErr_NoMemory(); 778877108e4SArnaldo Carvalho de Melo } 779877108e4SArnaldo Carvalho de Melo 780877108e4SArnaldo Carvalho de Melo 781877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist, 7821d037ca1SIrina Tirdea PyObject *args, 7831d037ca1SIrina Tirdea PyObject *kwargs __maybe_unused) 784877108e4SArnaldo Carvalho de Melo { 785877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 786877108e4SArnaldo Carvalho de Melo PyObject *pevsel; 787877108e4SArnaldo Carvalho de Melo struct perf_evsel *evsel; 788877108e4SArnaldo Carvalho de Melo 789877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTuple(args, "O", &pevsel)) 790877108e4SArnaldo Carvalho de Melo return NULL; 791877108e4SArnaldo Carvalho de Melo 792877108e4SArnaldo Carvalho de Melo Py_INCREF(pevsel); 793877108e4SArnaldo Carvalho de Melo evsel = &((struct pyrf_evsel *)pevsel)->evsel; 794877108e4SArnaldo Carvalho de Melo evsel->idx = evlist->nr_entries; 795877108e4SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 796877108e4SArnaldo Carvalho de Melo 797877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", evlist->nr_entries); 798877108e4SArnaldo Carvalho de Melo } 799877108e4SArnaldo Carvalho de Melo 800877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, 801877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 802877108e4SArnaldo Carvalho de Melo { 803877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 804877108e4SArnaldo Carvalho de Melo union perf_event *event; 805877108e4SArnaldo Carvalho de Melo int sample_id_all = 1, cpu; 80664348153SFrederic Weisbecker static char *kwlist[] = { "cpu", "sample_id_all", NULL }; 8075538becaSFrederic Weisbecker int err; 808877108e4SArnaldo Carvalho de Melo 809877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, 810877108e4SArnaldo Carvalho de Melo &cpu, &sample_id_all)) 811877108e4SArnaldo Carvalho de Melo return NULL; 812877108e4SArnaldo Carvalho de Melo 813aece948fSArnaldo Carvalho de Melo event = perf_evlist__mmap_read(evlist, cpu); 814877108e4SArnaldo Carvalho de Melo if (event != NULL) { 815877108e4SArnaldo Carvalho de Melo PyObject *pyevent = pyrf_event__new(event); 816877108e4SArnaldo Carvalho de Melo struct pyrf_event *pevent = (struct pyrf_event *)pyevent; 817877108e4SArnaldo Carvalho de Melo 8188e50d384SZhouyi Zhou perf_evlist__mmap_consume(evlist, cpu); 8198e50d384SZhouyi Zhou 820877108e4SArnaldo Carvalho de Melo if (pyevent == NULL) 821877108e4SArnaldo Carvalho de Melo return PyErr_NoMemory(); 822877108e4SArnaldo Carvalho de Melo 8230807d2d8SArnaldo Carvalho de Melo err = perf_evlist__parse_sample(evlist, event, &pevent->sample); 8245c6970afSArnaldo Carvalho de Melo if (err) 8255c6970afSArnaldo Carvalho de Melo return PyErr_Format(PyExc_OSError, 8265c6970afSArnaldo Carvalho de Melo "perf: can't parse sample, err=%d", err); 8275538becaSFrederic Weisbecker return pyevent; 8285538becaSFrederic Weisbecker } 8295c6970afSArnaldo Carvalho de Melo 830877108e4SArnaldo Carvalho de Melo Py_INCREF(Py_None); 831877108e4SArnaldo Carvalho de Melo return Py_None; 832877108e4SArnaldo Carvalho de Melo } 833877108e4SArnaldo Carvalho de Melo 834727ab04eSArnaldo Carvalho de Melo static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist, 835727ab04eSArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 836727ab04eSArnaldo Carvalho de Melo { 837727ab04eSArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 838727ab04eSArnaldo Carvalho de Melo int group = 0; 839727ab04eSArnaldo Carvalho de Melo static char *kwlist[] = { "group", NULL }; 840727ab04eSArnaldo Carvalho de Melo 841727ab04eSArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group)) 842727ab04eSArnaldo Carvalho de Melo return NULL; 843727ab04eSArnaldo Carvalho de Melo 8446a4bb04cSJiri Olsa if (group) 84563dab225SArnaldo Carvalho de Melo perf_evlist__set_leader(evlist); 8466a4bb04cSJiri Olsa 8476a4bb04cSJiri Olsa if (perf_evlist__open(evlist) < 0) { 848727ab04eSArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 849727ab04eSArnaldo Carvalho de Melo return NULL; 850727ab04eSArnaldo Carvalho de Melo } 851727ab04eSArnaldo Carvalho de Melo 852727ab04eSArnaldo Carvalho de Melo Py_INCREF(Py_None); 853727ab04eSArnaldo Carvalho de Melo return Py_None; 854727ab04eSArnaldo Carvalho de Melo } 855727ab04eSArnaldo Carvalho de Melo 856877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evlist__methods[] = { 857877108e4SArnaldo Carvalho de Melo { 858877108e4SArnaldo Carvalho de Melo .ml_name = "mmap", 859877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__mmap, 860877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 861877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("mmap the file descriptor table.") 862877108e4SArnaldo Carvalho de Melo }, 863877108e4SArnaldo Carvalho de Melo { 864727ab04eSArnaldo Carvalho de Melo .ml_name = "open", 865727ab04eSArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__open, 866727ab04eSArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 867727ab04eSArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("open the file descriptors.") 868727ab04eSArnaldo Carvalho de Melo }, 869727ab04eSArnaldo Carvalho de Melo { 870877108e4SArnaldo Carvalho de Melo .ml_name = "poll", 871877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__poll, 872877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 873877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("poll the file descriptor table.") 874877108e4SArnaldo Carvalho de Melo }, 875877108e4SArnaldo Carvalho de Melo { 876877108e4SArnaldo Carvalho de Melo .ml_name = "get_pollfd", 877877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd, 878877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 879877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("get the poll file descriptor table.") 880877108e4SArnaldo Carvalho de Melo }, 881877108e4SArnaldo Carvalho de Melo { 882877108e4SArnaldo Carvalho de Melo .ml_name = "add", 883877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__add, 884877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 885877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("adds an event selector to the list.") 886877108e4SArnaldo Carvalho de Melo }, 887877108e4SArnaldo Carvalho de Melo { 888877108e4SArnaldo Carvalho de Melo .ml_name = "read_on_cpu", 889877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu, 890877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 891877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("reads an event.") 892877108e4SArnaldo Carvalho de Melo }, 893f6bbc1daSArnaldo Carvalho de Melo { .ml_name = NULL, } 894877108e4SArnaldo Carvalho de Melo }; 895877108e4SArnaldo Carvalho de Melo 896877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_evlist__length(PyObject *obj) 897877108e4SArnaldo Carvalho de Melo { 898877108e4SArnaldo Carvalho de Melo struct pyrf_evlist *pevlist = (void *)obj; 899877108e4SArnaldo Carvalho de Melo 900877108e4SArnaldo Carvalho de Melo return pevlist->evlist.nr_entries; 901877108e4SArnaldo Carvalho de Melo } 902877108e4SArnaldo Carvalho de Melo 903877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i) 904877108e4SArnaldo Carvalho de Melo { 905877108e4SArnaldo Carvalho de Melo struct pyrf_evlist *pevlist = (void *)obj; 906877108e4SArnaldo Carvalho de Melo struct perf_evsel *pos; 907877108e4SArnaldo Carvalho de Melo 908877108e4SArnaldo Carvalho de Melo if (i >= pevlist->evlist.nr_entries) 909877108e4SArnaldo Carvalho de Melo return NULL; 910877108e4SArnaldo Carvalho de Melo 911*0050f7aaSArnaldo Carvalho de Melo evlist__for_each(&pevlist->evlist, pos) { 912877108e4SArnaldo Carvalho de Melo if (i-- == 0) 913877108e4SArnaldo Carvalho de Melo break; 914*0050f7aaSArnaldo Carvalho de Melo } 915877108e4SArnaldo Carvalho de Melo 916877108e4SArnaldo Carvalho de Melo return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel)); 917877108e4SArnaldo Carvalho de Melo } 918877108e4SArnaldo Carvalho de Melo 919877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_evlist__sequence_methods = { 920877108e4SArnaldo Carvalho de Melo .sq_length = pyrf_evlist__length, 921877108e4SArnaldo Carvalho de Melo .sq_item = pyrf_evlist__item, 922877108e4SArnaldo Carvalho de Melo }; 923877108e4SArnaldo Carvalho de Melo 924877108e4SArnaldo Carvalho de Melo static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object."); 925877108e4SArnaldo Carvalho de Melo 926877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evlist__type = { 927877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 928877108e4SArnaldo Carvalho de Melo .tp_name = "perf.evlist", 929877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_evlist), 930877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_evlist__delete, 931877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 932877108e4SArnaldo Carvalho de Melo .tp_as_sequence = &pyrf_evlist__sequence_methods, 933877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_evlist__doc, 934877108e4SArnaldo Carvalho de Melo .tp_methods = pyrf_evlist__methods, 935877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_evlist__init, 936877108e4SArnaldo Carvalho de Melo }; 937877108e4SArnaldo Carvalho de Melo 938877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__setup_types(void) 939877108e4SArnaldo Carvalho de Melo { 940877108e4SArnaldo Carvalho de Melo pyrf_evlist__type.tp_new = PyType_GenericNew; 941877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_evlist__type); 942877108e4SArnaldo Carvalho de Melo } 943877108e4SArnaldo Carvalho de Melo 944877108e4SArnaldo Carvalho de Melo static struct { 945877108e4SArnaldo Carvalho de Melo const char *name; 946877108e4SArnaldo Carvalho de Melo int value; 947877108e4SArnaldo Carvalho de Melo } perf__constants[] = { 948877108e4SArnaldo Carvalho de Melo { "TYPE_HARDWARE", PERF_TYPE_HARDWARE }, 949877108e4SArnaldo Carvalho de Melo { "TYPE_SOFTWARE", PERF_TYPE_SOFTWARE }, 950877108e4SArnaldo Carvalho de Melo { "TYPE_TRACEPOINT", PERF_TYPE_TRACEPOINT }, 951877108e4SArnaldo Carvalho de Melo { "TYPE_HW_CACHE", PERF_TYPE_HW_CACHE }, 952877108e4SArnaldo Carvalho de Melo { "TYPE_RAW", PERF_TYPE_RAW }, 953877108e4SArnaldo Carvalho de Melo { "TYPE_BREAKPOINT", PERF_TYPE_BREAKPOINT }, 954877108e4SArnaldo Carvalho de Melo 955877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CPU_CYCLES", PERF_COUNT_HW_CPU_CYCLES }, 956877108e4SArnaldo Carvalho de Melo { "COUNT_HW_INSTRUCTIONS", PERF_COUNT_HW_INSTRUCTIONS }, 957877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_REFERENCES", PERF_COUNT_HW_CACHE_REFERENCES }, 958877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_MISSES", PERF_COUNT_HW_CACHE_MISSES }, 959877108e4SArnaldo Carvalho de Melo { "COUNT_HW_BRANCH_INSTRUCTIONS", PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, 960877108e4SArnaldo Carvalho de Melo { "COUNT_HW_BRANCH_MISSES", PERF_COUNT_HW_BRANCH_MISSES }, 961877108e4SArnaldo Carvalho de Melo { "COUNT_HW_BUS_CYCLES", PERF_COUNT_HW_BUS_CYCLES }, 962877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_L1D", PERF_COUNT_HW_CACHE_L1D }, 963877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_L1I", PERF_COUNT_HW_CACHE_L1I }, 964877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_LL", PERF_COUNT_HW_CACHE_LL }, 965877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_DTLB", PERF_COUNT_HW_CACHE_DTLB }, 966877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_ITLB", PERF_COUNT_HW_CACHE_ITLB }, 967877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_BPU", PERF_COUNT_HW_CACHE_BPU }, 968877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_OP_READ", PERF_COUNT_HW_CACHE_OP_READ }, 969877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_OP_WRITE", PERF_COUNT_HW_CACHE_OP_WRITE }, 970877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_OP_PREFETCH", PERF_COUNT_HW_CACHE_OP_PREFETCH }, 971877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_RESULT_ACCESS", PERF_COUNT_HW_CACHE_RESULT_ACCESS }, 972877108e4SArnaldo Carvalho de Melo { "COUNT_HW_CACHE_RESULT_MISS", PERF_COUNT_HW_CACHE_RESULT_MISS }, 973877108e4SArnaldo Carvalho de Melo 974129c04cbSIngo Molnar { "COUNT_HW_STALLED_CYCLES_FRONTEND", PERF_COUNT_HW_STALLED_CYCLES_FRONTEND }, 975129c04cbSIngo Molnar { "COUNT_HW_STALLED_CYCLES_BACKEND", PERF_COUNT_HW_STALLED_CYCLES_BACKEND }, 976129c04cbSIngo Molnar 977877108e4SArnaldo Carvalho de Melo { "COUNT_SW_CPU_CLOCK", PERF_COUNT_SW_CPU_CLOCK }, 978877108e4SArnaldo Carvalho de Melo { "COUNT_SW_TASK_CLOCK", PERF_COUNT_SW_TASK_CLOCK }, 979877108e4SArnaldo Carvalho de Melo { "COUNT_SW_PAGE_FAULTS", PERF_COUNT_SW_PAGE_FAULTS }, 980877108e4SArnaldo Carvalho de Melo { "COUNT_SW_CONTEXT_SWITCHES", PERF_COUNT_SW_CONTEXT_SWITCHES }, 981877108e4SArnaldo Carvalho de Melo { "COUNT_SW_CPU_MIGRATIONS", PERF_COUNT_SW_CPU_MIGRATIONS }, 982877108e4SArnaldo Carvalho de Melo { "COUNT_SW_PAGE_FAULTS_MIN", PERF_COUNT_SW_PAGE_FAULTS_MIN }, 983877108e4SArnaldo Carvalho de Melo { "COUNT_SW_PAGE_FAULTS_MAJ", PERF_COUNT_SW_PAGE_FAULTS_MAJ }, 984877108e4SArnaldo Carvalho de Melo { "COUNT_SW_ALIGNMENT_FAULTS", PERF_COUNT_SW_ALIGNMENT_FAULTS }, 985877108e4SArnaldo Carvalho de Melo { "COUNT_SW_EMULATION_FAULTS", PERF_COUNT_SW_EMULATION_FAULTS }, 986d22d1a2aSAdrian Hunter { "COUNT_SW_DUMMY", PERF_COUNT_SW_DUMMY }, 987877108e4SArnaldo Carvalho de Melo 988877108e4SArnaldo Carvalho de Melo { "SAMPLE_IP", PERF_SAMPLE_IP }, 989877108e4SArnaldo Carvalho de Melo { "SAMPLE_TID", PERF_SAMPLE_TID }, 990877108e4SArnaldo Carvalho de Melo { "SAMPLE_TIME", PERF_SAMPLE_TIME }, 991877108e4SArnaldo Carvalho de Melo { "SAMPLE_ADDR", PERF_SAMPLE_ADDR }, 992877108e4SArnaldo Carvalho de Melo { "SAMPLE_READ", PERF_SAMPLE_READ }, 993877108e4SArnaldo Carvalho de Melo { "SAMPLE_CALLCHAIN", PERF_SAMPLE_CALLCHAIN }, 994877108e4SArnaldo Carvalho de Melo { "SAMPLE_ID", PERF_SAMPLE_ID }, 995877108e4SArnaldo Carvalho de Melo { "SAMPLE_CPU", PERF_SAMPLE_CPU }, 996877108e4SArnaldo Carvalho de Melo { "SAMPLE_PERIOD", PERF_SAMPLE_PERIOD }, 997877108e4SArnaldo Carvalho de Melo { "SAMPLE_STREAM_ID", PERF_SAMPLE_STREAM_ID }, 998877108e4SArnaldo Carvalho de Melo { "SAMPLE_RAW", PERF_SAMPLE_RAW }, 999877108e4SArnaldo Carvalho de Melo 1000877108e4SArnaldo Carvalho de Melo { "FORMAT_TOTAL_TIME_ENABLED", PERF_FORMAT_TOTAL_TIME_ENABLED }, 1001877108e4SArnaldo Carvalho de Melo { "FORMAT_TOTAL_TIME_RUNNING", PERF_FORMAT_TOTAL_TIME_RUNNING }, 1002877108e4SArnaldo Carvalho de Melo { "FORMAT_ID", PERF_FORMAT_ID }, 1003877108e4SArnaldo Carvalho de Melo { "FORMAT_GROUP", PERF_FORMAT_GROUP }, 1004877108e4SArnaldo Carvalho de Melo 1005877108e4SArnaldo Carvalho de Melo { "RECORD_MMAP", PERF_RECORD_MMAP }, 1006877108e4SArnaldo Carvalho de Melo { "RECORD_LOST", PERF_RECORD_LOST }, 1007877108e4SArnaldo Carvalho de Melo { "RECORD_COMM", PERF_RECORD_COMM }, 1008877108e4SArnaldo Carvalho de Melo { "RECORD_EXIT", PERF_RECORD_EXIT }, 1009877108e4SArnaldo Carvalho de Melo { "RECORD_THROTTLE", PERF_RECORD_THROTTLE }, 1010877108e4SArnaldo Carvalho de Melo { "RECORD_UNTHROTTLE", PERF_RECORD_UNTHROTTLE }, 1011877108e4SArnaldo Carvalho de Melo { "RECORD_FORK", PERF_RECORD_FORK }, 1012877108e4SArnaldo Carvalho de Melo { "RECORD_READ", PERF_RECORD_READ }, 1013877108e4SArnaldo Carvalho de Melo { "RECORD_SAMPLE", PERF_RECORD_SAMPLE }, 1014f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 1015877108e4SArnaldo Carvalho de Melo }; 1016877108e4SArnaldo Carvalho de Melo 1017877108e4SArnaldo Carvalho de Melo static PyMethodDef perf__methods[] = { 1018f6bbc1daSArnaldo Carvalho de Melo { .ml_name = NULL, } 1019877108e4SArnaldo Carvalho de Melo }; 1020877108e4SArnaldo Carvalho de Melo 1021877108e4SArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void) 1022877108e4SArnaldo Carvalho de Melo { 1023877108e4SArnaldo Carvalho de Melo PyObject *obj; 1024877108e4SArnaldo Carvalho de Melo int i; 1025877108e4SArnaldo Carvalho de Melo PyObject *dict, *module = Py_InitModule("perf", perf__methods); 1026877108e4SArnaldo Carvalho de Melo 1027877108e4SArnaldo Carvalho de Melo if (module == NULL || 1028877108e4SArnaldo Carvalho de Melo pyrf_event__setup_types() < 0 || 1029877108e4SArnaldo Carvalho de Melo pyrf_evlist__setup_types() < 0 || 1030877108e4SArnaldo Carvalho de Melo pyrf_evsel__setup_types() < 0 || 1031877108e4SArnaldo Carvalho de Melo pyrf_thread_map__setup_types() < 0 || 1032877108e4SArnaldo Carvalho de Melo pyrf_cpu_map__setup_types() < 0) 1033877108e4SArnaldo Carvalho de Melo return; 1034877108e4SArnaldo Carvalho de Melo 1035918512b4SJiri Olsa /* The page_size is placed in util object. */ 10360da2e9c2SArnaldo Carvalho de Melo page_size = sysconf(_SC_PAGE_SIZE); 10370da2e9c2SArnaldo Carvalho de Melo 1038877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_evlist__type); 1039877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type); 1040877108e4SArnaldo Carvalho de Melo 1041877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_evsel__type); 1042877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type); 1043877108e4SArnaldo Carvalho de Melo 1044877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_thread_map__type); 1045877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type); 1046877108e4SArnaldo Carvalho de Melo 1047877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_cpu_map__type); 1048877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type); 1049877108e4SArnaldo Carvalho de Melo 1050877108e4SArnaldo Carvalho de Melo dict = PyModule_GetDict(module); 1051877108e4SArnaldo Carvalho de Melo if (dict == NULL) 1052877108e4SArnaldo Carvalho de Melo goto error; 1053877108e4SArnaldo Carvalho de Melo 1054877108e4SArnaldo Carvalho de Melo for (i = 0; perf__constants[i].name != NULL; i++) { 1055877108e4SArnaldo Carvalho de Melo obj = PyInt_FromLong(perf__constants[i].value); 1056877108e4SArnaldo Carvalho de Melo if (obj == NULL) 1057877108e4SArnaldo Carvalho de Melo goto error; 1058877108e4SArnaldo Carvalho de Melo PyDict_SetItemString(dict, perf__constants[i].name, obj); 1059877108e4SArnaldo Carvalho de Melo Py_DECREF(obj); 1060877108e4SArnaldo Carvalho de Melo } 1061877108e4SArnaldo Carvalho de Melo 1062877108e4SArnaldo Carvalho de Melo error: 1063877108e4SArnaldo Carvalho de Melo if (PyErr_Occurred()) 1064877108e4SArnaldo Carvalho de Melo PyErr_SetString(PyExc_ImportError, "perf: Init failed!"); 1065877108e4SArnaldo Carvalho de Melo } 10660c6332e9SArnaldo Carvalho de Melo 10670c6332e9SArnaldo Carvalho de Melo /* 10680c6332e9SArnaldo Carvalho de Melo * Dummy, to avoid dragging all the test_attr infrastructure in the python 10690c6332e9SArnaldo Carvalho de Melo * binding. 10700c6332e9SArnaldo Carvalho de Melo */ 10710c6332e9SArnaldo Carvalho de Melo void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu, 10720c6332e9SArnaldo Carvalho de Melo int fd, int group_fd, unsigned long flags) 10730c6332e9SArnaldo Carvalho de Melo { 10740c6332e9SArnaldo Carvalho de Melo } 1075