perf record: Apply filter to all events in a glob matching
PATCH 1/2 perf record: Apply filter to all events in a glob matching
There is an old problem in perf's filter applying which first posted at Sep. 2014 at https://lkml.org/lkml/2014/9/9/944 that, if passing multiple events in a glob matching expression in cmdline then add '--filter' after them, the filter will be applied on only the last one.
For example:
# dd if=/dev/zero of=/dev/null & [1] 464 # perf record -a -e 'syscalls:sys_*_read' --filter 'common_pid != 464' sleep 0.1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.239 MB perf.data (2094 samples) ] # perf report --stdio | tee ... # Samples: 2K of event 'syscalls:sys_enter_read' # Event count (approx.): 2092 ... # Samples: 2 of event 'syscalls:sys_exit_read' # Event count (approx.): 2 ...
In this example, filter only applied on 'syscalls:sys_exit_read', and there's no way to set filter for ''syscalls:sys_enter_read'.
This patch adds a 'cmdline_group_boundary' for 'struct evsel', and apply filter on all events between two boundary marks.
After applying this patch:
# perf record -a -e 'syscalls:sys_*_read' --filter 'common_pid != 464' sleep 0.1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.031 MB perf.data (3 samples) ] # perf report --stdio | tee ... # Samples: 1 of event 'syscalls:sys_enter_read' # Event count (approx.): 1 ... # Samples: 2 of event 'syscalls:sys_exit_read' # Event count (approx.): 2 ...
--- tools/perf/util/evsel.c | 1 + tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 30 +++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 9 deletions(-)
tools/perf/util/evsel.c +1 -0
@@ -210,6 +210,7 @@ void perf_evsel__init(struct perf_evsel *evsel, perf_evsel__object.init (evsel); evsel->sample_size =__perf_evsel__sample_size (attr->sample_type);perf_evsel__calc_id_pos (evsel); + evsel->cmdline_group_boundary = false; }struct perf_evsel *perf_evsel__new_idx (struct perf_event_attr *attr,int idx)
tools/perf/util/evsel.h +1 -0
@@ -86,6 +86,7 @@ struct perf_evsel {unsigned long *per_pkg_mask;struct perf_evsel *leader;char *group_name; +bool cmdline_group_boundary; };union u64_swap {
tools/perf/util/parse-events.c +21 -9
@@ -1065,8 +1065,13 @@ int parse_events(struct perf_evlist *evlist, const char *str,perf_pmu__parse_cleanup ();if (!ret) {int entries = data.idx - evlist->nr_entries; +struct perf_evsel *last; +perf_evlist__splice_list_tail (evlist, &data.list, entries); evlist->nr_groups += data.nr_groups; + last =perf_evlist__last (evlist); + last->cmdline_group_boundary = true; +return 0 ; }
@@ -1171,16 +1176,23 @@ int parse_filter(const struct option *opt, const char *str,if (evlist->nr_entries >0 ) last =perf_evlist__last (evlist); -if (last ==NULL || last->attr.type !=PERF_TYPE_TRACEPOINT ) { -fprintf (stderr, -"--filter option should follow a -e tracepoint option\n" ); -return -1 ; - } +do { +if (last ==NULL || last->attr.type !=PERF_TYPE_TRACEPOINT ) { +fprintf (stderr, +"--filter option should follow a -e tracepoint option\n" ); +return -1 ; + } -if (perf_evsel__set_filter (last, str) <0 ) { -fprintf (stderr,"not enough memory to hold filter string\n" ); -return -1 ; - } +if (perf_evsel__set_filter (last, str) <0 ) { +fprintf (stderr, +"not enough memory to hold filter string\n" ); +return -1 ; + } + +if (last->node.prev == &evlist->entries) +return 0 ; + last = list_entry(last->node.prev, struct perf_evsel, node); + }while (!last->cmdline_group_boundary);return 0 ; }
Signature · 1 line
1.8.3.4
PATCH 2/2 perf record: Allow passing perf's own pid to '--filter'
This patch allows 'perf record' to exclude events issued by perf itself by '--exclude-perf' option.
Before this patch, when doing something like:
# perf record -a -e syscalls:sys_enter_write <cmd>
One could easily get result like this:
# /tmp/perf report --stdio
...
# Overhead Command Shared Object Symbol
# ........ ....... .................. ....................
#
99.99% perf libpthread-2.18.so [.] __write_nocancel
0.01% ls libc-2.18.so [.] write
0.01% sshd libc-2.18.so [.] write
...Where most events are generated by perf itself.
A shell trick can be done to filter perf itself out:
# cat << EOF > ./tmp
Quoted 3 lines
#!/bin/sh
exec perf record -e ... --filter="common_pid != \$\$" -a sleep 10 EOF
# chmod a+x ./tmp # ./tmp
However, doing so is user unfriendly.
This patch extracts evsel iteration framework introduced by patch 'perf
record: Apply filter to all events in a glob matching' into foreach_evsel_in_last_glob(), and makes exclude_perf() function append new filter expression to each evsel selected by a '-e' selector.
To avoid losing filters if user pass '--filter' after '--exclude-perf', this patch uses perf_evsel__append_filter() in both case, instead of perf_evsel__set_filter() which removes old filter. As a side effect, now it is possible to use multiple '--filter' option for one selector. They are combinded with '&&'.
--- tools/perf/Documentation/perf-record.txt | 11 ++++- tools/perf/builtin-record.c | 3 ++ tools/perf/util/parse-events.c | 83 +++++++++++++++++++++++++++----- tools/perf/util/parse-events.h | 1 + 4 files changed, 84 insertions(+), 14 deletions(-)
tools/perf/Documentation/perf-record.txt +10 -1
@@ -61,7 +61,16 @@ OPTIONS "perf report" to view group events together. --filter=<filter>:: - Event filter. + Event filter. This option should follow a event selector (-e) which + selects tracepoint event(s). Multiple '--filter' options are combined + using '&&'. + +--exclude-perf:: + Don't record events issued by perf itself. This option should follow + a event selector (-e) which selects tracepoint event(s). It adds a + filter expression 'common_pid != $PERFPID' to filters. If other + '--filter' exists, the new filter expression will be combined with + them by '&&'. -a:: --all-cpus::
tools/perf/builtin-record.c +3 -0
@@ -992,6 +992,9 @@ struct option __record_options[] = { parse_events_option),OPT_CALLBACK (0 ,"filter" , &record.evlist,"filter" ,"event filter" , parse_filter), +OPT_CALLBACK_NOOPT (0 ,"exclude-perf" , &record.evlist, +NULL ,"don't record events from perf itself" , + exclude_perf),OPT_STRING ('p' ,"pid" , &record.opts.target.pid,"pid" ,"record events on existing process id" ), OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
tools/perf/util/parse-events.c +70 -13
103 lines not on this page · raw message
tools/perf/util/parse-events.h +1 -0
@@ -34,6 +34,7 @@ extern int parse_events(struct perf_evlist *evlist, const char *str, struct parse_events_error *error);extern int parse_events_terms (struct list_head *terms,const char *str);extern int parse_filter (const struct option *opt,const char *str,int unset); +extern int exclude_perf (const struct option *opt,const char *arg,int unset);#define EVENTS_HELP_MAX (128*1024)
Signature · 1 line
1.8.3.4
tip-bot for Wang Nan
merge bot[tip:perf/core] perf record: Allow filtering perf' s pid via --exclude-perf
perf record: Allow filtering perf's pid via --exclude-perf
Commit-ID: 4ba1faa19fa5f415bd69b1d7c366028332468bca Gitweb: http://git.kernel.org/tip/4ba1faa19fa5f415bd69b1d7c366028332468bca Author: Wang Nan <[email protected]> AuthorDate: Fri, 10 Jul 2015 07:36:10 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 20 Jul 2015 17:49:17 -0300
perf record: Allow filtering perf's pid via --exclude-perf
This patch allows 'perf record' to exclude events issued by perf itself by '--exclude-perf' option.
Before this patch, when doing something like:
# perf record -a -e syscalls:sys_enter_write <cmd>
One could easily get result like this:
# /tmp/perf report --stdio
...
# Overhead Command Shared Object Symbol
# ........ ....... .................. ....................
#
99.99% perf libpthread-2.18.so [.] __write_nocancel
0.01% ls libc-2.18.so [.] write
0.01% sshd libc-2.18.so [.] write
...Where most events are generated by perf itself.
A shell trick can be done to filter perf itself out:
# cat << EOF > ./tmp
Quoted 3 lines
#!/bin/sh
exec perf record -e ... --filter="common_pid != \$\$" -a sleep 10 EOF
# chmod a+x ./tmp # ./tmp
However, doing so is user unfriendly.
This patch extracts evsel iteration framework introduced by patch 'perf
record: Apply filter to all events in a glob matching' into foreach_evsel_in_last_glob(), and makes exclude_perf() function append new filter expression to each evsel selected by a '-e' selector.
To avoid losing filters if user pass '--filter' after '--exclude-perf', this patch uses perf_evsel__append_filter() in both case, instead of perf_evsel__set_filter() which removes old filter. As a side effect, now it is possible to use multiple '--filter' option for one selector. They are combinded with '&&'.
--- tools/perf/Documentation/perf-record.txt | 11 ++++- tools/perf/builtin-record.c | 3 ++ tools/perf/util/parse-events.c | 83 +++++++++++++++++++++++++++----- tools/perf/util/parse-events.h | 1 + 4 files changed, 84 insertions(+), 14 deletions(-)
tools/perf/Documentation/perf-record.txt +10 -1
@@ -69,7 +69,16 @@ OPTIONS "perf report" to view group events together. --filter=<filter>:: - Event filter. + Event filter. This option should follow a event selector (-e) which + selects tracepoint event(s). Multiple '--filter' options are combined + using '&&'. + +--exclude-perf:: + Don't record events issued by perf itself. This option should follow + a event selector (-e) which selects tracepoint event(s). It adds a + filter expression 'common_pid != $PERFPID' to filters. If other + '--filter' exists, the new filter expression will be combined with + them by '&&'. -a:: --all-cpus::
tools/perf/builtin-record.c +3 -0
@@ -992,6 +992,9 @@ struct option __record_options[] = { parse_events_option),OPT_CALLBACK (0 ,"filter" , &record.evlist,"filter" ,"event filter" , parse_filter), +OPT_CALLBACK_NOOPT (0 ,"exclude-perf" , &record.evlist, +NULL ,"don't record events from perf itself" , + exclude_perf),OPT_STRING ('p' ,"pid" , &record.opts.target.pid,"pid" ,"record events on existing process id" ), OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
tools/perf/util/parse-events.c +70 -13
103 lines not on this page · raw message
tools/perf/util/parse-events.h +1 -0
@@ -34,6 +34,7 @@ extern int parse_events(struct perf_evlist *evlist, const char *str, struct parse_events_error *error);extern int parse_events_terms (struct list_head *terms,const char *str);extern int parse_filter (const struct option *opt,const char *str,int unset); +extern int exclude_perf (const struct option *opt,const char *arg,int unset);#define EVENTS_HELP_MAX (128*1024)
Em Fri, Jul 10, 2015 at 07:36:09AM +0000, Wang Nan escreveu:
Quoted 6 lines
There is an old problem in perf's filter applying which first posted at Sep. 2014 at https://lkml.org/lkml/2014/9/9/944 that, if passing multiple events in a glob matching expression in cmdline then add '--filter' after them, the filter will be applied on only the last one.
For example:
Thanks, applied both patches,
- Arnaldo
tip-bot for Wang Nan
merge botperf record: Apply filter to all events in a glob matching
Commit-ID: 15bfd2cc107a9971ac8aeb4b7724ced581a2ed30 Gitweb: http://git.kernel.org/tip/15bfd2cc107a9971ac8aeb4b7724ced581a2ed30 Author: Wang Nan <[email protected]> AuthorDate: Fri, 10 Jul 2015 07:36:09 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 20 Jul 2015 15:28:33 -0300
perf record: Apply filter to all events in a glob matching
There is an old problem in perf's filter applying which first posted at Sep. 2014 at https://lkml.org/lkml/2014/9/9/944 that, if passing multiple events in a glob matching expression in cmdline then add '--filter' after them, the filter will be applied on only the last one.
For example:
# dd if=/dev/zero of=/dev/null & [1] 464 # perf record -a -e 'syscalls:sys_*_read' --filter 'common_pid != 464' sleep 0.1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.239 MB perf.data (2094 samples) ] # perf report --stdio | tee ... # Samples: 2K of event 'syscalls:sys_enter_read' # Event count (approx.): 2092 ... # Samples: 2 of event 'syscalls:sys_exit_read' # Event count (approx.): 2 ...
In this example, filter only applied on 'syscalls:sys_exit_read', and there's no way to set filter for ''syscalls:sys_enter_read'.
This patch adds a 'cmdline_group_boundary' for 'struct evsel', and apply filter on all events between two boundary marks.
After applying this patch:
# perf record -a -e 'syscalls:sys_*_read' --filter 'common_pid != 464' sleep 0.1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.031 MB perf.data (3 samples) ] # perf report --stdio | tee ... # Samples: 1 of event 'syscalls:sys_enter_read' # Event count (approx.): 1 ... # Samples: 2 of event 'syscalls:sys_exit_read' # Event count (approx.): 2 ...
--- tools/perf/util/evsel.c | 1 + tools/perf/util/evsel.h | 1 + tools/perf/util/parse-events.c | 30 +++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 9 deletions(-)
tools/perf/util/evsel.c +1 -0
@@ -210,6 +210,7 @@ void perf_evsel__init(struct perf_evsel *evsel, perf_evsel__object.init (evsel); evsel->sample_size =__perf_evsel__sample_size (attr->sample_type);perf_evsel__calc_id_pos (evsel); + evsel->cmdline_group_boundary = false; }struct perf_evsel *perf_evsel__new_idx (struct perf_event_attr *attr,int idx)
tools/perf/util/evsel.h +1 -0
@@ -86,6 +86,7 @@ struct perf_evsel {unsigned long *per_pkg_mask;struct perf_evsel *leader;char *group_name; +bool cmdline_group_boundary; };union u64_swap {
tools/perf/util/parse-events.c +21 -9
@@ -1065,8 +1065,13 @@ int parse_events(struct perf_evlist *evlist, const char *str,perf_pmu__parse_cleanup ();if (!ret) {int entries = data.idx - evlist->nr_entries; +struct perf_evsel *last; +perf_evlist__splice_list_tail (evlist, &data.list, entries); evlist->nr_groups += data.nr_groups; + last =perf_evlist__last (evlist); + last->cmdline_group_boundary = true; +return 0 ; }
@@ -1171,16 +1176,23 @@ int parse_filter(const struct option *opt, const char *str,if (evlist->nr_entries >0 ) last =perf_evlist__last (evlist); -if (last ==NULL || last->attr.type !=PERF_TYPE_TRACEPOINT ) { -fprintf (stderr, -"--filter option should follow a -e tracepoint option\n" ); -return -1 ; - } +do { +if (last ==NULL || last->attr.type !=PERF_TYPE_TRACEPOINT ) { +fprintf (stderr, +"--filter option should follow a -e tracepoint option\n" ); +return -1 ; + } -if (perf_evsel__set_filter (last, str) <0 ) { -fprintf (stderr,"not enough memory to hold filter string\n" ); -return -1 ; - } +if (perf_evsel__set_filter (last, str) <0 ) { +fprintf (stderr, +"not enough memory to hold filter string\n" ); +return -1 ; + } + +if (last->node.prev == &evlist->entries) +return 0 ; + last = list_entry(last->node.prev, struct perf_evsel, node); + }while (!last->cmdline_group_boundary);return 0 ; }