1 // SPDX-License-Identifier: GPL-2.0
3 #include <structmember.h>
12 #include "print_binary.h"
13 #include "thread_map.h"
16 #if PY_MAJOR_VERSION < 3
17 #define _PyUnicode_FromString(arg) \
18 PyString_FromString(arg)
19 #define _PyUnicode_AsString(arg) \
20 PyString_AsString(arg)
21 #define _PyUnicode_FromFormat(...) \
22 PyString_FromFormat(__VA_ARGS__)
23 #define _PyLong_FromLong(arg) \
28 #define _PyUnicode_FromString(arg) \
29 PyUnicode_FromString(arg)
30 #define _PyUnicode_FromFormat(...) \
31 PyUnicode_FromFormat(__VA_ARGS__)
32 #define _PyLong_FromLong(arg) \
37 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
41 * Provide these two so that we don't have to link against callchain.c and
42 * start dragging hist.c, etc.
44 struct callchain_param callchain_param;
46 int parse_callchain_record(const char *arg __maybe_unused,
47 struct callchain_param *param __maybe_unused)
53 * Support debug printing even though util/debug.c is not linked. That means
54 * implementing 'verbose' and 'eprintf'.
58 int eprintf(int level, int var, const char *fmt, ...)
65 ret = vfprintf(stderr, fmt, args);
72 /* Define PyVarObject_HEAD_INIT for python 2.5 */
73 #ifndef PyVarObject_HEAD_INIT
74 # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
77 #if PY_MAJOR_VERSION < 3
78 PyMODINIT_FUNC initperf(void);
80 PyMODINIT_FUNC PyInit_perf(void);
83 #define member_def(type, member, ptype, help) \
85 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
88 #define sample_member_def(name, member, ptype, help) \
90 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
95 struct perf_evsel *evsel;
96 struct perf_sample sample;
97 union perf_event event;
100 #define sample_members \
101 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
102 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
103 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
104 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
105 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
106 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
107 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
108 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
109 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
111 static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
113 static PyMemberDef pyrf_mmap_event__members[] = {
115 member_def(perf_event_header, type, T_UINT, "event type"),
116 member_def(perf_event_header, misc, T_UINT, "event misc"),
117 member_def(mmap_event, pid, T_UINT, "event pid"),
118 member_def(mmap_event, tid, T_UINT, "event tid"),
119 member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
120 member_def(mmap_event, len, T_ULONGLONG, "map length"),
121 member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
122 member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
126 static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
131 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
132 "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
134 pevent->event.mmap.pid, pevent->event.mmap.tid,
135 pevent->event.mmap.start, pevent->event.mmap.len,
136 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
137 ret = PyErr_NoMemory();
139 ret = _PyUnicode_FromString(s);
145 static PyTypeObject pyrf_mmap_event__type = {
146 PyVarObject_HEAD_INIT(NULL, 0)
147 .tp_name = "perf.mmap_event",
148 .tp_basicsize = sizeof(struct pyrf_event),
149 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
150 .tp_doc = pyrf_mmap_event__doc,
151 .tp_members = pyrf_mmap_event__members,
152 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
155 static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
157 static PyMemberDef pyrf_task_event__members[] = {
159 member_def(perf_event_header, type, T_UINT, "event type"),
160 member_def(fork_event, pid, T_UINT, "event pid"),
161 member_def(fork_event, ppid, T_UINT, "event ppid"),
162 member_def(fork_event, tid, T_UINT, "event tid"),
163 member_def(fork_event, ptid, T_UINT, "event ptid"),
164 member_def(fork_event, time, T_ULONGLONG, "timestamp"),
168 static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
170 return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
171 "ptid: %u, time: %" PRIu64 "}",
172 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
173 pevent->event.fork.pid,
174 pevent->event.fork.ppid,
175 pevent->event.fork.tid,
176 pevent->event.fork.ptid,
177 pevent->event.fork.time);
180 static PyTypeObject pyrf_task_event__type = {
181 PyVarObject_HEAD_INIT(NULL, 0)
182 .tp_name = "perf.task_event",
183 .tp_basicsize = sizeof(struct pyrf_event),
184 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
185 .tp_doc = pyrf_task_event__doc,
186 .tp_members = pyrf_task_event__members,
187 .tp_repr = (reprfunc)pyrf_task_event__repr,
190 static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
192 static PyMemberDef pyrf_comm_event__members[] = {
194 member_def(perf_event_header, type, T_UINT, "event type"),
195 member_def(comm_event, pid, T_UINT, "event pid"),
196 member_def(comm_event, tid, T_UINT, "event tid"),
197 member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
201 static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
203 return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
204 pevent->event.comm.pid,
205 pevent->event.comm.tid,
206 pevent->event.comm.comm);
209 static PyTypeObject pyrf_comm_event__type = {
210 PyVarObject_HEAD_INIT(NULL, 0)
211 .tp_name = "perf.comm_event",
212 .tp_basicsize = sizeof(struct pyrf_event),
213 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
214 .tp_doc = pyrf_comm_event__doc,
215 .tp_members = pyrf_comm_event__members,
216 .tp_repr = (reprfunc)pyrf_comm_event__repr,
219 static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
221 static PyMemberDef pyrf_throttle_event__members[] = {
223 member_def(perf_event_header, type, T_UINT, "event type"),
224 member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
225 member_def(throttle_event, id, T_ULONGLONG, "event id"),
226 member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
230 static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
232 struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
234 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
235 ", stream_id: %" PRIu64 " }",
236 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
237 te->time, te->id, te->stream_id);
240 static PyTypeObject pyrf_throttle_event__type = {
241 PyVarObject_HEAD_INIT(NULL, 0)
242 .tp_name = "perf.throttle_event",
243 .tp_basicsize = sizeof(struct pyrf_event),
244 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
245 .tp_doc = pyrf_throttle_event__doc,
246 .tp_members = pyrf_throttle_event__members,
247 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
250 static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
252 static PyMemberDef pyrf_lost_event__members[] = {
254 member_def(lost_event, id, T_ULONGLONG, "event id"),
255 member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
259 static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
264 if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
265 "lost: %#" PRIx64 " }",
266 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
267 ret = PyErr_NoMemory();
269 ret = _PyUnicode_FromString(s);
275 static PyTypeObject pyrf_lost_event__type = {
276 PyVarObject_HEAD_INIT(NULL, 0)
277 .tp_name = "perf.lost_event",
278 .tp_basicsize = sizeof(struct pyrf_event),
279 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
280 .tp_doc = pyrf_lost_event__doc,
281 .tp_members = pyrf_lost_event__members,
282 .tp_repr = (reprfunc)pyrf_lost_event__repr,
285 static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
287 static PyMemberDef pyrf_read_event__members[] = {
289 member_def(read_event, pid, T_UINT, "event pid"),
290 member_def(read_event, tid, T_UINT, "event tid"),
294 static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
296 return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
297 pevent->event.read.pid,
298 pevent->event.read.tid);
300 * FIXME: return the array of read values,
301 * making this method useful ;-)
305 static PyTypeObject pyrf_read_event__type = {
306 PyVarObject_HEAD_INIT(NULL, 0)
307 .tp_name = "perf.read_event",
308 .tp_basicsize = sizeof(struct pyrf_event),
309 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
310 .tp_doc = pyrf_read_event__doc,
311 .tp_members = pyrf_read_event__members,
312 .tp_repr = (reprfunc)pyrf_read_event__repr,
315 static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
317 static PyMemberDef pyrf_sample_event__members[] = {
319 member_def(perf_event_header, type, T_UINT, "event type"),
323 static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
328 if (asprintf(&s, "{ type: sample }") < 0) {
329 ret = PyErr_NoMemory();
331 ret = _PyUnicode_FromString(s);
337 static bool is_tracepoint(struct pyrf_event *pevent)
339 return pevent->evsel->attr.type == PERF_TYPE_TRACEPOINT;
343 tracepoint_field(struct pyrf_event *pe, struct format_field *field)
345 struct tep_handle *pevent = field->event->pevent;
346 void *data = pe->sample.raw_data;
347 PyObject *ret = NULL;
348 unsigned long long val;
349 unsigned int offset, len;
351 if (field->flags & FIELD_IS_ARRAY) {
352 offset = field->offset;
354 if (field->flags & FIELD_IS_DYNAMIC) {
355 val = tep_read_number(pevent, data + offset, len);
360 if (field->flags & FIELD_IS_STRING &&
361 is_printable_array(data + offset, len)) {
362 ret = _PyUnicode_FromString((char *)data + offset);
364 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
365 field->flags &= ~FIELD_IS_STRING;
368 val = tep_read_number(pevent, data + field->offset,
370 if (field->flags & FIELD_IS_POINTER)
371 ret = PyLong_FromUnsignedLong((unsigned long) val);
372 else if (field->flags & FIELD_IS_SIGNED)
373 ret = PyLong_FromLong((long) val);
375 ret = PyLong_FromUnsignedLong((unsigned long) val);
382 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
384 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
385 struct perf_evsel *evsel = pevent->evsel;
386 struct format_field *field;
388 if (!evsel->tp_format) {
389 struct event_format *tp_format;
391 tp_format = trace_event__tp_format_id(evsel->attr.config);
395 evsel->tp_format = tp_format;
398 field = tep_find_any_field(evsel->tp_format, str);
402 return tracepoint_field(pevent, field);
406 pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
408 PyObject *obj = NULL;
410 if (is_tracepoint(pevent))
411 obj = get_tracepoint_field(pevent, attr_name);
413 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
416 static PyTypeObject pyrf_sample_event__type = {
417 PyVarObject_HEAD_INIT(NULL, 0)
418 .tp_name = "perf.sample_event",
419 .tp_basicsize = sizeof(struct pyrf_event),
420 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
421 .tp_doc = pyrf_sample_event__doc,
422 .tp_members = pyrf_sample_event__members,
423 .tp_repr = (reprfunc)pyrf_sample_event__repr,
424 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
427 static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
429 static PyMemberDef pyrf_context_switch_event__members[] = {
431 member_def(perf_event_header, type, T_UINT, "event type"),
432 member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"),
433 member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"),
437 static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
442 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
443 pevent->event.context_switch.next_prev_pid,
444 pevent->event.context_switch.next_prev_tid,
445 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
446 ret = PyErr_NoMemory();
448 ret = _PyUnicode_FromString(s);
454 static PyTypeObject pyrf_context_switch_event__type = {
455 PyVarObject_HEAD_INIT(NULL, 0)
456 .tp_name = "perf.context_switch_event",
457 .tp_basicsize = sizeof(struct pyrf_event),
458 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
459 .tp_doc = pyrf_context_switch_event__doc,
460 .tp_members = pyrf_context_switch_event__members,
461 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
464 static int pyrf_event__setup_types(void)
467 pyrf_mmap_event__type.tp_new =
468 pyrf_task_event__type.tp_new =
469 pyrf_comm_event__type.tp_new =
470 pyrf_lost_event__type.tp_new =
471 pyrf_read_event__type.tp_new =
472 pyrf_sample_event__type.tp_new =
473 pyrf_context_switch_event__type.tp_new =
474 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
475 err = PyType_Ready(&pyrf_mmap_event__type);
478 err = PyType_Ready(&pyrf_lost_event__type);
481 err = PyType_Ready(&pyrf_task_event__type);
484 err = PyType_Ready(&pyrf_comm_event__type);
487 err = PyType_Ready(&pyrf_throttle_event__type);
490 err = PyType_Ready(&pyrf_read_event__type);
493 err = PyType_Ready(&pyrf_sample_event__type);
496 err = PyType_Ready(&pyrf_context_switch_event__type);
503 static PyTypeObject *pyrf_event__type[] = {
504 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
505 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
506 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
507 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
508 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
509 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
510 [PERF_RECORD_FORK] = &pyrf_task_event__type,
511 [PERF_RECORD_READ] = &pyrf_read_event__type,
512 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
513 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
514 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
517 static PyObject *pyrf_event__new(union perf_event *event)
519 struct pyrf_event *pevent;
522 if ((event->header.type < PERF_RECORD_MMAP ||
523 event->header.type > PERF_RECORD_SAMPLE) &&
524 !(event->header.type == PERF_RECORD_SWITCH ||
525 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
528 ptype = pyrf_event__type[event->header.type];
529 pevent = PyObject_New(struct pyrf_event, ptype);
531 memcpy(&pevent->event, event, event->header.size);
532 return (PyObject *)pevent;
535 struct pyrf_cpu_map {
538 struct cpu_map *cpus;
541 static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
542 PyObject *args, PyObject *kwargs)
544 static char *kwlist[] = { "cpustr", NULL };
547 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
551 pcpus->cpus = cpu_map__new(cpustr);
552 if (pcpus->cpus == NULL)
557 static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
559 cpu_map__put(pcpus->cpus);
560 Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
563 static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
565 struct pyrf_cpu_map *pcpus = (void *)obj;
567 return pcpus->cpus->nr;
570 static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
572 struct pyrf_cpu_map *pcpus = (void *)obj;
574 if (i >= pcpus->cpus->nr)
577 return Py_BuildValue("i", pcpus->cpus->map[i]);
580 static PySequenceMethods pyrf_cpu_map__sequence_methods = {
581 .sq_length = pyrf_cpu_map__length,
582 .sq_item = pyrf_cpu_map__item,
585 static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
587 static PyTypeObject pyrf_cpu_map__type = {
588 PyVarObject_HEAD_INIT(NULL, 0)
589 .tp_name = "perf.cpu_map",
590 .tp_basicsize = sizeof(struct pyrf_cpu_map),
591 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
592 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
593 .tp_doc = pyrf_cpu_map__doc,
594 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
595 .tp_init = (initproc)pyrf_cpu_map__init,
598 static int pyrf_cpu_map__setup_types(void)
600 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
601 return PyType_Ready(&pyrf_cpu_map__type);
604 struct pyrf_thread_map {
607 struct thread_map *threads;
610 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
611 PyObject *args, PyObject *kwargs)
613 static char *kwlist[] = { "pid", "tid", "uid", NULL };
614 int pid = -1, tid = -1, uid = UINT_MAX;
616 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
617 kwlist, &pid, &tid, &uid))
620 pthreads->threads = thread_map__new(pid, tid, uid);
621 if (pthreads->threads == NULL)
626 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
628 thread_map__put(pthreads->threads);
629 Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
632 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
634 struct pyrf_thread_map *pthreads = (void *)obj;
636 return pthreads->threads->nr;
639 static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
641 struct pyrf_thread_map *pthreads = (void *)obj;
643 if (i >= pthreads->threads->nr)
646 return Py_BuildValue("i", pthreads->threads->map[i]);
649 static PySequenceMethods pyrf_thread_map__sequence_methods = {
650 .sq_length = pyrf_thread_map__length,
651 .sq_item = pyrf_thread_map__item,
654 static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
656 static PyTypeObject pyrf_thread_map__type = {
657 PyVarObject_HEAD_INIT(NULL, 0)
658 .tp_name = "perf.thread_map",
659 .tp_basicsize = sizeof(struct pyrf_thread_map),
660 .tp_dealloc = (destructor)pyrf_thread_map__delete,
661 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
662 .tp_doc = pyrf_thread_map__doc,
663 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
664 .tp_init = (initproc)pyrf_thread_map__init,
667 static int pyrf_thread_map__setup_types(void)
669 pyrf_thread_map__type.tp_new = PyType_GenericNew;
670 return PyType_Ready(&pyrf_thread_map__type);
676 struct perf_evsel evsel;
679 static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
680 PyObject *args, PyObject *kwargs)
682 struct perf_event_attr attr = {
683 .type = PERF_TYPE_HARDWARE,
684 .config = PERF_COUNT_HW_CPU_CYCLES,
685 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
687 static char *kwlist[] = {
719 u64 sample_period = 0;
741 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
742 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
743 &attr.type, &attr.config, &attr.sample_freq,
744 &sample_period, &attr.sample_type,
745 &attr.read_format, &disabled, &inherit,
746 &pinned, &exclusive, &exclude_user,
747 &exclude_kernel, &exclude_hv, &exclude_idle,
748 &mmap, &context_switch, &comm, &freq, &inherit_stat,
749 &enable_on_exec, &task, &watermark,
750 &precise_ip, &mmap_data, &sample_id_all,
751 &attr.wakeup_events, &attr.bp_type,
752 &attr.bp_addr, &attr.bp_len, &idx))
756 if (sample_period != 0) {
757 if (attr.sample_freq != 0)
758 return -1; /* FIXME: throw right exception */
759 attr.sample_period = sample_period;
763 attr.disabled = disabled;
764 attr.inherit = inherit;
765 attr.pinned = pinned;
766 attr.exclusive = exclusive;
767 attr.exclude_user = exclude_user;
768 attr.exclude_kernel = exclude_kernel;
769 attr.exclude_hv = exclude_hv;
770 attr.exclude_idle = exclude_idle;
772 attr.context_switch = context_switch;
775 attr.inherit_stat = inherit_stat;
776 attr.enable_on_exec = enable_on_exec;
778 attr.watermark = watermark;
779 attr.precise_ip = precise_ip;
780 attr.mmap_data = mmap_data;
781 attr.sample_id_all = sample_id_all;
782 attr.size = sizeof(attr);
784 perf_evsel__init(&pevsel->evsel, &attr, idx);
788 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
790 perf_evsel__exit(&pevsel->evsel);
791 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
794 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
795 PyObject *args, PyObject *kwargs)
797 struct perf_evsel *evsel = &pevsel->evsel;
798 struct cpu_map *cpus = NULL;
799 struct thread_map *threads = NULL;
800 PyObject *pcpus = NULL, *pthreads = NULL;
801 int group = 0, inherit = 0;
802 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
804 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
805 &pcpus, &pthreads, &group, &inherit))
808 if (pthreads != NULL)
809 threads = ((struct pyrf_thread_map *)pthreads)->threads;
812 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
814 evsel->attr.inherit = inherit;
816 * This will group just the fds for this single evsel, to group
817 * multiple events, use evlist.open().
819 if (perf_evsel__open(evsel, cpus, threads) < 0) {
820 PyErr_SetFromErrno(PyExc_OSError);
828 static PyMethodDef pyrf_evsel__methods[] = {
831 .ml_meth = (PyCFunction)pyrf_evsel__open,
832 .ml_flags = METH_VARARGS | METH_KEYWORDS,
833 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
838 static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
840 static PyTypeObject pyrf_evsel__type = {
841 PyVarObject_HEAD_INIT(NULL, 0)
842 .tp_name = "perf.evsel",
843 .tp_basicsize = sizeof(struct pyrf_evsel),
844 .tp_dealloc = (destructor)pyrf_evsel__delete,
845 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
846 .tp_doc = pyrf_evsel__doc,
847 .tp_methods = pyrf_evsel__methods,
848 .tp_init = (initproc)pyrf_evsel__init,
851 static int pyrf_evsel__setup_types(void)
853 pyrf_evsel__type.tp_new = PyType_GenericNew;
854 return PyType_Ready(&pyrf_evsel__type);
860 struct perf_evlist evlist;
863 static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
864 PyObject *args, PyObject *kwargs __maybe_unused)
866 PyObject *pcpus = NULL, *pthreads = NULL;
867 struct cpu_map *cpus;
868 struct thread_map *threads;
870 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
873 threads = ((struct pyrf_thread_map *)pthreads)->threads;
874 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
875 perf_evlist__init(&pevlist->evlist, cpus, threads);
879 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
881 perf_evlist__exit(&pevlist->evlist);
882 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
885 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
886 PyObject *args, PyObject *kwargs)
888 struct perf_evlist *evlist = &pevlist->evlist;
889 static char *kwlist[] = { "pages", "overwrite", NULL };
890 int pages = 128, overwrite = false;
892 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
896 if (perf_evlist__mmap(evlist, pages) < 0) {
897 PyErr_SetFromErrno(PyExc_OSError);
905 static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
906 PyObject *args, PyObject *kwargs)
908 struct perf_evlist *evlist = &pevlist->evlist;
909 static char *kwlist[] = { "timeout", NULL };
912 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
915 n = perf_evlist__poll(evlist, timeout);
917 PyErr_SetFromErrno(PyExc_OSError);
921 return Py_BuildValue("i", n);
924 static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
925 PyObject *args __maybe_unused,
926 PyObject *kwargs __maybe_unused)
928 struct perf_evlist *evlist = &pevlist->evlist;
929 PyObject *list = PyList_New(0);
932 for (i = 0; i < evlist->pollfd.nr; ++i) {
934 #if PY_MAJOR_VERSION < 3
935 FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
940 file = PyFile_FromFile(fp, "perf", "r", NULL);
942 file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1,
943 NULL, NULL, NULL, 0);
948 if (PyList_Append(list, file) != 0) {
958 return PyErr_NoMemory();
962 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
964 PyObject *kwargs __maybe_unused)
966 struct perf_evlist *evlist = &pevlist->evlist;
968 struct perf_evsel *evsel;
970 if (!PyArg_ParseTuple(args, "O", &pevsel))
974 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
975 evsel->idx = evlist->nr_entries;
976 perf_evlist__add(evlist, evsel);
978 return Py_BuildValue("i", evlist->nr_entries);
981 static struct perf_mmap *get_md(struct perf_evlist *evlist, int cpu)
985 for (i = 0; i < evlist->nr_mmaps; i++) {
986 struct perf_mmap *md = &evlist->mmap[i];
995 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
996 PyObject *args, PyObject *kwargs)
998 struct perf_evlist *evlist = &pevlist->evlist;
999 union perf_event *event;
1000 int sample_id_all = 1, cpu;
1001 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
1002 struct perf_mmap *md;
1005 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1006 &cpu, &sample_id_all))
1009 md = get_md(evlist, cpu);
1013 if (perf_mmap__read_init(md) < 0)
1016 event = perf_mmap__read_event(md);
1017 if (event != NULL) {
1018 PyObject *pyevent = pyrf_event__new(event);
1019 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
1020 struct perf_evsel *evsel;
1022 if (pyevent == NULL)
1023 return PyErr_NoMemory();
1025 evsel = perf_evlist__event2evsel(evlist, event);
1031 pevent->evsel = evsel;
1033 err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
1035 /* Consume the even only after we parsed it out. */
1036 perf_mmap__consume(md);
1039 return PyErr_Format(PyExc_OSError,
1040 "perf: can't parse sample, err=%d", err);
1048 static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1049 PyObject *args, PyObject *kwargs)
1051 struct perf_evlist *evlist = &pevlist->evlist;
1053 static char *kwlist[] = { "group", NULL };
1055 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1059 perf_evlist__set_leader(evlist);
1061 if (perf_evlist__open(evlist) < 0) {
1062 PyErr_SetFromErrno(PyExc_OSError);
1070 static PyMethodDef pyrf_evlist__methods[] = {
1073 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1074 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1075 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1079 .ml_meth = (PyCFunction)pyrf_evlist__open,
1080 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1081 .ml_doc = PyDoc_STR("open the file descriptors.")
1085 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1086 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1087 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1090 .ml_name = "get_pollfd",
1091 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1092 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1093 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1097 .ml_meth = (PyCFunction)pyrf_evlist__add,
1098 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1099 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1102 .ml_name = "read_on_cpu",
1103 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1104 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1105 .ml_doc = PyDoc_STR("reads an event.")
1107 { .ml_name = NULL, }
1110 static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1112 struct pyrf_evlist *pevlist = (void *)obj;
1114 return pevlist->evlist.nr_entries;
1117 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1119 struct pyrf_evlist *pevlist = (void *)obj;
1120 struct perf_evsel *pos;
1122 if (i >= pevlist->evlist.nr_entries)
1125 evlist__for_each_entry(&pevlist->evlist, pos) {
1130 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1133 static PySequenceMethods pyrf_evlist__sequence_methods = {
1134 .sq_length = pyrf_evlist__length,
1135 .sq_item = pyrf_evlist__item,
1138 static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1140 static PyTypeObject pyrf_evlist__type = {
1141 PyVarObject_HEAD_INIT(NULL, 0)
1142 .tp_name = "perf.evlist",
1143 .tp_basicsize = sizeof(struct pyrf_evlist),
1144 .tp_dealloc = (destructor)pyrf_evlist__delete,
1145 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1146 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1147 .tp_doc = pyrf_evlist__doc,
1148 .tp_methods = pyrf_evlist__methods,
1149 .tp_init = (initproc)pyrf_evlist__init,
1152 static int pyrf_evlist__setup_types(void)
1154 pyrf_evlist__type.tp_new = PyType_GenericNew;
1155 return PyType_Ready(&pyrf_evlist__type);
1158 #define PERF_CONST(name) { #name, PERF_##name }
1163 } perf__constants[] = {
1164 PERF_CONST(TYPE_HARDWARE),
1165 PERF_CONST(TYPE_SOFTWARE),
1166 PERF_CONST(TYPE_TRACEPOINT),
1167 PERF_CONST(TYPE_HW_CACHE),
1168 PERF_CONST(TYPE_RAW),
1169 PERF_CONST(TYPE_BREAKPOINT),
1171 PERF_CONST(COUNT_HW_CPU_CYCLES),
1172 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1173 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1174 PERF_CONST(COUNT_HW_CACHE_MISSES),
1175 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1176 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1177 PERF_CONST(COUNT_HW_BUS_CYCLES),
1178 PERF_CONST(COUNT_HW_CACHE_L1D),
1179 PERF_CONST(COUNT_HW_CACHE_L1I),
1180 PERF_CONST(COUNT_HW_CACHE_LL),
1181 PERF_CONST(COUNT_HW_CACHE_DTLB),
1182 PERF_CONST(COUNT_HW_CACHE_ITLB),
1183 PERF_CONST(COUNT_HW_CACHE_BPU),
1184 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1185 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1186 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1187 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1188 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1190 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1191 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1193 PERF_CONST(COUNT_SW_CPU_CLOCK),
1194 PERF_CONST(COUNT_SW_TASK_CLOCK),
1195 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1196 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1197 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1198 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1199 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1200 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1201 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1202 PERF_CONST(COUNT_SW_DUMMY),
1204 PERF_CONST(SAMPLE_IP),
1205 PERF_CONST(SAMPLE_TID),
1206 PERF_CONST(SAMPLE_TIME),
1207 PERF_CONST(SAMPLE_ADDR),
1208 PERF_CONST(SAMPLE_READ),
1209 PERF_CONST(SAMPLE_CALLCHAIN),
1210 PERF_CONST(SAMPLE_ID),
1211 PERF_CONST(SAMPLE_CPU),
1212 PERF_CONST(SAMPLE_PERIOD),
1213 PERF_CONST(SAMPLE_STREAM_ID),
1214 PERF_CONST(SAMPLE_RAW),
1216 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1217 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1218 PERF_CONST(FORMAT_ID),
1219 PERF_CONST(FORMAT_GROUP),
1221 PERF_CONST(RECORD_MMAP),
1222 PERF_CONST(RECORD_LOST),
1223 PERF_CONST(RECORD_COMM),
1224 PERF_CONST(RECORD_EXIT),
1225 PERF_CONST(RECORD_THROTTLE),
1226 PERF_CONST(RECORD_UNTHROTTLE),
1227 PERF_CONST(RECORD_FORK),
1228 PERF_CONST(RECORD_READ),
1229 PERF_CONST(RECORD_SAMPLE),
1230 PERF_CONST(RECORD_MMAP2),
1231 PERF_CONST(RECORD_AUX),
1232 PERF_CONST(RECORD_ITRACE_START),
1233 PERF_CONST(RECORD_LOST_SAMPLES),
1234 PERF_CONST(RECORD_SWITCH),
1235 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1237 PERF_CONST(RECORD_MISC_SWITCH_OUT),
1241 static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1242 PyObject *args, PyObject *kwargs)
1244 struct event_format *tp_format;
1245 static char *kwlist[] = { "sys", "name", NULL };
1249 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1253 tp_format = trace_event__tp_format(sys, name);
1254 if (IS_ERR(tp_format))
1255 return _PyLong_FromLong(-1);
1257 return _PyLong_FromLong(tp_format->id);
1260 static PyMethodDef perf__methods[] = {
1262 .ml_name = "tracepoint",
1263 .ml_meth = (PyCFunction) pyrf__tracepoint,
1264 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1265 .ml_doc = PyDoc_STR("Get tracepoint config.")
1267 { .ml_name = NULL, }
1270 #if PY_MAJOR_VERSION < 3
1271 PyMODINIT_FUNC initperf(void)
1273 PyMODINIT_FUNC PyInit_perf(void)
1279 #if PY_MAJOR_VERSION < 3
1280 PyObject *module = Py_InitModule("perf", perf__methods);
1282 static struct PyModuleDef moduledef = {
1283 PyModuleDef_HEAD_INIT,
1284 "perf", /* m_name */
1287 perf__methods, /* m_methods */
1288 NULL, /* m_reload */
1289 NULL, /* m_traverse */
1293 PyObject *module = PyModule_Create(&moduledef);
1296 if (module == NULL ||
1297 pyrf_event__setup_types() < 0 ||
1298 pyrf_evlist__setup_types() < 0 ||
1299 pyrf_evsel__setup_types() < 0 ||
1300 pyrf_thread_map__setup_types() < 0 ||
1301 pyrf_cpu_map__setup_types() < 0)
1302 #if PY_MAJOR_VERSION < 3
1308 /* The page_size is placed in util object. */
1309 page_size = sysconf(_SC_PAGE_SIZE);
1311 Py_INCREF(&pyrf_evlist__type);
1312 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1314 Py_INCREF(&pyrf_evsel__type);
1315 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1317 Py_INCREF(&pyrf_mmap_event__type);
1318 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1320 Py_INCREF(&pyrf_lost_event__type);
1321 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1323 Py_INCREF(&pyrf_comm_event__type);
1324 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1326 Py_INCREF(&pyrf_task_event__type);
1327 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1329 Py_INCREF(&pyrf_throttle_event__type);
1330 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1332 Py_INCREF(&pyrf_task_event__type);
1333 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1335 Py_INCREF(&pyrf_read_event__type);
1336 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1338 Py_INCREF(&pyrf_sample_event__type);
1339 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1341 Py_INCREF(&pyrf_context_switch_event__type);
1342 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1344 Py_INCREF(&pyrf_thread_map__type);
1345 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1347 Py_INCREF(&pyrf_cpu_map__type);
1348 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1350 dict = PyModule_GetDict(module);
1354 for (i = 0; perf__constants[i].name != NULL; i++) {
1355 obj = _PyLong_FromLong(perf__constants[i].value);
1358 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1363 if (PyErr_Occurred())
1364 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1365 #if PY_MAJOR_VERSION >= 3
1371 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1374 void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
1375 int fd, int group_fd, unsigned long flags)