xref: /linux/tools/perf/util/python.c (revision 6d18804b963b78dcd53851f11e9080408b3d85c2)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2877108e4SArnaldo Carvalho de Melo #include <Python.h>
3877108e4SArnaldo Carvalho de Melo #include <structmember.h>
4877108e4SArnaldo Carvalho de Melo #include <inttypes.h>
5877108e4SArnaldo Carvalho de Melo #include <poll.h>
61075fbb2SJiri Olsa #include <linux/err.h>
79c3516d1SJiri Olsa #include <perf/cpumap.h>
8f2a39fe8SArnaldo Carvalho de Melo #include <traceevent/event-parse.h>
97728fa0cSJiri Olsa #include <perf/mmap.h>
10877108e4SArnaldo Carvalho de Melo #include "evlist.h"
1156e2e056SArnaldo Carvalho de Melo #include "callchain.h"
12877108e4SArnaldo Carvalho de Melo #include "evsel.h"
13877108e4SArnaldo Carvalho de Melo #include "event.h"
14fea01392SArnaldo Carvalho de Melo #include "print_binary.h"
15877108e4SArnaldo Carvalho de Melo #include "thread_map.h"
16f2a39fe8SArnaldo Carvalho de Melo #include "trace-event.h"
17721f0dfcSJiri Olsa #include "mmap.h"
18b214ba8cSNamhyung Kim #include "stat.h"
19b214ba8cSNamhyung Kim #include "metricgroup.h"
209db0e363SArnaldo Carvalho de Melo #include "util/env.h"
2120f2be1dSJiri Olsa #include <internal/lib.h>
228366f0d2SJiri Olsa #include "util.h"
23877108e4SArnaldo Carvalho de Melo 
2466dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
2566dfdff0SJaroslav Škarvada #define _PyUnicode_FromString(arg) \
2666dfdff0SJaroslav Škarvada   PyString_FromString(arg)
2766dfdff0SJaroslav Škarvada #define _PyUnicode_AsString(arg) \
2866dfdff0SJaroslav Škarvada   PyString_AsString(arg)
2966dfdff0SJaroslav Škarvada #define _PyUnicode_FromFormat(...) \
3066dfdff0SJaroslav Škarvada   PyString_FromFormat(__VA_ARGS__)
3166dfdff0SJaroslav Škarvada #define _PyLong_FromLong(arg) \
3266dfdff0SJaroslav Škarvada   PyInt_FromLong(arg)
3366dfdff0SJaroslav Škarvada 
3466dfdff0SJaroslav Škarvada #else
3566dfdff0SJaroslav Škarvada 
3666dfdff0SJaroslav Škarvada #define _PyUnicode_FromString(arg) \
3766dfdff0SJaroslav Škarvada   PyUnicode_FromString(arg)
3866dfdff0SJaroslav Škarvada #define _PyUnicode_FromFormat(...) \
3966dfdff0SJaroslav Škarvada   PyUnicode_FromFormat(__VA_ARGS__)
4066dfdff0SJaroslav Škarvada #define _PyLong_FromLong(arg) \
4166dfdff0SJaroslav Škarvada   PyLong_FromLong(arg)
4266dfdff0SJaroslav Škarvada #endif
4366dfdff0SJaroslav Škarvada 
4466dfdff0SJaroslav Škarvada #ifndef Py_TYPE
4566dfdff0SJaroslav Škarvada #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
4666dfdff0SJaroslav Škarvada #endif
4766dfdff0SJaroslav Škarvada 
488afb4c01SAdrian Hunter /*
4956e2e056SArnaldo Carvalho de Melo  * Provide these two so that we don't have to link against callchain.c and
5056e2e056SArnaldo Carvalho de Melo  * start dragging hist.c, etc.
5156e2e056SArnaldo Carvalho de Melo  */
5256e2e056SArnaldo Carvalho de Melo struct callchain_param callchain_param;
5356e2e056SArnaldo Carvalho de Melo 
5456e2e056SArnaldo Carvalho de Melo int parse_callchain_record(const char *arg __maybe_unused,
5556e2e056SArnaldo Carvalho de Melo 			   struct callchain_param *param __maybe_unused)
5656e2e056SArnaldo Carvalho de Melo {
5756e2e056SArnaldo Carvalho de Melo 	return 0;
5856e2e056SArnaldo Carvalho de Melo }
5956e2e056SArnaldo Carvalho de Melo 
6056e2e056SArnaldo Carvalho de Melo /*
619db0e363SArnaldo Carvalho de Melo  * Add this one here not to drag util/env.c
629db0e363SArnaldo Carvalho de Melo  */
639db0e363SArnaldo Carvalho de Melo struct perf_env perf_env;
649db0e363SArnaldo Carvalho de Melo 
659db0e363SArnaldo Carvalho de Melo /*
66b214ba8cSNamhyung Kim  * Add this one here not to drag util/stat-shadow.c
67b214ba8cSNamhyung Kim  */
68b214ba8cSNamhyung Kim void perf_stat__collect_metric_expr(struct evlist *evsel_list)
69b214ba8cSNamhyung Kim {
70b214ba8cSNamhyung Kim }
71b214ba8cSNamhyung Kim 
72b214ba8cSNamhyung Kim /*
733500eeebSNamhyung Kim  * This one is needed not to drag the PMU bandwagon, jevents generated
743500eeebSNamhyung Kim  * pmu_sys_event_tables, etc and evsel__find_pmu() is used so far just for
753500eeebSNamhyung Kim  * doing per PMU perf_event_attr.exclude_guest handling, not really needed, so
763500eeebSNamhyung Kim  * far, for the perf python binding known usecases, revisit if this become
773500eeebSNamhyung Kim  * necessary.
783500eeebSNamhyung Kim  */
793500eeebSNamhyung Kim struct perf_pmu *evsel__find_pmu(struct evsel *evsel __maybe_unused)
803500eeebSNamhyung Kim {
813500eeebSNamhyung Kim 	return NULL;
823500eeebSNamhyung Kim }
833500eeebSNamhyung Kim 
843500eeebSNamhyung Kim /*
85b214ba8cSNamhyung Kim  * Add this one here not to drag util/metricgroup.c
86b214ba8cSNamhyung Kim  */
87b214ba8cSNamhyung Kim int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
88b214ba8cSNamhyung Kim 				    struct rblist *new_metric_events,
89b214ba8cSNamhyung Kim 				    struct rblist *old_metric_events)
90b214ba8cSNamhyung Kim {
91b214ba8cSNamhyung Kim 	return 0;
92b214ba8cSNamhyung Kim }
93b214ba8cSNamhyung Kim 
94b214ba8cSNamhyung Kim /*
95fa853c4bSSong Liu  * XXX: All these evsel destructors need some better mechanism, like a linked
96fa853c4bSSong Liu  * list of destructors registered when the relevant code indeed is used instead
97fa853c4bSSong Liu  * of having more and more calls in perf_evsel__delete(). -- acme
98fa853c4bSSong Liu  *
99fa853c4bSSong Liu  * For now, add some more:
100fa853c4bSSong Liu  *
101fa853c4bSSong Liu  * Not to drag the BPF bandwagon...
102fa853c4bSSong Liu  */
103fa853c4bSSong Liu void bpf_counter__destroy(struct evsel *evsel);
104fa853c4bSSong Liu int bpf_counter__install_pe(struct evsel *evsel, int cpu, int fd);
1055508c9daSSong Liu int bpf_counter__disable(struct evsel *evsel);
106fa853c4bSSong Liu 
107fa853c4bSSong Liu void bpf_counter__destroy(struct evsel *evsel __maybe_unused)
108fa853c4bSSong Liu {
109fa853c4bSSong Liu }
110fa853c4bSSong Liu 
111fa853c4bSSong Liu int bpf_counter__install_pe(struct evsel *evsel __maybe_unused, int cpu __maybe_unused, int fd __maybe_unused)
112fa853c4bSSong Liu {
113fa853c4bSSong Liu 	return 0;
114fa853c4bSSong Liu }
115fa853c4bSSong Liu 
1165508c9daSSong Liu int bpf_counter__disable(struct evsel *evsel __maybe_unused)
1175508c9daSSong Liu {
1185508c9daSSong Liu 	return 0;
1195508c9daSSong Liu }
1205508c9daSSong Liu 
121fa853c4bSSong Liu /*
1228afb4c01SAdrian Hunter  * Support debug printing even though util/debug.c is not linked.  That means
1238afb4c01SAdrian Hunter  * implementing 'verbose' and 'eprintf'.
1248afb4c01SAdrian Hunter  */
1258afb4c01SAdrian Hunter int verbose;
126ccd26741SRavi Bangoria int debug_peo_args;
1278afb4c01SAdrian Hunter 
1285939caccSArnaldo Carvalho de Melo int eprintf(int level, int var, const char *fmt, ...);
1295939caccSArnaldo Carvalho de Melo 
130c95688aaSJiri Olsa int eprintf(int level, int var, const char *fmt, ...)
1318afb4c01SAdrian Hunter {
1328afb4c01SAdrian Hunter 	va_list args;
1338afb4c01SAdrian Hunter 	int ret = 0;
1348afb4c01SAdrian Hunter 
135c95688aaSJiri Olsa 	if (var >= level) {
1368afb4c01SAdrian Hunter 		va_start(args, fmt);
1378afb4c01SAdrian Hunter 		ret = vfprintf(stderr, fmt, args);
1388afb4c01SAdrian Hunter 		va_end(args);
1398afb4c01SAdrian Hunter 	}
1408afb4c01SAdrian Hunter 
1418afb4c01SAdrian Hunter 	return ret;
1428afb4c01SAdrian Hunter }
1438afb4c01SAdrian Hunter 
144cfff2d90SFrederic Weisbecker /* Define PyVarObject_HEAD_INIT for python 2.5 */
145cfff2d90SFrederic Weisbecker #ifndef PyVarObject_HEAD_INIT
146cfff2d90SFrederic Weisbecker # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
147cfff2d90SFrederic Weisbecker #endif
148cfff2d90SFrederic Weisbecker 
14966dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
150f6bbc1daSArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void);
15166dfdff0SJaroslav Škarvada #else
15266dfdff0SJaroslav Škarvada PyMODINIT_FUNC PyInit_perf(void);
15366dfdff0SJaroslav Škarvada #endif
154f6bbc1daSArnaldo Carvalho de Melo 
155877108e4SArnaldo Carvalho de Melo #define member_def(type, member, ptype, help) \
156877108e4SArnaldo Carvalho de Melo 	{ #member, ptype, \
157877108e4SArnaldo Carvalho de Melo 	  offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
158877108e4SArnaldo Carvalho de Melo 	  0, help }
159877108e4SArnaldo Carvalho de Melo 
160877108e4SArnaldo Carvalho de Melo #define sample_member_def(name, member, ptype, help) \
161877108e4SArnaldo Carvalho de Melo 	{ #name, ptype, \
162877108e4SArnaldo Carvalho de Melo 	  offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
163877108e4SArnaldo Carvalho de Melo 	  0, help }
164877108e4SArnaldo Carvalho de Melo 
165877108e4SArnaldo Carvalho de Melo struct pyrf_event {
166877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
16732dcd021SJiri Olsa 	struct evsel *evsel;
168877108e4SArnaldo Carvalho de Melo 	struct perf_sample sample;
169877108e4SArnaldo Carvalho de Melo 	union perf_event   event;
170877108e4SArnaldo Carvalho de Melo };
171877108e4SArnaldo Carvalho de Melo 
172877108e4SArnaldo Carvalho de Melo #define sample_members \
173f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"),			 \
174877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_pid, pid, T_INT, "event pid"),			 \
175877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_tid, tid, T_INT, "event tid"),			 \
176f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"),		 \
177f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"),		 \
178f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_id, id, T_ULONGLONG, "event id"),			 \
179f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
180f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_period, period, T_ULONGLONG, "event period"),		 \
181877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
182877108e4SArnaldo Carvalho de Melo 
183877108e4SArnaldo Carvalho de Melo static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
184877108e4SArnaldo Carvalho de Melo 
185877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_mmap_event__members[] = {
186877108e4SArnaldo Carvalho de Melo 	sample_members
187877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
188ae938802SArnaldo Carvalho de Melo 	member_def(perf_event_header, misc, T_UINT, "event misc"),
18969d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, pid, T_UINT, "event pid"),
19069d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, tid, T_UINT, "event tid"),
19169d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
19269d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
19369d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
19469d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
195f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
196877108e4SArnaldo Carvalho de Melo };
197877108e4SArnaldo Carvalho de Melo 
198877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
199877108e4SArnaldo Carvalho de Melo {
200877108e4SArnaldo Carvalho de Melo 	PyObject *ret;
201877108e4SArnaldo Carvalho de Melo 	char *s;
202877108e4SArnaldo Carvalho de Melo 
2031345e2eeSJiri Olsa 	if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
2041345e2eeSJiri Olsa 			 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
205877108e4SArnaldo Carvalho de Melo 			 "filename: %s }",
206877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.pid, pevent->event.mmap.tid,
207877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.start, pevent->event.mmap.len,
208877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
209877108e4SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
210877108e4SArnaldo Carvalho de Melo 	} else {
21166dfdff0SJaroslav Škarvada 		ret = _PyUnicode_FromString(s);
212877108e4SArnaldo Carvalho de Melo 		free(s);
213877108e4SArnaldo Carvalho de Melo 	}
214877108e4SArnaldo Carvalho de Melo 	return ret;
215877108e4SArnaldo Carvalho de Melo }
216877108e4SArnaldo Carvalho de Melo 
217877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_mmap_event__type = {
218877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
219877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.mmap_event",
220877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
221877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
222877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_mmap_event__doc,
223877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_mmap_event__members,
224877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_mmap_event__repr,
225877108e4SArnaldo Carvalho de Melo };
226877108e4SArnaldo Carvalho de Melo 
227877108e4SArnaldo Carvalho de Melo static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
228877108e4SArnaldo Carvalho de Melo 
229877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_task_event__members[] = {
230877108e4SArnaldo Carvalho de Melo 	sample_members
231877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
23269d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, pid, T_UINT, "event pid"),
23369d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
23469d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, tid, T_UINT, "event tid"),
23569d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
23669d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
237f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
238877108e4SArnaldo Carvalho de Melo };
239877108e4SArnaldo Carvalho de Melo 
240877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
241877108e4SArnaldo Carvalho de Melo {
24266dfdff0SJaroslav Škarvada 	return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
243bceb59b1SJiri Olsa 				   "ptid: %u, time: %" PRI_lu64 "}",
244877108e4SArnaldo Carvalho de Melo 				   pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
245877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.pid,
246877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.ppid,
247877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.tid,
248877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.ptid,
249877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.time);
250877108e4SArnaldo Carvalho de Melo }
251877108e4SArnaldo Carvalho de Melo 
252877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_task_event__type = {
253877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
254877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.task_event",
255877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
256877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
257877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_task_event__doc,
258877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_task_event__members,
259877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_task_event__repr,
260877108e4SArnaldo Carvalho de Melo };
261877108e4SArnaldo Carvalho de Melo 
262877108e4SArnaldo Carvalho de Melo static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
263877108e4SArnaldo Carvalho de Melo 
264877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_comm_event__members[] = {
265877108e4SArnaldo Carvalho de Melo 	sample_members
266877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
26769d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_comm, pid, T_UINT, "event pid"),
26869d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_comm, tid, T_UINT, "event tid"),
26969d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
270f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
271877108e4SArnaldo Carvalho de Melo };
272877108e4SArnaldo Carvalho de Melo 
273877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
274877108e4SArnaldo Carvalho de Melo {
27566dfdff0SJaroslav Škarvada 	return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
276877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.pid,
277877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.tid,
278877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.comm);
279877108e4SArnaldo Carvalho de Melo }
280877108e4SArnaldo Carvalho de Melo 
281877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_comm_event__type = {
282877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
283877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.comm_event",
284877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
285877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
286877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_comm_event__doc,
287877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_comm_event__members,
288877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_comm_event__repr,
289877108e4SArnaldo Carvalho de Melo };
290877108e4SArnaldo Carvalho de Melo 
291877108e4SArnaldo Carvalho de Melo static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
292877108e4SArnaldo Carvalho de Melo 
293877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_throttle_event__members[] = {
294877108e4SArnaldo Carvalho de Melo 	sample_members
295877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
29669d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
29769d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
29869d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
299f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
300877108e4SArnaldo Carvalho de Melo };
301877108e4SArnaldo Carvalho de Melo 
302877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
303877108e4SArnaldo Carvalho de Melo {
30469d81f09SArnaldo Carvalho de Melo 	struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1);
305877108e4SArnaldo Carvalho de Melo 
306003c66feSJiri Olsa 	return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
307003c66feSJiri Olsa 				   ", stream_id: %" PRI_lu64 " }",
308877108e4SArnaldo Carvalho de Melo 				   pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
309877108e4SArnaldo Carvalho de Melo 				   te->time, te->id, te->stream_id);
310877108e4SArnaldo Carvalho de Melo }
311877108e4SArnaldo Carvalho de Melo 
312877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_throttle_event__type = {
313877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
314877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.throttle_event",
315877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
316877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
317877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_throttle_event__doc,
318877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_throttle_event__members,
319877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_throttle_event__repr,
320877108e4SArnaldo Carvalho de Melo };
321877108e4SArnaldo Carvalho de Melo 
3223e9f45a7SArnaldo Carvalho de Melo static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
3233e9f45a7SArnaldo Carvalho de Melo 
3243e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_lost_event__members[] = {
3253e9f45a7SArnaldo Carvalho de Melo 	sample_members
32669d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
32769d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
3283e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
3293e9f45a7SArnaldo Carvalho de Melo };
3303e9f45a7SArnaldo Carvalho de Melo 
3313e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
3323e9f45a7SArnaldo Carvalho de Melo {
3333e9f45a7SArnaldo Carvalho de Melo 	PyObject *ret;
3343e9f45a7SArnaldo Carvalho de Melo 	char *s;
3353e9f45a7SArnaldo Carvalho de Melo 
3365290ed69SJiri Olsa 	if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
3375290ed69SJiri Olsa 			 "lost: %#" PRI_lx64 " }",
3383e9f45a7SArnaldo Carvalho de Melo 		     pevent->event.lost.id, pevent->event.lost.lost) < 0) {
3393e9f45a7SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
3403e9f45a7SArnaldo Carvalho de Melo 	} else {
34166dfdff0SJaroslav Škarvada 		ret = _PyUnicode_FromString(s);
3423e9f45a7SArnaldo Carvalho de Melo 		free(s);
3433e9f45a7SArnaldo Carvalho de Melo 	}
3443e9f45a7SArnaldo Carvalho de Melo 	return ret;
3453e9f45a7SArnaldo Carvalho de Melo }
3463e9f45a7SArnaldo Carvalho de Melo 
3473e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_lost_event__type = {
3483e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
3493e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.lost_event",
3503e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
3513e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
3523e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_lost_event__doc,
3533e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_lost_event__members,
3543e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_lost_event__repr,
3553e9f45a7SArnaldo Carvalho de Melo };
3563e9f45a7SArnaldo Carvalho de Melo 
3573e9f45a7SArnaldo Carvalho de Melo static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
3583e9f45a7SArnaldo Carvalho de Melo 
3593e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_read_event__members[] = {
3603e9f45a7SArnaldo Carvalho de Melo 	sample_members
36169d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_read, pid, T_UINT, "event pid"),
36269d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_read, tid, T_UINT, "event tid"),
3633e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
3643e9f45a7SArnaldo Carvalho de Melo };
3653e9f45a7SArnaldo Carvalho de Melo 
3663e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
3673e9f45a7SArnaldo Carvalho de Melo {
36866dfdff0SJaroslav Škarvada 	return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
3693e9f45a7SArnaldo Carvalho de Melo 				   pevent->event.read.pid,
3703e9f45a7SArnaldo Carvalho de Melo 				   pevent->event.read.tid);
3713e9f45a7SArnaldo Carvalho de Melo 	/*
3723e9f45a7SArnaldo Carvalho de Melo  	 * FIXME: return the array of read values,
3733e9f45a7SArnaldo Carvalho de Melo  	 * making this method useful ;-)
3743e9f45a7SArnaldo Carvalho de Melo  	 */
3753e9f45a7SArnaldo Carvalho de Melo }
3763e9f45a7SArnaldo Carvalho de Melo 
3773e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_read_event__type = {
3783e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
3793e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.read_event",
3803e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
3813e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
3823e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_read_event__doc,
3833e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_read_event__members,
3843e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_read_event__repr,
3853e9f45a7SArnaldo Carvalho de Melo };
3863e9f45a7SArnaldo Carvalho de Melo 
3873e9f45a7SArnaldo Carvalho de Melo static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
3883e9f45a7SArnaldo Carvalho de Melo 
3893e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_sample_event__members[] = {
3903e9f45a7SArnaldo Carvalho de Melo 	sample_members
3913e9f45a7SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
3923e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
3933e9f45a7SArnaldo Carvalho de Melo };
3943e9f45a7SArnaldo Carvalho de Melo 
3953e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
3963e9f45a7SArnaldo Carvalho de Melo {
3973e9f45a7SArnaldo Carvalho de Melo 	PyObject *ret;
3983e9f45a7SArnaldo Carvalho de Melo 	char *s;
3993e9f45a7SArnaldo Carvalho de Melo 
4003e9f45a7SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: sample }") < 0) {
4013e9f45a7SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
4023e9f45a7SArnaldo Carvalho de Melo 	} else {
40366dfdff0SJaroslav Škarvada 		ret = _PyUnicode_FromString(s);
4043e9f45a7SArnaldo Carvalho de Melo 		free(s);
4053e9f45a7SArnaldo Carvalho de Melo 	}
4063e9f45a7SArnaldo Carvalho de Melo 	return ret;
4073e9f45a7SArnaldo Carvalho de Melo }
4083e9f45a7SArnaldo Carvalho de Melo 
409bae57e38SJiri Olsa static bool is_tracepoint(struct pyrf_event *pevent)
410bae57e38SJiri Olsa {
4111fc632ceSJiri Olsa 	return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
412bae57e38SJiri Olsa }
413bae57e38SJiri Olsa 
414bae57e38SJiri Olsa static PyObject*
4152c92f982STzvetomir Stoyanov (VMware) tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
416bae57e38SJiri Olsa {
41769769ce1STzvetomir Stoyanov 	struct tep_handle *pevent = field->event->tep;
418bae57e38SJiri Olsa 	void *data = pe->sample.raw_data;
419bae57e38SJiri Olsa 	PyObject *ret = NULL;
420bae57e38SJiri Olsa 	unsigned long long val;
421bae57e38SJiri Olsa 	unsigned int offset, len;
422bae57e38SJiri Olsa 
423bb39ccb2STzvetomir Stoyanov (VMware) 	if (field->flags & TEP_FIELD_IS_ARRAY) {
424bae57e38SJiri Olsa 		offset = field->offset;
425bae57e38SJiri Olsa 		len    = field->size;
426bb39ccb2STzvetomir Stoyanov (VMware) 		if (field->flags & TEP_FIELD_IS_DYNAMIC) {
42759c1baeeSTzvetomir Stoyanov (VMware) 			val     = tep_read_number(pevent, data + offset, len);
428bae57e38SJiri Olsa 			offset  = val;
429bae57e38SJiri Olsa 			len     = offset >> 16;
430bae57e38SJiri Olsa 			offset &= 0xffff;
431bae57e38SJiri Olsa 		}
432bb39ccb2STzvetomir Stoyanov (VMware) 		if (field->flags & TEP_FIELD_IS_STRING &&
433bae57e38SJiri Olsa 		    is_printable_array(data + offset, len)) {
43466dfdff0SJaroslav Škarvada 			ret = _PyUnicode_FromString((char *)data + offset);
435bae57e38SJiri Olsa 		} else {
436bae57e38SJiri Olsa 			ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
437bb39ccb2STzvetomir Stoyanov (VMware) 			field->flags &= ~TEP_FIELD_IS_STRING;
438bae57e38SJiri Olsa 		}
439bae57e38SJiri Olsa 	} else {
44059c1baeeSTzvetomir Stoyanov (VMware) 		val = tep_read_number(pevent, data + field->offset,
441bae57e38SJiri Olsa 				      field->size);
442bb39ccb2STzvetomir Stoyanov (VMware) 		if (field->flags & TEP_FIELD_IS_POINTER)
443bae57e38SJiri Olsa 			ret = PyLong_FromUnsignedLong((unsigned long) val);
444bb39ccb2STzvetomir Stoyanov (VMware) 		else if (field->flags & TEP_FIELD_IS_SIGNED)
445bae57e38SJiri Olsa 			ret = PyLong_FromLong((long) val);
446bae57e38SJiri Olsa 		else
447bae57e38SJiri Olsa 			ret = PyLong_FromUnsignedLong((unsigned long) val);
448bae57e38SJiri Olsa 	}
449bae57e38SJiri Olsa 
450bae57e38SJiri Olsa 	return ret;
451bae57e38SJiri Olsa }
452bae57e38SJiri Olsa 
453bae57e38SJiri Olsa static PyObject*
454bae57e38SJiri Olsa get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
455bae57e38SJiri Olsa {
45666dfdff0SJaroslav Škarvada 	const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
45732dcd021SJiri Olsa 	struct evsel *evsel = pevent->evsel;
4582c92f982STzvetomir Stoyanov (VMware) 	struct tep_format_field *field;
459bae57e38SJiri Olsa 
460bae57e38SJiri Olsa 	if (!evsel->tp_format) {
46197fbf3f0STzvetomir Stoyanov 		struct tep_event *tp_format;
462bae57e38SJiri Olsa 
4631fc632ceSJiri Olsa 		tp_format = trace_event__tp_format_id(evsel->core.attr.config);
4649937e8daSMiaoqian Lin 		if (IS_ERR_OR_NULL(tp_format))
465bae57e38SJiri Olsa 			return NULL;
466bae57e38SJiri Olsa 
467bae57e38SJiri Olsa 		evsel->tp_format = tp_format;
468bae57e38SJiri Olsa 	}
469bae57e38SJiri Olsa 
470af85cd19STzvetomir Stoyanov (VMware) 	field = tep_find_any_field(evsel->tp_format, str);
471bae57e38SJiri Olsa 	if (!field)
472bae57e38SJiri Olsa 		return NULL;
473bae57e38SJiri Olsa 
474bae57e38SJiri Olsa 	return tracepoint_field(pevent, field);
475bae57e38SJiri Olsa }
476bae57e38SJiri Olsa 
477bae57e38SJiri Olsa static PyObject*
478bae57e38SJiri Olsa pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
479bae57e38SJiri Olsa {
480bae57e38SJiri Olsa 	PyObject *obj = NULL;
481bae57e38SJiri Olsa 
482bae57e38SJiri Olsa 	if (is_tracepoint(pevent))
483bae57e38SJiri Olsa 		obj = get_tracepoint_field(pevent, attr_name);
484bae57e38SJiri Olsa 
485bae57e38SJiri Olsa 	return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
486bae57e38SJiri Olsa }
487bae57e38SJiri Olsa 
4883e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_sample_event__type = {
4893e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
4903e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.sample_event",
4913e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
4923e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
4933e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_sample_event__doc,
4943e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_sample_event__members,
4953e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_sample_event__repr,
496bae57e38SJiri Olsa 	.tp_getattro	= (getattrofunc) pyrf_sample_event__getattro,
4973e9f45a7SArnaldo Carvalho de Melo };
4983e9f45a7SArnaldo Carvalho de Melo 
499ae938802SArnaldo Carvalho de Melo static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
500ae938802SArnaldo Carvalho de Melo 
501ae938802SArnaldo Carvalho de Melo static PyMemberDef pyrf_context_switch_event__members[] = {
502ae938802SArnaldo Carvalho de Melo 	sample_members
503ae938802SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
50472932371SJiri Olsa 	member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
50572932371SJiri Olsa 	member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
506ae938802SArnaldo Carvalho de Melo 	{ .name = NULL, },
507ae938802SArnaldo Carvalho de Melo };
508ae938802SArnaldo Carvalho de Melo 
509ae938802SArnaldo Carvalho de Melo static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
510ae938802SArnaldo Carvalho de Melo {
511ae938802SArnaldo Carvalho de Melo 	PyObject *ret;
512ae938802SArnaldo Carvalho de Melo 	char *s;
513ae938802SArnaldo Carvalho de Melo 
514ae938802SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
515ae938802SArnaldo Carvalho de Melo 		     pevent->event.context_switch.next_prev_pid,
516ae938802SArnaldo Carvalho de Melo 		     pevent->event.context_switch.next_prev_tid,
517ae938802SArnaldo Carvalho de Melo 		     !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
518ae938802SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
519ae938802SArnaldo Carvalho de Melo 	} else {
52066dfdff0SJaroslav Škarvada 		ret = _PyUnicode_FromString(s);
521ae938802SArnaldo Carvalho de Melo 		free(s);
522ae938802SArnaldo Carvalho de Melo 	}
523ae938802SArnaldo Carvalho de Melo 	return ret;
524ae938802SArnaldo Carvalho de Melo }
525ae938802SArnaldo Carvalho de Melo 
526ae938802SArnaldo Carvalho de Melo static PyTypeObject pyrf_context_switch_event__type = {
527ae938802SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
528ae938802SArnaldo Carvalho de Melo 	.tp_name	= "perf.context_switch_event",
529ae938802SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
530ae938802SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
531ae938802SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_context_switch_event__doc,
532ae938802SArnaldo Carvalho de Melo 	.tp_members	= pyrf_context_switch_event__members,
533ae938802SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_context_switch_event__repr,
534ae938802SArnaldo Carvalho de Melo };
535ae938802SArnaldo Carvalho de Melo 
536877108e4SArnaldo Carvalho de Melo static int pyrf_event__setup_types(void)
537877108e4SArnaldo Carvalho de Melo {
538877108e4SArnaldo Carvalho de Melo 	int err;
539877108e4SArnaldo Carvalho de Melo 	pyrf_mmap_event__type.tp_new =
540877108e4SArnaldo Carvalho de Melo 	pyrf_task_event__type.tp_new =
541877108e4SArnaldo Carvalho de Melo 	pyrf_comm_event__type.tp_new =
5423e9f45a7SArnaldo Carvalho de Melo 	pyrf_lost_event__type.tp_new =
5433e9f45a7SArnaldo Carvalho de Melo 	pyrf_read_event__type.tp_new =
5443e9f45a7SArnaldo Carvalho de Melo 	pyrf_sample_event__type.tp_new =
545ae938802SArnaldo Carvalho de Melo 	pyrf_context_switch_event__type.tp_new =
546877108e4SArnaldo Carvalho de Melo 	pyrf_throttle_event__type.tp_new = PyType_GenericNew;
547877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_mmap_event__type);
548877108e4SArnaldo Carvalho de Melo 	if (err < 0)
549877108e4SArnaldo Carvalho de Melo 		goto out;
5503e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_lost_event__type);
5513e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
5523e9f45a7SArnaldo Carvalho de Melo 		goto out;
553877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_task_event__type);
554877108e4SArnaldo Carvalho de Melo 	if (err < 0)
555877108e4SArnaldo Carvalho de Melo 		goto out;
556877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_comm_event__type);
557877108e4SArnaldo Carvalho de Melo 	if (err < 0)
558877108e4SArnaldo Carvalho de Melo 		goto out;
559877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_throttle_event__type);
560877108e4SArnaldo Carvalho de Melo 	if (err < 0)
561877108e4SArnaldo Carvalho de Melo 		goto out;
5623e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_read_event__type);
5633e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
5643e9f45a7SArnaldo Carvalho de Melo 		goto out;
5653e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_sample_event__type);
5663e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
5673e9f45a7SArnaldo Carvalho de Melo 		goto out;
568ae938802SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_context_switch_event__type);
569ae938802SArnaldo Carvalho de Melo 	if (err < 0)
570ae938802SArnaldo Carvalho de Melo 		goto out;
571877108e4SArnaldo Carvalho de Melo out:
572877108e4SArnaldo Carvalho de Melo 	return err;
573877108e4SArnaldo Carvalho de Melo }
574877108e4SArnaldo Carvalho de Melo 
575877108e4SArnaldo Carvalho de Melo static PyTypeObject *pyrf_event__type[] = {
576877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_MMAP]	 = &pyrf_mmap_event__type,
5773e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_LOST]	 = &pyrf_lost_event__type,
578877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_COMM]	 = &pyrf_comm_event__type,
579877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_EXIT]	 = &pyrf_task_event__type,
580877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_THROTTLE]	 = &pyrf_throttle_event__type,
581877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
582877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_FORK]	 = &pyrf_task_event__type,
5833e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_READ]	 = &pyrf_read_event__type,
5843e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_SAMPLE]	 = &pyrf_sample_event__type,
585ae938802SArnaldo Carvalho de Melo 	[PERF_RECORD_SWITCH]	 = &pyrf_context_switch_event__type,
586ae938802SArnaldo Carvalho de Melo 	[PERF_RECORD_SWITCH_CPU_WIDE]  = &pyrf_context_switch_event__type,
587877108e4SArnaldo Carvalho de Melo };
588877108e4SArnaldo Carvalho de Melo 
589877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_event__new(union perf_event *event)
590877108e4SArnaldo Carvalho de Melo {
591877108e4SArnaldo Carvalho de Melo 	struct pyrf_event *pevent;
592877108e4SArnaldo Carvalho de Melo 	PyTypeObject *ptype;
593877108e4SArnaldo Carvalho de Melo 
594ae938802SArnaldo Carvalho de Melo 	if ((event->header.type < PERF_RECORD_MMAP ||
595ae938802SArnaldo Carvalho de Melo 	     event->header.type > PERF_RECORD_SAMPLE) &&
596ae938802SArnaldo Carvalho de Melo 	    !(event->header.type == PERF_RECORD_SWITCH ||
597ae938802SArnaldo Carvalho de Melo 	      event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
598877108e4SArnaldo Carvalho de Melo 		return NULL;
599877108e4SArnaldo Carvalho de Melo 
600877108e4SArnaldo Carvalho de Melo 	ptype = pyrf_event__type[event->header.type];
601877108e4SArnaldo Carvalho de Melo 	pevent = PyObject_New(struct pyrf_event, ptype);
602877108e4SArnaldo Carvalho de Melo 	if (pevent != NULL)
603877108e4SArnaldo Carvalho de Melo 		memcpy(&pevent->event, event, event->header.size);
604877108e4SArnaldo Carvalho de Melo 	return (PyObject *)pevent;
605877108e4SArnaldo Carvalho de Melo }
606877108e4SArnaldo Carvalho de Melo 
607877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map {
608877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
609877108e4SArnaldo Carvalho de Melo 
610f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
611877108e4SArnaldo Carvalho de Melo };
612877108e4SArnaldo Carvalho de Melo 
613877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
614877108e4SArnaldo Carvalho de Melo 			      PyObject *args, PyObject *kwargs)
615877108e4SArnaldo Carvalho de Melo {
61664348153SFrederic Weisbecker 	static char *kwlist[] = { "cpustr", NULL };
617877108e4SArnaldo Carvalho de Melo 	char *cpustr = NULL;
618877108e4SArnaldo Carvalho de Melo 
619877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
620877108e4SArnaldo Carvalho de Melo 					 kwlist, &cpustr))
621877108e4SArnaldo Carvalho de Melo 		return -1;
622877108e4SArnaldo Carvalho de Melo 
6239c3516d1SJiri Olsa 	pcpus->cpus = perf_cpu_map__new(cpustr);
624877108e4SArnaldo Carvalho de Melo 	if (pcpus->cpus == NULL)
625877108e4SArnaldo Carvalho de Melo 		return -1;
626877108e4SArnaldo Carvalho de Melo 	return 0;
627877108e4SArnaldo Carvalho de Melo }
628877108e4SArnaldo Carvalho de Melo 
629877108e4SArnaldo Carvalho de Melo static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
630877108e4SArnaldo Carvalho de Melo {
63138f01d8dSJiri Olsa 	perf_cpu_map__put(pcpus->cpus);
63266dfdff0SJaroslav Škarvada 	Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
633877108e4SArnaldo Carvalho de Melo }
634877108e4SArnaldo Carvalho de Melo 
635877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
636877108e4SArnaldo Carvalho de Melo {
637877108e4SArnaldo Carvalho de Melo 	struct pyrf_cpu_map *pcpus = (void *)obj;
638877108e4SArnaldo Carvalho de Melo 
639877108e4SArnaldo Carvalho de Melo 	return pcpus->cpus->nr;
640877108e4SArnaldo Carvalho de Melo }
641877108e4SArnaldo Carvalho de Melo 
642877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
643877108e4SArnaldo Carvalho de Melo {
644877108e4SArnaldo Carvalho de Melo 	struct pyrf_cpu_map *pcpus = (void *)obj;
645877108e4SArnaldo Carvalho de Melo 
646877108e4SArnaldo Carvalho de Melo 	if (i >= pcpus->cpus->nr)
647877108e4SArnaldo Carvalho de Melo 		return NULL;
648877108e4SArnaldo Carvalho de Melo 
649877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", pcpus->cpus->map[i]);
650877108e4SArnaldo Carvalho de Melo }
651877108e4SArnaldo Carvalho de Melo 
652877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_cpu_map__sequence_methods = {
653877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_cpu_map__length,
654877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_cpu_map__item,
655877108e4SArnaldo Carvalho de Melo };
656877108e4SArnaldo Carvalho de Melo 
657877108e4SArnaldo Carvalho de Melo static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
658877108e4SArnaldo Carvalho de Melo 
659877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_cpu_map__type = {
660877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
661877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.cpu_map",
662877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_cpu_map),
663877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_cpu_map__delete,
664877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
665877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_cpu_map__doc,
666877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_cpu_map__sequence_methods,
667877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_cpu_map__init,
668877108e4SArnaldo Carvalho de Melo };
669877108e4SArnaldo Carvalho de Melo 
670877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__setup_types(void)
671877108e4SArnaldo Carvalho de Melo {
672877108e4SArnaldo Carvalho de Melo 	pyrf_cpu_map__type.tp_new = PyType_GenericNew;
673877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_cpu_map__type);
674877108e4SArnaldo Carvalho de Melo }
675877108e4SArnaldo Carvalho de Melo 
676877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map {
677877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
678877108e4SArnaldo Carvalho de Melo 
6799749b90eSJiri Olsa 	struct perf_thread_map *threads;
680877108e4SArnaldo Carvalho de Melo };
681877108e4SArnaldo Carvalho de Melo 
682877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
683877108e4SArnaldo Carvalho de Melo 				 PyObject *args, PyObject *kwargs)
684877108e4SArnaldo Carvalho de Melo {
6850d37aa34SArnaldo Carvalho de Melo 	static char *kwlist[] = { "pid", "tid", "uid", NULL };
6860d37aa34SArnaldo Carvalho de Melo 	int pid = -1, tid = -1, uid = UINT_MAX;
687877108e4SArnaldo Carvalho de Melo 
6880d37aa34SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
6890d37aa34SArnaldo Carvalho de Melo 					 kwlist, &pid, &tid, &uid))
690877108e4SArnaldo Carvalho de Melo 		return -1;
691877108e4SArnaldo Carvalho de Melo 
6920d37aa34SArnaldo Carvalho de Melo 	pthreads->threads = thread_map__new(pid, tid, uid);
693877108e4SArnaldo Carvalho de Melo 	if (pthreads->threads == NULL)
694877108e4SArnaldo Carvalho de Melo 		return -1;
695877108e4SArnaldo Carvalho de Melo 	return 0;
696877108e4SArnaldo Carvalho de Melo }
697877108e4SArnaldo Carvalho de Melo 
698877108e4SArnaldo Carvalho de Melo static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
699877108e4SArnaldo Carvalho de Melo {
7007836e52eSJiri Olsa 	perf_thread_map__put(pthreads->threads);
70166dfdff0SJaroslav Škarvada 	Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
702877108e4SArnaldo Carvalho de Melo }
703877108e4SArnaldo Carvalho de Melo 
704877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
705877108e4SArnaldo Carvalho de Melo {
706877108e4SArnaldo Carvalho de Melo 	struct pyrf_thread_map *pthreads = (void *)obj;
707877108e4SArnaldo Carvalho de Melo 
708877108e4SArnaldo Carvalho de Melo 	return pthreads->threads->nr;
709877108e4SArnaldo Carvalho de Melo }
710877108e4SArnaldo Carvalho de Melo 
711877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
712877108e4SArnaldo Carvalho de Melo {
713877108e4SArnaldo Carvalho de Melo 	struct pyrf_thread_map *pthreads = (void *)obj;
714877108e4SArnaldo Carvalho de Melo 
715877108e4SArnaldo Carvalho de Melo 	if (i >= pthreads->threads->nr)
716877108e4SArnaldo Carvalho de Melo 		return NULL;
717877108e4SArnaldo Carvalho de Melo 
718877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", pthreads->threads->map[i]);
719877108e4SArnaldo Carvalho de Melo }
720877108e4SArnaldo Carvalho de Melo 
721877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_thread_map__sequence_methods = {
722877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_thread_map__length,
723877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_thread_map__item,
724877108e4SArnaldo Carvalho de Melo };
725877108e4SArnaldo Carvalho de Melo 
726877108e4SArnaldo Carvalho de Melo static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
727877108e4SArnaldo Carvalho de Melo 
728877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_thread_map__type = {
729877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
730877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.thread_map",
731877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_thread_map),
732877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_thread_map__delete,
733877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
734877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_thread_map__doc,
735877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_thread_map__sequence_methods,
736877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_thread_map__init,
737877108e4SArnaldo Carvalho de Melo };
738877108e4SArnaldo Carvalho de Melo 
739877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__setup_types(void)
740877108e4SArnaldo Carvalho de Melo {
741877108e4SArnaldo Carvalho de Melo 	pyrf_thread_map__type.tp_new = PyType_GenericNew;
742877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_thread_map__type);
743877108e4SArnaldo Carvalho de Melo }
744877108e4SArnaldo Carvalho de Melo 
745877108e4SArnaldo Carvalho de Melo struct pyrf_evsel {
746877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
747877108e4SArnaldo Carvalho de Melo 
74832dcd021SJiri Olsa 	struct evsel evsel;
749877108e4SArnaldo Carvalho de Melo };
750877108e4SArnaldo Carvalho de Melo 
751877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
752877108e4SArnaldo Carvalho de Melo 			    PyObject *args, PyObject *kwargs)
753877108e4SArnaldo Carvalho de Melo {
754877108e4SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
755877108e4SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
756877108e4SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
757877108e4SArnaldo Carvalho de Melo 		.sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
758877108e4SArnaldo Carvalho de Melo 	};
759877108e4SArnaldo Carvalho de Melo 	static char *kwlist[] = {
760877108e4SArnaldo Carvalho de Melo 		"type",
761877108e4SArnaldo Carvalho de Melo 		"config",
762877108e4SArnaldo Carvalho de Melo 		"sample_freq",
763877108e4SArnaldo Carvalho de Melo 		"sample_period",
764877108e4SArnaldo Carvalho de Melo 		"sample_type",
765877108e4SArnaldo Carvalho de Melo 		"read_format",
766877108e4SArnaldo Carvalho de Melo 		"disabled",
767877108e4SArnaldo Carvalho de Melo 		"inherit",
768877108e4SArnaldo Carvalho de Melo 		"pinned",
769877108e4SArnaldo Carvalho de Melo 		"exclusive",
770877108e4SArnaldo Carvalho de Melo 		"exclude_user",
771877108e4SArnaldo Carvalho de Melo 		"exclude_kernel",
772877108e4SArnaldo Carvalho de Melo 		"exclude_hv",
773877108e4SArnaldo Carvalho de Melo 		"exclude_idle",
774877108e4SArnaldo Carvalho de Melo 		"mmap",
775ae938802SArnaldo Carvalho de Melo 		"context_switch",
776877108e4SArnaldo Carvalho de Melo 		"comm",
777877108e4SArnaldo Carvalho de Melo 		"freq",
778877108e4SArnaldo Carvalho de Melo 		"inherit_stat",
779877108e4SArnaldo Carvalho de Melo 		"enable_on_exec",
780877108e4SArnaldo Carvalho de Melo 		"task",
781877108e4SArnaldo Carvalho de Melo 		"watermark",
782877108e4SArnaldo Carvalho de Melo 		"precise_ip",
783877108e4SArnaldo Carvalho de Melo 		"mmap_data",
784877108e4SArnaldo Carvalho de Melo 		"sample_id_all",
785877108e4SArnaldo Carvalho de Melo 		"wakeup_events",
786877108e4SArnaldo Carvalho de Melo 		"bp_type",
787877108e4SArnaldo Carvalho de Melo 		"bp_addr",
78864348153SFrederic Weisbecker 		"bp_len",
78964348153SFrederic Weisbecker 		 NULL
79064348153SFrederic Weisbecker 	};
791877108e4SArnaldo Carvalho de Melo 	u64 sample_period = 0;
792877108e4SArnaldo Carvalho de Melo 	u32 disabled = 0,
793877108e4SArnaldo Carvalho de Melo 	    inherit = 0,
794877108e4SArnaldo Carvalho de Melo 	    pinned = 0,
795877108e4SArnaldo Carvalho de Melo 	    exclusive = 0,
796877108e4SArnaldo Carvalho de Melo 	    exclude_user = 0,
797877108e4SArnaldo Carvalho de Melo 	    exclude_kernel = 0,
798877108e4SArnaldo Carvalho de Melo 	    exclude_hv = 0,
799877108e4SArnaldo Carvalho de Melo 	    exclude_idle = 0,
800877108e4SArnaldo Carvalho de Melo 	    mmap = 0,
801ae938802SArnaldo Carvalho de Melo 	    context_switch = 0,
802877108e4SArnaldo Carvalho de Melo 	    comm = 0,
803877108e4SArnaldo Carvalho de Melo 	    freq = 1,
804877108e4SArnaldo Carvalho de Melo 	    inherit_stat = 0,
805877108e4SArnaldo Carvalho de Melo 	    enable_on_exec = 0,
806877108e4SArnaldo Carvalho de Melo 	    task = 0,
807877108e4SArnaldo Carvalho de Melo 	    watermark = 0,
808877108e4SArnaldo Carvalho de Melo 	    precise_ip = 0,
809877108e4SArnaldo Carvalho de Melo 	    mmap_data = 0,
810877108e4SArnaldo Carvalho de Melo 	    sample_id_all = 1;
811877108e4SArnaldo Carvalho de Melo 	int idx = 0;
812877108e4SArnaldo Carvalho de Melo 
813877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
814ae938802SArnaldo Carvalho de Melo 					 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
815877108e4SArnaldo Carvalho de Melo 					 &attr.type, &attr.config, &attr.sample_freq,
816877108e4SArnaldo Carvalho de Melo 					 &sample_period, &attr.sample_type,
817877108e4SArnaldo Carvalho de Melo 					 &attr.read_format, &disabled, &inherit,
818877108e4SArnaldo Carvalho de Melo 					 &pinned, &exclusive, &exclude_user,
819877108e4SArnaldo Carvalho de Melo 					 &exclude_kernel, &exclude_hv, &exclude_idle,
820ae938802SArnaldo Carvalho de Melo 					 &mmap, &context_switch, &comm, &freq, &inherit_stat,
821877108e4SArnaldo Carvalho de Melo 					 &enable_on_exec, &task, &watermark,
822877108e4SArnaldo Carvalho de Melo 					 &precise_ip, &mmap_data, &sample_id_all,
823877108e4SArnaldo Carvalho de Melo 					 &attr.wakeup_events, &attr.bp_type,
824877108e4SArnaldo Carvalho de Melo 					 &attr.bp_addr, &attr.bp_len, &idx))
825877108e4SArnaldo Carvalho de Melo 		return -1;
826877108e4SArnaldo Carvalho de Melo 
827877108e4SArnaldo Carvalho de Melo 	/* union... */
828877108e4SArnaldo Carvalho de Melo 	if (sample_period != 0) {
829877108e4SArnaldo Carvalho de Melo 		if (attr.sample_freq != 0)
830877108e4SArnaldo Carvalho de Melo 			return -1; /* FIXME: throw right exception */
831877108e4SArnaldo Carvalho de Melo 		attr.sample_period = sample_period;
832877108e4SArnaldo Carvalho de Melo 	}
833877108e4SArnaldo Carvalho de Melo 
834877108e4SArnaldo Carvalho de Melo 	/* Bitfields */
835877108e4SArnaldo Carvalho de Melo 	attr.disabled	    = disabled;
836877108e4SArnaldo Carvalho de Melo 	attr.inherit	    = inherit;
837877108e4SArnaldo Carvalho de Melo 	attr.pinned	    = pinned;
838877108e4SArnaldo Carvalho de Melo 	attr.exclusive	    = exclusive;
839877108e4SArnaldo Carvalho de Melo 	attr.exclude_user   = exclude_user;
840877108e4SArnaldo Carvalho de Melo 	attr.exclude_kernel = exclude_kernel;
841877108e4SArnaldo Carvalho de Melo 	attr.exclude_hv	    = exclude_hv;
842877108e4SArnaldo Carvalho de Melo 	attr.exclude_idle   = exclude_idle;
843877108e4SArnaldo Carvalho de Melo 	attr.mmap	    = mmap;
844ae938802SArnaldo Carvalho de Melo 	attr.context_switch = context_switch;
845877108e4SArnaldo Carvalho de Melo 	attr.comm	    = comm;
846877108e4SArnaldo Carvalho de Melo 	attr.freq	    = freq;
847877108e4SArnaldo Carvalho de Melo 	attr.inherit_stat   = inherit_stat;
848877108e4SArnaldo Carvalho de Melo 	attr.enable_on_exec = enable_on_exec;
849877108e4SArnaldo Carvalho de Melo 	attr.task	    = task;
850877108e4SArnaldo Carvalho de Melo 	attr.watermark	    = watermark;
851877108e4SArnaldo Carvalho de Melo 	attr.precise_ip	    = precise_ip;
852877108e4SArnaldo Carvalho de Melo 	attr.mmap_data	    = mmap_data;
853877108e4SArnaldo Carvalho de Melo 	attr.sample_id_all  = sample_id_all;
854ad4e3c04SJiri Olsa 	attr.size	    = sizeof(attr);
855877108e4SArnaldo Carvalho de Melo 
856b4b62ee6SJiri Olsa 	evsel__init(&pevsel->evsel, &attr, idx);
857877108e4SArnaldo Carvalho de Melo 	return 0;
858877108e4SArnaldo Carvalho de Melo }
859877108e4SArnaldo Carvalho de Melo 
860877108e4SArnaldo Carvalho de Melo static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
861877108e4SArnaldo Carvalho de Melo {
86230f7c591SArnaldo Carvalho de Melo 	evsel__exit(&pevsel->evsel);
86366dfdff0SJaroslav Škarvada 	Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
864877108e4SArnaldo Carvalho de Melo }
865877108e4SArnaldo Carvalho de Melo 
866877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
867877108e4SArnaldo Carvalho de Melo 				  PyObject *args, PyObject *kwargs)
868877108e4SArnaldo Carvalho de Melo {
86932dcd021SJiri Olsa 	struct evsel *evsel = &pevsel->evsel;
870f854839bSJiri Olsa 	struct perf_cpu_map *cpus = NULL;
8719749b90eSJiri Olsa 	struct perf_thread_map *threads = NULL;
872877108e4SArnaldo Carvalho de Melo 	PyObject *pcpus = NULL, *pthreads = NULL;
8735d2cd909SArnaldo Carvalho de Melo 	int group = 0, inherit = 0;
87464348153SFrederic Weisbecker 	static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
875877108e4SArnaldo Carvalho de Melo 
876877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
8775d2cd909SArnaldo Carvalho de Melo 					 &pcpus, &pthreads, &group, &inherit))
878877108e4SArnaldo Carvalho de Melo 		return NULL;
879877108e4SArnaldo Carvalho de Melo 
880877108e4SArnaldo Carvalho de Melo 	if (pthreads != NULL)
881877108e4SArnaldo Carvalho de Melo 		threads = ((struct pyrf_thread_map *)pthreads)->threads;
882877108e4SArnaldo Carvalho de Melo 
883877108e4SArnaldo Carvalho de Melo 	if (pcpus != NULL)
884877108e4SArnaldo Carvalho de Melo 		cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
885877108e4SArnaldo Carvalho de Melo 
8861fc632ceSJiri Olsa 	evsel->core.attr.inherit = inherit;
887727ab04eSArnaldo Carvalho de Melo 	/*
888727ab04eSArnaldo Carvalho de Melo 	 * This will group just the fds for this single evsel, to group
889727ab04eSArnaldo Carvalho de Melo 	 * multiple events, use evlist.open().
890727ab04eSArnaldo Carvalho de Melo 	 */
8915972d1e0SJiri Olsa 	if (evsel__open(evsel, cpus, threads) < 0) {
892877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
893877108e4SArnaldo Carvalho de Melo 		return NULL;
894877108e4SArnaldo Carvalho de Melo 	}
895877108e4SArnaldo Carvalho de Melo 
896877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
897877108e4SArnaldo Carvalho de Melo 	return Py_None;
898877108e4SArnaldo Carvalho de Melo }
899877108e4SArnaldo Carvalho de Melo 
900877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evsel__methods[] = {
901877108e4SArnaldo Carvalho de Melo 	{
902877108e4SArnaldo Carvalho de Melo 		.ml_name  = "open",
903877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evsel__open,
904877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
905877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("open the event selector file descriptor table.")
906877108e4SArnaldo Carvalho de Melo 	},
907f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
908877108e4SArnaldo Carvalho de Melo };
909877108e4SArnaldo Carvalho de Melo 
910877108e4SArnaldo Carvalho de Melo static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
911877108e4SArnaldo Carvalho de Melo 
912877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evsel__type = {
913877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
914877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.evsel",
915877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_evsel),
916877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_evsel__delete,
917877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
918877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_evsel__doc,
919877108e4SArnaldo Carvalho de Melo 	.tp_methods	= pyrf_evsel__methods,
920877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_evsel__init,
921877108e4SArnaldo Carvalho de Melo };
922877108e4SArnaldo Carvalho de Melo 
923877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__setup_types(void)
924877108e4SArnaldo Carvalho de Melo {
925877108e4SArnaldo Carvalho de Melo 	pyrf_evsel__type.tp_new = PyType_GenericNew;
926877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_evsel__type);
927877108e4SArnaldo Carvalho de Melo }
928877108e4SArnaldo Carvalho de Melo 
929877108e4SArnaldo Carvalho de Melo struct pyrf_evlist {
930877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
931877108e4SArnaldo Carvalho de Melo 
93263503dbaSJiri Olsa 	struct evlist evlist;
933877108e4SArnaldo Carvalho de Melo };
934877108e4SArnaldo Carvalho de Melo 
935877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
9361d037ca1SIrina Tirdea 			     PyObject *args, PyObject *kwargs __maybe_unused)
937877108e4SArnaldo Carvalho de Melo {
9387e2ed097SArnaldo Carvalho de Melo 	PyObject *pcpus = NULL, *pthreads = NULL;
939f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
9409749b90eSJiri Olsa 	struct perf_thread_map *threads;
9417e2ed097SArnaldo Carvalho de Melo 
9427e2ed097SArnaldo Carvalho de Melo 	if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
9437e2ed097SArnaldo Carvalho de Melo 		return -1;
9447e2ed097SArnaldo Carvalho de Melo 
9457e2ed097SArnaldo Carvalho de Melo 	threads = ((struct pyrf_thread_map *)pthreads)->threads;
9467e2ed097SArnaldo Carvalho de Melo 	cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
94752c86bcaSJiri Olsa 	evlist__init(&pevlist->evlist, cpus, threads);
948877108e4SArnaldo Carvalho de Melo 	return 0;
949877108e4SArnaldo Carvalho de Melo }
950877108e4SArnaldo Carvalho de Melo 
951877108e4SArnaldo Carvalho de Melo static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
952877108e4SArnaldo Carvalho de Melo {
953470579b0SJiri Olsa 	evlist__exit(&pevlist->evlist);
95466dfdff0SJaroslav Škarvada 	Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
955877108e4SArnaldo Carvalho de Melo }
956877108e4SArnaldo Carvalho de Melo 
957877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
958877108e4SArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
959877108e4SArnaldo Carvalho de Melo {
96063503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
96164348153SFrederic Weisbecker 	static char *kwlist[] = { "pages", "overwrite", NULL };
962877108e4SArnaldo Carvalho de Melo 	int pages = 128, overwrite = false;
963877108e4SArnaldo Carvalho de Melo 
9647e2ed097SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
9657e2ed097SArnaldo Carvalho de Melo 					 &pages, &overwrite))
966877108e4SArnaldo Carvalho de Melo 		return NULL;
967877108e4SArnaldo Carvalho de Melo 
9689521b5f2SJiri Olsa 	if (evlist__mmap(evlist, pages) < 0) {
969877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
970877108e4SArnaldo Carvalho de Melo 		return NULL;
971877108e4SArnaldo Carvalho de Melo 	}
972877108e4SArnaldo Carvalho de Melo 
973877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
974877108e4SArnaldo Carvalho de Melo 	return Py_None;
975877108e4SArnaldo Carvalho de Melo }
976877108e4SArnaldo Carvalho de Melo 
977877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
978877108e4SArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
979877108e4SArnaldo Carvalho de Melo {
98063503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
98164348153SFrederic Weisbecker 	static char *kwlist[] = { "timeout", NULL };
982877108e4SArnaldo Carvalho de Melo 	int timeout = -1, n;
983877108e4SArnaldo Carvalho de Melo 
984877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
985877108e4SArnaldo Carvalho de Melo 		return NULL;
986877108e4SArnaldo Carvalho de Melo 
98780ab2987SJiri Olsa 	n = evlist__poll(evlist, timeout);
988877108e4SArnaldo Carvalho de Melo 	if (n < 0) {
989877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
990877108e4SArnaldo Carvalho de Melo 		return NULL;
991877108e4SArnaldo Carvalho de Melo 	}
992877108e4SArnaldo Carvalho de Melo 
993877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", n);
994877108e4SArnaldo Carvalho de Melo }
995877108e4SArnaldo Carvalho de Melo 
996877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
9971d037ca1SIrina Tirdea 					 PyObject *args __maybe_unused,
9981d037ca1SIrina Tirdea 					 PyObject *kwargs __maybe_unused)
999877108e4SArnaldo Carvalho de Melo {
100063503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
1001877108e4SArnaldo Carvalho de Melo         PyObject *list = PyList_New(0);
1002877108e4SArnaldo Carvalho de Melo 	int i;
1003877108e4SArnaldo Carvalho de Melo 
100440cb2d51SJiri Olsa 	for (i = 0; i < evlist->core.pollfd.nr; ++i) {
1005877108e4SArnaldo Carvalho de Melo 		PyObject *file;
100666dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
100740cb2d51SJiri Olsa 		FILE *fp = fdopen(evlist->core.pollfd.entries[i].fd, "r");
1008877108e4SArnaldo Carvalho de Melo 
1009877108e4SArnaldo Carvalho de Melo 		if (fp == NULL)
1010877108e4SArnaldo Carvalho de Melo 			goto free_list;
1011877108e4SArnaldo Carvalho de Melo 
1012877108e4SArnaldo Carvalho de Melo 		file = PyFile_FromFile(fp, "perf", "r", NULL);
101366dfdff0SJaroslav Škarvada #else
101440cb2d51SJiri Olsa 		file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1,
1015a389aeceSJiri Olsa 				     NULL, NULL, NULL, 0);
101666dfdff0SJaroslav Škarvada #endif
1017877108e4SArnaldo Carvalho de Melo 		if (file == NULL)
1018877108e4SArnaldo Carvalho de Melo 			goto free_list;
1019877108e4SArnaldo Carvalho de Melo 
1020877108e4SArnaldo Carvalho de Melo 		if (PyList_Append(list, file) != 0) {
1021877108e4SArnaldo Carvalho de Melo 			Py_DECREF(file);
1022877108e4SArnaldo Carvalho de Melo 			goto free_list;
1023877108e4SArnaldo Carvalho de Melo 		}
1024877108e4SArnaldo Carvalho de Melo 
1025877108e4SArnaldo Carvalho de Melo 		Py_DECREF(file);
1026877108e4SArnaldo Carvalho de Melo 	}
1027877108e4SArnaldo Carvalho de Melo 
1028877108e4SArnaldo Carvalho de Melo 	return list;
1029877108e4SArnaldo Carvalho de Melo free_list:
1030877108e4SArnaldo Carvalho de Melo 	return PyErr_NoMemory();
1031877108e4SArnaldo Carvalho de Melo }
1032877108e4SArnaldo Carvalho de Melo 
1033877108e4SArnaldo Carvalho de Melo 
1034877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
10351d037ca1SIrina Tirdea 				  PyObject *args,
10361d037ca1SIrina Tirdea 				  PyObject *kwargs __maybe_unused)
1037877108e4SArnaldo Carvalho de Melo {
103863503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
1039877108e4SArnaldo Carvalho de Melo 	PyObject *pevsel;
104032dcd021SJiri Olsa 	struct evsel *evsel;
1041877108e4SArnaldo Carvalho de Melo 
1042877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTuple(args, "O", &pevsel))
1043877108e4SArnaldo Carvalho de Melo 		return NULL;
1044877108e4SArnaldo Carvalho de Melo 
1045877108e4SArnaldo Carvalho de Melo 	Py_INCREF(pevsel);
1046877108e4SArnaldo Carvalho de Melo 	evsel = &((struct pyrf_evsel *)pevsel)->evsel;
104738fe0e01SJiri Olsa 	evsel->core.idx = evlist->core.nr_entries;
1048a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
1049877108e4SArnaldo Carvalho de Melo 
10506484d2f9SJiri Olsa 	return Py_BuildValue("i", evlist->core.nr_entries);
1051877108e4SArnaldo Carvalho de Melo }
1052877108e4SArnaldo Carvalho de Melo 
1053a5830532SJiri Olsa static struct mmap *get_md(struct evlist *evlist, int cpu)
1054721f0dfcSJiri Olsa {
1055721f0dfcSJiri Olsa 	int i;
1056721f0dfcSJiri Olsa 
1057c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
1058a5830532SJiri Olsa 		struct mmap *md = &evlist->mmap[i];
1059721f0dfcSJiri Olsa 
1060*6d18804bSIan Rogers 		if (md->core.cpu.cpu == cpu)
1061721f0dfcSJiri Olsa 			return md;
1062721f0dfcSJiri Olsa 	}
1063721f0dfcSJiri Olsa 
1064721f0dfcSJiri Olsa 	return NULL;
1065721f0dfcSJiri Olsa }
1066721f0dfcSJiri Olsa 
1067877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
1068877108e4SArnaldo Carvalho de Melo 					  PyObject *args, PyObject *kwargs)
1069877108e4SArnaldo Carvalho de Melo {
107063503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
1071877108e4SArnaldo Carvalho de Melo 	union perf_event *event;
1072877108e4SArnaldo Carvalho de Melo 	int sample_id_all = 1, cpu;
107364348153SFrederic Weisbecker 	static char *kwlist[] = { "cpu", "sample_id_all", NULL };
1074a5830532SJiri Olsa 	struct mmap *md;
10755538becaSFrederic Weisbecker 	int err;
1076877108e4SArnaldo Carvalho de Melo 
1077877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1078877108e4SArnaldo Carvalho de Melo 					 &cpu, &sample_id_all))
1079877108e4SArnaldo Carvalho de Melo 		return NULL;
1080877108e4SArnaldo Carvalho de Melo 
1081721f0dfcSJiri Olsa 	md = get_md(evlist, cpu);
1082721f0dfcSJiri Olsa 	if (!md)
1083721f0dfcSJiri Olsa 		return NULL;
1084721f0dfcSJiri Olsa 
10857c4d4182SJiri Olsa 	if (perf_mmap__read_init(&md->core) < 0)
108635b7cdc6SKan Liang 		goto end;
108735b7cdc6SKan Liang 
1088151ed5d7SJiri Olsa 	event = perf_mmap__read_event(&md->core);
1089877108e4SArnaldo Carvalho de Melo 	if (event != NULL) {
1090877108e4SArnaldo Carvalho de Melo 		PyObject *pyevent = pyrf_event__new(event);
1091877108e4SArnaldo Carvalho de Melo 		struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
109232dcd021SJiri Olsa 		struct evsel *evsel;
1093877108e4SArnaldo Carvalho de Melo 
1094877108e4SArnaldo Carvalho de Melo 		if (pyevent == NULL)
1095877108e4SArnaldo Carvalho de Melo 			return PyErr_NoMemory();
1096877108e4SArnaldo Carvalho de Melo 
10973ccf8a7bSArnaldo Carvalho de Melo 		evsel = evlist__event2evsel(evlist, event);
109883428f2fSPetr Machata 		if (!evsel) {
109983428f2fSPetr Machata 			Py_INCREF(Py_None);
1100377f698dSJiri Olsa 			return Py_None;
110183428f2fSPetr Machata 		}
1102377f698dSJiri Olsa 
1103377f698dSJiri Olsa 		pevent->evsel = evsel;
1104377f698dSJiri Olsa 
11056b6017a2SArnaldo Carvalho de Melo 		err = evsel__parse_sample(evsel, event, &pevent->sample);
1106e8968e65SJiri Olsa 
1107e8968e65SJiri Olsa 		/* Consume the even only after we parsed it out. */
11087728fa0cSJiri Olsa 		perf_mmap__consume(&md->core);
1109e8968e65SJiri Olsa 
11105c6970afSArnaldo Carvalho de Melo 		if (err)
11115c6970afSArnaldo Carvalho de Melo 			return PyErr_Format(PyExc_OSError,
11125c6970afSArnaldo Carvalho de Melo 					    "perf: can't parse sample, err=%d", err);
11135538becaSFrederic Weisbecker 		return pyevent;
11145538becaSFrederic Weisbecker 	}
111535b7cdc6SKan Liang end:
1116877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
1117877108e4SArnaldo Carvalho de Melo 	return Py_None;
1118877108e4SArnaldo Carvalho de Melo }
1119877108e4SArnaldo Carvalho de Melo 
1120727ab04eSArnaldo Carvalho de Melo static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1121727ab04eSArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
1122727ab04eSArnaldo Carvalho de Melo {
112363503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
1124727ab04eSArnaldo Carvalho de Melo 	int group = 0;
1125727ab04eSArnaldo Carvalho de Melo 	static char *kwlist[] = { "group", NULL };
1126727ab04eSArnaldo Carvalho de Melo 
1127727ab04eSArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1128727ab04eSArnaldo Carvalho de Melo 		return NULL;
1129727ab04eSArnaldo Carvalho de Melo 
11306a4bb04cSJiri Olsa 	if (group)
1131a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
11326a4bb04cSJiri Olsa 
1133474ddc4cSJiri Olsa 	if (evlist__open(evlist) < 0) {
1134727ab04eSArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
1135727ab04eSArnaldo Carvalho de Melo 		return NULL;
1136727ab04eSArnaldo Carvalho de Melo 	}
1137727ab04eSArnaldo Carvalho de Melo 
1138727ab04eSArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
1139727ab04eSArnaldo Carvalho de Melo 	return Py_None;
1140727ab04eSArnaldo Carvalho de Melo }
1141727ab04eSArnaldo Carvalho de Melo 
1142877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evlist__methods[] = {
1143877108e4SArnaldo Carvalho de Melo 	{
1144877108e4SArnaldo Carvalho de Melo 		.ml_name  = "mmap",
1145877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__mmap,
1146877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1147877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("mmap the file descriptor table.")
1148877108e4SArnaldo Carvalho de Melo 	},
1149877108e4SArnaldo Carvalho de Melo 	{
1150727ab04eSArnaldo Carvalho de Melo 		.ml_name  = "open",
1151727ab04eSArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__open,
1152727ab04eSArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1153727ab04eSArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("open the file descriptors.")
1154727ab04eSArnaldo Carvalho de Melo 	},
1155727ab04eSArnaldo Carvalho de Melo 	{
1156877108e4SArnaldo Carvalho de Melo 		.ml_name  = "poll",
1157877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__poll,
1158877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1159877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("poll the file descriptor table.")
1160877108e4SArnaldo Carvalho de Melo 	},
1161877108e4SArnaldo Carvalho de Melo 	{
1162877108e4SArnaldo Carvalho de Melo 		.ml_name  = "get_pollfd",
1163877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__get_pollfd,
1164877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1165877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("get the poll file descriptor table.")
1166877108e4SArnaldo Carvalho de Melo 	},
1167877108e4SArnaldo Carvalho de Melo 	{
1168877108e4SArnaldo Carvalho de Melo 		.ml_name  = "add",
1169877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__add,
1170877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1171877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("adds an event selector to the list.")
1172877108e4SArnaldo Carvalho de Melo 	},
1173877108e4SArnaldo Carvalho de Melo 	{
1174877108e4SArnaldo Carvalho de Melo 		.ml_name  = "read_on_cpu",
1175877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__read_on_cpu,
1176877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1177877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("reads an event.")
1178877108e4SArnaldo Carvalho de Melo 	},
1179f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
1180877108e4SArnaldo Carvalho de Melo };
1181877108e4SArnaldo Carvalho de Melo 
1182877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1183877108e4SArnaldo Carvalho de Melo {
1184877108e4SArnaldo Carvalho de Melo 	struct pyrf_evlist *pevlist = (void *)obj;
1185877108e4SArnaldo Carvalho de Melo 
11866484d2f9SJiri Olsa 	return pevlist->evlist.core.nr_entries;
1187877108e4SArnaldo Carvalho de Melo }
1188877108e4SArnaldo Carvalho de Melo 
1189877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1190877108e4SArnaldo Carvalho de Melo {
1191877108e4SArnaldo Carvalho de Melo 	struct pyrf_evlist *pevlist = (void *)obj;
119232dcd021SJiri Olsa 	struct evsel *pos;
1193877108e4SArnaldo Carvalho de Melo 
11946484d2f9SJiri Olsa 	if (i >= pevlist->evlist.core.nr_entries)
1195877108e4SArnaldo Carvalho de Melo 		return NULL;
1196877108e4SArnaldo Carvalho de Melo 
1197e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(&pevlist->evlist, pos) {
1198877108e4SArnaldo Carvalho de Melo 		if (i-- == 0)
1199877108e4SArnaldo Carvalho de Melo 			break;
12000050f7aaSArnaldo Carvalho de Melo 	}
1201877108e4SArnaldo Carvalho de Melo 
1202877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1203877108e4SArnaldo Carvalho de Melo }
1204877108e4SArnaldo Carvalho de Melo 
1205877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_evlist__sequence_methods = {
1206877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_evlist__length,
1207877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_evlist__item,
1208877108e4SArnaldo Carvalho de Melo };
1209877108e4SArnaldo Carvalho de Melo 
1210877108e4SArnaldo Carvalho de Melo static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1211877108e4SArnaldo Carvalho de Melo 
1212877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evlist__type = {
1213877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
1214877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.evlist",
1215877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_evlist),
1216877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_evlist__delete,
1217877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1218877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_evlist__sequence_methods,
1219877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_evlist__doc,
1220877108e4SArnaldo Carvalho de Melo 	.tp_methods	= pyrf_evlist__methods,
1221877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_evlist__init,
1222877108e4SArnaldo Carvalho de Melo };
1223877108e4SArnaldo Carvalho de Melo 
1224877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__setup_types(void)
1225877108e4SArnaldo Carvalho de Melo {
1226877108e4SArnaldo Carvalho de Melo 	pyrf_evlist__type.tp_new = PyType_GenericNew;
1227877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_evlist__type);
1228877108e4SArnaldo Carvalho de Melo }
1229877108e4SArnaldo Carvalho de Melo 
12305865fe36SArnaldo Carvalho de Melo #define PERF_CONST(name) { #name, PERF_##name }
12315865fe36SArnaldo Carvalho de Melo 
1232877108e4SArnaldo Carvalho de Melo static struct {
1233877108e4SArnaldo Carvalho de Melo 	const char *name;
1234877108e4SArnaldo Carvalho de Melo 	int	    value;
1235877108e4SArnaldo Carvalho de Melo } perf__constants[] = {
12365865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_HARDWARE),
12375865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_SOFTWARE),
12385865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_TRACEPOINT),
12395865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_HW_CACHE),
12405865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_RAW),
12415865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_BREAKPOINT),
1242877108e4SArnaldo Carvalho de Melo 
12435865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CPU_CYCLES),
12445865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_INSTRUCTIONS),
12455865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_REFERENCES),
12465865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_MISSES),
12475865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
12485865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_BRANCH_MISSES),
12495865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_BUS_CYCLES),
12505865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_L1D),
12515865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_L1I),
12525865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_LL),
12535865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_DTLB),
12545865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_ITLB),
12555865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_BPU),
12565865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_OP_READ),
12575865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
12585865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
12595865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
12605865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1261877108e4SArnaldo Carvalho de Melo 
12625865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
12635865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1264129c04cbSIngo Molnar 
12655865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_CPU_CLOCK),
12665865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_TASK_CLOCK),
12675865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_PAGE_FAULTS),
12685865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
12695865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
12705865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
12715865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
12725865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
12735865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_EMULATION_FAULTS),
12745865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_DUMMY),
1275877108e4SArnaldo Carvalho de Melo 
12765865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_IP),
12775865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_TID),
12785865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_TIME),
12795865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_ADDR),
12805865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_READ),
12815865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_CALLCHAIN),
12825865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_ID),
12835865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_CPU),
12845865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_PERIOD),
12855865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_STREAM_ID),
12865865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_RAW),
1287877108e4SArnaldo Carvalho de Melo 
12885865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
12895865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
12905865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_ID),
12915865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_GROUP),
1292877108e4SArnaldo Carvalho de Melo 
12935865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_MMAP),
12945865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_LOST),
12955865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_COMM),
12965865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_EXIT),
12975865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_THROTTLE),
12985865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_UNTHROTTLE),
12995865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_FORK),
13005865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_READ),
13015865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_SAMPLE),
130284576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_MMAP2),
130384576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_AUX),
130484576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_ITRACE_START),
130584576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_LOST_SAMPLES),
130684576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_SWITCH),
130784576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1308ae938802SArnaldo Carvalho de Melo 
1309ae938802SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_MISC_SWITCH_OUT),
1310f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
1311877108e4SArnaldo Carvalho de Melo };
1312877108e4SArnaldo Carvalho de Melo 
13131075fbb2SJiri Olsa static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
13141075fbb2SJiri Olsa 				  PyObject *args, PyObject *kwargs)
13151075fbb2SJiri Olsa {
131697fbf3f0STzvetomir Stoyanov 	struct tep_event *tp_format;
13171075fbb2SJiri Olsa 	static char *kwlist[] = { "sys", "name", NULL };
13181075fbb2SJiri Olsa 	char *sys  = NULL;
13191075fbb2SJiri Olsa 	char *name = NULL;
13201075fbb2SJiri Olsa 
13211075fbb2SJiri Olsa 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
13221075fbb2SJiri Olsa 					 &sys, &name))
13231075fbb2SJiri Olsa 		return NULL;
13241075fbb2SJiri Olsa 
13251075fbb2SJiri Olsa 	tp_format = trace_event__tp_format(sys, name);
13261075fbb2SJiri Olsa 	if (IS_ERR(tp_format))
132766dfdff0SJaroslav Škarvada 		return _PyLong_FromLong(-1);
13281075fbb2SJiri Olsa 
132966dfdff0SJaroslav Škarvada 	return _PyLong_FromLong(tp_format->id);
13301075fbb2SJiri Olsa }
13311075fbb2SJiri Olsa 
1332877108e4SArnaldo Carvalho de Melo static PyMethodDef perf__methods[] = {
13331075fbb2SJiri Olsa 	{
13341075fbb2SJiri Olsa 		.ml_name  = "tracepoint",
13351075fbb2SJiri Olsa 		.ml_meth  = (PyCFunction) pyrf__tracepoint,
13361075fbb2SJiri Olsa 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
13371075fbb2SJiri Olsa 		.ml_doc	  = PyDoc_STR("Get tracepoint config.")
13381075fbb2SJiri Olsa 	},
1339f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
1340877108e4SArnaldo Carvalho de Melo };
1341877108e4SArnaldo Carvalho de Melo 
134266dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
1343877108e4SArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void)
134466dfdff0SJaroslav Škarvada #else
134566dfdff0SJaroslav Škarvada PyMODINIT_FUNC PyInit_perf(void)
134666dfdff0SJaroslav Škarvada #endif
1347877108e4SArnaldo Carvalho de Melo {
1348877108e4SArnaldo Carvalho de Melo 	PyObject *obj;
1349877108e4SArnaldo Carvalho de Melo 	int i;
135066dfdff0SJaroslav Škarvada 	PyObject *dict;
135166dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
135266dfdff0SJaroslav Škarvada 	PyObject *module = Py_InitModule("perf", perf__methods);
135366dfdff0SJaroslav Škarvada #else
135466dfdff0SJaroslav Škarvada 	static struct PyModuleDef moduledef = {
135566dfdff0SJaroslav Škarvada 		PyModuleDef_HEAD_INIT,
135666dfdff0SJaroslav Škarvada 		"perf",			/* m_name */
135766dfdff0SJaroslav Škarvada 		"",			/* m_doc */
135866dfdff0SJaroslav Škarvada 		-1,			/* m_size */
135966dfdff0SJaroslav Škarvada 		perf__methods,		/* m_methods */
136066dfdff0SJaroslav Škarvada 		NULL,			/* m_reload */
136166dfdff0SJaroslav Škarvada 		NULL,			/* m_traverse */
136266dfdff0SJaroslav Škarvada 		NULL,			/* m_clear */
136366dfdff0SJaroslav Škarvada 		NULL,			/* m_free */
136466dfdff0SJaroslav Škarvada 	};
136566dfdff0SJaroslav Škarvada 	PyObject *module = PyModule_Create(&moduledef);
136666dfdff0SJaroslav Škarvada #endif
1367877108e4SArnaldo Carvalho de Melo 
1368877108e4SArnaldo Carvalho de Melo 	if (module == NULL ||
1369877108e4SArnaldo Carvalho de Melo 	    pyrf_event__setup_types() < 0 ||
1370877108e4SArnaldo Carvalho de Melo 	    pyrf_evlist__setup_types() < 0 ||
1371877108e4SArnaldo Carvalho de Melo 	    pyrf_evsel__setup_types() < 0 ||
1372877108e4SArnaldo Carvalho de Melo 	    pyrf_thread_map__setup_types() < 0 ||
1373877108e4SArnaldo Carvalho de Melo 	    pyrf_cpu_map__setup_types() < 0)
137466dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
1375877108e4SArnaldo Carvalho de Melo 		return;
137666dfdff0SJaroslav Škarvada #else
137766dfdff0SJaroslav Škarvada 		return module;
137866dfdff0SJaroslav Škarvada #endif
1379877108e4SArnaldo Carvalho de Melo 
1380918512b4SJiri Olsa 	/* The page_size is placed in util object. */
13810da2e9c2SArnaldo Carvalho de Melo 	page_size = sysconf(_SC_PAGE_SIZE);
13820da2e9c2SArnaldo Carvalho de Melo 
1383877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_evlist__type);
1384877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1385877108e4SArnaldo Carvalho de Melo 
1386877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_evsel__type);
1387877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1388877108e4SArnaldo Carvalho de Melo 
138985e37de3SJiri Olsa 	Py_INCREF(&pyrf_mmap_event__type);
139085e37de3SJiri Olsa 	PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
139185e37de3SJiri Olsa 
139285e37de3SJiri Olsa 	Py_INCREF(&pyrf_lost_event__type);
139385e37de3SJiri Olsa 	PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
139485e37de3SJiri Olsa 
139585e37de3SJiri Olsa 	Py_INCREF(&pyrf_comm_event__type);
139685e37de3SJiri Olsa 	PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
139785e37de3SJiri Olsa 
139885e37de3SJiri Olsa 	Py_INCREF(&pyrf_task_event__type);
139985e37de3SJiri Olsa 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
140085e37de3SJiri Olsa 
140185e37de3SJiri Olsa 	Py_INCREF(&pyrf_throttle_event__type);
140285e37de3SJiri Olsa 	PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
140385e37de3SJiri Olsa 
140485e37de3SJiri Olsa 	Py_INCREF(&pyrf_task_event__type);
140585e37de3SJiri Olsa 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
140685e37de3SJiri Olsa 
140785e37de3SJiri Olsa 	Py_INCREF(&pyrf_read_event__type);
140885e37de3SJiri Olsa 	PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
140985e37de3SJiri Olsa 
141085e37de3SJiri Olsa 	Py_INCREF(&pyrf_sample_event__type);
141185e37de3SJiri Olsa 	PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
141285e37de3SJiri Olsa 
141385e37de3SJiri Olsa 	Py_INCREF(&pyrf_context_switch_event__type);
141485e37de3SJiri Olsa 	PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
141585e37de3SJiri Olsa 
1416877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_thread_map__type);
1417877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1418877108e4SArnaldo Carvalho de Melo 
1419877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_cpu_map__type);
1420877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1421877108e4SArnaldo Carvalho de Melo 
1422877108e4SArnaldo Carvalho de Melo 	dict = PyModule_GetDict(module);
1423877108e4SArnaldo Carvalho de Melo 	if (dict == NULL)
1424877108e4SArnaldo Carvalho de Melo 		goto error;
1425877108e4SArnaldo Carvalho de Melo 
1426877108e4SArnaldo Carvalho de Melo 	for (i = 0; perf__constants[i].name != NULL; i++) {
142766dfdff0SJaroslav Škarvada 		obj = _PyLong_FromLong(perf__constants[i].value);
1428877108e4SArnaldo Carvalho de Melo 		if (obj == NULL)
1429877108e4SArnaldo Carvalho de Melo 			goto error;
1430877108e4SArnaldo Carvalho de Melo 		PyDict_SetItemString(dict, perf__constants[i].name, obj);
1431877108e4SArnaldo Carvalho de Melo 		Py_DECREF(obj);
1432877108e4SArnaldo Carvalho de Melo 	}
1433877108e4SArnaldo Carvalho de Melo 
1434877108e4SArnaldo Carvalho de Melo error:
1435877108e4SArnaldo Carvalho de Melo 	if (PyErr_Occurred())
1436877108e4SArnaldo Carvalho de Melo 		PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
143766dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION >= 3
143866dfdff0SJaroslav Škarvada 	return module;
143966dfdff0SJaroslav Škarvada #endif
1440877108e4SArnaldo Carvalho de Melo }
14410c6332e9SArnaldo Carvalho de Melo 
14420c6332e9SArnaldo Carvalho de Melo /*
14430c6332e9SArnaldo Carvalho de Melo  * Dummy, to avoid dragging all the test_attr infrastructure in the python
14440c6332e9SArnaldo Carvalho de Melo  * binding.
14450c6332e9SArnaldo Carvalho de Melo  */
1446*6d18804bSIan Rogers void test_attr__open(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
14470c6332e9SArnaldo Carvalho de Melo                      int fd, int group_fd, unsigned long flags)
14480c6332e9SArnaldo Carvalho de Melo {
14490c6332e9SArnaldo Carvalho de Melo }
1450