1 // SPDX-License-Identifier: GPL-2.0
3 #include <structmember.h>
7 #include <perf/cpumap.h>
8 #include <traceevent/event-parse.h>
11 #include "callchain.h"
14 #include "print_binary.h"
15 #include "thread_map.h"
16 #include "trace-event.h"
19 #include "metricgroup.h"
21 #include <internal/lib.h>
24 #if PY_MAJOR_VERSION < 3
25 #define _PyUnicode_FromString(arg) \
26 PyString_FromString(arg)
27 #define _PyUnicode_AsString(arg) \
28 PyString_AsString(arg)
29 #define _PyUnicode_FromFormat(...) \
30 PyString_FromFormat(__VA_ARGS__)
31 #define _PyLong_FromLong(arg) \
36 #define _PyUnicode_FromString(arg) \
37 PyUnicode_FromString(arg)
38 #define _PyUnicode_FromFormat(...) \
39 PyUnicode_FromFormat(__VA_ARGS__)
40 #define _PyLong_FromLong(arg) \
45 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
49 * Provide these two so that we don't have to link against callchain.c and
50 * start dragging hist.c, etc.
52 struct callchain_param callchain_param;
54 int parse_callchain_record(const char *arg __maybe_unused,
55 struct callchain_param *param __maybe_unused)
61 * Add this one here not to drag util/env.c
63 struct perf_env perf_env;
66 * Add this one here not to drag util/stat-shadow.c
68 void perf_stat__collect_metric_expr(struct evlist *evsel_list)
73 * This one is needed not to drag the PMU bandwagon, jevents generated
74 * pmu_sys_event_tables, etc and evsel__find_pmu() is used so far just for
75 * doing per PMU perf_event_attr.exclude_guest handling, not really needed, so
76 * far, for the perf python binding known usecases, revisit if this become
79 struct perf_pmu *evsel__find_pmu(struct evsel *evsel __maybe_unused)
85 * Add this one here not to drag util/metricgroup.c
87 int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
88 struct rblist *new_metric_events,
89 struct rblist *old_metric_events)
95 * XXX: All these evsel destructors need some better mechanism, like a linked
96 * list of destructors registered when the relevant code indeed is used instead
97 * of having more and more calls in perf_evsel__delete(). -- acme
99 * For now, add some more:
101 * Not to drag the BPF bandwagon...
103 void bpf_counter__destroy(struct evsel *evsel);
104 int bpf_counter__install_pe(struct evsel *evsel, int cpu, int fd);
105 int bpf_counter__disable(struct evsel *evsel);
107 void bpf_counter__destroy(struct evsel *evsel __maybe_unused)
111 int bpf_counter__install_pe(struct evsel *evsel __maybe_unused, int cpu __maybe_unused, int fd __maybe_unused)
116 int bpf_counter__disable(struct evsel *evsel __maybe_unused)
122 * Support debug printing even though util/debug.c is not linked. That means
123 * implementing 'verbose' and 'eprintf'.
128 int eprintf(int level, int var, const char *fmt, ...);
130 int eprintf(int level, int var, const char *fmt, ...)
137 ret = vfprintf(stderr, fmt, args);
144 /* Define PyVarObject_HEAD_INIT for python 2.5 */
145 #ifndef PyVarObject_HEAD_INIT
146 # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
149 #if PY_MAJOR_VERSION < 3
150 PyMODINIT_FUNC initperf(void);
152 PyMODINIT_FUNC PyInit_perf(void);
155 #define member_def(type, member, ptype, help) \
157 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
160 #define sample_member_def(name, member, ptype, help) \
162 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
168 struct perf_sample sample;
169 union perf_event event;
172 #define sample_members \
173 sample_member_def(sample_ip, ip, T_ULONGLONG, "event type"), \
174 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
175 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
176 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
177 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
178 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
179 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
180 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
181 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
183 static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
185 static PyMemberDef pyrf_mmap_event__members[] = {
187 member_def(perf_event_header, type, T_UINT, "event type"),
188 member_def(perf_event_header, misc, T_UINT, "event misc"),
189 member_def(perf_record_mmap, pid, T_UINT, "event pid"),
190 member_def(perf_record_mmap, tid, T_UINT, "event tid"),
191 member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
192 member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
193 member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
194 member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
198 static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
203 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
204 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
206 pevent->event.mmap.pid, pevent->event.mmap.tid,
207 pevent->event.mmap.start, pevent->event.mmap.len,
208 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
209 ret = PyErr_NoMemory();
211 ret = _PyUnicode_FromString(s);
217 static PyTypeObject pyrf_mmap_event__type = {
218 PyVarObject_HEAD_INIT(NULL, 0)
219 .tp_name = "perf.mmap_event",
220 .tp_basicsize = sizeof(struct pyrf_event),
221 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
222 .tp_doc = pyrf_mmap_event__doc,
223 .tp_members = pyrf_mmap_event__members,
224 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
227 static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
229 static PyMemberDef pyrf_task_event__members[] = {
231 member_def(perf_event_header, type, T_UINT, "event type"),
232 member_def(perf_record_fork, pid, T_UINT, "event pid"),
233 member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
234 member_def(perf_record_fork, tid, T_UINT, "event tid"),
235 member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
236 member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
240 static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
242 return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
243 "ptid: %u, time: %" PRI_lu64 "}",
244 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
245 pevent->event.fork.pid,
246 pevent->event.fork.ppid,
247 pevent->event.fork.tid,
248 pevent->event.fork.ptid,
249 pevent->event.fork.time);
252 static PyTypeObject pyrf_task_event__type = {
253 PyVarObject_HEAD_INIT(NULL, 0)
254 .tp_name = "perf.task_event",
255 .tp_basicsize = sizeof(struct pyrf_event),
256 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
257 .tp_doc = pyrf_task_event__doc,
258 .tp_members = pyrf_task_event__members,
259 .tp_repr = (reprfunc)pyrf_task_event__repr,
262 static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
264 static PyMemberDef pyrf_comm_event__members[] = {
266 member_def(perf_event_header, type, T_UINT, "event type"),
267 member_def(perf_record_comm, pid, T_UINT, "event pid"),
268 member_def(perf_record_comm, tid, T_UINT, "event tid"),
269 member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
273 static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
275 return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
276 pevent->event.comm.pid,
277 pevent->event.comm.tid,
278 pevent->event.comm.comm);
281 static PyTypeObject pyrf_comm_event__type = {
282 PyVarObject_HEAD_INIT(NULL, 0)
283 .tp_name = "perf.comm_event",
284 .tp_basicsize = sizeof(struct pyrf_event),
285 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
286 .tp_doc = pyrf_comm_event__doc,
287 .tp_members = pyrf_comm_event__members,
288 .tp_repr = (reprfunc)pyrf_comm_event__repr,
291 static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
293 static PyMemberDef pyrf_throttle_event__members[] = {
295 member_def(perf_event_header, type, T_UINT, "event type"),
296 member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
297 member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
298 member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
302 static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
304 struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1);
306 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
307 ", stream_id: %" PRI_lu64 " }",
308 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
309 te->time, te->id, te->stream_id);
312 static PyTypeObject pyrf_throttle_event__type = {
313 PyVarObject_HEAD_INIT(NULL, 0)
314 .tp_name = "perf.throttle_event",
315 .tp_basicsize = sizeof(struct pyrf_event),
316 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
317 .tp_doc = pyrf_throttle_event__doc,
318 .tp_members = pyrf_throttle_event__members,
319 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
322 static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
324 static PyMemberDef pyrf_lost_event__members[] = {
326 member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
327 member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
331 static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
336 if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
337 "lost: %#" PRI_lx64 " }",
338 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
339 ret = PyErr_NoMemory();
341 ret = _PyUnicode_FromString(s);
347 static PyTypeObject pyrf_lost_event__type = {
348 PyVarObject_HEAD_INIT(NULL, 0)
349 .tp_name = "perf.lost_event",
350 .tp_basicsize = sizeof(struct pyrf_event),
351 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
352 .tp_doc = pyrf_lost_event__doc,
353 .tp_members = pyrf_lost_event__members,
354 .tp_repr = (reprfunc)pyrf_lost_event__repr,
357 static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
359 static PyMemberDef pyrf_read_event__members[] = {
361 member_def(perf_record_read, pid, T_UINT, "event pid"),
362 member_def(perf_record_read, tid, T_UINT, "event tid"),
366 static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
368 return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
369 pevent->event.read.pid,
370 pevent->event.read.tid);
372 * FIXME: return the array of read values,
373 * making this method useful ;-)
377 static PyTypeObject pyrf_read_event__type = {
378 PyVarObject_HEAD_INIT(NULL, 0)
379 .tp_name = "perf.read_event",
380 .tp_basicsize = sizeof(struct pyrf_event),
381 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
382 .tp_doc = pyrf_read_event__doc,
383 .tp_members = pyrf_read_event__members,
384 .tp_repr = (reprfunc)pyrf_read_event__repr,
387 static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
389 static PyMemberDef pyrf_sample_event__members[] = {
391 member_def(perf_event_header, type, T_UINT, "event type"),
395 static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
400 if (asprintf(&s, "{ type: sample }") < 0) {
401 ret = PyErr_NoMemory();
403 ret = _PyUnicode_FromString(s);
409 static bool is_tracepoint(struct pyrf_event *pevent)
411 return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
415 tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
417 struct tep_handle *pevent = field->event->tep;
418 void *data = pe->sample.raw_data;
419 PyObject *ret = NULL;
420 unsigned long long val;
421 unsigned int offset, len;
423 if (field->flags & TEP_FIELD_IS_ARRAY) {
424 offset = field->offset;
426 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
427 val = tep_read_number(pevent, data + offset, len);
431 if (field->flags & TEP_FIELD_IS_RELATIVE)
432 offset += field->offset + field->size;
434 if (field->flags & TEP_FIELD_IS_STRING &&
435 is_printable_array(data + offset, len)) {
436 ret = _PyUnicode_FromString((char *)data + offset);
438 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
439 field->flags &= ~TEP_FIELD_IS_STRING;
442 val = tep_read_number(pevent, data + field->offset,
444 if (field->flags & TEP_FIELD_IS_POINTER)
445 ret = PyLong_FromUnsignedLong((unsigned long) val);
446 else if (field->flags & TEP_FIELD_IS_SIGNED)
447 ret = PyLong_FromLong((long) val);
449 ret = PyLong_FromUnsignedLong((unsigned long) val);
456 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
458 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
459 struct evsel *evsel = pevent->evsel;
460 struct tep_format_field *field;
462 if (!evsel->tp_format) {
463 struct tep_event *tp_format;
465 tp_format = trace_event__tp_format_id(evsel->core.attr.config);
466 if (IS_ERR_OR_NULL(tp_format))
469 evsel->tp_format = tp_format;
472 field = tep_find_any_field(evsel->tp_format, str);
476 return tracepoint_field(pevent, field);
480 pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
482 PyObject *obj = NULL;
484 if (is_tracepoint(pevent))
485 obj = get_tracepoint_field(pevent, attr_name);
487 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
490 static PyTypeObject pyrf_sample_event__type = {
491 PyVarObject_HEAD_INIT(NULL, 0)
492 .tp_name = "perf.sample_event",
493 .tp_basicsize = sizeof(struct pyrf_event),
494 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
495 .tp_doc = pyrf_sample_event__doc,
496 .tp_members = pyrf_sample_event__members,
497 .tp_repr = (reprfunc)pyrf_sample_event__repr,
498 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
501 static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
503 static PyMemberDef pyrf_context_switch_event__members[] = {
505 member_def(perf_event_header, type, T_UINT, "event type"),
506 member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
507 member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
511 static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
516 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
517 pevent->event.context_switch.next_prev_pid,
518 pevent->event.context_switch.next_prev_tid,
519 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
520 ret = PyErr_NoMemory();
522 ret = _PyUnicode_FromString(s);
528 static PyTypeObject pyrf_context_switch_event__type = {
529 PyVarObject_HEAD_INIT(NULL, 0)
530 .tp_name = "perf.context_switch_event",
531 .tp_basicsize = sizeof(struct pyrf_event),
532 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
533 .tp_doc = pyrf_context_switch_event__doc,
534 .tp_members = pyrf_context_switch_event__members,
535 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
538 static int pyrf_event__setup_types(void)
541 pyrf_mmap_event__type.tp_new =
542 pyrf_task_event__type.tp_new =
543 pyrf_comm_event__type.tp_new =
544 pyrf_lost_event__type.tp_new =
545 pyrf_read_event__type.tp_new =
546 pyrf_sample_event__type.tp_new =
547 pyrf_context_switch_event__type.tp_new =
548 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
549 err = PyType_Ready(&pyrf_mmap_event__type);
552 err = PyType_Ready(&pyrf_lost_event__type);
555 err = PyType_Ready(&pyrf_task_event__type);
558 err = PyType_Ready(&pyrf_comm_event__type);
561 err = PyType_Ready(&pyrf_throttle_event__type);
564 err = PyType_Ready(&pyrf_read_event__type);
567 err = PyType_Ready(&pyrf_sample_event__type);
570 err = PyType_Ready(&pyrf_context_switch_event__type);
577 static PyTypeObject *pyrf_event__type[] = {
578 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
579 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
580 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
581 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
582 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
583 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
584 [PERF_RECORD_FORK] = &pyrf_task_event__type,
585 [PERF_RECORD_READ] = &pyrf_read_event__type,
586 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
587 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
588 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
591 static PyObject *pyrf_event__new(union perf_event *event)
593 struct pyrf_event *pevent;
596 if ((event->header.type < PERF_RECORD_MMAP ||
597 event->header.type > PERF_RECORD_SAMPLE) &&
598 !(event->header.type == PERF_RECORD_SWITCH ||
599 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
602 ptype = pyrf_event__type[event->header.type];
603 pevent = PyObject_New(struct pyrf_event, ptype);
605 memcpy(&pevent->event, event, event->header.size);
606 return (PyObject *)pevent;
609 struct pyrf_cpu_map {
612 struct perf_cpu_map *cpus;
615 static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
616 PyObject *args, PyObject *kwargs)
618 static char *kwlist[] = { "cpustr", NULL };
621 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
625 pcpus->cpus = perf_cpu_map__new(cpustr);
626 if (pcpus->cpus == NULL)
631 static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
633 perf_cpu_map__put(pcpus->cpus);
634 Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
637 static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
639 struct pyrf_cpu_map *pcpus = (void *)obj;
641 return perf_cpu_map__nr(pcpus->cpus);
644 static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
646 struct pyrf_cpu_map *pcpus = (void *)obj;
648 if (i >= perf_cpu_map__nr(pcpus->cpus))
651 return Py_BuildValue("i", perf_cpu_map__cpu(pcpus->cpus, i).cpu);
654 static PySequenceMethods pyrf_cpu_map__sequence_methods = {
655 .sq_length = pyrf_cpu_map__length,
656 .sq_item = pyrf_cpu_map__item,
659 static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
661 static PyTypeObject pyrf_cpu_map__type = {
662 PyVarObject_HEAD_INIT(NULL, 0)
663 .tp_name = "perf.cpu_map",
664 .tp_basicsize = sizeof(struct pyrf_cpu_map),
665 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
666 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
667 .tp_doc = pyrf_cpu_map__doc,
668 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
669 .tp_init = (initproc)pyrf_cpu_map__init,
672 static int pyrf_cpu_map__setup_types(void)
674 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
675 return PyType_Ready(&pyrf_cpu_map__type);
678 struct pyrf_thread_map {
681 struct perf_thread_map *threads;
684 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
685 PyObject *args, PyObject *kwargs)
687 static char *kwlist[] = { "pid", "tid", "uid", NULL };
688 int pid = -1, tid = -1, uid = UINT_MAX;
690 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
691 kwlist, &pid, &tid, &uid))
694 pthreads->threads = thread_map__new(pid, tid, uid);
695 if (pthreads->threads == NULL)
700 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
702 perf_thread_map__put(pthreads->threads);
703 Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
706 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
708 struct pyrf_thread_map *pthreads = (void *)obj;
710 return pthreads->threads->nr;
713 static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
715 struct pyrf_thread_map *pthreads = (void *)obj;
717 if (i >= pthreads->threads->nr)
720 return Py_BuildValue("i", pthreads->threads->map[i]);
723 static PySequenceMethods pyrf_thread_map__sequence_methods = {
724 .sq_length = pyrf_thread_map__length,
725 .sq_item = pyrf_thread_map__item,
728 static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
730 static PyTypeObject pyrf_thread_map__type = {
731 PyVarObject_HEAD_INIT(NULL, 0)
732 .tp_name = "perf.thread_map",
733 .tp_basicsize = sizeof(struct pyrf_thread_map),
734 .tp_dealloc = (destructor)pyrf_thread_map__delete,
735 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
736 .tp_doc = pyrf_thread_map__doc,
737 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
738 .tp_init = (initproc)pyrf_thread_map__init,
741 static int pyrf_thread_map__setup_types(void)
743 pyrf_thread_map__type.tp_new = PyType_GenericNew;
744 return PyType_Ready(&pyrf_thread_map__type);
753 static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
754 PyObject *args, PyObject *kwargs)
756 struct perf_event_attr attr = {
757 .type = PERF_TYPE_HARDWARE,
758 .config = PERF_COUNT_HW_CPU_CYCLES,
759 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
761 static char *kwlist[] = {
793 u64 sample_period = 0;
815 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
816 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
817 &attr.type, &attr.config, &attr.sample_freq,
818 &sample_period, &attr.sample_type,
819 &attr.read_format, &disabled, &inherit,
820 &pinned, &exclusive, &exclude_user,
821 &exclude_kernel, &exclude_hv, &exclude_idle,
822 &mmap, &context_switch, &comm, &freq, &inherit_stat,
823 &enable_on_exec, &task, &watermark,
824 &precise_ip, &mmap_data, &sample_id_all,
825 &attr.wakeup_events, &attr.bp_type,
826 &attr.bp_addr, &attr.bp_len, &idx))
830 if (sample_period != 0) {
831 if (attr.sample_freq != 0)
832 return -1; /* FIXME: throw right exception */
833 attr.sample_period = sample_period;
837 attr.disabled = disabled;
838 attr.inherit = inherit;
839 attr.pinned = pinned;
840 attr.exclusive = exclusive;
841 attr.exclude_user = exclude_user;
842 attr.exclude_kernel = exclude_kernel;
843 attr.exclude_hv = exclude_hv;
844 attr.exclude_idle = exclude_idle;
846 attr.context_switch = context_switch;
849 attr.inherit_stat = inherit_stat;
850 attr.enable_on_exec = enable_on_exec;
852 attr.watermark = watermark;
853 attr.precise_ip = precise_ip;
854 attr.mmap_data = mmap_data;
855 attr.sample_id_all = sample_id_all;
856 attr.size = sizeof(attr);
858 evsel__init(&pevsel->evsel, &attr, idx);
862 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
864 evsel__exit(&pevsel->evsel);
865 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
868 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
869 PyObject *args, PyObject *kwargs)
871 struct evsel *evsel = &pevsel->evsel;
872 struct perf_cpu_map *cpus = NULL;
873 struct perf_thread_map *threads = NULL;
874 PyObject *pcpus = NULL, *pthreads = NULL;
875 int group = 0, inherit = 0;
876 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
878 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
879 &pcpus, &pthreads, &group, &inherit))
882 if (pthreads != NULL)
883 threads = ((struct pyrf_thread_map *)pthreads)->threads;
886 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
888 evsel->core.attr.inherit = inherit;
890 * This will group just the fds for this single evsel, to group
891 * multiple events, use evlist.open().
893 if (evsel__open(evsel, cpus, threads) < 0) {
894 PyErr_SetFromErrno(PyExc_OSError);
902 static PyMethodDef pyrf_evsel__methods[] = {
905 .ml_meth = (PyCFunction)pyrf_evsel__open,
906 .ml_flags = METH_VARARGS | METH_KEYWORDS,
907 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
912 static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
914 static PyTypeObject pyrf_evsel__type = {
915 PyVarObject_HEAD_INIT(NULL, 0)
916 .tp_name = "perf.evsel",
917 .tp_basicsize = sizeof(struct pyrf_evsel),
918 .tp_dealloc = (destructor)pyrf_evsel__delete,
919 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
920 .tp_doc = pyrf_evsel__doc,
921 .tp_methods = pyrf_evsel__methods,
922 .tp_init = (initproc)pyrf_evsel__init,
925 static int pyrf_evsel__setup_types(void)
927 pyrf_evsel__type.tp_new = PyType_GenericNew;
928 return PyType_Ready(&pyrf_evsel__type);
934 struct evlist evlist;
937 static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
938 PyObject *args, PyObject *kwargs __maybe_unused)
940 PyObject *pcpus = NULL, *pthreads = NULL;
941 struct perf_cpu_map *cpus;
942 struct perf_thread_map *threads;
944 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
947 threads = ((struct pyrf_thread_map *)pthreads)->threads;
948 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
949 evlist__init(&pevlist->evlist, cpus, threads);
953 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
955 evlist__exit(&pevlist->evlist);
956 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
959 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
960 PyObject *args, PyObject *kwargs)
962 struct evlist *evlist = &pevlist->evlist;
963 static char *kwlist[] = { "pages", "overwrite", NULL };
964 int pages = 128, overwrite = false;
966 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
970 if (evlist__mmap(evlist, pages) < 0) {
971 PyErr_SetFromErrno(PyExc_OSError);
979 static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
980 PyObject *args, PyObject *kwargs)
982 struct evlist *evlist = &pevlist->evlist;
983 static char *kwlist[] = { "timeout", NULL };
986 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
989 n = evlist__poll(evlist, timeout);
991 PyErr_SetFromErrno(PyExc_OSError);
995 return Py_BuildValue("i", n);
998 static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
999 PyObject *args __maybe_unused,
1000 PyObject *kwargs __maybe_unused)
1002 struct evlist *evlist = &pevlist->evlist;
1003 PyObject *list = PyList_New(0);
1006 for (i = 0; i < evlist->core.pollfd.nr; ++i) {
1008 #if PY_MAJOR_VERSION < 3
1009 FILE *fp = fdopen(evlist->core.pollfd.entries[i].fd, "r");
1014 file = PyFile_FromFile(fp, "perf", "r", NULL);
1016 file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1,
1017 NULL, NULL, NULL, 0);
1022 if (PyList_Append(list, file) != 0) {
1032 return PyErr_NoMemory();
1036 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
1038 PyObject *kwargs __maybe_unused)
1040 struct evlist *evlist = &pevlist->evlist;
1042 struct evsel *evsel;
1044 if (!PyArg_ParseTuple(args, "O", &pevsel))
1048 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
1049 evsel->core.idx = evlist->core.nr_entries;
1050 evlist__add(evlist, evsel);
1052 return Py_BuildValue("i", evlist->core.nr_entries);
1055 static struct mmap *get_md(struct evlist *evlist, int cpu)
1059 for (i = 0; i < evlist->core.nr_mmaps; i++) {
1060 struct mmap *md = &evlist->mmap[i];
1062 if (md->core.cpu.cpu == cpu)
1069 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
1070 PyObject *args, PyObject *kwargs)
1072 struct evlist *evlist = &pevlist->evlist;
1073 union perf_event *event;
1074 int sample_id_all = 1, cpu;
1075 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
1079 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
1080 &cpu, &sample_id_all))
1083 md = get_md(evlist, cpu);
1087 if (perf_mmap__read_init(&md->core) < 0)
1090 event = perf_mmap__read_event(&md->core);
1091 if (event != NULL) {
1092 PyObject *pyevent = pyrf_event__new(event);
1093 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
1094 struct evsel *evsel;
1096 if (pyevent == NULL)
1097 return PyErr_NoMemory();
1099 evsel = evlist__event2evsel(evlist, event);
1105 pevent->evsel = evsel;
1107 err = evsel__parse_sample(evsel, event, &pevent->sample);
1109 /* Consume the even only after we parsed it out. */
1110 perf_mmap__consume(&md->core);
1113 return PyErr_Format(PyExc_OSError,
1114 "perf: can't parse sample, err=%d", err);
1122 static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1123 PyObject *args, PyObject *kwargs)
1125 struct evlist *evlist = &pevlist->evlist;
1127 static char *kwlist[] = { "group", NULL };
1129 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist, &group))
1133 evlist__set_leader(evlist);
1135 if (evlist__open(evlist) < 0) {
1136 PyErr_SetFromErrno(PyExc_OSError);
1144 static PyMethodDef pyrf_evlist__methods[] = {
1147 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1148 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1149 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1153 .ml_meth = (PyCFunction)pyrf_evlist__open,
1154 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1155 .ml_doc = PyDoc_STR("open the file descriptors.")
1159 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1160 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1161 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1164 .ml_name = "get_pollfd",
1165 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1166 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1167 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1171 .ml_meth = (PyCFunction)pyrf_evlist__add,
1172 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1173 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1176 .ml_name = "read_on_cpu",
1177 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1178 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1179 .ml_doc = PyDoc_STR("reads an event.")
1181 { .ml_name = NULL, }
1184 static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1186 struct pyrf_evlist *pevlist = (void *)obj;
1188 return pevlist->evlist.core.nr_entries;
1191 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1193 struct pyrf_evlist *pevlist = (void *)obj;
1196 if (i >= pevlist->evlist.core.nr_entries)
1199 evlist__for_each_entry(&pevlist->evlist, pos) {
1204 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1207 static PySequenceMethods pyrf_evlist__sequence_methods = {
1208 .sq_length = pyrf_evlist__length,
1209 .sq_item = pyrf_evlist__item,
1212 static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1214 static PyTypeObject pyrf_evlist__type = {
1215 PyVarObject_HEAD_INIT(NULL, 0)
1216 .tp_name = "perf.evlist",
1217 .tp_basicsize = sizeof(struct pyrf_evlist),
1218 .tp_dealloc = (destructor)pyrf_evlist__delete,
1219 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1220 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1221 .tp_doc = pyrf_evlist__doc,
1222 .tp_methods = pyrf_evlist__methods,
1223 .tp_init = (initproc)pyrf_evlist__init,
1226 static int pyrf_evlist__setup_types(void)
1228 pyrf_evlist__type.tp_new = PyType_GenericNew;
1229 return PyType_Ready(&pyrf_evlist__type);
1232 #define PERF_CONST(name) { #name, PERF_##name }
1237 } perf__constants[] = {
1238 PERF_CONST(TYPE_HARDWARE),
1239 PERF_CONST(TYPE_SOFTWARE),
1240 PERF_CONST(TYPE_TRACEPOINT),
1241 PERF_CONST(TYPE_HW_CACHE),
1242 PERF_CONST(TYPE_RAW),
1243 PERF_CONST(TYPE_BREAKPOINT),
1245 PERF_CONST(COUNT_HW_CPU_CYCLES),
1246 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1247 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1248 PERF_CONST(COUNT_HW_CACHE_MISSES),
1249 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1250 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1251 PERF_CONST(COUNT_HW_BUS_CYCLES),
1252 PERF_CONST(COUNT_HW_CACHE_L1D),
1253 PERF_CONST(COUNT_HW_CACHE_L1I),
1254 PERF_CONST(COUNT_HW_CACHE_LL),
1255 PERF_CONST(COUNT_HW_CACHE_DTLB),
1256 PERF_CONST(COUNT_HW_CACHE_ITLB),
1257 PERF_CONST(COUNT_HW_CACHE_BPU),
1258 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1259 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1260 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1261 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1262 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1264 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1265 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1267 PERF_CONST(COUNT_SW_CPU_CLOCK),
1268 PERF_CONST(COUNT_SW_TASK_CLOCK),
1269 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1270 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1271 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1272 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1273 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1274 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1275 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1276 PERF_CONST(COUNT_SW_DUMMY),
1278 PERF_CONST(SAMPLE_IP),
1279 PERF_CONST(SAMPLE_TID),
1280 PERF_CONST(SAMPLE_TIME),
1281 PERF_CONST(SAMPLE_ADDR),
1282 PERF_CONST(SAMPLE_READ),
1283 PERF_CONST(SAMPLE_CALLCHAIN),
1284 PERF_CONST(SAMPLE_ID),
1285 PERF_CONST(SAMPLE_CPU),
1286 PERF_CONST(SAMPLE_PERIOD),
1287 PERF_CONST(SAMPLE_STREAM_ID),
1288 PERF_CONST(SAMPLE_RAW),
1290 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1291 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1292 PERF_CONST(FORMAT_ID),
1293 PERF_CONST(FORMAT_GROUP),
1295 PERF_CONST(RECORD_MMAP),
1296 PERF_CONST(RECORD_LOST),
1297 PERF_CONST(RECORD_COMM),
1298 PERF_CONST(RECORD_EXIT),
1299 PERF_CONST(RECORD_THROTTLE),
1300 PERF_CONST(RECORD_UNTHROTTLE),
1301 PERF_CONST(RECORD_FORK),
1302 PERF_CONST(RECORD_READ),
1303 PERF_CONST(RECORD_SAMPLE),
1304 PERF_CONST(RECORD_MMAP2),
1305 PERF_CONST(RECORD_AUX),
1306 PERF_CONST(RECORD_ITRACE_START),
1307 PERF_CONST(RECORD_LOST_SAMPLES),
1308 PERF_CONST(RECORD_SWITCH),
1309 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1311 PERF_CONST(RECORD_MISC_SWITCH_OUT),
1315 static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1316 PyObject *args, PyObject *kwargs)
1318 struct tep_event *tp_format;
1319 static char *kwlist[] = { "sys", "name", NULL };
1323 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1327 tp_format = trace_event__tp_format(sys, name);
1328 if (IS_ERR(tp_format))
1329 return _PyLong_FromLong(-1);
1331 return _PyLong_FromLong(tp_format->id);
1334 static PyMethodDef perf__methods[] = {
1336 .ml_name = "tracepoint",
1337 .ml_meth = (PyCFunction) pyrf__tracepoint,
1338 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1339 .ml_doc = PyDoc_STR("Get tracepoint config.")
1341 { .ml_name = NULL, }
1344 #if PY_MAJOR_VERSION < 3
1345 PyMODINIT_FUNC initperf(void)
1347 PyMODINIT_FUNC PyInit_perf(void)
1353 #if PY_MAJOR_VERSION < 3
1354 PyObject *module = Py_InitModule("perf", perf__methods);
1356 static struct PyModuleDef moduledef = {
1357 PyModuleDef_HEAD_INIT,
1358 "perf", /* m_name */
1361 perf__methods, /* m_methods */
1362 NULL, /* m_reload */
1363 NULL, /* m_traverse */
1367 PyObject *module = PyModule_Create(&moduledef);
1370 if (module == NULL ||
1371 pyrf_event__setup_types() < 0 ||
1372 pyrf_evlist__setup_types() < 0 ||
1373 pyrf_evsel__setup_types() < 0 ||
1374 pyrf_thread_map__setup_types() < 0 ||
1375 pyrf_cpu_map__setup_types() < 0)
1376 #if PY_MAJOR_VERSION < 3
1382 /* The page_size is placed in util object. */
1383 page_size = sysconf(_SC_PAGE_SIZE);
1385 Py_INCREF(&pyrf_evlist__type);
1386 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1388 Py_INCREF(&pyrf_evsel__type);
1389 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1391 Py_INCREF(&pyrf_mmap_event__type);
1392 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1394 Py_INCREF(&pyrf_lost_event__type);
1395 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1397 Py_INCREF(&pyrf_comm_event__type);
1398 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1400 Py_INCREF(&pyrf_task_event__type);
1401 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1403 Py_INCREF(&pyrf_throttle_event__type);
1404 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1406 Py_INCREF(&pyrf_task_event__type);
1407 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1409 Py_INCREF(&pyrf_read_event__type);
1410 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1412 Py_INCREF(&pyrf_sample_event__type);
1413 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1415 Py_INCREF(&pyrf_context_switch_event__type);
1416 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1418 Py_INCREF(&pyrf_thread_map__type);
1419 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1421 Py_INCREF(&pyrf_cpu_map__type);
1422 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1424 dict = PyModule_GetDict(module);
1428 for (i = 0; perf__constants[i].name != NULL; i++) {
1429 obj = _PyLong_FromLong(perf__constants[i].value);
1432 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1437 if (PyErr_Occurred())
1438 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1439 #if PY_MAJOR_VERSION >= 3
1445 * Dummy, to avoid dragging all the test_attr infrastructure in the python
1448 void test_attr__open(struct perf_event_attr *attr, pid_t pid, struct perf_cpu cpu,
1449 int fd, int group_fd, unsigned long flags)