perf: ftrace: Add filter support for option -F/--funcs
PATCH RESEND 1/1 perf: ftrace: Add filter support for option -F/--funcs
Same as 'perf probe -F', this patch adds filter support for the ftrace subcommand option '-F, --funcs <[FILTER]>'.
Here is an example that only lists functions which start with 'vfs_':
$ sudo perf ftrace -F vfs_* vfs_fadvise vfs_fallocate vfs_truncate vfs_open vfs_setpos vfs_llseek vfs_readf vfs_writef ...
--- tools/perf/Documentation/perf-ftrace.txt | 3 +- tools/perf/builtin-ftrace.c | 84 ++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 7 deletions(-)
tools/perf/Documentation/perf-ftrace.txt +2 -1
@@ -33,7 +33,8 @@ OPTIONS -F:: --funcs:: - List all available functions to trace. + List available functions to trace. It accepts a pattern to + only list interested functions. -p:: --pid=::
tools/perf/builtin-ftrace.c +78 -6
120 lines not on this page · raw message
Signature · 1 line
2.25.1
Arnaldo Carvalho de Melo
maintainerEm Fri, Sep 04, 2020 at 11:23:57PM +0800, Changbin Du escreveu:
Quoted 4 lines
Same as 'perf probe -F', this patch adds filter support for the ftrace subcommand option '-F, --funcs <[FILTER]>'.
Here is an example that only lists functions which start with '
vfs_':
Quoted 10 lines
$ sudo perf ftrace -F vfs_* vfs_fadvise vfs_fallocate vfs_truncate vfs_open vfs_setpos vfs_llseek vfs_readf vfs_writef ...
I'll process these now, the urgent ones were already sent to Linus, so I will now concentrate on the new stuff for v5.10,
Thanks for working on this!
- Arnaldo
Quoted 154 lines
Suggested-by Arnaldo Carvalho de Melo <[email protected]>Signed-off-by Changbin Du <[email protected]>--- tools/perf/Documentation/perf-ftrace.txt | 3 +- tools/perf/builtin-ftrace.c | 84 ++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 7 deletions(-)tools/perf/Documentation/perf-ftrace.txt +2
-1@@ -33,7 +33,8 @@ OPTIONS -F:: --funcs:: - List all available functions to trace. + List available functions to trace. It accepts a pattern to + only list interested functions. -p:: --pid=::tools/perf/builtin-ftrace.c +78
-6@@ -25,6 +25,7 @@#include "target.h" #include "cpumap.h" #include "thread_map.h" +#include "strfilter.h" #include "util/cap.h" #include "util/config.h" #include "util/units.h" @@ -36,7 +37,6 @@ struct perf_ftrace {struct evlist *evlist;struct target target;const char *tracer; -bool list_avail_functions;struct list_head filters;struct list_head notrace;struct list_head graph_funcs;@@ -181,6 +181,40 @@ static int read_tracing_file_to_stdout(const char *name)return ret; } +static int read_tracing_file_by_line (const char *name, +void (*cb)(char *str,void *arg), +void *cb_arg) +{ +char *line =NULL ; +size_t len =0 ; +char *file; +FILE *fp; + + file =get_tracing_file (name); +if (!file) { +pr_debug ("cannot get tracing file: %s\n" , name); +return -1 ; + } + + fp =fopen (file,"r" ); +if (fp ==NULL ) { +pr_debug ("cannot open tracing file: %s\n" , name); +put_tracing_file (file); +return -1 ; + } + +while (getline (&line, &len, fp) !=-1 ) { +cb (line, cb_arg); + } + +if (line) +free (line); + +fclose (fp); +put_tracing_file (file); +return 0 ; +} +static int write_tracing_file_int (const char *name,int value) {char buf[16 ];@@ -557,9 +591,6 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)signal (SIGCHLD , sig_handler);signal (SIGPIPE , sig_handler); -if (ftrace->list_avail_functions) -return read_tracing_file_to_stdout ("available_filter_functions" ); -if (reset_tracing_files (ftrace) <0 ) {pr_err ("failed to reset ftrace\n" ); goto out;@@ -683,6 +714,46 @@ static int perf_ftrace_config(const char *var, const char *value, void *cb)return -1 ; } +static void list_function_cb (char *str,void *arg) +{ +struct strfilter *filter = (struct strfilter *)arg; + +if (strfilter__compare (filter, str)) +printf ("%s" , str); +} + +static int opt_list_avail_functions (const struct option *opt__maybe_unused , +const char *str,int unset) +{ +struct strfilter *filter; +const char *err =NULL ; +int ret; + +if (unset || !str) +return -1 ; + + filter =strfilter__new (str, &err); +if (!filter) +return err ? -EINVAL : -ENOMEM ; + + ret =strfilter__or (filter, str, &err); +if (ret == -EINVAL ) { +pr_err ("Filter parse error at %td.\n" , err - str +1 ); +pr_err ("Source: \"%s\"\n" , str); +pr_err (" %*c\n" , (int )(err - str +1 ),'^' ); +strfilter__delete (filter); +return ret; + } + + ret =read_tracing_file_by_line ("available_filter_functions" , + list_function_cb, filter); +strfilter__delete (filter); +if (ret <0 ) +return ret; + +exit (0 ); +} +static int parse_filter_func (const struct option *opt,const char *str,int unset__maybe_unused ) {@@ -817,8 +888,9 @@ int cmd_ftrace(int argc, const char **argv)const struct option ftrace_options[] = {OPT_STRING ('t' ,"tracer" , &ftrace.tracer,"tracer" ,"Tracer to use: function_graph(default) or function" ), -OPT_BOOLEAN ('F' ,"funcs" , &ftrace.list_avail_functions, -"Show available functions to filter" ), +OPT_CALLBACK_DEFAULT ('F' ,"funcs" ,NULL ,"[FILTER]" , +"Show available functions to filter" , + opt_list_avail_functions,"*" ),OPT_STRING ('p' ,"pid" , &ftrace.target.pid,"pid" ,"Trace on existing process id" ),/* TODO: Add short option -t after -t/--tracer can be removed. */ Signature · 1 line
2.25.1
Signature · 1 line
- Arnaldo
Em Fri, Sep 04, 2020 at 01:27:16PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 16 lines
Em Fri, Sep 04, 2020 at 11:23:57PM +0800, Changbin Du escreveu:
Quoted 4 lines
Same as 'perf probe -F', this patch adds filter support for the ftrace subcommand option '-F, --funcs <[FILTER]>'.
Here is an example that only lists functions which start with '
vfs_':Quoted 10 lines
$ sudo perf ftrace -F vfs_* vfs_fadvise vfs_fallocate vfs_truncate vfs_open vfs_setpos vfs_llseek vfs_readf vfs_writef ...
Quoted 4 lines
I'll process these now, the urgent ones were already sent to Linus, so I will now concentrate on the new stuff for v5.10,
Thanks for working on this!
Thanks, applied, will go to v5.10, i.e. to my perf/core branch as soon as the usual set of tests pass,
- Arnaldo
Quoted 160 lines
- Arnaldo
Quoted 154 lines
Suggested-by Arnaldo Carvalho de Melo <[email protected]>Signed-off-by Changbin Du <[email protected]>--- tools/perf/Documentation/perf-ftrace.txt | 3 +- tools/perf/builtin-ftrace.c | 84 ++++++++++++++++++++++-- 2 files changed, 80 insertions(+), 7 deletions(-)tools/perf/Documentation/perf-ftrace.txt +2
-1@@ -33,7 +33,8 @@ OPTIONS -F:: --funcs:: - List all available functions to trace. + List available functions to trace. It accepts a pattern to + only list interested functions. -p:: --pid=::tools/perf/builtin-ftrace.c +78
-6@@ -25,6 +25,7 @@#include "target.h" #include "cpumap.h" #include "thread_map.h" +#include "strfilter.h" #include "util/cap.h" #include "util/config.h" #include "util/units.h" @@ -36,7 +37,6 @@ struct perf_ftrace {struct evlist *evlist;struct target target;const char *tracer; -bool list_avail_functions;struct list_head filters;struct list_head notrace;struct list_head graph_funcs;@@ -181,6 +181,40 @@ static int read_tracing_file_to_stdout(const char *name)return ret; } +static int read_tracing_file_by_line (const char *name, +void (*cb)(char *str,void *arg), +void *cb_arg) +{ +char *line =NULL ; +size_t len =0 ; +char *file; +FILE *fp; + + file =get_tracing_file (name); +if (!file) { +pr_debug ("cannot get tracing file: %s\n" , name); +return -1 ; + } + + fp =fopen (file,"r" ); +if (fp ==NULL ) { +pr_debug ("cannot open tracing file: %s\n" , name); +put_tracing_file (file); +return -1 ; + } + +while (getline (&line, &len, fp) !=-1 ) { +cb (line, cb_arg); + } + +if (line) +free (line); + +fclose (fp); +put_tracing_file (file); +return 0 ; +} +static int write_tracing_file_int (const char *name,int value) {char buf[16 ];@@ -557,9 +591,6 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)signal (SIGCHLD , sig_handler);signal (SIGPIPE , sig_handler); -if (ftrace->list_avail_functions) -return read_tracing_file_to_stdout ("available_filter_functions" ); -if (reset_tracing_files (ftrace) <0 ) {pr_err ("failed to reset ftrace\n" ); goto out;@@ -683,6 +714,46 @@ static int perf_ftrace_config(const char *var, const char *value, void *cb)return -1 ; } +static void list_function_cb (char *str,void *arg) +{ +struct strfilter *filter = (struct strfilter *)arg; + +if (strfilter__compare (filter, str)) +printf ("%s" , str); +} + +static int opt_list_avail_functions (const struct option *opt__maybe_unused , +const char *str,int unset) +{ +struct strfilter *filter; +const char *err =NULL ; +int ret; + +if (unset || !str) +return -1 ; + + filter =strfilter__new (str, &err); +if (!filter) +return err ? -EINVAL : -ENOMEM ; + + ret =strfilter__or (filter, str, &err); +if (ret == -EINVAL ) { +pr_err ("Filter parse error at %td.\n" , err - str +1 ); +pr_err ("Source: \"%s\"\n" , str); +pr_err (" %*c\n" , (int )(err - str +1 ),'^' ); +strfilter__delete (filter); +return ret; + } + + ret =read_tracing_file_by_line ("available_filter_functions" , + list_function_cb, filter); +strfilter__delete (filter); +if (ret <0 ) +return ret; + +exit (0 ); +} +static int parse_filter_func (const struct option *opt,const char *str,int unset__maybe_unused ) {@@ -817,8 +888,9 @@ int cmd_ftrace(int argc, const char **argv)const struct option ftrace_options[] = {OPT_STRING ('t' ,"tracer" , &ftrace.tracer,"tracer" ,"Tracer to use: function_graph(default) or function" ), -OPT_BOOLEAN ('F' ,"funcs" , &ftrace.list_avail_functions, -"Show available functions to filter" ), +OPT_CALLBACK_DEFAULT ('F' ,"funcs" ,NULL ,"[FILTER]" , +"Show available functions to filter" , + opt_list_avail_functions,"*" ),OPT_STRING ('p' ,"pid" , &ftrace.target.pid,"pid" ,"Trace on existing process id" ),/* TODO: Add short option -t after -t/--tracer can be removed. */ Signature · 1 line
2.25.1Signature · 1 line
- Arnaldo
Signature · 1 line
- Arnaldo
Quoted 20 lines · Arnaldo Carvalho de Melo
Em Fri, Sep 04, 2020 at 01:27:16PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 19 lines
Em Fri, Sep 04, 2020 at 11:23:57PM +0800, Changbin Du escreveu:
Quoted 4 lines
Same as 'perf probe -F', this patch adds filter support for the ftrace subcommand option '-F, --funcs <[FILTER]>'.
Here is an example that only lists functions which start with '
vfs_':Quoted 10 lines
$ sudo perf ftrace -F vfs_* vfs_fadvise vfs_fallocate vfs_truncate vfs_open vfs_setpos vfs_llseek vfs_readf vfs_writef ...I'll process these now, the urgent ones were already sent to Linus, so I will now concentrate on the new stuff for v5.10,
Quoted 6 lines
Quoted 1 line
Thanks for working on this!
Thanks, applied, will go to v5.10, i.e. to my perf/core branch as soon as the usual set of tests pass,
- Arnaldo
Got it. Thank you!
Signature · 2 lines
Cheers, Changbin Du