diff mbox

[RFC,V3,1/3] perf, tools: Support wildcards on pmu name in dynamic pmu events

Message ID 1520345084-42646-2-git-send-email-agustinv@codeaurora.org (mailing list archive)
State New, archived
Headers show

Commit Message

Agustin Vega-Frias March 6, 2018, 2:04 p.m. UTC
Starting on v4.12 event parsing code for dynamic pmu events already
supports prefix-based matching of multiple pmus when creating dynamic
events. E.g., in a system with the following dynamic pmus:

    mypmu_0
    mypmu_1
    mypmu_2
    mypmu_4

passing mypmu/<config>/ as an event spec will result in the creation
of the event in all of the pmus. This change expands this matching
through the use of fnmatch so glob-like expressions can be used to
create events in multiple pmus. E.g., in the system described above
if a user only wants to create the event in mypmu_0 and mypmu_1,
mypmu_[01]/<config>/ can be passed.

Change-Id: Icb25653fc5d5239c20f3bffdfdf4ab4c9c9bb20b
Signed-off-by: Agustin Vega-Frias <agustinv@codeaurora.org>
---
 tools/perf/Documentation/perf-list.txt |  8 +++++++-
 tools/perf/Documentation/perf-stat.txt | 13 +++++++++++++
 tools/perf/util/parse-events.l         |  2 +-
 tools/perf/util/parse-events.y         | 19 ++++++++++++-------
 4 files changed, 33 insertions(+), 9 deletions(-)

Comments

