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>
30 #include <linux/time64.h>
32 #include "../../perf.h"
34 #include "../callchain.h"
38 #include "../thread.h"
40 #include "../machine.h"
41 #include "../db-export.h"
42 #include "../thread-stack.h"
43 #include "../trace-event.h"
44 #include "../machine.h"
45 #include "../call-path.h"
46 #include "thread_map.h"
50 PyMODINIT_FUNC initperf_trace_context(void);
52 #define TRACE_EVENT_TYPE_MAX \
53 ((1 << (sizeof(unsigned short) * 8)) - 1)
55 static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
58 #define N_COMMON_FIELDS 7
60 extern struct scripting_context *scripting_context;
62 static char *cur_field_name;
63 static int zero_flag_atom;
65 static PyObject *main_module, *main_dict;
69 PyObject *evsel_handler;
70 PyObject *machine_handler;
71 PyObject *thread_handler;
72 PyObject *comm_handler;
73 PyObject *comm_thread_handler;
74 PyObject *dso_handler;
75 PyObject *symbol_handler;
76 PyObject *branch_type_handler;
77 PyObject *sample_handler;
78 PyObject *call_path_handler;
79 PyObject *call_return_handler;
83 static struct tables tables_global;
85 static void handler_call_die(const char *handler_name) NORETURN;
86 static void handler_call_die(const char *handler_name)
89 Py_FatalError("problem in Python trace event handler");
90 // Py_FatalError does not return
91 // but we have to make the compiler happy
96 * Insert val into into the dictionary and decrement the reference counter.
97 * This is necessary for dictionaries since PyDict_SetItemString() does not
98 * steal a reference, as opposed to PyTuple_SetItem().
100 static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
102 PyDict_SetItemString(dict, key, val);
106 static PyObject *get_handler(const char *handler_name)
110 handler = PyDict_GetItemString(main_dict, handler_name);
111 if (handler && !PyCallable_Check(handler))
116 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
120 retval = PyObject_CallObject(handler, args);
122 handler_call_die(die_msg);
126 static void try_call_object(const char *handler_name, PyObject *args)
130 handler = get_handler(handler_name);
132 call_object(handler, args, handler_name);
135 static void define_value(enum print_arg_type field_type,
137 const char *field_name,
138 const char *field_value,
139 const char *field_str)
141 const char *handler_name = "define_flag_value";
143 unsigned long long value;
146 if (field_type == PRINT_SYMBOL)
147 handler_name = "define_symbolic_value";
151 Py_FatalError("couldn't create Python tuple");
153 value = eval_flag(field_value);
155 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
156 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
157 PyTuple_SetItem(t, n++, PyInt_FromLong(value));
158 PyTuple_SetItem(t, n++, PyString_FromString(field_str));
160 try_call_object(handler_name, t);
165 static void define_values(enum print_arg_type field_type,
166 struct print_flag_sym *field,
168 const char *field_name)
170 define_value(field_type, ev_name, field_name, field->value,
174 define_values(field_type, field->next, ev_name, field_name);
177 static void define_field(enum print_arg_type field_type,
179 const char *field_name,
182 const char *handler_name = "define_flag_field";
186 if (field_type == PRINT_SYMBOL)
187 handler_name = "define_symbolic_field";
189 if (field_type == PRINT_FLAGS)
194 Py_FatalError("couldn't create Python tuple");
196 PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
197 PyTuple_SetItem(t, n++, PyString_FromString(field_name));
198 if (field_type == PRINT_FLAGS)
199 PyTuple_SetItem(t, n++, PyString_FromString(delim));
201 try_call_object(handler_name, t);
206 static void define_event_symbols(struct event_format *event,
208 struct print_arg *args)
213 switch (args->type) {
217 define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
222 free(cur_field_name);
223 cur_field_name = strdup(args->field.name);
226 define_event_symbols(event, ev_name, args->flags.field);
227 define_field(PRINT_FLAGS, ev_name, cur_field_name,
229 define_values(PRINT_FLAGS, args->flags.flags, ev_name,
233 define_event_symbols(event, ev_name, args->symbol.field);
234 define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
235 define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
240 define_event_symbols(event, ev_name, args->hex.field);
241 define_event_symbols(event, ev_name, args->hex.size);
243 case PRINT_INT_ARRAY:
244 define_event_symbols(event, ev_name, args->int_array.field);
245 define_event_symbols(event, ev_name, args->int_array.count);
246 define_event_symbols(event, ev_name, args->int_array.el_size);
251 define_event_symbols(event, ev_name, args->typecast.item);
254 if (strcmp(args->op.op, ":") == 0)
256 define_event_symbols(event, ev_name, args->op.left);
257 define_event_symbols(event, ev_name, args->op.right);
260 /* gcc warns for these? */
262 case PRINT_DYNAMIC_ARRAY:
263 case PRINT_DYNAMIC_ARRAY_LEN:
266 /* we should warn... */
271 define_event_symbols(event, ev_name, args->next);
274 static PyObject *get_field_numeric_entry(struct event_format *event,
275 struct format_field *field, void *data)
277 bool is_array = field->flags & FIELD_IS_ARRAY;
278 PyObject *obj = NULL, *list = NULL;
279 unsigned long long val;
280 unsigned int item_size, n_items, i;
283 list = PyList_New(field->arraylen);
284 item_size = field->size / field->arraylen;
285 n_items = field->arraylen;
287 item_size = field->size;
291 for (i = 0; i < n_items; i++) {
293 val = read_size(event, data + field->offset + i * item_size,
295 if (field->flags & FIELD_IS_SIGNED) {
296 if ((long long)val >= LONG_MIN &&
297 (long long)val <= LONG_MAX)
298 obj = PyInt_FromLong(val);
300 obj = PyLong_FromLongLong(val);
303 obj = PyInt_FromLong(val);
305 obj = PyLong_FromUnsignedLongLong(val);
308 PyList_SET_ITEM(list, i, obj);
316 static PyObject *python_process_callchain(struct perf_sample *sample,
317 struct perf_evsel *evsel,
318 struct addr_location *al)
322 pylist = PyList_New(0);
324 Py_FatalError("couldn't create Python list");
326 if (!symbol_conf.use_callchain || !sample->callchain)
329 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
331 scripting_max_stack) != 0) {
332 pr_err("Failed to resolve callchain. Skipping\n");
335 callchain_cursor_commit(&callchain_cursor);
340 struct callchain_cursor_node *node;
341 node = callchain_cursor_current(&callchain_cursor);
345 pyelem = PyDict_New();
347 Py_FatalError("couldn't create Python dictionary");
350 pydict_set_item_string_decref(pyelem, "ip",
351 PyLong_FromUnsignedLongLong(node->ip));
354 PyObject *pysym = PyDict_New();
356 Py_FatalError("couldn't create Python dictionary");
357 pydict_set_item_string_decref(pysym, "start",
358 PyLong_FromUnsignedLongLong(node->sym->start));
359 pydict_set_item_string_decref(pysym, "end",
360 PyLong_FromUnsignedLongLong(node->sym->end));
361 pydict_set_item_string_decref(pysym, "binding",
362 PyInt_FromLong(node->sym->binding));
363 pydict_set_item_string_decref(pysym, "name",
364 PyString_FromStringAndSize(node->sym->name,
365 node->sym->namelen));
366 pydict_set_item_string_decref(pyelem, "sym", pysym);
370 struct map *map = node->map;
371 const char *dsoname = "[unknown]";
372 if (map && map->dso) {
373 if (symbol_conf.show_kernel_path && map->dso->long_name)
374 dsoname = map->dso->long_name;
376 dsoname = map->dso->name;
378 pydict_set_item_string_decref(pyelem, "dso",
379 PyString_FromString(dsoname));
382 callchain_cursor_advance(&callchain_cursor);
383 PyList_Append(pylist, pyelem);
391 static void python_process_tracepoint(struct perf_sample *sample,
392 struct perf_evsel *evsel,
393 struct addr_location *al)
395 struct event_format *event = evsel->tp_format;
396 PyObject *handler, *context, *t, *obj = NULL, *callchain;
397 PyObject *dict = NULL;
398 static char handler_name[256];
399 struct format_field *field;
403 int cpu = sample->cpu;
404 void *data = sample->raw_data;
405 unsigned long long nsecs = sample->time;
406 const char *comm = thread__comm_str(al->thread);
408 t = PyTuple_New(MAX_FIELDS);
410 Py_FatalError("couldn't create Python tuple");
413 snprintf(handler_name, sizeof(handler_name),
414 "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
415 Py_FatalError(handler_name);
418 pid = raw_field_value(event, "common_pid", data);
420 sprintf(handler_name, "%s__%s", event->system, event->name);
422 if (!test_and_set_bit(event->id, events_defined))
423 define_event_symbols(event, handler_name, event->print_fmt.args);
425 handler = get_handler(handler_name);
429 Py_FatalError("couldn't create Python dict");
431 s = nsecs / NSEC_PER_SEC;
432 ns = nsecs - s * NSEC_PER_SEC;
434 scripting_context->event_data = data;
435 scripting_context->pevent = evsel->tp_format->pevent;
437 context = PyCObject_FromVoidPtr(scripting_context, NULL);
439 PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
440 PyTuple_SetItem(t, n++, context);
443 callchain = python_process_callchain(sample, evsel, al);
446 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
447 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
448 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
449 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
450 PyTuple_SetItem(t, n++, PyString_FromString(comm));
451 PyTuple_SetItem(t, n++, callchain);
453 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
454 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
455 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
456 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
457 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
458 pydict_set_item_string_decref(dict, "common_callchain", callchain);
460 for (field = event->format.fields; field; field = field->next) {
461 unsigned int offset, len;
462 unsigned long long val;
464 if (field->flags & FIELD_IS_ARRAY) {
465 offset = field->offset;
467 if (field->flags & FIELD_IS_DYNAMIC) {
468 val = pevent_read_number(scripting_context->pevent,
474 if (field->flags & FIELD_IS_STRING &&
475 is_printable_array(data + offset, len)) {
476 obj = PyString_FromString((char *) data + offset);
478 obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
479 field->flags &= ~FIELD_IS_STRING;
481 } else { /* FIELD_IS_NUMERIC */
482 obj = get_field_numeric_entry(event, field, data);
485 PyTuple_SetItem(t, n++, obj);
487 pydict_set_item_string_decref(dict, field->name, obj);
492 PyTuple_SetItem(t, n++, dict);
494 if (_PyTuple_Resize(&t, n) == -1)
495 Py_FatalError("error resizing Python tuple");
498 call_object(handler, t, handler_name);
500 try_call_object("trace_unhandled", t);
507 static PyObject *tuple_new(unsigned int sz)
513 Py_FatalError("couldn't create Python tuple");
517 static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
519 #if BITS_PER_LONG == 64
520 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
522 #if BITS_PER_LONG == 32
523 return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
527 static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
529 return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
532 static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
534 return PyTuple_SetItem(t, pos, PyString_FromString(s));
537 static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
539 struct tables *tables = container_of(dbe, struct tables, dbe);
544 tuple_set_u64(t, 0, evsel->db_id);
545 tuple_set_string(t, 1, perf_evsel__name(evsel));
547 call_object(tables->evsel_handler, t, "evsel_table");
554 static int python_export_machine(struct db_export *dbe,
555 struct machine *machine)
557 struct tables *tables = container_of(dbe, struct tables, dbe);
562 tuple_set_u64(t, 0, machine->db_id);
563 tuple_set_s32(t, 1, machine->pid);
564 tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
566 call_object(tables->machine_handler, t, "machine_table");
573 static int python_export_thread(struct db_export *dbe, struct thread *thread,
574 u64 main_thread_db_id, struct machine *machine)
576 struct tables *tables = container_of(dbe, struct tables, dbe);
581 tuple_set_u64(t, 0, thread->db_id);
582 tuple_set_u64(t, 1, machine->db_id);
583 tuple_set_u64(t, 2, main_thread_db_id);
584 tuple_set_s32(t, 3, thread->pid_);
585 tuple_set_s32(t, 4, thread->tid);
587 call_object(tables->thread_handler, t, "thread_table");
594 static int python_export_comm(struct db_export *dbe, struct comm *comm)
596 struct tables *tables = container_of(dbe, struct tables, dbe);
601 tuple_set_u64(t, 0, comm->db_id);
602 tuple_set_string(t, 1, comm__str(comm));
604 call_object(tables->comm_handler, t, "comm_table");
611 static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
612 struct comm *comm, struct thread *thread)
614 struct tables *tables = container_of(dbe, struct tables, dbe);
619 tuple_set_u64(t, 0, db_id);
620 tuple_set_u64(t, 1, comm->db_id);
621 tuple_set_u64(t, 2, thread->db_id);
623 call_object(tables->comm_thread_handler, t, "comm_thread_table");
630 static int python_export_dso(struct db_export *dbe, struct dso *dso,
631 struct machine *machine)
633 struct tables *tables = container_of(dbe, struct tables, dbe);
634 char sbuild_id[SBUILD_ID_SIZE];
637 build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
641 tuple_set_u64(t, 0, dso->db_id);
642 tuple_set_u64(t, 1, machine->db_id);
643 tuple_set_string(t, 2, dso->short_name);
644 tuple_set_string(t, 3, dso->long_name);
645 tuple_set_string(t, 4, sbuild_id);
647 call_object(tables->dso_handler, t, "dso_table");
654 static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
657 struct tables *tables = container_of(dbe, struct tables, dbe);
658 u64 *sym_db_id = symbol__priv(sym);
663 tuple_set_u64(t, 0, *sym_db_id);
664 tuple_set_u64(t, 1, dso->db_id);
665 tuple_set_u64(t, 2, sym->start);
666 tuple_set_u64(t, 3, sym->end);
667 tuple_set_s32(t, 4, sym->binding);
668 tuple_set_string(t, 5, sym->name);
670 call_object(tables->symbol_handler, t, "symbol_table");
677 static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
680 struct tables *tables = container_of(dbe, struct tables, dbe);
685 tuple_set_s32(t, 0, branch_type);
686 tuple_set_string(t, 1, name);
688 call_object(tables->branch_type_handler, t, "branch_type_table");
695 static int python_export_sample(struct db_export *dbe,
696 struct export_sample *es)
698 struct tables *tables = container_of(dbe, struct tables, dbe);
703 tuple_set_u64(t, 0, es->db_id);
704 tuple_set_u64(t, 1, es->evsel->db_id);
705 tuple_set_u64(t, 2, es->al->machine->db_id);
706 tuple_set_u64(t, 3, es->al->thread->db_id);
707 tuple_set_u64(t, 4, es->comm_db_id);
708 tuple_set_u64(t, 5, es->dso_db_id);
709 tuple_set_u64(t, 6, es->sym_db_id);
710 tuple_set_u64(t, 7, es->offset);
711 tuple_set_u64(t, 8, es->sample->ip);
712 tuple_set_u64(t, 9, es->sample->time);
713 tuple_set_s32(t, 10, es->sample->cpu);
714 tuple_set_u64(t, 11, es->addr_dso_db_id);
715 tuple_set_u64(t, 12, es->addr_sym_db_id);
716 tuple_set_u64(t, 13, es->addr_offset);
717 tuple_set_u64(t, 14, es->sample->addr);
718 tuple_set_u64(t, 15, es->sample->period);
719 tuple_set_u64(t, 16, es->sample->weight);
720 tuple_set_u64(t, 17, es->sample->transaction);
721 tuple_set_u64(t, 18, es->sample->data_src);
722 tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
723 tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
724 tuple_set_u64(t, 21, es->call_path_id);
726 call_object(tables->sample_handler, t, "sample_table");
733 static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
735 struct tables *tables = container_of(dbe, struct tables, dbe);
737 u64 parent_db_id, sym_db_id;
739 parent_db_id = cp->parent ? cp->parent->db_id : 0;
740 sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
744 tuple_set_u64(t, 0, cp->db_id);
745 tuple_set_u64(t, 1, parent_db_id);
746 tuple_set_u64(t, 2, sym_db_id);
747 tuple_set_u64(t, 3, cp->ip);
749 call_object(tables->call_path_handler, t, "call_path_table");
756 static int python_export_call_return(struct db_export *dbe,
757 struct call_return *cr)
759 struct tables *tables = container_of(dbe, struct tables, dbe);
760 u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
765 tuple_set_u64(t, 0, cr->db_id);
766 tuple_set_u64(t, 1, cr->thread->db_id);
767 tuple_set_u64(t, 2, comm_db_id);
768 tuple_set_u64(t, 3, cr->cp->db_id);
769 tuple_set_u64(t, 4, cr->call_time);
770 tuple_set_u64(t, 5, cr->return_time);
771 tuple_set_u64(t, 6, cr->branch_count);
772 tuple_set_u64(t, 7, cr->call_ref);
773 tuple_set_u64(t, 8, cr->return_ref);
774 tuple_set_u64(t, 9, cr->cp->parent->db_id);
775 tuple_set_s32(t, 10, cr->flags);
777 call_object(tables->call_return_handler, t, "call_return_table");
784 static int python_process_call_return(struct call_return *cr, void *data)
786 struct db_export *dbe = data;
788 return db_export__call_return(dbe, cr);
791 static void python_process_general_event(struct perf_sample *sample,
792 struct perf_evsel *evsel,
793 struct addr_location *al)
795 PyObject *handler, *t, *dict, *callchain, *dict_sample;
796 static char handler_name[64];
800 * Use the MAX_FIELDS to make the function expandable, though
801 * currently there is only one item for the tuple.
803 t = PyTuple_New(MAX_FIELDS);
805 Py_FatalError("couldn't create Python tuple");
809 Py_FatalError("couldn't create Python dictionary");
811 dict_sample = PyDict_New();
813 Py_FatalError("couldn't create Python dictionary");
815 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
817 handler = get_handler(handler_name);
821 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
822 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
823 (const char *)&evsel->attr, sizeof(evsel->attr)));
825 pydict_set_item_string_decref(dict_sample, "pid",
826 PyInt_FromLong(sample->pid));
827 pydict_set_item_string_decref(dict_sample, "tid",
828 PyInt_FromLong(sample->tid));
829 pydict_set_item_string_decref(dict_sample, "cpu",
830 PyInt_FromLong(sample->cpu));
831 pydict_set_item_string_decref(dict_sample, "ip",
832 PyLong_FromUnsignedLongLong(sample->ip));
833 pydict_set_item_string_decref(dict_sample, "time",
834 PyLong_FromUnsignedLongLong(sample->time));
835 pydict_set_item_string_decref(dict_sample, "period",
836 PyLong_FromUnsignedLongLong(sample->period));
837 pydict_set_item_string_decref(dict, "sample", dict_sample);
839 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
840 (const char *)sample->raw_data, sample->raw_size));
841 pydict_set_item_string_decref(dict, "comm",
842 PyString_FromString(thread__comm_str(al->thread)));
844 pydict_set_item_string_decref(dict, "dso",
845 PyString_FromString(al->map->dso->name));
848 pydict_set_item_string_decref(dict, "symbol",
849 PyString_FromString(al->sym->name));
853 callchain = python_process_callchain(sample, evsel, al);
854 pydict_set_item_string_decref(dict, "callchain", callchain);
856 PyTuple_SetItem(t, n++, dict);
857 if (_PyTuple_Resize(&t, n) == -1)
858 Py_FatalError("error resizing Python tuple");
860 call_object(handler, t, handler_name);
866 static void python_process_event(union perf_event *event,
867 struct perf_sample *sample,
868 struct perf_evsel *evsel,
869 struct addr_location *al)
871 struct tables *tables = &tables_global;
873 switch (evsel->attr.type) {
874 case PERF_TYPE_TRACEPOINT:
875 python_process_tracepoint(sample, evsel, al);
877 /* Reserve for future process_hw/sw/raw APIs */
879 if (tables->db_export_mode)
880 db_export__sample(&tables->dbe, event, sample, evsel, al);
882 python_process_general_event(sample, evsel, al);
886 static void get_handler_name(char *str, size_t size,
887 struct perf_evsel *evsel)
891 scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
893 while ((p = strchr(p, ':'))) {
900 process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
901 struct perf_counts_values *count)
903 PyObject *handler, *t;
904 static char handler_name[256];
907 t = PyTuple_New(MAX_FIELDS);
909 Py_FatalError("couldn't create Python tuple");
911 get_handler_name(handler_name, sizeof(handler_name),
914 handler = get_handler(handler_name);
916 pr_debug("can't find python handler %s\n", handler_name);
920 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
921 PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
923 tuple_set_u64(t, n++, tstamp);
924 tuple_set_u64(t, n++, count->val);
925 tuple_set_u64(t, n++, count->ena);
926 tuple_set_u64(t, n++, count->run);
928 if (_PyTuple_Resize(&t, n) == -1)
929 Py_FatalError("error resizing Python tuple");
931 call_object(handler, t, handler_name);
936 static void python_process_stat(struct perf_stat_config *config,
937 struct perf_evsel *counter, u64 tstamp)
939 struct thread_map *threads = counter->threads;
940 struct cpu_map *cpus = counter->cpus;
943 if (config->aggr_mode == AGGR_GLOBAL) {
944 process_stat(counter, -1, -1, tstamp,
945 &counter->counts->aggr);
949 for (thread = 0; thread < threads->nr; thread++) {
950 for (cpu = 0; cpu < cpus->nr; cpu++) {
951 process_stat(counter, cpus->map[cpu],
952 thread_map__pid(threads, thread), tstamp,
953 perf_counts(counter->counts, cpu, thread));
958 static void python_process_stat_interval(u64 tstamp)
960 PyObject *handler, *t;
961 static const char handler_name[] = "stat__interval";
964 t = PyTuple_New(MAX_FIELDS);
966 Py_FatalError("couldn't create Python tuple");
968 handler = get_handler(handler_name);
970 pr_debug("can't find python handler %s\n", handler_name);
974 tuple_set_u64(t, n++, tstamp);
976 if (_PyTuple_Resize(&t, n) == -1)
977 Py_FatalError("error resizing Python tuple");
979 call_object(handler, t, handler_name);
984 static int run_start_sub(void)
986 main_module = PyImport_AddModule("__main__");
987 if (main_module == NULL)
989 Py_INCREF(main_module);
991 main_dict = PyModule_GetDict(main_module);
992 if (main_dict == NULL)
994 Py_INCREF(main_dict);
996 try_call_object("trace_begin", NULL);
1001 Py_XDECREF(main_dict);
1002 Py_XDECREF(main_module);
1006 #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
1007 tables->handler_name = get_handler(#table_name); \
1008 if (tables->handler_name) \
1009 tables->dbe.export_ ## name = python_export_ ## name; \
1012 #define SET_TABLE_HANDLER(name) \
1013 SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
1015 static void set_table_handlers(struct tables *tables)
1017 const char *perf_db_export_mode = "perf_db_export_mode";
1018 const char *perf_db_export_calls = "perf_db_export_calls";
1019 const char *perf_db_export_callchains = "perf_db_export_callchains";
1020 PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
1021 bool export_calls = false;
1022 bool export_callchains = false;
1025 memset(tables, 0, sizeof(struct tables));
1026 if (db_export__init(&tables->dbe))
1027 Py_FatalError("failed to initialize export");
1029 db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
1030 if (!db_export_mode)
1033 ret = PyObject_IsTrue(db_export_mode);
1035 handler_call_die(perf_db_export_mode);
1039 /* handle export calls */
1040 tables->dbe.crp = NULL;
1041 db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
1042 if (db_export_calls) {
1043 ret = PyObject_IsTrue(db_export_calls);
1045 handler_call_die(perf_db_export_calls);
1046 export_calls = !!ret;
1051 call_return_processor__new(python_process_call_return,
1053 if (!tables->dbe.crp)
1054 Py_FatalError("failed to create calls processor");
1057 /* handle export callchains */
1058 tables->dbe.cpr = NULL;
1059 db_export_callchains = PyDict_GetItemString(main_dict,
1060 perf_db_export_callchains);
1061 if (db_export_callchains) {
1062 ret = PyObject_IsTrue(db_export_callchains);
1064 handler_call_die(perf_db_export_callchains);
1065 export_callchains = !!ret;
1068 if (export_callchains) {
1070 * Attempt to use the call path root from the call return
1071 * processor, if the call return processor is in use. Otherwise,
1072 * we allocate a new call path root. This prevents exporting
1073 * duplicate call path ids when both are in use simultaniously.
1075 if (tables->dbe.crp)
1076 tables->dbe.cpr = tables->dbe.crp->cpr;
1078 tables->dbe.cpr = call_path_root__new();
1080 if (!tables->dbe.cpr)
1081 Py_FatalError("failed to create call path root");
1084 tables->db_export_mode = true;
1086 * Reserve per symbol space for symbol->db_id via symbol__priv()
1088 symbol_conf.priv_size = sizeof(u64);
1090 SET_TABLE_HANDLER(evsel);
1091 SET_TABLE_HANDLER(machine);
1092 SET_TABLE_HANDLER(thread);
1093 SET_TABLE_HANDLER(comm);
1094 SET_TABLE_HANDLER(comm_thread);
1095 SET_TABLE_HANDLER(dso);
1096 SET_TABLE_HANDLER(symbol);
1097 SET_TABLE_HANDLER(branch_type);
1098 SET_TABLE_HANDLER(sample);
1099 SET_TABLE_HANDLER(call_path);
1100 SET_TABLE_HANDLER(call_return);
1104 * Start trace script
1106 static int python_start_script(const char *script, int argc, const char **argv)
1108 struct tables *tables = &tables_global;
1109 const char **command_line;
1114 command_line = malloc((argc + 1) * sizeof(const char *));
1115 command_line[0] = script;
1116 for (i = 1; i < argc + 1; i++)
1117 command_line[i] = argv[i - 1];
1121 initperf_trace_context();
1123 PySys_SetArgv(argc + 1, (char **)command_line);
1125 fp = fopen(script, "r");
1127 sprintf(buf, "Can't open python script \"%s\"", script);
1133 err = PyRun_SimpleFile(fp, script);
1135 fprintf(stderr, "Error running python script %s\n", script);
1139 err = run_start_sub();
1141 fprintf(stderr, "Error starting python script %s\n", script);
1145 set_table_handlers(tables);
1147 if (tables->db_export_mode) {
1148 err = db_export__branch_types(&tables->dbe);
1163 static int python_flush_script(void)
1165 struct tables *tables = &tables_global;
1167 return db_export__flush(&tables->dbe);
1173 static int python_stop_script(void)
1175 struct tables *tables = &tables_global;
1177 try_call_object("trace_end", NULL);
1179 db_export__exit(&tables->dbe);
1181 Py_XDECREF(main_dict);
1182 Py_XDECREF(main_module);
1188 static int python_generate_script(struct pevent *pevent, const char *outfile)
1190 struct event_format *event = NULL;
1191 struct format_field *f;
1192 char fname[PATH_MAX];
1193 int not_first, count;
1196 sprintf(fname, "%s.py", outfile);
1197 ofp = fopen(fname, "w");
1199 fprintf(stderr, "couldn't open %s\n", fname);
1202 fprintf(ofp, "# perf script event handlers, "
1203 "generated by perf script -g python\n");
1205 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
1206 " License version 2\n\n");
1208 fprintf(ofp, "# The common_* event handler fields are the most useful "
1209 "fields common to\n");
1211 fprintf(ofp, "# all events. They don't necessarily correspond to "
1212 "the 'common_*' fields\n");
1214 fprintf(ofp, "# in the format files. Those fields not available as "
1215 "handler params can\n");
1217 fprintf(ofp, "# be retrieved using Python functions of the form "
1218 "common_*(context).\n");
1220 fprintf(ofp, "# See the perf-trace-python Documentation for the list "
1221 "of available functions.\n\n");
1223 fprintf(ofp, "import os\n");
1224 fprintf(ofp, "import sys\n\n");
1226 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1227 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1228 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1229 fprintf(ofp, "from Core import *\n\n\n");
1231 fprintf(ofp, "def trace_begin():\n");
1232 fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
1234 fprintf(ofp, "def trace_end():\n");
1235 fprintf(ofp, "\tprint \"in trace_end\"\n\n");
1237 while ((event = trace_find_next_event(pevent, event))) {
1238 fprintf(ofp, "def %s__%s(", event->system, event->name);
1239 fprintf(ofp, "event_name, ");
1240 fprintf(ofp, "context, ");
1241 fprintf(ofp, "common_cpu,\n");
1242 fprintf(ofp, "\tcommon_secs, ");
1243 fprintf(ofp, "common_nsecs, ");
1244 fprintf(ofp, "common_pid, ");
1245 fprintf(ofp, "common_comm,\n\t");
1246 fprintf(ofp, "common_callchain, ");
1251 for (f = event->format.fields; f; f = f->next) {
1254 if (++count % 5 == 0)
1255 fprintf(ofp, "\n\t");
1257 fprintf(ofp, "%s", f->name);
1259 fprintf(ofp, "):\n");
1261 fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
1262 "common_secs, common_nsecs,\n\t\t\t"
1263 "common_pid, common_comm)\n\n");
1265 fprintf(ofp, "\t\tprint \"");
1270 for (f = event->format.fields; f; f = f->next) {
1273 if (count && count % 3 == 0) {
1274 fprintf(ofp, "\" \\\n\t\t\"");
1278 fprintf(ofp, "%s=", f->name);
1279 if (f->flags & FIELD_IS_STRING ||
1280 f->flags & FIELD_IS_FLAG ||
1281 f->flags & FIELD_IS_ARRAY ||
1282 f->flags & FIELD_IS_SYMBOLIC)
1283 fprintf(ofp, "%%s");
1284 else if (f->flags & FIELD_IS_SIGNED)
1285 fprintf(ofp, "%%d");
1287 fprintf(ofp, "%%u");
1290 fprintf(ofp, "\" %% \\\n\t\t(");
1295 for (f = event->format.fields; f; f = f->next) {
1299 if (++count % 5 == 0)
1300 fprintf(ofp, "\n\t\t");
1302 if (f->flags & FIELD_IS_FLAG) {
1303 if ((count - 1) % 5 != 0) {
1304 fprintf(ofp, "\n\t\t");
1307 fprintf(ofp, "flag_str(\"");
1308 fprintf(ofp, "%s__%s\", ", event->system,
1310 fprintf(ofp, "\"%s\", %s)", f->name,
1312 } else if (f->flags & FIELD_IS_SYMBOLIC) {
1313 if ((count - 1) % 5 != 0) {
1314 fprintf(ofp, "\n\t\t");
1317 fprintf(ofp, "symbol_str(\"");
1318 fprintf(ofp, "%s__%s\", ", event->system,
1320 fprintf(ofp, "\"%s\", %s)", f->name,
1323 fprintf(ofp, "%s", f->name);
1326 fprintf(ofp, ")\n\n");
1328 fprintf(ofp, "\t\tfor node in common_callchain:");
1329 fprintf(ofp, "\n\t\t\tif 'sym' in node:");
1330 fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
1331 fprintf(ofp, "\n\t\t\telse:");
1332 fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
1333 fprintf(ofp, "\t\tprint \"\\n\"\n\n");
1337 fprintf(ofp, "def trace_unhandled(event_name, context, "
1338 "event_fields_dict):\n");
1340 fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
1341 "for k,v in sorted(event_fields_dict.items())])\n\n");
1343 fprintf(ofp, "def print_header("
1344 "event_name, cpu, secs, nsecs, pid, comm):\n"
1345 "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
1346 "(event_name, cpu, secs, nsecs, pid, comm),\n");
1350 fprintf(stderr, "generated Python script: %s\n", fname);
1355 struct scripting_ops python_scripting_ops = {
1357 .start_script = python_start_script,
1358 .flush_script = python_flush_script,
1359 .stop_script = python_stop_script,
1360 .process_event = python_process_event,
1361 .process_stat = python_process_stat,
1362 .process_stat_interval = python_process_stat_interval,
1363 .generate_script = python_generate_script,