1 // SPDX-License-Identifier: GPL-2.0
2 #include <Python.h>
3 #include <structmember.h>
4 #include <inttypes.h>
5 #include <poll.h>
6 #include <linux/err.h>
7 #include <perf/cpumap.h>
8 #ifdef HAVE_LIBTRACEEVENT
9 #include <traceevent/event-parse.h>
10 #endif
11 #include <perf/mmap.h>
12 #include "evlist.h"
13 #include "evsel.h"
14 #include "event.h"
15 #include "print_binary.h"
16 #include "thread_map.h"
17 #include "trace-event.h"
18 #include "mmap.h"
19 #include "util/bpf-filter.h"
20 #include "util/env.h"
21 #include "util/kvm-stat.h"
22 #include "util/stat.h"
23 #include "util/kwork.h"
24 #include "util/sample.h"
25 #include "util/lock-contention.h"
26 #include <internal/lib.h>
27 #include "../builtin.h"
28
29 #if PY_MAJOR_VERSION < 3
30 #define _PyUnicode_FromString(arg) \
31 PyString_FromString(arg)
32 #define _PyUnicode_AsString(arg) \
33 PyString_AsString(arg)
34 #define _PyUnicode_FromFormat(...) \
35 PyString_FromFormat(__VA_ARGS__)
36 #define _PyLong_FromLong(arg) \
37 PyInt_FromLong(arg)
38
39 #else
40
41 #define _PyUnicode_FromString(arg) \
42 PyUnicode_FromString(arg)
43 #define _PyUnicode_FromFormat(...) \
44 PyUnicode_FromFormat(__VA_ARGS__)
45 #define _PyLong_FromLong(arg) \
46 PyLong_FromLong(arg)
47 #endif
48
49 #ifndef Py_TYPE
50 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
51 #endif
52
53 /* Define PyVarObject_HEAD_INIT for python 2.5 */
54 #ifndef PyVarObject_HEAD_INIT
55 # define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
56 #endif
57
58 #if PY_MAJOR_VERSION < 3
59 PyMODINIT_FUNC initperf(void);
60 #else
61 PyMODINIT_FUNC PyInit_perf(void);
62 #endif
63
64 #define member_def(type, member, ptype, help) \
65 { #member, ptype, \
66 offsetof(struct pyrf_event, event) + offsetof(struct type, member), \
67 0, help }
68
69 #define sample_member_def(name, member, ptype, help) \
70 { #name, ptype, \
71 offsetof(struct pyrf_event, sample) + offsetof(struct perf_sample, member), \
72 0, help }
73
74 struct pyrf_event {
75 PyObject_HEAD
76 struct evsel *evsel;
77 struct perf_sample sample;
78 union perf_event event;
79 };
80
81 #define sample_members \
82 sample_member_def(sample_ip, ip, T_ULONGLONG, "event ip"), \
83 sample_member_def(sample_pid, pid, T_INT, "event pid"), \
84 sample_member_def(sample_tid, tid, T_INT, "event tid"), \
85 sample_member_def(sample_time, time, T_ULONGLONG, "event timestamp"), \
86 sample_member_def(sample_addr, addr, T_ULONGLONG, "event addr"), \
87 sample_member_def(sample_id, id, T_ULONGLONG, "event id"), \
88 sample_member_def(sample_stream_id, stream_id, T_ULONGLONG, "event stream id"), \
89 sample_member_def(sample_period, period, T_ULONGLONG, "event period"), \
90 sample_member_def(sample_cpu, cpu, T_UINT, "event cpu"),
91
92 static char pyrf_mmap_event__doc[] = PyDoc_STR("perf mmap event object.");
93
94 static PyMemberDef pyrf_mmap_event__members[] = {
95 sample_members
96 member_def(perf_event_header, type, T_UINT, "event type"),
97 member_def(perf_event_header, misc, T_UINT, "event misc"),
98 member_def(perf_record_mmap, pid, T_UINT, "event pid"),
99 member_def(perf_record_mmap, tid, T_UINT, "event tid"),
100 member_def(perf_record_mmap, start, T_ULONGLONG, "start of the map"),
101 member_def(perf_record_mmap, len, T_ULONGLONG, "map length"),
102 member_def(perf_record_mmap, pgoff, T_ULONGLONG, "page offset"),
103 member_def(perf_record_mmap, filename, T_STRING_INPLACE, "backing store"),
104 { .name = NULL, },
105 };
106
pyrf_mmap_event__repr(struct pyrf_event * pevent)107 static PyObject *pyrf_mmap_event__repr(struct pyrf_event *pevent)
108 {
109 PyObject *ret;
110 char *s;
111
112 if (asprintf(&s, "{ type: mmap, pid: %u, tid: %u, start: %#" PRI_lx64 ", "
113 "length: %#" PRI_lx64 ", offset: %#" PRI_lx64 ", "
114 "filename: %s }",
115 pevent->event.mmap.pid, pevent->event.mmap.tid,
116 pevent->event.mmap.start, pevent->event.mmap.len,
117 pevent->event.mmap.pgoff, pevent->event.mmap.filename) < 0) {
118 ret = PyErr_NoMemory();
119 } else {
120 ret = _PyUnicode_FromString(s);
121 free(s);
122 }
123 return ret;
124 }
125
126 static PyTypeObject pyrf_mmap_event__type = {
127 PyVarObject_HEAD_INIT(NULL, 0)
128 .tp_name = "perf.mmap_event",
129 .tp_basicsize = sizeof(struct pyrf_event),
130 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
131 .tp_doc = pyrf_mmap_event__doc,
132 .tp_members = pyrf_mmap_event__members,
133 .tp_repr = (reprfunc)pyrf_mmap_event__repr,
134 };
135
136 static char pyrf_task_event__doc[] = PyDoc_STR("perf task (fork/exit) event object.");
137
138 static PyMemberDef pyrf_task_event__members[] = {
139 sample_members
140 member_def(perf_event_header, type, T_UINT, "event type"),
141 member_def(perf_record_fork, pid, T_UINT, "event pid"),
142 member_def(perf_record_fork, ppid, T_UINT, "event ppid"),
143 member_def(perf_record_fork, tid, T_UINT, "event tid"),
144 member_def(perf_record_fork, ptid, T_UINT, "event ptid"),
145 member_def(perf_record_fork, time, T_ULONGLONG, "timestamp"),
146 { .name = NULL, },
147 };
148
pyrf_task_event__repr(struct pyrf_event * pevent)149 static PyObject *pyrf_task_event__repr(struct pyrf_event *pevent)
150 {
151 return _PyUnicode_FromFormat("{ type: %s, pid: %u, ppid: %u, tid: %u, "
152 "ptid: %u, time: %" PRI_lu64 "}",
153 pevent->event.header.type == PERF_RECORD_FORK ? "fork" : "exit",
154 pevent->event.fork.pid,
155 pevent->event.fork.ppid,
156 pevent->event.fork.tid,
157 pevent->event.fork.ptid,
158 pevent->event.fork.time);
159 }
160
161 static PyTypeObject pyrf_task_event__type = {
162 PyVarObject_HEAD_INIT(NULL, 0)
163 .tp_name = "perf.task_event",
164 .tp_basicsize = sizeof(struct pyrf_event),
165 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
166 .tp_doc = pyrf_task_event__doc,
167 .tp_members = pyrf_task_event__members,
168 .tp_repr = (reprfunc)pyrf_task_event__repr,
169 };
170
171 static char pyrf_comm_event__doc[] = PyDoc_STR("perf comm event object.");
172
173 static PyMemberDef pyrf_comm_event__members[] = {
174 sample_members
175 member_def(perf_event_header, type, T_UINT, "event type"),
176 member_def(perf_record_comm, pid, T_UINT, "event pid"),
177 member_def(perf_record_comm, tid, T_UINT, "event tid"),
178 member_def(perf_record_comm, comm, T_STRING_INPLACE, "process name"),
179 { .name = NULL, },
180 };
181
pyrf_comm_event__repr(struct pyrf_event * pevent)182 static PyObject *pyrf_comm_event__repr(struct pyrf_event *pevent)
183 {
184 return _PyUnicode_FromFormat("{ type: comm, pid: %u, tid: %u, comm: %s }",
185 pevent->event.comm.pid,
186 pevent->event.comm.tid,
187 pevent->event.comm.comm);
188 }
189
190 static PyTypeObject pyrf_comm_event__type = {
191 PyVarObject_HEAD_INIT(NULL, 0)
192 .tp_name = "perf.comm_event",
193 .tp_basicsize = sizeof(struct pyrf_event),
194 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
195 .tp_doc = pyrf_comm_event__doc,
196 .tp_members = pyrf_comm_event__members,
197 .tp_repr = (reprfunc)pyrf_comm_event__repr,
198 };
199
200 static char pyrf_throttle_event__doc[] = PyDoc_STR("perf throttle event object.");
201
202 static PyMemberDef pyrf_throttle_event__members[] = {
203 sample_members
204 member_def(perf_event_header, type, T_UINT, "event type"),
205 member_def(perf_record_throttle, time, T_ULONGLONG, "timestamp"),
206 member_def(perf_record_throttle, id, T_ULONGLONG, "event id"),
207 member_def(perf_record_throttle, stream_id, T_ULONGLONG, "event stream id"),
208 { .name = NULL, },
209 };
210
pyrf_throttle_event__repr(struct pyrf_event * pevent)211 static PyObject *pyrf_throttle_event__repr(struct pyrf_event *pevent)
212 {
213 struct perf_record_throttle *te = (struct perf_record_throttle *)(&pevent->event.header + 1);
214
215 return _PyUnicode_FromFormat("{ type: %sthrottle, time: %" PRI_lu64 ", id: %" PRI_lu64
216 ", stream_id: %" PRI_lu64 " }",
217 pevent->event.header.type == PERF_RECORD_THROTTLE ? "" : "un",
218 te->time, te->id, te->stream_id);
219 }
220
221 static PyTypeObject pyrf_throttle_event__type = {
222 PyVarObject_HEAD_INIT(NULL, 0)
223 .tp_name = "perf.throttle_event",
224 .tp_basicsize = sizeof(struct pyrf_event),
225 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
226 .tp_doc = pyrf_throttle_event__doc,
227 .tp_members = pyrf_throttle_event__members,
228 .tp_repr = (reprfunc)pyrf_throttle_event__repr,
229 };
230
231 static char pyrf_lost_event__doc[] = PyDoc_STR("perf lost event object.");
232
233 static PyMemberDef pyrf_lost_event__members[] = {
234 sample_members
235 member_def(perf_record_lost, id, T_ULONGLONG, "event id"),
236 member_def(perf_record_lost, lost, T_ULONGLONG, "number of lost events"),
237 { .name = NULL, },
238 };
239
pyrf_lost_event__repr(struct pyrf_event * pevent)240 static PyObject *pyrf_lost_event__repr(struct pyrf_event *pevent)
241 {
242 PyObject *ret;
243 char *s;
244
245 if (asprintf(&s, "{ type: lost, id: %#" PRI_lx64 ", "
246 "lost: %#" PRI_lx64 " }",
247 pevent->event.lost.id, pevent->event.lost.lost) < 0) {
248 ret = PyErr_NoMemory();
249 } else {
250 ret = _PyUnicode_FromString(s);
251 free(s);
252 }
253 return ret;
254 }
255
256 static PyTypeObject pyrf_lost_event__type = {
257 PyVarObject_HEAD_INIT(NULL, 0)
258 .tp_name = "perf.lost_event",
259 .tp_basicsize = sizeof(struct pyrf_event),
260 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
261 .tp_doc = pyrf_lost_event__doc,
262 .tp_members = pyrf_lost_event__members,
263 .tp_repr = (reprfunc)pyrf_lost_event__repr,
264 };
265
266 static char pyrf_read_event__doc[] = PyDoc_STR("perf read event object.");
267
268 static PyMemberDef pyrf_read_event__members[] = {
269 sample_members
270 member_def(perf_record_read, pid, T_UINT, "event pid"),
271 member_def(perf_record_read, tid, T_UINT, "event tid"),
272 { .name = NULL, },
273 };
274
pyrf_read_event__repr(struct pyrf_event * pevent)275 static PyObject *pyrf_read_event__repr(struct pyrf_event *pevent)
276 {
277 return _PyUnicode_FromFormat("{ type: read, pid: %u, tid: %u }",
278 pevent->event.read.pid,
279 pevent->event.read.tid);
280 /*
281 * FIXME: return the array of read values,
282 * making this method useful ;-)
283 */
284 }
285
286 static PyTypeObject pyrf_read_event__type = {
287 PyVarObject_HEAD_INIT(NULL, 0)
288 .tp_name = "perf.read_event",
289 .tp_basicsize = sizeof(struct pyrf_event),
290 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
291 .tp_doc = pyrf_read_event__doc,
292 .tp_members = pyrf_read_event__members,
293 .tp_repr = (reprfunc)pyrf_read_event__repr,
294 };
295
296 static char pyrf_sample_event__doc[] = PyDoc_STR("perf sample event object.");
297
298 static PyMemberDef pyrf_sample_event__members[] = {
299 sample_members
300 member_def(perf_event_header, type, T_UINT, "event type"),
301 { .name = NULL, },
302 };
303
pyrf_sample_event__repr(struct pyrf_event * pevent)304 static PyObject *pyrf_sample_event__repr(struct pyrf_event *pevent)
305 {
306 PyObject *ret;
307 char *s;
308
309 if (asprintf(&s, "{ type: sample }") < 0) {
310 ret = PyErr_NoMemory();
311 } else {
312 ret = _PyUnicode_FromString(s);
313 free(s);
314 }
315 return ret;
316 }
317
318 #ifdef HAVE_LIBTRACEEVENT
is_tracepoint(struct pyrf_event * pevent)319 static bool is_tracepoint(struct pyrf_event *pevent)
320 {
321 return pevent->evsel->core.attr.type == PERF_TYPE_TRACEPOINT;
322 }
323
324 static PyObject*
tracepoint_field(struct pyrf_event * pe,struct tep_format_field * field)325 tracepoint_field(struct pyrf_event *pe, struct tep_format_field *field)
326 {
327 struct tep_handle *pevent = field->event->tep;
328 void *data = pe->sample.raw_data;
329 PyObject *ret = NULL;
330 unsigned long long val;
331 unsigned int offset, len;
332
333 if (field->flags & TEP_FIELD_IS_ARRAY) {
334 offset = field->offset;
335 len = field->size;
336 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
337 val = tep_read_number(pevent, data + offset, len);
338 offset = val;
339 len = offset >> 16;
340 offset &= 0xffff;
341 if (tep_field_is_relative(field->flags))
342 offset += field->offset + field->size;
343 }
344 if (field->flags & TEP_FIELD_IS_STRING &&
345 is_printable_array(data + offset, len)) {
346 ret = _PyUnicode_FromString((char *)data + offset);
347 } else {
348 ret = PyByteArray_FromStringAndSize((const char *) data + offset, len);
349 field->flags &= ~TEP_FIELD_IS_STRING;
350 }
351 } else {
352 val = tep_read_number(pevent, data + field->offset,
353 field->size);
354 if (field->flags & TEP_FIELD_IS_POINTER)
355 ret = PyLong_FromUnsignedLong((unsigned long) val);
356 else if (field->flags & TEP_FIELD_IS_SIGNED)
357 ret = PyLong_FromLong((long) val);
358 else
359 ret = PyLong_FromUnsignedLong((unsigned long) val);
360 }
361
362 return ret;
363 }
364
365 static PyObject*
get_tracepoint_field(struct pyrf_event * pevent,PyObject * attr_name)366 get_tracepoint_field(struct pyrf_event *pevent, PyObject *attr_name)
367 {
368 const char *str = _PyUnicode_AsString(PyObject_Str(attr_name));
369 struct evsel *evsel = pevent->evsel;
370 struct tep_format_field *field;
371
372 if (!evsel->tp_format) {
373 struct tep_event *tp_format;
374
375 tp_format = trace_event__tp_format_id(evsel->core.attr.config);
376 if (IS_ERR_OR_NULL(tp_format))
377 return NULL;
378
379 evsel->tp_format = tp_format;
380 }
381
382 field = tep_find_any_field(evsel->tp_format, str);
383 if (!field)
384 return NULL;
385
386 return tracepoint_field(pevent, field);
387 }
388 #endif /* HAVE_LIBTRACEEVENT */
389
390 static PyObject*
pyrf_sample_event__getattro(struct pyrf_event * pevent,PyObject * attr_name)391 pyrf_sample_event__getattro(struct pyrf_event *pevent, PyObject *attr_name)
392 {
393 PyObject *obj = NULL;
394
395 #ifdef HAVE_LIBTRACEEVENT
396 if (is_tracepoint(pevent))
397 obj = get_tracepoint_field(pevent, attr_name);
398 #endif
399
400 return obj ?: PyObject_GenericGetAttr((PyObject *) pevent, attr_name);
401 }
402
403 static PyTypeObject pyrf_sample_event__type = {
404 PyVarObject_HEAD_INIT(NULL, 0)
405 .tp_name = "perf.sample_event",
406 .tp_basicsize = sizeof(struct pyrf_event),
407 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
408 .tp_doc = pyrf_sample_event__doc,
409 .tp_members = pyrf_sample_event__members,
410 .tp_repr = (reprfunc)pyrf_sample_event__repr,
411 .tp_getattro = (getattrofunc) pyrf_sample_event__getattro,
412 };
413
414 static char pyrf_context_switch_event__doc[] = PyDoc_STR("perf context_switch event object.");
415
416 static PyMemberDef pyrf_context_switch_event__members[] = {
417 sample_members
418 member_def(perf_event_header, type, T_UINT, "event type"),
419 member_def(perf_record_switch, next_prev_pid, T_UINT, "next/prev pid"),
420 member_def(perf_record_switch, next_prev_tid, T_UINT, "next/prev tid"),
421 { .name = NULL, },
422 };
423
pyrf_context_switch_event__repr(struct pyrf_event * pevent)424 static PyObject *pyrf_context_switch_event__repr(struct pyrf_event *pevent)
425 {
426 PyObject *ret;
427 char *s;
428
429 if (asprintf(&s, "{ type: context_switch, next_prev_pid: %u, next_prev_tid: %u, switch_out: %u }",
430 pevent->event.context_switch.next_prev_pid,
431 pevent->event.context_switch.next_prev_tid,
432 !!(pevent->event.header.misc & PERF_RECORD_MISC_SWITCH_OUT)) < 0) {
433 ret = PyErr_NoMemory();
434 } else {
435 ret = _PyUnicode_FromString(s);
436 free(s);
437 }
438 return ret;
439 }
440
441 static PyTypeObject pyrf_context_switch_event__type = {
442 PyVarObject_HEAD_INIT(NULL, 0)
443 .tp_name = "perf.context_switch_event",
444 .tp_basicsize = sizeof(struct pyrf_event),
445 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
446 .tp_doc = pyrf_context_switch_event__doc,
447 .tp_members = pyrf_context_switch_event__members,
448 .tp_repr = (reprfunc)pyrf_context_switch_event__repr,
449 };
450
pyrf_event__setup_types(void)451 static int pyrf_event__setup_types(void)
452 {
453 int err;
454 pyrf_mmap_event__type.tp_new =
455 pyrf_task_event__type.tp_new =
456 pyrf_comm_event__type.tp_new =
457 pyrf_lost_event__type.tp_new =
458 pyrf_read_event__type.tp_new =
459 pyrf_sample_event__type.tp_new =
460 pyrf_context_switch_event__type.tp_new =
461 pyrf_throttle_event__type.tp_new = PyType_GenericNew;
462 err = PyType_Ready(&pyrf_mmap_event__type);
463 if (err < 0)
464 goto out;
465 err = PyType_Ready(&pyrf_lost_event__type);
466 if (err < 0)
467 goto out;
468 err = PyType_Ready(&pyrf_task_event__type);
469 if (err < 0)
470 goto out;
471 err = PyType_Ready(&pyrf_comm_event__type);
472 if (err < 0)
473 goto out;
474 err = PyType_Ready(&pyrf_throttle_event__type);
475 if (err < 0)
476 goto out;
477 err = PyType_Ready(&pyrf_read_event__type);
478 if (err < 0)
479 goto out;
480 err = PyType_Ready(&pyrf_sample_event__type);
481 if (err < 0)
482 goto out;
483 err = PyType_Ready(&pyrf_context_switch_event__type);
484 if (err < 0)
485 goto out;
486 out:
487 return err;
488 }
489
490 static PyTypeObject *pyrf_event__type[] = {
491 [PERF_RECORD_MMAP] = &pyrf_mmap_event__type,
492 [PERF_RECORD_LOST] = &pyrf_lost_event__type,
493 [PERF_RECORD_COMM] = &pyrf_comm_event__type,
494 [PERF_RECORD_EXIT] = &pyrf_task_event__type,
495 [PERF_RECORD_THROTTLE] = &pyrf_throttle_event__type,
496 [PERF_RECORD_UNTHROTTLE] = &pyrf_throttle_event__type,
497 [PERF_RECORD_FORK] = &pyrf_task_event__type,
498 [PERF_RECORD_READ] = &pyrf_read_event__type,
499 [PERF_RECORD_SAMPLE] = &pyrf_sample_event__type,
500 [PERF_RECORD_SWITCH] = &pyrf_context_switch_event__type,
501 [PERF_RECORD_SWITCH_CPU_WIDE] = &pyrf_context_switch_event__type,
502 };
503
pyrf_event__new(union perf_event * event)504 static PyObject *pyrf_event__new(union perf_event *event)
505 {
506 struct pyrf_event *pevent;
507 PyTypeObject *ptype;
508
509 if ((event->header.type < PERF_RECORD_MMAP ||
510 event->header.type > PERF_RECORD_SAMPLE) &&
511 !(event->header.type == PERF_RECORD_SWITCH ||
512 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE))
513 return NULL;
514
515 // FIXME this better be dynamic or we need to parse everything
516 // before calling perf_mmap__consume(), including tracepoint fields.
517 if (sizeof(pevent->event) < event->header.size)
518 return NULL;
519
520 ptype = pyrf_event__type[event->header.type];
521 pevent = PyObject_New(struct pyrf_event, ptype);
522 if (pevent != NULL)
523 memcpy(&pevent->event, event, event->header.size);
524 return (PyObject *)pevent;
525 }
526
527 struct pyrf_cpu_map {
528 PyObject_HEAD
529
530 struct perf_cpu_map *cpus;
531 };
532
pyrf_cpu_map__init(struct pyrf_cpu_map * pcpus,PyObject * args,PyObject * kwargs)533 static int pyrf_cpu_map__init(struct pyrf_cpu_map *pcpus,
534 PyObject *args, PyObject *kwargs)
535 {
536 static char *kwlist[] = { "cpustr", NULL };
537 char *cpustr = NULL;
538
539 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s",
540 kwlist, &cpustr))
541 return -1;
542
543 pcpus->cpus = perf_cpu_map__new(cpustr);
544 if (pcpus->cpus == NULL)
545 return -1;
546 return 0;
547 }
548
pyrf_cpu_map__delete(struct pyrf_cpu_map * pcpus)549 static void pyrf_cpu_map__delete(struct pyrf_cpu_map *pcpus)
550 {
551 perf_cpu_map__put(pcpus->cpus);
552 Py_TYPE(pcpus)->tp_free((PyObject*)pcpus);
553 }
554
pyrf_cpu_map__length(PyObject * obj)555 static Py_ssize_t pyrf_cpu_map__length(PyObject *obj)
556 {
557 struct pyrf_cpu_map *pcpus = (void *)obj;
558
559 return perf_cpu_map__nr(pcpus->cpus);
560 }
561
pyrf_cpu_map__item(PyObject * obj,Py_ssize_t i)562 static PyObject *pyrf_cpu_map__item(PyObject *obj, Py_ssize_t i)
563 {
564 struct pyrf_cpu_map *pcpus = (void *)obj;
565
566 if (i >= perf_cpu_map__nr(pcpus->cpus))
567 return NULL;
568
569 return Py_BuildValue("i", perf_cpu_map__cpu(pcpus->cpus, i).cpu);
570 }
571
572 static PySequenceMethods pyrf_cpu_map__sequence_methods = {
573 .sq_length = pyrf_cpu_map__length,
574 .sq_item = pyrf_cpu_map__item,
575 };
576
577 static char pyrf_cpu_map__doc[] = PyDoc_STR("cpu map object.");
578
579 static PyTypeObject pyrf_cpu_map__type = {
580 PyVarObject_HEAD_INIT(NULL, 0)
581 .tp_name = "perf.cpu_map",
582 .tp_basicsize = sizeof(struct pyrf_cpu_map),
583 .tp_dealloc = (destructor)pyrf_cpu_map__delete,
584 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
585 .tp_doc = pyrf_cpu_map__doc,
586 .tp_as_sequence = &pyrf_cpu_map__sequence_methods,
587 .tp_init = (initproc)pyrf_cpu_map__init,
588 };
589
pyrf_cpu_map__setup_types(void)590 static int pyrf_cpu_map__setup_types(void)
591 {
592 pyrf_cpu_map__type.tp_new = PyType_GenericNew;
593 return PyType_Ready(&pyrf_cpu_map__type);
594 }
595
596 struct pyrf_thread_map {
597 PyObject_HEAD
598
599 struct perf_thread_map *threads;
600 };
601
pyrf_thread_map__init(struct pyrf_thread_map * pthreads,PyObject * args,PyObject * kwargs)602 static int pyrf_thread_map__init(struct pyrf_thread_map *pthreads,
603 PyObject *args, PyObject *kwargs)
604 {
605 static char *kwlist[] = { "pid", "tid", "uid", NULL };
606 int pid = -1, tid = -1, uid = UINT_MAX;
607
608 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iii",
609 kwlist, &pid, &tid, &uid))
610 return -1;
611
612 pthreads->threads = thread_map__new(pid, tid, uid);
613 if (pthreads->threads == NULL)
614 return -1;
615 return 0;
616 }
617
pyrf_thread_map__delete(struct pyrf_thread_map * pthreads)618 static void pyrf_thread_map__delete(struct pyrf_thread_map *pthreads)
619 {
620 perf_thread_map__put(pthreads->threads);
621 Py_TYPE(pthreads)->tp_free((PyObject*)pthreads);
622 }
623
pyrf_thread_map__length(PyObject * obj)624 static Py_ssize_t pyrf_thread_map__length(PyObject *obj)
625 {
626 struct pyrf_thread_map *pthreads = (void *)obj;
627
628 return perf_thread_map__nr(pthreads->threads);
629 }
630
pyrf_thread_map__item(PyObject * obj,Py_ssize_t i)631 static PyObject *pyrf_thread_map__item(PyObject *obj, Py_ssize_t i)
632 {
633 struct pyrf_thread_map *pthreads = (void *)obj;
634
635 if (i >= perf_thread_map__nr(pthreads->threads))
636 return NULL;
637
638 return Py_BuildValue("i", perf_thread_map__pid(pthreads->threads, i));
639 }
640
641 static PySequenceMethods pyrf_thread_map__sequence_methods = {
642 .sq_length = pyrf_thread_map__length,
643 .sq_item = pyrf_thread_map__item,
644 };
645
646 static char pyrf_thread_map__doc[] = PyDoc_STR("thread map object.");
647
648 static PyTypeObject pyrf_thread_map__type = {
649 PyVarObject_HEAD_INIT(NULL, 0)
650 .tp_name = "perf.thread_map",
651 .tp_basicsize = sizeof(struct pyrf_thread_map),
652 .tp_dealloc = (destructor)pyrf_thread_map__delete,
653 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
654 .tp_doc = pyrf_thread_map__doc,
655 .tp_as_sequence = &pyrf_thread_map__sequence_methods,
656 .tp_init = (initproc)pyrf_thread_map__init,
657 };
658
pyrf_thread_map__setup_types(void)659 static int pyrf_thread_map__setup_types(void)
660 {
661 pyrf_thread_map__type.tp_new = PyType_GenericNew;
662 return PyType_Ready(&pyrf_thread_map__type);
663 }
664
665 struct pyrf_evsel {
666 PyObject_HEAD
667
668 struct evsel evsel;
669 };
670
pyrf_evsel__init(struct pyrf_evsel * pevsel,PyObject * args,PyObject * kwargs)671 static int pyrf_evsel__init(struct pyrf_evsel *pevsel,
672 PyObject *args, PyObject *kwargs)
673 {
674 struct perf_event_attr attr = {
675 .type = PERF_TYPE_HARDWARE,
676 .config = PERF_COUNT_HW_CPU_CYCLES,
677 .sample_type = PERF_SAMPLE_PERIOD | PERF_SAMPLE_TID,
678 };
679 static char *kwlist[] = {
680 "type",
681 "config",
682 "sample_freq",
683 "sample_period",
684 "sample_type",
685 "read_format",
686 "disabled",
687 "inherit",
688 "pinned",
689 "exclusive",
690 "exclude_user",
691 "exclude_kernel",
692 "exclude_hv",
693 "exclude_idle",
694 "mmap",
695 "context_switch",
696 "comm",
697 "freq",
698 "inherit_stat",
699 "enable_on_exec",
700 "task",
701 "watermark",
702 "precise_ip",
703 "mmap_data",
704 "sample_id_all",
705 "wakeup_events",
706 "bp_type",
707 "bp_addr",
708 "bp_len",
709 NULL
710 };
711 u64 sample_period = 0;
712 u32 disabled = 0,
713 inherit = 0,
714 pinned = 0,
715 exclusive = 0,
716 exclude_user = 0,
717 exclude_kernel = 0,
718 exclude_hv = 0,
719 exclude_idle = 0,
720 mmap = 0,
721 context_switch = 0,
722 comm = 0,
723 freq = 1,
724 inherit_stat = 0,
725 enable_on_exec = 0,
726 task = 0,
727 watermark = 0,
728 precise_ip = 0,
729 mmap_data = 0,
730 sample_id_all = 1;
731 int idx = 0;
732
733 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
734 "|iKiKKiiiiiiiiiiiiiiiiiiiiiiKK", kwlist,
735 &attr.type, &attr.config, &attr.sample_freq,
736 &sample_period, &attr.sample_type,
737 &attr.read_format, &disabled, &inherit,
738 &pinned, &exclusive, &exclude_user,
739 &exclude_kernel, &exclude_hv, &exclude_idle,
740 &mmap, &context_switch, &comm, &freq, &inherit_stat,
741 &enable_on_exec, &task, &watermark,
742 &precise_ip, &mmap_data, &sample_id_all,
743 &attr.wakeup_events, &attr.bp_type,
744 &attr.bp_addr, &attr.bp_len, &idx))
745 return -1;
746
747 /* union... */
748 if (sample_period != 0) {
749 if (attr.sample_freq != 0)
750 return -1; /* FIXME: throw right exception */
751 attr.sample_period = sample_period;
752 }
753
754 /* Bitfields */
755 attr.disabled = disabled;
756 attr.inherit = inherit;
757 attr.pinned = pinned;
758 attr.exclusive = exclusive;
759 attr.exclude_user = exclude_user;
760 attr.exclude_kernel = exclude_kernel;
761 attr.exclude_hv = exclude_hv;
762 attr.exclude_idle = exclude_idle;
763 attr.mmap = mmap;
764 attr.context_switch = context_switch;
765 attr.comm = comm;
766 attr.freq = freq;
767 attr.inherit_stat = inherit_stat;
768 attr.enable_on_exec = enable_on_exec;
769 attr.task = task;
770 attr.watermark = watermark;
771 attr.precise_ip = precise_ip;
772 attr.mmap_data = mmap_data;
773 attr.sample_id_all = sample_id_all;
774 attr.size = sizeof(attr);
775
776 evsel__init(&pevsel->evsel, &attr, idx);
777 return 0;
778 }
779
pyrf_evsel__delete(struct pyrf_evsel * pevsel)780 static void pyrf_evsel__delete(struct pyrf_evsel *pevsel)
781 {
782 evsel__exit(&pevsel->evsel);
783 Py_TYPE(pevsel)->tp_free((PyObject*)pevsel);
784 }
785
pyrf_evsel__open(struct pyrf_evsel * pevsel,PyObject * args,PyObject * kwargs)786 static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel,
787 PyObject *args, PyObject *kwargs)
788 {
789 struct evsel *evsel = &pevsel->evsel;
790 struct perf_cpu_map *cpus = NULL;
791 struct perf_thread_map *threads = NULL;
792 PyObject *pcpus = NULL, *pthreads = NULL;
793 int group = 0, inherit = 0;
794 static char *kwlist[] = { "cpus", "threads", "group", "inherit", NULL };
795
796 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OOii", kwlist,
797 &pcpus, &pthreads, &group, &inherit))
798 return NULL;
799
800 if (pthreads != NULL)
801 threads = ((struct pyrf_thread_map *)pthreads)->threads;
802
803 if (pcpus != NULL)
804 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
805
806 evsel->core.attr.inherit = inherit;
807 /*
808 * This will group just the fds for this single evsel, to group
809 * multiple events, use evlist.open().
810 */
811 if (evsel__open(evsel, cpus, threads) < 0) {
812 PyErr_SetFromErrno(PyExc_OSError);
813 return NULL;
814 }
815
816 Py_INCREF(Py_None);
817 return Py_None;
818 }
819
820 static PyMethodDef pyrf_evsel__methods[] = {
821 {
822 .ml_name = "open",
823 .ml_meth = (PyCFunction)pyrf_evsel__open,
824 .ml_flags = METH_VARARGS | METH_KEYWORDS,
825 .ml_doc = PyDoc_STR("open the event selector file descriptor table.")
826 },
827 { .ml_name = NULL, }
828 };
829
830 static char pyrf_evsel__doc[] = PyDoc_STR("perf event selector list object.");
831
832 static PyTypeObject pyrf_evsel__type = {
833 PyVarObject_HEAD_INIT(NULL, 0)
834 .tp_name = "perf.evsel",
835 .tp_basicsize = sizeof(struct pyrf_evsel),
836 .tp_dealloc = (destructor)pyrf_evsel__delete,
837 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
838 .tp_doc = pyrf_evsel__doc,
839 .tp_methods = pyrf_evsel__methods,
840 .tp_init = (initproc)pyrf_evsel__init,
841 };
842
pyrf_evsel__setup_types(void)843 static int pyrf_evsel__setup_types(void)
844 {
845 pyrf_evsel__type.tp_new = PyType_GenericNew;
846 return PyType_Ready(&pyrf_evsel__type);
847 }
848
849 struct pyrf_evlist {
850 PyObject_HEAD
851
852 struct evlist evlist;
853 };
854
pyrf_evlist__init(struct pyrf_evlist * pevlist,PyObject * args,PyObject * kwargs __maybe_unused)855 static int pyrf_evlist__init(struct pyrf_evlist *pevlist,
856 PyObject *args, PyObject *kwargs __maybe_unused)
857 {
858 PyObject *pcpus = NULL, *pthreads = NULL;
859 struct perf_cpu_map *cpus;
860 struct perf_thread_map *threads;
861
862 if (!PyArg_ParseTuple(args, "OO", &pcpus, &pthreads))
863 return -1;
864
865 threads = ((struct pyrf_thread_map *)pthreads)->threads;
866 cpus = ((struct pyrf_cpu_map *)pcpus)->cpus;
867 evlist__init(&pevlist->evlist, cpus, threads);
868 return 0;
869 }
870
pyrf_evlist__delete(struct pyrf_evlist * pevlist)871 static void pyrf_evlist__delete(struct pyrf_evlist *pevlist)
872 {
873 evlist__exit(&pevlist->evlist);
874 Py_TYPE(pevlist)->tp_free((PyObject*)pevlist);
875 }
876
pyrf_evlist__mmap(struct pyrf_evlist * pevlist,PyObject * args,PyObject * kwargs)877 static PyObject *pyrf_evlist__mmap(struct pyrf_evlist *pevlist,
878 PyObject *args, PyObject *kwargs)
879 {
880 struct evlist *evlist = &pevlist->evlist;
881 static char *kwlist[] = { "pages", "overwrite", NULL };
882 int pages = 128, overwrite = false;
883
884 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ii", kwlist,
885 &pages, &overwrite))
886 return NULL;
887
888 if (evlist__mmap(evlist, pages) < 0) {
889 PyErr_SetFromErrno(PyExc_OSError);
890 return NULL;
891 }
892
893 Py_INCREF(Py_None);
894 return Py_None;
895 }
896
pyrf_evlist__poll(struct pyrf_evlist * pevlist,PyObject * args,PyObject * kwargs)897 static PyObject *pyrf_evlist__poll(struct pyrf_evlist *pevlist,
898 PyObject *args, PyObject *kwargs)
899 {
900 struct evlist *evlist = &pevlist->evlist;
901 static char *kwlist[] = { "timeout", NULL };
902 int timeout = -1, n;
903
904 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &timeout))
905 return NULL;
906
907 n = evlist__poll(evlist, timeout);
908 if (n < 0) {
909 PyErr_SetFromErrno(PyExc_OSError);
910 return NULL;
911 }
912
913 return Py_BuildValue("i", n);
914 }
915
pyrf_evlist__get_pollfd(struct pyrf_evlist * pevlist,PyObject * args __maybe_unused,PyObject * kwargs __maybe_unused)916 static PyObject *pyrf_evlist__get_pollfd(struct pyrf_evlist *pevlist,
917 PyObject *args __maybe_unused,
918 PyObject *kwargs __maybe_unused)
919 {
920 struct evlist *evlist = &pevlist->evlist;
921 PyObject *list = PyList_New(0);
922 int i;
923
924 for (i = 0; i < evlist->core.pollfd.nr; ++i) {
925 PyObject *file;
926 #if PY_MAJOR_VERSION < 3
927 FILE *fp = fdopen(evlist->core.pollfd.entries[i].fd, "r");
928
929 if (fp == NULL)
930 goto free_list;
931
932 file = PyFile_FromFile(fp, "perf", "r", NULL);
933 #else
934 file = PyFile_FromFd(evlist->core.pollfd.entries[i].fd, "perf", "r", -1,
935 NULL, NULL, NULL, 0);
936 #endif
937 if (file == NULL)
938 goto free_list;
939
940 if (PyList_Append(list, file) != 0) {
941 Py_DECREF(file);
942 goto free_list;
943 }
944
945 Py_DECREF(file);
946 }
947
948 return list;
949 free_list:
950 return PyErr_NoMemory();
951 }
952
953
pyrf_evlist__add(struct pyrf_evlist * pevlist,PyObject * args,PyObject * kwargs __maybe_unused)954 static PyObject *pyrf_evlist__add(struct pyrf_evlist *pevlist,
955 PyObject *args,
956 PyObject *kwargs __maybe_unused)
957 {
958 struct evlist *evlist = &pevlist->evlist;
959 PyObject *pevsel;
960 struct evsel *evsel;
961
962 if (!PyArg_ParseTuple(args, "O", &pevsel))
963 return NULL;
964
965 Py_INCREF(pevsel);
966 evsel = &((struct pyrf_evsel *)pevsel)->evsel;
967 evsel->core.idx = evlist->core.nr_entries;
968 evlist__add(evlist, evsel);
969
970 return Py_BuildValue("i", evlist->core.nr_entries);
971 }
972
get_md(struct evlist * evlist,int cpu)973 static struct mmap *get_md(struct evlist *evlist, int cpu)
974 {
975 int i;
976
977 for (i = 0; i < evlist->core.nr_mmaps; i++) {
978 struct mmap *md = &evlist->mmap[i];
979
980 if (md->core.cpu.cpu == cpu)
981 return md;
982 }
983
984 return NULL;
985 }
986
pyrf_evlist__read_on_cpu(struct pyrf_evlist * pevlist,PyObject * args,PyObject * kwargs)987 static PyObject *pyrf_evlist__read_on_cpu(struct pyrf_evlist *pevlist,
988 PyObject *args, PyObject *kwargs)
989 {
990 struct evlist *evlist = &pevlist->evlist;
991 union perf_event *event;
992 int sample_id_all = 1, cpu;
993 static char *kwlist[] = { "cpu", "sample_id_all", NULL };
994 struct mmap *md;
995 int err;
996
997 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist,
998 &cpu, &sample_id_all))
999 return NULL;
1000
1001 md = get_md(evlist, cpu);
1002 if (!md)
1003 return NULL;
1004
1005 if (perf_mmap__read_init(&md->core) < 0)
1006 goto end;
1007
1008 event = perf_mmap__read_event(&md->core);
1009 if (event != NULL) {
1010 PyObject *pyevent = pyrf_event__new(event);
1011 struct pyrf_event *pevent = (struct pyrf_event *)pyevent;
1012 struct evsel *evsel;
1013
1014 if (pyevent == NULL)
1015 return PyErr_NoMemory();
1016
1017 evsel = evlist__event2evsel(evlist, event);
1018 if (!evsel) {
1019 Py_DECREF(pyevent);
1020 Py_INCREF(Py_None);
1021 return Py_None;
1022 }
1023
1024 pevent->evsel = evsel;
1025
1026 perf_mmap__consume(&md->core);
1027
1028 err = evsel__parse_sample(evsel, &pevent->event, &pevent->sample);
1029 if (err) {
1030 Py_DECREF(pyevent);
1031 return PyErr_Format(PyExc_OSError,
1032 "perf: can't parse sample, err=%d", err);
1033 }
1034
1035 return pyevent;
1036 }
1037 end:
1038 Py_INCREF(Py_None);
1039 return Py_None;
1040 }
1041
pyrf_evlist__open(struct pyrf_evlist * pevlist,PyObject * args,PyObject * kwargs)1042 static PyObject *pyrf_evlist__open(struct pyrf_evlist *pevlist,
1043 PyObject *args, PyObject *kwargs)
1044 {
1045 struct evlist *evlist = &pevlist->evlist;
1046
1047 if (evlist__open(evlist) < 0) {
1048 PyErr_SetFromErrno(PyExc_OSError);
1049 return NULL;
1050 }
1051
1052 Py_INCREF(Py_None);
1053 return Py_None;
1054 }
1055
1056 static PyMethodDef pyrf_evlist__methods[] = {
1057 {
1058 .ml_name = "mmap",
1059 .ml_meth = (PyCFunction)pyrf_evlist__mmap,
1060 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1061 .ml_doc = PyDoc_STR("mmap the file descriptor table.")
1062 },
1063 {
1064 .ml_name = "open",
1065 .ml_meth = (PyCFunction)pyrf_evlist__open,
1066 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1067 .ml_doc = PyDoc_STR("open the file descriptors.")
1068 },
1069 {
1070 .ml_name = "poll",
1071 .ml_meth = (PyCFunction)pyrf_evlist__poll,
1072 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1073 .ml_doc = PyDoc_STR("poll the file descriptor table.")
1074 },
1075 {
1076 .ml_name = "get_pollfd",
1077 .ml_meth = (PyCFunction)pyrf_evlist__get_pollfd,
1078 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1079 .ml_doc = PyDoc_STR("get the poll file descriptor table.")
1080 },
1081 {
1082 .ml_name = "add",
1083 .ml_meth = (PyCFunction)pyrf_evlist__add,
1084 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1085 .ml_doc = PyDoc_STR("adds an event selector to the list.")
1086 },
1087 {
1088 .ml_name = "read_on_cpu",
1089 .ml_meth = (PyCFunction)pyrf_evlist__read_on_cpu,
1090 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1091 .ml_doc = PyDoc_STR("reads an event.")
1092 },
1093 { .ml_name = NULL, }
1094 };
1095
pyrf_evlist__length(PyObject * obj)1096 static Py_ssize_t pyrf_evlist__length(PyObject *obj)
1097 {
1098 struct pyrf_evlist *pevlist = (void *)obj;
1099
1100 return pevlist->evlist.core.nr_entries;
1101 }
1102
pyrf_evlist__item(PyObject * obj,Py_ssize_t i)1103 static PyObject *pyrf_evlist__item(PyObject *obj, Py_ssize_t i)
1104 {
1105 struct pyrf_evlist *pevlist = (void *)obj;
1106 struct evsel *pos;
1107
1108 if (i >= pevlist->evlist.core.nr_entries)
1109 return NULL;
1110
1111 evlist__for_each_entry(&pevlist->evlist, pos) {
1112 if (i-- == 0)
1113 break;
1114 }
1115
1116 return Py_BuildValue("O", container_of(pos, struct pyrf_evsel, evsel));
1117 }
1118
1119 static PySequenceMethods pyrf_evlist__sequence_methods = {
1120 .sq_length = pyrf_evlist__length,
1121 .sq_item = pyrf_evlist__item,
1122 };
1123
1124 static char pyrf_evlist__doc[] = PyDoc_STR("perf event selector list object.");
1125
1126 static PyTypeObject pyrf_evlist__type = {
1127 PyVarObject_HEAD_INIT(NULL, 0)
1128 .tp_name = "perf.evlist",
1129 .tp_basicsize = sizeof(struct pyrf_evlist),
1130 .tp_dealloc = (destructor)pyrf_evlist__delete,
1131 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1132 .tp_as_sequence = &pyrf_evlist__sequence_methods,
1133 .tp_doc = pyrf_evlist__doc,
1134 .tp_methods = pyrf_evlist__methods,
1135 .tp_init = (initproc)pyrf_evlist__init,
1136 };
1137
pyrf_evlist__setup_types(void)1138 static int pyrf_evlist__setup_types(void)
1139 {
1140 pyrf_evlist__type.tp_new = PyType_GenericNew;
1141 return PyType_Ready(&pyrf_evlist__type);
1142 }
1143
1144 #define PERF_CONST(name) { #name, PERF_##name }
1145
1146 static struct {
1147 const char *name;
1148 int value;
1149 } perf__constants[] = {
1150 PERF_CONST(TYPE_HARDWARE),
1151 PERF_CONST(TYPE_SOFTWARE),
1152 PERF_CONST(TYPE_TRACEPOINT),
1153 PERF_CONST(TYPE_HW_CACHE),
1154 PERF_CONST(TYPE_RAW),
1155 PERF_CONST(TYPE_BREAKPOINT),
1156
1157 PERF_CONST(COUNT_HW_CPU_CYCLES),
1158 PERF_CONST(COUNT_HW_INSTRUCTIONS),
1159 PERF_CONST(COUNT_HW_CACHE_REFERENCES),
1160 PERF_CONST(COUNT_HW_CACHE_MISSES),
1161 PERF_CONST(COUNT_HW_BRANCH_INSTRUCTIONS),
1162 PERF_CONST(COUNT_HW_BRANCH_MISSES),
1163 PERF_CONST(COUNT_HW_BUS_CYCLES),
1164 PERF_CONST(COUNT_HW_CACHE_L1D),
1165 PERF_CONST(COUNT_HW_CACHE_L1I),
1166 PERF_CONST(COUNT_HW_CACHE_LL),
1167 PERF_CONST(COUNT_HW_CACHE_DTLB),
1168 PERF_CONST(COUNT_HW_CACHE_ITLB),
1169 PERF_CONST(COUNT_HW_CACHE_BPU),
1170 PERF_CONST(COUNT_HW_CACHE_OP_READ),
1171 PERF_CONST(COUNT_HW_CACHE_OP_WRITE),
1172 PERF_CONST(COUNT_HW_CACHE_OP_PREFETCH),
1173 PERF_CONST(COUNT_HW_CACHE_RESULT_ACCESS),
1174 PERF_CONST(COUNT_HW_CACHE_RESULT_MISS),
1175
1176 PERF_CONST(COUNT_HW_STALLED_CYCLES_FRONTEND),
1177 PERF_CONST(COUNT_HW_STALLED_CYCLES_BACKEND),
1178
1179 PERF_CONST(COUNT_SW_CPU_CLOCK),
1180 PERF_CONST(COUNT_SW_TASK_CLOCK),
1181 PERF_CONST(COUNT_SW_PAGE_FAULTS),
1182 PERF_CONST(COUNT_SW_CONTEXT_SWITCHES),
1183 PERF_CONST(COUNT_SW_CPU_MIGRATIONS),
1184 PERF_CONST(COUNT_SW_PAGE_FAULTS_MIN),
1185 PERF_CONST(COUNT_SW_PAGE_FAULTS_MAJ),
1186 PERF_CONST(COUNT_SW_ALIGNMENT_FAULTS),
1187 PERF_CONST(COUNT_SW_EMULATION_FAULTS),
1188 PERF_CONST(COUNT_SW_DUMMY),
1189
1190 PERF_CONST(SAMPLE_IP),
1191 PERF_CONST(SAMPLE_TID),
1192 PERF_CONST(SAMPLE_TIME),
1193 PERF_CONST(SAMPLE_ADDR),
1194 PERF_CONST(SAMPLE_READ),
1195 PERF_CONST(SAMPLE_CALLCHAIN),
1196 PERF_CONST(SAMPLE_ID),
1197 PERF_CONST(SAMPLE_CPU),
1198 PERF_CONST(SAMPLE_PERIOD),
1199 PERF_CONST(SAMPLE_STREAM_ID),
1200 PERF_CONST(SAMPLE_RAW),
1201
1202 PERF_CONST(FORMAT_TOTAL_TIME_ENABLED),
1203 PERF_CONST(FORMAT_TOTAL_TIME_RUNNING),
1204 PERF_CONST(FORMAT_ID),
1205 PERF_CONST(FORMAT_GROUP),
1206
1207 PERF_CONST(RECORD_MMAP),
1208 PERF_CONST(RECORD_LOST),
1209 PERF_CONST(RECORD_COMM),
1210 PERF_CONST(RECORD_EXIT),
1211 PERF_CONST(RECORD_THROTTLE),
1212 PERF_CONST(RECORD_UNTHROTTLE),
1213 PERF_CONST(RECORD_FORK),
1214 PERF_CONST(RECORD_READ),
1215 PERF_CONST(RECORD_SAMPLE),
1216 PERF_CONST(RECORD_MMAP2),
1217 PERF_CONST(RECORD_AUX),
1218 PERF_CONST(RECORD_ITRACE_START),
1219 PERF_CONST(RECORD_LOST_SAMPLES),
1220 PERF_CONST(RECORD_SWITCH),
1221 PERF_CONST(RECORD_SWITCH_CPU_WIDE),
1222
1223 PERF_CONST(RECORD_MISC_SWITCH_OUT),
1224 { .name = NULL, },
1225 };
1226
pyrf__tracepoint(struct pyrf_evsel * pevsel,PyObject * args,PyObject * kwargs)1227 static PyObject *pyrf__tracepoint(struct pyrf_evsel *pevsel,
1228 PyObject *args, PyObject *kwargs)
1229 {
1230 #ifndef HAVE_LIBTRACEEVENT
1231 return NULL;
1232 #else
1233 struct tep_event *tp_format;
1234 static char *kwlist[] = { "sys", "name", NULL };
1235 char *sys = NULL;
1236 char *name = NULL;
1237
1238 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss", kwlist,
1239 &sys, &name))
1240 return NULL;
1241
1242 tp_format = trace_event__tp_format(sys, name);
1243 if (IS_ERR(tp_format))
1244 return _PyLong_FromLong(-1);
1245
1246 return _PyLong_FromLong(tp_format->id);
1247 #endif // HAVE_LIBTRACEEVENT
1248 }
1249
1250 static PyMethodDef perf__methods[] = {
1251 {
1252 .ml_name = "tracepoint",
1253 .ml_meth = (PyCFunction) pyrf__tracepoint,
1254 .ml_flags = METH_VARARGS | METH_KEYWORDS,
1255 .ml_doc = PyDoc_STR("Get tracepoint config.")
1256 },
1257 { .ml_name = NULL, }
1258 };
1259
1260 #if PY_MAJOR_VERSION < 3
initperf(void)1261 PyMODINIT_FUNC initperf(void)
1262 #else
1263 PyMODINIT_FUNC PyInit_perf(void)
1264 #endif
1265 {
1266 PyObject *obj;
1267 int i;
1268 PyObject *dict;
1269 #if PY_MAJOR_VERSION < 3
1270 PyObject *module = Py_InitModule("perf", perf__methods);
1271 #else
1272 static struct PyModuleDef moduledef = {
1273 PyModuleDef_HEAD_INIT,
1274 "perf", /* m_name */
1275 "", /* m_doc */
1276 -1, /* m_size */
1277 perf__methods, /* m_methods */
1278 NULL, /* m_reload */
1279 NULL, /* m_traverse */
1280 NULL, /* m_clear */
1281 NULL, /* m_free */
1282 };
1283 PyObject *module = PyModule_Create(&moduledef);
1284 #endif
1285
1286 if (module == NULL ||
1287 pyrf_event__setup_types() < 0 ||
1288 pyrf_evlist__setup_types() < 0 ||
1289 pyrf_evsel__setup_types() < 0 ||
1290 pyrf_thread_map__setup_types() < 0 ||
1291 pyrf_cpu_map__setup_types() < 0)
1292 #if PY_MAJOR_VERSION < 3
1293 return;
1294 #else
1295 return module;
1296 #endif
1297
1298 /* The page_size is placed in util object. */
1299 page_size = sysconf(_SC_PAGE_SIZE);
1300
1301 Py_INCREF(&pyrf_evlist__type);
1302 PyModule_AddObject(module, "evlist", (PyObject*)&pyrf_evlist__type);
1303
1304 Py_INCREF(&pyrf_evsel__type);
1305 PyModule_AddObject(module, "evsel", (PyObject*)&pyrf_evsel__type);
1306
1307 Py_INCREF(&pyrf_mmap_event__type);
1308 PyModule_AddObject(module, "mmap_event", (PyObject *)&pyrf_mmap_event__type);
1309
1310 Py_INCREF(&pyrf_lost_event__type);
1311 PyModule_AddObject(module, "lost_event", (PyObject *)&pyrf_lost_event__type);
1312
1313 Py_INCREF(&pyrf_comm_event__type);
1314 PyModule_AddObject(module, "comm_event", (PyObject *)&pyrf_comm_event__type);
1315
1316 Py_INCREF(&pyrf_task_event__type);
1317 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1318
1319 Py_INCREF(&pyrf_throttle_event__type);
1320 PyModule_AddObject(module, "throttle_event", (PyObject *)&pyrf_throttle_event__type);
1321
1322 Py_INCREF(&pyrf_task_event__type);
1323 PyModule_AddObject(module, "task_event", (PyObject *)&pyrf_task_event__type);
1324
1325 Py_INCREF(&pyrf_read_event__type);
1326 PyModule_AddObject(module, "read_event", (PyObject *)&pyrf_read_event__type);
1327
1328 Py_INCREF(&pyrf_sample_event__type);
1329 PyModule_AddObject(module, "sample_event", (PyObject *)&pyrf_sample_event__type);
1330
1331 Py_INCREF(&pyrf_context_switch_event__type);
1332 PyModule_AddObject(module, "switch_event", (PyObject *)&pyrf_context_switch_event__type);
1333
1334 Py_INCREF(&pyrf_thread_map__type);
1335 PyModule_AddObject(module, "thread_map", (PyObject*)&pyrf_thread_map__type);
1336
1337 Py_INCREF(&pyrf_cpu_map__type);
1338 PyModule_AddObject(module, "cpu_map", (PyObject*)&pyrf_cpu_map__type);
1339
1340 dict = PyModule_GetDict(module);
1341 if (dict == NULL)
1342 goto error;
1343
1344 for (i = 0; perf__constants[i].name != NULL; i++) {
1345 obj = _PyLong_FromLong(perf__constants[i].value);
1346 if (obj == NULL)
1347 goto error;
1348 PyDict_SetItemString(dict, perf__constants[i].name, obj);
1349 Py_DECREF(obj);
1350 }
1351
1352 error:
1353 if (PyErr_Occurred())
1354 PyErr_SetString(PyExc_ImportError, "perf: Init failed!");
1355 #if PY_MAJOR_VERSION >= 3
1356 return module;
1357 #endif
1358 }
1359
1360
1361 /* The following are stubs to avoid dragging in builtin-* objects. */
1362 /* TODO: move the code out of the builtin-* file into util. */
1363
1364 unsigned int scripting_max_stack = PERF_MAX_STACK_DEPTH;
1365
1366 #ifdef HAVE_KVM_STAT_SUPPORT
kvm_entry_event(struct evsel * evsel __maybe_unused)1367 bool kvm_entry_event(struct evsel *evsel __maybe_unused)
1368 {
1369 return false;
1370 }
1371
kvm_exit_event(struct evsel * evsel __maybe_unused)1372 bool kvm_exit_event(struct evsel *evsel __maybe_unused)
1373 {
1374 return false;
1375 }
1376
exit_event_begin(struct evsel * evsel __maybe_unused,struct perf_sample * sample __maybe_unused,struct event_key * key __maybe_unused)1377 bool exit_event_begin(struct evsel *evsel __maybe_unused,
1378 struct perf_sample *sample __maybe_unused,
1379 struct event_key *key __maybe_unused)
1380 {
1381 return false;
1382 }
1383
exit_event_end(struct evsel * evsel __maybe_unused,struct perf_sample * sample __maybe_unused,struct event_key * key __maybe_unused)1384 bool exit_event_end(struct evsel *evsel __maybe_unused,
1385 struct perf_sample *sample __maybe_unused,
1386 struct event_key *key __maybe_unused)
1387 {
1388 return false;
1389 }
1390
exit_event_decode_key(struct perf_kvm_stat * kvm __maybe_unused,struct event_key * key __maybe_unused,char * decode __maybe_unused)1391 void exit_event_decode_key(struct perf_kvm_stat *kvm __maybe_unused,
1392 struct event_key *key __maybe_unused,
1393 char *decode __maybe_unused)
1394 {
1395 }
1396 #endif // HAVE_KVM_STAT_SUPPORT
1397
find_scripts(char ** scripts_array __maybe_unused,char ** scripts_path_array __maybe_unused,int num __maybe_unused,int pathlen __maybe_unused)1398 int find_scripts(char **scripts_array __maybe_unused, char **scripts_path_array __maybe_unused,
1399 int num __maybe_unused, int pathlen __maybe_unused)
1400 {
1401 return -1;
1402 }
1403
perf_stat__set_no_csv_summary(int set __maybe_unused)1404 void perf_stat__set_no_csv_summary(int set __maybe_unused)
1405 {
1406 }
1407
perf_stat__set_big_num(int set __maybe_unused)1408 void perf_stat__set_big_num(int set __maybe_unused)
1409 {
1410 }
1411
script_spec_register(const char * spec __maybe_unused,struct scripting_ops * ops __maybe_unused)1412 int script_spec_register(const char *spec __maybe_unused, struct scripting_ops *ops __maybe_unused)
1413 {
1414 return -1;
1415 }
1416
arch_syscalls__strerrno_function(const char * arch __maybe_unused)1417 arch_syscalls__strerrno_t *arch_syscalls__strerrno_function(const char *arch __maybe_unused)
1418 {
1419 return NULL;
1420 }
1421
perf_kwork_add_work(struct perf_kwork * kwork __maybe_unused,struct kwork_class * class __maybe_unused,struct kwork_work * key __maybe_unused)1422 struct kwork_work *perf_kwork_add_work(struct perf_kwork *kwork __maybe_unused,
1423 struct kwork_class *class __maybe_unused,
1424 struct kwork_work *key __maybe_unused)
1425 {
1426 return NULL;
1427 }
1428
script_fetch_insn(struct perf_sample * sample __maybe_unused,struct thread * thread __maybe_unused,struct machine * machine __maybe_unused)1429 void script_fetch_insn(struct perf_sample *sample __maybe_unused,
1430 struct thread *thread __maybe_unused,
1431 struct machine *machine __maybe_unused)
1432 {
1433 }
1434
perf_sample__sprintf_flags(u32 flags __maybe_unused,char * str __maybe_unused,size_t sz __maybe_unused)1435 int perf_sample__sprintf_flags(u32 flags __maybe_unused, char *str __maybe_unused,
1436 size_t sz __maybe_unused)
1437 {
1438 return -1;
1439 }
1440
match_callstack_filter(struct machine * machine __maybe_unused,u64 * callstack __maybe_unused)1441 bool match_callstack_filter(struct machine *machine __maybe_unused, u64 *callstack __maybe_unused)
1442 {
1443 return false;
1444 }
1445
lock_stat_find(u64 addr __maybe_unused)1446 struct lock_stat *lock_stat_find(u64 addr __maybe_unused)
1447 {
1448 return NULL;
1449 }
1450
lock_stat_findnew(u64 addr __maybe_unused,const char * name __maybe_unused,int flags __maybe_unused)1451 struct lock_stat *lock_stat_findnew(u64 addr __maybe_unused, const char *name __maybe_unused,
1452 int flags __maybe_unused)
1453 {
1454 return NULL;
1455 }
1456
cmd_inject(int argc __maybe_unused,const char * argv[]__maybe_unused)1457 int cmd_inject(int argc __maybe_unused, const char *argv[] __maybe_unused)
1458 {
1459 return -1;
1460 }
1461