2 * trace-event-python. Feed trace events to an embedded Python interpreter.
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 "../call-path.h"
45 #include "thread_map.h"
49 PyMODINIT_FUNC initperf_trace_context(void);
51 #define TRACE_EVENT_TYPE_MAX \
52 ((1 << (sizeof(unsigned short) * 8)) - 1)
54 static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
57 #define N_COMMON_FIELDS 7
59 extern struct scripting_context *scripting_context;
61 static char *cur_field_name;
62 static int zero_flag_atom;
64 static PyObject *main_module, *main_dict;
68 PyObject *evsel_handler;
69 PyObject *machine_handler;
70 PyObject *thread_handler;
71 PyObject *comm_handler;
72 PyObject *comm_thread_handler;
73 PyObject *dso_handler;
74 PyObject *symbol_handler;
75 PyObject *branch_type_handler;
76 PyObject *sample_handler;
77 PyObject *call_path_handler;
78 PyObject *call_return_handler;
82 static struct tables tables_global;
84 static void handler_call_die(const char *handler_name) NORETURN;
85 static void handler_call_die(const char *handler_name)
88 Py_FatalError("problem in Python trace event handler");
89 // Py_FatalError does not return
90 // but we have to make the compiler happy
95 * Insert val into into the dictionary and decrement the reference counter.
96 * This is necessary for dictionaries since PyDict_SetItemString() does not
97 * steal a reference, as opposed to PyTuple_SetItem().
99 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
101 PyDict_SetItemString(dict, key, val);
105 static PyObject *get_handler(const char *handler_name)
109 handler = PyDict_GetItemString(main_dict, handler_name);
110 if (handler && !PyCallable_Check(handler))
115 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
119 retval = PyObject_CallObject(handler, args);
121 handler_call_die(die_msg);
125 static void try_call_object(const char *handler_name, PyObject *args)
129 handler = get_handler(handler_name);
131 call_object(handler, args, handler_name);
134 static void define_value(enum print_arg_type field_type,
136 const char *field_name,
137 const char *field_value,
138 const char *field_str)
140 const char *handler_name = "define_flag_value";
142 unsigned long long value;
145 if (field_type == PRINT_SYMBOL)
146 handler_name = "define_symbolic_value";
150 Py_FatalError("couldn't create Python tuple");
152 value = eval_flag(field_value);
154 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
155 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
156 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
157 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
159 try_call_object(handler_name, t);
164 static void define_values(enum print_arg_type field_type,
165 struct print_flag_sym *field,
167 const char *field_name)
169 define_value(field_type, ev_name, field_name, field->value,
173 define_values(field_type, field->next, ev_name, field_name);
176 static void define_field(enum print_arg_type field_type,
178 const char *field_name,
181 const char *handler_name = "define_flag_field";
185 if (field_type == PRINT_SYMBOL)
186 handler_name = "define_symbolic_field";
188 if (field_type == PRINT_FLAGS)
193 Py_FatalError("couldn't create Python tuple");
195 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
196 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
197 if (field_type == PRINT_FLAGS)
198 PyTuple_SetItem(t, n++, PyString_FromString(delim));
200 try_call_object(handler_name, t);
205 static void define_event_symbols(struct event_format *event,
207 struct print_arg *args)
212 switch (args->type) {
216 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
221 free(cur_field_name);
222 cur_field_name = strdup(args->field.name);
225 define_event_symbols(event, ev_name, args->flags.field);
226 define_field(PRINT_FLAGS, ev_name, cur_field_name,
228 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
232 define_event_symbols(event, ev_name, args->symbol.field);
233 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
234 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
238 define_event_symbols(event, ev_name, args->hex.field);
239 define_event_symbols(event, ev_name, args->hex.size);
241 case PRINT_INT_ARRAY:
242 define_event_symbols(event, ev_name, args->int_array.field);
243 define_event_symbols(event, ev_name, args->int_array.count);
244 define_event_symbols(event, ev_name, args->int_array.el_size);
249 define_event_symbols(event, ev_name, args->typecast.item);
252 if (strcmp(args->op.op, ":") == 0)
254 define_event_symbols(event, ev_name, args->op.left);
255 define_event_symbols(event, ev_name, args->op.right);
258 /* gcc warns for these? */
260 case PRINT_DYNAMIC_ARRAY:
261 case PRINT_DYNAMIC_ARRAY_LEN:
264 /* we should warn... */
269 define_event_symbols(event, ev_name, args->next);
272 static PyObject *get_field_numeric_entry(struct event_format *event,
273 struct format_field *field, void *data)
275 bool is_array = field->flags & FIELD_IS_ARRAY;
276 PyObject *obj = NULL, *list = NULL;
277 unsigned long long val;
278 unsigned int item_size, n_items, i;
281 list = PyList_New(field->arraylen);
282 item_size = field->size / field->arraylen;
283 n_items = field->arraylen;
285 item_size = field->size;
289 for (i = 0; i < n_items; i++) {
291 val = read_size(event, data + field->offset + i * item_size,
293 if (field->flags & FIELD_IS_SIGNED) {
294 if ((long long)val >= LONG_MIN &&
295 (long long)val <= LONG_MAX)
296 obj = PyInt_FromLong(val);
298 obj = PyLong_FromLongLong(val);
301 obj = PyInt_FromLong(val);
303 obj = PyLong_FromUnsignedLongLong(val);
306 PyList_SET_ITEM(list, i, obj);
314 static PyObject *python_process_callchain(struct perf_sample *sample,
315 struct perf_evsel *evsel,
316 struct addr_location *al)
320 pylist = PyList_New(0);
322 Py_FatalError("couldn't create Python list");
324 if (!symbol_conf.use_callchain || !sample->callchain)
327 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
329 scripting_max_stack) != 0) {
330 pr_err("Failed to resolve callchain. Skipping\n");
333 callchain_cursor_commit(&callchain_cursor);
338 struct callchain_cursor_node *node;
339 node = callchain_cursor_current(&callchain_cursor);
343 pyelem = PyDict_New();
345 Py_FatalError("couldn't create Python dictionary");
348 pydict_set_item_string_decref(pyelem, "ip",
349 PyLong_FromUnsignedLongLong(node->ip));
352 PyObject *pysym = PyDict_New();
354 Py_FatalError("couldn't create Python dictionary");
355 pydict_set_item_string_decref(pysym, "start",
356 PyLong_FromUnsignedLongLong(node->sym->start));
357 pydict_set_item_string_decref(pysym, "end",
358 PyLong_FromUnsignedLongLong(node->sym->end));
359 pydict_set_item_string_decref(pysym, "binding",
360 PyInt_FromLong(node->sym->binding));
361 pydict_set_item_string_decref(pysym, "name",
362 PyString_FromStringAndSize(node->sym->name,
363 node->sym->namelen));
364 pydict_set_item_string_decref(pyelem, "sym", pysym);
368 struct map *map = node->map;
369 const char *dsoname = "[unknown]";
370 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
371 if (symbol_conf.show_kernel_path && map->dso->long_name)
372 dsoname = map->dso->long_name;
373 else if (map->dso->name)
374 dsoname = map->dso->name;
376 pydict_set_item_string_decref(pyelem, "dso",
377 PyString_FromString(dsoname));
380 callchain_cursor_advance(&callchain_cursor);
381 PyList_Append(pylist, pyelem);
389 static void python_process_tracepoint(struct perf_sample *sample,
390 struct perf_evsel *evsel,
391 struct addr_location *al)
393 struct event_format *event = evsel->tp_format;
394 PyObject *handler, *context, *t, *obj = NULL, *callchain;
395 PyObject *dict = NULL;
396 static char handler_name[256];
397 struct format_field *field;
401 int cpu = sample->cpu;
402 void *data = sample->raw_data;
403 unsigned long long nsecs = sample->time;
404 const char *comm = thread__comm_str(al->thread);
406 t = PyTuple_New(MAX_FIELDS);
408 Py_FatalError("couldn't create Python tuple");
411 snprintf(handler_name, sizeof(handler_name),
412 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
413 Py_FatalError(handler_name);
416 pid = raw_field_value(event, "common_pid", data);
418 sprintf(handler_name, "%s__%s", event->system, event->name);
420 if (!test_and_set_bit(event->id, events_defined))
421 define_event_symbols(event, handler_name, event->print_fmt.args);
423 handler = get_handler(handler_name);
427 Py_FatalError("couldn't create Python dict");
429 s = nsecs / NSECS_PER_SEC;
430 ns = nsecs - s * NSECS_PER_SEC;
432 scripting_context->event_data = data;
433 scripting_context->pevent = evsel->tp_format->pevent;
435 context = PyCObject_FromVoidPtr(scripting_context, NULL);
437 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
438 PyTuple_SetItem(t, n++, context);
441 callchain = python_process_callchain(sample, evsel, al);
444 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
445 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
446 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
447 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
448 PyTuple_SetItem(t, n++, PyString_FromString(comm));
449 PyTuple_SetItem(t, n++, callchain);
451 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
452 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
453 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
454 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
455 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
456 pydict_set_item_string_decref(dict, "common_callchain", callchain);
458 for (field = event->format.fields; field; field = field->next) {
459 unsigned int offset, len;
460 unsigned long long val;
462 if (field->flags & FIELD_IS_ARRAY) {
463 offset = field->offset;
465 if (field->flags & FIELD_IS_DYNAMIC) {
466 val = pevent_read_number(scripting_context->pevent,
472 if (field->flags & FIELD_IS_STRING &&
473 is_printable_array(data + offset, len)) {
474 obj = PyString_FromString((char *) data + offset);
476 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
477 field->flags &= ~FIELD_IS_STRING;
479 } else { /* FIELD_IS_NUMERIC */
480 obj = get_field_numeric_entry(event, field, data);
483 PyTuple_SetItem(t, n++, obj);
485 pydict_set_item_string_decref(dict, field->name, obj);
490 PyTuple_SetItem(t, n++, dict);
492 if (_PyTuple_Resize(&t, n) == -1)
493 Py_FatalError("error resizing Python tuple");
496 call_object(handler, t, handler_name);
498 try_call_object("trace_unhandled", t);
505 static PyObject *tuple_new(unsigned int sz)
511 Py_FatalError("couldn't create Python tuple");
515 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
517 #if BITS_PER_LONG == 64
518 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
520 #if BITS_PER_LONG == 32
521 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
525 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
527 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
530 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
532 return PyTuple_SetItem(t, pos, PyString_FromString(s));
535 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
537 struct tables *tables = container_of(dbe, struct tables, dbe);
542 tuple_set_u64(t, 0, evsel->db_id);
543 tuple_set_string(t, 1, perf_evsel__name(evsel));
545 call_object(tables->evsel_handler, t, "evsel_table");
552 static int python_export_machine(struct db_export *dbe,
553 struct machine *machine)
555 struct tables *tables = container_of(dbe, struct tables, dbe);
560 tuple_set_u64(t, 0, machine->db_id);
561 tuple_set_s32(t, 1, machine->pid);
562 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
564 call_object(tables->machine_handler, t, "machine_table");
571 static int python_export_thread(struct db_export *dbe, struct thread *thread,
572 u64 main_thread_db_id, struct machine *machine)
574 struct tables *tables = container_of(dbe, struct tables, dbe);
579 tuple_set_u64(t, 0, thread->db_id);
580 tuple_set_u64(t, 1, machine->db_id);
581 tuple_set_u64(t, 2, main_thread_db_id);
582 tuple_set_s32(t, 3, thread->pid_);
583 tuple_set_s32(t, 4, thread->tid);
585 call_object(tables->thread_handler, t, "thread_table");
592 static int python_export_comm(struct db_export *dbe, struct comm *comm)
594 struct tables *tables = container_of(dbe, struct tables, dbe);
599 tuple_set_u64(t, 0, comm->db_id);
600 tuple_set_string(t, 1, comm__str(comm));
602 call_object(tables->comm_handler, t, "comm_table");
609 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
610 struct comm *comm, struct thread *thread)
612 struct tables *tables = container_of(dbe, struct tables, dbe);
617 tuple_set_u64(t, 0, db_id);
618 tuple_set_u64(t, 1, comm->db_id);
619 tuple_set_u64(t, 2, thread->db_id);
621 call_object(tables->comm_thread_handler, t, "comm_thread_table");
628 static int python_export_dso(struct db_export *dbe, struct dso *dso,
629 struct machine *machine)
631 struct tables *tables = container_of(dbe, struct tables, dbe);
632 char sbuild_id[SBUILD_ID_SIZE];
635 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
639 tuple_set_u64(t, 0, dso->db_id);
640 tuple_set_u64(t, 1, machine->db_id);
641 tuple_set_string(t, 2, dso->short_name);
642 tuple_set_string(t, 3, dso->long_name);
643 tuple_set_string(t, 4, sbuild_id);
645 call_object(tables->dso_handler, t, "dso_table");
652 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
655 struct tables *tables = container_of(dbe, struct tables, dbe);
656 u64 *sym_db_id = symbol__priv(sym);
661 tuple_set_u64(t, 0, *sym_db_id);
662 tuple_set_u64(t, 1, dso->db_id);
663 tuple_set_u64(t, 2, sym->start);
664 tuple_set_u64(t, 3, sym->end);
665 tuple_set_s32(t, 4, sym->binding);
666 tuple_set_string(t, 5, sym->name);
668 call_object(tables->symbol_handler, t, "symbol_table");
675 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
678 struct tables *tables = container_of(dbe, struct tables, dbe);
683 tuple_set_s32(t, 0, branch_type);
684 tuple_set_string(t, 1, name);
686 call_object(tables->branch_type_handler, t, "branch_type_table");
693 static int python_export_sample(struct db_export *dbe,
694 struct export_sample *es)
696 struct tables *tables = container_of(dbe, struct tables, dbe);
701 tuple_set_u64(t, 0, es->db_id);
702 tuple_set_u64(t, 1, es->evsel->db_id);
703 tuple_set_u64(t, 2, es->al->machine->db_id);
704 tuple_set_u64(t, 3, es->al->thread->db_id);
705 tuple_set_u64(t, 4, es->comm_db_id);
706 tuple_set_u64(t, 5, es->dso_db_id);
707 tuple_set_u64(t, 6, es->sym_db_id);
708 tuple_set_u64(t, 7, es->offset);
709 tuple_set_u64(t, 8, es->sample->ip);
710 tuple_set_u64(t, 9, es->sample->time);
711 tuple_set_s32(t, 10, es->sample->cpu);
712 tuple_set_u64(t, 11, es->addr_dso_db_id);
713 tuple_set_u64(t, 12, es->addr_sym_db_id);
714 tuple_set_u64(t, 13, es->addr_offset);
715 tuple_set_u64(t, 14, es->sample->addr);
716 tuple_set_u64(t, 15, es->sample->period);
717 tuple_set_u64(t, 16, es->sample->weight);
718 tuple_set_u64(t, 17, es->sample->transaction);
719 tuple_set_u64(t, 18, es->sample->data_src);
720 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
721 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
722 tuple_set_u64(t, 21, es->call_path_id);
724 call_object(tables->sample_handler, t, "sample_table");
731 static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
733 struct tables *tables = container_of(dbe, struct tables, dbe);
735 u64 parent_db_id, sym_db_id;
737 parent_db_id = cp->parent ? cp->parent->db_id : 0;
738 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
742 tuple_set_u64(t, 0, cp->db_id);
743 tuple_set_u64(t, 1, parent_db_id);
744 tuple_set_u64(t, 2, sym_db_id);
745 tuple_set_u64(t, 3, cp->ip);
747 call_object(tables->call_path_handler, t, "call_path_table");
754 static int python_export_call_return(struct db_export *dbe,
755 struct call_return *cr)
757 struct tables *tables = container_of(dbe, struct tables, dbe);
758 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
763 tuple_set_u64(t, 0, cr->db_id);
764 tuple_set_u64(t, 1, cr->thread->db_id);
765 tuple_set_u64(t, 2, comm_db_id);
766 tuple_set_u64(t, 3, cr->cp->db_id);
767 tuple_set_u64(t, 4, cr->call_time);
768 tuple_set_u64(t, 5, cr->return_time);
769 tuple_set_u64(t, 6, cr->branch_count);
770 tuple_set_u64(t, 7, cr->call_ref);
771 tuple_set_u64(t, 8, cr->return_ref);
772 tuple_set_u64(t, 9, cr->cp->parent->db_id);
773 tuple_set_s32(t, 10, cr->flags);
775 call_object(tables->call_return_handler, t, "call_return_table");
782 static int python_process_call_return(struct call_return *cr, void *data)
784 struct db_export *dbe = data;
786 return db_export__call_return(dbe, cr);
789 static void python_process_general_event(struct perf_sample *sample,
790 struct perf_evsel *evsel,
791 struct addr_location *al)
793 PyObject *handler, *t, *dict, *callchain, *dict_sample;
794 static char handler_name[64];
798 * Use the MAX_FIELDS to make the function expandable, though
799 * currently there is only one item for the tuple.
801 t = PyTuple_New(MAX_FIELDS);
803 Py_FatalError("couldn't create Python tuple");
807 Py_FatalError("couldn't create Python dictionary");
809 dict_sample = PyDict_New();
811 Py_FatalError("couldn't create Python dictionary");
813 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
815 handler = get_handler(handler_name);
819 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
820 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
821 (const char *)&evsel->attr, sizeof(evsel->attr)));
823 pydict_set_item_string_decref(dict_sample, "pid",
824 PyInt_FromLong(sample->pid));
825 pydict_set_item_string_decref(dict_sample, "tid",
826 PyInt_FromLong(sample->tid));
827 pydict_set_item_string_decref(dict_sample, "cpu",
828 PyInt_FromLong(sample->cpu));
829 pydict_set_item_string_decref(dict_sample, "ip",
830 PyLong_FromUnsignedLongLong(sample->ip));
831 pydict_set_item_string_decref(dict_sample, "time",
832 PyLong_FromUnsignedLongLong(sample->time));
833 pydict_set_item_string_decref(dict_sample, "period",
834 PyLong_FromUnsignedLongLong(sample->period));
835 pydict_set_item_string_decref(dict, "sample", dict_sample);
837 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
838 (const char *)sample->raw_data, sample->raw_size));
839 pydict_set_item_string_decref(dict, "comm",
840 PyString_FromString(thread__comm_str(al->thread)));
842 pydict_set_item_string_decref(dict, "dso",
843 PyString_FromString(al->map->dso->name));
846 pydict_set_item_string_decref(dict, "symbol",
847 PyString_FromString(al->sym->name));
851 callchain = python_process_callchain(sample, evsel, al);
852 pydict_set_item_string_decref(dict, "callchain", callchain);
854 PyTuple_SetItem(t, n++, dict);
855 if (_PyTuple_Resize(&t, n) == -1)
856 Py_FatalError("error resizing Python tuple");
858 call_object(handler, t, handler_name);
864 static void python_process_event(union perf_event *event,
865 struct perf_sample *sample,
866 struct perf_evsel *evsel,
867 struct addr_location *al)
869 struct tables *tables = &tables_global;
871 switch (evsel->attr.type) {
872 case PERF_TYPE_TRACEPOINT:
873 python_process_tracepoint(sample, evsel, al);
875 /* Reserve for future process_hw/sw/raw APIs */
877 if (tables->db_export_mode)
878 db_export__sample(&tables->dbe, event, sample, evsel, al);
880 python_process_general_event(sample, evsel, al);
884 static void get_handler_name(char *str, size_t size,
885 struct perf_evsel *evsel)
889 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
891 while ((p = strchr(p, ':'))) {
898 process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
899 struct perf_counts_values *count)
901 PyObject *handler, *t;
902 static char handler_name[256];
905 t = PyTuple_New(MAX_FIELDS);
907 Py_FatalError("couldn't create Python tuple");
909 get_handler_name(handler_name, sizeof(handler_name),
912 handler = get_handler(handler_name);
914 pr_debug("can't find python handler %s\n", handler_name);
918 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
919 PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
921 tuple_set_u64(t, n++, tstamp);
922 tuple_set_u64(t, n++, count->val);
923 tuple_set_u64(t, n++, count->ena);
924 tuple_set_u64(t, n++, count->run);
926 if (_PyTuple_Resize(&t, n) == -1)
927 Py_FatalError("error resizing Python tuple");
929 call_object(handler, t, handler_name);
934 static void python_process_stat(struct perf_stat_config *config,
935 struct perf_evsel *counter, u64 tstamp)
937 struct thread_map *threads = counter->threads;
938 struct cpu_map *cpus = counter->cpus;
941 if (config->aggr_mode == AGGR_GLOBAL) {
942 process_stat(counter, -1, -1, tstamp,
943 &counter->counts->aggr);
947 for (thread = 0; thread < threads->nr; thread++) {
948 for (cpu = 0; cpu < cpus->nr; cpu++) {
949 process_stat(counter, cpus->map[cpu],
950 thread_map__pid(threads, thread), tstamp,
951 perf_counts(counter->counts, cpu, thread));
956 static void python_process_stat_interval(u64 tstamp)
958 PyObject *handler, *t;
959 static const char handler_name[] = "stat__interval";
962 t = PyTuple_New(MAX_FIELDS);
964 Py_FatalError("couldn't create Python tuple");
966 handler = get_handler(handler_name);
968 pr_debug("can't find python handler %s\n", handler_name);
972 tuple_set_u64(t, n++, tstamp);
974 if (_PyTuple_Resize(&t, n) == -1)
975 Py_FatalError("error resizing Python tuple");
977 call_object(handler, t, handler_name);
982 static int run_start_sub(void)
984 main_module = PyImport_AddModule("__main__");
985 if (main_module == NULL)
987 Py_INCREF(main_module);
989 main_dict = PyModule_GetDict(main_module);
990 if (main_dict == NULL)
992 Py_INCREF(main_dict);
994 try_call_object("trace_begin", NULL);
999 Py_XDECREF(main_dict);
1000 Py_XDECREF(main_module);
1004 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1005 tables->handler_name = get_handler(#table_name); \
1006 if (tables->handler_name) \
1007 tables->dbe.export_ ## name = python_export_ ## name; \
1010 #define SET_TABLE_HANDLER(name) \
1011 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1013 static void set_table_handlers(struct tables *tables)
1015 const char *perf_db_export_mode = "perf_db_export_mode";
1016 const char *perf_db_export_calls = "perf_db_export_calls";
1017 const char *perf_db_export_callchains = "perf_db_export_callchains";
1018 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
1019 bool export_calls = false;
1020 bool export_callchains = false;
1023 memset(tables, 0, sizeof(struct tables));
1024 if (db_export__init(&tables->dbe))
1025 Py_FatalError("failed to initialize export");
1027 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1028 if (!db_export_mode)
1031 ret = PyObject_IsTrue(db_export_mode);
1033 handler_call_die(perf_db_export_mode);
1037 /* handle export calls */
1038 tables->dbe.crp = NULL;
1039 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1040 if (db_export_calls) {
1041 ret = PyObject_IsTrue(db_export_calls);
1043 handler_call_die(perf_db_export_calls);
1044 export_calls = !!ret;
1049 call_return_processor__new(python_process_call_return,
1051 if (!tables->dbe.crp)
1052 Py_FatalError("failed to create calls processor");
1055 /* handle export callchains */
1056 tables->dbe.cpr = NULL;
1057 db_export_callchains = PyDict_GetItemString(main_dict,
1058 perf_db_export_callchains);
1059 if (db_export_callchains) {
1060 ret = PyObject_IsTrue(db_export_callchains);
1062 handler_call_die(perf_db_export_callchains);
1063 export_callchains = !!ret;
1066 if (export_callchains) {
1068 * Attempt to use the call path root from the call return
1069 * processor, if the call return processor is in use. Otherwise,
1070 * we allocate a new call path root. This prevents exporting
1071 * duplicate call path ids when both are in use simultaniously.
1073 if (tables->dbe.crp)
1074 tables->dbe.cpr = tables->dbe.crp->cpr;
1076 tables->dbe.cpr = call_path_root__new();
1078 if (!tables->dbe.cpr)
1079 Py_FatalError("failed to create call path root");
1082 tables->db_export_mode = true;
1084 * Reserve per symbol space for symbol->db_id via symbol__priv()
1086 symbol_conf.priv_size = sizeof(u64);
1088 SET_TABLE_HANDLER(evsel);
1089 SET_TABLE_HANDLER(machine);
1090 SET_TABLE_HANDLER(thread);
1091 SET_TABLE_HANDLER(comm);
1092 SET_TABLE_HANDLER(comm_thread);
1093 SET_TABLE_HANDLER(dso);
1094 SET_TABLE_HANDLER(symbol);
1095 SET_TABLE_HANDLER(branch_type);
1096 SET_TABLE_HANDLER(sample);
1097 SET_TABLE_HANDLER(call_path);
1098 SET_TABLE_HANDLER(call_return);
1102 * Start trace script
1104 static int python_start_script(const char *script, int argc, const char **argv)
1106 struct tables *tables = &tables_global;
1107 const char **command_line;
1112 command_line = malloc((argc + 1) * sizeof(const char *));
1113 command_line[0] = script;
1114 for (i = 1; i < argc + 1; i++)
1115 command_line[i] = argv[i - 1];
1119 initperf_trace_context();
1121 PySys_SetArgv(argc + 1, (char **)command_line);
1123 fp = fopen(script, "r");
1125 sprintf(buf, "Can't open python script \"%s\"", script);
1131 err = PyRun_SimpleFile(fp, script);
1133 fprintf(stderr, "Error running python script %s\n", script);
1137 err = run_start_sub();
1139 fprintf(stderr, "Error starting python script %s\n", script);
1143 set_table_handlers(tables);
1145 if (tables->db_export_mode) {
1146 err = db_export__branch_types(&tables->dbe);
1161 static int python_flush_script(void)
1163 struct tables *tables = &tables_global;
1165 return db_export__flush(&tables->dbe);
1171 static int python_stop_script(void)
1173 struct tables *tables = &tables_global;
1175 try_call_object("trace_end", NULL);
1177 db_export__exit(&tables->dbe);
1179 Py_XDECREF(main_dict);
1180 Py_XDECREF(main_module);
1186 static int python_generate_script(struct pevent *pevent, const char *outfile)
1188 struct event_format *event = NULL;
1189 struct format_field *f;
1190 char fname[PATH_MAX];
1191 int not_first, count;
1194 sprintf(fname, "%s.py", outfile);
1195 ofp = fopen(fname, "w");
1197 fprintf(stderr, "couldn't open %s\n", fname);
1200 fprintf(ofp, "# perf script event handlers, "
1201 "generated by perf script -g python\n");
1203 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1204 " License version 2\n\n");
1206 fprintf(ofp, "# The common_* event handler fields are the most useful "
1207 "fields common to\n");
1209 fprintf(ofp, "# all events. They don't necessarily correspond to "
1210 "the 'common_*' fields\n");
1212 fprintf(ofp, "# in the format files. Those fields not available as "
1213 "handler params can\n");
1215 fprintf(ofp, "# be retrieved using Python functions of the form "
1216 "common_*(context).\n");
1218 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1219 "of available functions.\n\n");
1221 fprintf(ofp, "import os\n");
1222 fprintf(ofp, "import sys\n\n");
1224 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1225 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1226 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1227 fprintf(ofp, "from Core import *\n\n\n");
1229 fprintf(ofp, "def trace_begin():\n");
1230 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1232 fprintf(ofp, "def trace_end():\n");
1233 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1235 while ((event = trace_find_next_event(pevent, event))) {
1236 fprintf(ofp, "def %s__%s(", event->system, event->name);
1237 fprintf(ofp, "event_name, ");
1238 fprintf(ofp, "context, ");
1239 fprintf(ofp, "common_cpu,\n");
1240 fprintf(ofp, "\tcommon_secs, ");
1241 fprintf(ofp, "common_nsecs, ");
1242 fprintf(ofp, "common_pid, ");
1243 fprintf(ofp, "common_comm,\n\t");
1244 fprintf(ofp, "common_callchain, ");
1249 for (f = event->format.fields; f; f = f->next) {
1252 if (++count % 5 == 0)
1253 fprintf(ofp, "\n\t");
1255 fprintf(ofp, "%s", f->name);
1257 fprintf(ofp, "):\n");
1259 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1260 "common_secs, common_nsecs,\n\t\t\t"
1261 "common_pid, common_comm)\n\n");
1263 fprintf(ofp, "\t\tprint \"");
1268 for (f = event->format.fields; f; f = f->next) {
1271 if (count && count % 3 == 0) {
1272 fprintf(ofp, "\" \\\n\t\t\"");
1276 fprintf(ofp, "%s=", f->name);
1277 if (f->flags & FIELD_IS_STRING ||
1278 f->flags & FIELD_IS_FLAG ||
1279 f->flags & FIELD_IS_ARRAY ||
1280 f->flags & FIELD_IS_SYMBOLIC)
1281 fprintf(ofp, "%%s");
1282 else if (f->flags & FIELD_IS_SIGNED)
1283 fprintf(ofp, "%%d");
1285 fprintf(ofp, "%%u");
1288 fprintf(ofp, "\" %% \\\n\t\t(");
1293 for (f = event->format.fields; f; f = f->next) {
1297 if (++count % 5 == 0)
1298 fprintf(ofp, "\n\t\t");
1300 if (f->flags & FIELD_IS_FLAG) {
1301 if ((count - 1) % 5 != 0) {
1302 fprintf(ofp, "\n\t\t");
1305 fprintf(ofp, "flag_str(\"");
1306 fprintf(ofp, "%s__%s\", ", event->system,
1308 fprintf(ofp, "\"%s\", %s)", f->name,
1310 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1311 if ((count - 1) % 5 != 0) {
1312 fprintf(ofp, "\n\t\t");
1315 fprintf(ofp, "symbol_str(\"");
1316 fprintf(ofp, "%s__%s\", ", event->system,
1318 fprintf(ofp, "\"%s\", %s)", f->name,
1321 fprintf(ofp, "%s", f->name);
1324 fprintf(ofp, ")\n\n");
1326 fprintf(ofp, "\t\tfor node in common_callchain:");
1327 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1328 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1329 fprintf(ofp, "\n\t\t\telse:");
1330 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1331 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1335 fprintf(ofp, "def trace_unhandled(event_name, context, "
1336 "event_fields_dict):\n");
1338 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1339 "for k,v in sorted(event_fields_dict.items())])\n\n");
1341 fprintf(ofp, "def print_header("
1342 "event_name, cpu, secs, nsecs, pid, comm):\n"
1343 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1344 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1348 fprintf(stderr, "generated Python script: %s\n", fname);
1353 struct scripting_ops python_scripting_ops = {
1355 .start_script = python_start_script,
1356 .flush_script = python_flush_script,
1357 .stop_script = python_stop_script,
1358 .process_event = python_process_event,
1359 .process_stat = python_process_stat,
1360 .process_stat_interval = python_process_stat_interval,
1361 .generate_script = python_generate_script,