xref: /linux/tools/perf/util/python.c (revision 63dab225f334e0e21f7106aed8d888b500b53ce6)
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>
5877108e4SArnaldo Carvalho de Melo #include "evlist.h"
6877108e4SArnaldo Carvalho de Melo #include "evsel.h"
7877108e4SArnaldo Carvalho de Melo #include "event.h"
8877108e4SArnaldo Carvalho de Melo #include "cpumap.h"
9877108e4SArnaldo Carvalho de Melo #include "thread_map.h"
10877108e4SArnaldo Carvalho de Melo 
11cfff2d90SFrederic Weisbecker /* Define PyVarObject_HEAD_INIT for python 2.5 */
12cfff2d90SFrederic Weisbecker #ifndef PyVarObject_HEAD_INIT
13cfff2d90SFrederic Weisbecker # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
14cfff2d90SFrederic Weisbecker #endif
15cfff2d90SFrederic Weisbecker 
16877108e4SArnaldo Carvalho de Melo struct throttle_event {
17877108e4SArnaldo Carvalho de Melo 	struct perf_event_header header;
18877108e4SArnaldo Carvalho de Melo 	u64			 time;
19877108e4SArnaldo Carvalho de Melo 	u64			 id;
20877108e4SArnaldo Carvalho de Melo 	u64			 stream_id;
21877108e4SArnaldo Carvalho de Melo };
22877108e4SArnaldo Carvalho de Melo 
23f6bbc1daSArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void);
24f6bbc1daSArnaldo Carvalho de Melo 
25877108e4SArnaldo Carvalho de Melo #define member_def(type, member, ptype, help) \
26877108e4SArnaldo Carvalho de Melo 	{ #member, ptype, \
27877108e4SArnaldo Carvalho de Melo 	  offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
28877108e4SArnaldo Carvalho de Melo 	  0, help }
29877108e4SArnaldo Carvalho de Melo 
30877108e4SArnaldo Carvalho de Melo #define sample_member_def(name, member, ptype, help) \
31877108e4SArnaldo Carvalho de Melo 	{ #name, ptype, \
32877108e4SArnaldo Carvalho de Melo 	  offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
33877108e4SArnaldo Carvalho de Melo 	  0, help }
34877108e4SArnaldo Carvalho de Melo 
35877108e4SArnaldo Carvalho de Melo struct pyrf_event {
36877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
37877108e4SArnaldo Carvalho de Melo 	struct perf_sample sample;
38877108e4SArnaldo Carvalho de Melo 	union perf_event   event;
39877108e4SArnaldo Carvalho de Melo };
40877108e4SArnaldo Carvalho de Melo 
41877108e4SArnaldo Carvalho de Melo #define sample_members \
42f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"),			 \
43877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_pid, pid, T_INT, "event pid"),			 \
44877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_tid, tid, T_INT, "event tid"),			 \
45f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"),		 \
46f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"),		 \
47f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_id, id, T_ULONGLONG, "event id"),			 \
48f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
49f6bbc1daSArnaldo Carvalho de Melo 	sample_member_def(sample_period, period, T_ULONGLONG, "event period"),		 \
50877108e4SArnaldo Carvalho de Melo 	sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
51877108e4SArnaldo Carvalho de Melo 
52877108e4SArnaldo Carvalho de Melo static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
53877108e4SArnaldo Carvalho de Melo 
54877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_mmap_event__members[] = {
55877108e4SArnaldo Carvalho de Melo 	sample_members
56877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
57877108e4SArnaldo Carvalho de Melo 	member_def(mmap_event, pid, T_UINT, "event pid"),
58877108e4SArnaldo Carvalho de Melo 	member_def(mmap_event, tid, T_UINT, "event tid"),
59f6bbc1daSArnaldo Carvalho de Melo 	member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
60f6bbc1daSArnaldo Carvalho de Melo 	member_def(mmap_event, len, T_ULONGLONG, "map length"),
61f6bbc1daSArnaldo Carvalho de Melo 	member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
62877108e4SArnaldo Carvalho de Melo 	member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
63f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
64877108e4SArnaldo Carvalho de Melo };
65877108e4SArnaldo Carvalho de Melo 
66877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
67877108e4SArnaldo Carvalho de Melo {
68877108e4SArnaldo Carvalho de Melo 	PyObject *ret;
69877108e4SArnaldo Carvalho de Melo 	char *s;
70877108e4SArnaldo Carvalho de Melo 
71877108e4SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
72877108e4SArnaldo Carvalho de Melo 			 "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
73877108e4SArnaldo Carvalho de Melo 			 "filename: %s }",
74877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.pid, pevent->event.mmap.tid,
75877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.start, pevent->event.mmap.len,
76877108e4SArnaldo Carvalho de Melo 		     pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
77877108e4SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
78877108e4SArnaldo Carvalho de Melo 	} else {
79877108e4SArnaldo Carvalho de Melo 		ret = PyString_FromString(s);
80877108e4SArnaldo Carvalho de Melo 		free(s);
81877108e4SArnaldo Carvalho de Melo 	}
82877108e4SArnaldo Carvalho de Melo 	return ret;
83877108e4SArnaldo Carvalho de Melo }
84877108e4SArnaldo Carvalho de Melo 
85877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_mmap_event__type = {
86877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
87877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.mmap_event",
88877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
89877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
90877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_mmap_event__doc,
91877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_mmap_event__members,
92877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_mmap_event__repr,
93877108e4SArnaldo Carvalho de Melo };
94877108e4SArnaldo Carvalho de Melo 
95877108e4SArnaldo Carvalho de Melo static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
96877108e4SArnaldo Carvalho de Melo 
97877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_task_event__members[] = {
98877108e4SArnaldo Carvalho de Melo 	sample_members
99877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
100877108e4SArnaldo Carvalho de Melo 	member_def(fork_event, pid, T_UINT, "event pid"),
101877108e4SArnaldo Carvalho de Melo 	member_def(fork_event, ppid, T_UINT, "event ppid"),
102877108e4SArnaldo Carvalho de Melo 	member_def(fork_event, tid, T_UINT, "event tid"),
103877108e4SArnaldo Carvalho de Melo 	member_def(fork_event, ptid, T_UINT, "event ptid"),
104f6bbc1daSArnaldo Carvalho de Melo 	member_def(fork_event, time, T_ULONGLONG, "timestamp"),
105f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
106877108e4SArnaldo Carvalho de Melo };
107877108e4SArnaldo Carvalho de Melo 
108877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
109877108e4SArnaldo Carvalho de Melo {
110877108e4SArnaldo Carvalho de Melo 	return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
111877108e4SArnaldo Carvalho de Melo 				   "ptid: %u, time: %" PRIu64 "}",
112877108e4SArnaldo Carvalho de Melo 				   pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
113877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.pid,
114877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.ppid,
115877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.tid,
116877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.ptid,
117877108e4SArnaldo Carvalho de Melo 				   pevent->event.fork.time);
118877108e4SArnaldo Carvalho de Melo }
119877108e4SArnaldo Carvalho de Melo 
120877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_task_event__type = {
121877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
122877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.task_event",
123877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
124877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
125877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_task_event__doc,
126877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_task_event__members,
127877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_task_event__repr,
128877108e4SArnaldo Carvalho de Melo };
129877108e4SArnaldo Carvalho de Melo 
130877108e4SArnaldo Carvalho de Melo static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
131877108e4SArnaldo Carvalho de Melo 
132877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_comm_event__members[] = {
133877108e4SArnaldo Carvalho de Melo 	sample_members
134877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
135877108e4SArnaldo Carvalho de Melo 	member_def(comm_event, pid, T_UINT, "event pid"),
136877108e4SArnaldo Carvalho de Melo 	member_def(comm_event, tid, T_UINT, "event tid"),
137877108e4SArnaldo Carvalho de Melo 	member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
138f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
139877108e4SArnaldo Carvalho de Melo };
140877108e4SArnaldo Carvalho de Melo 
141877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
142877108e4SArnaldo Carvalho de Melo {
143877108e4SArnaldo Carvalho de Melo 	return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
144877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.pid,
145877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.tid,
146877108e4SArnaldo Carvalho de Melo 				   pevent->event.comm.comm);
147877108e4SArnaldo Carvalho de Melo }
148877108e4SArnaldo Carvalho de Melo 
149877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_comm_event__type = {
150877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
151877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.comm_event",
152877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
153877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
154877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_comm_event__doc,
155877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_comm_event__members,
156877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_comm_event__repr,
157877108e4SArnaldo Carvalho de Melo };
158877108e4SArnaldo Carvalho de Melo 
159877108e4SArnaldo Carvalho de Melo static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
160877108e4SArnaldo Carvalho de Melo 
161877108e4SArnaldo Carvalho de Melo static PyMemberDef pyrf_throttle_event__members[] = {
162877108e4SArnaldo Carvalho de Melo 	sample_members
163877108e4SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
164f6bbc1daSArnaldo Carvalho de Melo 	member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
165f6bbc1daSArnaldo Carvalho de Melo 	member_def(throttle_event, id, T_ULONGLONG, "event id"),
166f6bbc1daSArnaldo Carvalho de Melo 	member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
167f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
168877108e4SArnaldo Carvalho de Melo };
169877108e4SArnaldo Carvalho de Melo 
170877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
171877108e4SArnaldo Carvalho de Melo {
172877108e4SArnaldo Carvalho de Melo 	struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
173877108e4SArnaldo Carvalho de Melo 
174877108e4SArnaldo Carvalho de Melo 	return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
175877108e4SArnaldo Carvalho de Melo 				   ", stream_id: %" PRIu64 " }",
176877108e4SArnaldo Carvalho de Melo 				   pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
177877108e4SArnaldo Carvalho de Melo 				   te->time, te->id, te->stream_id);
178877108e4SArnaldo Carvalho de Melo }
179877108e4SArnaldo Carvalho de Melo 
180877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_throttle_event__type = {
181877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
182877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.throttle_event",
183877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
184877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
185877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_throttle_event__doc,
186877108e4SArnaldo Carvalho de Melo 	.tp_members	= pyrf_throttle_event__members,
187877108e4SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_throttle_event__repr,
188877108e4SArnaldo Carvalho de Melo };
189877108e4SArnaldo Carvalho de Melo 
1903e9f45a7SArnaldo Carvalho de Melo static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
1913e9f45a7SArnaldo Carvalho de Melo 
1923e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_lost_event__members[] = {
1933e9f45a7SArnaldo Carvalho de Melo 	sample_members
1943e9f45a7SArnaldo Carvalho de Melo 	member_def(lost_event, id, T_ULONGLONG, "event id"),
1953e9f45a7SArnaldo Carvalho de Melo 	member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
1963e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
1973e9f45a7SArnaldo Carvalho de Melo };
1983e9f45a7SArnaldo Carvalho de Melo 
1993e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
2003e9f45a7SArnaldo Carvalho de Melo {
2013e9f45a7SArnaldo Carvalho de Melo 	PyObject *ret;
2023e9f45a7SArnaldo Carvalho de Melo 	char *s;
2033e9f45a7SArnaldo Carvalho de Melo 
2043e9f45a7SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
2053e9f45a7SArnaldo Carvalho de Melo 			 "lost: %#" PRIx64 " }",
2063e9f45a7SArnaldo Carvalho de Melo 		     pevent->event.lost.id, pevent->event.lost.lost) < 0) {
2073e9f45a7SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
2083e9f45a7SArnaldo Carvalho de Melo 	} else {
2093e9f45a7SArnaldo Carvalho de Melo 		ret = PyString_FromString(s);
2103e9f45a7SArnaldo Carvalho de Melo 		free(s);
2113e9f45a7SArnaldo Carvalho de Melo 	}
2123e9f45a7SArnaldo Carvalho de Melo 	return ret;
2133e9f45a7SArnaldo Carvalho de Melo }
2143e9f45a7SArnaldo Carvalho de Melo 
2153e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_lost_event__type = {
2163e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
2173e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.lost_event",
2183e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
2193e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
2203e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_lost_event__doc,
2213e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_lost_event__members,
2223e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_lost_event__repr,
2233e9f45a7SArnaldo Carvalho de Melo };
2243e9f45a7SArnaldo Carvalho de Melo 
2253e9f45a7SArnaldo Carvalho de Melo static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
2263e9f45a7SArnaldo Carvalho de Melo 
2273e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_read_event__members[] = {
2283e9f45a7SArnaldo Carvalho de Melo 	sample_members
2293e9f45a7SArnaldo Carvalho de Melo 	member_def(read_event, pid, T_UINT, "event pid"),
2303e9f45a7SArnaldo Carvalho de Melo 	member_def(read_event, tid, T_UINT, "event tid"),
2313e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
2323e9f45a7SArnaldo Carvalho de Melo };
2333e9f45a7SArnaldo Carvalho de Melo 
2343e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
2353e9f45a7SArnaldo Carvalho de Melo {
2363e9f45a7SArnaldo Carvalho de Melo 	return PyString_FromFormat("{ type: read, pid: %u, tid: %u }",
2373e9f45a7SArnaldo Carvalho de Melo 				   pevent->event.read.pid,
2383e9f45a7SArnaldo Carvalho de Melo 				   pevent->event.read.tid);
2393e9f45a7SArnaldo Carvalho de Melo 	/*
2403e9f45a7SArnaldo Carvalho de Melo  	 * FIXME: return the array of read values,
2413e9f45a7SArnaldo Carvalho de Melo  	 * making this method useful ;-)
2423e9f45a7SArnaldo Carvalho de Melo  	 */
2433e9f45a7SArnaldo Carvalho de Melo }
2443e9f45a7SArnaldo Carvalho de Melo 
2453e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_read_event__type = {
2463e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
2473e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.read_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_read_event__doc,
2513e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_read_event__members,
2523e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_read_event__repr,
2533e9f45a7SArnaldo Carvalho de Melo };
2543e9f45a7SArnaldo Carvalho de Melo 
2553e9f45a7SArnaldo Carvalho de Melo static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
2563e9f45a7SArnaldo Carvalho de Melo 
2573e9f45a7SArnaldo Carvalho de Melo static PyMemberDef pyrf_sample_event__members[] = {
2583e9f45a7SArnaldo Carvalho de Melo 	sample_members
2593e9f45a7SArnaldo Carvalho de Melo 	member_def(perf_event_header, type, T_UINT, "event type"),
2603e9f45a7SArnaldo Carvalho de Melo 	{ .name = NULL, },
2613e9f45a7SArnaldo Carvalho de Melo };
2623e9f45a7SArnaldo Carvalho de Melo 
2633e9f45a7SArnaldo Carvalho de Melo static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
2643e9f45a7SArnaldo Carvalho de Melo {
2653e9f45a7SArnaldo Carvalho de Melo 	PyObject *ret;
2663e9f45a7SArnaldo Carvalho de Melo 	char *s;
2673e9f45a7SArnaldo Carvalho de Melo 
2683e9f45a7SArnaldo Carvalho de Melo 	if (asprintf(&s, "{ type: sample }") < 0) {
2693e9f45a7SArnaldo Carvalho de Melo 		ret = PyErr_NoMemory();
2703e9f45a7SArnaldo Carvalho de Melo 	} else {
2713e9f45a7SArnaldo Carvalho de Melo 		ret = PyString_FromString(s);
2723e9f45a7SArnaldo Carvalho de Melo 		free(s);
2733e9f45a7SArnaldo Carvalho de Melo 	}
2743e9f45a7SArnaldo Carvalho de Melo 	return ret;
2753e9f45a7SArnaldo Carvalho de Melo }
2763e9f45a7SArnaldo Carvalho de Melo 
2773e9f45a7SArnaldo Carvalho de Melo static PyTypeObject pyrf_sample_event__type = {
2783e9f45a7SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
2793e9f45a7SArnaldo Carvalho de Melo 	.tp_name	= "perf.sample_event",
2803e9f45a7SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_event),
2813e9f45a7SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
2823e9f45a7SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_sample_event__doc,
2833e9f45a7SArnaldo Carvalho de Melo 	.tp_members	= pyrf_sample_event__members,
2843e9f45a7SArnaldo Carvalho de Melo 	.tp_repr	= (reprfunc)pyrf_sample_event__repr,
2853e9f45a7SArnaldo Carvalho de Melo };
2863e9f45a7SArnaldo Carvalho de Melo 
287877108e4SArnaldo Carvalho de Melo static int pyrf_event__setup_types(void)
288877108e4SArnaldo Carvalho de Melo {
289877108e4SArnaldo Carvalho de Melo 	int err;
290877108e4SArnaldo Carvalho de Melo 	pyrf_mmap_event__type.tp_new =
291877108e4SArnaldo Carvalho de Melo 	pyrf_task_event__type.tp_new =
292877108e4SArnaldo Carvalho de Melo 	pyrf_comm_event__type.tp_new =
2933e9f45a7SArnaldo Carvalho de Melo 	pyrf_lost_event__type.tp_new =
2943e9f45a7SArnaldo Carvalho de Melo 	pyrf_read_event__type.tp_new =
2953e9f45a7SArnaldo Carvalho de Melo 	pyrf_sample_event__type.tp_new =
296877108e4SArnaldo Carvalho de Melo 	pyrf_throttle_event__type.tp_new = PyType_GenericNew;
297877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_mmap_event__type);
298877108e4SArnaldo Carvalho de Melo 	if (err < 0)
299877108e4SArnaldo Carvalho de Melo 		goto out;
3003e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_lost_event__type);
3013e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
3023e9f45a7SArnaldo Carvalho de Melo 		goto out;
303877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_task_event__type);
304877108e4SArnaldo Carvalho de Melo 	if (err < 0)
305877108e4SArnaldo Carvalho de Melo 		goto out;
306877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_comm_event__type);
307877108e4SArnaldo Carvalho de Melo 	if (err < 0)
308877108e4SArnaldo Carvalho de Melo 		goto out;
309877108e4SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_throttle_event__type);
310877108e4SArnaldo Carvalho de Melo 	if (err < 0)
311877108e4SArnaldo Carvalho de Melo 		goto out;
3123e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_read_event__type);
3133e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
3143e9f45a7SArnaldo Carvalho de Melo 		goto out;
3153e9f45a7SArnaldo Carvalho de Melo 	err = PyType_Ready(&pyrf_sample_event__type);
3163e9f45a7SArnaldo Carvalho de Melo 	if (err < 0)
3173e9f45a7SArnaldo Carvalho de Melo 		goto out;
318877108e4SArnaldo Carvalho de Melo out:
319877108e4SArnaldo Carvalho de Melo 	return err;
320877108e4SArnaldo Carvalho de Melo }
321877108e4SArnaldo Carvalho de Melo 
322877108e4SArnaldo Carvalho de Melo static PyTypeObject *pyrf_event__type[] = {
323877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_MMAP]	 = &pyrf_mmap_event__type,
3243e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_LOST]	 = &pyrf_lost_event__type,
325877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_COMM]	 = &pyrf_comm_event__type,
326877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_EXIT]	 = &pyrf_task_event__type,
327877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_THROTTLE]	 = &pyrf_throttle_event__type,
328877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
329877108e4SArnaldo Carvalho de Melo 	[PERF_RECORD_FORK]	 = &pyrf_task_event__type,
3303e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_READ]	 = &pyrf_read_event__type,
3313e9f45a7SArnaldo Carvalho de Melo 	[PERF_RECORD_SAMPLE]	 = &pyrf_sample_event__type,
332877108e4SArnaldo Carvalho de Melo };
333877108e4SArnaldo Carvalho de Melo 
334877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_event__new(union perf_event *event)
335877108e4SArnaldo Carvalho de Melo {
336877108e4SArnaldo Carvalho de Melo 	struct pyrf_event *pevent;
337877108e4SArnaldo Carvalho de Melo 	PyTypeObject *ptype;
338877108e4SArnaldo Carvalho de Melo 
339877108e4SArnaldo Carvalho de Melo 	if (event->header.type < PERF_RECORD_MMAP ||
340877108e4SArnaldo Carvalho de Melo 	    event->header.type > PERF_RECORD_SAMPLE)
341877108e4SArnaldo Carvalho de Melo 		return NULL;
342877108e4SArnaldo Carvalho de Melo 
343877108e4SArnaldo Carvalho de Melo 	ptype = pyrf_event__type[event->header.type];
344877108e4SArnaldo Carvalho de Melo 	pevent = PyObject_New(struct pyrf_event, ptype);
345877108e4SArnaldo Carvalho de Melo 	if (pevent != NULL)
346877108e4SArnaldo Carvalho de Melo 		memcpy(&pevent->event, event, event->header.size);
347877108e4SArnaldo Carvalho de Melo 	return (PyObject *)pevent;
348877108e4SArnaldo Carvalho de Melo }
349877108e4SArnaldo Carvalho de Melo 
350877108e4SArnaldo Carvalho de Melo struct pyrf_cpu_map {
351877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
352877108e4SArnaldo Carvalho de Melo 
353877108e4SArnaldo Carvalho de Melo 	struct cpu_map *cpus;
354877108e4SArnaldo Carvalho de Melo };
355877108e4SArnaldo Carvalho de Melo 
356877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
357877108e4SArnaldo Carvalho de Melo 			      PyObject *args, PyObject *kwargs)
358877108e4SArnaldo Carvalho de Melo {
35964348153SFrederic Weisbecker 	static char *kwlist[] = { "cpustr", NULL };
360877108e4SArnaldo Carvalho de Melo 	char *cpustr = NULL;
361877108e4SArnaldo Carvalho de Melo 
362877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
363877108e4SArnaldo Carvalho de Melo 					 kwlist, &cpustr))
364877108e4SArnaldo Carvalho de Melo 		return -1;
365877108e4SArnaldo Carvalho de Melo 
366877108e4SArnaldo Carvalho de Melo 	pcpus->cpus = cpu_map__new(cpustr);
367877108e4SArnaldo Carvalho de Melo 	if (pcpus->cpus == NULL)
368877108e4SArnaldo Carvalho de Melo 		return -1;
369877108e4SArnaldo Carvalho de Melo 	return 0;
370877108e4SArnaldo Carvalho de Melo }
371877108e4SArnaldo Carvalho de Melo 
372877108e4SArnaldo Carvalho de Melo static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
373877108e4SArnaldo Carvalho de Melo {
374877108e4SArnaldo Carvalho de Melo 	cpu_map__delete(pcpus->cpus);
375877108e4SArnaldo Carvalho de Melo 	pcpus->ob_type->tp_free((PyObject*)pcpus);
376877108e4SArnaldo Carvalho de Melo }
377877108e4SArnaldo Carvalho de Melo 
378877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
379877108e4SArnaldo Carvalho de Melo {
380877108e4SArnaldo Carvalho de Melo 	struct pyrf_cpu_map *pcpus = (void *)obj;
381877108e4SArnaldo Carvalho de Melo 
382877108e4SArnaldo Carvalho de Melo 	return pcpus->cpus->nr;
383877108e4SArnaldo Carvalho de Melo }
384877108e4SArnaldo Carvalho de Melo 
385877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
386877108e4SArnaldo Carvalho de Melo {
387877108e4SArnaldo Carvalho de Melo 	struct pyrf_cpu_map *pcpus = (void *)obj;
388877108e4SArnaldo Carvalho de Melo 
389877108e4SArnaldo Carvalho de Melo 	if (i >= pcpus->cpus->nr)
390877108e4SArnaldo Carvalho de Melo 		return NULL;
391877108e4SArnaldo Carvalho de Melo 
392877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", pcpus->cpus->map[i]);
393877108e4SArnaldo Carvalho de Melo }
394877108e4SArnaldo Carvalho de Melo 
395877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_cpu_map__sequence_methods = {
396877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_cpu_map__length,
397877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_cpu_map__item,
398877108e4SArnaldo Carvalho de Melo };
399877108e4SArnaldo Carvalho de Melo 
400877108e4SArnaldo Carvalho de Melo static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
401877108e4SArnaldo Carvalho de Melo 
402877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_cpu_map__type = {
403877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
404877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.cpu_map",
405877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_cpu_map),
406877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_cpu_map__delete,
407877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
408877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_cpu_map__doc,
409877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_cpu_map__sequence_methods,
410877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_cpu_map__init,
411877108e4SArnaldo Carvalho de Melo };
412877108e4SArnaldo Carvalho de Melo 
413877108e4SArnaldo Carvalho de Melo static int pyrf_cpu_map__setup_types(void)
414877108e4SArnaldo Carvalho de Melo {
415877108e4SArnaldo Carvalho de Melo 	pyrf_cpu_map__type.tp_new = PyType_GenericNew;
416877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_cpu_map__type);
417877108e4SArnaldo Carvalho de Melo }
418877108e4SArnaldo Carvalho de Melo 
419877108e4SArnaldo Carvalho de Melo struct pyrf_thread_map {
420877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
421877108e4SArnaldo Carvalho de Melo 
422877108e4SArnaldo Carvalho de Melo 	struct thread_map *threads;
423877108e4SArnaldo Carvalho de Melo };
424877108e4SArnaldo Carvalho de Melo 
425877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
426877108e4SArnaldo Carvalho de Melo 				 PyObject *args, PyObject *kwargs)
427877108e4SArnaldo Carvalho de Melo {
4280d37aa34SArnaldo Carvalho de Melo 	static char *kwlist[] = { "pid", "tid", "uid", NULL };
4290d37aa34SArnaldo Carvalho de Melo 	int pid = -1, tid = -1, uid = UINT_MAX;
430877108e4SArnaldo Carvalho de Melo 
4310d37aa34SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
4320d37aa34SArnaldo Carvalho de Melo 					 kwlist, &pid, &tid, &uid))
433877108e4SArnaldo Carvalho de Melo 		return -1;
434877108e4SArnaldo Carvalho de Melo 
4350d37aa34SArnaldo Carvalho de Melo 	pthreads->threads = thread_map__new(pid, tid, uid);
436877108e4SArnaldo Carvalho de Melo 	if (pthreads->threads == NULL)
437877108e4SArnaldo Carvalho de Melo 		return -1;
438877108e4SArnaldo Carvalho de Melo 	return 0;
439877108e4SArnaldo Carvalho de Melo }
440877108e4SArnaldo Carvalho de Melo 
441877108e4SArnaldo Carvalho de Melo static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
442877108e4SArnaldo Carvalho de Melo {
443877108e4SArnaldo Carvalho de Melo 	thread_map__delete(pthreads->threads);
444877108e4SArnaldo Carvalho de Melo 	pthreads->ob_type->tp_free((PyObject*)pthreads);
445877108e4SArnaldo Carvalho de Melo }
446877108e4SArnaldo Carvalho de Melo 
447877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
448877108e4SArnaldo Carvalho de Melo {
449877108e4SArnaldo Carvalho de Melo 	struct pyrf_thread_map *pthreads = (void *)obj;
450877108e4SArnaldo Carvalho de Melo 
451877108e4SArnaldo Carvalho de Melo 	return pthreads->threads->nr;
452877108e4SArnaldo Carvalho de Melo }
453877108e4SArnaldo Carvalho de Melo 
454877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
455877108e4SArnaldo Carvalho de Melo {
456877108e4SArnaldo Carvalho de Melo 	struct pyrf_thread_map *pthreads = (void *)obj;
457877108e4SArnaldo Carvalho de Melo 
458877108e4SArnaldo Carvalho de Melo 	if (i >= pthreads->threads->nr)
459877108e4SArnaldo Carvalho de Melo 		return NULL;
460877108e4SArnaldo Carvalho de Melo 
461877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", pthreads->threads->map[i]);
462877108e4SArnaldo Carvalho de Melo }
463877108e4SArnaldo Carvalho de Melo 
464877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_thread_map__sequence_methods = {
465877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_thread_map__length,
466877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_thread_map__item,
467877108e4SArnaldo Carvalho de Melo };
468877108e4SArnaldo Carvalho de Melo 
469877108e4SArnaldo Carvalho de Melo static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
470877108e4SArnaldo Carvalho de Melo 
471877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_thread_map__type = {
472877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
473877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.thread_map",
474877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_thread_map),
475877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_thread_map__delete,
476877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
477877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_thread_map__doc,
478877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_thread_map__sequence_methods,
479877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_thread_map__init,
480877108e4SArnaldo Carvalho de Melo };
481877108e4SArnaldo Carvalho de Melo 
482877108e4SArnaldo Carvalho de Melo static int pyrf_thread_map__setup_types(void)
483877108e4SArnaldo Carvalho de Melo {
484877108e4SArnaldo Carvalho de Melo 	pyrf_thread_map__type.tp_new = PyType_GenericNew;
485877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_thread_map__type);
486877108e4SArnaldo Carvalho de Melo }
487877108e4SArnaldo Carvalho de Melo 
488877108e4SArnaldo Carvalho de Melo struct pyrf_evsel {
489877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
490877108e4SArnaldo Carvalho de Melo 
491877108e4SArnaldo Carvalho de Melo 	struct perf_evsel evsel;
492877108e4SArnaldo Carvalho de Melo };
493877108e4SArnaldo Carvalho de Melo 
494877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
495877108e4SArnaldo Carvalho de Melo 			    PyObject *args, PyObject *kwargs)
496877108e4SArnaldo Carvalho de Melo {
497877108e4SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
498877108e4SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
499877108e4SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
500877108e4SArnaldo Carvalho de Melo 		.sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
501877108e4SArnaldo Carvalho de Melo 	};
502877108e4SArnaldo Carvalho de Melo 	static char *kwlist[] = {
503877108e4SArnaldo Carvalho de Melo 		"type",
504877108e4SArnaldo Carvalho de Melo 		"config",
505877108e4SArnaldo Carvalho de Melo 		"sample_freq",
506877108e4SArnaldo Carvalho de Melo 		"sample_period",
507877108e4SArnaldo Carvalho de Melo 		"sample_type",
508877108e4SArnaldo Carvalho de Melo 		"read_format",
509877108e4SArnaldo Carvalho de Melo 		"disabled",
510877108e4SArnaldo Carvalho de Melo 		"inherit",
511877108e4SArnaldo Carvalho de Melo 		"pinned",
512877108e4SArnaldo Carvalho de Melo 		"exclusive",
513877108e4SArnaldo Carvalho de Melo 		"exclude_user",
514877108e4SArnaldo Carvalho de Melo 		"exclude_kernel",
515877108e4SArnaldo Carvalho de Melo 		"exclude_hv",
516877108e4SArnaldo Carvalho de Melo 		"exclude_idle",
517877108e4SArnaldo Carvalho de Melo 		"mmap",
518877108e4SArnaldo Carvalho de Melo 		"comm",
519877108e4SArnaldo Carvalho de Melo 		"freq",
520877108e4SArnaldo Carvalho de Melo 		"inherit_stat",
521877108e4SArnaldo Carvalho de Melo 		"enable_on_exec",
522877108e4SArnaldo Carvalho de Melo 		"task",
523877108e4SArnaldo Carvalho de Melo 		"watermark",
524877108e4SArnaldo Carvalho de Melo 		"precise_ip",
525877108e4SArnaldo Carvalho de Melo 		"mmap_data",
526877108e4SArnaldo Carvalho de Melo 		"sample_id_all",
527877108e4SArnaldo Carvalho de Melo 		"wakeup_events",
528877108e4SArnaldo Carvalho de Melo 		"bp_type",
529877108e4SArnaldo Carvalho de Melo 		"bp_addr",
53064348153SFrederic Weisbecker 		"bp_len",
53164348153SFrederic Weisbecker 		 NULL
53264348153SFrederic Weisbecker 	};
533877108e4SArnaldo Carvalho de Melo 	u64 sample_period = 0;
534877108e4SArnaldo Carvalho de Melo 	u32 disabled = 0,
535877108e4SArnaldo Carvalho de Melo 	    inherit = 0,
536877108e4SArnaldo Carvalho de Melo 	    pinned = 0,
537877108e4SArnaldo Carvalho de Melo 	    exclusive = 0,
538877108e4SArnaldo Carvalho de Melo 	    exclude_user = 0,
539877108e4SArnaldo Carvalho de Melo 	    exclude_kernel = 0,
540877108e4SArnaldo Carvalho de Melo 	    exclude_hv = 0,
541877108e4SArnaldo Carvalho de Melo 	    exclude_idle = 0,
542877108e4SArnaldo Carvalho de Melo 	    mmap = 0,
543877108e4SArnaldo Carvalho de Melo 	    comm = 0,
544877108e4SArnaldo Carvalho de Melo 	    freq = 1,
545877108e4SArnaldo Carvalho de Melo 	    inherit_stat = 0,
546877108e4SArnaldo Carvalho de Melo 	    enable_on_exec = 0,
547877108e4SArnaldo Carvalho de Melo 	    task = 0,
548877108e4SArnaldo Carvalho de Melo 	    watermark = 0,
549877108e4SArnaldo Carvalho de Melo 	    precise_ip = 0,
550877108e4SArnaldo Carvalho de Melo 	    mmap_data = 0,
551877108e4SArnaldo Carvalho de Melo 	    sample_id_all = 1;
552877108e4SArnaldo Carvalho de Melo 	int idx = 0;
553877108e4SArnaldo Carvalho de Melo 
554877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
555877108e4SArnaldo Carvalho de Melo 					 "|iKiKKiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
556877108e4SArnaldo Carvalho de Melo 					 &attr.type, &attr.config, &attr.sample_freq,
557877108e4SArnaldo Carvalho de Melo 					 &sample_period, &attr.sample_type,
558877108e4SArnaldo Carvalho de Melo 					 &attr.read_format, &disabled, &inherit,
559877108e4SArnaldo Carvalho de Melo 					 &pinned, &exclusive, &exclude_user,
560877108e4SArnaldo Carvalho de Melo 					 &exclude_kernel, &exclude_hv, &exclude_idle,
561877108e4SArnaldo Carvalho de Melo 					 &mmap, &comm, &freq, &inherit_stat,
562877108e4SArnaldo Carvalho de Melo 					 &enable_on_exec, &task, &watermark,
563877108e4SArnaldo Carvalho de Melo 					 &precise_ip, &mmap_data, &sample_id_all,
564877108e4SArnaldo Carvalho de Melo 					 &attr.wakeup_events, &attr.bp_type,
565877108e4SArnaldo Carvalho de Melo 					 &attr.bp_addr, &attr.bp_len, &idx))
566877108e4SArnaldo Carvalho de Melo 		return -1;
567877108e4SArnaldo Carvalho de Melo 
568877108e4SArnaldo Carvalho de Melo 	/* union... */
569877108e4SArnaldo Carvalho de Melo 	if (sample_period != 0) {
570877108e4SArnaldo Carvalho de Melo 		if (attr.sample_freq != 0)
571877108e4SArnaldo Carvalho de Melo 			return -1; /* FIXME: throw right exception */
572877108e4SArnaldo Carvalho de Melo 		attr.sample_period = sample_period;
573877108e4SArnaldo Carvalho de Melo 	}
574877108e4SArnaldo Carvalho de Melo 
575877108e4SArnaldo Carvalho de Melo 	/* Bitfields */
576877108e4SArnaldo Carvalho de Melo 	attr.disabled	    = disabled;
577877108e4SArnaldo Carvalho de Melo 	attr.inherit	    = inherit;
578877108e4SArnaldo Carvalho de Melo 	attr.pinned	    = pinned;
579877108e4SArnaldo Carvalho de Melo 	attr.exclusive	    = exclusive;
580877108e4SArnaldo Carvalho de Melo 	attr.exclude_user   = exclude_user;
581877108e4SArnaldo Carvalho de Melo 	attr.exclude_kernel = exclude_kernel;
582877108e4SArnaldo Carvalho de Melo 	attr.exclude_hv	    = exclude_hv;
583877108e4SArnaldo Carvalho de Melo 	attr.exclude_idle   = exclude_idle;
584877108e4SArnaldo Carvalho de Melo 	attr.mmap	    = mmap;
585877108e4SArnaldo Carvalho de Melo 	attr.comm	    = comm;
586877108e4SArnaldo Carvalho de Melo 	attr.freq	    = freq;
587877108e4SArnaldo Carvalho de Melo 	attr.inherit_stat   = inherit_stat;
588877108e4SArnaldo Carvalho de Melo 	attr.enable_on_exec = enable_on_exec;
589877108e4SArnaldo Carvalho de Melo 	attr.task	    = task;
590877108e4SArnaldo Carvalho de Melo 	attr.watermark	    = watermark;
591877108e4SArnaldo Carvalho de Melo 	attr.precise_ip	    = precise_ip;
592877108e4SArnaldo Carvalho de Melo 	attr.mmap_data	    = mmap_data;
593877108e4SArnaldo Carvalho de Melo 	attr.sample_id_all  = sample_id_all;
594877108e4SArnaldo Carvalho de Melo 
595877108e4SArnaldo Carvalho de Melo 	perf_evsel__init(&pevsel->evsel, &attr, idx);
596877108e4SArnaldo Carvalho de Melo 	return 0;
597877108e4SArnaldo Carvalho de Melo }
598877108e4SArnaldo Carvalho de Melo 
599877108e4SArnaldo Carvalho de Melo static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
600877108e4SArnaldo Carvalho de Melo {
601877108e4SArnaldo Carvalho de Melo 	perf_evsel__exit(&pevsel->evsel);
602877108e4SArnaldo Carvalho de Melo 	pevsel->ob_type->tp_free((PyObject*)pevsel);
603877108e4SArnaldo Carvalho de Melo }
604877108e4SArnaldo Carvalho de Melo 
605877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
606877108e4SArnaldo Carvalho de Melo 				  PyObject *args, PyObject *kwargs)
607877108e4SArnaldo Carvalho de Melo {
608877108e4SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = &pevsel->evsel;
609877108e4SArnaldo Carvalho de Melo 	struct cpu_map *cpus = NULL;
610877108e4SArnaldo Carvalho de Melo 	struct thread_map *threads = NULL;
611877108e4SArnaldo Carvalho de Melo 	PyObject *pcpus = NULL, *pthreads = NULL;
6125d2cd909SArnaldo Carvalho de Melo 	int group = 0, inherit = 0;
61364348153SFrederic Weisbecker 	static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
614877108e4SArnaldo Carvalho de Melo 
615877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
6165d2cd909SArnaldo Carvalho de Melo 					 &pcpus, &pthreads, &group, &inherit))
617877108e4SArnaldo Carvalho de Melo 		return NULL;
618877108e4SArnaldo Carvalho de Melo 
619877108e4SArnaldo Carvalho de Melo 	if (pthreads != NULL)
620877108e4SArnaldo Carvalho de Melo 		threads = ((struct pyrf_thread_map *)pthreads)->threads;
621877108e4SArnaldo Carvalho de Melo 
622877108e4SArnaldo Carvalho de Melo 	if (pcpus != NULL)
623877108e4SArnaldo Carvalho de Melo 		cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
624877108e4SArnaldo Carvalho de Melo 
6255d2cd909SArnaldo Carvalho de Melo 	evsel->attr.inherit = inherit;
626727ab04eSArnaldo Carvalho de Melo 	/*
627727ab04eSArnaldo Carvalho de Melo 	 * This will group just the fds for this single evsel, to group
628727ab04eSArnaldo Carvalho de Melo 	 * multiple events, use evlist.open().
629727ab04eSArnaldo Carvalho de Melo 	 */
6306a4bb04cSJiri Olsa 	if (perf_evsel__open(evsel, cpus, threads) < 0) {
631877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
632877108e4SArnaldo Carvalho de Melo 		return NULL;
633877108e4SArnaldo Carvalho de Melo 	}
634877108e4SArnaldo Carvalho de Melo 
635877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
636877108e4SArnaldo Carvalho de Melo 	return Py_None;
637877108e4SArnaldo Carvalho de Melo }
638877108e4SArnaldo Carvalho de Melo 
639877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evsel__methods[] = {
640877108e4SArnaldo Carvalho de Melo 	{
641877108e4SArnaldo Carvalho de Melo 		.ml_name  = "open",
642877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evsel__open,
643877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
644877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("open the event selector file descriptor table.")
645877108e4SArnaldo Carvalho de Melo 	},
646f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
647877108e4SArnaldo Carvalho de Melo };
648877108e4SArnaldo Carvalho de Melo 
649877108e4SArnaldo Carvalho de Melo static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
650877108e4SArnaldo Carvalho de Melo 
651877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evsel__type = {
652877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
653877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.evsel",
654877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_evsel),
655877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_evsel__delete,
656877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
657877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_evsel__doc,
658877108e4SArnaldo Carvalho de Melo 	.tp_methods	= pyrf_evsel__methods,
659877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_evsel__init,
660877108e4SArnaldo Carvalho de Melo };
661877108e4SArnaldo Carvalho de Melo 
662877108e4SArnaldo Carvalho de Melo static int pyrf_evsel__setup_types(void)
663877108e4SArnaldo Carvalho de Melo {
664877108e4SArnaldo Carvalho de Melo 	pyrf_evsel__type.tp_new = PyType_GenericNew;
665877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_evsel__type);
666877108e4SArnaldo Carvalho de Melo }
667877108e4SArnaldo Carvalho de Melo 
668877108e4SArnaldo Carvalho de Melo struct pyrf_evlist {
669877108e4SArnaldo Carvalho de Melo 	PyObject_HEAD
670877108e4SArnaldo Carvalho de Melo 
671877108e4SArnaldo Carvalho de Melo 	struct perf_evlist evlist;
672877108e4SArnaldo Carvalho de Melo };
673877108e4SArnaldo Carvalho de Melo 
674877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
675f6bbc1daSArnaldo Carvalho de Melo 			     PyObject *args, PyObject *kwargs __used)
676877108e4SArnaldo Carvalho de Melo {
6777e2ed097SArnaldo Carvalho de Melo 	PyObject *pcpus = NULL, *pthreads = NULL;
6787e2ed097SArnaldo Carvalho de Melo 	struct cpu_map *cpus;
6797e2ed097SArnaldo Carvalho de Melo 	struct thread_map *threads;
6807e2ed097SArnaldo Carvalho de Melo 
6817e2ed097SArnaldo Carvalho de Melo 	if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
6827e2ed097SArnaldo Carvalho de Melo 		return -1;
6837e2ed097SArnaldo Carvalho de Melo 
6847e2ed097SArnaldo Carvalho de Melo 	threads = ((struct pyrf_thread_map *)pthreads)->threads;
6857e2ed097SArnaldo Carvalho de Melo 	cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
6867e2ed097SArnaldo Carvalho de Melo 	perf_evlist__init(&pevlist->evlist, cpus, threads);
687877108e4SArnaldo Carvalho de Melo 	return 0;
688877108e4SArnaldo Carvalho de Melo }
689877108e4SArnaldo Carvalho de Melo 
690877108e4SArnaldo Carvalho de Melo static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
691877108e4SArnaldo Carvalho de Melo {
692877108e4SArnaldo Carvalho de Melo 	perf_evlist__exit(&pevlist->evlist);
693877108e4SArnaldo Carvalho de Melo 	pevlist->ob_type->tp_free((PyObject*)pevlist);
694877108e4SArnaldo Carvalho de Melo }
695877108e4SArnaldo Carvalho de Melo 
696877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
697877108e4SArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
698877108e4SArnaldo Carvalho de Melo {
699877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
70064348153SFrederic Weisbecker 	static char *kwlist[] = { "pages", "overwrite", NULL };
701877108e4SArnaldo Carvalho de Melo 	int pages = 128, overwrite = false;
702877108e4SArnaldo Carvalho de Melo 
7037e2ed097SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
7047e2ed097SArnaldo Carvalho de Melo 					 &pages, &overwrite))
705877108e4SArnaldo Carvalho de Melo 		return NULL;
706877108e4SArnaldo Carvalho de Melo 
7077e2ed097SArnaldo Carvalho de Melo 	if (perf_evlist__mmap(evlist, pages, overwrite) < 0) {
708877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
709877108e4SArnaldo Carvalho de Melo 		return NULL;
710877108e4SArnaldo Carvalho de Melo 	}
711877108e4SArnaldo Carvalho de Melo 
712877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
713877108e4SArnaldo Carvalho de Melo 	return Py_None;
714877108e4SArnaldo Carvalho de Melo }
715877108e4SArnaldo Carvalho de Melo 
716877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
717877108e4SArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
718877108e4SArnaldo Carvalho de Melo {
719877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
72064348153SFrederic Weisbecker 	static char *kwlist[] = { "timeout", NULL };
721877108e4SArnaldo Carvalho de Melo 	int timeout = -1, n;
722877108e4SArnaldo Carvalho de Melo 
723877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
724877108e4SArnaldo Carvalho de Melo 		return NULL;
725877108e4SArnaldo Carvalho de Melo 
726877108e4SArnaldo Carvalho de Melo 	n = poll(evlist->pollfd, evlist->nr_fds, timeout);
727877108e4SArnaldo Carvalho de Melo 	if (n < 0) {
728877108e4SArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
729877108e4SArnaldo Carvalho de Melo 		return NULL;
730877108e4SArnaldo Carvalho de Melo 	}
731877108e4SArnaldo Carvalho de Melo 
732877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", n);
733877108e4SArnaldo Carvalho de Melo }
734877108e4SArnaldo Carvalho de Melo 
735877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
736f6bbc1daSArnaldo Carvalho de Melo 					 PyObject *args __used, PyObject *kwargs __used)
737877108e4SArnaldo Carvalho de Melo {
738877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
739877108e4SArnaldo Carvalho de Melo         PyObject *list = PyList_New(0);
740877108e4SArnaldo Carvalho de Melo 	int i;
741877108e4SArnaldo Carvalho de Melo 
742877108e4SArnaldo Carvalho de Melo 	for (i = 0; i < evlist->nr_fds; ++i) {
743877108e4SArnaldo Carvalho de Melo 		PyObject *file;
744877108e4SArnaldo Carvalho de Melo 		FILE *fp = fdopen(evlist->pollfd[i].fd, "r");
745877108e4SArnaldo Carvalho de Melo 
746877108e4SArnaldo Carvalho de Melo 		if (fp == NULL)
747877108e4SArnaldo Carvalho de Melo 			goto free_list;
748877108e4SArnaldo Carvalho de Melo 
749877108e4SArnaldo Carvalho de Melo 		file = PyFile_FromFile(fp, "perf", "r", NULL);
750877108e4SArnaldo Carvalho de Melo 		if (file == NULL)
751877108e4SArnaldo Carvalho de Melo 			goto free_list;
752877108e4SArnaldo Carvalho de Melo 
753877108e4SArnaldo Carvalho de Melo 		if (PyList_Append(list, file) != 0) {
754877108e4SArnaldo Carvalho de Melo 			Py_DECREF(file);
755877108e4SArnaldo Carvalho de Melo 			goto free_list;
756877108e4SArnaldo Carvalho de Melo 		}
757877108e4SArnaldo Carvalho de Melo 
758877108e4SArnaldo Carvalho de Melo 		Py_DECREF(file);
759877108e4SArnaldo Carvalho de Melo 	}
760877108e4SArnaldo Carvalho de Melo 
761877108e4SArnaldo Carvalho de Melo 	return list;
762877108e4SArnaldo Carvalho de Melo free_list:
763877108e4SArnaldo Carvalho de Melo 	return PyErr_NoMemory();
764877108e4SArnaldo Carvalho de Melo }
765877108e4SArnaldo Carvalho de Melo 
766877108e4SArnaldo Carvalho de Melo 
767877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
768f6bbc1daSArnaldo Carvalho de Melo 				  PyObject *args, PyObject *kwargs __used)
769877108e4SArnaldo Carvalho de Melo {
770877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
771877108e4SArnaldo Carvalho de Melo 	PyObject *pevsel;
772877108e4SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
773877108e4SArnaldo Carvalho de Melo 
774877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTuple(args, "O", &pevsel))
775877108e4SArnaldo Carvalho de Melo 		return NULL;
776877108e4SArnaldo Carvalho de Melo 
777877108e4SArnaldo Carvalho de Melo 	Py_INCREF(pevsel);
778877108e4SArnaldo Carvalho de Melo 	evsel = &((struct pyrf_evsel *)pevsel)->evsel;
779877108e4SArnaldo Carvalho de Melo 	evsel->idx = evlist->nr_entries;
780877108e4SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
781877108e4SArnaldo Carvalho de Melo 
782877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("i", evlist->nr_entries);
783877108e4SArnaldo Carvalho de Melo }
784877108e4SArnaldo Carvalho de Melo 
785877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
786877108e4SArnaldo Carvalho de Melo 					  PyObject *args, PyObject *kwargs)
787877108e4SArnaldo Carvalho de Melo {
788877108e4SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
789877108e4SArnaldo Carvalho de Melo 	union perf_event *event;
790877108e4SArnaldo Carvalho de Melo 	int sample_id_all = 1, cpu;
79164348153SFrederic Weisbecker 	static char *kwlist[] = { "cpu", "sample_id_all", NULL };
7925538becaSFrederic Weisbecker 	int err;
793877108e4SArnaldo Carvalho de Melo 
794877108e4SArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
795877108e4SArnaldo Carvalho de Melo 					 &cpu, &sample_id_all))
796877108e4SArnaldo Carvalho de Melo 		return NULL;
797877108e4SArnaldo Carvalho de Melo 
798aece948fSArnaldo Carvalho de Melo 	event = perf_evlist__mmap_read(evlist, cpu);
799877108e4SArnaldo Carvalho de Melo 	if (event != NULL) {
800877108e4SArnaldo Carvalho de Melo 		PyObject *pyevent = pyrf_event__new(event);
801877108e4SArnaldo Carvalho de Melo 		struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
802877108e4SArnaldo Carvalho de Melo 
803877108e4SArnaldo Carvalho de Melo 		if (pyevent == NULL)
804877108e4SArnaldo Carvalho de Melo 			return PyErr_NoMemory();
805877108e4SArnaldo Carvalho de Melo 
806cb0b29e0SArnaldo Carvalho de Melo 		err = perf_evlist__parse_sample(evlist, event, &pevent->sample, false);
8075c6970afSArnaldo Carvalho de Melo 		if (err)
8085c6970afSArnaldo Carvalho de Melo 			return PyErr_Format(PyExc_OSError,
8095c6970afSArnaldo Carvalho de Melo 					    "perf: can't parse sample, err=%d", err);
8105538becaSFrederic Weisbecker 		return pyevent;
8115538becaSFrederic Weisbecker 	}
8125c6970afSArnaldo Carvalho de Melo 
813877108e4SArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
814877108e4SArnaldo Carvalho de Melo 	return Py_None;
815877108e4SArnaldo Carvalho de Melo }
816877108e4SArnaldo Carvalho de Melo 
817727ab04eSArnaldo Carvalho de Melo static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
818727ab04eSArnaldo Carvalho de Melo 				   PyObject *args, PyObject *kwargs)
819727ab04eSArnaldo Carvalho de Melo {
820727ab04eSArnaldo Carvalho de Melo 	struct perf_evlist *evlist = &pevlist->evlist;
821727ab04eSArnaldo Carvalho de Melo 	int group = 0;
822727ab04eSArnaldo Carvalho de Melo 	static char *kwlist[] = { "group", NULL };
823727ab04eSArnaldo Carvalho de Melo 
824727ab04eSArnaldo Carvalho de Melo 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
825727ab04eSArnaldo Carvalho de Melo 		return NULL;
826727ab04eSArnaldo Carvalho de Melo 
8276a4bb04cSJiri Olsa 	if (group)
828*63dab225SArnaldo Carvalho de Melo 		perf_evlist__set_leader(evlist);
8296a4bb04cSJiri Olsa 
8306a4bb04cSJiri Olsa 	if (perf_evlist__open(evlist) < 0) {
831727ab04eSArnaldo Carvalho de Melo 		PyErr_SetFromErrno(PyExc_OSError);
832727ab04eSArnaldo Carvalho de Melo 		return NULL;
833727ab04eSArnaldo Carvalho de Melo 	}
834727ab04eSArnaldo Carvalho de Melo 
835727ab04eSArnaldo Carvalho de Melo 	Py_INCREF(Py_None);
836727ab04eSArnaldo Carvalho de Melo 	return Py_None;
837727ab04eSArnaldo Carvalho de Melo }
838727ab04eSArnaldo Carvalho de Melo 
839877108e4SArnaldo Carvalho de Melo static PyMethodDef pyrf_evlist__methods[] = {
840877108e4SArnaldo Carvalho de Melo 	{
841877108e4SArnaldo Carvalho de Melo 		.ml_name  = "mmap",
842877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__mmap,
843877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
844877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("mmap the file descriptor table.")
845877108e4SArnaldo Carvalho de Melo 	},
846877108e4SArnaldo Carvalho de Melo 	{
847727ab04eSArnaldo Carvalho de Melo 		.ml_name  = "open",
848727ab04eSArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__open,
849727ab04eSArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
850727ab04eSArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("open the file descriptors.")
851727ab04eSArnaldo Carvalho de Melo 	},
852727ab04eSArnaldo Carvalho de Melo 	{
853877108e4SArnaldo Carvalho de Melo 		.ml_name  = "poll",
854877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__poll,
855877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
856877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("poll the file descriptor table.")
857877108e4SArnaldo Carvalho de Melo 	},
858877108e4SArnaldo Carvalho de Melo 	{
859877108e4SArnaldo Carvalho de Melo 		.ml_name  = "get_pollfd",
860877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__get_pollfd,
861877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
862877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("get the poll file descriptor table.")
863877108e4SArnaldo Carvalho de Melo 	},
864877108e4SArnaldo Carvalho de Melo 	{
865877108e4SArnaldo Carvalho de Melo 		.ml_name  = "add",
866877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__add,
867877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
868877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("adds an event selector to the list.")
869877108e4SArnaldo Carvalho de Melo 	},
870877108e4SArnaldo Carvalho de Melo 	{
871877108e4SArnaldo Carvalho de Melo 		.ml_name  = "read_on_cpu",
872877108e4SArnaldo Carvalho de Melo 		.ml_meth  = (PyCFunction)pyrf_evlist__read_on_cpu,
873877108e4SArnaldo Carvalho de Melo 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
874877108e4SArnaldo Carvalho de Melo 		.ml_doc	  = PyDoc_STR("reads an event.")
875877108e4SArnaldo Carvalho de Melo 	},
876f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
877877108e4SArnaldo Carvalho de Melo };
878877108e4SArnaldo Carvalho de Melo 
879877108e4SArnaldo Carvalho de Melo static Py_ssize_t pyrf_evlist__length(PyObject *obj)
880877108e4SArnaldo Carvalho de Melo {
881877108e4SArnaldo Carvalho de Melo 	struct pyrf_evlist *pevlist = (void *)obj;
882877108e4SArnaldo Carvalho de Melo 
883877108e4SArnaldo Carvalho de Melo 	return pevlist->evlist.nr_entries;
884877108e4SArnaldo Carvalho de Melo }
885877108e4SArnaldo Carvalho de Melo 
886877108e4SArnaldo Carvalho de Melo static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
887877108e4SArnaldo Carvalho de Melo {
888877108e4SArnaldo Carvalho de Melo 	struct pyrf_evlist *pevlist = (void *)obj;
889877108e4SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
890877108e4SArnaldo Carvalho de Melo 
891877108e4SArnaldo Carvalho de Melo 	if (i >= pevlist->evlist.nr_entries)
892877108e4SArnaldo Carvalho de Melo 		return NULL;
893877108e4SArnaldo Carvalho de Melo 
894877108e4SArnaldo Carvalho de Melo 	list_for_each_entry(pos, &pevlist->evlist.entries, node)
895877108e4SArnaldo Carvalho de Melo 		if (i-- == 0)
896877108e4SArnaldo Carvalho de Melo 			break;
897877108e4SArnaldo Carvalho de Melo 
898877108e4SArnaldo Carvalho de Melo 	return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
899877108e4SArnaldo Carvalho de Melo }
900877108e4SArnaldo Carvalho de Melo 
901877108e4SArnaldo Carvalho de Melo static PySequenceMethods pyrf_evlist__sequence_methods = {
902877108e4SArnaldo Carvalho de Melo 	.sq_length = pyrf_evlist__length,
903877108e4SArnaldo Carvalho de Melo 	.sq_item   = pyrf_evlist__item,
904877108e4SArnaldo Carvalho de Melo };
905877108e4SArnaldo Carvalho de Melo 
906877108e4SArnaldo Carvalho de Melo static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
907877108e4SArnaldo Carvalho de Melo 
908877108e4SArnaldo Carvalho de Melo static PyTypeObject pyrf_evlist__type = {
909877108e4SArnaldo Carvalho de Melo 	PyVarObject_HEAD_INIT(NULL, 0)
910877108e4SArnaldo Carvalho de Melo 	.tp_name	= "perf.evlist",
911877108e4SArnaldo Carvalho de Melo 	.tp_basicsize	= sizeof(struct pyrf_evlist),
912877108e4SArnaldo Carvalho de Melo 	.tp_dealloc	= (destructor)pyrf_evlist__delete,
913877108e4SArnaldo Carvalho de Melo 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
914877108e4SArnaldo Carvalho de Melo 	.tp_as_sequence	= &pyrf_evlist__sequence_methods,
915877108e4SArnaldo Carvalho de Melo 	.tp_doc		= pyrf_evlist__doc,
916877108e4SArnaldo Carvalho de Melo 	.tp_methods	= pyrf_evlist__methods,
917877108e4SArnaldo Carvalho de Melo 	.tp_init	= (initproc)pyrf_evlist__init,
918877108e4SArnaldo Carvalho de Melo };
919877108e4SArnaldo Carvalho de Melo 
920877108e4SArnaldo Carvalho de Melo static int pyrf_evlist__setup_types(void)
921877108e4SArnaldo Carvalho de Melo {
922877108e4SArnaldo Carvalho de Melo 	pyrf_evlist__type.tp_new = PyType_GenericNew;
923877108e4SArnaldo Carvalho de Melo 	return PyType_Ready(&pyrf_evlist__type);
924877108e4SArnaldo Carvalho de Melo }
925877108e4SArnaldo Carvalho de Melo 
926877108e4SArnaldo Carvalho de Melo static struct {
927877108e4SArnaldo Carvalho de Melo 	const char *name;
928877108e4SArnaldo Carvalho de Melo 	int	    value;
929877108e4SArnaldo Carvalho de Melo } perf__constants[] = {
930877108e4SArnaldo Carvalho de Melo 	{ "TYPE_HARDWARE",   PERF_TYPE_HARDWARE },
931877108e4SArnaldo Carvalho de Melo 	{ "TYPE_SOFTWARE",   PERF_TYPE_SOFTWARE },
932877108e4SArnaldo Carvalho de Melo 	{ "TYPE_TRACEPOINT", PERF_TYPE_TRACEPOINT },
933877108e4SArnaldo Carvalho de Melo 	{ "TYPE_HW_CACHE",   PERF_TYPE_HW_CACHE },
934877108e4SArnaldo Carvalho de Melo 	{ "TYPE_RAW",	     PERF_TYPE_RAW },
935877108e4SArnaldo Carvalho de Melo 	{ "TYPE_BREAKPOINT", PERF_TYPE_BREAKPOINT },
936877108e4SArnaldo Carvalho de Melo 
937877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CPU_CYCLES",	  PERF_COUNT_HW_CPU_CYCLES },
938877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_INSTRUCTIONS",	  PERF_COUNT_HW_INSTRUCTIONS },
939877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_REFERENCES",	  PERF_COUNT_HW_CACHE_REFERENCES },
940877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_MISSES",	  PERF_COUNT_HW_CACHE_MISSES },
941877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_BRANCH_INSTRUCTIONS", PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
942877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_BRANCH_MISSES",	  PERF_COUNT_HW_BRANCH_MISSES },
943877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_BUS_CYCLES",	  PERF_COUNT_HW_BUS_CYCLES },
944877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_L1D",		  PERF_COUNT_HW_CACHE_L1D },
945877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_L1I",		  PERF_COUNT_HW_CACHE_L1I },
946877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_LL",	  	  PERF_COUNT_HW_CACHE_LL },
947877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_DTLB",	  PERF_COUNT_HW_CACHE_DTLB },
948877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_ITLB",	  PERF_COUNT_HW_CACHE_ITLB },
949877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_BPU",		  PERF_COUNT_HW_CACHE_BPU },
950877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_OP_READ",	  PERF_COUNT_HW_CACHE_OP_READ },
951877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_OP_WRITE",	  PERF_COUNT_HW_CACHE_OP_WRITE },
952877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_OP_PREFETCH",	  PERF_COUNT_HW_CACHE_OP_PREFETCH },
953877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_RESULT_ACCESS", PERF_COUNT_HW_CACHE_RESULT_ACCESS },
954877108e4SArnaldo Carvalho de Melo 	{ "COUNT_HW_CACHE_RESULT_MISS",   PERF_COUNT_HW_CACHE_RESULT_MISS },
955877108e4SArnaldo Carvalho de Melo 
956129c04cbSIngo Molnar 	{ "COUNT_HW_STALLED_CYCLES_FRONTEND",	  PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
957129c04cbSIngo Molnar 	{ "COUNT_HW_STALLED_CYCLES_BACKEND",	  PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
958129c04cbSIngo Molnar 
959877108e4SArnaldo Carvalho de Melo 	{ "COUNT_SW_CPU_CLOCK",	       PERF_COUNT_SW_CPU_CLOCK },
960877108e4SArnaldo Carvalho de Melo 	{ "COUNT_SW_TASK_CLOCK",       PERF_COUNT_SW_TASK_CLOCK },
961877108e4SArnaldo Carvalho de Melo 	{ "COUNT_SW_PAGE_FAULTS",      PERF_COUNT_SW_PAGE_FAULTS },
962877108e4SArnaldo Carvalho de Melo 	{ "COUNT_SW_CONTEXT_SWITCHES", PERF_COUNT_SW_CONTEXT_SWITCHES },
963877108e4SArnaldo Carvalho de Melo 	{ "COUNT_SW_CPU_MIGRATIONS",   PERF_COUNT_SW_CPU_MIGRATIONS },
964877108e4SArnaldo Carvalho de Melo 	{ "COUNT_SW_PAGE_FAULTS_MIN",  PERF_COUNT_SW_PAGE_FAULTS_MIN },
965877108e4SArnaldo Carvalho de Melo 	{ "COUNT_SW_PAGE_FAULTS_MAJ",  PERF_COUNT_SW_PAGE_FAULTS_MAJ },
966877108e4SArnaldo Carvalho de Melo 	{ "COUNT_SW_ALIGNMENT_FAULTS", PERF_COUNT_SW_ALIGNMENT_FAULTS },
967877108e4SArnaldo Carvalho de Melo 	{ "COUNT_SW_EMULATION_FAULTS", PERF_COUNT_SW_EMULATION_FAULTS },
968877108e4SArnaldo Carvalho de Melo 
969877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_IP",	      PERF_SAMPLE_IP },
970877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_TID",	      PERF_SAMPLE_TID },
971877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_TIME",      PERF_SAMPLE_TIME },
972877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_ADDR",      PERF_SAMPLE_ADDR },
973877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_READ",      PERF_SAMPLE_READ },
974877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_CALLCHAIN", PERF_SAMPLE_CALLCHAIN },
975877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_ID",	      PERF_SAMPLE_ID },
976877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_CPU",	      PERF_SAMPLE_CPU },
977877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_PERIOD",    PERF_SAMPLE_PERIOD },
978877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_STREAM_ID", PERF_SAMPLE_STREAM_ID },
979877108e4SArnaldo Carvalho de Melo 	{ "SAMPLE_RAW",	      PERF_SAMPLE_RAW },
980877108e4SArnaldo Carvalho de Melo 
981877108e4SArnaldo Carvalho de Melo 	{ "FORMAT_TOTAL_TIME_ENABLED", PERF_FORMAT_TOTAL_TIME_ENABLED },
982877108e4SArnaldo Carvalho de Melo 	{ "FORMAT_TOTAL_TIME_RUNNING", PERF_FORMAT_TOTAL_TIME_RUNNING },
983877108e4SArnaldo Carvalho de Melo 	{ "FORMAT_ID",		       PERF_FORMAT_ID },
984877108e4SArnaldo Carvalho de Melo 	{ "FORMAT_GROUP",	       PERF_FORMAT_GROUP },
985877108e4SArnaldo Carvalho de Melo 
986877108e4SArnaldo Carvalho de Melo 	{ "RECORD_MMAP",       PERF_RECORD_MMAP },
987877108e4SArnaldo Carvalho de Melo 	{ "RECORD_LOST",       PERF_RECORD_LOST },
988877108e4SArnaldo Carvalho de Melo 	{ "RECORD_COMM",       PERF_RECORD_COMM },
989877108e4SArnaldo Carvalho de Melo 	{ "RECORD_EXIT",       PERF_RECORD_EXIT },
990877108e4SArnaldo Carvalho de Melo 	{ "RECORD_THROTTLE",   PERF_RECORD_THROTTLE },
991877108e4SArnaldo Carvalho de Melo 	{ "RECORD_UNTHROTTLE", PERF_RECORD_UNTHROTTLE },
992877108e4SArnaldo Carvalho de Melo 	{ "RECORD_FORK",       PERF_RECORD_FORK },
993877108e4SArnaldo Carvalho de Melo 	{ "RECORD_READ",       PERF_RECORD_READ },
994877108e4SArnaldo Carvalho de Melo 	{ "RECORD_SAMPLE",     PERF_RECORD_SAMPLE },
995f6bbc1daSArnaldo Carvalho de Melo 	{ .name = NULL, },
996877108e4SArnaldo Carvalho de Melo };
997877108e4SArnaldo Carvalho de Melo 
998877108e4SArnaldo Carvalho de Melo static PyMethodDef perf__methods[] = {
999f6bbc1daSArnaldo Carvalho de Melo 	{ .ml_name = NULL, }
1000877108e4SArnaldo Carvalho de Melo };
1001877108e4SArnaldo Carvalho de Melo 
1002877108e4SArnaldo Carvalho de Melo PyMODINIT_FUNC initperf(void)
1003877108e4SArnaldo Carvalho de Melo {
1004877108e4SArnaldo Carvalho de Melo 	PyObject *obj;
1005877108e4SArnaldo Carvalho de Melo 	int i;
1006877108e4SArnaldo Carvalho de Melo 	PyObject *dict, *module = Py_InitModule("perf", perf__methods);
1007877108e4SArnaldo Carvalho de Melo 
1008877108e4SArnaldo Carvalho de Melo 	if (module == NULL ||
1009877108e4SArnaldo Carvalho de Melo 	    pyrf_event__setup_types() < 0 ||
1010877108e4SArnaldo Carvalho de Melo 	    pyrf_evlist__setup_types() < 0 ||
1011877108e4SArnaldo Carvalho de Melo 	    pyrf_evsel__setup_types() < 0 ||
1012877108e4SArnaldo Carvalho de Melo 	    pyrf_thread_map__setup_types() < 0 ||
1013877108e4SArnaldo Carvalho de Melo 	    pyrf_cpu_map__setup_types() < 0)
1014877108e4SArnaldo Carvalho de Melo 		return;
1015877108e4SArnaldo Carvalho de Melo 
1016877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_evlist__type);
1017877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1018877108e4SArnaldo Carvalho de Melo 
1019877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_evsel__type);
1020877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1021877108e4SArnaldo Carvalho de Melo 
1022877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_thread_map__type);
1023877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1024877108e4SArnaldo Carvalho de Melo 
1025877108e4SArnaldo Carvalho de Melo 	Py_INCREF(&pyrf_cpu_map__type);
1026877108e4SArnaldo Carvalho de Melo 	PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1027877108e4SArnaldo Carvalho de Melo 
1028877108e4SArnaldo Carvalho de Melo 	dict = PyModule_GetDict(module);
1029877108e4SArnaldo Carvalho de Melo 	if (dict == NULL)
1030877108e4SArnaldo Carvalho de Melo 		goto error;
1031877108e4SArnaldo Carvalho de Melo 
1032877108e4SArnaldo Carvalho de Melo 	for (i = 0; perf__constants[i].name != NULL; i++) {
1033877108e4SArnaldo Carvalho de Melo 		obj = PyInt_FromLong(perf__constants[i].value);
1034877108e4SArnaldo Carvalho de Melo 		if (obj == NULL)
1035877108e4SArnaldo Carvalho de Melo 			goto error;
1036877108e4SArnaldo Carvalho de Melo 		PyDict_SetItemString(dict, perf__constants[i].name, obj);
1037877108e4SArnaldo Carvalho de Melo 		Py_DECREF(obj);
1038877108e4SArnaldo Carvalho de Melo 	}
1039877108e4SArnaldo Carvalho de Melo 
1040877108e4SArnaldo Carvalho de Melo error:
1041877108e4SArnaldo Carvalho de Melo 	if (PyErr_Occurred())
1042877108e4SArnaldo Carvalho de Melo 		PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1043877108e4SArnaldo Carvalho de Melo }
1044