xref: /linux/tools/perf/util/python.c (revision 6d765f5f7ec669f2a16b44afd23cd877efa640de)
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 "callchain.h"
13 #include "counts.h"
14 #include "evlist.h"
15 #include "evsel.h"
16 #include "event.h"
17 #include "print_binary.h"
18 #include "record.h"
19 #include "strbuf.h"
20 #include "thread_map.h"
21 #include "trace-event.h"
22 #include "metricgroup.h"
23 #include "mmap.h"
24 #include "util/sample.h"
25 #include <internal/lib.h>
26 
27 PyMODINIT_FUNC PyInit_perf(void);
28 
29 #define member_def(type, member, ptype, help) \
30 	{ #member, ptype, \
31 	  offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
32 	  0, help }
33 
34 #define sample_member_def(name, member, ptype, help) \
35 	{ #name, ptype, \
36 	  offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
37 	  0, help }
38 
39 struct pyrf_event {
40 	PyObject_HEAD
41 	struct evsel *evsel;
42 	struct perf_sample sample;
43 	union perf_event   event;
44 };
45 
46 #define sample_members \
47 	sample_member_def(sample_ip, ip, T_ULONGLONG, "event ip"),			 \
48 	sample_member_def(sample_pid, pid, T_INT, "event pid"),			 \
49 	sample_member_def(sample_tid, tid, T_INT, "event tid"),			 \
50 	sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"),		 \
51 	sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"),		 \
52 	sample_member_def(sample_id, id, T_ULONGLONG, "event id"),			 \
53 	sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
54 	sample_member_def(sample_period, period, T_ULONGLONG, "event period"),		 \
55 	sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
56 
57 static const char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
58 
59 static PyMemberDef pyrf_mmap_event__members[] = {
60 	sample_members
61 	member_def(perf_event_header, type, T_UINT, "event type"),
62 	member_def(perf_event_header, misc, T_UINT, "event misc"),
63 	member_def(perf_record_mmap, pid, T_UINT, "event pid"),
64 	member_def(perf_record_mmap, tid, T_UINT, "event tid"),
65 	member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
66 	member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
67 	member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
68 	member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
69 	{ .name = NULL, },
70 };
71 
72 static PyObject *pyrf_mmap_event__repr(const struct pyrf_event *pevent)
73 {
74 	PyObject *ret;
75 	char *s;
76 
77 	if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
78 			 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
79 			 "filename: %s }",
80 		     pevent->event.mmap.pid, pevent->event.mmap.tid,
81 		     pevent->event.mmap.start, pevent->event.mmap.len,
82 		     pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
83 		ret = PyErr_NoMemory();
84 	} else {
85 		ret = PyUnicode_FromString(s);
86 		free(s);
87 	}
88 	return ret;
89 }
90 
91 static PyTypeObject pyrf_mmap_event__type = {
92 	PyVarObject_HEAD_INIT(NULL, 0)
93 	.tp_name	= "perf.mmap_event",
94 	.tp_basicsize	= sizeof(struct pyrf_event),
95 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
96 	.tp_doc		= pyrf_mmap_event__doc,
97 	.tp_members	= pyrf_mmap_event__members,
98 	.tp_repr	= (reprfunc)pyrf_mmap_event__repr,
99 };
100 
101 static const char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
102 
103 static PyMemberDef pyrf_task_event__members[] = {
104 	sample_members
105 	member_def(perf_event_header, type, T_UINT, "event type"),
106 	member_def(perf_record_fork, pid, T_UINT, "event pid"),
107 	member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
108 	member_def(perf_record_fork, tid, T_UINT, "event tid"),
109 	member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
110 	member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
111 	{ .name = NULL, },
112 };
113 
114 static PyObject *pyrf_task_event__repr(const struct pyrf_event *pevent)
115 {
116 	return PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
117 				   "ptid: %u, time: %" PRI_lu64 "}",
118 				   pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
119 				   pevent->event.fork.pid,
120 				   pevent->event.fork.ppid,
121 				   pevent->event.fork.tid,
122 				   pevent->event.fork.ptid,
123 				   pevent->event.fork.time);
124 }
125 
126 static PyTypeObject pyrf_task_event__type = {
127 	PyVarObject_HEAD_INIT(NULL, 0)
128 	.tp_name	= "perf.task_event",
129 	.tp_basicsize	= sizeof(struct pyrf_event),
130 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
131 	.tp_doc		= pyrf_task_event__doc,
132 	.tp_members	= pyrf_task_event__members,
133 	.tp_repr	= (reprfunc)pyrf_task_event__repr,
134 };
135 
136 static const char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
137 
138 static PyMemberDef pyrf_comm_event__members[] = {
139 	sample_members
140 	member_def(perf_event_header, type, T_UINT, "event type"),
141 	member_def(perf_record_comm, pid, T_UINT, "event pid"),
142 	member_def(perf_record_comm, tid, T_UINT, "event tid"),
143 	member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
144 	{ .name = NULL, },
145 };
146 
147 static PyObject *pyrf_comm_event__repr(const struct pyrf_event *pevent)
148 {
149 	return PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
150 				   pevent->event.comm.pid,
151 				   pevent->event.comm.tid,
152 				   pevent->event.comm.comm);
153 }
154 
155 static PyTypeObject pyrf_comm_event__type = {
156 	PyVarObject_HEAD_INIT(NULL, 0)
157 	.tp_name	= "perf.comm_event",
158 	.tp_basicsize	= sizeof(struct pyrf_event),
159 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
160 	.tp_doc		= pyrf_comm_event__doc,
161 	.tp_members	= pyrf_comm_event__members,
162 	.tp_repr	= (reprfunc)pyrf_comm_event__repr,
163 };
164 
165 static const char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
166 
167 static PyMemberDef pyrf_throttle_event__members[] = {
168 	sample_members
169 	member_def(perf_event_header, type, T_UINT, "event type"),
170 	member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
171 	member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
172 	member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
173 	{ .name = NULL, },
174 };
175 
176 static PyObject *pyrf_throttle_event__repr(const struct pyrf_event *pevent)
177 {
178 	const struct perf_record_throttle *te = (const struct perf_record_throttle *)
179 		(&pevent->event.header + 1);
180 
181 	return PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
182 				   ", stream_id: %" PRI_lu64 " }",
183 				   pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
184 				   te->time, te->id, te->stream_id);
185 }
186 
187 static PyTypeObject pyrf_throttle_event__type = {
188 	PyVarObject_HEAD_INIT(NULL, 0)
189 	.tp_name	= "perf.throttle_event",
190 	.tp_basicsize	= sizeof(struct pyrf_event),
191 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
192 	.tp_doc		= pyrf_throttle_event__doc,
193 	.tp_members	= pyrf_throttle_event__members,
194 	.tp_repr	= (reprfunc)pyrf_throttle_event__repr,
195 };
196 
197 static const char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
198 
199 static PyMemberDef pyrf_lost_event__members[] = {
200 	sample_members
201 	member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
202 	member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
203 	{ .name = NULL, },
204 };
205 
206 static PyObject *pyrf_lost_event__repr(const struct pyrf_event *pevent)
207 {
208 	PyObject *ret;
209 	char *s;
210 
211 	if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
212 			 "lost: %#" PRI_lx64 " }",
213 		     pevent->event.lost.id, pevent->event.lost.lost) < 0) {
214 		ret = PyErr_NoMemory();
215 	} else {
216 		ret = PyUnicode_FromString(s);
217 		free(s);
218 	}
219 	return ret;
220 }
221 
222 static PyTypeObject pyrf_lost_event__type = {
223 	PyVarObject_HEAD_INIT(NULL, 0)
224 	.tp_name	= "perf.lost_event",
225 	.tp_basicsize	= sizeof(struct pyrf_event),
226 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
227 	.tp_doc		= pyrf_lost_event__doc,
228 	.tp_members	= pyrf_lost_event__members,
229 	.tp_repr	= (reprfunc)pyrf_lost_event__repr,
230 };
231 
232 static const char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
233 
234 static PyMemberDef pyrf_read_event__members[] = {
235 	sample_members
236 	member_def(perf_record_read, pid, T_UINT, "event pid"),
237 	member_def(perf_record_read, tid, T_UINT, "event tid"),
238 	{ .name = NULL, },
239 };
240 
241 static PyObject *pyrf_read_event__repr(const struct pyrf_event *pevent)
242 {
243 	return PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
244 				   pevent->event.read.pid,
245 				   pevent->event.read.tid);
246 	/*
247  	 * FIXME: return the array of read values,
248  	 * making this method useful ;-)
249  	 */
250 }
251 
252 static PyTypeObject pyrf_read_event__type = {
253 	PyVarObject_HEAD_INIT(NULL, 0)
254 	.tp_name	= "perf.read_event",
255 	.tp_basicsize	= sizeof(struct pyrf_event),
256 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
257 	.tp_doc		= pyrf_read_event__doc,
258 	.tp_members	= pyrf_read_event__members,
259 	.tp_repr	= (reprfunc)pyrf_read_event__repr,
260 };
261 
262 static const char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
263 
264 static PyMemberDef pyrf_sample_event__members[] = {
265 	sample_members
266 	member_def(perf_event_header, type, T_UINT, "event type"),
267 	{ .name = NULL, },
268 };
269 
270 static void pyrf_sample_event__delete(struct pyrf_event *pevent)
271 {
272 	perf_sample__exit(&pevent->sample);
273 	Py_TYPE(pevent)->tp_free((PyObject*)pevent);
274 }
275 
276 static PyObject *pyrf_sample_event__repr(const struct pyrf_event *pevent)
277 {
278 	PyObject *ret;
279 	char *s;
280 
281 	if (asprintf(&s, "{ type: sample }") < 0) {
282 		ret = PyErr_NoMemory();
283 	} else {
284 		ret = PyUnicode_FromString(s);
285 		free(s);
286 	}
287 	return ret;
288 }
289 
290 #ifdef HAVE_LIBTRACEEVENT
291 static bool is_tracepoint(const struct pyrf_event *pevent)
292 {
293 	return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
294 }
295 
296 static PyObject*
297 tracepoint_field(const struct pyrf_event *pe, struct tep_format_field *field)
298 {
299 	struct tep_handle *pevent = field->event->tep;
300 	void *data = pe->sample.raw_data;
301 	PyObject *ret = NULL;
302 	unsigned long long val;
303 	unsigned int offset, len;
304 
305 	if (field->flags & TEP_FIELD_IS_ARRAY) {
306 		offset = field->offset;
307 		len    = field->size;
308 		if (field->flags & TEP_FIELD_IS_DYNAMIC) {
309 			val     = tep_read_number(pevent, data + offset, len);
310 			offset  = val;
311 			len     = offset >> 16;
312 			offset &= 0xffff;
313 			if (tep_field_is_relative(field->flags))
314 				offset += field->offset + field->size;
315 		}
316 		if (field->flags & TEP_FIELD_IS_STRING &&
317 		    is_printable_array(data + offset, len)) {
318 			ret = PyUnicode_FromString((char *)data + offset);
319 		} else {
320 			ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
321 			field->flags &= ~TEP_FIELD_IS_STRING;
322 		}
323 	} else {
324 		val = tep_read_number(pevent, data + field->offset,
325 				      field->size);
326 		if (field->flags & TEP_FIELD_IS_POINTER)
327 			ret = PyLong_FromUnsignedLong((unsigned long) val);
328 		else if (field->flags & TEP_FIELD_IS_SIGNED)
329 			ret = PyLong_FromLong((long) val);
330 		else
331 			ret = PyLong_FromUnsignedLong((unsigned long) val);
332 	}
333 
334 	return ret;
335 }
336 
337 static PyObject*
338 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
339 {
340 	const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
341 	struct evsel *evsel = pevent->evsel;
342 	struct tep_event *tp_format = evsel__tp_format(evsel);
343 	struct tep_format_field *field;
344 
345 	if (IS_ERR_OR_NULL(tp_format))
346 		return NULL;
347 
348 	field = tep_find_any_field(tp_format, str);
349 	return field ? tracepoint_field(pevent, field) : NULL;
350 }
351 #endif /* HAVE_LIBTRACEEVENT */
352 
353 static PyObject*
354 pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
355 {
356 	PyObject *obj = NULL;
357 
358 #ifdef HAVE_LIBTRACEEVENT
359 	if (is_tracepoint(pevent))
360 		obj = get_tracepoint_field(pevent, attr_name);
361 #endif
362 
363 	return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
364 }
365 
366 static PyTypeObject pyrf_sample_event__type = {
367 	PyVarObject_HEAD_INIT(NULL, 0)
368 	.tp_name	= "perf.sample_event",
369 	.tp_basicsize	= sizeof(struct pyrf_event),
370 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
371 	.tp_doc		= pyrf_sample_event__doc,
372 	.tp_members	= pyrf_sample_event__members,
373 	.tp_repr	= (reprfunc)pyrf_sample_event__repr,
374 	.tp_getattro	= (getattrofunc) pyrf_sample_event__getattro,
375 };
376 
377 static const char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
378 
379 static PyMemberDef pyrf_context_switch_event__members[] = {
380 	sample_members
381 	member_def(perf_event_header, type, T_UINT, "event type"),
382 	member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
383 	member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
384 	{ .name = NULL, },
385 };
386 
387 static PyObject *pyrf_context_switch_event__repr(const struct pyrf_event *pevent)
388 {
389 	PyObject *ret;
390 	char *s;
391 
392 	if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
393 		     pevent->event.context_switch.next_prev_pid,
394 		     pevent->event.context_switch.next_prev_tid,
395 		     !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
396 		ret = PyErr_NoMemory();
397 	} else {
398 		ret = PyUnicode_FromString(s);
399 		free(s);
400 	}
401 	return ret;
402 }
403 
404 static PyTypeObject pyrf_context_switch_event__type = {
405 	PyVarObject_HEAD_INIT(NULL, 0)
406 	.tp_name	= "perf.context_switch_event",
407 	.tp_basicsize	= sizeof(struct pyrf_event),
408 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
409 	.tp_doc		= pyrf_context_switch_event__doc,
410 	.tp_members	= pyrf_context_switch_event__members,
411 	.tp_repr	= (reprfunc)pyrf_context_switch_event__repr,
412 };
413 
414 static int pyrf_event__setup_types(void)
415 {
416 	int err;
417 	pyrf_mmap_event__type.tp_new =
418 	pyrf_task_event__type.tp_new =
419 	pyrf_comm_event__type.tp_new =
420 	pyrf_lost_event__type.tp_new =
421 	pyrf_read_event__type.tp_new =
422 	pyrf_sample_event__type.tp_new =
423 	pyrf_context_switch_event__type.tp_new =
424 	pyrf_throttle_event__type.tp_new = PyType_GenericNew;
425 
426 	pyrf_sample_event__type.tp_dealloc = (destructor)pyrf_sample_event__delete,
427 
428 	err = PyType_Ready(&pyrf_mmap_event__type);
429 	if (err < 0)
430 		goto out;
431 	err = PyType_Ready(&pyrf_lost_event__type);
432 	if (err < 0)
433 		goto out;
434 	err = PyType_Ready(&pyrf_task_event__type);
435 	if (err < 0)
436 		goto out;
437 	err = PyType_Ready(&pyrf_comm_event__type);
438 	if (err < 0)
439 		goto out;
440 	err = PyType_Ready(&pyrf_throttle_event__type);
441 	if (err < 0)
442 		goto out;
443 	err = PyType_Ready(&pyrf_read_event__type);
444 	if (err < 0)
445 		goto out;
446 	err = PyType_Ready(&pyrf_sample_event__type);
447 	if (err < 0)
448 		goto out;
449 	err = PyType_Ready(&pyrf_context_switch_event__type);
450 	if (err < 0)
451 		goto out;
452 out:
453 	return err;
454 }
455 
456 static PyTypeObject *pyrf_event__type[] = {
457 	[PERF_RECORD_MMAP]	 = &pyrf_mmap_event__type,
458 	[PERF_RECORD_LOST]	 = &pyrf_lost_event__type,
459 	[PERF_RECORD_COMM]	 = &pyrf_comm_event__type,
460 	[PERF_RECORD_EXIT]	 = &pyrf_task_event__type,
461 	[PERF_RECORD_THROTTLE]	 = &pyrf_throttle_event__type,
462 	[PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
463 	[PERF_RECORD_FORK]	 = &pyrf_task_event__type,
464 	[PERF_RECORD_READ]	 = &pyrf_read_event__type,
465 	[PERF_RECORD_SAMPLE]	 = &pyrf_sample_event__type,
466 	[PERF_RECORD_SWITCH]	 = &pyrf_context_switch_event__type,
467 	[PERF_RECORD_SWITCH_CPU_WIDE]  = &pyrf_context_switch_event__type,
468 };
469 
470 static PyObject *pyrf_event__new(const union perf_event *event)
471 {
472 	struct pyrf_event *pevent;
473 	PyTypeObject *ptype;
474 
475 	if ((event->header.type < PERF_RECORD_MMAP ||
476 	     event->header.type > PERF_RECORD_SAMPLE) &&
477 	    !(event->header.type == PERF_RECORD_SWITCH ||
478 	      event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
479 		return NULL;
480 
481 	// FIXME this better be dynamic or we need to parse everything
482 	// before calling perf_mmap__consume(), including tracepoint fields.
483 	if (sizeof(pevent->event) < event->header.size)
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 		PyErr_SetString(PyExc_IndexError, "Index out of range");
534 		return NULL;
535 	}
536 
537 	return Py_BuildValue("i", perf_cpu_map__cpu(pcpus->cpus, i).cpu);
538 }
539 
540 static PySequenceMethods pyrf_cpu_map__sequence_methods = {
541 	.sq_length = pyrf_cpu_map__length,
542 	.sq_item   = pyrf_cpu_map__item,
543 };
544 
545 static const char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
546 
547 static PyTypeObject pyrf_cpu_map__type = {
548 	PyVarObject_HEAD_INIT(NULL, 0)
549 	.tp_name	= "perf.cpu_map",
550 	.tp_basicsize	= sizeof(struct pyrf_cpu_map),
551 	.tp_dealloc	= (destructor)pyrf_cpu_map__delete,
552 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
553 	.tp_doc		= pyrf_cpu_map__doc,
554 	.tp_as_sequence	= &pyrf_cpu_map__sequence_methods,
555 	.tp_init	= (initproc)pyrf_cpu_map__init,
556 };
557 
558 static int pyrf_cpu_map__setup_types(void)
559 {
560 	pyrf_cpu_map__type.tp_new = PyType_GenericNew;
561 	return PyType_Ready(&pyrf_cpu_map__type);
562 }
563 
564 struct pyrf_thread_map {
565 	PyObject_HEAD
566 
567 	struct perf_thread_map *threads;
568 };
569 
570 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
571 				 PyObject *args, PyObject *kwargs)
572 {
573 	static char *kwlist[] = { "pid", "tid", NULL };
574 	int pid = -1, tid = -1;
575 
576 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii",
577 					 kwlist, &pid, &tid))
578 		return -1;
579 
580 	pthreads->threads = thread_map__new(pid, tid);
581 	if (pthreads->threads == NULL)
582 		return -1;
583 	return 0;
584 }
585 
586 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
587 {
588 	perf_thread_map__put(pthreads->threads);
589 	Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
590 }
591 
592 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
593 {
594 	struct pyrf_thread_map *pthreads = (void *)obj;
595 
596 	return perf_thread_map__nr(pthreads->threads);
597 }
598 
599 static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
600 {
601 	struct pyrf_thread_map *pthreads = (void *)obj;
602 
603 	if (i >= perf_thread_map__nr(pthreads->threads)) {
604 		PyErr_SetString(PyExc_IndexError, "Index out of range");
605 		return NULL;
606 	}
607 
608 	return Py_BuildValue("i", perf_thread_map__pid(pthreads->threads, i));
609 }
610 
611 static PySequenceMethods pyrf_thread_map__sequence_methods = {
612 	.sq_length = pyrf_thread_map__length,
613 	.sq_item   = pyrf_thread_map__item,
614 };
615 
616 static const char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
617 
618 static PyTypeObject pyrf_thread_map__type = {
619 	PyVarObject_HEAD_INIT(NULL, 0)
620 	.tp_name	= "perf.thread_map",
621 	.tp_basicsize	= sizeof(struct pyrf_thread_map),
622 	.tp_dealloc	= (destructor)pyrf_thread_map__delete,
623 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
624 	.tp_doc		= pyrf_thread_map__doc,
625 	.tp_as_sequence	= &pyrf_thread_map__sequence_methods,
626 	.tp_init	= (initproc)pyrf_thread_map__init,
627 };
628 
629 static int pyrf_thread_map__setup_types(void)
630 {
631 	pyrf_thread_map__type.tp_new = PyType_GenericNew;
632 	return PyType_Ready(&pyrf_thread_map__type);
633 }
634 
635 struct pyrf_counts_values {
636 	PyObject_HEAD
637 
638 	struct perf_counts_values values;
639 };
640 
641 static const char pyrf_counts_values__doc[] = PyDoc_STR("perf counts values object.");
642 
643 static void pyrf_counts_values__delete(struct pyrf_counts_values *pcounts_values)
644 {
645 	Py_TYPE(pcounts_values)->tp_free((PyObject *)pcounts_values);
646 }
647 
648 #define counts_values_member_def(member, ptype, help) \
649 	{ #member, ptype, \
650 	  offsetof(struct pyrf_counts_values, values.member), \
651 	  0, help }
652 
653 static PyMemberDef pyrf_counts_values_members[] = {
654 	counts_values_member_def(val, T_ULONG, "Value of event"),
655 	counts_values_member_def(ena, T_ULONG, "Time for which enabled"),
656 	counts_values_member_def(run, T_ULONG, "Time for which running"),
657 	counts_values_member_def(id, T_ULONG, "Unique ID for an event"),
658 	counts_values_member_def(lost, T_ULONG, "Num of lost samples"),
659 	{ .name = NULL, },
660 };
661 
662 static PyObject *pyrf_counts_values_get_values(struct pyrf_counts_values *self, void *closure)
663 {
664 	PyObject *vals = PyList_New(5);
665 
666 	if (!vals)
667 		return NULL;
668 	for (int i = 0; i < 5; i++)
669 		PyList_SetItem(vals, i, PyLong_FromLong(self->values.values[i]));
670 
671 	return vals;
672 }
673 
674 static int pyrf_counts_values_set_values(struct pyrf_counts_values *self, PyObject *list,
675 					 void *closure)
676 {
677 	Py_ssize_t size;
678 	PyObject *item = NULL;
679 
680 	if (!PyList_Check(list)) {
681 		PyErr_SetString(PyExc_TypeError, "Value assigned must be a list");
682 		return -1;
683 	}
684 
685 	size = PyList_Size(list);
686 	for (Py_ssize_t i = 0; i < size; i++) {
687 		item = PyList_GetItem(list, i);
688 		if (!PyLong_Check(item)) {
689 			PyErr_SetString(PyExc_TypeError, "List members should be numbers");
690 			return -1;
691 		}
692 		self->values.values[i] = PyLong_AsLong(item);
693 	}
694 
695 	return 0;
696 }
697 
698 static PyGetSetDef pyrf_counts_values_getset[] = {
699 	{"values", (getter)pyrf_counts_values_get_values, (setter)pyrf_counts_values_set_values,
700 		"Name field", NULL},
701 	{ .name = NULL, },
702 };
703 
704 static PyTypeObject pyrf_counts_values__type = {
705 	PyVarObject_HEAD_INIT(NULL, 0)
706 	.tp_name	= "perf.counts_values",
707 	.tp_basicsize	= sizeof(struct pyrf_counts_values),
708 	.tp_dealloc	= (destructor)pyrf_counts_values__delete,
709 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
710 	.tp_doc		= pyrf_counts_values__doc,
711 	.tp_members	= pyrf_counts_values_members,
712 	.tp_getset	= pyrf_counts_values_getset,
713 };
714 
715 static int pyrf_counts_values__setup_types(void)
716 {
717 	pyrf_counts_values__type.tp_new = PyType_GenericNew;
718 	return PyType_Ready(&pyrf_counts_values__type);
719 }
720 
721 struct pyrf_evsel {
722 	PyObject_HEAD
723 
724 	struct evsel evsel;
725 };
726 
727 static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
728 			    PyObject *args, PyObject *kwargs)
729 {
730 	struct perf_event_attr attr = {
731 		.type = PERF_TYPE_HARDWARE,
732 		.config = PERF_COUNT_HW_CPU_CYCLES,
733 		.sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
734 	};
735 	static char *kwlist[] = {
736 		"type",
737 		"config",
738 		"sample_freq",
739 		"sample_period",
740 		"sample_type",
741 		"read_format",
742 		"disabled",
743 		"inherit",
744 		"pinned",
745 		"exclusive",
746 		"exclude_user",
747 		"exclude_kernel",
748 		"exclude_hv",
749 		"exclude_idle",
750 		"mmap",
751 		"context_switch",
752 		"comm",
753 		"freq",
754 		"inherit_stat",
755 		"enable_on_exec",
756 		"task",
757 		"watermark",
758 		"precise_ip",
759 		"mmap_data",
760 		"sample_id_all",
761 		"wakeup_events",
762 		"bp_type",
763 		"bp_addr",
764 		"bp_len",
765 		 NULL
766 	};
767 	u64 sample_period = 0;
768 	u32 disabled = 0,
769 	    inherit = 0,
770 	    pinned = 0,
771 	    exclusive = 0,
772 	    exclude_user = 0,
773 	    exclude_kernel = 0,
774 	    exclude_hv = 0,
775 	    exclude_idle = 0,
776 	    mmap = 0,
777 	    context_switch = 0,
778 	    comm = 0,
779 	    freq = 1,
780 	    inherit_stat = 0,
781 	    enable_on_exec = 0,
782 	    task = 0,
783 	    watermark = 0,
784 	    precise_ip = 0,
785 	    mmap_data = 0,
786 	    sample_id_all = 1;
787 	int idx = 0;
788 
789 	if (!PyArg_ParseTupleAndKeywords(args, kwargs,
790 					 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
791 					 &attr.type, &attr.config, &attr.sample_freq,
792 					 &sample_period, &attr.sample_type,
793 					 &attr.read_format, &disabled, &inherit,
794 					 &pinned, &exclusive, &exclude_user,
795 					 &exclude_kernel, &exclude_hv, &exclude_idle,
796 					 &mmap, &context_switch, &comm, &freq, &inherit_stat,
797 					 &enable_on_exec, &task, &watermark,
798 					 &precise_ip, &mmap_data, &sample_id_all,
799 					 &attr.wakeup_events, &attr.bp_type,
800 					 &attr.bp_addr, &attr.bp_len, &idx))
801 		return -1;
802 
803 	/* union... */
804 	if (sample_period != 0) {
805 		if (attr.sample_freq != 0)
806 			return -1; /* FIXME: throw right exception */
807 		attr.sample_period = sample_period;
808 	}
809 
810 	/* Bitfields */
811 	attr.disabled	    = disabled;
812 	attr.inherit	    = inherit;
813 	attr.pinned	    = pinned;
814 	attr.exclusive	    = exclusive;
815 	attr.exclude_user   = exclude_user;
816 	attr.exclude_kernel = exclude_kernel;
817 	attr.exclude_hv	    = exclude_hv;
818 	attr.exclude_idle   = exclude_idle;
819 	attr.mmap	    = mmap;
820 	attr.context_switch = context_switch;
821 	attr.comm	    = comm;
822 	attr.freq	    = freq;
823 	attr.inherit_stat   = inherit_stat;
824 	attr.enable_on_exec = enable_on_exec;
825 	attr.task	    = task;
826 	attr.watermark	    = watermark;
827 	attr.precise_ip	    = precise_ip;
828 	attr.mmap_data	    = mmap_data;
829 	attr.sample_id_all  = sample_id_all;
830 	attr.size	    = sizeof(attr);
831 
832 	evsel__init(&pevsel->evsel, &attr, idx);
833 	return 0;
834 }
835 
836 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
837 {
838 	evsel__exit(&pevsel->evsel);
839 	Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
840 }
841 
842 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
843 				  PyObject *args, PyObject *kwargs)
844 {
845 	struct evsel *evsel = &pevsel->evsel;
846 	struct perf_cpu_map *cpus = NULL;
847 	struct perf_thread_map *threads = NULL;
848 	PyObject *pcpus = NULL, *pthreads = NULL;
849 	int group = 0, inherit = 0;
850 	static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
851 
852 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
853 					 &pcpus, &pthreads, &group, &inherit))
854 		return NULL;
855 
856 	if (pthreads != NULL)
857 		threads = ((struct pyrf_thread_map *)pthreads)->threads;
858 
859 	if (pcpus != NULL)
860 		cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
861 
862 	evsel->core.attr.inherit = inherit;
863 	/*
864 	 * This will group just the fds for this single evsel, to group
865 	 * multiple events, use evlist.open().
866 	 */
867 	if (evsel__open(evsel, cpus, threads) < 0) {
868 		PyErr_SetFromErrno(PyExc_OSError);
869 		return NULL;
870 	}
871 
872 	Py_INCREF(Py_None);
873 	return Py_None;
874 }
875 
876 static PyObject *pyrf_evsel__cpus(struct pyrf_evsel *pevsel)
877 {
878 	struct pyrf_cpu_map *pcpu_map = PyObject_New(struct pyrf_cpu_map, &pyrf_cpu_map__type);
879 
880 	if (pcpu_map)
881 		pcpu_map->cpus = perf_cpu_map__get(pevsel->evsel.core.cpus);
882 
883 	return (PyObject *)pcpu_map;
884 }
885 
886 static PyObject *pyrf_evsel__threads(struct pyrf_evsel *pevsel)
887 {
888 	struct pyrf_thread_map *pthread_map =
889 		PyObject_New(struct pyrf_thread_map, &pyrf_thread_map__type);
890 
891 	if (pthread_map)
892 		pthread_map->threads = perf_thread_map__get(pevsel->evsel.core.threads);
893 
894 	return (PyObject *)pthread_map;
895 }
896 
897 /*
898  * Ensure evsel's counts and prev_raw_counts are allocated, the latter
899  * used by tool PMUs to compute the cumulative count as expected by
900  * stat's process_counter_values.
901  */
902 static int evsel__ensure_counts(struct evsel *evsel)
903 {
904 	int nthreads, ncpus;
905 
906 	if (evsel->counts != NULL)
907 		return 0;
908 
909 	nthreads = perf_thread_map__nr(evsel->core.threads);
910 	ncpus = perf_cpu_map__nr(evsel->core.cpus);
911 
912 	evsel->counts = perf_counts__new(ncpus, nthreads);
913 	if (evsel->counts == NULL)
914 		return -ENOMEM;
915 
916 	evsel->prev_raw_counts = perf_counts__new(ncpus, nthreads);
917 	if (evsel->prev_raw_counts == NULL)
918 		return -ENOMEM;
919 
920 	return 0;
921 }
922 
923 static PyObject *pyrf_evsel__read(struct pyrf_evsel *pevsel,
924 				  PyObject *args, PyObject *kwargs)
925 {
926 	struct evsel *evsel = &pevsel->evsel;
927 	int cpu = 0, cpu_idx, thread = 0, thread_idx;
928 	struct perf_counts_values *old_count, *new_count;
929 	struct pyrf_counts_values *count_values = PyObject_New(struct pyrf_counts_values,
930 							       &pyrf_counts_values__type);
931 
932 	if (!count_values)
933 		return NULL;
934 
935 	if (!PyArg_ParseTuple(args, "ii", &cpu, &thread))
936 		return NULL;
937 
938 	cpu_idx = perf_cpu_map__idx(evsel->core.cpus, (struct perf_cpu){.cpu = cpu});
939 	if (cpu_idx < 0) {
940 		PyErr_Format(PyExc_TypeError, "CPU %d is not part of evsel's CPUs", cpu);
941 		return NULL;
942 	}
943 	thread_idx = perf_thread_map__idx(evsel->core.threads, thread);
944 	if (thread_idx < 0) {
945 		PyErr_Format(PyExc_TypeError, "Thread %d is not part of evsel's threads",
946 			     thread);
947 		return NULL;
948 	}
949 
950 	if (evsel__ensure_counts(evsel))
951 		return PyErr_NoMemory();
952 
953 	/* Set up pointers to the old and newly read counter values. */
954 	old_count = perf_counts(evsel->prev_raw_counts, cpu_idx, thread_idx);
955 	new_count = perf_counts(evsel->counts, cpu_idx, thread_idx);
956 	/* Update the value in evsel->counts. */
957 	evsel__read_counter(evsel, cpu_idx, thread_idx);
958 	/* Copy the value and turn it into the delta from old_count. */
959 	count_values->values = *new_count;
960 	count_values->values.val -= old_count->val;
961 	count_values->values.ena -= old_count->ena;
962 	count_values->values.run -= old_count->run;
963 	/* Save the new count over the old_count for the next read. */
964 	*old_count = *new_count;
965 	return (PyObject *)count_values;
966 }
967 
968 static PyObject *pyrf_evsel__str(PyObject *self)
969 {
970 	struct pyrf_evsel *pevsel = (void *)self;
971 	struct evsel *evsel = &pevsel->evsel;
972 
973 	return PyUnicode_FromFormat("evsel(%s/%s/)", evsel__pmu_name(evsel), evsel__name(evsel));
974 }
975 
976 static PyMethodDef pyrf_evsel__methods[] = {
977 	{
978 		.ml_name  = "open",
979 		.ml_meth  = (PyCFunction)pyrf_evsel__open,
980 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
981 		.ml_doc	  = PyDoc_STR("open the event selector file descriptor table.")
982 	},
983 	{
984 		.ml_name  = "cpus",
985 		.ml_meth  = (PyCFunction)pyrf_evsel__cpus,
986 		.ml_flags = METH_NOARGS,
987 		.ml_doc	  = PyDoc_STR("CPUs the event is to be used with.")
988 	},
989 	{
990 		.ml_name  = "threads",
991 		.ml_meth  = (PyCFunction)pyrf_evsel__threads,
992 		.ml_flags = METH_NOARGS,
993 		.ml_doc	  = PyDoc_STR("threads the event is to be used with.")
994 	},
995 	{
996 		.ml_name  = "read",
997 		.ml_meth  = (PyCFunction)pyrf_evsel__read,
998 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
999 		.ml_doc	  = PyDoc_STR("read counters")
1000 	},
1001 	{ .ml_name = NULL, }
1002 };
1003 
1004 #define evsel_member_def(member, ptype, help) \
1005 	{ #member, ptype, \
1006 	  offsetof(struct pyrf_evsel, evsel.member), \
1007 	  0, help }
1008 
1009 #define evsel_attr_member_def(member, ptype, help) \
1010 	{ #member, ptype, \
1011 	  offsetof(struct pyrf_evsel, evsel.core.attr.member), \
1012 	  0, help }
1013 
1014 static PyMemberDef pyrf_evsel__members[] = {
1015 	evsel_member_def(tracking, T_BOOL, "tracking event."),
1016 	evsel_attr_member_def(type, T_UINT, "attribute type."),
1017 	evsel_attr_member_def(size, T_UINT, "attribute size."),
1018 	evsel_attr_member_def(config, T_ULONGLONG, "attribute config."),
1019 	evsel_attr_member_def(sample_period, T_ULONGLONG, "attribute sample_period."),
1020 	evsel_attr_member_def(sample_type, T_ULONGLONG, "attribute sample_type."),
1021 	evsel_attr_member_def(read_format, T_ULONGLONG, "attribute read_format."),
1022 	evsel_attr_member_def(wakeup_events, T_UINT, "attribute wakeup_events."),
1023 	{ .name = NULL, },
1024 };
1025 
1026 static const char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
1027 
1028 static PyTypeObject pyrf_evsel__type = {
1029 	PyVarObject_HEAD_INIT(NULL, 0)
1030 	.tp_name	= "perf.evsel",
1031 	.tp_basicsize	= sizeof(struct pyrf_evsel),
1032 	.tp_dealloc	= (destructor)pyrf_evsel__delete,
1033 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1034 	.tp_doc		= pyrf_evsel__doc,
1035 	.tp_members	= pyrf_evsel__members,
1036 	.tp_methods	= pyrf_evsel__methods,
1037 	.tp_init	= (initproc)pyrf_evsel__init,
1038 	.tp_str         = pyrf_evsel__str,
1039 	.tp_repr        = pyrf_evsel__str,
1040 };
1041 
1042 static int pyrf_evsel__setup_types(void)
1043 {
1044 	pyrf_evsel__type.tp_new = PyType_GenericNew;
1045 	return PyType_Ready(&pyrf_evsel__type);
1046 }
1047 
1048 struct pyrf_evlist {
1049 	PyObject_HEAD
1050 
1051 	struct evlist evlist;
1052 };
1053 
1054 static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
1055 			     PyObject *args, PyObject *kwargs __maybe_unused)
1056 {
1057 	PyObject *pcpus = NULL, *pthreads = NULL;
1058 	struct perf_cpu_map *cpus;
1059 	struct perf_thread_map *threads;
1060 
1061 	if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
1062 		return -1;
1063 
1064 	threads = ((struct pyrf_thread_map *)pthreads)->threads;
1065 	cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
1066 	evlist__init(&pevlist->evlist, cpus, threads);
1067 	return 0;
1068 }
1069 
1070 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
1071 {
1072 	evlist__exit(&pevlist->evlist);
1073 	Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
1074 }
1075 
1076 static PyObject *pyrf_evlist__all_cpus(struct pyrf_evlist *pevlist)
1077 {
1078 	struct pyrf_cpu_map *pcpu_map = PyObject_New(struct pyrf_cpu_map, &pyrf_cpu_map__type);
1079 
1080 	if (pcpu_map)
1081 		pcpu_map->cpus = perf_cpu_map__get(pevlist->evlist.core.all_cpus);
1082 
1083 	return (PyObject *)pcpu_map;
1084 }
1085 
1086 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
1087 				   PyObject *args, PyObject *kwargs)
1088 {
1089 	struct evlist *evlist = &pevlist->evlist;
1090 	static char *kwlist[] = { "pages", "overwrite", NULL };
1091 	int pages = 128, overwrite = false;
1092 
1093 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
1094 					 &pages, &overwrite))
1095 		return NULL;
1096 
1097 	if (evlist__mmap(evlist, pages) < 0) {
1098 		PyErr_SetFromErrno(PyExc_OSError);
1099 		return NULL;
1100 	}
1101 
1102 	Py_INCREF(Py_None);
1103 	return Py_None;
1104 }
1105 
1106 static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
1107 				   PyObject *args, PyObject *kwargs)
1108 {
1109 	struct evlist *evlist = &pevlist->evlist;
1110 	static char *kwlist[] = { "timeout", NULL };
1111 	int timeout = -1, n;
1112 
1113 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
1114 		return NULL;
1115 
1116 	n = evlist__poll(evlist, timeout);
1117 	if (n < 0) {
1118 		PyErr_SetFromErrno(PyExc_OSError);
1119 		return NULL;
1120 	}
1121 
1122 	return Py_BuildValue("i", n);
1123 }
1124 
1125 static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
1126 					 PyObject *args __maybe_unused,
1127 					 PyObject *kwargs __maybe_unused)
1128 {
1129 	struct evlist *evlist = &pevlist->evlist;
1130         PyObject *list = PyList_New(0);
1131 	int i;
1132 
1133 	for (i = 0; i < evlist->core.pollfd.nr; ++i) {
1134 		PyObject *file;
1135 		file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1,
1136 				     NULL, NULL, NULL, 0);
1137 		if (file == NULL)
1138 			goto free_list;
1139 
1140 		if (PyList_Append(list, file) != 0) {
1141 			Py_DECREF(file);
1142 			goto free_list;
1143 		}
1144 
1145 		Py_DECREF(file);
1146 	}
1147 
1148 	return list;
1149 free_list:
1150 	return PyErr_NoMemory();
1151 }
1152 
1153 
1154 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
1155 				  PyObject *args,
1156 				  PyObject *kwargs __maybe_unused)
1157 {
1158 	struct evlist *evlist = &pevlist->evlist;
1159 	PyObject *pevsel;
1160 	struct evsel *evsel;
1161 
1162 	if (!PyArg_ParseTuple(args, "O", &pevsel))
1163 		return NULL;
1164 
1165 	Py_INCREF(pevsel);
1166 	evsel = &((struct pyrf_evsel *)pevsel)->evsel;
1167 	evsel->core.idx = evlist->core.nr_entries;
1168 	evlist__add(evlist, evsel);
1169 
1170 	return Py_BuildValue("i", evlist->core.nr_entries);
1171 }
1172 
1173 static struct mmap *get_md(struct evlist *evlist, int cpu)
1174 {
1175 	int i;
1176 
1177 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
1178 		struct mmap *md = &evlist->mmap[i];
1179 
1180 		if (md->core.cpu.cpu == cpu)
1181 			return md;
1182 	}
1183 
1184 	return NULL;
1185 }
1186 
1187 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
1188 					  PyObject *args, PyObject *kwargs)
1189 {
1190 	struct evlist *evlist = &pevlist->evlist;
1191 	union perf_event *event;
1192 	int sample_id_all = 1, cpu;
1193 	static char *kwlist[] = { "cpu", "sample_id_all", NULL };
1194 	struct mmap *md;
1195 	int err;
1196 
1197 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1198 					 &cpu, &sample_id_all))
1199 		return NULL;
1200 
1201 	md = get_md(evlist, cpu);
1202 	if (!md)
1203 		return NULL;
1204 
1205 	if (perf_mmap__read_init(&md->core) < 0)
1206 		goto end;
1207 
1208 	event = perf_mmap__read_event(&md->core);
1209 	if (event != NULL) {
1210 		PyObject *pyevent = pyrf_event__new(event);
1211 		struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
1212 		struct evsel *evsel;
1213 
1214 		if (pyevent == NULL)
1215 			return PyErr_NoMemory();
1216 
1217 		evsel = evlist__event2evsel(evlist, event);
1218 		if (!evsel) {
1219 			Py_DECREF(pyevent);
1220 			Py_INCREF(Py_None);
1221 			return Py_None;
1222 		}
1223 
1224 		pevent->evsel = evsel;
1225 
1226 		perf_mmap__consume(&md->core);
1227 
1228 		err = evsel__parse_sample(evsel, &pevent->event, &pevent->sample);
1229 		if (err) {
1230 			Py_DECREF(pyevent);
1231 			return PyErr_Format(PyExc_OSError,
1232 					    "perf: can't parse sample, err=%d", err);
1233 		}
1234 
1235 		return pyevent;
1236 	}
1237 end:
1238 	Py_INCREF(Py_None);
1239 	return Py_None;
1240 }
1241 
1242 static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1243 				   PyObject *args, PyObject *kwargs)
1244 {
1245 	struct evlist *evlist = &pevlist->evlist;
1246 
1247 	if (evlist__open(evlist) < 0) {
1248 		PyErr_SetFromErrno(PyExc_OSError);
1249 		return NULL;
1250 	}
1251 
1252 	Py_INCREF(Py_None);
1253 	return Py_None;
1254 }
1255 
1256 static PyObject *pyrf_evlist__close(struct pyrf_evlist *pevlist)
1257 {
1258 	struct evlist *evlist = &pevlist->evlist;
1259 
1260 	evlist__close(evlist);
1261 
1262 	Py_INCREF(Py_None);
1263 	return Py_None;
1264 }
1265 
1266 static PyObject *pyrf_evlist__config(struct pyrf_evlist *pevlist)
1267 {
1268 	struct record_opts opts = {
1269 		.sample_time	     = true,
1270 		.mmap_pages	     = UINT_MAX,
1271 		.user_freq	     = UINT_MAX,
1272 		.user_interval	     = ULLONG_MAX,
1273 		.freq		     = 4000,
1274 		.target		     = {
1275 			.uses_mmap   = true,
1276 			.default_per_cpu = true,
1277 		},
1278 		.nr_threads_synthesize = 1,
1279 		.ctl_fd              = -1,
1280 		.ctl_fd_ack          = -1,
1281 		.no_buffering        = true,
1282 		.no_inherit          = true,
1283 	};
1284 	struct evlist *evlist = &pevlist->evlist;
1285 
1286 	evlist__config(evlist, &opts, &callchain_param);
1287 	Py_INCREF(Py_None);
1288 	return Py_None;
1289 }
1290 
1291 static PyObject *pyrf_evlist__disable(struct pyrf_evlist *pevlist)
1292 {
1293 	evlist__disable(&pevlist->evlist);
1294 	Py_INCREF(Py_None);
1295 	return Py_None;
1296 }
1297 
1298 static PyObject *pyrf_evlist__enable(struct pyrf_evlist *pevlist)
1299 {
1300 	evlist__enable(&pevlist->evlist);
1301 	Py_INCREF(Py_None);
1302 	return Py_None;
1303 }
1304 
1305 static PyMethodDef pyrf_evlist__methods[] = {
1306 	{
1307 		.ml_name  = "all_cpus",
1308 		.ml_meth  = (PyCFunction)pyrf_evlist__all_cpus,
1309 		.ml_flags = METH_NOARGS,
1310 		.ml_doc	  = PyDoc_STR("CPU map union of all evsel CPU maps.")
1311 	},
1312 	{
1313 		.ml_name  = "mmap",
1314 		.ml_meth  = (PyCFunction)pyrf_evlist__mmap,
1315 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1316 		.ml_doc	  = PyDoc_STR("mmap the file descriptor table.")
1317 	},
1318 	{
1319 		.ml_name  = "open",
1320 		.ml_meth  = (PyCFunction)pyrf_evlist__open,
1321 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1322 		.ml_doc	  = PyDoc_STR("open the file descriptors.")
1323 	},
1324 	{
1325 		.ml_name  = "close",
1326 		.ml_meth  = (PyCFunction)pyrf_evlist__close,
1327 		.ml_flags = METH_NOARGS,
1328 		.ml_doc	  = PyDoc_STR("close the file descriptors.")
1329 	},
1330 	{
1331 		.ml_name  = "poll",
1332 		.ml_meth  = (PyCFunction)pyrf_evlist__poll,
1333 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1334 		.ml_doc	  = PyDoc_STR("poll the file descriptor table.")
1335 	},
1336 	{
1337 		.ml_name  = "get_pollfd",
1338 		.ml_meth  = (PyCFunction)pyrf_evlist__get_pollfd,
1339 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1340 		.ml_doc	  = PyDoc_STR("get the poll file descriptor table.")
1341 	},
1342 	{
1343 		.ml_name  = "add",
1344 		.ml_meth  = (PyCFunction)pyrf_evlist__add,
1345 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1346 		.ml_doc	  = PyDoc_STR("adds an event selector to the list.")
1347 	},
1348 	{
1349 		.ml_name  = "read_on_cpu",
1350 		.ml_meth  = (PyCFunction)pyrf_evlist__read_on_cpu,
1351 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1352 		.ml_doc	  = PyDoc_STR("reads an event.")
1353 	},
1354 	{
1355 		.ml_name  = "config",
1356 		.ml_meth  = (PyCFunction)pyrf_evlist__config,
1357 		.ml_flags = METH_NOARGS,
1358 		.ml_doc	  = PyDoc_STR("Apply default record options to the evlist.")
1359 	},
1360 	{
1361 		.ml_name  = "disable",
1362 		.ml_meth  = (PyCFunction)pyrf_evlist__disable,
1363 		.ml_flags = METH_NOARGS,
1364 		.ml_doc	  = PyDoc_STR("Disable the evsels in the evlist.")
1365 	},
1366 	{
1367 		.ml_name  = "enable",
1368 		.ml_meth  = (PyCFunction)pyrf_evlist__enable,
1369 		.ml_flags = METH_NOARGS,
1370 		.ml_doc	  = PyDoc_STR("Enable the evsels in the evlist.")
1371 	},
1372 	{ .ml_name = NULL, }
1373 };
1374 
1375 static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1376 {
1377 	struct pyrf_evlist *pevlist = (void *)obj;
1378 
1379 	return pevlist->evlist.core.nr_entries;
1380 }
1381 
1382 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1383 {
1384 	struct pyrf_evlist *pevlist = (void *)obj;
1385 	struct evsel *pos;
1386 
1387 	if (i >= pevlist->evlist.core.nr_entries) {
1388 		PyErr_SetString(PyExc_IndexError, "Index out of range");
1389 		return NULL;
1390 	}
1391 
1392 	evlist__for_each_entry(&pevlist->evlist, pos) {
1393 		if (i-- == 0)
1394 			break;
1395 	}
1396 
1397 	return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1398 }
1399 
1400 static PyObject *pyrf_evlist__str(PyObject *self)
1401 {
1402 	struct pyrf_evlist *pevlist = (void *)self;
1403 	struct evsel *pos;
1404 	struct strbuf sb = STRBUF_INIT;
1405 	bool first = true;
1406 	PyObject *result;
1407 
1408 	strbuf_addstr(&sb, "evlist([");
1409 	evlist__for_each_entry(&pevlist->evlist, pos) {
1410 		if (!first)
1411 			strbuf_addch(&sb, ',');
1412 		if (!pos->pmu)
1413 			strbuf_addstr(&sb, evsel__name(pos));
1414 		else
1415 			strbuf_addf(&sb, "%s/%s/", pos->pmu->name, evsel__name(pos));
1416 		first = false;
1417 	}
1418 	strbuf_addstr(&sb, "])");
1419 	result = PyUnicode_FromString(sb.buf);
1420 	strbuf_release(&sb);
1421 	return result;
1422 }
1423 
1424 static PySequenceMethods pyrf_evlist__sequence_methods = {
1425 	.sq_length = pyrf_evlist__length,
1426 	.sq_item   = pyrf_evlist__item,
1427 };
1428 
1429 static const char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1430 
1431 static PyTypeObject pyrf_evlist__type = {
1432 	PyVarObject_HEAD_INIT(NULL, 0)
1433 	.tp_name	= "perf.evlist",
1434 	.tp_basicsize	= sizeof(struct pyrf_evlist),
1435 	.tp_dealloc	= (destructor)pyrf_evlist__delete,
1436 	.tp_flags	= Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1437 	.tp_as_sequence	= &pyrf_evlist__sequence_methods,
1438 	.tp_doc		= pyrf_evlist__doc,
1439 	.tp_methods	= pyrf_evlist__methods,
1440 	.tp_init	= (initproc)pyrf_evlist__init,
1441 	.tp_repr        = pyrf_evlist__str,
1442 	.tp_str         = pyrf_evlist__str,
1443 };
1444 
1445 static int pyrf_evlist__setup_types(void)
1446 {
1447 	pyrf_evlist__type.tp_new = PyType_GenericNew;
1448 	return PyType_Ready(&pyrf_evlist__type);
1449 }
1450 
1451 #define PERF_CONST(name) { #name, PERF_##name }
1452 
1453 struct perf_constant {
1454 	const char *name;
1455 	int	    value;
1456 };
1457 
1458 static const struct perf_constant perf__constants[] = {
1459 	PERF_CONST(TYPE_HARDWARE),
1460 	PERF_CONST(TYPE_SOFTWARE),
1461 	PERF_CONST(TYPE_TRACEPOINT),
1462 	PERF_CONST(TYPE_HW_CACHE),
1463 	PERF_CONST(TYPE_RAW),
1464 	PERF_CONST(TYPE_BREAKPOINT),
1465 
1466 	PERF_CONST(COUNT_HW_CPU_CYCLES),
1467 	PERF_CONST(COUNT_HW_INSTRUCTIONS),
1468 	PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1469 	PERF_CONST(COUNT_HW_CACHE_MISSES),
1470 	PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1471 	PERF_CONST(COUNT_HW_BRANCH_MISSES),
1472 	PERF_CONST(COUNT_HW_BUS_CYCLES),
1473 	PERF_CONST(COUNT_HW_CACHE_L1D),
1474 	PERF_CONST(COUNT_HW_CACHE_L1I),
1475 	PERF_CONST(COUNT_HW_CACHE_LL),
1476 	PERF_CONST(COUNT_HW_CACHE_DTLB),
1477 	PERF_CONST(COUNT_HW_CACHE_ITLB),
1478 	PERF_CONST(COUNT_HW_CACHE_BPU),
1479 	PERF_CONST(COUNT_HW_CACHE_OP_READ),
1480 	PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1481 	PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1482 	PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1483 	PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1484 
1485 	PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1486 	PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1487 
1488 	PERF_CONST(COUNT_SW_CPU_CLOCK),
1489 	PERF_CONST(COUNT_SW_TASK_CLOCK),
1490 	PERF_CONST(COUNT_SW_PAGE_FAULTS),
1491 	PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1492 	PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1493 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1494 	PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1495 	PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1496 	PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1497 	PERF_CONST(COUNT_SW_DUMMY),
1498 
1499 	PERF_CONST(SAMPLE_IP),
1500 	PERF_CONST(SAMPLE_TID),
1501 	PERF_CONST(SAMPLE_TIME),
1502 	PERF_CONST(SAMPLE_ADDR),
1503 	PERF_CONST(SAMPLE_READ),
1504 	PERF_CONST(SAMPLE_CALLCHAIN),
1505 	PERF_CONST(SAMPLE_ID),
1506 	PERF_CONST(SAMPLE_CPU),
1507 	PERF_CONST(SAMPLE_PERIOD),
1508 	PERF_CONST(SAMPLE_STREAM_ID),
1509 	PERF_CONST(SAMPLE_RAW),
1510 
1511 	PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1512 	PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1513 	PERF_CONST(FORMAT_ID),
1514 	PERF_CONST(FORMAT_GROUP),
1515 
1516 	PERF_CONST(RECORD_MMAP),
1517 	PERF_CONST(RECORD_LOST),
1518 	PERF_CONST(RECORD_COMM),
1519 	PERF_CONST(RECORD_EXIT),
1520 	PERF_CONST(RECORD_THROTTLE),
1521 	PERF_CONST(RECORD_UNTHROTTLE),
1522 	PERF_CONST(RECORD_FORK),
1523 	PERF_CONST(RECORD_READ),
1524 	PERF_CONST(RECORD_SAMPLE),
1525 	PERF_CONST(RECORD_MMAP2),
1526 	PERF_CONST(RECORD_AUX),
1527 	PERF_CONST(RECORD_ITRACE_START),
1528 	PERF_CONST(RECORD_LOST_SAMPLES),
1529 	PERF_CONST(RECORD_SWITCH),
1530 	PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1531 
1532 	PERF_CONST(RECORD_MISC_SWITCH_OUT),
1533 	{ .name = NULL, },
1534 };
1535 
1536 static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1537 				  PyObject *args, PyObject *kwargs)
1538 {
1539 #ifndef HAVE_LIBTRACEEVENT
1540 	return NULL;
1541 #else
1542 	struct tep_event *tp_format;
1543 	static char *kwlist[] = { "sys", "name", NULL };
1544 	char *sys  = NULL;
1545 	char *name = NULL;
1546 
1547 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1548 					 &sys, &name))
1549 		return NULL;
1550 
1551 	tp_format = trace_event__tp_format(sys, name);
1552 	if (IS_ERR(tp_format))
1553 		return PyLong_FromLong(-1);
1554 
1555 	return PyLong_FromLong(tp_format->id);
1556 #endif // HAVE_LIBTRACEEVENT
1557 }
1558 
1559 static PyObject *pyrf_evsel__from_evsel(struct evsel *evsel)
1560 {
1561 	struct pyrf_evsel *pevsel = PyObject_New(struct pyrf_evsel, &pyrf_evsel__type);
1562 
1563 	if (!pevsel)
1564 		return NULL;
1565 
1566 	memset(&pevsel->evsel, 0, sizeof(pevsel->evsel));
1567 	evsel__init(&pevsel->evsel, &evsel->core.attr, evsel->core.idx);
1568 
1569 	evsel__clone(&pevsel->evsel, evsel);
1570 	if (evsel__is_group_leader(evsel))
1571 		evsel__set_leader(&pevsel->evsel, &pevsel->evsel);
1572 	return (PyObject *)pevsel;
1573 }
1574 
1575 static int evlist__pos(struct evlist *evlist, struct evsel *evsel)
1576 {
1577 	struct evsel *pos;
1578 	int idx = 0;
1579 
1580 	evlist__for_each_entry(evlist, pos) {
1581 		if (evsel == pos)
1582 			return idx;
1583 		idx++;
1584 	}
1585 	return -1;
1586 }
1587 
1588 static struct evsel *evlist__at(struct evlist *evlist, int idx)
1589 {
1590 	struct evsel *pos;
1591 	int idx2 = 0;
1592 
1593 	evlist__for_each_entry(evlist, pos) {
1594 		if (idx == idx2)
1595 			return pos;
1596 		idx2++;
1597 	}
1598 	return NULL;
1599 }
1600 
1601 static PyObject *pyrf_evlist__from_evlist(struct evlist *evlist)
1602 {
1603 	struct pyrf_evlist *pevlist = PyObject_New(struct pyrf_evlist, &pyrf_evlist__type);
1604 	struct evsel *pos;
1605 	struct rb_node *node;
1606 
1607 	if (!pevlist)
1608 		return NULL;
1609 
1610 	memset(&pevlist->evlist, 0, sizeof(pevlist->evlist));
1611 	evlist__init(&pevlist->evlist, evlist->core.all_cpus, evlist->core.threads);
1612 	evlist__for_each_entry(evlist, pos) {
1613 		struct pyrf_evsel *pevsel = (void *)pyrf_evsel__from_evsel(pos);
1614 
1615 		evlist__add(&pevlist->evlist, &pevsel->evsel);
1616 	}
1617 	evlist__for_each_entry(&pevlist->evlist, pos) {
1618 		struct evsel *leader = evsel__leader(pos);
1619 
1620 		if (pos != leader) {
1621 			int idx = evlist__pos(evlist, leader);
1622 
1623 			if (idx >= 0)
1624 				evsel__set_leader(pos, evlist__at(&pevlist->evlist, idx));
1625 			else if (leader == NULL)
1626 				evsel__set_leader(pos, pos);
1627 		}
1628 	}
1629 	metricgroup__copy_metric_events(&pevlist->evlist, /*cgrp=*/NULL,
1630 					&pevlist->evlist.metric_events,
1631 					&evlist->metric_events);
1632 	for (node = rb_first_cached(&pevlist->evlist.metric_events.entries); node;
1633 	     node = rb_next(node)) {
1634 		struct metric_event *me = container_of(node, struct metric_event, nd);
1635 		struct list_head *mpos;
1636 		int idx = evlist__pos(evlist, me->evsel);
1637 
1638 		if (idx >= 0)
1639 			me->evsel = evlist__at(&pevlist->evlist, idx);
1640 		list_for_each(mpos, &me->head) {
1641 			struct metric_expr *e = container_of(mpos, struct metric_expr, nd);
1642 
1643 			for (int j = 0; e->metric_events[j]; j++) {
1644 				idx = evlist__pos(evlist, e->metric_events[j]);
1645 				if (idx >= 0)
1646 					e->metric_events[j] = evlist__at(&pevlist->evlist, idx);
1647 			}
1648 		}
1649 	}
1650 	return (PyObject *)pevlist;
1651 }
1652 
1653 static PyObject *pyrf__parse_events(PyObject *self, PyObject *args)
1654 {
1655 	const char *input;
1656 	struct evlist evlist = {};
1657 	struct parse_events_error err;
1658 	PyObject *result;
1659 	PyObject *pcpus = NULL, *pthreads = NULL;
1660 	struct perf_cpu_map *cpus;
1661 	struct perf_thread_map *threads;
1662 
1663 	if (!PyArg_ParseTuple(args, "s|OO", &input, &pcpus, &pthreads))
1664 		return NULL;
1665 
1666 	threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads : NULL;
1667 	cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL;
1668 
1669 	parse_events_error__init(&err);
1670 	evlist__init(&evlist, cpus, threads);
1671 	if (parse_events(&evlist, input, &err)) {
1672 		parse_events_error__print(&err, input);
1673 		PyErr_SetFromErrno(PyExc_OSError);
1674 		return NULL;
1675 	}
1676 	result = pyrf_evlist__from_evlist(&evlist);
1677 	evlist__exit(&evlist);
1678 	return result;
1679 }
1680 
1681 static PyMethodDef perf__methods[] = {
1682 	{
1683 		.ml_name  = "tracepoint",
1684 		.ml_meth  = (PyCFunction) pyrf__tracepoint,
1685 		.ml_flags = METH_VARARGS | METH_KEYWORDS,
1686 		.ml_doc	  = PyDoc_STR("Get tracepoint config.")
1687 	},
1688 	{
1689 		.ml_name  = "parse_events",
1690 		.ml_meth  = (PyCFunction) pyrf__parse_events,
1691 		.ml_flags = METH_VARARGS,
1692 		.ml_doc	  = PyDoc_STR("Parse a string of events and return an evlist.")
1693 	},
1694 	{ .ml_name = NULL, }
1695 };
1696 
1697 PyMODINIT_FUNC PyInit_perf(void)
1698 {
1699 	PyObject *obj;
1700 	int i;
1701 	PyObject *dict;
1702 	static struct PyModuleDef moduledef = {
1703 		PyModuleDef_HEAD_INIT,
1704 		"perf",			/* m_name */
1705 		"",			/* m_doc */
1706 		-1,			/* m_size */
1707 		perf__methods,		/* m_methods */
1708 		NULL,			/* m_reload */
1709 		NULL,			/* m_traverse */
1710 		NULL,			/* m_clear */
1711 		NULL,			/* m_free */
1712 	};
1713 	PyObject *module = PyModule_Create(&moduledef);
1714 
1715 	if (module == NULL ||
1716 	    pyrf_event__setup_types() < 0 ||
1717 	    pyrf_evlist__setup_types() < 0 ||
1718 	    pyrf_evsel__setup_types() < 0 ||
1719 	    pyrf_thread_map__setup_types() < 0 ||
1720 	    pyrf_cpu_map__setup_types() < 0 ||
1721 	    pyrf_counts_values__setup_types() < 0)
1722 		return module;
1723 
1724 	/* The page_size is placed in util object. */
1725 	page_size = sysconf(_SC_PAGE_SIZE);
1726 
1727 	Py_INCREF(&pyrf_evlist__type);
1728 	PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1729 
1730 	Py_INCREF(&pyrf_evsel__type);
1731 	PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1732 
1733 	Py_INCREF(&pyrf_mmap_event__type);
1734 	PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1735 
1736 	Py_INCREF(&pyrf_lost_event__type);
1737 	PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1738 
1739 	Py_INCREF(&pyrf_comm_event__type);
1740 	PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1741 
1742 	Py_INCREF(&pyrf_task_event__type);
1743 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1744 
1745 	Py_INCREF(&pyrf_throttle_event__type);
1746 	PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1747 
1748 	Py_INCREF(&pyrf_task_event__type);
1749 	PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1750 
1751 	Py_INCREF(&pyrf_read_event__type);
1752 	PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1753 
1754 	Py_INCREF(&pyrf_sample_event__type);
1755 	PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1756 
1757 	Py_INCREF(&pyrf_context_switch_event__type);
1758 	PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1759 
1760 	Py_INCREF(&pyrf_thread_map__type);
1761 	PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1762 
1763 	Py_INCREF(&pyrf_cpu_map__type);
1764 	PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1765 
1766 	Py_INCREF(&pyrf_counts_values__type);
1767 	PyModule_AddObject(module, "counts_values", (PyObject *)&pyrf_counts_values__type);
1768 
1769 	dict = PyModule_GetDict(module);
1770 	if (dict == NULL)
1771 		goto error;
1772 
1773 	for (i = 0; perf__constants[i].name != NULL; i++) {
1774 		obj = PyLong_FromLong(perf__constants[i].value);
1775 		if (obj == NULL)
1776 			goto error;
1777 		PyDict_SetItemString(dict, perf__constants[i].name, obj);
1778 		Py_DECREF(obj);
1779 	}
1780 
1781 error:
1782 	if (PyErr_Occurred())
1783 		PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1784 	return module;
1785 }
1786