Arnaldo Carvalho de Melo March 7, 2018, 4:02 p.m. UTC | #1
Em Tue, Mar 06, 2018 at 09:04:42AM -0500, Agustin Vega-Frias escreveu:
> Starting on v4.12 event parsing code for dynamic pmu events already
> supports prefix-based matching of multiple pmus when creating dynamic
> events. E.g., in a system with the following dynamic pmus:
> 
>     mypmu_0
>     mypmu_1
>     mypmu_2
>     mypmu_4
> 
> passing mypmu/<config>/ as an event spec will result in the creation
> of the event in all of the pmus. This change expands this matching
> through the use of fnmatch so glob-like expressions can be used to
> create events in multiple pmus. E.g., in the system described above
> if a user only wants to create the event in mypmu_0 and mypmu_1,
> mypmu_[01]/<config>/ can be passed.
> 
> Change-Id: Icb25653fc5d5239c20f3bffdfdf4ab4c9c9bb20b
> Signed-off-by: Agustin Vega-Frias <agustinv@codeaurora.org>
> ---
>  tools/perf/Documentation/perf-list.txt |  8 +++++++-
>  tools/perf/Documentation/perf-stat.txt | 13 +++++++++++++
>  tools/perf/util/parse-events.l         |  2 +-
>  tools/perf/util/parse-events.y         | 19 ++++++++++++-------
>  4 files changed, 33 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/perf/Documentation/perf-list.txt b/tools/perf/Documentation/perf-list.txt
> index e2a897a..2549c34 100644
> --- a/tools/perf/Documentation/perf-list.txt
> +++ b/tools/perf/Documentation/perf-list.txt
> @@ -141,7 +141,13 @@ on the first memory controller on socket 0 of a Intel Xeon system
>  
>  Each memory controller has its own PMU.  Measuring the complete system
>  bandwidth would require specifying all imc PMUs (see perf list output),
> -and adding the values together.
> +and adding the values together. To simplify creation of multiple events,
> +prefix and glob matching is supported in the PMU name, and the prefix
> +'uncore_' is also ignored when performing the match. So the command above
> +can be expanded to all memory controllers by using the syntaxes:
> +
> +  perf stat -C 0 -a imc/cas_count_read/,imc/cas_count_write/ -I 1000 ...
> +  perf stat -C 0 -a *imc*/cas_count_read/,*imc*/cas_count_write/ -I 1000 ...
>  
>  This example measures the combined core power every second
>  
> diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
> index 823fce7..5ee954f 100644
> --- a/tools/perf/Documentation/perf-stat.txt
> +++ b/tools/perf/Documentation/perf-stat.txt
> @@ -49,6 +49,13 @@ report::
>  	  parameters are defined by corresponding entries in
>  	  /sys/bus/event_source/devices/<pmu>/format/*
>  
> +	Note that the last two syntaxes support prefix and glob matching in
> +	the PMU name to simplify creation of events accross multiple instances
> +	of the same type of PMU in large systems (e.g. memory controller PMUs).
> +	Multiple PMU instances are typical for uncore PMUs, so the prefix
> +	'uncore_' is also ignored when performing this match.
> +
> +
>  -i::
>  --no-inherit::
>          child tasks do not inherit counters
> @@ -246,6 +253,12 @@ taskset.
>  --no-merge::
>  Do not merge results from same PMUs.
>  
> +When multiple events are created from a single event alias, stat will,
> +by default, aggregate the event counts and show the result in a single
> +row. This option disables that behavior and shows the individual events
> +and counts. Aliases are listed immediately after the Kernel PMU events
> +by perf list.
> +
>  --smi-cost::
>  Measure SMI cost if msr/aperf/ and msr/smi/ events are supported.
>  
> diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
> index 655ecff..a1a01b1 100644
> --- a/tools/perf/util/parse-events.l
> +++ b/tools/perf/util/parse-events.l
> @@ -175,7 +175,7 @@ bpf_source	[^,{}]+\.c[a-zA-Z0-9._]*
>  num_dec		[0-9]+
>  num_hex		0x[a-fA-F0-9]+
>  num_raw_hex	[a-fA-F0-9]+
> -name		[a-zA-Z_*?][a-zA-Z0-9_*?.]*
> +name		[a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]]*
>  name_minus	[a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
>  drv_cfg_term	[a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
>  /* If you add a modifier you need to update check_modifier() */
> diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y
> index e81a20e..316ac07 100644
> --- a/tools/perf/util/parse-events.y
> +++ b/tools/perf/util/parse-events.y
> @@ -8,6 +8,7 @@
>  
>  #define YYDEBUG 1
>  
> +#include <fnmatch.h>
>  #include <linux/compiler.h>
>  #include <linux/list.h>
>  #include <linux/types.h>
> @@ -234,21 +235,25 @@ PE_NAME opt_event_config
>  	if (parse_events_add_pmu(_parse_state, list, $1, $2)) {
>  		struct perf_pmu *pmu = NULL;
>  		int ok = 0;
> +		char *pattern;
>  
> -		while ((pmu = perf_pmu__scan(pmu)) != NULL) {
> -			char *name = pmu->name;
> +		if (asprintf(&pattern, "?(uncore_)%s*", $1) < 0)
> +			YYABORT;
>  
> -			if (!strncmp(name, "uncore_", 7) &&
> -			    strncmp($1, "uncore_", 7))
> -				name += 7;
> -			if (!strncmp($1, name, strlen($1))) {
> -				if (parse_events_copy_term_list(orig_terms, &terms))
> +		while ((pmu = perf_pmu__scan(pmu)) != NULL) {
> +			if (!fnmatch(pattern, pmu->name, FNM_EXTMATCH)) {
> +				if (parse_events_copy_term_list(orig_terms, &terms)) {
> +					free(pattern);

This breaks the build in at least Alpine Linux 3.4, 3.5 and 3.6 and android-ndk:r15c-arm

Alpine 3.6 build failure:

  CC       /tmp/build/perf/util/parse-events-bison.o
util/parse-events.y: In function 'parse_events_parse':
util/parse-events.y:244:37: error: 'FNM_EXTMATCH' undeclared (first use in this function)
    if (!fnmatch(pattern, pmu->name, FNM_EXTMATCH)) {
                                     ^~~~~~~~~~~~
util/parse-events.y:244:37: note: each undeclared identifier is reported only once for each function it appears in
  CC       /tmp/build/perf/bench/mem-functions.o
mv: can't rename '/tmp/build/perf/util/.parse-events-bison.o.tmp': No such file or directory
make[4]: *** [/git/linux/tools/build/Makefile.build:96: /tmp/build/perf/util/parse-events-bison.o] Error 1
make[3]: *** [/git/linux/tools/build/Makefile.build:139: util] Error 2
make[2]: *** [Makefile.perf:623: /tmp/build/perf/libperf-in.o] Error 2
make[2]: *** Waiting for unfinished jobs....

Android NDK r15c ARM, cross built from, IIRC, Fedora 24:

 CC       /tmp/build/perf/util/parse-events-bison.o
util/parse-events.y: In function 'parse_events_parse':
util/parse-events.y:244:37: error: 'FNM_EXTMATCH' undeclared (first use in this function)
    if (!fnmatch(pattern, pmu->name, FNM_EXTMATCH)) {
                                     ^
util/parse-events.y:244:37: note: each undeclared identifier is reported only once for each function it appears in
mv: cannot stat '/tmp/build/perf/util/.parse-events-bison.o.tmp': No such file or directory
make[4]: *** [/git/linux/tools/build/Makefile.build:96: /tmp/build/perf/util/parse-events-bison.o] Error 1
make[4]: *** Waiting for unfinished jobs....
  CC       /tmp/build/perf/builtin-kmem.o
make[3]: *** [/git/linux/tools/build/Makefile.build:139: util] Error 2
make[2]: *** [Makefile.perf:623: /tmp/build/perf/libperf-in.o] Error 2


Probably others, I'll investigate later, feel free to do it if you have
the time now :-)

Extra info:


   1    46.29 alpine:3.4                    : FAIL gcc (Alpine 5.3.0) 5.3.0
   2    53.37 alpine:3.5                    : FAIL gcc (Alpine 6.2.1) 6.2.1 20160822
   3    48.42 alpine:3.6                    : FAIL gcc (Alpine 6.3.0) 6.3.0
   4    35.88 alpine:edge                   : FAIL gcc (Alpine 6.4.0) 6.4.0
   5    96.79 amazonlinux:1                 : Ok   gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-11)
   6   116.40 amazonlinux:2                 : Ok   gcc (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2)
   7    34.30 android-ndk:r12b-arm          : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
   8    33.69 android-ndk:r15c-arm          : FAIL arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
   9    60.50 centos:5                      : Ok   gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-55)
  10    88.54 centos:6                      : Ok   gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18)
  11   108.89 centos:7                      : Ok   gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)


The builds are still ongoing.

- Arnaldo
Agustin Vega-Frias March 7, 2018, 5:39 p.m. UTC | #2
On 2018-03-07 11:02, Arnaldo Carvalho de Melo wrote:
> Em Tue, Mar 06, 2018 at 09:04:42AM -0500, Agustin Vega-Frias escreveu:

[snip]

> This breaks the build in at least Alpine Linux 3.4, 3.5 and 3.6 and
> android-ndk:r15c-arm
> 
> Alpine 3.6 build failure:
> 
>   CC       /tmp/build/perf/util/parse-events-bison.o
> util/parse-events.y: In function 'parse_events_parse':
> util/parse-events.y:244:37: error: 'FNM_EXTMATCH' undeclared (first
> use in this function)
>     if (!fnmatch(pattern, pmu->name, FNM_EXTMATCH)) {
>                                      ^~~~~~~~~~~~
> util/parse-events.y:244:37: note: each undeclared identifier is
> reported only once for each function it appears in
>   CC       /tmp/build/perf/bench/mem-functions.o
> mv: can't rename '/tmp/build/perf/util/.parse-events-bison.o.tmp': No
> such file or directory
> make[4]: *** [/git/linux/tools/build/Makefile.build:96:
> /tmp/build/perf/util/parse-events-bison.o] Error 1
> make[3]: *** [/git/linux/tools/build/Makefile.build:139: util] Error 2
> make[2]: *** [Makefile.perf:623: /tmp/build/perf/libperf-in.o] Error 2
> make[2]: *** Waiting for unfinished jobs....
> 
> Android NDK r15c ARM, cross built from, IIRC, Fedora 24:
> 
>  CC       /tmp/build/perf/util/parse-events-bison.o
> util/parse-events.y: In function 'parse_events_parse':
> util/parse-events.y:244:37: error: 'FNM_EXTMATCH' undeclared (first
> use in this function)
>     if (!fnmatch(pattern, pmu->name, FNM_EXTMATCH)) {
>                                      ^
> util/parse-events.y:244:37: note: each undeclared identifier is
> reported only once for each function it appears in
> mv: cannot stat '/tmp/build/perf/util/.parse-events-bison.o.tmp': No
> such file or directory
> make[4]: *** [/git/linux/tools/build/Makefile.build:96:
> /tmp/build/perf/util/parse-events-bison.o] Error 1
> make[4]: *** Waiting for unfinished jobs....
>   CC       /tmp/build/perf/builtin-kmem.o
> make[3]: *** [/git/linux/tools/build/Makefile.build:139: util] Error 2
> make[2]: *** [Makefile.perf:623: /tmp/build/perf/libperf-in.o] Error 2
> 
> 
> Probably others, I'll investigate later, feel free to do it if you have
> the time now :-)
> 

Sorry about that. That's probably because FNM_EXTMATCH is a GNU 
extension,
not POSIX, and the Alpine and Android runtimes likely don't implement 
that...
I'll send a fix reverting back to the strncmp to ignore the uncore_ 
prefix,
and dropping that extension.

Agustín

> Extra info:
> 
> 
>    1    46.29 alpine:3.4                    : FAIL gcc (Alpine 5.3.0) 
> 5.3.0
>    2    53.37 alpine:3.5                    : FAIL gcc (Alpine 6.2.1)
> 6.2.1 20160822
>    3    48.42 alpine:3.6                    : FAIL gcc (Alpine 6.3.0) 
> 6.3.0
>    4    35.88 alpine:edge                   : FAIL gcc (Alpine 6.4.0) 
> 6.4.0
>    5    96.79 amazonlinux:1                 : Ok   gcc (GCC) 4.8.5
> 20150623 (Red Hat 4.8.5-11)
>    6   116.40 amazonlinux:2                 : Ok   gcc (GCC) 7.2.1
> 20170915 (Red Hat 7.2.1-2)
>    7    34.30 android-ndk:r12b-arm          : FAIL
> arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
>    8    33.69 android-ndk:r15c-arm          : FAIL
> arm-linux-androideabi-gcc (GCC) 4.9.x 20150123 (prerelease)
>    9    60.50 centos:5                      : Ok   gcc (GCC) 4.1.2
> 20080704 (Red Hat 4.1.2-55)
>   10    88.54 centos:6                      : Ok   gcc (GCC) 4.4.7
> 20120313 (Red Hat 4.4.7-18)
>   11   108.89 centos:7                      : Ok   gcc (GCC) 4.8.5
> 20150623 (Red Hat 4.8.5-16)
> 
> 
> The builds are still ongoing.
> 
> - Arnaldo
Andi Kleen March 7, 2018, 6:54 p.m. UTC | #3
> Sorry about that. That's probably because FNM_EXTMATCH is a GNU extension,
> not POSIX, and the Alpine and Android runtimes likely don't implement
> that...
> I'll send a fix reverting back to the strncmp to ignore the uncore_ prefix,
> and dropping that extension.

Just don't set it? Even the basic glob patterns are useful.

-Andi
Arnaldo Carvalho de Melo March 7, 2018, 7:05 p.m. UTC | #4
Em Wed, Mar 07, 2018 at 10:54:15AM -0800, Andi Kleen escreveu:
> > Sorry about that. That's probably because FNM_EXTMATCH is a GNU extension,
> > not POSIX, and the Alpine and Android runtimes likely don't implement
> > that...
> > I'll send a fix reverting back to the strncmp to ignore the uncore_ prefix,
> > and dropping that extension.
> 
> Just don't set it? Even the basic glob patterns are useful.

Or use:

#ifndef FNM_EXTMATCH
#define FNM_EXTMATCH 0
#endif

So on systems without it, its not used, while on GNU systems, we have
that functionality (pretty fancy, someone may need that... ;-))

- Arnaldo
Arnaldo Carvalho de Melo March 7, 2018, 7:05 p.m. UTC | #5
Em Wed, Mar 07, 2018 at 04:05:09PM -0300, Arnaldo Carvalho de Melo escreveu:
> Em Wed, Mar 07, 2018 at 10:54:15AM -0800, Andi Kleen escreveu:
> > > Sorry about that. That's probably because FNM_EXTMATCH is a GNU extension,
> > > not POSIX, and the Alpine and Android runtimes likely don't implement
> > > that...
> > > I'll send a fix reverting back to the strncmp to ignore the uncore_ prefix,
> > > and dropping that extension.
> > 
> > Just don't set it? Even the basic glob patterns are useful.
> 
> Or use:
> 
> #ifndef FNM_EXTMATCH
> #define FNM_EXTMATCH 0
> #endif
> 
> So on systems without it, its not used, while on GNU systems, we have
> that functionality (pretty fancy, someone may need that... ;-))

I'll try this route, btw, no need to send more patches for now.

- Arnaldo
diff mbox

Patch

diff --git a/tools/perf/Documentation/perf-list.txt b/tools/perf/Documentation/perf-list.txt
index e2a897a..2549c34 100644
--- a/tools/perf/Documentation/perf-list.txt
+++ b/tools/perf/Documentation/perf-list.txt
@@ -141,7 +141,13 @@  on the first memory controller on socket 0 of a Intel Xeon system
 
 Each memory controller has its own PMU.  Measuring the complete system
 bandwidth would require specifying all imc PMUs (see perf list output),
-and adding the values together.
+and adding the values together. To simplify creation of multiple events,
+prefix and glob matching is supported in the PMU name, and the prefix
+'uncore_' is also ignored when performing the match. So the command above
+can be expanded to all memory controllers by using the syntaxes:
+
+  perf stat -C 0 -a imc/cas_count_read/,imc/cas_count_write/ -I 1000 ...
+  perf stat -C 0 -a *imc*/cas_count_read/,*imc*/cas_count_write/ -I 1000 ...
 
 This example measures the combined core power every second
 
diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
index 823fce7..5ee954f 100644
--- a/tools/perf/Documentation/perf-stat.txt
+++ b/tools/perf/Documentation/perf-stat.txt
@@ -49,6 +49,13 @@  report::
 	  parameters are defined by corresponding entries in
 	  /sys/bus/event_source/devices/<pmu>/format/*
 
+	Note that the last two syntaxes support prefix and glob matching in
+	the PMU name to simplify creation of events accross multiple instances
+	of the same type of PMU in large systems (e.g. memory controller PMUs).
+	Multiple PMU instances are typical for uncore PMUs, so the prefix
+	'uncore_' is also ignored when performing this match.
+
+
 -i::
 --no-inherit::
         child tasks do not inherit counters
@@ -246,6 +253,12 @@  taskset.
 --no-merge::
 Do not merge results from same PMUs.
 
+When multiple events are created from a single event alias, stat will,
+by default, aggregate the event counts and show the result in a single
+row. This option disables that behavior and shows the individual events
+and counts. Aliases are listed immediately after the Kernel PMU events
+by perf list.
+
 --smi-cost::
 Measure SMI cost if msr/aperf/ and msr/smi/ events are supported.
 
diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
index 655ecff..a1a01b1 100644
--- a/tools/perf/util/parse-events.l
+++ b/tools/perf/util/parse-events.l
@@ -175,7 +175,7 @@  bpf_source	[^,{}]+\.c[a-zA-Z0-9._]*
 num_dec		[0-9]+
 num_hex		0x[a-fA-F0-9]+
 num_raw_hex	[a-fA-F0-9]+
-name		[a-zA-Z_*?][a-zA-Z0-9_*?.]*
+name		[a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]]*
 name_minus	[a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
 drv_cfg_term	[a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
 /* If you add a modifier you need to update check_modifier() */
diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y
index e81a20e..316ac07 100644
--- a/tools/perf/util/parse-events.y
+++ b/tools/perf/util/parse-events.y
@@ -8,6 +8,7 @@ 
 
 #define YYDEBUG 1
 
+#include <fnmatch.h>
 #include <linux/compiler.h>
 #include <linux/list.h>
 #include <linux/types.h>
@@ -234,21 +235,25 @@  PE_NAME opt_event_config
 	if (parse_events_add_pmu(_parse_state, list, $1, $2)) {
 		struct perf_pmu *pmu = NULL;
 		int ok = 0;
+		char *pattern;
 
-		while ((pmu = perf_pmu__scan(pmu)) != NULL) {
-			char *name = pmu->name;
+		if (asprintf(&pattern, "?(uncore_)%s*", $1) < 0)
+			YYABORT;
 
-			if (!strncmp(name, "uncore_", 7) &&
-			    strncmp($1, "uncore_", 7))
-				name += 7;
-			if (!strncmp($1, name, strlen($1))) {
-				if (parse_events_copy_term_list(orig_terms, &terms))
+		while ((pmu = perf_pmu__scan(pmu)) != NULL) {
+			if (!fnmatch(pattern, pmu->name, FNM_EXTMATCH)) {
+				if (parse_events_copy_term_list(orig_terms, &terms)) {
+					free(pattern);
 					YYABORT;
+				}
 				if (!parse_events_add_pmu(_parse_state, list, pmu->name, terms))
 					ok++;
 				parse_events_terms__delete(terms);
 			}
 		}
+
+		free(pattern);
+
 		if (!ok)
 			YYABORT;
 	}