perf python: Fix build on 32-bit
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / tools / perf / util / python.c
1 #include <Python.h>
2 #include <structmember.h>
3 #include <inttypes.h>
4 #include <poll.h>
5 #include "evlist.h"
6 #include "evsel.h"
7 #include "event.h"
8 #include "cpumap.h"
9 #include "thread_map.h"
10
11 struct throttle_event {
12 struct perf_event_header header;
13 u64 time;
14 u64 id;
15 u64 stream_id;
16 };
17
18 PyMODINIT_FUNC initperf(void);
19
20 #define member_def(type, member, ptype, help) \
21 { #member, ptype, \
22 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
23 0, help }
24
25 #define sample_member_def(name, member, ptype, help) \
26 { #name, ptype, \
27 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
28 0, help }
29
30 struct pyrf_event {
31 PyObject_HEAD
32 struct perf_sample sample;
33 union perf_event event;
34 };
35
36 #define sample_members \
37 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
38 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
39 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
40 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
41 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
42 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
43 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
44 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
45 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
46
47 static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
48
49 static PyMemberDef pyrf_mmap_event__members[] = {
50 sample_members
51 member_def(perf_event_header, type, T_UINT, "event type"),
52 member_def(mmap_event, pid, T_UINT, "event pid"),
53 member_def(mmap_event, tid, T_UINT, "event tid"),
54 member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
55 member_def(mmap_event, len, T_ULONGLONG, "map length"),
56 member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
57 member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
58 { .name = NULL, },
59 };
60
61 static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
62 {
63 PyObject *ret;
64 char *s;
65
66 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
67 "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
68 "filename: %s }",
69 pevent->event.mmap.pid, pevent->event.mmap.tid,
70 pevent->event.mmap.start, pevent->event.mmap.len,
71 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
72 ret = PyErr_NoMemory();
73 } else {
74 ret = PyString_FromString(s);
75 free(s);
76 }
77 return ret;
78 }
79
80 static PyTypeObject pyrf_mmap_event__type = {
81 PyVarObject_HEAD_INIT(NULL, 0)
82 .tp_name = "perf.mmap_event",
83 .tp_basicsize = sizeof(struct pyrf_event),
84 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
85 .tp_doc = pyrf_mmap_event__doc,
86 .tp_members = pyrf_mmap_event__members,
87 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
88 };
89
90 static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
91
92 static PyMemberDef pyrf_task_event__members[] = {
93 sample_members
94 member_def(perf_event_header, type, T_UINT, "event type"),
95 member_def(fork_event, pid, T_UINT, "event pid"),
96 member_def(fork_event, ppid, T_UINT, "event ppid"),
97 member_def(fork_event, tid, T_UINT, "event tid"),
98 member_def(fork_event, ptid, T_UINT, "event ptid"),
99 member_def(fork_event, time, T_ULONGLONG, "timestamp"),
100 { .name = NULL, },
101 };
102
103 static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
104 {
105 return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
106 "ptid: %u, time: %" PRIu64 "}",
107 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
108 pevent->event.fork.pid,
109 pevent->event.fork.ppid,
110 pevent->event.fork.tid,
111 pevent->event.fork.ptid,
112 pevent->event.fork.time);
113 }
114
115 static PyTypeObject pyrf_task_event__type = {
116 PyVarObject_HEAD_INIT(NULL, 0)
117 .tp_name = "perf.task_event",
118 .tp_basicsize = sizeof(struct pyrf_event),
119 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
120 .tp_doc = pyrf_task_event__doc,
121 .tp_members = pyrf_task_event__members,
122 .tp_repr = (reprfunc)pyrf_task_event__repr,
123 };
124
125 static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
126
127 static PyMemberDef pyrf_comm_event__members[] = {
128 sample_members
129 member_def(perf_event_header, type, T_UINT, "event type"),
130 member_def(comm_event, pid, T_UINT, "event pid"),
131 member_def(comm_event, tid, T_UINT, "event tid"),
132 member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
133 { .name = NULL, },
134 };
135
136 static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
137 {
138 return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
139 pevent->event.comm.pid,
140 pevent->event.comm.tid,
141 pevent->event.comm.comm);
142 }
143
144 static PyTypeObject pyrf_comm_event__type = {
145 PyVarObject_HEAD_INIT(NULL, 0)
146 .tp_name = "perf.comm_event",
147 .tp_basicsize = sizeof(struct pyrf_event),
148 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
149 .tp_doc = pyrf_comm_event__doc,
150 .tp_members = pyrf_comm_event__members,
151 .tp_repr = (reprfunc)pyrf_comm_event__repr,
152 };
153
154 static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
155
156 static PyMemberDef pyrf_throttle_event__members[] = {
157 sample_members
158 member_def(perf_event_header, type, T_UINT, "event type"),
159 member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
160 member_def(throttle_event, id, T_ULONGLONG, "event id"),
161 member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
162 { .name = NULL, },
163 };
164
165 static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
166 {
167 struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
168
169 return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
170 ", stream_id: %" PRIu64 " }",
171 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
172 te->time, te->id, te->stream_id);
173 }
174
175 static PyTypeObject pyrf_throttle_event__type = {
176 PyVarObject_HEAD_INIT(NULL, 0)
177 .tp_name = "perf.throttle_event",
178 .tp_basicsize = sizeof(struct pyrf_event),
179 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
180 .tp_doc = pyrf_throttle_event__doc,
181 .tp_members = pyrf_throttle_event__members,
182 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
183 };
184
185 static int pyrf_event__setup_types(void)
186 {
187 int err;
188 pyrf_mmap_event__type.tp_new =
189 pyrf_task_event__type.tp_new =
190 pyrf_comm_event__type.tp_new =
191 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
192 err = PyType_Ready(&pyrf_mmap_event__type);
193 if (err < 0)
194 goto out;
195 err = PyType_Ready(&pyrf_task_event__type);
196 if (err < 0)
197 goto out;
198 err = PyType_Ready(&pyrf_comm_event__type);
199 if (err < 0)
200 goto out;
201 err = PyType_Ready(&pyrf_throttle_event__type);
202 if (err < 0)
203 goto out;
204 out:
205 return err;
206 }
207
208 static PyTypeObject *pyrf_event__type[] = {
209 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
210 [PERF_RECORD_LOST] = &pyrf_mmap_event__type,
211 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
212 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
213 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
214 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
215 [PERF_RECORD_FORK] = &pyrf_task_event__type,
216 [PERF_RECORD_READ] = &pyrf_mmap_event__type,
217 [PERF_RECORD_SAMPLE] = &pyrf_mmap_event__type,
218 };
219
220 static PyObject *pyrf_event__new(union perf_event *event)
221 {
222 struct pyrf_event *pevent;
223 PyTypeObject *ptype;
224
225 if (event->header.type < PERF_RECORD_MMAP ||
226 event->header.type > PERF_RECORD_SAMPLE)
227 return NULL;
228
229 ptype = pyrf_event__type[event->header.type];
230 pevent = PyObject_New(struct pyrf_event, ptype);
231 if (pevent != NULL)
232 memcpy(&pevent->event, event, event->header.size);
233 return (PyObject *)pevent;
234 }
235
236 struct pyrf_cpu_map {
237 PyObject_HEAD
238
239 struct cpu_map *cpus;
240 };
241
242 static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
243 PyObject *args, PyObject *kwargs)
244 {
245 static char *kwlist[] = { "cpustr", NULL, NULL, };
246 char *cpustr = NULL;
247
248 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
249 kwlist, &cpustr))
250 return -1;
251
252 pcpus->cpus = cpu_map__new(cpustr);
253 if (pcpus->cpus == NULL)
254 return -1;
255 return 0;
256 }
257
258 static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
259 {
260 cpu_map__delete(pcpus->cpus);
261 pcpus->ob_type->tp_free((PyObject*)pcpus);
262 }
263
264 static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
265 {
266 struct pyrf_cpu_map *pcpus = (void *)obj;
267
268 return pcpus->cpus->nr;
269 }
270
271 static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
272 {
273 struct pyrf_cpu_map *pcpus = (void *)obj;
274
275 if (i >= pcpus->cpus->nr)
276 return NULL;
277
278 return Py_BuildValue("i", pcpus->cpus->map[i]);
279 }
280
281 static PySequenceMethods pyrf_cpu_map__sequence_methods = {
282 .sq_length = pyrf_cpu_map__length,
283 .sq_item = pyrf_cpu_map__item,
284 };
285
286 static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
287
288 static PyTypeObject pyrf_cpu_map__type = {
289 PyVarObject_HEAD_INIT(NULL, 0)
290 .tp_name = "perf.cpu_map",
291 .tp_basicsize = sizeof(struct pyrf_cpu_map),
292 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
293 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
294 .tp_doc = pyrf_cpu_map__doc,
295 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
296 .tp_init = (initproc)pyrf_cpu_map__init,
297 };
298
299 static int pyrf_cpu_map__setup_types(void)
300 {
301 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
302 return PyType_Ready(&pyrf_cpu_map__type);
303 }
304
305 struct pyrf_thread_map {
306 PyObject_HEAD
307
308 struct thread_map *threads;
309 };
310
311 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
312 PyObject *args, PyObject *kwargs)
313 {
314 static char *kwlist[] = { "pid", "tid", NULL, NULL, };
315 int pid = -1, tid = -1;
316
317 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii",
318 kwlist, &pid, &tid))
319 return -1;
320
321 pthreads->threads = thread_map__new(pid, tid);
322 if (pthreads->threads == NULL)
323 return -1;
324 return 0;
325 }
326
327 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
328 {
329 thread_map__delete(pthreads->threads);
330 pthreads->ob_type->tp_free((PyObject*)pthreads);
331 }
332
333 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
334 {
335 struct pyrf_thread_map *pthreads = (void *)obj;
336
337 return pthreads->threads->nr;
338 }
339
340 static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
341 {
342 struct pyrf_thread_map *pthreads = (void *)obj;
343
344 if (i >= pthreads->threads->nr)
345 return NULL;
346
347 return Py_BuildValue("i", pthreads->threads->map[i]);
348 }
349
350 static PySequenceMethods pyrf_thread_map__sequence_methods = {
351 .sq_length = pyrf_thread_map__length,
352 .sq_item = pyrf_thread_map__item,
353 };
354
355 static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
356
357 static PyTypeObject pyrf_thread_map__type = {
358 PyVarObject_HEAD_INIT(NULL, 0)
359 .tp_name = "perf.thread_map",
360 .tp_basicsize = sizeof(struct pyrf_thread_map),
361 .tp_dealloc = (destructor)pyrf_thread_map__delete,
362 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
363 .tp_doc = pyrf_thread_map__doc,
364 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
365 .tp_init = (initproc)pyrf_thread_map__init,
366 };
367
368 static int pyrf_thread_map__setup_types(void)
369 {
370 pyrf_thread_map__type.tp_new = PyType_GenericNew;
371 return PyType_Ready(&pyrf_thread_map__type);
372 }
373
374 struct pyrf_evsel {
375 PyObject_HEAD
376
377 struct perf_evsel evsel;
378 };
379
380 static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
381 PyObject *args, PyObject *kwargs)
382 {
383 struct perf_event_attr attr = {
384 .type = PERF_TYPE_HARDWARE,
385 .config = PERF_COUNT_HW_CPU_CYCLES,
386 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
387 };
388 static char *kwlist[] = {
389 "type",
390 "config",
391 "sample_freq",
392 "sample_period",
393 "sample_type",
394 "read_format",
395 "disabled",
396 "inherit",
397 "pinned",
398 "exclusive",
399 "exclude_user",
400 "exclude_kernel",
401 "exclude_hv",
402 "exclude_idle",
403 "mmap",
404 "comm",
405 "freq",
406 "inherit_stat",
407 "enable_on_exec",
408 "task",
409 "watermark",
410 "precise_ip",
411 "mmap_data",
412 "sample_id_all",
413 "wakeup_events",
414 "bp_type",
415 "bp_addr",
416 "bp_len", NULL, NULL, };
417 u64 sample_period = 0;
418 u32 disabled = 0,
419 inherit = 0,
420 pinned = 0,
421 exclusive = 0,
422 exclude_user = 0,
423 exclude_kernel = 0,
424 exclude_hv = 0,
425 exclude_idle = 0,
426 mmap = 0,
427 comm = 0,
428 freq = 1,
429 inherit_stat = 0,
430 enable_on_exec = 0,
431 task = 0,
432 watermark = 0,
433 precise_ip = 0,
434 mmap_data = 0,
435 sample_id_all = 1;
436 int idx = 0;
437
438 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
439 "|iKiKKiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
440 &attr.type, &attr.config, &attr.sample_freq,
441 &sample_period, &attr.sample_type,
442 &attr.read_format, &disabled, &inherit,
443 &pinned, &exclusive, &exclude_user,
444 &exclude_kernel, &exclude_hv, &exclude_idle,
445 &mmap, &comm, &freq, &inherit_stat,
446 &enable_on_exec, &task, &watermark,
447 &precise_ip, &mmap_data, &sample_id_all,
448 &attr.wakeup_events, &attr.bp_type,
449 &attr.bp_addr, &attr.bp_len, &idx))
450 return -1;
451
452 /* union... */
453 if (sample_period != 0) {
454 if (attr.sample_freq != 0)
455 return -1; /* FIXME: throw right exception */
456 attr.sample_period = sample_period;
457 }
458
459 /* Bitfields */
460 attr.disabled = disabled;
461 attr.inherit = inherit;
462 attr.pinned = pinned;
463 attr.exclusive = exclusive;
464 attr.exclude_user = exclude_user;
465 attr.exclude_kernel = exclude_kernel;
466 attr.exclude_hv = exclude_hv;
467 attr.exclude_idle = exclude_idle;
468 attr.mmap = mmap;
469 attr.comm = comm;
470 attr.freq = freq;
471 attr.inherit_stat = inherit_stat;
472 attr.enable_on_exec = enable_on_exec;
473 attr.task = task;
474 attr.watermark = watermark;
475 attr.precise_ip = precise_ip;
476 attr.mmap_data = mmap_data;
477 attr.sample_id_all = sample_id_all;
478
479 perf_evsel__init(&pevsel->evsel, &attr, idx);
480 return 0;
481 }
482
483 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
484 {
485 perf_evsel__exit(&pevsel->evsel);
486 pevsel->ob_type->tp_free((PyObject*)pevsel);
487 }
488
489 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
490 PyObject *args, PyObject *kwargs)
491 {
492 struct perf_evsel *evsel = &pevsel->evsel;
493 struct cpu_map *cpus = NULL;
494 struct thread_map *threads = NULL;
495 PyObject *pcpus = NULL, *pthreads = NULL;
496 int group = 0, overwrite = 0;
497 static char *kwlist[] = {"cpus", "threads", "group", "overwrite", NULL, NULL};
498
499 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
500 &pcpus, &pthreads, &group, &overwrite))
501 return NULL;
502
503 if (pthreads != NULL)
504 threads = ((struct pyrf_thread_map *)pthreads)->threads;
505
506 if (pcpus != NULL)
507 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
508
509 if (perf_evsel__open(evsel, cpus, threads, group, overwrite) < 0) {
510 PyErr_SetFromErrno(PyExc_OSError);
511 return NULL;
512 }
513
514 Py_INCREF(Py_None);
515 return Py_None;
516 }
517
518 static PyMethodDef pyrf_evsel__methods[] = {
519 {
520 .ml_name = "open",
521 .ml_meth = (PyCFunction)pyrf_evsel__open,
522 .ml_flags = METH_VARARGS | METH_KEYWORDS,
523 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
524 },
525 { .ml_name = NULL, }
526 };
527
528 static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
529
530 static PyTypeObject pyrf_evsel__type = {
531 PyVarObject_HEAD_INIT(NULL, 0)
532 .tp_name = "perf.evsel",
533 .tp_basicsize = sizeof(struct pyrf_evsel),
534 .tp_dealloc = (destructor)pyrf_evsel__delete,
535 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
536 .tp_doc = pyrf_evsel__doc,
537 .tp_methods = pyrf_evsel__methods,
538 .tp_init = (initproc)pyrf_evsel__init,
539 };
540
541 static int pyrf_evsel__setup_types(void)
542 {
543 pyrf_evsel__type.tp_new = PyType_GenericNew;
544 return PyType_Ready(&pyrf_evsel__type);
545 }
546
547 struct pyrf_evlist {
548 PyObject_HEAD
549
550 struct perf_evlist evlist;
551 };
552
553 static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
554 PyObject *args, PyObject *kwargs __used)
555 {
556 PyObject *pcpus = NULL, *pthreads = NULL;
557 struct cpu_map *cpus;
558 struct thread_map *threads;
559
560 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
561 return -1;
562
563 threads = ((struct pyrf_thread_map *)pthreads)->threads;
564 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
565 perf_evlist__init(&pevlist->evlist, cpus, threads);
566 return 0;
567 }
568
569 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
570 {
571 perf_evlist__exit(&pevlist->evlist);
572 pevlist->ob_type->tp_free((PyObject*)pevlist);
573 }
574
575 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
576 PyObject *args, PyObject *kwargs)
577 {
578 struct perf_evlist *evlist = &pevlist->evlist;
579 static char *kwlist[] = {"pages", "overwrite",
580 NULL, NULL};
581 int pages = 128, overwrite = false;
582
583 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
584 &pages, &overwrite))
585 return NULL;
586
587 if (perf_evlist__mmap(evlist, pages, overwrite) < 0) {
588 PyErr_SetFromErrno(PyExc_OSError);
589 return NULL;
590 }
591
592 Py_INCREF(Py_None);
593 return Py_None;
594 }
595
596 static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
597 PyObject *args, PyObject *kwargs)
598 {
599 struct perf_evlist *evlist = &pevlist->evlist;
600 static char *kwlist[] = {"timeout", NULL, NULL};
601 int timeout = -1, n;
602
603 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
604 return NULL;
605
606 n = poll(evlist->pollfd, evlist->nr_fds, timeout);
607 if (n < 0) {
608 PyErr_SetFromErrno(PyExc_OSError);
609 return NULL;
610 }
611
612 return Py_BuildValue("i", n);
613 }
614
615 static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
616 PyObject *args __used, PyObject *kwargs __used)
617 {
618 struct perf_evlist *evlist = &pevlist->evlist;
619 PyObject *list = PyList_New(0);
620 int i;
621
622 for (i = 0; i < evlist->nr_fds; ++i) {
623 PyObject *file;
624 FILE *fp = fdopen(evlist->pollfd[i].fd, "r");
625
626 if (fp == NULL)
627 goto free_list;
628
629 file = PyFile_FromFile(fp, "perf", "r", NULL);
630 if (file == NULL)
631 goto free_list;
632
633 if (PyList_Append(list, file) != 0) {
634 Py_DECREF(file);
635 goto free_list;
636 }
637
638 Py_DECREF(file);
639 }
640
641 return list;
642 free_list:
643 return PyErr_NoMemory();
644 }
645
646
647 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
648 PyObject *args, PyObject *kwargs __used)
649 {
650 struct perf_evlist *evlist = &pevlist->evlist;
651 PyObject *pevsel;
652 struct perf_evsel *evsel;
653
654 if (!PyArg_ParseTuple(args, "O", &pevsel))
655 return NULL;
656
657 Py_INCREF(pevsel);
658 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
659 evsel->idx = evlist->nr_entries;
660 perf_evlist__add(evlist, evsel);
661
662 return Py_BuildValue("i", evlist->nr_entries);
663 }
664
665 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
666 PyObject *args, PyObject *kwargs)
667 {
668 struct perf_evlist *evlist = &pevlist->evlist;
669 union perf_event *event;
670 int sample_id_all = 1, cpu;
671 static char *kwlist[] = {"sample_id_all", NULL, NULL};
672
673 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
674 &cpu, &sample_id_all))
675 return NULL;
676
677 event = perf_evlist__read_on_cpu(evlist, cpu);
678 if (event != NULL) {
679 struct perf_evsel *first;
680 PyObject *pyevent = pyrf_event__new(event);
681 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
682
683 if (pyevent == NULL)
684 return PyErr_NoMemory();
685
686 first = list_entry(evlist->entries.next, struct perf_evsel, node);
687 perf_event__parse_sample(event, first->attr.sample_type, sample_id_all,
688 &pevent->sample);
689 return pyevent;
690 }
691
692 Py_INCREF(Py_None);
693 return Py_None;
694 }
695
696 static PyMethodDef pyrf_evlist__methods[] = {
697 {
698 .ml_name = "mmap",
699 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
700 .ml_flags = METH_VARARGS | METH_KEYWORDS,
701 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
702 },
703 {
704 .ml_name = "poll",
705 .ml_meth = (PyCFunction)pyrf_evlist__poll,
706 .ml_flags = METH_VARARGS | METH_KEYWORDS,
707 .ml_doc = PyDoc_STR("poll the file descriptor table.")
708 },
709 {
710 .ml_name = "get_pollfd",
711 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
712 .ml_flags = METH_VARARGS | METH_KEYWORDS,
713 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
714 },
715 {
716 .ml_name = "add",
717 .ml_meth = (PyCFunction)pyrf_evlist__add,
718 .ml_flags = METH_VARARGS | METH_KEYWORDS,
719 .ml_doc = PyDoc_STR("adds an event selector to the list.")
720 },
721 {
722 .ml_name = "read_on_cpu",
723 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
724 .ml_flags = METH_VARARGS | METH_KEYWORDS,
725 .ml_doc = PyDoc_STR("reads an event.")
726 },
727 { .ml_name = NULL, }
728 };
729
730 static Py_ssize_t pyrf_evlist__length(PyObject *obj)
731 {
732 struct pyrf_evlist *pevlist = (void *)obj;
733
734 return pevlist->evlist.nr_entries;
735 }
736
737 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
738 {
739 struct pyrf_evlist *pevlist = (void *)obj;
740 struct perf_evsel *pos;
741
742 if (i >= pevlist->evlist.nr_entries)
743 return NULL;
744
745 list_for_each_entry(pos, &pevlist->evlist.entries, node)
746 if (i-- == 0)
747 break;
748
749 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
750 }
751
752 static PySequenceMethods pyrf_evlist__sequence_methods = {
753 .sq_length = pyrf_evlist__length,
754 .sq_item = pyrf_evlist__item,
755 };
756
757 static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
758
759 static PyTypeObject pyrf_evlist__type = {
760 PyVarObject_HEAD_INIT(NULL, 0)
761 .tp_name = "perf.evlist",
762 .tp_basicsize = sizeof(struct pyrf_evlist),
763 .tp_dealloc = (destructor)pyrf_evlist__delete,
764 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
765 .tp_as_sequence = &pyrf_evlist__sequence_methods,
766 .tp_doc = pyrf_evlist__doc,
767 .tp_methods = pyrf_evlist__methods,
768 .tp_init = (initproc)pyrf_evlist__init,
769 };
770
771 static int pyrf_evlist__setup_types(void)
772 {
773 pyrf_evlist__type.tp_new = PyType_GenericNew;
774 return PyType_Ready(&pyrf_evlist__type);
775 }
776
777 static struct {
778 const char *name;
779 int value;
780 } perf__constants[] = {
781 { "TYPE_HARDWARE", PERF_TYPE_HARDWARE },
782 { "TYPE_SOFTWARE", PERF_TYPE_SOFTWARE },
783 { "TYPE_TRACEPOINT", PERF_TYPE_TRACEPOINT },
784 { "TYPE_HW_CACHE", PERF_TYPE_HW_CACHE },
785 { "TYPE_RAW", PERF_TYPE_RAW },
786 { "TYPE_BREAKPOINT", PERF_TYPE_BREAKPOINT },
787
788 { "COUNT_HW_CPU_CYCLES", PERF_COUNT_HW_CPU_CYCLES },
789 { "COUNT_HW_INSTRUCTIONS", PERF_COUNT_HW_INSTRUCTIONS },
790 { "COUNT_HW_CACHE_REFERENCES", PERF_COUNT_HW_CACHE_REFERENCES },
791 { "COUNT_HW_CACHE_MISSES", PERF_COUNT_HW_CACHE_MISSES },
792 { "COUNT_HW_BRANCH_INSTRUCTIONS", PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
793 { "COUNT_HW_BRANCH_MISSES", PERF_COUNT_HW_BRANCH_MISSES },
794 { "COUNT_HW_BUS_CYCLES", PERF_COUNT_HW_BUS_CYCLES },
795 { "COUNT_HW_CACHE_L1D", PERF_COUNT_HW_CACHE_L1D },
796 { "COUNT_HW_CACHE_L1I", PERF_COUNT_HW_CACHE_L1I },
797 { "COUNT_HW_CACHE_LL", PERF_COUNT_HW_CACHE_LL },
798 { "COUNT_HW_CACHE_DTLB", PERF_COUNT_HW_CACHE_DTLB },
799 { "COUNT_HW_CACHE_ITLB", PERF_COUNT_HW_CACHE_ITLB },
800 { "COUNT_HW_CACHE_BPU", PERF_COUNT_HW_CACHE_BPU },
801 { "COUNT_HW_CACHE_OP_READ", PERF_COUNT_HW_CACHE_OP_READ },
802 { "COUNT_HW_CACHE_OP_WRITE", PERF_COUNT_HW_CACHE_OP_WRITE },
803 { "COUNT_HW_CACHE_OP_PREFETCH", PERF_COUNT_HW_CACHE_OP_PREFETCH },
804 { "COUNT_HW_CACHE_RESULT_ACCESS", PERF_COUNT_HW_CACHE_RESULT_ACCESS },
805 { "COUNT_HW_CACHE_RESULT_MISS", PERF_COUNT_HW_CACHE_RESULT_MISS },
806
807 { "COUNT_SW_CPU_CLOCK", PERF_COUNT_SW_CPU_CLOCK },
808 { "COUNT_SW_TASK_CLOCK", PERF_COUNT_SW_TASK_CLOCK },
809 { "COUNT_SW_PAGE_FAULTS", PERF_COUNT_SW_PAGE_FAULTS },
810 { "COUNT_SW_CONTEXT_SWITCHES", PERF_COUNT_SW_CONTEXT_SWITCHES },
811 { "COUNT_SW_CPU_MIGRATIONS", PERF_COUNT_SW_CPU_MIGRATIONS },
812 { "COUNT_SW_PAGE_FAULTS_MIN", PERF_COUNT_SW_PAGE_FAULTS_MIN },
813 { "COUNT_SW_PAGE_FAULTS_MAJ", PERF_COUNT_SW_PAGE_FAULTS_MAJ },
814 { "COUNT_SW_ALIGNMENT_FAULTS", PERF_COUNT_SW_ALIGNMENT_FAULTS },
815 { "COUNT_SW_EMULATION_FAULTS", PERF_COUNT_SW_EMULATION_FAULTS },
816
817 { "SAMPLE_IP", PERF_SAMPLE_IP },
818 { "SAMPLE_TID", PERF_SAMPLE_TID },
819 { "SAMPLE_TIME", PERF_SAMPLE_TIME },
820 { "SAMPLE_ADDR", PERF_SAMPLE_ADDR },
821 { "SAMPLE_READ", PERF_SAMPLE_READ },
822 { "SAMPLE_CALLCHAIN", PERF_SAMPLE_CALLCHAIN },
823 { "SAMPLE_ID", PERF_SAMPLE_ID },
824 { "SAMPLE_CPU", PERF_SAMPLE_CPU },
825 { "SAMPLE_PERIOD", PERF_SAMPLE_PERIOD },
826 { "SAMPLE_STREAM_ID", PERF_SAMPLE_STREAM_ID },
827 { "SAMPLE_RAW", PERF_SAMPLE_RAW },
828
829 { "FORMAT_TOTAL_TIME_ENABLED", PERF_FORMAT_TOTAL_TIME_ENABLED },
830 { "FORMAT_TOTAL_TIME_RUNNING", PERF_FORMAT_TOTAL_TIME_RUNNING },
831 { "FORMAT_ID", PERF_FORMAT_ID },
832 { "FORMAT_GROUP", PERF_FORMAT_GROUP },
833
834 { "RECORD_MMAP", PERF_RECORD_MMAP },
835 { "RECORD_LOST", PERF_RECORD_LOST },
836 { "RECORD_COMM", PERF_RECORD_COMM },
837 { "RECORD_EXIT", PERF_RECORD_EXIT },
838 { "RECORD_THROTTLE", PERF_RECORD_THROTTLE },
839 { "RECORD_UNTHROTTLE", PERF_RECORD_UNTHROTTLE },
840 { "RECORD_FORK", PERF_RECORD_FORK },
841 { "RECORD_READ", PERF_RECORD_READ },
842 { "RECORD_SAMPLE", PERF_RECORD_SAMPLE },
843 { .name = NULL, },
844 };
845
846 static PyMethodDef perf__methods[] = {
847 { .ml_name = NULL, }
848 };
849
850 PyMODINIT_FUNC initperf(void)
851 {
852 PyObject *obj;
853 int i;
854 PyObject *dict, *module = Py_InitModule("perf", perf__methods);
855
856 if (module == NULL ||
857 pyrf_event__setup_types() < 0 ||
858 pyrf_evlist__setup_types() < 0 ||
859 pyrf_evsel__setup_types() < 0 ||
860 pyrf_thread_map__setup_types() < 0 ||
861 pyrf_cpu_map__setup_types() < 0)
862 return;
863
864 Py_INCREF(&pyrf_evlist__type);
865 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
866
867 Py_INCREF(&pyrf_evsel__type);
868 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
869
870 Py_INCREF(&pyrf_thread_map__type);
871 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
872
873 Py_INCREF(&pyrf_cpu_map__type);
874 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
875
876 dict = PyModule_GetDict(module);
877 if (dict == NULL)
878 goto error;
879
880 for (i = 0; perf__constants[i].name != NULL; i++) {
881 obj = PyInt_FromLong(perf__constants[i].value);
882 if (obj == NULL)
883 goto error;
884 PyDict_SetItemString(dict, perf__constants[i].name, obj);
885 Py_DECREF(obj);
886 }
887
888 error:
889 if (PyErr_Occurred())
890 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
891 }