* [PATCH v2 1/3] perf script: Add general python handler to process non-tracepoint events @ 2012-05-31 6:34 Feng Tang 2012-05-31 6:34 ` [PATCH v2 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" Feng Tang 2012-05-31 6:34 ` [PATCH v2 3/3] perf script/python: Pass thread/dso name and symbol info to event handler in python Feng Tang 0 siblings, 2 replies; 6+ messages in thread From: Feng Tang @ 2012-05-31 6:34 UTC (permalink / raw) To: Arnaldo Carvalho de Melo, David Ahern, Peter Zijlstra, Ingo Molnar, Andi Kleen, Robert Richter, linux-kernel Cc: Feng Tang This patch just follows Robert Richter's idea and the commit 37a058ea0 "perf script: Add generic perl handler to process events" to similarly add a python handler for general events other than tracepoints. For non-tracepoint events, this patch will try to find a function named "process_event" in the python script, and pass the event attribute, perf_sample, raw_data in format of raw string. And the python script can use "struct" module's unpack function to disasemble the needed info and process. Signed-off-by: Feng Tang <feng.tang@intel.com> --- .../util/scripting-engines/trace-event-python.c | 60 +++++++++++++++++++- 1 files changed, 59 insertions(+), 1 deletions(-) diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index acb9795..ab441a4 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -31,6 +31,7 @@ #include "../event.h" #include "../thread.h" #include "../trace-event.h" +#include "../evsel.h" PyMODINIT_FUNC initperf_trace_context(void); @@ -209,7 +210,7 @@ static inline struct event_format *find_cache_event(int type) return event; } -static void python_process_event(union perf_event *pevent __unused, +static void python_process_tracepoint(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel __unused, struct machine *machine __unused, @@ -328,6 +329,63 @@ static void python_process_event(union perf_event *pevent __unused, Py_DECREF(t); } +static void python_process_general_event(union perf_event *pevent __unused, + struct perf_sample *sample, + struct perf_evsel *evsel, + struct machine *machine __unused, + struct thread *thread __unused) +{ + PyObject *handler, *retval, *t; + static char handler_name[64]; + unsigned n = 0; + void *data = sample->raw_data; + + t = PyTuple_New(MAX_FIELDS); + if (!t) + Py_FatalError("couldn't create Python tuple"); + + snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); + + handler = PyDict_GetItemString(main_dict, handler_name); + if (handler && !PyCallable_Check(handler)) { + handler = NULL; + goto exit; + } + + /* Pass 3 parameters: event_attr, perf_sample, raw data */ + PyTuple_SetItem(t, n++, PyString_FromStringAndSize( + (const char *)&evsel->attr, sizeof(evsel->attr))); + PyTuple_SetItem(t, n++, PyString_FromStringAndSize( + (const char *)sample, sizeof(*sample))); + PyTuple_SetItem(t, n++, PyString_FromStringAndSize( + data, sample->raw_size)); + + if (_PyTuple_Resize(&t, n) == -1) + Py_FatalError("error resizing Python tuple"); + + retval = PyObject_CallObject(handler, t); + if (retval == NULL) + handler_call_die(handler_name); +exit: + Py_DECREF(t); +} + +static void python_process_event(union perf_event *pevent, + struct perf_sample *sample, + struct perf_evsel *evsel, + struct machine *machine, + struct thread *thread) +{ + switch (evsel->attr.type) { + case PERF_TYPE_TRACEPOINT: + python_process_tracepoint(pevent, sample, evsel, machine, thread); + break; + /* Reserve for future process_hw/sw/raw APIs */ + default: + python_process_general_event(pevent, sample, evsel, machine, thread); + } +} + static int run_start_sub(void) { PyObject *handler, *retval; -- 1.7.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" 2012-05-31 6:34 [PATCH v2 1/3] perf script: Add general python handler to process non-tracepoint events Feng Tang @ 2012-05-31 6:34 ` Feng Tang 2012-05-31 14:50 ` Arnaldo Carvalho de Melo 2012-05-31 6:34 ` [PATCH v2 3/3] perf script/python: Pass thread/dso name and symbol info to event handler in python Feng Tang 1 sibling, 1 reply; 6+ messages in thread From: Feng Tang @ 2012-05-31 6:34 UTC (permalink / raw) To: Arnaldo Carvalho de Melo, David Ahern, Peter Zijlstra, Ingo Molnar, Andi Kleen, Robert Richter, linux-kernel Cc: Feng Tang Both perl and python script start processing events other than trace points, and it's useful to pass the resolved symbol and the dso info to the event handler in script for better analysis and statistics. Struct thread is already a member of struct addr_location, using addr_location will keep the thread info, while providing additional symbol and dso info if exist, so that the script itself doesn't need to bother to do the symbol resolving and dso searching work. Signed-off-by: Feng Tang <feng.tang@intel.com> Acked-by: David Ahern <dsahern@gmail.com> Tested-by: David Ahern <dsahern@gmail.com> --- tools/perf/builtin-script.c | 5 +++-- .../perf/util/scripting-engines/trace-event-perl.c | 11 ++++++----- .../util/scripting-engines/trace-event-python.c | 13 +++++++------ tools/perf/util/trace-event-scripting.c | 2 +- tools/perf/util/trace-event.h | 4 +++- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 8e395a5..282e206 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -405,9 +405,10 @@ static void process_event(union perf_event *event __unused, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread) + struct addr_location *al __unused) { struct perf_event_attr *attr = &evsel->attr; + struct thread *thread = al->thread; if (output[attr->type].fields == 0) return; @@ -520,7 +521,7 @@ static int process_sample_event(struct perf_tool *tool __used, if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) return 0; - scripting_ops->process_event(event, sample, evsel, machine, thread); + scripting_ops->process_event(event, sample, evsel, machine, &al); evsel->hists.stats.total_period += sample->period; return 0; diff --git a/tools/perf/util/scripting-engines/trace-event-perl.c b/tools/perf/util/scripting-engines/trace-event-perl.c index 4c1b3d7..af9a6a4 100644 --- a/tools/perf/util/scripting-engines/trace-event-perl.c +++ b/tools/perf/util/scripting-engines/trace-event-perl.c @@ -256,7 +256,7 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine __unused, - struct thread *thread) + struct addr_location *al __unused) { struct format_field *field; static char handler[256]; @@ -268,6 +268,7 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, int cpu = sample->cpu; void *data = sample->raw_data; unsigned long long nsecs = sample->time; + struct thread *thread = al->thread; char *comm = thread->comm; dSP; @@ -346,7 +347,7 @@ static void perl_process_event_generic(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel __unused, struct machine *machine __unused, - struct thread *thread __unused) + struct addr_location *al __unused) { dSP; @@ -372,10 +373,10 @@ static void perl_process_event(union perf_event *pevent, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread) + struct addr_location *al) { - perl_process_tracepoint(pevent, sample, evsel, machine, thread); - perl_process_event_generic(pevent, sample, evsel, machine, thread); + perl_process_tracepoint(pevent, sample, evsel, machine, al); + perl_process_event_generic(pevent, sample, evsel, machine, al); } static void run_start_sub(void) diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index ab441a4..97f4fad 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -214,7 +214,7 @@ static void python_process_tracepoint(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel __unused, struct machine *machine __unused, - struct thread *thread) + struct addr_location *al) { PyObject *handler, *retval, *context, *t, *obj, *dict = NULL; static char handler_name[256]; @@ -228,6 +228,7 @@ static void python_process_tracepoint(union perf_event *pevent __unused, int cpu = sample->cpu; void *data = sample->raw_data; unsigned long long nsecs = sample->time; + struct thread *thread = al->thread; char *comm = thread->comm; t = PyTuple_New(MAX_FIELDS); @@ -333,7 +334,7 @@ static void python_process_general_event(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine __unused, - struct thread *thread __unused) + struct addr_location *al) { PyObject *handler, *retval, *t; static char handler_name[64]; @@ -352,7 +353,7 @@ static void python_process_general_event(union perf_event *pevent __unused, goto exit; } - /* Pass 3 parameters: event_attr, perf_sample, raw data */ + /* Pass 4 parameters: event_attr, perf_sample, raw data, thread name */ PyTuple_SetItem(t, n++, PyString_FromStringAndSize( (const char *)&evsel->attr, sizeof(evsel->attr))); PyTuple_SetItem(t, n++, PyString_FromStringAndSize( @@ -374,15 +375,15 @@ static void python_process_event(union perf_event *pevent, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread) + struct addr_location *al) { switch (evsel->attr.type) { case PERF_TYPE_TRACEPOINT: - python_process_tracepoint(pevent, sample, evsel, machine, thread); + python_process_tracepoint(pevent, sample, evsel, machine, al); break; /* Reserve for future process_hw/sw/raw APIs */ default: - python_process_general_event(pevent, sample, evsel, machine, thread); + python_process_general_event(pevent, sample, evsel, machine, al); } } diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c index 18ae6c1..b26459e 100644 --- a/tools/perf/util/trace-event-scripting.c +++ b/tools/perf/util/trace-event-scripting.c @@ -39,7 +39,7 @@ static void process_event_unsupported(union perf_event *event __unused, struct perf_sample *sample __unused, struct perf_evsel *evsel __unused, struct machine *machine __unused, - struct thread *thread __unused) + struct addr_location *al __unused) { } diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h index 639852a..7b49397 100644 --- a/tools/perf/util/trace-event.h +++ b/tools/perf/util/trace-event.h @@ -72,6 +72,8 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs, void tracing_data_put(struct tracing_data *tdata); +struct addr_location; + struct scripting_ops { const char *name; int (*start_script) (const char *script, int argc, const char **argv); @@ -80,7 +82,7 @@ struct scripting_ops { struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread); + struct addr_location *al); int (*generate_script) (const char *outfile); }; -- 1.7.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" 2012-05-31 6:34 ` [PATCH v2 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" Feng Tang @ 2012-05-31 14:50 ` Arnaldo Carvalho de Melo 2012-06-01 1:45 ` [PATCH v3 " Feng Tang 0 siblings, 1 reply; 6+ messages in thread From: Arnaldo Carvalho de Melo @ 2012-05-31 14:50 UTC (permalink / raw) To: Feng Tang Cc: David Ahern, Peter Zijlstra, Ingo Molnar, Andi Kleen, Robert Richter, linux-kernel Em Thu, May 31, 2012 at 02:34:31PM +0800, Feng Tang escreveu: > diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c > index 8e395a5..282e206 100644 > --- a/tools/perf/builtin-script.c > +++ b/tools/perf/builtin-script.c > @@ -405,9 +405,10 @@ static void process_event(union perf_event *event __unused, > struct perf_sample *sample, > struct perf_evsel *evsel, > struct machine *machine, > - struct thread *thread) > + struct addr_location *al __unused) Why the __unused modifier, since you... > { > struct perf_event_attr *attr = &evsel->attr; > + struct thread *thread = al->thread; ... are using al? > if (output[attr->type].fields == 0) > return; > @@ -520,7 +521,7 @@ static int process_sample_event(struct perf_tool *tool __used, > if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) > return 0; > > - scripting_ops->process_event(event, sample, evsel, machine, thread); > + scripting_ops->process_event(event, sample, evsel, machine, &al); > > evsel->hists.stats.total_period += sample->period; > return 0; > diff --git a/tools/perf/util/scripting-engines/trace-event-perl.c b/tools/perf/util/scripting-engines/trace-event-perl.c > index 4c1b3d7..af9a6a4 100644 > --- a/tools/perf/util/scripting-engines/trace-event-perl.c > +++ b/tools/perf/util/scripting-engines/trace-event-perl.c > @@ -256,7 +256,7 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, > struct perf_sample *sample, > struct perf_evsel *evsel, > struct machine *machine __unused, > - struct thread *thread) > + struct addr_location *al __unused) Ditto > { > struct format_field *field; > static char handler[256]; > @@ -268,6 +268,7 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, > int cpu = sample->cpu; > void *data = sample->raw_data; > unsigned long long nsecs = sample->time; > + struct thread *thread = al->thread; > char *comm = thread->comm; > > dSP; > @@ -346,7 +347,7 @@ static void perl_process_event_generic(union perf_event *pevent __unused, > struct perf_sample *sample, > struct perf_evsel *evsel __unused, > struct machine *machine __unused, > - struct thread *thread __unused) > + struct addr_location *al __unused) Here it seems ok > { > dSP; > > @@ -372,10 +373,10 @@ static void perl_process_event(union perf_event *pevent, > struct perf_sample *sample, > struct perf_evsel *evsel, > struct machine *machine, > - struct thread *thread) > + struct addr_location *al) And here it is correctly without the __unused marker Check the others please ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" 2012-05-31 14:50 ` Arnaldo Carvalho de Melo @ 2012-06-01 1:45 ` Feng Tang 0 siblings, 0 replies; 6+ messages in thread From: Feng Tang @ 2012-06-01 1:45 UTC (permalink / raw) To: Arnaldo Carvalho de Melo Cc: David Ahern, Peter Zijlstra, Ingo Molnar, Andi Kleen, Robert Richter, linux-kernel >From 124d1d8b1b5c41e3d6593fec234c27bdaedf3331 Mon Sep 17 00:00:00 2001 From: Feng Tang <feng.tang@intel.com> Date: Thu, 31 May 2012 10:54:57 +0800 Subject: [PATCH v3 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" Both perl and python script start processing events other than trace points, and it's useful to pass the resolved symbol and the dso info to the event handler in script for better analysis and statistics. Struct thread is already a member of struct addr_location, using addr_location will keep the thread info, while providing additional symbol and dso info if exist, so that the script itself doesn't need to bother to do the symbol resolving and dso searching work. v3: Correct "__unused" usage in v2 patch. Remove a wrong "__unused" in perl_process_event_generic() Signed-off-by: Feng Tang <feng.tang@intel.com> Acked-by: David Ahern <dsahern@gmail.com> Tested-by: David Ahern <dsahern@gmail.com> --- tools/perf/builtin-script.c | 5 +++-- .../perf/util/scripting-engines/trace-event-perl.c | 13 +++++++------ .../util/scripting-engines/trace-event-python.c | 13 +++++++------ tools/perf/util/trace-event-scripting.c | 2 +- tools/perf/util/trace-event.h | 4 +++- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 8e395a5..faddacc 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -405,9 +405,10 @@ static void process_event(union perf_event *event __unused, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread) + struct addr_location *al) { struct perf_event_attr *attr = &evsel->attr; + struct thread *thread = al->thread; if (output[attr->type].fields == 0) return; @@ -520,7 +521,7 @@ static int process_sample_event(struct perf_tool *tool __used, if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) return 0; - scripting_ops->process_event(event, sample, evsel, machine, thread); + scripting_ops->process_event(event, sample, evsel, machine, &al); evsel->hists.stats.total_period += sample->period; return 0; diff --git a/tools/perf/util/scripting-engines/trace-event-perl.c b/tools/perf/util/scripting-engines/trace-event-perl.c index 4c1b3d7..997ef5d 100644 --- a/tools/perf/util/scripting-engines/trace-event-perl.c +++ b/tools/perf/util/scripting-engines/trace-event-perl.c @@ -256,7 +256,7 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine __unused, - struct thread *thread) + struct addr_location *al) { struct format_field *field; static char handler[256]; @@ -268,6 +268,7 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, int cpu = sample->cpu; void *data = sample->raw_data; unsigned long long nsecs = sample->time; + struct thread *thread = al->thread; char *comm = thread->comm; dSP; @@ -344,9 +345,9 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, static void perl_process_event_generic(union perf_event *pevent __unused, struct perf_sample *sample, - struct perf_evsel *evsel __unused, + struct perf_evsel *evsel, struct machine *machine __unused, - struct thread *thread __unused) + struct addr_location *al __unused) { dSP; @@ -372,10 +373,10 @@ static void perl_process_event(union perf_event *pevent, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread) + struct addr_location *al) { - perl_process_tracepoint(pevent, sample, evsel, machine, thread); - perl_process_event_generic(pevent, sample, evsel, machine, thread); + perl_process_tracepoint(pevent, sample, evsel, machine, al); + perl_process_event_generic(pevent, sample, evsel, machine, al); } static void run_start_sub(void) diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index ab441a4..97f4fad 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -214,7 +214,7 @@ static void python_process_tracepoint(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel __unused, struct machine *machine __unused, - struct thread *thread) + struct addr_location *al) { PyObject *handler, *retval, *context, *t, *obj, *dict = NULL; static char handler_name[256]; @@ -228,6 +228,7 @@ static void python_process_tracepoint(union perf_event *pevent __unused, int cpu = sample->cpu; void *data = sample->raw_data; unsigned long long nsecs = sample->time; + struct thread *thread = al->thread; char *comm = thread->comm; t = PyTuple_New(MAX_FIELDS); @@ -333,7 +334,7 @@ static void python_process_general_event(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine __unused, - struct thread *thread __unused) + struct addr_location *al) { PyObject *handler, *retval, *t; static char handler_name[64]; @@ -352,7 +353,7 @@ static void python_process_general_event(union perf_event *pevent __unused, goto exit; } - /* Pass 3 parameters: event_attr, perf_sample, raw data */ + /* Pass 4 parameters: event_attr, perf_sample, raw data, thread name */ PyTuple_SetItem(t, n++, PyString_FromStringAndSize( (const char *)&evsel->attr, sizeof(evsel->attr))); PyTuple_SetItem(t, n++, PyString_FromStringAndSize( @@ -374,15 +375,15 @@ static void python_process_event(union perf_event *pevent, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread) + struct addr_location *al) { switch (evsel->attr.type) { case PERF_TYPE_TRACEPOINT: - python_process_tracepoint(pevent, sample, evsel, machine, thread); + python_process_tracepoint(pevent, sample, evsel, machine, al); break; /* Reserve for future process_hw/sw/raw APIs */ default: - python_process_general_event(pevent, sample, evsel, machine, thread); + python_process_general_event(pevent, sample, evsel, machine, al); } } diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c index 18ae6c1..b26459e 100644 --- a/tools/perf/util/trace-event-scripting.c +++ b/tools/perf/util/trace-event-scripting.c @@ -39,7 +39,7 @@ static void process_event_unsupported(union perf_event *event __unused, struct perf_sample *sample __unused, struct perf_evsel *evsel __unused, struct machine *machine __unused, - struct thread *thread __unused) + struct addr_location *al __unused) { } diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h index 639852a..7b49397 100644 --- a/tools/perf/util/trace-event.h +++ b/tools/perf/util/trace-event.h @@ -72,6 +72,8 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs, void tracing_data_put(struct tracing_data *tdata); +struct addr_location; + struct scripting_ops { const char *name; int (*start_script) (const char *script, int argc, const char **argv); @@ -80,7 +82,7 @@ struct scripting_ops { struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread); + struct addr_location *al); int (*generate_script) (const char *outfile); }; -- 1.7.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v2 3/3] perf script/python: Pass thread/dso name and symbol info to event handler in python 2012-05-31 6:34 [PATCH v2 1/3] perf script: Add general python handler to process non-tracepoint events Feng Tang 2012-05-31 6:34 ` [PATCH v2 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" Feng Tang @ 2012-05-31 6:34 ` Feng Tang 1 sibling, 0 replies; 6+ messages in thread From: Feng Tang @ 2012-05-31 6:34 UTC (permalink / raw) To: Arnaldo Carvalho de Melo, David Ahern, Peter Zijlstra, Ingo Molnar, Andi Kleen, Robert Richter, linux-kernel Cc: Feng Tang Also as suggested by Arnaldo, pack all these parameters to a dictionary, which is more expandable for adding new parameters while keep the compatibility for old scripts. Signed-off-by: Feng Tang <feng.tang@intel.com> --- .../util/scripting-engines/trace-event-python.c | 31 +++++++++++++++---- 1 files changed, 24 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index 97f4fad..92f1833 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -336,15 +336,23 @@ static void python_process_general_event(union perf_event *pevent __unused, struct machine *machine __unused, struct addr_location *al) { - PyObject *handler, *retval, *t; + PyObject *handler, *retval, *t, *dict; static char handler_name[64]; unsigned n = 0; - void *data = sample->raw_data; + struct thread *thread = al->thread; + /* + * Use the MAX_FIELDS to make the function expandable, though + * currently there is only one itme for the tuple. + */ t = PyTuple_New(MAX_FIELDS); if (!t) Py_FatalError("couldn't create Python tuple"); + dict = PyDict_New(); + if (!dict) + Py_FatalError("couldn't create Python dictionary"); + snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); handler = PyDict_GetItemString(main_dict, handler_name); @@ -353,14 +361,22 @@ static void python_process_general_event(union perf_event *pevent __unused, goto exit; } - /* Pass 4 parameters: event_attr, perf_sample, raw data, thread name */ - PyTuple_SetItem(t, n++, PyString_FromStringAndSize( + PyDict_SetItemString(dict, "attr", PyString_FromStringAndSize( (const char *)&evsel->attr, sizeof(evsel->attr))); - PyTuple_SetItem(t, n++, PyString_FromStringAndSize( + PyDict_SetItemString(dict, "sample", PyString_FromStringAndSize( (const char *)sample, sizeof(*sample))); - PyTuple_SetItem(t, n++, PyString_FromStringAndSize( - data, sample->raw_size)); + PyDict_SetItemString(dict, "raw_buf", PyString_FromStringAndSize( + (const char *)sample->raw_data, sample->raw_size)); + PyDict_SetItemString(dict, "comm", + PyString_FromString(thread->comm)); + PyDict_SetItemString(dict, "dso", + PyString_FromString(al->map->dso->name)); + if (al->sym) { + PyDict_SetItemString(dict, "symbol", + PyString_FromString(al->sym->name)); + } + PyTuple_SetItem(t, n++, dict); if (_PyTuple_Resize(&t, n) == -1) Py_FatalError("error resizing Python tuple"); @@ -368,6 +384,7 @@ static void python_process_general_event(union perf_event *pevent __unused, if (retval == NULL) handler_call_die(handler_name); exit: + Py_DECREF(dict); Py_DECREF(t); } -- 1.7.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH v3 0/3] perf script: Add general event support to event handler of python script
@ 2012-06-18 6:10 Feng Tang
2012-06-18 6:10 ` [PATCH v3 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" Feng Tang
0 siblings, 1 reply; 6+ messages in thread
From: Feng Tang @ 2012-06-18 6:10 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, David Ahern, Peter Zijlstra,
Ingo Molnar, linux-kernel
Cc: Robert Richter, Andi Kleen, Stephane Eranian, Feng Tang
Current python script can only handle trace point type of events, this
patch serie try to follow Robert Richter's idea in commit 37a058ea0
"perf script: Add generic perl handler to process events"
to similarly add a python handler for general events other than
tracepoints.
Please help to revew, thanks.
- Feng
Change logs:
Since v2:
* Correct some __unused usage
* Add a new parameter: event name to event handler
Since v1:
* Use dictory as the paramter passwd from c to python
* Use "process_event" name to comply with perl
----------------
Feng Tang (3):
perf script: Add general python handler to process non-tracepoint
events
perf script: Replace "struct thread" with "struct addr_location" as a
parameter for "process_event()"
perf script/python: Pass event/thread/dso name and symbol info to
event handler in python
tools/perf/builtin-script.c | 5 +-
.../perf/util/scripting-engines/trace-event-perl.c | 13 ++--
.../util/scripting-engines/trace-event-python.c | 81 +++++++++++++++++++-
tools/perf/util/trace-event-scripting.c | 2 +-
tools/perf/util/trace-event.h | 4 +-
5 files changed, 93 insertions(+), 12 deletions(-)
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v3 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" 2012-06-18 6:10 [PATCH v3 0/3] perf script: Add general event support to event handler of python script Feng Tang @ 2012-06-18 6:10 ` Feng Tang 0 siblings, 0 replies; 6+ messages in thread From: Feng Tang @ 2012-06-18 6:10 UTC (permalink / raw) To: Arnaldo Carvalho de Melo, David Ahern, Peter Zijlstra, Ingo Molnar, linux-kernel Cc: Robert Richter, Andi Kleen, Stephane Eranian, Feng Tang Both perl and python script start processing events other than trace points, and it's useful to pass the resolved symbol and the dso info to the event handler in script for better analysis and statistics. Struct thread is already a member of struct addr_location, using addr_location will keep the thread info, while providing additional symbol and dso info if exist, so that the script itself doesn't need to bother to do the symbol resolving and dso searching work. Signed-off-by: Feng Tang <feng.tang@intel.com> Acked-by: David Ahern <dsahern@gmail.com> Tested-by: David Ahern <dsahern@gmail.com> --- tools/perf/builtin-script.c | 5 +++-- .../perf/util/scripting-engines/trace-event-perl.c | 13 +++++++------ .../util/scripting-engines/trace-event-python.c | 13 +++++++------ tools/perf/util/trace-event-scripting.c | 2 +- tools/perf/util/trace-event.h | 4 +++- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 8e395a5..faddacc 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -405,9 +405,10 @@ static void process_event(union perf_event *event __unused, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread) + struct addr_location *al) { struct perf_event_attr *attr = &evsel->attr; + struct thread *thread = al->thread; if (output[attr->type].fields == 0) return; @@ -520,7 +521,7 @@ static int process_sample_event(struct perf_tool *tool __used, if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) return 0; - scripting_ops->process_event(event, sample, evsel, machine, thread); + scripting_ops->process_event(event, sample, evsel, machine, &al); evsel->hists.stats.total_period += sample->period; return 0; diff --git a/tools/perf/util/scripting-engines/trace-event-perl.c b/tools/perf/util/scripting-engines/trace-event-perl.c index 4c1b3d7..997ef5d 100644 --- a/tools/perf/util/scripting-engines/trace-event-perl.c +++ b/tools/perf/util/scripting-engines/trace-event-perl.c @@ -256,7 +256,7 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine __unused, - struct thread *thread) + struct addr_location *al) { struct format_field *field; static char handler[256]; @@ -268,6 +268,7 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, int cpu = sample->cpu; void *data = sample->raw_data; unsigned long long nsecs = sample->time; + struct thread *thread = al->thread; char *comm = thread->comm; dSP; @@ -344,9 +345,9 @@ static void perl_process_tracepoint(union perf_event *pevent __unused, static void perl_process_event_generic(union perf_event *pevent __unused, struct perf_sample *sample, - struct perf_evsel *evsel __unused, + struct perf_evsel *evsel, struct machine *machine __unused, - struct thread *thread __unused) + struct addr_location *al __unused) { dSP; @@ -372,10 +373,10 @@ static void perl_process_event(union perf_event *pevent, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread) + struct addr_location *al) { - perl_process_tracepoint(pevent, sample, evsel, machine, thread); - perl_process_event_generic(pevent, sample, evsel, machine, thread); + perl_process_tracepoint(pevent, sample, evsel, machine, al); + perl_process_event_generic(pevent, sample, evsel, machine, al); } static void run_start_sub(void) diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index ab441a4..97f4fad 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c @@ -214,7 +214,7 @@ static void python_process_tracepoint(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel __unused, struct machine *machine __unused, - struct thread *thread) + struct addr_location *al) { PyObject *handler, *retval, *context, *t, *obj, *dict = NULL; static char handler_name[256]; @@ -228,6 +228,7 @@ static void python_process_tracepoint(union perf_event *pevent __unused, int cpu = sample->cpu; void *data = sample->raw_data; unsigned long long nsecs = sample->time; + struct thread *thread = al->thread; char *comm = thread->comm; t = PyTuple_New(MAX_FIELDS); @@ -333,7 +334,7 @@ static void python_process_general_event(union perf_event *pevent __unused, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine __unused, - struct thread *thread __unused) + struct addr_location *al) { PyObject *handler, *retval, *t; static char handler_name[64]; @@ -352,7 +353,7 @@ static void python_process_general_event(union perf_event *pevent __unused, goto exit; } - /* Pass 3 parameters: event_attr, perf_sample, raw data */ + /* Pass 4 parameters: event_attr, perf_sample, raw data, thread name */ PyTuple_SetItem(t, n++, PyString_FromStringAndSize( (const char *)&evsel->attr, sizeof(evsel->attr))); PyTuple_SetItem(t, n++, PyString_FromStringAndSize( @@ -374,15 +375,15 @@ static void python_process_event(union perf_event *pevent, struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread) + struct addr_location *al) { switch (evsel->attr.type) { case PERF_TYPE_TRACEPOINT: - python_process_tracepoint(pevent, sample, evsel, machine, thread); + python_process_tracepoint(pevent, sample, evsel, machine, al); break; /* Reserve for future process_hw/sw/raw APIs */ default: - python_process_general_event(pevent, sample, evsel, machine, thread); + python_process_general_event(pevent, sample, evsel, machine, al); } } diff --git a/tools/perf/util/trace-event-scripting.c b/tools/perf/util/trace-event-scripting.c index 18ae6c1..b26459e 100644 --- a/tools/perf/util/trace-event-scripting.c +++ b/tools/perf/util/trace-event-scripting.c @@ -39,7 +39,7 @@ static void process_event_unsupported(union perf_event *event __unused, struct perf_sample *sample __unused, struct perf_evsel *evsel __unused, struct machine *machine __unused, - struct thread *thread __unused) + struct addr_location *al __unused) { } diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h index 639852a..7b49397 100644 --- a/tools/perf/util/trace-event.h +++ b/tools/perf/util/trace-event.h @@ -72,6 +72,8 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs, void tracing_data_put(struct tracing_data *tdata); +struct addr_location; + struct scripting_ops { const char *name; int (*start_script) (const char *script, int argc, const char **argv); @@ -80,7 +82,7 @@ struct scripting_ops { struct perf_sample *sample, struct perf_evsel *evsel, struct machine *machine, - struct thread *thread); + struct addr_location *al); int (*generate_script) (const char *outfile); }; -- 1.7.1 ^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2012-06-18 6:14 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2012-05-31 6:34 [PATCH v2 1/3] perf script: Add general python handler to process non-tracepoint events Feng Tang 2012-05-31 6:34 ` [PATCH v2 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" Feng Tang 2012-05-31 14:50 ` Arnaldo Carvalho de Melo 2012-06-01 1:45 ` [PATCH v3 " Feng Tang 2012-05-31 6:34 ` [PATCH v2 3/3] perf script/python: Pass thread/dso name and symbol info to event handler in python Feng Tang -- strict thread matches above, loose matches on Subject: below -- 2012-06-18 6:10 [PATCH v3 0/3] perf script: Add general event support to event handler of python script Feng Tang 2012-06-18 6:10 ` [PATCH v3 2/3] perf script: Replace "struct thread" with "struct addr_location" as a parameter for "process_event()" Feng Tang
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox