1 // SPDX-License-Identifier: GPL-2.0
3 #include <structmember.h>
7 #include <perf/cpumap.h>
13 #include "print_binary.h"
14 #include "thread_map.h"
18 #if PY_MAJOR_VERSION < 3
19 #define _PyUnicode_FromString(arg) \
20 PyString_FromString(arg)
21 #define _PyUnicode_AsString(arg) \
22 PyString_AsString(arg)
23 #define _PyUnicode_FromFormat(...) \
24 PyString_FromFormat(__VA_ARGS__)
25 #define _PyLong_FromLong(arg) \
30 #define _PyUnicode_FromString(arg) \
31 PyUnicode_FromString(arg)
32 #define _PyUnicode_FromFormat(...) \
33 PyUnicode_FromFormat(__VA_ARGS__)
34 #define _PyLong_FromLong(arg) \
39 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
43 * Provide these two so that we don't have to link against callchain.c and
44 * start dragging hist.c, etc.
46 struct callchain_param callchain_param;
48 int parse_callchain_record(const char *arg __maybe_unused,
49 struct callchain_param *param __maybe_unused)
55 * Support debug printing even though util/debug.c is not linked. That means
56 * implementing 'verbose' and 'eprintf'.
60 int eprintf(int level, int var, const char *fmt, ...)
67 ret = vfprintf(stderr, fmt, args);
74 /* Define PyVarObject_HEAD_INIT for python 2.5 */
75 #ifndef PyVarObject_HEAD_INIT
76 # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
79 #if PY_MAJOR_VERSION < 3
80 PyMODINIT_FUNC initperf(void);
82 PyMODINIT_FUNC PyInit_perf(void);
85 #define member_def(type, member, ptype, help) \
87 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
90 #define sample_member_def(name, member, ptype, help) \
92 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
98 struct perf_sample sample;
99 union perf_event event;
102 #define sample_members \
103 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
104 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
105 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
106 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
107 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
108 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
109 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
110 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
111 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
113 static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
115 static PyMemberDef pyrf_mmap_event__members[] = {
117 member_def(perf_event_header, type, T_UINT, "event type"),
118 member_def(perf_event_header, misc, T_UINT, "event misc"),
119 member_def(perf_record_mmap, pid, T_UINT, "event pid"),
120 member_def(perf_record_mmap, tid, T_UINT, "event tid"),
121 member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
122 member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
123 member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
124 member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
128 static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
133 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
134 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
136 pevent->event.mmap.pid, pevent->event.mmap.tid,
137 pevent->event.mmap.start, pevent->event.mmap.len,
138 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
139 ret = PyErr_NoMemory();
141 ret = _PyUnicode_FromString(s);
147 static PyTypeObject pyrf_mmap_event__type = {
148 PyVarObject_HEAD_INIT(NULL, 0)
149 .tp_name = "perf.mmap_event",
150 .tp_basicsize = sizeof(struct pyrf_event),
151 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
152 .tp_doc = pyrf_mmap_event__doc,
153 .tp_members = pyrf_mmap_event__members,
154 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
157 static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
159 static PyMemberDef pyrf_task_event__members[] = {
161 member_def(perf_event_header, type, T_UINT, "event type"),
162 member_def(perf_record_fork, pid, T_UINT, "event pid"),
163 member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
164 member_def(perf_record_fork, tid, T_UINT, "event tid"),
165 member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
166 member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
170 static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
172 return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
173 "ptid: %u, time: %" PRI_lu64 "}",
174 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
175 pevent->event.fork.pid,
176 pevent->event.fork.ppid,
177 pevent->event.fork.tid,
178 pevent->event.fork.ptid,
179 pevent->event.fork.time);
182 static PyTypeObject pyrf_task_event__type = {
183 PyVarObject_HEAD_INIT(NULL, 0)
184 .tp_name = "perf.task_event",
185 .tp_basicsize = sizeof(struct pyrf_event),
186 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
187 .tp_doc = pyrf_task_event__doc,
188 .tp_members = pyrf_task_event__members,
189 .tp_repr = (reprfunc)pyrf_task_event__repr,
192 static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
194 static PyMemberDef pyrf_comm_event__members[] = {
196 member_def(perf_event_header, type, T_UINT, "event type"),
197 member_def(perf_record_comm, pid, T_UINT, "event pid"),
198 member_def(perf_record_comm, tid, T_UINT, "event tid"),
199 member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
203 static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
205 return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
206 pevent->event.comm.pid,
207 pevent->event.comm.tid,
208 pevent->event.comm.comm);
211 static PyTypeObject pyrf_comm_event__type = {
212 PyVarObject_HEAD_INIT(NULL, 0)
213 .tp_name = "perf.comm_event",
214 .tp_basicsize = sizeof(struct pyrf_event),
215 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
216 .tp_doc = pyrf_comm_event__doc,
217 .tp_members = pyrf_comm_event__members,
218 .tp_repr = (reprfunc)pyrf_comm_event__repr,
221 static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
223 static PyMemberDef pyrf_throttle_event__members[] = {
225 member_def(perf_event_header, type, T_UINT, "event type"),
226 member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
227 member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
228 member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
232 static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
234 struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1);
236 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
237 ", stream_id: %" PRI_lu64 " }",
238 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
239 te->time, te->id, te->stream_id);
242 static PyTypeObject pyrf_throttle_event__type = {
243 PyVarObject_HEAD_INIT(NULL, 0)
244 .tp_name = "perf.throttle_event",
245 .tp_basicsize = sizeof(struct pyrf_event),
246 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
247 .tp_doc = pyrf_throttle_event__doc,
248 .tp_members = pyrf_throttle_event__members,
249 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
252 static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
254 static PyMemberDef pyrf_lost_event__members[] = {
256 member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
257 member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
261 static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
266 if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
267 "lost: %#" PRI_lx64 " }",
268 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
269 ret = PyErr_NoMemory();
271 ret = _PyUnicode_FromString(s);
277 static PyTypeObject pyrf_lost_event__type = {
278 PyVarObject_HEAD_INIT(NULL, 0)
279 .tp_name = "perf.lost_event",
280 .tp_basicsize = sizeof(struct pyrf_event),
281 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
282 .tp_doc = pyrf_lost_event__doc,
283 .tp_members = pyrf_lost_event__members,
284 .tp_repr = (reprfunc)pyrf_lost_event__repr,
287 static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
289 static PyMemberDef pyrf_read_event__members[] = {
291 member_def(perf_record_read, pid, T_UINT, "event pid"),
292 member_def(perf_record_read, tid, T_UINT, "event tid"),
296 static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
298 return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
299 pevent->event.read.pid,
300 pevent->event.read.tid);
302 * FIXME: return the array of read values,
303 * making this method useful ;-)
307 static PyTypeObject pyrf_read_event__type = {
308 PyVarObject_HEAD_INIT(NULL, 0)
309 .tp_name = "perf.read_event",
310 .tp_basicsize = sizeof(struct pyrf_event),
311 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
312 .tp_doc = pyrf_read_event__doc,
313 .tp_members = pyrf_read_event__members,
314 .tp_repr = (reprfunc)pyrf_read_event__repr,
317 static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
319 static PyMemberDef pyrf_sample_event__members[] = {
321 member_def(perf_event_header, type, T_UINT, "event type"),
325 static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
330 if (asprintf(&s, "{ type: sample }") < 0) {
331 ret = PyErr_NoMemory();
333 ret = _PyUnicode_FromString(s);
339 static bool is_tracepoint(struct pyrf_event *pevent)
341 return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
345 tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
347 struct tep_handle *pevent = field->event->tep;
348 void *data = pe->sample.raw_data;
349 PyObject *ret = NULL;
350 unsigned long long val;
351 unsigned int offset, len;
353 if (field->flags & TEP_FIELD_IS_ARRAY) {
354 offset = field->offset;
356 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
357 val = tep_read_number(pevent, data + offset, len);
362 if (field->flags & TEP_FIELD_IS_STRING &&
363 is_printable_array(data + offset, len)) {
364 ret = _PyUnicode_FromString((char *)data + offset);
366 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
367 field->flags &= ~TEP_FIELD_IS_STRING;
370 val = tep_read_number(pevent, data + field->offset,
372 if (field->flags & TEP_FIELD_IS_POINTER)
373 ret = PyLong_FromUnsignedLong((unsigned long) val);
374 else if (field->flags & TEP_FIELD_IS_SIGNED)
375 ret = PyLong_FromLong((long) val);
377 ret = PyLong_FromUnsignedLong((unsigned long) val);
384 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
386 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
387 struct evsel *evsel = pevent->evsel;
388 struct tep_format_field *field;
390 if (!evsel->tp_format) {
391 struct tep_event *tp_format;
393 tp_format = trace_event__tp_format_id(evsel->core.attr.config);
397 evsel->tp_format = tp_format;
400 field = tep_find_any_field(evsel->tp_format, str);
404 return tracepoint_field(pevent, field);
408 pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
410 PyObject *obj = NULL;
412 if (is_tracepoint(pevent))
413 obj = get_tracepoint_field(pevent, attr_name);
415 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
418 static PyTypeObject pyrf_sample_event__type = {
419 PyVarObject_HEAD_INIT(NULL, 0)
420 .tp_name = "perf.sample_event",
421 .tp_basicsize = sizeof(struct pyrf_event),
422 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
423 .tp_doc = pyrf_sample_event__doc,
424 .tp_members = pyrf_sample_event__members,
425 .tp_repr = (reprfunc)pyrf_sample_event__repr,
426 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
429 static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
431 static PyMemberDef pyrf_context_switch_event__members[] = {
433 member_def(perf_event_header, type, T_UINT, "event type"),
434 member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
435 member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
439 static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
444 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
445 pevent->event.context_switch.next_prev_pid,
446 pevent->event.context_switch.next_prev_tid,
447 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
448 ret = PyErr_NoMemory();
450 ret = _PyUnicode_FromString(s);
456 static PyTypeObject pyrf_context_switch_event__type = {
457 PyVarObject_HEAD_INIT(NULL, 0)
458 .tp_name = "perf.context_switch_event",
459 .tp_basicsize = sizeof(struct pyrf_event),
460 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
461 .tp_doc = pyrf_context_switch_event__doc,
462 .tp_members = pyrf_context_switch_event__members,
463 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
466 static int pyrf_event__setup_types(void)
469 pyrf_mmap_event__type.tp_new =
470 pyrf_task_event__type.tp_new =
471 pyrf_comm_event__type.tp_new =
472 pyrf_lost_event__type.tp_new =
473 pyrf_read_event__type.tp_new =
474 pyrf_sample_event__type.tp_new =
475 pyrf_context_switch_event__type.tp_new =
476 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
477 err = PyType_Ready(&pyrf_mmap_event__type);
480 err = PyType_Ready(&pyrf_lost_event__type);
483 err = PyType_Ready(&pyrf_task_event__type);
486 err = PyType_Ready(&pyrf_comm_event__type);
489 err = PyType_Ready(&pyrf_throttle_event__type);
492 err = PyType_Ready(&pyrf_read_event__type);
495 err = PyType_Ready(&pyrf_sample_event__type);
498 err = PyType_Ready(&pyrf_context_switch_event__type);
505 static PyTypeObject *pyrf_event__type[] = {
506 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
507 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
508 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
509 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
510 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
511 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
512 [PERF_RECORD_FORK] = &pyrf_task_event__type,
513 [PERF_RECORD_READ] = &pyrf_read_event__type,
514 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
515 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
516 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
519 static PyObject *pyrf_event__new(union perf_event *event)
521 struct pyrf_event *pevent;
524 if ((event->header.type < PERF_RECORD_MMAP ||
525 event->header.type > PERF_RECORD_SAMPLE) &&
526 !(event->header.type == PERF_RECORD_SWITCH ||
527 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
530 ptype = pyrf_event__type[event->header.type];
531 pevent = PyObject_New(struct pyrf_event, ptype);
533 memcpy(&pevent->event, event, event->header.size);
534 return (PyObject *)pevent;
537 struct pyrf_cpu_map {
540 struct perf_cpu_map *cpus;
543 static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
544 PyObject *args, PyObject *kwargs)
546 static char *kwlist[] = { "cpustr", NULL };
549 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
553 pcpus->cpus = perf_cpu_map__new(cpustr);
554 if (pcpus->cpus == NULL)
559 static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
561 perf_cpu_map__put(pcpus->cpus);
562 Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
565 static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
567 struct pyrf_cpu_map *pcpus = (void *)obj;
569 return pcpus->cpus->nr;
572 static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
574 struct pyrf_cpu_map *pcpus = (void *)obj;
576 if (i >= pcpus->cpus->nr)
579 return Py_BuildValue("i", pcpus->cpus->map[i]);
582 static PySequenceMethods pyrf_cpu_map__sequence_methods = {
583 .sq_length = pyrf_cpu_map__length,
584 .sq_item = pyrf_cpu_map__item,
587 static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
589 static PyTypeObject pyrf_cpu_map__type = {
590 PyVarObject_HEAD_INIT(NULL, 0)
591 .tp_name = "perf.cpu_map",
592 .tp_basicsize = sizeof(struct pyrf_cpu_map),
593 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
594 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
595 .tp_doc = pyrf_cpu_map__doc,
596 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
597 .tp_init = (initproc)pyrf_cpu_map__init,
600 static int pyrf_cpu_map__setup_types(void)
602 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
603 return PyType_Ready(&pyrf_cpu_map__type);
606 struct pyrf_thread_map {
609 struct perf_thread_map *threads;
612 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
613 PyObject *args, PyObject *kwargs)
615 static char *kwlist[] = { "pid", "tid", "uid", NULL };
616 int pid = -1, tid = -1, uid = UINT_MAX;
618 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
619 kwlist, &pid, &tid, &uid))
622 pthreads->threads = thread_map__new(pid, tid, uid);
623 if (pthreads->threads == NULL)
628 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
630 perf_thread_map__put(pthreads->threads);
631 Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
634 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
636 struct pyrf_thread_map *pthreads = (void *)obj;
638 return pthreads->threads->nr;
641 static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
643 struct pyrf_thread_map *pthreads = (void *)obj;
645 if (i >= pthreads->threads->nr)
648 return Py_BuildValue("i", pthreads->threads->map[i]);
651 static PySequenceMethods pyrf_thread_map__sequence_methods = {
652 .sq_length = pyrf_thread_map__length,
653 .sq_item = pyrf_thread_map__item,
656 static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
658 static PyTypeObject pyrf_thread_map__type = {
659 PyVarObject_HEAD_INIT(NULL, 0)
660 .tp_name = "perf.thread_map",
661 .tp_basicsize = sizeof(struct pyrf_thread_map),
662 .tp_dealloc = (destructor)pyrf_thread_map__delete,
663 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
664 .tp_doc = pyrf_thread_map__doc,
665 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
666 .tp_init = (initproc)pyrf_thread_map__init,
669 static int pyrf_thread_map__setup_types(void)
671 pyrf_thread_map__type.tp_new = PyType_GenericNew;
672 return PyType_Ready(&pyrf_thread_map__type);
681 static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
682 PyObject *args, PyObject *kwargs)
684 struct perf_event_attr attr = {
685 .type = PERF_TYPE_HARDWARE,
686 .config = PERF_COUNT_HW_CPU_CYCLES,
687 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
689 static char *kwlist[] = {
721 u64 sample_period = 0;
743 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
744 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
745 &attr.type, &attr.config, &attr.sample_freq,
746 &sample_period, &attr.sample_type,
747 &attr.read_format, &disabled, &inherit,
748 &pinned, &exclusive, &exclude_user,
749 &exclude_kernel, &exclude_hv, &exclude_idle,
750 &mmap, &context_switch, &comm, &freq, &inherit_stat,
751 &enable_on_exec, &task, &watermark,
752 &precise_ip, &mmap_data, &sample_id_all,
753 &attr.wakeup_events, &attr.bp_type,
754 &attr.bp_addr, &attr.bp_len, &idx))
758 if (sample_period != 0) {
759 if (attr.sample_freq != 0)
760 return -1; /* FIXME: throw right exception */
761 attr.sample_period = sample_period;
765 attr.disabled = disabled;
766 attr.inherit = inherit;
767 attr.pinned = pinned;
768 attr.exclusive = exclusive;
769 attr.exclude_user = exclude_user;
770 attr.exclude_kernel = exclude_kernel;
771 attr.exclude_hv = exclude_hv;
772 attr.exclude_idle = exclude_idle;
774 attr.context_switch = context_switch;
777 attr.inherit_stat = inherit_stat;
778 attr.enable_on_exec = enable_on_exec;
780 attr.watermark = watermark;
781 attr.precise_ip = precise_ip;
782 attr.mmap_data = mmap_data;
783 attr.sample_id_all = sample_id_all;
784 attr.size = sizeof(attr);
786 evsel__init(&pevsel->evsel, &attr, idx);
790 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
792 perf_evsel__exit(&pevsel->evsel);
793 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
796 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
797 PyObject *args, PyObject *kwargs)
799 struct evsel *evsel = &pevsel->evsel;
800 struct perf_cpu_map *cpus = NULL;
801 struct perf_thread_map *threads = NULL;
802 PyObject *pcpus = NULL, *pthreads = NULL;
803 int group = 0, inherit = 0;
804 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
806 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
807 &pcpus, &pthreads, &group, &inherit))
810 if (pthreads != NULL)
811 threads = ((struct pyrf_thread_map *)pthreads)->threads;
814 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
816 evsel->core.attr.inherit = inherit;
818 * This will group just the fds for this single evsel, to group
819 * multiple events, use evlist.open().
821 if (evsel__open(evsel, cpus, threads) < 0) {
822 PyErr_SetFromErrno(PyExc_OSError);
830 static PyMethodDef pyrf_evsel__methods[] = {
833 .ml_meth = (PyCFunction)pyrf_evsel__open,
834 .ml_flags = METH_VARARGS | METH_KEYWORDS,
835 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
840 static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
842 static PyTypeObject pyrf_evsel__type = {
843 PyVarObject_HEAD_INIT(NULL, 0)
844 .tp_name = "perf.evsel",
845 .tp_basicsize = sizeof(struct pyrf_evsel),
846 .tp_dealloc = (destructor)pyrf_evsel__delete,
847 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
848 .tp_doc = pyrf_evsel__doc,
849 .tp_methods = pyrf_evsel__methods,
850 .tp_init = (initproc)pyrf_evsel__init,
853 static int pyrf_evsel__setup_types(void)
855 pyrf_evsel__type.tp_new = PyType_GenericNew;
856 return PyType_Ready(&pyrf_evsel__type);
862 struct evlist evlist;
865 static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
866 PyObject *args, PyObject *kwargs __maybe_unused)
868 PyObject *pcpus = NULL, *pthreads = NULL;
869 struct perf_cpu_map *cpus;
870 struct perf_thread_map *threads;
872 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
875 threads = ((struct pyrf_thread_map *)pthreads)->threads;
876 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
877 evlist__init(&pevlist->evlist, cpus, threads);
881 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
883 perf_evlist__exit(&pevlist->evlist);
884 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
887 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
888 PyObject *args, PyObject *kwargs)
890 struct evlist *evlist = &pevlist->evlist;
891 static char *kwlist[] = { "pages", "overwrite", NULL };
892 int pages = 128, overwrite = false;
894 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
898 if (perf_evlist__mmap(evlist, pages) < 0) {
899 PyErr_SetFromErrno(PyExc_OSError);
907 static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
908 PyObject *args, PyObject *kwargs)
910 struct evlist *evlist = &pevlist->evlist;
911 static char *kwlist[] = { "timeout", NULL };
914 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
917 n = perf_evlist__poll(evlist, timeout);
919 PyErr_SetFromErrno(PyExc_OSError);
923 return Py_BuildValue("i", n);
926 static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
927 PyObject *args __maybe_unused,
928 PyObject *kwargs __maybe_unused)
930 struct evlist *evlist = &pevlist->evlist;
931 PyObject *list = PyList_New(0);
934 for (i = 0; i < evlist->pollfd.nr; ++i) {
936 #if PY_MAJOR_VERSION < 3
937 FILE *fp = fdopen(evlist->pollfd.entries[i].fd, "r");
942 file = PyFile_FromFile(fp, "perf", "r", NULL);
944 file = PyFile_FromFd(evlist->pollfd.entries[i].fd, "perf", "r", -1,
945 NULL, NULL, NULL, 0);
950 if (PyList_Append(list, file) != 0) {
960 return PyErr_NoMemory();
964 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
966 PyObject *kwargs __maybe_unused)
968 struct evlist *evlist = &pevlist->evlist;
972 if (!PyArg_ParseTuple(args, "O", &pevsel))
976 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
977 evsel->idx = evlist->core.nr_entries;
978 evlist__add(evlist, evsel);
980 return Py_BuildValue("i", evlist->core.nr_entries);
983 static struct perf_mmap *get_md(struct evlist *evlist, int cpu)
987 for (i = 0; i < evlist->nr_mmaps; i++) {
988 struct perf_mmap *md = &evlist->mmap[i];
997 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
998 PyObject *args, PyObject *kwargs)
1000 struct evlist *evlist = &pevlist->evlist;
1001 union perf_event *event;
1002 int sample_id_all = 1, cpu;
1003 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
1004 struct perf_mmap *md;
1007 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1008 &cpu, &sample_id_all))
1011 md = get_md(evlist, cpu);
1015 if (perf_mmap__read_init(md) < 0)
1018 event = perf_mmap__read_event(md);
1019 if (event != NULL) {
1020 PyObject *pyevent = pyrf_event__new(event);
1021 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
1022 struct evsel *evsel;
1024 if (pyevent == NULL)
1025 return PyErr_NoMemory();
1027 evsel = perf_evlist__event2evsel(evlist, event);
1033 pevent->evsel = evsel;
1035 err = perf_evsel__parse_sample(evsel, event, &pevent->sample);
1037 /* Consume the even only after we parsed it out. */
1038 perf_mmap__consume(md);
1041 return PyErr_Format(PyExc_OSError,
1042 "perf: can't parse sample, err=%d", err);
1050 static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1051 PyObject *args, PyObject *kwargs)
1053 struct evlist *evlist = &pevlist->evlist;
1055 static char *kwlist[] = { "group", NULL };
1057 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1061 perf_evlist__set_leader(evlist);
1063 if (evlist__open(evlist) < 0) {
1064 PyErr_SetFromErrno(PyExc_OSError);
1072 static PyMethodDef pyrf_evlist__methods[] = {
1075 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1076 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1077 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1081 .ml_meth = (PyCFunction)pyrf_evlist__open,
1082 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1083 .ml_doc = PyDoc_STR("open the file descriptors.")
1087 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1088 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1089 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1092 .ml_name = "get_pollfd",
1093 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1094 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1095 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1099 .ml_meth = (PyCFunction)pyrf_evlist__add,
1100 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1101 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1104 .ml_name = "read_on_cpu",
1105 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1106 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1107 .ml_doc = PyDoc_STR("reads an event.")
1109 { .ml_name = NULL, }
1112 static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1114 struct pyrf_evlist *pevlist = (void *)obj;
1116 return pevlist->evlist.core.nr_entries;
1119 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1121 struct pyrf_evlist *pevlist = (void *)obj;
1124 if (i >= pevlist->evlist.core.nr_entries)
1127 evlist__for_each_entry(&pevlist->evlist, pos) {
1132 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1135 static PySequenceMethods pyrf_evlist__sequence_methods = {
1136 .sq_length = pyrf_evlist__length,
1137 .sq_item = pyrf_evlist__item,
1140 static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1142 static PyTypeObject pyrf_evlist__type = {
1143 PyVarObject_HEAD_INIT(NULL, 0)
1144 .tp_name = "perf.evlist",
1145 .tp_basicsize = sizeof(struct pyrf_evlist),
1146 .tp_dealloc = (destructor)pyrf_evlist__delete,
1147 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1148 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1149 .tp_doc = pyrf_evlist__doc,
1150 .tp_methods = pyrf_evlist__methods,
1151 .tp_init = (initproc)pyrf_evlist__init,
1154 static int pyrf_evlist__setup_types(void)
1156 pyrf_evlist__type.tp_new = PyType_GenericNew;
1157 return PyType_Ready(&pyrf_evlist__type);
1160 #define PERF_CONST(name) { #name, PERF_##name }
1165 } perf__constants[] = {
1166 PERF_CONST(TYPE_HARDWARE),
1167 PERF_CONST(TYPE_SOFTWARE),
1168 PERF_CONST(TYPE_TRACEPOINT),
1169 PERF_CONST(TYPE_HW_CACHE),
1170 PERF_CONST(TYPE_RAW),
1171 PERF_CONST(TYPE_BREAKPOINT),
1173 PERF_CONST(COUNT_HW_CPU_CYCLES),
1174 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1175 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1176 PERF_CONST(COUNT_HW_CACHE_MISSES),
1177 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1178 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1179 PERF_CONST(COUNT_HW_BUS_CYCLES),
1180 PERF_CONST(COUNT_HW_CACHE_L1D),
1181 PERF_CONST(COUNT_HW_CACHE_L1I),
1182 PERF_CONST(COUNT_HW_CACHE_LL),
1183 PERF_CONST(COUNT_HW_CACHE_DTLB),
1184 PERF_CONST(COUNT_HW_CACHE_ITLB),
1185 PERF_CONST(COUNT_HW_CACHE_BPU),
1186 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1187 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1188 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1189 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1190 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1192 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1193 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1195 PERF_CONST(COUNT_SW_CPU_CLOCK),
1196 PERF_CONST(COUNT_SW_TASK_CLOCK),
1197 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1198 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1199 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1200 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1201 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1202 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1203 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1204 PERF_CONST(COUNT_SW_DUMMY),
1206 PERF_CONST(SAMPLE_IP),
1207 PERF_CONST(SAMPLE_TID),
1208 PERF_CONST(SAMPLE_TIME),
1209 PERF_CONST(SAMPLE_ADDR),
1210 PERF_CONST(SAMPLE_READ),
1211 PERF_CONST(SAMPLE_CALLCHAIN),
1212 PERF_CONST(SAMPLE_ID),
1213 PERF_CONST(SAMPLE_CPU),
1214 PERF_CONST(SAMPLE_PERIOD),
1215 PERF_CONST(SAMPLE_STREAM_ID),
1216 PERF_CONST(SAMPLE_RAW),
1218 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1219 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1220 PERF_CONST(FORMAT_ID),
1221 PERF_CONST(FORMAT_GROUP),
1223 PERF_CONST(RECORD_MMAP),
1224 PERF_CONST(RECORD_LOST),
1225 PERF_CONST(RECORD_COMM),
1226 PERF_CONST(RECORD_EXIT),
1227 PERF_CONST(RECORD_THROTTLE),
1228 PERF_CONST(RECORD_UNTHROTTLE),
1229 PERF_CONST(RECORD_FORK),
1230 PERF_CONST(RECORD_READ),
1231 PERF_CONST(RECORD_SAMPLE),
1232 PERF_CONST(RECORD_MMAP2),
1233 PERF_CONST(RECORD_AUX),
1234 PERF_CONST(RECORD_ITRACE_START),
1235 PERF_CONST(RECORD_LOST_SAMPLES),
1236 PERF_CONST(RECORD_SWITCH),
1237 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1239 PERF_CONST(RECORD_MISC_SWITCH_OUT),
1243 static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1244 PyObject *args, PyObject *kwargs)
1246 struct tep_event *tp_format;
1247 static char *kwlist[] = { "sys", "name", NULL };
1251 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1255 tp_format = trace_event__tp_format(sys, name);
1256 if (IS_ERR(tp_format))
1257 return _PyLong_FromLong(-1);
1259 return _PyLong_FromLong(tp_format->id);
1262 static PyMethodDef perf__methods[] = {
1264 .ml_name = "tracepoint",
1265 .ml_meth = (PyCFunction) pyrf__tracepoint,
1266 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1267 .ml_doc = PyDoc_STR("Get tracepoint config.")
1269 { .ml_name = NULL, }
1272 #if PY_MAJOR_VERSION < 3
1273 PyMODINIT_FUNC initperf(void)
1275 PyMODINIT_FUNC PyInit_perf(void)
1281 #if PY_MAJOR_VERSION < 3
1282 PyObject *module = Py_InitModule("perf", perf__methods);
1284 static struct PyModuleDef moduledef = {
1285 PyModuleDef_HEAD_INIT,
1286 "perf", /* m_name */
1289 perf__methods, /* m_methods */
1290 NULL, /* m_reload */
1291 NULL, /* m_traverse */
1295 PyObject *module = PyModule_Create(&moduledef);
1298 if (module == NULL ||
1299 pyrf_event__setup_types() < 0 ||
1300 pyrf_evlist__setup_types() < 0 ||
1301 pyrf_evsel__setup_types() < 0 ||
1302 pyrf_thread_map__setup_types() < 0 ||
1303 pyrf_cpu_map__setup_types() < 0)
1304 #if PY_MAJOR_VERSION < 3
1310 /* The page_size is placed in util object. */
1311 page_size = sysconf(_SC_PAGE_SIZE);
1313 Py_INCREF(&pyrf_evlist__type);
1314 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1316 Py_INCREF(&pyrf_evsel__type);
1317 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1319 Py_INCREF(&pyrf_mmap_event__type);
1320 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1322 Py_INCREF(&pyrf_lost_event__type);
1323 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1325 Py_INCREF(&pyrf_comm_event__type);
1326 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1328 Py_INCREF(&pyrf_task_event__type);
1329 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1331 Py_INCREF(&pyrf_throttle_event__type);
1332 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1334 Py_INCREF(&pyrf_task_event__type);
1335 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1337 Py_INCREF(&pyrf_read_event__type);
1338 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1340 Py_INCREF(&pyrf_sample_event__type);
1341 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1343 Py_INCREF(&pyrf_context_switch_event__type);
1344 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1346 Py_INCREF(&pyrf_thread_map__type);
1347 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1349 Py_INCREF(&pyrf_cpu_map__type);
1350 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1352 dict = PyModule_GetDict(module);
1356 for (i = 0; perf__constants[i].name != NULL; i++) {
1357 obj = _PyLong_FromLong(perf__constants[i].value);
1360 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1365 if (PyErr_Occurred())
1366 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1367 #if PY_MAJOR_VERSION >= 3
1373 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1376 void test_attr__open(struct perf_event_attr *attr, pid_t pid, int cpu,
1377 int fd, int group_fd, unsigned long flags)