2 #include <structmember.h>
10 #include "print_binary.h"
11 #include "thread_map.h"
14 * Support debug printing even though util/debug.c is not linked. That means
15 * implementing 'verbose' and 'eprintf'.
19 int eprintf(int level, int var, const char *fmt, ...)
26 ret = vfprintf(stderr, fmt, args);
33 /* Define PyVarObject_HEAD_INIT for python 2.5 */
34 #ifndef PyVarObject_HEAD_INIT
35 # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
38 PyMODINIT_FUNC initperf(void);
40 #define member_def(type, member, ptype, help) \
42 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
45 #define sample_member_def(name, member, ptype, help) \
47 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
52 struct perf_evsel *evsel;
53 struct perf_sample sample;
54 union perf_event event;
57 #define sample_members \
58 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
59 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
60 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
61 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
62 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
63 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
64 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
65 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
66 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
68 static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
70 static PyMemberDef pyrf_mmap_event__members[] = {
72 member_def(perf_event_header, type, T_UINT, "event type"),
73 member_def(perf_event_header, misc, T_UINT, "event misc"),
74 member_def(mmap_event, pid, T_UINT, "event pid"),
75 member_def(mmap_event, tid, T_UINT, "event tid"),
76 member_def(mmap_event, start, T_ULONGLONG, "start of the map"),
77 member_def(mmap_event, len, T_ULONGLONG, "map length"),
78 member_def(mmap_event, pgoff, T_ULONGLONG, "page offset"),
79 member_def(mmap_event, filename, T_STRING_INPLACE, "backing store"),
83 static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
88 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRIx64 ", "
89 "length: %#" PRIx64 ", offset: %#" PRIx64 ", "
91 pevent->event.mmap.pid, pevent->event.mmap.tid,
92 pevent->event.mmap.start, pevent->event.mmap.len,
93 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
94 ret = PyErr_NoMemory();
96 ret = PyString_FromString(s);
102 static PyTypeObject pyrf_mmap_event__type = {
103 PyVarObject_HEAD_INIT(NULL, 0)
104 .tp_name = "perf.mmap_event",
105 .tp_basicsize = sizeof(struct pyrf_event),
106 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
107 .tp_doc = pyrf_mmap_event__doc,
108 .tp_members = pyrf_mmap_event__members,
109 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
112 static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
114 static PyMemberDef pyrf_task_event__members[] = {
116 member_def(perf_event_header, type, T_UINT, "event type"),
117 member_def(fork_event, pid, T_UINT, "event pid"),
118 member_def(fork_event, ppid, T_UINT, "event ppid"),
119 member_def(fork_event, tid, T_UINT, "event tid"),
120 member_def(fork_event, ptid, T_UINT, "event ptid"),
121 member_def(fork_event, time, T_ULONGLONG, "timestamp"),
125 static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
127 return PyString_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
128 "ptid: %u, time: %" PRIu64 "}",
129 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
130 pevent->event.fork.pid,
131 pevent->event.fork.ppid,
132 pevent->event.fork.tid,
133 pevent->event.fork.ptid,
134 pevent->event.fork.time);
137 static PyTypeObject pyrf_task_event__type = {
138 PyVarObject_HEAD_INIT(NULL, 0)
139 .tp_name = "perf.task_event",
140 .tp_basicsize = sizeof(struct pyrf_event),
141 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
142 .tp_doc = pyrf_task_event__doc,
143 .tp_members = pyrf_task_event__members,
144 .tp_repr = (reprfunc)pyrf_task_event__repr,
147 static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
149 static PyMemberDef pyrf_comm_event__members[] = {
151 member_def(perf_event_header, type, T_UINT, "event type"),
152 member_def(comm_event, pid, T_UINT, "event pid"),
153 member_def(comm_event, tid, T_UINT, "event tid"),
154 member_def(comm_event, comm, T_STRING_INPLACE, "process name"),
158 static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
160 return PyString_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
161 pevent->event.comm.pid,
162 pevent->event.comm.tid,
163 pevent->event.comm.comm);
166 static PyTypeObject pyrf_comm_event__type = {
167 PyVarObject_HEAD_INIT(NULL, 0)
168 .tp_name = "perf.comm_event",
169 .tp_basicsize = sizeof(struct pyrf_event),
170 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
171 .tp_doc = pyrf_comm_event__doc,
172 .tp_members = pyrf_comm_event__members,
173 .tp_repr = (reprfunc)pyrf_comm_event__repr,
176 static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
178 static PyMemberDef pyrf_throttle_event__members[] = {
180 member_def(perf_event_header, type, T_UINT, "event type"),
181 member_def(throttle_event, time, T_ULONGLONG, "timestamp"),
182 member_def(throttle_event, id, T_ULONGLONG, "event id"),
183 member_def(throttle_event, stream_id, T_ULONGLONG, "event stream id"),
187 static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
189 struct throttle_event *te = (struct throttle_event *)(&pevent->event.header + 1);
191 return PyString_FromFormat("{ type: %sthrottle, time: %" PRIu64 ", id: %" PRIu64
192 ", stream_id: %" PRIu64 " }",
193 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
194 te->time, te->id, te->stream_id);
197 static PyTypeObject pyrf_throttle_event__type = {
198 PyVarObject_HEAD_INIT(NULL, 0)
199 .tp_name = "perf.throttle_event",
200 .tp_basicsize = sizeof(struct pyrf_event),
201 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
202 .tp_doc = pyrf_throttle_event__doc,
203 .tp_members = pyrf_throttle_event__members,
204 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
207 static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
209 static PyMemberDef pyrf_lost_event__members[] = {
211 member_def(lost_event, id, T_ULONGLONG, "event id"),
212 member_def(lost_event, lost, T_ULONGLONG, "number of lost events"),
216 static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
221 if (asprintf(&s, "{ type: lost, id: %#" PRIx64 ", "
222 "lost: %#" PRIx64 " }",
223 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
224 ret = PyErr_NoMemory();
226 ret = PyString_FromString(s);
232 static PyTypeObject pyrf_lost_event__type = {
233 PyVarObject_HEAD_INIT(NULL, 0)
234 .tp_name = "perf.lost_event",
235 .tp_basicsize = sizeof(struct pyrf_event),
236 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
237 .tp_doc = pyrf_lost_event__doc,
238 .tp_members = pyrf_lost_event__members,
239 .tp_repr = (reprfunc)pyrf_lost_event__repr,
242 static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
244 static PyMemberDef pyrf_read_event__members[] = {
246 member_def(read_event, pid, T_UINT, "event pid"),
247 member_def(read_event, tid, T_UINT, "event tid"),
251 static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
253 return PyString_FromFormat("{ type: read, pid: %u, tid: %u }",
254 pevent->event.read.pid,
255 pevent->event.read.tid);
257 * FIXME: return the array of read values,
258 * making this method useful ;-)
262 static PyTypeObject pyrf_read_event__type = {
263 PyVarObject_HEAD_INIT(NULL, 0)
264 .tp_name = "perf.read_event",
265 .tp_basicsize = sizeof(struct pyrf_event),
266 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
267 .tp_doc = pyrf_read_event__doc,
268 .tp_members = pyrf_read_event__members,
269 .tp_repr = (reprfunc)pyrf_read_event__repr,
272 static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
274 static PyMemberDef pyrf_sample_event__members[] = {
276 member_def(perf_event_header, type, T_UINT, "event type"),
280 static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
285 if (asprintf(&s, "{ type: sample }") < 0) {
286 ret = PyErr_NoMemory();
288 ret = PyString_FromString(s);
294 static bool is_tracepoint(struct pyrf_event *pevent)
296 return pevent->evsel->attr.type == PERF_TYPE_TRACEPOINT;
300 tracepoint_field(struct pyrf_event *pe, struct format_field *field)
302 struct pevent *pevent = field->event->pevent;
303 void *data = pe->sample.raw_data;
304 PyObject *ret = NULL;
305 unsigned long long val;
306 unsigned int offset, len;
308 if (field->flags & FIELD_IS_ARRAY) {
309 offset = field->offset;
311 if (field->flags & FIELD_IS_DYNAMIC) {
312 val = pevent_read_number(pevent, data + offset, len);
317 if (field->flags & FIELD_IS_STRING &&
318 is_printable_array(data + offset, len)) {
319 ret = PyString_FromString((char *)data + offset);
321 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
322 field->flags &= ~FIELD_IS_STRING;
325 val = pevent_read_number(pevent, data + field->offset,
327 if (field->flags & FIELD_IS_POINTER)
328 ret = PyLong_FromUnsignedLong((unsigned long) val);
329 else if (field->flags & FIELD_IS_SIGNED)
330 ret = PyLong_FromLong((long) val);
332 ret = PyLong_FromUnsignedLong((unsigned long) val);
339 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
341 const char *str = PyString_AsString(PyObject_Str(attr_name));
342 struct perf_evsel *evsel = pevent->evsel;
343 struct format_field *field;
345 if (!evsel->tp_format) {
346 struct event_format *tp_format;
348 tp_format = trace_event__tp_format_id(evsel->attr.config);
352 evsel->tp_format = tp_format;
355 field = pevent_find_any_field(evsel->tp_format, str);
359 return tracepoint_field(pevent, field);
363 pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
365 PyObject *obj = NULL;
367 if (is_tracepoint(pevent))
368 obj = get_tracepoint_field(pevent, attr_name);
370 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
373 static PyTypeObject pyrf_sample_event__type = {
374 PyVarObject_HEAD_INIT(NULL, 0)
375 .tp_name = "perf.sample_event",
376 .tp_basicsize = sizeof(struct pyrf_event),
377 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
378 .tp_doc = pyrf_sample_event__doc,
379 .tp_members = pyrf_sample_event__members,
380 .tp_repr = (reprfunc)pyrf_sample_event__repr,
381 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
384 static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
386 static PyMemberDef pyrf_context_switch_event__members[] = {
388 member_def(perf_event_header, type, T_UINT, "event type"),
389 member_def(context_switch_event, next_prev_pid, T_UINT, "next/prev pid"),
390 member_def(context_switch_event, next_prev_tid, T_UINT, "next/prev tid"),
394 static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
399 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
400 pevent->event.context_switch.next_prev_pid,
401 pevent->event.context_switch.next_prev_tid,
402 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
403 ret = PyErr_NoMemory();
405 ret = PyString_FromString(s);
411 static PyTypeObject pyrf_context_switch_event__type = {
412 PyVarObject_HEAD_INIT(NULL, 0)
413 .tp_name = "perf.context_switch_event",
414 .tp_basicsize = sizeof(struct pyrf_event),
415 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
416 .tp_doc = pyrf_context_switch_event__doc,
417 .tp_members = pyrf_context_switch_event__members,
418 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
421 static int pyrf_event__setup_types(void)
424 pyrf_mmap_event__type.tp_new =
425 pyrf_task_event__type.tp_new =
426 pyrf_comm_event__type.tp_new =
427 pyrf_lost_event__type.tp_new =
428 pyrf_read_event__type.tp_new =
429 pyrf_sample_event__type.tp_new =
430 pyrf_context_switch_event__type.tp_new =
431 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
432 err = PyType_Ready(&pyrf_mmap_event__type);
435 err = PyType_Ready(&pyrf_lost_event__type);
438 err = PyType_Ready(&pyrf_task_event__type);
441 err = PyType_Ready(&pyrf_comm_event__type);
444 err = PyType_Ready(&pyrf_throttle_event__type);
447 err = PyType_Ready(&pyrf_read_event__type);
450 err = PyType_Ready(&pyrf_sample_event__type);
453 err = PyType_Ready(&pyrf_context_switch_event__type);
460 static PyTypeObject *pyrf_event__type[] = {
461 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
462 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
463 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
464 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
465 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
466 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
467 [PERF_RECORD_FORK] = &pyrf_task_event__type,
468 [PERF_RECORD_READ] = &pyrf_read_event__type,
469 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
470 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
471 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
474 static PyObject *pyrf_event__new(union perf_event *event)
476 struct pyrf_event *pevent;
479 if ((event->header.type < PERF_RECORD_MMAP ||
480 event->header.type > PERF_RECORD_SAMPLE) &&
481 !(event->header.type == PERF_RECORD_SWITCH ||
482 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
485 ptype = pyrf_event__type[event->header.type];
486 pevent = PyObject_New(struct pyrf_event, ptype);
488 memcpy(&pevent->event, event, event->header.size);
489 return (PyObject *)pevent;
492 struct pyrf_cpu_map {
495 struct cpu_map *cpus;
498 static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
499 PyObject *args, PyObject *kwargs)
501 static char *kwlist[] = { "cpustr", NULL };
504 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
508 pcpus->cpus = cpu_map__new(cpustr);
509 if (pcpus->cpus == NULL)
514 static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
516 cpu_map__put(pcpus->cpus);
517 pcpus->ob_type->tp_free((PyObject*)pcpus);
520 static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
522 struct pyrf_cpu_map *pcpus = (void *)obj;
524 return pcpus->cpus->nr;
527 static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
529 struct pyrf_cpu_map *pcpus = (void *)obj;
531 if (i >= pcpus->cpus->nr)
534 return Py_BuildValue("i", pcpus->cpus->map[i]);
537 static PySequenceMethods pyrf_cpu_map__sequence_methods = {
538 .sq_length = pyrf_cpu_map__length,
539 .sq_item = pyrf_cpu_map__item,
542 static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
544 static PyTypeObject pyrf_cpu_map__type = {
545 PyVarObject_HEAD_INIT(NULL, 0)
546 .tp_name = "perf.cpu_map",
547 .tp_basicsize = sizeof(struct pyrf_cpu_map),
548 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
549 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
550 .tp_doc = pyrf_cpu_map__doc,
551 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
552 .tp_init = (initproc)pyrf_cpu_map__init,
555 static int pyrf_cpu_map__setup_types(void)
557 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
558 return PyType_Ready(&pyrf_cpu_map__type);
561 struct pyrf_thread_map {
564 struct thread_map *threads;
567 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
568 PyObject *args, PyObject *kwargs)
570 static char *kwlist[] = { "pid", "tid", "uid", NULL };
571 int pid = -1, tid = -1, uid = UINT_MAX;
573 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
574 kwlist, &pid, &tid, &uid))
577 pthreads->threads = thread_map__new(pid, tid, uid);
578 if (pthreads->threads == NULL)
583 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
585 thread_map__put(pthreads->threads);
586 pthreads->ob_type->tp_free((PyObject*)pthreads);
589 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
591 struct pyrf_thread_map *pthreads = (void *)obj;
593 return pthreads->threads->nr;
596 static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
598 struct pyrf_thread_map *pthreads = (void *)obj;
600 if (i >= pthreads->threads->nr)
603 return Py_BuildValue("i", pthreads->threads->map[i]);
606 static PySequenceMethods pyrf_thread_map__sequence_methods = {
607 .sq_length = pyrf_thread_map__length,
608 .sq_item = pyrf_thread_map__item,
611 static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
613 static PyTypeObject pyrf_thread_map__type = {
614 PyVarObject_HEAD_INIT(NULL, 0)
615 .tp_name = "perf.thread_map",
616 .tp_basicsize = sizeof(struct pyrf_thread_map),
617 .tp_dealloc = (destructor)pyrf_thread_map__delete,
618 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
619 .tp_doc = pyrf_thread_map__doc,
620 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
621 .tp_init = (initproc)pyrf_thread_map__init,
624 static int pyrf_thread_map__setup_types(void)
626 pyrf_thread_map__type.tp_new = PyType_GenericNew;
627 return PyType_Ready(&pyrf_thread_map__type);
633 struct perf_evsel evsel;
636 static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
637 PyObject *args, PyObject *kwargs)
639 struct perf_event_attr attr = {
640 .type = PERF_TYPE_HARDWARE,
641 .config = PERF_COUNT_HW_CPU_CYCLES,
642 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
644 static char *kwlist[] = {
676 u64 sample_period = 0;
698 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
699 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
700 &attr.type, &attr.config, &attr.sample_freq,
701 &sample_period, &attr.sample_type,
702 &attr.read_format, &disabled, &inherit,
703 &pinned, &exclusive, &exclude_user,
704 &exclude_kernel, &exclude_hv, &exclude_idle,
705 &mmap, &context_switch, &comm, &freq, &inherit_stat,
706 &enable_on_exec, &task, &watermark,
707 &precise_ip, &mmap_data, &sample_id_all,
708 &attr.wakeup_events, &attr.bp_type,
709 &attr.bp_addr, &attr.bp_len, &idx))
713 if (sample_period != 0) {
714 if (attr.sample_freq != 0)
715 return -1; /* FIXME: throw right exception */
716 attr.sample_period = sample_period;
720 attr.disabled = disabled;
721 attr.inherit = inherit;
722 attr.pinned = pinned;
723 attr.exclusive = exclusive;
724 attr.exclude_user = exclude_user;
725 attr.exclude_kernel = exclude_kernel;
726 attr.exclude_hv = exclude_hv;
727 attr.exclude_idle = exclude_idle;
729 attr.context_switch = context_switch;
732 attr.inherit_stat = inherit_stat;
733 attr.enable_on_exec = enable_on_exec;
735 attr.watermark = watermark;
736 attr.precise_ip = precise_ip;
737 attr.mmap_data = mmap_data;
738 attr.sample_id_all = sample_id_all;
739 attr.size = sizeof(attr);
741 perf_evsel__init(&pevsel->evsel, &attr, idx);
745 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
747 perf_evsel__exit(&pevsel->evsel);
748 pevsel->ob_type->tp_free((PyObject*)pevsel);
751 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
752 PyObject *args, PyObject *kwargs)
754 struct perf_evsel *evsel = &pevsel->evsel;
755 struct cpu_map *cpus = NULL;
756 struct thread_map *threads = NULL;
757 PyObject *pcpus = NULL, *pthreads = NULL;
758 int group = 0, inherit = 0;
759 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
761 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
762 &pcpus, &pthreads, &group, &inherit))
765 if (pthreads != NULL)
766 threads = ((struct pyrf_thread_map *)pthreads)->threads;
769 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
771 evsel->attr.inherit = inherit;
773 * This will group just the fds for this single evsel, to group
774 * multiple events, use evlist.open().
776 if (perf_evsel__open(evsel, cpus, threads) < 0) {
777 PyErr_SetFromErrno(PyExc_OSError);
785 static PyMethodDef pyrf_evsel__methods[] = {
788 .ml_meth = (PyCFunction)pyrf_evsel__open,
789 .ml_flags = METH_VARARGS | METH_KEYWORDS,
790 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
795 static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
797 static PyTypeObject pyrf_evsel__type = {
798 PyVarObject_HEAD_INIT(NULL, 0)
799 .tp_name = "perf.evsel",
800 .tp_basicsize = sizeof(struct pyrf_evsel),
801 .tp_dealloc = (destructor)pyrf_evsel__delete,
802 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
803 .tp_doc = pyrf_evsel__doc,
804 .tp_methods = pyrf_evsel__methods,
805 .tp_init = (initproc)pyrf_evsel__init,
808 static int pyrf_evsel__setup_types(void)
810 pyrf_evsel__type.tp_new = PyType_GenericNew;
811 return PyType_Ready(&pyrf_evsel__type);
817 struct perf_evlist evlist;
820 static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
821 PyObject *args, PyObject *kwargs __maybe_unused)
823 PyObject *pcpus = NULL, *pthreads = NULL;
824 struct cpu_map *cpus;
825 struct thread_map *threads;
827 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
830 threads = ((struct pyrf_thread_map *)pthreads)->threads;
831 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
832 perf_evlist__init(&pevlist->evlist, cpus, threads);
836 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
838 perf_evlist__exit(&pevlist->evlist);
839 pevlist->ob_type->tp_free((PyObject*)pevlist);
842 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
843 PyObject *args, PyObject *kwargs)
845 struct perf_evlist *evlist = &pevlist->evlist;
846 static char *kwlist[] = { "pages", "overwrite", NULL };
847 int pages = 128, overwrite = false;
849 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
853 if (perf_evlist__mmap(evlist, pages, overwrite) < 0) {
854 PyErr_SetFromErrno(PyExc_OSError);
862 static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
863 PyObject *args, PyObject *kwargs)
865 struct perf_evlist *evlist = &pevlist->evlist;
866 static char *kwlist[] = { "timeout", NULL };
869 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
872 n = perf_evlist__poll(evlist, timeout);
874 PyErr_SetFromErrno(PyExc_OSError);
878 return Py_BuildValue("i", n);
881 static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
882 PyObject *args __maybe_unused,
883 PyObject *kwargs __maybe_unused)
885 struct perf_evlist *evlist = &pevlist->evlist;
886 PyObject *list = PyList_New(0);
889 for (i = 0; i < evlist->pollfd.nr; ++i) {
891 FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
896 file = PyFile_FromFile(fp, "perf", "r", NULL);
900 if (PyList_Append(list, file) != 0) {
910 return PyErr_NoMemory();
914 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
916 PyObject *kwargs __maybe_unused)
918 struct perf_evlist *evlist = &pevlist->evlist;
920 struct perf_evsel *evsel;
922 if (!PyArg_ParseTuple(args, "O", &pevsel))
926 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
927 evsel->idx = evlist->nr_entries;
928 perf_evlist__add(evlist, evsel);
930 return Py_BuildValue("i", evlist->nr_entries);
933 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
934 PyObject *args, PyObject *kwargs)
936 struct perf_evlist *evlist = &pevlist->evlist;
937 union perf_event *event;
938 int sample_id_all = 1, cpu;
939 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
942 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
943 &cpu, &sample_id_all))
946 event = perf_evlist__mmap_read(evlist, cpu);
948 PyObject *pyevent = pyrf_event__new(event);
949 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
950 struct perf_evsel *evsel;
953 return PyErr_NoMemory();
955 evsel = perf_evlist__event2evsel(evlist, event);
959 pevent->evsel = evsel;
961 err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
963 /* Consume the even only after we parsed it out. */
964 perf_evlist__mmap_consume(evlist, cpu);
967 return PyErr_Format(PyExc_OSError,
968 "perf: can't parse sample, err=%d", err);
976 static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
977 PyObject *args, PyObject *kwargs)
979 struct perf_evlist *evlist = &pevlist->evlist;
981 static char *kwlist[] = { "group", NULL };
983 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
987 perf_evlist__set_leader(evlist);
989 if (perf_evlist__open(evlist) < 0) {
990 PyErr_SetFromErrno(PyExc_OSError);
998 static PyMethodDef pyrf_evlist__methods[] = {
1001 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1002 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1003 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1007 .ml_meth = (PyCFunction)pyrf_evlist__open,
1008 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1009 .ml_doc = PyDoc_STR("open the file descriptors.")
1013 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1014 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1015 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1018 .ml_name = "get_pollfd",
1019 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1020 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1021 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1025 .ml_meth = (PyCFunction)pyrf_evlist__add,
1026 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1027 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1030 .ml_name = "read_on_cpu",
1031 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1032 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1033 .ml_doc = PyDoc_STR("reads an event.")
1035 { .ml_name = NULL, }
1038 static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1040 struct pyrf_evlist *pevlist = (void *)obj;
1042 return pevlist->evlist.nr_entries;
1045 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1047 struct pyrf_evlist *pevlist = (void *)obj;
1048 struct perf_evsel *pos;
1050 if (i >= pevlist->evlist.nr_entries)
1053 evlist__for_each_entry(&pevlist->evlist, pos) {
1058 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1061 static PySequenceMethods pyrf_evlist__sequence_methods = {
1062 .sq_length = pyrf_evlist__length,
1063 .sq_item = pyrf_evlist__item,
1066 static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1068 static PyTypeObject pyrf_evlist__type = {
1069 PyVarObject_HEAD_INIT(NULL, 0)
1070 .tp_name = "perf.evlist",
1071 .tp_basicsize = sizeof(struct pyrf_evlist),
1072 .tp_dealloc = (destructor)pyrf_evlist__delete,
1073 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1074 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1075 .tp_doc = pyrf_evlist__doc,
1076 .tp_methods = pyrf_evlist__methods,
1077 .tp_init = (initproc)pyrf_evlist__init,
1080 static int pyrf_evlist__setup_types(void)
1082 pyrf_evlist__type.tp_new = PyType_GenericNew;
1083 return PyType_Ready(&pyrf_evlist__type);
1086 #define PERF_CONST(name) { #name, PERF_##name }
1091 } perf__constants[] = {
1092 PERF_CONST(TYPE_HARDWARE),
1093 PERF_CONST(TYPE_SOFTWARE),
1094 PERF_CONST(TYPE_TRACEPOINT),
1095 PERF_CONST(TYPE_HW_CACHE),
1096 PERF_CONST(TYPE_RAW),
1097 PERF_CONST(TYPE_BREAKPOINT),
1099 PERF_CONST(COUNT_HW_CPU_CYCLES),
1100 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1101 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1102 PERF_CONST(COUNT_HW_CACHE_MISSES),
1103 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1104 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1105 PERF_CONST(COUNT_HW_BUS_CYCLES),
1106 PERF_CONST(COUNT_HW_CACHE_L1D),
1107 PERF_CONST(COUNT_HW_CACHE_L1I),
1108 PERF_CONST(COUNT_HW_CACHE_LL),
1109 PERF_CONST(COUNT_HW_CACHE_DTLB),
1110 PERF_CONST(COUNT_HW_CACHE_ITLB),
1111 PERF_CONST(COUNT_HW_CACHE_BPU),
1112 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1113 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1114 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1115 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1116 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1118 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1119 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1121 PERF_CONST(COUNT_SW_CPU_CLOCK),
1122 PERF_CONST(COUNT_SW_TASK_CLOCK),
1123 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1124 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1125 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1126 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1127 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1128 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1129 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1130 PERF_CONST(COUNT_SW_DUMMY),
1132 PERF_CONST(SAMPLE_IP),
1133 PERF_CONST(SAMPLE_TID),
1134 PERF_CONST(SAMPLE_TIME),
1135 PERF_CONST(SAMPLE_ADDR),
1136 PERF_CONST(SAMPLE_READ),
1137 PERF_CONST(SAMPLE_CALLCHAIN),
1138 PERF_CONST(SAMPLE_ID),
1139 PERF_CONST(SAMPLE_CPU),
1140 PERF_CONST(SAMPLE_PERIOD),
1141 PERF_CONST(SAMPLE_STREAM_ID),
1142 PERF_CONST(SAMPLE_RAW),
1144 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1145 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1146 PERF_CONST(FORMAT_ID),
1147 PERF_CONST(FORMAT_GROUP),
1149 PERF_CONST(RECORD_MMAP),
1150 PERF_CONST(RECORD_LOST),
1151 PERF_CONST(RECORD_COMM),
1152 PERF_CONST(RECORD_EXIT),
1153 PERF_CONST(RECORD_THROTTLE),
1154 PERF_CONST(RECORD_UNTHROTTLE),
1155 PERF_CONST(RECORD_FORK),
1156 PERF_CONST(RECORD_READ),
1157 PERF_CONST(RECORD_SAMPLE),
1158 PERF_CONST(RECORD_MMAP2),
1159 PERF_CONST(RECORD_AUX),
1160 PERF_CONST(RECORD_ITRACE_START),
1161 PERF_CONST(RECORD_LOST_SAMPLES),
1162 PERF_CONST(RECORD_SWITCH),
1163 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1165 PERF_CONST(RECORD_MISC_SWITCH_OUT),
1169 static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1170 PyObject *args, PyObject *kwargs)
1172 struct event_format *tp_format;
1173 static char *kwlist[] = { "sys", "name", NULL };
1177 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1181 tp_format = trace_event__tp_format(sys, name);
1182 if (IS_ERR(tp_format))
1183 return PyInt_FromLong(-1);
1185 return PyInt_FromLong(tp_format->id);
1188 static PyMethodDef perf__methods[] = {
1190 .ml_name = "tracepoint",
1191 .ml_meth = (PyCFunction) pyrf__tracepoint,
1192 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1193 .ml_doc = PyDoc_STR("Get tracepoint config.")
1195 { .ml_name = NULL, }
1198 PyMODINIT_FUNC initperf(void)
1202 PyObject *dict, *module = Py_InitModule("perf", perf__methods);
1204 if (module == NULL ||
1205 pyrf_event__setup_types() < 0 ||
1206 pyrf_evlist__setup_types() < 0 ||
1207 pyrf_evsel__setup_types() < 0 ||
1208 pyrf_thread_map__setup_types() < 0 ||
1209 pyrf_cpu_map__setup_types() < 0)
1212 /* The page_size is placed in util object. */
1213 page_size = sysconf(_SC_PAGE_SIZE);
1215 Py_INCREF(&pyrf_evlist__type);
1216 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1218 Py_INCREF(&pyrf_evsel__type);
1219 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1221 Py_INCREF(&pyrf_mmap_event__type);
1222 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1224 Py_INCREF(&pyrf_lost_event__type);
1225 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1227 Py_INCREF(&pyrf_comm_event__type);
1228 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1230 Py_INCREF(&pyrf_task_event__type);
1231 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1233 Py_INCREF(&pyrf_throttle_event__type);
1234 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1236 Py_INCREF(&pyrf_task_event__type);
1237 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1239 Py_INCREF(&pyrf_read_event__type);
1240 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1242 Py_INCREF(&pyrf_sample_event__type);
1243 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1245 Py_INCREF(&pyrf_context_switch_event__type);
1246 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1248 Py_INCREF(&pyrf_thread_map__type);
1249 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1251 Py_INCREF(&pyrf_cpu_map__type);
1252 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1254 dict = PyModule_GetDict(module);
1258 for (i = 0; perf__constants[i].name != NULL; i++) {
1259 obj = PyInt_FromLong(perf__constants[i].value);
1262 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1267 if (PyErr_Occurred())
1268 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1272 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1275 void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
1276 int fd, int group_fd, unsigned long flags)