xref: /linux/tools/perf/util/python.c (revision 56e2e05644a9494e8ba3165182dcdf43d40cc6a2)
1877108e4SArnaldo Carvalho de Melo #include <Python.h>
2877108e4SArnaldo Carvalho de Melo #include <structmember.h>
3877108e4SArnaldo Carvalho de Melo #include <inttypes.h>
4877108e4SArnaldo Carvalho de Melo #include <poll.h>
51075fbb2SJiri Olsa #include <linux/err.h>
6877108e4SArnaldo Carvalho de Melo #include "evlist.h"
7*56e2e056SArnaldo Carvalho de Melo #include "callchain.h"
8877108e4SArnaldo Carvalho de Melo #include "evsel.h"
9877108e4SArnaldo Carvalho de Melo #include "event.h"
10877108e4SArnaldo Carvalho de Melo #include "cpumap.h"
11fea01392SArnaldo Carvalho de Melo #include "print_binary.h"
12877108e4SArnaldo Carvalho de Melo #include "thread_map.h"
13877108e4SArnaldo Carvalho de Melo 
148afb4c01SAdrian Hunter /*
15*56e2e056SArnaldo Carvalho de Melo  * Provide these two so that we don't have to link against callchain.c and
16*56e2e056SArnaldo Carvalho de Melo  * start dragging hist.c, etc.
17*56e2e056SArnaldo Carvalho de Melo  */
18*56e2e056SArnaldo Carvalho de Melo struct callchain_param callchain_param;
19*56e2e056SArnaldo Carvalho de Melo 
20*56e2e056SArnaldo Carvalho de Melo int parse_callchain_record(const char *arg __maybe_unused,
21*56e2e056SArnaldo Carvalho de Melo 			   struct callchain_param *param __maybe_unused)
22*56e2e056SArnaldo Carvalho de Melo {
23*56e2e056SArnaldo Carvalho de Melo 	return 0;
24*56e2e056SArnaldo Carvalho de Melo }
25*56e2e056SArnaldo Carvalho de Melo 
26*56e2e056SArnaldo Carvalho de Melo /*
278afb4c01SAdrian Hunter  * Support debug printing even though util/debug.c is not linked.  That means
288afb4c01SAdrian Hunter  * implementing 'verbose' and 'eprintf'.
298afb4c01SAdrian Hunter  */
308afb4c01SAdrian Hunter int verbose;
318afb4c01SAdrian Hunter 
32c95688aaSJiri Olsa int eprintf(int level, int var, const char *fmt, ...)
338afb4c01SAdrian Hunter {
348afb4c01SAdrian Hunter 	va_list args;
358afb4c01SAdrian Hunter 	int ret = 0;
368afb4c01SAdrian Hunter 
37c95688aaSJiri Olsa 	if (var >= level) {
388afb4c01SAdrian Hunter 		va_start(args, fmt);
398afb4c01SAdrian Hunter 		ret = vfprintf(stderr, fmt, args);
408afb4c01SAdrian Hunter 		va_end(args);
418afb4c01SAdrian Hunter 	}
428afb4c01SAdrian Hunter 
438afb4c01SAdrian Hunter 	return ret;
448afb4c01SAdrian Hunter }
458afb4c01SAdrian Hunter 
46cfff2d90SFrederic Weisbecker /* Define PyVarObject_HEAD_INIT for python 2.5 */
47cfff2d90SFrederic Weisbecker #ifndef PyVarObject_HEAD_INIT
48cfff2d90SFrederic Weisbecker # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
49cfff2d90SFrederic Weisbecker #endif
50cfff2d90SFrederic Weisbecker 
51f6bbc1daSArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void);
52f6bbc1daSArnaldo Carvalho de Melo 
53877108e4SArnaldo Carvalho de Melo #define member_def(type, member, ptype, help) \
54877108e4SArnaldo Carvalho de Melo 	{ #member, ptype, \
55877108e4SArnaldo Carvalho de Melo 	  offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
56877108e4SArnaldo Carvalho de Melo 	  0, help }
57877108e4SArnaldo Carvalho de Melo 
58877108e4SArnaldo Carvalho de Melo #define sample_member_def(name, member, ptype, help) \
59877108e4SArnaldo Carvalho de Melo 	{ #name, ptype, \
60877108e4SArnaldo Carvalho de Melo 	  offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
61877108e4SArnaldo Carvalho de Melo 	  0, help }
62877108e4SArnaldo Carvalho de Melo 
63877108e4SArnaldo Carvalho de Melo struct pyrf_event {
64877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
65377f698dSJiri Olsa 	struct perf_evsel *evsel;
66877108e4SArnaldo Carvalho de Melo 	struct perf_sample sample;
67877108e4SArnaldo Carvalho de Melo 	union perf_event   event;
68877108e4SArnaldo Carvalho de Melo };
69877108e4SArnaldo Carvalho de Melo 
70877108e4SArnaldo Carvalho de Melo #define sample_members \
71f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"),			 \
72877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_pid, pid, T_INT, "event pid"),			 \
73877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_tid, tid, T_INT, "event tid"),			 \
74f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"),		 \
75f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"),		 \
76f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_id, id, T_ULONGLONG, "event id"),			 \
77f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
78f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_period, period, T_ULONGLONG, "event period"),		 \
79877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
80877108e4SArnaldo Carvalho de Melo 
81877108e4SArnaldo Carvalho de Melo static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
82877108e4SArnaldo Carvalho de Melo 
83877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_mmap_event__members[] = {
84877108e4SArnaldo Carvalho de Melo 	sample_members
85877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
86ae938802SArnaldo Carvalho de Melo 	member_def(perf_event_header, misc, T_UINT, "event misc"),
87877108e4SArnaldo Carvalho de Melo 	member_def(mmap_event, pid, T_UINT, "event pid"),
88877108e4SArnaldo Carvalho de Melo 	member_def(mmap_event, tid, T_UINT, "event tid"),
89f6bbc1daSArnaldo Carvalho de Melo 	member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
90f6bbc1daSArnaldo Carvalho de Melo 	member_def(mmap_event, len, T_ULONGLONG, "map length"),
91f6bbc1daSArnaldo Carvalho de Melo 	member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
92877108e4SArnaldo Carvalho de Melo 	member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
93f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
94877108e4SArnaldo Carvalho de Melo };
95877108e4SArnaldo Carvalho de Melo 
96877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
97877108e4SArnaldo Carvalho de Melo {
98877108e4SArnaldo Carvalho de Melo 	PyObject *ret;
99877108e4SArnaldo Carvalho de Melo 	char *s;
100877108e4SArnaldo Carvalho de Melo 
101877108e4SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
102877108e4SArnaldo Carvalho de Melo 			 "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
103877108e4SArnaldo Carvalho de Melo 			 "filename: %s }",
104877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.pid, pevent->event.mmap.tid,
105877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.start, pevent->event.mmap.len,
106877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
107877108e4SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
108877108e4SArnaldo Carvalho de Melo 	} else {
109877108e4SArnaldo Carvalho de Melo 		ret = PyString_FromString(s);
110877108e4SArnaldo Carvalho de Melo 		free(s);
111877108e4SArnaldo Carvalho de Melo 	}
112877108e4SArnaldo Carvalho de Melo 	return ret;
113877108e4SArnaldo Carvalho de Melo }
114877108e4SArnaldo Carvalho de Melo 
115877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_mmap_event__type = {
116877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
117877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.mmap_event",
118877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
119877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
120877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_mmap_event__doc,
121877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_mmap_event__members,
122877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_mmap_event__repr,
123877108e4SArnaldo Carvalho de Melo };
124877108e4SArnaldo Carvalho de Melo 
125877108e4SArnaldo Carvalho de Melo static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
126877108e4SArnaldo Carvalho de Melo 
127877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_task_event__members[] = {
128877108e4SArnaldo Carvalho de Melo 	sample_members
129877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
130877108e4SArnaldo Carvalho de Melo 	member_def(fork_event, pid, T_UINT, "event pid"),
131877108e4SArnaldo Carvalho de Melo 	member_def(fork_event, ppid, T_UINT, "event ppid"),
132877108e4SArnaldo Carvalho de Melo 	member_def(fork_event, tid, T_UINT, "event tid"),
133877108e4SArnaldo Carvalho de Melo 	member_def(fork_event, ptid, T_UINT, "event ptid"),
134f6bbc1daSArnaldo Carvalho de Melo 	member_def(fork_event, time, T_ULONGLONG, "timestamp"),
135f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
136877108e4SArnaldo Carvalho de Melo };
137877108e4SArnaldo Carvalho de Melo 
138877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
139877108e4SArnaldo Carvalho de Melo {
140877108e4SArnaldo Carvalho de Melo 	return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
141877108e4SArnaldo Carvalho de Melo 				   "ptid: %u, time: %" PRIu64 "}",
142877108e4SArnaldo Carvalho de Melo 				   pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
143877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.pid,
144877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.ppid,
145877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.tid,
146877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.ptid,
147877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.time);
148877108e4SArnaldo Carvalho de Melo }
149877108e4SArnaldo Carvalho de Melo 
150877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_task_event__type = {
151877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
152877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.task_event",
153877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
154877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
155877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_task_event__doc,
156877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_task_event__members,
157877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_task_event__repr,
158877108e4SArnaldo Carvalho de Melo };
159877108e4SArnaldo Carvalho de Melo 
160877108e4SArnaldo Carvalho de Melo static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
161877108e4SArnaldo Carvalho de Melo 
162877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_comm_event__members[] = {
163877108e4SArnaldo Carvalho de Melo 	sample_members
164877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
165877108e4SArnaldo Carvalho de Melo 	member_def(comm_event, pid, T_UINT, "event pid"),
166877108e4SArnaldo Carvalho de Melo 	member_def(comm_event, tid, T_UINT, "event tid"),
167877108e4SArnaldo Carvalho de Melo 	member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
168f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
169877108e4SArnaldo Carvalho de Melo };
170877108e4SArnaldo Carvalho de Melo 
171877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
172877108e4SArnaldo Carvalho de Melo {
173877108e4SArnaldo Carvalho de Melo 	return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
174877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.pid,
175877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.tid,
176877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.comm);
177877108e4SArnaldo Carvalho de Melo }
178877108e4SArnaldo Carvalho de Melo 
179877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_comm_event__type = {
180877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
181877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.comm_event",
182877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
183877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
184877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_comm_event__doc,
185877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_comm_event__members,
186877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_comm_event__repr,
187877108e4SArnaldo Carvalho de Melo };
188877108e4SArnaldo Carvalho de Melo 
189877108e4SArnaldo Carvalho de Melo static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
190877108e4SArnaldo Carvalho de Melo 
191877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_throttle_event__members[] = {
192877108e4SArnaldo Carvalho de Melo 	sample_members
193877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
194f6bbc1daSArnaldo Carvalho de Melo 	member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
195f6bbc1daSArnaldo Carvalho de Melo 	member_def(throttle_event, id, T_ULONGLONG, "event id"),
196f6bbc1daSArnaldo Carvalho de Melo 	member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
197f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
198877108e4SArnaldo Carvalho de Melo };
199877108e4SArnaldo Carvalho de Melo 
200877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
201877108e4SArnaldo Carvalho de Melo {
202877108e4SArnaldo Carvalho de Melo 	struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
203877108e4SArnaldo Carvalho de Melo 
204877108e4SArnaldo Carvalho de Melo 	return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
205877108e4SArnaldo Carvalho de Melo 				   ", stream_id: %" PRIu64 " }",
206877108e4SArnaldo Carvalho de Melo 				   pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
207877108e4SArnaldo Carvalho de Melo 				   te->time, te->id, te->stream_id);
208877108e4SArnaldo Carvalho de Melo }
209877108e4SArnaldo Carvalho de Melo 
210877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_throttle_event__type = {
211877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
212877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.throttle_event",
213877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
214877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
215877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_throttle_event__doc,
216877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_throttle_event__members,
217877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_throttle_event__repr,
218877108e4SArnaldo Carvalho de Melo };
219877108e4SArnaldo Carvalho de Melo 
2203e9f45a7SArnaldo Carvalho de Melo static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
2213e9f45a7SArnaldo Carvalho de Melo 
2223e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_lost_event__members[] = {
2233e9f45a7SArnaldo Carvalho de Melo 	sample_members
2243e9f45a7SArnaldo Carvalho de Melo 	member_def(lost_event, id, T_ULONGLONG, "event id"),
2253e9f45a7SArnaldo Carvalho de Melo 	member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
2263e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
2273e9f45a7SArnaldo Carvalho de Melo };
2283e9f45a7SArnaldo Carvalho de Melo 
2293e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
2303e9f45a7SArnaldo Carvalho de Melo {
2313e9f45a7SArnaldo Carvalho de Melo 	PyObject *ret;
2323e9f45a7SArnaldo Carvalho de Melo 	char *s;
2333e9f45a7SArnaldo Carvalho de Melo 
2343e9f45a7SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
2353e9f45a7SArnaldo Carvalho de Melo 			 "lost: %#" PRIx64 " }",
2363e9f45a7SArnaldo Carvalho de Melo 		     pevent->event.lost.id, pevent->event.lost.lost) < 0) {
2373e9f45a7SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
2383e9f45a7SArnaldo Carvalho de Melo 	} else {
2393e9f45a7SArnaldo Carvalho de Melo 		ret = PyString_FromString(s);
2403e9f45a7SArnaldo Carvalho de Melo 		free(s);
2413e9f45a7SArnaldo Carvalho de Melo 	}
2423e9f45a7SArnaldo Carvalho de Melo 	return ret;
2433e9f45a7SArnaldo Carvalho de Melo }
2443e9f45a7SArnaldo Carvalho de Melo 
2453e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_lost_event__type = {
2463e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
2473e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.lost_event",
2483e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
2493e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
2503e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_lost_event__doc,
2513e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_lost_event__members,
2523e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_lost_event__repr,
2533e9f45a7SArnaldo Carvalho de Melo };
2543e9f45a7SArnaldo Carvalho de Melo 
2553e9f45a7SArnaldo Carvalho de Melo static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
2563e9f45a7SArnaldo Carvalho de Melo 
2573e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_read_event__members[] = {
2583e9f45a7SArnaldo Carvalho de Melo 	sample_members
2593e9f45a7SArnaldo Carvalho de Melo 	member_def(read_event, pid, T_UINT, "event pid"),
2603e9f45a7SArnaldo Carvalho de Melo 	member_def(read_event, tid, T_UINT, "event tid"),
2613e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
2623e9f45a7SArnaldo Carvalho de Melo };
2633e9f45a7SArnaldo Carvalho de Melo 
2643e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
2653e9f45a7SArnaldo Carvalho de Melo {
2663e9f45a7SArnaldo Carvalho de Melo 	return PyString_FromFormat("{ type: read, pid: %u, tid: %u }",
2673e9f45a7SArnaldo Carvalho de Melo 				   pevent->event.read.pid,
2683e9f45a7SArnaldo Carvalho de Melo 				   pevent->event.read.tid);
2693e9f45a7SArnaldo Carvalho de Melo 	/*
2703e9f45a7SArnaldo Carvalho de Melo  	 * FIXME: return the array of read values,
2713e9f45a7SArnaldo Carvalho de Melo  	 * making this method useful ;-)
2723e9f45a7SArnaldo Carvalho de Melo  	 */
2733e9f45a7SArnaldo Carvalho de Melo }
2743e9f45a7SArnaldo Carvalho de Melo 
2753e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_read_event__type = {
2763e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
2773e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.read_event",
2783e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
2793e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
2803e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_read_event__doc,
2813e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_read_event__members,
2823e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_read_event__repr,
2833e9f45a7SArnaldo Carvalho de Melo };
2843e9f45a7SArnaldo Carvalho de Melo 
2853e9f45a7SArnaldo Carvalho de Melo static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
2863e9f45a7SArnaldo Carvalho de Melo 
2873e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_sample_event__members[] = {
2883e9f45a7SArnaldo Carvalho de Melo 	sample_members
2893e9f45a7SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
2903e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
2913e9f45a7SArnaldo Carvalho de Melo };
2923e9f45a7SArnaldo Carvalho de Melo 
2933e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
2943e9f45a7SArnaldo Carvalho de Melo {
2953e9f45a7SArnaldo Carvalho de Melo 	PyObject *ret;
2963e9f45a7SArnaldo Carvalho de Melo 	char *s;
2973e9f45a7SArnaldo Carvalho de Melo 
2983e9f45a7SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: sample }") < 0) {
2993e9f45a7SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
3003e9f45a7SArnaldo Carvalho de Melo 	} else {
3013e9f45a7SArnaldo Carvalho de Melo 		ret = PyString_FromString(s);
3023e9f45a7SArnaldo Carvalho de Melo 		free(s);
3033e9f45a7SArnaldo Carvalho de Melo 	}
3043e9f45a7SArnaldo Carvalho de Melo 	return ret;
3053e9f45a7SArnaldo Carvalho de Melo }
3063e9f45a7SArnaldo Carvalho de Melo 
307bae57e38SJiri Olsa static bool is_tracepoint(struct pyrf_event *pevent)
308bae57e38SJiri Olsa {
309bae57e38SJiri Olsa 	return pevent->evsel->attr.type == PERF_TYPE_TRACEPOINT;
310bae57e38SJiri Olsa }
311bae57e38SJiri Olsa 
312bae57e38SJiri Olsa static PyObject*
313bae57e38SJiri Olsa tracepoint_field(struct pyrf_event *pe, struct format_field *field)
314bae57e38SJiri Olsa {
315bae57e38SJiri Olsa 	struct pevent *pevent = field->event->pevent;
316bae57e38SJiri Olsa 	void *data = pe->sample.raw_data;
317bae57e38SJiri Olsa 	PyObject *ret = NULL;
318bae57e38SJiri Olsa 	unsigned long long val;
319bae57e38SJiri Olsa 	unsigned int offset, len;
320bae57e38SJiri Olsa 
321bae57e38SJiri Olsa 	if (field->flags & FIELD_IS_ARRAY) {
322bae57e38SJiri Olsa 		offset = field->offset;
323bae57e38SJiri Olsa 		len    = field->size;
324bae57e38SJiri Olsa 		if (field->flags & FIELD_IS_DYNAMIC) {
325bae57e38SJiri Olsa 			val     = pevent_read_number(pevent, data + offset, len);
326bae57e38SJiri Olsa 			offset  = val;
327bae57e38SJiri Olsa 			len     = offset >> 16;
328bae57e38SJiri Olsa 			offset &= 0xffff;
329bae57e38SJiri Olsa 		}
330bae57e38SJiri Olsa 		if (field->flags & FIELD_IS_STRING &&
331bae57e38SJiri Olsa 		    is_printable_array(data + offset, len)) {
332bae57e38SJiri Olsa 			ret = PyString_FromString((char *)data + offset);
333bae57e38SJiri Olsa 		} else {
334bae57e38SJiri Olsa 			ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
335bae57e38SJiri Olsa 			field->flags &= ~FIELD_IS_STRING;
336bae57e38SJiri Olsa 		}
337bae57e38SJiri Olsa 	} else {
338bae57e38SJiri Olsa 		val = pevent_read_number(pevent, data + field->offset,
339bae57e38SJiri Olsa 					 field->size);
340bae57e38SJiri Olsa 		if (field->flags & FIELD_IS_POINTER)
341bae57e38SJiri Olsa 			ret = PyLong_FromUnsignedLong((unsigned long) val);
342bae57e38SJiri Olsa 		else if (field->flags & FIELD_IS_SIGNED)
343bae57e38SJiri Olsa 			ret = PyLong_FromLong((long) val);
344bae57e38SJiri Olsa 		else
345bae57e38SJiri Olsa 			ret = PyLong_FromUnsignedLong((unsigned long) val);
346bae57e38SJiri Olsa 	}
347bae57e38SJiri Olsa 
348bae57e38SJiri Olsa 	return ret;
349bae57e38SJiri Olsa }
350bae57e38SJiri Olsa 
351bae57e38SJiri Olsa static PyObject*
352bae57e38SJiri Olsa get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
353bae57e38SJiri Olsa {
354bae57e38SJiri Olsa 	const char *str = PyString_AsString(PyObject_Str(attr_name));
355bae57e38SJiri Olsa 	struct perf_evsel *evsel = pevent->evsel;
356bae57e38SJiri Olsa 	struct format_field *field;
357bae57e38SJiri Olsa 
358bae57e38SJiri Olsa 	if (!evsel->tp_format) {
359bae57e38SJiri Olsa 		struct event_format *tp_format;
360bae57e38SJiri Olsa 
361bae57e38SJiri Olsa 		tp_format = trace_event__tp_format_id(evsel->attr.config);
362bae57e38SJiri Olsa 		if (!tp_format)
363bae57e38SJiri Olsa 			return NULL;
364bae57e38SJiri Olsa 
365bae57e38SJiri Olsa 		evsel->tp_format = tp_format;
366bae57e38SJiri Olsa 	}
367bae57e38SJiri Olsa 
368bae57e38SJiri Olsa 	field = pevent_find_any_field(evsel->tp_format, str);
369bae57e38SJiri Olsa 	if (!field)
370bae57e38SJiri Olsa 		return NULL;
371bae57e38SJiri Olsa 
372bae57e38SJiri Olsa 	return tracepoint_field(pevent, field);
373bae57e38SJiri Olsa }
374bae57e38SJiri Olsa 
375bae57e38SJiri Olsa static PyObject*
376bae57e38SJiri Olsa pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
377bae57e38SJiri Olsa {
378bae57e38SJiri Olsa 	PyObject *obj = NULL;
379bae57e38SJiri Olsa 
380bae57e38SJiri Olsa 	if (is_tracepoint(pevent))
381bae57e38SJiri Olsa 		obj = get_tracepoint_field(pevent, attr_name);
382bae57e38SJiri Olsa 
383bae57e38SJiri Olsa 	return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
384bae57e38SJiri Olsa }
385bae57e38SJiri Olsa 
3863e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_sample_event__type = {
3873e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
3883e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.sample_event",
3893e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
3903e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
3913e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_sample_event__doc,
3923e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_sample_event__members,
3933e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_sample_event__repr,
394bae57e38SJiri Olsa 	.tp_getattro	= (getattrofunc) pyrf_sample_event__getattro,
3953e9f45a7SArnaldo Carvalho de Melo };
3963e9f45a7SArnaldo Carvalho de Melo 
397ae938802SArnaldo Carvalho de Melo static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
398ae938802SArnaldo Carvalho de Melo 
399ae938802SArnaldo Carvalho de Melo static PyMemberDef pyrf_context_switch_event__members[] = {
400ae938802SArnaldo Carvalho de Melo 	sample_members
401ae938802SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
402ae938802SArnaldo Carvalho de Melo 	member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"),
403ae938802SArnaldo Carvalho de Melo 	member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"),
404ae938802SArnaldo Carvalho de Melo 	{ .name = NULL, },
405ae938802SArnaldo Carvalho de Melo };
406ae938802SArnaldo Carvalho de Melo 
407ae938802SArnaldo Carvalho de Melo static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
408ae938802SArnaldo Carvalho de Melo {
409ae938802SArnaldo Carvalho de Melo 	PyObject *ret;
410ae938802SArnaldo Carvalho de Melo 	char *s;
411ae938802SArnaldo Carvalho de Melo 
412ae938802SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
413ae938802SArnaldo Carvalho de Melo 		     pevent->event.context_switch.next_prev_pid,
414ae938802SArnaldo Carvalho de Melo 		     pevent->event.context_switch.next_prev_tid,
415ae938802SArnaldo Carvalho de Melo 		     !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
416ae938802SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
417ae938802SArnaldo Carvalho de Melo 	} else {
418ae938802SArnaldo Carvalho de Melo 		ret = PyString_FromString(s);
419ae938802SArnaldo Carvalho de Melo 		free(s);
420ae938802SArnaldo Carvalho de Melo 	}
421ae938802SArnaldo Carvalho de Melo 	return ret;
422ae938802SArnaldo Carvalho de Melo }
423ae938802SArnaldo Carvalho de Melo 
424ae938802SArnaldo Carvalho de Melo static PyTypeObject pyrf_context_switch_event__type = {
425ae938802SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
426ae938802SArnaldo Carvalho de Melo 	.tp_name	= "perf.context_switch_event",
427ae938802SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
428ae938802SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
429ae938802SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_context_switch_event__doc,
430ae938802SArnaldo Carvalho de Melo 	.tp_members	= pyrf_context_switch_event__members,
431ae938802SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_context_switch_event__repr,
432ae938802SArnaldo Carvalho de Melo };
433ae938802SArnaldo Carvalho de Melo 
434877108e4SArnaldo Carvalho de Melo static int pyrf_event__setup_types(void)
435877108e4SArnaldo Carvalho de Melo {
436877108e4SArnaldo Carvalho de Melo 	int err;
437877108e4SArnaldo Carvalho de Melo 	pyrf_mmap_event__type.tp_new =
438877108e4SArnaldo Carvalho de Melo 	pyrf_task_event__type.tp_new =
439877108e4SArnaldo Carvalho de Melo 	pyrf_comm_event__type.tp_new =
4403e9f45a7SArnaldo Carvalho de Melo 	pyrf_lost_event__type.tp_new =
4413e9f45a7SArnaldo Carvalho de Melo 	pyrf_read_event__type.tp_new =
4423e9f45a7SArnaldo Carvalho de Melo 	pyrf_sample_event__type.tp_new =
443ae938802SArnaldo Carvalho de Melo 	pyrf_context_switch_event__type.tp_new =
444877108e4SArnaldo Carvalho de Melo 	pyrf_throttle_event__type.tp_new = PyType_GenericNew;
445877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_mmap_event__type);
446877108e4SArnaldo Carvalho de Melo 	if (err < 0)
447877108e4SArnaldo Carvalho de Melo 		goto out;
4483e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_lost_event__type);
4493e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
4503e9f45a7SArnaldo Carvalho de Melo 		goto out;
451877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_task_event__type);
452877108e4SArnaldo Carvalho de Melo 	if (err < 0)
453877108e4SArnaldo Carvalho de Melo 		goto out;
454877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_comm_event__type);
455877108e4SArnaldo Carvalho de Melo 	if (err < 0)
456877108e4SArnaldo Carvalho de Melo 		goto out;
457877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_throttle_event__type);
458877108e4SArnaldo Carvalho de Melo 	if (err < 0)
459877108e4SArnaldo Carvalho de Melo 		goto out;
4603e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_read_event__type);
4613e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
4623e9f45a7SArnaldo Carvalho de Melo 		goto out;
4633e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_sample_event__type);
4643e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
4653e9f45a7SArnaldo Carvalho de Melo 		goto out;
466ae938802SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_context_switch_event__type);
467ae938802SArnaldo Carvalho de Melo 	if (err < 0)
468ae938802SArnaldo Carvalho de Melo 		goto out;
469877108e4SArnaldo Carvalho de Melo out:
470877108e4SArnaldo Carvalho de Melo 	return err;
471877108e4SArnaldo Carvalho de Melo }
472877108e4SArnaldo Carvalho de Melo 
473877108e4SArnaldo Carvalho de Melo static PyTypeObject *pyrf_event__type[] = {
474877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_MMAP]	 = &pyrf_mmap_event__type,
4753e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_LOST]	 = &pyrf_lost_event__type,
476877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_COMM]	 = &pyrf_comm_event__type,
477877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_EXIT]	 = &pyrf_task_event__type,
478877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_THROTTLE]	 = &pyrf_throttle_event__type,
479877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
480877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_FORK]	 = &pyrf_task_event__type,
4813e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_READ]	 = &pyrf_read_event__type,
4823e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_SAMPLE]	 = &pyrf_sample_event__type,
483ae938802SArnaldo Carvalho de Melo 	[PERF_RECORD_SWITCH]	 = &pyrf_context_switch_event__type,
484ae938802SArnaldo Carvalho de Melo 	[PERF_RECORD_SWITCH_CPU_WIDE]  = &pyrf_context_switch_event__type,
485877108e4SArnaldo Carvalho de Melo };
486877108e4SArnaldo Carvalho de Melo 
487877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_event__new(union perf_event *event)
488877108e4SArnaldo Carvalho de Melo {
489877108e4SArnaldo Carvalho de Melo 	struct pyrf_event *pevent;
490877108e4SArnaldo Carvalho de Melo 	PyTypeObject *ptype;
491877108e4SArnaldo Carvalho de Melo 
492ae938802SArnaldo Carvalho de Melo 	if ((event->header.type < PERF_RECORD_MMAP ||
493ae938802SArnaldo Carvalho de Melo 	     event->header.type > PERF_RECORD_SAMPLE) &&
494ae938802SArnaldo Carvalho de Melo 	    !(event->header.type == PERF_RECORD_SWITCH ||
495ae938802SArnaldo Carvalho de Melo 	      event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
496877108e4SArnaldo Carvalho de Melo 		return NULL;
497877108e4SArnaldo Carvalho de Melo 
498877108e4SArnaldo Carvalho de Melo 	ptype = pyrf_event__type[event->header.type];
499877108e4SArnaldo Carvalho de Melo 	pevent = PyObject_New(struct pyrf_event, ptype);
500877108e4SArnaldo Carvalho de Melo 	if (pevent != NULL)
501877108e4SArnaldo Carvalho de Melo 		memcpy(&pevent->event, event, event->header.size);
502877108e4SArnaldo Carvalho de Melo 	return (PyObject *)pevent;
503877108e4SArnaldo Carvalho de Melo }
504877108e4SArnaldo Carvalho de Melo 
505877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map {
506877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
507877108e4SArnaldo Carvalho de Melo 
508877108e4SArnaldo Carvalho de Melo 	struct cpu_map *cpus;
509877108e4SArnaldo Carvalho de Melo };
510877108e4SArnaldo Carvalho de Melo 
511877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
512877108e4SArnaldo Carvalho de Melo 			      PyObject *args, PyObject *kwargs)
513877108e4SArnaldo Carvalho de Melo {
51464348153SFrederic Weisbecker 	static char *kwlist[] = { "cpustr", NULL };
515877108e4SArnaldo Carvalho de Melo 	char *cpustr = NULL;
516877108e4SArnaldo Carvalho de Melo 
517877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
518877108e4SArnaldo Carvalho de Melo 					 kwlist, &cpustr))
519877108e4SArnaldo Carvalho de Melo 		return -1;
520877108e4SArnaldo Carvalho de Melo 
521877108e4SArnaldo Carvalho de Melo 	pcpus->cpus = cpu_map__new(cpustr);
522877108e4SArnaldo Carvalho de Melo 	if (pcpus->cpus == NULL)
523877108e4SArnaldo Carvalho de Melo 		return -1;
524877108e4SArnaldo Carvalho de Melo 	return 0;
525877108e4SArnaldo Carvalho de Melo }
526877108e4SArnaldo Carvalho de Melo 
527877108e4SArnaldo Carvalho de Melo static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
528877108e4SArnaldo Carvalho de Melo {
529f30a79b0SJiri Olsa 	cpu_map__put(pcpus->cpus);
530877108e4SArnaldo Carvalho de Melo 	pcpus->ob_type->tp_free((PyObject*)pcpus);
531877108e4SArnaldo Carvalho de Melo }
532877108e4SArnaldo Carvalho de Melo 
533877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
534877108e4SArnaldo Carvalho de Melo {
535877108e4SArnaldo Carvalho de Melo 	struct pyrf_cpu_map *pcpus = (void *)obj;
536877108e4SArnaldo Carvalho de Melo 
537877108e4SArnaldo Carvalho de Melo 	return pcpus->cpus->nr;
538877108e4SArnaldo Carvalho de Melo }
539877108e4SArnaldo Carvalho de Melo 
540877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
541877108e4SArnaldo Carvalho de Melo {
542877108e4SArnaldo Carvalho de Melo 	struct pyrf_cpu_map *pcpus = (void *)obj;
543877108e4SArnaldo Carvalho de Melo 
544877108e4SArnaldo Carvalho de Melo 	if (i >= pcpus->cpus->nr)
545877108e4SArnaldo Carvalho de Melo 		return NULL;
546877108e4SArnaldo Carvalho de Melo 
547877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", pcpus->cpus->map[i]);
548877108e4SArnaldo Carvalho de Melo }
549877108e4SArnaldo Carvalho de Melo 
550877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_cpu_map__sequence_methods = {
551877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_cpu_map__length,
552877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_cpu_map__item,
553877108e4SArnaldo Carvalho de Melo };
554877108e4SArnaldo Carvalho de Melo 
555877108e4SArnaldo Carvalho de Melo static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
556877108e4SArnaldo Carvalho de Melo 
557877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_cpu_map__type = {
558877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
559877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.cpu_map",
560877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_cpu_map),
561877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_cpu_map__delete,
562877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
563877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_cpu_map__doc,
564877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_cpu_map__sequence_methods,
565877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_cpu_map__init,
566877108e4SArnaldo Carvalho de Melo };
567877108e4SArnaldo Carvalho de Melo 
568877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__setup_types(void)
569877108e4SArnaldo Carvalho de Melo {
570877108e4SArnaldo Carvalho de Melo 	pyrf_cpu_map__type.tp_new = PyType_GenericNew;
571877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_cpu_map__type);
572877108e4SArnaldo Carvalho de Melo }
573877108e4SArnaldo Carvalho de Melo 
574877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map {
575877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
576877108e4SArnaldo Carvalho de Melo 
577877108e4SArnaldo Carvalho de Melo 	struct thread_map *threads;
578877108e4SArnaldo Carvalho de Melo };
579877108e4SArnaldo Carvalho de Melo 
580877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
581877108e4SArnaldo Carvalho de Melo 				 PyObject *args, PyObject *kwargs)
582877108e4SArnaldo Carvalho de Melo {
5830d37aa34SArnaldo Carvalho de Melo 	static char *kwlist[] = { "pid", "tid", "uid", NULL };
5840d37aa34SArnaldo Carvalho de Melo 	int pid = -1, tid = -1, uid = UINT_MAX;
585877108e4SArnaldo Carvalho de Melo 
5860d37aa34SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
5870d37aa34SArnaldo Carvalho de Melo 					 kwlist, &pid, &tid, &uid))
588877108e4SArnaldo Carvalho de Melo 		return -1;
589877108e4SArnaldo Carvalho de Melo 
5900d37aa34SArnaldo Carvalho de Melo 	pthreads->threads = thread_map__new(pid, tid, uid);
591877108e4SArnaldo Carvalho de Melo 	if (pthreads->threads == NULL)
592877108e4SArnaldo Carvalho de Melo 		return -1;
593877108e4SArnaldo Carvalho de Melo 	return 0;
594877108e4SArnaldo Carvalho de Melo }
595877108e4SArnaldo Carvalho de Melo 
596877108e4SArnaldo Carvalho de Melo static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
597877108e4SArnaldo Carvalho de Melo {
598186fbb74SJiri Olsa 	thread_map__put(pthreads->threads);
599877108e4SArnaldo Carvalho de Melo 	pthreads->ob_type->tp_free((PyObject*)pthreads);
600877108e4SArnaldo Carvalho de Melo }
601877108e4SArnaldo Carvalho de Melo 
602877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
603877108e4SArnaldo Carvalho de Melo {
604877108e4SArnaldo Carvalho de Melo 	struct pyrf_thread_map *pthreads = (void *)obj;
605877108e4SArnaldo Carvalho de Melo 
606877108e4SArnaldo Carvalho de Melo 	return pthreads->threads->nr;
607877108e4SArnaldo Carvalho de Melo }
608877108e4SArnaldo Carvalho de Melo 
609877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
610877108e4SArnaldo Carvalho de Melo {
611877108e4SArnaldo Carvalho de Melo 	struct pyrf_thread_map *pthreads = (void *)obj;
612877108e4SArnaldo Carvalho de Melo 
613877108e4SArnaldo Carvalho de Melo 	if (i >= pthreads->threads->nr)
614877108e4SArnaldo Carvalho de Melo 		return NULL;
615877108e4SArnaldo Carvalho de Melo 
616877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", pthreads->threads->map[i]);
617877108e4SArnaldo Carvalho de Melo }
618877108e4SArnaldo Carvalho de Melo 
619877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_thread_map__sequence_methods = {
620877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_thread_map__length,
621877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_thread_map__item,
622877108e4SArnaldo Carvalho de Melo };
623877108e4SArnaldo Carvalho de Melo 
624877108e4SArnaldo Carvalho de Melo static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
625877108e4SArnaldo Carvalho de Melo 
626877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_thread_map__type = {
627877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
628877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.thread_map",
629877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_thread_map),
630877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_thread_map__delete,
631877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
632877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_thread_map__doc,
633877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_thread_map__sequence_methods,
634877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_thread_map__init,
635877108e4SArnaldo Carvalho de Melo };
636877108e4SArnaldo Carvalho de Melo 
637877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__setup_types(void)
638877108e4SArnaldo Carvalho de Melo {
639877108e4SArnaldo Carvalho de Melo 	pyrf_thread_map__type.tp_new = PyType_GenericNew;
640877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_thread_map__type);
641877108e4SArnaldo Carvalho de Melo }
642877108e4SArnaldo Carvalho de Melo 
643877108e4SArnaldo Carvalho de Melo struct pyrf_evsel {
644877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
645877108e4SArnaldo Carvalho de Melo 
646877108e4SArnaldo Carvalho de Melo 	struct perf_evsel evsel;
647877108e4SArnaldo Carvalho de Melo };
648877108e4SArnaldo Carvalho de Melo 
649877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
650877108e4SArnaldo Carvalho de Melo 			    PyObject *args, PyObject *kwargs)
651877108e4SArnaldo Carvalho de Melo {
652877108e4SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
653877108e4SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
654877108e4SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
655877108e4SArnaldo Carvalho de Melo 		.sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
656877108e4SArnaldo Carvalho de Melo 	};
657877108e4SArnaldo Carvalho de Melo 	static char *kwlist[] = {
658877108e4SArnaldo Carvalho de Melo 		"type",
659877108e4SArnaldo Carvalho de Melo 		"config",
660877108e4SArnaldo Carvalho de Melo 		"sample_freq",
661877108e4SArnaldo Carvalho de Melo 		"sample_period",
662877108e4SArnaldo Carvalho de Melo 		"sample_type",
663877108e4SArnaldo Carvalho de Melo 		"read_format",
664877108e4SArnaldo Carvalho de Melo 		"disabled",
665877108e4SArnaldo Carvalho de Melo 		"inherit",
666877108e4SArnaldo Carvalho de Melo 		"pinned",
667877108e4SArnaldo Carvalho de Melo 		"exclusive",
668877108e4SArnaldo Carvalho de Melo 		"exclude_user",
669877108e4SArnaldo Carvalho de Melo 		"exclude_kernel",
670877108e4SArnaldo Carvalho de Melo 		"exclude_hv",
671877108e4SArnaldo Carvalho de Melo 		"exclude_idle",
672877108e4SArnaldo Carvalho de Melo 		"mmap",
673ae938802SArnaldo Carvalho de Melo 		"context_switch",
674877108e4SArnaldo Carvalho de Melo 		"comm",
675877108e4SArnaldo Carvalho de Melo 		"freq",
676877108e4SArnaldo Carvalho de Melo 		"inherit_stat",
677877108e4SArnaldo Carvalho de Melo 		"enable_on_exec",
678877108e4SArnaldo Carvalho de Melo 		"task",
679877108e4SArnaldo Carvalho de Melo 		"watermark",
680877108e4SArnaldo Carvalho de Melo 		"precise_ip",
681877108e4SArnaldo Carvalho de Melo 		"mmap_data",
682877108e4SArnaldo Carvalho de Melo 		"sample_id_all",
683877108e4SArnaldo Carvalho de Melo 		"wakeup_events",
684877108e4SArnaldo Carvalho de Melo 		"bp_type",
685877108e4SArnaldo Carvalho de Melo 		"bp_addr",
68664348153SFrederic Weisbecker 		"bp_len",
68764348153SFrederic Weisbecker 		 NULL
68864348153SFrederic Weisbecker 	};
689877108e4SArnaldo Carvalho de Melo 	u64 sample_period = 0;
690877108e4SArnaldo Carvalho de Melo 	u32 disabled = 0,
691877108e4SArnaldo Carvalho de Melo 	    inherit = 0,
692877108e4SArnaldo Carvalho de Melo 	    pinned = 0,
693877108e4SArnaldo Carvalho de Melo 	    exclusive = 0,
694877108e4SArnaldo Carvalho de Melo 	    exclude_user = 0,
695877108e4SArnaldo Carvalho de Melo 	    exclude_kernel = 0,
696877108e4SArnaldo Carvalho de Melo 	    exclude_hv = 0,
697877108e4SArnaldo Carvalho de Melo 	    exclude_idle = 0,
698877108e4SArnaldo Carvalho de Melo 	    mmap = 0,
699ae938802SArnaldo Carvalho de Melo 	    context_switch = 0,
700877108e4SArnaldo Carvalho de Melo 	    comm = 0,
701877108e4SArnaldo Carvalho de Melo 	    freq = 1,
702877108e4SArnaldo Carvalho de Melo 	    inherit_stat = 0,
703877108e4SArnaldo Carvalho de Melo 	    enable_on_exec = 0,
704877108e4SArnaldo Carvalho de Melo 	    task = 0,
705877108e4SArnaldo Carvalho de Melo 	    watermark = 0,
706877108e4SArnaldo Carvalho de Melo 	    precise_ip = 0,
707877108e4SArnaldo Carvalho de Melo 	    mmap_data = 0,
708877108e4SArnaldo Carvalho de Melo 	    sample_id_all = 1;
709877108e4SArnaldo Carvalho de Melo 	int idx = 0;
710877108e4SArnaldo Carvalho de Melo 
711877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
712ae938802SArnaldo Carvalho de Melo 					 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
713877108e4SArnaldo Carvalho de Melo 					 &attr.type, &attr.config, &attr.sample_freq,
714877108e4SArnaldo Carvalho de Melo 					 &sample_period, &attr.sample_type,
715877108e4SArnaldo Carvalho de Melo 					 &attr.read_format, &disabled, &inherit,
716877108e4SArnaldo Carvalho de Melo 					 &pinned, &exclusive, &exclude_user,
717877108e4SArnaldo Carvalho de Melo 					 &exclude_kernel, &exclude_hv, &exclude_idle,
718ae938802SArnaldo Carvalho de Melo 					 &mmap, &context_switch, &comm, &freq, &inherit_stat,
719877108e4SArnaldo Carvalho de Melo 					 &enable_on_exec, &task, &watermark,
720877108e4SArnaldo Carvalho de Melo 					 &precise_ip, &mmap_data, &sample_id_all,
721877108e4SArnaldo Carvalho de Melo 					 &attr.wakeup_events, &attr.bp_type,
722877108e4SArnaldo Carvalho de Melo 					 &attr.bp_addr, &attr.bp_len, &idx))
723877108e4SArnaldo Carvalho de Melo 		return -1;
724877108e4SArnaldo Carvalho de Melo 
725877108e4SArnaldo Carvalho de Melo 	/* union... */
726877108e4SArnaldo Carvalho de Melo 	if (sample_period != 0) {
727877108e4SArnaldo Carvalho de Melo 		if (attr.sample_freq != 0)
728877108e4SArnaldo Carvalho de Melo 			return -1; /* FIXME: throw right exception */
729877108e4SArnaldo Carvalho de Melo 		attr.sample_period = sample_period;
730877108e4SArnaldo Carvalho de Melo 	}
731877108e4SArnaldo Carvalho de Melo 
732877108e4SArnaldo Carvalho de Melo 	/* Bitfields */
733877108e4SArnaldo Carvalho de Melo 	attr.disabled	    = disabled;
734877108e4SArnaldo Carvalho de Melo 	attr.inherit	    = inherit;
735877108e4SArnaldo Carvalho de Melo 	attr.pinned	    = pinned;
736877108e4SArnaldo Carvalho de Melo 	attr.exclusive	    = exclusive;
737877108e4SArnaldo Carvalho de Melo 	attr.exclude_user   = exclude_user;
738877108e4SArnaldo Carvalho de Melo 	attr.exclude_kernel = exclude_kernel;
739877108e4SArnaldo Carvalho de Melo 	attr.exclude_hv	    = exclude_hv;
740877108e4SArnaldo Carvalho de Melo 	attr.exclude_idle   = exclude_idle;
741877108e4SArnaldo Carvalho de Melo 	attr.mmap	    = mmap;
742ae938802SArnaldo Carvalho de Melo 	attr.context_switch = context_switch;
743877108e4SArnaldo Carvalho de Melo 	attr.comm	    = comm;
744877108e4SArnaldo Carvalho de Melo 	attr.freq	    = freq;
745877108e4SArnaldo Carvalho de Melo 	attr.inherit_stat   = inherit_stat;
746877108e4SArnaldo Carvalho de Melo 	attr.enable_on_exec = enable_on_exec;
747877108e4SArnaldo Carvalho de Melo 	attr.task	    = task;
748877108e4SArnaldo Carvalho de Melo 	attr.watermark	    = watermark;
749877108e4SArnaldo Carvalho de Melo 	attr.precise_ip	    = precise_ip;
750877108e4SArnaldo Carvalho de Melo 	attr.mmap_data	    = mmap_data;
751877108e4SArnaldo Carvalho de Melo 	attr.sample_id_all  = sample_id_all;
752ad4e3c04SJiri Olsa 	attr.size	    = sizeof(attr);
753877108e4SArnaldo Carvalho de Melo 
754877108e4SArnaldo Carvalho de Melo 	perf_evsel__init(&pevsel->evsel, &attr, idx);
755877108e4SArnaldo Carvalho de Melo 	return 0;
756877108e4SArnaldo Carvalho de Melo }
757877108e4SArnaldo Carvalho de Melo 
758877108e4SArnaldo Carvalho de Melo static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
759877108e4SArnaldo Carvalho de Melo {
760877108e4SArnaldo Carvalho de Melo 	perf_evsel__exit(&pevsel->evsel);
761877108e4SArnaldo Carvalho de Melo 	pevsel->ob_type->tp_free((PyObject*)pevsel);
762877108e4SArnaldo Carvalho de Melo }
763877108e4SArnaldo Carvalho de Melo 
764877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
765877108e4SArnaldo Carvalho de Melo 				  PyObject *args, PyObject *kwargs)
766877108e4SArnaldo Carvalho de Melo {
767877108e4SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = &pevsel->evsel;
768877108e4SArnaldo Carvalho de Melo 	struct cpu_map *cpus = NULL;
769877108e4SArnaldo Carvalho de Melo 	struct thread_map *threads = NULL;
770877108e4SArnaldo Carvalho de Melo 	PyObject *pcpus = NULL, *pthreads = NULL;
7715d2cd909SArnaldo Carvalho de Melo 	int group = 0, inherit = 0;
77264348153SFrederic Weisbecker 	static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
773877108e4SArnaldo Carvalho de Melo 
774877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
7755d2cd909SArnaldo Carvalho de Melo 					 &pcpus, &pthreads, &group, &inherit))
776877108e4SArnaldo Carvalho de Melo 		return NULL;
777877108e4SArnaldo Carvalho de Melo 
778877108e4SArnaldo Carvalho de Melo 	if (pthreads != NULL)
779877108e4SArnaldo Carvalho de Melo 		threads = ((struct pyrf_thread_map *)pthreads)->threads;
780877108e4SArnaldo Carvalho de Melo 
781877108e4SArnaldo Carvalho de Melo 	if (pcpus != NULL)
782877108e4SArnaldo Carvalho de Melo 		cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
783877108e4SArnaldo Carvalho de Melo 
7845d2cd909SArnaldo Carvalho de Melo 	evsel->attr.inherit = inherit;
785727ab04eSArnaldo Carvalho de Melo 	/*
786727ab04eSArnaldo Carvalho de Melo 	 * This will group just the fds for this single evsel, to group
787727ab04eSArnaldo Carvalho de Melo 	 * multiple events, use evlist.open().
788727ab04eSArnaldo Carvalho de Melo 	 */
7896a4bb04cSJiri Olsa 	if (perf_evsel__open(evsel, cpus, threads) < 0) {
790877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
791877108e4SArnaldo Carvalho de Melo 		return NULL;
792877108e4SArnaldo Carvalho de Melo 	}
793877108e4SArnaldo Carvalho de Melo 
794877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
795877108e4SArnaldo Carvalho de Melo 	return Py_None;
796877108e4SArnaldo Carvalho de Melo }
797877108e4SArnaldo Carvalho de Melo 
798877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evsel__methods[] = {
799877108e4SArnaldo Carvalho de Melo 	{
800877108e4SArnaldo Carvalho de Melo 		.ml_name  = "open",
801877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evsel__open,
802877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
803877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("open the event selector file descriptor table.")
804877108e4SArnaldo Carvalho de Melo 	},
805f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
806877108e4SArnaldo Carvalho de Melo };
807877108e4SArnaldo Carvalho de Melo 
808877108e4SArnaldo Carvalho de Melo static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
809877108e4SArnaldo Carvalho de Melo 
810877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evsel__type = {
811877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
812877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.evsel",
813877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_evsel),
814877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_evsel__delete,
815877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
816877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_evsel__doc,
817877108e4SArnaldo Carvalho de Melo 	.tp_methods	= pyrf_evsel__methods,
818877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_evsel__init,
819877108e4SArnaldo Carvalho de Melo };
820877108e4SArnaldo Carvalho de Melo 
821877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__setup_types(void)
822877108e4SArnaldo Carvalho de Melo {
823877108e4SArnaldo Carvalho de Melo 	pyrf_evsel__type.tp_new = PyType_GenericNew;
824877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_evsel__type);
825877108e4SArnaldo Carvalho de Melo }
826877108e4SArnaldo Carvalho de Melo 
827877108e4SArnaldo Carvalho de Melo struct pyrf_evlist {
828877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
829877108e4SArnaldo Carvalho de Melo 
830877108e4SArnaldo Carvalho de Melo 	struct perf_evlist evlist;
831877108e4SArnaldo Carvalho de Melo };
832877108e4SArnaldo Carvalho de Melo 
833877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
8341d037ca1SIrina Tirdea 			     PyObject *args, PyObject *kwargs __maybe_unused)
835877108e4SArnaldo Carvalho de Melo {
8367e2ed097SArnaldo Carvalho de Melo 	PyObject *pcpus = NULL, *pthreads = NULL;
8377e2ed097SArnaldo Carvalho de Melo 	struct cpu_map *cpus;
8387e2ed097SArnaldo Carvalho de Melo 	struct thread_map *threads;
8397e2ed097SArnaldo Carvalho de Melo 
8407e2ed097SArnaldo Carvalho de Melo 	if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
8417e2ed097SArnaldo Carvalho de Melo 		return -1;
8427e2ed097SArnaldo Carvalho de Melo 
8437e2ed097SArnaldo Carvalho de Melo 	threads = ((struct pyrf_thread_map *)pthreads)->threads;
8447e2ed097SArnaldo Carvalho de Melo 	cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
8457e2ed097SArnaldo Carvalho de Melo 	perf_evlist__init(&pevlist->evlist, cpus, threads);
846877108e4SArnaldo Carvalho de Melo 	return 0;
847877108e4SArnaldo Carvalho de Melo }
848877108e4SArnaldo Carvalho de Melo 
849877108e4SArnaldo Carvalho de Melo static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
850877108e4SArnaldo Carvalho de Melo {
851877108e4SArnaldo Carvalho de Melo 	perf_evlist__exit(&pevlist->evlist);
852877108e4SArnaldo Carvalho de Melo 	pevlist->ob_type->tp_free((PyObject*)pevlist);
853877108e4SArnaldo Carvalho de Melo }
854877108e4SArnaldo Carvalho de Melo 
855877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
856877108e4SArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
857877108e4SArnaldo Carvalho de Melo {
858877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
85964348153SFrederic Weisbecker 	static char *kwlist[] = { "pages", "overwrite", NULL };
860877108e4SArnaldo Carvalho de Melo 	int pages = 128, overwrite = false;
861877108e4SArnaldo Carvalho de Melo 
8627e2ed097SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
8637e2ed097SArnaldo Carvalho de Melo 					 &pages, &overwrite))
864877108e4SArnaldo Carvalho de Melo 		return NULL;
865877108e4SArnaldo Carvalho de Melo 
8667e2ed097SArnaldo Carvalho de Melo 	if (perf_evlist__mmap(evlist, pages, overwrite) < 0) {
867877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
868877108e4SArnaldo Carvalho de Melo 		return NULL;
869877108e4SArnaldo Carvalho de Melo 	}
870877108e4SArnaldo Carvalho de Melo 
871877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
872877108e4SArnaldo Carvalho de Melo 	return Py_None;
873877108e4SArnaldo Carvalho de Melo }
874877108e4SArnaldo Carvalho de Melo 
875877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
876877108e4SArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
877877108e4SArnaldo Carvalho de Melo {
878877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
87964348153SFrederic Weisbecker 	static char *kwlist[] = { "timeout", NULL };
880877108e4SArnaldo Carvalho de Melo 	int timeout = -1, n;
881877108e4SArnaldo Carvalho de Melo 
882877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
883877108e4SArnaldo Carvalho de Melo 		return NULL;
884877108e4SArnaldo Carvalho de Melo 
885f66a889dSArnaldo Carvalho de Melo 	n = perf_evlist__poll(evlist, timeout);
886877108e4SArnaldo Carvalho de Melo 	if (n < 0) {
887877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
888877108e4SArnaldo Carvalho de Melo 		return NULL;
889877108e4SArnaldo Carvalho de Melo 	}
890877108e4SArnaldo Carvalho de Melo 
891877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", n);
892877108e4SArnaldo Carvalho de Melo }
893877108e4SArnaldo Carvalho de Melo 
894877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
8951d037ca1SIrina Tirdea 					 PyObject *args __maybe_unused,
8961d037ca1SIrina Tirdea 					 PyObject *kwargs __maybe_unused)
897877108e4SArnaldo Carvalho de Melo {
898877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
899877108e4SArnaldo Carvalho de Melo         PyObject *list = PyList_New(0);
900877108e4SArnaldo Carvalho de Melo 	int i;
901877108e4SArnaldo Carvalho de Melo 
9021b85337dSArnaldo Carvalho de Melo 	for (i = 0; i < evlist->pollfd.nr; ++i) {
903877108e4SArnaldo Carvalho de Melo 		PyObject *file;
9041b85337dSArnaldo Carvalho de Melo 		FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
905877108e4SArnaldo Carvalho de Melo 
906877108e4SArnaldo Carvalho de Melo 		if (fp == NULL)
907877108e4SArnaldo Carvalho de Melo 			goto free_list;
908877108e4SArnaldo Carvalho de Melo 
909877108e4SArnaldo Carvalho de Melo 		file = PyFile_FromFile(fp, "perf", "r", NULL);
910877108e4SArnaldo Carvalho de Melo 		if (file == NULL)
911877108e4SArnaldo Carvalho de Melo 			goto free_list;
912877108e4SArnaldo Carvalho de Melo 
913877108e4SArnaldo Carvalho de Melo 		if (PyList_Append(list, file) != 0) {
914877108e4SArnaldo Carvalho de Melo 			Py_DECREF(file);
915877108e4SArnaldo Carvalho de Melo 			goto free_list;
916877108e4SArnaldo Carvalho de Melo 		}
917877108e4SArnaldo Carvalho de Melo 
918877108e4SArnaldo Carvalho de Melo 		Py_DECREF(file);
919877108e4SArnaldo Carvalho de Melo 	}
920877108e4SArnaldo Carvalho de Melo 
921877108e4SArnaldo Carvalho de Melo 	return list;
922877108e4SArnaldo Carvalho de Melo free_list:
923877108e4SArnaldo Carvalho de Melo 	return PyErr_NoMemory();
924877108e4SArnaldo Carvalho de Melo }
925877108e4SArnaldo Carvalho de Melo 
926877108e4SArnaldo Carvalho de Melo 
927877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
9281d037ca1SIrina Tirdea 				  PyObject *args,
9291d037ca1SIrina Tirdea 				  PyObject *kwargs __maybe_unused)
930877108e4SArnaldo Carvalho de Melo {
931877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
932877108e4SArnaldo Carvalho de Melo 	PyObject *pevsel;
933877108e4SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
934877108e4SArnaldo Carvalho de Melo 
935877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTuple(args, "O", &pevsel))
936877108e4SArnaldo Carvalho de Melo 		return NULL;
937877108e4SArnaldo Carvalho de Melo 
938877108e4SArnaldo Carvalho de Melo 	Py_INCREF(pevsel);
939877108e4SArnaldo Carvalho de Melo 	evsel = &((struct pyrf_evsel *)pevsel)->evsel;
940877108e4SArnaldo Carvalho de Melo 	evsel->idx = evlist->nr_entries;
941877108e4SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
942877108e4SArnaldo Carvalho de Melo 
943877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", evlist->nr_entries);
944877108e4SArnaldo Carvalho de Melo }
945877108e4SArnaldo Carvalho de Melo 
946877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
947877108e4SArnaldo Carvalho de Melo 					  PyObject *args, PyObject *kwargs)
948877108e4SArnaldo Carvalho de Melo {
949877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
950877108e4SArnaldo Carvalho de Melo 	union perf_event *event;
951877108e4SArnaldo Carvalho de Melo 	int sample_id_all = 1, cpu;
95264348153SFrederic Weisbecker 	static char *kwlist[] = { "cpu", "sample_id_all", NULL };
9535538becaSFrederic Weisbecker 	int err;
954877108e4SArnaldo Carvalho de Melo 
955877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
956877108e4SArnaldo Carvalho de Melo 					 &cpu, &sample_id_all))
957877108e4SArnaldo Carvalho de Melo 		return NULL;
958877108e4SArnaldo Carvalho de Melo 
959aece948fSArnaldo Carvalho de Melo 	event = perf_evlist__mmap_read(evlist, cpu);
960877108e4SArnaldo Carvalho de Melo 	if (event != NULL) {
961877108e4SArnaldo Carvalho de Melo 		PyObject *pyevent = pyrf_event__new(event);
962877108e4SArnaldo Carvalho de Melo 		struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
963377f698dSJiri Olsa 		struct perf_evsel *evsel;
964877108e4SArnaldo Carvalho de Melo 
965877108e4SArnaldo Carvalho de Melo 		if (pyevent == NULL)
966877108e4SArnaldo Carvalho de Melo 			return PyErr_NoMemory();
967877108e4SArnaldo Carvalho de Melo 
968377f698dSJiri Olsa 		evsel = perf_evlist__event2evsel(evlist, event);
969377f698dSJiri Olsa 		if (!evsel)
970377f698dSJiri Olsa 			return Py_None;
971377f698dSJiri Olsa 
972377f698dSJiri Olsa 		pevent->evsel = evsel;
973377f698dSJiri Olsa 
974377f698dSJiri Olsa 		err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
975e8968e65SJiri Olsa 
976e8968e65SJiri Olsa 		/* Consume the even only after we parsed it out. */
977e8968e65SJiri Olsa 		perf_evlist__mmap_consume(evlist, cpu);
978e8968e65SJiri Olsa 
9795c6970afSArnaldo Carvalho de Melo 		if (err)
9805c6970afSArnaldo Carvalho de Melo 			return PyErr_Format(PyExc_OSError,
9815c6970afSArnaldo Carvalho de Melo 					    "perf: can't parse sample, err=%d", err);
9825538becaSFrederic Weisbecker 		return pyevent;
9835538becaSFrederic Weisbecker 	}
9845c6970afSArnaldo Carvalho de Melo 
985877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
986877108e4SArnaldo Carvalho de Melo 	return Py_None;
987877108e4SArnaldo Carvalho de Melo }
988877108e4SArnaldo Carvalho de Melo 
989727ab04eSArnaldo Carvalho de Melo static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
990727ab04eSArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
991727ab04eSArnaldo Carvalho de Melo {
992727ab04eSArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
993727ab04eSArnaldo Carvalho de Melo 	int group = 0;
994727ab04eSArnaldo Carvalho de Melo 	static char *kwlist[] = { "group", NULL };
995727ab04eSArnaldo Carvalho de Melo 
996727ab04eSArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
997727ab04eSArnaldo Carvalho de Melo 		return NULL;
998727ab04eSArnaldo Carvalho de Melo 
9996a4bb04cSJiri Olsa 	if (group)
100063dab225SArnaldo Carvalho de Melo 		perf_evlist__set_leader(evlist);
10016a4bb04cSJiri Olsa 
10026a4bb04cSJiri Olsa 	if (perf_evlist__open(evlist) < 0) {
1003727ab04eSArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
1004727ab04eSArnaldo Carvalho de Melo 		return NULL;
1005727ab04eSArnaldo Carvalho de Melo 	}
1006727ab04eSArnaldo Carvalho de Melo 
1007727ab04eSArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
1008727ab04eSArnaldo Carvalho de Melo 	return Py_None;
1009727ab04eSArnaldo Carvalho de Melo }
1010727ab04eSArnaldo Carvalho de Melo 
1011877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evlist__methods[] = {
1012877108e4SArnaldo Carvalho de Melo 	{
1013877108e4SArnaldo Carvalho de Melo 		.ml_name  = "mmap",
1014877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__mmap,
1015877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1016877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("mmap the file descriptor table.")
1017877108e4SArnaldo Carvalho de Melo 	},
1018877108e4SArnaldo Carvalho de Melo 	{
1019727ab04eSArnaldo Carvalho de Melo 		.ml_name  = "open",
1020727ab04eSArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__open,
1021727ab04eSArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1022727ab04eSArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("open the file descriptors.")
1023727ab04eSArnaldo Carvalho de Melo 	},
1024727ab04eSArnaldo Carvalho de Melo 	{
1025877108e4SArnaldo Carvalho de Melo 		.ml_name  = "poll",
1026877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__poll,
1027877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1028877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("poll the file descriptor table.")
1029877108e4SArnaldo Carvalho de Melo 	},
1030877108e4SArnaldo Carvalho de Melo 	{
1031877108e4SArnaldo Carvalho de Melo 		.ml_name  = "get_pollfd",
1032877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__get_pollfd,
1033877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1034877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("get the poll file descriptor table.")
1035877108e4SArnaldo Carvalho de Melo 	},
1036877108e4SArnaldo Carvalho de Melo 	{
1037877108e4SArnaldo Carvalho de Melo 		.ml_name  = "add",
1038877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__add,
1039877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1040877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("adds an event selector to the list.")
1041877108e4SArnaldo Carvalho de Melo 	},
1042877108e4SArnaldo Carvalho de Melo 	{
1043877108e4SArnaldo Carvalho de Melo 		.ml_name  = "read_on_cpu",
1044877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__read_on_cpu,
1045877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1046877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("reads an event.")
1047877108e4SArnaldo Carvalho de Melo 	},
1048f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
1049877108e4SArnaldo Carvalho de Melo };
1050877108e4SArnaldo Carvalho de Melo 
1051877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1052877108e4SArnaldo Carvalho de Melo {
1053877108e4SArnaldo Carvalho de Melo 	struct pyrf_evlist *pevlist = (void *)obj;
1054877108e4SArnaldo Carvalho de Melo 
1055877108e4SArnaldo Carvalho de Melo 	return pevlist->evlist.nr_entries;
1056877108e4SArnaldo Carvalho de Melo }
1057877108e4SArnaldo Carvalho de Melo 
1058877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1059877108e4SArnaldo Carvalho de Melo {
1060877108e4SArnaldo Carvalho de Melo 	struct pyrf_evlist *pevlist = (void *)obj;
1061877108e4SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
1062877108e4SArnaldo Carvalho de Melo 
1063877108e4SArnaldo Carvalho de Melo 	if (i >= pevlist->evlist.nr_entries)
1064877108e4SArnaldo Carvalho de Melo 		return NULL;
1065877108e4SArnaldo Carvalho de Melo 
1066e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(&pevlist->evlist, pos) {
1067877108e4SArnaldo Carvalho de Melo 		if (i-- == 0)
1068877108e4SArnaldo Carvalho de Melo 			break;
10690050f7aaSArnaldo Carvalho de Melo 	}
1070877108e4SArnaldo Carvalho de Melo 
1071877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1072877108e4SArnaldo Carvalho de Melo }
1073877108e4SArnaldo Carvalho de Melo 
1074877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_evlist__sequence_methods = {
1075877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_evlist__length,
1076877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_evlist__item,
1077877108e4SArnaldo Carvalho de Melo };
1078877108e4SArnaldo Carvalho de Melo 
1079877108e4SArnaldo Carvalho de Melo static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1080877108e4SArnaldo Carvalho de Melo 
1081877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evlist__type = {
1082877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
1083877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.evlist",
1084877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_evlist),
1085877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_evlist__delete,
1086877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1087877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_evlist__sequence_methods,
1088877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_evlist__doc,
1089877108e4SArnaldo Carvalho de Melo 	.tp_methods	= pyrf_evlist__methods,
1090877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_evlist__init,
1091877108e4SArnaldo Carvalho de Melo };
1092877108e4SArnaldo Carvalho de Melo 
1093877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__setup_types(void)
1094877108e4SArnaldo Carvalho de Melo {
1095877108e4SArnaldo Carvalho de Melo 	pyrf_evlist__type.tp_new = PyType_GenericNew;
1096877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_evlist__type);
1097877108e4SArnaldo Carvalho de Melo }
1098877108e4SArnaldo Carvalho de Melo 
10995865fe36SArnaldo Carvalho de Melo #define PERF_CONST(name) { #name, PERF_##name }
11005865fe36SArnaldo Carvalho de Melo 
1101877108e4SArnaldo Carvalho de Melo static struct {
1102877108e4SArnaldo Carvalho de Melo 	const char *name;
1103877108e4SArnaldo Carvalho de Melo 	int	    value;
1104877108e4SArnaldo Carvalho de Melo } perf__constants[] = {
11055865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_HARDWARE),
11065865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_SOFTWARE),
11075865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_TRACEPOINT),
11085865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_HW_CACHE),
11095865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_RAW),
11105865fe36SArnaldo Carvalho de Melo 	PERF_CONST(TYPE_BREAKPOINT),
1111877108e4SArnaldo Carvalho de Melo 
11125865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CPU_CYCLES),
11135865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_INSTRUCTIONS),
11145865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_REFERENCES),
11155865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_MISSES),
11165865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
11175865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_BRANCH_MISSES),
11185865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_BUS_CYCLES),
11195865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_L1D),
11205865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_L1I),
11215865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_LL),
11225865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_DTLB),
11235865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_ITLB),
11245865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_BPU),
11255865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_OP_READ),
11265865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
11275865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
11285865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
11295865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1130877108e4SArnaldo Carvalho de Melo 
11315865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
11325865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1133129c04cbSIngo Molnar 
11345865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_CPU_CLOCK),
11355865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_TASK_CLOCK),
11365865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_PAGE_FAULTS),
11375865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
11385865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
11395865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
11405865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
11415865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
11425865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_EMULATION_FAULTS),
11435865fe36SArnaldo Carvalho de Melo 	PERF_CONST(COUNT_SW_DUMMY),
1144877108e4SArnaldo Carvalho de Melo 
11455865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_IP),
11465865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_TID),
11475865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_TIME),
11485865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_ADDR),
11495865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_READ),
11505865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_CALLCHAIN),
11515865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_ID),
11525865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_CPU),
11535865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_PERIOD),
11545865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_STREAM_ID),
11555865fe36SArnaldo Carvalho de Melo 	PERF_CONST(SAMPLE_RAW),
1156877108e4SArnaldo Carvalho de Melo 
11575865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
11585865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
11595865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_ID),
11605865fe36SArnaldo Carvalho de Melo 	PERF_CONST(FORMAT_GROUP),
1161877108e4SArnaldo Carvalho de Melo 
11625865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_MMAP),
11635865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_LOST),
11645865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_COMM),
11655865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_EXIT),
11665865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_THROTTLE),
11675865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_UNTHROTTLE),
11685865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_FORK),
11695865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_READ),
11705865fe36SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_SAMPLE),
117184576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_MMAP2),
117284576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_AUX),
117384576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_ITRACE_START),
117484576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_LOST_SAMPLES),
117584576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_SWITCH),
117684576da2SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1177ae938802SArnaldo Carvalho de Melo 
1178ae938802SArnaldo Carvalho de Melo 	PERF_CONST(RECORD_MISC_SWITCH_OUT),
1179f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
1180877108e4SArnaldo Carvalho de Melo };
1181877108e4SArnaldo Carvalho de Melo 
11821075fbb2SJiri Olsa static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
11831075fbb2SJiri Olsa 				  PyObject *args, PyObject *kwargs)
11841075fbb2SJiri Olsa {
11851075fbb2SJiri Olsa 	struct event_format *tp_format;
11861075fbb2SJiri Olsa 	static char *kwlist[] = { "sys", "name", NULL };
11871075fbb2SJiri Olsa 	char *sys  = NULL;
11881075fbb2SJiri Olsa 	char *name = NULL;
11891075fbb2SJiri Olsa 
11901075fbb2SJiri Olsa 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
11911075fbb2SJiri Olsa 					 &sys, &name))
11921075fbb2SJiri Olsa 		return NULL;
11931075fbb2SJiri Olsa 
11941075fbb2SJiri Olsa 	tp_format = trace_event__tp_format(sys, name);
11951075fbb2SJiri Olsa 	if (IS_ERR(tp_format))
11961075fbb2SJiri Olsa 		return PyInt_FromLong(-1);
11971075fbb2SJiri Olsa 
11981075fbb2SJiri Olsa 	return PyInt_FromLong(tp_format->id);
11991075fbb2SJiri Olsa }
12001075fbb2SJiri Olsa 
1201877108e4SArnaldo Carvalho de Melo static PyMethodDef perf__methods[] = {
12021075fbb2SJiri Olsa 	{
12031075fbb2SJiri Olsa 		.ml_name  = "tracepoint",
12041075fbb2SJiri Olsa 		.ml_meth  = (PyCFunction) pyrf__tracepoint,
12051075fbb2SJiri Olsa 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
12061075fbb2SJiri Olsa 		.ml_doc	  = PyDoc_STR("Get tracepoint config.")
12071075fbb2SJiri Olsa 	},
1208f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
1209877108e4SArnaldo Carvalho de Melo };
1210877108e4SArnaldo Carvalho de Melo 
1211877108e4SArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void)
1212877108e4SArnaldo Carvalho de Melo {
1213877108e4SArnaldo Carvalho de Melo 	PyObject *obj;
1214877108e4SArnaldo Carvalho de Melo 	int i;
1215877108e4SArnaldo Carvalho de Melo 	PyObject *dict, *module = Py_InitModule("perf", perf__methods);
1216877108e4SArnaldo Carvalho de Melo 
1217877108e4SArnaldo Carvalho de Melo 	if (module == NULL ||
1218877108e4SArnaldo Carvalho de Melo 	    pyrf_event__setup_types() < 0 ||
1219877108e4SArnaldo Carvalho de Melo 	    pyrf_evlist__setup_types() < 0 ||
1220877108e4SArnaldo Carvalho de Melo 	    pyrf_evsel__setup_types() < 0 ||
1221877108e4SArnaldo Carvalho de Melo 	    pyrf_thread_map__setup_types() < 0 ||
1222877108e4SArnaldo Carvalho de Melo 	    pyrf_cpu_map__setup_types() < 0)
1223877108e4SArnaldo Carvalho de Melo 		return;
1224877108e4SArnaldo Carvalho de Melo 
1225918512b4SJiri Olsa 	/* The page_size is placed in util object. */
12260da2e9c2SArnaldo Carvalho de Melo 	page_size = sysconf(_SC_PAGE_SIZE);
12270da2e9c2SArnaldo Carvalho de Melo 
1228877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_evlist__type);
1229877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1230877108e4SArnaldo Carvalho de Melo 
1231877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_evsel__type);
1232877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1233877108e4SArnaldo Carvalho de Melo 
123485e37de3SJiri Olsa 	Py_INCREF(&pyrf_mmap_event__type);
123585e37de3SJiri Olsa 	PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
123685e37de3SJiri Olsa 
123785e37de3SJiri Olsa 	Py_INCREF(&pyrf_lost_event__type);
123885e37de3SJiri Olsa 	PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
123985e37de3SJiri Olsa 
124085e37de3SJiri Olsa 	Py_INCREF(&pyrf_comm_event__type);
124185e37de3SJiri Olsa 	PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
124285e37de3SJiri Olsa 
124385e37de3SJiri Olsa 	Py_INCREF(&pyrf_task_event__type);
124485e37de3SJiri Olsa 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
124585e37de3SJiri Olsa 
124685e37de3SJiri Olsa 	Py_INCREF(&pyrf_throttle_event__type);
124785e37de3SJiri Olsa 	PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
124885e37de3SJiri Olsa 
124985e37de3SJiri Olsa 	Py_INCREF(&pyrf_task_event__type);
125085e37de3SJiri Olsa 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
125185e37de3SJiri Olsa 
125285e37de3SJiri Olsa 	Py_INCREF(&pyrf_read_event__type);
125385e37de3SJiri Olsa 	PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
125485e37de3SJiri Olsa 
125585e37de3SJiri Olsa 	Py_INCREF(&pyrf_sample_event__type);
125685e37de3SJiri Olsa 	PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
125785e37de3SJiri Olsa 
125885e37de3SJiri Olsa 	Py_INCREF(&pyrf_context_switch_event__type);
125985e37de3SJiri Olsa 	PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
126085e37de3SJiri Olsa 
1261877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_thread_map__type);
1262877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1263877108e4SArnaldo Carvalho de Melo 
1264877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_cpu_map__type);
1265877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1266877108e4SArnaldo Carvalho de Melo 
1267877108e4SArnaldo Carvalho de Melo 	dict = PyModule_GetDict(module);
1268877108e4SArnaldo Carvalho de Melo 	if (dict == NULL)
1269877108e4SArnaldo Carvalho de Melo 		goto error;
1270877108e4SArnaldo Carvalho de Melo 
1271877108e4SArnaldo Carvalho de Melo 	for (i = 0; perf__constants[i].name != NULL; i++) {
1272877108e4SArnaldo Carvalho de Melo 		obj = PyInt_FromLong(perf__constants[i].value);
1273877108e4SArnaldo Carvalho de Melo 		if (obj == NULL)
1274877108e4SArnaldo Carvalho de Melo 			goto error;
1275877108e4SArnaldo Carvalho de Melo 		PyDict_SetItemString(dict, perf__constants[i].name, obj);
1276877108e4SArnaldo Carvalho de Melo 		Py_DECREF(obj);
1277877108e4SArnaldo Carvalho de Melo 	}
1278877108e4SArnaldo Carvalho de Melo 
1279877108e4SArnaldo Carvalho de Melo error:
1280877108e4SArnaldo Carvalho de Melo 	if (PyErr_Occurred())
1281877108e4SArnaldo Carvalho de Melo 		PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1282877108e4SArnaldo Carvalho de Melo }
12830c6332e9SArnaldo Carvalho de Melo 
12840c6332e9SArnaldo Carvalho de Melo /*
12850c6332e9SArnaldo Carvalho de Melo  * Dummy, to avoid dragging all the test_attr infrastructure in the python
12860c6332e9SArnaldo Carvalho de Melo  * binding.
12870c6332e9SArnaldo Carvalho de Melo  */
12880c6332e9SArnaldo Carvalho de Melo void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
12890c6332e9SArnaldo Carvalho de Melo                      int fd, int group_fd, unsigned long flags)
12900c6332e9SArnaldo Carvalho de Melo {
12910c6332e9SArnaldo Carvalho de Melo }
1292