perf record: Allow passing perf's own pid to '--filter'
PATCH 1/1 perf record: Allow passing perf's own pid to '--filter'
This patch allows passing perf's own PID to '--filter' by using '@PERFPID'. This should be useful when system-widely capturing tracepoints events.
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 introduces '@PERFPID' placeholder to '--filter' options. Now user is allowed to the above work with:
# perf record -e ... --filter="common_pid != @PERFPID' sleep 10
--- tools/perf/Documentation/perf-record.txt | 1 + tools/perf/util/parse-events.c | 96 ++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+)
tools/perf/Documentation/perf-record.txt +1 -0
@@ -62,6 +62,7 @@ OPTIONS --filter=<filter>:: Event filter. + String '@PERFPID' is allowed to be used to represent pid of 'perf' itself. -a:: --all-cpus::
tools/perf/util/parse-events.c +96 -0
108 lines not on this page · raw message
Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerEm Mon, Jul 06, 2015 at 04:17:31AM +0000, Wang Nan escreveu:
Quoted 3 lines
This patch allows passing perf's own PID to '--filter' by using '@PERFPID'. This should be useful when system-widely capturing tracepoints events.
Steven, does filters have any special meaning for @?
Quoted 37 lines
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 > ./tmpQuoted 3 lines
#!/bin/sh
exec perf record -e ... --filter="common_pid != \$\$" -a sleep 10 EOF# chmod a+x ./tmp # ./tmpHowever, doing so is user unfriendly.
This patch introduces '@PERFPID' placeholder to '--filter' options. Now user is allowed to the above work with:
# perf record -e ... --filter="common_pid != @PERFPID' sleep 10Signed-off-by Wang Nan <[email protected]>--- tools/perf/Documentation/perf-record.txt | 1 +
<SNIP>
tools/perf/util/parse-events.c int parse_events_option(const struct option *opt, const char *str, · line 1180
Quoted 8 lines
+++
b/tools/perf/util/parse-events.ctools/perf/util/parse-events.c +3
-0@@ -1175,6 +1175,101 @@ int parse_events_option(const struct option *opt, const char *str,return ret; } +#ifndef PAGE_SIZE +# define PAGE_SIZE 4096 +#endif
You can use 'page_size', its available and filled via:
page_size = sysconf(_SC_PAGE_SIZE);
early in perf's main() routine.
tools/perf/util/parse-events.c int parse_events_option(const struct option *opt, const char *str, · line 1189
Quoted 9 lines
tools/perf/util/parse-events.c +9
-0+static int +postproc_filter_append_token (const char *key,char *new_filter, +ssize_t *pspace) +{ +if (strcmp (key,"PERFPID" ) ==0 ) { +char pid_buf[32 ]; +pid_t self_pid =getpid (); + +snprintf (pid_buf,32 ,"%d" , self_pid);
snprintf(pid_buf, sizeof(pid_buf), "%d", self_pid);
tools/perf/util/parse-events.c int parse_events_option(const struct option *opt, const char *str, · line 1197
Quoted 8 lines
tools/perf/util/parse-events.c +8
-0+strncat (new_filter, pid_buf, *pspace); + *pspace -=strlen (pid_buf); +if (*pspace <0 ) +return -1 ; +return 0 ; + } + +return -1 ;
but then, please take a look at my perf/core branch, by coincidence I worked on having multiple filters set on a evsel in 'perf trace', where at a minimum, the tools' pid is added to the filter for all tracepoints used, i.e.:
commom_pid != getpid()
is always present, to avoid a feedback loop, neverending tracing of syscalls generated by the tracer itself.
Then, if you use --filter-pids PID-1,PID-2,PID3, it will create an expression with that first part, for things like gnome-terminal, xorg, etc.
Now we need to keep that in place and if the user uses -e to specify which syscalls it wants (or wants filtered out), we need to again concatenate with that commom_pid list, so that we call the filter ioctl just once, else the kernel returns EEXIST.
Because I needed to append, etc, there are new functions there for go on creating such expressions, please use them, its all in my perf/core branch, the latest patches.
I.e. having something in the filter expression that gets transformed into the tools' pid, I have no problem with that, just curious about what would be the best character to signal that a substitution needs to be performed, if it is really '@VAR', as my first selection would be '$VAR', but then I haven't looked deeply at ftrace's filter stuff to see if it has provision for substitution in the kernel, etc.
Andi also did this at some point, forgot why that wasn't applied at the time :-\
For reference:
- Arnaldo
Quoted 88 lines
tools/perf/util/parse-events.c +76
-0+} + +static void postproc_filter (struct perf_evsel *evsel) +{ +char *at =NULL , *sep =NULL , *old_filter, *new_filter; +ssize_t space; + +if (!evsel->filter) +return ; + + old_filter = evsel->filter; + at =strchr (old_filter,'@' ); +if (!at) +return ; + +/* +* See perf_event_set_filter(). Length of a valid filter is +* limited by PAGE_SIZE. +*/ + new_filter =malloc (PAGE_SIZE ); +if (!new_filter) { +fprintf (stderr,"No enough memory for post proc filter '%s'\n" , + old_filter); +return ; + } + *new_filter ='\0' ; + space =PAGE_SIZE -1 ; + +while (1 ) { +if (at) + *at ='\0' ; +strncat (new_filter, old_filter, space); + space -=strlen (old_filter); +if (space <0 ) + goto errout; +if (!at) +break ; + *at ='@' ; + + sep =strchr (at +1 ,' ' ); +if (sep) + *sep ='\0' ; + +if (postproc_filter_append_token (at +1 , new_filter, &space)) + goto errout; + +if (!sep) +break ; + *sep =' ' ; + + old_filter = sep; + at =strchr (old_filter,'@' ); + } + +free (evsel->filter); +/* +* It is safe to use new_filter directly. However, try to +* release some memory by strdup() a smaller string and free +* new_filter, which takes a full page. +*/ + evsel->filter =strdup (new_filter); +if (!evsel->filter) + evsel->filter = new_filter; +else +free (new_filter); +return ; +errout: +if (at) + *at ='@' ; +if (sep) + *sep =' ' ; +fprintf (stderr,"Can't post proc filter '%s'\n" , evsel->filter); +free (new_filter); +} +int parse_filter (const struct option *opt,const char *str,int unset__maybe_unused ) {@@ -1196,6 +1291,7 @@ int parse_filter(const struct option *opt, const char *str,return -1 ; } +postproc_filter (last);return 0 ; }Signature · 1 line
1.8.3.4
发自我的 iPhone
Quoted 108 lines · 在 2015年7月6日,下午9:56,Arnaldo Carvalho de Melo <[email protected]> 写道:
Em Mon, Jul 06, 2015 at 04:17:31AM +0000, Wang Nan escreveu:
Quoted 3 lines
This patch allows passing perf's own PID to '--filter' by using '@PERFPID'. This should be useful when system-widely capturing tracepoints events.
Steven, does filters have any special meaning for @?
Quoted 37 lines
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 > ./tmpQuoted 3 lines
#!/bin/sh
exec perf record -e ... --filter="common_pid != \$\$" -a sleep 10 EOF# chmod a+x ./tmp # ./tmpHowever, doing so is user unfriendly.
This patch introduces '@PERFPID' placeholder to '--filter' options. Now user is allowed to the above work with:
# perf record -e ... --filter="common_pid != @PERFPID' sleep 10Signed-off-by Wang Nan <[email protected]>---
tools/perf/Documentation/perf-record.txt| 1 +<SNIP>
tools/perf/util/parse-events.c int parse_events_option(const struct option *opt, const char *str, · line 1180
Quoted 8 lines
+++
b/tools/perf/util/parse-events.ctools/perf/util/parse-events.c +3
-0@@ -1175,6 +1175,101 @@ int parse_events_option(const struct option *opt, const char *str,return ret; } +#ifndef PAGE_SIZE +# define PAGE_SIZE 4096 +#endif You can use '
page_size', its available and filled via:page_size = sysconf(_SC_PAGE_SIZE);early in perf's
main()routine.tools/perf/util/parse-events.c int parse_events_option(const struct option *opt, const char *str, · line 1189
Quoted 9 lines
tools/perf/util/parse-events.c +9
-0+static int +postproc_filter_append_token (const char *key,char *new_filter, +ssize_t *pspace) +{ +if (strcmp (key,"PERFPID" ) ==0 ) { +char pid_buf[32 ]; +pid_t self_pid =getpid (); + +snprintf (pid_buf,32 ,"%d" , self_pid);snprintf(pid_buf, sizeof(pid_buf), "%d", self_pid);tools/perf/util/parse-events.c int parse_events_option(const struct option *opt, const char *str, · line 1197
Quoted 8 lines
tools/perf/util/parse-events.c +8
-0+strncat (new_filter, pid_buf, *pspace); + *pspace -=strlen (pid_buf); +if (*pspace <0 ) +return -1 ; +return 0 ; + } + +return -1 ;but then, please take a look at my perf/core branch, by coincidence I worked on having multiple filters set on a evsel in 'perf trace', where at a minimum, the tools' pid is added to the filter for all tracepoints used, i.e.:
commom_pid != getpid()is always present, to avoid a feedback loop, neverending tracing of syscalls generated by the tracer itself.
Then, if you use --filter-pids PID-1,PID-2,PID3, it will create an expression with that first part, for things like gnome-terminal, xorg, etc.
Now we need to keep that in place and if the user uses -e to specify which syscalls it wants (or wants filtered out), we need to again concatenate with that
commom_pidlist, so that we call the filter ioctl just once, else the kernel returns EEXIST.Because I needed to append, etc, there are new functions there for go on creating such expressions, please use them, its all in my perf/core branch, the latest patches.
Quoted 5 lines
I.e. having something in the filter expression that gets transformed into the tools' pid, I have no problem with that, just curious about what would be the best character to signal that a substitution needs to be performed, if it is really '@VAR', as my first selection would be '$VAR',
$ has special meaning for shell. Using $ in cmdline require users use escaping or '' quoted string. Therefore I believe @ should be better. What do you think?
Quoted 10 lines
but then I haven't looked deeply at ftrace's filter stuff to see if it has provision for substitution in the kernel, etc.
Andi also did this at some point, forgot why that wasn't applied at the time :-\
For reference:
OK. I'll rebase this patch on it.
Thank you.
Quoted 90 lines
- Arnaldo
Quoted 88 lines
tools/perf/util/parse-events.c +76
-0+} + +static void postproc_filter (struct perf_evsel *evsel) +{ +char *at =NULL , *sep =NULL , *old_filter, *new_filter; +ssize_t space; + +if (!evsel->filter) +return ; + + old_filter = evsel->filter; + at =strchr (old_filter,'@' ); +if (!at) +return ; + +/* +* See perf_event_set_filter(). Length of a valid filter is +* limited by PAGE_SIZE. +*/ + new_filter =malloc (PAGE_SIZE ); +if (!new_filter) { +fprintf (stderr,"No enough memory for post proc filter '%s'\n" , + old_filter); +return ; + } + *new_filter ='\0' ; + space =PAGE_SIZE -1 ; + +while (1 ) { +if (at) + *at ='\0' ; +strncat (new_filter, old_filter, space); + space -=strlen (old_filter); +if (space <0 ) + goto errout; +if (!at) +break ; + *at ='@' ; + + sep =strchr (at +1 ,' ' ); +if (sep) + *sep ='\0' ; + +if (postproc_filter_append_token (at +1 , new_filter, &space)) + goto errout; + +if (!sep) +break ; + *sep =' ' ; + + old_filter = sep; + at =strchr (old_filter,'@' ); + } + +free (evsel->filter); +/* +* It is safe to use new_filter directly. However, try to +* release some memory by strdup() a smaller string and free +* new_filter, which takes a full page. +*/ + evsel->filter =strdup (new_filter); +if (!evsel->filter) + evsel->filter = new_filter; +else +free (new_filter); +return ; +errout: +if (at) + *at ='@' ; +if (sep) + *sep =' ' ; +fprintf (stderr,"Can't post proc filter '%s'\n" , evsel->filter); +free (new_filter); +} +int parse_filter (const struct option *opt,const char *str,int unset__maybe_unused ) {@@ -1196,6 +1291,7 @@ int parse_filter(const struct option *opt, const char *str,return -1 ; } +postproc_filter (last);return 0 ; }Signature · 1 line
1.8.3.4
Arnaldo Carvalho de Melo
maintainerEm Mon, Jul 06, 2015 at 11:00:10PM +0800, pi3orama escreveu:
Quoted 10 lines
发自我的 iPhone
Quoted 6 lines · 在 2015年7月6日,下午9:56,Arnaldo Carvalho de Melo <[email protected]> 写道:
I.e. having something in the filter expression that gets transformed into the tools' pid, I have no problem with that, just curious about what would be the best character to signal that a substitution needs to be performed, if it is really '@VAR', as my first selection would be '$VAR',
$ has special meaning for shell. Using $ in cmdline require users use escaping or '' quoted string. Therefore I believe @ should be better. What do you think?
Yeah, that gets in the way, as it gets in the way for '!', i.e. negating, and even tho, that is what is used in strace (and in 'perf trace'):
strace -e \!open,write ls
Or:
strace -e '!open,write' ls
But apart from that, it would be good if expressions used in 'perf probe' and here could have as much as possible the same semantics for those markers, i.e. 'perf probe' already uses @ for some stuff, probably the meaning is for "at", i.e. something at some place.
'$' strongly associated with variables, so I don't think it would be a big problem to enclose expressions where variables (we may end having others, no?) in '', i.e.
perf record -e sched:*switch --filter 'common_pid != $PERF_PID' -a
Doesn't look so ugly or cumbersome :-)
- Arnaldo
Quoted 26 lines · Arnaldo Carvalho de Melo
Em Mon, Jul 06, 2015 at 11:00:10PM +0800, pi3orama escreveu:
Quoted 10 lines
发自我的 iPhone
Quoted 6 lines · 在 2015年7月6日,下午9:56,Arnaldo Carvalho de Melo <[email protected]> 写道:
I.e. having something in the filter expression that gets transformed into the tools' pid, I have no problem with that, just curious about what would be the best character to signal that a substitution needs to be performed, if it is really '@VAR', as my first selection would be '$VAR',
$ has special meaning for shell. Using $ in cmdline require users use escaping or '' quoted string. Therefore I believe @ should be better. What do you think?Yeah, that gets in the way, as it gets in the way for '!', i.e. negating, and even tho, that is what is used in strace (and in 'perf trace'):
strace -e \!open,write ls
Or:
strace -e '!open,write' ls
But apart from that, it would be good if expressions used in 'perf probe' and here could have as much as possible the same semantics for those markers, i.e. 'perf probe' already uses @ for some stuff, probably the meaning is for "at", i.e. something at some place.
Quoted 7 lines
'$' strongly associated with variables, so I don't think it would be a big problem to enclose expressions where variables (we may end having others, no?) in '', i.e.
perf record -e sched:*switch --filter 'common_pid != $PERF_PID' -aDoesn't look so ugly or cumbersome :-)
But what about user want to use real shell variables also?
perf record -e raw_syscalls:* "common_pid !="'$PERF_PID'" &&common_pid != $X_PID"
Or
perf record -e raw_syscalls:* "common_pid !=\$PERF_PID && common_pid
!= $X_PID"
right?
However, since you and Steven prefer '$' than '@' and '@' has its own meaning 'at' in 'perf probe', I'll use '$' in my next version.
I looked your new code. You added perf_evsel__append_filter() to enable us append a filter expression in '(%s) <op> (%s)' manner, and also perf_evlist__set_filter_pid() to add 'common_pid != %d' expression. They are nice scaffolds if we'd like to add a new cmdline option '--filter-pids' and '--filter-perf'. However, I think we should let users who use --filter take full control of their filters, instead of providing many helpers which can do similar things to confuse them. So I decide not to use those functions you added these days in my next version.
Thank you.
Quoted 2 lines
- Arnaldo
Arnaldo Carvalho de Melo
maintainerEm Tue, Jul 07, 2015 at 10:43:15AM +0800, Wangnan (F) escreveu:
Quoted 22 lines · Arnaldo Carvalho de Melo
Quoted 8 lines
Em Mon, Jul 06, 2015 at 11:00:10PM +0800, pi3orama escreveu:
Quoted 2 lines
发自我的 iPhone
Quoted 1 line · 在 2015年7月6日,下午9:56,Arnaldo Carvalho de Melo <[email protected]> 写道:
But apart from that, it would be good if expressions used in 'perf probe' and here could have as much as possible the same semantics for those markers, i.e. 'perf probe' already uses @ for some stuff, probably the meaning is for "at", i.e. something at some place.
Quoted 7 lines
'$' strongly associated with variables, so I don't think it would be a big problem to enclose expressions where variables (we may end having others, no?) in '', i.e.
perf record -e sched:*switch --filter 'common_pid != $PERF_PID' -aDoesn't look so ugly or cumbersome :-)
But what about user want to use real shell variables also?
perf record -e raw_syscalls:* "
common_pid!="'$PERF_PID'" &&common_pid!=
$X_PID"
Quoted 6 lines
Or
perf record -e raw_syscalls:* "
common_pid!=\$PERF_PID &&common_pid!=
$X_PID"right?
Yes, it depends to what entity you want to ask for the variable to be expanded into its value, if for the shell, that gets to interpret the line first, do no escaping, if for perf, that is next in line, do the escaping, the shell will remove the escaping character, perf will see act on it.
For both, '$', unescaped means: this is a variable, please replace it with its current value.
Quoted 2 lines
However, since you and Steven prefer '$' than '@' and '@' has its own meaning 'at' in 'perf probe', I'll use '$' in my next version.
Thanks!
Quoted 5 lines
I looked your new code. You added
perf_evsel__append_filter()to enable us append a filter expression in '(%s) <op> (%s)' manner, and alsoperf_evlist__set_filter_pid()to add 'common_pid!= %d' expression. They are nice scaffolds if we'd like to add a new cmdline option '--filter-pids' and '--filter-perf'.
For --filter-perf I think we would need to either use int and do:
int pids[] = { getpid(), }, err = -1;
char *filter = asprintf_expr_in_ints("common_pid", 1, pids);
if (!filter)
goto out_err;
err = perf_evlist__append_filter(evlist, "&&", filter))
free(filter);
if (err)
goto out_error;To be done after we parse the command line, where we may have set some filters already to a subset of the specified --events.
BTW above is an ad-hoc implementation of perf_evlist__append_filter_pid/pids(), in this case with just one entry, for the tracer itself.
Quoted 3 lines
However, I think we should let users who use --filter take full control of their filters, instead of providing many helpers which can do similar things to confuse them. So I decide not to use those functions
Well, we don't have to add many helpers, just the ones that makes sense and may reduce existing code duplication among builtin- tools or that could be useful in other builtins, like these appends, that I added for 'trace', but expected to be useful for 'record', as we are seeing here.
It will be useful in 'top' too.
Quoted 1 line
you added these days in my next version.
Well, if they make sense, use it, if not, do as you need to do :-)
After looking at the end result we may see more patterns emerging and do something to improve/reduce redundancies.
- Arnaldo
Quoted 6 lines · On Mon, 6 Jul 2015 10:56:50 -0300 Arnaldo Carvalho de Melo <[email protected]>…
Em Mon, Jul 06, 2015 at 04:17:31AM +0000, Wang Nan escreveu:
Quoted 3 lines
This patch allows passing perf's own PID to '--filter' by using '@PERFPID'. This should be useful when system-widely capturing tracepoints events.
Steven, does filters have any special meaning for @?
Not as of yet.
Quoted 28 lines
Quoted 28 lines
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 > ./tmpQuoted 3 lines
#!/bin/sh
exec perf record -e ... --filter="common_pid != \$\$" -a sleep 10 EOF# chmod a+x ./tmp # ./tmp
Quoted 6 lines
Quoted 6 lines
However, doing so is user unfriendly.
This patch introduces '@PERFPID' placeholder to '--filter' options. Now user is allowed to the above work with:
# perf record -e ... --filter="common_pid != @PERFPID' sleep 10
What about using '$' instead. That is more common to shell scripts of being a variable.
-- Steve
Quoted 7 lines
Quoted 4 lines
Signed-off-by Wang Nan <[email protected]>--- tools/perf/Documentation/perf-record.txt | 1 +<SNIP>
Arnaldo Carvalho de Melo
maintainerEm Mon, Jul 06, 2015 at 12:37:02PM -0400, Steven Rostedt escreveu:
Quoted 8 lines · On Mon, 6 Jul 2015 10:56:50 -0300 Arnaldo Carvalho de Melo <[email protected]>…
Quoted 4 lines
Quoted 4 lines
This patch introduces '@PERFPID' placeholder to '--filter' options. Now user is allowed to the above work with:
# perf record -e ... --filter="common_pid != @PERFPID' sleep 10What about using '$' instead. That is more common to shell scripts of being a variable.
Agreed.
- Arnaldo
Quoted 3 lines · Wang Nan
This patch allows passing perf's own PID to '--filter' by using '@PERFPID'. This should be useful when system-widely capturing tracepoints events.
I sent a similar patch some time ago.
https://lkml.org/lkml/2014/9/9/928
-Andi