Re: [RESEND PATCH] perf: ftrace: Add filter support for option -F/--funcs
From: Arnaldo Carvalho de Melo
Date: Fri Sep 04 2020 - 12:27:21 EST
Em Fri, Sep 04, 2020 at 11:23:57PM +0800, Changbin Du escreveu:
> 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
> ...
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
> Suggested-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
> Signed-off-by: Changbin Du <changbin.du@xxxxxxxxx>
> ---
> tools/perf/Documentation/perf-ftrace.txt | 3 +-
> tools/perf/builtin-ftrace.c | 84 ++++++++++++++++++++++--
> 2 files changed, 80 insertions(+), 7 deletions(-)
>
> diff --git a/tools/perf/Documentation/perf-ftrace.txt b/tools/perf/Documentation/perf-ftrace.txt
> index 78358af9a1c4..1e91121bac0f 100644
> --- a/tools/perf/Documentation/perf-ftrace.txt
> +++ b/tools/perf/Documentation/perf-ftrace.txt
> @@ -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=::
> diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> index 1d44bc2f63d8..9366fad591dc 100644
> --- a/tools/perf/builtin-ftrace.c
> +++ b/tools/perf/builtin-ftrace.c
> @@ -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. */
> --
> 2.25.1
>
--
- Arnaldo