blob: 82a4fa6c87bde715acfdff818c98b28bf37bbf25 [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>
Jiri Olsa7728fa02019-10-07 14:53:17 +02009#include <perf/mmap.h>
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020010#include "evlist.h"
Arnaldo Carvalho de Melo56e2e052017-04-19 18:38:33 -030011#include "callchain.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020012#include "evsel.h"
13#include "event.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030014#include "print_binary.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020015#include "thread_map.h"
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030016#include "trace-event.h"
Jiri Olsa721f0df2018-08-17 13:45:56 +020017#include "mmap.h"
Arnaldo Carvalho de Melo9db0e362019-09-30 11:48:32 -030018#include "util/env.h"
Jiri Olsa20f2be12019-08-06 15:25:25 +020019#include <internal/lib.h>
Arnaldo Carvalho de Melo91854f92019-08-29 14:59:50 -030020#include "../perf-sys.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020021
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010022#if PY_MAJOR_VERSION < 3
23#define _PyUnicode_FromString(arg) \
24 PyString_FromString(arg)
25#define _PyUnicode_AsString(arg) \
26 PyString_AsString(arg)
27#define _PyUnicode_FromFormat(...) \
28 PyString_FromFormat(__VA_ARGS__)
29#define _PyLong_FromLong(arg) \
30 PyInt_FromLong(arg)
31
32#else
33
34#define _PyUnicode_FromString(arg) \
35 PyUnicode_FromString(arg)
36#define _PyUnicode_FromFormat(...) \
37 PyUnicode_FromFormat(__VA_ARGS__)
38#define _PyLong_FromLong(arg) \
39 PyLong_FromLong(arg)
40#endif
41
42#ifndef Py_TYPE
43#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
44#endif
45
Adrian Hunter8afb4c02013-08-14 15:48:23 +030046/*
Arnaldo Carvalho de Melo56e2e052017-04-19 18:38:33 -030047 * Provide these two so that we don't have to link against callchain.c and
48 * start dragging hist.c, etc.
49 */
50struct callchain_param callchain_param;
51
52int parse_callchain_record(const char *arg __maybe_unused,
53 struct callchain_param *param __maybe_unused)
54{
55 return 0;
56}
57
58/*
Arnaldo Carvalho de Melo9db0e362019-09-30 11:48:32 -030059 * Add this one here not to drag util/env.c
60 */
61struct perf_env perf_env;
62
63/*
Adrian Hunter8afb4c02013-08-14 15:48:23 +030064 * Support debug printing even though util/debug.c is not linked. That means
65 * implementing 'verbose' and 'eprintf'.
66 */
67int verbose;
68
Arnaldo Carvalho de Melo5939cac2019-09-11 12:54:33 +010069int eprintf(int level, int var, const char *fmt, ...);
70
Jiri Olsac95688a2014-07-14 23:46:49 +020071int eprintf(int level, int var, const char *fmt, ...)
Adrian Hunter8afb4c02013-08-14 15:48:23 +030072{
73 va_list args;
74 int ret = 0;
75
Jiri Olsac95688a2014-07-14 23:46:49 +020076 if (var >= level) {
Adrian Hunter8afb4c02013-08-14 15:48:23 +030077 va_start(args, fmt);
78 ret = vfprintf(stderr, fmt, args);
79 va_end(args);
80 }
81
82 return ret;
83}
84
Frederic Weisbeckercfff2d92011-02-25 21:30:16 +010085/* Define PyVarObject_HEAD_INIT for python 2.5 */
86#ifndef PyVarObject_HEAD_INIT
87# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
88#endif
89
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010090#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -020091PyMODINIT_FUNC initperf(void);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010092#else
93PyMODINIT_FUNC PyInit_perf(void);
94#endif
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -020095
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020096#define member_def(type, member, ptype, help) \
97 { #member, ptype, \
98 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
99 0, help }
100
101#define sample_member_def(name, member, ptype, help) \
102 { #name, ptype, \
103 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
104 0, help }
105
106struct pyrf_event {
107 PyObject_HEAD
Jiri Olsa32dcd022019-07-21 13:23:51 +0200108 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200109 struct perf_sample sample;
110 union perf_event event;
111};
112
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200113#define sample_members \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200114 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200115 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
116 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200117 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
118 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
119 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
120 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
121 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200122 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
123
124static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
125
126static PyMemberDef pyrf_mmap_event__members[] = {
127 sample_members
128 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300129 member_def(perf_event_header, misc, T_UINT, "event misc"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300130 member_def(perf_record_mmap, pid, T_UINT, "event pid"),
131 member_def(perf_record_mmap, tid, T_UINT, "event tid"),
132 member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
133 member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
134 member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
135 member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200136 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200137};
138
139static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
140{
141 PyObject *ret;
142 char *s;
143
Jiri Olsa1345e2e2019-08-25 20:17:41 +0200144 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
145 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200146 "filename: %s }",
147 pevent->event.mmap.pid, pevent->event.mmap.tid,
148 pevent->event.mmap.start, pevent->event.mmap.len,
149 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
150 ret = PyErr_NoMemory();
151 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100152 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200153 free(s);
154 }
155 return ret;
156}
157
158static PyTypeObject pyrf_mmap_event__type = {
159 PyVarObject_HEAD_INIT(NULL, 0)
160 .tp_name = "perf.mmap_event",
161 .tp_basicsize = sizeof(struct pyrf_event),
162 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
163 .tp_doc = pyrf_mmap_event__doc,
164 .tp_members = pyrf_mmap_event__members,
165 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
166};
167
168static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
169
170static PyMemberDef pyrf_task_event__members[] = {
171 sample_members
172 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300173 member_def(perf_record_fork, pid, T_UINT, "event pid"),
174 member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
175 member_def(perf_record_fork, tid, T_UINT, "event tid"),
176 member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
177 member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200178 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200179};
180
181static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
182{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100183 return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
Jiri Olsabceb59b2019-08-25 20:17:45 +0200184 "ptid: %u, time: %" PRI_lu64 "}",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200185 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
186 pevent->event.fork.pid,
187 pevent->event.fork.ppid,
188 pevent->event.fork.tid,
189 pevent->event.fork.ptid,
190 pevent->event.fork.time);
191}
192
193static PyTypeObject pyrf_task_event__type = {
194 PyVarObject_HEAD_INIT(NULL, 0)
195 .tp_name = "perf.task_event",
196 .tp_basicsize = sizeof(struct pyrf_event),
197 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
198 .tp_doc = pyrf_task_event__doc,
199 .tp_members = pyrf_task_event__members,
200 .tp_repr = (reprfunc)pyrf_task_event__repr,
201};
202
203static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
204
205static PyMemberDef pyrf_comm_event__members[] = {
206 sample_members
207 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300208 member_def(perf_record_comm, pid, T_UINT, "event pid"),
209 member_def(perf_record_comm, tid, T_UINT, "event tid"),
210 member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200211 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200212};
213
214static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
215{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100216 return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200217 pevent->event.comm.pid,
218 pevent->event.comm.tid,
219 pevent->event.comm.comm);
220}
221
222static PyTypeObject pyrf_comm_event__type = {
223 PyVarObject_HEAD_INIT(NULL, 0)
224 .tp_name = "perf.comm_event",
225 .tp_basicsize = sizeof(struct pyrf_event),
226 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
227 .tp_doc = pyrf_comm_event__doc,
228 .tp_members = pyrf_comm_event__members,
229 .tp_repr = (reprfunc)pyrf_comm_event__repr,
230};
231
232static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
233
234static PyMemberDef pyrf_throttle_event__members[] = {
235 sample_members
236 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300237 member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
238 member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
239 member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200240 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200241};
242
243static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
244{
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300245 struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200246
Jiri Olsa003c66f2019-08-25 20:17:49 +0200247 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
248 ", stream_id: %" PRI_lu64 " }",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200249 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
250 te->time, te->id, te->stream_id);
251}
252
253static PyTypeObject pyrf_throttle_event__type = {
254 PyVarObject_HEAD_INIT(NULL, 0)
255 .tp_name = "perf.throttle_event",
256 .tp_basicsize = sizeof(struct pyrf_event),
257 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
258 .tp_doc = pyrf_throttle_event__doc,
259 .tp_members = pyrf_throttle_event__members,
260 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
261};
262
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300263static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
264
265static PyMemberDef pyrf_lost_event__members[] = {
266 sample_members
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300267 member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
268 member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300269 { .name = NULL, },
270};
271
272static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
273{
274 PyObject *ret;
275 char *s;
276
Jiri Olsa5290ed62019-08-25 20:17:46 +0200277 if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
278 "lost: %#" PRI_lx64 " }",
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300279 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
280 ret = PyErr_NoMemory();
281 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100282 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300283 free(s);
284 }
285 return ret;
286}
287
288static PyTypeObject pyrf_lost_event__type = {
289 PyVarObject_HEAD_INIT(NULL, 0)
290 .tp_name = "perf.lost_event",
291 .tp_basicsize = sizeof(struct pyrf_event),
292 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
293 .tp_doc = pyrf_lost_event__doc,
294 .tp_members = pyrf_lost_event__members,
295 .tp_repr = (reprfunc)pyrf_lost_event__repr,
296};
297
298static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
299
300static PyMemberDef pyrf_read_event__members[] = {
301 sample_members
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300302 member_def(perf_record_read, pid, T_UINT, "event pid"),
303 member_def(perf_record_read, tid, T_UINT, "event tid"),
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300304 { .name = NULL, },
305};
306
307static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
308{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100309 return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300310 pevent->event.read.pid,
311 pevent->event.read.tid);
312 /*
313 * FIXME: return the array of read values,
314 * making this method useful ;-)
315 */
316}
317
318static PyTypeObject pyrf_read_event__type = {
319 PyVarObject_HEAD_INIT(NULL, 0)
320 .tp_name = "perf.read_event",
321 .tp_basicsize = sizeof(struct pyrf_event),
322 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
323 .tp_doc = pyrf_read_event__doc,
324 .tp_members = pyrf_read_event__members,
325 .tp_repr = (reprfunc)pyrf_read_event__repr,
326};
327
328static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
329
330static PyMemberDef pyrf_sample_event__members[] = {
331 sample_members
332 member_def(perf_event_header, type, T_UINT, "event type"),
333 { .name = NULL, },
334};
335
336static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
337{
338 PyObject *ret;
339 char *s;
340
341 if (asprintf(&s, "{ type: sample }") < 0) {
342 ret = PyErr_NoMemory();
343 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100344 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300345 free(s);
346 }
347 return ret;
348}
349
Jiri Olsabae57e32016-07-10 13:08:00 +0200350static bool is_tracepoint(struct pyrf_event *pevent)
351{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200352 return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
Jiri Olsabae57e32016-07-10 13:08:00 +0200353}
354
Jiri Olsabae57e32016-07-10 13:08:00 +0200355static PyObject*
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400356tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
Jiri Olsabae57e32016-07-10 13:08:00 +0200357{
Tzvetomir Stoyanov69769ce2019-04-01 12:43:18 -0400358 struct tep_handle *pevent = field->event->tep;
Jiri Olsabae57e32016-07-10 13:08:00 +0200359 void *data = pe->sample.raw_data;
360 PyObject *ret = NULL;
361 unsigned long long val;
362 unsigned int offset, len;
363
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400364 if (field->flags & TEP_FIELD_IS_ARRAY) {
Jiri Olsabae57e32016-07-10 13:08:00 +0200365 offset = field->offset;
366 len = field->size;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400367 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400368 val = tep_read_number(pevent, data + offset, len);
Jiri Olsabae57e32016-07-10 13:08:00 +0200369 offset = val;
370 len = offset >> 16;
371 offset &= 0xffff;
372 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400373 if (field->flags & TEP_FIELD_IS_STRING &&
Jiri Olsabae57e32016-07-10 13:08:00 +0200374 is_printable_array(data + offset, len)) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100375 ret = _PyUnicode_FromString((char *)data + offset);
Jiri Olsabae57e32016-07-10 13:08:00 +0200376 } else {
377 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400378 field->flags &= ~TEP_FIELD_IS_STRING;
Jiri Olsabae57e32016-07-10 13:08:00 +0200379 }
380 } else {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400381 val = tep_read_number(pevent, data + field->offset,
382 field->size);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400383 if (field->flags & TEP_FIELD_IS_POINTER)
Jiri Olsabae57e32016-07-10 13:08:00 +0200384 ret = PyLong_FromUnsignedLong((unsigned long) val);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400385 else if (field->flags & TEP_FIELD_IS_SIGNED)
Jiri Olsabae57e32016-07-10 13:08:00 +0200386 ret = PyLong_FromLong((long) val);
387 else
388 ret = PyLong_FromUnsignedLong((unsigned long) val);
389 }
390
391 return ret;
392}
393
394static PyObject*
395get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
396{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100397 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
Jiri Olsa32dcd022019-07-21 13:23:51 +0200398 struct evsel *evsel = pevent->evsel;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400399 struct tep_format_field *field;
Jiri Olsabae57e32016-07-10 13:08:00 +0200400
401 if (!evsel->tp_format) {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500402 struct tep_event *tp_format;
Jiri Olsabae57e32016-07-10 13:08:00 +0200403
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200404 tp_format = trace_event__tp_format_id(evsel->core.attr.config);
Jiri Olsabae57e32016-07-10 13:08:00 +0200405 if (!tp_format)
406 return NULL;
407
408 evsel->tp_format = tp_format;
409 }
410
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -0400411 field = tep_find_any_field(evsel->tp_format, str);
Jiri Olsabae57e32016-07-10 13:08:00 +0200412 if (!field)
413 return NULL;
414
415 return tracepoint_field(pevent, field);
416}
417
418static PyObject*
419pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
420{
421 PyObject *obj = NULL;
422
423 if (is_tracepoint(pevent))
424 obj = get_tracepoint_field(pevent, attr_name);
425
426 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
427}
428
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300429static PyTypeObject pyrf_sample_event__type = {
430 PyVarObject_HEAD_INIT(NULL, 0)
431 .tp_name = "perf.sample_event",
432 .tp_basicsize = sizeof(struct pyrf_event),
433 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
434 .tp_doc = pyrf_sample_event__doc,
435 .tp_members = pyrf_sample_event__members,
436 .tp_repr = (reprfunc)pyrf_sample_event__repr,
Jiri Olsabae57e32016-07-10 13:08:00 +0200437 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300438};
439
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300440static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
441
442static PyMemberDef pyrf_context_switch_event__members[] = {
443 sample_members
444 member_def(perf_event_header, type, T_UINT, "event type"),
Jiri Olsa72932372019-08-28 15:57:16 +0200445 member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
446 member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300447 { .name = NULL, },
448};
449
450static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
451{
452 PyObject *ret;
453 char *s;
454
455 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
456 pevent->event.context_switch.next_prev_pid,
457 pevent->event.context_switch.next_prev_tid,
458 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
459 ret = PyErr_NoMemory();
460 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100461 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300462 free(s);
463 }
464 return ret;
465}
466
467static PyTypeObject pyrf_context_switch_event__type = {
468 PyVarObject_HEAD_INIT(NULL, 0)
469 .tp_name = "perf.context_switch_event",
470 .tp_basicsize = sizeof(struct pyrf_event),
471 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
472 .tp_doc = pyrf_context_switch_event__doc,
473 .tp_members = pyrf_context_switch_event__members,
474 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
475};
476
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200477static int pyrf_event__setup_types(void)
478{
479 int err;
480 pyrf_mmap_event__type.tp_new =
481 pyrf_task_event__type.tp_new =
482 pyrf_comm_event__type.tp_new =
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300483 pyrf_lost_event__type.tp_new =
484 pyrf_read_event__type.tp_new =
485 pyrf_sample_event__type.tp_new =
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300486 pyrf_context_switch_event__type.tp_new =
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200487 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
488 err = PyType_Ready(&pyrf_mmap_event__type);
489 if (err < 0)
490 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300491 err = PyType_Ready(&pyrf_lost_event__type);
492 if (err < 0)
493 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200494 err = PyType_Ready(&pyrf_task_event__type);
495 if (err < 0)
496 goto out;
497 err = PyType_Ready(&pyrf_comm_event__type);
498 if (err < 0)
499 goto out;
500 err = PyType_Ready(&pyrf_throttle_event__type);
501 if (err < 0)
502 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300503 err = PyType_Ready(&pyrf_read_event__type);
504 if (err < 0)
505 goto out;
506 err = PyType_Ready(&pyrf_sample_event__type);
507 if (err < 0)
508 goto out;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300509 err = PyType_Ready(&pyrf_context_switch_event__type);
510 if (err < 0)
511 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200512out:
513 return err;
514}
515
516static PyTypeObject *pyrf_event__type[] = {
517 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300518 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200519 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
520 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
521 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
522 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
523 [PERF_RECORD_FORK] = &pyrf_task_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300524 [PERF_RECORD_READ] = &pyrf_read_event__type,
525 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300526 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
527 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200528};
529
530static PyObject *pyrf_event__new(union perf_event *event)
531{
532 struct pyrf_event *pevent;
533 PyTypeObject *ptype;
534
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300535 if ((event->header.type < PERF_RECORD_MMAP ||
536 event->header.type > PERF_RECORD_SAMPLE) &&
537 !(event->header.type == PERF_RECORD_SWITCH ||
538 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200539 return NULL;
540
541 ptype = pyrf_event__type[event->header.type];
542 pevent = PyObject_New(struct pyrf_event, ptype);
543 if (pevent != NULL)
544 memcpy(&pevent->event, event, event->header.size);
545 return (PyObject *)pevent;
546}
547
548struct pyrf_cpu_map {
549 PyObject_HEAD
550
Jiri Olsaf8548392019-07-21 13:23:49 +0200551 struct perf_cpu_map *cpus;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200552};
553
554static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
555 PyObject *args, PyObject *kwargs)
556{
Frederic Weisbecker64348152011-03-31 18:27:43 +0200557 static char *kwlist[] = { "cpustr", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200558 char *cpustr = NULL;
559
560 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
561 kwlist, &cpustr))
562 return -1;
563
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200564 pcpus->cpus = perf_cpu_map__new(cpustr);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200565 if (pcpus->cpus == NULL)
566 return -1;
567 return 0;
568}
569
570static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
571{
Jiri Olsa38f01d82019-07-21 13:24:17 +0200572 perf_cpu_map__put(pcpus->cpus);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100573 Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200574}
575
576static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
577{
578 struct pyrf_cpu_map *pcpus = (void *)obj;
579
580 return pcpus->cpus->nr;
581}
582
583static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
584{
585 struct pyrf_cpu_map *pcpus = (void *)obj;
586
587 if (i >= pcpus->cpus->nr)
588 return NULL;
589
590 return Py_BuildValue("i", pcpus->cpus->map[i]);
591}
592
593static PySequenceMethods pyrf_cpu_map__sequence_methods = {
594 .sq_length = pyrf_cpu_map__length,
595 .sq_item = pyrf_cpu_map__item,
596};
597
598static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
599
600static PyTypeObject pyrf_cpu_map__type = {
601 PyVarObject_HEAD_INIT(NULL, 0)
602 .tp_name = "perf.cpu_map",
603 .tp_basicsize = sizeof(struct pyrf_cpu_map),
604 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
605 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
606 .tp_doc = pyrf_cpu_map__doc,
607 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
608 .tp_init = (initproc)pyrf_cpu_map__init,
609};
610
611static int pyrf_cpu_map__setup_types(void)
612{
613 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
614 return PyType_Ready(&pyrf_cpu_map__type);
615}
616
617struct pyrf_thread_map {
618 PyObject_HEAD
619
Jiri Olsa9749b902019-07-21 13:23:50 +0200620 struct perf_thread_map *threads;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200621};
622
623static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
624 PyObject *args, PyObject *kwargs)
625{
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200626 static char *kwlist[] = { "pid", "tid", "uid", NULL };
627 int pid = -1, tid = -1, uid = UINT_MAX;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200628
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200629 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
630 kwlist, &pid, &tid, &uid))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200631 return -1;
632
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200633 pthreads->threads = thread_map__new(pid, tid, uid);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200634 if (pthreads->threads == NULL)
635 return -1;
636 return 0;
637}
638
639static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
640{
Jiri Olsa7836e522019-07-21 13:24:20 +0200641 perf_thread_map__put(pthreads->threads);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100642 Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200643}
644
645static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
646{
647 struct pyrf_thread_map *pthreads = (void *)obj;
648
649 return pthreads->threads->nr;
650}
651
652static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
653{
654 struct pyrf_thread_map *pthreads = (void *)obj;
655
656 if (i >= pthreads->threads->nr)
657 return NULL;
658
659 return Py_BuildValue("i", pthreads->threads->map[i]);
660}
661
662static PySequenceMethods pyrf_thread_map__sequence_methods = {
663 .sq_length = pyrf_thread_map__length,
664 .sq_item = pyrf_thread_map__item,
665};
666
667static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
668
669static PyTypeObject pyrf_thread_map__type = {
670 PyVarObject_HEAD_INIT(NULL, 0)
671 .tp_name = "perf.thread_map",
672 .tp_basicsize = sizeof(struct pyrf_thread_map),
673 .tp_dealloc = (destructor)pyrf_thread_map__delete,
674 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
675 .tp_doc = pyrf_thread_map__doc,
676 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
677 .tp_init = (initproc)pyrf_thread_map__init,
678};
679
680static int pyrf_thread_map__setup_types(void)
681{
682 pyrf_thread_map__type.tp_new = PyType_GenericNew;
683 return PyType_Ready(&pyrf_thread_map__type);
684}
685
686struct pyrf_evsel {
687 PyObject_HEAD
688
Jiri Olsa32dcd022019-07-21 13:23:51 +0200689 struct evsel evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200690};
691
692static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
693 PyObject *args, PyObject *kwargs)
694{
695 struct perf_event_attr attr = {
696 .type = PERF_TYPE_HARDWARE,
697 .config = PERF_COUNT_HW_CPU_CYCLES,
698 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
699 };
700 static char *kwlist[] = {
701 "type",
702 "config",
703 "sample_freq",
704 "sample_period",
705 "sample_type",
706 "read_format",
707 "disabled",
708 "inherit",
709 "pinned",
710 "exclusive",
711 "exclude_user",
712 "exclude_kernel",
713 "exclude_hv",
714 "exclude_idle",
715 "mmap",
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300716 "context_switch",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200717 "comm",
718 "freq",
719 "inherit_stat",
720 "enable_on_exec",
721 "task",
722 "watermark",
723 "precise_ip",
724 "mmap_data",
725 "sample_id_all",
726 "wakeup_events",
727 "bp_type",
728 "bp_addr",
Frederic Weisbecker64348152011-03-31 18:27:43 +0200729 "bp_len",
730 NULL
731 };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200732 u64 sample_period = 0;
733 u32 disabled = 0,
734 inherit = 0,
735 pinned = 0,
736 exclusive = 0,
737 exclude_user = 0,
738 exclude_kernel = 0,
739 exclude_hv = 0,
740 exclude_idle = 0,
741 mmap = 0,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300742 context_switch = 0,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200743 comm = 0,
744 freq = 1,
745 inherit_stat = 0,
746 enable_on_exec = 0,
747 task = 0,
748 watermark = 0,
749 precise_ip = 0,
750 mmap_data = 0,
751 sample_id_all = 1;
752 int idx = 0;
753
754 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300755 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200756 &attr.type, &attr.config, &attr.sample_freq,
757 &sample_period, &attr.sample_type,
758 &attr.read_format, &disabled, &inherit,
759 &pinned, &exclusive, &exclude_user,
760 &exclude_kernel, &exclude_hv, &exclude_idle,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300761 &mmap, &context_switch, &comm, &freq, &inherit_stat,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200762 &enable_on_exec, &task, &watermark,
763 &precise_ip, &mmap_data, &sample_id_all,
764 &attr.wakeup_events, &attr.bp_type,
765 &attr.bp_addr, &attr.bp_len, &idx))
766 return -1;
767
768 /* union... */
769 if (sample_period != 0) {
770 if (attr.sample_freq != 0)
771 return -1; /* FIXME: throw right exception */
772 attr.sample_period = sample_period;
773 }
774
775 /* Bitfields */
776 attr.disabled = disabled;
777 attr.inherit = inherit;
778 attr.pinned = pinned;
779 attr.exclusive = exclusive;
780 attr.exclude_user = exclude_user;
781 attr.exclude_kernel = exclude_kernel;
782 attr.exclude_hv = exclude_hv;
783 attr.exclude_idle = exclude_idle;
784 attr.mmap = mmap;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300785 attr.context_switch = context_switch;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200786 attr.comm = comm;
787 attr.freq = freq;
788 attr.inherit_stat = inherit_stat;
789 attr.enable_on_exec = enable_on_exec;
790 attr.task = task;
791 attr.watermark = watermark;
792 attr.precise_ip = precise_ip;
793 attr.mmap_data = mmap_data;
794 attr.sample_id_all = sample_id_all;
Jiri Olsaad4e3c042016-07-10 13:07:55 +0200795 attr.size = sizeof(attr);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200796
Jiri Olsab4b62ee2019-07-21 13:23:53 +0200797 evsel__init(&pevsel->evsel, &attr, idx);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200798 return 0;
799}
800
801static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
802{
803 perf_evsel__exit(&pevsel->evsel);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100804 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200805}
806
807static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
808 PyObject *args, PyObject *kwargs)
809{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200810 struct evsel *evsel = &pevsel->evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +0200811 struct perf_cpu_map *cpus = NULL;
Jiri Olsa9749b902019-07-21 13:23:50 +0200812 struct perf_thread_map *threads = NULL;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200813 PyObject *pcpus = NULL, *pthreads = NULL;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300814 int group = 0, inherit = 0;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200815 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200816
817 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300818 &pcpus, &pthreads, &group, &inherit))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200819 return NULL;
820
821 if (pthreads != NULL)
822 threads = ((struct pyrf_thread_map *)pthreads)->threads;
823
824 if (pcpus != NULL)
825 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
826
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200827 evsel->core.attr.inherit = inherit;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200828 /*
829 * This will group just the fds for this single evsel, to group
830 * multiple events, use evlist.open().
831 */
Jiri Olsa5972d1e2019-07-21 13:24:01 +0200832 if (evsel__open(evsel, cpus, threads) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200833 PyErr_SetFromErrno(PyExc_OSError);
834 return NULL;
835 }
836
837 Py_INCREF(Py_None);
838 return Py_None;
839}
840
841static PyMethodDef pyrf_evsel__methods[] = {
842 {
843 .ml_name = "open",
844 .ml_meth = (PyCFunction)pyrf_evsel__open,
845 .ml_flags = METH_VARARGS | METH_KEYWORDS,
846 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
847 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200848 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200849};
850
851static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
852
853static PyTypeObject pyrf_evsel__type = {
854 PyVarObject_HEAD_INIT(NULL, 0)
855 .tp_name = "perf.evsel",
856 .tp_basicsize = sizeof(struct pyrf_evsel),
857 .tp_dealloc = (destructor)pyrf_evsel__delete,
858 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
859 .tp_doc = pyrf_evsel__doc,
860 .tp_methods = pyrf_evsel__methods,
861 .tp_init = (initproc)pyrf_evsel__init,
862};
863
864static int pyrf_evsel__setup_types(void)
865{
866 pyrf_evsel__type.tp_new = PyType_GenericNew;
867 return PyType_Ready(&pyrf_evsel__type);
868}
869
870struct pyrf_evlist {
871 PyObject_HEAD
872
Jiri Olsa63503db2019-07-21 13:23:52 +0200873 struct evlist evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200874};
875
876static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300877 PyObject *args, PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200878{
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200879 PyObject *pcpus = NULL, *pthreads = NULL;
Jiri Olsaf8548392019-07-21 13:23:49 +0200880 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +0200881 struct perf_thread_map *threads;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200882
883 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
884 return -1;
885
886 threads = ((struct pyrf_thread_map *)pthreads)->threads;
887 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
Jiri Olsa52c86bc2019-07-21 13:23:54 +0200888 evlist__init(&pevlist->evlist, cpus, threads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200889 return 0;
890}
891
892static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
893{
Jiri Olsa470579b2019-09-02 14:34:52 +0200894 evlist__exit(&pevlist->evlist);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100895 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200896}
897
898static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
899 PyObject *args, PyObject *kwargs)
900{
Jiri Olsa63503db2019-07-21 13:23:52 +0200901 struct evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200902 static char *kwlist[] = { "pages", "overwrite", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200903 int pages = 128, overwrite = false;
904
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200905 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
906 &pages, &overwrite))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200907 return NULL;
908
Jiri Olsa9521b5f2019-07-28 12:45:35 +0200909 if (evlist__mmap(evlist, pages) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200910 PyErr_SetFromErrno(PyExc_OSError);
911 return NULL;
912 }
913
914 Py_INCREF(Py_None);
915 return Py_None;
916}
917
918static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
919 PyObject *args, PyObject *kwargs)
920{
Jiri Olsa63503db2019-07-21 13:23:52 +0200921 struct evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200922 static char *kwlist[] = { "timeout", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200923 int timeout = -1, n;
924
925 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
926 return NULL;
927
Jiri Olsa80ab2982019-08-31 22:48:33 +0200928 n = evlist__poll(evlist, timeout);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200929 if (n < 0) {
930 PyErr_SetFromErrno(PyExc_OSError);
931 return NULL;
932 }
933
934 return Py_BuildValue("i", n);
935}
936
937static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300938 PyObject *args __maybe_unused,
939 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200940{
Jiri Olsa63503db2019-07-21 13:23:52 +0200941 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200942 PyObject *list = PyList_New(0);
943 int i;
944
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200945 for (i = 0; i < evlist->core.pollfd.nr; ++i) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200946 PyObject *file;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100947#if PY_MAJOR_VERSION < 3
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200948 FILE *fp = fdopen(evlist->core.pollfd.entries[i].fd, "r");
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200949
950 if (fp == NULL)
951 goto free_list;
952
953 file = PyFile_FromFile(fp, "perf", "r", NULL);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100954#else
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200955 file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1,
Jiri Olsaa389aec2018-12-26 12:21:21 +0100956 NULL, NULL, NULL, 0);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100957#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200958 if (file == NULL)
959 goto free_list;
960
961 if (PyList_Append(list, file) != 0) {
962 Py_DECREF(file);
963 goto free_list;
964 }
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300965
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200966 Py_DECREF(file);
967 }
968
969 return list;
970free_list:
971 return PyErr_NoMemory();
972}
973
974
975static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300976 PyObject *args,
977 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200978{
Jiri Olsa63503db2019-07-21 13:23:52 +0200979 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200980 PyObject *pevsel;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200981 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200982
983 if (!PyArg_ParseTuple(args, "O", &pevsel))
984 return NULL;
985
986 Py_INCREF(pevsel);
987 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200988 evsel->idx = evlist->core.nr_entries;
Jiri Olsaa1cf3a72019-07-21 13:23:59 +0200989 evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200990
Jiri Olsa6484d2f2019-07-21 13:24:28 +0200991 return Py_BuildValue("i", evlist->core.nr_entries);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200992}
993
Jiri Olsaa5830532019-07-27 20:30:53 +0200994static struct mmap *get_md(struct evlist *evlist, int cpu)
Jiri Olsa721f0df2018-08-17 13:45:56 +0200995{
996 int i;
997
Jiri Olsac976ee12019-07-30 13:04:59 +0200998 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Jiri Olsaa5830532019-07-27 20:30:53 +0200999 struct mmap *md = &evlist->mmap[i];
Jiri Olsa721f0df2018-08-17 13:45:56 +02001000
Jiri Olsa56a94702019-07-27 22:33:20 +02001001 if (md->core.cpu == cpu)
Jiri Olsa721f0df2018-08-17 13:45:56 +02001002 return md;
1003 }
1004
1005 return NULL;
1006}
1007
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001008static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
1009 PyObject *args, PyObject *kwargs)
1010{
Jiri Olsa63503db2019-07-21 13:23:52 +02001011 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001012 union perf_event *event;
1013 int sample_id_all = 1, cpu;
Frederic Weisbecker64348152011-03-31 18:27:43 +02001014 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
Jiri Olsaa5830532019-07-27 20:30:53 +02001015 struct mmap *md;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001016 int err;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001017
1018 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1019 &cpu, &sample_id_all))
1020 return NULL;
1021
Jiri Olsa721f0df2018-08-17 13:45:56 +02001022 md = get_md(evlist, cpu);
1023 if (!md)
1024 return NULL;
1025
Kan Liangb9bae2c2018-03-06 10:36:07 -05001026 if (perf_mmap__read_init(md) < 0)
Kan Liang35b7cdc2018-03-01 18:09:00 -05001027 goto end;
1028
Kan Liang0019dc872018-03-06 10:36:06 -05001029 event = perf_mmap__read_event(md);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001030 if (event != NULL) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001031 PyObject *pyevent = pyrf_event__new(event);
1032 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001033 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001034
1035 if (pyevent == NULL)
1036 return PyErr_NoMemory();
1037
Jiri Olsa377f6982016-07-10 13:07:59 +02001038 evsel = perf_evlist__event2evsel(evlist, event);
Petr Machata83428f22018-03-22 00:57:32 +01001039 if (!evsel) {
1040 Py_INCREF(Py_None);
Jiri Olsa377f6982016-07-10 13:07:59 +02001041 return Py_None;
Petr Machata83428f22018-03-22 00:57:32 +01001042 }
Jiri Olsa377f6982016-07-10 13:07:59 +02001043
1044 pevent->evsel = evsel;
1045
1046 err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
Jiri Olsae8968e62016-07-10 13:07:56 +02001047
1048 /* Consume the even only after we parsed it out. */
Jiri Olsa7728fa02019-10-07 14:53:17 +02001049 perf_mmap__consume(&md->core);
Jiri Olsae8968e62016-07-10 13:07:56 +02001050
Arnaldo Carvalho de Melo5c6970a2011-06-02 10:55:10 -03001051 if (err)
1052 return PyErr_Format(PyExc_OSError,
1053 "perf: can't parse sample, err=%d", err);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001054 return pyevent;
1055 }
Kan Liang35b7cdc2018-03-01 18:09:00 -05001056end:
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001057 Py_INCREF(Py_None);
1058 return Py_None;
1059}
1060
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001061static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1062 PyObject *args, PyObject *kwargs)
1063{
Jiri Olsa63503db2019-07-21 13:23:52 +02001064 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001065 int group = 0;
1066 static char *kwlist[] = { "group", NULL };
1067
1068 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1069 return NULL;
1070
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001071 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001072 perf_evlist__set_leader(evlist);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001073
Jiri Olsa474ddc42019-07-21 13:24:06 +02001074 if (evlist__open(evlist) < 0) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001075 PyErr_SetFromErrno(PyExc_OSError);
1076 return NULL;
1077 }
1078
1079 Py_INCREF(Py_None);
1080 return Py_None;
1081}
1082
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001083static PyMethodDef pyrf_evlist__methods[] = {
1084 {
1085 .ml_name = "mmap",
1086 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1087 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1088 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1089 },
1090 {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001091 .ml_name = "open",
1092 .ml_meth = (PyCFunction)pyrf_evlist__open,
1093 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1094 .ml_doc = PyDoc_STR("open the file descriptors.")
1095 },
1096 {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001097 .ml_name = "poll",
1098 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1099 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1100 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1101 },
1102 {
1103 .ml_name = "get_pollfd",
1104 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1105 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1106 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1107 },
1108 {
1109 .ml_name = "add",
1110 .ml_meth = (PyCFunction)pyrf_evlist__add,
1111 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1112 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1113 },
1114 {
1115 .ml_name = "read_on_cpu",
1116 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1117 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1118 .ml_doc = PyDoc_STR("reads an event.")
1119 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001120 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001121};
1122
1123static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1124{
1125 struct pyrf_evlist *pevlist = (void *)obj;
1126
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001127 return pevlist->evlist.core.nr_entries;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001128}
1129
1130static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1131{
1132 struct pyrf_evlist *pevlist = (void *)obj;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001133 struct evsel *pos;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001134
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001135 if (i >= pevlist->evlist.core.nr_entries)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001136 return NULL;
1137
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001138 evlist__for_each_entry(&pevlist->evlist, pos) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001139 if (i-- == 0)
1140 break;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001141 }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001142
1143 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1144}
1145
1146static PySequenceMethods pyrf_evlist__sequence_methods = {
1147 .sq_length = pyrf_evlist__length,
1148 .sq_item = pyrf_evlist__item,
1149};
1150
1151static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1152
1153static PyTypeObject pyrf_evlist__type = {
1154 PyVarObject_HEAD_INIT(NULL, 0)
1155 .tp_name = "perf.evlist",
1156 .tp_basicsize = sizeof(struct pyrf_evlist),
1157 .tp_dealloc = (destructor)pyrf_evlist__delete,
1158 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1159 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1160 .tp_doc = pyrf_evlist__doc,
1161 .tp_methods = pyrf_evlist__methods,
1162 .tp_init = (initproc)pyrf_evlist__init,
1163};
1164
1165static int pyrf_evlist__setup_types(void)
1166{
1167 pyrf_evlist__type.tp_new = PyType_GenericNew;
1168 return PyType_Ready(&pyrf_evlist__type);
1169}
1170
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001171#define PERF_CONST(name) { #name, PERF_##name }
1172
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001173static struct {
1174 const char *name;
1175 int value;
1176} perf__constants[] = {
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001177 PERF_CONST(TYPE_HARDWARE),
1178 PERF_CONST(TYPE_SOFTWARE),
1179 PERF_CONST(TYPE_TRACEPOINT),
1180 PERF_CONST(TYPE_HW_CACHE),
1181 PERF_CONST(TYPE_RAW),
1182 PERF_CONST(TYPE_BREAKPOINT),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001183
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001184 PERF_CONST(COUNT_HW_CPU_CYCLES),
1185 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1186 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1187 PERF_CONST(COUNT_HW_CACHE_MISSES),
1188 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1189 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1190 PERF_CONST(COUNT_HW_BUS_CYCLES),
1191 PERF_CONST(COUNT_HW_CACHE_L1D),
1192 PERF_CONST(COUNT_HW_CACHE_L1I),
1193 PERF_CONST(COUNT_HW_CACHE_LL),
1194 PERF_CONST(COUNT_HW_CACHE_DTLB),
1195 PERF_CONST(COUNT_HW_CACHE_ITLB),
1196 PERF_CONST(COUNT_HW_CACHE_BPU),
1197 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1198 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1199 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1200 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1201 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001202
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001203 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1204 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
Ingo Molnar129c04c2011-04-29 14:41:28 +02001205
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001206 PERF_CONST(COUNT_SW_CPU_CLOCK),
1207 PERF_CONST(COUNT_SW_TASK_CLOCK),
1208 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1209 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1210 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1211 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1212 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1213 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1214 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1215 PERF_CONST(COUNT_SW_DUMMY),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001216
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001217 PERF_CONST(SAMPLE_IP),
1218 PERF_CONST(SAMPLE_TID),
1219 PERF_CONST(SAMPLE_TIME),
1220 PERF_CONST(SAMPLE_ADDR),
1221 PERF_CONST(SAMPLE_READ),
1222 PERF_CONST(SAMPLE_CALLCHAIN),
1223 PERF_CONST(SAMPLE_ID),
1224 PERF_CONST(SAMPLE_CPU),
1225 PERF_CONST(SAMPLE_PERIOD),
1226 PERF_CONST(SAMPLE_STREAM_ID),
1227 PERF_CONST(SAMPLE_RAW),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001228
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001229 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1230 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1231 PERF_CONST(FORMAT_ID),
1232 PERF_CONST(FORMAT_GROUP),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001233
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001234 PERF_CONST(RECORD_MMAP),
1235 PERF_CONST(RECORD_LOST),
1236 PERF_CONST(RECORD_COMM),
1237 PERF_CONST(RECORD_EXIT),
1238 PERF_CONST(RECORD_THROTTLE),
1239 PERF_CONST(RECORD_UNTHROTTLE),
1240 PERF_CONST(RECORD_FORK),
1241 PERF_CONST(RECORD_READ),
1242 PERF_CONST(RECORD_SAMPLE),
Arnaldo Carvalho de Melo84576da2015-07-24 13:04:09 -03001243 PERF_CONST(RECORD_MMAP2),
1244 PERF_CONST(RECORD_AUX),
1245 PERF_CONST(RECORD_ITRACE_START),
1246 PERF_CONST(RECORD_LOST_SAMPLES),
1247 PERF_CONST(RECORD_SWITCH),
1248 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -03001249
1250 PERF_CONST(RECORD_MISC_SWITCH_OUT),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001251 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001252};
1253
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001254static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1255 PyObject *args, PyObject *kwargs)
1256{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001257 struct tep_event *tp_format;
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001258 static char *kwlist[] = { "sys", "name", NULL };
1259 char *sys = NULL;
1260 char *name = NULL;
1261
1262 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1263 &sys, &name))
1264 return NULL;
1265
1266 tp_format = trace_event__tp_format(sys, name);
1267 if (IS_ERR(tp_format))
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001268 return _PyLong_FromLong(-1);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001269
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001270 return _PyLong_FromLong(tp_format->id);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001271}
1272
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001273static PyMethodDef perf__methods[] = {
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001274 {
1275 .ml_name = "tracepoint",
1276 .ml_meth = (PyCFunction) pyrf__tracepoint,
1277 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1278 .ml_doc = PyDoc_STR("Get tracepoint config.")
1279 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001280 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001281};
1282
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001283#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001284PyMODINIT_FUNC initperf(void)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001285#else
1286PyMODINIT_FUNC PyInit_perf(void)
1287#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001288{
1289 PyObject *obj;
1290 int i;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001291 PyObject *dict;
1292#if PY_MAJOR_VERSION < 3
1293 PyObject *module = Py_InitModule("perf", perf__methods);
1294#else
1295 static struct PyModuleDef moduledef = {
1296 PyModuleDef_HEAD_INIT,
1297 "perf", /* m_name */
1298 "", /* m_doc */
1299 -1, /* m_size */
1300 perf__methods, /* m_methods */
1301 NULL, /* m_reload */
1302 NULL, /* m_traverse */
1303 NULL, /* m_clear */
1304 NULL, /* m_free */
1305 };
1306 PyObject *module = PyModule_Create(&moduledef);
1307#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001308
1309 if (module == NULL ||
1310 pyrf_event__setup_types() < 0 ||
1311 pyrf_evlist__setup_types() < 0 ||
1312 pyrf_evsel__setup_types() < 0 ||
1313 pyrf_thread_map__setup_types() < 0 ||
1314 pyrf_cpu_map__setup_types() < 0)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001315#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001316 return;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001317#else
1318 return module;
1319#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001320
Jiri Olsa918512b2013-09-12 18:39:35 +02001321 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0da2e9c2012-10-16 14:51:04 -03001322 page_size = sysconf(_SC_PAGE_SIZE);
1323
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001324 Py_INCREF(&pyrf_evlist__type);
1325 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1326
1327 Py_INCREF(&pyrf_evsel__type);
1328 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1329
Jiri Olsa85e37de2016-07-10 13:07:57 +02001330 Py_INCREF(&pyrf_mmap_event__type);
1331 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1332
1333 Py_INCREF(&pyrf_lost_event__type);
1334 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1335
1336 Py_INCREF(&pyrf_comm_event__type);
1337 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1338
1339 Py_INCREF(&pyrf_task_event__type);
1340 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1341
1342 Py_INCREF(&pyrf_throttle_event__type);
1343 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1344
1345 Py_INCREF(&pyrf_task_event__type);
1346 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1347
1348 Py_INCREF(&pyrf_read_event__type);
1349 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1350
1351 Py_INCREF(&pyrf_sample_event__type);
1352 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1353
1354 Py_INCREF(&pyrf_context_switch_event__type);
1355 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1356
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001357 Py_INCREF(&pyrf_thread_map__type);
1358 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1359
1360 Py_INCREF(&pyrf_cpu_map__type);
1361 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1362
1363 dict = PyModule_GetDict(module);
1364 if (dict == NULL)
1365 goto error;
1366
1367 for (i = 0; perf__constants[i].name != NULL; i++) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001368 obj = _PyLong_FromLong(perf__constants[i].value);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001369 if (obj == NULL)
1370 goto error;
1371 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1372 Py_DECREF(obj);
1373 }
1374
1375error:
1376 if (PyErr_Occurred())
1377 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001378#if PY_MAJOR_VERSION >= 3
1379 return module;
1380#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001381}
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -03001382
1383/*
1384 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1385 * binding.
1386 */
1387void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
1388 int fd, int group_fd, unsigned long flags)
1389{
1390}