xref: /linux/tools/perf/util/python.c (revision 8366f0d268c20929d82d4b1407bd4c6f9232bdec)
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"
189db0e363SArnaldo Carvalho de Melo #include "util/env.h"
1920f2be1dSJiri Olsa #include <internal/lib.h>
20*8366f0d2SJiri Olsa #include "util.h"
21877108e4SArnaldo Carvalho de Melo 
2266dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
2366dfdff0SJaroslav Škarvada #define _PyUnicode_FromString(arg) \
2466dfdff0SJaroslav Škarvada   PyString_FromString(arg)
2566dfdff0SJaroslav Škarvada #define _PyUnicode_AsString(arg) \
2666dfdff0SJaroslav Škarvada   PyString_AsString(arg)
2766dfdff0SJaroslav Škarvada #define _PyUnicode_FromFormat(...) \
2866dfdff0SJaroslav Škarvada   PyString_FromFormat(__VA_ARGS__)
2966dfdff0SJaroslav Škarvada #define _PyLong_FromLong(arg) \
3066dfdff0SJaroslav Škarvada   PyInt_FromLong(arg)
3166dfdff0SJaroslav Škarvada 
3266dfdff0SJaroslav Škarvada #else
3366dfdff0SJaroslav Škarvada 
3466dfdff0SJaroslav Škarvada #define _PyUnicode_FromString(arg) \
3566dfdff0SJaroslav Škarvada   PyUnicode_FromString(arg)
3666dfdff0SJaroslav Škarvada #define _PyUnicode_FromFormat(...) \
3766dfdff0SJaroslav Škarvada   PyUnicode_FromFormat(__VA_ARGS__)
3866dfdff0SJaroslav Škarvada #define _PyLong_FromLong(arg) \
3966dfdff0SJaroslav Škarvada   PyLong_FromLong(arg)
4066dfdff0SJaroslav Škarvada #endif
4166dfdff0SJaroslav Škarvada 
4266dfdff0SJaroslav Škarvada #ifndef Py_TYPE
4366dfdff0SJaroslav Škarvada #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
4466dfdff0SJaroslav Škarvada #endif
4566dfdff0SJaroslav Škarvada 
468afb4c01SAdrian Hunter /*
4756e2e056SArnaldo Carvalho de Melo  * Provide these two so that we don't have to link against callchain.c and
4856e2e056SArnaldo Carvalho de Melo  * start dragging hist.c, etc.
4956e2e056SArnaldo Carvalho de Melo  */
5056e2e056SArnaldo Carvalho de Melo struct callchain_param callchain_param;
5156e2e056SArnaldo Carvalho de Melo 
5256e2e056SArnaldo Carvalho de Melo int parse_callchain_record(const char *arg __maybe_unused,
5356e2e056SArnaldo Carvalho de Melo 			   struct callchain_param *param __maybe_unused)
5456e2e056SArnaldo Carvalho de Melo {
5556e2e056SArnaldo Carvalho de Melo 	return 0;
5656e2e056SArnaldo Carvalho de Melo }
5756e2e056SArnaldo Carvalho de Melo 
5856e2e056SArnaldo Carvalho de Melo /*
599db0e363SArnaldo Carvalho de Melo  * Add this one here not to drag util/env.c
609db0e363SArnaldo Carvalho de Melo  */
619db0e363SArnaldo Carvalho de Melo struct perf_env perf_env;
629db0e363SArnaldo Carvalho de Melo 
639db0e363SArnaldo Carvalho de Melo /*
648afb4c01SAdrian Hunter  * Support debug printing even though util/debug.c is not linked.  That means
658afb4c01SAdrian Hunter  * implementing 'verbose' and 'eprintf'.
668afb4c01SAdrian Hunter  */
678afb4c01SAdrian Hunter int verbose;
68ccd26741SRavi Bangoria int debug_peo_args;
698afb4c01SAdrian Hunter 
705939caccSArnaldo Carvalho de Melo int eprintf(int level, int var, const char *fmt, ...);
715939caccSArnaldo Carvalho de Melo 
72c95688aaSJiri Olsa int eprintf(int level, int var, const char *fmt, ...)
738afb4c01SAdrian Hunter {
748afb4c01SAdrian Hunter 	va_list args;
758afb4c01SAdrian Hunter 	int ret = 0;
768afb4c01SAdrian Hunter 
77c95688aaSJiri Olsa 	if (var >= level) {
788afb4c01SAdrian Hunter 		va_start(args, fmt);
798afb4c01SAdrian Hunter 		ret = vfprintf(stderr, fmt, args);
808afb4c01SAdrian Hunter 		va_end(args);
818afb4c01SAdrian Hunter 	}
828afb4c01SAdrian Hunter 
838afb4c01SAdrian Hunter 	return ret;
848afb4c01SAdrian Hunter }
858afb4c01SAdrian Hunter 
86cfff2d90SFrederic Weisbecker /* Define PyVarObject_HEAD_INIT for python 2.5 */
87cfff2d90SFrederic Weisbecker #ifndef PyVarObject_HEAD_INIT
88cfff2d90SFrederic Weisbecker # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
89cfff2d90SFrederic Weisbecker #endif
90cfff2d90SFrederic Weisbecker 
9166dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
92f6bbc1daSArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void);
9366dfdff0SJaroslav Škarvada #else
9466dfdff0SJaroslav Škarvada PyMODINIT_FUNC PyInit_perf(void);
9566dfdff0SJaroslav Škarvada #endif
96f6bbc1daSArnaldo Carvalho de Melo 
97877108e4SArnaldo Carvalho de Melo #define member_def(type, member, ptype, help) \
98877108e4SArnaldo Carvalho de Melo 	{ #member, ptype, \
99877108e4SArnaldo Carvalho de Melo 	  offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
100877108e4SArnaldo Carvalho de Melo 	  0, help }
101877108e4SArnaldo Carvalho de Melo 
102877108e4SArnaldo Carvalho de Melo #define sample_member_def(name, member, ptype, help) \
103877108e4SArnaldo Carvalho de Melo 	{ #name, ptype, \
104877108e4SArnaldo Carvalho de Melo 	  offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
105877108e4SArnaldo Carvalho de Melo 	  0, help }
106877108e4SArnaldo Carvalho de Melo 
107877108e4SArnaldo Carvalho de Melo struct pyrf_event {
108877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
10932dcd021SJiri Olsa 	struct evsel *evsel;
110877108e4SArnaldo Carvalho de Melo 	struct perf_sample sample;
111877108e4SArnaldo Carvalho de Melo 	union perf_event   event;
112877108e4SArnaldo Carvalho de Melo };
113877108e4SArnaldo Carvalho de Melo 
114877108e4SArnaldo Carvalho de Melo #define sample_members \
115f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"),			 \
116877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_pid, pid, T_INT, "event pid"),			 \
117877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_tid, tid, T_INT, "event tid"),			 \
118f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"),		 \
119f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"),		 \
120f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_id, id, T_ULONGLONG, "event id"),			 \
121f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
122f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_period, period, T_ULONGLONG, "event period"),		 \
123877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
124877108e4SArnaldo Carvalho de Melo 
125877108e4SArnaldo Carvalho de Melo static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
126877108e4SArnaldo Carvalho de Melo 
127877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_mmap_event__members[] = {
128877108e4SArnaldo Carvalho de Melo 	sample_members
129877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
130ae938802SArnaldo Carvalho de Melo 	member_def(perf_event_header, misc, T_UINT, "event misc"),
13169d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, pid, T_UINT, "event pid"),
13269d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, tid, T_UINT, "event tid"),
13369d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
13469d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
13569d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
13669d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
137f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
138877108e4SArnaldo Carvalho de Melo };
139877108e4SArnaldo Carvalho de Melo 
140877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
141877108e4SArnaldo Carvalho de Melo {
142877108e4SArnaldo Carvalho de Melo 	PyObject *ret;
143877108e4SArnaldo Carvalho de Melo 	char *s;
144877108e4SArnaldo Carvalho de Melo 
1451345e2eeSJiri Olsa 	if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
1461345e2eeSJiri Olsa 			 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
147877108e4SArnaldo Carvalho de Melo 			 "filename: %s }",
148877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.pid, pevent->event.mmap.tid,
149877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.start, pevent->event.mmap.len,
150877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
151877108e4SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
152877108e4SArnaldo Carvalho de Melo 	} else {
15366dfdff0SJaroslav Škarvada 		ret = _PyUnicode_FromString(s);
154877108e4SArnaldo Carvalho de Melo 		free(s);
155877108e4SArnaldo Carvalho de Melo 	}
156877108e4SArnaldo Carvalho de Melo 	return ret;
157877108e4SArnaldo Carvalho de Melo }
158877108e4SArnaldo Carvalho de Melo 
159877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_mmap_event__type = {
160877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
161877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.mmap_event",
162877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
163877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
164877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_mmap_event__doc,
165877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_mmap_event__members,
166877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_mmap_event__repr,
167877108e4SArnaldo Carvalho de Melo };
168877108e4SArnaldo Carvalho de Melo 
169877108e4SArnaldo Carvalho de Melo static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
170877108e4SArnaldo Carvalho de Melo 
171877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_task_event__members[] = {
172877108e4SArnaldo Carvalho de Melo 	sample_members
173877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
17469d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, pid, T_UINT, "event pid"),
17569d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
17669d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, tid, T_UINT, "event tid"),
17769d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
17869d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
179f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
180877108e4SArnaldo Carvalho de Melo };
181877108e4SArnaldo Carvalho de Melo 
182877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
183877108e4SArnaldo Carvalho de Melo {
18466dfdff0SJaroslav Škarvada 	return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
185bceb59b1SJiri Olsa 				   "ptid: %u, time: %" PRI_lu64 "}",
186877108e4SArnaldo Carvalho de Melo 				   pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
187877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.pid,
188877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.ppid,
189877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.tid,
190877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.ptid,
191877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.time);
192877108e4SArnaldo Carvalho de Melo }
193877108e4SArnaldo Carvalho de Melo 
194877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_task_event__type = {
195877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
196877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.task_event",
197877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
198877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
199877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_task_event__doc,
200877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_task_event__members,
201877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_task_event__repr,
202877108e4SArnaldo Carvalho de Melo };
203877108e4SArnaldo Carvalho de Melo 
204877108e4SArnaldo Carvalho de Melo static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
205877108e4SArnaldo Carvalho de Melo 
206877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_comm_event__members[] = {
207877108e4SArnaldo Carvalho de Melo 	sample_members
208877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
20969d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_comm, pid, T_UINT, "event pid"),
21069d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_comm, tid, T_UINT, "event tid"),
21169d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
212f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
213877108e4SArnaldo Carvalho de Melo };
214877108e4SArnaldo Carvalho de Melo 
215877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
216877108e4SArnaldo Carvalho de Melo {
21766dfdff0SJaroslav Škarvada 	return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
218877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.pid,
219877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.tid,
220877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.comm);
221877108e4SArnaldo Carvalho de Melo }
222877108e4SArnaldo Carvalho de Melo 
223877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_comm_event__type = {
224877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
225877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.comm_event",
226877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
227877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
228877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_comm_event__doc,
229877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_comm_event__members,
230877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_comm_event__repr,
231877108e4SArnaldo Carvalho de Melo };
232877108e4SArnaldo Carvalho de Melo 
233877108e4SArnaldo Carvalho de Melo static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
234877108e4SArnaldo Carvalho de Melo 
235877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_throttle_event__members[] = {
236877108e4SArnaldo Carvalho de Melo 	sample_members
237877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
23869d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
23969d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
24069d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
241f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
242877108e4SArnaldo Carvalho de Melo };
243877108e4SArnaldo Carvalho de Melo 
244877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
245877108e4SArnaldo Carvalho de Melo {
24669d81f09SArnaldo Carvalho de Melo 	struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1);
247877108e4SArnaldo Carvalho de Melo 
248003c66feSJiri Olsa 	return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
249003c66feSJiri Olsa 				   ", stream_id: %" PRI_lu64 " }",
250877108e4SArnaldo Carvalho de Melo 				   pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
251877108e4SArnaldo Carvalho de Melo 				   te->time, te->id, te->stream_id);
252877108e4SArnaldo Carvalho de Melo }
253877108e4SArnaldo Carvalho de Melo 
254877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_throttle_event__type = {
255877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
256877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.throttle_event",
257877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
258877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
259877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_throttle_event__doc,
260877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_throttle_event__members,
261877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_throttle_event__repr,
262877108e4SArnaldo Carvalho de Melo };
263877108e4SArnaldo Carvalho de Melo 
2643e9f45a7SArnaldo Carvalho de Melo static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
2653e9f45a7SArnaldo Carvalho de Melo 
2663e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_lost_event__members[] = {
2673e9f45a7SArnaldo Carvalho de Melo 	sample_members
26869d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
26969d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
2703e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
2713e9f45a7SArnaldo Carvalho de Melo };
2723e9f45a7SArnaldo Carvalho de Melo 
2733e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
2743e9f45a7SArnaldo Carvalho de Melo {
2753e9f45a7SArnaldo Carvalho de Melo 	PyObject *ret;
2763e9f45a7SArnaldo Carvalho de Melo 	char *s;
2773e9f45a7SArnaldo Carvalho de Melo 
2785290ed69SJiri Olsa 	if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
2795290ed69SJiri Olsa 			 "lost: %#" PRI_lx64 " }",
2803e9f45a7SArnaldo Carvalho de Melo 		     pevent->event.lost.id, pevent->event.lost.lost) < 0) {
2813e9f45a7SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
2823e9f45a7SArnaldo Carvalho de Melo 	} else {
28366dfdff0SJaroslav Škarvada 		ret = _PyUnicode_FromString(s);
2843e9f45a7SArnaldo Carvalho de Melo 		free(s);
2853e9f45a7SArnaldo Carvalho de Melo 	}
2863e9f45a7SArnaldo Carvalho de Melo 	return ret;
2873e9f45a7SArnaldo Carvalho de Melo }
2883e9f45a7SArnaldo Carvalho de Melo 
2893e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_lost_event__type = {
2903e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
2913e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.lost_event",
2923e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
2933e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
2943e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_lost_event__doc,
2953e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_lost_event__members,
2963e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_lost_event__repr,
2973e9f45a7SArnaldo Carvalho de Melo };
2983e9f45a7SArnaldo Carvalho de Melo 
2993e9f45a7SArnaldo Carvalho de Melo static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
3003e9f45a7SArnaldo Carvalho de Melo 
3013e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_read_event__members[] = {
3023e9f45a7SArnaldo Carvalho de Melo 	sample_members
30369d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_read, pid, T_UINT, "event pid"),
30469d81f09SArnaldo Carvalho de Melo 	member_def(perf_record_read, tid, T_UINT, "event tid"),
3053e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
3063e9f45a7SArnaldo Carvalho de Melo };
3073e9f45a7SArnaldo Carvalho de Melo 
3083e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
3093e9f45a7SArnaldo Carvalho de Melo {
31066dfdff0SJaroslav Škarvada 	return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
3113e9f45a7SArnaldo Carvalho de Melo 				   pevent->event.read.pid,
3123e9f45a7SArnaldo Carvalho de Melo 				   pevent->event.read.tid);
3133e9f45a7SArnaldo Carvalho de Melo 	/*
3143e9f45a7SArnaldo Carvalho de Melo  	 * FIXME: return the array of read values,
3153e9f45a7SArnaldo Carvalho de Melo  	 * making this method useful ;-)
3163e9f45a7SArnaldo Carvalho de Melo  	 */
3173e9f45a7SArnaldo Carvalho de Melo }
3183e9f45a7SArnaldo Carvalho de Melo 
3193e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_read_event__type = {
3203e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
3213e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.read_event",
3223e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
3233e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
3243e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_read_event__doc,
3253e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_read_event__members,
3263e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_read_event__repr,
3273e9f45a7SArnaldo Carvalho de Melo };
3283e9f45a7SArnaldo Carvalho de Melo 
3293e9f45a7SArnaldo Carvalho de Melo static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
3303e9f45a7SArnaldo Carvalho de Melo 
3313e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_sample_event__members[] = {
3323e9f45a7SArnaldo Carvalho de Melo 	sample_members
3333e9f45a7SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
3343e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
3353e9f45a7SArnaldo Carvalho de Melo };
3363e9f45a7SArnaldo Carvalho de Melo 
3373e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
3383e9f45a7SArnaldo Carvalho de Melo {
3393e9f45a7SArnaldo Carvalho de Melo 	PyObject *ret;
3403e9f45a7SArnaldo Carvalho de Melo 	char *s;
3413e9f45a7SArnaldo Carvalho de Melo 
3423e9f45a7SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: sample }") < 0) {
3433e9f45a7SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
3443e9f45a7SArnaldo Carvalho de Melo 	} else {
34566dfdff0SJaroslav Škarvada 		ret = _PyUnicode_FromString(s);
3463e9f45a7SArnaldo Carvalho de Melo 		free(s);
3473e9f45a7SArnaldo Carvalho de Melo 	}
3483e9f45a7SArnaldo Carvalho de Melo 	return ret;
3493e9f45a7SArnaldo Carvalho de Melo }
3503e9f45a7SArnaldo Carvalho de Melo 
351bae57e38SJiri Olsa static bool is_tracepoint(struct pyrf_event *pevent)
352bae57e38SJiri Olsa {
3531fc632ceSJiri Olsa 	return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
354bae57e38SJiri Olsa }
355bae57e38SJiri Olsa 
356bae57e38SJiri Olsa static PyObject*
3572c92f982STzvetomir Stoyanov (VMware) tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
358bae57e38SJiri Olsa {
35969769ce1STzvetomir Stoyanov 	struct tep_handle *pevent = field->event->tep;
360bae57e38SJiri Olsa 	void *data = pe->sample.raw_data;
361bae57e38SJiri Olsa 	PyObject *ret = NULL;
362bae57e38SJiri Olsa 	unsigned long long val;
363bae57e38SJiri Olsa 	unsigned int offset, len;
364bae57e38SJiri Olsa 
365bb39ccb2STzvetomir Stoyanov (VMware) 	if (field->flags & TEP_FIELD_IS_ARRAY) {
366bae57e38SJiri Olsa 		offset = field->offset;
367bae57e38SJiri Olsa 		len    = field->size;
368bb39ccb2STzvetomir Stoyanov (VMware) 		if (field->flags & TEP_FIELD_IS_DYNAMIC) {
36959c1baeeSTzvetomir Stoyanov (VMware) 			val     = tep_read_number(pevent, data + offset, len);
370bae57e38SJiri Olsa 			offset  = val;
371bae57e38SJiri Olsa 			len     = offset >> 16;
372bae57e38SJiri Olsa 			offset &= 0xffff;
373bae57e38SJiri Olsa 		}
374bb39ccb2STzvetomir Stoyanov (VMware) 		if (field->flags & TEP_FIELD_IS_STRING &&
375bae57e38SJiri Olsa 		    is_printable_array(data + offset, len)) {
37666dfdff0SJaroslav Škarvada 			ret = _PyUnicode_FromString((char *)data + offset);
377bae57e38SJiri Olsa 		} else {
378bae57e38SJiri Olsa 			ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
379bb39ccb2STzvetomir Stoyanov (VMware) 			field->flags &= ~TEP_FIELD_IS_STRING;
380bae57e38SJiri Olsa 		}
381bae57e38SJiri Olsa 	} else {
38259c1baeeSTzvetomir Stoyanov (VMware) 		val = tep_read_number(pevent, data + field->offset,
383bae57e38SJiri Olsa 				      field->size);
384bb39ccb2STzvetomir Stoyanov (VMware) 		if (field->flags & TEP_FIELD_IS_POINTER)
385bae57e38SJiri Olsa 			ret = PyLong_FromUnsignedLong((unsigned long) val);
386bb39ccb2STzvetomir Stoyanov (VMware) 		else if (field->flags & TEP_FIELD_IS_SIGNED)
387bae57e38SJiri Olsa 			ret = PyLong_FromLong((long) val);
388bae57e38SJiri Olsa 		else
389bae57e38SJiri Olsa 			ret = PyLong_FromUnsignedLong((unsigned long) val);
390bae57e38SJiri Olsa 	}
391bae57e38SJiri Olsa 
392bae57e38SJiri Olsa 	return ret;
393bae57e38SJiri Olsa }
394bae57e38SJiri Olsa 
395bae57e38SJiri Olsa static PyObject*
396bae57e38SJiri Olsa get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
397bae57e38SJiri Olsa {
39866dfdff0SJaroslav Škarvada 	const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
39932dcd021SJiri Olsa 	struct evsel *evsel = pevent->evsel;
4002c92f982STzvetomir Stoyanov (VMware) 	struct tep_format_field *field;
401bae57e38SJiri Olsa 
402bae57e38SJiri Olsa 	if (!evsel->tp_format) {
40397fbf3f0STzvetomir Stoyanov 		struct tep_event *tp_format;
404bae57e38SJiri Olsa 
4051fc632ceSJiri Olsa 		tp_format = trace_event__tp_format_id(evsel->core.attr.config);
406bae57e38SJiri Olsa 		if (!tp_format)
407bae57e38SJiri Olsa 			return NULL;
408bae57e38SJiri Olsa 
409bae57e38SJiri Olsa 		evsel->tp_format = tp_format;
410bae57e38SJiri Olsa 	}
411bae57e38SJiri Olsa 
412af85cd19STzvetomir Stoyanov (VMware) 	field = tep_find_any_field(evsel->tp_format, str);
413bae57e38SJiri Olsa 	if (!field)
414bae57e38SJiri Olsa 		return NULL;
415bae57e38SJiri Olsa 
416bae57e38SJiri Olsa 	return tracepoint_field(pevent, field);
417bae57e38SJiri Olsa }
418bae57e38SJiri Olsa 
419bae57e38SJiri Olsa static PyObject*
420bae57e38SJiri Olsa pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
421bae57e38SJiri Olsa {
422bae57e38SJiri Olsa 	PyObject *obj = NULL;
423bae57e38SJiri Olsa 
424bae57e38SJiri Olsa 	if (is_tracepoint(pevent))
425bae57e38SJiri Olsa 		obj = get_tracepoint_field(pevent, attr_name);
426bae57e38SJiri Olsa 
427bae57e38SJiri Olsa 	return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
428bae57e38SJiri Olsa }
429bae57e38SJiri Olsa 
4303e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_sample_event__type = {
4313e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
4323e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.sample_event",
4333e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
4343e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
4353e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_sample_event__doc,
4363e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_sample_event__members,
4373e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_sample_event__repr,
438bae57e38SJiri Olsa 	.tp_getattro	= (getattrofunc) pyrf_sample_event__getattro,
4393e9f45a7SArnaldo Carvalho de Melo };
4403e9f45a7SArnaldo Carvalho de Melo 
441ae938802SArnaldo Carvalho de Melo static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
442ae938802SArnaldo Carvalho de Melo 
443ae938802SArnaldo Carvalho de Melo static PyMemberDef pyrf_context_switch_event__members[] = {
444ae938802SArnaldo Carvalho de Melo 	sample_members
445ae938802SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
44672932371SJiri Olsa 	member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
44772932371SJiri Olsa 	member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
448ae938802SArnaldo Carvalho de Melo 	{ .name = NULL, },
449ae938802SArnaldo Carvalho de Melo };
450ae938802SArnaldo Carvalho de Melo 
451ae938802SArnaldo Carvalho de Melo static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
452ae938802SArnaldo Carvalho de Melo {
453ae938802SArnaldo Carvalho de Melo 	PyObject *ret;
454ae938802SArnaldo Carvalho de Melo 	char *s;
455ae938802SArnaldo Carvalho de Melo 
456ae938802SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
457ae938802SArnaldo Carvalho de Melo 		     pevent->event.context_switch.next_prev_pid,
458ae938802SArnaldo Carvalho de Melo 		     pevent->event.context_switch.next_prev_tid,
459ae938802SArnaldo Carvalho de Melo 		     !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
460ae938802SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
461ae938802SArnaldo Carvalho de Melo 	} else {
46266dfdff0SJaroslav Škarvada 		ret = _PyUnicode_FromString(s);
463ae938802SArnaldo Carvalho de Melo 		free(s);
464ae938802SArnaldo Carvalho de Melo 	}
465ae938802SArnaldo Carvalho de Melo 	return ret;
466ae938802SArnaldo Carvalho de Melo }
467ae938802SArnaldo Carvalho de Melo 
468ae938802SArnaldo Carvalho de Melo static PyTypeObject pyrf_context_switch_event__type = {
469ae938802SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
470ae938802SArnaldo Carvalho de Melo 	.tp_name	= "perf.context_switch_event",
471ae938802SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
472ae938802SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
473ae938802SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_context_switch_event__doc,
474ae938802SArnaldo Carvalho de Melo 	.tp_members	= pyrf_context_switch_event__members,
475ae938802SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_context_switch_event__repr,
476ae938802SArnaldo Carvalho de Melo };
477ae938802SArnaldo Carvalho de Melo 
478877108e4SArnaldo Carvalho de Melo static int pyrf_event__setup_types(void)
479877108e4SArnaldo Carvalho de Melo {
480877108e4SArnaldo Carvalho de Melo 	int err;
481877108e4SArnaldo Carvalho de Melo 	pyrf_mmap_event__type.tp_new =
482877108e4SArnaldo Carvalho de Melo 	pyrf_task_event__type.tp_new =
483877108e4SArnaldo Carvalho de Melo 	pyrf_comm_event__type.tp_new =
4843e9f45a7SArnaldo Carvalho de Melo 	pyrf_lost_event__type.tp_new =
4853e9f45a7SArnaldo Carvalho de Melo 	pyrf_read_event__type.tp_new =
4863e9f45a7SArnaldo Carvalho de Melo 	pyrf_sample_event__type.tp_new =
487ae938802SArnaldo Carvalho de Melo 	pyrf_context_switch_event__type.tp_new =
488877108e4SArnaldo Carvalho de Melo 	pyrf_throttle_event__type.tp_new = PyType_GenericNew;
489877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_mmap_event__type);
490877108e4SArnaldo Carvalho de Melo 	if (err < 0)
491877108e4SArnaldo Carvalho de Melo 		goto out;
4923e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_lost_event__type);
4933e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
4943e9f45a7SArnaldo Carvalho de Melo 		goto out;
495877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_task_event__type);
496877108e4SArnaldo Carvalho de Melo 	if (err < 0)
497877108e4SArnaldo Carvalho de Melo 		goto out;
498877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_comm_event__type);
499877108e4SArnaldo Carvalho de Melo 	if (err < 0)
500877108e4SArnaldo Carvalho de Melo 		goto out;
501877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_throttle_event__type);
502877108e4SArnaldo Carvalho de Melo 	if (err < 0)
503877108e4SArnaldo Carvalho de Melo 		goto out;
5043e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_read_event__type);
5053e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
5063e9f45a7SArnaldo Carvalho de Melo 		goto out;
5073e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_sample_event__type);
5083e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
5093e9f45a7SArnaldo Carvalho de Melo 		goto out;
510ae938802SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_context_switch_event__type);
511ae938802SArnaldo Carvalho de Melo 	if (err < 0)
512ae938802SArnaldo Carvalho de Melo 		goto out;
513877108e4SArnaldo Carvalho de Melo out:
514877108e4SArnaldo Carvalho de Melo 	return err;
515877108e4SArnaldo Carvalho de Melo }
516877108e4SArnaldo Carvalho de Melo 
517877108e4SArnaldo Carvalho de Melo static PyTypeObject *pyrf_event__type[] = {
518877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_MMAP]	 = &pyrf_mmap_event__type,
5193e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_LOST]	 = &pyrf_lost_event__type,
520877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_COMM]	 = &pyrf_comm_event__type,
521877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_EXIT]	 = &pyrf_task_event__type,
522877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_THROTTLE]	 = &pyrf_throttle_event__type,
523877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
524877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_FORK]	 = &pyrf_task_event__type,
5253e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_READ]	 = &pyrf_read_event__type,
5263e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_SAMPLE]	 = &pyrf_sample_event__type,
527ae938802SArnaldo Carvalho de Melo 	[PERF_RECORD_SWITCH]	 = &pyrf_context_switch_event__type,
528ae938802SArnaldo Carvalho de Melo 	[PERF_RECORD_SWITCH_CPU_WIDE]  = &pyrf_context_switch_event__type,
529877108e4SArnaldo Carvalho de Melo };
530877108e4SArnaldo Carvalho de Melo 
531877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_event__new(union perf_event *event)
532877108e4SArnaldo Carvalho de Melo {
533877108e4SArnaldo Carvalho de Melo 	struct pyrf_event *pevent;
534877108e4SArnaldo Carvalho de Melo 	PyTypeObject *ptype;
535877108e4SArnaldo Carvalho de Melo 
536ae938802SArnaldo Carvalho de Melo 	if ((event->header.type < PERF_RECORD_MMAP ||
537ae938802SArnaldo Carvalho de Melo 	     event->header.type > PERF_RECORD_SAMPLE) &&
538ae938802SArnaldo Carvalho de Melo 	    !(event->header.type == PERF_RECORD_SWITCH ||
539ae938802SArnaldo Carvalho de Melo 	      event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
540877108e4SArnaldo Carvalho de Melo 		return NULL;
541877108e4SArnaldo Carvalho de Melo 
542877108e4SArnaldo Carvalho de Melo 	ptype = pyrf_event__type[event->header.type];
543877108e4SArnaldo Carvalho de Melo 	pevent = PyObject_New(struct pyrf_event, ptype);
544877108e4SArnaldo Carvalho de Melo 	if (pevent != NULL)
545877108e4SArnaldo Carvalho de Melo 		memcpy(&pevent->event, event, event->header.size);
546877108e4SArnaldo Carvalho de Melo 	return (PyObject *)pevent;
547877108e4SArnaldo Carvalho de Melo }
548877108e4SArnaldo Carvalho de Melo 
549877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map {
550877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
551877108e4SArnaldo Carvalho de Melo 
552f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
553877108e4SArnaldo Carvalho de Melo };
554877108e4SArnaldo Carvalho de Melo 
555877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
556877108e4SArnaldo Carvalho de Melo 			      PyObject *args, PyObject *kwargs)
557877108e4SArnaldo Carvalho de Melo {
55864348153SFrederic Weisbecker 	static char *kwlist[] = { "cpustr", NULL };
559877108e4SArnaldo Carvalho de Melo 	char *cpustr = NULL;
560877108e4SArnaldo Carvalho de Melo 
561877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
562877108e4SArnaldo Carvalho de Melo 					 kwlist, &cpustr))
563877108e4SArnaldo Carvalho de Melo 		return -1;
564877108e4SArnaldo Carvalho de Melo 
5659c3516d1SJiri Olsa 	pcpus->cpus = perf_cpu_map__new(cpustr);
566877108e4SArnaldo Carvalho de Melo 	if (pcpus->cpus == NULL)
567877108e4SArnaldo Carvalho de Melo 		return -1;
568877108e4SArnaldo Carvalho de Melo 	return 0;
569877108e4SArnaldo Carvalho de Melo }
570877108e4SArnaldo Carvalho de Melo 
571877108e4SArnaldo Carvalho de Melo static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
572877108e4SArnaldo Carvalho de Melo {
57338f01d8dSJiri Olsa 	perf_cpu_map__put(pcpus->cpus);
57466dfdff0SJaroslav Škarvada 	Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
575877108e4SArnaldo Carvalho de Melo }
576877108e4SArnaldo Carvalho de Melo 
577877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
578877108e4SArnaldo Carvalho de Melo {
579877108e4SArnaldo Carvalho de Melo 	struct pyrf_cpu_map *pcpus = (void *)obj;
580877108e4SArnaldo Carvalho de Melo 
581877108e4SArnaldo Carvalho de Melo 	return pcpus->cpus->nr;
582877108e4SArnaldo Carvalho de Melo }
583877108e4SArnaldo Carvalho de Melo 
584877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
585877108e4SArnaldo Carvalho de Melo {
586877108e4SArnaldo Carvalho de Melo 	struct pyrf_cpu_map *pcpus = (void *)obj;
587877108e4SArnaldo Carvalho de Melo 
588877108e4SArnaldo Carvalho de Melo 	if (i >= pcpus->cpus->nr)
589877108e4SArnaldo Carvalho de Melo 		return NULL;
590877108e4SArnaldo Carvalho de Melo 
591877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", pcpus->cpus->map[i]);
592877108e4SArnaldo Carvalho de Melo }
593877108e4SArnaldo Carvalho de Melo 
594877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_cpu_map__sequence_methods = {
595877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_cpu_map__length,
596877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_cpu_map__item,
597877108e4SArnaldo Carvalho de Melo };
598877108e4SArnaldo Carvalho de Melo 
599877108e4SArnaldo Carvalho de Melo static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
600877108e4SArnaldo Carvalho de Melo 
601877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_cpu_map__type = {
602877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
603877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.cpu_map",
604877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_cpu_map),
605877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_cpu_map__delete,
606877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
607877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_cpu_map__doc,
608877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_cpu_map__sequence_methods,
609877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_cpu_map__init,
610877108e4SArnaldo Carvalho de Melo };
611877108e4SArnaldo Carvalho de Melo 
612877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__setup_types(void)
613877108e4SArnaldo Carvalho de Melo {
614877108e4SArnaldo Carvalho de Melo 	pyrf_cpu_map__type.tp_new = PyType_GenericNew;
615877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_cpu_map__type);
616877108e4SArnaldo Carvalho de Melo }
617877108e4SArnaldo Carvalho de Melo 
618877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map {
619877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
620877108e4SArnaldo Carvalho de Melo 
6219749b90eSJiri Olsa 	struct perf_thread_map *threads;
622877108e4SArnaldo Carvalho de Melo };
623877108e4SArnaldo Carvalho de Melo 
624877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
625877108e4SArnaldo Carvalho de Melo 				 PyObject *args, PyObject *kwargs)
626877108e4SArnaldo Carvalho de Melo {
6270d37aa34SArnaldo Carvalho de Melo 	static char *kwlist[] = { "pid", "tid", "uid", NULL };
6280d37aa34SArnaldo Carvalho de Melo 	int pid = -1, tid = -1, uid = UINT_MAX;
629877108e4SArnaldo Carvalho de Melo 
6300d37aa34SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
6310d37aa34SArnaldo Carvalho de Melo 					 kwlist, &pid, &tid, &uid))
632877108e4SArnaldo Carvalho de Melo 		return -1;
633877108e4SArnaldo Carvalho de Melo 
6340d37aa34SArnaldo Carvalho de Melo 	pthreads->threads = thread_map__new(pid, tid, uid);
635877108e4SArnaldo Carvalho de Melo 	if (pthreads->threads == NULL)
636877108e4SArnaldo Carvalho de Melo 		return -1;
637877108e4SArnaldo Carvalho de Melo 	return 0;
638877108e4SArnaldo Carvalho de Melo }
639877108e4SArnaldo Carvalho de Melo 
640877108e4SArnaldo Carvalho de Melo static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
641877108e4SArnaldo Carvalho de Melo {
6427836e52eSJiri Olsa 	perf_thread_map__put(pthreads->threads);
64366dfdff0SJaroslav Škarvada 	Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
644877108e4SArnaldo Carvalho de Melo }
645877108e4SArnaldo Carvalho de Melo 
646877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
647877108e4SArnaldo Carvalho de Melo {
648877108e4SArnaldo Carvalho de Melo 	struct pyrf_thread_map *pthreads = (void *)obj;
649877108e4SArnaldo Carvalho de Melo 
650877108e4SArnaldo Carvalho de Melo 	return pthreads->threads->nr;
651877108e4SArnaldo Carvalho de Melo }
652877108e4SArnaldo Carvalho de Melo 
653877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
654877108e4SArnaldo Carvalho de Melo {
655877108e4SArnaldo Carvalho de Melo 	struct pyrf_thread_map *pthreads = (void *)obj;
656877108e4SArnaldo Carvalho de Melo 
657877108e4SArnaldo Carvalho de Melo 	if (i >= pthreads->threads->nr)
658877108e4SArnaldo Carvalho de Melo 		return NULL;
659877108e4SArnaldo Carvalho de Melo 
660877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", pthreads->threads->map[i]);
661877108e4SArnaldo Carvalho de Melo }
662877108e4SArnaldo Carvalho de Melo 
663877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_thread_map__sequence_methods = {
664877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_thread_map__length,
665877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_thread_map__item,
666877108e4SArnaldo Carvalho de Melo };
667877108e4SArnaldo Carvalho de Melo 
668877108e4SArnaldo Carvalho de Melo static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
669877108e4SArnaldo Carvalho de Melo 
670877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_thread_map__type = {
671877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
672877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.thread_map",
673877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_thread_map),
674877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_thread_map__delete,
675877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
676877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_thread_map__doc,
677877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_thread_map__sequence_methods,
678877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_thread_map__init,
679877108e4SArnaldo Carvalho de Melo };
680877108e4SArnaldo Carvalho de Melo 
681877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__setup_types(void)
682877108e4SArnaldo Carvalho de Melo {
683877108e4SArnaldo Carvalho de Melo 	pyrf_thread_map__type.tp_new = PyType_GenericNew;
684877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_thread_map__type);
685877108e4SArnaldo Carvalho de Melo }
686877108e4SArnaldo Carvalho de Melo 
687877108e4SArnaldo Carvalho de Melo struct pyrf_evsel {
688877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
689877108e4SArnaldo Carvalho de Melo 
69032dcd021SJiri Olsa 	struct evsel evsel;
691877108e4SArnaldo Carvalho de Melo };
692877108e4SArnaldo Carvalho de Melo 
693877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
694877108e4SArnaldo Carvalho de Melo 			    PyObject *args, PyObject *kwargs)
695877108e4SArnaldo Carvalho de Melo {
696877108e4SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
697877108e4SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
698877108e4SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
699877108e4SArnaldo Carvalho de Melo 		.sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
700877108e4SArnaldo Carvalho de Melo 	};
701877108e4SArnaldo Carvalho de Melo 	static char *kwlist[] = {
702877108e4SArnaldo Carvalho de Melo 		"type",
703877108e4SArnaldo Carvalho de Melo 		"config",
704877108e4SArnaldo Carvalho de Melo 		"sample_freq",
705877108e4SArnaldo Carvalho de Melo 		"sample_period",
706877108e4SArnaldo Carvalho de Melo 		"sample_type",
707877108e4SArnaldo Carvalho de Melo 		"read_format",
708877108e4SArnaldo Carvalho de Melo 		"disabled",
709877108e4SArnaldo Carvalho de Melo 		"inherit",
710877108e4SArnaldo Carvalho de Melo 		"pinned",
711877108e4SArnaldo Carvalho de Melo 		"exclusive",
712877108e4SArnaldo Carvalho de Melo 		"exclude_user",
713877108e4SArnaldo Carvalho de Melo 		"exclude_kernel",
714877108e4SArnaldo Carvalho de Melo 		"exclude_hv",
715877108e4SArnaldo Carvalho de Melo 		"exclude_idle",
716877108e4SArnaldo Carvalho de Melo 		"mmap",
717ae938802SArnaldo Carvalho de Melo 		"context_switch",
718877108e4SArnaldo Carvalho de Melo 		"comm",
719877108e4SArnaldo Carvalho de Melo 		"freq",
720877108e4SArnaldo Carvalho de Melo 		"inherit_stat",
721877108e4SArnaldo Carvalho de Melo 		"enable_on_exec",
722877108e4SArnaldo Carvalho de Melo 		"task",
723877108e4SArnaldo Carvalho de Melo 		"watermark",
724877108e4SArnaldo Carvalho de Melo 		"precise_ip",
725877108e4SArnaldo Carvalho de Melo 		"mmap_data",
726877108e4SArnaldo Carvalho de Melo 		"sample_id_all",
727877108e4SArnaldo Carvalho de Melo 		"wakeup_events",
728877108e4SArnaldo Carvalho de Melo 		"bp_type",
729877108e4SArnaldo Carvalho de Melo 		"bp_addr",
73064348153SFrederic Weisbecker 		"bp_len",
73164348153SFrederic Weisbecker 		 NULL
73264348153SFrederic Weisbecker 	};
733877108e4SArnaldo Carvalho de Melo 	u64 sample_period = 0;
734877108e4SArnaldo Carvalho de Melo 	u32 disabled = 0,
735877108e4SArnaldo Carvalho de Melo 	    inherit = 0,
736877108e4SArnaldo Carvalho de Melo 	    pinned = 0,
737877108e4SArnaldo Carvalho de Melo 	    exclusive = 0,
738877108e4SArnaldo Carvalho de Melo 	    exclude_user = 0,
739877108e4SArnaldo Carvalho de Melo 	    exclude_kernel = 0,
740877108e4SArnaldo Carvalho de Melo 	    exclude_hv = 0,
741877108e4SArnaldo Carvalho de Melo 	    exclude_idle = 0,
742877108e4SArnaldo Carvalho de Melo 	    mmap = 0,
743ae938802SArnaldo Carvalho de Melo 	    context_switch = 0,
744877108e4SArnaldo Carvalho de Melo 	    comm = 0,
745877108e4SArnaldo Carvalho de Melo 	    freq = 1,
746877108e4SArnaldo Carvalho de Melo 	    inherit_stat = 0,
747877108e4SArnaldo Carvalho de Melo 	    enable_on_exec = 0,
748877108e4SArnaldo Carvalho de Melo 	    task = 0,
749877108e4SArnaldo Carvalho de Melo 	    watermark = 0,
750877108e4SArnaldo Carvalho de Melo 	    precise_ip = 0,
751877108e4SArnaldo Carvalho de Melo 	    mmap_data = 0,
752877108e4SArnaldo Carvalho de Melo 	    sample_id_all = 1;
753877108e4SArnaldo Carvalho de Melo 	int idx = 0;
754877108e4SArnaldo Carvalho de Melo 
755877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
756ae938802SArnaldo Carvalho de Melo 					 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
757877108e4SArnaldo Carvalho de Melo 					 &attr.type, &attr.config, &attr.sample_freq,
758877108e4SArnaldo Carvalho de Melo 					 &sample_period, &attr.sample_type,
759877108e4SArnaldo Carvalho de Melo 					 &attr.read_format, &disabled, &inherit,
760877108e4SArnaldo Carvalho de Melo 					 &pinned, &exclusive, &exclude_user,
761877108e4SArnaldo Carvalho de Melo 					 &exclude_kernel, &exclude_hv, &exclude_idle,
762ae938802SArnaldo Carvalho de Melo 					 &mmap, &context_switch, &comm, &freq, &inherit_stat,
763877108e4SArnaldo Carvalho de Melo 					 &enable_on_exec, &task, &watermark,
764877108e4SArnaldo Carvalho de Melo 					 &precise_ip, &mmap_data, &sample_id_all,
765877108e4SArnaldo Carvalho de Melo 					 &attr.wakeup_events, &attr.bp_type,
766877108e4SArnaldo Carvalho de Melo 					 &attr.bp_addr, &attr.bp_len, &idx))
767877108e4SArnaldo Carvalho de Melo 		return -1;
768877108e4SArnaldo Carvalho de Melo 
769877108e4SArnaldo Carvalho de Melo 	/* union... */
770877108e4SArnaldo Carvalho de Melo 	if (sample_period != 0) {
771877108e4SArnaldo Carvalho de Melo 		if (attr.sample_freq != 0)
772877108e4SArnaldo Carvalho de Melo 			return -1; /* FIXME: throw right exception */
773877108e4SArnaldo Carvalho de Melo 		attr.sample_period = sample_period;
774877108e4SArnaldo Carvalho de Melo 	}
775877108e4SArnaldo Carvalho de Melo 
776877108e4SArnaldo Carvalho de Melo 	/* Bitfields */
777877108e4SArnaldo Carvalho de Melo 	attr.disabled	    = disabled;
778877108e4SArnaldo Carvalho de Melo 	attr.inherit	    = inherit;
779877108e4SArnaldo Carvalho de Melo 	attr.pinned	    = pinned;
780877108e4SArnaldo Carvalho de Melo 	attr.exclusive	    = exclusive;
781877108e4SArnaldo Carvalho de Melo 	attr.exclude_user   = exclude_user;
782877108e4SArnaldo Carvalho de Melo 	attr.exclude_kernel = exclude_kernel;
783877108e4SArnaldo Carvalho de Melo 	attr.exclude_hv	    = exclude_hv;
784877108e4SArnaldo Carvalho de Melo 	attr.exclude_idle   = exclude_idle;
785877108e4SArnaldo Carvalho de Melo 	attr.mmap	    = mmap;
786ae938802SArnaldo Carvalho de Melo 	attr.context_switch = context_switch;
787877108e4SArnaldo Carvalho de Melo 	attr.comm	    = comm;
788877108e4SArnaldo Carvalho de Melo 	attr.freq	    = freq;
789877108e4SArnaldo Carvalho de Melo 	attr.inherit_stat   = inherit_stat;
790877108e4SArnaldo Carvalho de Melo 	attr.enable_on_exec = enable_on_exec;
791877108e4SArnaldo Carvalho de Melo 	attr.task	    = task;
792877108e4SArnaldo Carvalho de Melo 	attr.watermark	    = watermark;
793877108e4SArnaldo Carvalho de Melo 	attr.precise_ip	    = precise_ip;
794877108e4SArnaldo Carvalho de Melo 	attr.mmap_data	    = mmap_data;
795877108e4SArnaldo Carvalho de Melo 	attr.sample_id_all  = sample_id_all;
796ad4e3c04SJiri Olsa 	attr.size	    = sizeof(attr);
797877108e4SArnaldo Carvalho de Melo 
798b4b62ee6SJiri Olsa 	evsel__init(&pevsel->evsel, &attr, idx);
799877108e4SArnaldo Carvalho de Melo 	return 0;
800877108e4SArnaldo Carvalho de Melo }
801877108e4SArnaldo Carvalho de Melo 
802877108e4SArnaldo Carvalho de Melo static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
803877108e4SArnaldo Carvalho de Melo {
80430f7c591SArnaldo Carvalho de Melo 	evsel__exit(&pevsel->evsel);
80566dfdff0SJaroslav Škarvada 	Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
806877108e4SArnaldo Carvalho de Melo }
807877108e4SArnaldo Carvalho de Melo 
808877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
809877108e4SArnaldo Carvalho de Melo 				  PyObject *args, PyObject *kwargs)
810877108e4SArnaldo Carvalho de Melo {
81132dcd021SJiri Olsa 	struct evsel *evsel = &pevsel->evsel;
812f854839bSJiri Olsa 	struct perf_cpu_map *cpus = NULL;
8139749b90eSJiri Olsa 	struct perf_thread_map *threads = NULL;
814877108e4SArnaldo Carvalho de Melo 	PyObject *pcpus = NULL, *pthreads = NULL;
8155d2cd909SArnaldo Carvalho de Melo 	int group = 0, inherit = 0;
81664348153SFrederic Weisbecker 	static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
817877108e4SArnaldo Carvalho de Melo 
818877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
8195d2cd909SArnaldo Carvalho de Melo 					 &pcpus, &pthreads, &group, &inherit))
820877108e4SArnaldo Carvalho de Melo 		return NULL;
821877108e4SArnaldo Carvalho de Melo 
822877108e4SArnaldo Carvalho de Melo 	if (pthreads != NULL)
823877108e4SArnaldo Carvalho de Melo 		threads = ((struct pyrf_thread_map *)pthreads)->threads;
824877108e4SArnaldo Carvalho de Melo 
825877108e4SArnaldo Carvalho de Melo 	if (pcpus != NULL)
826877108e4SArnaldo Carvalho de Melo 		cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
827877108e4SArnaldo Carvalho de Melo 
8281fc632ceSJiri Olsa 	evsel->core.attr.inherit = inherit;
829727ab04eSArnaldo Carvalho de Melo 	/*
830727ab04eSArnaldo Carvalho de Melo 	 * This will group just the fds for this single evsel, to group
831727ab04eSArnaldo Carvalho de Melo 	 * multiple events, use evlist.open().
832727ab04eSArnaldo Carvalho de Melo 	 */
8335972d1e0SJiri Olsa 	if (evsel__open(evsel, cpus, threads) < 0) {
834877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
835877108e4SArnaldo Carvalho de Melo 		return NULL;
836877108e4SArnaldo Carvalho de Melo 	}
837877108e4SArnaldo Carvalho de Melo 
838877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
839877108e4SArnaldo Carvalho de Melo 	return Py_None;
840877108e4SArnaldo Carvalho de Melo }
841877108e4SArnaldo Carvalho de Melo 
842877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evsel__methods[] = {
843877108e4SArnaldo Carvalho de Melo 	{
844877108e4SArnaldo Carvalho de Melo 		.ml_name  = "open",
845877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evsel__open,
846877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
847877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("open the event selector file descriptor table.")
848877108e4SArnaldo Carvalho de Melo 	},
849f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
850877108e4SArnaldo Carvalho de Melo };
851877108e4SArnaldo Carvalho de Melo 
852877108e4SArnaldo Carvalho de Melo static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
853877108e4SArnaldo Carvalho de Melo 
854877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evsel__type = {
855877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
856877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.evsel",
857877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_evsel),
858877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_evsel__delete,
859877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
860877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_evsel__doc,
861877108e4SArnaldo Carvalho de Melo 	.tp_methods	= pyrf_evsel__methods,
862877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_evsel__init,
863877108e4SArnaldo Carvalho de Melo };
864877108e4SArnaldo Carvalho de Melo 
865877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__setup_types(void)
866877108e4SArnaldo Carvalho de Melo {
867877108e4SArnaldo Carvalho de Melo 	pyrf_evsel__type.tp_new = PyType_GenericNew;
868877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_evsel__type);
869877108e4SArnaldo Carvalho de Melo }
870877108e4SArnaldo Carvalho de Melo 
871877108e4SArnaldo Carvalho de Melo struct pyrf_evlist {
872877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
873877108e4SArnaldo Carvalho de Melo 
87463503dbaSJiri Olsa 	struct evlist evlist;
875877108e4SArnaldo Carvalho de Melo };
876877108e4SArnaldo Carvalho de Melo 
877877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
8781d037ca1SIrina Tirdea 			     PyObject *args, PyObject *kwargs __maybe_unused)
879877108e4SArnaldo Carvalho de Melo {
8807e2ed097SArnaldo Carvalho de Melo 	PyObject *pcpus = NULL, *pthreads = NULL;
881f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
8829749b90eSJiri Olsa 	struct perf_thread_map *threads;
8837e2ed097SArnaldo Carvalho de Melo 
8847e2ed097SArnaldo Carvalho de Melo 	if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
8857e2ed097SArnaldo Carvalho de Melo 		return -1;
8867e2ed097SArnaldo Carvalho de Melo 
8877e2ed097SArnaldo Carvalho de Melo 	threads = ((struct pyrf_thread_map *)pthreads)->threads;
8887e2ed097SArnaldo Carvalho de Melo 	cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
88952c86bcaSJiri Olsa 	evlist__init(&pevlist->evlist, cpus, threads);
890877108e4SArnaldo Carvalho de Melo 	return 0;
891877108e4SArnaldo Carvalho de Melo }
892877108e4SArnaldo Carvalho de Melo 
893877108e4SArnaldo Carvalho de Melo static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
894877108e4SArnaldo Carvalho de Melo {
895470579b0SJiri Olsa 	evlist__exit(&pevlist->evlist);
89666dfdff0SJaroslav Škarvada 	Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
897877108e4SArnaldo Carvalho de Melo }
898877108e4SArnaldo Carvalho de Melo 
899877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
900877108e4SArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
901877108e4SArnaldo Carvalho de Melo {
90263503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
90364348153SFrederic Weisbecker 	static char *kwlist[] = { "pages", "overwrite", NULL };
904877108e4SArnaldo Carvalho de Melo 	int pages = 128, overwrite = false;
905877108e4SArnaldo Carvalho de Melo 
9067e2ed097SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
9077e2ed097SArnaldo Carvalho de Melo 					 &pages, &overwrite))
908877108e4SArnaldo Carvalho de Melo 		return NULL;
909877108e4SArnaldo Carvalho de Melo 
9109521b5f2SJiri Olsa 	if (evlist__mmap(evlist, pages) < 0) {
911877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
912877108e4SArnaldo Carvalho de Melo 		return NULL;
913877108e4SArnaldo Carvalho de Melo 	}
914877108e4SArnaldo Carvalho de Melo 
915877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
916877108e4SArnaldo Carvalho de Melo 	return Py_None;
917877108e4SArnaldo Carvalho de Melo }
918877108e4SArnaldo Carvalho de Melo 
919877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
920877108e4SArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
921877108e4SArnaldo Carvalho de Melo {
92263503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
92364348153SFrederic Weisbecker 	static char *kwlist[] = { "timeout", NULL };
924877108e4SArnaldo Carvalho de Melo 	int timeout = -1, n;
925877108e4SArnaldo Carvalho de Melo 
926877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
927877108e4SArnaldo Carvalho de Melo 		return NULL;
928877108e4SArnaldo Carvalho de Melo 
92980ab2987SJiri Olsa 	n = evlist__poll(evlist, timeout);
930877108e4SArnaldo Carvalho de Melo 	if (n < 0) {
931877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
932877108e4SArnaldo Carvalho de Melo 		return NULL;
933877108e4SArnaldo Carvalho de Melo 	}
934877108e4SArnaldo Carvalho de Melo 
935877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", n);
936877108e4SArnaldo Carvalho de Melo }
937877108e4SArnaldo Carvalho de Melo 
938877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
9391d037ca1SIrina Tirdea 					 PyObject *args __maybe_unused,
9401d037ca1SIrina Tirdea 					 PyObject *kwargs __maybe_unused)
941877108e4SArnaldo Carvalho de Melo {
94263503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
943877108e4SArnaldo Carvalho de Melo         PyObject *list = PyList_New(0);
944877108e4SArnaldo Carvalho de Melo 	int i;
945877108e4SArnaldo Carvalho de Melo 
94640cb2d51SJiri Olsa 	for (i = 0; i < evlist->core.pollfd.nr; ++i) {
947877108e4SArnaldo Carvalho de Melo 		PyObject *file;
94866dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
94940cb2d51SJiri Olsa 		FILE *fp = fdopen(evlist->core.pollfd.entries[i].fd, "r");
950877108e4SArnaldo Carvalho de Melo 
951877108e4SArnaldo Carvalho de Melo 		if (fp == NULL)
952877108e4SArnaldo Carvalho de Melo 			goto free_list;
953877108e4SArnaldo Carvalho de Melo 
954877108e4SArnaldo Carvalho de Melo 		file = PyFile_FromFile(fp, "perf", "r", NULL);
95566dfdff0SJaroslav Škarvada #else
95640cb2d51SJiri Olsa 		file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1,
957a389aeceSJiri Olsa 				     NULL, NULL, NULL, 0);
95866dfdff0SJaroslav Škarvada #endif
959877108e4SArnaldo Carvalho de Melo 		if (file == NULL)
960877108e4SArnaldo Carvalho de Melo 			goto free_list;
961877108e4SArnaldo Carvalho de Melo 
962877108e4SArnaldo Carvalho de Melo 		if (PyList_Append(list, file) != 0) {
963877108e4SArnaldo Carvalho de Melo 			Py_DECREF(file);
964877108e4SArnaldo Carvalho de Melo 			goto free_list;
965877108e4SArnaldo Carvalho de Melo 		}
966877108e4SArnaldo Carvalho de Melo 
967877108e4SArnaldo Carvalho de Melo 		Py_DECREF(file);
968877108e4SArnaldo Carvalho de Melo 	}
969877108e4SArnaldo Carvalho de Melo 
970877108e4SArnaldo Carvalho de Melo 	return list;
971877108e4SArnaldo Carvalho de Melo free_list:
972877108e4SArnaldo Carvalho de Melo 	return PyErr_NoMemory();
973877108e4SArnaldo Carvalho de Melo }
974877108e4SArnaldo Carvalho de Melo 
975877108e4SArnaldo Carvalho de Melo 
976877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
9771d037ca1SIrina Tirdea 				  PyObject *args,
9781d037ca1SIrina Tirdea 				  PyObject *kwargs __maybe_unused)
979877108e4SArnaldo Carvalho de Melo {
98063503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
981877108e4SArnaldo Carvalho de Melo 	PyObject *pevsel;
98232dcd021SJiri Olsa 	struct evsel *evsel;
983877108e4SArnaldo Carvalho de Melo 
984877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTuple(args, "O", &pevsel))
985877108e4SArnaldo Carvalho de Melo 		return NULL;
986877108e4SArnaldo Carvalho de Melo 
987877108e4SArnaldo Carvalho de Melo 	Py_INCREF(pevsel);
988877108e4SArnaldo Carvalho de Melo 	evsel = &((struct pyrf_evsel *)pevsel)->evsel;
9896484d2f9SJiri Olsa 	evsel->idx = evlist->core.nr_entries;
990a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
991877108e4SArnaldo Carvalho de Melo 
9926484d2f9SJiri Olsa 	return Py_BuildValue("i", evlist->core.nr_entries);
993877108e4SArnaldo Carvalho de Melo }
994877108e4SArnaldo Carvalho de Melo 
995a5830532SJiri Olsa static struct mmap *get_md(struct evlist *evlist, int cpu)
996721f0dfcSJiri Olsa {
997721f0dfcSJiri Olsa 	int i;
998721f0dfcSJiri Olsa 
999c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
1000a5830532SJiri Olsa 		struct mmap *md = &evlist->mmap[i];
1001721f0dfcSJiri Olsa 
100256a94706SJiri Olsa 		if (md->core.cpu == cpu)
1003721f0dfcSJiri Olsa 			return md;
1004721f0dfcSJiri Olsa 	}
1005721f0dfcSJiri Olsa 
1006721f0dfcSJiri Olsa 	return NULL;
1007721f0dfcSJiri Olsa }
1008721f0dfcSJiri Olsa 
1009877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
1010877108e4SArnaldo Carvalho de Melo 					  PyObject *args, PyObject *kwargs)
1011877108e4SArnaldo Carvalho de Melo {
101263503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
1013877108e4SArnaldo Carvalho de Melo 	union perf_event *event;
1014877108e4SArnaldo Carvalho de Melo 	int sample_id_all = 1, cpu;
101564348153SFrederic Weisbecker 	static char *kwlist[] = { "cpu", "sample_id_all", NULL };
1016a5830532SJiri Olsa 	struct mmap *md;
10175538becaSFrederic Weisbecker 	int err;
1018877108e4SArnaldo Carvalho de Melo 
1019877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1020877108e4SArnaldo Carvalho de Melo 					 &cpu, &sample_id_all))
1021877108e4SArnaldo Carvalho de Melo 		return NULL;
1022877108e4SArnaldo Carvalho de Melo 
1023721f0dfcSJiri Olsa 	md = get_md(evlist, cpu);
1024721f0dfcSJiri Olsa 	if (!md)
1025721f0dfcSJiri Olsa 		return NULL;
1026721f0dfcSJiri Olsa 
10277c4d4182SJiri Olsa 	if (perf_mmap__read_init(&md->core) < 0)
102835b7cdc6SKan Liang 		goto end;
102935b7cdc6SKan Liang 
1030151ed5d7SJiri Olsa 	event = perf_mmap__read_event(&md->core);
1031877108e4SArnaldo Carvalho de Melo 	if (event != NULL) {
1032877108e4SArnaldo Carvalho de Melo 		PyObject *pyevent = pyrf_event__new(event);
1033877108e4SArnaldo Carvalho de Melo 		struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
103432dcd021SJiri Olsa 		struct evsel *evsel;
1035877108e4SArnaldo Carvalho de Melo 
1036877108e4SArnaldo Carvalho de Melo 		if (pyevent == NULL)
1037877108e4SArnaldo Carvalho de Melo 			return PyErr_NoMemory();
1038877108e4SArnaldo Carvalho de Melo 
1039377f698dSJiri Olsa 		evsel = perf_evlist__event2evsel(evlist, event);
104083428f2fSPetr Machata 		if (!evsel) {
104183428f2fSPetr Machata 			Py_INCREF(Py_None);
1042377f698dSJiri Olsa 			return Py_None;
104383428f2fSPetr Machata 		}
1044377f698dSJiri Olsa 
1045377f698dSJiri Olsa 		pevent->evsel = evsel;
1046377f698dSJiri Olsa 
10476b6017a2SArnaldo Carvalho de Melo 		err = evsel__parse_sample(evsel, event, &pevent->sample);
1048e8968e65SJiri Olsa 
1049e8968e65SJiri Olsa 		/* Consume the even only after we parsed it out. */
10507728fa0cSJiri Olsa 		perf_mmap__consume(&md->core);
1051e8968e65SJiri Olsa 
10525c6970afSArnaldo Carvalho de Melo 		if (err)
10535c6970afSArnaldo Carvalho de Melo 			return PyErr_Format(PyExc_OSError,
10545c6970afSArnaldo Carvalho de Melo 					    "perf: can't parse sample, err=%d", err);
10555538becaSFrederic Weisbecker 		return pyevent;
10565538becaSFrederic Weisbecker 	}
105735b7cdc6SKan Liang end:
1058877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
1059877108e4SArnaldo Carvalho de Melo 	return Py_None;
1060877108e4SArnaldo Carvalho de Melo }
1061877108e4SArnaldo Carvalho de Melo 
1062727ab04eSArnaldo Carvalho de Melo static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1063727ab04eSArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
1064727ab04eSArnaldo Carvalho de Melo {
106563503dbaSJiri Olsa 	struct evlist *evlist = &pevlist->evlist;
1066727ab04eSArnaldo Carvalho de Melo 	int group = 0;
1067727ab04eSArnaldo Carvalho de Melo 	static char *kwlist[] = { "group", NULL };
1068727ab04eSArnaldo Carvalho de Melo 
1069727ab04eSArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1070727ab04eSArnaldo Carvalho de Melo 		return NULL;
1071727ab04eSArnaldo Carvalho de Melo 
10726a4bb04cSJiri Olsa 	if (group)
107363dab225SArnaldo Carvalho de Melo 		perf_evlist__set_leader(evlist);
10746a4bb04cSJiri Olsa 
1075474ddc4cSJiri Olsa 	if (evlist__open(evlist) < 0) {
1076727ab04eSArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
1077727ab04eSArnaldo Carvalho de Melo 		return NULL;
1078727ab04eSArnaldo Carvalho de Melo 	}
1079727ab04eSArnaldo Carvalho de Melo 
1080727ab04eSArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
1081727ab04eSArnaldo Carvalho de Melo 	return Py_None;
1082727ab04eSArnaldo Carvalho de Melo }
1083727ab04eSArnaldo Carvalho de Melo 
1084877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evlist__methods[] = {
1085877108e4SArnaldo Carvalho de Melo 	{
1086877108e4SArnaldo Carvalho de Melo 		.ml_name  = "mmap",
1087877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__mmap,
1088877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1089877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("mmap the file descriptor table.")
1090877108e4SArnaldo Carvalho de Melo 	},
1091877108e4SArnaldo Carvalho de Melo 	{
1092727ab04eSArnaldo Carvalho de Melo 		.ml_name  = "open",
1093727ab04eSArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__open,
1094727ab04eSArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1095727ab04eSArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("open the file descriptors.")
1096727ab04eSArnaldo Carvalho de Melo 	},
1097727ab04eSArnaldo Carvalho de Melo 	{
1098877108e4SArnaldo Carvalho de Melo 		.ml_name  = "poll",
1099877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__poll,
1100877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1101877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("poll the file descriptor table.")
1102877108e4SArnaldo Carvalho de Melo 	},
1103877108e4SArnaldo Carvalho de Melo 	{
1104877108e4SArnaldo Carvalho de Melo 		.ml_name  = "get_pollfd",
1105877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__get_pollfd,
1106877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1107877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("get the poll file descriptor table.")
1108877108e4SArnaldo Carvalho de Melo 	},
1109877108e4SArnaldo Carvalho de Melo 	{
1110877108e4SArnaldo Carvalho de Melo 		.ml_name  = "add",
1111877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__add,
1112877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1113877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("adds an event selector to the list.")
1114877108e4SArnaldo Carvalho de Melo 	},
1115877108e4SArnaldo Carvalho de Melo 	{
1116877108e4SArnaldo Carvalho de Melo 		.ml_name  = "read_on_cpu",
1117877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__read_on_cpu,
1118877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1119877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("reads an event.")
1120877108e4SArnaldo Carvalho de Melo 	},
1121f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
1122877108e4SArnaldo Carvalho de Melo };
1123877108e4SArnaldo Carvalho de Melo 
1124877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1125877108e4SArnaldo Carvalho de Melo {
1126877108e4SArnaldo Carvalho de Melo 	struct pyrf_evlist *pevlist = (void *)obj;
1127877108e4SArnaldo Carvalho de Melo 
11286484d2f9SJiri Olsa 	return pevlist->evlist.core.nr_entries;
1129877108e4SArnaldo Carvalho de Melo }
1130877108e4SArnaldo Carvalho de Melo 
1131877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1132877108e4SArnaldo Carvalho de Melo {
1133877108e4SArnaldo Carvalho de Melo 	struct pyrf_evlist *pevlist = (void *)obj;
113432dcd021SJiri Olsa 	struct evsel *pos;
1135877108e4SArnaldo Carvalho de Melo 
11366484d2f9SJiri Olsa 	if (i >= pevlist->evlist.core.nr_entries)
1137877108e4SArnaldo Carvalho de Melo 		return NULL;
1138877108e4SArnaldo Carvalho de Melo 
1139e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(&pevlist->evlist, pos) {
1140877108e4SArnaldo Carvalho de Melo 		if (i-- == 0)
1141877108e4SArnaldo Carvalho de Melo 			break;
11420050f7aaSArnaldo Carvalho de Melo 	}
1143877108e4SArnaldo Carvalho de Melo 
1144877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1145877108e4SArnaldo Carvalho de Melo }
1146877108e4SArnaldo Carvalho de Melo 
1147877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_evlist__sequence_methods = {
1148877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_evlist__length,
1149877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_evlist__item,
1150877108e4SArnaldo Carvalho de Melo };
1151877108e4SArnaldo Carvalho de Melo 
1152877108e4SArnaldo Carvalho de Melo static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1153877108e4SArnaldo Carvalho de Melo 
1154877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evlist__type = {
1155877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
1156877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.evlist",
1157877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_evlist),
1158877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_evlist__delete,
1159877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1160877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_evlist__sequence_methods,
1161877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_evlist__doc,
1162877108e4SArnaldo Carvalho de Melo 	.tp_methods	= pyrf_evlist__methods,
1163877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_evlist__init,
1164877108e4SArnaldo Carvalho de Melo };
1165877108e4SArnaldo Carvalho de Melo 
1166877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__setup_types(void)
1167877108e4SArnaldo Carvalho de Melo {
1168877108e4SArnaldo Carvalho de Melo 	pyrf_evlist__type.tp_new = PyType_GenericNew;
1169877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_evlist__type);
1170877108e4SArnaldo Carvalho de Melo }
1171877108e4SArnaldo Carvalho de Melo 
11725865fe36SArnaldo Carvalho de Melo #define PERF_CONST(name) { #name, PERF_##name }
11735865fe36SArnaldo Carvalho de Melo 
1174877108e4SArnaldo Carvalho de Melo static struct {
1175877108e4SArnaldo Carvalho de Melo 	const char *name;
1176877108e4SArnaldo Carvalho de Melo 	int	    value;
1177877108e4SArnaldo Carvalho de Melo } perf__constants[] = {
11785865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_HARDWARE),
11795865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_SOFTWARE),
11805865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_TRACEPOINT),
11815865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_HW_CACHE),
11825865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_RAW),
11835865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_BREAKPOINT),
1184877108e4SArnaldo Carvalho de Melo 
11855865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CPU_CYCLES),
11865865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_INSTRUCTIONS),
11875865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_REFERENCES),
11885865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_MISSES),
11895865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
11905865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_BRANCH_MISSES),
11915865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_BUS_CYCLES),
11925865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_L1D),
11935865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_L1I),
11945865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_LL),
11955865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_DTLB),
11965865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_ITLB),
11975865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_BPU),
11985865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_OP_READ),
11995865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
12005865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
12015865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
12025865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1203877108e4SArnaldo Carvalho de Melo 
12045865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
12055865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1206129c04cbSIngo Molnar 
12075865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_CPU_CLOCK),
12085865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_TASK_CLOCK),
12095865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_PAGE_FAULTS),
12105865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
12115865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
12125865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
12135865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
12145865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
12155865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_EMULATION_FAULTS),
12165865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_DUMMY),
1217877108e4SArnaldo Carvalho de Melo 
12185865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_IP),
12195865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_TID),
12205865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_TIME),
12215865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_ADDR),
12225865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_READ),
12235865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_CALLCHAIN),
12245865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_ID),
12255865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_CPU),
12265865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_PERIOD),
12275865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_STREAM_ID),
12285865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_RAW),
1229877108e4SArnaldo Carvalho de Melo 
12305865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
12315865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
12325865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_ID),
12335865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_GROUP),
1234877108e4SArnaldo Carvalho de Melo 
12355865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_MMAP),
12365865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_LOST),
12375865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_COMM),
12385865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_EXIT),
12395865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_THROTTLE),
12405865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_UNTHROTTLE),
12415865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_FORK),
12425865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_READ),
12435865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_SAMPLE),
124484576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_MMAP2),
124584576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_AUX),
124684576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_ITRACE_START),
124784576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_LOST_SAMPLES),
124884576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_SWITCH),
124984576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1250ae938802SArnaldo Carvalho de Melo 
1251ae938802SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_MISC_SWITCH_OUT),
1252f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
1253877108e4SArnaldo Carvalho de Melo };
1254877108e4SArnaldo Carvalho de Melo 
12551075fbb2SJiri Olsa static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
12561075fbb2SJiri Olsa 				  PyObject *args, PyObject *kwargs)
12571075fbb2SJiri Olsa {
125897fbf3f0STzvetomir Stoyanov 	struct tep_event *tp_format;
12591075fbb2SJiri Olsa 	static char *kwlist[] = { "sys", "name", NULL };
12601075fbb2SJiri Olsa 	char *sys  = NULL;
12611075fbb2SJiri Olsa 	char *name = NULL;
12621075fbb2SJiri Olsa 
12631075fbb2SJiri Olsa 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
12641075fbb2SJiri Olsa 					 &sys, &name))
12651075fbb2SJiri Olsa 		return NULL;
12661075fbb2SJiri Olsa 
12671075fbb2SJiri Olsa 	tp_format = trace_event__tp_format(sys, name);
12681075fbb2SJiri Olsa 	if (IS_ERR(tp_format))
126966dfdff0SJaroslav Škarvada 		return _PyLong_FromLong(-1);
12701075fbb2SJiri Olsa 
127166dfdff0SJaroslav Škarvada 	return _PyLong_FromLong(tp_format->id);
12721075fbb2SJiri Olsa }
12731075fbb2SJiri Olsa 
1274877108e4SArnaldo Carvalho de Melo static PyMethodDef perf__methods[] = {
12751075fbb2SJiri Olsa 	{
12761075fbb2SJiri Olsa 		.ml_name  = "tracepoint",
12771075fbb2SJiri Olsa 		.ml_meth  = (PyCFunction) pyrf__tracepoint,
12781075fbb2SJiri Olsa 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
12791075fbb2SJiri Olsa 		.ml_doc	  = PyDoc_STR("Get tracepoint config.")
12801075fbb2SJiri Olsa 	},
1281f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
1282877108e4SArnaldo Carvalho de Melo };
1283877108e4SArnaldo Carvalho de Melo 
128466dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
1285877108e4SArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void)
128666dfdff0SJaroslav Škarvada #else
128766dfdff0SJaroslav Škarvada PyMODINIT_FUNC PyInit_perf(void)
128866dfdff0SJaroslav Škarvada #endif
1289877108e4SArnaldo Carvalho de Melo {
1290877108e4SArnaldo Carvalho de Melo 	PyObject *obj;
1291877108e4SArnaldo Carvalho de Melo 	int i;
129266dfdff0SJaroslav Škarvada 	PyObject *dict;
129366dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
129466dfdff0SJaroslav Škarvada 	PyObject *module = Py_InitModule("perf", perf__methods);
129566dfdff0SJaroslav Škarvada #else
129666dfdff0SJaroslav Škarvada 	static struct PyModuleDef moduledef = {
129766dfdff0SJaroslav Škarvada 		PyModuleDef_HEAD_INIT,
129866dfdff0SJaroslav Škarvada 		"perf",			/* m_name */
129966dfdff0SJaroslav Škarvada 		"",			/* m_doc */
130066dfdff0SJaroslav Škarvada 		-1,			/* m_size */
130166dfdff0SJaroslav Škarvada 		perf__methods,		/* m_methods */
130266dfdff0SJaroslav Škarvada 		NULL,			/* m_reload */
130366dfdff0SJaroslav Škarvada 		NULL,			/* m_traverse */
130466dfdff0SJaroslav Škarvada 		NULL,			/* m_clear */
130566dfdff0SJaroslav Škarvada 		NULL,			/* m_free */
130666dfdff0SJaroslav Škarvada 	};
130766dfdff0SJaroslav Škarvada 	PyObject *module = PyModule_Create(&moduledef);
130866dfdff0SJaroslav Škarvada #endif
1309877108e4SArnaldo Carvalho de Melo 
1310877108e4SArnaldo Carvalho de Melo 	if (module == NULL ||
1311877108e4SArnaldo Carvalho de Melo 	    pyrf_event__setup_types() < 0 ||
1312877108e4SArnaldo Carvalho de Melo 	    pyrf_evlist__setup_types() < 0 ||
1313877108e4SArnaldo Carvalho de Melo 	    pyrf_evsel__setup_types() < 0 ||
1314877108e4SArnaldo Carvalho de Melo 	    pyrf_thread_map__setup_types() < 0 ||
1315877108e4SArnaldo Carvalho de Melo 	    pyrf_cpu_map__setup_types() < 0)
131666dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION < 3
1317877108e4SArnaldo Carvalho de Melo 		return;
131866dfdff0SJaroslav Škarvada #else
131966dfdff0SJaroslav Škarvada 		return module;
132066dfdff0SJaroslav Škarvada #endif
1321877108e4SArnaldo Carvalho de Melo 
1322918512b4SJiri Olsa 	/* The page_size is placed in util object. */
13230da2e9c2SArnaldo Carvalho de Melo 	page_size = sysconf(_SC_PAGE_SIZE);
13240da2e9c2SArnaldo Carvalho de Melo 
1325877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_evlist__type);
1326877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1327877108e4SArnaldo Carvalho de Melo 
1328877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_evsel__type);
1329877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1330877108e4SArnaldo Carvalho de Melo 
133185e37de3SJiri Olsa 	Py_INCREF(&pyrf_mmap_event__type);
133285e37de3SJiri Olsa 	PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
133385e37de3SJiri Olsa 
133485e37de3SJiri Olsa 	Py_INCREF(&pyrf_lost_event__type);
133585e37de3SJiri Olsa 	PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
133685e37de3SJiri Olsa 
133785e37de3SJiri Olsa 	Py_INCREF(&pyrf_comm_event__type);
133885e37de3SJiri Olsa 	PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
133985e37de3SJiri Olsa 
134085e37de3SJiri Olsa 	Py_INCREF(&pyrf_task_event__type);
134185e37de3SJiri Olsa 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
134285e37de3SJiri Olsa 
134385e37de3SJiri Olsa 	Py_INCREF(&pyrf_throttle_event__type);
134485e37de3SJiri Olsa 	PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
134585e37de3SJiri Olsa 
134685e37de3SJiri Olsa 	Py_INCREF(&pyrf_task_event__type);
134785e37de3SJiri Olsa 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
134885e37de3SJiri Olsa 
134985e37de3SJiri Olsa 	Py_INCREF(&pyrf_read_event__type);
135085e37de3SJiri Olsa 	PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
135185e37de3SJiri Olsa 
135285e37de3SJiri Olsa 	Py_INCREF(&pyrf_sample_event__type);
135385e37de3SJiri Olsa 	PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
135485e37de3SJiri Olsa 
135585e37de3SJiri Olsa 	Py_INCREF(&pyrf_context_switch_event__type);
135685e37de3SJiri Olsa 	PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
135785e37de3SJiri Olsa 
1358877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_thread_map__type);
1359877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1360877108e4SArnaldo Carvalho de Melo 
1361877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_cpu_map__type);
1362877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1363877108e4SArnaldo Carvalho de Melo 
1364877108e4SArnaldo Carvalho de Melo 	dict = PyModule_GetDict(module);
1365877108e4SArnaldo Carvalho de Melo 	if (dict == NULL)
1366877108e4SArnaldo Carvalho de Melo 		goto error;
1367877108e4SArnaldo Carvalho de Melo 
1368877108e4SArnaldo Carvalho de Melo 	for (i = 0; perf__constants[i].name != NULL; i++) {
136966dfdff0SJaroslav Škarvada 		obj = _PyLong_FromLong(perf__constants[i].value);
1370877108e4SArnaldo Carvalho de Melo 		if (obj == NULL)
1371877108e4SArnaldo Carvalho de Melo 			goto error;
1372877108e4SArnaldo Carvalho de Melo 		PyDict_SetItemString(dict, perf__constants[i].name, obj);
1373877108e4SArnaldo Carvalho de Melo 		Py_DECREF(obj);
1374877108e4SArnaldo Carvalho de Melo 	}
1375877108e4SArnaldo Carvalho de Melo 
1376877108e4SArnaldo Carvalho de Melo error:
1377877108e4SArnaldo Carvalho de Melo 	if (PyErr_Occurred())
1378877108e4SArnaldo Carvalho de Melo 		PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
137966dfdff0SJaroslav Škarvada #if PY_MAJOR_VERSION >= 3
138066dfdff0SJaroslav Škarvada 	return module;
138166dfdff0SJaroslav Škarvada #endif
1382877108e4SArnaldo Carvalho de Melo }
13830c6332e9SArnaldo Carvalho de Melo 
13840c6332e9SArnaldo Carvalho de Melo /*
13850c6332e9SArnaldo Carvalho de Melo  * Dummy, to avoid dragging all the test_attr infrastructure in the python
13860c6332e9SArnaldo Carvalho de Melo  * binding.
13870c6332e9SArnaldo Carvalho de Melo  */
13880c6332e9SArnaldo Carvalho de Melo void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
13890c6332e9SArnaldo Carvalho de Melo                      int fd, int group_fd, unsigned long flags)
13900c6332e9SArnaldo Carvalho de Melo {
13910c6332e9SArnaldo Carvalho de Melo }
1392