xref: /linux-6.15/tools/perf/util/python.c (revision 16ecb431)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <Python.h>
3 #include <structmember.h>
4 #include <inttypes.h>
5 #include <poll.h>
6 #include <linux/err.h>
7 #include <perf/cpumap.h>
8 #ifdef HAVE_LIBTRACEEVENT
9 #include <event-parse.h>
10 #endif
11 #include <perf/mmap.h>
12 #include "evlist.h"
13 #include "evsel.h"
14 #include "event.h"
15 #include "print_binary.h"
16 #include "thread_map.h"
17 #include "trace-event.h"
18 #include "mmap.h"
19 #include "util/kwork.h"
20 #include "util/sample.h"
21 #include "util/lock-contention.h"
22 #include <internal/lib.h>
23 #include "../builtin.h"
24 
25 #define _PyUnicode_FromString(arg) \
26   PyUnicode_FromString(arg)
27 #define _PyUnicode_FromFormat(...) \
28   PyUnicode_FromFormat(__VA_ARGS__)
29 #define _PyLong_FromLong(arg) \
30   PyLong_FromLong(arg)
31 
32 PyMODINIT_FUNC PyInit_perf(void);
33 
34 #define member_def(type, member, ptype, help) \
35 	{ #member, ptype, \
36 	  offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
37 	  0, help }
38 
39 #define sample_member_def(name, member, ptype, help) \
40 	{ #name, ptype, \
41 	  offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
42 	  0, help }
43 
44 struct pyrf_event {
45 	PyObject_HEAD
46 	struct evsel *evsel;
47 	struct perf_sample sample;
48 	union perf_event   event;
49 };
50 
51 #define sample_members \
52 	sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"),			 \
53 	sample_member_def(sample_pid, pid, T_INT, "event pid"),			 \
54 	sample_member_def(sample_tid, tid, T_INT, "event tid"),			 \
55 	sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"),		 \
56 	sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"),		 \
57 	sample_member_def(sample_id, id, T_ULONGLONG, "event id"),			 \
58 	sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
59 	sample_member_def(sample_period, period, T_ULONGLONG, "event period"),		 \
60 	sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
61 
62 static const char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
63 
64 static PyMemberDef pyrf_mmap_event__members[] = {
65 	sample_members
66 	member_def(perf_event_header, type, T_UINT, "event type"),
67 	member_def(perf_event_header, misc, T_UINT, "event misc"),
68 	member_def(perf_record_mmap, pid, T_UINT, "event pid"),
69 	member_def(perf_record_mmap, tid, T_UINT, "event tid"),
70 	member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
71 	member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
72 	member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
73 	member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
74 	{ .name = NULL, },
75 };
76 
77 static PyObject *pyrf_mmap_event__repr(const struct pyrf_event *pevent)
78 {
79 	PyObject *ret;
80 	char *s;
81 
82 	if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
83 			 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
84 			 "filename: %s }",
85 		     pevent->event.mmap.pid, pevent->event.mmap.tid,
86 		     pevent->event.mmap.start, pevent->event.mmap.len,
87 		     pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
88 		ret = PyErr_NoMemory();
89 	} else {
90 		ret = PyUnicode_FromString(s);
91 		free(s);
92 	}
93 	return ret;
94 }
95 
96 static PyTypeObject pyrf_mmap_event__type = {
97 	PyVarObject_HEAD_INIT(NULL, 0)
98 	.tp_name	= "perf.mmap_event",
99 	.tp_basicsize	= sizeof(struct pyrf_event),
100 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
101 	.tp_doc		= pyrf_mmap_event__doc,
102 	.tp_members	= pyrf_mmap_event__members,
103 	.tp_repr	= (reprfunc)pyrf_mmap_event__repr,
104 };
105 
106 static const char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
107 
108 static PyMemberDef pyrf_task_event__members[] = {
109 	sample_members
110 	member_def(perf_event_header, type, T_UINT, "event type"),
111 	member_def(perf_record_fork, pid, T_UINT, "event pid"),
112 	member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
113 	member_def(perf_record_fork, tid, T_UINT, "event tid"),
114 	member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
115 	member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
116 	{ .name = NULL, },
117 };
118 
119 static PyObject *pyrf_task_event__repr(const struct pyrf_event *pevent)
120 {
121 	return PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
122 				   "ptid: %u, time: %" PRI_lu64 "}",
123 				   pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
124 				   pevent->event.fork.pid,
125 				   pevent->event.fork.ppid,
126 				   pevent->event.fork.tid,
127 				   pevent->event.fork.ptid,
128 				   pevent->event.fork.time);
129 }
130 
131 static PyTypeObject pyrf_task_event__type = {
132 	PyVarObject_HEAD_INIT(NULL, 0)
133 	.tp_name	= "perf.task_event",
134 	.tp_basicsize	= sizeof(struct pyrf_event),
135 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
136 	.tp_doc		= pyrf_task_event__doc,
137 	.tp_members	= pyrf_task_event__members,
138 	.tp_repr	= (reprfunc)pyrf_task_event__repr,
139 };
140 
141 static const char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
142 
143 static PyMemberDef pyrf_comm_event__members[] = {
144 	sample_members
145 	member_def(perf_event_header, type, T_UINT, "event type"),
146 	member_def(perf_record_comm, pid, T_UINT, "event pid"),
147 	member_def(perf_record_comm, tid, T_UINT, "event tid"),
148 	member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
149 	{ .name = NULL, },
150 };
151 
152 static PyObject *pyrf_comm_event__repr(const struct pyrf_event *pevent)
153 {
154 	return PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
155 				   pevent->event.comm.pid,
156 				   pevent->event.comm.tid,
157 				   pevent->event.comm.comm);
158 }
159 
160 static PyTypeObject pyrf_comm_event__type = {
161 	PyVarObject_HEAD_INIT(NULL, 0)
162 	.tp_name	= "perf.comm_event",
163 	.tp_basicsize	= sizeof(struct pyrf_event),
164 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
165 	.tp_doc		= pyrf_comm_event__doc,
166 	.tp_members	= pyrf_comm_event__members,
167 	.tp_repr	= (reprfunc)pyrf_comm_event__repr,
168 };
169 
170 static const char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
171 
172 static PyMemberDef pyrf_throttle_event__members[] = {
173 	sample_members
174 	member_def(perf_event_header, type, T_UINT, "event type"),
175 	member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
176 	member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
177 	member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
178 	{ .name = NULL, },
179 };
180 
181 static PyObject *pyrf_throttle_event__repr(const struct pyrf_event *pevent)
182 {
183 	const struct perf_record_throttle *te = (const struct perf_record_throttle *)
184 		(&pevent->event.header + 1);
185 
186 	return PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
187 				   ", stream_id: %" PRI_lu64 " }",
188 				   pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
189 				   te->time, te->id, te->stream_id);
190 }
191 
192 static PyTypeObject pyrf_throttle_event__type = {
193 	PyVarObject_HEAD_INIT(NULL, 0)
194 	.tp_name	= "perf.throttle_event",
195 	.tp_basicsize	= sizeof(struct pyrf_event),
196 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
197 	.tp_doc		= pyrf_throttle_event__doc,
198 	.tp_members	= pyrf_throttle_event__members,
199 	.tp_repr	= (reprfunc)pyrf_throttle_event__repr,
200 };
201 
202 static const char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
203 
204 static PyMemberDef pyrf_lost_event__members[] = {
205 	sample_members
206 	member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
207 	member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
208 	{ .name = NULL, },
209 };
210 
211 static PyObject *pyrf_lost_event__repr(const struct pyrf_event *pevent)
212 {
213 	PyObject *ret;
214 	char *s;
215 
216 	if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
217 			 "lost: %#" PRI_lx64 " }",
218 		     pevent->event.lost.id, pevent->event.lost.lost) < 0) {
219 		ret = PyErr_NoMemory();
220 	} else {
221 		ret = PyUnicode_FromString(s);
222 		free(s);
223 	}
224 	return ret;
225 }
226 
227 static PyTypeObject pyrf_lost_event__type = {
228 	PyVarObject_HEAD_INIT(NULL, 0)
229 	.tp_name	= "perf.lost_event",
230 	.tp_basicsize	= sizeof(struct pyrf_event),
231 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
232 	.tp_doc		= pyrf_lost_event__doc,
233 	.tp_members	= pyrf_lost_event__members,
234 	.tp_repr	= (reprfunc)pyrf_lost_event__repr,
235 };
236 
237 static const char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
238 
239 static PyMemberDef pyrf_read_event__members[] = {
240 	sample_members
241 	member_def(perf_record_read, pid, T_UINT, "event pid"),
242 	member_def(perf_record_read, tid, T_UINT, "event tid"),
243 	{ .name = NULL, },
244 };
245 
246 static PyObject *pyrf_read_event__repr(const struct pyrf_event *pevent)
247 {
248 	return PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
249 				   pevent->event.read.pid,
250 				   pevent->event.read.tid);
251 	/*
252  	 * FIXME: return the array of read values,
253  	 * making this method useful ;-)
254  	 */
255 }
256 
257 static PyTypeObject pyrf_read_event__type = {
258 	PyVarObject_HEAD_INIT(NULL, 0)
259 	.tp_name	= "perf.read_event",
260 	.tp_basicsize	= sizeof(struct pyrf_event),
261 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
262 	.tp_doc		= pyrf_read_event__doc,
263 	.tp_members	= pyrf_read_event__members,
264 	.tp_repr	= (reprfunc)pyrf_read_event__repr,
265 };
266 
267 static const char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
268 
269 static PyMemberDef pyrf_sample_event__members[] = {
270 	sample_members
271 	member_def(perf_event_header, type, T_UINT, "event type"),
272 	{ .name = NULL, },
273 };
274 
275 static PyObject *pyrf_sample_event__repr(const struct pyrf_event *pevent)
276 {
277 	PyObject *ret;
278 	char *s;
279 
280 	if (asprintf(&s, "{ type: sample }") < 0) {
281 		ret = PyErr_NoMemory();
282 	} else {
283 		ret = PyUnicode_FromString(s);
284 		free(s);
285 	}
286 	return ret;
287 }
288 
289 #ifdef HAVE_LIBTRACEEVENT
290 static bool is_tracepoint(const struct pyrf_event *pevent)
291 {
292 	return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
293 }
294 
295 static PyObject*
296 tracepoint_field(const struct pyrf_event *pe, struct tep_format_field *field)
297 {
298 	struct tep_handle *pevent = field->event->tep;
299 	void *data = pe->sample.raw_data;
300 	PyObject *ret = NULL;
301 	unsigned long long val;
302 	unsigned int offset, len;
303 
304 	if (field->flags & TEP_FIELD_IS_ARRAY) {
305 		offset = field->offset;
306 		len    = field->size;
307 		if (field->flags & TEP_FIELD_IS_DYNAMIC) {
308 			val     = tep_read_number(pevent, data + offset, len);
309 			offset  = val;
310 			len     = offset >> 16;
311 			offset &= 0xffff;
312 			if (tep_field_is_relative(field->flags))
313 				offset += field->offset + field->size;
314 		}
315 		if (field->flags & TEP_FIELD_IS_STRING &&
316 		    is_printable_array(data + offset, len)) {
317 			ret = PyUnicode_FromString((char *)data + offset);
318 		} else {
319 			ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
320 			field->flags &= ~TEP_FIELD_IS_STRING;
321 		}
322 	} else {
323 		val = tep_read_number(pevent, data + field->offset,
324 				      field->size);
325 		if (field->flags & TEP_FIELD_IS_POINTER)
326 			ret = PyLong_FromUnsignedLong((unsigned long) val);
327 		else if (field->flags & TEP_FIELD_IS_SIGNED)
328 			ret = PyLong_FromLong((long) val);
329 		else
330 			ret = PyLong_FromUnsignedLong((unsigned long) val);
331 	}
332 
333 	return ret;
334 }
335 
336 static PyObject*
337 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
338 {
339 	const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
340 	struct evsel *evsel = pevent->evsel;
341 	struct tep_format_field *field;
342 
343 	if (!evsel->tp_format) {
344 		struct tep_event *tp_format;
345 
346 		tp_format = trace_event__tp_format_id(evsel->core.attr.config);
347 		if (IS_ERR_OR_NULL(tp_format))
348 			return NULL;
349 
350 		evsel->tp_format = tp_format;
351 	}
352 
353 	field = tep_find_any_field(evsel->tp_format, str);
354 	if (!field)
355 		return NULL;
356 
357 	return tracepoint_field(pevent, field);
358 }
359 #endif /* HAVE_LIBTRACEEVENT */
360 
361 static PyObject*
362 pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
363 {
364 	PyObject *obj = NULL;
365 
366 #ifdef HAVE_LIBTRACEEVENT
367 	if (is_tracepoint(pevent))
368 		obj = get_tracepoint_field(pevent, attr_name);
369 #endif
370 
371 	return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
372 }
373 
374 static PyTypeObject pyrf_sample_event__type = {
375 	PyVarObject_HEAD_INIT(NULL, 0)
376 	.tp_name	= "perf.sample_event",
377 	.tp_basicsize	= sizeof(struct pyrf_event),
378 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
379 	.tp_doc		= pyrf_sample_event__doc,
380 	.tp_members	= pyrf_sample_event__members,
381 	.tp_repr	= (reprfunc)pyrf_sample_event__repr,
382 	.tp_getattro	= (getattrofunc) pyrf_sample_event__getattro,
383 };
384 
385 static const char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
386 
387 static PyMemberDef pyrf_context_switch_event__members[] = {
388 	sample_members
389 	member_def(perf_event_header, type, T_UINT, "event type"),
390 	member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
391 	member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
392 	{ .name = NULL, },
393 };
394 
395 static PyObject *pyrf_context_switch_event__repr(const struct pyrf_event *pevent)
396 {
397 	PyObject *ret;
398 	char *s;
399 
400 	if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
401 		     pevent->event.context_switch.next_prev_pid,
402 		     pevent->event.context_switch.next_prev_tid,
403 		     !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
404 		ret = PyErr_NoMemory();
405 	} else {
406 		ret = PyUnicode_FromString(s);
407 		free(s);
408 	}
409 	return ret;
410 }
411 
412 static PyTypeObject pyrf_context_switch_event__type = {
413 	PyVarObject_HEAD_INIT(NULL, 0)
414 	.tp_name	= "perf.context_switch_event",
415 	.tp_basicsize	= sizeof(struct pyrf_event),
416 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
417 	.tp_doc		= pyrf_context_switch_event__doc,
418 	.tp_members	= pyrf_context_switch_event__members,
419 	.tp_repr	= (reprfunc)pyrf_context_switch_event__repr,
420 };
421 
422 static int pyrf_event__setup_types(void)
423 {
424 	int err;
425 	pyrf_mmap_event__type.tp_new =
426 	pyrf_task_event__type.tp_new =
427 	pyrf_comm_event__type.tp_new =
428 	pyrf_lost_event__type.tp_new =
429 	pyrf_read_event__type.tp_new =
430 	pyrf_sample_event__type.tp_new =
431 	pyrf_context_switch_event__type.tp_new =
432 	pyrf_throttle_event__type.tp_new = PyType_GenericNew;
433 	err = PyType_Ready(&pyrf_mmap_event__type);
434 	if (err < 0)
435 		goto out;
436 	err = PyType_Ready(&pyrf_lost_event__type);
437 	if (err < 0)
438 		goto out;
439 	err = PyType_Ready(&pyrf_task_event__type);
440 	if (err < 0)
441 		goto out;
442 	err = PyType_Ready(&pyrf_comm_event__type);
443 	if (err < 0)
444 		goto out;
445 	err = PyType_Ready(&pyrf_throttle_event__type);
446 	if (err < 0)
447 		goto out;
448 	err = PyType_Ready(&pyrf_read_event__type);
449 	if (err < 0)
450 		goto out;
451 	err = PyType_Ready(&pyrf_sample_event__type);
452 	if (err < 0)
453 		goto out;
454 	err = PyType_Ready(&pyrf_context_switch_event__type);
455 	if (err < 0)
456 		goto out;
457 out:
458 	return err;
459 }
460 
461 static PyTypeObject *pyrf_event__type[] = {
462 	[PERF_RECORD_MMAP]	 = &pyrf_mmap_event__type,
463 	[PERF_RECORD_LOST]	 = &pyrf_lost_event__type,
464 	[PERF_RECORD_COMM]	 = &pyrf_comm_event__type,
465 	[PERF_RECORD_EXIT]	 = &pyrf_task_event__type,
466 	[PERF_RECORD_THROTTLE]	 = &pyrf_throttle_event__type,
467 	[PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
468 	[PERF_RECORD_FORK]	 = &pyrf_task_event__type,
469 	[PERF_RECORD_READ]	 = &pyrf_read_event__type,
470 	[PERF_RECORD_SAMPLE]	 = &pyrf_sample_event__type,
471 	[PERF_RECORD_SWITCH]	 = &pyrf_context_switch_event__type,
472 	[PERF_RECORD_SWITCH_CPU_WIDE]  = &pyrf_context_switch_event__type,
473 };
474 
475 static PyObject *pyrf_event__new(const union perf_event *event)
476 {
477 	struct pyrf_event *pevent;
478 	PyTypeObject *ptype;
479 
480 	if ((event->header.type < PERF_RECORD_MMAP ||
481 	     event->header.type > PERF_RECORD_SAMPLE) &&
482 	    !(event->header.type == PERF_RECORD_SWITCH ||
483 	      event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
484 		return NULL;
485 
486 	ptype = pyrf_event__type[event->header.type];
487 	pevent = PyObject_New(struct pyrf_event, ptype);
488 	if (pevent != NULL)
489 		memcpy(&pevent->event, event, event->header.size);
490 	return (PyObject *)pevent;
491 }
492 
493 struct pyrf_cpu_map {
494 	PyObject_HEAD
495 
496 	struct perf_cpu_map *cpus;
497 };
498 
499 static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
500 			      PyObject *args, PyObject *kwargs)
501 {
502 	static char *kwlist[] = { "cpustr", NULL };
503 	char *cpustr = NULL;
504 
505 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
506 					 kwlist, &cpustr))
507 		return -1;
508 
509 	pcpus->cpus = perf_cpu_map__new(cpustr);
510 	if (pcpus->cpus == NULL)
511 		return -1;
512 	return 0;
513 }
514 
515 static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
516 {
517 	perf_cpu_map__put(pcpus->cpus);
518 	Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
519 }
520 
521 static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
522 {
523 	struct pyrf_cpu_map *pcpus = (void *)obj;
524 
525 	return perf_cpu_map__nr(pcpus->cpus);
526 }
527 
528 static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
529 {
530 	struct pyrf_cpu_map *pcpus = (void *)obj;
531 
532 	if (i >= perf_cpu_map__nr(pcpus->cpus))
533 		return NULL;
534 
535 	return Py_BuildValue("i", perf_cpu_map__cpu(pcpus->cpus, i).cpu);
536 }
537 
538 static PySequenceMethods pyrf_cpu_map__sequence_methods = {
539 	.sq_length = pyrf_cpu_map__length,
540 	.sq_item   = pyrf_cpu_map__item,
541 };
542 
543 static const char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
544 
545 static PyTypeObject pyrf_cpu_map__type = {
546 	PyVarObject_HEAD_INIT(NULL, 0)
547 	.tp_name	= "perf.cpu_map",
548 	.tp_basicsize	= sizeof(struct pyrf_cpu_map),
549 	.tp_dealloc	= (destructor)pyrf_cpu_map__delete,
550 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
551 	.tp_doc		= pyrf_cpu_map__doc,
552 	.tp_as_sequence	= &pyrf_cpu_map__sequence_methods,
553 	.tp_init	= (initproc)pyrf_cpu_map__init,
554 };
555 
556 static int pyrf_cpu_map__setup_types(void)
557 {
558 	pyrf_cpu_map__type.tp_new = PyType_GenericNew;
559 	return PyType_Ready(&pyrf_cpu_map__type);
560 }
561 
562 struct pyrf_thread_map {
563 	PyObject_HEAD
564 
565 	struct perf_thread_map *threads;
566 };
567 
568 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
569 				 PyObject *args, PyObject *kwargs)
570 {
571 	static char *kwlist[] = { "pid", "tid", "uid", NULL };
572 	int pid = -1, tid = -1, uid = UINT_MAX;
573 
574 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
575 					 kwlist, &pid, &tid, &uid))
576 		return -1;
577 
578 	pthreads->threads = thread_map__new(pid, tid, uid);
579 	if (pthreads->threads == NULL)
580 		return -1;
581 	return 0;
582 }
583 
584 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
585 {
586 	perf_thread_map__put(pthreads->threads);
587 	Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
588 }
589 
590 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
591 {
592 	struct pyrf_thread_map *pthreads = (void *)obj;
593 
594 	return perf_thread_map__nr(pthreads->threads);
595 }
596 
597 static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
598 {
599 	struct pyrf_thread_map *pthreads = (void *)obj;
600 
601 	if (i >= perf_thread_map__nr(pthreads->threads))
602 		return NULL;
603 
604 	return Py_BuildValue("i", perf_thread_map__pid(pthreads->threads, i));
605 }
606 
607 static PySequenceMethods pyrf_thread_map__sequence_methods = {
608 	.sq_length = pyrf_thread_map__length,
609 	.sq_item   = pyrf_thread_map__item,
610 };
611 
612 static const char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
613 
614 static PyTypeObject pyrf_thread_map__type = {
615 	PyVarObject_HEAD_INIT(NULL, 0)
616 	.tp_name	= "perf.thread_map",
617 	.tp_basicsize	= sizeof(struct pyrf_thread_map),
618 	.tp_dealloc	= (destructor)pyrf_thread_map__delete,
619 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
620 	.tp_doc		= pyrf_thread_map__doc,
621 	.tp_as_sequence	= &pyrf_thread_map__sequence_methods,
622 	.tp_init	= (initproc)pyrf_thread_map__init,
623 };
624 
625 static int pyrf_thread_map__setup_types(void)
626 {
627 	pyrf_thread_map__type.tp_new = PyType_GenericNew;
628 	return PyType_Ready(&pyrf_thread_map__type);
629 }
630 
631 struct pyrf_evsel {
632 	PyObject_HEAD
633 
634 	struct evsel evsel;
635 };
636 
637 static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
638 			    PyObject *args, PyObject *kwargs)
639 {
640 	struct perf_event_attr attr = {
641 		.type = PERF_TYPE_HARDWARE,
642 		.config = PERF_COUNT_HW_CPU_CYCLES,
643 		.sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
644 	};
645 	static char *kwlist[] = {
646 		"type",
647 		"config",
648 		"sample_freq",
649 		"sample_period",
650 		"sample_type",
651 		"read_format",
652 		"disabled",
653 		"inherit",
654 		"pinned",
655 		"exclusive",
656 		"exclude_user",
657 		"exclude_kernel",
658 		"exclude_hv",
659 		"exclude_idle",
660 		"mmap",
661 		"context_switch",
662 		"comm",
663 		"freq",
664 		"inherit_stat",
665 		"enable_on_exec",
666 		"task",
667 		"watermark",
668 		"precise_ip",
669 		"mmap_data",
670 		"sample_id_all",
671 		"wakeup_events",
672 		"bp_type",
673 		"bp_addr",
674 		"bp_len",
675 		 NULL
676 	};
677 	u64 sample_period = 0;
678 	u32 disabled = 0,
679 	    inherit = 0,
680 	    pinned = 0,
681 	    exclusive = 0,
682 	    exclude_user = 0,
683 	    exclude_kernel = 0,
684 	    exclude_hv = 0,
685 	    exclude_idle = 0,
686 	    mmap = 0,
687 	    context_switch = 0,
688 	    comm = 0,
689 	    freq = 1,
690 	    inherit_stat = 0,
691 	    enable_on_exec = 0,
692 	    task = 0,
693 	    watermark = 0,
694 	    precise_ip = 0,
695 	    mmap_data = 0,
696 	    sample_id_all = 1;
697 	int idx = 0;
698 
699 	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
700 					 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
701 					 &attr.type, &attr.config, &attr.sample_freq,
702 					 &sample_period, &attr.sample_type,
703 					 &attr.read_format, &disabled, &inherit,
704 					 &pinned, &exclusive, &exclude_user,
705 					 &exclude_kernel, &exclude_hv, &exclude_idle,
706 					 &mmap, &context_switch, &comm, &freq, &inherit_stat,
707 					 &enable_on_exec, &task, &watermark,
708 					 &precise_ip, &mmap_data, &sample_id_all,
709 					 &attr.wakeup_events, &attr.bp_type,
710 					 &attr.bp_addr, &attr.bp_len, &idx))
711 		return -1;
712 
713 	/* union... */
714 	if (sample_period != 0) {
715 		if (attr.sample_freq != 0)
716 			return -1; /* FIXME: throw right exception */
717 		attr.sample_period = sample_period;
718 	}
719 
720 	/* Bitfields */
721 	attr.disabled	    = disabled;
722 	attr.inherit	    = inherit;
723 	attr.pinned	    = pinned;
724 	attr.exclusive	    = exclusive;
725 	attr.exclude_user   = exclude_user;
726 	attr.exclude_kernel = exclude_kernel;
727 	attr.exclude_hv	    = exclude_hv;
728 	attr.exclude_idle   = exclude_idle;
729 	attr.mmap	    = mmap;
730 	attr.context_switch = context_switch;
731 	attr.comm	    = comm;
732 	attr.freq	    = freq;
733 	attr.inherit_stat   = inherit_stat;
734 	attr.enable_on_exec = enable_on_exec;
735 	attr.task	    = task;
736 	attr.watermark	    = watermark;
737 	attr.precise_ip	    = precise_ip;
738 	attr.mmap_data	    = mmap_data;
739 	attr.sample_id_all  = sample_id_all;
740 	attr.size	    = sizeof(attr);
741 
742 	evsel__init(&pevsel->evsel, &attr, idx);
743 	return 0;
744 }
745 
746 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
747 {
748 	evsel__exit(&pevsel->evsel);
749 	Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
750 }
751 
752 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
753 				  PyObject *args, PyObject *kwargs)
754 {
755 	struct evsel *evsel = &pevsel->evsel;
756 	struct perf_cpu_map *cpus = NULL;
757 	struct perf_thread_map *threads = NULL;
758 	PyObject *pcpus = NULL, *pthreads = NULL;
759 	int group = 0, inherit = 0;
760 	static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
761 
762 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
763 					 &pcpus, &pthreads, &group, &inherit))
764 		return NULL;
765 
766 	if (pthreads != NULL)
767 		threads = ((struct pyrf_thread_map *)pthreads)->threads;
768 
769 	if (pcpus != NULL)
770 		cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
771 
772 	evsel->core.attr.inherit = inherit;
773 	/*
774 	 * This will group just the fds for this single evsel, to group
775 	 * multiple events, use evlist.open().
776 	 */
777 	if (evsel__open(evsel, cpus, threads) < 0) {
778 		PyErr_SetFromErrno(PyExc_OSError);
779 		return NULL;
780 	}
781 
782 	Py_INCREF(Py_None);
783 	return Py_None;
784 }
785 
786 static PyMethodDef pyrf_evsel__methods[] = {
787 	{
788 		.ml_name  = "open",
789 		.ml_meth  = (PyCFunction)pyrf_evsel__open,
790 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
791 		.ml_doc	  = PyDoc_STR("open the event selector file descriptor table.")
792 	},
793 	{ .ml_name = NULL, }
794 };
795 
796 static const char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
797 
798 static PyTypeObject pyrf_evsel__type = {
799 	PyVarObject_HEAD_INIT(NULL, 0)
800 	.tp_name	= "perf.evsel",
801 	.tp_basicsize	= sizeof(struct pyrf_evsel),
802 	.tp_dealloc	= (destructor)pyrf_evsel__delete,
803 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
804 	.tp_doc		= pyrf_evsel__doc,
805 	.tp_methods	= pyrf_evsel__methods,
806 	.tp_init	= (initproc)pyrf_evsel__init,
807 };
808 
809 static int pyrf_evsel__setup_types(void)
810 {
811 	pyrf_evsel__type.tp_new = PyType_GenericNew;
812 	return PyType_Ready(&pyrf_evsel__type);
813 }
814 
815 struct pyrf_evlist {
816 	PyObject_HEAD
817 
818 	struct evlist evlist;
819 };
820 
821 static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
822 			     PyObject *args, PyObject *kwargs __maybe_unused)
823 {
824 	PyObject *pcpus = NULL, *pthreads = NULL;
825 	struct perf_cpu_map *cpus;
826 	struct perf_thread_map *threads;
827 
828 	if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
829 		return -1;
830 
831 	threads = ((struct pyrf_thread_map *)pthreads)->threads;
832 	cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
833 	evlist__init(&pevlist->evlist, cpus, threads);
834 	return 0;
835 }
836 
837 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
838 {
839 	evlist__exit(&pevlist->evlist);
840 	Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
841 }
842 
843 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
844 				   PyObject *args, PyObject *kwargs)
845 {
846 	struct evlist *evlist = &pevlist->evlist;
847 	static char *kwlist[] = { "pages", "overwrite", NULL };
848 	int pages = 128, overwrite = false;
849 
850 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
851 					 &pages, &overwrite))
852 		return NULL;
853 
854 	if (evlist__mmap(evlist, pages) < 0) {
855 		PyErr_SetFromErrno(PyExc_OSError);
856 		return NULL;
857 	}
858 
859 	Py_INCREF(Py_None);
860 	return Py_None;
861 }
862 
863 static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
864 				   PyObject *args, PyObject *kwargs)
865 {
866 	struct evlist *evlist = &pevlist->evlist;
867 	static char *kwlist[] = { "timeout", NULL };
868 	int timeout = -1, n;
869 
870 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
871 		return NULL;
872 
873 	n = evlist__poll(evlist, timeout);
874 	if (n < 0) {
875 		PyErr_SetFromErrno(PyExc_OSError);
876 		return NULL;
877 	}
878 
879 	return Py_BuildValue("i", n);
880 }
881 
882 static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
883 					 PyObject *args __maybe_unused,
884 					 PyObject *kwargs __maybe_unused)
885 {
886 	struct evlist *evlist = &pevlist->evlist;
887         PyObject *list = PyList_New(0);
888 	int i;
889 
890 	for (i = 0; i < evlist->core.pollfd.nr; ++i) {
891 		PyObject *file;
892 		file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1,
893 				     NULL, NULL, NULL, 0);
894 		if (file == NULL)
895 			goto free_list;
896 
897 		if (PyList_Append(list, file) != 0) {
898 			Py_DECREF(file);
899 			goto free_list;
900 		}
901 
902 		Py_DECREF(file);
903 	}
904 
905 	return list;
906 free_list:
907 	return PyErr_NoMemory();
908 }
909 
910 
911 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
912 				  PyObject *args,
913 				  PyObject *kwargs __maybe_unused)
914 {
915 	struct evlist *evlist = &pevlist->evlist;
916 	PyObject *pevsel;
917 	struct evsel *evsel;
918 
919 	if (!PyArg_ParseTuple(args, "O", &pevsel))
920 		return NULL;
921 
922 	Py_INCREF(pevsel);
923 	evsel = &((struct pyrf_evsel *)pevsel)->evsel;
924 	evsel->core.idx = evlist->core.nr_entries;
925 	evlist__add(evlist, evsel);
926 
927 	return Py_BuildValue("i", evlist->core.nr_entries);
928 }
929 
930 static struct mmap *get_md(struct evlist *evlist, int cpu)
931 {
932 	int i;
933 
934 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
935 		struct mmap *md = &evlist->mmap[i];
936 
937 		if (md->core.cpu.cpu == cpu)
938 			return md;
939 	}
940 
941 	return NULL;
942 }
943 
944 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
945 					  PyObject *args, PyObject *kwargs)
946 {
947 	struct evlist *evlist = &pevlist->evlist;
948 	union perf_event *event;
949 	int sample_id_all = 1, cpu;
950 	static char *kwlist[] = { "cpu", "sample_id_all", NULL };
951 	struct mmap *md;
952 	int err;
953 
954 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
955 					 &cpu, &sample_id_all))
956 		return NULL;
957 
958 	md = get_md(evlist, cpu);
959 	if (!md)
960 		return NULL;
961 
962 	if (perf_mmap__read_init(&md->core) < 0)
963 		goto end;
964 
965 	event = perf_mmap__read_event(&md->core);
966 	if (event != NULL) {
967 		PyObject *pyevent = pyrf_event__new(event);
968 		struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
969 		struct evsel *evsel;
970 
971 		if (pyevent == NULL)
972 			return PyErr_NoMemory();
973 
974 		evsel = evlist__event2evsel(evlist, event);
975 		if (!evsel) {
976 			Py_INCREF(Py_None);
977 			return Py_None;
978 		}
979 
980 		pevent->evsel = evsel;
981 
982 		err = evsel__parse_sample(evsel, event, &pevent->sample);
983 
984 		/* Consume the even only after we parsed it out. */
985 		perf_mmap__consume(&md->core);
986 
987 		if (err)
988 			return PyErr_Format(PyExc_OSError,
989 					    "perf: can't parse sample, err=%d", err);
990 		return pyevent;
991 	}
992 end:
993 	Py_INCREF(Py_None);
994 	return Py_None;
995 }
996 
997 static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
998 				   PyObject *args, PyObject *kwargs)
999 {
1000 	struct evlist *evlist = &pevlist->evlist;
1001 
1002 	if (evlist__open(evlist) < 0) {
1003 		PyErr_SetFromErrno(PyExc_OSError);
1004 		return NULL;
1005 	}
1006 
1007 	Py_INCREF(Py_None);
1008 	return Py_None;
1009 }
1010 
1011 static PyMethodDef pyrf_evlist__methods[] = {
1012 	{
1013 		.ml_name  = "mmap",
1014 		.ml_meth  = (PyCFunction)pyrf_evlist__mmap,
1015 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1016 		.ml_doc	  = PyDoc_STR("mmap the file descriptor table.")
1017 	},
1018 	{
1019 		.ml_name  = "open",
1020 		.ml_meth  = (PyCFunction)pyrf_evlist__open,
1021 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1022 		.ml_doc	  = PyDoc_STR("open the file descriptors.")
1023 	},
1024 	{
1025 		.ml_name  = "poll",
1026 		.ml_meth  = (PyCFunction)pyrf_evlist__poll,
1027 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1028 		.ml_doc	  = PyDoc_STR("poll the file descriptor table.")
1029 	},
1030 	{
1031 		.ml_name  = "get_pollfd",
1032 		.ml_meth  = (PyCFunction)pyrf_evlist__get_pollfd,
1033 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1034 		.ml_doc	  = PyDoc_STR("get the poll file descriptor table.")
1035 	},
1036 	{
1037 		.ml_name  = "add",
1038 		.ml_meth  = (PyCFunction)pyrf_evlist__add,
1039 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1040 		.ml_doc	  = PyDoc_STR("adds an event selector to the list.")
1041 	},
1042 	{
1043 		.ml_name  = "read_on_cpu",
1044 		.ml_meth  = (PyCFunction)pyrf_evlist__read_on_cpu,
1045 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1046 		.ml_doc	  = PyDoc_STR("reads an event.")
1047 	},
1048 	{ .ml_name = NULL, }
1049 };
1050 
1051 static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1052 {
1053 	struct pyrf_evlist *pevlist = (void *)obj;
1054 
1055 	return pevlist->evlist.core.nr_entries;
1056 }
1057 
1058 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1059 {
1060 	struct pyrf_evlist *pevlist = (void *)obj;
1061 	struct evsel *pos;
1062 
1063 	if (i >= pevlist->evlist.core.nr_entries)
1064 		return NULL;
1065 
1066 	evlist__for_each_entry(&pevlist->evlist, pos) {
1067 		if (i-- == 0)
1068 			break;
1069 	}
1070 
1071 	return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1072 }
1073 
1074 static PySequenceMethods pyrf_evlist__sequence_methods = {
1075 	.sq_length = pyrf_evlist__length,
1076 	.sq_item   = pyrf_evlist__item,
1077 };
1078 
1079 static const char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1080 
1081 static PyTypeObject pyrf_evlist__type = {
1082 	PyVarObject_HEAD_INIT(NULL, 0)
1083 	.tp_name	= "perf.evlist",
1084 	.tp_basicsize	= sizeof(struct pyrf_evlist),
1085 	.tp_dealloc	= (destructor)pyrf_evlist__delete,
1086 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1087 	.tp_as_sequence	= &pyrf_evlist__sequence_methods,
1088 	.tp_doc		= pyrf_evlist__doc,
1089 	.tp_methods	= pyrf_evlist__methods,
1090 	.tp_init	= (initproc)pyrf_evlist__init,
1091 };
1092 
1093 static int pyrf_evlist__setup_types(void)
1094 {
1095 	pyrf_evlist__type.tp_new = PyType_GenericNew;
1096 	return PyType_Ready(&pyrf_evlist__type);
1097 }
1098 
1099 #define PERF_CONST(name) { #name, PERF_##name }
1100 
1101 struct perf_constant {
1102 	const char *name;
1103 	int	    value;
1104 };
1105 
1106 static const struct perf_constant perf__constants[] = {
1107 	PERF_CONST(TYPE_HARDWARE),
1108 	PERF_CONST(TYPE_SOFTWARE),
1109 	PERF_CONST(TYPE_TRACEPOINT),
1110 	PERF_CONST(TYPE_HW_CACHE),
1111 	PERF_CONST(TYPE_RAW),
1112 	PERF_CONST(TYPE_BREAKPOINT),
1113 
1114 	PERF_CONST(COUNT_HW_CPU_CYCLES),
1115 	PERF_CONST(COUNT_HW_INSTRUCTIONS),
1116 	PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1117 	PERF_CONST(COUNT_HW_CACHE_MISSES),
1118 	PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1119 	PERF_CONST(COUNT_HW_BRANCH_MISSES),
1120 	PERF_CONST(COUNT_HW_BUS_CYCLES),
1121 	PERF_CONST(COUNT_HW_CACHE_L1D),
1122 	PERF_CONST(COUNT_HW_CACHE_L1I),
1123 	PERF_CONST(COUNT_HW_CACHE_LL),
1124 	PERF_CONST(COUNT_HW_CACHE_DTLB),
1125 	PERF_CONST(COUNT_HW_CACHE_ITLB),
1126 	PERF_CONST(COUNT_HW_CACHE_BPU),
1127 	PERF_CONST(COUNT_HW_CACHE_OP_READ),
1128 	PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1129 	PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1130 	PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1131 	PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1132 
1133 	PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1134 	PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1135 
1136 	PERF_CONST(COUNT_SW_CPU_CLOCK),
1137 	PERF_CONST(COUNT_SW_TASK_CLOCK),
1138 	PERF_CONST(COUNT_SW_PAGE_FAULTS),
1139 	PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1140 	PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1141 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1142 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1143 	PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1144 	PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1145 	PERF_CONST(COUNT_SW_DUMMY),
1146 
1147 	PERF_CONST(SAMPLE_IP),
1148 	PERF_CONST(SAMPLE_TID),
1149 	PERF_CONST(SAMPLE_TIME),
1150 	PERF_CONST(SAMPLE_ADDR),
1151 	PERF_CONST(SAMPLE_READ),
1152 	PERF_CONST(SAMPLE_CALLCHAIN),
1153 	PERF_CONST(SAMPLE_ID),
1154 	PERF_CONST(SAMPLE_CPU),
1155 	PERF_CONST(SAMPLE_PERIOD),
1156 	PERF_CONST(SAMPLE_STREAM_ID),
1157 	PERF_CONST(SAMPLE_RAW),
1158 
1159 	PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1160 	PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1161 	PERF_CONST(FORMAT_ID),
1162 	PERF_CONST(FORMAT_GROUP),
1163 
1164 	PERF_CONST(RECORD_MMAP),
1165 	PERF_CONST(RECORD_LOST),
1166 	PERF_CONST(RECORD_COMM),
1167 	PERF_CONST(RECORD_EXIT),
1168 	PERF_CONST(RECORD_THROTTLE),
1169 	PERF_CONST(RECORD_UNTHROTTLE),
1170 	PERF_CONST(RECORD_FORK),
1171 	PERF_CONST(RECORD_READ),
1172 	PERF_CONST(RECORD_SAMPLE),
1173 	PERF_CONST(RECORD_MMAP2),
1174 	PERF_CONST(RECORD_AUX),
1175 	PERF_CONST(RECORD_ITRACE_START),
1176 	PERF_CONST(RECORD_LOST_SAMPLES),
1177 	PERF_CONST(RECORD_SWITCH),
1178 	PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1179 
1180 	PERF_CONST(RECORD_MISC_SWITCH_OUT),
1181 	{ .name = NULL, },
1182 };
1183 
1184 static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1185 				  PyObject *args, PyObject *kwargs)
1186 {
1187 #ifndef HAVE_LIBTRACEEVENT
1188 	return NULL;
1189 #else
1190 	struct tep_event *tp_format;
1191 	static char *kwlist[] = { "sys", "name", NULL };
1192 	char *sys  = NULL;
1193 	char *name = NULL;
1194 
1195 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1196 					 &sys, &name))
1197 		return NULL;
1198 
1199 	tp_format = trace_event__tp_format(sys, name);
1200 	if (IS_ERR(tp_format))
1201 		return PyLong_FromLong(-1);
1202 
1203 	return PyLong_FromLong(tp_format->id);
1204 #endif // HAVE_LIBTRACEEVENT
1205 }
1206 
1207 static PyMethodDef perf__methods[] = {
1208 	{
1209 		.ml_name  = "tracepoint",
1210 		.ml_meth  = (PyCFunction) pyrf__tracepoint,
1211 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1212 		.ml_doc	  = PyDoc_STR("Get tracepoint config.")
1213 	},
1214 	{ .ml_name = NULL, }
1215 };
1216 
1217 PyMODINIT_FUNC PyInit_perf(void)
1218 {
1219 	PyObject *obj;
1220 	int i;
1221 	PyObject *dict;
1222 	static struct PyModuleDef moduledef = {
1223 		PyModuleDef_HEAD_INIT,
1224 		"perf",			/* m_name */
1225 		"",			/* m_doc */
1226 		-1,			/* m_size */
1227 		perf__methods,		/* m_methods */
1228 		NULL,			/* m_reload */
1229 		NULL,			/* m_traverse */
1230 		NULL,			/* m_clear */
1231 		NULL,			/* m_free */
1232 	};
1233 	PyObject *module = PyModule_Create(&moduledef);
1234 
1235 	if (module == NULL ||
1236 	    pyrf_event__setup_types() < 0 ||
1237 	    pyrf_evlist__setup_types() < 0 ||
1238 	    pyrf_evsel__setup_types() < 0 ||
1239 	    pyrf_thread_map__setup_types() < 0 ||
1240 	    pyrf_cpu_map__setup_types() < 0)
1241 		return module;
1242 
1243 	/* The page_size is placed in util object. */
1244 	page_size = sysconf(_SC_PAGE_SIZE);
1245 
1246 	Py_INCREF(&pyrf_evlist__type);
1247 	PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1248 
1249 	Py_INCREF(&pyrf_evsel__type);
1250 	PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1251 
1252 	Py_INCREF(&pyrf_mmap_event__type);
1253 	PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1254 
1255 	Py_INCREF(&pyrf_lost_event__type);
1256 	PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1257 
1258 	Py_INCREF(&pyrf_comm_event__type);
1259 	PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1260 
1261 	Py_INCREF(&pyrf_task_event__type);
1262 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1263 
1264 	Py_INCREF(&pyrf_throttle_event__type);
1265 	PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1266 
1267 	Py_INCREF(&pyrf_task_event__type);
1268 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1269 
1270 	Py_INCREF(&pyrf_read_event__type);
1271 	PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1272 
1273 	Py_INCREF(&pyrf_sample_event__type);
1274 	PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1275 
1276 	Py_INCREF(&pyrf_context_switch_event__type);
1277 	PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1278 
1279 	Py_INCREF(&pyrf_thread_map__type);
1280 	PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1281 
1282 	Py_INCREF(&pyrf_cpu_map__type);
1283 	PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1284 
1285 	dict = PyModule_GetDict(module);
1286 	if (dict == NULL)
1287 		goto error;
1288 
1289 	for (i = 0; perf__constants[i].name != NULL; i++) {
1290 		obj = PyLong_FromLong(perf__constants[i].value);
1291 		if (obj == NULL)
1292 			goto error;
1293 		PyDict_SetItemString(dict, perf__constants[i].name, obj);
1294 		Py_DECREF(obj);
1295 	}
1296 
1297 error:
1298 	if (PyErr_Occurred())
1299 		PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1300 	return module;
1301 }
1302 
1303 
1304 /* The following are stubs to avoid dragging in builtin-* objects. */
1305 /* TODO: move the code out of the builtin-* file into util. */
1306 
1307 struct kwork_work *perf_kwork_add_work(struct perf_kwork *kwork __maybe_unused,
1308 				       struct kwork_class *class __maybe_unused,
1309 				       struct kwork_work *key  __maybe_unused)
1310 {
1311 	return NULL;
1312 }
1313 
1314 bool match_callstack_filter(struct machine *machine __maybe_unused, u64 *callstack __maybe_unused)
1315 {
1316 	return false;
1317 }
1318 
1319 struct lock_stat *lock_stat_find(u64 addr __maybe_unused)
1320 {
1321 	return NULL;
1322 }
1323 
1324 struct lock_stat *lock_stat_findnew(u64 addr __maybe_unused, const char *name __maybe_unused,
1325 				int flags __maybe_unused)
1326 {
1327 	return NULL;
1328 }
1329 
1330 int cmd_inject(int argc __maybe_unused, const char *argv[] __maybe_unused)
1331 {
1332 	return -1;
1333 }
1334