blob: 53f31053a27a8ef97c61ff0d55074669f53d0848 [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>
Jiri Olsa9c3516d2019-07-21 13:24:30 +02007#include <perf/cpumap.h>
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -03008#include <traceevent/event-parse.h>
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02009#include "evlist.h"
Arnaldo Carvalho de Melo56e2e052017-04-19 18:38:33 -030010#include "callchain.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020011#include "evsel.h"
12#include "event.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030013#include "print_binary.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020014#include "thread_map.h"
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030015#include "trace-event.h"
Jiri Olsa721f0df2018-08-17 13:45:56 +020016#include "mmap.h"
Jiri Olsa20f2be12019-08-06 15:25:25 +020017#include <internal/lib.h>
Arnaldo Carvalho de Melo91854f92019-08-29 14:59:50 -030018#include "../perf-sys.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020019
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010020#if PY_MAJOR_VERSION < 3
21#define _PyUnicode_FromString(arg) \
22 PyString_FromString(arg)
23#define _PyUnicode_AsString(arg) \
24 PyString_AsString(arg)
25#define _PyUnicode_FromFormat(...) \
26 PyString_FromFormat(__VA_ARGS__)
27#define _PyLong_FromLong(arg) \
28 PyInt_FromLong(arg)
29
30#else
31
32#define _PyUnicode_FromString(arg) \
33 PyUnicode_FromString(arg)
34#define _PyUnicode_FromFormat(...) \
35 PyUnicode_FromFormat(__VA_ARGS__)
36#define _PyLong_FromLong(arg) \
37 PyLong_FromLong(arg)
38#endif
39
40#ifndef Py_TYPE
41#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
42#endif
43
Adrian Hunter8afb4c02013-08-14 15:48:23 +030044/*
Arnaldo Carvalho de Melo56e2e052017-04-19 18:38:33 -030045 * Provide these two so that we don't have to link against callchain.c and
46 * start dragging hist.c, etc.
47 */
48struct callchain_param callchain_param;
49
50int parse_callchain_record(const char *arg __maybe_unused,
51 struct callchain_param *param __maybe_unused)
52{
53 return 0;
54}
55
56/*
Adrian Hunter8afb4c02013-08-14 15:48:23 +030057 * Support debug printing even though util/debug.c is not linked. That means
58 * implementing 'verbose' and 'eprintf'.
59 */
60int verbose;
61
Arnaldo Carvalho de Melo5939cac2019-09-11 12:54:33 +010062int eprintf(int level, int var, const char *fmt, ...);
63
Jiri Olsac95688a2014-07-14 23:46:49 +020064int eprintf(int level, int var, const char *fmt, ...)
Adrian Hunter8afb4c02013-08-14 15:48:23 +030065{
66 va_list args;
67 int ret = 0;
68
Jiri Olsac95688a2014-07-14 23:46:49 +020069 if (var >= level) {
Adrian Hunter8afb4c02013-08-14 15:48:23 +030070 va_start(args, fmt);
71 ret = vfprintf(stderr, fmt, args);
72 va_end(args);
73 }
74
75 return ret;
76}
77
Frederic Weisbeckercfff2d92011-02-25 21:30:16 +010078/* Define PyVarObject_HEAD_INIT for python 2.5 */
79#ifndef PyVarObject_HEAD_INIT
80# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
81#endif
82
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010083#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -020084PyMODINIT_FUNC initperf(void);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010085#else
86PyMODINIT_FUNC PyInit_perf(void);
87#endif
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -020088
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020089#define member_def(type, member, ptype, help) \
90 { #member, ptype, \
91 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
92 0, help }
93
94#define sample_member_def(name, member, ptype, help) \
95 { #name, ptype, \
96 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
97 0, help }
98
99struct pyrf_event {
100 PyObject_HEAD
Jiri Olsa32dcd022019-07-21 13:23:51 +0200101 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200102 struct perf_sample sample;
103 union perf_event event;
104};
105
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200106#define sample_members \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200107 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200108 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
109 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200110 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
111 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
112 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
113 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
114 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200115 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
116
117static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
118
119static PyMemberDef pyrf_mmap_event__members[] = {
120 sample_members
121 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300122 member_def(perf_event_header, misc, T_UINT, "event misc"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300123 member_def(perf_record_mmap, pid, T_UINT, "event pid"),
124 member_def(perf_record_mmap, tid, T_UINT, "event tid"),
125 member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
126 member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
127 member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
128 member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200129 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200130};
131
132static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
133{
134 PyObject *ret;
135 char *s;
136
Jiri Olsa1345e2e2019-08-25 20:17:41 +0200137 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
138 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200139 "filename: %s }",
140 pevent->event.mmap.pid, pevent->event.mmap.tid,
141 pevent->event.mmap.start, pevent->event.mmap.len,
142 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
143 ret = PyErr_NoMemory();
144 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100145 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200146 free(s);
147 }
148 return ret;
149}
150
151static PyTypeObject pyrf_mmap_event__type = {
152 PyVarObject_HEAD_INIT(NULL, 0)
153 .tp_name = "perf.mmap_event",
154 .tp_basicsize = sizeof(struct pyrf_event),
155 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
156 .tp_doc = pyrf_mmap_event__doc,
157 .tp_members = pyrf_mmap_event__members,
158 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
159};
160
161static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
162
163static PyMemberDef pyrf_task_event__members[] = {
164 sample_members
165 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300166 member_def(perf_record_fork, pid, T_UINT, "event pid"),
167 member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
168 member_def(perf_record_fork, tid, T_UINT, "event tid"),
169 member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
170 member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200171 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200172};
173
174static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
175{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100176 return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
Jiri Olsabceb59b2019-08-25 20:17:45 +0200177 "ptid: %u, time: %" PRI_lu64 "}",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200178 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
179 pevent->event.fork.pid,
180 pevent->event.fork.ppid,
181 pevent->event.fork.tid,
182 pevent->event.fork.ptid,
183 pevent->event.fork.time);
184}
185
186static PyTypeObject pyrf_task_event__type = {
187 PyVarObject_HEAD_INIT(NULL, 0)
188 .tp_name = "perf.task_event",
189 .tp_basicsize = sizeof(struct pyrf_event),
190 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
191 .tp_doc = pyrf_task_event__doc,
192 .tp_members = pyrf_task_event__members,
193 .tp_repr = (reprfunc)pyrf_task_event__repr,
194};
195
196static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
197
198static PyMemberDef pyrf_comm_event__members[] = {
199 sample_members
200 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300201 member_def(perf_record_comm, pid, T_UINT, "event pid"),
202 member_def(perf_record_comm, tid, T_UINT, "event tid"),
203 member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200204 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200205};
206
207static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
208{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100209 return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200210 pevent->event.comm.pid,
211 pevent->event.comm.tid,
212 pevent->event.comm.comm);
213}
214
215static PyTypeObject pyrf_comm_event__type = {
216 PyVarObject_HEAD_INIT(NULL, 0)
217 .tp_name = "perf.comm_event",
218 .tp_basicsize = sizeof(struct pyrf_event),
219 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
220 .tp_doc = pyrf_comm_event__doc,
221 .tp_members = pyrf_comm_event__members,
222 .tp_repr = (reprfunc)pyrf_comm_event__repr,
223};
224
225static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
226
227static PyMemberDef pyrf_throttle_event__members[] = {
228 sample_members
229 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300230 member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
231 member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
232 member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200233 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200234};
235
236static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
237{
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300238 struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200239
Jiri Olsa003c66f2019-08-25 20:17:49 +0200240 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
241 ", stream_id: %" PRI_lu64 " }",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200242 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
243 te->time, te->id, te->stream_id);
244}
245
246static PyTypeObject pyrf_throttle_event__type = {
247 PyVarObject_HEAD_INIT(NULL, 0)
248 .tp_name = "perf.throttle_event",
249 .tp_basicsize = sizeof(struct pyrf_event),
250 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
251 .tp_doc = pyrf_throttle_event__doc,
252 .tp_members = pyrf_throttle_event__members,
253 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
254};
255
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300256static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
257
258static PyMemberDef pyrf_lost_event__members[] = {
259 sample_members
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300260 member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
261 member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300262 { .name = NULL, },
263};
264
265static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
266{
267 PyObject *ret;
268 char *s;
269
Jiri Olsa5290ed62019-08-25 20:17:46 +0200270 if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
271 "lost: %#" PRI_lx64 " }",
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300272 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
273 ret = PyErr_NoMemory();
274 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100275 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300276 free(s);
277 }
278 return ret;
279}
280
281static PyTypeObject pyrf_lost_event__type = {
282 PyVarObject_HEAD_INIT(NULL, 0)
283 .tp_name = "perf.lost_event",
284 .tp_basicsize = sizeof(struct pyrf_event),
285 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
286 .tp_doc = pyrf_lost_event__doc,
287 .tp_members = pyrf_lost_event__members,
288 .tp_repr = (reprfunc)pyrf_lost_event__repr,
289};
290
291static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
292
293static PyMemberDef pyrf_read_event__members[] = {
294 sample_members
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300295 member_def(perf_record_read, pid, T_UINT, "event pid"),
296 member_def(perf_record_read, tid, T_UINT, "event tid"),
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300297 { .name = NULL, },
298};
299
300static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
301{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100302 return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300303 pevent->event.read.pid,
304 pevent->event.read.tid);
305 /*
306 * FIXME: return the array of read values,
307 * making this method useful ;-)
308 */
309}
310
311static PyTypeObject pyrf_read_event__type = {
312 PyVarObject_HEAD_INIT(NULL, 0)
313 .tp_name = "perf.read_event",
314 .tp_basicsize = sizeof(struct pyrf_event),
315 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
316 .tp_doc = pyrf_read_event__doc,
317 .tp_members = pyrf_read_event__members,
318 .tp_repr = (reprfunc)pyrf_read_event__repr,
319};
320
321static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
322
323static PyMemberDef pyrf_sample_event__members[] = {
324 sample_members
325 member_def(perf_event_header, type, T_UINT, "event type"),
326 { .name = NULL, },
327};
328
329static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
330{
331 PyObject *ret;
332 char *s;
333
334 if (asprintf(&s, "{ type: sample }") < 0) {
335 ret = PyErr_NoMemory();
336 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100337 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300338 free(s);
339 }
340 return ret;
341}
342
Jiri Olsabae57e32016-07-10 13:08:00 +0200343static bool is_tracepoint(struct pyrf_event *pevent)
344{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200345 return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
Jiri Olsabae57e32016-07-10 13:08:00 +0200346}
347
Jiri Olsabae57e32016-07-10 13:08:00 +0200348static PyObject*
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400349tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
Jiri Olsabae57e32016-07-10 13:08:00 +0200350{
Tzvetomir Stoyanov69769ce2019-04-01 12:43:18 -0400351 struct tep_handle *pevent = field->event->tep;
Jiri Olsabae57e32016-07-10 13:08:00 +0200352 void *data = pe->sample.raw_data;
353 PyObject *ret = NULL;
354 unsigned long long val;
355 unsigned int offset, len;
356
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400357 if (field->flags & TEP_FIELD_IS_ARRAY) {
Jiri Olsabae57e32016-07-10 13:08:00 +0200358 offset = field->offset;
359 len = field->size;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400360 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400361 val = tep_read_number(pevent, data + offset, len);
Jiri Olsabae57e32016-07-10 13:08:00 +0200362 offset = val;
363 len = offset >> 16;
364 offset &= 0xffff;
365 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400366 if (field->flags & TEP_FIELD_IS_STRING &&
Jiri Olsabae57e32016-07-10 13:08:00 +0200367 is_printable_array(data + offset, len)) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100368 ret = _PyUnicode_FromString((char *)data + offset);
Jiri Olsabae57e32016-07-10 13:08:00 +0200369 } else {
370 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400371 field->flags &= ~TEP_FIELD_IS_STRING;
Jiri Olsabae57e32016-07-10 13:08:00 +0200372 }
373 } else {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400374 val = tep_read_number(pevent, data + field->offset,
375 field->size);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400376 if (field->flags & TEP_FIELD_IS_POINTER)
Jiri Olsabae57e32016-07-10 13:08:00 +0200377 ret = PyLong_FromUnsignedLong((unsigned long) val);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400378 else if (field->flags & TEP_FIELD_IS_SIGNED)
Jiri Olsabae57e32016-07-10 13:08:00 +0200379 ret = PyLong_FromLong((long) val);
380 else
381 ret = PyLong_FromUnsignedLong((unsigned long) val);
382 }
383
384 return ret;
385}
386
387static PyObject*
388get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
389{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100390 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
Jiri Olsa32dcd022019-07-21 13:23:51 +0200391 struct evsel *evsel = pevent->evsel;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400392 struct tep_format_field *field;
Jiri Olsabae57e32016-07-10 13:08:00 +0200393
394 if (!evsel->tp_format) {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500395 struct tep_event *tp_format;
Jiri Olsabae57e32016-07-10 13:08:00 +0200396
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200397 tp_format = trace_event__tp_format_id(evsel->core.attr.config);
Jiri Olsabae57e32016-07-10 13:08:00 +0200398 if (!tp_format)
399 return NULL;
400
401 evsel->tp_format = tp_format;
402 }
403
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -0400404 field = tep_find_any_field(evsel->tp_format, str);
Jiri Olsabae57e32016-07-10 13:08:00 +0200405 if (!field)
406 return NULL;
407
408 return tracepoint_field(pevent, field);
409}
410
411static PyObject*
412pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
413{
414 PyObject *obj = NULL;
415
416 if (is_tracepoint(pevent))
417 obj = get_tracepoint_field(pevent, attr_name);
418
419 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
420}
421
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300422static PyTypeObject pyrf_sample_event__type = {
423 PyVarObject_HEAD_INIT(NULL, 0)
424 .tp_name = "perf.sample_event",
425 .tp_basicsize = sizeof(struct pyrf_event),
426 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
427 .tp_doc = pyrf_sample_event__doc,
428 .tp_members = pyrf_sample_event__members,
429 .tp_repr = (reprfunc)pyrf_sample_event__repr,
Jiri Olsabae57e32016-07-10 13:08:00 +0200430 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300431};
432
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300433static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
434
435static PyMemberDef pyrf_context_switch_event__members[] = {
436 sample_members
437 member_def(perf_event_header, type, T_UINT, "event type"),
Jiri Olsa72932372019-08-28 15:57:16 +0200438 member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
439 member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300440 { .name = NULL, },
441};
442
443static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
444{
445 PyObject *ret;
446 char *s;
447
448 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
449 pevent->event.context_switch.next_prev_pid,
450 pevent->event.context_switch.next_prev_tid,
451 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
452 ret = PyErr_NoMemory();
453 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100454 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300455 free(s);
456 }
457 return ret;
458}
459
460static PyTypeObject pyrf_context_switch_event__type = {
461 PyVarObject_HEAD_INIT(NULL, 0)
462 .tp_name = "perf.context_switch_event",
463 .tp_basicsize = sizeof(struct pyrf_event),
464 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
465 .tp_doc = pyrf_context_switch_event__doc,
466 .tp_members = pyrf_context_switch_event__members,
467 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
468};
469
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200470static int pyrf_event__setup_types(void)
471{
472 int err;
473 pyrf_mmap_event__type.tp_new =
474 pyrf_task_event__type.tp_new =
475 pyrf_comm_event__type.tp_new =
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300476 pyrf_lost_event__type.tp_new =
477 pyrf_read_event__type.tp_new =
478 pyrf_sample_event__type.tp_new =
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300479 pyrf_context_switch_event__type.tp_new =
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200480 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
481 err = PyType_Ready(&pyrf_mmap_event__type);
482 if (err < 0)
483 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300484 err = PyType_Ready(&pyrf_lost_event__type);
485 if (err < 0)
486 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200487 err = PyType_Ready(&pyrf_task_event__type);
488 if (err < 0)
489 goto out;
490 err = PyType_Ready(&pyrf_comm_event__type);
491 if (err < 0)
492 goto out;
493 err = PyType_Ready(&pyrf_throttle_event__type);
494 if (err < 0)
495 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300496 err = PyType_Ready(&pyrf_read_event__type);
497 if (err < 0)
498 goto out;
499 err = PyType_Ready(&pyrf_sample_event__type);
500 if (err < 0)
501 goto out;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300502 err = PyType_Ready(&pyrf_context_switch_event__type);
503 if (err < 0)
504 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200505out:
506 return err;
507}
508
509static PyTypeObject *pyrf_event__type[] = {
510 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300511 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200512 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
513 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
514 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
515 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
516 [PERF_RECORD_FORK] = &pyrf_task_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300517 [PERF_RECORD_READ] = &pyrf_read_event__type,
518 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300519 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
520 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200521};
522
523static PyObject *pyrf_event__new(union perf_event *event)
524{
525 struct pyrf_event *pevent;
526 PyTypeObject *ptype;
527
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300528 if ((event->header.type < PERF_RECORD_MMAP ||
529 event->header.type > PERF_RECORD_SAMPLE) &&
530 !(event->header.type == PERF_RECORD_SWITCH ||
531 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200532 return NULL;
533
534 ptype = pyrf_event__type[event->header.type];
535 pevent = PyObject_New(struct pyrf_event, ptype);
536 if (pevent != NULL)
537 memcpy(&pevent->event, event, event->header.size);
538 return (PyObject *)pevent;
539}
540
541struct pyrf_cpu_map {
542 PyObject_HEAD
543
Jiri Olsaf8548392019-07-21 13:23:49 +0200544 struct perf_cpu_map *cpus;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200545};
546
547static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
548 PyObject *args, PyObject *kwargs)
549{
Frederic Weisbecker64348152011-03-31 18:27:43 +0200550 static char *kwlist[] = { "cpustr", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200551 char *cpustr = NULL;
552
553 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
554 kwlist, &cpustr))
555 return -1;
556
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200557 pcpus->cpus = perf_cpu_map__new(cpustr);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200558 if (pcpus->cpus == NULL)
559 return -1;
560 return 0;
561}
562
563static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
564{
Jiri Olsa38f01d82019-07-21 13:24:17 +0200565 perf_cpu_map__put(pcpus->cpus);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100566 Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200567}
568
569static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
570{
571 struct pyrf_cpu_map *pcpus = (void *)obj;
572
573 return pcpus->cpus->nr;
574}
575
576static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
577{
578 struct pyrf_cpu_map *pcpus = (void *)obj;
579
580 if (i >= pcpus->cpus->nr)
581 return NULL;
582
583 return Py_BuildValue("i", pcpus->cpus->map[i]);
584}
585
586static PySequenceMethods pyrf_cpu_map__sequence_methods = {
587 .sq_length = pyrf_cpu_map__length,
588 .sq_item = pyrf_cpu_map__item,
589};
590
591static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
592
593static PyTypeObject pyrf_cpu_map__type = {
594 PyVarObject_HEAD_INIT(NULL, 0)
595 .tp_name = "perf.cpu_map",
596 .tp_basicsize = sizeof(struct pyrf_cpu_map),
597 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
598 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
599 .tp_doc = pyrf_cpu_map__doc,
600 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
601 .tp_init = (initproc)pyrf_cpu_map__init,
602};
603
604static int pyrf_cpu_map__setup_types(void)
605{
606 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
607 return PyType_Ready(&pyrf_cpu_map__type);
608}
609
610struct pyrf_thread_map {
611 PyObject_HEAD
612
Jiri Olsa9749b902019-07-21 13:23:50 +0200613 struct perf_thread_map *threads;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200614};
615
616static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
617 PyObject *args, PyObject *kwargs)
618{
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200619 static char *kwlist[] = { "pid", "tid", "uid", NULL };
620 int pid = -1, tid = -1, uid = UINT_MAX;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200621
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200622 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
623 kwlist, &pid, &tid, &uid))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200624 return -1;
625
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200626 pthreads->threads = thread_map__new(pid, tid, uid);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200627 if (pthreads->threads == NULL)
628 return -1;
629 return 0;
630}
631
632static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
633{
Jiri Olsa7836e522019-07-21 13:24:20 +0200634 perf_thread_map__put(pthreads->threads);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100635 Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200636}
637
638static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
639{
640 struct pyrf_thread_map *pthreads = (void *)obj;
641
642 return pthreads->threads->nr;
643}
644
645static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
646{
647 struct pyrf_thread_map *pthreads = (void *)obj;
648
649 if (i >= pthreads->threads->nr)
650 return NULL;
651
652 return Py_BuildValue("i", pthreads->threads->map[i]);
653}
654
655static PySequenceMethods pyrf_thread_map__sequence_methods = {
656 .sq_length = pyrf_thread_map__length,
657 .sq_item = pyrf_thread_map__item,
658};
659
660static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
661
662static PyTypeObject pyrf_thread_map__type = {
663 PyVarObject_HEAD_INIT(NULL, 0)
664 .tp_name = "perf.thread_map",
665 .tp_basicsize = sizeof(struct pyrf_thread_map),
666 .tp_dealloc = (destructor)pyrf_thread_map__delete,
667 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
668 .tp_doc = pyrf_thread_map__doc,
669 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
670 .tp_init = (initproc)pyrf_thread_map__init,
671};
672
673static int pyrf_thread_map__setup_types(void)
674{
675 pyrf_thread_map__type.tp_new = PyType_GenericNew;
676 return PyType_Ready(&pyrf_thread_map__type);
677}
678
679struct pyrf_evsel {
680 PyObject_HEAD
681
Jiri Olsa32dcd022019-07-21 13:23:51 +0200682 struct evsel evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200683};
684
685static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
686 PyObject *args, PyObject *kwargs)
687{
688 struct perf_event_attr attr = {
689 .type = PERF_TYPE_HARDWARE,
690 .config = PERF_COUNT_HW_CPU_CYCLES,
691 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
692 };
693 static char *kwlist[] = {
694 "type",
695 "config",
696 "sample_freq",
697 "sample_period",
698 "sample_type",
699 "read_format",
700 "disabled",
701 "inherit",
702 "pinned",
703 "exclusive",
704 "exclude_user",
705 "exclude_kernel",
706 "exclude_hv",
707 "exclude_idle",
708 "mmap",
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300709 "context_switch",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200710 "comm",
711 "freq",
712 "inherit_stat",
713 "enable_on_exec",
714 "task",
715 "watermark",
716 "precise_ip",
717 "mmap_data",
718 "sample_id_all",
719 "wakeup_events",
720 "bp_type",
721 "bp_addr",
Frederic Weisbecker64348152011-03-31 18:27:43 +0200722 "bp_len",
723 NULL
724 };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200725 u64 sample_period = 0;
726 u32 disabled = 0,
727 inherit = 0,
728 pinned = 0,
729 exclusive = 0,
730 exclude_user = 0,
731 exclude_kernel = 0,
732 exclude_hv = 0,
733 exclude_idle = 0,
734 mmap = 0,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300735 context_switch = 0,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200736 comm = 0,
737 freq = 1,
738 inherit_stat = 0,
739 enable_on_exec = 0,
740 task = 0,
741 watermark = 0,
742 precise_ip = 0,
743 mmap_data = 0,
744 sample_id_all = 1;
745 int idx = 0;
746
747 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300748 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200749 &attr.type, &attr.config, &attr.sample_freq,
750 &sample_period, &attr.sample_type,
751 &attr.read_format, &disabled, &inherit,
752 &pinned, &exclusive, &exclude_user,
753 &exclude_kernel, &exclude_hv, &exclude_idle,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300754 &mmap, &context_switch, &comm, &freq, &inherit_stat,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200755 &enable_on_exec, &task, &watermark,
756 &precise_ip, &mmap_data, &sample_id_all,
757 &attr.wakeup_events, &attr.bp_type,
758 &attr.bp_addr, &attr.bp_len, &idx))
759 return -1;
760
761 /* union... */
762 if (sample_period != 0) {
763 if (attr.sample_freq != 0)
764 return -1; /* FIXME: throw right exception */
765 attr.sample_period = sample_period;
766 }
767
768 /* Bitfields */
769 attr.disabled = disabled;
770 attr.inherit = inherit;
771 attr.pinned = pinned;
772 attr.exclusive = exclusive;
773 attr.exclude_user = exclude_user;
774 attr.exclude_kernel = exclude_kernel;
775 attr.exclude_hv = exclude_hv;
776 attr.exclude_idle = exclude_idle;
777 attr.mmap = mmap;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300778 attr.context_switch = context_switch;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200779 attr.comm = comm;
780 attr.freq = freq;
781 attr.inherit_stat = inherit_stat;
782 attr.enable_on_exec = enable_on_exec;
783 attr.task = task;
784 attr.watermark = watermark;
785 attr.precise_ip = precise_ip;
786 attr.mmap_data = mmap_data;
787 attr.sample_id_all = sample_id_all;
Jiri Olsaad4e3c042016-07-10 13:07:55 +0200788 attr.size = sizeof(attr);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200789
Jiri Olsab4b62ee2019-07-21 13:23:53 +0200790 evsel__init(&pevsel->evsel, &attr, idx);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200791 return 0;
792}
793
794static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
795{
796 perf_evsel__exit(&pevsel->evsel);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100797 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200798}
799
800static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
801 PyObject *args, PyObject *kwargs)
802{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200803 struct evsel *evsel = &pevsel->evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +0200804 struct perf_cpu_map *cpus = NULL;
Jiri Olsa9749b902019-07-21 13:23:50 +0200805 struct perf_thread_map *threads = NULL;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200806 PyObject *pcpus = NULL, *pthreads = NULL;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300807 int group = 0, inherit = 0;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200808 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200809
810 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300811 &pcpus, &pthreads, &group, &inherit))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200812 return NULL;
813
814 if (pthreads != NULL)
815 threads = ((struct pyrf_thread_map *)pthreads)->threads;
816
817 if (pcpus != NULL)
818 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
819
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200820 evsel->core.attr.inherit = inherit;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200821 /*
822 * This will group just the fds for this single evsel, to group
823 * multiple events, use evlist.open().
824 */
Jiri Olsa5972d1e2019-07-21 13:24:01 +0200825 if (evsel__open(evsel, cpus, threads) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200826 PyErr_SetFromErrno(PyExc_OSError);
827 return NULL;
828 }
829
830 Py_INCREF(Py_None);
831 return Py_None;
832}
833
834static PyMethodDef pyrf_evsel__methods[] = {
835 {
836 .ml_name = "open",
837 .ml_meth = (PyCFunction)pyrf_evsel__open,
838 .ml_flags = METH_VARARGS | METH_KEYWORDS,
839 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
840 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200841 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200842};
843
844static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
845
846static PyTypeObject pyrf_evsel__type = {
847 PyVarObject_HEAD_INIT(NULL, 0)
848 .tp_name = "perf.evsel",
849 .tp_basicsize = sizeof(struct pyrf_evsel),
850 .tp_dealloc = (destructor)pyrf_evsel__delete,
851 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
852 .tp_doc = pyrf_evsel__doc,
853 .tp_methods = pyrf_evsel__methods,
854 .tp_init = (initproc)pyrf_evsel__init,
855};
856
857static int pyrf_evsel__setup_types(void)
858{
859 pyrf_evsel__type.tp_new = PyType_GenericNew;
860 return PyType_Ready(&pyrf_evsel__type);
861}
862
863struct pyrf_evlist {
864 PyObject_HEAD
865
Jiri Olsa63503db2019-07-21 13:23:52 +0200866 struct evlist evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200867};
868
869static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300870 PyObject *args, PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200871{
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200872 PyObject *pcpus = NULL, *pthreads = NULL;
Jiri Olsaf8548392019-07-21 13:23:49 +0200873 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +0200874 struct perf_thread_map *threads;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200875
876 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
877 return -1;
878
879 threads = ((struct pyrf_thread_map *)pthreads)->threads;
880 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
Jiri Olsa52c86bc2019-07-21 13:23:54 +0200881 evlist__init(&pevlist->evlist, cpus, threads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200882 return 0;
883}
884
885static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
886{
Jiri Olsa470579b2019-09-02 14:34:52 +0200887 evlist__exit(&pevlist->evlist);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100888 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200889}
890
891static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
892 PyObject *args, PyObject *kwargs)
893{
Jiri Olsa63503db2019-07-21 13:23:52 +0200894 struct evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200895 static char *kwlist[] = { "pages", "overwrite", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200896 int pages = 128, overwrite = false;
897
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200898 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
899 &pages, &overwrite))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200900 return NULL;
901
Jiri Olsa9521b5f2019-07-28 12:45:35 +0200902 if (evlist__mmap(evlist, pages) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200903 PyErr_SetFromErrno(PyExc_OSError);
904 return NULL;
905 }
906
907 Py_INCREF(Py_None);
908 return Py_None;
909}
910
911static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
912 PyObject *args, PyObject *kwargs)
913{
Jiri Olsa63503db2019-07-21 13:23:52 +0200914 struct evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200915 static char *kwlist[] = { "timeout", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200916 int timeout = -1, n;
917
918 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
919 return NULL;
920
Jiri Olsa80ab2982019-08-31 22:48:33 +0200921 n = evlist__poll(evlist, timeout);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200922 if (n < 0) {
923 PyErr_SetFromErrno(PyExc_OSError);
924 return NULL;
925 }
926
927 return Py_BuildValue("i", n);
928}
929
930static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300931 PyObject *args __maybe_unused,
932 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200933{
Jiri Olsa63503db2019-07-21 13:23:52 +0200934 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200935 PyObject *list = PyList_New(0);
936 int i;
937
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200938 for (i = 0; i < evlist->core.pollfd.nr; ++i) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200939 PyObject *file;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100940#if PY_MAJOR_VERSION < 3
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200941 FILE *fp = fdopen(evlist->core.pollfd.entries[i].fd, "r");
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200942
943 if (fp == NULL)
944 goto free_list;
945
946 file = PyFile_FromFile(fp, "perf", "r", NULL);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100947#else
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200948 file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1,
Jiri Olsaa389aec2018-12-26 12:21:21 +0100949 NULL, NULL, NULL, 0);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100950#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200951 if (file == NULL)
952 goto free_list;
953
954 if (PyList_Append(list, file) != 0) {
955 Py_DECREF(file);
956 goto free_list;
957 }
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300958
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200959 Py_DECREF(file);
960 }
961
962 return list;
963free_list:
964 return PyErr_NoMemory();
965}
966
967
968static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300969 PyObject *args,
970 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200971{
Jiri Olsa63503db2019-07-21 13:23:52 +0200972 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200973 PyObject *pevsel;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200974 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200975
976 if (!PyArg_ParseTuple(args, "O", &pevsel))
977 return NULL;
978
979 Py_INCREF(pevsel);
980 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200981 evsel->idx = evlist->core.nr_entries;
Jiri Olsaa1cf3a72019-07-21 13:23:59 +0200982 evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200983
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200984 return Py_BuildValue("i", evlist->core.nr_entries);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200985}
986
Jiri Olsaa5830532019-07-27 20:30:53 +0200987static struct mmap *get_md(struct evlist *evlist, int cpu)
Jiri Olsa721f0df2018-08-17 13:45:56 +0200988{
989 int i;
990
Jiri Olsac976ee12019-07-30 13:04:59 +0200991 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Jiri Olsaa5830532019-07-27 20:30:53 +0200992 struct mmap *md = &evlist->mmap[i];
Jiri Olsa721f0df2018-08-17 13:45:56 +0200993
Jiri Olsa56a94702019-07-27 22:33:20 +0200994 if (md->core.cpu == cpu)
Jiri Olsa721f0df2018-08-17 13:45:56 +0200995 return md;
996 }
997
998 return NULL;
999}
1000
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001001static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
1002 PyObject *args, PyObject *kwargs)
1003{
Jiri Olsa63503db2019-07-21 13:23:52 +02001004 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001005 union perf_event *event;
1006 int sample_id_all = 1, cpu;
Frederic Weisbecker64348152011-03-31 18:27:43 +02001007 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
Jiri Olsaa5830532019-07-27 20:30:53 +02001008 struct mmap *md;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001009 int err;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001010
1011 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1012 &cpu, &sample_id_all))
1013 return NULL;
1014
Jiri Olsa721f0df2018-08-17 13:45:56 +02001015 md = get_md(evlist, cpu);
1016 if (!md)
1017 return NULL;
1018
Kan Liangb9bae2c2018-03-06 10:36:07 -05001019 if (perf_mmap__read_init(md) < 0)
Kan Liang35b7cdc2018-03-01 18:09:00 -05001020 goto end;
1021
Kan Liang0019dc872018-03-06 10:36:06 -05001022 event = perf_mmap__read_event(md);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001023 if (event != NULL) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001024 PyObject *pyevent = pyrf_event__new(event);
1025 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001026 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001027
1028 if (pyevent == NULL)
1029 return PyErr_NoMemory();
1030
Jiri Olsa377f6982016-07-10 13:07:59 +02001031 evsel = perf_evlist__event2evsel(evlist, event);
Petr Machata83428f22018-03-22 00:57:32 +01001032 if (!evsel) {
1033 Py_INCREF(Py_None);
Jiri Olsa377f6982016-07-10 13:07:59 +02001034 return Py_None;
Petr Machata83428f22018-03-22 00:57:32 +01001035 }
Jiri Olsa377f6982016-07-10 13:07:59 +02001036
1037 pevent->evsel = evsel;
1038
1039 err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
Jiri Olsae8968e62016-07-10 13:07:56 +02001040
1041 /* Consume the even only after we parsed it out. */
Kan Liangd6ace3d2018-03-06 10:36:05 -05001042 perf_mmap__consume(md);
Jiri Olsae8968e62016-07-10 13:07:56 +02001043
Arnaldo Carvalho de Melo5c6970a2011-06-02 10:55:10 -03001044 if (err)
1045 return PyErr_Format(PyExc_OSError,
1046 "perf: can't parse sample, err=%d", err);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001047 return pyevent;
1048 }
Kan Liang35b7cdc2018-03-01 18:09:00 -05001049end:
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001050 Py_INCREF(Py_None);
1051 return Py_None;
1052}
1053
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001054static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1055 PyObject *args, PyObject *kwargs)
1056{
Jiri Olsa63503db2019-07-21 13:23:52 +02001057 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001058 int group = 0;
1059 static char *kwlist[] = { "group", NULL };
1060
1061 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1062 return NULL;
1063
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001064 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001065 perf_evlist__set_leader(evlist);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001066
Jiri Olsa474ddc42019-07-21 13:24:06 +02001067 if (evlist__open(evlist) < 0) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001068 PyErr_SetFromErrno(PyExc_OSError);
1069 return NULL;
1070 }
1071
1072 Py_INCREF(Py_None);
1073 return Py_None;
1074}
1075
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001076static PyMethodDef pyrf_evlist__methods[] = {
1077 {
1078 .ml_name = "mmap",
1079 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1080 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1081 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1082 },
1083 {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001084 .ml_name = "open",
1085 .ml_meth = (PyCFunction)pyrf_evlist__open,
1086 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1087 .ml_doc = PyDoc_STR("open the file descriptors.")
1088 },
1089 {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001090 .ml_name = "poll",
1091 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1092 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1093 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1094 },
1095 {
1096 .ml_name = "get_pollfd",
1097 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1098 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1099 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1100 },
1101 {
1102 .ml_name = "add",
1103 .ml_meth = (PyCFunction)pyrf_evlist__add,
1104 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1105 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1106 },
1107 {
1108 .ml_name = "read_on_cpu",
1109 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1110 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1111 .ml_doc = PyDoc_STR("reads an event.")
1112 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001113 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001114};
1115
1116static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1117{
1118 struct pyrf_evlist *pevlist = (void *)obj;
1119
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001120 return pevlist->evlist.core.nr_entries;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001121}
1122
1123static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1124{
1125 struct pyrf_evlist *pevlist = (void *)obj;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001126 struct evsel *pos;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001127
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001128 if (i >= pevlist->evlist.core.nr_entries)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001129 return NULL;
1130
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001131 evlist__for_each_entry(&pevlist->evlist, pos) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001132 if (i-- == 0)
1133 break;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001134 }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001135
1136 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1137}
1138
1139static PySequenceMethods pyrf_evlist__sequence_methods = {
1140 .sq_length = pyrf_evlist__length,
1141 .sq_item = pyrf_evlist__item,
1142};
1143
1144static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1145
1146static PyTypeObject pyrf_evlist__type = {
1147 PyVarObject_HEAD_INIT(NULL, 0)
1148 .tp_name = "perf.evlist",
1149 .tp_basicsize = sizeof(struct pyrf_evlist),
1150 .tp_dealloc = (destructor)pyrf_evlist__delete,
1151 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1152 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1153 .tp_doc = pyrf_evlist__doc,
1154 .tp_methods = pyrf_evlist__methods,
1155 .tp_init = (initproc)pyrf_evlist__init,
1156};
1157
1158static int pyrf_evlist__setup_types(void)
1159{
1160 pyrf_evlist__type.tp_new = PyType_GenericNew;
1161 return PyType_Ready(&pyrf_evlist__type);
1162}
1163
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001164#define PERF_CONST(name) { #name, PERF_##name }
1165
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001166static struct {
1167 const char *name;
1168 int value;
1169} perf__constants[] = {
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001170 PERF_CONST(TYPE_HARDWARE),
1171 PERF_CONST(TYPE_SOFTWARE),
1172 PERF_CONST(TYPE_TRACEPOINT),
1173 PERF_CONST(TYPE_HW_CACHE),
1174 PERF_CONST(TYPE_RAW),
1175 PERF_CONST(TYPE_BREAKPOINT),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001176
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001177 PERF_CONST(COUNT_HW_CPU_CYCLES),
1178 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1179 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1180 PERF_CONST(COUNT_HW_CACHE_MISSES),
1181 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1182 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1183 PERF_CONST(COUNT_HW_BUS_CYCLES),
1184 PERF_CONST(COUNT_HW_CACHE_L1D),
1185 PERF_CONST(COUNT_HW_CACHE_L1I),
1186 PERF_CONST(COUNT_HW_CACHE_LL),
1187 PERF_CONST(COUNT_HW_CACHE_DTLB),
1188 PERF_CONST(COUNT_HW_CACHE_ITLB),
1189 PERF_CONST(COUNT_HW_CACHE_BPU),
1190 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1191 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1192 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1193 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1194 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001195
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001196 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1197 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
Ingo Molnar129c04c2011-04-29 14:41:28 +02001198
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001199 PERF_CONST(COUNT_SW_CPU_CLOCK),
1200 PERF_CONST(COUNT_SW_TASK_CLOCK),
1201 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1202 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1203 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1204 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1205 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1206 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1207 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1208 PERF_CONST(COUNT_SW_DUMMY),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001209
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001210 PERF_CONST(SAMPLE_IP),
1211 PERF_CONST(SAMPLE_TID),
1212 PERF_CONST(SAMPLE_TIME),
1213 PERF_CONST(SAMPLE_ADDR),
1214 PERF_CONST(SAMPLE_READ),
1215 PERF_CONST(SAMPLE_CALLCHAIN),
1216 PERF_CONST(SAMPLE_ID),
1217 PERF_CONST(SAMPLE_CPU),
1218 PERF_CONST(SAMPLE_PERIOD),
1219 PERF_CONST(SAMPLE_STREAM_ID),
1220 PERF_CONST(SAMPLE_RAW),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001221
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001222 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1223 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1224 PERF_CONST(FORMAT_ID),
1225 PERF_CONST(FORMAT_GROUP),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001226
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001227 PERF_CONST(RECORD_MMAP),
1228 PERF_CONST(RECORD_LOST),
1229 PERF_CONST(RECORD_COMM),
1230 PERF_CONST(RECORD_EXIT),
1231 PERF_CONST(RECORD_THROTTLE),
1232 PERF_CONST(RECORD_UNTHROTTLE),
1233 PERF_CONST(RECORD_FORK),
1234 PERF_CONST(RECORD_READ),
1235 PERF_CONST(RECORD_SAMPLE),
Arnaldo Carvalho de Melo84576da2015-07-24 13:04:09 -03001236 PERF_CONST(RECORD_MMAP2),
1237 PERF_CONST(RECORD_AUX),
1238 PERF_CONST(RECORD_ITRACE_START),
1239 PERF_CONST(RECORD_LOST_SAMPLES),
1240 PERF_CONST(RECORD_SWITCH),
1241 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -03001242
1243 PERF_CONST(RECORD_MISC_SWITCH_OUT),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001244 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001245};
1246
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001247static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1248 PyObject *args, PyObject *kwargs)
1249{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001250 struct tep_event *tp_format;
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001251 static char *kwlist[] = { "sys", "name", NULL };
1252 char *sys = NULL;
1253 char *name = NULL;
1254
1255 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1256 &sys, &name))
1257 return NULL;
1258
1259 tp_format = trace_event__tp_format(sys, name);
1260 if (IS_ERR(tp_format))
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001261 return _PyLong_FromLong(-1);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001262
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001263 return _PyLong_FromLong(tp_format->id);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001264}
1265
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001266static PyMethodDef perf__methods[] = {
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001267 {
1268 .ml_name = "tracepoint",
1269 .ml_meth = (PyCFunction) pyrf__tracepoint,
1270 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1271 .ml_doc = PyDoc_STR("Get tracepoint config.")
1272 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001273 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001274};
1275
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001276#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001277PyMODINIT_FUNC initperf(void)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001278#else
1279PyMODINIT_FUNC PyInit_perf(void)
1280#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001281{
1282 PyObject *obj;
1283 int i;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001284 PyObject *dict;
1285#if PY_MAJOR_VERSION < 3
1286 PyObject *module = Py_InitModule("perf", perf__methods);
1287#else
1288 static struct PyModuleDef moduledef = {
1289 PyModuleDef_HEAD_INIT,
1290 "perf", /* m_name */
1291 "", /* m_doc */
1292 -1, /* m_size */
1293 perf__methods, /* m_methods */
1294 NULL, /* m_reload */
1295 NULL, /* m_traverse */
1296 NULL, /* m_clear */
1297 NULL, /* m_free */
1298 };
1299 PyObject *module = PyModule_Create(&moduledef);
1300#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001301
1302 if (module == NULL ||
1303 pyrf_event__setup_types() < 0 ||
1304 pyrf_evlist__setup_types() < 0 ||
1305 pyrf_evsel__setup_types() < 0 ||
1306 pyrf_thread_map__setup_types() < 0 ||
1307 pyrf_cpu_map__setup_types() < 0)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001308#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001309 return;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001310#else
1311 return module;
1312#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001313
Jiri Olsa918512b2013-09-12 18:39:35 +02001314 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0da2e9c2012-10-16 14:51:04 -03001315 page_size = sysconf(_SC_PAGE_SIZE);
1316
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001317 Py_INCREF(&pyrf_evlist__type);
1318 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1319
1320 Py_INCREF(&pyrf_evsel__type);
1321 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1322
Jiri Olsa85e37de2016-07-10 13:07:57 +02001323 Py_INCREF(&pyrf_mmap_event__type);
1324 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1325
1326 Py_INCREF(&pyrf_lost_event__type);
1327 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1328
1329 Py_INCREF(&pyrf_comm_event__type);
1330 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1331
1332 Py_INCREF(&pyrf_task_event__type);
1333 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1334
1335 Py_INCREF(&pyrf_throttle_event__type);
1336 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1337
1338 Py_INCREF(&pyrf_task_event__type);
1339 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1340
1341 Py_INCREF(&pyrf_read_event__type);
1342 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1343
1344 Py_INCREF(&pyrf_sample_event__type);
1345 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1346
1347 Py_INCREF(&pyrf_context_switch_event__type);
1348 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1349
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001350 Py_INCREF(&pyrf_thread_map__type);
1351 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1352
1353 Py_INCREF(&pyrf_cpu_map__type);
1354 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1355
1356 dict = PyModule_GetDict(module);
1357 if (dict == NULL)
1358 goto error;
1359
1360 for (i = 0; perf__constants[i].name != NULL; i++) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001361 obj = _PyLong_FromLong(perf__constants[i].value);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001362 if (obj == NULL)
1363 goto error;
1364 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1365 Py_DECREF(obj);
1366 }
1367
1368error:
1369 if (PyErr_Occurred())
1370 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001371#if PY_MAJOR_VERSION >= 3
1372 return module;
1373#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001374}
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -03001375
1376/*
1377 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1378 * binding.
1379 */
1380void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
1381 int fd, int group_fd, unsigned long flags)
1382{
1383}