blob: 278abecb5bdfc0d23c39da1fc5733b22e4da06fc [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"
Namhyung Kimb214ba82020-09-24 21:44:53 +090018#include "stat.h"
19#include "metricgroup.h"
Arnaldo Carvalho de Melo9db0e362019-09-30 11:48:32 -030020#include "util/env.h"
Jiri Olsa20f2be12019-08-06 15:25:25 +020021#include <internal/lib.h>
Jiri Olsa8366f0d2020-08-27 21:32:01 +020022#include "util.h"
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -020023
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +010024#if PY_MAJOR_VERSION < 3
25#define _PyUnicode_FromString(arg) \
26 PyString_FromString(arg)
27#define _PyUnicode_AsString(arg) \
28 PyString_AsString(arg)
29#define _PyUnicode_FromFormat(...) \
30 PyString_FromFormat(__VA_ARGS__)
31#define _PyLong_FromLong(arg) \
32 PyInt_FromLong(arg)
33
34#else
35
36#define _PyUnicode_FromString(arg) \
37 PyUnicode_FromString(arg)
38#define _PyUnicode_FromFormat(...) \
39 PyUnicode_FromFormat(__VA_ARGS__)
40#define _PyLong_FromLong(arg) \
41 PyLong_FromLong(arg)
42#endif
43
44#ifndef Py_TYPE
45#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
46#endif
47
Adrian Hunter8afb4c02013-08-14 15:48:23 +030048/*
Arnaldo Carvalho de Melo56e2e052017-04-19 18:38:33 -030049 * Provide these two so that we don't have to link against callchain.c and
50 * start dragging hist.c, etc.
51 */
52struct callchain_param callchain_param;
53
54int parse_callchain_record(const char *arg __maybe_unused,
55 struct callchain_param *param __maybe_unused)
56{
57 return 0;
58}
59
60/*
Arnaldo Carvalho de Melo9db0e362019-09-30 11:48:32 -030061 * Add this one here not to drag util/env.c
62 */
63struct perf_env perf_env;
64
65/*
Namhyung Kimb214ba82020-09-24 21:44:53 +090066 * Add this one here not to drag util/stat-shadow.c
67 */
68void perf_stat__collect_metric_expr(struct evlist *evsel_list)
69{
70}
71
72/*
73 * Add this one here not to drag util/metricgroup.c
74 */
75int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
76 struct rblist *new_metric_events,
77 struct rblist *old_metric_events)
78{
79 return 0;
80}
81
82/*
Song Liufa853c42020-12-29 13:42:14 -080083 * XXX: All these evsel destructors need some better mechanism, like a linked
84 * list of destructors registered when the relevant code indeed is used instead
85 * of having more and more calls in perf_evsel__delete(). -- acme
86 *
87 * For now, add some more:
88 *
89 * Not to drag the BPF bandwagon...
90 */
91void bpf_counter__destroy(struct evsel *evsel);
92int bpf_counter__install_pe(struct evsel *evsel, int cpu, int fd);
93
94void bpf_counter__destroy(struct evsel *evsel __maybe_unused)
95{
96}
97
98int bpf_counter__install_pe(struct evsel *evsel __maybe_unused, int cpu __maybe_unused, int fd __maybe_unused)
99{
100 return 0;
101}
102
103/*
Adrian Hunter8afb4c02013-08-14 15:48:23 +0300104 * Support debug printing even though util/debug.c is not linked. That means
105 * implementing 'verbose' and 'eprintf'.
106 */
107int verbose;
Ravi Bangoriaccd26742019-11-08 15:11:28 +0530108int debug_peo_args;
Adrian Hunter8afb4c02013-08-14 15:48:23 +0300109
Arnaldo Carvalho de Melo5939cac2019-09-11 12:54:33 +0100110int eprintf(int level, int var, const char *fmt, ...);
111
Jiri Olsac95688a2014-07-14 23:46:49 +0200112int eprintf(int level, int var, const char *fmt, ...)
Adrian Hunter8afb4c02013-08-14 15:48:23 +0300113{
114 va_list args;
115 int ret = 0;
116
Jiri Olsac95688a2014-07-14 23:46:49 +0200117 if (var >= level) {
Adrian Hunter8afb4c02013-08-14 15:48:23 +0300118 va_start(args, fmt);
119 ret = vfprintf(stderr, fmt, args);
120 va_end(args);
121 }
122
123 return ret;
124}
125
Frederic Weisbeckercfff2d92011-02-25 21:30:16 +0100126/* Define PyVarObject_HEAD_INIT for python 2.5 */
127#ifndef PyVarObject_HEAD_INIT
128# define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
129#endif
130
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100131#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200132PyMODINIT_FUNC initperf(void);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100133#else
134PyMODINIT_FUNC PyInit_perf(void);
135#endif
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200136
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200137#define member_def(type, member, ptype, help) \
138 { #member, ptype, \
139 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
140 0, help }
141
142#define sample_member_def(name, member, ptype, help) \
143 { #name, ptype, \
144 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
145 0, help }
146
147struct pyrf_event {
148 PyObject_HEAD
Jiri Olsa32dcd022019-07-21 13:23:51 +0200149 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200150 struct perf_sample sample;
151 union perf_event event;
152};
153
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200154#define sample_members \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200155 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200156 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
157 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200158 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
159 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
160 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
161 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
162 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200163 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
164
165static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
166
167static PyMemberDef pyrf_mmap_event__members[] = {
168 sample_members
169 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300170 member_def(perf_event_header, misc, T_UINT, "event misc"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300171 member_def(perf_record_mmap, pid, T_UINT, "event pid"),
172 member_def(perf_record_mmap, tid, T_UINT, "event tid"),
173 member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
174 member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
175 member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
176 member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200177 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200178};
179
180static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
181{
182 PyObject *ret;
183 char *s;
184
Jiri Olsa1345e2e2019-08-25 20:17:41 +0200185 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
186 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200187 "filename: %s }",
188 pevent->event.mmap.pid, pevent->event.mmap.tid,
189 pevent->event.mmap.start, pevent->event.mmap.len,
190 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
191 ret = PyErr_NoMemory();
192 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100193 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200194 free(s);
195 }
196 return ret;
197}
198
199static PyTypeObject pyrf_mmap_event__type = {
200 PyVarObject_HEAD_INIT(NULL, 0)
201 .tp_name = "perf.mmap_event",
202 .tp_basicsize = sizeof(struct pyrf_event),
203 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
204 .tp_doc = pyrf_mmap_event__doc,
205 .tp_members = pyrf_mmap_event__members,
206 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
207};
208
209static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
210
211static PyMemberDef pyrf_task_event__members[] = {
212 sample_members
213 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300214 member_def(perf_record_fork, pid, T_UINT, "event pid"),
215 member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
216 member_def(perf_record_fork, tid, T_UINT, "event tid"),
217 member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
218 member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200219 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200220};
221
222static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
223{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100224 return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
Jiri Olsabceb59b2019-08-25 20:17:45 +0200225 "ptid: %u, time: %" PRI_lu64 "}",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200226 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
227 pevent->event.fork.pid,
228 pevent->event.fork.ppid,
229 pevent->event.fork.tid,
230 pevent->event.fork.ptid,
231 pevent->event.fork.time);
232}
233
234static PyTypeObject pyrf_task_event__type = {
235 PyVarObject_HEAD_INIT(NULL, 0)
236 .tp_name = "perf.task_event",
237 .tp_basicsize = sizeof(struct pyrf_event),
238 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
239 .tp_doc = pyrf_task_event__doc,
240 .tp_members = pyrf_task_event__members,
241 .tp_repr = (reprfunc)pyrf_task_event__repr,
242};
243
244static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
245
246static PyMemberDef pyrf_comm_event__members[] = {
247 sample_members
248 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300249 member_def(perf_record_comm, pid, T_UINT, "event pid"),
250 member_def(perf_record_comm, tid, T_UINT, "event tid"),
251 member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200252 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200253};
254
255static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
256{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100257 return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200258 pevent->event.comm.pid,
259 pevent->event.comm.tid,
260 pevent->event.comm.comm);
261}
262
263static PyTypeObject pyrf_comm_event__type = {
264 PyVarObject_HEAD_INIT(NULL, 0)
265 .tp_name = "perf.comm_event",
266 .tp_basicsize = sizeof(struct pyrf_event),
267 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
268 .tp_doc = pyrf_comm_event__doc,
269 .tp_members = pyrf_comm_event__members,
270 .tp_repr = (reprfunc)pyrf_comm_event__repr,
271};
272
273static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
274
275static PyMemberDef pyrf_throttle_event__members[] = {
276 sample_members
277 member_def(perf_event_header, type, T_UINT, "event type"),
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300278 member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
279 member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
280 member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200281 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200282};
283
284static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
285{
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300286 struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200287
Jiri Olsa003c66f2019-08-25 20:17:49 +0200288 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
289 ", stream_id: %" PRI_lu64 " }",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200290 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
291 te->time, te->id, te->stream_id);
292}
293
294static PyTypeObject pyrf_throttle_event__type = {
295 PyVarObject_HEAD_INIT(NULL, 0)
296 .tp_name = "perf.throttle_event",
297 .tp_basicsize = sizeof(struct pyrf_event),
298 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
299 .tp_doc = pyrf_throttle_event__doc,
300 .tp_members = pyrf_throttle_event__members,
301 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
302};
303
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300304static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
305
306static PyMemberDef pyrf_lost_event__members[] = {
307 sample_members
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300308 member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
309 member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300310 { .name = NULL, },
311};
312
313static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
314{
315 PyObject *ret;
316 char *s;
317
Jiri Olsa5290ed62019-08-25 20:17:46 +0200318 if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
319 "lost: %#" PRI_lx64 " }",
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300320 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
321 ret = PyErr_NoMemory();
322 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100323 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300324 free(s);
325 }
326 return ret;
327}
328
329static PyTypeObject pyrf_lost_event__type = {
330 PyVarObject_HEAD_INIT(NULL, 0)
331 .tp_name = "perf.lost_event",
332 .tp_basicsize = sizeof(struct pyrf_event),
333 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
334 .tp_doc = pyrf_lost_event__doc,
335 .tp_members = pyrf_lost_event__members,
336 .tp_repr = (reprfunc)pyrf_lost_event__repr,
337};
338
339static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
340
341static PyMemberDef pyrf_read_event__members[] = {
342 sample_members
Arnaldo Carvalho de Melo69d81f02019-08-26 19:02:31 -0300343 member_def(perf_record_read, pid, T_UINT, "event pid"),
344 member_def(perf_record_read, tid, T_UINT, "event tid"),
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300345 { .name = NULL, },
346};
347
348static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
349{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100350 return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300351 pevent->event.read.pid,
352 pevent->event.read.tid);
353 /*
354 * FIXME: return the array of read values,
355 * making this method useful ;-)
356 */
357}
358
359static PyTypeObject pyrf_read_event__type = {
360 PyVarObject_HEAD_INIT(NULL, 0)
361 .tp_name = "perf.read_event",
362 .tp_basicsize = sizeof(struct pyrf_event),
363 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
364 .tp_doc = pyrf_read_event__doc,
365 .tp_members = pyrf_read_event__members,
366 .tp_repr = (reprfunc)pyrf_read_event__repr,
367};
368
369static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
370
371static PyMemberDef pyrf_sample_event__members[] = {
372 sample_members
373 member_def(perf_event_header, type, T_UINT, "event type"),
374 { .name = NULL, },
375};
376
377static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
378{
379 PyObject *ret;
380 char *s;
381
382 if (asprintf(&s, "{ type: sample }") < 0) {
383 ret = PyErr_NoMemory();
384 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100385 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300386 free(s);
387 }
388 return ret;
389}
390
Jiri Olsabae57e32016-07-10 13:08:00 +0200391static bool is_tracepoint(struct pyrf_event *pevent)
392{
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200393 return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
Jiri Olsabae57e32016-07-10 13:08:00 +0200394}
395
Jiri Olsabae57e32016-07-10 13:08:00 +0200396static PyObject*
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400397tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
Jiri Olsabae57e32016-07-10 13:08:00 +0200398{
Tzvetomir Stoyanov69769ce2019-04-01 12:43:18 -0400399 struct tep_handle *pevent = field->event->tep;
Jiri Olsabae57e32016-07-10 13:08:00 +0200400 void *data = pe->sample.raw_data;
401 PyObject *ret = NULL;
402 unsigned long long val;
403 unsigned int offset, len;
404
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400405 if (field->flags & TEP_FIELD_IS_ARRAY) {
Jiri Olsabae57e32016-07-10 13:08:00 +0200406 offset = field->offset;
407 len = field->size;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400408 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400409 val = tep_read_number(pevent, data + offset, len);
Jiri Olsabae57e32016-07-10 13:08:00 +0200410 offset = val;
411 len = offset >> 16;
412 offset &= 0xffff;
413 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400414 if (field->flags & TEP_FIELD_IS_STRING &&
Jiri Olsabae57e32016-07-10 13:08:00 +0200415 is_printable_array(data + offset, len)) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100416 ret = _PyUnicode_FromString((char *)data + offset);
Jiri Olsabae57e32016-07-10 13:08:00 +0200417 } else {
418 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400419 field->flags &= ~TEP_FIELD_IS_STRING;
Jiri Olsabae57e32016-07-10 13:08:00 +0200420 }
421 } else {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -0400422 val = tep_read_number(pevent, data + field->offset,
423 field->size);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400424 if (field->flags & TEP_FIELD_IS_POINTER)
Jiri Olsabae57e32016-07-10 13:08:00 +0200425 ret = PyLong_FromUnsignedLong((unsigned long) val);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -0400426 else if (field->flags & TEP_FIELD_IS_SIGNED)
Jiri Olsabae57e32016-07-10 13:08:00 +0200427 ret = PyLong_FromLong((long) val);
428 else
429 ret = PyLong_FromUnsignedLong((unsigned long) val);
430 }
431
432 return ret;
433}
434
435static PyObject*
436get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
437{
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100438 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
Jiri Olsa32dcd022019-07-21 13:23:51 +0200439 struct evsel *evsel = pevent->evsel;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -0400440 struct tep_format_field *field;
Jiri Olsabae57e32016-07-10 13:08:00 +0200441
442 if (!evsel->tp_format) {
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500443 struct tep_event *tp_format;
Jiri Olsabae57e32016-07-10 13:08:00 +0200444
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200445 tp_format = trace_event__tp_format_id(evsel->core.attr.config);
Jiri Olsabae57e32016-07-10 13:08:00 +0200446 if (!tp_format)
447 return NULL;
448
449 evsel->tp_format = tp_format;
450 }
451
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -0400452 field = tep_find_any_field(evsel->tp_format, str);
Jiri Olsabae57e32016-07-10 13:08:00 +0200453 if (!field)
454 return NULL;
455
456 return tracepoint_field(pevent, field);
457}
458
459static PyObject*
460pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
461{
462 PyObject *obj = NULL;
463
464 if (is_tracepoint(pevent))
465 obj = get_tracepoint_field(pevent, attr_name);
466
467 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
468}
469
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300470static PyTypeObject pyrf_sample_event__type = {
471 PyVarObject_HEAD_INIT(NULL, 0)
472 .tp_name = "perf.sample_event",
473 .tp_basicsize = sizeof(struct pyrf_event),
474 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
475 .tp_doc = pyrf_sample_event__doc,
476 .tp_members = pyrf_sample_event__members,
477 .tp_repr = (reprfunc)pyrf_sample_event__repr,
Jiri Olsabae57e32016-07-10 13:08:00 +0200478 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300479};
480
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300481static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
482
483static PyMemberDef pyrf_context_switch_event__members[] = {
484 sample_members
485 member_def(perf_event_header, type, T_UINT, "event type"),
Jiri Olsa72932372019-08-28 15:57:16 +0200486 member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
487 member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300488 { .name = NULL, },
489};
490
491static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
492{
493 PyObject *ret;
494 char *s;
495
496 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
497 pevent->event.context_switch.next_prev_pid,
498 pevent->event.context_switch.next_prev_tid,
499 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
500 ret = PyErr_NoMemory();
501 } else {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100502 ret = _PyUnicode_FromString(s);
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300503 free(s);
504 }
505 return ret;
506}
507
508static PyTypeObject pyrf_context_switch_event__type = {
509 PyVarObject_HEAD_INIT(NULL, 0)
510 .tp_name = "perf.context_switch_event",
511 .tp_basicsize = sizeof(struct pyrf_event),
512 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
513 .tp_doc = pyrf_context_switch_event__doc,
514 .tp_members = pyrf_context_switch_event__members,
515 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
516};
517
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200518static int pyrf_event__setup_types(void)
519{
520 int err;
521 pyrf_mmap_event__type.tp_new =
522 pyrf_task_event__type.tp_new =
523 pyrf_comm_event__type.tp_new =
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300524 pyrf_lost_event__type.tp_new =
525 pyrf_read_event__type.tp_new =
526 pyrf_sample_event__type.tp_new =
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300527 pyrf_context_switch_event__type.tp_new =
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200528 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
529 err = PyType_Ready(&pyrf_mmap_event__type);
530 if (err < 0)
531 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300532 err = PyType_Ready(&pyrf_lost_event__type);
533 if (err < 0)
534 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200535 err = PyType_Ready(&pyrf_task_event__type);
536 if (err < 0)
537 goto out;
538 err = PyType_Ready(&pyrf_comm_event__type);
539 if (err < 0)
540 goto out;
541 err = PyType_Ready(&pyrf_throttle_event__type);
542 if (err < 0)
543 goto out;
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300544 err = PyType_Ready(&pyrf_read_event__type);
545 if (err < 0)
546 goto out;
547 err = PyType_Ready(&pyrf_sample_event__type);
548 if (err < 0)
549 goto out;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300550 err = PyType_Ready(&pyrf_context_switch_event__type);
551 if (err < 0)
552 goto out;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200553out:
554 return err;
555}
556
557static PyTypeObject *pyrf_event__type[] = {
558 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300559 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200560 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
561 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
562 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
563 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
564 [PERF_RECORD_FORK] = &pyrf_task_event__type,
Arnaldo Carvalho de Melo3e9f45a72011-07-25 17:13:27 -0300565 [PERF_RECORD_READ] = &pyrf_read_event__type,
566 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300567 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
568 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200569};
570
571static PyObject *pyrf_event__new(union perf_event *event)
572{
573 struct pyrf_event *pevent;
574 PyTypeObject *ptype;
575
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300576 if ((event->header.type < PERF_RECORD_MMAP ||
577 event->header.type > PERF_RECORD_SAMPLE) &&
578 !(event->header.type == PERF_RECORD_SWITCH ||
579 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200580 return NULL;
581
582 ptype = pyrf_event__type[event->header.type];
583 pevent = PyObject_New(struct pyrf_event, ptype);
584 if (pevent != NULL)
585 memcpy(&pevent->event, event, event->header.size);
586 return (PyObject *)pevent;
587}
588
589struct pyrf_cpu_map {
590 PyObject_HEAD
591
Jiri Olsaf8548392019-07-21 13:23:49 +0200592 struct perf_cpu_map *cpus;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200593};
594
595static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
596 PyObject *args, PyObject *kwargs)
597{
Frederic Weisbecker64348152011-03-31 18:27:43 +0200598 static char *kwlist[] = { "cpustr", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200599 char *cpustr = NULL;
600
601 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
602 kwlist, &cpustr))
603 return -1;
604
Jiri Olsa9c3516d2019-07-21 13:24:30 +0200605 pcpus->cpus = perf_cpu_map__new(cpustr);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200606 if (pcpus->cpus == NULL)
607 return -1;
608 return 0;
609}
610
611static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
612{
Jiri Olsa38f01d82019-07-21 13:24:17 +0200613 perf_cpu_map__put(pcpus->cpus);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100614 Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200615}
616
617static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
618{
619 struct pyrf_cpu_map *pcpus = (void *)obj;
620
621 return pcpus->cpus->nr;
622}
623
624static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
625{
626 struct pyrf_cpu_map *pcpus = (void *)obj;
627
628 if (i >= pcpus->cpus->nr)
629 return NULL;
630
631 return Py_BuildValue("i", pcpus->cpus->map[i]);
632}
633
634static PySequenceMethods pyrf_cpu_map__sequence_methods = {
635 .sq_length = pyrf_cpu_map__length,
636 .sq_item = pyrf_cpu_map__item,
637};
638
639static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
640
641static PyTypeObject pyrf_cpu_map__type = {
642 PyVarObject_HEAD_INIT(NULL, 0)
643 .tp_name = "perf.cpu_map",
644 .tp_basicsize = sizeof(struct pyrf_cpu_map),
645 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
646 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
647 .tp_doc = pyrf_cpu_map__doc,
648 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
649 .tp_init = (initproc)pyrf_cpu_map__init,
650};
651
652static int pyrf_cpu_map__setup_types(void)
653{
654 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
655 return PyType_Ready(&pyrf_cpu_map__type);
656}
657
658struct pyrf_thread_map {
659 PyObject_HEAD
660
Jiri Olsa9749b902019-07-21 13:23:50 +0200661 struct perf_thread_map *threads;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200662};
663
664static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
665 PyObject *args, PyObject *kwargs)
666{
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200667 static char *kwlist[] = { "pid", "tid", "uid", NULL };
668 int pid = -1, tid = -1, uid = UINT_MAX;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200669
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200670 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
671 kwlist, &pid, &tid, &uid))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200672 return -1;
673
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200674 pthreads->threads = thread_map__new(pid, tid, uid);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200675 if (pthreads->threads == NULL)
676 return -1;
677 return 0;
678}
679
680static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
681{
Jiri Olsa7836e522019-07-21 13:24:20 +0200682 perf_thread_map__put(pthreads->threads);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100683 Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200684}
685
686static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
687{
688 struct pyrf_thread_map *pthreads = (void *)obj;
689
690 return pthreads->threads->nr;
691}
692
693static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
694{
695 struct pyrf_thread_map *pthreads = (void *)obj;
696
697 if (i >= pthreads->threads->nr)
698 return NULL;
699
700 return Py_BuildValue("i", pthreads->threads->map[i]);
701}
702
703static PySequenceMethods pyrf_thread_map__sequence_methods = {
704 .sq_length = pyrf_thread_map__length,
705 .sq_item = pyrf_thread_map__item,
706};
707
708static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
709
710static PyTypeObject pyrf_thread_map__type = {
711 PyVarObject_HEAD_INIT(NULL, 0)
712 .tp_name = "perf.thread_map",
713 .tp_basicsize = sizeof(struct pyrf_thread_map),
714 .tp_dealloc = (destructor)pyrf_thread_map__delete,
715 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
716 .tp_doc = pyrf_thread_map__doc,
717 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
718 .tp_init = (initproc)pyrf_thread_map__init,
719};
720
721static int pyrf_thread_map__setup_types(void)
722{
723 pyrf_thread_map__type.tp_new = PyType_GenericNew;
724 return PyType_Ready(&pyrf_thread_map__type);
725}
726
727struct pyrf_evsel {
728 PyObject_HEAD
729
Jiri Olsa32dcd022019-07-21 13:23:51 +0200730 struct evsel evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200731};
732
733static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
734 PyObject *args, PyObject *kwargs)
735{
736 struct perf_event_attr attr = {
737 .type = PERF_TYPE_HARDWARE,
738 .config = PERF_COUNT_HW_CPU_CYCLES,
739 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
740 };
741 static char *kwlist[] = {
742 "type",
743 "config",
744 "sample_freq",
745 "sample_period",
746 "sample_type",
747 "read_format",
748 "disabled",
749 "inherit",
750 "pinned",
751 "exclusive",
752 "exclude_user",
753 "exclude_kernel",
754 "exclude_hv",
755 "exclude_idle",
756 "mmap",
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300757 "context_switch",
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200758 "comm",
759 "freq",
760 "inherit_stat",
761 "enable_on_exec",
762 "task",
763 "watermark",
764 "precise_ip",
765 "mmap_data",
766 "sample_id_all",
767 "wakeup_events",
768 "bp_type",
769 "bp_addr",
Frederic Weisbecker64348152011-03-31 18:27:43 +0200770 "bp_len",
771 NULL
772 };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200773 u64 sample_period = 0;
774 u32 disabled = 0,
775 inherit = 0,
776 pinned = 0,
777 exclusive = 0,
778 exclude_user = 0,
779 exclude_kernel = 0,
780 exclude_hv = 0,
781 exclude_idle = 0,
782 mmap = 0,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300783 context_switch = 0,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200784 comm = 0,
785 freq = 1,
786 inherit_stat = 0,
787 enable_on_exec = 0,
788 task = 0,
789 watermark = 0,
790 precise_ip = 0,
791 mmap_data = 0,
792 sample_id_all = 1;
793 int idx = 0;
794
795 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300796 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200797 &attr.type, &attr.config, &attr.sample_freq,
798 &sample_period, &attr.sample_type,
799 &attr.read_format, &disabled, &inherit,
800 &pinned, &exclusive, &exclude_user,
801 &exclude_kernel, &exclude_hv, &exclude_idle,
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300802 &mmap, &context_switch, &comm, &freq, &inherit_stat,
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200803 &enable_on_exec, &task, &watermark,
804 &precise_ip, &mmap_data, &sample_id_all,
805 &attr.wakeup_events, &attr.bp_type,
806 &attr.bp_addr, &attr.bp_len, &idx))
807 return -1;
808
809 /* union... */
810 if (sample_period != 0) {
811 if (attr.sample_freq != 0)
812 return -1; /* FIXME: throw right exception */
813 attr.sample_period = sample_period;
814 }
815
816 /* Bitfields */
817 attr.disabled = disabled;
818 attr.inherit = inherit;
819 attr.pinned = pinned;
820 attr.exclusive = exclusive;
821 attr.exclude_user = exclude_user;
822 attr.exclude_kernel = exclude_kernel;
823 attr.exclude_hv = exclude_hv;
824 attr.exclude_idle = exclude_idle;
825 attr.mmap = mmap;
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -0300826 attr.context_switch = context_switch;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200827 attr.comm = comm;
828 attr.freq = freq;
829 attr.inherit_stat = inherit_stat;
830 attr.enable_on_exec = enable_on_exec;
831 attr.task = task;
832 attr.watermark = watermark;
833 attr.precise_ip = precise_ip;
834 attr.mmap_data = mmap_data;
835 attr.sample_id_all = sample_id_all;
Jiri Olsaad4e3c042016-07-10 13:07:55 +0200836 attr.size = sizeof(attr);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200837
Jiri Olsab4b62ee2019-07-21 13:23:53 +0200838 evsel__init(&pevsel->evsel, &attr, idx);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200839 return 0;
840}
841
842static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
843{
Arnaldo Carvalho de Melo30f7c592020-04-29 15:53:17 -0300844 evsel__exit(&pevsel->evsel);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100845 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200846}
847
848static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
849 PyObject *args, PyObject *kwargs)
850{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200851 struct evsel *evsel = &pevsel->evsel;
Jiri Olsaf8548392019-07-21 13:23:49 +0200852 struct perf_cpu_map *cpus = NULL;
Jiri Olsa9749b902019-07-21 13:23:50 +0200853 struct perf_thread_map *threads = NULL;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200854 PyObject *pcpus = NULL, *pthreads = NULL;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300855 int group = 0, inherit = 0;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200856 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200857
858 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300859 &pcpus, &pthreads, &group, &inherit))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200860 return NULL;
861
862 if (pthreads != NULL)
863 threads = ((struct pyrf_thread_map *)pthreads)->threads;
864
865 if (pcpus != NULL)
866 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
867
Jiri Olsa1fc632c2019-07-21 13:24:29 +0200868 evsel->core.attr.inherit = inherit;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200869 /*
870 * This will group just the fds for this single evsel, to group
871 * multiple events, use evlist.open().
872 */
Jiri Olsa5972d1e2019-07-21 13:24:01 +0200873 if (evsel__open(evsel, cpus, threads) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200874 PyErr_SetFromErrno(PyExc_OSError);
875 return NULL;
876 }
877
878 Py_INCREF(Py_None);
879 return Py_None;
880}
881
882static PyMethodDef pyrf_evsel__methods[] = {
883 {
884 .ml_name = "open",
885 .ml_meth = (PyCFunction)pyrf_evsel__open,
886 .ml_flags = METH_VARARGS | METH_KEYWORDS,
887 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
888 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -0200889 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200890};
891
892static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
893
894static PyTypeObject pyrf_evsel__type = {
895 PyVarObject_HEAD_INIT(NULL, 0)
896 .tp_name = "perf.evsel",
897 .tp_basicsize = sizeof(struct pyrf_evsel),
898 .tp_dealloc = (destructor)pyrf_evsel__delete,
899 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
900 .tp_doc = pyrf_evsel__doc,
901 .tp_methods = pyrf_evsel__methods,
902 .tp_init = (initproc)pyrf_evsel__init,
903};
904
905static int pyrf_evsel__setup_types(void)
906{
907 pyrf_evsel__type.tp_new = PyType_GenericNew;
908 return PyType_Ready(&pyrf_evsel__type);
909}
910
911struct pyrf_evlist {
912 PyObject_HEAD
913
Jiri Olsa63503db2019-07-21 13:23:52 +0200914 struct evlist evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200915};
916
917static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300918 PyObject *args, PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200919{
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200920 PyObject *pcpus = NULL, *pthreads = NULL;
Jiri Olsaf8548392019-07-21 13:23:49 +0200921 struct perf_cpu_map *cpus;
Jiri Olsa9749b902019-07-21 13:23:50 +0200922 struct perf_thread_map *threads;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200923
924 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
925 return -1;
926
927 threads = ((struct pyrf_thread_map *)pthreads)->threads;
928 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
Jiri Olsa52c86bc2019-07-21 13:23:54 +0200929 evlist__init(&pevlist->evlist, cpus, threads);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200930 return 0;
931}
932
933static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
934{
Jiri Olsa470579b2019-09-02 14:34:52 +0200935 evlist__exit(&pevlist->evlist);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100936 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200937}
938
939static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
940 PyObject *args, PyObject *kwargs)
941{
Jiri Olsa63503db2019-07-21 13:23:52 +0200942 struct evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200943 static char *kwlist[] = { "pages", "overwrite", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200944 int pages = 128, overwrite = false;
945
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200946 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
947 &pages, &overwrite))
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200948 return NULL;
949
Jiri Olsa9521b5f2019-07-28 12:45:35 +0200950 if (evlist__mmap(evlist, pages) < 0) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200951 PyErr_SetFromErrno(PyExc_OSError);
952 return NULL;
953 }
954
955 Py_INCREF(Py_None);
956 return Py_None;
957}
958
959static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
960 PyObject *args, PyObject *kwargs)
961{
Jiri Olsa63503db2019-07-21 13:23:52 +0200962 struct evlist *evlist = &pevlist->evlist;
Frederic Weisbecker64348152011-03-31 18:27:43 +0200963 static char *kwlist[] = { "timeout", NULL };
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200964 int timeout = -1, n;
965
966 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
967 return NULL;
968
Jiri Olsa80ab2982019-08-31 22:48:33 +0200969 n = evlist__poll(evlist, timeout);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200970 if (n < 0) {
971 PyErr_SetFromErrno(PyExc_OSError);
972 return NULL;
973 }
974
975 return Py_BuildValue("i", n);
976}
977
978static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300979 PyObject *args __maybe_unused,
980 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200981{
Jiri Olsa63503db2019-07-21 13:23:52 +0200982 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200983 PyObject *list = PyList_New(0);
984 int i;
985
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200986 for (i = 0; i < evlist->core.pollfd.nr; ++i) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200987 PyObject *file;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100988#if PY_MAJOR_VERSION < 3
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200989 FILE *fp = fdopen(evlist->core.pollfd.entries[i].fd, "r");
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200990
991 if (fp == NULL)
992 goto free_list;
993
994 file = PyFile_FromFile(fp, "perf", "r", NULL);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100995#else
Jiri Olsa40cb2d52019-08-06 11:28:02 +0200996 file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1,
Jiri Olsaa389aec2018-12-26 12:21:21 +0100997 NULL, NULL, NULL, 0);
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +0100998#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -0200999 if (file == NULL)
1000 goto free_list;
1001
1002 if (PyList_Append(list, file) != 0) {
1003 Py_DECREF(file);
1004 goto free_list;
1005 }
Arnaldo Carvalho de Melo48000a12014-12-17 17:24:45 -03001006
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001007 Py_DECREF(file);
1008 }
1009
1010 return list;
1011free_list:
1012 return PyErr_NoMemory();
1013}
1014
1015
1016static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001017 PyObject *args,
1018 PyObject *kwargs __maybe_unused)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001019{
Jiri Olsa63503db2019-07-21 13:23:52 +02001020 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001021 PyObject *pevsel;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001022 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001023
1024 if (!PyArg_ParseTuple(args, "O", &pevsel))
1025 return NULL;
1026
1027 Py_INCREF(pevsel);
1028 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001029 evsel->idx = evlist->core.nr_entries;
Jiri Olsaa1cf3a72019-07-21 13:23:59 +02001030 evlist__add(evlist, evsel);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001031
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001032 return Py_BuildValue("i", evlist->core.nr_entries);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001033}
1034
Jiri Olsaa5830532019-07-27 20:30:53 +02001035static struct mmap *get_md(struct evlist *evlist, int cpu)
Jiri Olsa721f0df2018-08-17 13:45:56 +02001036{
1037 int i;
1038
Jiri Olsac976ee12019-07-30 13:04:59 +02001039 for (i = 0; i < evlist->core.nr_mmaps; i++) {
Jiri Olsaa5830532019-07-27 20:30:53 +02001040 struct mmap *md = &evlist->mmap[i];
Jiri Olsa721f0df2018-08-17 13:45:56 +02001041
Jiri Olsa56a94702019-07-27 22:33:20 +02001042 if (md->core.cpu == cpu)
Jiri Olsa721f0df2018-08-17 13:45:56 +02001043 return md;
1044 }
1045
1046 return NULL;
1047}
1048
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001049static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
1050 PyObject *args, PyObject *kwargs)
1051{
Jiri Olsa63503db2019-07-21 13:23:52 +02001052 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001053 union perf_event *event;
1054 int sample_id_all = 1, cpu;
Frederic Weisbecker64348152011-03-31 18:27:43 +02001055 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
Jiri Olsaa5830532019-07-27 20:30:53 +02001056 struct mmap *md;
Frederic Weisbecker5538bec2011-05-22 02:17:22 +02001057 int err;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001058
1059 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1060 &cpu, &sample_id_all))
1061 return NULL;
1062
Jiri Olsa721f0df2018-08-17 13:45:56 +02001063 md = get_md(evlist, cpu);
1064 if (!md)
1065 return NULL;
1066
Jiri Olsa7c4d4182019-10-07 14:53:18 +02001067 if (perf_mmap__read_init(&md->core) < 0)
Kan Liang35b7cdc2018-03-01 18:09:00 -05001068 goto end;
1069
Jiri Olsa151ed5d2019-10-07 14:53:20 +02001070 event = perf_mmap__read_event(&md->core);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001071 if (event != NULL) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001072 PyObject *pyevent = pyrf_event__new(event);
1073 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001074 struct evsel *evsel;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001075
1076 if (pyevent == NULL)
1077 return PyErr_NoMemory();
1078
Arnaldo Carvalho de Melo3ccf8a72020-11-30 14:17:57 -03001079 evsel = evlist__event2evsel(evlist, event);
Petr Machata83428f22018-03-22 00:57:32 +01001080 if (!evsel) {
1081 Py_INCREF(Py_None);
Jiri Olsa377f6982016-07-10 13:07:59 +02001082 return Py_None;
Petr Machata83428f22018-03-22 00:57:32 +01001083 }
Jiri Olsa377f6982016-07-10 13:07:59 +02001084
1085 pevent->evsel = evsel;
1086
Arnaldo Carvalho de Melo6b6017a2020-04-30 11:03:49 -03001087 err = evsel__parse_sample(evsel, event, &pevent->sample);
Jiri Olsae8968e62016-07-10 13:07:56 +02001088
1089 /* Consume the even only after we parsed it out. */
Jiri Olsa7728fa02019-10-07 14:53:17 +02001090 perf_mmap__consume(&md->core);
Jiri Olsae8968e62016-07-10 13:07:56 +02001091
Arnaldo Carvalho de Melo5c6970a2011-06-02 10:55:10 -03001092 if (err)
1093 return PyErr_Format(PyExc_OSError,
1094 "perf: can't parse sample, err=%d", err);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001095 return pyevent;
1096 }
Kan Liang35b7cdc2018-03-01 18:09:00 -05001097end:
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001098 Py_INCREF(Py_None);
1099 return Py_None;
1100}
1101
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001102static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1103 PyObject *args, PyObject *kwargs)
1104{
Jiri Olsa63503db2019-07-21 13:23:52 +02001105 struct evlist *evlist = &pevlist->evlist;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001106 int group = 0;
1107 static char *kwlist[] = { "group", NULL };
1108
1109 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1110 return NULL;
1111
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001112 if (group)
Arnaldo Carvalho de Meloa622eaf2020-11-30 09:22:07 -03001113 evlist__set_leader(evlist);
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001114
Jiri Olsa474ddc42019-07-21 13:24:06 +02001115 if (evlist__open(evlist) < 0) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001116 PyErr_SetFromErrno(PyExc_OSError);
1117 return NULL;
1118 }
1119
1120 Py_INCREF(Py_None);
1121 return Py_None;
1122}
1123
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001124static PyMethodDef pyrf_evlist__methods[] = {
1125 {
1126 .ml_name = "mmap",
1127 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1128 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1129 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1130 },
1131 {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001132 .ml_name = "open",
1133 .ml_meth = (PyCFunction)pyrf_evlist__open,
1134 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1135 .ml_doc = PyDoc_STR("open the file descriptors.")
1136 },
1137 {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001138 .ml_name = "poll",
1139 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1140 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1141 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1142 },
1143 {
1144 .ml_name = "get_pollfd",
1145 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1146 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1147 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1148 },
1149 {
1150 .ml_name = "add",
1151 .ml_meth = (PyCFunction)pyrf_evlist__add,
1152 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1153 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1154 },
1155 {
1156 .ml_name = "read_on_cpu",
1157 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1158 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1159 .ml_doc = PyDoc_STR("reads an event.")
1160 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001161 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001162};
1163
1164static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1165{
1166 struct pyrf_evlist *pevlist = (void *)obj;
1167
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001168 return pevlist->evlist.core.nr_entries;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001169}
1170
1171static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1172{
1173 struct pyrf_evlist *pevlist = (void *)obj;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001174 struct evsel *pos;
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001175
Jiri Olsa6484d2f2019-07-21 13:24:28 +02001176 if (i >= pevlist->evlist.core.nr_entries)
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001177 return NULL;
1178
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001179 evlist__for_each_entry(&pevlist->evlist, pos) {
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001180 if (i-- == 0)
1181 break;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001182 }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001183
1184 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1185}
1186
1187static PySequenceMethods pyrf_evlist__sequence_methods = {
1188 .sq_length = pyrf_evlist__length,
1189 .sq_item = pyrf_evlist__item,
1190};
1191
1192static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1193
1194static PyTypeObject pyrf_evlist__type = {
1195 PyVarObject_HEAD_INIT(NULL, 0)
1196 .tp_name = "perf.evlist",
1197 .tp_basicsize = sizeof(struct pyrf_evlist),
1198 .tp_dealloc = (destructor)pyrf_evlist__delete,
1199 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1200 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1201 .tp_doc = pyrf_evlist__doc,
1202 .tp_methods = pyrf_evlist__methods,
1203 .tp_init = (initproc)pyrf_evlist__init,
1204};
1205
1206static int pyrf_evlist__setup_types(void)
1207{
1208 pyrf_evlist__type.tp_new = PyType_GenericNew;
1209 return PyType_Ready(&pyrf_evlist__type);
1210}
1211
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001212#define PERF_CONST(name) { #name, PERF_##name }
1213
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001214static struct {
1215 const char *name;
1216 int value;
1217} perf__constants[] = {
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001218 PERF_CONST(TYPE_HARDWARE),
1219 PERF_CONST(TYPE_SOFTWARE),
1220 PERF_CONST(TYPE_TRACEPOINT),
1221 PERF_CONST(TYPE_HW_CACHE),
1222 PERF_CONST(TYPE_RAW),
1223 PERF_CONST(TYPE_BREAKPOINT),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001224
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001225 PERF_CONST(COUNT_HW_CPU_CYCLES),
1226 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1227 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1228 PERF_CONST(COUNT_HW_CACHE_MISSES),
1229 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1230 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1231 PERF_CONST(COUNT_HW_BUS_CYCLES),
1232 PERF_CONST(COUNT_HW_CACHE_L1D),
1233 PERF_CONST(COUNT_HW_CACHE_L1I),
1234 PERF_CONST(COUNT_HW_CACHE_LL),
1235 PERF_CONST(COUNT_HW_CACHE_DTLB),
1236 PERF_CONST(COUNT_HW_CACHE_ITLB),
1237 PERF_CONST(COUNT_HW_CACHE_BPU),
1238 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1239 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1240 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1241 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1242 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001243
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001244 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1245 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
Ingo Molnar129c04c2011-04-29 14:41:28 +02001246
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001247 PERF_CONST(COUNT_SW_CPU_CLOCK),
1248 PERF_CONST(COUNT_SW_TASK_CLOCK),
1249 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1250 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1251 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1252 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1253 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1254 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1255 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1256 PERF_CONST(COUNT_SW_DUMMY),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001257
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001258 PERF_CONST(SAMPLE_IP),
1259 PERF_CONST(SAMPLE_TID),
1260 PERF_CONST(SAMPLE_TIME),
1261 PERF_CONST(SAMPLE_ADDR),
1262 PERF_CONST(SAMPLE_READ),
1263 PERF_CONST(SAMPLE_CALLCHAIN),
1264 PERF_CONST(SAMPLE_ID),
1265 PERF_CONST(SAMPLE_CPU),
1266 PERF_CONST(SAMPLE_PERIOD),
1267 PERF_CONST(SAMPLE_STREAM_ID),
1268 PERF_CONST(SAMPLE_RAW),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001269
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001270 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1271 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1272 PERF_CONST(FORMAT_ID),
1273 PERF_CONST(FORMAT_GROUP),
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001274
Arnaldo Carvalho de Melo5865fe32015-07-24 13:00:03 -03001275 PERF_CONST(RECORD_MMAP),
1276 PERF_CONST(RECORD_LOST),
1277 PERF_CONST(RECORD_COMM),
1278 PERF_CONST(RECORD_EXIT),
1279 PERF_CONST(RECORD_THROTTLE),
1280 PERF_CONST(RECORD_UNTHROTTLE),
1281 PERF_CONST(RECORD_FORK),
1282 PERF_CONST(RECORD_READ),
1283 PERF_CONST(RECORD_SAMPLE),
Arnaldo Carvalho de Melo84576da2015-07-24 13:04:09 -03001284 PERF_CONST(RECORD_MMAP2),
1285 PERF_CONST(RECORD_AUX),
1286 PERF_CONST(RECORD_ITRACE_START),
1287 PERF_CONST(RECORD_LOST_SAMPLES),
1288 PERF_CONST(RECORD_SWITCH),
1289 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
Arnaldo Carvalho de Meloae938802015-10-06 17:46:46 -03001290
1291 PERF_CONST(RECORD_MISC_SWITCH_OUT),
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001292 { .name = NULL, },
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001293};
1294
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001295static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1296 PyObject *args, PyObject *kwargs)
1297{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001298 struct tep_event *tp_format;
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001299 static char *kwlist[] = { "sys", "name", NULL };
1300 char *sys = NULL;
1301 char *name = NULL;
1302
1303 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1304 &sys, &name))
1305 return NULL;
1306
1307 tp_format = trace_event__tp_format(sys, name);
1308 if (IS_ERR(tp_format))
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001309 return _PyLong_FromLong(-1);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001310
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001311 return _PyLong_FromLong(tp_format->id);
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001312}
1313
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001314static PyMethodDef perf__methods[] = {
Jiri Olsa1075fbb2016-07-10 13:07:58 +02001315 {
1316 .ml_name = "tracepoint",
1317 .ml_meth = (PyCFunction) pyrf__tracepoint,
1318 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1319 .ml_doc = PyDoc_STR("Get tracepoint config.")
1320 },
Arnaldo Carvalho de Melof6bbc1d2011-01-31 20:56:27 -02001321 { .ml_name = NULL, }
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001322};
1323
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001324#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001325PyMODINIT_FUNC initperf(void)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001326#else
1327PyMODINIT_FUNC PyInit_perf(void)
1328#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001329{
1330 PyObject *obj;
1331 int i;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001332 PyObject *dict;
1333#if PY_MAJOR_VERSION < 3
1334 PyObject *module = Py_InitModule("perf", perf__methods);
1335#else
1336 static struct PyModuleDef moduledef = {
1337 PyModuleDef_HEAD_INIT,
1338 "perf", /* m_name */
1339 "", /* m_doc */
1340 -1, /* m_size */
1341 perf__methods, /* m_methods */
1342 NULL, /* m_reload */
1343 NULL, /* m_traverse */
1344 NULL, /* m_clear */
1345 NULL, /* m_free */
1346 };
1347 PyObject *module = PyModule_Create(&moduledef);
1348#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001349
1350 if (module == NULL ||
1351 pyrf_event__setup_types() < 0 ||
1352 pyrf_evlist__setup_types() < 0 ||
1353 pyrf_evsel__setup_types() < 0 ||
1354 pyrf_thread_map__setup_types() < 0 ||
1355 pyrf_cpu_map__setup_types() < 0)
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001356#if PY_MAJOR_VERSION < 3
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001357 return;
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001358#else
1359 return module;
1360#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001361
Jiri Olsa918512b2013-09-12 18:39:35 +02001362 /* The page_size is placed in util object. */
Arnaldo Carvalho de Melo0da2e9c2012-10-16 14:51:04 -03001363 page_size = sysconf(_SC_PAGE_SIZE);
1364
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001365 Py_INCREF(&pyrf_evlist__type);
1366 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1367
1368 Py_INCREF(&pyrf_evsel__type);
1369 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1370
Jiri Olsa85e37de2016-07-10 13:07:57 +02001371 Py_INCREF(&pyrf_mmap_event__type);
1372 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1373
1374 Py_INCREF(&pyrf_lost_event__type);
1375 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1376
1377 Py_INCREF(&pyrf_comm_event__type);
1378 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1379
1380 Py_INCREF(&pyrf_task_event__type);
1381 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1382
1383 Py_INCREF(&pyrf_throttle_event__type);
1384 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1385
1386 Py_INCREF(&pyrf_task_event__type);
1387 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1388
1389 Py_INCREF(&pyrf_read_event__type);
1390 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1391
1392 Py_INCREF(&pyrf_sample_event__type);
1393 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1394
1395 Py_INCREF(&pyrf_context_switch_event__type);
1396 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1397
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001398 Py_INCREF(&pyrf_thread_map__type);
1399 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1400
1401 Py_INCREF(&pyrf_cpu_map__type);
1402 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1403
1404 dict = PyModule_GetDict(module);
1405 if (dict == NULL)
1406 goto error;
1407
1408 for (i = 0; perf__constants[i].name != NULL; i++) {
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001409 obj = _PyLong_FromLong(perf__constants[i].value);
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001410 if (obj == NULL)
1411 goto error;
1412 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1413 Py_DECREF(obj);
1414 }
1415
1416error:
1417 if (PyErr_Occurred())
1418 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
Jaroslav Škarvada66dfdff2018-01-19 21:56:41 +01001419#if PY_MAJOR_VERSION >= 3
1420 return module;
1421#endif
Arnaldo Carvalho de Melo877108e2011-01-29 15:44:29 -02001422}
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -03001423
1424/*
1425 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1426 * binding.
1427 */
1428void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
1429 int fd, int group_fd, unsigned long flags)
1430{
1431}