2 * trace-event-python. Feed trace events to an embedded Python interpreter.
4 * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <linux/bitmap.h>
31 #include "../../perf.h"
33 #include "../callchain.h"
37 #include "../thread.h"
39 #include "../machine.h"
40 #include "../db-export.h"
41 #include "../thread-stack.h"
42 #include "../trace-event.h"
43 #include "../machine.h"
44 #include "thread_map.h"
48 PyMODINIT_FUNC
initperf_trace_context(void);
50 #define TRACE_EVENT_TYPE_MAX \
51 ((1 << (sizeof(unsigned short) * 8)) - 1)
53 static DECLARE_BITMAP(events_defined
, TRACE_EVENT_TYPE_MAX
);
56 #define N_COMMON_FIELDS 7
58 extern struct scripting_context
*scripting_context
;
60 static char *cur_field_name
;
61 static int zero_flag_atom
;
63 static PyObject
*main_module
, *main_dict
;
67 PyObject
*evsel_handler
;
68 PyObject
*machine_handler
;
69 PyObject
*thread_handler
;
70 PyObject
*comm_handler
;
71 PyObject
*comm_thread_handler
;
72 PyObject
*dso_handler
;
73 PyObject
*symbol_handler
;
74 PyObject
*branch_type_handler
;
75 PyObject
*sample_handler
;
76 PyObject
*call_path_handler
;
77 PyObject
*call_return_handler
;
81 static struct tables tables_global
;
83 static void handler_call_die(const char *handler_name
) NORETURN
;
84 static void handler_call_die(const char *handler_name
)
87 Py_FatalError("problem in Python trace event handler");
88 // Py_FatalError does not return
89 // but we have to make the compiler happy
94 * Insert val into into the dictionary and decrement the reference counter.
95 * This is necessary for dictionaries since PyDict_SetItemString() does not
96 * steal a reference, as opposed to PyTuple_SetItem().
98 static void pydict_set_item_string_decref(PyObject
*dict
, const char *key
, PyObject
*val
)
100 PyDict_SetItemString(dict
, key
, val
);
104 static PyObject
*get_handler(const char *handler_name
)
108 handler
= PyDict_GetItemString(main_dict
, handler_name
);
109 if (handler
&& !PyCallable_Check(handler
))
114 static void call_object(PyObject
*handler
, PyObject
*args
, const char *die_msg
)
118 retval
= PyObject_CallObject(handler
, args
);
120 handler_call_die(die_msg
);
124 static void try_call_object(const char *handler_name
, PyObject
*args
)
128 handler
= get_handler(handler_name
);
130 call_object(handler
, args
, handler_name
);
133 static void define_value(enum print_arg_type field_type
,
135 const char *field_name
,
136 const char *field_value
,
137 const char *field_str
)
139 const char *handler_name
= "define_flag_value";
141 unsigned long long value
;
144 if (field_type
== PRINT_SYMBOL
)
145 handler_name
= "define_symbolic_value";
149 Py_FatalError("couldn't create Python tuple");
151 value
= eval_flag(field_value
);
153 PyTuple_SetItem(t
, n
++, PyString_FromString(ev_name
));
154 PyTuple_SetItem(t
, n
++, PyString_FromString(field_name
));
155 PyTuple_SetItem(t
, n
++, PyInt_FromLong(value
));
156 PyTuple_SetItem(t
, n
++, PyString_FromString(field_str
));
158 try_call_object(handler_name
, t
);
163 static void define_values(enum print_arg_type field_type
,
164 struct print_flag_sym
*field
,
166 const char *field_name
)
168 define_value(field_type
, ev_name
, field_name
, field
->value
,
172 define_values(field_type
, field
->next
, ev_name
, field_name
);
175 static void define_field(enum print_arg_type field_type
,
177 const char *field_name
,
180 const char *handler_name
= "define_flag_field";
184 if (field_type
== PRINT_SYMBOL
)
185 handler_name
= "define_symbolic_field";
187 if (field_type
== PRINT_FLAGS
)
192 Py_FatalError("couldn't create Python tuple");
194 PyTuple_SetItem(t
, n
++, PyString_FromString(ev_name
));
195 PyTuple_SetItem(t
, n
++, PyString_FromString(field_name
));
196 if (field_type
== PRINT_FLAGS
)
197 PyTuple_SetItem(t
, n
++, PyString_FromString(delim
));
199 try_call_object(handler_name
, t
);
204 static void define_event_symbols(struct event_format
*event
,
206 struct print_arg
*args
)
208 switch (args
->type
) {
212 define_value(PRINT_FLAGS
, ev_name
, cur_field_name
, "0",
217 free(cur_field_name
);
218 cur_field_name
= strdup(args
->field
.name
);
221 define_event_symbols(event
, ev_name
, args
->flags
.field
);
222 define_field(PRINT_FLAGS
, ev_name
, cur_field_name
,
224 define_values(PRINT_FLAGS
, args
->flags
.flags
, ev_name
,
228 define_event_symbols(event
, ev_name
, args
->symbol
.field
);
229 define_field(PRINT_SYMBOL
, ev_name
, cur_field_name
, NULL
);
230 define_values(PRINT_SYMBOL
, args
->symbol
.symbols
, ev_name
,
234 define_event_symbols(event
, ev_name
, args
->hex
.field
);
235 define_event_symbols(event
, ev_name
, args
->hex
.size
);
237 case PRINT_INT_ARRAY
:
238 define_event_symbols(event
, ev_name
, args
->int_array
.field
);
239 define_event_symbols(event
, ev_name
, args
->int_array
.count
);
240 define_event_symbols(event
, ev_name
, args
->int_array
.el_size
);
245 define_event_symbols(event
, ev_name
, args
->typecast
.item
);
248 if (strcmp(args
->op
.op
, ":") == 0)
250 define_event_symbols(event
, ev_name
, args
->op
.left
);
251 define_event_symbols(event
, ev_name
, args
->op
.right
);
254 /* gcc warns for these? */
256 case PRINT_DYNAMIC_ARRAY
:
257 case PRINT_DYNAMIC_ARRAY_LEN
:
260 /* we should warn... */
265 define_event_symbols(event
, ev_name
, args
->next
);
268 static PyObject
*get_field_numeric_entry(struct event_format
*event
,
269 struct format_field
*field
, void *data
)
271 bool is_array
= field
->flags
& FIELD_IS_ARRAY
;
272 PyObject
*obj
, *list
= NULL
;
273 unsigned long long val
;
274 unsigned int item_size
, n_items
, i
;
277 list
= PyList_New(field
->arraylen
);
278 item_size
= field
->size
/ field
->arraylen
;
279 n_items
= field
->arraylen
;
281 item_size
= field
->size
;
285 for (i
= 0; i
< n_items
; i
++) {
287 val
= read_size(event
, data
+ field
->offset
+ i
* item_size
,
289 if (field
->flags
& FIELD_IS_SIGNED
) {
290 if ((long long)val
>= LONG_MIN
&&
291 (long long)val
<= LONG_MAX
)
292 obj
= PyInt_FromLong(val
);
294 obj
= PyLong_FromLongLong(val
);
297 obj
= PyInt_FromLong(val
);
299 obj
= PyLong_FromUnsignedLongLong(val
);
302 PyList_SET_ITEM(list
, i
, obj
);
310 static PyObject
*python_process_callchain(struct perf_sample
*sample
,
311 struct perf_evsel
*evsel
,
312 struct addr_location
*al
)
316 pylist
= PyList_New(0);
318 Py_FatalError("couldn't create Python list");
320 if (!symbol_conf
.use_callchain
|| !sample
->callchain
)
323 if (thread__resolve_callchain(al
->thread
, evsel
,
325 scripting_max_stack
) != 0) {
326 pr_err("Failed to resolve callchain. Skipping\n");
329 callchain_cursor_commit(&callchain_cursor
);
334 struct callchain_cursor_node
*node
;
335 node
= callchain_cursor_current(&callchain_cursor
);
339 pyelem
= PyDict_New();
341 Py_FatalError("couldn't create Python dictionary");
344 pydict_set_item_string_decref(pyelem
, "ip",
345 PyLong_FromUnsignedLongLong(node
->ip
));
348 PyObject
*pysym
= PyDict_New();
350 Py_FatalError("couldn't create Python dictionary");
351 pydict_set_item_string_decref(pysym
, "start",
352 PyLong_FromUnsignedLongLong(node
->sym
->start
));
353 pydict_set_item_string_decref(pysym
, "end",
354 PyLong_FromUnsignedLongLong(node
->sym
->end
));
355 pydict_set_item_string_decref(pysym
, "binding",
356 PyInt_FromLong(node
->sym
->binding
));
357 pydict_set_item_string_decref(pysym
, "name",
358 PyString_FromStringAndSize(node
->sym
->name
,
359 node
->sym
->namelen
));
360 pydict_set_item_string_decref(pyelem
, "sym", pysym
);
364 struct map
*map
= node
->map
;
365 const char *dsoname
= "[unknown]";
366 if (map
&& map
->dso
&& (map
->dso
->name
|| map
->dso
->long_name
)) {
367 if (symbol_conf
.show_kernel_path
&& map
->dso
->long_name
)
368 dsoname
= map
->dso
->long_name
;
369 else if (map
->dso
->name
)
370 dsoname
= map
->dso
->name
;
372 pydict_set_item_string_decref(pyelem
, "dso",
373 PyString_FromString(dsoname
));
376 callchain_cursor_advance(&callchain_cursor
);
377 PyList_Append(pylist
, pyelem
);
386 static void python_process_tracepoint(struct perf_sample
*sample
,
387 struct perf_evsel
*evsel
,
388 struct addr_location
*al
)
390 struct event_format
*event
= evsel
->tp_format
;
391 PyObject
*handler
, *context
, *t
, *obj
, *callchain
;
392 PyObject
*dict
= NULL
;
393 static char handler_name
[256];
394 struct format_field
*field
;
398 int cpu
= sample
->cpu
;
399 void *data
= sample
->raw_data
;
400 unsigned long long nsecs
= sample
->time
;
401 const char *comm
= thread__comm_str(al
->thread
);
403 t
= PyTuple_New(MAX_FIELDS
);
405 Py_FatalError("couldn't create Python tuple");
408 die("ug! no event found for type %d", (int)evsel
->attr
.config
);
410 pid
= raw_field_value(event
, "common_pid", data
);
412 sprintf(handler_name
, "%s__%s", event
->system
, event
->name
);
414 if (!test_and_set_bit(event
->id
, events_defined
))
415 define_event_symbols(event
, handler_name
, event
->print_fmt
.args
);
417 handler
= get_handler(handler_name
);
421 Py_FatalError("couldn't create Python dict");
423 s
= nsecs
/ NSECS_PER_SEC
;
424 ns
= nsecs
- s
* NSECS_PER_SEC
;
426 scripting_context
->event_data
= data
;
427 scripting_context
->pevent
= evsel
->tp_format
->pevent
;
429 context
= PyCObject_FromVoidPtr(scripting_context
, NULL
);
431 PyTuple_SetItem(t
, n
++, PyString_FromString(handler_name
));
432 PyTuple_SetItem(t
, n
++, context
);
435 callchain
= python_process_callchain(sample
, evsel
, al
);
438 PyTuple_SetItem(t
, n
++, PyInt_FromLong(cpu
));
439 PyTuple_SetItem(t
, n
++, PyInt_FromLong(s
));
440 PyTuple_SetItem(t
, n
++, PyInt_FromLong(ns
));
441 PyTuple_SetItem(t
, n
++, PyInt_FromLong(pid
));
442 PyTuple_SetItem(t
, n
++, PyString_FromString(comm
));
443 PyTuple_SetItem(t
, n
++, callchain
);
445 pydict_set_item_string_decref(dict
, "common_cpu", PyInt_FromLong(cpu
));
446 pydict_set_item_string_decref(dict
, "common_s", PyInt_FromLong(s
));
447 pydict_set_item_string_decref(dict
, "common_ns", PyInt_FromLong(ns
));
448 pydict_set_item_string_decref(dict
, "common_pid", PyInt_FromLong(pid
));
449 pydict_set_item_string_decref(dict
, "common_comm", PyString_FromString(comm
));
450 pydict_set_item_string_decref(dict
, "common_callchain", callchain
);
452 for (field
= event
->format
.fields
; field
; field
= field
->next
) {
453 if (field
->flags
& FIELD_IS_STRING
) {
455 if (field
->flags
& FIELD_IS_DYNAMIC
) {
456 offset
= *(int *)(data
+ field
->offset
);
459 offset
= field
->offset
;
460 obj
= PyString_FromString((char *)data
+ offset
);
461 } else { /* FIELD_IS_NUMERIC */
462 obj
= get_field_numeric_entry(event
, field
, data
);
465 PyTuple_SetItem(t
, n
++, obj
);
467 pydict_set_item_string_decref(dict
, field
->name
, obj
);
472 PyTuple_SetItem(t
, n
++, dict
);
474 if (_PyTuple_Resize(&t
, n
) == -1)
475 Py_FatalError("error resizing Python tuple");
478 call_object(handler
, t
, handler_name
);
480 try_call_object("trace_unhandled", t
);
487 static PyObject
*tuple_new(unsigned int sz
)
493 Py_FatalError("couldn't create Python tuple");
497 static int tuple_set_u64(PyObject
*t
, unsigned int pos
, u64 val
)
499 #if BITS_PER_LONG == 64
500 return PyTuple_SetItem(t
, pos
, PyInt_FromLong(val
));
502 #if BITS_PER_LONG == 32
503 return PyTuple_SetItem(t
, pos
, PyLong_FromLongLong(val
));
507 static int tuple_set_s32(PyObject
*t
, unsigned int pos
, s32 val
)
509 return PyTuple_SetItem(t
, pos
, PyInt_FromLong(val
));
512 static int tuple_set_string(PyObject
*t
, unsigned int pos
, const char *s
)
514 return PyTuple_SetItem(t
, pos
, PyString_FromString(s
));
517 static int python_export_evsel(struct db_export
*dbe
, struct perf_evsel
*evsel
)
519 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
524 tuple_set_u64(t
, 0, evsel
->db_id
);
525 tuple_set_string(t
, 1, perf_evsel__name(evsel
));
527 call_object(tables
->evsel_handler
, t
, "evsel_table");
534 static int python_export_machine(struct db_export
*dbe
,
535 struct machine
*machine
)
537 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
542 tuple_set_u64(t
, 0, machine
->db_id
);
543 tuple_set_s32(t
, 1, machine
->pid
);
544 tuple_set_string(t
, 2, machine
->root_dir
? machine
->root_dir
: "");
546 call_object(tables
->machine_handler
, t
, "machine_table");
553 static int python_export_thread(struct db_export
*dbe
, struct thread
*thread
,
554 u64 main_thread_db_id
, struct machine
*machine
)
556 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
561 tuple_set_u64(t
, 0, thread
->db_id
);
562 tuple_set_u64(t
, 1, machine
->db_id
);
563 tuple_set_u64(t
, 2, main_thread_db_id
);
564 tuple_set_s32(t
, 3, thread
->pid_
);
565 tuple_set_s32(t
, 4, thread
->tid
);
567 call_object(tables
->thread_handler
, t
, "thread_table");
574 static int python_export_comm(struct db_export
*dbe
, struct comm
*comm
)
576 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
581 tuple_set_u64(t
, 0, comm
->db_id
);
582 tuple_set_string(t
, 1, comm__str(comm
));
584 call_object(tables
->comm_handler
, t
, "comm_table");
591 static int python_export_comm_thread(struct db_export
*dbe
, u64 db_id
,
592 struct comm
*comm
, struct thread
*thread
)
594 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
599 tuple_set_u64(t
, 0, db_id
);
600 tuple_set_u64(t
, 1, comm
->db_id
);
601 tuple_set_u64(t
, 2, thread
->db_id
);
603 call_object(tables
->comm_thread_handler
, t
, "comm_thread_table");
610 static int python_export_dso(struct db_export
*dbe
, struct dso
*dso
,
611 struct machine
*machine
)
613 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
614 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
617 build_id__sprintf(dso
->build_id
, sizeof(dso
->build_id
), sbuild_id
);
621 tuple_set_u64(t
, 0, dso
->db_id
);
622 tuple_set_u64(t
, 1, machine
->db_id
);
623 tuple_set_string(t
, 2, dso
->short_name
);
624 tuple_set_string(t
, 3, dso
->long_name
);
625 tuple_set_string(t
, 4, sbuild_id
);
627 call_object(tables
->dso_handler
, t
, "dso_table");
634 static int python_export_symbol(struct db_export
*dbe
, struct symbol
*sym
,
637 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
638 u64
*sym_db_id
= symbol__priv(sym
);
643 tuple_set_u64(t
, 0, *sym_db_id
);
644 tuple_set_u64(t
, 1, dso
->db_id
);
645 tuple_set_u64(t
, 2, sym
->start
);
646 tuple_set_u64(t
, 3, sym
->end
);
647 tuple_set_s32(t
, 4, sym
->binding
);
648 tuple_set_string(t
, 5, sym
->name
);
650 call_object(tables
->symbol_handler
, t
, "symbol_table");
657 static int python_export_branch_type(struct db_export
*dbe
, u32 branch_type
,
660 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
665 tuple_set_s32(t
, 0, branch_type
);
666 tuple_set_string(t
, 1, name
);
668 call_object(tables
->branch_type_handler
, t
, "branch_type_table");
675 static int python_export_sample(struct db_export
*dbe
,
676 struct export_sample
*es
)
678 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
683 tuple_set_u64(t
, 0, es
->db_id
);
684 tuple_set_u64(t
, 1, es
->evsel
->db_id
);
685 tuple_set_u64(t
, 2, es
->al
->machine
->db_id
);
686 tuple_set_u64(t
, 3, es
->al
->thread
->db_id
);
687 tuple_set_u64(t
, 4, es
->comm_db_id
);
688 tuple_set_u64(t
, 5, es
->dso_db_id
);
689 tuple_set_u64(t
, 6, es
->sym_db_id
);
690 tuple_set_u64(t
, 7, es
->offset
);
691 tuple_set_u64(t
, 8, es
->sample
->ip
);
692 tuple_set_u64(t
, 9, es
->sample
->time
);
693 tuple_set_s32(t
, 10, es
->sample
->cpu
);
694 tuple_set_u64(t
, 11, es
->addr_dso_db_id
);
695 tuple_set_u64(t
, 12, es
->addr_sym_db_id
);
696 tuple_set_u64(t
, 13, es
->addr_offset
);
697 tuple_set_u64(t
, 14, es
->sample
->addr
);
698 tuple_set_u64(t
, 15, es
->sample
->period
);
699 tuple_set_u64(t
, 16, es
->sample
->weight
);
700 tuple_set_u64(t
, 17, es
->sample
->transaction
);
701 tuple_set_u64(t
, 18, es
->sample
->data_src
);
702 tuple_set_s32(t
, 19, es
->sample
->flags
& PERF_BRANCH_MASK
);
703 tuple_set_s32(t
, 20, !!(es
->sample
->flags
& PERF_IP_FLAG_IN_TX
));
705 call_object(tables
->sample_handler
, t
, "sample_table");
712 static int python_export_call_path(struct db_export
*dbe
, struct call_path
*cp
)
714 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
716 u64 parent_db_id
, sym_db_id
;
718 parent_db_id
= cp
->parent
? cp
->parent
->db_id
: 0;
719 sym_db_id
= cp
->sym
? *(u64
*)symbol__priv(cp
->sym
) : 0;
723 tuple_set_u64(t
, 0, cp
->db_id
);
724 tuple_set_u64(t
, 1, parent_db_id
);
725 tuple_set_u64(t
, 2, sym_db_id
);
726 tuple_set_u64(t
, 3, cp
->ip
);
728 call_object(tables
->call_path_handler
, t
, "call_path_table");
735 static int python_export_call_return(struct db_export
*dbe
,
736 struct call_return
*cr
)
738 struct tables
*tables
= container_of(dbe
, struct tables
, dbe
);
739 u64 comm_db_id
= cr
->comm
? cr
->comm
->db_id
: 0;
744 tuple_set_u64(t
, 0, cr
->db_id
);
745 tuple_set_u64(t
, 1, cr
->thread
->db_id
);
746 tuple_set_u64(t
, 2, comm_db_id
);
747 tuple_set_u64(t
, 3, cr
->cp
->db_id
);
748 tuple_set_u64(t
, 4, cr
->call_time
);
749 tuple_set_u64(t
, 5, cr
->return_time
);
750 tuple_set_u64(t
, 6, cr
->branch_count
);
751 tuple_set_u64(t
, 7, cr
->call_ref
);
752 tuple_set_u64(t
, 8, cr
->return_ref
);
753 tuple_set_u64(t
, 9, cr
->cp
->parent
->db_id
);
754 tuple_set_s32(t
, 10, cr
->flags
);
756 call_object(tables
->call_return_handler
, t
, "call_return_table");
763 static int python_process_call_return(struct call_return
*cr
, void *data
)
765 struct db_export
*dbe
= data
;
767 return db_export__call_return(dbe
, cr
);
770 static void python_process_general_event(struct perf_sample
*sample
,
771 struct perf_evsel
*evsel
,
772 struct addr_location
*al
)
774 PyObject
*handler
, *t
, *dict
, *callchain
, *dict_sample
;
775 static char handler_name
[64];
779 * Use the MAX_FIELDS to make the function expandable, though
780 * currently there is only one item for the tuple.
782 t
= PyTuple_New(MAX_FIELDS
);
784 Py_FatalError("couldn't create Python tuple");
788 Py_FatalError("couldn't create Python dictionary");
790 dict_sample
= PyDict_New();
792 Py_FatalError("couldn't create Python dictionary");
794 snprintf(handler_name
, sizeof(handler_name
), "%s", "process_event");
796 handler
= get_handler(handler_name
);
800 pydict_set_item_string_decref(dict
, "ev_name", PyString_FromString(perf_evsel__name(evsel
)));
801 pydict_set_item_string_decref(dict
, "attr", PyString_FromStringAndSize(
802 (const char *)&evsel
->attr
, sizeof(evsel
->attr
)));
804 pydict_set_item_string_decref(dict_sample
, "pid",
805 PyInt_FromLong(sample
->pid
));
806 pydict_set_item_string_decref(dict_sample
, "tid",
807 PyInt_FromLong(sample
->tid
));
808 pydict_set_item_string_decref(dict_sample
, "cpu",
809 PyInt_FromLong(sample
->cpu
));
810 pydict_set_item_string_decref(dict_sample
, "ip",
811 PyLong_FromUnsignedLongLong(sample
->ip
));
812 pydict_set_item_string_decref(dict_sample
, "time",
813 PyLong_FromUnsignedLongLong(sample
->time
));
814 pydict_set_item_string_decref(dict_sample
, "period",
815 PyLong_FromUnsignedLongLong(sample
->period
));
816 pydict_set_item_string_decref(dict
, "sample", dict_sample
);
818 pydict_set_item_string_decref(dict
, "raw_buf", PyString_FromStringAndSize(
819 (const char *)sample
->raw_data
, sample
->raw_size
));
820 pydict_set_item_string_decref(dict
, "comm",
821 PyString_FromString(thread__comm_str(al
->thread
)));
823 pydict_set_item_string_decref(dict
, "dso",
824 PyString_FromString(al
->map
->dso
->name
));
827 pydict_set_item_string_decref(dict
, "symbol",
828 PyString_FromString(al
->sym
->name
));
832 callchain
= python_process_callchain(sample
, evsel
, al
);
833 pydict_set_item_string_decref(dict
, "callchain", callchain
);
835 PyTuple_SetItem(t
, n
++, dict
);
836 if (_PyTuple_Resize(&t
, n
) == -1)
837 Py_FatalError("error resizing Python tuple");
839 call_object(handler
, t
, handler_name
);
845 static void python_process_event(union perf_event
*event
,
846 struct perf_sample
*sample
,
847 struct perf_evsel
*evsel
,
848 struct addr_location
*al
)
850 struct tables
*tables
= &tables_global
;
852 switch (evsel
->attr
.type
) {
853 case PERF_TYPE_TRACEPOINT
:
854 python_process_tracepoint(sample
, evsel
, al
);
856 /* Reserve for future process_hw/sw/raw APIs */
858 if (tables
->db_export_mode
)
859 db_export__sample(&tables
->dbe
, event
, sample
, evsel
, al
);
861 python_process_general_event(sample
, evsel
, al
);
865 static void get_handler_name(char *str
, size_t size
,
866 struct perf_evsel
*evsel
)
870 scnprintf(str
, size
, "stat__%s", perf_evsel__name(evsel
));
872 while ((p
= strchr(p
, ':'))) {
879 process_stat(struct perf_evsel
*counter
, int cpu
, int thread
, u64 tstamp
,
880 struct perf_counts_values
*count
)
882 PyObject
*handler
, *t
;
883 static char handler_name
[256];
886 t
= PyTuple_New(MAX_FIELDS
);
888 Py_FatalError("couldn't create Python tuple");
890 get_handler_name(handler_name
, sizeof(handler_name
),
893 handler
= get_handler(handler_name
);
895 pr_debug("can't find python handler %s\n", handler_name
);
899 PyTuple_SetItem(t
, n
++, PyInt_FromLong(cpu
));
900 PyTuple_SetItem(t
, n
++, PyInt_FromLong(thread
));
902 tuple_set_u64(t
, n
++, tstamp
);
903 tuple_set_u64(t
, n
++, count
->val
);
904 tuple_set_u64(t
, n
++, count
->ena
);
905 tuple_set_u64(t
, n
++, count
->run
);
907 if (_PyTuple_Resize(&t
, n
) == -1)
908 Py_FatalError("error resizing Python tuple");
910 call_object(handler
, t
, handler_name
);
915 static void python_process_stat(struct perf_stat_config
*config
,
916 struct perf_evsel
*counter
, u64 tstamp
)
918 struct thread_map
*threads
= counter
->threads
;
919 struct cpu_map
*cpus
= counter
->cpus
;
922 if (config
->aggr_mode
== AGGR_GLOBAL
) {
923 process_stat(counter
, -1, -1, tstamp
,
924 &counter
->counts
->aggr
);
928 for (thread
= 0; thread
< threads
->nr
; thread
++) {
929 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
930 process_stat(counter
, cpus
->map
[cpu
],
931 thread_map__pid(threads
, thread
), tstamp
,
932 perf_counts(counter
->counts
, cpu
, thread
));
937 static void python_process_stat_interval(u64 tstamp
)
939 PyObject
*handler
, *t
;
940 static const char handler_name
[] = "stat__interval";
943 t
= PyTuple_New(MAX_FIELDS
);
945 Py_FatalError("couldn't create Python tuple");
947 handler
= get_handler(handler_name
);
949 pr_debug("can't find python handler %s\n", handler_name
);
953 tuple_set_u64(t
, n
++, tstamp
);
955 if (_PyTuple_Resize(&t
, n
) == -1)
956 Py_FatalError("error resizing Python tuple");
958 call_object(handler
, t
, handler_name
);
963 static int run_start_sub(void)
965 main_module
= PyImport_AddModule("__main__");
966 if (main_module
== NULL
)
968 Py_INCREF(main_module
);
970 main_dict
= PyModule_GetDict(main_module
);
971 if (main_dict
== NULL
)
973 Py_INCREF(main_dict
);
975 try_call_object("trace_begin", NULL
);
980 Py_XDECREF(main_dict
);
981 Py_XDECREF(main_module
);
985 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
986 tables->handler_name = get_handler(#table_name); \
987 if (tables->handler_name) \
988 tables->dbe.export_ ## name = python_export_ ## name; \
991 #define SET_TABLE_HANDLER(name) \
992 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
994 static void set_table_handlers(struct tables
*tables
)
996 const char *perf_db_export_mode
= "perf_db_export_mode";
997 const char *perf_db_export_calls
= "perf_db_export_calls";
998 PyObject
*db_export_mode
, *db_export_calls
;
999 bool export_calls
= false;
1002 memset(tables
, 0, sizeof(struct tables
));
1003 if (db_export__init(&tables
->dbe
))
1004 Py_FatalError("failed to initialize export");
1006 db_export_mode
= PyDict_GetItemString(main_dict
, perf_db_export_mode
);
1007 if (!db_export_mode
)
1010 ret
= PyObject_IsTrue(db_export_mode
);
1012 handler_call_die(perf_db_export_mode
);
1016 tables
->dbe
.crp
= NULL
;
1017 db_export_calls
= PyDict_GetItemString(main_dict
, perf_db_export_calls
);
1018 if (db_export_calls
) {
1019 ret
= PyObject_IsTrue(db_export_calls
);
1021 handler_call_die(perf_db_export_calls
);
1022 export_calls
= !!ret
;
1027 call_return_processor__new(python_process_call_return
,
1029 if (!tables
->dbe
.crp
)
1030 Py_FatalError("failed to create calls processor");
1033 tables
->db_export_mode
= true;
1035 * Reserve per symbol space for symbol->db_id via symbol__priv()
1037 symbol_conf
.priv_size
= sizeof(u64
);
1039 SET_TABLE_HANDLER(evsel
);
1040 SET_TABLE_HANDLER(machine
);
1041 SET_TABLE_HANDLER(thread
);
1042 SET_TABLE_HANDLER(comm
);
1043 SET_TABLE_HANDLER(comm_thread
);
1044 SET_TABLE_HANDLER(dso
);
1045 SET_TABLE_HANDLER(symbol
);
1046 SET_TABLE_HANDLER(branch_type
);
1047 SET_TABLE_HANDLER(sample
);
1048 SET_TABLE_HANDLER(call_path
);
1049 SET_TABLE_HANDLER(call_return
);
1053 * Start trace script
1055 static int python_start_script(const char *script
, int argc
, const char **argv
)
1057 struct tables
*tables
= &tables_global
;
1058 const char **command_line
;
1063 command_line
= malloc((argc
+ 1) * sizeof(const char *));
1064 command_line
[0] = script
;
1065 for (i
= 1; i
< argc
+ 1; i
++)
1066 command_line
[i
] = argv
[i
- 1];
1070 initperf_trace_context();
1072 PySys_SetArgv(argc
+ 1, (char **)command_line
);
1074 fp
= fopen(script
, "r");
1076 sprintf(buf
, "Can't open python script \"%s\"", script
);
1082 err
= PyRun_SimpleFile(fp
, script
);
1084 fprintf(stderr
, "Error running python script %s\n", script
);
1088 err
= run_start_sub();
1090 fprintf(stderr
, "Error starting python script %s\n", script
);
1096 set_table_handlers(tables
);
1098 if (tables
->db_export_mode
) {
1099 err
= db_export__branch_types(&tables
->dbe
);
1112 static int python_flush_script(void)
1114 struct tables
*tables
= &tables_global
;
1116 return db_export__flush(&tables
->dbe
);
1122 static int python_stop_script(void)
1124 struct tables
*tables
= &tables_global
;
1126 try_call_object("trace_end", NULL
);
1128 db_export__exit(&tables
->dbe
);
1130 Py_XDECREF(main_dict
);
1131 Py_XDECREF(main_module
);
1137 static int python_generate_script(struct pevent
*pevent
, const char *outfile
)
1139 struct event_format
*event
= NULL
;
1140 struct format_field
*f
;
1141 char fname
[PATH_MAX
];
1142 int not_first
, count
;
1145 sprintf(fname
, "%s.py", outfile
);
1146 ofp
= fopen(fname
, "w");
1148 fprintf(stderr
, "couldn't open %s\n", fname
);
1151 fprintf(ofp
, "# perf script event handlers, "
1152 "generated by perf script -g python\n");
1154 fprintf(ofp
, "# Licensed under the terms of the GNU GPL"
1155 " License version 2\n\n");
1157 fprintf(ofp
, "# The common_* event handler fields are the most useful "
1158 "fields common to\n");
1160 fprintf(ofp
, "# all events. They don't necessarily correspond to "
1161 "the 'common_*' fields\n");
1163 fprintf(ofp
, "# in the format files. Those fields not available as "
1164 "handler params can\n");
1166 fprintf(ofp
, "# be retrieved using Python functions of the form "
1167 "common_*(context).\n");
1169 fprintf(ofp
, "# See the perf-trace-python Documentation for the list "
1170 "of available functions.\n\n");
1172 fprintf(ofp
, "import os\n");
1173 fprintf(ofp
, "import sys\n\n");
1175 fprintf(ofp
, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1176 fprintf(ofp
, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1177 fprintf(ofp
, "\nfrom perf_trace_context import *\n");
1178 fprintf(ofp
, "from Core import *\n\n\n");
1180 fprintf(ofp
, "def trace_begin():\n");
1181 fprintf(ofp
, "\tprint \"in trace_begin\"\n\n");
1183 fprintf(ofp
, "def trace_end():\n");
1184 fprintf(ofp
, "\tprint \"in trace_end\"\n\n");
1186 while ((event
= trace_find_next_event(pevent
, event
))) {
1187 fprintf(ofp
, "def %s__%s(", event
->system
, event
->name
);
1188 fprintf(ofp
, "event_name, ");
1189 fprintf(ofp
, "context, ");
1190 fprintf(ofp
, "common_cpu,\n");
1191 fprintf(ofp
, "\tcommon_secs, ");
1192 fprintf(ofp
, "common_nsecs, ");
1193 fprintf(ofp
, "common_pid, ");
1194 fprintf(ofp
, "common_comm,\n\t");
1195 fprintf(ofp
, "common_callchain, ");
1200 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
1203 if (++count
% 5 == 0)
1204 fprintf(ofp
, "\n\t");
1206 fprintf(ofp
, "%s", f
->name
);
1208 fprintf(ofp
, "):\n");
1210 fprintf(ofp
, "\t\tprint_header(event_name, common_cpu, "
1211 "common_secs, common_nsecs,\n\t\t\t"
1212 "common_pid, common_comm)\n\n");
1214 fprintf(ofp
, "\t\tprint \"");
1219 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
1222 if (count
&& count
% 3 == 0) {
1223 fprintf(ofp
, "\" \\\n\t\t\"");
1227 fprintf(ofp
, "%s=", f
->name
);
1228 if (f
->flags
& FIELD_IS_STRING
||
1229 f
->flags
& FIELD_IS_FLAG
||
1230 f
->flags
& FIELD_IS_ARRAY
||
1231 f
->flags
& FIELD_IS_SYMBOLIC
)
1232 fprintf(ofp
, "%%s");
1233 else if (f
->flags
& FIELD_IS_SIGNED
)
1234 fprintf(ofp
, "%%d");
1236 fprintf(ofp
, "%%u");
1239 fprintf(ofp
, "\" %% \\\n\t\t(");
1244 for (f
= event
->format
.fields
; f
; f
= f
->next
) {
1248 if (++count
% 5 == 0)
1249 fprintf(ofp
, "\n\t\t");
1251 if (f
->flags
& FIELD_IS_FLAG
) {
1252 if ((count
- 1) % 5 != 0) {
1253 fprintf(ofp
, "\n\t\t");
1256 fprintf(ofp
, "flag_str(\"");
1257 fprintf(ofp
, "%s__%s\", ", event
->system
,
1259 fprintf(ofp
, "\"%s\", %s)", f
->name
,
1261 } else if (f
->flags
& FIELD_IS_SYMBOLIC
) {
1262 if ((count
- 1) % 5 != 0) {
1263 fprintf(ofp
, "\n\t\t");
1266 fprintf(ofp
, "symbol_str(\"");
1267 fprintf(ofp
, "%s__%s\", ", event
->system
,
1269 fprintf(ofp
, "\"%s\", %s)", f
->name
,
1272 fprintf(ofp
, "%s", f
->name
);
1275 fprintf(ofp
, ")\n\n");
1277 fprintf(ofp
, "\t\tfor node in common_callchain:");
1278 fprintf(ofp
, "\n\t\t\tif 'sym' in node:");
1279 fprintf(ofp
, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1280 fprintf(ofp
, "\n\t\t\telse:");
1281 fprintf(ofp
, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1282 fprintf(ofp
, "\t\tprint \"\\n\"\n\n");
1286 fprintf(ofp
, "def trace_unhandled(event_name, context, "
1287 "event_fields_dict):\n");
1289 fprintf(ofp
, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1290 "for k,v in sorted(event_fields_dict.items())])\n\n");
1292 fprintf(ofp
, "def print_header("
1293 "event_name, cpu, secs, nsecs, pid, comm):\n"
1294 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1295 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1299 fprintf(stderr
, "generated Python script: %s\n", fname
);
1304 struct scripting_ops python_scripting_ops
= {
1306 .start_script
= python_start_script
,
1307 .flush_script
= python_flush_script
,
1308 .stop_script
= python_stop_script
,
1309 .process_event
= python_process_event
,
1310 .process_stat
= python_process_stat
,
1311 .process_stat_interval
= python_process_stat_interval
,
1312 .generate_script
= python_generate_script
,