blob: dda0ac978b1eb2371d9feebc64154d5cf9bb949e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02002#include <Python.h>
3#include <structmember.h>
4#include <inttypes.h>
5#include <poll.h>
Jiri Olsa1075fbb2016-07-10 13:07:58 +02006#include <linux/err.h>
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02007#include "evlist.h"
Arnaldo Carvalho de Melo56e2e052017-04-19 18:38:33 -03008#include "callchain.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02009#include "evsel.h"
10#include "event.h"
11#include "cpumap.h"
Arnaldo Carvalho de Melofea01392017-04-17 16:23:22 -030012#include "print_binary.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020013#include "thread_map.h"
Jiri Olsa721f0df2018-08-17 13:45:56 +020014#include "mmap.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020015
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010016#if PY_MAJOR_VERSION < 3
17#define _PyUnicode_FromString(arg) \
18 PyString_FromString(arg)
19#define _PyUnicode_AsString(arg) \
20 PyString_AsString(arg)
21#define _PyUnicode_FromFormat(...) \
22 PyString_FromFormat(__VA_ARGS__)
23#define _PyLong_FromLong(arg) \
24 PyInt_FromLong(arg)
25
26#else
27
28#define _PyUnicode_FromString(arg) \
29 PyUnicode_FromString(arg)
30#define _PyUnicode_FromFormat(...) \
31 PyUnicode_FromFormat(__VA_ARGS__)
32#define _PyLong_FromLong(arg) \
33 PyLong_FromLong(arg)
34#endif
35
36#ifndef Py_TYPE
37#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
38#endif
39
Adrian Hunter8afb4c02013-08-14 15:48:23 +030040/*
Arnaldo Carvalho de Melo56e2e052017-04-19 18:38:33 -030041 * Provide these two so that we don't have to link against callchain.c and
42 * start dragging hist.c, etc.
43 */
44struct callchain_param callchain_param;
45
46int parse_callchain_record(const char *arg __maybe_unused,
47 struct callchain_param *param __maybe_unused)
48{
49 return 0;
50}
51
52/*
Adrian Hunter8afb4c02013-08-14 15:48:23 +030053 * Support debug printing even though util/debug.c is not linked. That means
54 * implementing 'verbose' and 'eprintf'.
55 */
56int verbose;
57
Jiri Olsac95688a2014-07-14 23:46:49 +020058int eprintf(int level, int var, const char *fmt, ...)
Adrian Hunter8afb4c02013-08-14 15:48:23 +030059{
60 va_list args;
61 int ret = 0;
62
Jiri Olsac95688a2014-07-14 23:46:49 +020063 if (var >= level) {
Adrian Hunter8afb4c02013-08-14 15:48:23 +030064 va_start(args, fmt);
65 ret = vfprintf(stderr, fmt, args);
66 va_end(args);
67 }
68
69 return ret;
70}
71
Frederic Weisbeckercfff2d92011-02-25 21:30:16 +010072/* Define PyVarObject_HEAD_INIT for python 2.5 */
73#ifndef PyVarObject_HEAD_INIT
74# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
75#endif
76
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010077#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -020078PyMODINIT_FUNC initperf(void);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010079#else
80PyMODINIT_FUNC PyInit_perf(void);
81#endif
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -020082
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020083#define member_def(type, member, ptype, help) \
84 { #member, ptype, \
85 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
86 0, help }
87
88#define sample_member_def(name, member, ptype, help) \
89 { #name, ptype, \
90 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
91 0, help }
92
93struct pyrf_event {
94 PyObject_HEAD
Jiri Olsa377f6982016-07-10 13:07:59 +020095 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020096 struct perf_sample sample;
97 union perf_event event;
98};
99
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200100#define sample_members \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200101 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200102 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
103 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200104 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
105 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
106 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
107 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
108 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200109 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
110
111static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
112
113static PyMemberDef pyrf_mmap_event__members[] = {
114 sample_members
115 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300116 member_def(perf_event_header, misc, T_UINT, "event misc"),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200117 member_def(mmap_event, pid, T_UINT, "event pid"),
118 member_def(mmap_event, tid, T_UINT, "event tid"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200119 member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
120 member_def(mmap_event, len, T_ULONGLONG, "map length"),
121 member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200122 member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200123 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200124};
125
126static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
127{
128 PyObject *ret;
129 char *s;
130
131 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
132 "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
133 "filename: %s }",
134 pevent->event.mmap.pid, pevent->event.mmap.tid,
135 pevent->event.mmap.start, pevent->event.mmap.len,
136 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
137 ret = PyErr_NoMemory();
138 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100139 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200140 free(s);
141 }
142 return ret;
143}
144
145static PyTypeObject pyrf_mmap_event__type = {
146 PyVarObject_HEAD_INIT(NULL, 0)
147 .tp_name = "perf.mmap_event",
148 .tp_basicsize = sizeof(struct pyrf_event),
149 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
150 .tp_doc = pyrf_mmap_event__doc,
151 .tp_members = pyrf_mmap_event__members,
152 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
153};
154
155static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
156
157static PyMemberDef pyrf_task_event__members[] = {
158 sample_members
159 member_def(perf_event_header, type, T_UINT, "event type"),
160 member_def(fork_event, pid, T_UINT, "event pid"),
161 member_def(fork_event, ppid, T_UINT, "event ppid"),
162 member_def(fork_event, tid, T_UINT, "event tid"),
163 member_def(fork_event, ptid, T_UINT, "event ptid"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200164 member_def(fork_event, time, T_ULONGLONG, "timestamp"),
165 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200166};
167
168static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
169{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100170 return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200171 "ptid: %u, time: %" PRIu64 "}",
172 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
173 pevent->event.fork.pid,
174 pevent->event.fork.ppid,
175 pevent->event.fork.tid,
176 pevent->event.fork.ptid,
177 pevent->event.fork.time);
178}
179
180static PyTypeObject pyrf_task_event__type = {
181 PyVarObject_HEAD_INIT(NULL, 0)
182 .tp_name = "perf.task_event",
183 .tp_basicsize = sizeof(struct pyrf_event),
184 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
185 .tp_doc = pyrf_task_event__doc,
186 .tp_members = pyrf_task_event__members,
187 .tp_repr = (reprfunc)pyrf_task_event__repr,
188};
189
190static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
191
192static PyMemberDef pyrf_comm_event__members[] = {
193 sample_members
194 member_def(perf_event_header, type, T_UINT, "event type"),
195 member_def(comm_event, pid, T_UINT, "event pid"),
196 member_def(comm_event, tid, T_UINT, "event tid"),
197 member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200198 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200199};
200
201static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
202{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100203 return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200204 pevent->event.comm.pid,
205 pevent->event.comm.tid,
206 pevent->event.comm.comm);
207}
208
209static PyTypeObject pyrf_comm_event__type = {
210 PyVarObject_HEAD_INIT(NULL, 0)
211 .tp_name = "perf.comm_event",
212 .tp_basicsize = sizeof(struct pyrf_event),
213 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
214 .tp_doc = pyrf_comm_event__doc,
215 .tp_members = pyrf_comm_event__members,
216 .tp_repr = (reprfunc)pyrf_comm_event__repr,
217};
218
219static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
220
221static PyMemberDef pyrf_throttle_event__members[] = {
222 sample_members
223 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200224 member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
225 member_def(throttle_event, id, T_ULONGLONG, "event id"),
226 member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
227 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200228};
229
230static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
231{
232 struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
233
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100234 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200235 ", stream_id: %" PRIu64 " }",
236 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
237 te->time, te->id, te->stream_id);
238}
239
240static PyTypeObject pyrf_throttle_event__type = {
241 PyVarObject_HEAD_INIT(NULL, 0)
242 .tp_name = "perf.throttle_event",
243 .tp_basicsize = sizeof(struct pyrf_event),
244 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
245 .tp_doc = pyrf_throttle_event__doc,
246 .tp_members = pyrf_throttle_event__members,
247 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
248};
249
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300250static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
251
252static PyMemberDef pyrf_lost_event__members[] = {
253 sample_members
254 member_def(lost_event, id, T_ULONGLONG, "event id"),
255 member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
256 { .name = NULL, },
257};
258
259static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
260{
261 PyObject *ret;
262 char *s;
263
264 if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
265 "lost: %#" PRIx64 " }",
266 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
267 ret = PyErr_NoMemory();
268 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100269 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300270 free(s);
271 }
272 return ret;
273}
274
275static PyTypeObject pyrf_lost_event__type = {
276 PyVarObject_HEAD_INIT(NULL, 0)
277 .tp_name = "perf.lost_event",
278 .tp_basicsize = sizeof(struct pyrf_event),
279 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
280 .tp_doc = pyrf_lost_event__doc,
281 .tp_members = pyrf_lost_event__members,
282 .tp_repr = (reprfunc)pyrf_lost_event__repr,
283};
284
285static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
286
287static PyMemberDef pyrf_read_event__members[] = {
288 sample_members
289 member_def(read_event, pid, T_UINT, "event pid"),
290 member_def(read_event, tid, T_UINT, "event tid"),
291 { .name = NULL, },
292};
293
294static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
295{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100296 return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300297 pevent->event.read.pid,
298 pevent->event.read.tid);
299 /*
300 * FIXME: return the array of read values,
301 * making this method useful ;-)
302 */
303}
304
305static PyTypeObject pyrf_read_event__type = {
306 PyVarObject_HEAD_INIT(NULL, 0)
307 .tp_name = "perf.read_event",
308 .tp_basicsize = sizeof(struct pyrf_event),
309 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
310 .tp_doc = pyrf_read_event__doc,
311 .tp_members = pyrf_read_event__members,
312 .tp_repr = (reprfunc)pyrf_read_event__repr,
313};
314
315static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
316
317static PyMemberDef pyrf_sample_event__members[] = {
318 sample_members
319 member_def(perf_event_header, type, T_UINT, "event type"),
320 { .name = NULL, },
321};
322
323static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
324{
325 PyObject *ret;
326 char *s;
327
328 if (asprintf(&s, "{ type: sample }") < 0) {
329 ret = PyErr_NoMemory();
330 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100331 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300332 free(s);
333 }
334 return ret;
335}
336
Jiri Olsabae57e32016-07-10 13:08:00 +0200337static bool is_tracepoint(struct pyrf_event *pevent)
338{
339 return pevent->evsel->attr.type == PERF_TYPE_TRACEPOINT;
340}
341
Jiri Olsabae57e32016-07-10 13:08:00 +0200342static PyObject*
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400343tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
Jiri Olsabae57e32016-07-10 13:08:00 +0200344{
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -0400345 struct tep_handle *pevent = field->event->pevent;
Jiri Olsabae57e32016-07-10 13:08:00 +0200346 void *data = pe->sample.raw_data;
347 PyObject *ret = NULL;
348 unsigned long long val;
349 unsigned int offset, len;
350
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400351 if (field->flags & TEP_FIELD_IS_ARRAY) {
Jiri Olsabae57e32016-07-10 13:08:00 +0200352 offset = field->offset;
353 len = field->size;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400354 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400355 val = tep_read_number(pevent, data + offset, len);
Jiri Olsabae57e32016-07-10 13:08:00 +0200356 offset = val;
357 len = offset >> 16;
358 offset &= 0xffff;
359 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400360 if (field->flags & TEP_FIELD_IS_STRING &&
Jiri Olsabae57e32016-07-10 13:08:00 +0200361 is_printable_array(data + offset, len)) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100362 ret = _PyUnicode_FromString((char *)data + offset);
Jiri Olsabae57e32016-07-10 13:08:00 +0200363 } else {
364 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400365 field->flags &= ~TEP_FIELD_IS_STRING;
Jiri Olsabae57e32016-07-10 13:08:00 +0200366 }
367 } else {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400368 val = tep_read_number(pevent, data + field->offset,
369 field->size);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400370 if (field->flags & TEP_FIELD_IS_POINTER)
Jiri Olsabae57e32016-07-10 13:08:00 +0200371 ret = PyLong_FromUnsignedLong((unsigned long) val);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400372 else if (field->flags & TEP_FIELD_IS_SIGNED)
Jiri Olsabae57e32016-07-10 13:08:00 +0200373 ret = PyLong_FromLong((long) val);
374 else
375 ret = PyLong_FromUnsignedLong((unsigned long) val);
376 }
377
378 return ret;
379}
380
381static PyObject*
382get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
383{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100384 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
Jiri Olsabae57e32016-07-10 13:08:00 +0200385 struct perf_evsel *evsel = pevent->evsel;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400386 struct tep_format_field *field;
Jiri Olsabae57e32016-07-10 13:08:00 +0200387
388 if (!evsel->tp_format) {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500389 struct tep_event *tp_format;
Jiri Olsabae57e32016-07-10 13:08:00 +0200390
391 tp_format = trace_event__tp_format_id(evsel->attr.config);
392 if (!tp_format)
393 return NULL;
394
395 evsel->tp_format = tp_format;
396 }
397
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -0400398 field = tep_find_any_field(evsel->tp_format, str);
Jiri Olsabae57e32016-07-10 13:08:00 +0200399 if (!field)
400 return NULL;
401
402 return tracepoint_field(pevent, field);
403}
404
405static PyObject*
406pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
407{
408 PyObject *obj = NULL;
409
410 if (is_tracepoint(pevent))
411 obj = get_tracepoint_field(pevent, attr_name);
412
413 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
414}
415
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300416static PyTypeObject pyrf_sample_event__type = {
417 PyVarObject_HEAD_INIT(NULL, 0)
418 .tp_name = "perf.sample_event",
419 .tp_basicsize = sizeof(struct pyrf_event),
420 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
421 .tp_doc = pyrf_sample_event__doc,
422 .tp_members = pyrf_sample_event__members,
423 .tp_repr = (reprfunc)pyrf_sample_event__repr,
Jiri Olsabae57e32016-07-10 13:08:00 +0200424 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300425};
426
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300427static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
428
429static PyMemberDef pyrf_context_switch_event__members[] = {
430 sample_members
431 member_def(perf_event_header, type, T_UINT, "event type"),
432 member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"),
433 member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"),
434 { .name = NULL, },
435};
436
437static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
438{
439 PyObject *ret;
440 char *s;
441
442 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
443 pevent->event.context_switch.next_prev_pid,
444 pevent->event.context_switch.next_prev_tid,
445 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
446 ret = PyErr_NoMemory();
447 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100448 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300449 free(s);
450 }
451 return ret;
452}
453
454static PyTypeObject pyrf_context_switch_event__type = {
455 PyVarObject_HEAD_INIT(NULL, 0)
456 .tp_name = "perf.context_switch_event",
457 .tp_basicsize = sizeof(struct pyrf_event),
458 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
459 .tp_doc = pyrf_context_switch_event__doc,
460 .tp_members = pyrf_context_switch_event__members,
461 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
462};
463
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200464static int pyrf_event__setup_types(void)
465{
466 int err;
467 pyrf_mmap_event__type.tp_new =
468 pyrf_task_event__type.tp_new =
469 pyrf_comm_event__type.tp_new =
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300470 pyrf_lost_event__type.tp_new =
471 pyrf_read_event__type.tp_new =
472 pyrf_sample_event__type.tp_new =
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300473 pyrf_context_switch_event__type.tp_new =
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200474 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
475 err = PyType_Ready(&pyrf_mmap_event__type);
476 if (err < 0)
477 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300478 err = PyType_Ready(&pyrf_lost_event__type);
479 if (err < 0)
480 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200481 err = PyType_Ready(&pyrf_task_event__type);
482 if (err < 0)
483 goto out;
484 err = PyType_Ready(&pyrf_comm_event__type);
485 if (err < 0)
486 goto out;
487 err = PyType_Ready(&pyrf_throttle_event__type);
488 if (err < 0)
489 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300490 err = PyType_Ready(&pyrf_read_event__type);
491 if (err < 0)
492 goto out;
493 err = PyType_Ready(&pyrf_sample_event__type);
494 if (err < 0)
495 goto out;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300496 err = PyType_Ready(&pyrf_context_switch_event__type);
497 if (err < 0)
498 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200499out:
500 return err;
501}
502
503static PyTypeObject *pyrf_event__type[] = {
504 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300505 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200506 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
507 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
508 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
509 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
510 [PERF_RECORD_FORK] = &pyrf_task_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300511 [PERF_RECORD_READ] = &pyrf_read_event__type,
512 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300513 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
514 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200515};
516
517static PyObject *pyrf_event__new(union perf_event *event)
518{
519 struct pyrf_event *pevent;
520 PyTypeObject *ptype;
521
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300522 if ((event->header.type < PERF_RECORD_MMAP ||
523 event->header.type > PERF_RECORD_SAMPLE) &&
524 !(event->header.type == PERF_RECORD_SWITCH ||
525 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200526 return NULL;
527
528 ptype = pyrf_event__type[event->header.type];
529 pevent = PyObject_New(struct pyrf_event, ptype);
530 if (pevent != NULL)
531 memcpy(&pevent->event, event, event->header.size);
532 return (PyObject *)pevent;
533}
534
535struct pyrf_cpu_map {
536 PyObject_HEAD
537
538 struct cpu_map *cpus;
539};
540
541static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
542 PyObject *args, PyObject *kwargs)
543{
Frederic Weisbecker64348152011-03-31 18:27:43 +0200544 static char *kwlist[] = { "cpustr", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200545 char *cpustr = NULL;
546
547 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
548 kwlist, &cpustr))
549 return -1;
550
551 pcpus->cpus = cpu_map__new(cpustr);
552 if (pcpus->cpus == NULL)
553 return -1;
554 return 0;
555}
556
557static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
558{
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200559 cpu_map__put(pcpus->cpus);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100560 Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200561}
562
563static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
564{
565 struct pyrf_cpu_map *pcpus = (void *)obj;
566
567 return pcpus->cpus->nr;
568}
569
570static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
571{
572 struct pyrf_cpu_map *pcpus = (void *)obj;
573
574 if (i >= pcpus->cpus->nr)
575 return NULL;
576
577 return Py_BuildValue("i", pcpus->cpus->map[i]);
578}
579
580static PySequenceMethods pyrf_cpu_map__sequence_methods = {
581 .sq_length = pyrf_cpu_map__length,
582 .sq_item = pyrf_cpu_map__item,
583};
584
585static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
586
587static PyTypeObject pyrf_cpu_map__type = {
588 PyVarObject_HEAD_INIT(NULL, 0)
589 .tp_name = "perf.cpu_map",
590 .tp_basicsize = sizeof(struct pyrf_cpu_map),
591 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
592 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
593 .tp_doc = pyrf_cpu_map__doc,
594 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
595 .tp_init = (initproc)pyrf_cpu_map__init,
596};
597
598static int pyrf_cpu_map__setup_types(void)
599{
600 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
601 return PyType_Ready(&pyrf_cpu_map__type);
602}
603
604struct pyrf_thread_map {
605 PyObject_HEAD
606
607 struct thread_map *threads;
608};
609
610static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
611 PyObject *args, PyObject *kwargs)
612{
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200613 static char *kwlist[] = { "pid", "tid", "uid", NULL };
614 int pid = -1, tid = -1, uid = UINT_MAX;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200615
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200616 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
617 kwlist, &pid, &tid, &uid))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200618 return -1;
619
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200620 pthreads->threads = thread_map__new(pid, tid, uid);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200621 if (pthreads->threads == NULL)
622 return -1;
623 return 0;
624}
625
626static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
627{
Jiri Olsa186fbb72015-06-23 00:36:05 +0200628 thread_map__put(pthreads->threads);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100629 Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200630}
631
632static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
633{
634 struct pyrf_thread_map *pthreads = (void *)obj;
635
636 return pthreads->threads->nr;
637}
638
639static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
640{
641 struct pyrf_thread_map *pthreads = (void *)obj;
642
643 if (i >= pthreads->threads->nr)
644 return NULL;
645
646 return Py_BuildValue("i", pthreads->threads->map[i]);
647}
648
649static PySequenceMethods pyrf_thread_map__sequence_methods = {
650 .sq_length = pyrf_thread_map__length,
651 .sq_item = pyrf_thread_map__item,
652};
653
654static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
655
656static PyTypeObject pyrf_thread_map__type = {
657 PyVarObject_HEAD_INIT(NULL, 0)
658 .tp_name = "perf.thread_map",
659 .tp_basicsize = sizeof(struct pyrf_thread_map),
660 .tp_dealloc = (destructor)pyrf_thread_map__delete,
661 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
662 .tp_doc = pyrf_thread_map__doc,
663 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
664 .tp_init = (initproc)pyrf_thread_map__init,
665};
666
667static int pyrf_thread_map__setup_types(void)
668{
669 pyrf_thread_map__type.tp_new = PyType_GenericNew;
670 return PyType_Ready(&pyrf_thread_map__type);
671}
672
673struct pyrf_evsel {
674 PyObject_HEAD
675
676 struct perf_evsel evsel;
677};
678
679static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
680 PyObject *args, PyObject *kwargs)
681{
682 struct perf_event_attr attr = {
683 .type = PERF_TYPE_HARDWARE,
684 .config = PERF_COUNT_HW_CPU_CYCLES,
685 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
686 };
687 static char *kwlist[] = {
688 "type",
689 "config",
690 "sample_freq",
691 "sample_period",
692 "sample_type",
693 "read_format",
694 "disabled",
695 "inherit",
696 "pinned",
697 "exclusive",
698 "exclude_user",
699 "exclude_kernel",
700 "exclude_hv",
701 "exclude_idle",
702 "mmap",
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300703 "context_switch",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200704 "comm",
705 "freq",
706 "inherit_stat",
707 "enable_on_exec",
708 "task",
709 "watermark",
710 "precise_ip",
711 "mmap_data",
712 "sample_id_all",
713 "wakeup_events",
714 "bp_type",
715 "bp_addr",
Frederic Weisbecker64348152011-03-31 18:27:43 +0200716 "bp_len",
717 NULL
718 };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200719 u64 sample_period = 0;
720 u32 disabled = 0,
721 inherit = 0,
722 pinned = 0,
723 exclusive = 0,
724 exclude_user = 0,
725 exclude_kernel = 0,
726 exclude_hv = 0,
727 exclude_idle = 0,
728 mmap = 0,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300729 context_switch = 0,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200730 comm = 0,
731 freq = 1,
732 inherit_stat = 0,
733 enable_on_exec = 0,
734 task = 0,
735 watermark = 0,
736 precise_ip = 0,
737 mmap_data = 0,
738 sample_id_all = 1;
739 int idx = 0;
740
741 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300742 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200743 &attr.type, &attr.config, &attr.sample_freq,
744 &sample_period, &attr.sample_type,
745 &attr.read_format, &disabled, &inherit,
746 &pinned, &exclusive, &exclude_user,
747 &exclude_kernel, &exclude_hv, &exclude_idle,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300748 &mmap, &context_switch, &comm, &freq, &inherit_stat,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200749 &enable_on_exec, &task, &watermark,
750 &precise_ip, &mmap_data, &sample_id_all,
751 &attr.wakeup_events, &attr.bp_type,
752 &attr.bp_addr, &attr.bp_len, &idx))
753 return -1;
754
755 /* union... */
756 if (sample_period != 0) {
757 if (attr.sample_freq != 0)
758 return -1; /* FIXME: throw right exception */
759 attr.sample_period = sample_period;
760 }
761
762 /* Bitfields */
763 attr.disabled = disabled;
764 attr.inherit = inherit;
765 attr.pinned = pinned;
766 attr.exclusive = exclusive;
767 attr.exclude_user = exclude_user;
768 attr.exclude_kernel = exclude_kernel;
769 attr.exclude_hv = exclude_hv;
770 attr.exclude_idle = exclude_idle;
771 attr.mmap = mmap;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300772 attr.context_switch = context_switch;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200773 attr.comm = comm;
774 attr.freq = freq;
775 attr.inherit_stat = inherit_stat;
776 attr.enable_on_exec = enable_on_exec;
777 attr.task = task;
778 attr.watermark = watermark;
779 attr.precise_ip = precise_ip;
780 attr.mmap_data = mmap_data;
781 attr.sample_id_all = sample_id_all;
Jiri Olsaad4e3c042016-07-10 13:07:55 +0200782 attr.size = sizeof(attr);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200783
784 perf_evsel__init(&pevsel->evsel, &attr, idx);
785 return 0;
786}
787
788static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
789{
790 perf_evsel__exit(&pevsel->evsel);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100791 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200792}
793
794static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
795 PyObject *args, PyObject *kwargs)
796{
797 struct perf_evsel *evsel = &pevsel->evsel;
798 struct cpu_map *cpus = NULL;
799 struct thread_map *threads = NULL;
800 PyObject *pcpus = NULL, *pthreads = NULL;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300801 int group = 0, inherit = 0;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200802 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200803
804 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300805 &pcpus, &pthreads, &group, &inherit))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200806 return NULL;
807
808 if (pthreads != NULL)
809 threads = ((struct pyrf_thread_map *)pthreads)->threads;
810
811 if (pcpus != NULL)
812 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
813
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300814 evsel->attr.inherit = inherit;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200815 /*
816 * This will group just the fds for this single evsel, to group
817 * multiple events, use evlist.open().
818 */
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200819 if (perf_evsel__open(evsel, cpus, threads) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200820 PyErr_SetFromErrno(PyExc_OSError);
821 return NULL;
822 }
823
824 Py_INCREF(Py_None);
825 return Py_None;
826}
827
828static PyMethodDef pyrf_evsel__methods[] = {
829 {
830 .ml_name = "open",
831 .ml_meth = (PyCFunction)pyrf_evsel__open,
832 .ml_flags = METH_VARARGS | METH_KEYWORDS,
833 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
834 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200835 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200836};
837
838static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
839
840static PyTypeObject pyrf_evsel__type = {
841 PyVarObject_HEAD_INIT(NULL, 0)
842 .tp_name = "perf.evsel",
843 .tp_basicsize = sizeof(struct pyrf_evsel),
844 .tp_dealloc = (destructor)pyrf_evsel__delete,
845 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
846 .tp_doc = pyrf_evsel__doc,
847 .tp_methods = pyrf_evsel__methods,
848 .tp_init = (initproc)pyrf_evsel__init,
849};
850
851static int pyrf_evsel__setup_types(void)
852{
853 pyrf_evsel__type.tp_new = PyType_GenericNew;
854 return PyType_Ready(&pyrf_evsel__type);
855}
856
857struct pyrf_evlist {
858 PyObject_HEAD
859
860 struct perf_evlist evlist;
861};
862
863static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300864 PyObject *args, PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200865{
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200866 PyObject *pcpus = NULL, *pthreads = NULL;
867 struct cpu_map *cpus;
868 struct thread_map *threads;
869
870 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
871 return -1;
872
873 threads = ((struct pyrf_thread_map *)pthreads)->threads;
874 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
875 perf_evlist__init(&pevlist->evlist, cpus, threads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200876 return 0;
877}
878
879static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
880{
881 perf_evlist__exit(&pevlist->evlist);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100882 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200883}
884
885static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
886 PyObject *args, PyObject *kwargs)
887{
888 struct perf_evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200889 static char *kwlist[] = { "pages", "overwrite", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200890 int pages = 128, overwrite = false;
891
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200892 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
893 &pages, &overwrite))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200894 return NULL;
895
Wang Nanf74b9d3a2017-12-03 02:00:37 +0000896 if (perf_evlist__mmap(evlist, pages) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200897 PyErr_SetFromErrno(PyExc_OSError);
898 return NULL;
899 }
900
901 Py_INCREF(Py_None);
902 return Py_None;
903}
904
905static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
906 PyObject *args, PyObject *kwargs)
907{
908 struct perf_evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200909 static char *kwlist[] = { "timeout", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200910 int timeout = -1, n;
911
912 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
913 return NULL;
914
Arnaldo Carvalho de Melof66a889d2014-08-18 17:25:59 -0300915 n = perf_evlist__poll(evlist, timeout);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200916 if (n < 0) {
917 PyErr_SetFromErrno(PyExc_OSError);
918 return NULL;
919 }
920
921 return Py_BuildValue("i", n);
922}
923
924static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300925 PyObject *args __maybe_unused,
926 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200927{
928 struct perf_evlist *evlist = &pevlist->evlist;
929 PyObject *list = PyList_New(0);
930 int i;
931
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300932 for (i = 0; i < evlist->pollfd.nr; ++i) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200933 PyObject *file;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100934#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo1b853372014-09-03 18:02:59 -0300935 FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200936
937 if (fp == NULL)
938 goto free_list;
939
940 file = PyFile_FromFile(fp, "perf", "r", NULL);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100941#else
Jiri Olsaa389aec2018-12-26 12:21:21 +0100942 file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1,
943 NULL, NULL, NULL, 0);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100944#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200945 if (file == NULL)
946 goto free_list;
947
948 if (PyList_Append(list, file) != 0) {
949 Py_DECREF(file);
950 goto free_list;
951 }
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -0300952
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200953 Py_DECREF(file);
954 }
955
956 return list;
957free_list:
958 return PyErr_NoMemory();
959}
960
961
962static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300963 PyObject *args,
964 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200965{
966 struct perf_evlist *evlist = &pevlist->evlist;
967 PyObject *pevsel;
968 struct perf_evsel *evsel;
969
970 if (!PyArg_ParseTuple(args, "O", &pevsel))
971 return NULL;
972
973 Py_INCREF(pevsel);
974 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
975 evsel->idx = evlist->nr_entries;
976 perf_evlist__add(evlist, evsel);
977
978 return Py_BuildValue("i", evlist->nr_entries);
979}
980
Jiri Olsa721f0df2018-08-17 13:45:56 +0200981static struct perf_mmap *get_md(struct perf_evlist *evlist, int cpu)
982{
983 int i;
984
985 for (i = 0; i < evlist->nr_mmaps; i++) {
986 struct perf_mmap *md = &evlist->mmap[i];
987
988 if (md->cpu == cpu)
989 return md;
990 }
991
992 return NULL;
993}
994
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200995static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
996 PyObject *args, PyObject *kwargs)
997{
998 struct perf_evlist *evlist = &pevlist->evlist;
999 union perf_event *event;
1000 int sample_id_all = 1, cpu;
Frederic Weisbecker64348152011-03-31 18:27:43 +02001001 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
Kan Liang35b7cdc2018-03-01 18:09:00 -05001002 struct perf_mmap *md;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001003 int err;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001004
1005 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1006 &cpu, &sample_id_all))
1007 return NULL;
1008
Jiri Olsa721f0df2018-08-17 13:45:56 +02001009 md = get_md(evlist, cpu);
1010 if (!md)
1011 return NULL;
1012
Kan Liangb9bae2c2018-03-06 10:36:07 -05001013 if (perf_mmap__read_init(md) < 0)
Kan Liang35b7cdc2018-03-01 18:09:00 -05001014 goto end;
1015
Kan Liang0019dc872018-03-06 10:36:06 -05001016 event = perf_mmap__read_event(md);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001017 if (event != NULL) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001018 PyObject *pyevent = pyrf_event__new(event);
1019 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
Jiri Olsa377f6982016-07-10 13:07:59 +02001020 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001021
1022 if (pyevent == NULL)
1023 return PyErr_NoMemory();
1024
Jiri Olsa377f6982016-07-10 13:07:59 +02001025 evsel = perf_evlist__event2evsel(evlist, event);
Petr Machata83428f22018-03-22 00:57:32 +01001026 if (!evsel) {
1027 Py_INCREF(Py_None);
Jiri Olsa377f6982016-07-10 13:07:59 +02001028 return Py_None;
Petr Machata83428f22018-03-22 00:57:32 +01001029 }
Jiri Olsa377f6982016-07-10 13:07:59 +02001030
1031 pevent->evsel = evsel;
1032
1033 err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
Jiri Olsae8968e62016-07-10 13:07:56 +02001034
1035 /* Consume the even only after we parsed it out. */
Kan Liangd6ace3d2018-03-06 10:36:05 -05001036 perf_mmap__consume(md);
Jiri Olsae8968e62016-07-10 13:07:56 +02001037
Arnaldo Carvalho de Melo5c6970a2011-06-02 10:55:10 -03001038 if (err)
1039 return PyErr_Format(PyExc_OSError,
1040 "perf: can't parse sample, err=%d", err);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001041 return pyevent;
1042 }
Kan Liang35b7cdc2018-03-01 18:09:00 -05001043end:
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001044 Py_INCREF(Py_None);
1045 return Py_None;
1046}
1047
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001048static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1049 PyObject *args, PyObject *kwargs)
1050{
1051 struct perf_evlist *evlist = &pevlist->evlist;
1052 int group = 0;
1053 static char *kwlist[] = { "group", NULL };
1054
1055 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1056 return NULL;
1057
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001058 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -03001059 perf_evlist__set_leader(evlist);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001060
1061 if (perf_evlist__open(evlist) < 0) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001062 PyErr_SetFromErrno(PyExc_OSError);
1063 return NULL;
1064 }
1065
1066 Py_INCREF(Py_None);
1067 return Py_None;
1068}
1069
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001070static PyMethodDef pyrf_evlist__methods[] = {
1071 {
1072 .ml_name = "mmap",
1073 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1074 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1075 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1076 },
1077 {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001078 .ml_name = "open",
1079 .ml_meth = (PyCFunction)pyrf_evlist__open,
1080 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1081 .ml_doc = PyDoc_STR("open the file descriptors.")
1082 },
1083 {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001084 .ml_name = "poll",
1085 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1086 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1087 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1088 },
1089 {
1090 .ml_name = "get_pollfd",
1091 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1092 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1093 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1094 },
1095 {
1096 .ml_name = "add",
1097 .ml_meth = (PyCFunction)pyrf_evlist__add,
1098 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1099 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1100 },
1101 {
1102 .ml_name = "read_on_cpu",
1103 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1104 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1105 .ml_doc = PyDoc_STR("reads an event.")
1106 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001107 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001108};
1109
1110static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1111{
1112 struct pyrf_evlist *pevlist = (void *)obj;
1113
1114 return pevlist->evlist.nr_entries;
1115}
1116
1117static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1118{
1119 struct pyrf_evlist *pevlist = (void *)obj;
1120 struct perf_evsel *pos;
1121
1122 if (i >= pevlist->evlist.nr_entries)
1123 return NULL;
1124
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001125 evlist__for_each_entry(&pevlist->evlist, pos) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001126 if (i-- == 0)
1127 break;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001128 }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001129
1130 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1131}
1132
1133static PySequenceMethods pyrf_evlist__sequence_methods = {
1134 .sq_length = pyrf_evlist__length,
1135 .sq_item = pyrf_evlist__item,
1136};
1137
1138static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1139
1140static PyTypeObject pyrf_evlist__type = {
1141 PyVarObject_HEAD_INIT(NULL, 0)
1142 .tp_name = "perf.evlist",
1143 .tp_basicsize = sizeof(struct pyrf_evlist),
1144 .tp_dealloc = (destructor)pyrf_evlist__delete,
1145 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1146 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1147 .tp_doc = pyrf_evlist__doc,
1148 .tp_methods = pyrf_evlist__methods,
1149 .tp_init = (initproc)pyrf_evlist__init,
1150};
1151
1152static int pyrf_evlist__setup_types(void)
1153{
1154 pyrf_evlist__type.tp_new = PyType_GenericNew;
1155 return PyType_Ready(&pyrf_evlist__type);
1156}
1157
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001158#define PERF_CONST(name) { #name, PERF_##name }
1159
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001160static struct {
1161 const char *name;
1162 int value;
1163} perf__constants[] = {
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001164 PERF_CONST(TYPE_HARDWARE),
1165 PERF_CONST(TYPE_SOFTWARE),
1166 PERF_CONST(TYPE_TRACEPOINT),
1167 PERF_CONST(TYPE_HW_CACHE),
1168 PERF_CONST(TYPE_RAW),
1169 PERF_CONST(TYPE_BREAKPOINT),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001170
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001171 PERF_CONST(COUNT_HW_CPU_CYCLES),
1172 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1173 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1174 PERF_CONST(COUNT_HW_CACHE_MISSES),
1175 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1176 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1177 PERF_CONST(COUNT_HW_BUS_CYCLES),
1178 PERF_CONST(COUNT_HW_CACHE_L1D),
1179 PERF_CONST(COUNT_HW_CACHE_L1I),
1180 PERF_CONST(COUNT_HW_CACHE_LL),
1181 PERF_CONST(COUNT_HW_CACHE_DTLB),
1182 PERF_CONST(COUNT_HW_CACHE_ITLB),
1183 PERF_CONST(COUNT_HW_CACHE_BPU),
1184 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1185 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1186 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1187 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1188 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001189
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001190 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1191 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
Ingo Molnar129c04c2011-04-29 14:41:28 +02001192
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001193 PERF_CONST(COUNT_SW_CPU_CLOCK),
1194 PERF_CONST(COUNT_SW_TASK_CLOCK),
1195 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1196 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1197 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1198 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1199 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1200 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1201 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1202 PERF_CONST(COUNT_SW_DUMMY),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001203
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001204 PERF_CONST(SAMPLE_IP),
1205 PERF_CONST(SAMPLE_TID),
1206 PERF_CONST(SAMPLE_TIME),
1207 PERF_CONST(SAMPLE_ADDR),
1208 PERF_CONST(SAMPLE_READ),
1209 PERF_CONST(SAMPLE_CALLCHAIN),
1210 PERF_CONST(SAMPLE_ID),
1211 PERF_CONST(SAMPLE_CPU),
1212 PERF_CONST(SAMPLE_PERIOD),
1213 PERF_CONST(SAMPLE_STREAM_ID),
1214 PERF_CONST(SAMPLE_RAW),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001215
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001216 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1217 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1218 PERF_CONST(FORMAT_ID),
1219 PERF_CONST(FORMAT_GROUP),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001220
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001221 PERF_CONST(RECORD_MMAP),
1222 PERF_CONST(RECORD_LOST),
1223 PERF_CONST(RECORD_COMM),
1224 PERF_CONST(RECORD_EXIT),
1225 PERF_CONST(RECORD_THROTTLE),
1226 PERF_CONST(RECORD_UNTHROTTLE),
1227 PERF_CONST(RECORD_FORK),
1228 PERF_CONST(RECORD_READ),
1229 PERF_CONST(RECORD_SAMPLE),
Arnaldo Carvalho de Melo84576da2015-07-24 13:04:09 -03001230 PERF_CONST(RECORD_MMAP2),
1231 PERF_CONST(RECORD_AUX),
1232 PERF_CONST(RECORD_ITRACE_START),
1233 PERF_CONST(RECORD_LOST_SAMPLES),
1234 PERF_CONST(RECORD_SWITCH),
1235 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -03001236
1237 PERF_CONST(RECORD_MISC_SWITCH_OUT),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001238 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001239};
1240
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001241static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1242 PyObject *args, PyObject *kwargs)
1243{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001244 struct tep_event *tp_format;
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001245 static char *kwlist[] = { "sys", "name", NULL };
1246 char *sys = NULL;
1247 char *name = NULL;
1248
1249 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1250 &sys, &name))
1251 return NULL;
1252
1253 tp_format = trace_event__tp_format(sys, name);
1254 if (IS_ERR(tp_format))
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001255 return _PyLong_FromLong(-1);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001256
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001257 return _PyLong_FromLong(tp_format->id);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001258}
1259
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001260static PyMethodDef perf__methods[] = {
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001261 {
1262 .ml_name = "tracepoint",
1263 .ml_meth = (PyCFunction) pyrf__tracepoint,
1264 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1265 .ml_doc = PyDoc_STR("Get tracepoint config.")
1266 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001267 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001268};
1269
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001270#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001271PyMODINIT_FUNC initperf(void)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001272#else
1273PyMODINIT_FUNC PyInit_perf(void)
1274#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001275{
1276 PyObject *obj;
1277 int i;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001278 PyObject *dict;
1279#if PY_MAJOR_VERSION < 3
1280 PyObject *module = Py_InitModule("perf", perf__methods);
1281#else
1282 static struct PyModuleDef moduledef = {
1283 PyModuleDef_HEAD_INIT,
1284 "perf", /* m_name */
1285 "", /* m_doc */
1286 -1, /* m_size */
1287 perf__methods, /* m_methods */
1288 NULL, /* m_reload */
1289 NULL, /* m_traverse */
1290 NULL, /* m_clear */
1291 NULL, /* m_free */
1292 };
1293 PyObject *module = PyModule_Create(&moduledef);
1294#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001295
1296 if (module == NULL ||
1297 pyrf_event__setup_types() < 0 ||
1298 pyrf_evlist__setup_types() < 0 ||
1299 pyrf_evsel__setup_types() < 0 ||
1300 pyrf_thread_map__setup_types() < 0 ||
1301 pyrf_cpu_map__setup_types() < 0)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001302#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001303 return;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001304#else
1305 return module;
1306#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001307
Jiri Olsa918512b2013-09-12 18:39:35 +02001308 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0da2e9c2012-10-16 14:51:04 -03001309 page_size = sysconf(_SC_PAGE_SIZE);
1310
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001311 Py_INCREF(&pyrf_evlist__type);
1312 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1313
1314 Py_INCREF(&pyrf_evsel__type);
1315 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1316
Jiri Olsa85e37de2016-07-10 13:07:57 +02001317 Py_INCREF(&pyrf_mmap_event__type);
1318 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1319
1320 Py_INCREF(&pyrf_lost_event__type);
1321 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1322
1323 Py_INCREF(&pyrf_comm_event__type);
1324 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1325
1326 Py_INCREF(&pyrf_task_event__type);
1327 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1328
1329 Py_INCREF(&pyrf_throttle_event__type);
1330 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_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_read_event__type);
1336 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1337
1338 Py_INCREF(&pyrf_sample_event__type);
1339 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1340
1341 Py_INCREF(&pyrf_context_switch_event__type);
1342 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1343
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001344 Py_INCREF(&pyrf_thread_map__type);
1345 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1346
1347 Py_INCREF(&pyrf_cpu_map__type);
1348 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1349
1350 dict = PyModule_GetDict(module);
1351 if (dict == NULL)
1352 goto error;
1353
1354 for (i = 0; perf__constants[i].name != NULL; i++) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001355 obj = _PyLong_FromLong(perf__constants[i].value);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001356 if (obj == NULL)
1357 goto error;
1358 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1359 Py_DECREF(obj);
1360 }
1361
1362error:
1363 if (PyErr_Occurred())
1364 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001365#if PY_MAJOR_VERSION >= 3
1366 return module;
1367#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001368}
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -03001369
1370/*
1371 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1372 * binding.
1373 */
1374void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
1375 int fd, int group_fd, unsigned long flags)
1376{
1377}