blob: cf0a18d49018f17da52b00d27474477c7f567adf [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02002#include <Python.h>
3#include <structmember.h>
4#include <inttypes.h>
5#include <poll.h>
Jiri Olsa1075fbb2016-07-10 13:07:58 +02006#include <linux/err.h>
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02007#include "evlist.h"
Arnaldo Carvalho de Melo56e2e052017-04-19 18:38:33 -03008#include "callchain.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02009#include "evsel.h"
10#include "event.h"
11#include "cpumap.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030012#include "print_binary.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020013#include "thread_map.h"
Jiri Olsa721f0df2018-08-17 13:45:56 +020014#include "mmap.h"
Arnaldo Carvalho de Melo1b2fc352019-06-25 18:35:34 -030015#include "util.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020016
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010017#if PY_MAJOR_VERSION < 3
18#define _PyUnicode_FromString(arg) \
19 PyString_FromString(arg)
20#define _PyUnicode_AsString(arg) \
21 PyString_AsString(arg)
22#define _PyUnicode_FromFormat(...) \
23 PyString_FromFormat(__VA_ARGS__)
24#define _PyLong_FromLong(arg) \
25 PyInt_FromLong(arg)
26
27#else
28
29#define _PyUnicode_FromString(arg) \
30 PyUnicode_FromString(arg)
31#define _PyUnicode_FromFormat(...) \
32 PyUnicode_FromFormat(__VA_ARGS__)
33#define _PyLong_FromLong(arg) \
34 PyLong_FromLong(arg)
35#endif
36
37#ifndef Py_TYPE
38#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
39#endif
40
Adrian Hunter8afb4c02013-08-14 15:48:23 +030041/*
Arnaldo Carvalho de Melo56e2e052017-04-19 18:38:33 -030042 * Provide these two so that we don't have to link against callchain.c and
43 * start dragging hist.c, etc.
44 */
45struct callchain_param callchain_param;
46
47int parse_callchain_record(const char *arg __maybe_unused,
48 struct callchain_param *param __maybe_unused)
49{
50 return 0;
51}
52
53/*
Adrian Hunter8afb4c02013-08-14 15:48:23 +030054 * Support debug printing even though util/debug.c is not linked. That means
55 * implementing 'verbose' and 'eprintf'.
56 */
57int verbose;
58
Jiri Olsac95688a2014-07-14 23:46:49 +020059int eprintf(int level, int var, const char *fmt, ...)
Adrian Hunter8afb4c02013-08-14 15:48:23 +030060{
61 va_list args;
62 int ret = 0;
63
Jiri Olsac95688a2014-07-14 23:46:49 +020064 if (var >= level) {
Adrian Hunter8afb4c02013-08-14 15:48:23 +030065 va_start(args, fmt);
66 ret = vfprintf(stderr, fmt, args);
67 va_end(args);
68 }
69
70 return ret;
71}
72
Frederic Weisbeckercfff2d92011-02-25 21:30:16 +010073/* Define PyVarObject_HEAD_INIT for python 2.5 */
74#ifndef PyVarObject_HEAD_INIT
75# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
76#endif
77
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010078#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -020079PyMODINIT_FUNC initperf(void);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010080#else
81PyMODINIT_FUNC PyInit_perf(void);
82#endif
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -020083
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020084#define member_def(type, member, ptype, help) \
85 { #member, ptype, \
86 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
87 0, help }
88
89#define sample_member_def(name, member, ptype, help) \
90 { #name, ptype, \
91 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
92 0, help }
93
94struct pyrf_event {
95 PyObject_HEAD
Jiri Olsa32dcd022019-07-21 13:23:51 +020096 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020097 struct perf_sample sample;
98 union perf_event event;
99};
100
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200101#define sample_members \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200102 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200103 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
104 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200105 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
106 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
107 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
108 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
109 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200110 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
111
112static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
113
114static PyMemberDef pyrf_mmap_event__members[] = {
115 sample_members
116 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300117 member_def(perf_event_header, misc, T_UINT, "event misc"),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200118 member_def(mmap_event, pid, T_UINT, "event pid"),
119 member_def(mmap_event, tid, T_UINT, "event tid"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200120 member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
121 member_def(mmap_event, len, T_ULONGLONG, "map length"),
122 member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200123 member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200124 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200125};
126
127static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
128{
129 PyObject *ret;
130 char *s;
131
132 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
133 "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
134 "filename: %s }",
135 pevent->event.mmap.pid, pevent->event.mmap.tid,
136 pevent->event.mmap.start, pevent->event.mmap.len,
137 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
138 ret = PyErr_NoMemory();
139 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100140 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200141 free(s);
142 }
143 return ret;
144}
145
146static PyTypeObject pyrf_mmap_event__type = {
147 PyVarObject_HEAD_INIT(NULL, 0)
148 .tp_name = "perf.mmap_event",
149 .tp_basicsize = sizeof(struct pyrf_event),
150 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
151 .tp_doc = pyrf_mmap_event__doc,
152 .tp_members = pyrf_mmap_event__members,
153 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
154};
155
156static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
157
158static PyMemberDef pyrf_task_event__members[] = {
159 sample_members
160 member_def(perf_event_header, type, T_UINT, "event type"),
161 member_def(fork_event, pid, T_UINT, "event pid"),
162 member_def(fork_event, ppid, T_UINT, "event ppid"),
163 member_def(fork_event, tid, T_UINT, "event tid"),
164 member_def(fork_event, ptid, T_UINT, "event ptid"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200165 member_def(fork_event, time, T_ULONGLONG, "timestamp"),
166 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200167};
168
169static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
170{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100171 return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200172 "ptid: %u, time: %" PRIu64 "}",
173 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
174 pevent->event.fork.pid,
175 pevent->event.fork.ppid,
176 pevent->event.fork.tid,
177 pevent->event.fork.ptid,
178 pevent->event.fork.time);
179}
180
181static PyTypeObject pyrf_task_event__type = {
182 PyVarObject_HEAD_INIT(NULL, 0)
183 .tp_name = "perf.task_event",
184 .tp_basicsize = sizeof(struct pyrf_event),
185 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
186 .tp_doc = pyrf_task_event__doc,
187 .tp_members = pyrf_task_event__members,
188 .tp_repr = (reprfunc)pyrf_task_event__repr,
189};
190
191static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
192
193static PyMemberDef pyrf_comm_event__members[] = {
194 sample_members
195 member_def(perf_event_header, type, T_UINT, "event type"),
196 member_def(comm_event, pid, T_UINT, "event pid"),
197 member_def(comm_event, tid, T_UINT, "event tid"),
198 member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200199 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200200};
201
202static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
203{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100204 return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200205 pevent->event.comm.pid,
206 pevent->event.comm.tid,
207 pevent->event.comm.comm);
208}
209
210static PyTypeObject pyrf_comm_event__type = {
211 PyVarObject_HEAD_INIT(NULL, 0)
212 .tp_name = "perf.comm_event",
213 .tp_basicsize = sizeof(struct pyrf_event),
214 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
215 .tp_doc = pyrf_comm_event__doc,
216 .tp_members = pyrf_comm_event__members,
217 .tp_repr = (reprfunc)pyrf_comm_event__repr,
218};
219
220static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
221
222static PyMemberDef pyrf_throttle_event__members[] = {
223 sample_members
224 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200225 member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
226 member_def(throttle_event, id, T_ULONGLONG, "event id"),
227 member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
228 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200229};
230
231static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
232{
233 struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
234
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100235 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200236 ", stream_id: %" PRIu64 " }",
237 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
238 te->time, te->id, te->stream_id);
239}
240
241static PyTypeObject pyrf_throttle_event__type = {
242 PyVarObject_HEAD_INIT(NULL, 0)
243 .tp_name = "perf.throttle_event",
244 .tp_basicsize = sizeof(struct pyrf_event),
245 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
246 .tp_doc = pyrf_throttle_event__doc,
247 .tp_members = pyrf_throttle_event__members,
248 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
249};
250
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300251static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
252
253static PyMemberDef pyrf_lost_event__members[] = {
254 sample_members
255 member_def(lost_event, id, T_ULONGLONG, "event id"),
256 member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
257 { .name = NULL, },
258};
259
260static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
261{
262 PyObject *ret;
263 char *s;
264
265 if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
266 "lost: %#" PRIx64 " }",
267 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
268 ret = PyErr_NoMemory();
269 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100270 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300271 free(s);
272 }
273 return ret;
274}
275
276static PyTypeObject pyrf_lost_event__type = {
277 PyVarObject_HEAD_INIT(NULL, 0)
278 .tp_name = "perf.lost_event",
279 .tp_basicsize = sizeof(struct pyrf_event),
280 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
281 .tp_doc = pyrf_lost_event__doc,
282 .tp_members = pyrf_lost_event__members,
283 .tp_repr = (reprfunc)pyrf_lost_event__repr,
284};
285
286static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
287
288static PyMemberDef pyrf_read_event__members[] = {
289 sample_members
290 member_def(read_event, pid, T_UINT, "event pid"),
291 member_def(read_event, tid, T_UINT, "event tid"),
292 { .name = NULL, },
293};
294
295static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
296{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100297 return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300298 pevent->event.read.pid,
299 pevent->event.read.tid);
300 /*
301 * FIXME: return the array of read values,
302 * making this method useful ;-)
303 */
304}
305
306static PyTypeObject pyrf_read_event__type = {
307 PyVarObject_HEAD_INIT(NULL, 0)
308 .tp_name = "perf.read_event",
309 .tp_basicsize = sizeof(struct pyrf_event),
310 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
311 .tp_doc = pyrf_read_event__doc,
312 .tp_members = pyrf_read_event__members,
313 .tp_repr = (reprfunc)pyrf_read_event__repr,
314};
315
316static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
317
318static PyMemberDef pyrf_sample_event__members[] = {
319 sample_members
320 member_def(perf_event_header, type, T_UINT, "event type"),
321 { .name = NULL, },
322};
323
324static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
325{
326 PyObject *ret;
327 char *s;
328
329 if (asprintf(&s, "{ type: sample }") < 0) {
330 ret = PyErr_NoMemory();
331 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100332 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300333 free(s);
334 }
335 return ret;
336}
337
Jiri Olsabae57e32016-07-10 13:08:00 +0200338static bool is_tracepoint(struct pyrf_event *pevent)
339{
340 return pevent->evsel->attr.type == PERF_TYPE_TRACEPOINT;
341}
342
Jiri Olsabae57e32016-07-10 13:08:00 +0200343static PyObject*
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400344tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
Jiri Olsabae57e32016-07-10 13:08:00 +0200345{
Tzvetomir Stoyanov69769ce2019-04-01 12:43:18 -0400346 struct tep_handle *pevent = field->event->tep;
Jiri Olsabae57e32016-07-10 13:08:00 +0200347 void *data = pe->sample.raw_data;
348 PyObject *ret = NULL;
349 unsigned long long val;
350 unsigned int offset, len;
351
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400352 if (field->flags & TEP_FIELD_IS_ARRAY) {
Jiri Olsabae57e32016-07-10 13:08:00 +0200353 offset = field->offset;
354 len = field->size;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400355 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400356 val = tep_read_number(pevent, data + offset, len);
Jiri Olsabae57e32016-07-10 13:08:00 +0200357 offset = val;
358 len = offset >> 16;
359 offset &= 0xffff;
360 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400361 if (field->flags & TEP_FIELD_IS_STRING &&
Jiri Olsabae57e32016-07-10 13:08:00 +0200362 is_printable_array(data + offset, len)) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100363 ret = _PyUnicode_FromString((char *)data + offset);
Jiri Olsabae57e32016-07-10 13:08:00 +0200364 } else {
365 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400366 field->flags &= ~TEP_FIELD_IS_STRING;
Jiri Olsabae57e32016-07-10 13:08:00 +0200367 }
368 } else {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400369 val = tep_read_number(pevent, data + field->offset,
370 field->size);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400371 if (field->flags & TEP_FIELD_IS_POINTER)
Jiri Olsabae57e32016-07-10 13:08:00 +0200372 ret = PyLong_FromUnsignedLong((unsigned long) val);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400373 else if (field->flags & TEP_FIELD_IS_SIGNED)
Jiri Olsabae57e32016-07-10 13:08:00 +0200374 ret = PyLong_FromLong((long) val);
375 else
376 ret = PyLong_FromUnsignedLong((unsigned long) val);
377 }
378
379 return ret;
380}
381
382static PyObject*
383get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
384{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100385 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
Jiri Olsa32dcd022019-07-21 13:23:51 +0200386 struct evsel *evsel = pevent->evsel;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400387 struct tep_format_field *field;
Jiri Olsabae57e32016-07-10 13:08:00 +0200388
389 if (!evsel->tp_format) {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500390 struct tep_event *tp_format;
Jiri Olsabae57e32016-07-10 13:08:00 +0200391
392 tp_format = trace_event__tp_format_id(evsel->attr.config);
393 if (!tp_format)
394 return NULL;
395
396 evsel->tp_format = tp_format;
397 }
398
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -0400399 field = tep_find_any_field(evsel->tp_format, str);
Jiri Olsabae57e32016-07-10 13:08:00 +0200400 if (!field)
401 return NULL;
402
403 return tracepoint_field(pevent, field);
404}
405
406static PyObject*
407pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
408{
409 PyObject *obj = NULL;
410
411 if (is_tracepoint(pevent))
412 obj = get_tracepoint_field(pevent, attr_name);
413
414 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
415}
416
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300417static PyTypeObject pyrf_sample_event__type = {
418 PyVarObject_HEAD_INIT(NULL, 0)
419 .tp_name = "perf.sample_event",
420 .tp_basicsize = sizeof(struct pyrf_event),
421 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
422 .tp_doc = pyrf_sample_event__doc,
423 .tp_members = pyrf_sample_event__members,
424 .tp_repr = (reprfunc)pyrf_sample_event__repr,
Jiri Olsabae57e32016-07-10 13:08:00 +0200425 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300426};
427
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300428static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
429
430static PyMemberDef pyrf_context_switch_event__members[] = {
431 sample_members
432 member_def(perf_event_header, type, T_UINT, "event type"),
433 member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"),
434 member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"),
435 { .name = NULL, },
436};
437
438static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
439{
440 PyObject *ret;
441 char *s;
442
443 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
444 pevent->event.context_switch.next_prev_pid,
445 pevent->event.context_switch.next_prev_tid,
446 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
447 ret = PyErr_NoMemory();
448 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100449 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300450 free(s);
451 }
452 return ret;
453}
454
455static PyTypeObject pyrf_context_switch_event__type = {
456 PyVarObject_HEAD_INIT(NULL, 0)
457 .tp_name = "perf.context_switch_event",
458 .tp_basicsize = sizeof(struct pyrf_event),
459 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
460 .tp_doc = pyrf_context_switch_event__doc,
461 .tp_members = pyrf_context_switch_event__members,
462 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
463};
464
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200465static int pyrf_event__setup_types(void)
466{
467 int err;
468 pyrf_mmap_event__type.tp_new =
469 pyrf_task_event__type.tp_new =
470 pyrf_comm_event__type.tp_new =
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300471 pyrf_lost_event__type.tp_new =
472 pyrf_read_event__type.tp_new =
473 pyrf_sample_event__type.tp_new =
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300474 pyrf_context_switch_event__type.tp_new =
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200475 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
476 err = PyType_Ready(&pyrf_mmap_event__type);
477 if (err < 0)
478 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300479 err = PyType_Ready(&pyrf_lost_event__type);
480 if (err < 0)
481 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200482 err = PyType_Ready(&pyrf_task_event__type);
483 if (err < 0)
484 goto out;
485 err = PyType_Ready(&pyrf_comm_event__type);
486 if (err < 0)
487 goto out;
488 err = PyType_Ready(&pyrf_throttle_event__type);
489 if (err < 0)
490 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300491 err = PyType_Ready(&pyrf_read_event__type);
492 if (err < 0)
493 goto out;
494 err = PyType_Ready(&pyrf_sample_event__type);
495 if (err < 0)
496 goto out;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300497 err = PyType_Ready(&pyrf_context_switch_event__type);
498 if (err < 0)
499 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200500out:
501 return err;
502}
503
504static PyTypeObject *pyrf_event__type[] = {
505 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300506 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200507 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
508 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
509 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
510 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
511 [PERF_RECORD_FORK] = &pyrf_task_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300512 [PERF_RECORD_READ] = &pyrf_read_event__type,
513 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300514 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
515 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200516};
517
518static PyObject *pyrf_event__new(union perf_event *event)
519{
520 struct pyrf_event *pevent;
521 PyTypeObject *ptype;
522
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300523 if ((event->header.type < PERF_RECORD_MMAP ||
524 event->header.type > PERF_RECORD_SAMPLE) &&
525 !(event->header.type == PERF_RECORD_SWITCH ||
526 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200527 return NULL;
528
529 ptype = pyrf_event__type[event->header.type];
530 pevent = PyObject_New(struct pyrf_event, ptype);
531 if (pevent != NULL)
532 memcpy(&pevent->event, event, event->header.size);
533 return (PyObject *)pevent;
534}
535
536struct pyrf_cpu_map {
537 PyObject_HEAD
538
Jiri Olsaf8548392019-07-21 13:23:49 +0200539 struct perf_cpu_map *cpus;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200540};
541
542static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
543 PyObject *args, PyObject *kwargs)
544{
Frederic Weisbecker64348152011-03-31 18:27:43 +0200545 static char *kwlist[] = { "cpustr", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200546 char *cpustr = NULL;
547
548 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
549 kwlist, &cpustr))
550 return -1;
551
552 pcpus->cpus = cpu_map__new(cpustr);
553 if (pcpus->cpus == NULL)
554 return -1;
555 return 0;
556}
557
558static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
559{
Jiri Olsa38f01d82019-07-21 13:24:17 +0200560 perf_cpu_map__put(pcpus->cpus);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100561 Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200562}
563
564static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
565{
566 struct pyrf_cpu_map *pcpus = (void *)obj;
567
568 return pcpus->cpus->nr;
569}
570
571static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
572{
573 struct pyrf_cpu_map *pcpus = (void *)obj;
574
575 if (i >= pcpus->cpus->nr)
576 return NULL;
577
578 return Py_BuildValue("i", pcpus->cpus->map[i]);
579}
580
581static PySequenceMethods pyrf_cpu_map__sequence_methods = {
582 .sq_length = pyrf_cpu_map__length,
583 .sq_item = pyrf_cpu_map__item,
584};
585
586static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
587
588static PyTypeObject pyrf_cpu_map__type = {
589 PyVarObject_HEAD_INIT(NULL, 0)
590 .tp_name = "perf.cpu_map",
591 .tp_basicsize = sizeof(struct pyrf_cpu_map),
592 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
593 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
594 .tp_doc = pyrf_cpu_map__doc,
595 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
596 .tp_init = (initproc)pyrf_cpu_map__init,
597};
598
599static int pyrf_cpu_map__setup_types(void)
600{
601 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
602 return PyType_Ready(&pyrf_cpu_map__type);
603}
604
605struct pyrf_thread_map {
606 PyObject_HEAD
607
Jiri Olsa9749b902019-07-21 13:23:50 +0200608 struct perf_thread_map *threads;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200609};
610
611static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
612 PyObject *args, PyObject *kwargs)
613{
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200614 static char *kwlist[] = { "pid", "tid", "uid", NULL };
615 int pid = -1, tid = -1, uid = UINT_MAX;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200616
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200617 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
618 kwlist, &pid, &tid, &uid))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200619 return -1;
620
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200621 pthreads->threads = thread_map__new(pid, tid, uid);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200622 if (pthreads->threads == NULL)
623 return -1;
624 return 0;
625}
626
627static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
628{
Jiri Olsa7836e522019-07-21 13:24:20 +0200629 perf_thread_map__put(pthreads->threads);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100630 Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200631}
632
633static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
634{
635 struct pyrf_thread_map *pthreads = (void *)obj;
636
637 return pthreads->threads->nr;
638}
639
640static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
641{
642 struct pyrf_thread_map *pthreads = (void *)obj;
643
644 if (i >= pthreads->threads->nr)
645 return NULL;
646
647 return Py_BuildValue("i", pthreads->threads->map[i]);
648}
649
650static PySequenceMethods pyrf_thread_map__sequence_methods = {
651 .sq_length = pyrf_thread_map__length,
652 .sq_item = pyrf_thread_map__item,
653};
654
655static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
656
657static PyTypeObject pyrf_thread_map__type = {
658 PyVarObject_HEAD_INIT(NULL, 0)
659 .tp_name = "perf.thread_map",
660 .tp_basicsize = sizeof(struct pyrf_thread_map),
661 .tp_dealloc = (destructor)pyrf_thread_map__delete,
662 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
663 .tp_doc = pyrf_thread_map__doc,
664 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
665 .tp_init = (initproc)pyrf_thread_map__init,
666};
667
668static int pyrf_thread_map__setup_types(void)
669{
670 pyrf_thread_map__type.tp_new = PyType_GenericNew;
671 return PyType_Ready(&pyrf_thread_map__type);
672}
673
674struct pyrf_evsel {
675 PyObject_HEAD
676
Jiri Olsa32dcd022019-07-21 13:23:51 +0200677 struct evsel evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200678};
679
680static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
681 PyObject *args, PyObject *kwargs)
682{
683 struct perf_event_attr attr = {
684 .type = PERF_TYPE_HARDWARE,
685 .config = PERF_COUNT_HW_CPU_CYCLES,
686 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
687 };
688 static char *kwlist[] = {
689 "type",
690 "config",
691 "sample_freq",
692 "sample_period",
693 "sample_type",
694 "read_format",
695 "disabled",
696 "inherit",
697 "pinned",
698 "exclusive",
699 "exclude_user",
700 "exclude_kernel",
701 "exclude_hv",
702 "exclude_idle",
703 "mmap",
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300704 "context_switch",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200705 "comm",
706 "freq",
707 "inherit_stat",
708 "enable_on_exec",
709 "task",
710 "watermark",
711 "precise_ip",
712 "mmap_data",
713 "sample_id_all",
714 "wakeup_events",
715 "bp_type",
716 "bp_addr",
Frederic Weisbecker64348152011-03-31 18:27:43 +0200717 "bp_len",
718 NULL
719 };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200720 u64 sample_period = 0;
721 u32 disabled = 0,
722 inherit = 0,
723 pinned = 0,
724 exclusive = 0,
725 exclude_user = 0,
726 exclude_kernel = 0,
727 exclude_hv = 0,
728 exclude_idle = 0,
729 mmap = 0,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300730 context_switch = 0,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200731 comm = 0,
732 freq = 1,
733 inherit_stat = 0,
734 enable_on_exec = 0,
735 task = 0,
736 watermark = 0,
737 precise_ip = 0,
738 mmap_data = 0,
739 sample_id_all = 1;
740 int idx = 0;
741
742 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300743 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200744 &attr.type, &attr.config, &attr.sample_freq,
745 &sample_period, &attr.sample_type,
746 &attr.read_format, &disabled, &inherit,
747 &pinned, &exclusive, &exclude_user,
748 &exclude_kernel, &exclude_hv, &exclude_idle,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300749 &mmap, &context_switch, &comm, &freq, &inherit_stat,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200750 &enable_on_exec, &task, &watermark,
751 &precise_ip, &mmap_data, &sample_id_all,
752 &attr.wakeup_events, &attr.bp_type,
753 &attr.bp_addr, &attr.bp_len, &idx))
754 return -1;
755
756 /* union... */
757 if (sample_period != 0) {
758 if (attr.sample_freq != 0)
759 return -1; /* FIXME: throw right exception */
760 attr.sample_period = sample_period;
761 }
762
763 /* Bitfields */
764 attr.disabled = disabled;
765 attr.inherit = inherit;
766 attr.pinned = pinned;
767 attr.exclusive = exclusive;
768 attr.exclude_user = exclude_user;
769 attr.exclude_kernel = exclude_kernel;
770 attr.exclude_hv = exclude_hv;
771 attr.exclude_idle = exclude_idle;
772 attr.mmap = mmap;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300773 attr.context_switch = context_switch;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200774 attr.comm = comm;
775 attr.freq = freq;
776 attr.inherit_stat = inherit_stat;
777 attr.enable_on_exec = enable_on_exec;
778 attr.task = task;
779 attr.watermark = watermark;
780 attr.precise_ip = precise_ip;
781 attr.mmap_data = mmap_data;
782 attr.sample_id_all = sample_id_all;
Jiri Olsaad4e3c042016-07-10 13:07:55 +0200783 attr.size = sizeof(attr);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200784
Jiri Olsab4b62ee2019-07-21 13:23:53 +0200785 evsel__init(&pevsel->evsel, &attr, idx);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200786 return 0;
787}
788
789static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
790{
791 perf_evsel__exit(&pevsel->evsel);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100792 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200793}
794
795static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
796 PyObject *args, PyObject *kwargs)
797{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200798 struct evsel *evsel = &pevsel->evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +0200799 struct perf_cpu_map *cpus = NULL;
Jiri Olsa9749b902019-07-21 13:23:50 +0200800 struct perf_thread_map *threads = NULL;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200801 PyObject *pcpus = NULL, *pthreads = NULL;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300802 int group = 0, inherit = 0;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200803 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200804
805 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300806 &pcpus, &pthreads, &group, &inherit))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200807 return NULL;
808
809 if (pthreads != NULL)
810 threads = ((struct pyrf_thread_map *)pthreads)->threads;
811
812 if (pcpus != NULL)
813 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
814
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300815 evsel->attr.inherit = inherit;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200816 /*
817 * This will group just the fds for this single evsel, to group
818 * multiple events, use evlist.open().
819 */
Jiri Olsa5972d1e2019-07-21 13:24:01 +0200820 if (evsel__open(evsel, cpus, threads) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200821 PyErr_SetFromErrno(PyExc_OSError);
822 return NULL;
823 }
824
825 Py_INCREF(Py_None);
826 return Py_None;
827}
828
829static PyMethodDef pyrf_evsel__methods[] = {
830 {
831 .ml_name = "open",
832 .ml_meth = (PyCFunction)pyrf_evsel__open,
833 .ml_flags = METH_VARARGS | METH_KEYWORDS,
834 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
835 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200836 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200837};
838
839static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
840
841static PyTypeObject pyrf_evsel__type = {
842 PyVarObject_HEAD_INIT(NULL, 0)
843 .tp_name = "perf.evsel",
844 .tp_basicsize = sizeof(struct pyrf_evsel),
845 .tp_dealloc = (destructor)pyrf_evsel__delete,
846 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
847 .tp_doc = pyrf_evsel__doc,
848 .tp_methods = pyrf_evsel__methods,
849 .tp_init = (initproc)pyrf_evsel__init,
850};
851
852static int pyrf_evsel__setup_types(void)
853{
854 pyrf_evsel__type.tp_new = PyType_GenericNew;
855 return PyType_Ready(&pyrf_evsel__type);
856}
857
858struct pyrf_evlist {
859 PyObject_HEAD
860
Jiri Olsa63503db2019-07-21 13:23:52 +0200861 struct evlist evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200862};
863
864static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300865 PyObject *args, PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200866{
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200867 PyObject *pcpus = NULL, *pthreads = NULL;
Jiri Olsaf8548392019-07-21 13:23:49 +0200868 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +0200869 struct perf_thread_map *threads;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200870
871 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
872 return -1;
873
874 threads = ((struct pyrf_thread_map *)pthreads)->threads;
875 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
Jiri Olsa52c86bc2019-07-21 13:23:54 +0200876 evlist__init(&pevlist->evlist, cpus, threads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200877 return 0;
878}
879
880static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
881{
882 perf_evlist__exit(&pevlist->evlist);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100883 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200884}
885
886static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
887 PyObject *args, PyObject *kwargs)
888{
Jiri Olsa63503db2019-07-21 13:23:52 +0200889 struct evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200890 static char *kwlist[] = { "pages", "overwrite", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200891 int pages = 128, overwrite = false;
892
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200893 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
894 &pages, &overwrite))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200895 return NULL;
896
Wang Nanf74b9d3a2017-12-03 02:00:37 +0000897 if (perf_evlist__mmap(evlist, pages) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200898 PyErr_SetFromErrno(PyExc_OSError);
899 return NULL;
900 }
901
902 Py_INCREF(Py_None);
903 return Py_None;
904}
905
906static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
907 PyObject *args, PyObject *kwargs)
908{
Jiri Olsa63503db2019-07-21 13:23:52 +0200909 struct evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200910 static char *kwlist[] = { "timeout", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200911 int timeout = -1, n;
912
913 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
914 return NULL;
915
Arnaldo Carvalho de Melof66a889d2014-08-18 17:25:59 -0300916 n = perf_evlist__poll(evlist, timeout);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200917 if (n < 0) {
918 PyErr_SetFromErrno(PyExc_OSError);
919 return NULL;
920 }
921
922 return Py_BuildValue("i", n);
923}
924
925static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300926 PyObject *args __maybe_unused,
927 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200928{
Jiri Olsa63503db2019-07-21 13:23:52 +0200929 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200930 PyObject *list = PyList_New(0);
931 int i;
932
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300933 for (i = 0; i < evlist->pollfd.nr; ++i) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200934 PyObject *file;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100935#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300936 FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200937
938 if (fp == NULL)
939 goto free_list;
940
941 file = PyFile_FromFile(fp, "perf", "r", NULL);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100942#else
Jiri Olsaa389aec2018-12-26 12:21:21 +0100943 file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1,
944 NULL, NULL, NULL, 0);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100945#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200946 if (file == NULL)
947 goto free_list;
948
949 if (PyList_Append(list, file) != 0) {
950 Py_DECREF(file);
951 goto free_list;
952 }
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300953
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200954 Py_DECREF(file);
955 }
956
957 return list;
958free_list:
959 return PyErr_NoMemory();
960}
961
962
963static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300964 PyObject *args,
965 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200966{
Jiri Olsa63503db2019-07-21 13:23:52 +0200967 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200968 PyObject *pevsel;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200969 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200970
971 if (!PyArg_ParseTuple(args, "O", &pevsel))
972 return NULL;
973
974 Py_INCREF(pevsel);
975 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200976 evsel->idx = evlist->core.nr_entries;
Jiri Olsaa1cf3a72019-07-21 13:23:59 +0200977 evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200978
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200979 return Py_BuildValue("i", evlist->core.nr_entries);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200980}
981
Jiri Olsa63503db2019-07-21 13:23:52 +0200982static struct perf_mmap *get_md(struct evlist *evlist, int cpu)
Jiri Olsa721f0df2018-08-17 13:45:56 +0200983{
984 int i;
985
986 for (i = 0; i < evlist->nr_mmaps; i++) {
987 struct perf_mmap *md = &evlist->mmap[i];
988
989 if (md->cpu == cpu)
990 return md;
991 }
992
993 return NULL;
994}
995
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200996static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
997 PyObject *args, PyObject *kwargs)
998{
Jiri Olsa63503db2019-07-21 13:23:52 +0200999 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001000 union perf_event *event;
1001 int sample_id_all = 1, cpu;
Frederic Weisbecker64348152011-03-31 18:27:43 +02001002 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
Kan Liang35b7cdc2018-03-01 18:09:00 -05001003 struct perf_mmap *md;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001004 int err;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001005
1006 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1007 &cpu, &sample_id_all))
1008 return NULL;
1009
Jiri Olsa721f0df2018-08-17 13:45:56 +02001010 md = get_md(evlist, cpu);
1011 if (!md)
1012 return NULL;
1013
Kan Liangb9bae2c2018-03-06 10:36:07 -05001014 if (perf_mmap__read_init(md) < 0)
Kan Liang35b7cdc2018-03-01 18:09:00 -05001015 goto end;
1016
Kan Liang0019dc872018-03-06 10:36:06 -05001017 event = perf_mmap__read_event(md);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001018 if (event != NULL) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001019 PyObject *pyevent = pyrf_event__new(event);
1020 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001021 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001022
1023 if (pyevent == NULL)
1024 return PyErr_NoMemory();
1025
Jiri Olsa377f6982016-07-10 13:07:59 +02001026 evsel = perf_evlist__event2evsel(evlist, event);
Petr Machata83428f22018-03-22 00:57:32 +01001027 if (!evsel) {
1028 Py_INCREF(Py_None);
Jiri Olsa377f6982016-07-10 13:07:59 +02001029 return Py_None;
Petr Machata83428f22018-03-22 00:57:32 +01001030 }
Jiri Olsa377f6982016-07-10 13:07:59 +02001031
1032 pevent->evsel = evsel;
1033
1034 err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
Jiri Olsae8968e62016-07-10 13:07:56 +02001035
1036 /* Consume the even only after we parsed it out. */
Kan Liangd6ace3d2018-03-06 10:36:05 -05001037 perf_mmap__consume(md);
Jiri Olsae8968e62016-07-10 13:07:56 +02001038
Arnaldo Carvalho de Melo5c6970a2011-06-02 10:55:10 -03001039 if (err)
1040 return PyErr_Format(PyExc_OSError,
1041 "perf: can't parse sample, err=%d", err);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001042 return pyevent;
1043 }
Kan Liang35b7cdc2018-03-01 18:09:00 -05001044end:
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001045 Py_INCREF(Py_None);
1046 return Py_None;
1047}
1048
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001049static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1050 PyObject *args, PyObject *kwargs)
1051{
Jiri Olsa63503db2019-07-21 13:23:52 +02001052 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001053 int group = 0;
1054 static char *kwlist[] = { "group", NULL };
1055
1056 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1057 return NULL;
1058
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001059 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001060 perf_evlist__set_leader(evlist);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001061
Jiri Olsa474ddc42019-07-21 13:24:06 +02001062 if (evlist__open(evlist) < 0) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001063 PyErr_SetFromErrno(PyExc_OSError);
1064 return NULL;
1065 }
1066
1067 Py_INCREF(Py_None);
1068 return Py_None;
1069}
1070
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001071static PyMethodDef pyrf_evlist__methods[] = {
1072 {
1073 .ml_name = "mmap",
1074 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1075 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1076 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1077 },
1078 {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001079 .ml_name = "open",
1080 .ml_meth = (PyCFunction)pyrf_evlist__open,
1081 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1082 .ml_doc = PyDoc_STR("open the file descriptors.")
1083 },
1084 {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001085 .ml_name = "poll",
1086 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1087 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1088 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1089 },
1090 {
1091 .ml_name = "get_pollfd",
1092 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1093 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1094 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1095 },
1096 {
1097 .ml_name = "add",
1098 .ml_meth = (PyCFunction)pyrf_evlist__add,
1099 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1100 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1101 },
1102 {
1103 .ml_name = "read_on_cpu",
1104 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1105 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1106 .ml_doc = PyDoc_STR("reads an event.")
1107 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001108 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001109};
1110
1111static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1112{
1113 struct pyrf_evlist *pevlist = (void *)obj;
1114
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001115 return pevlist->evlist.core.nr_entries;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001116}
1117
1118static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1119{
1120 struct pyrf_evlist *pevlist = (void *)obj;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001121 struct evsel *pos;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001122
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001123 if (i >= pevlist->evlist.core.nr_entries)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001124 return NULL;
1125
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001126 evlist__for_each_entry(&pevlist->evlist, pos) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001127 if (i-- == 0)
1128 break;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001129 }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001130
1131 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1132}
1133
1134static PySequenceMethods pyrf_evlist__sequence_methods = {
1135 .sq_length = pyrf_evlist__length,
1136 .sq_item = pyrf_evlist__item,
1137};
1138
1139static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1140
1141static PyTypeObject pyrf_evlist__type = {
1142 PyVarObject_HEAD_INIT(NULL, 0)
1143 .tp_name = "perf.evlist",
1144 .tp_basicsize = sizeof(struct pyrf_evlist),
1145 .tp_dealloc = (destructor)pyrf_evlist__delete,
1146 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1147 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1148 .tp_doc = pyrf_evlist__doc,
1149 .tp_methods = pyrf_evlist__methods,
1150 .tp_init = (initproc)pyrf_evlist__init,
1151};
1152
1153static int pyrf_evlist__setup_types(void)
1154{
1155 pyrf_evlist__type.tp_new = PyType_GenericNew;
1156 return PyType_Ready(&pyrf_evlist__type);
1157}
1158
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001159#define PERF_CONST(name) { #name, PERF_##name }
1160
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001161static struct {
1162 const char *name;
1163 int value;
1164} perf__constants[] = {
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001165 PERF_CONST(TYPE_HARDWARE),
1166 PERF_CONST(TYPE_SOFTWARE),
1167 PERF_CONST(TYPE_TRACEPOINT),
1168 PERF_CONST(TYPE_HW_CACHE),
1169 PERF_CONST(TYPE_RAW),
1170 PERF_CONST(TYPE_BREAKPOINT),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001171
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001172 PERF_CONST(COUNT_HW_CPU_CYCLES),
1173 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1174 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1175 PERF_CONST(COUNT_HW_CACHE_MISSES),
1176 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1177 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1178 PERF_CONST(COUNT_HW_BUS_CYCLES),
1179 PERF_CONST(COUNT_HW_CACHE_L1D),
1180 PERF_CONST(COUNT_HW_CACHE_L1I),
1181 PERF_CONST(COUNT_HW_CACHE_LL),
1182 PERF_CONST(COUNT_HW_CACHE_DTLB),
1183 PERF_CONST(COUNT_HW_CACHE_ITLB),
1184 PERF_CONST(COUNT_HW_CACHE_BPU),
1185 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1186 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1187 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1188 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1189 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001190
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001191 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1192 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
Ingo Molnar129c04c2011-04-29 14:41:28 +02001193
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001194 PERF_CONST(COUNT_SW_CPU_CLOCK),
1195 PERF_CONST(COUNT_SW_TASK_CLOCK),
1196 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1197 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1198 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1199 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1200 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1201 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1202 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1203 PERF_CONST(COUNT_SW_DUMMY),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001204
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001205 PERF_CONST(SAMPLE_IP),
1206 PERF_CONST(SAMPLE_TID),
1207 PERF_CONST(SAMPLE_TIME),
1208 PERF_CONST(SAMPLE_ADDR),
1209 PERF_CONST(SAMPLE_READ),
1210 PERF_CONST(SAMPLE_CALLCHAIN),
1211 PERF_CONST(SAMPLE_ID),
1212 PERF_CONST(SAMPLE_CPU),
1213 PERF_CONST(SAMPLE_PERIOD),
1214 PERF_CONST(SAMPLE_STREAM_ID),
1215 PERF_CONST(SAMPLE_RAW),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001216
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001217 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1218 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1219 PERF_CONST(FORMAT_ID),
1220 PERF_CONST(FORMAT_GROUP),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001221
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001222 PERF_CONST(RECORD_MMAP),
1223 PERF_CONST(RECORD_LOST),
1224 PERF_CONST(RECORD_COMM),
1225 PERF_CONST(RECORD_EXIT),
1226 PERF_CONST(RECORD_THROTTLE),
1227 PERF_CONST(RECORD_UNTHROTTLE),
1228 PERF_CONST(RECORD_FORK),
1229 PERF_CONST(RECORD_READ),
1230 PERF_CONST(RECORD_SAMPLE),
Arnaldo Carvalho de Melo84576da2015-07-24 13:04:09 -03001231 PERF_CONST(RECORD_MMAP2),
1232 PERF_CONST(RECORD_AUX),
1233 PERF_CONST(RECORD_ITRACE_START),
1234 PERF_CONST(RECORD_LOST_SAMPLES),
1235 PERF_CONST(RECORD_SWITCH),
1236 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -03001237
1238 PERF_CONST(RECORD_MISC_SWITCH_OUT),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001239 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001240};
1241
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001242static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1243 PyObject *args, PyObject *kwargs)
1244{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001245 struct tep_event *tp_format;
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001246 static char *kwlist[] = { "sys", "name", NULL };
1247 char *sys = NULL;
1248 char *name = NULL;
1249
1250 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1251 &sys, &name))
1252 return NULL;
1253
1254 tp_format = trace_event__tp_format(sys, name);
1255 if (IS_ERR(tp_format))
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001256 return _PyLong_FromLong(-1);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001257
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001258 return _PyLong_FromLong(tp_format->id);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001259}
1260
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001261static PyMethodDef perf__methods[] = {
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001262 {
1263 .ml_name = "tracepoint",
1264 .ml_meth = (PyCFunction) pyrf__tracepoint,
1265 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1266 .ml_doc = PyDoc_STR("Get tracepoint config.")
1267 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001268 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001269};
1270
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001271#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001272PyMODINIT_FUNC initperf(void)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001273#else
1274PyMODINIT_FUNC PyInit_perf(void)
1275#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001276{
1277 PyObject *obj;
1278 int i;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001279 PyObject *dict;
1280#if PY_MAJOR_VERSION < 3
1281 PyObject *module = Py_InitModule("perf", perf__methods);
1282#else
1283 static struct PyModuleDef moduledef = {
1284 PyModuleDef_HEAD_INIT,
1285 "perf", /* m_name */
1286 "", /* m_doc */
1287 -1, /* m_size */
1288 perf__methods, /* m_methods */
1289 NULL, /* m_reload */
1290 NULL, /* m_traverse */
1291 NULL, /* m_clear */
1292 NULL, /* m_free */
1293 };
1294 PyObject *module = PyModule_Create(&moduledef);
1295#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001296
1297 if (module == NULL ||
1298 pyrf_event__setup_types() < 0 ||
1299 pyrf_evlist__setup_types() < 0 ||
1300 pyrf_evsel__setup_types() < 0 ||
1301 pyrf_thread_map__setup_types() < 0 ||
1302 pyrf_cpu_map__setup_types() < 0)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001303#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001304 return;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001305#else
1306 return module;
1307#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001308
Jiri Olsa918512b2013-09-12 18:39:35 +02001309 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0da2e9c2012-10-16 14:51:04 -03001310 page_size = sysconf(_SC_PAGE_SIZE);
1311
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001312 Py_INCREF(&pyrf_evlist__type);
1313 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1314
1315 Py_INCREF(&pyrf_evsel__type);
1316 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1317
Jiri Olsa85e37de2016-07-10 13:07:57 +02001318 Py_INCREF(&pyrf_mmap_event__type);
1319 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1320
1321 Py_INCREF(&pyrf_lost_event__type);
1322 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1323
1324 Py_INCREF(&pyrf_comm_event__type);
1325 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1326
1327 Py_INCREF(&pyrf_task_event__type);
1328 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1329
1330 Py_INCREF(&pyrf_throttle_event__type);
1331 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1332
1333 Py_INCREF(&pyrf_task_event__type);
1334 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1335
1336 Py_INCREF(&pyrf_read_event__type);
1337 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1338
1339 Py_INCREF(&pyrf_sample_event__type);
1340 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1341
1342 Py_INCREF(&pyrf_context_switch_event__type);
1343 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1344
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001345 Py_INCREF(&pyrf_thread_map__type);
1346 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1347
1348 Py_INCREF(&pyrf_cpu_map__type);
1349 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1350
1351 dict = PyModule_GetDict(module);
1352 if (dict == NULL)
1353 goto error;
1354
1355 for (i = 0; perf__constants[i].name != NULL; i++) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001356 obj = _PyLong_FromLong(perf__constants[i].value);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001357 if (obj == NULL)
1358 goto error;
1359 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1360 Py_DECREF(obj);
1361 }
1362
1363error:
1364 if (PyErr_Occurred())
1365 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001366#if PY_MAJOR_VERSION >= 3
1367 return module;
1368#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001369}
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -03001370
1371/*
1372 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1373 * binding.
1374 */
1375void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
1376 int fd, int group_fd, unsigned long flags)
1377{
1378}