net: Convert dccp_v4_ops
[linux/fpc-iii.git] / tools / perf / util / python.c
blobb1e999bd21ef9ddcbb210bf6526231a5fac7b4d1
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 "evlist.h"
8 #include "callchain.h"
9 #include "evsel.h"
10 #include "event.h"
11 #include "cpumap.h"
12 #include "print_binary.h"
13 #include "thread_map.h"
16 * Provide these two so that we don't have to link against callchain.c and
17 * start dragging hist.c, etc.
19 struct callchain_param callchain_param;
21 int parse_callchain_record(const char *arg __maybe_unused,
22 struct callchain_param *param __maybe_unused)
24 return 0;
28 * Support debug printing even though util/debug.c is not linked. That means
29 * implementing 'verbose' and 'eprintf'.
31 int verbose;
33 int eprintf(int level, int var, const char *fmt, ...)
35 va_list args;
36 int ret = 0;
38 if (var >= level) {
39 va_start(args, fmt);
40 ret = vfprintf(stderr, fmt, args);
41 va_end(args);
44 return ret;
47 /* Define PyVarObject_HEAD_INIT for python 2.5 */
48 #ifndef PyVarObject_HEAD_INIT
49 # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
50 #endif
52 PyMODINIT_FUNC initperf(void);
54 #define member_def(type, member, ptype, help) \
55 { #member, ptype, \
56 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
57 0, help }
59 #define sample_member_def(name, member, ptype, help) \
60 { #name, ptype, \
61 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
62 0, help }
64 struct pyrf_event {
65 PyObject_HEAD
66 struct perf_evsel *evsel;
67 struct perf_sample sample;
68 union perf_event event;
71 #define sample_members \
72 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
73 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
74 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
75 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
76 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
77 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
78 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
79 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
80 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
82 static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
84 static PyMemberDef pyrf_mmap_event__members[] = {
85 sample_members
86 member_def(perf_event_header, type, T_UINT, "event type"),
87 member_def(perf_event_header, misc, T_UINT, "event misc"),
88 member_def(mmap_event, pid, T_UINT, "event pid"),
89 member_def(mmap_event, tid, T_UINT, "event tid"),
90 member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
91 member_def(mmap_event, len, T_ULONGLONG, "map length"),
92 member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
93 member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
94 { .name = NULL, },
97 static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
99 PyObject *ret;
100 char *s;
102 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
103 "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
104 "filename: %s }",
105 pevent->event.mmap.pid, pevent->event.mmap.tid,
106 pevent->event.mmap.start, pevent->event.mmap.len,
107 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
108 ret = PyErr_NoMemory();
109 } else {
110 ret = PyString_FromString(s);
111 free(s);
113 return ret;
116 static PyTypeObject pyrf_mmap_event__type = {
117 PyVarObject_HEAD_INIT(NULL, 0)
118 .tp_name = "perf.mmap_event",
119 .tp_basicsize = sizeof(struct pyrf_event),
120 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
121 .tp_doc = pyrf_mmap_event__doc,
122 .tp_members = pyrf_mmap_event__members,
123 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
126 static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
128 static PyMemberDef pyrf_task_event__members[] = {
129 sample_members
130 member_def(perf_event_header, type, T_UINT, "event type"),
131 member_def(fork_event, pid, T_UINT, "event pid"),
132 member_def(fork_event, ppid, T_UINT, "event ppid"),
133 member_def(fork_event, tid, T_UINT, "event tid"),
134 member_def(fork_event, ptid, T_UINT, "event ptid"),
135 member_def(fork_event, time, T_ULONGLONG, "timestamp"),
136 { .name = NULL, },
139 static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
141 return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
142 "ptid: %u, time: %" PRIu64 "}",
143 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
144 pevent->event.fork.pid,
145 pevent->event.fork.ppid,
146 pevent->event.fork.tid,
147 pevent->event.fork.ptid,
148 pevent->event.fork.time);
151 static PyTypeObject pyrf_task_event__type = {
152 PyVarObject_HEAD_INIT(NULL, 0)
153 .tp_name = "perf.task_event",
154 .tp_basicsize = sizeof(struct pyrf_event),
155 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
156 .tp_doc = pyrf_task_event__doc,
157 .tp_members = pyrf_task_event__members,
158 .tp_repr = (reprfunc)pyrf_task_event__repr,
161 static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
163 static PyMemberDef pyrf_comm_event__members[] = {
164 sample_members
165 member_def(perf_event_header, type, T_UINT, "event type"),
166 member_def(comm_event, pid, T_UINT, "event pid"),
167 member_def(comm_event, tid, T_UINT, "event tid"),
168 member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
169 { .name = NULL, },
172 static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
174 return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
175 pevent->event.comm.pid,
176 pevent->event.comm.tid,
177 pevent->event.comm.comm);
180 static PyTypeObject pyrf_comm_event__type = {
181 PyVarObject_HEAD_INIT(NULL, 0)
182 .tp_name = "perf.comm_event",
183 .tp_basicsize = sizeof(struct pyrf_event),
184 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
185 .tp_doc = pyrf_comm_event__doc,
186 .tp_members = pyrf_comm_event__members,
187 .tp_repr = (reprfunc)pyrf_comm_event__repr,
190 static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
192 static PyMemberDef pyrf_throttle_event__members[] = {
193 sample_members
194 member_def(perf_event_header, type, T_UINT, "event type"),
195 member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
196 member_def(throttle_event, id, T_ULONGLONG, "event id"),
197 member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
198 { .name = NULL, },
201 static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
203 struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
205 return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
206 ", stream_id: %" PRIu64 " }",
207 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
208 te->time, te->id, te->stream_id);
211 static PyTypeObject pyrf_throttle_event__type = {
212 PyVarObject_HEAD_INIT(NULL, 0)
213 .tp_name = "perf.throttle_event",
214 .tp_basicsize = sizeof(struct pyrf_event),
215 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
216 .tp_doc = pyrf_throttle_event__doc,
217 .tp_members = pyrf_throttle_event__members,
218 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
221 static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
223 static PyMemberDef pyrf_lost_event__members[] = {
224 sample_members
225 member_def(lost_event, id, T_ULONGLONG, "event id"),
226 member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
227 { .name = NULL, },
230 static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
232 PyObject *ret;
233 char *s;
235 if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
236 "lost: %#" PRIx64 " }",
237 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
238 ret = PyErr_NoMemory();
239 } else {
240 ret = PyString_FromString(s);
241 free(s);
243 return ret;
246 static PyTypeObject pyrf_lost_event__type = {
247 PyVarObject_HEAD_INIT(NULL, 0)
248 .tp_name = "perf.lost_event",
249 .tp_basicsize = sizeof(struct pyrf_event),
250 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
251 .tp_doc = pyrf_lost_event__doc,
252 .tp_members = pyrf_lost_event__members,
253 .tp_repr = (reprfunc)pyrf_lost_event__repr,
256 static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
258 static PyMemberDef pyrf_read_event__members[] = {
259 sample_members
260 member_def(read_event, pid, T_UINT, "event pid"),
261 member_def(read_event, tid, T_UINT, "event tid"),
262 { .name = NULL, },
265 static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
267 return PyString_FromFormat("{ type: read, pid: %u, tid: %u }",
268 pevent->event.read.pid,
269 pevent->event.read.tid);
271 * FIXME: return the array of read values,
272 * making this method useful ;-)
276 static PyTypeObject pyrf_read_event__type = {
277 PyVarObject_HEAD_INIT(NULL, 0)
278 .tp_name = "perf.read_event",
279 .tp_basicsize = sizeof(struct pyrf_event),
280 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
281 .tp_doc = pyrf_read_event__doc,
282 .tp_members = pyrf_read_event__members,
283 .tp_repr = (reprfunc)pyrf_read_event__repr,
286 static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
288 static PyMemberDef pyrf_sample_event__members[] = {
289 sample_members
290 member_def(perf_event_header, type, T_UINT, "event type"),
291 { .name = NULL, },
294 static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
296 PyObject *ret;
297 char *s;
299 if (asprintf(&s, "{ type: sample }") < 0) {
300 ret = PyErr_NoMemory();
301 } else {
302 ret = PyString_FromString(s);
303 free(s);
305 return ret;
308 static bool is_tracepoint(struct pyrf_event *pevent)
310 return pevent->evsel->attr.type == PERF_TYPE_TRACEPOINT;
313 static PyObject*
314 tracepoint_field(struct pyrf_event *pe, struct format_field *field)
316 struct pevent *pevent = field->event->pevent;
317 void *data = pe->sample.raw_data;
318 PyObject *ret = NULL;
319 unsigned long long val;
320 unsigned int offset, len;
322 if (field->flags & FIELD_IS_ARRAY) {
323 offset = field->offset;
324 len = field->size;
325 if (field->flags & FIELD_IS_DYNAMIC) {
326 val = pevent_read_number(pevent, data + offset, len);
327 offset = val;
328 len = offset >> 16;
329 offset &= 0xffff;
331 if (field->flags & FIELD_IS_STRING &&
332 is_printable_array(data + offset, len)) {
333 ret = PyString_FromString((char *)data + offset);
334 } else {
335 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
336 field->flags &= ~FIELD_IS_STRING;
338 } else {
339 val = pevent_read_number(pevent, data + field->offset,
340 field->size);
341 if (field->flags & FIELD_IS_POINTER)
342 ret = PyLong_FromUnsignedLong((unsigned long) val);
343 else if (field->flags & FIELD_IS_SIGNED)
344 ret = PyLong_FromLong((long) val);
345 else
346 ret = PyLong_FromUnsignedLong((unsigned long) val);
349 return ret;
352 static PyObject*
353 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
355 const char *str = PyString_AsString(PyObject_Str(attr_name));
356 struct perf_evsel *evsel = pevent->evsel;
357 struct format_field *field;
359 if (!evsel->tp_format) {
360 struct event_format *tp_format;
362 tp_format = trace_event__tp_format_id(evsel->attr.config);
363 if (!tp_format)
364 return NULL;
366 evsel->tp_format = tp_format;
369 field = pevent_find_any_field(evsel->tp_format, str);
370 if (!field)
371 return NULL;
373 return tracepoint_field(pevent, field);
376 static PyObject*
377 pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
379 PyObject *obj = NULL;
381 if (is_tracepoint(pevent))
382 obj = get_tracepoint_field(pevent, attr_name);
384 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
387 static PyTypeObject pyrf_sample_event__type = {
388 PyVarObject_HEAD_INIT(NULL, 0)
389 .tp_name = "perf.sample_event",
390 .tp_basicsize = sizeof(struct pyrf_event),
391 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
392 .tp_doc = pyrf_sample_event__doc,
393 .tp_members = pyrf_sample_event__members,
394 .tp_repr = (reprfunc)pyrf_sample_event__repr,
395 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
398 static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
400 static PyMemberDef pyrf_context_switch_event__members[] = {
401 sample_members
402 member_def(perf_event_header, type, T_UINT, "event type"),
403 member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"),
404 member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"),
405 { .name = NULL, },
408 static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
410 PyObject *ret;
411 char *s;
413 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
414 pevent->event.context_switch.next_prev_pid,
415 pevent->event.context_switch.next_prev_tid,
416 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
417 ret = PyErr_NoMemory();
418 } else {
419 ret = PyString_FromString(s);
420 free(s);
422 return ret;
425 static PyTypeObject pyrf_context_switch_event__type = {
426 PyVarObject_HEAD_INIT(NULL, 0)
427 .tp_name = "perf.context_switch_event",
428 .tp_basicsize = sizeof(struct pyrf_event),
429 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
430 .tp_doc = pyrf_context_switch_event__doc,
431 .tp_members = pyrf_context_switch_event__members,
432 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
435 static int pyrf_event__setup_types(void)
437 int err;
438 pyrf_mmap_event__type.tp_new =
439 pyrf_task_event__type.tp_new =
440 pyrf_comm_event__type.tp_new =
441 pyrf_lost_event__type.tp_new =
442 pyrf_read_event__type.tp_new =
443 pyrf_sample_event__type.tp_new =
444 pyrf_context_switch_event__type.tp_new =
445 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
446 err = PyType_Ready(&pyrf_mmap_event__type);
447 if (err < 0)
448 goto out;
449 err = PyType_Ready(&pyrf_lost_event__type);
450 if (err < 0)
451 goto out;
452 err = PyType_Ready(&pyrf_task_event__type);
453 if (err < 0)
454 goto out;
455 err = PyType_Ready(&pyrf_comm_event__type);
456 if (err < 0)
457 goto out;
458 err = PyType_Ready(&pyrf_throttle_event__type);
459 if (err < 0)
460 goto out;
461 err = PyType_Ready(&pyrf_read_event__type);
462 if (err < 0)
463 goto out;
464 err = PyType_Ready(&pyrf_sample_event__type);
465 if (err < 0)
466 goto out;
467 err = PyType_Ready(&pyrf_context_switch_event__type);
468 if (err < 0)
469 goto out;
470 out:
471 return err;
474 static PyTypeObject *pyrf_event__type[] = {
475 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
476 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
477 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
478 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
479 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
480 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
481 [PERF_RECORD_FORK] = &pyrf_task_event__type,
482 [PERF_RECORD_READ] = &pyrf_read_event__type,
483 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
484 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
485 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
488 static PyObject *pyrf_event__new(union perf_event *event)
490 struct pyrf_event *pevent;
491 PyTypeObject *ptype;
493 if ((event->header.type < PERF_RECORD_MMAP ||
494 event->header.type > PERF_RECORD_SAMPLE) &&
495 !(event->header.type == PERF_RECORD_SWITCH ||
496 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
497 return NULL;
499 ptype = pyrf_event__type[event->header.type];
500 pevent = PyObject_New(struct pyrf_event, ptype);
501 if (pevent != NULL)
502 memcpy(&pevent->event, event, event->header.size);
503 return (PyObject *)pevent;
506 struct pyrf_cpu_map {
507 PyObject_HEAD
509 struct cpu_map *cpus;
512 static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
513 PyObject *args, PyObject *kwargs)
515 static char *kwlist[] = { "cpustr", NULL };
516 char *cpustr = NULL;
518 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
519 kwlist, &cpustr))
520 return -1;
522 pcpus->cpus = cpu_map__new(cpustr);
523 if (pcpus->cpus == NULL)
524 return -1;
525 return 0;
528 static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
530 cpu_map__put(pcpus->cpus);
531 pcpus->ob_type->tp_free((PyObject*)pcpus);
534 static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
536 struct pyrf_cpu_map *pcpus = (void *)obj;
538 return pcpus->cpus->nr;
541 static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
543 struct pyrf_cpu_map *pcpus = (void *)obj;
545 if (i >= pcpus->cpus->nr)
546 return NULL;
548 return Py_BuildValue("i", pcpus->cpus->map[i]);
551 static PySequenceMethods pyrf_cpu_map__sequence_methods = {
552 .sq_length = pyrf_cpu_map__length,
553 .sq_item = pyrf_cpu_map__item,
556 static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
558 static PyTypeObject pyrf_cpu_map__type = {
559 PyVarObject_HEAD_INIT(NULL, 0)
560 .tp_name = "perf.cpu_map",
561 .tp_basicsize = sizeof(struct pyrf_cpu_map),
562 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
563 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
564 .tp_doc = pyrf_cpu_map__doc,
565 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
566 .tp_init = (initproc)pyrf_cpu_map__init,
569 static int pyrf_cpu_map__setup_types(void)
571 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
572 return PyType_Ready(&pyrf_cpu_map__type);
575 struct pyrf_thread_map {
576 PyObject_HEAD
578 struct thread_map *threads;
581 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
582 PyObject *args, PyObject *kwargs)
584 static char *kwlist[] = { "pid", "tid", "uid", NULL };
585 int pid = -1, tid = -1, uid = UINT_MAX;
587 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
588 kwlist, &pid, &tid, &uid))
589 return -1;
591 pthreads->threads = thread_map__new(pid, tid, uid);
592 if (pthreads->threads == NULL)
593 return -1;
594 return 0;
597 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
599 thread_map__put(pthreads->threads);
600 pthreads->ob_type->tp_free((PyObject*)pthreads);
603 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
605 struct pyrf_thread_map *pthreads = (void *)obj;
607 return pthreads->threads->nr;
610 static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
612 struct pyrf_thread_map *pthreads = (void *)obj;
614 if (i >= pthreads->threads->nr)
615 return NULL;
617 return Py_BuildValue("i", pthreads->threads->map[i]);
620 static PySequenceMethods pyrf_thread_map__sequence_methods = {
621 .sq_length = pyrf_thread_map__length,
622 .sq_item = pyrf_thread_map__item,
625 static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
627 static PyTypeObject pyrf_thread_map__type = {
628 PyVarObject_HEAD_INIT(NULL, 0)
629 .tp_name = "perf.thread_map",
630 .tp_basicsize = sizeof(struct pyrf_thread_map),
631 .tp_dealloc = (destructor)pyrf_thread_map__delete,
632 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
633 .tp_doc = pyrf_thread_map__doc,
634 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
635 .tp_init = (initproc)pyrf_thread_map__init,
638 static int pyrf_thread_map__setup_types(void)
640 pyrf_thread_map__type.tp_new = PyType_GenericNew;
641 return PyType_Ready(&pyrf_thread_map__type);
644 struct pyrf_evsel {
645 PyObject_HEAD
647 struct perf_evsel evsel;
650 static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
651 PyObject *args, PyObject *kwargs)
653 struct perf_event_attr attr = {
654 .type = PERF_TYPE_HARDWARE,
655 .config = PERF_COUNT_HW_CPU_CYCLES,
656 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
658 static char *kwlist[] = {
659 "type",
660 "config",
661 "sample_freq",
662 "sample_period",
663 "sample_type",
664 "read_format",
665 "disabled",
666 "inherit",
667 "pinned",
668 "exclusive",
669 "exclude_user",
670 "exclude_kernel",
671 "exclude_hv",
672 "exclude_idle",
673 "mmap",
674 "context_switch",
675 "comm",
676 "freq",
677 "inherit_stat",
678 "enable_on_exec",
679 "task",
680 "watermark",
681 "precise_ip",
682 "mmap_data",
683 "sample_id_all",
684 "wakeup_events",
685 "bp_type",
686 "bp_addr",
687 "bp_len",
688 NULL
690 u64 sample_period = 0;
691 u32 disabled = 0,
692 inherit = 0,
693 pinned = 0,
694 exclusive = 0,
695 exclude_user = 0,
696 exclude_kernel = 0,
697 exclude_hv = 0,
698 exclude_idle = 0,
699 mmap = 0,
700 context_switch = 0,
701 comm = 0,
702 freq = 1,
703 inherit_stat = 0,
704 enable_on_exec = 0,
705 task = 0,
706 watermark = 0,
707 precise_ip = 0,
708 mmap_data = 0,
709 sample_id_all = 1;
710 int idx = 0;
712 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
713 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
714 &attr.type, &attr.config, &attr.sample_freq,
715 &sample_period, &attr.sample_type,
716 &attr.read_format, &disabled, &inherit,
717 &pinned, &exclusive, &exclude_user,
718 &exclude_kernel, &exclude_hv, &exclude_idle,
719 &mmap, &context_switch, &comm, &freq, &inherit_stat,
720 &enable_on_exec, &task, &watermark,
721 &precise_ip, &mmap_data, &sample_id_all,
722 &attr.wakeup_events, &attr.bp_type,
723 &attr.bp_addr, &attr.bp_len, &idx))
724 return -1;
726 /* union... */
727 if (sample_period != 0) {
728 if (attr.sample_freq != 0)
729 return -1; /* FIXME: throw right exception */
730 attr.sample_period = sample_period;
733 /* Bitfields */
734 attr.disabled = disabled;
735 attr.inherit = inherit;
736 attr.pinned = pinned;
737 attr.exclusive = exclusive;
738 attr.exclude_user = exclude_user;
739 attr.exclude_kernel = exclude_kernel;
740 attr.exclude_hv = exclude_hv;
741 attr.exclude_idle = exclude_idle;
742 attr.mmap = mmap;
743 attr.context_switch = context_switch;
744 attr.comm = comm;
745 attr.freq = freq;
746 attr.inherit_stat = inherit_stat;
747 attr.enable_on_exec = enable_on_exec;
748 attr.task = task;
749 attr.watermark = watermark;
750 attr.precise_ip = precise_ip;
751 attr.mmap_data = mmap_data;
752 attr.sample_id_all = sample_id_all;
753 attr.size = sizeof(attr);
755 perf_evsel__init(&pevsel->evsel, &attr, idx);
756 return 0;
759 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
761 perf_evsel__exit(&pevsel->evsel);
762 pevsel->ob_type->tp_free((PyObject*)pevsel);
765 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
766 PyObject *args, PyObject *kwargs)
768 struct perf_evsel *evsel = &pevsel->evsel;
769 struct cpu_map *cpus = NULL;
770 struct thread_map *threads = NULL;
771 PyObject *pcpus = NULL, *pthreads = NULL;
772 int group = 0, inherit = 0;
773 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
775 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
776 &pcpus, &pthreads, &group, &inherit))
777 return NULL;
779 if (pthreads != NULL)
780 threads = ((struct pyrf_thread_map *)pthreads)->threads;
782 if (pcpus != NULL)
783 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
785 evsel->attr.inherit = inherit;
787 * This will group just the fds for this single evsel, to group
788 * multiple events, use evlist.open().
790 if (perf_evsel__open(evsel, cpus, threads) < 0) {
791 PyErr_SetFromErrno(PyExc_OSError);
792 return NULL;
795 Py_INCREF(Py_None);
796 return Py_None;
799 static PyMethodDef pyrf_evsel__methods[] = {
801 .ml_name = "open",
802 .ml_meth = (PyCFunction)pyrf_evsel__open,
803 .ml_flags = METH_VARARGS | METH_KEYWORDS,
804 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
806 { .ml_name = NULL, }
809 static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
811 static PyTypeObject pyrf_evsel__type = {
812 PyVarObject_HEAD_INIT(NULL, 0)
813 .tp_name = "perf.evsel",
814 .tp_basicsize = sizeof(struct pyrf_evsel),
815 .tp_dealloc = (destructor)pyrf_evsel__delete,
816 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
817 .tp_doc = pyrf_evsel__doc,
818 .tp_methods = pyrf_evsel__methods,
819 .tp_init = (initproc)pyrf_evsel__init,
822 static int pyrf_evsel__setup_types(void)
824 pyrf_evsel__type.tp_new = PyType_GenericNew;
825 return PyType_Ready(&pyrf_evsel__type);
828 struct pyrf_evlist {
829 PyObject_HEAD
831 struct perf_evlist evlist;
834 static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
835 PyObject *args, PyObject *kwargs __maybe_unused)
837 PyObject *pcpus = NULL, *pthreads = NULL;
838 struct cpu_map *cpus;
839 struct thread_map *threads;
841 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
842 return -1;
844 threads = ((struct pyrf_thread_map *)pthreads)->threads;
845 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
846 perf_evlist__init(&pevlist->evlist, cpus, threads);
847 return 0;
850 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
852 perf_evlist__exit(&pevlist->evlist);
853 pevlist->ob_type->tp_free((PyObject*)pevlist);
856 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
857 PyObject *args, PyObject *kwargs)
859 struct perf_evlist *evlist = &pevlist->evlist;
860 static char *kwlist[] = { "pages", "overwrite", NULL };
861 int pages = 128, overwrite = false;
863 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
864 &pages, &overwrite))
865 return NULL;
867 if (perf_evlist__mmap(evlist, pages) < 0) {
868 PyErr_SetFromErrno(PyExc_OSError);
869 return NULL;
872 Py_INCREF(Py_None);
873 return Py_None;
876 static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
877 PyObject *args, PyObject *kwargs)
879 struct perf_evlist *evlist = &pevlist->evlist;
880 static char *kwlist[] = { "timeout", NULL };
881 int timeout = -1, n;
883 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
884 return NULL;
886 n = perf_evlist__poll(evlist, timeout);
887 if (n < 0) {
888 PyErr_SetFromErrno(PyExc_OSError);
889 return NULL;
892 return Py_BuildValue("i", n);
895 static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
896 PyObject *args __maybe_unused,
897 PyObject *kwargs __maybe_unused)
899 struct perf_evlist *evlist = &pevlist->evlist;
900 PyObject *list = PyList_New(0);
901 int i;
903 for (i = 0; i < evlist->pollfd.nr; ++i) {
904 PyObject *file;
905 FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
907 if (fp == NULL)
908 goto free_list;
910 file = PyFile_FromFile(fp, "perf", "r", NULL);
911 if (file == NULL)
912 goto free_list;
914 if (PyList_Append(list, file) != 0) {
915 Py_DECREF(file);
916 goto free_list;
919 Py_DECREF(file);
922 return list;
923 free_list:
924 return PyErr_NoMemory();
928 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
929 PyObject *args,
930 PyObject *kwargs __maybe_unused)
932 struct perf_evlist *evlist = &pevlist->evlist;
933 PyObject *pevsel;
934 struct perf_evsel *evsel;
936 if (!PyArg_ParseTuple(args, "O", &pevsel))
937 return NULL;
939 Py_INCREF(pevsel);
940 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
941 evsel->idx = evlist->nr_entries;
942 perf_evlist__add(evlist, evsel);
944 return Py_BuildValue("i", evlist->nr_entries);
947 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
948 PyObject *args, PyObject *kwargs)
950 struct perf_evlist *evlist = &pevlist->evlist;
951 union perf_event *event;
952 int sample_id_all = 1, cpu;
953 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
954 int err;
956 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
957 &cpu, &sample_id_all))
958 return NULL;
960 event = perf_evlist__mmap_read(evlist, cpu);
961 if (event != NULL) {
962 PyObject *pyevent = pyrf_event__new(event);
963 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
964 struct perf_evsel *evsel;
966 if (pyevent == NULL)
967 return PyErr_NoMemory();
969 evsel = perf_evlist__event2evsel(evlist, event);
970 if (!evsel)
971 return Py_None;
973 pevent->evsel = evsel;
975 err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
977 /* Consume the even only after we parsed it out. */
978 perf_evlist__mmap_consume(evlist, cpu);
980 if (err)
981 return PyErr_Format(PyExc_OSError,
982 "perf: can't parse sample, err=%d", err);
983 return pyevent;
986 Py_INCREF(Py_None);
987 return Py_None;
990 static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
991 PyObject *args, PyObject *kwargs)
993 struct perf_evlist *evlist = &pevlist->evlist;
994 int group = 0;
995 static char *kwlist[] = { "group", NULL };
997 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
998 return NULL;
1000 if (group)
1001 perf_evlist__set_leader(evlist);
1003 if (perf_evlist__open(evlist) < 0) {
1004 PyErr_SetFromErrno(PyExc_OSError);
1005 return NULL;
1008 Py_INCREF(Py_None);
1009 return Py_None;
1012 static PyMethodDef pyrf_evlist__methods[] = {
1014 .ml_name = "mmap",
1015 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1016 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1017 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1020 .ml_name = "open",
1021 .ml_meth = (PyCFunction)pyrf_evlist__open,
1022 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1023 .ml_doc = PyDoc_STR("open the file descriptors.")
1026 .ml_name = "poll",
1027 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1028 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1029 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1032 .ml_name = "get_pollfd",
1033 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1034 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1035 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1038 .ml_name = "add",
1039 .ml_meth = (PyCFunction)pyrf_evlist__add,
1040 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1041 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1044 .ml_name = "read_on_cpu",
1045 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1046 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1047 .ml_doc = PyDoc_STR("reads an event.")
1049 { .ml_name = NULL, }
1052 static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1054 struct pyrf_evlist *pevlist = (void *)obj;
1056 return pevlist->evlist.nr_entries;
1059 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1061 struct pyrf_evlist *pevlist = (void *)obj;
1062 struct perf_evsel *pos;
1064 if (i >= pevlist->evlist.nr_entries)
1065 return NULL;
1067 evlist__for_each_entry(&pevlist->evlist, pos) {
1068 if (i-- == 0)
1069 break;
1072 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1075 static PySequenceMethods pyrf_evlist__sequence_methods = {
1076 .sq_length = pyrf_evlist__length,
1077 .sq_item = pyrf_evlist__item,
1080 static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1082 static PyTypeObject pyrf_evlist__type = {
1083 PyVarObject_HEAD_INIT(NULL, 0)
1084 .tp_name = "perf.evlist",
1085 .tp_basicsize = sizeof(struct pyrf_evlist),
1086 .tp_dealloc = (destructor)pyrf_evlist__delete,
1087 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1088 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1089 .tp_doc = pyrf_evlist__doc,
1090 .tp_methods = pyrf_evlist__methods,
1091 .tp_init = (initproc)pyrf_evlist__init,
1094 static int pyrf_evlist__setup_types(void)
1096 pyrf_evlist__type.tp_new = PyType_GenericNew;
1097 return PyType_Ready(&pyrf_evlist__type);
1100 #define PERF_CONST(name) { #name, PERF_##name }
1102 static struct {
1103 const char *name;
1104 int value;
1105 } perf__constants[] = {
1106 PERF_CONST(TYPE_HARDWARE),
1107 PERF_CONST(TYPE_SOFTWARE),
1108 PERF_CONST(TYPE_TRACEPOINT),
1109 PERF_CONST(TYPE_HW_CACHE),
1110 PERF_CONST(TYPE_RAW),
1111 PERF_CONST(TYPE_BREAKPOINT),
1113 PERF_CONST(COUNT_HW_CPU_CYCLES),
1114 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1115 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1116 PERF_CONST(COUNT_HW_CACHE_MISSES),
1117 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1118 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1119 PERF_CONST(COUNT_HW_BUS_CYCLES),
1120 PERF_CONST(COUNT_HW_CACHE_L1D),
1121 PERF_CONST(COUNT_HW_CACHE_L1I),
1122 PERF_CONST(COUNT_HW_CACHE_LL),
1123 PERF_CONST(COUNT_HW_CACHE_DTLB),
1124 PERF_CONST(COUNT_HW_CACHE_ITLB),
1125 PERF_CONST(COUNT_HW_CACHE_BPU),
1126 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1127 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1128 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1129 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1130 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1132 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1133 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1135 PERF_CONST(COUNT_SW_CPU_CLOCK),
1136 PERF_CONST(COUNT_SW_TASK_CLOCK),
1137 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1138 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1139 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1140 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1141 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1142 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1143 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1144 PERF_CONST(COUNT_SW_DUMMY),
1146 PERF_CONST(SAMPLE_IP),
1147 PERF_CONST(SAMPLE_TID),
1148 PERF_CONST(SAMPLE_TIME),
1149 PERF_CONST(SAMPLE_ADDR),
1150 PERF_CONST(SAMPLE_READ),
1151 PERF_CONST(SAMPLE_CALLCHAIN),
1152 PERF_CONST(SAMPLE_ID),
1153 PERF_CONST(SAMPLE_CPU),
1154 PERF_CONST(SAMPLE_PERIOD),
1155 PERF_CONST(SAMPLE_STREAM_ID),
1156 PERF_CONST(SAMPLE_RAW),
1158 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1159 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1160 PERF_CONST(FORMAT_ID),
1161 PERF_CONST(FORMAT_GROUP),
1163 PERF_CONST(RECORD_MMAP),
1164 PERF_CONST(RECORD_LOST),
1165 PERF_CONST(RECORD_COMM),
1166 PERF_CONST(RECORD_EXIT),
1167 PERF_CONST(RECORD_THROTTLE),
1168 PERF_CONST(RECORD_UNTHROTTLE),
1169 PERF_CONST(RECORD_FORK),
1170 PERF_CONST(RECORD_READ),
1171 PERF_CONST(RECORD_SAMPLE),
1172 PERF_CONST(RECORD_MMAP2),
1173 PERF_CONST(RECORD_AUX),
1174 PERF_CONST(RECORD_ITRACE_START),
1175 PERF_CONST(RECORD_LOST_SAMPLES),
1176 PERF_CONST(RECORD_SWITCH),
1177 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1179 PERF_CONST(RECORD_MISC_SWITCH_OUT),
1180 { .name = NULL, },
1183 static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1184 PyObject *args, PyObject *kwargs)
1186 struct event_format *tp_format;
1187 static char *kwlist[] = { "sys", "name", NULL };
1188 char *sys = NULL;
1189 char *name = NULL;
1191 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1192 &sys, &name))
1193 return NULL;
1195 tp_format = trace_event__tp_format(sys, name);
1196 if (IS_ERR(tp_format))
1197 return PyInt_FromLong(-1);
1199 return PyInt_FromLong(tp_format->id);
1202 static PyMethodDef perf__methods[] = {
1204 .ml_name = "tracepoint",
1205 .ml_meth = (PyCFunction) pyrf__tracepoint,
1206 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1207 .ml_doc = PyDoc_STR("Get tracepoint config.")
1209 { .ml_name = NULL, }
1212 PyMODINIT_FUNC initperf(void)
1214 PyObject *obj;
1215 int i;
1216 PyObject *dict, *module = Py_InitModule("perf", perf__methods);
1218 if (module == NULL ||
1219 pyrf_event__setup_types() < 0 ||
1220 pyrf_evlist__setup_types() < 0 ||
1221 pyrf_evsel__setup_types() < 0 ||
1222 pyrf_thread_map__setup_types() < 0 ||
1223 pyrf_cpu_map__setup_types() < 0)
1224 return;
1226 /* The page_size is placed in util object. */
1227 page_size = sysconf(_SC_PAGE_SIZE);
1229 Py_INCREF(&pyrf_evlist__type);
1230 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1232 Py_INCREF(&pyrf_evsel__type);
1233 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1235 Py_INCREF(&pyrf_mmap_event__type);
1236 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1238 Py_INCREF(&pyrf_lost_event__type);
1239 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1241 Py_INCREF(&pyrf_comm_event__type);
1242 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1244 Py_INCREF(&pyrf_task_event__type);
1245 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1247 Py_INCREF(&pyrf_throttle_event__type);
1248 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1250 Py_INCREF(&pyrf_task_event__type);
1251 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1253 Py_INCREF(&pyrf_read_event__type);
1254 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1256 Py_INCREF(&pyrf_sample_event__type);
1257 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1259 Py_INCREF(&pyrf_context_switch_event__type);
1260 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1262 Py_INCREF(&pyrf_thread_map__type);
1263 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1265 Py_INCREF(&pyrf_cpu_map__type);
1266 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1268 dict = PyModule_GetDict(module);
1269 if (dict == NULL)
1270 goto error;
1272 for (i = 0; perf__constants[i].name != NULL; i++) {
1273 obj = PyInt_FromLong(perf__constants[i].value);
1274 if (obj == NULL)
1275 goto error;
1276 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1277 Py_DECREF(obj);
1280 error:
1281 if (PyErr_Occurred())
1282 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1286 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1287 * binding.
1289 void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
1290 int fd, int group_fd, unsigned long flags)