Commit c0251485 authored by Pierre Tardy's avatar Pierre Tardy Committed by Arnaldo Carvalho de Melo

perf scripts python: Give field dict to unhandled callback

trace_unhandled() callback does not allow to access event fields, this patch
resolves the problem.

It can also been used as a more pythonic and flexible way for script writters
to demux event types

This will for example greatly simplify pytimechart event demux.
Acked-by: default avatarFrederic Weisbecker <fweisbec@gmail.com>
Acked-by: default avatarTom Zanussi <tzanussi@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>,
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Tom Zanussi <tzanussi@gmail.com>
LKML-Reference: <1275340329-2397-1-git-send-email-tardyp@gmail.com>
Signed-off-by: default avatarPierre Tardy <tardyp@gmail.com>
Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 75d9ef17
...@@ -51,8 +51,7 @@ def kmem__kmalloc(event_name, context, common_cpu, ...@@ -51,8 +51,7 @@ def kmem__kmalloc(event_name, context, common_cpu,
flag_str("kmem__kmalloc", "gfp_flags", gfp_flags)), flag_str("kmem__kmalloc", "gfp_flags", gfp_flags)),
def trace_unhandled(event_name, context, common_cpu, common_secs, common_nsecs, def trace_unhandled(event_name, context, event_fields_dict):
common_pid, common_comm):
try: try:
unhandled[event_name] += 1 unhandled[event_name] += 1
except TypeError: except TypeError:
......
...@@ -208,7 +208,7 @@ static void python_process_event(int cpu, void *data, ...@@ -208,7 +208,7 @@ static void python_process_event(int cpu, void *data,
int size __unused, int size __unused,
unsigned long long nsecs, char *comm) unsigned long long nsecs, char *comm)
{ {
PyObject *handler, *retval, *context, *t, *obj; PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
static char handler_name[256]; static char handler_name[256];
struct format_field *field; struct format_field *field;
unsigned long long val; unsigned long long val;
...@@ -232,6 +232,14 @@ static void python_process_event(int cpu, void *data, ...@@ -232,6 +232,14 @@ static void python_process_event(int cpu, void *data,
sprintf(handler_name, "%s__%s", event->system, event->name); sprintf(handler_name, "%s__%s", event->system, event->name);
handler = PyDict_GetItemString(main_dict, handler_name);
if (handler && !PyCallable_Check(handler))
handler = NULL;
if (!handler) {
dict = PyDict_New();
if (!dict)
Py_FatalError("couldn't create Python dict");
}
s = nsecs / NSECS_PER_SEC; s = nsecs / NSECS_PER_SEC;
ns = nsecs - s * NSECS_PER_SEC; ns = nsecs - s * NSECS_PER_SEC;
...@@ -242,12 +250,20 @@ static void python_process_event(int cpu, void *data, ...@@ -242,12 +250,20 @@ static void python_process_event(int cpu, void *data,
PyTuple_SetItem(t, n++, PyString_FromString(handler_name)); PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
PyTuple_SetItem(t, n++, PyTuple_SetItem(t, n++,
PyCObject_FromVoidPtr(scripting_context, NULL)); PyCObject_FromVoidPtr(scripting_context, NULL));
if (handler) {
PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
PyTuple_SetItem(t, n++, PyInt_FromLong(s)); PyTuple_SetItem(t, n++, PyInt_FromLong(s));
PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
PyTuple_SetItem(t, n++, PyString_FromString(comm)); PyTuple_SetItem(t, n++, PyString_FromString(comm));
} else {
PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu));
PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s));
PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns));
PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid));
PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm));
}
for (field = event->format.fields; field; field = field->next) { for (field = event->format.fields; field; field = field->next) {
if (field->flags & FIELD_IS_STRING) { if (field->flags & FIELD_IS_STRING) {
int offset; int offset;
...@@ -272,27 +288,31 @@ static void python_process_event(int cpu, void *data, ...@@ -272,27 +288,31 @@ static void python_process_event(int cpu, void *data,
obj = PyLong_FromUnsignedLongLong(val); obj = PyLong_FromUnsignedLongLong(val);
} }
} }
if (handler)
PyTuple_SetItem(t, n++, obj); PyTuple_SetItem(t, n++, obj);
else
PyDict_SetItemString(dict, field->name, obj);
} }
if (!handler)
PyTuple_SetItem(t, n++, dict);
if (_PyTuple_Resize(&t, n) == -1) if (_PyTuple_Resize(&t, n) == -1)
Py_FatalError("error resizing Python tuple"); Py_FatalError("error resizing Python tuple");
handler = PyDict_GetItemString(main_dict, handler_name); if (handler) {
if (handler && PyCallable_Check(handler)) {
retval = PyObject_CallObject(handler, t); retval = PyObject_CallObject(handler, t);
if (retval == NULL) if (retval == NULL)
handler_call_die(handler_name); handler_call_die(handler_name);
} else { } else {
handler = PyDict_GetItemString(main_dict, "trace_unhandled"); handler = PyDict_GetItemString(main_dict, "trace_unhandled");
if (handler && PyCallable_Check(handler)) { if (handler && PyCallable_Check(handler)) {
if (_PyTuple_Resize(&t, N_COMMON_FIELDS) == -1)
Py_FatalError("error resizing Python tuple");
retval = PyObject_CallObject(handler, t); retval = PyObject_CallObject(handler, t);
if (retval == NULL) if (retval == NULL)
handler_call_die("trace_unhandled"); handler_call_die("trace_unhandled");
} }
Py_DECREF(dict);
} }
Py_DECREF(t); Py_DECREF(t);
...@@ -548,12 +568,10 @@ static int python_generate_script(const char *outfile) ...@@ -548,12 +568,10 @@ static int python_generate_script(const char *outfile)
} }
fprintf(ofp, "def trace_unhandled(event_name, context, " fprintf(ofp, "def trace_unhandled(event_name, context, "
"common_cpu, common_secs, common_nsecs,\n\t\t" "event_fields_dict):\n");
"common_pid, common_comm):\n");
fprintf(ofp, "\t\tprint_header(event_name, common_cpu, " fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
"common_secs, common_nsecs,\n\t\tcommon_pid, " "for k,v in sorted(event_fields_dict.items())])\n\n");
"common_comm)\n\n");
fprintf(ofp, "def print_header(" fprintf(ofp, "def print_header("
"event_name, cpu, secs, nsecs, pid, comm):\n" "event_name, cpu, secs, nsecs, pid, comm):\n"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment