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