linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RESEND PATCH] perf: ftrace: Add filter support for option -F/--funcs
@ 2020-09-04 15:23 Changbin Du
  2020-09-04 16:27 ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 4+ messages in thread
From: Changbin Du @ 2020-09-04 15:23 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Namhyung Kim,
	linux-kernel, Changbin Du, Arnaldo Carvalho de Melo

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
...

Suggested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Changbin Du <changbin.du@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [RESEND PATCH] perf: ftrace: Add filter support for option -F/--funcs
  2020-09-04 15:23 [RESEND PATCH] perf: ftrace: Add filter support for option -F/--funcs Changbin Du
@ 2020-09-04 16:27 ` Arnaldo Carvalho de Melo
  2020-09-04 18:59   ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2020-09-04 16:27 UTC (permalink / raw)
  To: Changbin Du
  Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Namhyung Kim,
	linux-kernel, Arnaldo Carvalho de Melo

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@redhat.com>
> Signed-off-by: Changbin Du <changbin.du@gmail.com>
> ---
>  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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RESEND PATCH] perf: ftrace: Add filter support for option -F/--funcs
  2020-09-04 16:27 ` Arnaldo Carvalho de Melo
@ 2020-09-04 18:59   ` Arnaldo Carvalho de Melo
  2020-09-05 22:49     ` Changbin Du
  0 siblings, 1 reply; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2020-09-04 18:59 UTC (permalink / raw)
  To: Changbin Du
  Cc: Peter Zijlstra, Ingo Molnar, Jiri Olsa, Namhyung Kim,
	linux-kernel, Arnaldo Carvalho de Melo

Em Fri, Sep 04, 2020 at 01:27:16PM -0300, Arnaldo Carvalho de Melo escreveu:
> 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!

Thanks, applied, will go to v5.10, i.e. to my perf/core branch as soon
as the usual set of tests pass,

- Arnaldo
 
> - Arnaldo
>  
> > Suggested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> > Signed-off-by: Changbin Du <changbin.du@gmail.com>
> > ---
> >  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

-- 

- Arnaldo

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RESEND PATCH] perf: ftrace: Add filter support for option -F/--funcs
  2020-09-04 18:59   ` Arnaldo Carvalho de Melo
@ 2020-09-05 22:49     ` Changbin Du
  0 siblings, 0 replies; 4+ messages in thread
From: Changbin Du @ 2020-09-05 22:49 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Changbin Du, Peter Zijlstra, Ingo Molnar, Jiri Olsa,
	Namhyung Kim, linux-kernel, Arnaldo Carvalho de Melo

On Fri, Sep 04, 2020 at 03:59:59PM -0300, Arnaldo Carvalho de Melo wrote:
> Em Fri, Sep 04, 2020 at 01:27:16PM -0300, Arnaldo Carvalho de Melo escreveu:
> > 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!
> 
> 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!

-- 
Cheers,
Changbin Du

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-09-05 22:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-04 15:23 [RESEND PATCH] perf: ftrace: Add filter support for option -F/--funcs Changbin Du
2020-09-04 16:27 ` Arnaldo Carvalho de Melo
2020-09-04 18:59   ` Arnaldo Carvalho de Melo
2020-09-05 22:49     ` Changbin Du

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).