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> 79c3516d1SJiri Olsa #include <perf/cpumap.h> 8f2a39fe8SArnaldo Carvalho de Melo #include <traceevent/event-parse.h> 9877108e4SArnaldo Carvalho de Melo #include "evlist.h" 1056e2e056SArnaldo Carvalho de Melo #include "callchain.h" 11877108e4SArnaldo Carvalho de Melo #include "evsel.h" 12877108e4SArnaldo Carvalho de Melo #include "event.h" 13fea01392SArnaldo Carvalho de Melo #include "print_binary.h" 14877108e4SArnaldo Carvalho de Melo #include "thread_map.h" 15f2a39fe8SArnaldo Carvalho de Melo #include "trace-event.h" 16721f0dfcSJiri Olsa #include "mmap.h" 171b2fc358SArnaldo Carvalho de Melo #include "util.h" 1891854f9aSArnaldo Carvalho de Melo #include "../perf-sys.h" 19877108e4SArnaldo Carvalho de Melo 2066dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 2166dfdff0SJaroslav Škarvada #define _PyUnicode_FromString(arg) \ 2266dfdff0SJaroslav Škarvada PyString_FromString(arg) 2366dfdff0SJaroslav Škarvada #define _PyUnicode_AsString(arg) \ 2466dfdff0SJaroslav Škarvada PyString_AsString(arg) 2566dfdff0SJaroslav Škarvada #define _PyUnicode_FromFormat(...) \ 2666dfdff0SJaroslav Škarvada PyString_FromFormat(__VA_ARGS__) 2766dfdff0SJaroslav Škarvada #define _PyLong_FromLong(arg) \ 2866dfdff0SJaroslav Škarvada PyInt_FromLong(arg) 2966dfdff0SJaroslav Škarvada 3066dfdff0SJaroslav Škarvada #else 3166dfdff0SJaroslav Škarvada 3266dfdff0SJaroslav Škarvada #define _PyUnicode_FromString(arg) \ 3366dfdff0SJaroslav Škarvada PyUnicode_FromString(arg) 3466dfdff0SJaroslav Škarvada #define _PyUnicode_FromFormat(...) \ 3566dfdff0SJaroslav Škarvada PyUnicode_FromFormat(__VA_ARGS__) 3666dfdff0SJaroslav Škarvada #define _PyLong_FromLong(arg) \ 3766dfdff0SJaroslav Škarvada PyLong_FromLong(arg) 3866dfdff0SJaroslav Škarvada #endif 3966dfdff0SJaroslav Škarvada 4066dfdff0SJaroslav Škarvada #ifndef Py_TYPE 4166dfdff0SJaroslav Škarvada #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) 4266dfdff0SJaroslav Škarvada #endif 4366dfdff0SJaroslav Škarvada 448afb4c01SAdrian Hunter /* 4556e2e056SArnaldo Carvalho de Melo * Provide these two so that we don't have to link against callchain.c and 4656e2e056SArnaldo Carvalho de Melo * start dragging hist.c, etc. 4756e2e056SArnaldo Carvalho de Melo */ 4856e2e056SArnaldo Carvalho de Melo struct callchain_param callchain_param; 4956e2e056SArnaldo Carvalho de Melo 5056e2e056SArnaldo Carvalho de Melo int parse_callchain_record(const char *arg __maybe_unused, 5156e2e056SArnaldo Carvalho de Melo struct callchain_param *param __maybe_unused) 5256e2e056SArnaldo Carvalho de Melo { 5356e2e056SArnaldo Carvalho de Melo return 0; 5456e2e056SArnaldo Carvalho de Melo } 5556e2e056SArnaldo Carvalho de Melo 5656e2e056SArnaldo Carvalho de Melo /* 578afb4c01SAdrian Hunter * Support debug printing even though util/debug.c is not linked. That means 588afb4c01SAdrian Hunter * implementing 'verbose' and 'eprintf'. 598afb4c01SAdrian Hunter */ 608afb4c01SAdrian Hunter int verbose; 618afb4c01SAdrian Hunter 62*5939caccSArnaldo Carvalho de Melo int eprintf(int level, int var, const char *fmt, ...); 63*5939caccSArnaldo Carvalho de Melo 64c95688aaSJiri Olsa int eprintf(int level, int var, const char *fmt, ...) 658afb4c01SAdrian Hunter { 668afb4c01SAdrian Hunter va_list args; 678afb4c01SAdrian Hunter int ret = 0; 688afb4c01SAdrian Hunter 69c95688aaSJiri Olsa if (var >= level) { 708afb4c01SAdrian Hunter va_start(args, fmt); 718afb4c01SAdrian Hunter ret = vfprintf(stderr, fmt, args); 728afb4c01SAdrian Hunter va_end(args); 738afb4c01SAdrian Hunter } 748afb4c01SAdrian Hunter 758afb4c01SAdrian Hunter return ret; 768afb4c01SAdrian Hunter } 778afb4c01SAdrian Hunter 78cfff2d90SFrederic Weisbecker /* Define PyVarObject_HEAD_INIT for python 2.5 */ 79cfff2d90SFrederic Weisbecker #ifndef PyVarObject_HEAD_INIT 80cfff2d90SFrederic Weisbecker # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, 81cfff2d90SFrederic Weisbecker #endif 82cfff2d90SFrederic Weisbecker 8366dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 84f6bbc1daSArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void); 8566dfdff0SJaroslav Škarvada #else 8666dfdff0SJaroslav Škarvada PyMODINIT_FUNC PyInit_perf(void); 8766dfdff0SJaroslav Škarvada #endif 88f6bbc1daSArnaldo Carvalho de Melo 89877108e4SArnaldo Carvalho de Melo #define member_def(type, member, ptype, help) \ 90877108e4SArnaldo Carvalho de Melo { #member, ptype, \ 91877108e4SArnaldo Carvalho de Melo offsetof(struct pyrf_event, event) + offsetof(struct type, member), \ 92877108e4SArnaldo Carvalho de Melo 0, help } 93877108e4SArnaldo Carvalho de Melo 94877108e4SArnaldo Carvalho de Melo #define sample_member_def(name, member, ptype, help) \ 95877108e4SArnaldo Carvalho de Melo { #name, ptype, \ 96877108e4SArnaldo Carvalho de Melo offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \ 97877108e4SArnaldo Carvalho de Melo 0, help } 98877108e4SArnaldo Carvalho de Melo 99877108e4SArnaldo Carvalho de Melo struct pyrf_event { 100877108e4SArnaldo Carvalho de Melo PyObject_HEAD 10132dcd021SJiri Olsa struct evsel *evsel; 102877108e4SArnaldo Carvalho de Melo struct perf_sample sample; 103877108e4SArnaldo Carvalho de Melo union perf_event event; 104877108e4SArnaldo Carvalho de Melo }; 105877108e4SArnaldo Carvalho de Melo 106877108e4SArnaldo Carvalho de Melo #define sample_members \ 107f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \ 108877108e4SArnaldo Carvalho de Melo sample_member_def(sample_pid, pid, T_INT, "event pid"), \ 109877108e4SArnaldo Carvalho de Melo sample_member_def(sample_tid, tid, T_INT, "event tid"), \ 110f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \ 111f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \ 112f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \ 113f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \ 114f6bbc1daSArnaldo Carvalho de Melo sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \ 115877108e4SArnaldo Carvalho de Melo sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"), 116877108e4SArnaldo Carvalho de Melo 117877108e4SArnaldo Carvalho de Melo static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object."); 118877108e4SArnaldo Carvalho de Melo 119877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_mmap_event__members[] = { 120877108e4SArnaldo Carvalho de Melo sample_members 121877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 122ae938802SArnaldo Carvalho de Melo member_def(perf_event_header, misc, T_UINT, "event misc"), 12369d81f09SArnaldo Carvalho de Melo member_def(perf_record_mmap, pid, T_UINT, "event pid"), 12469d81f09SArnaldo Carvalho de Melo member_def(perf_record_mmap, tid, T_UINT, "event tid"), 12569d81f09SArnaldo Carvalho de Melo member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"), 12669d81f09SArnaldo Carvalho de Melo member_def(perf_record_mmap, len, T_ULONGLONG, "map length"), 12769d81f09SArnaldo Carvalho de Melo member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"), 12869d81f09SArnaldo Carvalho de Melo member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"), 129f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 130877108e4SArnaldo Carvalho de Melo }; 131877108e4SArnaldo Carvalho de Melo 132877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent) 133877108e4SArnaldo Carvalho de Melo { 134877108e4SArnaldo Carvalho de Melo PyObject *ret; 135877108e4SArnaldo Carvalho de Melo char *s; 136877108e4SArnaldo Carvalho de Melo 1371345e2eeSJiri Olsa if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", " 1381345e2eeSJiri Olsa "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", " 139877108e4SArnaldo Carvalho de Melo "filename: %s }", 140877108e4SArnaldo Carvalho de Melo pevent->event.mmap.pid, pevent->event.mmap.tid, 141877108e4SArnaldo Carvalho de Melo pevent->event.mmap.start, pevent->event.mmap.len, 142877108e4SArnaldo Carvalho de Melo pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) { 143877108e4SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 144877108e4SArnaldo Carvalho de Melo } else { 14566dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString(s); 146877108e4SArnaldo Carvalho de Melo free(s); 147877108e4SArnaldo Carvalho de Melo } 148877108e4SArnaldo Carvalho de Melo return ret; 149877108e4SArnaldo Carvalho de Melo } 150877108e4SArnaldo Carvalho de Melo 151877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_mmap_event__type = { 152877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 153877108e4SArnaldo Carvalho de Melo .tp_name = "perf.mmap_event", 154877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 155877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 156877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_mmap_event__doc, 157877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_mmap_event__members, 158877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_mmap_event__repr, 159877108e4SArnaldo Carvalho de Melo }; 160877108e4SArnaldo Carvalho de Melo 161877108e4SArnaldo Carvalho de Melo static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object."); 162877108e4SArnaldo Carvalho de Melo 163877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_task_event__members[] = { 164877108e4SArnaldo Carvalho de Melo sample_members 165877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 16669d81f09SArnaldo Carvalho de Melo member_def(perf_record_fork, pid, T_UINT, "event pid"), 16769d81f09SArnaldo Carvalho de Melo member_def(perf_record_fork, ppid, T_UINT, "event ppid"), 16869d81f09SArnaldo Carvalho de Melo member_def(perf_record_fork, tid, T_UINT, "event tid"), 16969d81f09SArnaldo Carvalho de Melo member_def(perf_record_fork, ptid, T_UINT, "event ptid"), 17069d81f09SArnaldo Carvalho de Melo member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"), 171f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 172877108e4SArnaldo Carvalho de Melo }; 173877108e4SArnaldo Carvalho de Melo 174877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent) 175877108e4SArnaldo Carvalho de Melo { 17666dfdff0SJaroslav Škarvada return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, " 177bceb59b1SJiri Olsa "ptid: %u, time: %" PRI_lu64 "}", 178877108e4SArnaldo Carvalho de Melo pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit", 179877108e4SArnaldo Carvalho de Melo pevent->event.fork.pid, 180877108e4SArnaldo Carvalho de Melo pevent->event.fork.ppid, 181877108e4SArnaldo Carvalho de Melo pevent->event.fork.tid, 182877108e4SArnaldo Carvalho de Melo pevent->event.fork.ptid, 183877108e4SArnaldo Carvalho de Melo pevent->event.fork.time); 184877108e4SArnaldo Carvalho de Melo } 185877108e4SArnaldo Carvalho de Melo 186877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_task_event__type = { 187877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 188877108e4SArnaldo Carvalho de Melo .tp_name = "perf.task_event", 189877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 190877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 191877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_task_event__doc, 192877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_task_event__members, 193877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_task_event__repr, 194877108e4SArnaldo Carvalho de Melo }; 195877108e4SArnaldo Carvalho de Melo 196877108e4SArnaldo Carvalho de Melo static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object."); 197877108e4SArnaldo Carvalho de Melo 198877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_comm_event__members[] = { 199877108e4SArnaldo Carvalho de Melo sample_members 200877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 20169d81f09SArnaldo Carvalho de Melo member_def(perf_record_comm, pid, T_UINT, "event pid"), 20269d81f09SArnaldo Carvalho de Melo member_def(perf_record_comm, tid, T_UINT, "event tid"), 20369d81f09SArnaldo Carvalho de Melo member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"), 204f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 205877108e4SArnaldo Carvalho de Melo }; 206877108e4SArnaldo Carvalho de Melo 207877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent) 208877108e4SArnaldo Carvalho de Melo { 20966dfdff0SJaroslav Škarvada return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }", 210877108e4SArnaldo Carvalho de Melo pevent->event.comm.pid, 211877108e4SArnaldo Carvalho de Melo pevent->event.comm.tid, 212877108e4SArnaldo Carvalho de Melo pevent->event.comm.comm); 213877108e4SArnaldo Carvalho de Melo } 214877108e4SArnaldo Carvalho de Melo 215877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_comm_event__type = { 216877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 217877108e4SArnaldo Carvalho de Melo .tp_name = "perf.comm_event", 218877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 219877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 220877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_comm_event__doc, 221877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_comm_event__members, 222877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_comm_event__repr, 223877108e4SArnaldo Carvalho de Melo }; 224877108e4SArnaldo Carvalho de Melo 225877108e4SArnaldo Carvalho de Melo static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object."); 226877108e4SArnaldo Carvalho de Melo 227877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_throttle_event__members[] = { 228877108e4SArnaldo Carvalho de Melo sample_members 229877108e4SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 23069d81f09SArnaldo Carvalho de Melo member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"), 23169d81f09SArnaldo Carvalho de Melo member_def(perf_record_throttle, id, T_ULONGLONG, "event id"), 23269d81f09SArnaldo Carvalho de Melo member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"), 233f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 234877108e4SArnaldo Carvalho de Melo }; 235877108e4SArnaldo Carvalho de Melo 236877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent) 237877108e4SArnaldo Carvalho de Melo { 23869d81f09SArnaldo Carvalho de Melo struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1); 239877108e4SArnaldo Carvalho de Melo 240003c66feSJiri Olsa return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64 241003c66feSJiri Olsa ", stream_id: %" PRI_lu64 " }", 242877108e4SArnaldo Carvalho de Melo pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un", 243877108e4SArnaldo Carvalho de Melo te->time, te->id, te->stream_id); 244877108e4SArnaldo Carvalho de Melo } 245877108e4SArnaldo Carvalho de Melo 246877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_throttle_event__type = { 247877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 248877108e4SArnaldo Carvalho de Melo .tp_name = "perf.throttle_event", 249877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 250877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 251877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_throttle_event__doc, 252877108e4SArnaldo Carvalho de Melo .tp_members = pyrf_throttle_event__members, 253877108e4SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_throttle_event__repr, 254877108e4SArnaldo Carvalho de Melo }; 255877108e4SArnaldo Carvalho de Melo 2563e9f45a7SArnaldo Carvalho de Melo static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object."); 2573e9f45a7SArnaldo Carvalho de Melo 2583e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_lost_event__members[] = { 2593e9f45a7SArnaldo Carvalho de Melo sample_members 26069d81f09SArnaldo Carvalho de Melo member_def(perf_record_lost, id, T_ULONGLONG, "event id"), 26169d81f09SArnaldo Carvalho de Melo member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"), 2623e9f45a7SArnaldo Carvalho de Melo { .name = NULL, }, 2633e9f45a7SArnaldo Carvalho de Melo }; 2643e9f45a7SArnaldo Carvalho de Melo 2653e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent) 2663e9f45a7SArnaldo Carvalho de Melo { 2673e9f45a7SArnaldo Carvalho de Melo PyObject *ret; 2683e9f45a7SArnaldo Carvalho de Melo char *s; 2693e9f45a7SArnaldo Carvalho de Melo 2705290ed69SJiri Olsa if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", " 2715290ed69SJiri Olsa "lost: %#" PRI_lx64 " }", 2723e9f45a7SArnaldo Carvalho de Melo pevent->event.lost.id, pevent->event.lost.lost) < 0) { 2733e9f45a7SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 2743e9f45a7SArnaldo Carvalho de Melo } else { 27566dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString(s); 2763e9f45a7SArnaldo Carvalho de Melo free(s); 2773e9f45a7SArnaldo Carvalho de Melo } 2783e9f45a7SArnaldo Carvalho de Melo return ret; 2793e9f45a7SArnaldo Carvalho de Melo } 2803e9f45a7SArnaldo Carvalho de Melo 2813e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_lost_event__type = { 2823e9f45a7SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 2833e9f45a7SArnaldo Carvalho de Melo .tp_name = "perf.lost_event", 2843e9f45a7SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 2853e9f45a7SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 2863e9f45a7SArnaldo Carvalho de Melo .tp_doc = pyrf_lost_event__doc, 2873e9f45a7SArnaldo Carvalho de Melo .tp_members = pyrf_lost_event__members, 2883e9f45a7SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_lost_event__repr, 2893e9f45a7SArnaldo Carvalho de Melo }; 2903e9f45a7SArnaldo Carvalho de Melo 2913e9f45a7SArnaldo Carvalho de Melo static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object."); 2923e9f45a7SArnaldo Carvalho de Melo 2933e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_read_event__members[] = { 2943e9f45a7SArnaldo Carvalho de Melo sample_members 29569d81f09SArnaldo Carvalho de Melo member_def(perf_record_read, pid, T_UINT, "event pid"), 29669d81f09SArnaldo Carvalho de Melo member_def(perf_record_read, tid, T_UINT, "event tid"), 2973e9f45a7SArnaldo Carvalho de Melo { .name = NULL, }, 2983e9f45a7SArnaldo Carvalho de Melo }; 2993e9f45a7SArnaldo Carvalho de Melo 3003e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent) 3013e9f45a7SArnaldo Carvalho de Melo { 30266dfdff0SJaroslav Škarvada return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }", 3033e9f45a7SArnaldo Carvalho de Melo pevent->event.read.pid, 3043e9f45a7SArnaldo Carvalho de Melo pevent->event.read.tid); 3053e9f45a7SArnaldo Carvalho de Melo /* 3063e9f45a7SArnaldo Carvalho de Melo * FIXME: return the array of read values, 3073e9f45a7SArnaldo Carvalho de Melo * making this method useful ;-) 3083e9f45a7SArnaldo Carvalho de Melo */ 3093e9f45a7SArnaldo Carvalho de Melo } 3103e9f45a7SArnaldo Carvalho de Melo 3113e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_read_event__type = { 3123e9f45a7SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 3133e9f45a7SArnaldo Carvalho de Melo .tp_name = "perf.read_event", 3143e9f45a7SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 3153e9f45a7SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 3163e9f45a7SArnaldo Carvalho de Melo .tp_doc = pyrf_read_event__doc, 3173e9f45a7SArnaldo Carvalho de Melo .tp_members = pyrf_read_event__members, 3183e9f45a7SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_read_event__repr, 3193e9f45a7SArnaldo Carvalho de Melo }; 3203e9f45a7SArnaldo Carvalho de Melo 3213e9f45a7SArnaldo Carvalho de Melo static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object."); 3223e9f45a7SArnaldo Carvalho de Melo 3233e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_sample_event__members[] = { 3243e9f45a7SArnaldo Carvalho de Melo sample_members 3253e9f45a7SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 3263e9f45a7SArnaldo Carvalho de Melo { .name = NULL, }, 3273e9f45a7SArnaldo Carvalho de Melo }; 3283e9f45a7SArnaldo Carvalho de Melo 3293e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent) 3303e9f45a7SArnaldo Carvalho de Melo { 3313e9f45a7SArnaldo Carvalho de Melo PyObject *ret; 3323e9f45a7SArnaldo Carvalho de Melo char *s; 3333e9f45a7SArnaldo Carvalho de Melo 3343e9f45a7SArnaldo Carvalho de Melo if (asprintf(&s, "{ type: sample }") < 0) { 3353e9f45a7SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 3363e9f45a7SArnaldo Carvalho de Melo } else { 33766dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString(s); 3383e9f45a7SArnaldo Carvalho de Melo free(s); 3393e9f45a7SArnaldo Carvalho de Melo } 3403e9f45a7SArnaldo Carvalho de Melo return ret; 3413e9f45a7SArnaldo Carvalho de Melo } 3423e9f45a7SArnaldo Carvalho de Melo 343bae57e38SJiri Olsa static bool is_tracepoint(struct pyrf_event *pevent) 344bae57e38SJiri Olsa { 3451fc632ceSJiri Olsa return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT; 346bae57e38SJiri Olsa } 347bae57e38SJiri Olsa 348bae57e38SJiri Olsa static PyObject* 3492c92f982STzvetomir Stoyanov (VMware) tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field) 350bae57e38SJiri Olsa { 35169769ce1STzvetomir Stoyanov struct tep_handle *pevent = field->event->tep; 352bae57e38SJiri Olsa void *data = pe->sample.raw_data; 353bae57e38SJiri Olsa PyObject *ret = NULL; 354bae57e38SJiri Olsa unsigned long long val; 355bae57e38SJiri Olsa unsigned int offset, len; 356bae57e38SJiri Olsa 357bb39ccb2STzvetomir Stoyanov (VMware) if (field->flags & TEP_FIELD_IS_ARRAY) { 358bae57e38SJiri Olsa offset = field->offset; 359bae57e38SJiri Olsa len = field->size; 360bb39ccb2STzvetomir Stoyanov (VMware) if (field->flags & TEP_FIELD_IS_DYNAMIC) { 36159c1baeeSTzvetomir Stoyanov (VMware) val = tep_read_number(pevent, data + offset, len); 362bae57e38SJiri Olsa offset = val; 363bae57e38SJiri Olsa len = offset >> 16; 364bae57e38SJiri Olsa offset &= 0xffff; 365bae57e38SJiri Olsa } 366bb39ccb2STzvetomir Stoyanov (VMware) if (field->flags & TEP_FIELD_IS_STRING && 367bae57e38SJiri Olsa is_printable_array(data + offset, len)) { 36866dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString((char *)data + offset); 369bae57e38SJiri Olsa } else { 370bae57e38SJiri Olsa ret = PyByteArray_FromStringAndSize((const char *) data + offset, len); 371bb39ccb2STzvetomir Stoyanov (VMware) field->flags &= ~TEP_FIELD_IS_STRING; 372bae57e38SJiri Olsa } 373bae57e38SJiri Olsa } else { 37459c1baeeSTzvetomir Stoyanov (VMware) val = tep_read_number(pevent, data + field->offset, 375bae57e38SJiri Olsa field->size); 376bb39ccb2STzvetomir Stoyanov (VMware) if (field->flags & TEP_FIELD_IS_POINTER) 377bae57e38SJiri Olsa ret = PyLong_FromUnsignedLong((unsigned long) val); 378bb39ccb2STzvetomir Stoyanov (VMware) else if (field->flags & TEP_FIELD_IS_SIGNED) 379bae57e38SJiri Olsa ret = PyLong_FromLong((long) val); 380bae57e38SJiri Olsa else 381bae57e38SJiri Olsa ret = PyLong_FromUnsignedLong((unsigned long) val); 382bae57e38SJiri Olsa } 383bae57e38SJiri Olsa 384bae57e38SJiri Olsa return ret; 385bae57e38SJiri Olsa } 386bae57e38SJiri Olsa 387bae57e38SJiri Olsa static PyObject* 388bae57e38SJiri Olsa get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name) 389bae57e38SJiri Olsa { 39066dfdff0SJaroslav Škarvada const char *str = _PyUnicode_AsString(PyObject_Str(attr_name)); 39132dcd021SJiri Olsa struct evsel *evsel = pevent->evsel; 3922c92f982STzvetomir Stoyanov (VMware) struct tep_format_field *field; 393bae57e38SJiri Olsa 394bae57e38SJiri Olsa if (!evsel->tp_format) { 39597fbf3f0STzvetomir Stoyanov struct tep_event *tp_format; 396bae57e38SJiri Olsa 3971fc632ceSJiri Olsa tp_format = trace_event__tp_format_id(evsel->core.attr.config); 398bae57e38SJiri Olsa if (!tp_format) 399bae57e38SJiri Olsa return NULL; 400bae57e38SJiri Olsa 401bae57e38SJiri Olsa evsel->tp_format = tp_format; 402bae57e38SJiri Olsa } 403bae57e38SJiri Olsa 404af85cd19STzvetomir Stoyanov (VMware) field = tep_find_any_field(evsel->tp_format, str); 405bae57e38SJiri Olsa if (!field) 406bae57e38SJiri Olsa return NULL; 407bae57e38SJiri Olsa 408bae57e38SJiri Olsa return tracepoint_field(pevent, field); 409bae57e38SJiri Olsa } 410bae57e38SJiri Olsa 411bae57e38SJiri Olsa static PyObject* 412bae57e38SJiri Olsa pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name) 413bae57e38SJiri Olsa { 414bae57e38SJiri Olsa PyObject *obj = NULL; 415bae57e38SJiri Olsa 416bae57e38SJiri Olsa if (is_tracepoint(pevent)) 417bae57e38SJiri Olsa obj = get_tracepoint_field(pevent, attr_name); 418bae57e38SJiri Olsa 419bae57e38SJiri Olsa return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name); 420bae57e38SJiri Olsa } 421bae57e38SJiri Olsa 4223e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_sample_event__type = { 4233e9f45a7SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 4243e9f45a7SArnaldo Carvalho de Melo .tp_name = "perf.sample_event", 4253e9f45a7SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 4263e9f45a7SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 4273e9f45a7SArnaldo Carvalho de Melo .tp_doc = pyrf_sample_event__doc, 4283e9f45a7SArnaldo Carvalho de Melo .tp_members = pyrf_sample_event__members, 4293e9f45a7SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_sample_event__repr, 430bae57e38SJiri Olsa .tp_getattro = (getattrofunc) pyrf_sample_event__getattro, 4313e9f45a7SArnaldo Carvalho de Melo }; 4323e9f45a7SArnaldo Carvalho de Melo 433ae938802SArnaldo Carvalho de Melo static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object."); 434ae938802SArnaldo Carvalho de Melo 435ae938802SArnaldo Carvalho de Melo static PyMemberDef pyrf_context_switch_event__members[] = { 436ae938802SArnaldo Carvalho de Melo sample_members 437ae938802SArnaldo Carvalho de Melo member_def(perf_event_header, type, T_UINT, "event type"), 43872932371SJiri Olsa member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"), 43972932371SJiri Olsa member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"), 440ae938802SArnaldo Carvalho de Melo { .name = NULL, }, 441ae938802SArnaldo Carvalho de Melo }; 442ae938802SArnaldo Carvalho de Melo 443ae938802SArnaldo Carvalho de Melo static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent) 444ae938802SArnaldo Carvalho de Melo { 445ae938802SArnaldo Carvalho de Melo PyObject *ret; 446ae938802SArnaldo Carvalho de Melo char *s; 447ae938802SArnaldo Carvalho de Melo 448ae938802SArnaldo Carvalho de Melo if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }", 449ae938802SArnaldo Carvalho de Melo pevent->event.context_switch.next_prev_pid, 450ae938802SArnaldo Carvalho de Melo pevent->event.context_switch.next_prev_tid, 451ae938802SArnaldo Carvalho de Melo !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) { 452ae938802SArnaldo Carvalho de Melo ret = PyErr_NoMemory(); 453ae938802SArnaldo Carvalho de Melo } else { 45466dfdff0SJaroslav Škarvada ret = _PyUnicode_FromString(s); 455ae938802SArnaldo Carvalho de Melo free(s); 456ae938802SArnaldo Carvalho de Melo } 457ae938802SArnaldo Carvalho de Melo return ret; 458ae938802SArnaldo Carvalho de Melo } 459ae938802SArnaldo Carvalho de Melo 460ae938802SArnaldo Carvalho de Melo static PyTypeObject pyrf_context_switch_event__type = { 461ae938802SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 462ae938802SArnaldo Carvalho de Melo .tp_name = "perf.context_switch_event", 463ae938802SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_event), 464ae938802SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 465ae938802SArnaldo Carvalho de Melo .tp_doc = pyrf_context_switch_event__doc, 466ae938802SArnaldo Carvalho de Melo .tp_members = pyrf_context_switch_event__members, 467ae938802SArnaldo Carvalho de Melo .tp_repr = (reprfunc)pyrf_context_switch_event__repr, 468ae938802SArnaldo Carvalho de Melo }; 469ae938802SArnaldo Carvalho de Melo 470877108e4SArnaldo Carvalho de Melo static int pyrf_event__setup_types(void) 471877108e4SArnaldo Carvalho de Melo { 472877108e4SArnaldo Carvalho de Melo int err; 473877108e4SArnaldo Carvalho de Melo pyrf_mmap_event__type.tp_new = 474877108e4SArnaldo Carvalho de Melo pyrf_task_event__type.tp_new = 475877108e4SArnaldo Carvalho de Melo pyrf_comm_event__type.tp_new = 4763e9f45a7SArnaldo Carvalho de Melo pyrf_lost_event__type.tp_new = 4773e9f45a7SArnaldo Carvalho de Melo pyrf_read_event__type.tp_new = 4783e9f45a7SArnaldo Carvalho de Melo pyrf_sample_event__type.tp_new = 479ae938802SArnaldo Carvalho de Melo pyrf_context_switch_event__type.tp_new = 480877108e4SArnaldo Carvalho de Melo pyrf_throttle_event__type.tp_new = PyType_GenericNew; 481877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_mmap_event__type); 482877108e4SArnaldo Carvalho de Melo if (err < 0) 483877108e4SArnaldo Carvalho de Melo goto out; 4843e9f45a7SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_lost_event__type); 4853e9f45a7SArnaldo Carvalho de Melo if (err < 0) 4863e9f45a7SArnaldo Carvalho de Melo goto out; 487877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_task_event__type); 488877108e4SArnaldo Carvalho de Melo if (err < 0) 489877108e4SArnaldo Carvalho de Melo goto out; 490877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_comm_event__type); 491877108e4SArnaldo Carvalho de Melo if (err < 0) 492877108e4SArnaldo Carvalho de Melo goto out; 493877108e4SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_throttle_event__type); 494877108e4SArnaldo Carvalho de Melo if (err < 0) 495877108e4SArnaldo Carvalho de Melo goto out; 4963e9f45a7SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_read_event__type); 4973e9f45a7SArnaldo Carvalho de Melo if (err < 0) 4983e9f45a7SArnaldo Carvalho de Melo goto out; 4993e9f45a7SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_sample_event__type); 5003e9f45a7SArnaldo Carvalho de Melo if (err < 0) 5013e9f45a7SArnaldo Carvalho de Melo goto out; 502ae938802SArnaldo Carvalho de Melo err = PyType_Ready(&pyrf_context_switch_event__type); 503ae938802SArnaldo Carvalho de Melo if (err < 0) 504ae938802SArnaldo Carvalho de Melo goto out; 505877108e4SArnaldo Carvalho de Melo out: 506877108e4SArnaldo Carvalho de Melo return err; 507877108e4SArnaldo Carvalho de Melo } 508877108e4SArnaldo Carvalho de Melo 509877108e4SArnaldo Carvalho de Melo static PyTypeObject *pyrf_event__type[] = { 510877108e4SArnaldo Carvalho de Melo [PERF_RECORD_MMAP] = &pyrf_mmap_event__type, 5113e9f45a7SArnaldo Carvalho de Melo [PERF_RECORD_LOST] = &pyrf_lost_event__type, 512877108e4SArnaldo Carvalho de Melo [PERF_RECORD_COMM] = &pyrf_comm_event__type, 513877108e4SArnaldo Carvalho de Melo [PERF_RECORD_EXIT] = &pyrf_task_event__type, 514877108e4SArnaldo Carvalho de Melo [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type, 515877108e4SArnaldo Carvalho de Melo [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type, 516877108e4SArnaldo Carvalho de Melo [PERF_RECORD_FORK] = &pyrf_task_event__type, 5173e9f45a7SArnaldo Carvalho de Melo [PERF_RECORD_READ] = &pyrf_read_event__type, 5183e9f45a7SArnaldo Carvalho de Melo [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type, 519ae938802SArnaldo Carvalho de Melo [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type, 520ae938802SArnaldo Carvalho de Melo [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type, 521877108e4SArnaldo Carvalho de Melo }; 522877108e4SArnaldo Carvalho de Melo 523877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_event__new(union perf_event *event) 524877108e4SArnaldo Carvalho de Melo { 525877108e4SArnaldo Carvalho de Melo struct pyrf_event *pevent; 526877108e4SArnaldo Carvalho de Melo PyTypeObject *ptype; 527877108e4SArnaldo Carvalho de Melo 528ae938802SArnaldo Carvalho de Melo if ((event->header.type < PERF_RECORD_MMAP || 529ae938802SArnaldo Carvalho de Melo event->header.type > PERF_RECORD_SAMPLE) && 530ae938802SArnaldo Carvalho de Melo !(event->header.type == PERF_RECORD_SWITCH || 531ae938802SArnaldo Carvalho de Melo event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)) 532877108e4SArnaldo Carvalho de Melo return NULL; 533877108e4SArnaldo Carvalho de Melo 534877108e4SArnaldo Carvalho de Melo ptype = pyrf_event__type[event->header.type]; 535877108e4SArnaldo Carvalho de Melo pevent = PyObject_New(struct pyrf_event, ptype); 536877108e4SArnaldo Carvalho de Melo if (pevent != NULL) 537877108e4SArnaldo Carvalho de Melo memcpy(&pevent->event, event, event->header.size); 538877108e4SArnaldo Carvalho de Melo return (PyObject *)pevent; 539877108e4SArnaldo Carvalho de Melo } 540877108e4SArnaldo Carvalho de Melo 541877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map { 542877108e4SArnaldo Carvalho de Melo PyObject_HEAD 543877108e4SArnaldo Carvalho de Melo 544f854839bSJiri Olsa struct perf_cpu_map *cpus; 545877108e4SArnaldo Carvalho de Melo }; 546877108e4SArnaldo Carvalho de Melo 547877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus, 548877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 549877108e4SArnaldo Carvalho de Melo { 55064348153SFrederic Weisbecker static char *kwlist[] = { "cpustr", NULL }; 551877108e4SArnaldo Carvalho de Melo char *cpustr = NULL; 552877108e4SArnaldo Carvalho de Melo 553877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", 554877108e4SArnaldo Carvalho de Melo kwlist, &cpustr)) 555877108e4SArnaldo Carvalho de Melo return -1; 556877108e4SArnaldo Carvalho de Melo 5579c3516d1SJiri Olsa pcpus->cpus = perf_cpu_map__new(cpustr); 558877108e4SArnaldo Carvalho de Melo if (pcpus->cpus == NULL) 559877108e4SArnaldo Carvalho de Melo return -1; 560877108e4SArnaldo Carvalho de Melo return 0; 561877108e4SArnaldo Carvalho de Melo } 562877108e4SArnaldo Carvalho de Melo 563877108e4SArnaldo Carvalho de Melo static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus) 564877108e4SArnaldo Carvalho de Melo { 56538f01d8dSJiri Olsa perf_cpu_map__put(pcpus->cpus); 56666dfdff0SJaroslav Škarvada Py_TYPE(pcpus)->tp_free((PyObject*)pcpus); 567877108e4SArnaldo Carvalho de Melo } 568877108e4SArnaldo Carvalho de Melo 569877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_cpu_map__length(PyObject *obj) 570877108e4SArnaldo Carvalho de Melo { 571877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map *pcpus = (void *)obj; 572877108e4SArnaldo Carvalho de Melo 573877108e4SArnaldo Carvalho de Melo return pcpus->cpus->nr; 574877108e4SArnaldo Carvalho de Melo } 575877108e4SArnaldo Carvalho de Melo 576877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i) 577877108e4SArnaldo Carvalho de Melo { 578877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map *pcpus = (void *)obj; 579877108e4SArnaldo Carvalho de Melo 580877108e4SArnaldo Carvalho de Melo if (i >= pcpus->cpus->nr) 581877108e4SArnaldo Carvalho de Melo return NULL; 582877108e4SArnaldo Carvalho de Melo 583877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", pcpus->cpus->map[i]); 584877108e4SArnaldo Carvalho de Melo } 585877108e4SArnaldo Carvalho de Melo 586877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_cpu_map__sequence_methods = { 587877108e4SArnaldo Carvalho de Melo .sq_length = pyrf_cpu_map__length, 588877108e4SArnaldo Carvalho de Melo .sq_item = pyrf_cpu_map__item, 589877108e4SArnaldo Carvalho de Melo }; 590877108e4SArnaldo Carvalho de Melo 591877108e4SArnaldo Carvalho de Melo static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object."); 592877108e4SArnaldo Carvalho de Melo 593877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_cpu_map__type = { 594877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 595877108e4SArnaldo Carvalho de Melo .tp_name = "perf.cpu_map", 596877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_cpu_map), 597877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_cpu_map__delete, 598877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 599877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_cpu_map__doc, 600877108e4SArnaldo Carvalho de Melo .tp_as_sequence = &pyrf_cpu_map__sequence_methods, 601877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_cpu_map__init, 602877108e4SArnaldo Carvalho de Melo }; 603877108e4SArnaldo Carvalho de Melo 604877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__setup_types(void) 605877108e4SArnaldo Carvalho de Melo { 606877108e4SArnaldo Carvalho de Melo pyrf_cpu_map__type.tp_new = PyType_GenericNew; 607877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_cpu_map__type); 608877108e4SArnaldo Carvalho de Melo } 609877108e4SArnaldo Carvalho de Melo 610877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map { 611877108e4SArnaldo Carvalho de Melo PyObject_HEAD 612877108e4SArnaldo Carvalho de Melo 6139749b90eSJiri Olsa struct perf_thread_map *threads; 614877108e4SArnaldo Carvalho de Melo }; 615877108e4SArnaldo Carvalho de Melo 616877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads, 617877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 618877108e4SArnaldo Carvalho de Melo { 6190d37aa34SArnaldo Carvalho de Melo static char *kwlist[] = { "pid", "tid", "uid", NULL }; 6200d37aa34SArnaldo Carvalho de Melo int pid = -1, tid = -1, uid = UINT_MAX; 621877108e4SArnaldo Carvalho de Melo 6220d37aa34SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii", 6230d37aa34SArnaldo Carvalho de Melo kwlist, &pid, &tid, &uid)) 624877108e4SArnaldo Carvalho de Melo return -1; 625877108e4SArnaldo Carvalho de Melo 6260d37aa34SArnaldo Carvalho de Melo pthreads->threads = thread_map__new(pid, tid, uid); 627877108e4SArnaldo Carvalho de Melo if (pthreads->threads == NULL) 628877108e4SArnaldo Carvalho de Melo return -1; 629877108e4SArnaldo Carvalho de Melo return 0; 630877108e4SArnaldo Carvalho de Melo } 631877108e4SArnaldo Carvalho de Melo 632877108e4SArnaldo Carvalho de Melo static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads) 633877108e4SArnaldo Carvalho de Melo { 6347836e52eSJiri Olsa perf_thread_map__put(pthreads->threads); 63566dfdff0SJaroslav Škarvada Py_TYPE(pthreads)->tp_free((PyObject*)pthreads); 636877108e4SArnaldo Carvalho de Melo } 637877108e4SArnaldo Carvalho de Melo 638877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_thread_map__length(PyObject *obj) 639877108e4SArnaldo Carvalho de Melo { 640877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map *pthreads = (void *)obj; 641877108e4SArnaldo Carvalho de Melo 642877108e4SArnaldo Carvalho de Melo return pthreads->threads->nr; 643877108e4SArnaldo Carvalho de Melo } 644877108e4SArnaldo Carvalho de Melo 645877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i) 646877108e4SArnaldo Carvalho de Melo { 647877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map *pthreads = (void *)obj; 648877108e4SArnaldo Carvalho de Melo 649877108e4SArnaldo Carvalho de Melo if (i >= pthreads->threads->nr) 650877108e4SArnaldo Carvalho de Melo return NULL; 651877108e4SArnaldo Carvalho de Melo 652877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", pthreads->threads->map[i]); 653877108e4SArnaldo Carvalho de Melo } 654877108e4SArnaldo Carvalho de Melo 655877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_thread_map__sequence_methods = { 656877108e4SArnaldo Carvalho de Melo .sq_length = pyrf_thread_map__length, 657877108e4SArnaldo Carvalho de Melo .sq_item = pyrf_thread_map__item, 658877108e4SArnaldo Carvalho de Melo }; 659877108e4SArnaldo Carvalho de Melo 660877108e4SArnaldo Carvalho de Melo static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object."); 661877108e4SArnaldo Carvalho de Melo 662877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_thread_map__type = { 663877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 664877108e4SArnaldo Carvalho de Melo .tp_name = "perf.thread_map", 665877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_thread_map), 666877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_thread_map__delete, 667877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 668877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_thread_map__doc, 669877108e4SArnaldo Carvalho de Melo .tp_as_sequence = &pyrf_thread_map__sequence_methods, 670877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_thread_map__init, 671877108e4SArnaldo Carvalho de Melo }; 672877108e4SArnaldo Carvalho de Melo 673877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__setup_types(void) 674877108e4SArnaldo Carvalho de Melo { 675877108e4SArnaldo Carvalho de Melo pyrf_thread_map__type.tp_new = PyType_GenericNew; 676877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_thread_map__type); 677877108e4SArnaldo Carvalho de Melo } 678877108e4SArnaldo Carvalho de Melo 679877108e4SArnaldo Carvalho de Melo struct pyrf_evsel { 680877108e4SArnaldo Carvalho de Melo PyObject_HEAD 681877108e4SArnaldo Carvalho de Melo 68232dcd021SJiri Olsa struct evsel evsel; 683877108e4SArnaldo Carvalho de Melo }; 684877108e4SArnaldo Carvalho de Melo 685877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__init(struct pyrf_evsel *pevsel, 686877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 687877108e4SArnaldo Carvalho de Melo { 688877108e4SArnaldo Carvalho de Melo struct perf_event_attr attr = { 689877108e4SArnaldo Carvalho de Melo .type = PERF_TYPE_HARDWARE, 690877108e4SArnaldo Carvalho de Melo .config = PERF_COUNT_HW_CPU_CYCLES, 691877108e4SArnaldo Carvalho de Melo .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID, 692877108e4SArnaldo Carvalho de Melo }; 693877108e4SArnaldo Carvalho de Melo static char *kwlist[] = { 694877108e4SArnaldo Carvalho de Melo "type", 695877108e4SArnaldo Carvalho de Melo "config", 696877108e4SArnaldo Carvalho de Melo "sample_freq", 697877108e4SArnaldo Carvalho de Melo "sample_period", 698877108e4SArnaldo Carvalho de Melo "sample_type", 699877108e4SArnaldo Carvalho de Melo "read_format", 700877108e4SArnaldo Carvalho de Melo "disabled", 701877108e4SArnaldo Carvalho de Melo "inherit", 702877108e4SArnaldo Carvalho de Melo "pinned", 703877108e4SArnaldo Carvalho de Melo "exclusive", 704877108e4SArnaldo Carvalho de Melo "exclude_user", 705877108e4SArnaldo Carvalho de Melo "exclude_kernel", 706877108e4SArnaldo Carvalho de Melo "exclude_hv", 707877108e4SArnaldo Carvalho de Melo "exclude_idle", 708877108e4SArnaldo Carvalho de Melo "mmap", 709ae938802SArnaldo Carvalho de Melo "context_switch", 710877108e4SArnaldo Carvalho de Melo "comm", 711877108e4SArnaldo Carvalho de Melo "freq", 712877108e4SArnaldo Carvalho de Melo "inherit_stat", 713877108e4SArnaldo Carvalho de Melo "enable_on_exec", 714877108e4SArnaldo Carvalho de Melo "task", 715877108e4SArnaldo Carvalho de Melo "watermark", 716877108e4SArnaldo Carvalho de Melo "precise_ip", 717877108e4SArnaldo Carvalho de Melo "mmap_data", 718877108e4SArnaldo Carvalho de Melo "sample_id_all", 719877108e4SArnaldo Carvalho de Melo "wakeup_events", 720877108e4SArnaldo Carvalho de Melo "bp_type", 721877108e4SArnaldo Carvalho de Melo "bp_addr", 72264348153SFrederic Weisbecker "bp_len", 72364348153SFrederic Weisbecker NULL 72464348153SFrederic Weisbecker }; 725877108e4SArnaldo Carvalho de Melo u64 sample_period = 0; 726877108e4SArnaldo Carvalho de Melo u32 disabled = 0, 727877108e4SArnaldo Carvalho de Melo inherit = 0, 728877108e4SArnaldo Carvalho de Melo pinned = 0, 729877108e4SArnaldo Carvalho de Melo exclusive = 0, 730877108e4SArnaldo Carvalho de Melo exclude_user = 0, 731877108e4SArnaldo Carvalho de Melo exclude_kernel = 0, 732877108e4SArnaldo Carvalho de Melo exclude_hv = 0, 733877108e4SArnaldo Carvalho de Melo exclude_idle = 0, 734877108e4SArnaldo Carvalho de Melo mmap = 0, 735ae938802SArnaldo Carvalho de Melo context_switch = 0, 736877108e4SArnaldo Carvalho de Melo comm = 0, 737877108e4SArnaldo Carvalho de Melo freq = 1, 738877108e4SArnaldo Carvalho de Melo inherit_stat = 0, 739877108e4SArnaldo Carvalho de Melo enable_on_exec = 0, 740877108e4SArnaldo Carvalho de Melo task = 0, 741877108e4SArnaldo Carvalho de Melo watermark = 0, 742877108e4SArnaldo Carvalho de Melo precise_ip = 0, 743877108e4SArnaldo Carvalho de Melo mmap_data = 0, 744877108e4SArnaldo Carvalho de Melo sample_id_all = 1; 745877108e4SArnaldo Carvalho de Melo int idx = 0; 746877108e4SArnaldo Carvalho de Melo 747877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, 748ae938802SArnaldo Carvalho de Melo "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist, 749877108e4SArnaldo Carvalho de Melo &attr.type, &attr.config, &attr.sample_freq, 750877108e4SArnaldo Carvalho de Melo &sample_period, &attr.sample_type, 751877108e4SArnaldo Carvalho de Melo &attr.read_format, &disabled, &inherit, 752877108e4SArnaldo Carvalho de Melo &pinned, &exclusive, &exclude_user, 753877108e4SArnaldo Carvalho de Melo &exclude_kernel, &exclude_hv, &exclude_idle, 754ae938802SArnaldo Carvalho de Melo &mmap, &context_switch, &comm, &freq, &inherit_stat, 755877108e4SArnaldo Carvalho de Melo &enable_on_exec, &task, &watermark, 756877108e4SArnaldo Carvalho de Melo &precise_ip, &mmap_data, &sample_id_all, 757877108e4SArnaldo Carvalho de Melo &attr.wakeup_events, &attr.bp_type, 758877108e4SArnaldo Carvalho de Melo &attr.bp_addr, &attr.bp_len, &idx)) 759877108e4SArnaldo Carvalho de Melo return -1; 760877108e4SArnaldo Carvalho de Melo 761877108e4SArnaldo Carvalho de Melo /* union... */ 762877108e4SArnaldo Carvalho de Melo if (sample_period != 0) { 763877108e4SArnaldo Carvalho de Melo if (attr.sample_freq != 0) 764877108e4SArnaldo Carvalho de Melo return -1; /* FIXME: throw right exception */ 765877108e4SArnaldo Carvalho de Melo attr.sample_period = sample_period; 766877108e4SArnaldo Carvalho de Melo } 767877108e4SArnaldo Carvalho de Melo 768877108e4SArnaldo Carvalho de Melo /* Bitfields */ 769877108e4SArnaldo Carvalho de Melo attr.disabled = disabled; 770877108e4SArnaldo Carvalho de Melo attr.inherit = inherit; 771877108e4SArnaldo Carvalho de Melo attr.pinned = pinned; 772877108e4SArnaldo Carvalho de Melo attr.exclusive = exclusive; 773877108e4SArnaldo Carvalho de Melo attr.exclude_user = exclude_user; 774877108e4SArnaldo Carvalho de Melo attr.exclude_kernel = exclude_kernel; 775877108e4SArnaldo Carvalho de Melo attr.exclude_hv = exclude_hv; 776877108e4SArnaldo Carvalho de Melo attr.exclude_idle = exclude_idle; 777877108e4SArnaldo Carvalho de Melo attr.mmap = mmap; 778ae938802SArnaldo Carvalho de Melo attr.context_switch = context_switch; 779877108e4SArnaldo Carvalho de Melo attr.comm = comm; 780877108e4SArnaldo Carvalho de Melo attr.freq = freq; 781877108e4SArnaldo Carvalho de Melo attr.inherit_stat = inherit_stat; 782877108e4SArnaldo Carvalho de Melo attr.enable_on_exec = enable_on_exec; 783877108e4SArnaldo Carvalho de Melo attr.task = task; 784877108e4SArnaldo Carvalho de Melo attr.watermark = watermark; 785877108e4SArnaldo Carvalho de Melo attr.precise_ip = precise_ip; 786877108e4SArnaldo Carvalho de Melo attr.mmap_data = mmap_data; 787877108e4SArnaldo Carvalho de Melo attr.sample_id_all = sample_id_all; 788ad4e3c04SJiri Olsa attr.size = sizeof(attr); 789877108e4SArnaldo Carvalho de Melo 790b4b62ee6SJiri Olsa evsel__init(&pevsel->evsel, &attr, idx); 791877108e4SArnaldo Carvalho de Melo return 0; 792877108e4SArnaldo Carvalho de Melo } 793877108e4SArnaldo Carvalho de Melo 794877108e4SArnaldo Carvalho de Melo static void pyrf_evsel__delete(struct pyrf_evsel *pevsel) 795877108e4SArnaldo Carvalho de Melo { 796877108e4SArnaldo Carvalho de Melo perf_evsel__exit(&pevsel->evsel); 79766dfdff0SJaroslav Škarvada Py_TYPE(pevsel)->tp_free((PyObject*)pevsel); 798877108e4SArnaldo Carvalho de Melo } 799877108e4SArnaldo Carvalho de Melo 800877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, 801877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 802877108e4SArnaldo Carvalho de Melo { 80332dcd021SJiri Olsa struct evsel *evsel = &pevsel->evsel; 804f854839bSJiri Olsa struct perf_cpu_map *cpus = NULL; 8059749b90eSJiri Olsa struct perf_thread_map *threads = NULL; 806877108e4SArnaldo Carvalho de Melo PyObject *pcpus = NULL, *pthreads = NULL; 8075d2cd909SArnaldo Carvalho de Melo int group = 0, inherit = 0; 80864348153SFrederic Weisbecker static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL }; 809877108e4SArnaldo Carvalho de Melo 810877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, 8115d2cd909SArnaldo Carvalho de Melo &pcpus, &pthreads, &group, &inherit)) 812877108e4SArnaldo Carvalho de Melo return NULL; 813877108e4SArnaldo Carvalho de Melo 814877108e4SArnaldo Carvalho de Melo if (pthreads != NULL) 815877108e4SArnaldo Carvalho de Melo threads = ((struct pyrf_thread_map *)pthreads)->threads; 816877108e4SArnaldo Carvalho de Melo 817877108e4SArnaldo Carvalho de Melo if (pcpus != NULL) 818877108e4SArnaldo Carvalho de Melo cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; 819877108e4SArnaldo Carvalho de Melo 8201fc632ceSJiri Olsa evsel->core.attr.inherit = inherit; 821727ab04eSArnaldo Carvalho de Melo /* 822727ab04eSArnaldo Carvalho de Melo * This will group just the fds for this single evsel, to group 823727ab04eSArnaldo Carvalho de Melo * multiple events, use evlist.open(). 824727ab04eSArnaldo Carvalho de Melo */ 8255972d1e0SJiri Olsa if (evsel__open(evsel, cpus, threads) < 0) { 826877108e4SArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 827877108e4SArnaldo Carvalho de Melo return NULL; 828877108e4SArnaldo Carvalho de Melo } 829877108e4SArnaldo 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 834877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evsel__methods[] = { 835877108e4SArnaldo Carvalho de Melo { 836877108e4SArnaldo Carvalho de Melo .ml_name = "open", 837877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evsel__open, 838877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 839877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("open the event selector file descriptor table.") 840877108e4SArnaldo Carvalho de Melo }, 841f6bbc1daSArnaldo Carvalho de Melo { .ml_name = NULL, } 842877108e4SArnaldo Carvalho de Melo }; 843877108e4SArnaldo Carvalho de Melo 844877108e4SArnaldo Carvalho de Melo static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object."); 845877108e4SArnaldo Carvalho de Melo 846877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evsel__type = { 847877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 848877108e4SArnaldo Carvalho de Melo .tp_name = "perf.evsel", 849877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_evsel), 850877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_evsel__delete, 851877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 852877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_evsel__doc, 853877108e4SArnaldo Carvalho de Melo .tp_methods = pyrf_evsel__methods, 854877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_evsel__init, 855877108e4SArnaldo Carvalho de Melo }; 856877108e4SArnaldo Carvalho de Melo 857877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__setup_types(void) 858877108e4SArnaldo Carvalho de Melo { 859877108e4SArnaldo Carvalho de Melo pyrf_evsel__type.tp_new = PyType_GenericNew; 860877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_evsel__type); 861877108e4SArnaldo Carvalho de Melo } 862877108e4SArnaldo Carvalho de Melo 863877108e4SArnaldo Carvalho de Melo struct pyrf_evlist { 864877108e4SArnaldo Carvalho de Melo PyObject_HEAD 865877108e4SArnaldo Carvalho de Melo 86663503dbaSJiri Olsa struct evlist evlist; 867877108e4SArnaldo Carvalho de Melo }; 868877108e4SArnaldo Carvalho de Melo 869877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__init(struct pyrf_evlist *pevlist, 8701d037ca1SIrina Tirdea PyObject *args, PyObject *kwargs __maybe_unused) 871877108e4SArnaldo Carvalho de Melo { 8727e2ed097SArnaldo Carvalho de Melo PyObject *pcpus = NULL, *pthreads = NULL; 873f854839bSJiri Olsa struct perf_cpu_map *cpus; 8749749b90eSJiri Olsa struct perf_thread_map *threads; 8757e2ed097SArnaldo Carvalho de Melo 8767e2ed097SArnaldo Carvalho de Melo if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads)) 8777e2ed097SArnaldo Carvalho de Melo return -1; 8787e2ed097SArnaldo Carvalho de Melo 8797e2ed097SArnaldo Carvalho de Melo threads = ((struct pyrf_thread_map *)pthreads)->threads; 8807e2ed097SArnaldo Carvalho de Melo cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; 88152c86bcaSJiri Olsa evlist__init(&pevlist->evlist, cpus, threads); 882877108e4SArnaldo Carvalho de Melo return 0; 883877108e4SArnaldo Carvalho de Melo } 884877108e4SArnaldo Carvalho de Melo 885877108e4SArnaldo Carvalho de Melo static void pyrf_evlist__delete(struct pyrf_evlist *pevlist) 886877108e4SArnaldo Carvalho de Melo { 887877108e4SArnaldo Carvalho de Melo perf_evlist__exit(&pevlist->evlist); 88866dfdff0SJaroslav Škarvada Py_TYPE(pevlist)->tp_free((PyObject*)pevlist); 889877108e4SArnaldo Carvalho de Melo } 890877108e4SArnaldo Carvalho de Melo 891877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist, 892877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 893877108e4SArnaldo Carvalho de Melo { 89463503dbaSJiri Olsa struct evlist *evlist = &pevlist->evlist; 89564348153SFrederic Weisbecker static char *kwlist[] = { "pages", "overwrite", NULL }; 896877108e4SArnaldo Carvalho de Melo int pages = 128, overwrite = false; 897877108e4SArnaldo Carvalho de Melo 8987e2ed097SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist, 8997e2ed097SArnaldo Carvalho de Melo &pages, &overwrite)) 900877108e4SArnaldo Carvalho de Melo return NULL; 901877108e4SArnaldo Carvalho de Melo 902f74b9d3aSWang Nan if (perf_evlist__mmap(evlist, pages) < 0) { 903877108e4SArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 904877108e4SArnaldo Carvalho de Melo return NULL; 905877108e4SArnaldo Carvalho de Melo } 906877108e4SArnaldo Carvalho de Melo 907877108e4SArnaldo Carvalho de Melo Py_INCREF(Py_None); 908877108e4SArnaldo Carvalho de Melo return Py_None; 909877108e4SArnaldo Carvalho de Melo } 910877108e4SArnaldo Carvalho de Melo 911877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist, 912877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 913877108e4SArnaldo Carvalho de Melo { 91463503dbaSJiri Olsa struct evlist *evlist = &pevlist->evlist; 91564348153SFrederic Weisbecker static char *kwlist[] = { "timeout", NULL }; 916877108e4SArnaldo Carvalho de Melo int timeout = -1, n; 917877108e4SArnaldo Carvalho de Melo 918877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout)) 919877108e4SArnaldo Carvalho de Melo return NULL; 920877108e4SArnaldo Carvalho de Melo 921f66a889dSArnaldo Carvalho de Melo n = perf_evlist__poll(evlist, timeout); 922877108e4SArnaldo Carvalho de Melo if (n < 0) { 923877108e4SArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 924877108e4SArnaldo Carvalho de Melo return NULL; 925877108e4SArnaldo Carvalho de Melo } 926877108e4SArnaldo Carvalho de Melo 927877108e4SArnaldo Carvalho de Melo return Py_BuildValue("i", n); 928877108e4SArnaldo Carvalho de Melo } 929877108e4SArnaldo Carvalho de Melo 930877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist, 9311d037ca1SIrina Tirdea PyObject *args __maybe_unused, 9321d037ca1SIrina Tirdea PyObject *kwargs __maybe_unused) 933877108e4SArnaldo Carvalho de Melo { 93463503dbaSJiri Olsa struct evlist *evlist = &pevlist->evlist; 935877108e4SArnaldo Carvalho de Melo PyObject *list = PyList_New(0); 936877108e4SArnaldo Carvalho de Melo int i; 937877108e4SArnaldo Carvalho de Melo 9381b85337dSArnaldo Carvalho de Melo for (i = 0; i < evlist->pollfd.nr; ++i) { 939877108e4SArnaldo Carvalho de Melo PyObject *file; 94066dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 9411b85337dSArnaldo Carvalho de Melo FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r"); 942877108e4SArnaldo Carvalho de Melo 943877108e4SArnaldo Carvalho de Melo if (fp == NULL) 944877108e4SArnaldo Carvalho de Melo goto free_list; 945877108e4SArnaldo Carvalho de Melo 946877108e4SArnaldo Carvalho de Melo file = PyFile_FromFile(fp, "perf", "r", NULL); 94766dfdff0SJaroslav Škarvada #else 948a389aeceSJiri Olsa file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1, 949a389aeceSJiri Olsa NULL, NULL, NULL, 0); 95066dfdff0SJaroslav Škarvada #endif 951877108e4SArnaldo Carvalho de Melo if (file == NULL) 952877108e4SArnaldo Carvalho de Melo goto free_list; 953877108e4SArnaldo Carvalho de Melo 954877108e4SArnaldo Carvalho de Melo if (PyList_Append(list, file) != 0) { 955877108e4SArnaldo Carvalho de Melo Py_DECREF(file); 956877108e4SArnaldo Carvalho de Melo goto free_list; 957877108e4SArnaldo Carvalho de Melo } 958877108e4SArnaldo Carvalho de Melo 959877108e4SArnaldo Carvalho de Melo Py_DECREF(file); 960877108e4SArnaldo Carvalho de Melo } 961877108e4SArnaldo Carvalho de Melo 962877108e4SArnaldo Carvalho de Melo return list; 963877108e4SArnaldo Carvalho de Melo free_list: 964877108e4SArnaldo Carvalho de Melo return PyErr_NoMemory(); 965877108e4SArnaldo Carvalho de Melo } 966877108e4SArnaldo Carvalho de Melo 967877108e4SArnaldo Carvalho de Melo 968877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist, 9691d037ca1SIrina Tirdea PyObject *args, 9701d037ca1SIrina Tirdea PyObject *kwargs __maybe_unused) 971877108e4SArnaldo Carvalho de Melo { 97263503dbaSJiri Olsa struct evlist *evlist = &pevlist->evlist; 973877108e4SArnaldo Carvalho de Melo PyObject *pevsel; 97432dcd021SJiri Olsa struct evsel *evsel; 975877108e4SArnaldo Carvalho de Melo 976877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTuple(args, "O", &pevsel)) 977877108e4SArnaldo Carvalho de Melo return NULL; 978877108e4SArnaldo Carvalho de Melo 979877108e4SArnaldo Carvalho de Melo Py_INCREF(pevsel); 980877108e4SArnaldo Carvalho de Melo evsel = &((struct pyrf_evsel *)pevsel)->evsel; 9816484d2f9SJiri Olsa evsel->idx = evlist->core.nr_entries; 982a1cf3a75SJiri Olsa evlist__add(evlist, evsel); 983877108e4SArnaldo Carvalho de Melo 9846484d2f9SJiri Olsa return Py_BuildValue("i", evlist->core.nr_entries); 985877108e4SArnaldo Carvalho de Melo } 986877108e4SArnaldo Carvalho de Melo 98763503dbaSJiri Olsa static struct perf_mmap *get_md(struct evlist *evlist, int cpu) 988721f0dfcSJiri Olsa { 989721f0dfcSJiri Olsa int i; 990721f0dfcSJiri Olsa 991721f0dfcSJiri Olsa for (i = 0; i < evlist->nr_mmaps; i++) { 992721f0dfcSJiri Olsa struct perf_mmap *md = &evlist->mmap[i]; 993721f0dfcSJiri Olsa 994721f0dfcSJiri Olsa if (md->cpu == cpu) 995721f0dfcSJiri Olsa return md; 996721f0dfcSJiri Olsa } 997721f0dfcSJiri Olsa 998721f0dfcSJiri Olsa return NULL; 999721f0dfcSJiri Olsa } 1000721f0dfcSJiri Olsa 1001877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist, 1002877108e4SArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 1003877108e4SArnaldo Carvalho de Melo { 100463503dbaSJiri Olsa struct evlist *evlist = &pevlist->evlist; 1005877108e4SArnaldo Carvalho de Melo union perf_event *event; 1006877108e4SArnaldo Carvalho de Melo int sample_id_all = 1, cpu; 100764348153SFrederic Weisbecker static char *kwlist[] = { "cpu", "sample_id_all", NULL }; 100835b7cdc6SKan Liang struct perf_mmap *md; 10095538becaSFrederic Weisbecker int err; 1010877108e4SArnaldo Carvalho de Melo 1011877108e4SArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, 1012877108e4SArnaldo Carvalho de Melo &cpu, &sample_id_all)) 1013877108e4SArnaldo Carvalho de Melo return NULL; 1014877108e4SArnaldo Carvalho de Melo 1015721f0dfcSJiri Olsa md = get_md(evlist, cpu); 1016721f0dfcSJiri Olsa if (!md) 1017721f0dfcSJiri Olsa return NULL; 1018721f0dfcSJiri Olsa 1019b9bae2c8SKan Liang if (perf_mmap__read_init(md) < 0) 102035b7cdc6SKan Liang goto end; 102135b7cdc6SKan Liang 10220019dc87SKan Liang event = perf_mmap__read_event(md); 1023877108e4SArnaldo Carvalho de Melo if (event != NULL) { 1024877108e4SArnaldo Carvalho de Melo PyObject *pyevent = pyrf_event__new(event); 1025877108e4SArnaldo Carvalho de Melo struct pyrf_event *pevent = (struct pyrf_event *)pyevent; 102632dcd021SJiri Olsa struct evsel *evsel; 1027877108e4SArnaldo Carvalho de Melo 1028877108e4SArnaldo Carvalho de Melo if (pyevent == NULL) 1029877108e4SArnaldo Carvalho de Melo return PyErr_NoMemory(); 1030877108e4SArnaldo Carvalho de Melo 1031377f698dSJiri Olsa evsel = perf_evlist__event2evsel(evlist, event); 103283428f2fSPetr Machata if (!evsel) { 103383428f2fSPetr Machata Py_INCREF(Py_None); 1034377f698dSJiri Olsa return Py_None; 103583428f2fSPetr Machata } 1036377f698dSJiri Olsa 1037377f698dSJiri Olsa pevent->evsel = evsel; 1038377f698dSJiri Olsa 1039377f698dSJiri Olsa err = perf_evsel__parse_sample(evsel, event, &pevent->sample); 1040e8968e65SJiri Olsa 1041e8968e65SJiri Olsa /* Consume the even only after we parsed it out. */ 1042d6ace3dfSKan Liang perf_mmap__consume(md); 1043e8968e65SJiri Olsa 10445c6970afSArnaldo Carvalho de Melo if (err) 10455c6970afSArnaldo Carvalho de Melo return PyErr_Format(PyExc_OSError, 10465c6970afSArnaldo Carvalho de Melo "perf: can't parse sample, err=%d", err); 10475538becaSFrederic Weisbecker return pyevent; 10485538becaSFrederic Weisbecker } 104935b7cdc6SKan Liang end: 1050877108e4SArnaldo Carvalho de Melo Py_INCREF(Py_None); 1051877108e4SArnaldo Carvalho de Melo return Py_None; 1052877108e4SArnaldo Carvalho de Melo } 1053877108e4SArnaldo Carvalho de Melo 1054727ab04eSArnaldo Carvalho de Melo static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist, 1055727ab04eSArnaldo Carvalho de Melo PyObject *args, PyObject *kwargs) 1056727ab04eSArnaldo Carvalho de Melo { 105763503dbaSJiri Olsa struct evlist *evlist = &pevlist->evlist; 1058727ab04eSArnaldo Carvalho de Melo int group = 0; 1059727ab04eSArnaldo Carvalho de Melo static char *kwlist[] = { "group", NULL }; 1060727ab04eSArnaldo Carvalho de Melo 1061727ab04eSArnaldo Carvalho de Melo if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group)) 1062727ab04eSArnaldo Carvalho de Melo return NULL; 1063727ab04eSArnaldo Carvalho de Melo 10646a4bb04cSJiri Olsa if (group) 106563dab225SArnaldo Carvalho de Melo perf_evlist__set_leader(evlist); 10666a4bb04cSJiri Olsa 1067474ddc4cSJiri Olsa if (evlist__open(evlist) < 0) { 1068727ab04eSArnaldo Carvalho de Melo PyErr_SetFromErrno(PyExc_OSError); 1069727ab04eSArnaldo Carvalho de Melo return NULL; 1070727ab04eSArnaldo Carvalho de Melo } 1071727ab04eSArnaldo Carvalho de Melo 1072727ab04eSArnaldo Carvalho de Melo Py_INCREF(Py_None); 1073727ab04eSArnaldo Carvalho de Melo return Py_None; 1074727ab04eSArnaldo Carvalho de Melo } 1075727ab04eSArnaldo Carvalho de Melo 1076877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evlist__methods[] = { 1077877108e4SArnaldo Carvalho de Melo { 1078877108e4SArnaldo Carvalho de Melo .ml_name = "mmap", 1079877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__mmap, 1080877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1081877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("mmap the file descriptor table.") 1082877108e4SArnaldo Carvalho de Melo }, 1083877108e4SArnaldo Carvalho de Melo { 1084727ab04eSArnaldo Carvalho de Melo .ml_name = "open", 1085727ab04eSArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__open, 1086727ab04eSArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1087727ab04eSArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("open the file descriptors.") 1088727ab04eSArnaldo Carvalho de Melo }, 1089727ab04eSArnaldo Carvalho de Melo { 1090877108e4SArnaldo Carvalho de Melo .ml_name = "poll", 1091877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__poll, 1092877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1093877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("poll the file descriptor table.") 1094877108e4SArnaldo Carvalho de Melo }, 1095877108e4SArnaldo Carvalho de Melo { 1096877108e4SArnaldo Carvalho de Melo .ml_name = "get_pollfd", 1097877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd, 1098877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1099877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("get the poll file descriptor table.") 1100877108e4SArnaldo Carvalho de Melo }, 1101877108e4SArnaldo Carvalho de Melo { 1102877108e4SArnaldo Carvalho de Melo .ml_name = "add", 1103877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__add, 1104877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1105877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("adds an event selector to the list.") 1106877108e4SArnaldo Carvalho de Melo }, 1107877108e4SArnaldo Carvalho de Melo { 1108877108e4SArnaldo Carvalho de Melo .ml_name = "read_on_cpu", 1109877108e4SArnaldo Carvalho de Melo .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu, 1110877108e4SArnaldo Carvalho de Melo .ml_flags = METH_VARARGS | METH_KEYWORDS, 1111877108e4SArnaldo Carvalho de Melo .ml_doc = PyDoc_STR("reads an event.") 1112877108e4SArnaldo Carvalho de Melo }, 1113f6bbc1daSArnaldo Carvalho de Melo { .ml_name = NULL, } 1114877108e4SArnaldo Carvalho de Melo }; 1115877108e4SArnaldo Carvalho de Melo 1116877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_evlist__length(PyObject *obj) 1117877108e4SArnaldo Carvalho de Melo { 1118877108e4SArnaldo Carvalho de Melo struct pyrf_evlist *pevlist = (void *)obj; 1119877108e4SArnaldo Carvalho de Melo 11206484d2f9SJiri Olsa return pevlist->evlist.core.nr_entries; 1121877108e4SArnaldo Carvalho de Melo } 1122877108e4SArnaldo Carvalho de Melo 1123877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i) 1124877108e4SArnaldo Carvalho de Melo { 1125877108e4SArnaldo Carvalho de Melo struct pyrf_evlist *pevlist = (void *)obj; 112632dcd021SJiri Olsa struct evsel *pos; 1127877108e4SArnaldo Carvalho de Melo 11286484d2f9SJiri Olsa if (i >= pevlist->evlist.core.nr_entries) 1129877108e4SArnaldo Carvalho de Melo return NULL; 1130877108e4SArnaldo Carvalho de Melo 1131e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(&pevlist->evlist, pos) { 1132877108e4SArnaldo Carvalho de Melo if (i-- == 0) 1133877108e4SArnaldo Carvalho de Melo break; 11340050f7aaSArnaldo Carvalho de Melo } 1135877108e4SArnaldo Carvalho de Melo 1136877108e4SArnaldo Carvalho de Melo return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel)); 1137877108e4SArnaldo Carvalho de Melo } 1138877108e4SArnaldo Carvalho de Melo 1139877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_evlist__sequence_methods = { 1140877108e4SArnaldo Carvalho de Melo .sq_length = pyrf_evlist__length, 1141877108e4SArnaldo Carvalho de Melo .sq_item = pyrf_evlist__item, 1142877108e4SArnaldo Carvalho de Melo }; 1143877108e4SArnaldo Carvalho de Melo 1144877108e4SArnaldo Carvalho de Melo static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object."); 1145877108e4SArnaldo Carvalho de Melo 1146877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evlist__type = { 1147877108e4SArnaldo Carvalho de Melo PyVarObject_HEAD_INIT(NULL, 0) 1148877108e4SArnaldo Carvalho de Melo .tp_name = "perf.evlist", 1149877108e4SArnaldo Carvalho de Melo .tp_basicsize = sizeof(struct pyrf_evlist), 1150877108e4SArnaldo Carvalho de Melo .tp_dealloc = (destructor)pyrf_evlist__delete, 1151877108e4SArnaldo Carvalho de Melo .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 1152877108e4SArnaldo Carvalho de Melo .tp_as_sequence = &pyrf_evlist__sequence_methods, 1153877108e4SArnaldo Carvalho de Melo .tp_doc = pyrf_evlist__doc, 1154877108e4SArnaldo Carvalho de Melo .tp_methods = pyrf_evlist__methods, 1155877108e4SArnaldo Carvalho de Melo .tp_init = (initproc)pyrf_evlist__init, 1156877108e4SArnaldo Carvalho de Melo }; 1157877108e4SArnaldo Carvalho de Melo 1158877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__setup_types(void) 1159877108e4SArnaldo Carvalho de Melo { 1160877108e4SArnaldo Carvalho de Melo pyrf_evlist__type.tp_new = PyType_GenericNew; 1161877108e4SArnaldo Carvalho de Melo return PyType_Ready(&pyrf_evlist__type); 1162877108e4SArnaldo Carvalho de Melo } 1163877108e4SArnaldo Carvalho de Melo 11645865fe36SArnaldo Carvalho de Melo #define PERF_CONST(name) { #name, PERF_##name } 11655865fe36SArnaldo Carvalho de Melo 1166877108e4SArnaldo Carvalho de Melo static struct { 1167877108e4SArnaldo Carvalho de Melo const char *name; 1168877108e4SArnaldo Carvalho de Melo int value; 1169877108e4SArnaldo Carvalho de Melo } perf__constants[] = { 11705865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_HARDWARE), 11715865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_SOFTWARE), 11725865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_TRACEPOINT), 11735865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_HW_CACHE), 11745865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_RAW), 11755865fe36SArnaldo Carvalho de Melo PERF_CONST(TYPE_BREAKPOINT), 1176877108e4SArnaldo Carvalho de Melo 11775865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CPU_CYCLES), 11785865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_INSTRUCTIONS), 11795865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_REFERENCES), 11805865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_MISSES), 11815865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS), 11825865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_BRANCH_MISSES), 11835865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_BUS_CYCLES), 11845865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_L1D), 11855865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_L1I), 11865865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_LL), 11875865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_DTLB), 11885865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_ITLB), 11895865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_BPU), 11905865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_OP_READ), 11915865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_OP_WRITE), 11925865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH), 11935865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS), 11945865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_CACHE_RESULT_MISS), 1195877108e4SArnaldo Carvalho de Melo 11965865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND), 11975865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND), 1198129c04cbSIngo Molnar 11995865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_CPU_CLOCK), 12005865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_TASK_CLOCK), 12015865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_PAGE_FAULTS), 12025865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_CONTEXT_SWITCHES), 12035865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_CPU_MIGRATIONS), 12045865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN), 12055865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ), 12065865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS), 12075865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_EMULATION_FAULTS), 12085865fe36SArnaldo Carvalho de Melo PERF_CONST(COUNT_SW_DUMMY), 1209877108e4SArnaldo Carvalho de Melo 12105865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_IP), 12115865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_TID), 12125865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_TIME), 12135865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_ADDR), 12145865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_READ), 12155865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_CALLCHAIN), 12165865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_ID), 12175865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_CPU), 12185865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_PERIOD), 12195865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_STREAM_ID), 12205865fe36SArnaldo Carvalho de Melo PERF_CONST(SAMPLE_RAW), 1221877108e4SArnaldo Carvalho de Melo 12225865fe36SArnaldo Carvalho de Melo PERF_CONST(FORMAT_TOTAL_TIME_ENABLED), 12235865fe36SArnaldo Carvalho de Melo PERF_CONST(FORMAT_TOTAL_TIME_RUNNING), 12245865fe36SArnaldo Carvalho de Melo PERF_CONST(FORMAT_ID), 12255865fe36SArnaldo Carvalho de Melo PERF_CONST(FORMAT_GROUP), 1226877108e4SArnaldo Carvalho de Melo 12275865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_MMAP), 12285865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_LOST), 12295865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_COMM), 12305865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_EXIT), 12315865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_THROTTLE), 12325865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_UNTHROTTLE), 12335865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_FORK), 12345865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_READ), 12355865fe36SArnaldo Carvalho de Melo PERF_CONST(RECORD_SAMPLE), 123684576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_MMAP2), 123784576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_AUX), 123884576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_ITRACE_START), 123984576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_LOST_SAMPLES), 124084576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_SWITCH), 124184576da2SArnaldo Carvalho de Melo PERF_CONST(RECORD_SWITCH_CPU_WIDE), 1242ae938802SArnaldo Carvalho de Melo 1243ae938802SArnaldo Carvalho de Melo PERF_CONST(RECORD_MISC_SWITCH_OUT), 1244f6bbc1daSArnaldo Carvalho de Melo { .name = NULL, }, 1245877108e4SArnaldo Carvalho de Melo }; 1246877108e4SArnaldo Carvalho de Melo 12471075fbb2SJiri Olsa static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel, 12481075fbb2SJiri Olsa PyObject *args, PyObject *kwargs) 12491075fbb2SJiri Olsa { 125097fbf3f0STzvetomir Stoyanov struct tep_event *tp_format; 12511075fbb2SJiri Olsa static char *kwlist[] = { "sys", "name", NULL }; 12521075fbb2SJiri Olsa char *sys = NULL; 12531075fbb2SJiri Olsa char *name = NULL; 12541075fbb2SJiri Olsa 12551075fbb2SJiri Olsa if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist, 12561075fbb2SJiri Olsa &sys, &name)) 12571075fbb2SJiri Olsa return NULL; 12581075fbb2SJiri Olsa 12591075fbb2SJiri Olsa tp_format = trace_event__tp_format(sys, name); 12601075fbb2SJiri Olsa if (IS_ERR(tp_format)) 126166dfdff0SJaroslav Škarvada return _PyLong_FromLong(-1); 12621075fbb2SJiri Olsa 126366dfdff0SJaroslav Škarvada return _PyLong_FromLong(tp_format->id); 12641075fbb2SJiri Olsa } 12651075fbb2SJiri Olsa 1266877108e4SArnaldo Carvalho de Melo static PyMethodDef perf__methods[] = { 12671075fbb2SJiri Olsa { 12681075fbb2SJiri Olsa .ml_name = "tracepoint", 12691075fbb2SJiri Olsa .ml_meth = (PyCFunction) pyrf__tracepoint, 12701075fbb2SJiri Olsa .ml_flags = METH_VARARGS | METH_KEYWORDS, 12711075fbb2SJiri Olsa .ml_doc = PyDoc_STR("Get tracepoint config.") 12721075fbb2SJiri Olsa }, 1273f6bbc1daSArnaldo Carvalho de Melo { .ml_name = NULL, } 1274877108e4SArnaldo Carvalho de Melo }; 1275877108e4SArnaldo Carvalho de Melo 127666dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 1277877108e4SArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void) 127866dfdff0SJaroslav Škarvada #else 127966dfdff0SJaroslav Škarvada PyMODINIT_FUNC PyInit_perf(void) 128066dfdff0SJaroslav Škarvada #endif 1281877108e4SArnaldo Carvalho de Melo { 1282877108e4SArnaldo Carvalho de Melo PyObject *obj; 1283877108e4SArnaldo Carvalho de Melo int i; 128466dfdff0SJaroslav Škarvada PyObject *dict; 128566dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 128666dfdff0SJaroslav Škarvada PyObject *module = Py_InitModule("perf", perf__methods); 128766dfdff0SJaroslav Škarvada #else 128866dfdff0SJaroslav Škarvada static struct PyModuleDef moduledef = { 128966dfdff0SJaroslav Škarvada PyModuleDef_HEAD_INIT, 129066dfdff0SJaroslav Škarvada "perf", /* m_name */ 129166dfdff0SJaroslav Škarvada "", /* m_doc */ 129266dfdff0SJaroslav Škarvada -1, /* m_size */ 129366dfdff0SJaroslav Škarvada perf__methods, /* m_methods */ 129466dfdff0SJaroslav Škarvada NULL, /* m_reload */ 129566dfdff0SJaroslav Škarvada NULL, /* m_traverse */ 129666dfdff0SJaroslav Škarvada NULL, /* m_clear */ 129766dfdff0SJaroslav Škarvada NULL, /* m_free */ 129866dfdff0SJaroslav Škarvada }; 129966dfdff0SJaroslav Škarvada PyObject *module = PyModule_Create(&moduledef); 130066dfdff0SJaroslav Škarvada #endif 1301877108e4SArnaldo Carvalho de Melo 1302877108e4SArnaldo Carvalho de Melo if (module == NULL || 1303877108e4SArnaldo Carvalho de Melo pyrf_event__setup_types() < 0 || 1304877108e4SArnaldo Carvalho de Melo pyrf_evlist__setup_types() < 0 || 1305877108e4SArnaldo Carvalho de Melo pyrf_evsel__setup_types() < 0 || 1306877108e4SArnaldo Carvalho de Melo pyrf_thread_map__setup_types() < 0 || 1307877108e4SArnaldo Carvalho de Melo pyrf_cpu_map__setup_types() < 0) 130866dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3 1309877108e4SArnaldo Carvalho de Melo return; 131066dfdff0SJaroslav Škarvada #else 131166dfdff0SJaroslav Škarvada return module; 131266dfdff0SJaroslav Škarvada #endif 1313877108e4SArnaldo Carvalho de Melo 1314918512b4SJiri Olsa /* The page_size is placed in util object. */ 13150da2e9c2SArnaldo Carvalho de Melo page_size = sysconf(_SC_PAGE_SIZE); 13160da2e9c2SArnaldo Carvalho de Melo 1317877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_evlist__type); 1318877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type); 1319877108e4SArnaldo Carvalho de Melo 1320877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_evsel__type); 1321877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type); 1322877108e4SArnaldo Carvalho de Melo 132385e37de3SJiri Olsa Py_INCREF(&pyrf_mmap_event__type); 132485e37de3SJiri Olsa PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type); 132585e37de3SJiri Olsa 132685e37de3SJiri Olsa Py_INCREF(&pyrf_lost_event__type); 132785e37de3SJiri Olsa PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type); 132885e37de3SJiri Olsa 132985e37de3SJiri Olsa Py_INCREF(&pyrf_comm_event__type); 133085e37de3SJiri Olsa PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type); 133185e37de3SJiri Olsa 133285e37de3SJiri Olsa Py_INCREF(&pyrf_task_event__type); 133385e37de3SJiri Olsa PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type); 133485e37de3SJiri Olsa 133585e37de3SJiri Olsa Py_INCREF(&pyrf_throttle_event__type); 133685e37de3SJiri Olsa PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type); 133785e37de3SJiri Olsa 133885e37de3SJiri Olsa Py_INCREF(&pyrf_task_event__type); 133985e37de3SJiri Olsa PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type); 134085e37de3SJiri Olsa 134185e37de3SJiri Olsa Py_INCREF(&pyrf_read_event__type); 134285e37de3SJiri Olsa PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type); 134385e37de3SJiri Olsa 134485e37de3SJiri Olsa Py_INCREF(&pyrf_sample_event__type); 134585e37de3SJiri Olsa PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type); 134685e37de3SJiri Olsa 134785e37de3SJiri Olsa Py_INCREF(&pyrf_context_switch_event__type); 134885e37de3SJiri Olsa PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type); 134985e37de3SJiri Olsa 1350877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_thread_map__type); 1351877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type); 1352877108e4SArnaldo Carvalho de Melo 1353877108e4SArnaldo Carvalho de Melo Py_INCREF(&pyrf_cpu_map__type); 1354877108e4SArnaldo Carvalho de Melo PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type); 1355877108e4SArnaldo Carvalho de Melo 1356877108e4SArnaldo Carvalho de Melo dict = PyModule_GetDict(module); 1357877108e4SArnaldo Carvalho de Melo if (dict == NULL) 1358877108e4SArnaldo Carvalho de Melo goto error; 1359877108e4SArnaldo Carvalho de Melo 1360877108e4SArnaldo Carvalho de Melo for (i = 0; perf__constants[i].name != NULL; i++) { 136166dfdff0SJaroslav Škarvada obj = _PyLong_FromLong(perf__constants[i].value); 1362877108e4SArnaldo Carvalho de Melo if (obj == NULL) 1363877108e4SArnaldo Carvalho de Melo goto error; 1364877108e4SArnaldo Carvalho de Melo PyDict_SetItemString(dict, perf__constants[i].name, obj); 1365877108e4SArnaldo Carvalho de Melo Py_DECREF(obj); 1366877108e4SArnaldo Carvalho de Melo } 1367877108e4SArnaldo Carvalho de Melo 1368877108e4SArnaldo Carvalho de Melo error: 1369877108e4SArnaldo Carvalho de Melo if (PyErr_Occurred()) 1370877108e4SArnaldo Carvalho de Melo PyErr_SetString(PyExc_ImportError, "perf: Init failed!"); 137166dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION >= 3 137266dfdff0SJaroslav Škarvada return module; 137366dfdff0SJaroslav Škarvada #endif 1374877108e4SArnaldo Carvalho de Melo } 13750c6332e9SArnaldo Carvalho de Melo 13760c6332e9SArnaldo Carvalho de Melo /* 13770c6332e9SArnaldo Carvalho de Melo * Dummy, to avoid dragging all the test_attr infrastructure in the python 13780c6332e9SArnaldo Carvalho de Melo * binding. 13790c6332e9SArnaldo Carvalho de Melo */ 13800c6332e9SArnaldo Carvalho de Melo void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu, 13810c6332e9SArnaldo Carvalho de Melo int fd, int group_fd, unsigned long flags) 13820c6332e9SArnaldo Carvalho de Melo { 13830c6332e9SArnaldo Carvalho de Melo } 1384