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