1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2877108e4SArnaldo Carvalho de Melo #include <Python.h> 3877108e4SArnaldo Carvalho de Melo #include <structmember.h> 4877108e4SArnaldo Carvalho de Melo #include <inttypes.h> 5877108e4SArnaldo Carvalho de Melo #include <poll.h> 61075fbb2SJiri Olsa #include <linux/err.h> 7877108e4SArnaldo Carvalho de Melo #include "evlist.h" 856e2e056SArnaldo Carvalho de Melo #include "callchain.h" 9877108e4SArnaldo Carvalho de Melo #include "evsel.h" 10877108e4SArnaldo Carvalho de Melo #include "event.h" 11877108e4SArnaldo Carvalho de Melo #include "cpumap.h" 12fea01392SArnaldo Carvalho de Melo #include "print_binary.h" 13877108e4SArnaldo Carvalho de Melo #include "thread_map.h" 14721f0dfcSJiri Olsa #include "mmap.h" 15*1b2fc358SArnaldo Carvalho de Melo #include "util.h" 16877108e4SArnaldo Carvalho de Melo 1766dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 1866dfdff0SJaroslav Škarvada #define _PyUnicode_FromString(arg) \ 1966dfdff0SJaroslav Škarvada PyString_FromString(arg) 2066dfdff0SJaroslav Škarvada #define _PyUnicode_AsString(arg) \ 2166dfdff0SJaroslav Škarvada PyString_AsString(arg) 2266dfdff0SJaroslav Škarvada #define _PyUnicode_FromFormat(...) \ 2366dfdff0SJaroslav Škarvada PyString_FromFormat(__VA_ARGS__) 2466dfdff0SJaroslav Škarvada #define _PyLong_FromLong(arg) \ 2566dfdff0SJaroslav Škarvada PyInt_FromLong(arg) 2666dfdff0SJaroslav Škarvada 2766dfdff0SJaroslav Škarvada #else 2866dfdff0SJaroslav Škarvada 2966dfdff0SJaroslav Škarvada #define _PyUnicode_FromString(arg) \ 3066dfdff0SJaroslav Škarvada PyUnicode_FromString(arg) 3166dfdff0SJaroslav Škarvada #define _PyUnicode_FromFormat(...) \ 3266dfdff0SJaroslav Škarvada PyUnicode_FromFormat(__VA_ARGS__) 3366dfdff0SJaroslav Škarvada #define _PyLong_FromLong(arg) \ 3466dfdff0SJaroslav Škarvada PyLong_FromLong(arg) 3566dfdff0SJaroslav Škarvada #endif 3666dfdff0SJaroslav Škarvada 3766dfdff0SJaroslav Škarvada #ifndef Py_TYPE 3866dfdff0SJaroslav Škarvada #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) 3966dfdff0SJaroslav Škarvada #endif 4066dfdff0SJaroslav Škarvada 418afb4c01SAdrian Hunter /* 4256e2e056SArnaldo Carvalho de Melo * Provide these two so that we don't have to link against callchain.c and 4356e2e056SArnaldo Carvalho de Melo * start dragging hist.c, etc. 4456e2e056SArnaldo Carvalho de Melo */ 4556e2e056SArnaldo Carvalho de Melo struct callchain_param callchain_param; 4656e2e056SArnaldo Carvalho de Melo 4756e2e056SArnaldo Carvalho de Melo int parse_callchain_record(const char *arg __maybe_unused, 4856e2e056SArnaldo Carvalho de Melo struct callchain_param *param __maybe_unused) 4956e2e056SArnaldo Carvalho de Melo { 5056e2e056SArnaldo Carvalho de Melo return 0; 5156e2e056SArnaldo Carvalho de Melo } 5256e2e056SArnaldo Carvalho de Melo 5356e2e056SArnaldo Carvalho de Melo /* 548afb4c01SAdrian Hunter * Support debug printing even though util/debug.c is not linked. That means 558afb4c01SAdrian Hunter * implementing 'verbose' and 'eprintf'. 568afb4c01SAdrian Hunter */ 578afb4c01SAdrian Hunter int verbose; 588afb4c01SAdrian Hunter 59c95688aaSJiri Olsa int eprintf(int level, int var, const char *fmt, ...) 608afb4c01SAdrian Hunter { 618afb4c01SAdrian Hunter va_list args; 628afb4c01SAdrian Hunter int ret = 0; 638afb4c01SAdrian Hunter 64c95688aaSJiri Olsa if (var >= level) { 658afb4c01SAdrian Hunter va_start(args, fmt); 668afb4c01SAdrian Hunter ret = vfprintf(stderr, fmt, args); 678afb4c01SAdrian Hunter va_end(args); 688afb4c01SAdrian Hunter } 698afb4c01SAdrian Hunter 708afb4c01SAdrian Hunter return ret; 718afb4c01SAdrian Hunter } 728afb4c01SAdrian Hunter 73cfff2d90SFrederic Weisbecker /* Define PyVarObject_HEAD_INIT for python 2.5 */ 74cfff2d90SFrederic Weisbecker #ifndef PyVarObject_HEAD_INIT 75cfff2d90SFrederic Weisbecker # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, 76cfff2d90SFrederic Weisbecker #endif 77cfff2d90SFrederic Weisbecker 7866dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 79f6bbc1daSArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void); 8066dfdff0SJaroslav Škarvada #else 8166dfdff0SJaroslav Škarvada PyMODINIT_FUNC PyInit_perf(void); 8266dfdff0SJaroslav Škarvada #endif 83f6bbc1daSArnaldo Carvalho de Melo 84877108e4SArnaldo Carvalho de Melo #define member_def(type, member, ptype, help) \ 85877108e4SArnaldo Carvalho de Melo { #member, ptype, \ 86877108e4SArnaldo Carvalho de Melo offsetof(struct pyrf_event, event) + offsetof(struct type, member), \ 87877108e4SArnaldo Carvalho de Melo 0, help } 88877108e4SArnaldo Carvalho de Melo 89877108e4SArnaldo Carvalho de Melo #define sample_member_def(name, member, ptype, help) \ 90877108e4SArnaldo Carvalho de Melo { #name, ptype, \ 91877108e4SArnaldo Carvalho de Melo offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \ 92877108e4SArnaldo Carvalho de Melo 0, help } 93877108e4SArnaldo Carvalho de Melo 94877108e4SArnaldo Carvalho de Melo struct pyrf_event { 95877108e4SArnaldo Carvalho de Melo PyObject_HEAD 96377f698dSJiri Olsa struct perf_evsel *evsel; 97877108e4SArnaldo Carvalho de Melo struct perf_sample sample; 98877108e4SArnaldo Carvalho de Melo union perf_event event; 99877108e4SArnaldo Carvalho de Melo }; 100877108e4SArnaldo Carvalho de Melo 101877108e4SArnaldo Carvalho de Melo #define sample_members \ 102f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \ 103877108e4SArnaldo Carvalho de Melo sample_member_def(sample_pid, pid, T_INT, "event pid"), \ 104877108e4SArnaldo Carvalho de Melo sample_member_def(sample_tid, tid, T_INT, "event tid"), \ 105f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \ 106f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \ 107f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \ 108f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \ 109f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \ 110877108e4SArnaldo Carvalho de Melo sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"), 111877108e4SArnaldo Carvalho de Melo 112877108e4SArnaldo Carvalho de Melo static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object."); 113877108e4SArnaldo Carvalho de Melo 114877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_mmap_event__members[] = { 115877108e4SArnaldo Carvalho de Melo sample_members 116877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 117ae938802SArnaldo Carvalho de Melo member_def(perf_event_header, misc, T_UINT, "event misc"), 118877108e4SArnaldo Carvalho de Melo member_def(mmap_event, pid, T_UINT, "event pid"), 119877108e4SArnaldo Carvalho de Melo member_def(mmap_event, tid, T_UINT, "event tid"), 120f6bbc1daSArnaldo Carvalho de Melo member_def(mmap_event, start, T_ULONGLONG, "start of the map"), 121f6bbc1daSArnaldo Carvalho de Melo member_def(mmap_event, len, T_ULONGLONG, "map length"), 122f6bbc1daSArnaldo Carvalho de Melo member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"), 123877108e4SArnaldo Carvalho de Melo member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"), 124f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 125877108e4SArnaldo Carvalho de Melo }; 126877108e4SArnaldo Carvalho de Melo 127877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent) 128877108e4SArnaldo Carvalho de Melo { 129877108e4SArnaldo Carvalho de Melo PyObject *ret; 130877108e4SArnaldo Carvalho de Melo char *s; 131877108e4SArnaldo Carvalho de Melo 132877108e4SArnaldo Carvalho de Melo if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", " 133877108e4SArnaldo Carvalho de Melo "length: %#" PRIx64 ", offset: %#" PRIx64 ", " 134877108e4SArnaldo Carvalho de Melo "filename: %s }", 135877108e4SArnaldo Carvalho de Melo pevent->event.mmap.pid, pevent->event.mmap.tid, 136877108e4SArnaldo Carvalho de Melo pevent->event.mmap.start, pevent->event.mmap.len, 137877108e4SArnaldo Carvalho de Melo pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { 138877108e4SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 139877108e4SArnaldo Carvalho de Melo } else { 14066dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString(s); 141877108e4SArnaldo Carvalho de Melo free(s); 142877108e4SArnaldo Carvalho de Melo } 143877108e4SArnaldo Carvalho de Melo return ret; 144877108e4SArnaldo Carvalho de Melo } 145877108e4SArnaldo Carvalho de Melo 146877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_mmap_event__type = { 147877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 148877108e4SArnaldo Carvalho de Melo .tp_name = "perf.mmap_event", 149877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 150877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 151877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_mmap_event__doc, 152877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_mmap_event__members, 153877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_mmap_event__repr, 154877108e4SArnaldo Carvalho de Melo }; 155877108e4SArnaldo Carvalho de Melo 156877108e4SArnaldo Carvalho de Melo static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object."); 157877108e4SArnaldo Carvalho de Melo 158877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_task_event__members[] = { 159877108e4SArnaldo Carvalho de Melo sample_members 160877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 161877108e4SArnaldo Carvalho de Melo member_def(fork_event, pid, T_UINT, "event pid"), 162877108e4SArnaldo Carvalho de Melo member_def(fork_event, ppid, T_UINT, "event ppid"), 163877108e4SArnaldo Carvalho de Melo member_def(fork_event, tid, T_UINT, "event tid"), 164877108e4SArnaldo Carvalho de Melo member_def(fork_event, ptid, T_UINT, "event ptid"), 165f6bbc1daSArnaldo Carvalho de Melo member_def(fork_event, time, T_ULONGLONG, "timestamp"), 166f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 167877108e4SArnaldo Carvalho de Melo }; 168877108e4SArnaldo Carvalho de Melo 169877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent) 170877108e4SArnaldo Carvalho de Melo { 17166dfdff0SJaroslav Škarvada return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " 172877108e4SArnaldo Carvalho de Melo "ptid: %u, time: %" PRIu64 "}", 173877108e4SArnaldo Carvalho de Melo pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit", 174877108e4SArnaldo Carvalho de Melo pevent->event.fork.pid, 175877108e4SArnaldo Carvalho de Melo pevent->event.fork.ppid, 176877108e4SArnaldo Carvalho de Melo pevent->event.fork.tid, 177877108e4SArnaldo Carvalho de Melo pevent->event.fork.ptid, 178877108e4SArnaldo Carvalho de Melo pevent->event.fork.time); 179877108e4SArnaldo Carvalho de Melo } 180877108e4SArnaldo Carvalho de Melo 181877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_task_event__type = { 182877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 183877108e4SArnaldo Carvalho de Melo .tp_name = "perf.task_event", 184877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 185877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 186877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_task_event__doc, 187877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_task_event__members, 188877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_task_event__repr, 189877108e4SArnaldo Carvalho de Melo }; 190877108e4SArnaldo Carvalho de Melo 191877108e4SArnaldo Carvalho de Melo static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object."); 192877108e4SArnaldo Carvalho de Melo 193877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_comm_event__members[] = { 194877108e4SArnaldo Carvalho de Melo sample_members 195877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 196877108e4SArnaldo Carvalho de Melo member_def(comm_event, pid, T_UINT, "event pid"), 197877108e4SArnaldo Carvalho de Melo member_def(comm_event, tid, T_UINT, "event tid"), 198877108e4SArnaldo Carvalho de Melo member_def(comm_event, comm, T_STRING_INPLACE, "process name"), 199f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 200877108e4SArnaldo Carvalho de Melo }; 201877108e4SArnaldo Carvalho de Melo 202877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent) 203877108e4SArnaldo Carvalho de Melo { 20466dfdff0SJaroslav Škarvada return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", 205877108e4SArnaldo Carvalho de Melo pevent->event.comm.pid, 206877108e4SArnaldo Carvalho de Melo pevent->event.comm.tid, 207877108e4SArnaldo Carvalho de Melo pevent->event.comm.comm); 208877108e4SArnaldo Carvalho de Melo } 209877108e4SArnaldo Carvalho de Melo 210877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_comm_event__type = { 211877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 212877108e4SArnaldo Carvalho de Melo .tp_name = "perf.comm_event", 213877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 214877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 215877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_comm_event__doc, 216877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_comm_event__members, 217877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_comm_event__repr, 218877108e4SArnaldo Carvalho de Melo }; 219877108e4SArnaldo Carvalho de Melo 220877108e4SArnaldo Carvalho de Melo static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object."); 221877108e4SArnaldo Carvalho de Melo 222877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_throttle_event__members[] = { 223877108e4SArnaldo Carvalho de Melo sample_members 224877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 225f6bbc1daSArnaldo Carvalho de Melo member_def(throttle_event, time, T_ULONGLONG, "timestamp"), 226f6bbc1daSArnaldo Carvalho de Melo member_def(throttle_event, id, T_ULONGLONG, "event id"), 227f6bbc1daSArnaldo Carvalho de Melo member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"), 228f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 229877108e4SArnaldo Carvalho de Melo }; 230877108e4SArnaldo Carvalho de Melo 231877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent) 232877108e4SArnaldo Carvalho de Melo { 233877108e4SArnaldo Carvalho de Melo struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1); 234877108e4SArnaldo Carvalho de Melo 23566dfdff0SJaroslav Škarvada return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64 236877108e4SArnaldo Carvalho de Melo ", stream_id: %" PRIu64 " }", 237877108e4SArnaldo Carvalho de Melo pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un", 238877108e4SArnaldo Carvalho de Melo te->time, te->id, te->stream_id); 239877108e4SArnaldo Carvalho de Melo } 240877108e4SArnaldo Carvalho de Melo 241877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_throttle_event__type = { 242877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 243877108e4SArnaldo Carvalho de Melo .tp_name = "perf.throttle_event", 244877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 245877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 246877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_throttle_event__doc, 247877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_throttle_event__members, 248877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_throttle_event__repr, 249877108e4SArnaldo Carvalho de Melo }; 250877108e4SArnaldo Carvalho de Melo 2513e9f45a7SArnaldo Carvalho de Melo static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object."); 2523e9f45a7SArnaldo Carvalho de Melo 2533e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_lost_event__members[] = { 2543e9f45a7SArnaldo Carvalho de Melo sample_members 2553e9f45a7SArnaldo Carvalho de Melo member_def(lost_event, id, T_ULONGLONG, "event id"), 2563e9f45a7SArnaldo Carvalho de Melo member_def(lost_event, lost, T_ULONGLONG, "number of lost events"), 2573e9f45a7SArnaldo Carvalho de Melo { .name = NULL, }, 2583e9f45a7SArnaldo Carvalho de Melo }; 2593e9f45a7SArnaldo Carvalho de Melo 2603e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent) 2613e9f45a7SArnaldo Carvalho de Melo { 2623e9f45a7SArnaldo Carvalho de Melo PyObject *ret; 2633e9f45a7SArnaldo Carvalho de Melo char *s; 2643e9f45a7SArnaldo Carvalho de Melo 2653e9f45a7SArnaldo Carvalho de Melo if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", " 2663e9f45a7SArnaldo Carvalho de Melo "lost: %#" PRIx64 " }", 2673e9f45a7SArnaldo Carvalho de Melo pevent->event.lost.id, pevent->event.lost.lost) < 0) { 2683e9f45a7SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 2693e9f45a7SArnaldo Carvalho de Melo } else { 27066dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString(s); 2713e9f45a7SArnaldo Carvalho de Melo free(s); 2723e9f45a7SArnaldo Carvalho de Melo } 2733e9f45a7SArnaldo Carvalho de Melo return ret; 2743e9f45a7SArnaldo Carvalho de Melo } 2753e9f45a7SArnaldo Carvalho de Melo 2763e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_lost_event__type = { 2773e9f45a7SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 2783e9f45a7SArnaldo Carvalho de Melo .tp_name = "perf.lost_event", 2793e9f45a7SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 2803e9f45a7SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 2813e9f45a7SArnaldo Carvalho de Melo .tp_doc = pyrf_lost_event__doc, 2823e9f45a7SArnaldo Carvalho de Melo .tp_members = pyrf_lost_event__members, 2833e9f45a7SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_lost_event__repr, 2843e9f45a7SArnaldo Carvalho de Melo }; 2853e9f45a7SArnaldo Carvalho de Melo 2863e9f45a7SArnaldo Carvalho de Melo static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object."); 2873e9f45a7SArnaldo Carvalho de Melo 2883e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_read_event__members[] = { 2893e9f45a7SArnaldo Carvalho de Melo sample_members 2903e9f45a7SArnaldo Carvalho de Melo member_def(read_event, pid, T_UINT, "event pid"), 2913e9f45a7SArnaldo Carvalho de Melo member_def(read_event, tid, T_UINT, "event tid"), 2923e9f45a7SArnaldo Carvalho de Melo { .name = NULL, }, 2933e9f45a7SArnaldo Carvalho de Melo }; 2943e9f45a7SArnaldo Carvalho de Melo 2953e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent) 2963e9f45a7SArnaldo Carvalho de Melo { 29766dfdff0SJaroslav Škarvada return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }", 2983e9f45a7SArnaldo Carvalho de Melo pevent->event.read.pid, 2993e9f45a7SArnaldo Carvalho de Melo pevent->event.read.tid); 3003e9f45a7SArnaldo Carvalho de Melo /* 3013e9f45a7SArnaldo Carvalho de Melo * FIXME: return the array of read values, 3023e9f45a7SArnaldo Carvalho de Melo * making this method useful ;-) 3033e9f45a7SArnaldo Carvalho de Melo */ 3043e9f45a7SArnaldo Carvalho de Melo } 3053e9f45a7SArnaldo Carvalho de Melo 3063e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_read_event__type = { 3073e9f45a7SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 3083e9f45a7SArnaldo Carvalho de Melo .tp_name = "perf.read_event", 3093e9f45a7SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 3103e9f45a7SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 3113e9f45a7SArnaldo Carvalho de Melo .tp_doc = pyrf_read_event__doc, 3123e9f45a7SArnaldo Carvalho de Melo .tp_members = pyrf_read_event__members, 3133e9f45a7SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_read_event__repr, 3143e9f45a7SArnaldo Carvalho de Melo }; 3153e9f45a7SArnaldo Carvalho de Melo 3163e9f45a7SArnaldo Carvalho de Melo static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object."); 3173e9f45a7SArnaldo Carvalho de Melo 3183e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_sample_event__members[] = { 3193e9f45a7SArnaldo Carvalho de Melo sample_members 3203e9f45a7SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 3213e9f45a7SArnaldo Carvalho de Melo { .name = NULL, }, 3223e9f45a7SArnaldo Carvalho de Melo }; 3233e9f45a7SArnaldo Carvalho de Melo 3243e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent) 3253e9f45a7SArnaldo Carvalho de Melo { 3263e9f45a7SArnaldo Carvalho de Melo PyObject *ret; 3273e9f45a7SArnaldo Carvalho de Melo char *s; 3283e9f45a7SArnaldo Carvalho de Melo 3293e9f45a7SArnaldo Carvalho de Melo if (asprintf(&s, "{ type: sample }") < 0) { 3303e9f45a7SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 3313e9f45a7SArnaldo Carvalho de Melo } else { 33266dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString(s); 3333e9f45a7SArnaldo Carvalho de Melo free(s); 3343e9f45a7SArnaldo Carvalho de Melo } 3353e9f45a7SArnaldo Carvalho de Melo return ret; 3363e9f45a7SArnaldo Carvalho de Melo } 3373e9f45a7SArnaldo Carvalho de Melo 338bae57e38SJiri Olsa static bool is_tracepoint(struct pyrf_event *pevent) 339bae57e38SJiri Olsa { 340bae57e38SJiri Olsa return pevent->evsel->attr.type == PERF_TYPE_TRACEPOINT; 341bae57e38SJiri Olsa } 342bae57e38SJiri Olsa 343bae57e38SJiri Olsa static PyObject* 3442c92f982STzvetomir Stoyanov (VMware) tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field) 345bae57e38SJiri Olsa { 34669769ce1STzvetomir Stoyanov struct tep_handle *pevent = field->event->tep; 347bae57e38SJiri Olsa void *data = pe->sample.raw_data; 348bae57e38SJiri Olsa PyObject *ret = NULL; 349bae57e38SJiri Olsa unsigned long long val; 350bae57e38SJiri Olsa unsigned int offset, len; 351bae57e38SJiri Olsa 352bb39ccb2STzvetomir Stoyanov (VMware) if (field->flags & TEP_FIELD_IS_ARRAY) { 353bae57e38SJiri Olsa offset = field->offset; 354bae57e38SJiri Olsa len = field->size; 355bb39ccb2STzvetomir Stoyanov (VMware) if (field->flags & TEP_FIELD_IS_DYNAMIC) { 35659c1baeeSTzvetomir Stoyanov (VMware) val = tep_read_number(pevent, data + offset, len); 357bae57e38SJiri Olsa offset = val; 358bae57e38SJiri Olsa len = offset >> 16; 359bae57e38SJiri Olsa offset &= 0xffff; 360bae57e38SJiri Olsa } 361bb39ccb2STzvetomir Stoyanov (VMware) if (field->flags & TEP_FIELD_IS_STRING && 362bae57e38SJiri Olsa is_printable_array(data + offset, len)) { 36366dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString((char *)data + offset); 364bae57e38SJiri Olsa } else { 365bae57e38SJiri Olsa ret = PyByteArray_FromStringAndSize((const char *) data + offset, len); 366bb39ccb2STzvetomir Stoyanov (VMware) field->flags &= ~TEP_FIELD_IS_STRING; 367bae57e38SJiri Olsa } 368bae57e38SJiri Olsa } else { 36959c1baeeSTzvetomir Stoyanov (VMware) val = tep_read_number(pevent, data + field->offset, 370bae57e38SJiri Olsa field->size); 371bb39ccb2STzvetomir Stoyanov (VMware) if (field->flags & TEP_FIELD_IS_POINTER) 372bae57e38SJiri Olsa ret = PyLong_FromUnsignedLong((unsigned long) val); 373bb39ccb2STzvetomir Stoyanov (VMware) else if (field->flags & TEP_FIELD_IS_SIGNED) 374bae57e38SJiri Olsa ret = PyLong_FromLong((long) val); 375bae57e38SJiri Olsa else 376bae57e38SJiri Olsa ret = PyLong_FromUnsignedLong((unsigned long) val); 377bae57e38SJiri Olsa } 378bae57e38SJiri Olsa 379bae57e38SJiri Olsa return ret; 380bae57e38SJiri Olsa } 381bae57e38SJiri Olsa 382bae57e38SJiri Olsa static PyObject* 383bae57e38SJiri Olsa get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) 384bae57e38SJiri Olsa { 38566dfdff0SJaroslav Škarvada const char *str = _PyUnicode_AsString(PyObject_Str(attr_name)); 386bae57e38SJiri Olsa struct perf_evsel *evsel = pevent->evsel; 3872c92f982STzvetomir Stoyanov (VMware) struct tep_format_field *field; 388bae57e38SJiri Olsa 389bae57e38SJiri Olsa if (!evsel->tp_format) { 39097fbf3f0STzvetomir Stoyanov struct tep_event *tp_format; 391bae57e38SJiri Olsa 392bae57e38SJiri Olsa tp_format = trace_event__tp_format_id(evsel->attr.config); 393bae57e38SJiri Olsa if (!tp_format) 394bae57e38SJiri Olsa return NULL; 395bae57e38SJiri Olsa 396bae57e38SJiri Olsa evsel->tp_format = tp_format; 397bae57e38SJiri Olsa } 398bae57e38SJiri Olsa 399af85cd19STzvetomir Stoyanov (VMware) field = tep_find_any_field(evsel->tp_format, str); 400bae57e38SJiri Olsa if (!field) 401bae57e38SJiri Olsa return NULL; 402bae57e38SJiri Olsa 403bae57e38SJiri Olsa return tracepoint_field(pevent, field); 404bae57e38SJiri Olsa } 405bae57e38SJiri Olsa 406bae57e38SJiri Olsa static PyObject* 407bae57e38SJiri Olsa pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name) 408bae57e38SJiri Olsa { 409bae57e38SJiri Olsa PyObject *obj = NULL; 410bae57e38SJiri Olsa 411bae57e38SJiri Olsa if (is_tracepoint(pevent)) 412bae57e38SJiri Olsa obj = get_tracepoint_field(pevent, attr_name); 413bae57e38SJiri Olsa 414bae57e38SJiri Olsa return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name); 415bae57e38SJiri Olsa } 416bae57e38SJiri Olsa 4173e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_sample_event__type = { 4183e9f45a7SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 4193e9f45a7SArnaldo Carvalho de Melo .tp_name = "perf.sample_event", 4203e9f45a7SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 4213e9f45a7SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 4223e9f45a7SArnaldo Carvalho de Melo .tp_doc = pyrf_sample_event__doc, 4233e9f45a7SArnaldo Carvalho de Melo .tp_members = pyrf_sample_event__members, 4243e9f45a7SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_sample_event__repr, 425bae57e38SJiri Olsa .tp_getattro = (getattrofunc) pyrf_sample_event__getattro, 4263e9f45a7SArnaldo Carvalho de Melo }; 4273e9f45a7SArnaldo Carvalho de Melo 428ae938802SArnaldo Carvalho de Melo static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object."); 429ae938802SArnaldo Carvalho de Melo 430ae938802SArnaldo Carvalho de Melo static PyMemberDef pyrf_context_switch_event__members[] = { 431ae938802SArnaldo Carvalho de Melo sample_members 432ae938802SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 433ae938802SArnaldo Carvalho de Melo member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"), 434ae938802SArnaldo Carvalho de Melo member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"), 435ae938802SArnaldo Carvalho de Melo { .name = NULL, }, 436ae938802SArnaldo Carvalho de Melo }; 437ae938802SArnaldo Carvalho de Melo 438ae938802SArnaldo Carvalho de Melo static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent) 439ae938802SArnaldo Carvalho de Melo { 440ae938802SArnaldo Carvalho de Melo PyObject *ret; 441ae938802SArnaldo Carvalho de Melo char *s; 442ae938802SArnaldo Carvalho de Melo 443ae938802SArnaldo Carvalho de Melo if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }", 444ae938802SArnaldo Carvalho de Melo pevent->event.context_switch.next_prev_pid, 445ae938802SArnaldo Carvalho de Melo pevent->event.context_switch.next_prev_tid, 446ae938802SArnaldo Carvalho de Melo !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) { 447ae938802SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 448ae938802SArnaldo Carvalho de Melo } else { 44966dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString(s); 450ae938802SArnaldo Carvalho de Melo free(s); 451ae938802SArnaldo Carvalho de Melo } 452ae938802SArnaldo Carvalho de Melo return ret; 453ae938802SArnaldo Carvalho de Melo } 454ae938802SArnaldo Carvalho de Melo 455ae938802SArnaldo Carvalho de Melo static PyTypeObject pyrf_context_switch_event__type = { 456ae938802SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 457ae938802SArnaldo Carvalho de Melo .tp_name = "perf.context_switch_event", 458ae938802SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 459ae938802SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 460ae938802SArnaldo Carvalho de Melo .tp_doc = pyrf_context_switch_event__doc, 461ae938802SArnaldo Carvalho de Melo .tp_members = pyrf_context_switch_event__members, 462ae938802SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_context_switch_event__repr, 463ae938802SArnaldo Carvalho de Melo }; 464ae938802SArnaldo Carvalho de Melo 465877108e4SArnaldo Carvalho de Melo static int pyrf_event__setup_types(void) 466877108e4SArnaldo Carvalho de Melo { 467877108e4SArnaldo Carvalho de Melo int err; 468877108e4SArnaldo Carvalho de Melo pyrf_mmap_event__type.tp_new = 469877108e4SArnaldo Carvalho de Melo pyrf_task_event__type.tp_new = 470877108e4SArnaldo Carvalho de Melo pyrf_comm_event__type.tp_new = 4713e9f45a7SArnaldo Carvalho de Melo pyrf_lost_event__type.tp_new = 4723e9f45a7SArnaldo Carvalho de Melo pyrf_read_event__type.tp_new = 4733e9f45a7SArnaldo Carvalho de Melo pyrf_sample_event__type.tp_new = 474ae938802SArnaldo Carvalho de Melo pyrf_context_switch_event__type.tp_new = 475877108e4SArnaldo Carvalho de Melo pyrf_throttle_event__type.tp_new = PyType_GenericNew; 476877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_mmap_event__type); 477877108e4SArnaldo Carvalho de Melo if (err < 0) 478877108e4SArnaldo Carvalho de Melo goto out; 4793e9f45a7SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_lost_event__type); 4803e9f45a7SArnaldo Carvalho de Melo if (err < 0) 4813e9f45a7SArnaldo Carvalho de Melo goto out; 482877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_task_event__type); 483877108e4SArnaldo Carvalho de Melo if (err < 0) 484877108e4SArnaldo Carvalho de Melo goto out; 485877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_comm_event__type); 486877108e4SArnaldo Carvalho de Melo if (err < 0) 487877108e4SArnaldo Carvalho de Melo goto out; 488877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_throttle_event__type); 489877108e4SArnaldo Carvalho de Melo if (err < 0) 490877108e4SArnaldo Carvalho de Melo goto out; 4913e9f45a7SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_read_event__type); 4923e9f45a7SArnaldo Carvalho de Melo if (err < 0) 4933e9f45a7SArnaldo Carvalho de Melo goto out; 4943e9f45a7SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_sample_event__type); 4953e9f45a7SArnaldo Carvalho de Melo if (err < 0) 4963e9f45a7SArnaldo Carvalho de Melo goto out; 497ae938802SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_context_switch_event__type); 498ae938802SArnaldo Carvalho de Melo if (err < 0) 499ae938802SArnaldo Carvalho de Melo goto out; 500877108e4SArnaldo Carvalho de Melo out: 501877108e4SArnaldo Carvalho de Melo return err; 502877108e4SArnaldo Carvalho de Melo } 503877108e4SArnaldo Carvalho de Melo 504877108e4SArnaldo Carvalho de Melo static PyTypeObject *pyrf_event__type[] = { 505877108e4SArnaldo Carvalho de Melo [PERF_RECORD_MMAP] = &pyrf_mmap_event__type, 5063e9f45a7SArnaldo Carvalho de Melo [PERF_RECORD_LOST] = &pyrf_lost_event__type, 507877108e4SArnaldo Carvalho de Melo [PERF_RECORD_COMM] = &pyrf_comm_event__type, 508877108e4SArnaldo Carvalho de Melo [PERF_RECORD_EXIT] = &pyrf_task_event__type, 509877108e4SArnaldo Carvalho de Melo [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type, 510877108e4SArnaldo Carvalho de Melo [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type, 511877108e4SArnaldo Carvalho de Melo [PERF_RECORD_FORK] = &pyrf_task_event__type, 5123e9f45a7SArnaldo Carvalho de Melo [PERF_RECORD_READ] = &pyrf_read_event__type, 5133e9f45a7SArnaldo Carvalho de Melo [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type, 514ae938802SArnaldo Carvalho de Melo [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type, 515ae938802SArnaldo Carvalho de Melo [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type, 516877108e4SArnaldo Carvalho de Melo }; 517877108e4SArnaldo Carvalho de Melo 518877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_event__new(union perf_event *event) 519877108e4SArnaldo Carvalho de Melo { 520877108e4SArnaldo Carvalho de Melo struct pyrf_event *pevent; 521877108e4SArnaldo Carvalho de Melo PyTypeObject *ptype; 522877108e4SArnaldo Carvalho de Melo 523ae938802SArnaldo Carvalho de Melo if ((event->header.type < PERF_RECORD_MMAP || 524ae938802SArnaldo Carvalho de Melo event->header.type > PERF_RECORD_SAMPLE) && 525ae938802SArnaldo Carvalho de Melo !(event->header.type == PERF_RECORD_SWITCH || 526ae938802SArnaldo Carvalho de Melo event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)) 527877108e4SArnaldo Carvalho de Melo return NULL; 528877108e4SArnaldo Carvalho de Melo 529877108e4SArnaldo Carvalho de Melo ptype = pyrf_event__type[event->header.type]; 530877108e4SArnaldo Carvalho de Melo pevent = PyObject_New(struct pyrf_event, ptype); 531877108e4SArnaldo Carvalho de Melo if (pevent != NULL) 532877108e4SArnaldo Carvalho de Melo memcpy(&pevent->event, event, event->header.size); 533877108e4SArnaldo Carvalho de Melo return (PyObject *)pevent; 534877108e4SArnaldo Carvalho de Melo } 535877108e4SArnaldo Carvalho de Melo 536877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map { 537877108e4SArnaldo Carvalho de Melo PyObject_HEAD 538877108e4SArnaldo Carvalho de Melo 539877108e4SArnaldo Carvalho de Melo struct cpu_map *cpus; 540877108e4SArnaldo Carvalho de Melo }; 541877108e4SArnaldo Carvalho de Melo 542877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus, 543877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 544877108e4SArnaldo Carvalho de Melo { 54564348153SFrederic Weisbecker static char *kwlist[] = { "cpustr", NULL }; 546877108e4SArnaldo Carvalho de Melo char *cpustr = NULL; 547877108e4SArnaldo Carvalho de Melo 548877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", 549877108e4SArnaldo Carvalho de Melo kwlist, &cpustr)) 550877108e4SArnaldo Carvalho de Melo return -1; 551877108e4SArnaldo Carvalho de Melo 552877108e4SArnaldo Carvalho de Melo pcpus->cpus = cpu_map__new(cpustr); 553877108e4SArnaldo Carvalho de Melo if (pcpus->cpus == NULL) 554877108e4SArnaldo Carvalho de Melo return -1; 555877108e4SArnaldo Carvalho de Melo return 0; 556877108e4SArnaldo Carvalho de Melo } 557877108e4SArnaldo Carvalho de Melo 558877108e4SArnaldo Carvalho de Melo static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus) 559877108e4SArnaldo Carvalho de Melo { 560f30a79b0SJiri Olsa cpu_map__put(pcpus->cpus); 56166dfdff0SJaroslav Škarvada Py_TYPE(pcpus)->tp_free((PyObject*)pcpus); 562877108e4SArnaldo Carvalho de Melo } 563877108e4SArnaldo Carvalho de Melo 564877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_cpu_map__length(PyObject *obj) 565877108e4SArnaldo Carvalho de Melo { 566877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map *pcpus = (void *)obj; 567877108e4SArnaldo Carvalho de Melo 568877108e4SArnaldo Carvalho de Melo return pcpus->cpus->nr; 569877108e4SArnaldo Carvalho de Melo } 570877108e4SArnaldo Carvalho de Melo 571877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i) 572877108e4SArnaldo Carvalho de Melo { 573877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map *pcpus = (void *)obj; 574877108e4SArnaldo Carvalho de Melo 575877108e4SArnaldo Carvalho de Melo if (i >= pcpus->cpus->nr) 576877108e4SArnaldo Carvalho de Melo return NULL; 577877108e4SArnaldo Carvalho de Melo 578877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", pcpus->cpus->map[i]); 579877108e4SArnaldo Carvalho de Melo } 580877108e4SArnaldo Carvalho de Melo 581877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_cpu_map__sequence_methods = { 582877108e4SArnaldo Carvalho de Melo .sq_length = pyrf_cpu_map__length, 583877108e4SArnaldo Carvalho de Melo .sq_item = pyrf_cpu_map__item, 584877108e4SArnaldo Carvalho de Melo }; 585877108e4SArnaldo Carvalho de Melo 586877108e4SArnaldo Carvalho de Melo static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object."); 587877108e4SArnaldo Carvalho de Melo 588877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_cpu_map__type = { 589877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 590877108e4SArnaldo Carvalho de Melo .tp_name = "perf.cpu_map", 591877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_cpu_map), 592877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_cpu_map__delete, 593877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 594877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_cpu_map__doc, 595877108e4SArnaldo Carvalho de Melo .tp_as_sequence = &pyrf_cpu_map__sequence_methods, 596877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_cpu_map__init, 597877108e4SArnaldo Carvalho de Melo }; 598877108e4SArnaldo Carvalho de Melo 599877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__setup_types(void) 600877108e4SArnaldo Carvalho de Melo { 601877108e4SArnaldo Carvalho de Melo pyrf_cpu_map__type.tp_new = PyType_GenericNew; 602877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_cpu_map__type); 603877108e4SArnaldo Carvalho de Melo } 604877108e4SArnaldo Carvalho de Melo 605877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map { 606877108e4SArnaldo Carvalho de Melo PyObject_HEAD 607877108e4SArnaldo Carvalho de Melo 608877108e4SArnaldo Carvalho de Melo struct thread_map *threads; 609877108e4SArnaldo Carvalho de Melo }; 610877108e4SArnaldo Carvalho de Melo 611877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads, 612877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 613877108e4SArnaldo Carvalho de Melo { 6140d37aa34SArnaldo Carvalho de Melo static char *kwlist[] = { "pid", "tid", "uid", NULL }; 6150d37aa34SArnaldo Carvalho de Melo int pid = -1, tid = -1, uid = UINT_MAX; 616877108e4SArnaldo Carvalho de Melo 6170d37aa34SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii", 6180d37aa34SArnaldo Carvalho de Melo kwlist, &pid, &tid, &uid)) 619877108e4SArnaldo Carvalho de Melo return -1; 620877108e4SArnaldo Carvalho de Melo 6210d37aa34SArnaldo Carvalho de Melo pthreads->threads = thread_map__new(pid, tid, uid); 622877108e4SArnaldo Carvalho de Melo if (pthreads->threads == NULL) 623877108e4SArnaldo Carvalho de Melo return -1; 624877108e4SArnaldo Carvalho de Melo return 0; 625877108e4SArnaldo Carvalho de Melo } 626877108e4SArnaldo Carvalho de Melo 627877108e4SArnaldo Carvalho de Melo static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads) 628877108e4SArnaldo Carvalho de Melo { 629186fbb74SJiri Olsa thread_map__put(pthreads->threads); 63066dfdff0SJaroslav Škarvada Py_TYPE(pthreads)->tp_free((PyObject*)pthreads); 631877108e4SArnaldo Carvalho de Melo } 632877108e4SArnaldo Carvalho de Melo 633877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_thread_map__length(PyObject *obj) 634877108e4SArnaldo Carvalho de Melo { 635877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map *pthreads = (void *)obj; 636877108e4SArnaldo Carvalho de Melo 637877108e4SArnaldo Carvalho de Melo return pthreads->threads->nr; 638877108e4SArnaldo Carvalho de Melo } 639877108e4SArnaldo Carvalho de Melo 640877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i) 641877108e4SArnaldo Carvalho de Melo { 642877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map *pthreads = (void *)obj; 643877108e4SArnaldo Carvalho de Melo 644877108e4SArnaldo Carvalho de Melo if (i >= pthreads->threads->nr) 645877108e4SArnaldo Carvalho de Melo return NULL; 646877108e4SArnaldo Carvalho de Melo 647877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", pthreads->threads->map[i]); 648877108e4SArnaldo Carvalho de Melo } 649877108e4SArnaldo Carvalho de Melo 650877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_thread_map__sequence_methods = { 651877108e4SArnaldo Carvalho de Melo .sq_length = pyrf_thread_map__length, 652877108e4SArnaldo Carvalho de Melo .sq_item = pyrf_thread_map__item, 653877108e4SArnaldo Carvalho de Melo }; 654877108e4SArnaldo Carvalho de Melo 655877108e4SArnaldo Carvalho de Melo static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object."); 656877108e4SArnaldo Carvalho de Melo 657877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_thread_map__type = { 658877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 659877108e4SArnaldo Carvalho de Melo .tp_name = "perf.thread_map", 660877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_thread_map), 661877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_thread_map__delete, 662877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 663877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_thread_map__doc, 664877108e4SArnaldo Carvalho de Melo .tp_as_sequence = &pyrf_thread_map__sequence_methods, 665877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_thread_map__init, 666877108e4SArnaldo Carvalho de Melo }; 667877108e4SArnaldo Carvalho de Melo 668877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__setup_types(void) 669877108e4SArnaldo Carvalho de Melo { 670877108e4SArnaldo Carvalho de Melo pyrf_thread_map__type.tp_new = PyType_GenericNew; 671877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_thread_map__type); 672877108e4SArnaldo Carvalho de Melo } 673877108e4SArnaldo Carvalho de Melo 674877108e4SArnaldo Carvalho de Melo struct pyrf_evsel { 675877108e4SArnaldo Carvalho de Melo PyObject_HEAD 676877108e4SArnaldo Carvalho de Melo 677877108e4SArnaldo Carvalho de Melo struct perf_evsel evsel; 678877108e4SArnaldo Carvalho de Melo }; 679877108e4SArnaldo Carvalho de Melo 680877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__init(struct pyrf_evsel *pevsel, 681877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 682877108e4SArnaldo Carvalho de Melo { 683877108e4SArnaldo Carvalho de Melo struct perf_event_attr attr = { 684877108e4SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 685877108e4SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 686877108e4SArnaldo Carvalho de Melo .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID, 687877108e4SArnaldo Carvalho de Melo }; 688877108e4SArnaldo Carvalho de Melo static char *kwlist[] = { 689877108e4SArnaldo Carvalho de Melo "type", 690877108e4SArnaldo Carvalho de Melo "config", 691877108e4SArnaldo Carvalho de Melo "sample_freq", 692877108e4SArnaldo Carvalho de Melo "sample_period", 693877108e4SArnaldo Carvalho de Melo "sample_type", 694877108e4SArnaldo Carvalho de Melo "read_format", 695877108e4SArnaldo Carvalho de Melo "disabled", 696877108e4SArnaldo Carvalho de Melo "inherit", 697877108e4SArnaldo Carvalho de Melo "pinned", 698877108e4SArnaldo Carvalho de Melo "exclusive", 699877108e4SArnaldo Carvalho de Melo "exclude_user", 700877108e4SArnaldo Carvalho de Melo "exclude_kernel", 701877108e4SArnaldo Carvalho de Melo "exclude_hv", 702877108e4SArnaldo Carvalho de Melo "exclude_idle", 703877108e4SArnaldo Carvalho de Melo "mmap", 704ae938802SArnaldo Carvalho de Melo "context_switch", 705877108e4SArnaldo Carvalho de Melo "comm", 706877108e4SArnaldo Carvalho de Melo "freq", 707877108e4SArnaldo Carvalho de Melo "inherit_stat", 708877108e4SArnaldo Carvalho de Melo "enable_on_exec", 709877108e4SArnaldo Carvalho de Melo "task", 710877108e4SArnaldo Carvalho de Melo "watermark", 711877108e4SArnaldo Carvalho de Melo "precise_ip", 712877108e4SArnaldo Carvalho de Melo "mmap_data", 713877108e4SArnaldo Carvalho de Melo "sample_id_all", 714877108e4SArnaldo Carvalho de Melo "wakeup_events", 715877108e4SArnaldo Carvalho de Melo "bp_type", 716877108e4SArnaldo Carvalho de Melo "bp_addr", 71764348153SFrederic Weisbecker "bp_len", 71864348153SFrederic Weisbecker NULL 71964348153SFrederic Weisbecker }; 720877108e4SArnaldo Carvalho de Melo u64 sample_period = 0; 721877108e4SArnaldo Carvalho de Melo u32 disabled = 0, 722877108e4SArnaldo Carvalho de Melo inherit = 0, 723877108e4SArnaldo Carvalho de Melo pinned = 0, 724877108e4SArnaldo Carvalho de Melo exclusive = 0, 725877108e4SArnaldo Carvalho de Melo exclude_user = 0, 726877108e4SArnaldo Carvalho de Melo exclude_kernel = 0, 727877108e4SArnaldo Carvalho de Melo exclude_hv = 0, 728877108e4SArnaldo Carvalho de Melo exclude_idle = 0, 729877108e4SArnaldo Carvalho de Melo mmap = 0, 730ae938802SArnaldo Carvalho de Melo context_switch = 0, 731877108e4SArnaldo Carvalho de Melo comm = 0, 732877108e4SArnaldo Carvalho de Melo freq = 1, 733877108e4SArnaldo Carvalho de Melo inherit_stat = 0, 734877108e4SArnaldo Carvalho de Melo enable_on_exec = 0, 735877108e4SArnaldo Carvalho de Melo task = 0, 736877108e4SArnaldo Carvalho de Melo watermark = 0, 737877108e4SArnaldo Carvalho de Melo precise_ip = 0, 738877108e4SArnaldo Carvalho de Melo mmap_data = 0, 739877108e4SArnaldo Carvalho de Melo sample_id_all = 1; 740877108e4SArnaldo Carvalho de Melo int idx = 0; 741877108e4SArnaldo Carvalho de Melo 742877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, 743ae938802SArnaldo Carvalho de Melo "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist, 744877108e4SArnaldo Carvalho de Melo &attr.type, &attr.config, &attr.sample_freq, 745877108e4SArnaldo Carvalho de Melo &sample_period, &attr.sample_type, 746877108e4SArnaldo Carvalho de Melo &attr.read_format, &disabled, &inherit, 747877108e4SArnaldo Carvalho de Melo &pinned, &exclusive, &exclude_user, 748877108e4SArnaldo Carvalho de Melo &exclude_kernel, &exclude_hv, &exclude_idle, 749ae938802SArnaldo Carvalho de Melo &mmap, &context_switch, &comm, &freq, &inherit_stat, 750877108e4SArnaldo Carvalho de Melo &enable_on_exec, &task, &watermark, 751877108e4SArnaldo Carvalho de Melo &precise_ip, &mmap_data, &sample_id_all, 752877108e4SArnaldo Carvalho de Melo &attr.wakeup_events, &attr.bp_type, 753877108e4SArnaldo Carvalho de Melo &attr.bp_addr, &attr.bp_len, &idx)) 754877108e4SArnaldo Carvalho de Melo return -1; 755877108e4SArnaldo Carvalho de Melo 756877108e4SArnaldo Carvalho de Melo /* union... */ 757877108e4SArnaldo Carvalho de Melo if (sample_period != 0) { 758877108e4SArnaldo Carvalho de Melo if (attr.sample_freq != 0) 759877108e4SArnaldo Carvalho de Melo return -1; /* FIXME: throw right exception */ 760877108e4SArnaldo Carvalho de Melo attr.sample_period = sample_period; 761877108e4SArnaldo Carvalho de Melo } 762877108e4SArnaldo Carvalho de Melo 763877108e4SArnaldo Carvalho de Melo /* Bitfields */ 764877108e4SArnaldo Carvalho de Melo attr.disabled = disabled; 765877108e4SArnaldo Carvalho de Melo attr.inherit = inherit; 766877108e4SArnaldo Carvalho de Melo attr.pinned = pinned; 767877108e4SArnaldo Carvalho de Melo attr.exclusive = exclusive; 768877108e4SArnaldo Carvalho de Melo attr.exclude_user = exclude_user; 769877108e4SArnaldo Carvalho de Melo attr.exclude_kernel = exclude_kernel; 770877108e4SArnaldo Carvalho de Melo attr.exclude_hv = exclude_hv; 771877108e4SArnaldo Carvalho de Melo attr.exclude_idle = exclude_idle; 772877108e4SArnaldo Carvalho de Melo attr.mmap = mmap; 773ae938802SArnaldo Carvalho de Melo attr.context_switch = context_switch; 774877108e4SArnaldo Carvalho de Melo attr.comm = comm; 775877108e4SArnaldo Carvalho de Melo attr.freq = freq; 776877108e4SArnaldo Carvalho de Melo attr.inherit_stat = inherit_stat; 777877108e4SArnaldo Carvalho de Melo attr.enable_on_exec = enable_on_exec; 778877108e4SArnaldo Carvalho de Melo attr.task = task; 779877108e4SArnaldo Carvalho de Melo attr.watermark = watermark; 780877108e4SArnaldo Carvalho de Melo attr.precise_ip = precise_ip; 781877108e4SArnaldo Carvalho de Melo attr.mmap_data = mmap_data; 782877108e4SArnaldo Carvalho de Melo attr.sample_id_all = sample_id_all; 783ad4e3c04SJiri Olsa attr.size = sizeof(attr); 784877108e4SArnaldo Carvalho de Melo 785877108e4SArnaldo Carvalho de Melo perf_evsel__init(&pevsel->evsel, &attr, idx); 786877108e4SArnaldo Carvalho de Melo return 0; 787877108e4SArnaldo Carvalho de Melo } 788877108e4SArnaldo Carvalho de Melo 789877108e4SArnaldo Carvalho de Melo static void pyrf_evsel__delete(struct pyrf_evsel *pevsel) 790877108e4SArnaldo Carvalho de Melo { 791877108e4SArnaldo Carvalho de Melo perf_evsel__exit(&pevsel->evsel); 79266dfdff0SJaroslav Škarvada Py_TYPE(pevsel)->tp_free((PyObject*)pevsel); 793877108e4SArnaldo Carvalho de Melo } 794877108e4SArnaldo Carvalho de Melo 795877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, 796877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 797877108e4SArnaldo Carvalho de Melo { 798877108e4SArnaldo Carvalho de Melo struct perf_evsel *evsel = &pevsel->evsel; 799877108e4SArnaldo Carvalho de Melo struct cpu_map *cpus = NULL; 800877108e4SArnaldo Carvalho de Melo struct thread_map *threads = NULL; 801877108e4SArnaldo Carvalho de Melo PyObject *pcpus = NULL, *pthreads = NULL; 8025d2cd909SArnaldo Carvalho de Melo int group = 0, inherit = 0; 80364348153SFrederic Weisbecker static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL }; 804877108e4SArnaldo Carvalho de Melo 805877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, 8065d2cd909SArnaldo Carvalho de Melo &pcpus, &pthreads, &group, &inherit)) 807877108e4SArnaldo Carvalho de Melo return NULL; 808877108e4SArnaldo Carvalho de Melo 809877108e4SArnaldo Carvalho de Melo if (pthreads != NULL) 810877108e4SArnaldo Carvalho de Melo threads = ((struct pyrf_thread_map *)pthreads)->threads; 811877108e4SArnaldo Carvalho de Melo 812877108e4SArnaldo Carvalho de Melo if (pcpus != NULL) 813877108e4SArnaldo Carvalho de Melo cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; 814877108e4SArnaldo Carvalho de Melo 8155d2cd909SArnaldo Carvalho de Melo evsel->attr.inherit = inherit; 816727ab04eSArnaldo Carvalho de Melo /* 817727ab04eSArnaldo Carvalho de Melo * This will group just the fds for this single evsel, to group 818727ab04eSArnaldo Carvalho de Melo * multiple events, use evlist.open(). 819727ab04eSArnaldo Carvalho de Melo */ 8206a4bb04cSJiri Olsa if (perf_evsel__open(evsel, cpus, threads) < 0) { 821877108e4SArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 822877108e4SArnaldo Carvalho de Melo return NULL; 823877108e4SArnaldo Carvalho de Melo } 824877108e4SArnaldo Carvalho de Melo 825877108e4SArnaldo Carvalho de Melo Py_INCREF(Py_None); 826877108e4SArnaldo Carvalho de Melo return Py_None; 827877108e4SArnaldo Carvalho de Melo } 828877108e4SArnaldo Carvalho de Melo 829877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evsel__methods[] = { 830877108e4SArnaldo Carvalho de Melo { 831877108e4SArnaldo Carvalho de Melo .ml_name = "open", 832877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evsel__open, 833877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 834877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("open the event selector file descriptor table.") 835877108e4SArnaldo Carvalho de Melo }, 836f6bbc1daSArnaldo Carvalho de Melo { .ml_name = NULL, } 837877108e4SArnaldo Carvalho de Melo }; 838877108e4SArnaldo Carvalho de Melo 839877108e4SArnaldo Carvalho de Melo static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object."); 840877108e4SArnaldo Carvalho de Melo 841877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evsel__type = { 842877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 843877108e4SArnaldo Carvalho de Melo .tp_name = "perf.evsel", 844877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_evsel), 845877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_evsel__delete, 846877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 847877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_evsel__doc, 848877108e4SArnaldo Carvalho de Melo .tp_methods = pyrf_evsel__methods, 849877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_evsel__init, 850877108e4SArnaldo Carvalho de Melo }; 851877108e4SArnaldo Carvalho de Melo 852877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__setup_types(void) 853877108e4SArnaldo Carvalho de Melo { 854877108e4SArnaldo Carvalho de Melo pyrf_evsel__type.tp_new = PyType_GenericNew; 855877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_evsel__type); 856877108e4SArnaldo Carvalho de Melo } 857877108e4SArnaldo Carvalho de Melo 858877108e4SArnaldo Carvalho de Melo struct pyrf_evlist { 859877108e4SArnaldo Carvalho de Melo PyObject_HEAD 860877108e4SArnaldo Carvalho de Melo 861877108e4SArnaldo Carvalho de Melo struct perf_evlist evlist; 862877108e4SArnaldo Carvalho de Melo }; 863877108e4SArnaldo Carvalho de Melo 864877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__init(struct pyrf_evlist *pevlist, 8651d037ca1SIrina Tirdea PyObject *args, PyObject *kwargs __maybe_unused) 866877108e4SArnaldo Carvalho de Melo { 8677e2ed097SArnaldo Carvalho de Melo PyObject *pcpus = NULL, *pthreads = NULL; 8687e2ed097SArnaldo Carvalho de Melo struct cpu_map *cpus; 8697e2ed097SArnaldo Carvalho de Melo struct thread_map *threads; 8707e2ed097SArnaldo Carvalho de Melo 8717e2ed097SArnaldo Carvalho de Melo if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads)) 8727e2ed097SArnaldo Carvalho de Melo return -1; 8737e2ed097SArnaldo Carvalho de Melo 8747e2ed097SArnaldo Carvalho de Melo threads = ((struct pyrf_thread_map *)pthreads)->threads; 8757e2ed097SArnaldo Carvalho de Melo cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; 8767e2ed097SArnaldo Carvalho de Melo perf_evlist__init(&pevlist->evlist, cpus, threads); 877877108e4SArnaldo Carvalho de Melo return 0; 878877108e4SArnaldo Carvalho de Melo } 879877108e4SArnaldo Carvalho de Melo 880877108e4SArnaldo Carvalho de Melo static void pyrf_evlist__delete(struct pyrf_evlist *pevlist) 881877108e4SArnaldo Carvalho de Melo { 882877108e4SArnaldo Carvalho de Melo perf_evlist__exit(&pevlist->evlist); 88366dfdff0SJaroslav Škarvada Py_TYPE(pevlist)->tp_free((PyObject*)pevlist); 884877108e4SArnaldo Carvalho de Melo } 885877108e4SArnaldo Carvalho de Melo 886877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, 887877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 888877108e4SArnaldo Carvalho de Melo { 889877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 89064348153SFrederic Weisbecker static char *kwlist[] = { "pages", "overwrite", NULL }; 891877108e4SArnaldo Carvalho de Melo int pages = 128, overwrite = false; 892877108e4SArnaldo Carvalho de Melo 8937e2ed097SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist, 8947e2ed097SArnaldo Carvalho de Melo &pages, &overwrite)) 895877108e4SArnaldo Carvalho de Melo return NULL; 896877108e4SArnaldo Carvalho de Melo 897f74b9d3aSWang Nan if (perf_evlist__mmap(evlist, pages) < 0) { 898877108e4SArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 899877108e4SArnaldo Carvalho de Melo return NULL; 900877108e4SArnaldo Carvalho de Melo } 901877108e4SArnaldo Carvalho de Melo 902877108e4SArnaldo Carvalho de Melo Py_INCREF(Py_None); 903877108e4SArnaldo Carvalho de Melo return Py_None; 904877108e4SArnaldo Carvalho de Melo } 905877108e4SArnaldo Carvalho de Melo 906877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist, 907877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 908877108e4SArnaldo Carvalho de Melo { 909877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 91064348153SFrederic Weisbecker static char *kwlist[] = { "timeout", NULL }; 911877108e4SArnaldo Carvalho de Melo int timeout = -1, n; 912877108e4SArnaldo Carvalho de Melo 913877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout)) 914877108e4SArnaldo Carvalho de Melo return NULL; 915877108e4SArnaldo Carvalho de Melo 916f66a889dSArnaldo Carvalho de Melo n = perf_evlist__poll(evlist, timeout); 917877108e4SArnaldo Carvalho de Melo if (n < 0) { 918877108e4SArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 919877108e4SArnaldo Carvalho de Melo return NULL; 920877108e4SArnaldo Carvalho de Melo } 921877108e4SArnaldo Carvalho de Melo 922877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", n); 923877108e4SArnaldo Carvalho de Melo } 924877108e4SArnaldo Carvalho de Melo 925877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist, 9261d037ca1SIrina Tirdea PyObject *args __maybe_unused, 9271d037ca1SIrina Tirdea PyObject *kwargs __maybe_unused) 928877108e4SArnaldo Carvalho de Melo { 929877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 930877108e4SArnaldo Carvalho de Melo PyObject *list = PyList_New(0); 931877108e4SArnaldo Carvalho de Melo int i; 932877108e4SArnaldo Carvalho de Melo 9331b85337dSArnaldo Carvalho de Melo for (i = 0; i < evlist->pollfd.nr; ++i) { 934877108e4SArnaldo Carvalho de Melo PyObject *file; 93566dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 9361b85337dSArnaldo Carvalho de Melo FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r"); 937877108e4SArnaldo Carvalho de Melo 938877108e4SArnaldo Carvalho de Melo if (fp == NULL) 939877108e4SArnaldo Carvalho de Melo goto free_list; 940877108e4SArnaldo Carvalho de Melo 941877108e4SArnaldo Carvalho de Melo file = PyFile_FromFile(fp, "perf", "r", NULL); 94266dfdff0SJaroslav Škarvada #else 943a389aeceSJiri Olsa file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1, 944a389aeceSJiri Olsa NULL, NULL, NULL, 0); 94566dfdff0SJaroslav Škarvada #endif 946877108e4SArnaldo Carvalho de Melo if (file == NULL) 947877108e4SArnaldo Carvalho de Melo goto free_list; 948877108e4SArnaldo Carvalho de Melo 949877108e4SArnaldo Carvalho de Melo if (PyList_Append(list, file) != 0) { 950877108e4SArnaldo Carvalho de Melo Py_DECREF(file); 951877108e4SArnaldo Carvalho de Melo goto free_list; 952877108e4SArnaldo Carvalho de Melo } 953877108e4SArnaldo Carvalho de Melo 954877108e4SArnaldo Carvalho de Melo Py_DECREF(file); 955877108e4SArnaldo Carvalho de Melo } 956877108e4SArnaldo Carvalho de Melo 957877108e4SArnaldo Carvalho de Melo return list; 958877108e4SArnaldo Carvalho de Melo free_list: 959877108e4SArnaldo Carvalho de Melo return PyErr_NoMemory(); 960877108e4SArnaldo Carvalho de Melo } 961877108e4SArnaldo Carvalho de Melo 962877108e4SArnaldo Carvalho de Melo 963877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist, 9641d037ca1SIrina Tirdea PyObject *args, 9651d037ca1SIrina Tirdea PyObject *kwargs __maybe_unused) 966877108e4SArnaldo Carvalho de Melo { 967877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 968877108e4SArnaldo Carvalho de Melo PyObject *pevsel; 969877108e4SArnaldo Carvalho de Melo struct perf_evsel *evsel; 970877108e4SArnaldo Carvalho de Melo 971877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTuple(args, "O", &pevsel)) 972877108e4SArnaldo Carvalho de Melo return NULL; 973877108e4SArnaldo Carvalho de Melo 974877108e4SArnaldo Carvalho de Melo Py_INCREF(pevsel); 975877108e4SArnaldo Carvalho de Melo evsel = &((struct pyrf_evsel *)pevsel)->evsel; 976877108e4SArnaldo Carvalho de Melo evsel->idx = evlist->nr_entries; 977877108e4SArnaldo Carvalho de Melo perf_evlist__add(evlist, evsel); 978877108e4SArnaldo Carvalho de Melo 979877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", evlist->nr_entries); 980877108e4SArnaldo Carvalho de Melo } 981877108e4SArnaldo Carvalho de Melo 982721f0dfcSJiri Olsa static struct perf_mmap *get_md(struct perf_evlist *evlist, int cpu) 983721f0dfcSJiri Olsa { 984721f0dfcSJiri Olsa int i; 985721f0dfcSJiri Olsa 986721f0dfcSJiri Olsa for (i = 0; i < evlist->nr_mmaps; i++) { 987721f0dfcSJiri Olsa struct perf_mmap *md = &evlist->mmap[i]; 988721f0dfcSJiri Olsa 989721f0dfcSJiri Olsa if (md->cpu == cpu) 990721f0dfcSJiri Olsa return md; 991721f0dfcSJiri Olsa } 992721f0dfcSJiri Olsa 993721f0dfcSJiri Olsa return NULL; 994721f0dfcSJiri Olsa } 995721f0dfcSJiri Olsa 996877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, 997877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 998877108e4SArnaldo Carvalho de Melo { 999877108e4SArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 1000877108e4SArnaldo Carvalho de Melo union perf_event *event; 1001877108e4SArnaldo Carvalho de Melo int sample_id_all = 1, cpu; 100264348153SFrederic Weisbecker static char *kwlist[] = { "cpu", "sample_id_all", NULL }; 100335b7cdc6SKan Liang struct perf_mmap *md; 10045538becaSFrederic Weisbecker int err; 1005877108e4SArnaldo Carvalho de Melo 1006877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, 1007877108e4SArnaldo Carvalho de Melo &cpu, &sample_id_all)) 1008877108e4SArnaldo Carvalho de Melo return NULL; 1009877108e4SArnaldo Carvalho de Melo 1010721f0dfcSJiri Olsa md = get_md(evlist, cpu); 1011721f0dfcSJiri Olsa if (!md) 1012721f0dfcSJiri Olsa return NULL; 1013721f0dfcSJiri Olsa 1014b9bae2c8SKan Liang if (perf_mmap__read_init(md) < 0) 101535b7cdc6SKan Liang goto end; 101635b7cdc6SKan Liang 10170019dc87SKan Liang event = perf_mmap__read_event(md); 1018877108e4SArnaldo Carvalho de Melo if (event != NULL) { 1019877108e4SArnaldo Carvalho de Melo PyObject *pyevent = pyrf_event__new(event); 1020877108e4SArnaldo Carvalho de Melo struct pyrf_event *pevent = (struct pyrf_event *)pyevent; 1021377f698dSJiri Olsa struct perf_evsel *evsel; 1022877108e4SArnaldo Carvalho de Melo 1023877108e4SArnaldo Carvalho de Melo if (pyevent == NULL) 1024877108e4SArnaldo Carvalho de Melo return PyErr_NoMemory(); 1025877108e4SArnaldo Carvalho de Melo 1026377f698dSJiri Olsa evsel = perf_evlist__event2evsel(evlist, event); 102783428f2fSPetr Machata if (!evsel) { 102883428f2fSPetr Machata Py_INCREF(Py_None); 1029377f698dSJiri Olsa return Py_None; 103083428f2fSPetr Machata } 1031377f698dSJiri Olsa 1032377f698dSJiri Olsa pevent->evsel = evsel; 1033377f698dSJiri Olsa 1034377f698dSJiri Olsa err = perf_evsel__parse_sample(evsel, event, &pevent->sample); 1035e8968e65SJiri Olsa 1036e8968e65SJiri Olsa /* Consume the even only after we parsed it out. */ 1037d6ace3dfSKan Liang perf_mmap__consume(md); 1038e8968e65SJiri Olsa 10395c6970afSArnaldo Carvalho de Melo if (err) 10405c6970afSArnaldo Carvalho de Melo return PyErr_Format(PyExc_OSError, 10415c6970afSArnaldo Carvalho de Melo "perf: can't parse sample, err=%d", err); 10425538becaSFrederic Weisbecker return pyevent; 10435538becaSFrederic Weisbecker } 104435b7cdc6SKan Liang end: 1045877108e4SArnaldo Carvalho de Melo Py_INCREF(Py_None); 1046877108e4SArnaldo Carvalho de Melo return Py_None; 1047877108e4SArnaldo Carvalho de Melo } 1048877108e4SArnaldo Carvalho de Melo 1049727ab04eSArnaldo Carvalho de Melo static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist, 1050727ab04eSArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 1051727ab04eSArnaldo Carvalho de Melo { 1052727ab04eSArnaldo Carvalho de Melo struct perf_evlist *evlist = &pevlist->evlist; 1053727ab04eSArnaldo Carvalho de Melo int group = 0; 1054727ab04eSArnaldo Carvalho de Melo static char *kwlist[] = { "group", NULL }; 1055727ab04eSArnaldo Carvalho de Melo 1056727ab04eSArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group)) 1057727ab04eSArnaldo Carvalho de Melo return NULL; 1058727ab04eSArnaldo Carvalho de Melo 10596a4bb04cSJiri Olsa if (group) 106063dab225SArnaldo Carvalho de Melo perf_evlist__set_leader(evlist); 10616a4bb04cSJiri Olsa 10626a4bb04cSJiri Olsa if (perf_evlist__open(evlist) < 0) { 1063727ab04eSArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 1064727ab04eSArnaldo Carvalho de Melo return NULL; 1065727ab04eSArnaldo Carvalho de Melo } 1066727ab04eSArnaldo Carvalho de Melo 1067727ab04eSArnaldo Carvalho de Melo Py_INCREF(Py_None); 1068727ab04eSArnaldo Carvalho de Melo return Py_None; 1069727ab04eSArnaldo Carvalho de Melo } 1070727ab04eSArnaldo Carvalho de Melo 1071877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evlist__methods[] = { 1072877108e4SArnaldo Carvalho de Melo { 1073877108e4SArnaldo Carvalho de Melo .ml_name = "mmap", 1074877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__mmap, 1075877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1076877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("mmap the file descriptor table.") 1077877108e4SArnaldo Carvalho de Melo }, 1078877108e4SArnaldo Carvalho de Melo { 1079727ab04eSArnaldo Carvalho de Melo .ml_name = "open", 1080727ab04eSArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__open, 1081727ab04eSArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1082727ab04eSArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("open the file descriptors.") 1083727ab04eSArnaldo Carvalho de Melo }, 1084727ab04eSArnaldo Carvalho de Melo { 1085877108e4SArnaldo Carvalho de Melo .ml_name = "poll", 1086877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__poll, 1087877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1088877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("poll the file descriptor table.") 1089877108e4SArnaldo Carvalho de Melo }, 1090877108e4SArnaldo Carvalho de Melo { 1091877108e4SArnaldo Carvalho de Melo .ml_name = "get_pollfd", 1092877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd, 1093877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1094877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("get the poll file descriptor table.") 1095877108e4SArnaldo Carvalho de Melo }, 1096877108e4SArnaldo Carvalho de Melo { 1097877108e4SArnaldo Carvalho de Melo .ml_name = "add", 1098877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__add, 1099877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1100877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("adds an event selector to the list.") 1101877108e4SArnaldo Carvalho de Melo }, 1102877108e4SArnaldo Carvalho de Melo { 1103877108e4SArnaldo Carvalho de Melo .ml_name = "read_on_cpu", 1104877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu, 1105877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1106877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("reads an event.") 1107877108e4SArnaldo Carvalho de Melo }, 1108f6bbc1daSArnaldo Carvalho de Melo { .ml_name = NULL, } 1109877108e4SArnaldo Carvalho de Melo }; 1110877108e4SArnaldo Carvalho de Melo 1111877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_evlist__length(PyObject *obj) 1112877108e4SArnaldo Carvalho de Melo { 1113877108e4SArnaldo Carvalho de Melo struct pyrf_evlist *pevlist = (void *)obj; 1114877108e4SArnaldo Carvalho de Melo 1115877108e4SArnaldo Carvalho de Melo return pevlist->evlist.nr_entries; 1116877108e4SArnaldo Carvalho de Melo } 1117877108e4SArnaldo Carvalho de Melo 1118877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i) 1119877108e4SArnaldo Carvalho de Melo { 1120877108e4SArnaldo Carvalho de Melo struct pyrf_evlist *pevlist = (void *)obj; 1121877108e4SArnaldo Carvalho de Melo struct perf_evsel *pos; 1122877108e4SArnaldo Carvalho de Melo 1123877108e4SArnaldo Carvalho de Melo if (i >= pevlist->evlist.nr_entries) 1124877108e4SArnaldo Carvalho de Melo return NULL; 1125877108e4SArnaldo Carvalho de Melo 1126e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(&pevlist->evlist, pos) { 1127877108e4SArnaldo Carvalho de Melo if (i-- == 0) 1128877108e4SArnaldo Carvalho de Melo break; 11290050f7aaSArnaldo Carvalho de Melo } 1130877108e4SArnaldo Carvalho de Melo 1131877108e4SArnaldo Carvalho de Melo return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel)); 1132877108e4SArnaldo Carvalho de Melo } 1133877108e4SArnaldo Carvalho de Melo 1134877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_evlist__sequence_methods = { 1135877108e4SArnaldo Carvalho de Melo .sq_length = pyrf_evlist__length, 1136877108e4SArnaldo Carvalho de Melo .sq_item = pyrf_evlist__item, 1137877108e4SArnaldo Carvalho de Melo }; 1138877108e4SArnaldo Carvalho de Melo 1139877108e4SArnaldo Carvalho de Melo static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object."); 1140877108e4SArnaldo Carvalho de Melo 1141877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evlist__type = { 1142877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 1143877108e4SArnaldo Carvalho de Melo .tp_name = "perf.evlist", 1144877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_evlist), 1145877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_evlist__delete, 1146877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 1147877108e4SArnaldo Carvalho de Melo .tp_as_sequence = &pyrf_evlist__sequence_methods, 1148877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_evlist__doc, 1149877108e4SArnaldo Carvalho de Melo .tp_methods = pyrf_evlist__methods, 1150877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_evlist__init, 1151877108e4SArnaldo Carvalho de Melo }; 1152877108e4SArnaldo Carvalho de Melo 1153877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__setup_types(void) 1154877108e4SArnaldo Carvalho de Melo { 1155877108e4SArnaldo Carvalho de Melo pyrf_evlist__type.tp_new = PyType_GenericNew; 1156877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_evlist__type); 1157877108e4SArnaldo Carvalho de Melo } 1158877108e4SArnaldo Carvalho de Melo 11595865fe36SArnaldo Carvalho de Melo #define PERF_CONST(name) { #name, PERF_##name } 11605865fe36SArnaldo Carvalho de Melo 1161877108e4SArnaldo Carvalho de Melo static struct { 1162877108e4SArnaldo Carvalho de Melo const char *name; 1163877108e4SArnaldo Carvalho de Melo int value; 1164877108e4SArnaldo Carvalho de Melo } perf__constants[] = { 11655865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_HARDWARE), 11665865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_SOFTWARE), 11675865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_TRACEPOINT), 11685865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_HW_CACHE), 11695865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_RAW), 11705865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_BREAKPOINT), 1171877108e4SArnaldo Carvalho de Melo 11725865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CPU_CYCLES), 11735865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_INSTRUCTIONS), 11745865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_REFERENCES), 11755865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_MISSES), 11765865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS), 11775865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_BRANCH_MISSES), 11785865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_BUS_CYCLES), 11795865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_L1D), 11805865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_L1I), 11815865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_LL), 11825865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_DTLB), 11835865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_ITLB), 11845865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_BPU), 11855865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_OP_READ), 11865865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_OP_WRITE), 11875865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH), 11885865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS), 11895865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_RESULT_MISS), 1190877108e4SArnaldo Carvalho de Melo 11915865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND), 11925865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND), 1193129c04cbSIngo Molnar 11945865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_CPU_CLOCK), 11955865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_TASK_CLOCK), 11965865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_PAGE_FAULTS), 11975865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_CONTEXT_SWITCHES), 11985865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_CPU_MIGRATIONS), 11995865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN), 12005865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ), 12015865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS), 12025865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_EMULATION_FAULTS), 12035865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_DUMMY), 1204877108e4SArnaldo Carvalho de Melo 12055865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_IP), 12065865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_TID), 12075865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_TIME), 12085865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_ADDR), 12095865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_READ), 12105865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_CALLCHAIN), 12115865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_ID), 12125865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_CPU), 12135865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_PERIOD), 12145865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_STREAM_ID), 12155865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_RAW), 1216877108e4SArnaldo Carvalho de Melo 12175865fe36SArnaldo Carvalho de Melo PERF_CONST(FORMAT_TOTAL_TIME_ENABLED), 12185865fe36SArnaldo Carvalho de Melo PERF_CONST(FORMAT_TOTAL_TIME_RUNNING), 12195865fe36SArnaldo Carvalho de Melo PERF_CONST(FORMAT_ID), 12205865fe36SArnaldo Carvalho de Melo PERF_CONST(FORMAT_GROUP), 1221877108e4SArnaldo Carvalho de Melo 12225865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_MMAP), 12235865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_LOST), 12245865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_COMM), 12255865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_EXIT), 12265865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_THROTTLE), 12275865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_UNTHROTTLE), 12285865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_FORK), 12295865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_READ), 12305865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_SAMPLE), 123184576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_MMAP2), 123284576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_AUX), 123384576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_ITRACE_START), 123484576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_LOST_SAMPLES), 123584576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_SWITCH), 123684576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_SWITCH_CPU_WIDE), 1237ae938802SArnaldo Carvalho de Melo 1238ae938802SArnaldo Carvalho de Melo PERF_CONST(RECORD_MISC_SWITCH_OUT), 1239f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 1240877108e4SArnaldo Carvalho de Melo }; 1241877108e4SArnaldo Carvalho de Melo 12421075fbb2SJiri Olsa static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel, 12431075fbb2SJiri Olsa PyObject *args, PyObject *kwargs) 12441075fbb2SJiri Olsa { 124597fbf3f0STzvetomir Stoyanov struct tep_event *tp_format; 12461075fbb2SJiri Olsa static char *kwlist[] = { "sys", "name", NULL }; 12471075fbb2SJiri Olsa char *sys = NULL; 12481075fbb2SJiri Olsa char *name = NULL; 12491075fbb2SJiri Olsa 12501075fbb2SJiri Olsa if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist, 12511075fbb2SJiri Olsa &sys, &name)) 12521075fbb2SJiri Olsa return NULL; 12531075fbb2SJiri Olsa 12541075fbb2SJiri Olsa tp_format = trace_event__tp_format(sys, name); 12551075fbb2SJiri Olsa if (IS_ERR(tp_format)) 125666dfdff0SJaroslav Škarvada return _PyLong_FromLong(-1); 12571075fbb2SJiri Olsa 125866dfdff0SJaroslav Škarvada return _PyLong_FromLong(tp_format->id); 12591075fbb2SJiri Olsa } 12601075fbb2SJiri Olsa 1261877108e4SArnaldo Carvalho de Melo static PyMethodDef perf__methods[] = { 12621075fbb2SJiri Olsa { 12631075fbb2SJiri Olsa .ml_name = "tracepoint", 12641075fbb2SJiri Olsa .ml_meth = (PyCFunction) pyrf__tracepoint, 12651075fbb2SJiri Olsa .ml_flags = METH_VARARGS | METH_KEYWORDS, 12661075fbb2SJiri Olsa .ml_doc = PyDoc_STR("Get tracepoint config.") 12671075fbb2SJiri Olsa }, 1268f6bbc1daSArnaldo Carvalho de Melo { .ml_name = NULL, } 1269877108e4SArnaldo Carvalho de Melo }; 1270877108e4SArnaldo Carvalho de Melo 127166dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 1272877108e4SArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void) 127366dfdff0SJaroslav Škarvada #else 127466dfdff0SJaroslav Škarvada PyMODINIT_FUNC PyInit_perf(void) 127566dfdff0SJaroslav Škarvada #endif 1276877108e4SArnaldo Carvalho de Melo { 1277877108e4SArnaldo Carvalho de Melo PyObject *obj; 1278877108e4SArnaldo Carvalho de Melo int i; 127966dfdff0SJaroslav Škarvada PyObject *dict; 128066dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 128166dfdff0SJaroslav Škarvada PyObject *module = Py_InitModule("perf", perf__methods); 128266dfdff0SJaroslav Škarvada #else 128366dfdff0SJaroslav Škarvada static struct PyModuleDef moduledef = { 128466dfdff0SJaroslav Škarvada PyModuleDef_HEAD_INIT, 128566dfdff0SJaroslav Škarvada "perf", /* m_name */ 128666dfdff0SJaroslav Škarvada "", /* m_doc */ 128766dfdff0SJaroslav Škarvada -1, /* m_size */ 128866dfdff0SJaroslav Škarvada perf__methods, /* m_methods */ 128966dfdff0SJaroslav Škarvada NULL, /* m_reload */ 129066dfdff0SJaroslav Škarvada NULL, /* m_traverse */ 129166dfdff0SJaroslav Škarvada NULL, /* m_clear */ 129266dfdff0SJaroslav Škarvada NULL, /* m_free */ 129366dfdff0SJaroslav Škarvada }; 129466dfdff0SJaroslav Škarvada PyObject *module = PyModule_Create(&moduledef); 129566dfdff0SJaroslav Škarvada #endif 1296877108e4SArnaldo Carvalho de Melo 1297877108e4SArnaldo Carvalho de Melo if (module == NULL || 1298877108e4SArnaldo Carvalho de Melo pyrf_event__setup_types() < 0 || 1299877108e4SArnaldo Carvalho de Melo pyrf_evlist__setup_types() < 0 || 1300877108e4SArnaldo Carvalho de Melo pyrf_evsel__setup_types() < 0 || 1301877108e4SArnaldo Carvalho de Melo pyrf_thread_map__setup_types() < 0 || 1302877108e4SArnaldo Carvalho de Melo pyrf_cpu_map__setup_types() < 0) 130366dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 1304877108e4SArnaldo Carvalho de Melo return; 130566dfdff0SJaroslav Škarvada #else 130666dfdff0SJaroslav Škarvada return module; 130766dfdff0SJaroslav Škarvada #endif 1308877108e4SArnaldo Carvalho de Melo 1309918512b4SJiri Olsa /* The page_size is placed in util object. */ 13100da2e9c2SArnaldo Carvalho de Melo page_size = sysconf(_SC_PAGE_SIZE); 13110da2e9c2SArnaldo Carvalho de Melo 1312877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_evlist__type); 1313877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type); 1314877108e4SArnaldo Carvalho de Melo 1315877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_evsel__type); 1316877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type); 1317877108e4SArnaldo Carvalho de Melo 131885e37de3SJiri Olsa Py_INCREF(&pyrf_mmap_event__type); 131985e37de3SJiri Olsa PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type); 132085e37de3SJiri Olsa 132185e37de3SJiri Olsa Py_INCREF(&pyrf_lost_event__type); 132285e37de3SJiri Olsa PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type); 132385e37de3SJiri Olsa 132485e37de3SJiri Olsa Py_INCREF(&pyrf_comm_event__type); 132585e37de3SJiri Olsa PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type); 132685e37de3SJiri Olsa 132785e37de3SJiri Olsa Py_INCREF(&pyrf_task_event__type); 132885e37de3SJiri Olsa PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type); 132985e37de3SJiri Olsa 133085e37de3SJiri Olsa Py_INCREF(&pyrf_throttle_event__type); 133185e37de3SJiri Olsa PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type); 133285e37de3SJiri Olsa 133385e37de3SJiri Olsa Py_INCREF(&pyrf_task_event__type); 133485e37de3SJiri Olsa PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type); 133585e37de3SJiri Olsa 133685e37de3SJiri Olsa Py_INCREF(&pyrf_read_event__type); 133785e37de3SJiri Olsa PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type); 133885e37de3SJiri Olsa 133985e37de3SJiri Olsa Py_INCREF(&pyrf_sample_event__type); 134085e37de3SJiri Olsa PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type); 134185e37de3SJiri Olsa 134285e37de3SJiri Olsa Py_INCREF(&pyrf_context_switch_event__type); 134385e37de3SJiri Olsa PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type); 134485e37de3SJiri Olsa 1345877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_thread_map__type); 1346877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type); 1347877108e4SArnaldo Carvalho de Melo 1348877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_cpu_map__type); 1349877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type); 1350877108e4SArnaldo Carvalho de Melo 1351877108e4SArnaldo Carvalho de Melo dict = PyModule_GetDict(module); 1352877108e4SArnaldo Carvalho de Melo if (dict == NULL) 1353877108e4SArnaldo Carvalho de Melo goto error; 1354877108e4SArnaldo Carvalho de Melo 1355877108e4SArnaldo Carvalho de Melo for (i = 0; perf__constants[i].name != NULL; i++) { 135666dfdff0SJaroslav Škarvada obj = _PyLong_FromLong(perf__constants[i].value); 1357877108e4SArnaldo Carvalho de Melo if (obj == NULL) 1358877108e4SArnaldo Carvalho de Melo goto error; 1359877108e4SArnaldo Carvalho de Melo PyDict_SetItemString(dict, perf__constants[i].name, obj); 1360877108e4SArnaldo Carvalho de Melo Py_DECREF(obj); 1361877108e4SArnaldo Carvalho de Melo } 1362877108e4SArnaldo Carvalho de Melo 1363877108e4SArnaldo Carvalho de Melo error: 1364877108e4SArnaldo Carvalho de Melo if (PyErr_Occurred()) 1365877108e4SArnaldo Carvalho de Melo PyErr_SetString(PyExc_ImportError, "perf: Init failed!"); 136666dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION >= 3 136766dfdff0SJaroslav Škarvada return module; 136866dfdff0SJaroslav Škarvada #endif 1369877108e4SArnaldo Carvalho de Melo } 13700c6332e9SArnaldo Carvalho de Melo 13710c6332e9SArnaldo Carvalho de Melo /* 13720c6332e9SArnaldo Carvalho de Melo * Dummy, to avoid dragging all the test_attr infrastructure in the python 13730c6332e9SArnaldo Carvalho de Melo * binding. 13740c6332e9SArnaldo Carvalho de Melo */ 13750c6332e9SArnaldo Carvalho de Melo void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu, 13760c6332e9SArnaldo Carvalho de Melo int fd, int group_fd, unsigned long flags) 13770c6332e9SArnaldo Carvalho de Melo { 13780c6332e9SArnaldo Carvalho de Melo } 1379