All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v12 0/4] perf tools: add support for libpfm4
@ 2020-04-29 23:14 Ian Rogers
  2020-04-29 23:14 ` [PATCH v12 1/4] perf doc: pass ASCIIDOC_EXTRA as an argument Ian Rogers
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Ian Rogers @ 2020-04-29 23:14 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau,
	Yonghong Song, Andrii Nakryiko, Greg Kroah-Hartman,
	Thomas Gleixner, Igor Lubashev, Alexey Budankov,
	Florian Fainelli, Adrian Hunter, Andi Kleen, Jiwei Sun,
	yuzhoujian, Kan Liang, Jin Yao, Leo Yan, John Garry,
	linux-kernel, netdev, bpf, linux-perf-users
  Cc: Stephane Eranian, Ian Rogers

This patch links perf with the libpfm4 library if it is available and
LIBPFM4 is passed to the build. The libpfm4 library contains hardware
event tables for all processors supported by perf_events. It is a
helper library that helps convert from a symbolic event name to the
event encoding required by the underlying kernel interface. This
library is open-source and available from: http://perfmon2.sf.net.
    
With this patch, it is possible to specify full hardware events
by name. Hardware filters are also supported. Events must be
specified via the --pfm-events and not -e option. Both options
are active at the same time and it is possible to mix and match:
    
$ perf stat --pfm-events inst_retired:any_p:c=1:i -e cycles ....

v12 changes NO_LIBPFM4 as a make option to LIBPFM4, ie opt-in rather
    than opt-out of feature detection and build support. Suggested by
    acme@kernel.org. It also moves passing the ASCIIDOC_EXTRA argument
    into its own commit.
v11 reformats the perf list output.
v10 addresses review comments from jolsa@redhat.com.
v9 removes some unnecessary #ifs.
v8 addresses review comments from jolsa@redhat.com.
   Breaks the patch into 4, adds a test and moves the libpfm code into its
   own file. perf list encoding tries to be closer to existing.
v7 rebases and adds fallback code for libpfm4 events.
   The fallback code is to force user only priv level in case the
   perf_event_open() syscall failed for permissions reason.
   the fallback forces a user privilege level restriction on the event
   string, so depending on the syntax either u or :u is needed.
    
   But libpfm4 can use a : or . as the separator, so simply searching
   for ':' vs. '/' is not good enough to determine the syntax needed.
   Therefore, this patch introduces a new evsel boolean field to mark
   events coming from  libpfm4. The field is then used to adjust the
   fallback string.
v6 was a rebase.
v5 was a rebase.
v4 was a rebase on
   git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git
   branch perf/core and re-adds the tools/build/feature/test-libpfm4.c
   missed in v3.
v3 is against acme/perf/core and removes a diagnostic warning.
v2 of this patch makes the --pfm-events man page documentation
   conditional on libpfm4 behing configured. It tidies some of the
   documentation and adds the feature test missed in the v1 patch.

Ian Rogers (1):
  perf doc: pass ASCIIDOC_EXTRA as an argument

Stephane Eranian (3):
  tools feature: add support for detecting libpfm4
  perf pmu: add perf_pmu__find_by_type helper
  perf tools: add support for libpfm4

 tools/build/Makefile.feature             |   3 +-
 tools/build/feature/Makefile             |   6 +-
 tools/build/feature/test-libpfm4.c       |   9 +
 tools/perf/Documentation/perf-record.txt |  11 +
 tools/perf/Documentation/perf-stat.txt   |  10 +
 tools/perf/Documentation/perf-top.txt    |  11 +
 tools/perf/Makefile.config               |  13 ++
 tools/perf/Makefile.perf                 |   8 +-
 tools/perf/builtin-list.c                |   3 +
 tools/perf/builtin-record.c              |   8 +
 tools/perf/builtin-stat.c                |   8 +
 tools/perf/builtin-top.c                 |   8 +
 tools/perf/tests/Build                   |   1 +
 tools/perf/tests/builtin-test.c          |   9 +
 tools/perf/tests/pfm.c                   | 207 +++++++++++++++++
 tools/perf/tests/tests.h                 |   3 +
 tools/perf/util/Build                    |   2 +
 tools/perf/util/evsel.c                  |   2 +-
 tools/perf/util/evsel.h                  |   1 +
 tools/perf/util/parse-events.c           |  30 ++-
 tools/perf/util/parse-events.h           |   4 +
 tools/perf/util/pfm.c                    | 277 +++++++++++++++++++++++
 tools/perf/util/pfm.h                    |  43 ++++
 tools/perf/util/pmu.c                    |  11 +
 tools/perf/util/pmu.h                    |   1 +
 25 files changed, 676 insertions(+), 13 deletions(-)
 create mode 100644 tools/build/feature/test-libpfm4.c
 create mode 100644 tools/perf/tests/pfm.c
 create mode 100644 tools/perf/util/pfm.c
 create mode 100644 tools/perf/util/pfm.h

-- 
2.26.2.303.gf8c07b1a785-goog


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

* [PATCH v12 1/4] perf doc: pass ASCIIDOC_EXTRA as an argument
  2020-04-29 23:14 [PATCH v12 0/4] perf tools: add support for libpfm4 Ian Rogers
@ 2020-04-29 23:14 ` Ian Rogers
  2020-05-08 13:04   ` [tip: perf/core] perf doc: Pass " tip-bot2 for Ian Rogers
  2020-04-29 23:14 ` [PATCH v12 2/4] tools feature: add support for detecting libpfm4 Ian Rogers
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2020-04-29 23:14 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau,
	Yonghong Song, Andrii Nakryiko, Greg Kroah-Hartman,
	Thomas Gleixner, Igor Lubashev, Alexey Budankov,
	Florian Fainelli, Adrian Hunter, Andi Kleen, Jiwei Sun,
	yuzhoujian, Kan Liang, Jin Yao, Leo Yan, John Garry,
	linux-kernel, netdev, bpf, linux-perf-users
  Cc: Stephane Eranian, Ian Rogers

commit e9cfa47e687d ("perf doc: allow ASCIIDOC_EXTRA to be an argument")
allowed ASCIIDOC_EXTRA to be passed as an option to the Documentation
Makefile. This change passes ASCIIDOC_EXTRA, set by detected features or
command line options, prior to doing a Documentation build. This is
necessary to allow conditional compilation, based on configuration
variables, in asciidoc code.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/Makefile.perf | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index d15a311408f1..94a495594e99 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -188,7 +188,7 @@ AWK     = awk
 # non-config cases
 config := 1
 
-NON_CONFIG_TARGETS := clean python-clean TAGS tags cscope help install-doc install-man install-html install-info install-pdf doc man html info pdf
+NON_CONFIG_TARGETS := clean python-clean TAGS tags cscope help
 
 ifdef MAKECMDGOALS
 ifeq ($(filter-out $(NON_CONFIG_TARGETS),$(MAKECMDGOALS)),)
@@ -832,7 +832,7 @@ INSTALL_DOC_TARGETS += quick-install-doc quick-install-man quick-install-html
 
 # 'make doc' should call 'make -C Documentation all'
 $(DOC_TARGETS):
-	$(Q)$(MAKE) -C $(DOC_DIR) O=$(OUTPUT) $(@:doc=all)
+	$(Q)$(MAKE) -C $(DOC_DIR) O=$(OUTPUT) $(@:doc=all) ASCIIDOC_EXTRA=$(ASCIIDOC_EXTRA)
 
 TAG_FOLDERS= . ../lib ../include
 TAG_FILES= ../../include/uapi/linux/perf_event.h
@@ -959,7 +959,7 @@ install-python_ext:
 
 # 'make install-doc' should call 'make -C Documentation install'
 $(INSTALL_DOC_TARGETS):
-	$(Q)$(MAKE) -C $(DOC_DIR) O=$(OUTPUT) $(@:-doc=)
+	$(Q)$(MAKE) -C $(DOC_DIR) O=$(OUTPUT) $(@:-doc=) ASCIIDOC_EXTRA=$(ASCIIDOC_EXTRA)
 
 ### Cleaning rules
 
-- 
2.26.2.303.gf8c07b1a785-goog


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

* [PATCH v12 2/4] tools feature: add support for detecting libpfm4
  2020-04-29 23:14 [PATCH v12 0/4] perf tools: add support for libpfm4 Ian Rogers
  2020-04-29 23:14 ` [PATCH v12 1/4] perf doc: pass ASCIIDOC_EXTRA as an argument Ian Rogers
@ 2020-04-29 23:14 ` Ian Rogers
  2020-05-08 13:04   ` [tip: perf/core] tools feature: Add " tip-bot2 for Stephane Eranian
  2020-04-29 23:14 ` [PATCH v12 3/4] perf pmu: add perf_pmu__find_by_type helper Ian Rogers
  2020-04-29 23:14 ` [PATCH v12 4/4] perf tools: add support for libpfm4 Ian Rogers
  3 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2020-04-29 23:14 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau,
	Yonghong Song, Andrii Nakryiko, Greg Kroah-Hartman,
	Thomas Gleixner, Igor Lubashev, Alexey Budankov,
	Florian Fainelli, Adrian Hunter, Andi Kleen, Jiwei Sun,
	yuzhoujian, Kan Liang, Jin Yao, Leo Yan, John Garry,
	linux-kernel, netdev, bpf, linux-perf-users
  Cc: Stephane Eranian, Ian Rogers

From: Stephane Eranian <eranian@google.com>

libpfm4 provides an alternate command line encoding of perf events.

Signed-off-by: Stephane Eranian <eranian@google.com>
Reviewed-by: Ian Rogers <irogers@google.com>
---
 tools/build/Makefile.feature       | 3 ++-
 tools/build/feature/Makefile       | 6 +++++-
 tools/build/feature/test-libpfm4.c | 9 +++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 tools/build/feature/test-libpfm4.c

diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature
index 3e0c019ef297..3abd4316cd4f 100644
--- a/tools/build/Makefile.feature
+++ b/tools/build/Makefile.feature
@@ -98,7 +98,8 @@ FEATURE_TESTS_EXTRA :=                  \
          llvm                           \
          llvm-version                   \
          clang                          \
-         libbpf
+         libbpf                         \
+         libpfm4
 
 FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)
 
diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index 92012381393a..84f845b9627d 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -69,7 +69,8 @@ FILES=                                          \
          test-libaio.bin			\
          test-libzstd.bin			\
          test-clang-bpf-global-var.bin		\
-         test-file-handle.bin
+         test-file-handle.bin			\
+         test-libpfm4.bin
 
 FILES := $(addprefix $(OUTPUT),$(FILES))
 
@@ -331,6 +332,9 @@ $(OUTPUT)test-clang-bpf-global-var.bin:
 $(OUTPUT)test-file-handle.bin:
 	$(BUILD)
 
+$(OUTPUT)test-libpfm4.bin:
+	$(BUILD) -lpfm
+
 ###############################
 
 clean:
diff --git a/tools/build/feature/test-libpfm4.c b/tools/build/feature/test-libpfm4.c
new file mode 100644
index 000000000000..af49b259459e
--- /dev/null
+++ b/tools/build/feature/test-libpfm4.c
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <sys/types.h>
+#include <perfmon/pfmlib.h>
+
+int main(void)
+{
+	pfm_initialize();
+	return 0;
+}
-- 
2.26.2.303.gf8c07b1a785-goog


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

* [PATCH v12 3/4] perf pmu: add perf_pmu__find_by_type helper
  2020-04-29 23:14 [PATCH v12 0/4] perf tools: add support for libpfm4 Ian Rogers
  2020-04-29 23:14 ` [PATCH v12 1/4] perf doc: pass ASCIIDOC_EXTRA as an argument Ian Rogers
  2020-04-29 23:14 ` [PATCH v12 2/4] tools feature: add support for detecting libpfm4 Ian Rogers
@ 2020-04-29 23:14 ` Ian Rogers
  2020-05-08 13:04   ` [tip: perf/core] perf pmu: Add " tip-bot2 for Stephane Eranian
  2020-04-29 23:14 ` [PATCH v12 4/4] perf tools: add support for libpfm4 Ian Rogers
  3 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2020-04-29 23:14 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau,
	Yonghong Song, Andrii Nakryiko, Greg Kroah-Hartman,
	Thomas Gleixner, Igor Lubashev, Alexey Budankov,
	Florian Fainelli, Adrian Hunter, Andi Kleen, Jiwei Sun,
	yuzhoujian, Kan Liang, Jin Yao, Leo Yan, John Garry,
	linux-kernel, netdev, bpf, linux-perf-users
  Cc: Stephane Eranian, Ian Rogers

From: Stephane Eranian <eranian@google.com>

This is used by libpfm4 during event parsing to locate the pmu for an
event.

Signed-off-by: Stephane Eranian <eranian@google.com>
Reviewed-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/pmu.c | 11 +++++++++++
 tools/perf/util/pmu.h |  1 +
 2 files changed, 12 insertions(+)

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index d9f89ed18dea..0fb7d438d9ad 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -871,6 +871,17 @@ static struct perf_pmu *pmu_find(const char *name)
 	return NULL;
 }
 
+struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
+{
+	struct perf_pmu *pmu;
+
+	list_for_each_entry(pmu, &pmus, list)
+		if (pmu->type == type)
+			return pmu;
+
+	return NULL;
+}
+
 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
 {
 	/*
diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index 1edd214b75a5..cb6fbec50313 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -72,6 +72,7 @@ struct perf_pmu_alias {
 };
 
 struct perf_pmu *perf_pmu__find(const char *name);
+struct perf_pmu *perf_pmu__find_by_type(unsigned int type);
 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
 		     struct list_head *head_terms,
 		     struct parse_events_error *error);
-- 
2.26.2.303.gf8c07b1a785-goog


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

* [PATCH v12 4/4] perf tools: add support for libpfm4
  2020-04-29 23:14 [PATCH v12 0/4] perf tools: add support for libpfm4 Ian Rogers
                   ` (2 preceding siblings ...)
  2020-04-29 23:14 ` [PATCH v12 3/4] perf pmu: add perf_pmu__find_by_type helper Ian Rogers
@ 2020-04-29 23:14 ` Ian Rogers
  2020-04-30 13:34     ` Arnaldo Carvalho de Melo
  3 siblings, 1 reply; 14+ messages in thread
From: Ian Rogers @ 2020-04-29 23:14 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Alexei Starovoitov, Daniel Borkmann, Martin KaFai Lau,
	Yonghong Song, Andrii Nakryiko, Greg Kroah-Hartman,
	Thomas Gleixner, Igor Lubashev, Alexey Budankov,
	Florian Fainelli, Adrian Hunter, Andi Kleen, Jiwei Sun,
	yuzhoujian, Kan Liang, Jin Yao, Leo Yan, John Garry,
	linux-kernel, netdev, bpf, linux-perf-users
  Cc: Stephane Eranian, Ian Rogers

From: Stephane Eranian <eranian@google.com>

This patch links perf with the libpfm4 library if it is available
and LIBPFM4 is passed to the build. The libpfm4 library
contains hardware event tables for all processors supported by
perf_events. It is a helper library that helps convert from a
symbolic event name to the event encoding required by the
underlying kernel interface. This library is open-source and
available from: http://perfmon2.sf.net.

With this patch, it is possible to specify full hardware events
by name. Hardware filters are also supported. Events must be
specified via the --pfm-events and not -e option. Both options
are active at the same time and it is possible to mix and match:

$ perf stat --pfm-events inst_retired:any_p:c=1:i -e cycles ....

Signed-off-by: Stephane Eranian <eranian@google.com>
Reviewed-by: Ian Rogers <irogers@google.com>
---
 tools/perf/Documentation/perf-record.txt |  11 +
 tools/perf/Documentation/perf-stat.txt   |  10 +
 tools/perf/Documentation/perf-top.txt    |  11 +
 tools/perf/Makefile.config               |  13 ++
 tools/perf/Makefile.perf                 |   2 +
 tools/perf/builtin-list.c                |   3 +
 tools/perf/builtin-record.c              |   8 +
 tools/perf/builtin-stat.c                |   8 +
 tools/perf/builtin-top.c                 |   8 +
 tools/perf/tests/Build                   |   1 +
 tools/perf/tests/builtin-test.c          |   9 +
 tools/perf/tests/pfm.c                   | 207 +++++++++++++++++
 tools/perf/tests/tests.h                 |   3 +
 tools/perf/util/Build                    |   2 +
 tools/perf/util/evsel.c                  |   2 +-
 tools/perf/util/evsel.h                  |   1 +
 tools/perf/util/parse-events.c           |  30 ++-
 tools/perf/util/parse-events.h           |   4 +
 tools/perf/util/pfm.c                    | 277 +++++++++++++++++++++++
 tools/perf/util/pfm.h                    |  43 ++++
 20 files changed, 645 insertions(+), 8 deletions(-)
 create mode 100644 tools/perf/tests/pfm.c
 create mode 100644 tools/perf/util/pfm.c
 create mode 100644 tools/perf/util/pfm.h

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index b3f3b3f1c161..ad2c41f595c2 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -596,6 +596,17 @@ Make a copy of /proc/kcore and place it into a directory with the perf data file
 Limit the sample data max size, <size> is expected to be a number with
 appended unit character - B/K/M/G
 
+ifdef::HAVE_LIBPFM[]
+--pfm-events events::
+Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
+including support for event filters. For example '--pfm-events
+inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
+option using the comma separator. Hardware events and generic hardware
+events cannot be mixed together. The latter must be used with the -e
+option. The -e option and this one can be mixed and matched.  Events
+can be grouped using the {} notation.
+endif::HAVE_LIBPFM[]
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-list[1], linkperf:perf-intel-pt[1]
diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
index 4d56586b2fb9..536952ad592c 100644
--- a/tools/perf/Documentation/perf-stat.txt
+++ b/tools/perf/Documentation/perf-stat.txt
@@ -71,6 +71,16 @@ report::
 --tid=<tid>::
         stat events on existing thread id (comma separated list)
 
+ifdef::HAVE_LIBPFM[]
+--pfm-events events::
+Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
+including support for event filters. For example '--pfm-events
+inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
+option using the comma separator. Hardware events and generic hardware
+events cannot be mixed together. The latter must be used with the -e
+option. The -e option and this one can be mixed and matched.  Events
+can be grouped using the {} notation.
+endif::HAVE_LIBPFM[]
 
 -a::
 --all-cpus::
diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt
index 20227dabc208..ee2024691d46 100644
--- a/tools/perf/Documentation/perf-top.txt
+++ b/tools/perf/Documentation/perf-top.txt
@@ -329,6 +329,17 @@ Default is to monitor all CPUS.
 	The known limitations include exception handing such as
 	setjmp/longjmp will have calls/returns not match.
 
+ifdef::HAVE_LIBPFM[]
+--pfm-events events::
+Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
+including support for event filters. For example '--pfm-events
+inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
+option using the comma separator. Hardware events and generic hardware
+events cannot be mixed together. The latter must be used with the -e
+option. The -e option and this one can be mixed and matched.  Events
+can be grouped using the {} notation.
+endif::HAVE_LIBPFM[]
+
 INTERACTIVE PROMPTING KEYS
 --------------------------
 
diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index 12a8204d63c6..b67804fee1e3 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -1012,6 +1012,19 @@ ifdef LIBCLANGLLVM
   endif
 endif
 
+ifdef LIBPFM4
+  $(call feature_check,libpfm4)
+  ifeq ($(feature-libpfm4), 1)
+    CFLAGS += -DHAVE_LIBPFM
+    EXTLIBS += -lpfm
+    ASCIIDOC_EXTRA = -aHAVE_LIBPFM=1
+    $(call detected,CONFIG_LIBPFM4)
+  else
+    msg := $(warning libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev);
+    NO_LIBPFM4 := 1
+  endif
+endif
+
 # Among the variables below, these:
 #   perfexecdir
 #   perf_include_dir
diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 94a495594e99..dc82578c8773 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -118,6 +118,8 @@ include ../scripts/utilities.mak
 #
 # Define LIBBPF_DYNAMIC to enable libbpf dynamic linking.
 #
+# Define LIBPFM4 to enable libpfm4 events extension.
+#
 
 # As per kernel Makefile, avoid funny character set dependencies
 unexport LC_ALL
diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
index 965ef017496f..d2069bdcb469 100644
--- a/tools/perf/builtin-list.c
+++ b/tools/perf/builtin-list.c
@@ -14,6 +14,7 @@
 #include "util/pmu.h"
 #include "util/debug.h"
 #include "util/metricgroup.h"
+#include "util/pfm.h"
 #include <subcmd/pager.h>
 #include <subcmd/parse-options.h>
 #include <stdio.h>
@@ -53,6 +54,8 @@ int cmd_list(int argc, const char **argv)
 
 	setup_pager();
 
+	libpfm_initialize();
+
 	if (!raw_dump && pager_in_use())
 		printf("\nList of pre-defined events (to be used in -e):\n\n");
 
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 1ab349abe904..fc55b641849f 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -43,6 +43,7 @@
 #include "util/time-utils.h"
 #include "util/units.h"
 #include "util/bpf-event.h"
+#include "util/pfm.h"
 #include "asm/bug.h"
 #include "perf.h"
 
@@ -2421,6 +2422,11 @@ static struct option __record_options[] = {
 #endif
 	OPT_CALLBACK(0, "max-size", &record.output_max_size,
 		     "size", "Limit the maximum size of the output file", parse_output_max_size),
+#ifdef HAVE_LIBPFM
+	OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
+		"libpfm4 event selector. use 'perf list' to list available events",
+		parse_libpfm_events_option),
+#endif
 	OPT_END()
 };
 
@@ -2461,6 +2467,8 @@ int cmd_record(int argc, const char **argv)
 	if (rec->evlist == NULL)
 		return -ENOMEM;
 
+	libpfm_initialize();
+
 	err = perf_config(perf_record_config, rec);
 	if (err)
 		return err;
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 9207b6c45475..607920321c15 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -66,6 +66,7 @@
 #include "util/time-utils.h"
 #include "util/top.h"
 #include "util/affinity.h"
+#include "util/pfm.h"
 #include "asm/bug.h"
 
 #include <linux/time64.h>
@@ -936,6 +937,11 @@ static struct option stat_options[] = {
 		    "Use with 'percore' event qualifier to show the event "
 		    "counts of one hardware thread by sum up total hardware "
 		    "threads of same physical core"),
+#ifdef HAVE_LIBPFM
+	OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
+		"libpfm4 event selector. use 'perf list' to list available events",
+		parse_libpfm_events_option),
+#endif
 	OPT_END()
 };
 
@@ -1874,6 +1880,8 @@ int cmd_stat(int argc, const char **argv)
 	unsigned int interval, timeout;
 	const char * const stat_subcommands[] = { "record", "report" };
 
+	libpfm_initialize();
+
 	setlocale(LC_ALL, "");
 
 	evsel_list = evlist__new();
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 6b067a5ba1d5..e1f477a18f93 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -53,6 +53,7 @@
 
 #include "util/debug.h"
 #include "util/ordered-events.h"
+#include "util/pfm.h"
 
 #include <assert.h>
 #include <elf.h>
@@ -1577,6 +1578,11 @@ int cmd_top(int argc, const char **argv)
 		    "WARNING: should be used on grouped events."),
 	OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
 		    "Enable LBR callgraph stitching approach"),
+#ifdef HAVE_LIBPFM
+	OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
+		"libpfm4 event selector. use 'perf list' to list available events",
+		parse_libpfm_events_option),
+#endif
 	OPTS_EVSWITCH(&top.evswitch),
 	OPT_END()
 	};
@@ -1590,6 +1596,8 @@ int cmd_top(int argc, const char **argv)
 	if (status < 0)
 		return status;
 
+	libpfm_initialize();
+
 	top.annotation_opts.min_pcnt = 5;
 	top.annotation_opts.context  = 4;
 
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index b3d1bf13ca07..7d2ccec9fd75 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -56,6 +56,7 @@ perf-y += mem2node.o
 perf-y += maps.o
 perf-y += time-utils-test.o
 perf-y += genelf.o
+perf-y += pfm.o
 
 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
 	$(call rule_mkdir)
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index b6322eb0f423..8b323151f22c 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -313,6 +313,15 @@ static struct test generic_tests[] = {
 		.desc = "maps__merge_in",
 		.func = test__maps__merge_in,
 	},
+	{
+		.desc = "Test libpfm4 support",
+		.func = test__pfm,
+		.subtest = {
+			.skip_if_fail	= true,
+			.get_nr		= test__pfm_subtest_get_nr,
+			.get_desc	= test__pfm_subtest_get_desc,
+		}
+	},
 	{
 		.func = NULL,
 	},
diff --git a/tools/perf/tests/pfm.c b/tools/perf/tests/pfm.c
new file mode 100644
index 000000000000..1d594fd3d9c1
--- /dev/null
+++ b/tools/perf/tests/pfm.c
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test support for libpfm4 event encodings.
+ *
+ * Copyright 2020 Google LLC.
+ */
+#include "tests.h"
+#include "util/debug.h"
+#include "util/evlist.h"
+#include "util/pfm.h"
+
+#include <linux/kernel.h>
+
+#ifdef HAVE_LIBPFM
+static int test__pfm_events(void);
+static int test__pfm_group(void);
+#endif
+
+static const struct {
+	int (*func)(void);
+	const char *desc;
+} pfm_testcase_table[] = {
+#ifdef HAVE_LIBPFM
+	{
+		.func = test__pfm_events,
+		.desc = "test of individual --pfm-events",
+	},
+	{
+		.func = test__pfm_group,
+		.desc = "test groups of --pfm-events",
+	},
+#endif
+};
+
+#ifdef HAVE_LIBPFM
+static int count_pfm_events(struct perf_evlist *evlist)
+{
+	struct perf_evsel *evsel;
+	int count = 0;
+
+	perf_evlist__for_each_entry(evlist, evsel) {
+		count++;
+	}
+	return count;
+}
+
+static int test__pfm_events(void)
+{
+	struct evlist *evlist;
+	struct option opt;
+	size_t i;
+	const struct {
+		const char *events;
+		int nr_events;
+	} table[] = {
+		{
+			.events = "",
+			.nr_events = 0,
+		},
+		{
+			.events = "instructions",
+			.nr_events = 1,
+		},
+		{
+			.events = "instructions,cycles",
+			.nr_events = 2,
+		},
+		{
+			.events = "stereolab",
+			.nr_events = 0,
+		},
+		{
+			.events = "instructions,instructions",
+			.nr_events = 2,
+		},
+		{
+			.events = "stereolab,instructions",
+			.nr_events = 0,
+		},
+		{
+			.events = "instructions,stereolab",
+			.nr_events = 1,
+		},
+	};
+
+	libpfm_initialize();
+
+	for (i = 0; i < ARRAY_SIZE(table); i++) {
+		evlist = evlist__new();
+		if (evlist == NULL)
+			return -ENOMEM;
+
+		opt.value = evlist;
+		parse_libpfm_events_option(&opt,
+					table[i].events,
+					0);
+		TEST_ASSERT_EQUAL(table[i].events,
+				count_pfm_events(&evlist->core),
+				table[i].nr_events);
+		TEST_ASSERT_EQUAL(table[i].events,
+				evlist->nr_groups,
+				0);
+
+		evlist__delete(evlist);
+	}
+	return 0;
+}
+
+static int test__pfm_group(void)
+{
+	struct evlist *evlist;
+	struct option opt;
+	size_t i;
+	const struct {
+		const char *events;
+		int nr_events;
+		int nr_groups;
+	} table[] = {
+		{
+			.events = "{},",
+			.nr_events = 0,
+			.nr_groups = 0,
+		},
+		{
+			.events = "{instructions}",
+			.nr_events = 1,
+			.nr_groups = 1,
+		},
+		{
+			.events = "{instructions},{}",
+			.nr_events = 1,
+			.nr_groups = 1,
+		},
+		{
+			.events = "{},{instructions}",
+			.nr_events = 0,
+			.nr_groups = 0,
+		},
+		{
+			.events = "{instructions},{instructions}",
+			.nr_events = 2,
+			.nr_groups = 2,
+		},
+		{
+			.events = "{instructions,cycles},{instructions,cycles}",
+			.nr_events = 4,
+			.nr_groups = 2,
+		},
+		{
+			.events = "{stereolab}",
+			.nr_events = 0,
+			.nr_groups = 0,
+		},
+		{
+			.events =
+			"{instructions,cycles},{instructions,stereolab}",
+			.nr_events = 3,
+			.nr_groups = 1,
+		},
+	};
+
+	libpfm_initialize();
+
+	for (i = 0; i < ARRAY_SIZE(table); i++) {
+		evlist = evlist__new();
+		if (evlist == NULL)
+			return -ENOMEM;
+
+		opt.value = evlist;
+		parse_libpfm_events_option(&opt,
+					table[i].events,
+					0);
+		TEST_ASSERT_EQUAL(table[i].events,
+				count_pfm_events(&evlist->core),
+				table[i].nr_events);
+		TEST_ASSERT_EQUAL(table[i].events,
+				evlist->nr_groups,
+				table[i].nr_groups);
+
+		evlist__delete(evlist);
+	}
+	return 0;
+}
+#endif
+
+const char *test__pfm_subtest_get_desc(int i)
+{
+	if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
+		return NULL;
+	return pfm_testcase_table[i].desc;
+}
+
+int test__pfm_subtest_get_nr(void)
+{
+	return (int)ARRAY_SIZE(pfm_testcase_table);
+}
+
+int test__pfm(struct test *test __maybe_unused, int i __maybe_unused)
+{
+#ifdef HAVE_LIBPFM
+	if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
+		return TEST_FAIL;
+	return pfm_testcase_table[i].func();
+#else
+	return TEST_SKIP;
+#endif
+}
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 61a1ab032080..47cbae753b42 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -112,6 +112,9 @@ int test__mem2node(struct test *t, int subtest);
 int test__maps__merge_in(struct test *t, int subtest);
 int test__time_utils(struct test *t, int subtest);
 int test__jit_write_elf(struct test *test, int subtest);
+int test__pfm(struct test *test, int subtest);
+const char *test__pfm_subtest_get_desc(int subtest);
+int test__pfm_subtest_get_nr(void);
 
 bool test__bp_signal_is_supported(void);
 bool test__bp_account_is_supported(void);
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index c0cf8dff694e..afd3d2221c83 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -177,6 +177,8 @@ perf-$(CONFIG_LIBBPF) += bpf-event.o
 
 perf-$(CONFIG_CXX) += c++/
 
+perf-$(CONFIG_LIBPFM4) += pfm.o
+
 CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 CFLAGS_llvm-utils.o += -DPERF_INCLUDE_DIR="BUILD_STR($(perf_include_dir_SQ))"
 
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 6a571d322bb2..a5c88d52c11c 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -2433,7 +2433,7 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
 
 		/* Is there already the separator in the name. */
 		if (strchr(name, '/') ||
-		    strchr(name, ':'))
+		    (strchr(name, ':') && !evsel->is_libpfm_event))
 			sep = "";
 
 		if (asprintf(&new_name, "%s%su", name, sep) < 0)
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index a463bc65b001..709a9d494767 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -76,6 +76,7 @@ struct evsel {
 	bool			ignore_missing_thread;
 	bool			forced_leader;
 	bool			use_uncore_alias;
+	bool			is_libpfm_event;
 	/* parse modifier helper */
 	int			exclude_GH;
 	int			sample_read;
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 10107747b361..e7e0faf36e3c 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -36,6 +36,7 @@
 #include "metricgroup.h"
 #include "util/evsel_config.h"
 #include "util/event.h"
+#include "util/pfm.h"
 
 #define MAX_NAME_LEN 100
 
@@ -344,6 +345,7 @@ static char *get_config_name(struct list_head *head_terms)
 static struct evsel *
 __add_event(struct list_head *list, int *idx,
 	    struct perf_event_attr *attr,
+	    bool init_attr,
 	    char *name, struct perf_pmu *pmu,
 	    struct list_head *config_terms, bool auto_merge_stats,
 	    const char *cpu_list)
@@ -352,7 +354,8 @@ __add_event(struct list_head *list, int *idx,
 	struct perf_cpu_map *cpus = pmu ? pmu->cpus :
 			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
 
-	event_attr_init(attr);
+	if (init_attr)
+		event_attr_init(attr);
 
 	evsel = perf_evsel__new_idx(attr, *idx);
 	if (!evsel)
@@ -370,15 +373,25 @@ __add_event(struct list_head *list, int *idx,
 	if (config_terms)
 		list_splice(config_terms, &evsel->config_terms);
 
-	list_add_tail(&evsel->core.node, list);
+	if (list)
+		list_add_tail(&evsel->core.node, list);
+
 	return evsel;
 }
 
+struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
+					char *name, struct perf_pmu *pmu)
+{
+	return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
+			   NULL);
+}
+
 static int add_event(struct list_head *list, int *idx,
 		     struct perf_event_attr *attr, char *name,
 		     struct list_head *config_terms)
 {
-	return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
+	return __add_event(list, idx, attr, true, name, NULL, config_terms,
+			   false, NULL) ? 0 : -ENOMEM;
 }
 
 static int add_event_tool(struct list_head *list, int *idx,
@@ -390,7 +403,8 @@ static int add_event_tool(struct list_head *list, int *idx,
 		.config = PERF_COUNT_SW_DUMMY,
 	};
 
-	evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
+	evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
+			    "0");
 	if (!evsel)
 		return -ENOMEM;
 	evsel->tool_event = tool_event;
@@ -1446,8 +1460,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
 
 	if (!head_config) {
 		attr.type = pmu->type;
-		evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
-				    auto_merge_stats, NULL);
+		evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
+				    pmu, NULL, auto_merge_stats, NULL);
 		if (evsel) {
 			evsel->pmu_name = name ? strdup(name) : NULL;
 			evsel->use_uncore_alias = use_uncore_alias;
@@ -1487,7 +1501,7 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
 		return -EINVAL;
 	}
 
-	evsel = __add_event(list, &parse_state->idx, &attr,
+	evsel = __add_event(list, &parse_state->idx, &attr, true,
 			    get_config_name(head_config), pmu,
 			    &config_terms, auto_merge_stats, NULL);
 	if (evsel) {
@@ -2794,6 +2808,8 @@ void print_events(const char *event_glob, bool name_only, bool quiet_flag,
 	print_sdt_events(NULL, NULL, name_only);
 
 	metricgroup__print(true, true, NULL, name_only, details_flag);
+
+	print_libpfm_events(name_only, long_desc);
 }
 
 int parse_events__is_hardcoded_term(struct parse_events_term *term)
diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
index 27596cbd0ba0..f7f555b1ca35 100644
--- a/tools/perf/util/parse-events.h
+++ b/tools/perf/util/parse-events.h
@@ -17,6 +17,7 @@ struct evlist;
 struct parse_events_error;
 
 struct option;
+struct perf_pmu;
 
 struct tracepoint_path {
 	char *system;
@@ -186,6 +187,9 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
 			 bool auto_merge_stats,
 			 bool use_alias);
 
+struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
+					char *name, struct perf_pmu *pmu);
+
 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
 			       char *str,
 			       struct list_head **listp);
diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c
new file mode 100644
index 000000000000..4b11adf832a0
--- /dev/null
+++ b/tools/perf/util/pfm.c
@@ -0,0 +1,277 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Support for libpfm4 event encoding.
+ *
+ * Copyright 2020 Google LLC.
+ */
+#include "util/cpumap.h"
+#include "util/debug.h"
+#include "util/event.h"
+#include "util/evlist.h"
+#include "util/evsel.h"
+#include "util/parse-events.h"
+#include "util/pmu.h"
+#include "util/pfm.h"
+
+#include <string.h>
+#include <linux/kernel.h>
+#include <perfmon/pfmlib_perf_event.h>
+
+void libpfm_initialize(void)
+{
+	int ret;
+
+	ret = pfm_initialize();
+	if (ret != PFM_SUCCESS) {
+		ui__warning("libpfm failed to initialize: %s\n",
+			pfm_strerror(ret));
+	}
+}
+
+int parse_libpfm_events_option(const struct option *opt, const char *str,
+			int unset __maybe_unused)
+{
+	struct evlist *evlist = *(struct evlist **)opt->value;
+	struct perf_event_attr attr;
+	struct perf_pmu *pmu;
+	struct evsel *evsel, *grp_leader = NULL;
+	char *p, *q, *p_orig;
+	const char *sep;
+	int grp_evt = -1;
+	int ret;
+
+	p_orig = p = strdup(str);
+	if (!p)
+		return -1;
+	/*
+	 * force loading of the PMU list
+	 */
+	perf_pmu__scan(NULL);
+
+	for (q = p; strsep(&p, ",{}"); q = p) {
+		sep = p ? str + (p - p_orig - 1) : "";
+		if (*sep == '{') {
+			if (grp_evt > -1) {
+				ui__error(
+					"nested event groups not supported\n");
+				goto error;
+			}
+			grp_evt++;
+		}
+
+		/* no event */
+		if (*q == '\0')
+			continue;
+
+		memset(&attr, 0, sizeof(attr));
+		event_attr_init(&attr);
+
+		ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
+						&attr, NULL, NULL);
+
+		if (ret != PFM_SUCCESS) {
+			ui__error("failed to parse event %s : %s\n", str,
+				  pfm_strerror(ret));
+			goto error;
+		}
+
+		pmu = perf_pmu__find_by_type((unsigned int)attr.type);
+		evsel = parse_events__add_event(evlist->core.nr_entries,
+						&attr, q, pmu);
+		if (evsel == NULL)
+			goto error;
+
+		evsel->is_libpfm_event = true;
+
+		evlist__add(evlist, evsel);
+
+		if (grp_evt == 0)
+			grp_leader = evsel;
+
+		if (grp_evt > -1) {
+			evsel->leader = grp_leader;
+			grp_leader->core.nr_members++;
+			grp_evt++;
+		}
+
+		if (*sep == '}') {
+			if (grp_evt < 0) {
+				ui__error(
+				   "cannot close a non-existing event group\n");
+				goto error;
+			}
+			evlist->nr_groups++;
+			grp_leader = NULL;
+			grp_evt = -1;
+		}
+	}
+	return 0;
+error:
+	free(p_orig);
+	return -1;
+}
+
+static const char *srcs[PFM_ATTR_CTRL_MAX] = {
+	[PFM_ATTR_CTRL_UNKNOWN] = "???",
+	[PFM_ATTR_CTRL_PMU] = "PMU",
+	[PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
+};
+
+static void
+print_attr_flags(pfm_event_attr_info_t *info)
+{
+	int n = 0;
+
+	if (info->is_dfl) {
+		printf("[default] ");
+		n++;
+	}
+
+	if (info->is_precise) {
+		printf("[precise] ");
+		n++;
+	}
+
+	if (!n)
+		printf("- ");
+}
+
+static void
+print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc)
+{
+	pfm_event_attr_info_t ainfo;
+	const char *src;
+	int j, ret;
+
+	ainfo.size = sizeof(ainfo);
+
+	printf("  %s\n", info->name);
+	printf("    [%s]\n", info->desc);
+	if (long_desc) {
+		if (info->equiv)
+			printf("      Equiv: %s\n", info->equiv);
+
+		printf("      Code  : 0x%"PRIx64"\n", info->code);
+	}
+	pfm_for_each_event_attr(j, info) {
+		ret = pfm_get_event_attr_info(info->idx, j,
+					      PFM_OS_PERF_EVENT_EXT, &ainfo);
+		if (ret != PFM_SUCCESS)
+			continue;
+
+		if (ainfo.type == PFM_ATTR_UMASK) {
+			printf("      %s:%s\n", info->name, ainfo.name);
+			printf("        [%s]\n", ainfo.desc);
+		}
+
+		if (!long_desc)
+			continue;
+
+		if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
+			ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
+
+		src = srcs[ainfo.ctrl];
+		switch (ainfo.type) {
+		case PFM_ATTR_UMASK:
+			printf("        Umask : 0x%02"PRIx64" : %s: ",
+				ainfo.code, src);
+			print_attr_flags(&ainfo);
+			putchar('\n');
+			break;
+		case PFM_ATTR_MOD_BOOL:
+			printf("      Modif : %s: [%s] : %s (boolean)\n", src,
+				ainfo.name, ainfo.desc);
+			break;
+		case PFM_ATTR_MOD_INTEGER:
+			printf("      Modif : %s: [%s] : %s (integer)\n", src,
+				ainfo.name, ainfo.desc);
+			break;
+		case PFM_ATTR_NONE:
+		case PFM_ATTR_RAW_UMASK:
+		case PFM_ATTR_MAX:
+		default:
+			printf("      Attr  : %s: [%s] : %s\n", src,
+				ainfo.name, ainfo.desc);
+		}
+	}
+}
+
+/*
+ * list all pmu::event:umask, pmu::event
+ * printed events may not be all valid combinations of umask for an event
+ */
+static void
+print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info)
+{
+	pfm_event_attr_info_t ainfo;
+	int j, ret;
+	bool has_umask = false;
+
+	ainfo.size = sizeof(ainfo);
+
+	pfm_for_each_event_attr(j, info) {
+		ret = pfm_get_event_attr_info(info->idx, j,
+					      PFM_OS_PERF_EVENT_EXT, &ainfo);
+		if (ret != PFM_SUCCESS)
+			continue;
+
+		if (ainfo.type != PFM_ATTR_UMASK)
+			continue;
+
+		printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name);
+		has_umask = true;
+	}
+	if (!has_umask)
+		printf("%s::%s\n", pinfo->name, info->name);
+}
+
+void print_libpfm_events(bool name_only, bool long_desc)
+{
+	pfm_event_info_t info;
+	pfm_pmu_info_t pinfo;
+	int i, p, ret;
+
+	/* initialize to zero to indicate ABI version */
+	info.size  = sizeof(info);
+	pinfo.size = sizeof(pinfo);
+
+	if (!name_only)
+		puts("\nList of pre-defined events (to be used in --pfm-events):\n");
+
+	pfm_for_all_pmus(p) {
+		bool printed_pmu = false;
+
+		ret = pfm_get_pmu_info(p, &pinfo);
+		if (ret != PFM_SUCCESS)
+			continue;
+
+		/* only print events that are supported by host HW */
+		if (!pinfo.is_present)
+			continue;
+
+		/* handled by perf directly */
+		if (pinfo.pmu == PFM_PMU_PERF_EVENT)
+			continue;
+
+		for (i = pinfo.first_event; i != -1;
+		     i = pfm_get_event_next(i)) {
+
+			ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
+						&info);
+			if (ret != PFM_SUCCESS)
+				continue;
+
+			if (!name_only && !printed_pmu) {
+				printf("%s:\n", pinfo.name);
+				printed_pmu = true;
+			}
+
+			if (!name_only)
+				print_libpfm_events_detailed(&info, long_desc);
+			else
+				print_libpfm_events_raw(&pinfo, &info);
+		}
+		if (!name_only && printed_pmu)
+			putchar('\n');
+	}
+}
diff --git a/tools/perf/util/pfm.h b/tools/perf/util/pfm.h
new file mode 100644
index 000000000000..532dafe88bc0
--- /dev/null
+++ b/tools/perf/util/pfm.h
@@ -0,0 +1,43 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Support for libpfm4 event encoding.
+ *
+ * Copyright 2020 Google LLC.
+ */
+#ifndef __PERF_PFM_H
+#define __PERF_PFM_H
+
+#include <subcmd/parse-options.h>
+
+#ifdef HAVE_LIBPFM
+void libpfm_initialize(void);
+
+int parse_libpfm_events_option(const struct option *opt, const char *str,
+			int unset);
+
+void print_libpfm_events(bool name_only, bool long_desc);
+
+#else
+#include <linux/compiler.h>
+
+static inline void libpfm_initialize(void)
+{
+}
+
+static inline int parse_libpfm_events_option(
+	const struct option *opt __maybe_unused,
+	const char *str __maybe_unused,
+	int unset __maybe_unused)
+{
+	return 0;
+}
+
+static inline void print_libpfm_events(bool name_only __maybe_unused,
+				       bool long_desc __maybe_unused)
+{
+}
+
+#endif
+
+
+#endif /* __PERF_PFM_H */
-- 
2.26.2.303.gf8c07b1a785-goog


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

* Re: [PATCH v12 4/4] perf tools: add support for libpfm4
  2020-04-29 23:14 ` [PATCH v12 4/4] perf tools: add support for libpfm4 Ian Rogers
@ 2020-04-30 13:34     ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2020-04-30 13:34 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Yonghong Song, Andrii Nakryiko,
	Greg Kroah-Hartman, Thomas Gleixner, Igor Lubashev,
	Alexey Budankov, Florian Fainelli, Adrian Hunter, Andi Kleen,
	Jiwei Sun, yuzhoujian, Kan Liang, Jin Yao, Leo Yan, John Garry,
	linux-kernel, netdev, bpf, linux-perf-users, Stephane Eranian

Em Wed, Apr 29, 2020 at 04:14:43PM -0700, Ian Rogers escreveu:
> From: Stephane Eranian <eranian@google.com>
> 
> This patch links perf with the libpfm4 library if it is available
> and LIBPFM4 is passed to the build. The libpfm4 library
> contains hardware event tables for all processors supported by
> perf_events. It is a helper library that helps convert from a
> symbolic event name to the event encoding required by the
> underlying kernel interface. This library is open-source and
> available from: http://perfmon2.sf.net.

Hey, I think we should not call libpfm_initialize() in all tools, better
leave it to when the --pfm-event callback gets called, because the user
is really using that event syntax, i.e. do it lazily, to reduce the
amount of initialization that perf does, which is already excessive and
needs triming down, to do more things only when they are really, really
needed.

I have applied the previous, preparatory patches.

- Arnaldo
 
> With this patch, it is possible to specify full hardware events
> by name. Hardware filters are also supported. Events must be
> specified via the --pfm-events and not -e option. Both options
> are active at the same time and it is possible to mix and match:
> 
> $ perf stat --pfm-events inst_retired:any_p:c=1:i -e cycles ....
> 
> Signed-off-by: Stephane Eranian <eranian@google.com>
> Reviewed-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/Documentation/perf-record.txt |  11 +
>  tools/perf/Documentation/perf-stat.txt   |  10 +
>  tools/perf/Documentation/perf-top.txt    |  11 +
>  tools/perf/Makefile.config               |  13 ++
>  tools/perf/Makefile.perf                 |   2 +
>  tools/perf/builtin-list.c                |   3 +
>  tools/perf/builtin-record.c              |   8 +
>  tools/perf/builtin-stat.c                |   8 +
>  tools/perf/builtin-top.c                 |   8 +
>  tools/perf/tests/Build                   |   1 +
>  tools/perf/tests/builtin-test.c          |   9 +
>  tools/perf/tests/pfm.c                   | 207 +++++++++++++++++
>  tools/perf/tests/tests.h                 |   3 +
>  tools/perf/util/Build                    |   2 +
>  tools/perf/util/evsel.c                  |   2 +-
>  tools/perf/util/evsel.h                  |   1 +
>  tools/perf/util/parse-events.c           |  30 ++-
>  tools/perf/util/parse-events.h           |   4 +
>  tools/perf/util/pfm.c                    | 277 +++++++++++++++++++++++
>  tools/perf/util/pfm.h                    |  43 ++++
>  20 files changed, 645 insertions(+), 8 deletions(-)
>  create mode 100644 tools/perf/tests/pfm.c
>  create mode 100644 tools/perf/util/pfm.c
>  create mode 100644 tools/perf/util/pfm.h
> 
> diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> index b3f3b3f1c161..ad2c41f595c2 100644
> --- a/tools/perf/Documentation/perf-record.txt
> +++ b/tools/perf/Documentation/perf-record.txt
> @@ -596,6 +596,17 @@ Make a copy of /proc/kcore and place it into a directory with the perf data file
>  Limit the sample data max size, <size> is expected to be a number with
>  appended unit character - B/K/M/G
>  
> +ifdef::HAVE_LIBPFM[]
> +--pfm-events events::
> +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> +including support for event filters. For example '--pfm-events
> +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> +option using the comma separator. Hardware events and generic hardware
> +events cannot be mixed together. The latter must be used with the -e
> +option. The -e option and this one can be mixed and matched.  Events
> +can be grouped using the {} notation.
> +endif::HAVE_LIBPFM[]
> +
>  SEE ALSO
>  --------
>  linkperf:perf-stat[1], linkperf:perf-list[1], linkperf:perf-intel-pt[1]
> diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
> index 4d56586b2fb9..536952ad592c 100644
> --- a/tools/perf/Documentation/perf-stat.txt
> +++ b/tools/perf/Documentation/perf-stat.txt
> @@ -71,6 +71,16 @@ report::
>  --tid=<tid>::
>          stat events on existing thread id (comma separated list)
>  
> +ifdef::HAVE_LIBPFM[]
> +--pfm-events events::
> +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> +including support for event filters. For example '--pfm-events
> +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> +option using the comma separator. Hardware events and generic hardware
> +events cannot be mixed together. The latter must be used with the -e
> +option. The -e option and this one can be mixed and matched.  Events
> +can be grouped using the {} notation.
> +endif::HAVE_LIBPFM[]
>  
>  -a::
>  --all-cpus::
> diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt
> index 20227dabc208..ee2024691d46 100644
> --- a/tools/perf/Documentation/perf-top.txt
> +++ b/tools/perf/Documentation/perf-top.txt
> @@ -329,6 +329,17 @@ Default is to monitor all CPUS.
>  	The known limitations include exception handing such as
>  	setjmp/longjmp will have calls/returns not match.
>  
> +ifdef::HAVE_LIBPFM[]
> +--pfm-events events::
> +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> +including support for event filters. For example '--pfm-events
> +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> +option using the comma separator. Hardware events and generic hardware
> +events cannot be mixed together. The latter must be used with the -e
> +option. The -e option and this one can be mixed and matched.  Events
> +can be grouped using the {} notation.
> +endif::HAVE_LIBPFM[]
> +
>  INTERACTIVE PROMPTING KEYS
>  --------------------------
>  
> diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> index 12a8204d63c6..b67804fee1e3 100644
> --- a/tools/perf/Makefile.config
> +++ b/tools/perf/Makefile.config
> @@ -1012,6 +1012,19 @@ ifdef LIBCLANGLLVM
>    endif
>  endif
>  
> +ifdef LIBPFM4
> +  $(call feature_check,libpfm4)
> +  ifeq ($(feature-libpfm4), 1)
> +    CFLAGS += -DHAVE_LIBPFM
> +    EXTLIBS += -lpfm
> +    ASCIIDOC_EXTRA = -aHAVE_LIBPFM=1
> +    $(call detected,CONFIG_LIBPFM4)
> +  else
> +    msg := $(warning libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev);
> +    NO_LIBPFM4 := 1
> +  endif
> +endif
> +
>  # Among the variables below, these:
>  #   perfexecdir
>  #   perf_include_dir
> diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
> index 94a495594e99..dc82578c8773 100644
> --- a/tools/perf/Makefile.perf
> +++ b/tools/perf/Makefile.perf
> @@ -118,6 +118,8 @@ include ../scripts/utilities.mak
>  #
>  # Define LIBBPF_DYNAMIC to enable libbpf dynamic linking.
>  #
> +# Define LIBPFM4 to enable libpfm4 events extension.
> +#
>  
>  # As per kernel Makefile, avoid funny character set dependencies
>  unexport LC_ALL
> diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
> index 965ef017496f..d2069bdcb469 100644
> --- a/tools/perf/builtin-list.c
> +++ b/tools/perf/builtin-list.c
> @@ -14,6 +14,7 @@
>  #include "util/pmu.h"
>  #include "util/debug.h"
>  #include "util/metricgroup.h"
> +#include "util/pfm.h"
>  #include <subcmd/pager.h>
>  #include <subcmd/parse-options.h>
>  #include <stdio.h>
> @@ -53,6 +54,8 @@ int cmd_list(int argc, const char **argv)
>  
>  	setup_pager();
>  
> +	libpfm_initialize();
> +
>  	if (!raw_dump && pager_in_use())
>  		printf("\nList of pre-defined events (to be used in -e):\n\n");
>  
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index 1ab349abe904..fc55b641849f 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -43,6 +43,7 @@
>  #include "util/time-utils.h"
>  #include "util/units.h"
>  #include "util/bpf-event.h"
> +#include "util/pfm.h"
>  #include "asm/bug.h"
>  #include "perf.h"
>  
> @@ -2421,6 +2422,11 @@ static struct option __record_options[] = {
>  #endif
>  	OPT_CALLBACK(0, "max-size", &record.output_max_size,
>  		     "size", "Limit the maximum size of the output file", parse_output_max_size),
> +#ifdef HAVE_LIBPFM
> +	OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
> +		"libpfm4 event selector. use 'perf list' to list available events",
> +		parse_libpfm_events_option),
> +#endif
>  	OPT_END()
>  };
>  
> @@ -2461,6 +2467,8 @@ int cmd_record(int argc, const char **argv)
>  	if (rec->evlist == NULL)
>  		return -ENOMEM;
>  
> +	libpfm_initialize();
> +
>  	err = perf_config(perf_record_config, rec);
>  	if (err)
>  		return err;
> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index 9207b6c45475..607920321c15 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -66,6 +66,7 @@
>  #include "util/time-utils.h"
>  #include "util/top.h"
>  #include "util/affinity.h"
> +#include "util/pfm.h"
>  #include "asm/bug.h"
>  
>  #include <linux/time64.h>
> @@ -936,6 +937,11 @@ static struct option stat_options[] = {
>  		    "Use with 'percore' event qualifier to show the event "
>  		    "counts of one hardware thread by sum up total hardware "
>  		    "threads of same physical core"),
> +#ifdef HAVE_LIBPFM
> +	OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
> +		"libpfm4 event selector. use 'perf list' to list available events",
> +		parse_libpfm_events_option),
> +#endif
>  	OPT_END()
>  };
>  
> @@ -1874,6 +1880,8 @@ int cmd_stat(int argc, const char **argv)
>  	unsigned int interval, timeout;
>  	const char * const stat_subcommands[] = { "record", "report" };
>  
> +	libpfm_initialize();
> +
>  	setlocale(LC_ALL, "");
>  
>  	evsel_list = evlist__new();
> diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
> index 6b067a5ba1d5..e1f477a18f93 100644
> --- a/tools/perf/builtin-top.c
> +++ b/tools/perf/builtin-top.c
> @@ -53,6 +53,7 @@
>  
>  #include "util/debug.h"
>  #include "util/ordered-events.h"
> +#include "util/pfm.h"
>  
>  #include <assert.h>
>  #include <elf.h>
> @@ -1577,6 +1578,11 @@ int cmd_top(int argc, const char **argv)
>  		    "WARNING: should be used on grouped events."),
>  	OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
>  		    "Enable LBR callgraph stitching approach"),
> +#ifdef HAVE_LIBPFM
> +	OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
> +		"libpfm4 event selector. use 'perf list' to list available events",
> +		parse_libpfm_events_option),
> +#endif
>  	OPTS_EVSWITCH(&top.evswitch),
>  	OPT_END()
>  	};
> @@ -1590,6 +1596,8 @@ int cmd_top(int argc, const char **argv)
>  	if (status < 0)
>  		return status;
>  
> +	libpfm_initialize();
> +
>  	top.annotation_opts.min_pcnt = 5;
>  	top.annotation_opts.context  = 4;
>  
> diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
> index b3d1bf13ca07..7d2ccec9fd75 100644
> --- a/tools/perf/tests/Build
> +++ b/tools/perf/tests/Build
> @@ -56,6 +56,7 @@ perf-y += mem2node.o
>  perf-y += maps.o
>  perf-y += time-utils-test.o
>  perf-y += genelf.o
> +perf-y += pfm.o
>  
>  $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
>  	$(call rule_mkdir)
> diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
> index b6322eb0f423..8b323151f22c 100644
> --- a/tools/perf/tests/builtin-test.c
> +++ b/tools/perf/tests/builtin-test.c
> @@ -313,6 +313,15 @@ static struct test generic_tests[] = {
>  		.desc = "maps__merge_in",
>  		.func = test__maps__merge_in,
>  	},
> +	{
> +		.desc = "Test libpfm4 support",
> +		.func = test__pfm,
> +		.subtest = {
> +			.skip_if_fail	= true,
> +			.get_nr		= test__pfm_subtest_get_nr,
> +			.get_desc	= test__pfm_subtest_get_desc,
> +		}
> +	},
>  	{
>  		.func = NULL,
>  	},
> diff --git a/tools/perf/tests/pfm.c b/tools/perf/tests/pfm.c
> new file mode 100644
> index 000000000000..1d594fd3d9c1
> --- /dev/null
> +++ b/tools/perf/tests/pfm.c
> @@ -0,0 +1,207 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Test support for libpfm4 event encodings.
> + *
> + * Copyright 2020 Google LLC.
> + */
> +#include "tests.h"
> +#include "util/debug.h"
> +#include "util/evlist.h"
> +#include "util/pfm.h"
> +
> +#include <linux/kernel.h>
> +
> +#ifdef HAVE_LIBPFM
> +static int test__pfm_events(void);
> +static int test__pfm_group(void);
> +#endif
> +
> +static const struct {
> +	int (*func)(void);
> +	const char *desc;
> +} pfm_testcase_table[] = {
> +#ifdef HAVE_LIBPFM
> +	{
> +		.func = test__pfm_events,
> +		.desc = "test of individual --pfm-events",
> +	},
> +	{
> +		.func = test__pfm_group,
> +		.desc = "test groups of --pfm-events",
> +	},
> +#endif
> +};
> +
> +#ifdef HAVE_LIBPFM
> +static int count_pfm_events(struct perf_evlist *evlist)
> +{
> +	struct perf_evsel *evsel;
> +	int count = 0;
> +
> +	perf_evlist__for_each_entry(evlist, evsel) {
> +		count++;
> +	}
> +	return count;
> +}
> +
> +static int test__pfm_events(void)
> +{
> +	struct evlist *evlist;
> +	struct option opt;
> +	size_t i;
> +	const struct {
> +		const char *events;
> +		int nr_events;
> +	} table[] = {
> +		{
> +			.events = "",
> +			.nr_events = 0,
> +		},
> +		{
> +			.events = "instructions",
> +			.nr_events = 1,
> +		},
> +		{
> +			.events = "instructions,cycles",
> +			.nr_events = 2,
> +		},
> +		{
> +			.events = "stereolab",
> +			.nr_events = 0,
> +		},
> +		{
> +			.events = "instructions,instructions",
> +			.nr_events = 2,
> +		},
> +		{
> +			.events = "stereolab,instructions",
> +			.nr_events = 0,
> +		},
> +		{
> +			.events = "instructions,stereolab",
> +			.nr_events = 1,
> +		},
> +	};
> +
> +	libpfm_initialize();
> +
> +	for (i = 0; i < ARRAY_SIZE(table); i++) {
> +		evlist = evlist__new();
> +		if (evlist == NULL)
> +			return -ENOMEM;
> +
> +		opt.value = evlist;
> +		parse_libpfm_events_option(&opt,
> +					table[i].events,
> +					0);
> +		TEST_ASSERT_EQUAL(table[i].events,
> +				count_pfm_events(&evlist->core),
> +				table[i].nr_events);
> +		TEST_ASSERT_EQUAL(table[i].events,
> +				evlist->nr_groups,
> +				0);
> +
> +		evlist__delete(evlist);
> +	}
> +	return 0;
> +}
> +
> +static int test__pfm_group(void)
> +{
> +	struct evlist *evlist;
> +	struct option opt;
> +	size_t i;
> +	const struct {
> +		const char *events;
> +		int nr_events;
> +		int nr_groups;
> +	} table[] = {
> +		{
> +			.events = "{},",
> +			.nr_events = 0,
> +			.nr_groups = 0,
> +		},
> +		{
> +			.events = "{instructions}",
> +			.nr_events = 1,
> +			.nr_groups = 1,
> +		},
> +		{
> +			.events = "{instructions},{}",
> +			.nr_events = 1,
> +			.nr_groups = 1,
> +		},
> +		{
> +			.events = "{},{instructions}",
> +			.nr_events = 0,
> +			.nr_groups = 0,
> +		},
> +		{
> +			.events = "{instructions},{instructions}",
> +			.nr_events = 2,
> +			.nr_groups = 2,
> +		},
> +		{
> +			.events = "{instructions,cycles},{instructions,cycles}",
> +			.nr_events = 4,
> +			.nr_groups = 2,
> +		},
> +		{
> +			.events = "{stereolab}",
> +			.nr_events = 0,
> +			.nr_groups = 0,
> +		},
> +		{
> +			.events =
> +			"{instructions,cycles},{instructions,stereolab}",
> +			.nr_events = 3,
> +			.nr_groups = 1,
> +		},
> +	};
> +
> +	libpfm_initialize();
> +
> +	for (i = 0; i < ARRAY_SIZE(table); i++) {
> +		evlist = evlist__new();
> +		if (evlist == NULL)
> +			return -ENOMEM;
> +
> +		opt.value = evlist;
> +		parse_libpfm_events_option(&opt,
> +					table[i].events,
> +					0);
> +		TEST_ASSERT_EQUAL(table[i].events,
> +				count_pfm_events(&evlist->core),
> +				table[i].nr_events);
> +		TEST_ASSERT_EQUAL(table[i].events,
> +				evlist->nr_groups,
> +				table[i].nr_groups);
> +
> +		evlist__delete(evlist);
> +	}
> +	return 0;
> +}
> +#endif
> +
> +const char *test__pfm_subtest_get_desc(int i)
> +{
> +	if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> +		return NULL;
> +	return pfm_testcase_table[i].desc;
> +}
> +
> +int test__pfm_subtest_get_nr(void)
> +{
> +	return (int)ARRAY_SIZE(pfm_testcase_table);
> +}
> +
> +int test__pfm(struct test *test __maybe_unused, int i __maybe_unused)
> +{
> +#ifdef HAVE_LIBPFM
> +	if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> +		return TEST_FAIL;
> +	return pfm_testcase_table[i].func();
> +#else
> +	return TEST_SKIP;
> +#endif
> +}
> diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
> index 61a1ab032080..47cbae753b42 100644
> --- a/tools/perf/tests/tests.h
> +++ b/tools/perf/tests/tests.h
> @@ -112,6 +112,9 @@ int test__mem2node(struct test *t, int subtest);
>  int test__maps__merge_in(struct test *t, int subtest);
>  int test__time_utils(struct test *t, int subtest);
>  int test__jit_write_elf(struct test *test, int subtest);
> +int test__pfm(struct test *test, int subtest);
> +const char *test__pfm_subtest_get_desc(int subtest);
> +int test__pfm_subtest_get_nr(void);
>  
>  bool test__bp_signal_is_supported(void);
>  bool test__bp_account_is_supported(void);
> diff --git a/tools/perf/util/Build b/tools/perf/util/Build
> index c0cf8dff694e..afd3d2221c83 100644
> --- a/tools/perf/util/Build
> +++ b/tools/perf/util/Build
> @@ -177,6 +177,8 @@ perf-$(CONFIG_LIBBPF) += bpf-event.o
>  
>  perf-$(CONFIG_CXX) += c++/
>  
> +perf-$(CONFIG_LIBPFM4) += pfm.o
> +
>  CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
>  CFLAGS_llvm-utils.o += -DPERF_INCLUDE_DIR="BUILD_STR($(perf_include_dir_SQ))"
>  
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 6a571d322bb2..a5c88d52c11c 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -2433,7 +2433,7 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
>  
>  		/* Is there already the separator in the name. */
>  		if (strchr(name, '/') ||
> -		    strchr(name, ':'))
> +		    (strchr(name, ':') && !evsel->is_libpfm_event))
>  			sep = "";
>  
>  		if (asprintf(&new_name, "%s%su", name, sep) < 0)
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index a463bc65b001..709a9d494767 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -76,6 +76,7 @@ struct evsel {
>  	bool			ignore_missing_thread;
>  	bool			forced_leader;
>  	bool			use_uncore_alias;
> +	bool			is_libpfm_event;
>  	/* parse modifier helper */
>  	int			exclude_GH;
>  	int			sample_read;
> diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> index 10107747b361..e7e0faf36e3c 100644
> --- a/tools/perf/util/parse-events.c
> +++ b/tools/perf/util/parse-events.c
> @@ -36,6 +36,7 @@
>  #include "metricgroup.h"
>  #include "util/evsel_config.h"
>  #include "util/event.h"
> +#include "util/pfm.h"
>  
>  #define MAX_NAME_LEN 100
>  
> @@ -344,6 +345,7 @@ static char *get_config_name(struct list_head *head_terms)
>  static struct evsel *
>  __add_event(struct list_head *list, int *idx,
>  	    struct perf_event_attr *attr,
> +	    bool init_attr,
>  	    char *name, struct perf_pmu *pmu,
>  	    struct list_head *config_terms, bool auto_merge_stats,
>  	    const char *cpu_list)
> @@ -352,7 +354,8 @@ __add_event(struct list_head *list, int *idx,
>  	struct perf_cpu_map *cpus = pmu ? pmu->cpus :
>  			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
>  
> -	event_attr_init(attr);
> +	if (init_attr)
> +		event_attr_init(attr);
>  
>  	evsel = perf_evsel__new_idx(attr, *idx);
>  	if (!evsel)
> @@ -370,15 +373,25 @@ __add_event(struct list_head *list, int *idx,
>  	if (config_terms)
>  		list_splice(config_terms, &evsel->config_terms);
>  
> -	list_add_tail(&evsel->core.node, list);
> +	if (list)
> +		list_add_tail(&evsel->core.node, list);
> +
>  	return evsel;
>  }
>  
> +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> +					char *name, struct perf_pmu *pmu)
> +{
> +	return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
> +			   NULL);
> +}
> +
>  static int add_event(struct list_head *list, int *idx,
>  		     struct perf_event_attr *attr, char *name,
>  		     struct list_head *config_terms)
>  {
> -	return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
> +	return __add_event(list, idx, attr, true, name, NULL, config_terms,
> +			   false, NULL) ? 0 : -ENOMEM;
>  }
>  
>  static int add_event_tool(struct list_head *list, int *idx,
> @@ -390,7 +403,8 @@ static int add_event_tool(struct list_head *list, int *idx,
>  		.config = PERF_COUNT_SW_DUMMY,
>  	};
>  
> -	evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
> +	evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
> +			    "0");
>  	if (!evsel)
>  		return -ENOMEM;
>  	evsel->tool_event = tool_event;
> @@ -1446,8 +1460,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
>  
>  	if (!head_config) {
>  		attr.type = pmu->type;
> -		evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
> -				    auto_merge_stats, NULL);
> +		evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
> +				    pmu, NULL, auto_merge_stats, NULL);
>  		if (evsel) {
>  			evsel->pmu_name = name ? strdup(name) : NULL;
>  			evsel->use_uncore_alias = use_uncore_alias;
> @@ -1487,7 +1501,7 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
>  		return -EINVAL;
>  	}
>  
> -	evsel = __add_event(list, &parse_state->idx, &attr,
> +	evsel = __add_event(list, &parse_state->idx, &attr, true,
>  			    get_config_name(head_config), pmu,
>  			    &config_terms, auto_merge_stats, NULL);
>  	if (evsel) {
> @@ -2794,6 +2808,8 @@ void print_events(const char *event_glob, bool name_only, bool quiet_flag,
>  	print_sdt_events(NULL, NULL, name_only);
>  
>  	metricgroup__print(true, true, NULL, name_only, details_flag);
> +
> +	print_libpfm_events(name_only, long_desc);
>  }
>  
>  int parse_events__is_hardcoded_term(struct parse_events_term *term)
> diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
> index 27596cbd0ba0..f7f555b1ca35 100644
> --- a/tools/perf/util/parse-events.h
> +++ b/tools/perf/util/parse-events.h
> @@ -17,6 +17,7 @@ struct evlist;
>  struct parse_events_error;
>  
>  struct option;
> +struct perf_pmu;
>  
>  struct tracepoint_path {
>  	char *system;
> @@ -186,6 +187,9 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
>  			 bool auto_merge_stats,
>  			 bool use_alias);
>  
> +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> +					char *name, struct perf_pmu *pmu);
> +
>  int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
>  			       char *str,
>  			       struct list_head **listp);
> diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c
> new file mode 100644
> index 000000000000..4b11adf832a0
> --- /dev/null
> +++ b/tools/perf/util/pfm.c
> @@ -0,0 +1,277 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Support for libpfm4 event encoding.
> + *
> + * Copyright 2020 Google LLC.
> + */
> +#include "util/cpumap.h"
> +#include "util/debug.h"
> +#include "util/event.h"
> +#include "util/evlist.h"
> +#include "util/evsel.h"
> +#include "util/parse-events.h"
> +#include "util/pmu.h"
> +#include "util/pfm.h"
> +
> +#include <string.h>
> +#include <linux/kernel.h>
> +#include <perfmon/pfmlib_perf_event.h>
> +
> +void libpfm_initialize(void)
> +{
> +	int ret;
> +
> +	ret = pfm_initialize();
> +	if (ret != PFM_SUCCESS) {
> +		ui__warning("libpfm failed to initialize: %s\n",
> +			pfm_strerror(ret));
> +	}
> +}
> +
> +int parse_libpfm_events_option(const struct option *opt, const char *str,
> +			int unset __maybe_unused)
> +{
> +	struct evlist *evlist = *(struct evlist **)opt->value;
> +	struct perf_event_attr attr;
> +	struct perf_pmu *pmu;
> +	struct evsel *evsel, *grp_leader = NULL;
> +	char *p, *q, *p_orig;
> +	const char *sep;
> +	int grp_evt = -1;
> +	int ret;
> +
> +	p_orig = p = strdup(str);
> +	if (!p)
> +		return -1;
> +	/*
> +	 * force loading of the PMU list
> +	 */
> +	perf_pmu__scan(NULL);
> +
> +	for (q = p; strsep(&p, ",{}"); q = p) {
> +		sep = p ? str + (p - p_orig - 1) : "";
> +		if (*sep == '{') {
> +			if (grp_evt > -1) {
> +				ui__error(
> +					"nested event groups not supported\n");
> +				goto error;
> +			}
> +			grp_evt++;
> +		}
> +
> +		/* no event */
> +		if (*q == '\0')
> +			continue;
> +
> +		memset(&attr, 0, sizeof(attr));
> +		event_attr_init(&attr);
> +
> +		ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
> +						&attr, NULL, NULL);
> +
> +		if (ret != PFM_SUCCESS) {
> +			ui__error("failed to parse event %s : %s\n", str,
> +				  pfm_strerror(ret));
> +			goto error;
> +		}
> +
> +		pmu = perf_pmu__find_by_type((unsigned int)attr.type);
> +		evsel = parse_events__add_event(evlist->core.nr_entries,
> +						&attr, q, pmu);
> +		if (evsel == NULL)
> +			goto error;
> +
> +		evsel->is_libpfm_event = true;
> +
> +		evlist__add(evlist, evsel);
> +
> +		if (grp_evt == 0)
> +			grp_leader = evsel;
> +
> +		if (grp_evt > -1) {
> +			evsel->leader = grp_leader;
> +			grp_leader->core.nr_members++;
> +			grp_evt++;
> +		}
> +
> +		if (*sep == '}') {
> +			if (grp_evt < 0) {
> +				ui__error(
> +				   "cannot close a non-existing event group\n");
> +				goto error;
> +			}
> +			evlist->nr_groups++;
> +			grp_leader = NULL;
> +			grp_evt = -1;
> +		}
> +	}
> +	return 0;
> +error:
> +	free(p_orig);
> +	return -1;
> +}
> +
> +static const char *srcs[PFM_ATTR_CTRL_MAX] = {
> +	[PFM_ATTR_CTRL_UNKNOWN] = "???",
> +	[PFM_ATTR_CTRL_PMU] = "PMU",
> +	[PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
> +};
> +
> +static void
> +print_attr_flags(pfm_event_attr_info_t *info)
> +{
> +	int n = 0;
> +
> +	if (info->is_dfl) {
> +		printf("[default] ");
> +		n++;
> +	}
> +
> +	if (info->is_precise) {
> +		printf("[precise] ");
> +		n++;
> +	}
> +
> +	if (!n)
> +		printf("- ");
> +}
> +
> +static void
> +print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc)
> +{
> +	pfm_event_attr_info_t ainfo;
> +	const char *src;
> +	int j, ret;
> +
> +	ainfo.size = sizeof(ainfo);
> +
> +	printf("  %s\n", info->name);
> +	printf("    [%s]\n", info->desc);
> +	if (long_desc) {
> +		if (info->equiv)
> +			printf("      Equiv: %s\n", info->equiv);
> +
> +		printf("      Code  : 0x%"PRIx64"\n", info->code);
> +	}
> +	pfm_for_each_event_attr(j, info) {
> +		ret = pfm_get_event_attr_info(info->idx, j,
> +					      PFM_OS_PERF_EVENT_EXT, &ainfo);
> +		if (ret != PFM_SUCCESS)
> +			continue;
> +
> +		if (ainfo.type == PFM_ATTR_UMASK) {
> +			printf("      %s:%s\n", info->name, ainfo.name);
> +			printf("        [%s]\n", ainfo.desc);
> +		}
> +
> +		if (!long_desc)
> +			continue;
> +
> +		if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
> +			ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
> +
> +		src = srcs[ainfo.ctrl];
> +		switch (ainfo.type) {
> +		case PFM_ATTR_UMASK:
> +			printf("        Umask : 0x%02"PRIx64" : %s: ",
> +				ainfo.code, src);
> +			print_attr_flags(&ainfo);
> +			putchar('\n');
> +			break;
> +		case PFM_ATTR_MOD_BOOL:
> +			printf("      Modif : %s: [%s] : %s (boolean)\n", src,
> +				ainfo.name, ainfo.desc);
> +			break;
> +		case PFM_ATTR_MOD_INTEGER:
> +			printf("      Modif : %s: [%s] : %s (integer)\n", src,
> +				ainfo.name, ainfo.desc);
> +			break;
> +		case PFM_ATTR_NONE:
> +		case PFM_ATTR_RAW_UMASK:
> +		case PFM_ATTR_MAX:
> +		default:
> +			printf("      Attr  : %s: [%s] : %s\n", src,
> +				ainfo.name, ainfo.desc);
> +		}
> +	}
> +}
> +
> +/*
> + * list all pmu::event:umask, pmu::event
> + * printed events may not be all valid combinations of umask for an event
> + */
> +static void
> +print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info)
> +{
> +	pfm_event_attr_info_t ainfo;
> +	int j, ret;
> +	bool has_umask = false;
> +
> +	ainfo.size = sizeof(ainfo);
> +
> +	pfm_for_each_event_attr(j, info) {
> +		ret = pfm_get_event_attr_info(info->idx, j,
> +					      PFM_OS_PERF_EVENT_EXT, &ainfo);
> +		if (ret != PFM_SUCCESS)
> +			continue;
> +
> +		if (ainfo.type != PFM_ATTR_UMASK)
> +			continue;
> +
> +		printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name);
> +		has_umask = true;
> +	}
> +	if (!has_umask)
> +		printf("%s::%s\n", pinfo->name, info->name);
> +}
> +
> +void print_libpfm_events(bool name_only, bool long_desc)
> +{
> +	pfm_event_info_t info;
> +	pfm_pmu_info_t pinfo;
> +	int i, p, ret;
> +
> +	/* initialize to zero to indicate ABI version */
> +	info.size  = sizeof(info);
> +	pinfo.size = sizeof(pinfo);
> +
> +	if (!name_only)
> +		puts("\nList of pre-defined events (to be used in --pfm-events):\n");
> +
> +	pfm_for_all_pmus(p) {
> +		bool printed_pmu = false;
> +
> +		ret = pfm_get_pmu_info(p, &pinfo);
> +		if (ret != PFM_SUCCESS)
> +			continue;
> +
> +		/* only print events that are supported by host HW */
> +		if (!pinfo.is_present)
> +			continue;
> +
> +		/* handled by perf directly */
> +		if (pinfo.pmu == PFM_PMU_PERF_EVENT)
> +			continue;
> +
> +		for (i = pinfo.first_event; i != -1;
> +		     i = pfm_get_event_next(i)) {
> +
> +			ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
> +						&info);
> +			if (ret != PFM_SUCCESS)
> +				continue;
> +
> +			if (!name_only && !printed_pmu) {
> +				printf("%s:\n", pinfo.name);
> +				printed_pmu = true;
> +			}
> +
> +			if (!name_only)
> +				print_libpfm_events_detailed(&info, long_desc);
> +			else
> +				print_libpfm_events_raw(&pinfo, &info);
> +		}
> +		if (!name_only && printed_pmu)
> +			putchar('\n');
> +	}
> +}
> diff --git a/tools/perf/util/pfm.h b/tools/perf/util/pfm.h
> new file mode 100644
> index 000000000000..532dafe88bc0
> --- /dev/null
> +++ b/tools/perf/util/pfm.h
> @@ -0,0 +1,43 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Support for libpfm4 event encoding.
> + *
> + * Copyright 2020 Google LLC.
> + */
> +#ifndef __PERF_PFM_H
> +#define __PERF_PFM_H
> +
> +#include <subcmd/parse-options.h>
> +
> +#ifdef HAVE_LIBPFM
> +void libpfm_initialize(void);
> +
> +int parse_libpfm_events_option(const struct option *opt, const char *str,
> +			int unset);
> +
> +void print_libpfm_events(bool name_only, bool long_desc);
> +
> +#else
> +#include <linux/compiler.h>
> +
> +static inline void libpfm_initialize(void)
> +{
> +}
> +
> +static inline int parse_libpfm_events_option(
> +	const struct option *opt __maybe_unused,
> +	const char *str __maybe_unused,
> +	int unset __maybe_unused)
> +{
> +	return 0;
> +}
> +
> +static inline void print_libpfm_events(bool name_only __maybe_unused,
> +				       bool long_desc __maybe_unused)
> +{
> +}
> +
> +#endif
> +
> +
> +#endif /* __PERF_PFM_H */
> -- 
> 2.26.2.303.gf8c07b1a785-goog
> 

-- 

- Arnaldo

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

* Re: [PATCH v12 4/4] perf tools: add support for libpfm4
@ 2020-04-30 13:34     ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 14+ messages in thread
From: Arnaldo Carvalho de Melo @ 2020-04-30 13:34 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Yonghong Song, Andrii Nakryiko,
	Greg Kroah-Hartman, Thomas Gleixner, Igor Lubashev,
	Alexey Budankov, Florian Fainelli, Adrian Hunter, Andi Kleen,
	Jiwei Sun, yuzhoujian

Em Wed, Apr 29, 2020 at 04:14:43PM -0700, Ian Rogers escreveu:
> From: Stephane Eranian <eranian@google.com>
> 
> This patch links perf with the libpfm4 library if it is available
> and LIBPFM4 is passed to the build. The libpfm4 library
> contains hardware event tables for all processors supported by
> perf_events. It is a helper library that helps convert from a
> symbolic event name to the event encoding required by the
> underlying kernel interface. This library is open-source and
> available from: http://perfmon2.sf.net.

Hey, I think we should not call libpfm_initialize() in all tools, better
leave it to when the --pfm-event callback gets called, because the user
is really using that event syntax, i.e. do it lazily, to reduce the
amount of initialization that perf does, which is already excessive and
needs triming down, to do more things only when they are really, really
needed.

I have applied the previous, preparatory patches.

- Arnaldo
 
> With this patch, it is possible to specify full hardware events
> by name. Hardware filters are also supported. Events must be
> specified via the --pfm-events and not -e option. Both options
> are active at the same time and it is possible to mix and match:
> 
> $ perf stat --pfm-events inst_retired:any_p:c=1:i -e cycles ....
> 
> Signed-off-by: Stephane Eranian <eranian@google.com>
> Reviewed-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/Documentation/perf-record.txt |  11 +
>  tools/perf/Documentation/perf-stat.txt   |  10 +
>  tools/perf/Documentation/perf-top.txt    |  11 +
>  tools/perf/Makefile.config               |  13 ++
>  tools/perf/Makefile.perf                 |   2 +
>  tools/perf/builtin-list.c                |   3 +
>  tools/perf/builtin-record.c              |   8 +
>  tools/perf/builtin-stat.c                |   8 +
>  tools/perf/builtin-top.c                 |   8 +
>  tools/perf/tests/Build                   |   1 +
>  tools/perf/tests/builtin-test.c          |   9 +
>  tools/perf/tests/pfm.c                   | 207 +++++++++++++++++
>  tools/perf/tests/tests.h                 |   3 +
>  tools/perf/util/Build                    |   2 +
>  tools/perf/util/evsel.c                  |   2 +-
>  tools/perf/util/evsel.h                  |   1 +
>  tools/perf/util/parse-events.c           |  30 ++-
>  tools/perf/util/parse-events.h           |   4 +
>  tools/perf/util/pfm.c                    | 277 +++++++++++++++++++++++
>  tools/perf/util/pfm.h                    |  43 ++++
>  20 files changed, 645 insertions(+), 8 deletions(-)
>  create mode 100644 tools/perf/tests/pfm.c
>  create mode 100644 tools/perf/util/pfm.c
>  create mode 100644 tools/perf/util/pfm.h
> 
> diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> index b3f3b3f1c161..ad2c41f595c2 100644
> --- a/tools/perf/Documentation/perf-record.txt
> +++ b/tools/perf/Documentation/perf-record.txt
> @@ -596,6 +596,17 @@ Make a copy of /proc/kcore and place it into a directory with the perf data file
>  Limit the sample data max size, <size> is expected to be a number with
>  appended unit character - B/K/M/G
>  
> +ifdef::HAVE_LIBPFM[]
> +--pfm-events events::
> +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> +including support for event filters. For example '--pfm-events
> +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> +option using the comma separator. Hardware events and generic hardware
> +events cannot be mixed together. The latter must be used with the -e
> +option. The -e option and this one can be mixed and matched.  Events
> +can be grouped using the {} notation.
> +endif::HAVE_LIBPFM[]
> +
>  SEE ALSO
>  --------
>  linkperf:perf-stat[1], linkperf:perf-list[1], linkperf:perf-intel-pt[1]
> diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
> index 4d56586b2fb9..536952ad592c 100644
> --- a/tools/perf/Documentation/perf-stat.txt
> +++ b/tools/perf/Documentation/perf-stat.txt
> @@ -71,6 +71,16 @@ report::
>  --tid=<tid>::
>          stat events on existing thread id (comma separated list)
>  
> +ifdef::HAVE_LIBPFM[]
> +--pfm-events events::
> +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> +including support for event filters. For example '--pfm-events
> +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> +option using the comma separator. Hardware events and generic hardware
> +events cannot be mixed together. The latter must be used with the -e
> +option. The -e option and this one can be mixed and matched.  Events
> +can be grouped using the {} notation.
> +endif::HAVE_LIBPFM[]
>  
>  -a::
>  --all-cpus::
> diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt
> index 20227dabc208..ee2024691d46 100644
> --- a/tools/perf/Documentation/perf-top.txt
> +++ b/tools/perf/Documentation/perf-top.txt
> @@ -329,6 +329,17 @@ Default is to monitor all CPUS.
>  	The known limitations include exception handing such as
>  	setjmp/longjmp will have calls/returns not match.
>  
> +ifdef::HAVE_LIBPFM[]
> +--pfm-events events::
> +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> +including support for event filters. For example '--pfm-events
> +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> +option using the comma separator. Hardware events and generic hardware
> +events cannot be mixed together. The latter must be used with the -e
> +option. The -e option and this one can be mixed and matched.  Events
> +can be grouped using the {} notation.
> +endif::HAVE_LIBPFM[]
> +
>  INTERACTIVE PROMPTING KEYS
>  --------------------------
>  
> diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> index 12a8204d63c6..b67804fee1e3 100644
> --- a/tools/perf/Makefile.config
> +++ b/tools/perf/Makefile.config
> @@ -1012,6 +1012,19 @@ ifdef LIBCLANGLLVM
>    endif
>  endif
>  
> +ifdef LIBPFM4
> +  $(call feature_check,libpfm4)
> +  ifeq ($(feature-libpfm4), 1)
> +    CFLAGS += -DHAVE_LIBPFM
> +    EXTLIBS += -lpfm
> +    ASCIIDOC_EXTRA = -aHAVE_LIBPFM=1
> +    $(call detected,CONFIG_LIBPFM4)
> +  else
> +    msg := $(warning libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev);
> +    NO_LIBPFM4 := 1
> +  endif
> +endif
> +
>  # Among the variables below, these:
>  #   perfexecdir
>  #   perf_include_dir
> diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
> index 94a495594e99..dc82578c8773 100644
> --- a/tools/perf/Makefile.perf
> +++ b/tools/perf/Makefile.perf
> @@ -118,6 +118,8 @@ include ../scripts/utilities.mak
>  #
>  # Define LIBBPF_DYNAMIC to enable libbpf dynamic linking.
>  #
> +# Define LIBPFM4 to enable libpfm4 events extension.
> +#
>  
>  # As per kernel Makefile, avoid funny character set dependencies
>  unexport LC_ALL
> diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
> index 965ef017496f..d2069bdcb469 100644
> --- a/tools/perf/builtin-list.c
> +++ b/tools/perf/builtin-list.c
> @@ -14,6 +14,7 @@
>  #include "util/pmu.h"
>  #include "util/debug.h"
>  #include "util/metricgroup.h"
> +#include "util/pfm.h"
>  #include <subcmd/pager.h>
>  #include <subcmd/parse-options.h>
>  #include <stdio.h>
> @@ -53,6 +54,8 @@ int cmd_list(int argc, const char **argv)
>  
>  	setup_pager();
>  
> +	libpfm_initialize();
> +
>  	if (!raw_dump && pager_in_use())
>  		printf("\nList of pre-defined events (to be used in -e):\n\n");
>  
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index 1ab349abe904..fc55b641849f 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -43,6 +43,7 @@
>  #include "util/time-utils.h"
>  #include "util/units.h"
>  #include "util/bpf-event.h"
> +#include "util/pfm.h"
>  #include "asm/bug.h"
>  #include "perf.h"
>  
> @@ -2421,6 +2422,11 @@ static struct option __record_options[] = {
>  #endif
>  	OPT_CALLBACK(0, "max-size", &record.output_max_size,
>  		     "size", "Limit the maximum size of the output file", parse_output_max_size),
> +#ifdef HAVE_LIBPFM
> +	OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
> +		"libpfm4 event selector. use 'perf list' to list available events",
> +		parse_libpfm_events_option),
> +#endif
>  	OPT_END()
>  };
>  
> @@ -2461,6 +2467,8 @@ int cmd_record(int argc, const char **argv)
>  	if (rec->evlist == NULL)
>  		return -ENOMEM;
>  
> +	libpfm_initialize();
> +
>  	err = perf_config(perf_record_config, rec);
>  	if (err)
>  		return err;
> diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> index 9207b6c45475..607920321c15 100644
> --- a/tools/perf/builtin-stat.c
> +++ b/tools/perf/builtin-stat.c
> @@ -66,6 +66,7 @@
>  #include "util/time-utils.h"
>  #include "util/top.h"
>  #include "util/affinity.h"
> +#include "util/pfm.h"
>  #include "asm/bug.h"
>  
>  #include <linux/time64.h>
> @@ -936,6 +937,11 @@ static struct option stat_options[] = {
>  		    "Use with 'percore' event qualifier to show the event "
>  		    "counts of one hardware thread by sum up total hardware "
>  		    "threads of same physical core"),
> +#ifdef HAVE_LIBPFM
> +	OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
> +		"libpfm4 event selector. use 'perf list' to list available events",
> +		parse_libpfm_events_option),
> +#endif
>  	OPT_END()
>  };
>  
> @@ -1874,6 +1880,8 @@ int cmd_stat(int argc, const char **argv)
>  	unsigned int interval, timeout;
>  	const char * const stat_subcommands[] = { "record", "report" };
>  
> +	libpfm_initialize();
> +
>  	setlocale(LC_ALL, "");
>  
>  	evsel_list = evlist__new();
> diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
> index 6b067a5ba1d5..e1f477a18f93 100644
> --- a/tools/perf/builtin-top.c
> +++ b/tools/perf/builtin-top.c
> @@ -53,6 +53,7 @@
>  
>  #include "util/debug.h"
>  #include "util/ordered-events.h"
> +#include "util/pfm.h"
>  
>  #include <assert.h>
>  #include <elf.h>
> @@ -1577,6 +1578,11 @@ int cmd_top(int argc, const char **argv)
>  		    "WARNING: should be used on grouped events."),
>  	OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
>  		    "Enable LBR callgraph stitching approach"),
> +#ifdef HAVE_LIBPFM
> +	OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
> +		"libpfm4 event selector. use 'perf list' to list available events",
> +		parse_libpfm_events_option),
> +#endif
>  	OPTS_EVSWITCH(&top.evswitch),
>  	OPT_END()
>  	};
> @@ -1590,6 +1596,8 @@ int cmd_top(int argc, const char **argv)
>  	if (status < 0)
>  		return status;
>  
> +	libpfm_initialize();
> +
>  	top.annotation_opts.min_pcnt = 5;
>  	top.annotation_opts.context  = 4;
>  
> diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
> index b3d1bf13ca07..7d2ccec9fd75 100644
> --- a/tools/perf/tests/Build
> +++ b/tools/perf/tests/Build
> @@ -56,6 +56,7 @@ perf-y += mem2node.o
>  perf-y += maps.o
>  perf-y += time-utils-test.o
>  perf-y += genelf.o
> +perf-y += pfm.o
>  
>  $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
>  	$(call rule_mkdir)
> diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
> index b6322eb0f423..8b323151f22c 100644
> --- a/tools/perf/tests/builtin-test.c
> +++ b/tools/perf/tests/builtin-test.c
> @@ -313,6 +313,15 @@ static struct test generic_tests[] = {
>  		.desc = "maps__merge_in",
>  		.func = test__maps__merge_in,
>  	},
> +	{
> +		.desc = "Test libpfm4 support",
> +		.func = test__pfm,
> +		.subtest = {
> +			.skip_if_fail	= true,
> +			.get_nr		= test__pfm_subtest_get_nr,
> +			.get_desc	= test__pfm_subtest_get_desc,
> +		}
> +	},
>  	{
>  		.func = NULL,
>  	},
> diff --git a/tools/perf/tests/pfm.c b/tools/perf/tests/pfm.c
> new file mode 100644
> index 000000000000..1d594fd3d9c1
> --- /dev/null
> +++ b/tools/perf/tests/pfm.c
> @@ -0,0 +1,207 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Test support for libpfm4 event encodings.
> + *
> + * Copyright 2020 Google LLC.
> + */
> +#include "tests.h"
> +#include "util/debug.h"
> +#include "util/evlist.h"
> +#include "util/pfm.h"
> +
> +#include <linux/kernel.h>
> +
> +#ifdef HAVE_LIBPFM
> +static int test__pfm_events(void);
> +static int test__pfm_group(void);
> +#endif
> +
> +static const struct {
> +	int (*func)(void);
> +	const char *desc;
> +} pfm_testcase_table[] = {
> +#ifdef HAVE_LIBPFM
> +	{
> +		.func = test__pfm_events,
> +		.desc = "test of individual --pfm-events",
> +	},
> +	{
> +		.func = test__pfm_group,
> +		.desc = "test groups of --pfm-events",
> +	},
> +#endif
> +};
> +
> +#ifdef HAVE_LIBPFM
> +static int count_pfm_events(struct perf_evlist *evlist)
> +{
> +	struct perf_evsel *evsel;
> +	int count = 0;
> +
> +	perf_evlist__for_each_entry(evlist, evsel) {
> +		count++;
> +	}
> +	return count;
> +}
> +
> +static int test__pfm_events(void)
> +{
> +	struct evlist *evlist;
> +	struct option opt;
> +	size_t i;
> +	const struct {
> +		const char *events;
> +		int nr_events;
> +	} table[] = {
> +		{
> +			.events = "",
> +			.nr_events = 0,
> +		},
> +		{
> +			.events = "instructions",
> +			.nr_events = 1,
> +		},
> +		{
> +			.events = "instructions,cycles",
> +			.nr_events = 2,
> +		},
> +		{
> +			.events = "stereolab",
> +			.nr_events = 0,
> +		},
> +		{
> +			.events = "instructions,instructions",
> +			.nr_events = 2,
> +		},
> +		{
> +			.events = "stereolab,instructions",
> +			.nr_events = 0,
> +		},
> +		{
> +			.events = "instructions,stereolab",
> +			.nr_events = 1,
> +		},
> +	};
> +
> +	libpfm_initialize();
> +
> +	for (i = 0; i < ARRAY_SIZE(table); i++) {
> +		evlist = evlist__new();
> +		if (evlist == NULL)
> +			return -ENOMEM;
> +
> +		opt.value = evlist;
> +		parse_libpfm_events_option(&opt,
> +					table[i].events,
> +					0);
> +		TEST_ASSERT_EQUAL(table[i].events,
> +				count_pfm_events(&evlist->core),
> +				table[i].nr_events);
> +		TEST_ASSERT_EQUAL(table[i].events,
> +				evlist->nr_groups,
> +				0);
> +
> +		evlist__delete(evlist);
> +	}
> +	return 0;
> +}
> +
> +static int test__pfm_group(void)
> +{
> +	struct evlist *evlist;
> +	struct option opt;
> +	size_t i;
> +	const struct {
> +		const char *events;
> +		int nr_events;
> +		int nr_groups;
> +	} table[] = {
> +		{
> +			.events = "{},",
> +			.nr_events = 0,
> +			.nr_groups = 0,
> +		},
> +		{
> +			.events = "{instructions}",
> +			.nr_events = 1,
> +			.nr_groups = 1,
> +		},
> +		{
> +			.events = "{instructions},{}",
> +			.nr_events = 1,
> +			.nr_groups = 1,
> +		},
> +		{
> +			.events = "{},{instructions}",
> +			.nr_events = 0,
> +			.nr_groups = 0,
> +		},
> +		{
> +			.events = "{instructions},{instructions}",
> +			.nr_events = 2,
> +			.nr_groups = 2,
> +		},
> +		{
> +			.events = "{instructions,cycles},{instructions,cycles}",
> +			.nr_events = 4,
> +			.nr_groups = 2,
> +		},
> +		{
> +			.events = "{stereolab}",
> +			.nr_events = 0,
> +			.nr_groups = 0,
> +		},
> +		{
> +			.events =
> +			"{instructions,cycles},{instructions,stereolab}",
> +			.nr_events = 3,
> +			.nr_groups = 1,
> +		},
> +	};
> +
> +	libpfm_initialize();
> +
> +	for (i = 0; i < ARRAY_SIZE(table); i++) {
> +		evlist = evlist__new();
> +		if (evlist == NULL)
> +			return -ENOMEM;
> +
> +		opt.value = evlist;
> +		parse_libpfm_events_option(&opt,
> +					table[i].events,
> +					0);
> +		TEST_ASSERT_EQUAL(table[i].events,
> +				count_pfm_events(&evlist->core),
> +				table[i].nr_events);
> +		TEST_ASSERT_EQUAL(table[i].events,
> +				evlist->nr_groups,
> +				table[i].nr_groups);
> +
> +		evlist__delete(evlist);
> +	}
> +	return 0;
> +}
> +#endif
> +
> +const char *test__pfm_subtest_get_desc(int i)
> +{
> +	if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> +		return NULL;
> +	return pfm_testcase_table[i].desc;
> +}
> +
> +int test__pfm_subtest_get_nr(void)
> +{
> +	return (int)ARRAY_SIZE(pfm_testcase_table);
> +}
> +
> +int test__pfm(struct test *test __maybe_unused, int i __maybe_unused)
> +{
> +#ifdef HAVE_LIBPFM
> +	if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> +		return TEST_FAIL;
> +	return pfm_testcase_table[i].func();
> +#else
> +	return TEST_SKIP;
> +#endif
> +}
> diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
> index 61a1ab032080..47cbae753b42 100644
> --- a/tools/perf/tests/tests.h
> +++ b/tools/perf/tests/tests.h
> @@ -112,6 +112,9 @@ int test__mem2node(struct test *t, int subtest);
>  int test__maps__merge_in(struct test *t, int subtest);
>  int test__time_utils(struct test *t, int subtest);
>  int test__jit_write_elf(struct test *test, int subtest);
> +int test__pfm(struct test *test, int subtest);
> +const char *test__pfm_subtest_get_desc(int subtest);
> +int test__pfm_subtest_get_nr(void);
>  
>  bool test__bp_signal_is_supported(void);
>  bool test__bp_account_is_supported(void);
> diff --git a/tools/perf/util/Build b/tools/perf/util/Build
> index c0cf8dff694e..afd3d2221c83 100644
> --- a/tools/perf/util/Build
> +++ b/tools/perf/util/Build
> @@ -177,6 +177,8 @@ perf-$(CONFIG_LIBBPF) += bpf-event.o
>  
>  perf-$(CONFIG_CXX) += c++/
>  
> +perf-$(CONFIG_LIBPFM4) += pfm.o
> +
>  CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
>  CFLAGS_llvm-utils.o += -DPERF_INCLUDE_DIR="BUILD_STR($(perf_include_dir_SQ))"
>  
> diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> index 6a571d322bb2..a5c88d52c11c 100644
> --- a/tools/perf/util/evsel.c
> +++ b/tools/perf/util/evsel.c
> @@ -2433,7 +2433,7 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
>  
>  		/* Is there already the separator in the name. */
>  		if (strchr(name, '/') ||
> -		    strchr(name, ':'))
> +		    (strchr(name, ':') && !evsel->is_libpfm_event))
>  			sep = "";
>  
>  		if (asprintf(&new_name, "%s%su", name, sep) < 0)
> diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> index a463bc65b001..709a9d494767 100644
> --- a/tools/perf/util/evsel.h
> +++ b/tools/perf/util/evsel.h
> @@ -76,6 +76,7 @@ struct evsel {
>  	bool			ignore_missing_thread;
>  	bool			forced_leader;
>  	bool			use_uncore_alias;
> +	bool			is_libpfm_event;
>  	/* parse modifier helper */
>  	int			exclude_GH;
>  	int			sample_read;
> diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> index 10107747b361..e7e0faf36e3c 100644
> --- a/tools/perf/util/parse-events.c
> +++ b/tools/perf/util/parse-events.c
> @@ -36,6 +36,7 @@
>  #include "metricgroup.h"
>  #include "util/evsel_config.h"
>  #include "util/event.h"
> +#include "util/pfm.h"
>  
>  #define MAX_NAME_LEN 100
>  
> @@ -344,6 +345,7 @@ static char *get_config_name(struct list_head *head_terms)
>  static struct evsel *
>  __add_event(struct list_head *list, int *idx,
>  	    struct perf_event_attr *attr,
> +	    bool init_attr,
>  	    char *name, struct perf_pmu *pmu,
>  	    struct list_head *config_terms, bool auto_merge_stats,
>  	    const char *cpu_list)
> @@ -352,7 +354,8 @@ __add_event(struct list_head *list, int *idx,
>  	struct perf_cpu_map *cpus = pmu ? pmu->cpus :
>  			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
>  
> -	event_attr_init(attr);
> +	if (init_attr)
> +		event_attr_init(attr);
>  
>  	evsel = perf_evsel__new_idx(attr, *idx);
>  	if (!evsel)
> @@ -370,15 +373,25 @@ __add_event(struct list_head *list, int *idx,
>  	if (config_terms)
>  		list_splice(config_terms, &evsel->config_terms);
>  
> -	list_add_tail(&evsel->core.node, list);
> +	if (list)
> +		list_add_tail(&evsel->core.node, list);
> +
>  	return evsel;
>  }
>  
> +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> +					char *name, struct perf_pmu *pmu)
> +{
> +	return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
> +			   NULL);
> +}
> +
>  static int add_event(struct list_head *list, int *idx,
>  		     struct perf_event_attr *attr, char *name,
>  		     struct list_head *config_terms)
>  {
> -	return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
> +	return __add_event(list, idx, attr, true, name, NULL, config_terms,
> +			   false, NULL) ? 0 : -ENOMEM;
>  }
>  
>  static int add_event_tool(struct list_head *list, int *idx,
> @@ -390,7 +403,8 @@ static int add_event_tool(struct list_head *list, int *idx,
>  		.config = PERF_COUNT_SW_DUMMY,
>  	};
>  
> -	evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
> +	evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
> +			    "0");
>  	if (!evsel)
>  		return -ENOMEM;
>  	evsel->tool_event = tool_event;
> @@ -1446,8 +1460,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
>  
>  	if (!head_config) {
>  		attr.type = pmu->type;
> -		evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
> -				    auto_merge_stats, NULL);
> +		evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
> +				    pmu, NULL, auto_merge_stats, NULL);
>  		if (evsel) {
>  			evsel->pmu_name = name ? strdup(name) : NULL;
>  			evsel->use_uncore_alias = use_uncore_alias;
> @@ -1487,7 +1501,7 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
>  		return -EINVAL;
>  	}
>  
> -	evsel = __add_event(list, &parse_state->idx, &attr,
> +	evsel = __add_event(list, &parse_state->idx, &attr, true,
>  			    get_config_name(head_config), pmu,
>  			    &config_terms, auto_merge_stats, NULL);
>  	if (evsel) {
> @@ -2794,6 +2808,8 @@ void print_events(const char *event_glob, bool name_only, bool quiet_flag,
>  	print_sdt_events(NULL, NULL, name_only);
>  
>  	metricgroup__print(true, true, NULL, name_only, details_flag);
> +
> +	print_libpfm_events(name_only, long_desc);
>  }
>  
>  int parse_events__is_hardcoded_term(struct parse_events_term *term)
> diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
> index 27596cbd0ba0..f7f555b1ca35 100644
> --- a/tools/perf/util/parse-events.h
> +++ b/tools/perf/util/parse-events.h
> @@ -17,6 +17,7 @@ struct evlist;
>  struct parse_events_error;
>  
>  struct option;
> +struct perf_pmu;
>  
>  struct tracepoint_path {
>  	char *system;
> @@ -186,6 +187,9 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
>  			 bool auto_merge_stats,
>  			 bool use_alias);
>  
> +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> +					char *name, struct perf_pmu *pmu);
> +
>  int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
>  			       char *str,
>  			       struct list_head **listp);
> diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c
> new file mode 100644
> index 000000000000..4b11adf832a0
> --- /dev/null
> +++ b/tools/perf/util/pfm.c
> @@ -0,0 +1,277 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Support for libpfm4 event encoding.
> + *
> + * Copyright 2020 Google LLC.
> + */
> +#include "util/cpumap.h"
> +#include "util/debug.h"
> +#include "util/event.h"
> +#include "util/evlist.h"
> +#include "util/evsel.h"
> +#include "util/parse-events.h"
> +#include "util/pmu.h"
> +#include "util/pfm.h"
> +
> +#include <string.h>
> +#include <linux/kernel.h>
> +#include <perfmon/pfmlib_perf_event.h>
> +
> +void libpfm_initialize(void)
> +{
> +	int ret;
> +
> +	ret = pfm_initialize();
> +	if (ret != PFM_SUCCESS) {
> +		ui__warning("libpfm failed to initialize: %s\n",
> +			pfm_strerror(ret));
> +	}
> +}
> +
> +int parse_libpfm_events_option(const struct option *opt, const char *str,
> +			int unset __maybe_unused)
> +{
> +	struct evlist *evlist = *(struct evlist **)opt->value;
> +	struct perf_event_attr attr;
> +	struct perf_pmu *pmu;
> +	struct evsel *evsel, *grp_leader = NULL;
> +	char *p, *q, *p_orig;
> +	const char *sep;
> +	int grp_evt = -1;
> +	int ret;
> +
> +	p_orig = p = strdup(str);
> +	if (!p)
> +		return -1;
> +	/*
> +	 * force loading of the PMU list
> +	 */
> +	perf_pmu__scan(NULL);
> +
> +	for (q = p; strsep(&p, ",{}"); q = p) {
> +		sep = p ? str + (p - p_orig - 1) : "";
> +		if (*sep == '{') {
> +			if (grp_evt > -1) {
> +				ui__error(
> +					"nested event groups not supported\n");
> +				goto error;
> +			}
> +			grp_evt++;
> +		}
> +
> +		/* no event */
> +		if (*q == '\0')
> +			continue;
> +
> +		memset(&attr, 0, sizeof(attr));
> +		event_attr_init(&attr);
> +
> +		ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
> +						&attr, NULL, NULL);
> +
> +		if (ret != PFM_SUCCESS) {
> +			ui__error("failed to parse event %s : %s\n", str,
> +				  pfm_strerror(ret));
> +			goto error;
> +		}
> +
> +		pmu = perf_pmu__find_by_type((unsigned int)attr.type);
> +		evsel = parse_events__add_event(evlist->core.nr_entries,
> +						&attr, q, pmu);
> +		if (evsel == NULL)
> +			goto error;
> +
> +		evsel->is_libpfm_event = true;
> +
> +		evlist__add(evlist, evsel);
> +
> +		if (grp_evt == 0)
> +			grp_leader = evsel;
> +
> +		if (grp_evt > -1) {
> +			evsel->leader = grp_leader;
> +			grp_leader->core.nr_members++;
> +			grp_evt++;
> +		}
> +
> +		if (*sep == '}') {
> +			if (grp_evt < 0) {
> +				ui__error(
> +				   "cannot close a non-existing event group\n");
> +				goto error;
> +			}
> +			evlist->nr_groups++;
> +			grp_leader = NULL;
> +			grp_evt = -1;
> +		}
> +	}
> +	return 0;
> +error:
> +	free(p_orig);
> +	return -1;
> +}
> +
> +static const char *srcs[PFM_ATTR_CTRL_MAX] = {
> +	[PFM_ATTR_CTRL_UNKNOWN] = "???",
> +	[PFM_ATTR_CTRL_PMU] = "PMU",
> +	[PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
> +};
> +
> +static void
> +print_attr_flags(pfm_event_attr_info_t *info)
> +{
> +	int n = 0;
> +
> +	if (info->is_dfl) {
> +		printf("[default] ");
> +		n++;
> +	}
> +
> +	if (info->is_precise) {
> +		printf("[precise] ");
> +		n++;
> +	}
> +
> +	if (!n)
> +		printf("- ");
> +}
> +
> +static void
> +print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc)
> +{
> +	pfm_event_attr_info_t ainfo;
> +	const char *src;
> +	int j, ret;
> +
> +	ainfo.size = sizeof(ainfo);
> +
> +	printf("  %s\n", info->name);
> +	printf("    [%s]\n", info->desc);
> +	if (long_desc) {
> +		if (info->equiv)
> +			printf("      Equiv: %s\n", info->equiv);
> +
> +		printf("      Code  : 0x%"PRIx64"\n", info->code);
> +	}
> +	pfm_for_each_event_attr(j, info) {
> +		ret = pfm_get_event_attr_info(info->idx, j,
> +					      PFM_OS_PERF_EVENT_EXT, &ainfo);
> +		if (ret != PFM_SUCCESS)
> +			continue;
> +
> +		if (ainfo.type == PFM_ATTR_UMASK) {
> +			printf("      %s:%s\n", info->name, ainfo.name);
> +			printf("        [%s]\n", ainfo.desc);
> +		}
> +
> +		if (!long_desc)
> +			continue;
> +
> +		if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
> +			ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
> +
> +		src = srcs[ainfo.ctrl];
> +		switch (ainfo.type) {
> +		case PFM_ATTR_UMASK:
> +			printf("        Umask : 0x%02"PRIx64" : %s: ",
> +				ainfo.code, src);
> +			print_attr_flags(&ainfo);
> +			putchar('\n');
> +			break;
> +		case PFM_ATTR_MOD_BOOL:
> +			printf("      Modif : %s: [%s] : %s (boolean)\n", src,
> +				ainfo.name, ainfo.desc);
> +			break;
> +		case PFM_ATTR_MOD_INTEGER:
> +			printf("      Modif : %s: [%s] : %s (integer)\n", src,
> +				ainfo.name, ainfo.desc);
> +			break;
> +		case PFM_ATTR_NONE:
> +		case PFM_ATTR_RAW_UMASK:
> +		case PFM_ATTR_MAX:
> +		default:
> +			printf("      Attr  : %s: [%s] : %s\n", src,
> +				ainfo.name, ainfo.desc);
> +		}
> +	}
> +}
> +
> +/*
> + * list all pmu::event:umask, pmu::event
> + * printed events may not be all valid combinations of umask for an event
> + */
> +static void
> +print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info)
> +{
> +	pfm_event_attr_info_t ainfo;
> +	int j, ret;
> +	bool has_umask = false;
> +
> +	ainfo.size = sizeof(ainfo);
> +
> +	pfm_for_each_event_attr(j, info) {
> +		ret = pfm_get_event_attr_info(info->idx, j,
> +					      PFM_OS_PERF_EVENT_EXT, &ainfo);
> +		if (ret != PFM_SUCCESS)
> +			continue;
> +
> +		if (ainfo.type != PFM_ATTR_UMASK)
> +			continue;
> +
> +		printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name);
> +		has_umask = true;
> +	}
> +	if (!has_umask)
> +		printf("%s::%s\n", pinfo->name, info->name);
> +}
> +
> +void print_libpfm_events(bool name_only, bool long_desc)
> +{
> +	pfm_event_info_t info;
> +	pfm_pmu_info_t pinfo;
> +	int i, p, ret;
> +
> +	/* initialize to zero to indicate ABI version */
> +	info.size  = sizeof(info);
> +	pinfo.size = sizeof(pinfo);
> +
> +	if (!name_only)
> +		puts("\nList of pre-defined events (to be used in --pfm-events):\n");
> +
> +	pfm_for_all_pmus(p) {
> +		bool printed_pmu = false;
> +
> +		ret = pfm_get_pmu_info(p, &pinfo);
> +		if (ret != PFM_SUCCESS)
> +			continue;
> +
> +		/* only print events that are supported by host HW */
> +		if (!pinfo.is_present)
> +			continue;
> +
> +		/* handled by perf directly */
> +		if (pinfo.pmu == PFM_PMU_PERF_EVENT)
> +			continue;
> +
> +		for (i = pinfo.first_event; i != -1;
> +		     i = pfm_get_event_next(i)) {
> +
> +			ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
> +						&info);
> +			if (ret != PFM_SUCCESS)
> +				continue;
> +
> +			if (!name_only && !printed_pmu) {
> +				printf("%s:\n", pinfo.name);
> +				printed_pmu = true;
> +			}
> +
> +			if (!name_only)
> +				print_libpfm_events_detailed(&info, long_desc);
> +			else
> +				print_libpfm_events_raw(&pinfo, &info);
> +		}
> +		if (!name_only && printed_pmu)
> +			putchar('\n');
> +	}
> +}
> diff --git a/tools/perf/util/pfm.h b/tools/perf/util/pfm.h
> new file mode 100644
> index 000000000000..532dafe88bc0
> --- /dev/null
> +++ b/tools/perf/util/pfm.h
> @@ -0,0 +1,43 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Support for libpfm4 event encoding.
> + *
> + * Copyright 2020 Google LLC.
> + */
> +#ifndef __PERF_PFM_H
> +#define __PERF_PFM_H
> +
> +#include <subcmd/parse-options.h>
> +
> +#ifdef HAVE_LIBPFM
> +void libpfm_initialize(void);
> +
> +int parse_libpfm_events_option(const struct option *opt, const char *str,
> +			int unset);
> +
> +void print_libpfm_events(bool name_only, bool long_desc);
> +
> +#else
> +#include <linux/compiler.h>
> +
> +static inline void libpfm_initialize(void)
> +{
> +}
> +
> +static inline int parse_libpfm_events_option(
> +	const struct option *opt __maybe_unused,
> +	const char *str __maybe_unused,
> +	int unset __maybe_unused)
> +{
> +	return 0;
> +}
> +
> +static inline void print_libpfm_events(bool name_only __maybe_unused,
> +				       bool long_desc __maybe_unused)
> +{
> +}
> +
> +#endif
> +
> +
> +#endif /* __PERF_PFM_H */
> -- 
> 2.26.2.303.gf8c07b1a785-goog
> 

-- 

- Arnaldo

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

* Re: [PATCH v12 4/4] perf tools: add support for libpfm4
  2020-04-30 13:34     ` Arnaldo Carvalho de Melo
@ 2020-04-30 14:26       ` Ian Rogers
  -1 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2020-04-30 14:26 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Yonghong Song, Andrii Nakryiko,
	Greg Kroah-Hartman, Thomas Gleixner, Igor Lubashev,
	Alexey Budankov, Florian Fainelli, Adrian Hunter, Andi Kleen,
	Jiwei Sun, yuzhoujian, Kan Liang, Jin Yao, Leo Yan, John Garry,
	LKML, netdev, bpf, linux-perf-users, Stephane Eranian

On Thu, Apr 30, 2020 at 6:34 AM Arnaldo Carvalho de Melo
<arnaldo.melo@gmail.com> wrote:
>
> Em Wed, Apr 29, 2020 at 04:14:43PM -0700, Ian Rogers escreveu:
> > From: Stephane Eranian <eranian@google.com>
> >
> > This patch links perf with the libpfm4 library if it is available
> > and LIBPFM4 is passed to the build. The libpfm4 library
> > contains hardware event tables for all processors supported by
> > perf_events. It is a helper library that helps convert from a
> > symbolic event name to the event encoding required by the
> > underlying kernel interface. This library is open-source and
> > available from: http://perfmon2.sf.net.
>
> Hey, I think we should not call libpfm_initialize() in all tools, better
> leave it to when the --pfm-event callback gets called, because the user
> is really using that event syntax, i.e. do it lazily, to reduce the
> amount of initialization that perf does, which is already excessive and
> needs triming down, to do more things only when they are really, really
> needed.
>
> I have applied the previous, preparatory patches.

Thanks! I didn't see the whole set rebasing on:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/log/?h=perf/core
and so sent it all again:
https://lore.kernel.org/lkml/20200430142419.252180-5-irogers@google.com/T/#u

Thanks,
Ian

> - Arnaldo
>
> > With this patch, it is possible to specify full hardware events
> > by name. Hardware filters are also supported. Events must be
> > specified via the --pfm-events and not -e option. Both options
> > are active at the same time and it is possible to mix and match:
> >
> > $ perf stat --pfm-events inst_retired:any_p:c=1:i -e cycles ....
> >
> > Signed-off-by: Stephane Eranian <eranian@google.com>
> > Reviewed-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/perf/Documentation/perf-record.txt |  11 +
> >  tools/perf/Documentation/perf-stat.txt   |  10 +
> >  tools/perf/Documentation/perf-top.txt    |  11 +
> >  tools/perf/Makefile.config               |  13 ++
> >  tools/perf/Makefile.perf                 |   2 +
> >  tools/perf/builtin-list.c                |   3 +
> >  tools/perf/builtin-record.c              |   8 +
> >  tools/perf/builtin-stat.c                |   8 +
> >  tools/perf/builtin-top.c                 |   8 +
> >  tools/perf/tests/Build                   |   1 +
> >  tools/perf/tests/builtin-test.c          |   9 +
> >  tools/perf/tests/pfm.c                   | 207 +++++++++++++++++
> >  tools/perf/tests/tests.h                 |   3 +
> >  tools/perf/util/Build                    |   2 +
> >  tools/perf/util/evsel.c                  |   2 +-
> >  tools/perf/util/evsel.h                  |   1 +
> >  tools/perf/util/parse-events.c           |  30 ++-
> >  tools/perf/util/parse-events.h           |   4 +
> >  tools/perf/util/pfm.c                    | 277 +++++++++++++++++++++++
> >  tools/perf/util/pfm.h                    |  43 ++++
> >  20 files changed, 645 insertions(+), 8 deletions(-)
> >  create mode 100644 tools/perf/tests/pfm.c
> >  create mode 100644 tools/perf/util/pfm.c
> >  create mode 100644 tools/perf/util/pfm.h
> >
> > diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> > index b3f3b3f1c161..ad2c41f595c2 100644
> > --- a/tools/perf/Documentation/perf-record.txt
> > +++ b/tools/perf/Documentation/perf-record.txt
> > @@ -596,6 +596,17 @@ Make a copy of /proc/kcore and place it into a directory with the perf data file
> >  Limit the sample data max size, <size> is expected to be a number with
> >  appended unit character - B/K/M/G
> >
> > +ifdef::HAVE_LIBPFM[]
> > +--pfm-events events::
> > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > +including support for event filters. For example '--pfm-events
> > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > +option using the comma separator. Hardware events and generic hardware
> > +events cannot be mixed together. The latter must be used with the -e
> > +option. The -e option and this one can be mixed and matched.  Events
> > +can be grouped using the {} notation.
> > +endif::HAVE_LIBPFM[]
> > +
> >  SEE ALSO
> >  --------
> >  linkperf:perf-stat[1], linkperf:perf-list[1], linkperf:perf-intel-pt[1]
> > diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
> > index 4d56586b2fb9..536952ad592c 100644
> > --- a/tools/perf/Documentation/perf-stat.txt
> > +++ b/tools/perf/Documentation/perf-stat.txt
> > @@ -71,6 +71,16 @@ report::
> >  --tid=<tid>::
> >          stat events on existing thread id (comma separated list)
> >
> > +ifdef::HAVE_LIBPFM[]
> > +--pfm-events events::
> > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > +including support for event filters. For example '--pfm-events
> > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > +option using the comma separator. Hardware events and generic hardware
> > +events cannot be mixed together. The latter must be used with the -e
> > +option. The -e option and this one can be mixed and matched.  Events
> > +can be grouped using the {} notation.
> > +endif::HAVE_LIBPFM[]
> >
> >  -a::
> >  --all-cpus::
> > diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt
> > index 20227dabc208..ee2024691d46 100644
> > --- a/tools/perf/Documentation/perf-top.txt
> > +++ b/tools/perf/Documentation/perf-top.txt
> > @@ -329,6 +329,17 @@ Default is to monitor all CPUS.
> >       The known limitations include exception handing such as
> >       setjmp/longjmp will have calls/returns not match.
> >
> > +ifdef::HAVE_LIBPFM[]
> > +--pfm-events events::
> > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > +including support for event filters. For example '--pfm-events
> > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > +option using the comma separator. Hardware events and generic hardware
> > +events cannot be mixed together. The latter must be used with the -e
> > +option. The -e option and this one can be mixed and matched.  Events
> > +can be grouped using the {} notation.
> > +endif::HAVE_LIBPFM[]
> > +
> >  INTERACTIVE PROMPTING KEYS
> >  --------------------------
> >
> > diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> > index 12a8204d63c6..b67804fee1e3 100644
> > --- a/tools/perf/Makefile.config
> > +++ b/tools/perf/Makefile.config
> > @@ -1012,6 +1012,19 @@ ifdef LIBCLANGLLVM
> >    endif
> >  endif
> >
> > +ifdef LIBPFM4
> > +  $(call feature_check,libpfm4)
> > +  ifeq ($(feature-libpfm4), 1)
> > +    CFLAGS += -DHAVE_LIBPFM
> > +    EXTLIBS += -lpfm
> > +    ASCIIDOC_EXTRA = -aHAVE_LIBPFM=1
> > +    $(call detected,CONFIG_LIBPFM4)
> > +  else
> > +    msg := $(warning libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev);
> > +    NO_LIBPFM4 := 1
> > +  endif
> > +endif
> > +
> >  # Among the variables below, these:
> >  #   perfexecdir
> >  #   perf_include_dir
> > diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
> > index 94a495594e99..dc82578c8773 100644
> > --- a/tools/perf/Makefile.perf
> > +++ b/tools/perf/Makefile.perf
> > @@ -118,6 +118,8 @@ include ../scripts/utilities.mak
> >  #
> >  # Define LIBBPF_DYNAMIC to enable libbpf dynamic linking.
> >  #
> > +# Define LIBPFM4 to enable libpfm4 events extension.
> > +#
> >
> >  # As per kernel Makefile, avoid funny character set dependencies
> >  unexport LC_ALL
> > diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
> > index 965ef017496f..d2069bdcb469 100644
> > --- a/tools/perf/builtin-list.c
> > +++ b/tools/perf/builtin-list.c
> > @@ -14,6 +14,7 @@
> >  #include "util/pmu.h"
> >  #include "util/debug.h"
> >  #include "util/metricgroup.h"
> > +#include "util/pfm.h"
> >  #include <subcmd/pager.h>
> >  #include <subcmd/parse-options.h>
> >  #include <stdio.h>
> > @@ -53,6 +54,8 @@ int cmd_list(int argc, const char **argv)
> >
> >       setup_pager();
> >
> > +     libpfm_initialize();
> > +
> >       if (!raw_dump && pager_in_use())
> >               printf("\nList of pre-defined events (to be used in -e):\n\n");
> >
> > diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> > index 1ab349abe904..fc55b641849f 100644
> > --- a/tools/perf/builtin-record.c
> > +++ b/tools/perf/builtin-record.c
> > @@ -43,6 +43,7 @@
> >  #include "util/time-utils.h"
> >  #include "util/units.h"
> >  #include "util/bpf-event.h"
> > +#include "util/pfm.h"
> >  #include "asm/bug.h"
> >  #include "perf.h"
> >
> > @@ -2421,6 +2422,11 @@ static struct option __record_options[] = {
> >  #endif
> >       OPT_CALLBACK(0, "max-size", &record.output_max_size,
> >                    "size", "Limit the maximum size of the output file", parse_output_max_size),
> > +#ifdef HAVE_LIBPFM
> > +     OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
> > +             "libpfm4 event selector. use 'perf list' to list available events",
> > +             parse_libpfm_events_option),
> > +#endif
> >       OPT_END()
> >  };
> >
> > @@ -2461,6 +2467,8 @@ int cmd_record(int argc, const char **argv)
> >       if (rec->evlist == NULL)
> >               return -ENOMEM;
> >
> > +     libpfm_initialize();
> > +
> >       err = perf_config(perf_record_config, rec);
> >       if (err)
> >               return err;
> > diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> > index 9207b6c45475..607920321c15 100644
> > --- a/tools/perf/builtin-stat.c
> > +++ b/tools/perf/builtin-stat.c
> > @@ -66,6 +66,7 @@
> >  #include "util/time-utils.h"
> >  #include "util/top.h"
> >  #include "util/affinity.h"
> > +#include "util/pfm.h"
> >  #include "asm/bug.h"
> >
> >  #include <linux/time64.h>
> > @@ -936,6 +937,11 @@ static struct option stat_options[] = {
> >                   "Use with 'percore' event qualifier to show the event "
> >                   "counts of one hardware thread by sum up total hardware "
> >                   "threads of same physical core"),
> > +#ifdef HAVE_LIBPFM
> > +     OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
> > +             "libpfm4 event selector. use 'perf list' to list available events",
> > +             parse_libpfm_events_option),
> > +#endif
> >       OPT_END()
> >  };
> >
> > @@ -1874,6 +1880,8 @@ int cmd_stat(int argc, const char **argv)
> >       unsigned int interval, timeout;
> >       const char * const stat_subcommands[] = { "record", "report" };
> >
> > +     libpfm_initialize();
> > +
> >       setlocale(LC_ALL, "");
> >
> >       evsel_list = evlist__new();
> > diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
> > index 6b067a5ba1d5..e1f477a18f93 100644
> > --- a/tools/perf/builtin-top.c
> > +++ b/tools/perf/builtin-top.c
> > @@ -53,6 +53,7 @@
> >
> >  #include "util/debug.h"
> >  #include "util/ordered-events.h"
> > +#include "util/pfm.h"
> >
> >  #include <assert.h>
> >  #include <elf.h>
> > @@ -1577,6 +1578,11 @@ int cmd_top(int argc, const char **argv)
> >                   "WARNING: should be used on grouped events."),
> >       OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
> >                   "Enable LBR callgraph stitching approach"),
> > +#ifdef HAVE_LIBPFM
> > +     OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
> > +             "libpfm4 event selector. use 'perf list' to list available events",
> > +             parse_libpfm_events_option),
> > +#endif
> >       OPTS_EVSWITCH(&top.evswitch),
> >       OPT_END()
> >       };
> > @@ -1590,6 +1596,8 @@ int cmd_top(int argc, const char **argv)
> >       if (status < 0)
> >               return status;
> >
> > +     libpfm_initialize();
> > +
> >       top.annotation_opts.min_pcnt = 5;
> >       top.annotation_opts.context  = 4;
> >
> > diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
> > index b3d1bf13ca07..7d2ccec9fd75 100644
> > --- a/tools/perf/tests/Build
> > +++ b/tools/perf/tests/Build
> > @@ -56,6 +56,7 @@ perf-y += mem2node.o
> >  perf-y += maps.o
> >  perf-y += time-utils-test.o
> >  perf-y += genelf.o
> > +perf-y += pfm.o
> >
> >  $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
> >       $(call rule_mkdir)
> > diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
> > index b6322eb0f423..8b323151f22c 100644
> > --- a/tools/perf/tests/builtin-test.c
> > +++ b/tools/perf/tests/builtin-test.c
> > @@ -313,6 +313,15 @@ static struct test generic_tests[] = {
> >               .desc = "maps__merge_in",
> >               .func = test__maps__merge_in,
> >       },
> > +     {
> > +             .desc = "Test libpfm4 support",
> > +             .func = test__pfm,
> > +             .subtest = {
> > +                     .skip_if_fail   = true,
> > +                     .get_nr         = test__pfm_subtest_get_nr,
> > +                     .get_desc       = test__pfm_subtest_get_desc,
> > +             }
> > +     },
> >       {
> >               .func = NULL,
> >       },
> > diff --git a/tools/perf/tests/pfm.c b/tools/perf/tests/pfm.c
> > new file mode 100644
> > index 000000000000..1d594fd3d9c1
> > --- /dev/null
> > +++ b/tools/perf/tests/pfm.c
> > @@ -0,0 +1,207 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Test support for libpfm4 event encodings.
> > + *
> > + * Copyright 2020 Google LLC.
> > + */
> > +#include "tests.h"
> > +#include "util/debug.h"
> > +#include "util/evlist.h"
> > +#include "util/pfm.h"
> > +
> > +#include <linux/kernel.h>
> > +
> > +#ifdef HAVE_LIBPFM
> > +static int test__pfm_events(void);
> > +static int test__pfm_group(void);
> > +#endif
> > +
> > +static const struct {
> > +     int (*func)(void);
> > +     const char *desc;
> > +} pfm_testcase_table[] = {
> > +#ifdef HAVE_LIBPFM
> > +     {
> > +             .func = test__pfm_events,
> > +             .desc = "test of individual --pfm-events",
> > +     },
> > +     {
> > +             .func = test__pfm_group,
> > +             .desc = "test groups of --pfm-events",
> > +     },
> > +#endif
> > +};
> > +
> > +#ifdef HAVE_LIBPFM
> > +static int count_pfm_events(struct perf_evlist *evlist)
> > +{
> > +     struct perf_evsel *evsel;
> > +     int count = 0;
> > +
> > +     perf_evlist__for_each_entry(evlist, evsel) {
> > +             count++;
> > +     }
> > +     return count;
> > +}
> > +
> > +static int test__pfm_events(void)
> > +{
> > +     struct evlist *evlist;
> > +     struct option opt;
> > +     size_t i;
> > +     const struct {
> > +             const char *events;
> > +             int nr_events;
> > +     } table[] = {
> > +             {
> > +                     .events = "",
> > +                     .nr_events = 0,
> > +             },
> > +             {
> > +                     .events = "instructions",
> > +                     .nr_events = 1,
> > +             },
> > +             {
> > +                     .events = "instructions,cycles",
> > +                     .nr_events = 2,
> > +             },
> > +             {
> > +                     .events = "stereolab",
> > +                     .nr_events = 0,
> > +             },
> > +             {
> > +                     .events = "instructions,instructions",
> > +                     .nr_events = 2,
> > +             },
> > +             {
> > +                     .events = "stereolab,instructions",
> > +                     .nr_events = 0,
> > +             },
> > +             {
> > +                     .events = "instructions,stereolab",
> > +                     .nr_events = 1,
> > +             },
> > +     };
> > +
> > +     libpfm_initialize();
> > +
> > +     for (i = 0; i < ARRAY_SIZE(table); i++) {
> > +             evlist = evlist__new();
> > +             if (evlist == NULL)
> > +                     return -ENOMEM;
> > +
> > +             opt.value = evlist;
> > +             parse_libpfm_events_option(&opt,
> > +                                     table[i].events,
> > +                                     0);
> > +             TEST_ASSERT_EQUAL(table[i].events,
> > +                             count_pfm_events(&evlist->core),
> > +                             table[i].nr_events);
> > +             TEST_ASSERT_EQUAL(table[i].events,
> > +                             evlist->nr_groups,
> > +                             0);
> > +
> > +             evlist__delete(evlist);
> > +     }
> > +     return 0;
> > +}
> > +
> > +static int test__pfm_group(void)
> > +{
> > +     struct evlist *evlist;
> > +     struct option opt;
> > +     size_t i;
> > +     const struct {
> > +             const char *events;
> > +             int nr_events;
> > +             int nr_groups;
> > +     } table[] = {
> > +             {
> > +                     .events = "{},",
> > +                     .nr_events = 0,
> > +                     .nr_groups = 0,
> > +             },
> > +             {
> > +                     .events = "{instructions}",
> > +                     .nr_events = 1,
> > +                     .nr_groups = 1,
> > +             },
> > +             {
> > +                     .events = "{instructions},{}",
> > +                     .nr_events = 1,
> > +                     .nr_groups = 1,
> > +             },
> > +             {
> > +                     .events = "{},{instructions}",
> > +                     .nr_events = 0,
> > +                     .nr_groups = 0,
> > +             },
> > +             {
> > +                     .events = "{instructions},{instructions}",
> > +                     .nr_events = 2,
> > +                     .nr_groups = 2,
> > +             },
> > +             {
> > +                     .events = "{instructions,cycles},{instructions,cycles}",
> > +                     .nr_events = 4,
> > +                     .nr_groups = 2,
> > +             },
> > +             {
> > +                     .events = "{stereolab}",
> > +                     .nr_events = 0,
> > +                     .nr_groups = 0,
> > +             },
> > +             {
> > +                     .events =
> > +                     "{instructions,cycles},{instructions,stereolab}",
> > +                     .nr_events = 3,
> > +                     .nr_groups = 1,
> > +             },
> > +     };
> > +
> > +     libpfm_initialize();
> > +
> > +     for (i = 0; i < ARRAY_SIZE(table); i++) {
> > +             evlist = evlist__new();
> > +             if (evlist == NULL)
> > +                     return -ENOMEM;
> > +
> > +             opt.value = evlist;
> > +             parse_libpfm_events_option(&opt,
> > +                                     table[i].events,
> > +                                     0);
> > +             TEST_ASSERT_EQUAL(table[i].events,
> > +                             count_pfm_events(&evlist->core),
> > +                             table[i].nr_events);
> > +             TEST_ASSERT_EQUAL(table[i].events,
> > +                             evlist->nr_groups,
> > +                             table[i].nr_groups);
> > +
> > +             evlist__delete(evlist);
> > +     }
> > +     return 0;
> > +}
> > +#endif
> > +
> > +const char *test__pfm_subtest_get_desc(int i)
> > +{
> > +     if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> > +             return NULL;
> > +     return pfm_testcase_table[i].desc;
> > +}
> > +
> > +int test__pfm_subtest_get_nr(void)
> > +{
> > +     return (int)ARRAY_SIZE(pfm_testcase_table);
> > +}
> > +
> > +int test__pfm(struct test *test __maybe_unused, int i __maybe_unused)
> > +{
> > +#ifdef HAVE_LIBPFM
> > +     if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> > +             return TEST_FAIL;
> > +     return pfm_testcase_table[i].func();
> > +#else
> > +     return TEST_SKIP;
> > +#endif
> > +}
> > diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
> > index 61a1ab032080..47cbae753b42 100644
> > --- a/tools/perf/tests/tests.h
> > +++ b/tools/perf/tests/tests.h
> > @@ -112,6 +112,9 @@ int test__mem2node(struct test *t, int subtest);
> >  int test__maps__merge_in(struct test *t, int subtest);
> >  int test__time_utils(struct test *t, int subtest);
> >  int test__jit_write_elf(struct test *test, int subtest);
> > +int test__pfm(struct test *test, int subtest);
> > +const char *test__pfm_subtest_get_desc(int subtest);
> > +int test__pfm_subtest_get_nr(void);
> >
> >  bool test__bp_signal_is_supported(void);
> >  bool test__bp_account_is_supported(void);
> > diff --git a/tools/perf/util/Build b/tools/perf/util/Build
> > index c0cf8dff694e..afd3d2221c83 100644
> > --- a/tools/perf/util/Build
> > +++ b/tools/perf/util/Build
> > @@ -177,6 +177,8 @@ perf-$(CONFIG_LIBBPF) += bpf-event.o
> >
> >  perf-$(CONFIG_CXX) += c++/
> >
> > +perf-$(CONFIG_LIBPFM4) += pfm.o
> > +
> >  CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
> >  CFLAGS_llvm-utils.o += -DPERF_INCLUDE_DIR="BUILD_STR($(perf_include_dir_SQ))"
> >
> > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> > index 6a571d322bb2..a5c88d52c11c 100644
> > --- a/tools/perf/util/evsel.c
> > +++ b/tools/perf/util/evsel.c
> > @@ -2433,7 +2433,7 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
> >
> >               /* Is there already the separator in the name. */
> >               if (strchr(name, '/') ||
> > -                 strchr(name, ':'))
> > +                 (strchr(name, ':') && !evsel->is_libpfm_event))
> >                       sep = "";
> >
> >               if (asprintf(&new_name, "%s%su", name, sep) < 0)
> > diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> > index a463bc65b001..709a9d494767 100644
> > --- a/tools/perf/util/evsel.h
> > +++ b/tools/perf/util/evsel.h
> > @@ -76,6 +76,7 @@ struct evsel {
> >       bool                    ignore_missing_thread;
> >       bool                    forced_leader;
> >       bool                    use_uncore_alias;
> > +     bool                    is_libpfm_event;
> >       /* parse modifier helper */
> >       int                     exclude_GH;
> >       int                     sample_read;
> > diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> > index 10107747b361..e7e0faf36e3c 100644
> > --- a/tools/perf/util/parse-events.c
> > +++ b/tools/perf/util/parse-events.c
> > @@ -36,6 +36,7 @@
> >  #include "metricgroup.h"
> >  #include "util/evsel_config.h"
> >  #include "util/event.h"
> > +#include "util/pfm.h"
> >
> >  #define MAX_NAME_LEN 100
> >
> > @@ -344,6 +345,7 @@ static char *get_config_name(struct list_head *head_terms)
> >  static struct evsel *
> >  __add_event(struct list_head *list, int *idx,
> >           struct perf_event_attr *attr,
> > +         bool init_attr,
> >           char *name, struct perf_pmu *pmu,
> >           struct list_head *config_terms, bool auto_merge_stats,
> >           const char *cpu_list)
> > @@ -352,7 +354,8 @@ __add_event(struct list_head *list, int *idx,
> >       struct perf_cpu_map *cpus = pmu ? pmu->cpus :
> >                              cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
> >
> > -     event_attr_init(attr);
> > +     if (init_attr)
> > +             event_attr_init(attr);
> >
> >       evsel = perf_evsel__new_idx(attr, *idx);
> >       if (!evsel)
> > @@ -370,15 +373,25 @@ __add_event(struct list_head *list, int *idx,
> >       if (config_terms)
> >               list_splice(config_terms, &evsel->config_terms);
> >
> > -     list_add_tail(&evsel->core.node, list);
> > +     if (list)
> > +             list_add_tail(&evsel->core.node, list);
> > +
> >       return evsel;
> >  }
> >
> > +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> > +                                     char *name, struct perf_pmu *pmu)
> > +{
> > +     return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
> > +                        NULL);
> > +}
> > +
> >  static int add_event(struct list_head *list, int *idx,
> >                    struct perf_event_attr *attr, char *name,
> >                    struct list_head *config_terms)
> >  {
> > -     return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
> > +     return __add_event(list, idx, attr, true, name, NULL, config_terms,
> > +                        false, NULL) ? 0 : -ENOMEM;
> >  }
> >
> >  static int add_event_tool(struct list_head *list, int *idx,
> > @@ -390,7 +403,8 @@ static int add_event_tool(struct list_head *list, int *idx,
> >               .config = PERF_COUNT_SW_DUMMY,
> >       };
> >
> > -     evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
> > +     evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
> > +                         "0");
> >       if (!evsel)
> >               return -ENOMEM;
> >       evsel->tool_event = tool_event;
> > @@ -1446,8 +1460,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> >
> >       if (!head_config) {
> >               attr.type = pmu->type;
> > -             evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
> > -                                 auto_merge_stats, NULL);
> > +             evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
> > +                                 pmu, NULL, auto_merge_stats, NULL);
> >               if (evsel) {
> >                       evsel->pmu_name = name ? strdup(name) : NULL;
> >                       evsel->use_uncore_alias = use_uncore_alias;
> > @@ -1487,7 +1501,7 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> >               return -EINVAL;
> >       }
> >
> > -     evsel = __add_event(list, &parse_state->idx, &attr,
> > +     evsel = __add_event(list, &parse_state->idx, &attr, true,
> >                           get_config_name(head_config), pmu,
> >                           &config_terms, auto_merge_stats, NULL);
> >       if (evsel) {
> > @@ -2794,6 +2808,8 @@ void print_events(const char *event_glob, bool name_only, bool quiet_flag,
> >       print_sdt_events(NULL, NULL, name_only);
> >
> >       metricgroup__print(true, true, NULL, name_only, details_flag);
> > +
> > +     print_libpfm_events(name_only, long_desc);
> >  }
> >
> >  int parse_events__is_hardcoded_term(struct parse_events_term *term)
> > diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
> > index 27596cbd0ba0..f7f555b1ca35 100644
> > --- a/tools/perf/util/parse-events.h
> > +++ b/tools/perf/util/parse-events.h
> > @@ -17,6 +17,7 @@ struct evlist;
> >  struct parse_events_error;
> >
> >  struct option;
> > +struct perf_pmu;
> >
> >  struct tracepoint_path {
> >       char *system;
> > @@ -186,6 +187,9 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> >                        bool auto_merge_stats,
> >                        bool use_alias);
> >
> > +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> > +                                     char *name, struct perf_pmu *pmu);
> > +
> >  int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
> >                              char *str,
> >                              struct list_head **listp);
> > diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c
> > new file mode 100644
> > index 000000000000..4b11adf832a0
> > --- /dev/null
> > +++ b/tools/perf/util/pfm.c
> > @@ -0,0 +1,277 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Support for libpfm4 event encoding.
> > + *
> > + * Copyright 2020 Google LLC.
> > + */
> > +#include "util/cpumap.h"
> > +#include "util/debug.h"
> > +#include "util/event.h"
> > +#include "util/evlist.h"
> > +#include "util/evsel.h"
> > +#include "util/parse-events.h"
> > +#include "util/pmu.h"
> > +#include "util/pfm.h"
> > +
> > +#include <string.h>
> > +#include <linux/kernel.h>
> > +#include <perfmon/pfmlib_perf_event.h>
> > +
> > +void libpfm_initialize(void)
> > +{
> > +     int ret;
> > +
> > +     ret = pfm_initialize();
> > +     if (ret != PFM_SUCCESS) {
> > +             ui__warning("libpfm failed to initialize: %s\n",
> > +                     pfm_strerror(ret));
> > +     }
> > +}
> > +
> > +int parse_libpfm_events_option(const struct option *opt, const char *str,
> > +                     int unset __maybe_unused)
> > +{
> > +     struct evlist *evlist = *(struct evlist **)opt->value;
> > +     struct perf_event_attr attr;
> > +     struct perf_pmu *pmu;
> > +     struct evsel *evsel, *grp_leader = NULL;
> > +     char *p, *q, *p_orig;
> > +     const char *sep;
> > +     int grp_evt = -1;
> > +     int ret;
> > +
> > +     p_orig = p = strdup(str);
> > +     if (!p)
> > +             return -1;
> > +     /*
> > +      * force loading of the PMU list
> > +      */
> > +     perf_pmu__scan(NULL);
> > +
> > +     for (q = p; strsep(&p, ",{}"); q = p) {
> > +             sep = p ? str + (p - p_orig - 1) : "";
> > +             if (*sep == '{') {
> > +                     if (grp_evt > -1) {
> > +                             ui__error(
> > +                                     "nested event groups not supported\n");
> > +                             goto error;
> > +                     }
> > +                     grp_evt++;
> > +             }
> > +
> > +             /* no event */
> > +             if (*q == '\0')
> > +                     continue;
> > +
> > +             memset(&attr, 0, sizeof(attr));
> > +             event_attr_init(&attr);
> > +
> > +             ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
> > +                                             &attr, NULL, NULL);
> > +
> > +             if (ret != PFM_SUCCESS) {
> > +                     ui__error("failed to parse event %s : %s\n", str,
> > +                               pfm_strerror(ret));
> > +                     goto error;
> > +             }
> > +
> > +             pmu = perf_pmu__find_by_type((unsigned int)attr.type);
> > +             evsel = parse_events__add_event(evlist->core.nr_entries,
> > +                                             &attr, q, pmu);
> > +             if (evsel == NULL)
> > +                     goto error;
> > +
> > +             evsel->is_libpfm_event = true;
> > +
> > +             evlist__add(evlist, evsel);
> > +
> > +             if (grp_evt == 0)
> > +                     grp_leader = evsel;
> > +
> > +             if (grp_evt > -1) {
> > +                     evsel->leader = grp_leader;
> > +                     grp_leader->core.nr_members++;
> > +                     grp_evt++;
> > +             }
> > +
> > +             if (*sep == '}') {
> > +                     if (grp_evt < 0) {
> > +                             ui__error(
> > +                                "cannot close a non-existing event group\n");
> > +                             goto error;
> > +                     }
> > +                     evlist->nr_groups++;
> > +                     grp_leader = NULL;
> > +                     grp_evt = -1;
> > +             }
> > +     }
> > +     return 0;
> > +error:
> > +     free(p_orig);
> > +     return -1;
> > +}
> > +
> > +static const char *srcs[PFM_ATTR_CTRL_MAX] = {
> > +     [PFM_ATTR_CTRL_UNKNOWN] = "???",
> > +     [PFM_ATTR_CTRL_PMU] = "PMU",
> > +     [PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
> > +};
> > +
> > +static void
> > +print_attr_flags(pfm_event_attr_info_t *info)
> > +{
> > +     int n = 0;
> > +
> > +     if (info->is_dfl) {
> > +             printf("[default] ");
> > +             n++;
> > +     }
> > +
> > +     if (info->is_precise) {
> > +             printf("[precise] ");
> > +             n++;
> > +     }
> > +
> > +     if (!n)
> > +             printf("- ");
> > +}
> > +
> > +static void
> > +print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc)
> > +{
> > +     pfm_event_attr_info_t ainfo;
> > +     const char *src;
> > +     int j, ret;
> > +
> > +     ainfo.size = sizeof(ainfo);
> > +
> > +     printf("  %s\n", info->name);
> > +     printf("    [%s]\n", info->desc);
> > +     if (long_desc) {
> > +             if (info->equiv)
> > +                     printf("      Equiv: %s\n", info->equiv);
> > +
> > +             printf("      Code  : 0x%"PRIx64"\n", info->code);
> > +     }
> > +     pfm_for_each_event_attr(j, info) {
> > +             ret = pfm_get_event_attr_info(info->idx, j,
> > +                                           PFM_OS_PERF_EVENT_EXT, &ainfo);
> > +             if (ret != PFM_SUCCESS)
> > +                     continue;
> > +
> > +             if (ainfo.type == PFM_ATTR_UMASK) {
> > +                     printf("      %s:%s\n", info->name, ainfo.name);
> > +                     printf("        [%s]\n", ainfo.desc);
> > +             }
> > +
> > +             if (!long_desc)
> > +                     continue;
> > +
> > +             if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
> > +                     ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
> > +
> > +             src = srcs[ainfo.ctrl];
> > +             switch (ainfo.type) {
> > +             case PFM_ATTR_UMASK:
> > +                     printf("        Umask : 0x%02"PRIx64" : %s: ",
> > +                             ainfo.code, src);
> > +                     print_attr_flags(&ainfo);
> > +                     putchar('\n');
> > +                     break;
> > +             case PFM_ATTR_MOD_BOOL:
> > +                     printf("      Modif : %s: [%s] : %s (boolean)\n", src,
> > +                             ainfo.name, ainfo.desc);
> > +                     break;
> > +             case PFM_ATTR_MOD_INTEGER:
> > +                     printf("      Modif : %s: [%s] : %s (integer)\n", src,
> > +                             ainfo.name, ainfo.desc);
> > +                     break;
> > +             case PFM_ATTR_NONE:
> > +             case PFM_ATTR_RAW_UMASK:
> > +             case PFM_ATTR_MAX:
> > +             default:
> > +                     printf("      Attr  : %s: [%s] : %s\n", src,
> > +                             ainfo.name, ainfo.desc);
> > +             }
> > +     }
> > +}
> > +
> > +/*
> > + * list all pmu::event:umask, pmu::event
> > + * printed events may not be all valid combinations of umask for an event
> > + */
> > +static void
> > +print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info)
> > +{
> > +     pfm_event_attr_info_t ainfo;
> > +     int j, ret;
> > +     bool has_umask = false;
> > +
> > +     ainfo.size = sizeof(ainfo);
> > +
> > +     pfm_for_each_event_attr(j, info) {
> > +             ret = pfm_get_event_attr_info(info->idx, j,
> > +                                           PFM_OS_PERF_EVENT_EXT, &ainfo);
> > +             if (ret != PFM_SUCCESS)
> > +                     continue;
> > +
> > +             if (ainfo.type != PFM_ATTR_UMASK)
> > +                     continue;
> > +
> > +             printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name);
> > +             has_umask = true;
> > +     }
> > +     if (!has_umask)
> > +             printf("%s::%s\n", pinfo->name, info->name);
> > +}
> > +
> > +void print_libpfm_events(bool name_only, bool long_desc)
> > +{
> > +     pfm_event_info_t info;
> > +     pfm_pmu_info_t pinfo;
> > +     int i, p, ret;
> > +
> > +     /* initialize to zero to indicate ABI version */
> > +     info.size  = sizeof(info);
> > +     pinfo.size = sizeof(pinfo);
> > +
> > +     if (!name_only)
> > +             puts("\nList of pre-defined events (to be used in --pfm-events):\n");
> > +
> > +     pfm_for_all_pmus(p) {
> > +             bool printed_pmu = false;
> > +
> > +             ret = pfm_get_pmu_info(p, &pinfo);
> > +             if (ret != PFM_SUCCESS)
> > +                     continue;
> > +
> > +             /* only print events that are supported by host HW */
> > +             if (!pinfo.is_present)
> > +                     continue;
> > +
> > +             /* handled by perf directly */
> > +             if (pinfo.pmu == PFM_PMU_PERF_EVENT)
> > +                     continue;
> > +
> > +             for (i = pinfo.first_event; i != -1;
> > +                  i = pfm_get_event_next(i)) {
> > +
> > +                     ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
> > +                                             &info);
> > +                     if (ret != PFM_SUCCESS)
> > +                             continue;
> > +
> > +                     if (!name_only && !printed_pmu) {
> > +                             printf("%s:\n", pinfo.name);
> > +                             printed_pmu = true;
> > +                     }
> > +
> > +                     if (!name_only)
> > +                             print_libpfm_events_detailed(&info, long_desc);
> > +                     else
> > +                             print_libpfm_events_raw(&pinfo, &info);
> > +             }
> > +             if (!name_only && printed_pmu)
> > +                     putchar('\n');
> > +     }
> > +}
> > diff --git a/tools/perf/util/pfm.h b/tools/perf/util/pfm.h
> > new file mode 100644
> > index 000000000000..532dafe88bc0
> > --- /dev/null
> > +++ b/tools/perf/util/pfm.h
> > @@ -0,0 +1,43 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +/*
> > + * Support for libpfm4 event encoding.
> > + *
> > + * Copyright 2020 Google LLC.
> > + */
> > +#ifndef __PERF_PFM_H
> > +#define __PERF_PFM_H
> > +
> > +#include <subcmd/parse-options.h>
> > +
> > +#ifdef HAVE_LIBPFM
> > +void libpfm_initialize(void);
> > +
> > +int parse_libpfm_events_option(const struct option *opt, const char *str,
> > +                     int unset);
> > +
> > +void print_libpfm_events(bool name_only, bool long_desc);
> > +
> > +#else
> > +#include <linux/compiler.h>
> > +
> > +static inline void libpfm_initialize(void)
> > +{
> > +}
> > +
> > +static inline int parse_libpfm_events_option(
> > +     const struct option *opt __maybe_unused,
> > +     const char *str __maybe_unused,
> > +     int unset __maybe_unused)
> > +{
> > +     return 0;
> > +}
> > +
> > +static inline void print_libpfm_events(bool name_only __maybe_unused,
> > +                                    bool long_desc __maybe_unused)
> > +{
> > +}
> > +
> > +#endif
> > +
> > +
> > +#endif /* __PERF_PFM_H */
> > --
> > 2.26.2.303.gf8c07b1a785-goog
> >
>
> --
>
> - Arnaldo

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

* Re: [PATCH v12 4/4] perf tools: add support for libpfm4
@ 2020-04-30 14:26       ` Ian Rogers
  0 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2020-04-30 14:26 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Yonghong Song, Andrii Nakryiko,
	Greg Kroah-Hartman, Thomas Gleixner, Igor Lubashev,
	Alexey Budankov, Florian Fainelli, Adrian Hunter, Andi Kleen,
	Jiwei Sun, yuzhoujian

On Thu, Apr 30, 2020 at 6:34 AM Arnaldo Carvalho de Melo
<arnaldo.melo@gmail.com> wrote:
>
> Em Wed, Apr 29, 2020 at 04:14:43PM -0700, Ian Rogers escreveu:
> > From: Stephane Eranian <eranian@google.com>
> >
> > This patch links perf with the libpfm4 library if it is available
> > and LIBPFM4 is passed to the build. The libpfm4 library
> > contains hardware event tables for all processors supported by
> > perf_events. It is a helper library that helps convert from a
> > symbolic event name to the event encoding required by the
> > underlying kernel interface. This library is open-source and
> > available from: http://perfmon2.sf.net.
>
> Hey, I think we should not call libpfm_initialize() in all tools, better
> leave it to when the --pfm-event callback gets called, because the user
> is really using that event syntax, i.e. do it lazily, to reduce the
> amount of initialization that perf does, which is already excessive and
> needs triming down, to do more things only when they are really, really
> needed.
>
> I have applied the previous, preparatory patches.

Thanks! I didn't see the whole set rebasing on:
https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/log/?h=perf/core
and so sent it all again:
https://lore.kernel.org/lkml/20200430142419.252180-5-irogers@google.com/T/#u

Thanks,
Ian

> - Arnaldo
>
> > With this patch, it is possible to specify full hardware events
> > by name. Hardware filters are also supported. Events must be
> > specified via the --pfm-events and not -e option. Both options
> > are active at the same time and it is possible to mix and match:
> >
> > $ perf stat --pfm-events inst_retired:any_p:c=1:i -e cycles ....
> >
> > Signed-off-by: Stephane Eranian <eranian@google.com>
> > Reviewed-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/perf/Documentation/perf-record.txt |  11 +
> >  tools/perf/Documentation/perf-stat.txt   |  10 +
> >  tools/perf/Documentation/perf-top.txt    |  11 +
> >  tools/perf/Makefile.config               |  13 ++
> >  tools/perf/Makefile.perf                 |   2 +
> >  tools/perf/builtin-list.c                |   3 +
> >  tools/perf/builtin-record.c              |   8 +
> >  tools/perf/builtin-stat.c                |   8 +
> >  tools/perf/builtin-top.c                 |   8 +
> >  tools/perf/tests/Build                   |   1 +
> >  tools/perf/tests/builtin-test.c          |   9 +
> >  tools/perf/tests/pfm.c                   | 207 +++++++++++++++++
> >  tools/perf/tests/tests.h                 |   3 +
> >  tools/perf/util/Build                    |   2 +
> >  tools/perf/util/evsel.c                  |   2 +-
> >  tools/perf/util/evsel.h                  |   1 +
> >  tools/perf/util/parse-events.c           |  30 ++-
> >  tools/perf/util/parse-events.h           |   4 +
> >  tools/perf/util/pfm.c                    | 277 +++++++++++++++++++++++
> >  tools/perf/util/pfm.h                    |  43 ++++
> >  20 files changed, 645 insertions(+), 8 deletions(-)
> >  create mode 100644 tools/perf/tests/pfm.c
> >  create mode 100644 tools/perf/util/pfm.c
> >  create mode 100644 tools/perf/util/pfm.h
> >
> > diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> > index b3f3b3f1c161..ad2c41f595c2 100644
> > --- a/tools/perf/Documentation/perf-record.txt
> > +++ b/tools/perf/Documentation/perf-record.txt
> > @@ -596,6 +596,17 @@ Make a copy of /proc/kcore and place it into a directory with the perf data file
> >  Limit the sample data max size, <size> is expected to be a number with
> >  appended unit character - B/K/M/G
> >
> > +ifdef::HAVE_LIBPFM[]
> > +--pfm-events events::
> > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > +including support for event filters. For example '--pfm-events
> > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > +option using the comma separator. Hardware events and generic hardware
> > +events cannot be mixed together. The latter must be used with the -e
> > +option. The -e option and this one can be mixed and matched.  Events
> > +can be grouped using the {} notation.
> > +endif::HAVE_LIBPFM[]
> > +
> >  SEE ALSO
> >  --------
> >  linkperf:perf-stat[1], linkperf:perf-list[1], linkperf:perf-intel-pt[1]
> > diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
> > index 4d56586b2fb9..536952ad592c 100644
> > --- a/tools/perf/Documentation/perf-stat.txt
> > +++ b/tools/perf/Documentation/perf-stat.txt
> > @@ -71,6 +71,16 @@ report::
> >  --tid=<tid>::
> >          stat events on existing thread id (comma separated list)
> >
> > +ifdef::HAVE_LIBPFM[]
> > +--pfm-events events::
> > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > +including support for event filters. For example '--pfm-events
> > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > +option using the comma separator. Hardware events and generic hardware
> > +events cannot be mixed together. The latter must be used with the -e
> > +option. The -e option and this one can be mixed and matched.  Events
> > +can be grouped using the {} notation.
> > +endif::HAVE_LIBPFM[]
> >
> >  -a::
> >  --all-cpus::
> > diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt
> > index 20227dabc208..ee2024691d46 100644
> > --- a/tools/perf/Documentation/perf-top.txt
> > +++ b/tools/perf/Documentation/perf-top.txt
> > @@ -329,6 +329,17 @@ Default is to monitor all CPUS.
> >       The known limitations include exception handing such as
> >       setjmp/longjmp will have calls/returns not match.
> >
> > +ifdef::HAVE_LIBPFM[]
> > +--pfm-events events::
> > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > +including support for event filters. For example '--pfm-events
> > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > +option using the comma separator. Hardware events and generic hardware
> > +events cannot be mixed together. The latter must be used with the -e
> > +option. The -e option and this one can be mixed and matched.  Events
> > +can be grouped using the {} notation.
> > +endif::HAVE_LIBPFM[]
> > +
> >  INTERACTIVE PROMPTING KEYS
> >  --------------------------
> >
> > diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> > index 12a8204d63c6..b67804fee1e3 100644
> > --- a/tools/perf/Makefile.config
> > +++ b/tools/perf/Makefile.config
> > @@ -1012,6 +1012,19 @@ ifdef LIBCLANGLLVM
> >    endif
> >  endif
> >
> > +ifdef LIBPFM4
> > +  $(call feature_check,libpfm4)
> > +  ifeq ($(feature-libpfm4), 1)
> > +    CFLAGS += -DHAVE_LIBPFM
> > +    EXTLIBS += -lpfm
> > +    ASCIIDOC_EXTRA = -aHAVE_LIBPFM=1
> > +    $(call detected,CONFIG_LIBPFM4)
> > +  else
> > +    msg := $(warning libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev);
> > +    NO_LIBPFM4 := 1
> > +  endif
> > +endif
> > +
> >  # Among the variables below, these:
> >  #   perfexecdir
> >  #   perf_include_dir
> > diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
> > index 94a495594e99..dc82578c8773 100644
> > --- a/tools/perf/Makefile.perf
> > +++ b/tools/perf/Makefile.perf
> > @@ -118,6 +118,8 @@ include ../scripts/utilities.mak
> >  #
> >  # Define LIBBPF_DYNAMIC to enable libbpf dynamic linking.
> >  #
> > +# Define LIBPFM4 to enable libpfm4 events extension.
> > +#
> >
> >  # As per kernel Makefile, avoid funny character set dependencies
> >  unexport LC_ALL
> > diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
> > index 965ef017496f..d2069bdcb469 100644
> > --- a/tools/perf/builtin-list.c
> > +++ b/tools/perf/builtin-list.c
> > @@ -14,6 +14,7 @@
> >  #include "util/pmu.h"
> >  #include "util/debug.h"
> >  #include "util/metricgroup.h"
> > +#include "util/pfm.h"
> >  #include <subcmd/pager.h>
> >  #include <subcmd/parse-options.h>
> >  #include <stdio.h>
> > @@ -53,6 +54,8 @@ int cmd_list(int argc, const char **argv)
> >
> >       setup_pager();
> >
> > +     libpfm_initialize();
> > +
> >       if (!raw_dump && pager_in_use())
> >               printf("\nList of pre-defined events (to be used in -e):\n\n");
> >
> > diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> > index 1ab349abe904..fc55b641849f 100644
> > --- a/tools/perf/builtin-record.c
> > +++ b/tools/perf/builtin-record.c
> > @@ -43,6 +43,7 @@
> >  #include "util/time-utils.h"
> >  #include "util/units.h"
> >  #include "util/bpf-event.h"
> > +#include "util/pfm.h"
> >  #include "asm/bug.h"
> >  #include "perf.h"
> >
> > @@ -2421,6 +2422,11 @@ static struct option __record_options[] = {
> >  #endif
> >       OPT_CALLBACK(0, "max-size", &record.output_max_size,
> >                    "size", "Limit the maximum size of the output file", parse_output_max_size),
> > +#ifdef HAVE_LIBPFM
> > +     OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
> > +             "libpfm4 event selector. use 'perf list' to list available events",
> > +             parse_libpfm_events_option),
> > +#endif
> >       OPT_END()
> >  };
> >
> > @@ -2461,6 +2467,8 @@ int cmd_record(int argc, const char **argv)
> >       if (rec->evlist == NULL)
> >               return -ENOMEM;
> >
> > +     libpfm_initialize();
> > +
> >       err = perf_config(perf_record_config, rec);
> >       if (err)
> >               return err;
> > diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> > index 9207b6c45475..607920321c15 100644
> > --- a/tools/perf/builtin-stat.c
> > +++ b/tools/perf/builtin-stat.c
> > @@ -66,6 +66,7 @@
> >  #include "util/time-utils.h"
> >  #include "util/top.h"
> >  #include "util/affinity.h"
> > +#include "util/pfm.h"
> >  #include "asm/bug.h"
> >
> >  #include <linux/time64.h>
> > @@ -936,6 +937,11 @@ static struct option stat_options[] = {
> >                   "Use with 'percore' event qualifier to show the event "
> >                   "counts of one hardware thread by sum up total hardware "
> >                   "threads of same physical core"),
> > +#ifdef HAVE_LIBPFM
> > +     OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
> > +             "libpfm4 event selector. use 'perf list' to list available events",
> > +             parse_libpfm_events_option),
> > +#endif
> >       OPT_END()
> >  };
> >
> > @@ -1874,6 +1880,8 @@ int cmd_stat(int argc, const char **argv)
> >       unsigned int interval, timeout;
> >       const char * const stat_subcommands[] = { "record", "report" };
> >
> > +     libpfm_initialize();
> > +
> >       setlocale(LC_ALL, "");
> >
> >       evsel_list = evlist__new();
> > diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
> > index 6b067a5ba1d5..e1f477a18f93 100644
> > --- a/tools/perf/builtin-top.c
> > +++ b/tools/perf/builtin-top.c
> > @@ -53,6 +53,7 @@
> >
> >  #include "util/debug.h"
> >  #include "util/ordered-events.h"
> > +#include "util/pfm.h"
> >
> >  #include <assert.h>
> >  #include <elf.h>
> > @@ -1577,6 +1578,11 @@ int cmd_top(int argc, const char **argv)
> >                   "WARNING: should be used on grouped events."),
> >       OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
> >                   "Enable LBR callgraph stitching approach"),
> > +#ifdef HAVE_LIBPFM
> > +     OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
> > +             "libpfm4 event selector. use 'perf list' to list available events",
> > +             parse_libpfm_events_option),
> > +#endif
> >       OPTS_EVSWITCH(&top.evswitch),
> >       OPT_END()
> >       };
> > @@ -1590,6 +1596,8 @@ int cmd_top(int argc, const char **argv)
> >       if (status < 0)
> >               return status;
> >
> > +     libpfm_initialize();
> > +
> >       top.annotation_opts.min_pcnt = 5;
> >       top.annotation_opts.context  = 4;
> >
> > diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
> > index b3d1bf13ca07..7d2ccec9fd75 100644
> > --- a/tools/perf/tests/Build
> > +++ b/tools/perf/tests/Build
> > @@ -56,6 +56,7 @@ perf-y += mem2node.o
> >  perf-y += maps.o
> >  perf-y += time-utils-test.o
> >  perf-y += genelf.o
> > +perf-y += pfm.o
> >
> >  $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
> >       $(call rule_mkdir)
> > diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
> > index b6322eb0f423..8b323151f22c 100644
> > --- a/tools/perf/tests/builtin-test.c
> > +++ b/tools/perf/tests/builtin-test.c
> > @@ -313,6 +313,15 @@ static struct test generic_tests[] = {
> >               .desc = "maps__merge_in",
> >               .func = test__maps__merge_in,
> >       },
> > +     {
> > +             .desc = "Test libpfm4 support",
> > +             .func = test__pfm,
> > +             .subtest = {
> > +                     .skip_if_fail   = true,
> > +                     .get_nr         = test__pfm_subtest_get_nr,
> > +                     .get_desc       = test__pfm_subtest_get_desc,
> > +             }
> > +     },
> >       {
> >               .func = NULL,
> >       },
> > diff --git a/tools/perf/tests/pfm.c b/tools/perf/tests/pfm.c
> > new file mode 100644
> > index 000000000000..1d594fd3d9c1
> > --- /dev/null
> > +++ b/tools/perf/tests/pfm.c
> > @@ -0,0 +1,207 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Test support for libpfm4 event encodings.
> > + *
> > + * Copyright 2020 Google LLC.
> > + */
> > +#include "tests.h"
> > +#include "util/debug.h"
> > +#include "util/evlist.h"
> > +#include "util/pfm.h"
> > +
> > +#include <linux/kernel.h>
> > +
> > +#ifdef HAVE_LIBPFM
> > +static int test__pfm_events(void);
> > +static int test__pfm_group(void);
> > +#endif
> > +
> > +static const struct {
> > +     int (*func)(void);
> > +     const char *desc;
> > +} pfm_testcase_table[] = {
> > +#ifdef HAVE_LIBPFM
> > +     {
> > +             .func = test__pfm_events,
> > +             .desc = "test of individual --pfm-events",
> > +     },
> > +     {
> > +             .func = test__pfm_group,
> > +             .desc = "test groups of --pfm-events",
> > +     },
> > +#endif
> > +};
> > +
> > +#ifdef HAVE_LIBPFM
> > +static int count_pfm_events(struct perf_evlist *evlist)
> > +{
> > +     struct perf_evsel *evsel;
> > +     int count = 0;
> > +
> > +     perf_evlist__for_each_entry(evlist, evsel) {
> > +             count++;
> > +     }
> > +     return count;
> > +}
> > +
> > +static int test__pfm_events(void)
> > +{
> > +     struct evlist *evlist;
> > +     struct option opt;
> > +     size_t i;
> > +     const struct {
> > +             const char *events;
> > +             int nr_events;
> > +     } table[] = {
> > +             {
> > +                     .events = "",
> > +                     .nr_events = 0,
> > +             },
> > +             {
> > +                     .events = "instructions",
> > +                     .nr_events = 1,
> > +             },
> > +             {
> > +                     .events = "instructions,cycles",
> > +                     .nr_events = 2,
> > +             },
> > +             {
> > +                     .events = "stereolab",
> > +                     .nr_events = 0,
> > +             },
> > +             {
> > +                     .events = "instructions,instructions",
> > +                     .nr_events = 2,
> > +             },
> > +             {
> > +                     .events = "stereolab,instructions",
> > +                     .nr_events = 0,
> > +             },
> > +             {
> > +                     .events = "instructions,stereolab",
> > +                     .nr_events = 1,
> > +             },
> > +     };
> > +
> > +     libpfm_initialize();
> > +
> > +     for (i = 0; i < ARRAY_SIZE(table); i++) {
> > +             evlist = evlist__new();
> > +             if (evlist == NULL)
> > +                     return -ENOMEM;
> > +
> > +             opt.value = evlist;
> > +             parse_libpfm_events_option(&opt,
> > +                                     table[i].events,
> > +                                     0);
> > +             TEST_ASSERT_EQUAL(table[i].events,
> > +                             count_pfm_events(&evlist->core),
> > +                             table[i].nr_events);
> > +             TEST_ASSERT_EQUAL(table[i].events,
> > +                             evlist->nr_groups,
> > +                             0);
> > +
> > +             evlist__delete(evlist);
> > +     }
> > +     return 0;
> > +}
> > +
> > +static int test__pfm_group(void)
> > +{
> > +     struct evlist *evlist;
> > +     struct option opt;
> > +     size_t i;
> > +     const struct {
> > +             const char *events;
> > +             int nr_events;
> > +             int nr_groups;
> > +     } table[] = {
> > +             {
> > +                     .events = "{},",
> > +                     .nr_events = 0,
> > +                     .nr_groups = 0,
> > +             },
> > +             {
> > +                     .events = "{instructions}",
> > +                     .nr_events = 1,
> > +                     .nr_groups = 1,
> > +             },
> > +             {
> > +                     .events = "{instructions},{}",
> > +                     .nr_events = 1,
> > +                     .nr_groups = 1,
> > +             },
> > +             {
> > +                     .events = "{},{instructions}",
> > +                     .nr_events = 0,
> > +                     .nr_groups = 0,
> > +             },
> > +             {
> > +                     .events = "{instructions},{instructions}",
> > +                     .nr_events = 2,
> > +                     .nr_groups = 2,
> > +             },
> > +             {
> > +                     .events = "{instructions,cycles},{instructions,cycles}",
> > +                     .nr_events = 4,
> > +                     .nr_groups = 2,
> > +             },
> > +             {
> > +                     .events = "{stereolab}",
> > +                     .nr_events = 0,
> > +                     .nr_groups = 0,
> > +             },
> > +             {
> > +                     .events =
> > +                     "{instructions,cycles},{instructions,stereolab}",
> > +                     .nr_events = 3,
> > +                     .nr_groups = 1,
> > +             },
> > +     };
> > +
> > +     libpfm_initialize();
> > +
> > +     for (i = 0; i < ARRAY_SIZE(table); i++) {
> > +             evlist = evlist__new();
> > +             if (evlist == NULL)
> > +                     return -ENOMEM;
> > +
> > +             opt.value = evlist;
> > +             parse_libpfm_events_option(&opt,
> > +                                     table[i].events,
> > +                                     0);
> > +             TEST_ASSERT_EQUAL(table[i].events,
> > +                             count_pfm_events(&evlist->core),
> > +                             table[i].nr_events);
> > +             TEST_ASSERT_EQUAL(table[i].events,
> > +                             evlist->nr_groups,
> > +                             table[i].nr_groups);
> > +
> > +             evlist__delete(evlist);
> > +     }
> > +     return 0;
> > +}
> > +#endif
> > +
> > +const char *test__pfm_subtest_get_desc(int i)
> > +{
> > +     if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> > +             return NULL;
> > +     return pfm_testcase_table[i].desc;
> > +}
> > +
> > +int test__pfm_subtest_get_nr(void)
> > +{
> > +     return (int)ARRAY_SIZE(pfm_testcase_table);
> > +}
> > +
> > +int test__pfm(struct test *test __maybe_unused, int i __maybe_unused)
> > +{
> > +#ifdef HAVE_LIBPFM
> > +     if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> > +             return TEST_FAIL;
> > +     return pfm_testcase_table[i].func();
> > +#else
> > +     return TEST_SKIP;
> > +#endif
> > +}
> > diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
> > index 61a1ab032080..47cbae753b42 100644
> > --- a/tools/perf/tests/tests.h
> > +++ b/tools/perf/tests/tests.h
> > @@ -112,6 +112,9 @@ int test__mem2node(struct test *t, int subtest);
> >  int test__maps__merge_in(struct test *t, int subtest);
> >  int test__time_utils(struct test *t, int subtest);
> >  int test__jit_write_elf(struct test *test, int subtest);
> > +int test__pfm(struct test *test, int subtest);
> > +const char *test__pfm_subtest_get_desc(int subtest);
> > +int test__pfm_subtest_get_nr(void);
> >
> >  bool test__bp_signal_is_supported(void);
> >  bool test__bp_account_is_supported(void);
> > diff --git a/tools/perf/util/Build b/tools/perf/util/Build
> > index c0cf8dff694e..afd3d2221c83 100644
> > --- a/tools/perf/util/Build
> > +++ b/tools/perf/util/Build
> > @@ -177,6 +177,8 @@ perf-$(CONFIG_LIBBPF) += bpf-event.o
> >
> >  perf-$(CONFIG_CXX) += c++/
> >
> > +perf-$(CONFIG_LIBPFM4) += pfm.o
> > +
> >  CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
> >  CFLAGS_llvm-utils.o += -DPERF_INCLUDE_DIR="BUILD_STR($(perf_include_dir_SQ))"
> >
> > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> > index 6a571d322bb2..a5c88d52c11c 100644
> > --- a/tools/perf/util/evsel.c
> > +++ b/tools/perf/util/evsel.c
> > @@ -2433,7 +2433,7 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
> >
> >               /* Is there already the separator in the name. */
> >               if (strchr(name, '/') ||
> > -                 strchr(name, ':'))
> > +                 (strchr(name, ':') && !evsel->is_libpfm_event))
> >                       sep = "";
> >
> >               if (asprintf(&new_name, "%s%su", name, sep) < 0)
> > diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> > index a463bc65b001..709a9d494767 100644
> > --- a/tools/perf/util/evsel.h
> > +++ b/tools/perf/util/evsel.h
> > @@ -76,6 +76,7 @@ struct evsel {
> >       bool                    ignore_missing_thread;
> >       bool                    forced_leader;
> >       bool                    use_uncore_alias;
> > +     bool                    is_libpfm_event;
> >       /* parse modifier helper */
> >       int                     exclude_GH;
> >       int                     sample_read;
> > diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> > index 10107747b361..e7e0faf36e3c 100644
> > --- a/tools/perf/util/parse-events.c
> > +++ b/tools/perf/util/parse-events.c
> > @@ -36,6 +36,7 @@
> >  #include "metricgroup.h"
> >  #include "util/evsel_config.h"
> >  #include "util/event.h"
> > +#include "util/pfm.h"
> >
> >  #define MAX_NAME_LEN 100
> >
> > @@ -344,6 +345,7 @@ static char *get_config_name(struct list_head *head_terms)
> >  static struct evsel *
> >  __add_event(struct list_head *list, int *idx,
> >           struct perf_event_attr *attr,
> > +         bool init_attr,
> >           char *name, struct perf_pmu *pmu,
> >           struct list_head *config_terms, bool auto_merge_stats,
> >           const char *cpu_list)
> > @@ -352,7 +354,8 @@ __add_event(struct list_head *list, int *idx,
> >       struct perf_cpu_map *cpus = pmu ? pmu->cpus :
> >                              cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
> >
> > -     event_attr_init(attr);
> > +     if (init_attr)
> > +             event_attr_init(attr);
> >
> >       evsel = perf_evsel__new_idx(attr, *idx);
> >       if (!evsel)
> > @@ -370,15 +373,25 @@ __add_event(struct list_head *list, int *idx,
> >       if (config_terms)
> >               list_splice(config_terms, &evsel->config_terms);
> >
> > -     list_add_tail(&evsel->core.node, list);
> > +     if (list)
> > +             list_add_tail(&evsel->core.node, list);
> > +
> >       return evsel;
> >  }
> >
> > +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> > +                                     char *name, struct perf_pmu *pmu)
> > +{
> > +     return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
> > +                        NULL);
> > +}
> > +
> >  static int add_event(struct list_head *list, int *idx,
> >                    struct perf_event_attr *attr, char *name,
> >                    struct list_head *config_terms)
> >  {
> > -     return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
> > +     return __add_event(list, idx, attr, true, name, NULL, config_terms,
> > +                        false, NULL) ? 0 : -ENOMEM;
> >  }
> >
> >  static int add_event_tool(struct list_head *list, int *idx,
> > @@ -390,7 +403,8 @@ static int add_event_tool(struct list_head *list, int *idx,
> >               .config = PERF_COUNT_SW_DUMMY,
> >       };
> >
> > -     evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
> > +     evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
> > +                         "0");
> >       if (!evsel)
> >               return -ENOMEM;
> >       evsel->tool_event = tool_event;
> > @@ -1446,8 +1460,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> >
> >       if (!head_config) {
> >               attr.type = pmu->type;
> > -             evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
> > -                                 auto_merge_stats, NULL);
> > +             evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
> > +                                 pmu, NULL, auto_merge_stats, NULL);
> >               if (evsel) {
> >                       evsel->pmu_name = name ? strdup(name) : NULL;
> >                       evsel->use_uncore_alias = use_uncore_alias;
> > @@ -1487,7 +1501,7 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> >               return -EINVAL;
> >       }
> >
> > -     evsel = __add_event(list, &parse_state->idx, &attr,
> > +     evsel = __add_event(list, &parse_state->idx, &attr, true,
> >                           get_config_name(head_config), pmu,
> >                           &config_terms, auto_merge_stats, NULL);
> >       if (evsel) {
> > @@ -2794,6 +2808,8 @@ void print_events(const char *event_glob, bool name_only, bool quiet_flag,
> >       print_sdt_events(NULL, NULL, name_only);
> >
> >       metricgroup__print(true, true, NULL, name_only, details_flag);
> > +
> > +     print_libpfm_events(name_only, long_desc);
> >  }
> >
> >  int parse_events__is_hardcoded_term(struct parse_events_term *term)
> > diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
> > index 27596cbd0ba0..f7f555b1ca35 100644
> > --- a/tools/perf/util/parse-events.h
> > +++ b/tools/perf/util/parse-events.h
> > @@ -17,6 +17,7 @@ struct evlist;
> >  struct parse_events_error;
> >
> >  struct option;
> > +struct perf_pmu;
> >
> >  struct tracepoint_path {
> >       char *system;
> > @@ -186,6 +187,9 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> >                        bool auto_merge_stats,
> >                        bool use_alias);
> >
> > +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> > +                                     char *name, struct perf_pmu *pmu);
> > +
> >  int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
> >                              char *str,
> >                              struct list_head **listp);
> > diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c
> > new file mode 100644
> > index 000000000000..4b11adf832a0
> > --- /dev/null
> > +++ b/tools/perf/util/pfm.c
> > @@ -0,0 +1,277 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Support for libpfm4 event encoding.
> > + *
> > + * Copyright 2020 Google LLC.
> > + */
> > +#include "util/cpumap.h"
> > +#include "util/debug.h"
> > +#include "util/event.h"
> > +#include "util/evlist.h"
> > +#include "util/evsel.h"
> > +#include "util/parse-events.h"
> > +#include "util/pmu.h"
> > +#include "util/pfm.h"
> > +
> > +#include <string.h>
> > +#include <linux/kernel.h>
> > +#include <perfmon/pfmlib_perf_event.h>
> > +
> > +void libpfm_initialize(void)
> > +{
> > +     int ret;
> > +
> > +     ret = pfm_initialize();
> > +     if (ret != PFM_SUCCESS) {
> > +             ui__warning("libpfm failed to initialize: %s\n",
> > +                     pfm_strerror(ret));
> > +     }
> > +}
> > +
> > +int parse_libpfm_events_option(const struct option *opt, const char *str,
> > +                     int unset __maybe_unused)
> > +{
> > +     struct evlist *evlist = *(struct evlist **)opt->value;
> > +     struct perf_event_attr attr;
> > +     struct perf_pmu *pmu;
> > +     struct evsel *evsel, *grp_leader = NULL;
> > +     char *p, *q, *p_orig;
> > +     const char *sep;
> > +     int grp_evt = -1;
> > +     int ret;
> > +
> > +     p_orig = p = strdup(str);
> > +     if (!p)
> > +             return -1;
> > +     /*
> > +      * force loading of the PMU list
> > +      */
> > +     perf_pmu__scan(NULL);
> > +
> > +     for (q = p; strsep(&p, ",{}"); q = p) {
> > +             sep = p ? str + (p - p_orig - 1) : "";
> > +             if (*sep == '{') {
> > +                     if (grp_evt > -1) {
> > +                             ui__error(
> > +                                     "nested event groups not supported\n");
> > +                             goto error;
> > +                     }
> > +                     grp_evt++;
> > +             }
> > +
> > +             /* no event */
> > +             if (*q == '\0')
> > +                     continue;
> > +
> > +             memset(&attr, 0, sizeof(attr));
> > +             event_attr_init(&attr);
> > +
> > +             ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
> > +                                             &attr, NULL, NULL);
> > +
> > +             if (ret != PFM_SUCCESS) {
> > +                     ui__error("failed to parse event %s : %s\n", str,
> > +                               pfm_strerror(ret));
> > +                     goto error;
> > +             }
> > +
> > +             pmu = perf_pmu__find_by_type((unsigned int)attr.type);
> > +             evsel = parse_events__add_event(evlist->core.nr_entries,
> > +                                             &attr, q, pmu);
> > +             if (evsel == NULL)
> > +                     goto error;
> > +
> > +             evsel->is_libpfm_event = true;
> > +
> > +             evlist__add(evlist, evsel);
> > +
> > +             if (grp_evt == 0)
> > +                     grp_leader = evsel;
> > +
> > +             if (grp_evt > -1) {
> > +                     evsel->leader = grp_leader;
> > +                     grp_leader->core.nr_members++;
> > +                     grp_evt++;
> > +             }
> > +
> > +             if (*sep == '}') {
> > +                     if (grp_evt < 0) {
> > +                             ui__error(
> > +                                "cannot close a non-existing event group\n");
> > +                             goto error;
> > +                     }
> > +                     evlist->nr_groups++;
> > +                     grp_leader = NULL;
> > +                     grp_evt = -1;
> > +             }
> > +     }
> > +     return 0;
> > +error:
> > +     free(p_orig);
> > +     return -1;
> > +}
> > +
> > +static const char *srcs[PFM_ATTR_CTRL_MAX] = {
> > +     [PFM_ATTR_CTRL_UNKNOWN] = "???",
> > +     [PFM_ATTR_CTRL_PMU] = "PMU",
> > +     [PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
> > +};
> > +
> > +static void
> > +print_attr_flags(pfm_event_attr_info_t *info)
> > +{
> > +     int n = 0;
> > +
> > +     if (info->is_dfl) {
> > +             printf("[default] ");
> > +             n++;
> > +     }
> > +
> > +     if (info->is_precise) {
> > +             printf("[precise] ");
> > +             n++;
> > +     }
> > +
> > +     if (!n)
> > +             printf("- ");
> > +}
> > +
> > +static void
> > +print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc)
> > +{
> > +     pfm_event_attr_info_t ainfo;
> > +     const char *src;
> > +     int j, ret;
> > +
> > +     ainfo.size = sizeof(ainfo);
> > +
> > +     printf("  %s\n", info->name);
> > +     printf("    [%s]\n", info->desc);
> > +     if (long_desc) {
> > +             if (info->equiv)
> > +                     printf("      Equiv: %s\n", info->equiv);
> > +
> > +             printf("      Code  : 0x%"PRIx64"\n", info->code);
> > +     }
> > +     pfm_for_each_event_attr(j, info) {
> > +             ret = pfm_get_event_attr_info(info->idx, j,
> > +                                           PFM_OS_PERF_EVENT_EXT, &ainfo);
> > +             if (ret != PFM_SUCCESS)
> > +                     continue;
> > +
> > +             if (ainfo.type == PFM_ATTR_UMASK) {
> > +                     printf("      %s:%s\n", info->name, ainfo.name);
> > +                     printf("        [%s]\n", ainfo.desc);
> > +             }
> > +
> > +             if (!long_desc)
> > +                     continue;
> > +
> > +             if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
> > +                     ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
> > +
> > +             src = srcs[ainfo.ctrl];
> > +             switch (ainfo.type) {
> > +             case PFM_ATTR_UMASK:
> > +                     printf("        Umask : 0x%02"PRIx64" : %s: ",
> > +                             ainfo.code, src);
> > +                     print_attr_flags(&ainfo);
> > +                     putchar('\n');
> > +                     break;
> > +             case PFM_ATTR_MOD_BOOL:
> > +                     printf("      Modif : %s: [%s] : %s (boolean)\n", src,
> > +                             ainfo.name, ainfo.desc);
> > +                     break;
> > +             case PFM_ATTR_MOD_INTEGER:
> > +                     printf("      Modif : %s: [%s] : %s (integer)\n", src,
> > +                             ainfo.name, ainfo.desc);
> > +                     break;
> > +             case PFM_ATTR_NONE:
> > +             case PFM_ATTR_RAW_UMASK:
> > +             case PFM_ATTR_MAX:
> > +             default:
> > +                     printf("      Attr  : %s: [%s] : %s\n", src,
> > +                             ainfo.name, ainfo.desc);
> > +             }
> > +     }
> > +}
> > +
> > +/*
> > + * list all pmu::event:umask, pmu::event
> > + * printed events may not be all valid combinations of umask for an event
> > + */
> > +static void
> > +print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info)
> > +{
> > +     pfm_event_attr_info_t ainfo;
> > +     int j, ret;
> > +     bool has_umask = false;
> > +
> > +     ainfo.size = sizeof(ainfo);
> > +
> > +     pfm_for_each_event_attr(j, info) {
> > +             ret = pfm_get_event_attr_info(info->idx, j,
> > +                                           PFM_OS_PERF_EVENT_EXT, &ainfo);
> > +             if (ret != PFM_SUCCESS)
> > +                     continue;
> > +
> > +             if (ainfo.type != PFM_ATTR_UMASK)
> > +                     continue;
> > +
> > +             printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name);
> > +             has_umask = true;
> > +     }
> > +     if (!has_umask)
> > +             printf("%s::%s\n", pinfo->name, info->name);
> > +}
> > +
> > +void print_libpfm_events(bool name_only, bool long_desc)
> > +{
> > +     pfm_event_info_t info;
> > +     pfm_pmu_info_t pinfo;
> > +     int i, p, ret;
> > +
> > +     /* initialize to zero to indicate ABI version */
> > +     info.size  = sizeof(info);
> > +     pinfo.size = sizeof(pinfo);
> > +
> > +     if (!name_only)
> > +             puts("\nList of pre-defined events (to be used in --pfm-events):\n");
> > +
> > +     pfm_for_all_pmus(p) {
> > +             bool printed_pmu = false;
> > +
> > +             ret = pfm_get_pmu_info(p, &pinfo);
> > +             if (ret != PFM_SUCCESS)
> > +                     continue;
> > +
> > +             /* only print events that are supported by host HW */
> > +             if (!pinfo.is_present)
> > +                     continue;
> > +
> > +             /* handled by perf directly */
> > +             if (pinfo.pmu == PFM_PMU_PERF_EVENT)
> > +                     continue;
> > +
> > +             for (i = pinfo.first_event; i != -1;
> > +                  i = pfm_get_event_next(i)) {
> > +
> > +                     ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
> > +                                             &info);
> > +                     if (ret != PFM_SUCCESS)
> > +                             continue;
> > +
> > +                     if (!name_only && !printed_pmu) {
> > +                             printf("%s:\n", pinfo.name);
> > +                             printed_pmu = true;
> > +                     }
> > +
> > +                     if (!name_only)
> > +                             print_libpfm_events_detailed(&info, long_desc);
> > +                     else
> > +                             print_libpfm_events_raw(&pinfo, &info);
> > +             }
> > +             if (!name_only && printed_pmu)
> > +                     putchar('\n');
> > +     }
> > +}
> > diff --git a/tools/perf/util/pfm.h b/tools/perf/util/pfm.h
> > new file mode 100644
> > index 000000000000..532dafe88bc0
> > --- /dev/null
> > +++ b/tools/perf/util/pfm.h
> > @@ -0,0 +1,43 @@
> > +/* SPDX-License-Identifier: GPL-2.0 */
> > +/*
> > + * Support for libpfm4 event encoding.
> > + *
> > + * Copyright 2020 Google LLC.
> > + */
> > +#ifndef __PERF_PFM_H
> > +#define __PERF_PFM_H
> > +
> > +#include <subcmd/parse-options.h>
> > +
> > +#ifdef HAVE_LIBPFM
> > +void libpfm_initialize(void);
> > +
> > +int parse_libpfm_events_option(const struct option *opt, const char *str,
> > +                     int unset);
> > +
> > +void print_libpfm_events(bool name_only, bool long_desc);
> > +
> > +#else
> > +#include <linux/compiler.h>
> > +
> > +static inline void libpfm_initialize(void)
> > +{
> > +}
> > +
> > +static inline int parse_libpfm_events_option(
> > +     const struct option *opt __maybe_unused,
> > +     const char *str __maybe_unused,
> > +     int unset __maybe_unused)
> > +{
> > +     return 0;
> > +}
> > +
> > +static inline void print_libpfm_events(bool name_only __maybe_unused,
> > +                                    bool long_desc __maybe_unused)
> > +{
> > +}
> > +
> > +#endif
> > +
> > +
> > +#endif /* __PERF_PFM_H */
> > --
> > 2.26.2.303.gf8c07b1a785-goog
> >
>
> --
>
> - Arnaldo

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

* Re: [PATCH v12 4/4] perf tools: add support for libpfm4
  2020-04-30 14:26       ` Ian Rogers
@ 2020-05-05 18:31         ` Ian Rogers
  -1 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2020-05-05 18:31 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Yonghong Song, Andrii Nakryiko,
	Greg Kroah-Hartman, Thomas Gleixner, Igor Lubashev,
	Alexey Budankov, Florian Fainelli, Adrian Hunter, Andi Kleen,
	Jiwei Sun, yuzhoujian, Kan Liang, Jin Yao, Leo Yan, John Garry,
	LKML, netdev, bpf, linux-perf-users, Stephane Eranian

On Thu, Apr 30, 2020 at 7:26 AM Ian Rogers <irogers@google.com> wrote:
>
> On Thu, Apr 30, 2020 at 6:34 AM Arnaldo Carvalho de Melo
> <arnaldo.melo@gmail.com> wrote:
> >
> > Em Wed, Apr 29, 2020 at 04:14:43PM -0700, Ian Rogers escreveu:
> > > From: Stephane Eranian <eranian@google.com>
> > >
> > > This patch links perf with the libpfm4 library if it is available
> > > and LIBPFM4 is passed to the build. The libpfm4 library
> > > contains hardware event tables for all processors supported by
> > > perf_events. It is a helper library that helps convert from a
> > > symbolic event name to the event encoding required by the
> > > underlying kernel interface. This library is open-source and
> > > available from: http://perfmon2.sf.net.
> >
> > Hey, I think we should not call libpfm_initialize() in all tools, better
> > leave it to when the --pfm-event callback gets called, because the user
> > is really using that event syntax, i.e. do it lazily, to reduce the
> > amount of initialization that perf does, which is already excessive and
> > needs triming down, to do more things only when they are really, really
> > needed.
> >
> > I have applied the previous, preparatory patches.
>
> Thanks! I didn't see the whole set rebasing on:
> https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/log/?h=perf/core
> and so sent it all again:
> https://lore.kernel.org/lkml/20200430142419.252180-5-irogers@google.com/T/#u

I now see the preparatory patches and so I've re-sent this patch
rebased on its own:
https://lore.kernel.org/lkml/20200505182943.218248-1-irogers@google.com/T/#t

Thanks,
Ian

> Thanks,
> Ian
>
> > - Arnaldo
> >
> > > With this patch, it is possible to specify full hardware events
> > > by name. Hardware filters are also supported. Events must be
> > > specified via the --pfm-events and not -e option. Both options
> > > are active at the same time and it is possible to mix and match:
> > >
> > > $ perf stat --pfm-events inst_retired:any_p:c=1:i -e cycles ....
> > >
> > > Signed-off-by: Stephane Eranian <eranian@google.com>
> > > Reviewed-by: Ian Rogers <irogers@google.com>
> > > ---
> > >  tools/perf/Documentation/perf-record.txt |  11 +
> > >  tools/perf/Documentation/perf-stat.txt   |  10 +
> > >  tools/perf/Documentation/perf-top.txt    |  11 +
> > >  tools/perf/Makefile.config               |  13 ++
> > >  tools/perf/Makefile.perf                 |   2 +
> > >  tools/perf/builtin-list.c                |   3 +
> > >  tools/perf/builtin-record.c              |   8 +
> > >  tools/perf/builtin-stat.c                |   8 +
> > >  tools/perf/builtin-top.c                 |   8 +
> > >  tools/perf/tests/Build                   |   1 +
> > >  tools/perf/tests/builtin-test.c          |   9 +
> > >  tools/perf/tests/pfm.c                   | 207 +++++++++++++++++
> > >  tools/perf/tests/tests.h                 |   3 +
> > >  tools/perf/util/Build                    |   2 +
> > >  tools/perf/util/evsel.c                  |   2 +-
> > >  tools/perf/util/evsel.h                  |   1 +
> > >  tools/perf/util/parse-events.c           |  30 ++-
> > >  tools/perf/util/parse-events.h           |   4 +
> > >  tools/perf/util/pfm.c                    | 277 +++++++++++++++++++++++
> > >  tools/perf/util/pfm.h                    |  43 ++++
> > >  20 files changed, 645 insertions(+), 8 deletions(-)
> > >  create mode 100644 tools/perf/tests/pfm.c
> > >  create mode 100644 tools/perf/util/pfm.c
> > >  create mode 100644 tools/perf/util/pfm.h
> > >
> > > diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> > > index b3f3b3f1c161..ad2c41f595c2 100644
> > > --- a/tools/perf/Documentation/perf-record.txt
> > > +++ b/tools/perf/Documentation/perf-record.txt
> > > @@ -596,6 +596,17 @@ Make a copy of /proc/kcore and place it into a directory with the perf data file
> > >  Limit the sample data max size, <size> is expected to be a number with
> > >  appended unit character - B/K/M/G
> > >
> > > +ifdef::HAVE_LIBPFM[]
> > > +--pfm-events events::
> > > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > > +including support for event filters. For example '--pfm-events
> > > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > > +option using the comma separator. Hardware events and generic hardware
> > > +events cannot be mixed together. The latter must be used with the -e
> > > +option. The -e option and this one can be mixed and matched.  Events
> > > +can be grouped using the {} notation.
> > > +endif::HAVE_LIBPFM[]
> > > +
> > >  SEE ALSO
> > >  --------
> > >  linkperf:perf-stat[1], linkperf:perf-list[1], linkperf:perf-intel-pt[1]
> > > diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
> > > index 4d56586b2fb9..536952ad592c 100644
> > > --- a/tools/perf/Documentation/perf-stat.txt
> > > +++ b/tools/perf/Documentation/perf-stat.txt
> > > @@ -71,6 +71,16 @@ report::
> > >  --tid=<tid>::
> > >          stat events on existing thread id (comma separated list)
> > >
> > > +ifdef::HAVE_LIBPFM[]
> > > +--pfm-events events::
> > > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > > +including support for event filters. For example '--pfm-events
> > > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > > +option using the comma separator. Hardware events and generic hardware
> > > +events cannot be mixed together. The latter must be used with the -e
> > > +option. The -e option and this one can be mixed and matched.  Events
> > > +can be grouped using the {} notation.
> > > +endif::HAVE_LIBPFM[]
> > >
> > >  -a::
> > >  --all-cpus::
> > > diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt
> > > index 20227dabc208..ee2024691d46 100644
> > > --- a/tools/perf/Documentation/perf-top.txt
> > > +++ b/tools/perf/Documentation/perf-top.txt
> > > @@ -329,6 +329,17 @@ Default is to monitor all CPUS.
> > >       The known limitations include exception handing such as
> > >       setjmp/longjmp will have calls/returns not match.
> > >
> > > +ifdef::HAVE_LIBPFM[]
> > > +--pfm-events events::
> > > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > > +including support for event filters. For example '--pfm-events
> > > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > > +option using the comma separator. Hardware events and generic hardware
> > > +events cannot be mixed together. The latter must be used with the -e
> > > +option. The -e option and this one can be mixed and matched.  Events
> > > +can be grouped using the {} notation.
> > > +endif::HAVE_LIBPFM[]
> > > +
> > >  INTERACTIVE PROMPTING KEYS
> > >  --------------------------
> > >
> > > diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> > > index 12a8204d63c6..b67804fee1e3 100644
> > > --- a/tools/perf/Makefile.config
> > > +++ b/tools/perf/Makefile.config
> > > @@ -1012,6 +1012,19 @@ ifdef LIBCLANGLLVM
> > >    endif
> > >  endif
> > >
> > > +ifdef LIBPFM4
> > > +  $(call feature_check,libpfm4)
> > > +  ifeq ($(feature-libpfm4), 1)
> > > +    CFLAGS += -DHAVE_LIBPFM
> > > +    EXTLIBS += -lpfm
> > > +    ASCIIDOC_EXTRA = -aHAVE_LIBPFM=1
> > > +    $(call detected,CONFIG_LIBPFM4)
> > > +  else
> > > +    msg := $(warning libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev);
> > > +    NO_LIBPFM4 := 1
> > > +  endif
> > > +endif
> > > +
> > >  # Among the variables below, these:
> > >  #   perfexecdir
> > >  #   perf_include_dir
> > > diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
> > > index 94a495594e99..dc82578c8773 100644
> > > --- a/tools/perf/Makefile.perf
> > > +++ b/tools/perf/Makefile.perf
> > > @@ -118,6 +118,8 @@ include ../scripts/utilities.mak
> > >  #
> > >  # Define LIBBPF_DYNAMIC to enable libbpf dynamic linking.
> > >  #
> > > +# Define LIBPFM4 to enable libpfm4 events extension.
> > > +#
> > >
> > >  # As per kernel Makefile, avoid funny character set dependencies
> > >  unexport LC_ALL
> > > diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
> > > index 965ef017496f..d2069bdcb469 100644
> > > --- a/tools/perf/builtin-list.c
> > > +++ b/tools/perf/builtin-list.c
> > > @@ -14,6 +14,7 @@
> > >  #include "util/pmu.h"
> > >  #include "util/debug.h"
> > >  #include "util/metricgroup.h"
> > > +#include "util/pfm.h"
> > >  #include <subcmd/pager.h>
> > >  #include <subcmd/parse-options.h>
> > >  #include <stdio.h>
> > > @@ -53,6 +54,8 @@ int cmd_list(int argc, const char **argv)
> > >
> > >       setup_pager();
> > >
> > > +     libpfm_initialize();
> > > +
> > >       if (!raw_dump && pager_in_use())
> > >               printf("\nList of pre-defined events (to be used in -e):\n\n");
> > >
> > > diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> > > index 1ab349abe904..fc55b641849f 100644
> > > --- a/tools/perf/builtin-record.c
> > > +++ b/tools/perf/builtin-record.c
> > > @@ -43,6 +43,7 @@
> > >  #include "util/time-utils.h"
> > >  #include "util/units.h"
> > >  #include "util/bpf-event.h"
> > > +#include "util/pfm.h"
> > >  #include "asm/bug.h"
> > >  #include "perf.h"
> > >
> > > @@ -2421,6 +2422,11 @@ static struct option __record_options[] = {
> > >  #endif
> > >       OPT_CALLBACK(0, "max-size", &record.output_max_size,
> > >                    "size", "Limit the maximum size of the output file", parse_output_max_size),
> > > +#ifdef HAVE_LIBPFM
> > > +     OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
> > > +             "libpfm4 event selector. use 'perf list' to list available events",
> > > +             parse_libpfm_events_option),
> > > +#endif
> > >       OPT_END()
> > >  };
> > >
> > > @@ -2461,6 +2467,8 @@ int cmd_record(int argc, const char **argv)
> > >       if (rec->evlist == NULL)
> > >               return -ENOMEM;
> > >
> > > +     libpfm_initialize();
> > > +
> > >       err = perf_config(perf_record_config, rec);
> > >       if (err)
> > >               return err;
> > > diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> > > index 9207b6c45475..607920321c15 100644
> > > --- a/tools/perf/builtin-stat.c
> > > +++ b/tools/perf/builtin-stat.c
> > > @@ -66,6 +66,7 @@
> > >  #include "util/time-utils.h"
> > >  #include "util/top.h"
> > >  #include "util/affinity.h"
> > > +#include "util/pfm.h"
> > >  #include "asm/bug.h"
> > >
> > >  #include <linux/time64.h>
> > > @@ -936,6 +937,11 @@ static struct option stat_options[] = {
> > >                   "Use with 'percore' event qualifier to show the event "
> > >                   "counts of one hardware thread by sum up total hardware "
> > >                   "threads of same physical core"),
> > > +#ifdef HAVE_LIBPFM
> > > +     OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
> > > +             "libpfm4 event selector. use 'perf list' to list available events",
> > > +             parse_libpfm_events_option),
> > > +#endif
> > >       OPT_END()
> > >  };
> > >
> > > @@ -1874,6 +1880,8 @@ int cmd_stat(int argc, const char **argv)
> > >       unsigned int interval, timeout;
> > >       const char * const stat_subcommands[] = { "record", "report" };
> > >
> > > +     libpfm_initialize();
> > > +
> > >       setlocale(LC_ALL, "");
> > >
> > >       evsel_list = evlist__new();
> > > diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
> > > index 6b067a5ba1d5..e1f477a18f93 100644
> > > --- a/tools/perf/builtin-top.c
> > > +++ b/tools/perf/builtin-top.c
> > > @@ -53,6 +53,7 @@
> > >
> > >  #include "util/debug.h"
> > >  #include "util/ordered-events.h"
> > > +#include "util/pfm.h"
> > >
> > >  #include <assert.h>
> > >  #include <elf.h>
> > > @@ -1577,6 +1578,11 @@ int cmd_top(int argc, const char **argv)
> > >                   "WARNING: should be used on grouped events."),
> > >       OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
> > >                   "Enable LBR callgraph stitching approach"),
> > > +#ifdef HAVE_LIBPFM
> > > +     OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
> > > +             "libpfm4 event selector. use 'perf list' to list available events",
> > > +             parse_libpfm_events_option),
> > > +#endif
> > >       OPTS_EVSWITCH(&top.evswitch),
> > >       OPT_END()
> > >       };
> > > @@ -1590,6 +1596,8 @@ int cmd_top(int argc, const char **argv)
> > >       if (status < 0)
> > >               return status;
> > >
> > > +     libpfm_initialize();
> > > +
> > >       top.annotation_opts.min_pcnt = 5;
> > >       top.annotation_opts.context  = 4;
> > >
> > > diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
> > > index b3d1bf13ca07..7d2ccec9fd75 100644
> > > --- a/tools/perf/tests/Build
> > > +++ b/tools/perf/tests/Build
> > > @@ -56,6 +56,7 @@ perf-y += mem2node.o
> > >  perf-y += maps.o
> > >  perf-y += time-utils-test.o
> > >  perf-y += genelf.o
> > > +perf-y += pfm.o
> > >
> > >  $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
> > >       $(call rule_mkdir)
> > > diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
> > > index b6322eb0f423..8b323151f22c 100644
> > > --- a/tools/perf/tests/builtin-test.c
> > > +++ b/tools/perf/tests/builtin-test.c
> > > @@ -313,6 +313,15 @@ static struct test generic_tests[] = {
> > >               .desc = "maps__merge_in",
> > >               .func = test__maps__merge_in,
> > >       },
> > > +     {
> > > +             .desc = "Test libpfm4 support",
> > > +             .func = test__pfm,
> > > +             .subtest = {
> > > +                     .skip_if_fail   = true,
> > > +                     .get_nr         = test__pfm_subtest_get_nr,
> > > +                     .get_desc       = test__pfm_subtest_get_desc,
> > > +             }
> > > +     },
> > >       {
> > >               .func = NULL,
> > >       },
> > > diff --git a/tools/perf/tests/pfm.c b/tools/perf/tests/pfm.c
> > > new file mode 100644
> > > index 000000000000..1d594fd3d9c1
> > > --- /dev/null
> > > +++ b/tools/perf/tests/pfm.c
> > > @@ -0,0 +1,207 @@
> > > +// SPDX-License-Identifier: GPL-2.0
> > > +/*
> > > + * Test support for libpfm4 event encodings.
> > > + *
> > > + * Copyright 2020 Google LLC.
> > > + */
> > > +#include "tests.h"
> > > +#include "util/debug.h"
> > > +#include "util/evlist.h"
> > > +#include "util/pfm.h"
> > > +
> > > +#include <linux/kernel.h>
> > > +
> > > +#ifdef HAVE_LIBPFM
> > > +static int test__pfm_events(void);
> > > +static int test__pfm_group(void);
> > > +#endif
> > > +
> > > +static const struct {
> > > +     int (*func)(void);
> > > +     const char *desc;
> > > +} pfm_testcase_table[] = {
> > > +#ifdef HAVE_LIBPFM
> > > +     {
> > > +             .func = test__pfm_events,
> > > +             .desc = "test of individual --pfm-events",
> > > +     },
> > > +     {
> > > +             .func = test__pfm_group,
> > > +             .desc = "test groups of --pfm-events",
> > > +     },
> > > +#endif
> > > +};
> > > +
> > > +#ifdef HAVE_LIBPFM
> > > +static int count_pfm_events(struct perf_evlist *evlist)
> > > +{
> > > +     struct perf_evsel *evsel;
> > > +     int count = 0;
> > > +
> > > +     perf_evlist__for_each_entry(evlist, evsel) {
> > > +             count++;
> > > +     }
> > > +     return count;
> > > +}
> > > +
> > > +static int test__pfm_events(void)
> > > +{
> > > +     struct evlist *evlist;
> > > +     struct option opt;
> > > +     size_t i;
> > > +     const struct {
> > > +             const char *events;
> > > +             int nr_events;
> > > +     } table[] = {
> > > +             {
> > > +                     .events = "",
> > > +                     .nr_events = 0,
> > > +             },
> > > +             {
> > > +                     .events = "instructions",
> > > +                     .nr_events = 1,
> > > +             },
> > > +             {
> > > +                     .events = "instructions,cycles",
> > > +                     .nr_events = 2,
> > > +             },
> > > +             {
> > > +                     .events = "stereolab",
> > > +                     .nr_events = 0,
> > > +             },
> > > +             {
> > > +                     .events = "instructions,instructions",
> > > +                     .nr_events = 2,
> > > +             },
> > > +             {
> > > +                     .events = "stereolab,instructions",
> > > +                     .nr_events = 0,
> > > +             },
> > > +             {
> > > +                     .events = "instructions,stereolab",
> > > +                     .nr_events = 1,
> > > +             },
> > > +     };
> > > +
> > > +     libpfm_initialize();
> > > +
> > > +     for (i = 0; i < ARRAY_SIZE(table); i++) {
> > > +             evlist = evlist__new();
> > > +             if (evlist == NULL)
> > > +                     return -ENOMEM;
> > > +
> > > +             opt.value = evlist;
> > > +             parse_libpfm_events_option(&opt,
> > > +                                     table[i].events,
> > > +                                     0);
> > > +             TEST_ASSERT_EQUAL(table[i].events,
> > > +                             count_pfm_events(&evlist->core),
> > > +                             table[i].nr_events);
> > > +             TEST_ASSERT_EQUAL(table[i].events,
> > > +                             evlist->nr_groups,
> > > +                             0);
> > > +
> > > +             evlist__delete(evlist);
> > > +     }
> > > +     return 0;
> > > +}
> > > +
> > > +static int test__pfm_group(void)
> > > +{
> > > +     struct evlist *evlist;
> > > +     struct option opt;
> > > +     size_t i;
> > > +     const struct {
> > > +             const char *events;
> > > +             int nr_events;
> > > +             int nr_groups;
> > > +     } table[] = {
> > > +             {
> > > +                     .events = "{},",
> > > +                     .nr_events = 0,
> > > +                     .nr_groups = 0,
> > > +             },
> > > +             {
> > > +                     .events = "{instructions}",
> > > +                     .nr_events = 1,
> > > +                     .nr_groups = 1,
> > > +             },
> > > +             {
> > > +                     .events = "{instructions},{}",
> > > +                     .nr_events = 1,
> > > +                     .nr_groups = 1,
> > > +             },
> > > +             {
> > > +                     .events = "{},{instructions}",
> > > +                     .nr_events = 0,
> > > +                     .nr_groups = 0,
> > > +             },
> > > +             {
> > > +                     .events = "{instructions},{instructions}",
> > > +                     .nr_events = 2,
> > > +                     .nr_groups = 2,
> > > +             },
> > > +             {
> > > +                     .events = "{instructions,cycles},{instructions,cycles}",
> > > +                     .nr_events = 4,
> > > +                     .nr_groups = 2,
> > > +             },
> > > +             {
> > > +                     .events = "{stereolab}",
> > > +                     .nr_events = 0,
> > > +                     .nr_groups = 0,
> > > +             },
> > > +             {
> > > +                     .events =
> > > +                     "{instructions,cycles},{instructions,stereolab}",
> > > +                     .nr_events = 3,
> > > +                     .nr_groups = 1,
> > > +             },
> > > +     };
> > > +
> > > +     libpfm_initialize();
> > > +
> > > +     for (i = 0; i < ARRAY_SIZE(table); i++) {
> > > +             evlist = evlist__new();
> > > +             if (evlist == NULL)
> > > +                     return -ENOMEM;
> > > +
> > > +             opt.value = evlist;
> > > +             parse_libpfm_events_option(&opt,
> > > +                                     table[i].events,
> > > +                                     0);
> > > +             TEST_ASSERT_EQUAL(table[i].events,
> > > +                             count_pfm_events(&evlist->core),
> > > +                             table[i].nr_events);
> > > +             TEST_ASSERT_EQUAL(table[i].events,
> > > +                             evlist->nr_groups,
> > > +                             table[i].nr_groups);
> > > +
> > > +             evlist__delete(evlist);
> > > +     }
> > > +     return 0;
> > > +}
> > > +#endif
> > > +
> > > +const char *test__pfm_subtest_get_desc(int i)
> > > +{
> > > +     if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> > > +             return NULL;
> > > +     return pfm_testcase_table[i].desc;
> > > +}
> > > +
> > > +int test__pfm_subtest_get_nr(void)
> > > +{
> > > +     return (int)ARRAY_SIZE(pfm_testcase_table);
> > > +}
> > > +
> > > +int test__pfm(struct test *test __maybe_unused, int i __maybe_unused)
> > > +{
> > > +#ifdef HAVE_LIBPFM
> > > +     if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> > > +             return TEST_FAIL;
> > > +     return pfm_testcase_table[i].func();
> > > +#else
> > > +     return TEST_SKIP;
> > > +#endif
> > > +}
> > > diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
> > > index 61a1ab032080..47cbae753b42 100644
> > > --- a/tools/perf/tests/tests.h
> > > +++ b/tools/perf/tests/tests.h
> > > @@ -112,6 +112,9 @@ int test__mem2node(struct test *t, int subtest);
> > >  int test__maps__merge_in(struct test *t, int subtest);
> > >  int test__time_utils(struct test *t, int subtest);
> > >  int test__jit_write_elf(struct test *test, int subtest);
> > > +int test__pfm(struct test *test, int subtest);
> > > +const char *test__pfm_subtest_get_desc(int subtest);
> > > +int test__pfm_subtest_get_nr(void);
> > >
> > >  bool test__bp_signal_is_supported(void);
> > >  bool test__bp_account_is_supported(void);
> > > diff --git a/tools/perf/util/Build b/tools/perf/util/Build
> > > index c0cf8dff694e..afd3d2221c83 100644
> > > --- a/tools/perf/util/Build
> > > +++ b/tools/perf/util/Build
> > > @@ -177,6 +177,8 @@ perf-$(CONFIG_LIBBPF) += bpf-event.o
> > >
> > >  perf-$(CONFIG_CXX) += c++/
> > >
> > > +perf-$(CONFIG_LIBPFM4) += pfm.o
> > > +
> > >  CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
> > >  CFLAGS_llvm-utils.o += -DPERF_INCLUDE_DIR="BUILD_STR($(perf_include_dir_SQ))"
> > >
> > > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> > > index 6a571d322bb2..a5c88d52c11c 100644
> > > --- a/tools/perf/util/evsel.c
> > > +++ b/tools/perf/util/evsel.c
> > > @@ -2433,7 +2433,7 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
> > >
> > >               /* Is there already the separator in the name. */
> > >               if (strchr(name, '/') ||
> > > -                 strchr(name, ':'))
> > > +                 (strchr(name, ':') && !evsel->is_libpfm_event))
> > >                       sep = "";
> > >
> > >               if (asprintf(&new_name, "%s%su", name, sep) < 0)
> > > diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> > > index a463bc65b001..709a9d494767 100644
> > > --- a/tools/perf/util/evsel.h
> > > +++ b/tools/perf/util/evsel.h
> > > @@ -76,6 +76,7 @@ struct evsel {
> > >       bool                    ignore_missing_thread;
> > >       bool                    forced_leader;
> > >       bool                    use_uncore_alias;
> > > +     bool                    is_libpfm_event;
> > >       /* parse modifier helper */
> > >       int                     exclude_GH;
> > >       int                     sample_read;
> > > diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> > > index 10107747b361..e7e0faf36e3c 100644
> > > --- a/tools/perf/util/parse-events.c
> > > +++ b/tools/perf/util/parse-events.c
> > > @@ -36,6 +36,7 @@
> > >  #include "metricgroup.h"
> > >  #include "util/evsel_config.h"
> > >  #include "util/event.h"
> > > +#include "util/pfm.h"
> > >
> > >  #define MAX_NAME_LEN 100
> > >
> > > @@ -344,6 +345,7 @@ static char *get_config_name(struct list_head *head_terms)
> > >  static struct evsel *
> > >  __add_event(struct list_head *list, int *idx,
> > >           struct perf_event_attr *attr,
> > > +         bool init_attr,
> > >           char *name, struct perf_pmu *pmu,
> > >           struct list_head *config_terms, bool auto_merge_stats,
> > >           const char *cpu_list)
> > > @@ -352,7 +354,8 @@ __add_event(struct list_head *list, int *idx,
> > >       struct perf_cpu_map *cpus = pmu ? pmu->cpus :
> > >                              cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
> > >
> > > -     event_attr_init(attr);
> > > +     if (init_attr)
> > > +             event_attr_init(attr);
> > >
> > >       evsel = perf_evsel__new_idx(attr, *idx);
> > >       if (!evsel)
> > > @@ -370,15 +373,25 @@ __add_event(struct list_head *list, int *idx,
> > >       if (config_terms)
> > >               list_splice(config_terms, &evsel->config_terms);
> > >
> > > -     list_add_tail(&evsel->core.node, list);
> > > +     if (list)
> > > +             list_add_tail(&evsel->core.node, list);
> > > +
> > >       return evsel;
> > >  }
> > >
> > > +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> > > +                                     char *name, struct perf_pmu *pmu)
> > > +{
> > > +     return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
> > > +                        NULL);
> > > +}
> > > +
> > >  static int add_event(struct list_head *list, int *idx,
> > >                    struct perf_event_attr *attr, char *name,
> > >                    struct list_head *config_terms)
> > >  {
> > > -     return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
> > > +     return __add_event(list, idx, attr, true, name, NULL, config_terms,
> > > +                        false, NULL) ? 0 : -ENOMEM;
> > >  }
> > >
> > >  static int add_event_tool(struct list_head *list, int *idx,
> > > @@ -390,7 +403,8 @@ static int add_event_tool(struct list_head *list, int *idx,
> > >               .config = PERF_COUNT_SW_DUMMY,
> > >       };
> > >
> > > -     evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
> > > +     evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
> > > +                         "0");
> > >       if (!evsel)
> > >               return -ENOMEM;
> > >       evsel->tool_event = tool_event;
> > > @@ -1446,8 +1460,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> > >
> > >       if (!head_config) {
> > >               attr.type = pmu->type;
> > > -             evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
> > > -                                 auto_merge_stats, NULL);
> > > +             evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
> > > +                                 pmu, NULL, auto_merge_stats, NULL);
> > >               if (evsel) {
> > >                       evsel->pmu_name = name ? strdup(name) : NULL;
> > >                       evsel->use_uncore_alias = use_uncore_alias;
> > > @@ -1487,7 +1501,7 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> > >               return -EINVAL;
> > >       }
> > >
> > > -     evsel = __add_event(list, &parse_state->idx, &attr,
> > > +     evsel = __add_event(list, &parse_state->idx, &attr, true,
> > >                           get_config_name(head_config), pmu,
> > >                           &config_terms, auto_merge_stats, NULL);
> > >       if (evsel) {
> > > @@ -2794,6 +2808,8 @@ void print_events(const char *event_glob, bool name_only, bool quiet_flag,
> > >       print_sdt_events(NULL, NULL, name_only);
> > >
> > >       metricgroup__print(true, true, NULL, name_only, details_flag);
> > > +
> > > +     print_libpfm_events(name_only, long_desc);
> > >  }
> > >
> > >  int parse_events__is_hardcoded_term(struct parse_events_term *term)
> > > diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
> > > index 27596cbd0ba0..f7f555b1ca35 100644
> > > --- a/tools/perf/util/parse-events.h
> > > +++ b/tools/perf/util/parse-events.h
> > > @@ -17,6 +17,7 @@ struct evlist;
> > >  struct parse_events_error;
> > >
> > >  struct option;
> > > +struct perf_pmu;
> > >
> > >  struct tracepoint_path {
> > >       char *system;
> > > @@ -186,6 +187,9 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> > >                        bool auto_merge_stats,
> > >                        bool use_alias);
> > >
> > > +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> > > +                                     char *name, struct perf_pmu *pmu);
> > > +
> > >  int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
> > >                              char *str,
> > >                              struct list_head **listp);
> > > diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c
> > > new file mode 100644
> > > index 000000000000..4b11adf832a0
> > > --- /dev/null
> > > +++ b/tools/perf/util/pfm.c
> > > @@ -0,0 +1,277 @@
> > > +// SPDX-License-Identifier: GPL-2.0
> > > +/*
> > > + * Support for libpfm4 event encoding.
> > > + *
> > > + * Copyright 2020 Google LLC.
> > > + */
> > > +#include "util/cpumap.h"
> > > +#include "util/debug.h"
> > > +#include "util/event.h"
> > > +#include "util/evlist.h"
> > > +#include "util/evsel.h"
> > > +#include "util/parse-events.h"
> > > +#include "util/pmu.h"
> > > +#include "util/pfm.h"
> > > +
> > > +#include <string.h>
> > > +#include <linux/kernel.h>
> > > +#include <perfmon/pfmlib_perf_event.h>
> > > +
> > > +void libpfm_initialize(void)
> > > +{
> > > +     int ret;
> > > +
> > > +     ret = pfm_initialize();
> > > +     if (ret != PFM_SUCCESS) {
> > > +             ui__warning("libpfm failed to initialize: %s\n",
> > > +                     pfm_strerror(ret));
> > > +     }
> > > +}
> > > +
> > > +int parse_libpfm_events_option(const struct option *opt, const char *str,
> > > +                     int unset __maybe_unused)
> > > +{
> > > +     struct evlist *evlist = *(struct evlist **)opt->value;
> > > +     struct perf_event_attr attr;
> > > +     struct perf_pmu *pmu;
> > > +     struct evsel *evsel, *grp_leader = NULL;
> > > +     char *p, *q, *p_orig;
> > > +     const char *sep;
> > > +     int grp_evt = -1;
> > > +     int ret;
> > > +
> > > +     p_orig = p = strdup(str);
> > > +     if (!p)
> > > +             return -1;
> > > +     /*
> > > +      * force loading of the PMU list
> > > +      */
> > > +     perf_pmu__scan(NULL);
> > > +
> > > +     for (q = p; strsep(&p, ",{}"); q = p) {
> > > +             sep = p ? str + (p - p_orig - 1) : "";
> > > +             if (*sep == '{') {
> > > +                     if (grp_evt > -1) {
> > > +                             ui__error(
> > > +                                     "nested event groups not supported\n");
> > > +                             goto error;
> > > +                     }
> > > +                     grp_evt++;
> > > +             }
> > > +
> > > +             /* no event */
> > > +             if (*q == '\0')
> > > +                     continue;
> > > +
> > > +             memset(&attr, 0, sizeof(attr));
> > > +             event_attr_init(&attr);
> > > +
> > > +             ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
> > > +                                             &attr, NULL, NULL);
> > > +
> > > +             if (ret != PFM_SUCCESS) {
> > > +                     ui__error("failed to parse event %s : %s\n", str,
> > > +                               pfm_strerror(ret));
> > > +                     goto error;
> > > +             }
> > > +
> > > +             pmu = perf_pmu__find_by_type((unsigned int)attr.type);
> > > +             evsel = parse_events__add_event(evlist->core.nr_entries,
> > > +                                             &attr, q, pmu);
> > > +             if (evsel == NULL)
> > > +                     goto error;
> > > +
> > > +             evsel->is_libpfm_event = true;
> > > +
> > > +             evlist__add(evlist, evsel);
> > > +
> > > +             if (grp_evt == 0)
> > > +                     grp_leader = evsel;
> > > +
> > > +             if (grp_evt > -1) {
> > > +                     evsel->leader = grp_leader;
> > > +                     grp_leader->core.nr_members++;
> > > +                     grp_evt++;
> > > +             }
> > > +
> > > +             if (*sep == '}') {
> > > +                     if (grp_evt < 0) {
> > > +                             ui__error(
> > > +                                "cannot close a non-existing event group\n");
> > > +                             goto error;
> > > +                     }
> > > +                     evlist->nr_groups++;
> > > +                     grp_leader = NULL;
> > > +                     grp_evt = -1;
> > > +             }
> > > +     }
> > > +     return 0;
> > > +error:
> > > +     free(p_orig);
> > > +     return -1;
> > > +}
> > > +
> > > +static const char *srcs[PFM_ATTR_CTRL_MAX] = {
> > > +     [PFM_ATTR_CTRL_UNKNOWN] = "???",
> > > +     [PFM_ATTR_CTRL_PMU] = "PMU",
> > > +     [PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
> > > +};
> > > +
> > > +static void
> > > +print_attr_flags(pfm_event_attr_info_t *info)
> > > +{
> > > +     int n = 0;
> > > +
> > > +     if (info->is_dfl) {
> > > +             printf("[default] ");
> > > +             n++;
> > > +     }
> > > +
> > > +     if (info->is_precise) {
> > > +             printf("[precise] ");
> > > +             n++;
> > > +     }
> > > +
> > > +     if (!n)
> > > +             printf("- ");
> > > +}
> > > +
> > > +static void
> > > +print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc)
> > > +{
> > > +     pfm_event_attr_info_t ainfo;
> > > +     const char *src;
> > > +     int j, ret;
> > > +
> > > +     ainfo.size = sizeof(ainfo);
> > > +
> > > +     printf("  %s\n", info->name);
> > > +     printf("    [%s]\n", info->desc);
> > > +     if (long_desc) {
> > > +             if (info->equiv)
> > > +                     printf("      Equiv: %s\n", info->equiv);
> > > +
> > > +             printf("      Code  : 0x%"PRIx64"\n", info->code);
> > > +     }
> > > +     pfm_for_each_event_attr(j, info) {
> > > +             ret = pfm_get_event_attr_info(info->idx, j,
> > > +                                           PFM_OS_PERF_EVENT_EXT, &ainfo);
> > > +             if (ret != PFM_SUCCESS)
> > > +                     continue;
> > > +
> > > +             if (ainfo.type == PFM_ATTR_UMASK) {
> > > +                     printf("      %s:%s\n", info->name, ainfo.name);
> > > +                     printf("        [%s]\n", ainfo.desc);
> > > +             }
> > > +
> > > +             if (!long_desc)
> > > +                     continue;
> > > +
> > > +             if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
> > > +                     ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
> > > +
> > > +             src = srcs[ainfo.ctrl];
> > > +             switch (ainfo.type) {
> > > +             case PFM_ATTR_UMASK:
> > > +                     printf("        Umask : 0x%02"PRIx64" : %s: ",
> > > +                             ainfo.code, src);
> > > +                     print_attr_flags(&ainfo);
> > > +                     putchar('\n');
> > > +                     break;
> > > +             case PFM_ATTR_MOD_BOOL:
> > > +                     printf("      Modif : %s: [%s] : %s (boolean)\n", src,
> > > +                             ainfo.name, ainfo.desc);
> > > +                     break;
> > > +             case PFM_ATTR_MOD_INTEGER:
> > > +                     printf("      Modif : %s: [%s] : %s (integer)\n", src,
> > > +                             ainfo.name, ainfo.desc);
> > > +                     break;
> > > +             case PFM_ATTR_NONE:
> > > +             case PFM_ATTR_RAW_UMASK:
> > > +             case PFM_ATTR_MAX:
> > > +             default:
> > > +                     printf("      Attr  : %s: [%s] : %s\n", src,
> > > +                             ainfo.name, ainfo.desc);
> > > +             }
> > > +     }
> > > +}
> > > +
> > > +/*
> > > + * list all pmu::event:umask, pmu::event
> > > + * printed events may not be all valid combinations of umask for an event
> > > + */
> > > +static void
> > > +print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info)
> > > +{
> > > +     pfm_event_attr_info_t ainfo;
> > > +     int j, ret;
> > > +     bool has_umask = false;
> > > +
> > > +     ainfo.size = sizeof(ainfo);
> > > +
> > > +     pfm_for_each_event_attr(j, info) {
> > > +             ret = pfm_get_event_attr_info(info->idx, j,
> > > +                                           PFM_OS_PERF_EVENT_EXT, &ainfo);
> > > +             if (ret != PFM_SUCCESS)
> > > +                     continue;
> > > +
> > > +             if (ainfo.type != PFM_ATTR_UMASK)
> > > +                     continue;
> > > +
> > > +             printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name);
> > > +             has_umask = true;
> > > +     }
> > > +     if (!has_umask)
> > > +             printf("%s::%s\n", pinfo->name, info->name);
> > > +}
> > > +
> > > +void print_libpfm_events(bool name_only, bool long_desc)
> > > +{
> > > +     pfm_event_info_t info;
> > > +     pfm_pmu_info_t pinfo;
> > > +     int i, p, ret;
> > > +
> > > +     /* initialize to zero to indicate ABI version */
> > > +     info.size  = sizeof(info);
> > > +     pinfo.size = sizeof(pinfo);
> > > +
> > > +     if (!name_only)
> > > +             puts("\nList of pre-defined events (to be used in --pfm-events):\n");
> > > +
> > > +     pfm_for_all_pmus(p) {
> > > +             bool printed_pmu = false;
> > > +
> > > +             ret = pfm_get_pmu_info(p, &pinfo);
> > > +             if (ret != PFM_SUCCESS)
> > > +                     continue;
> > > +
> > > +             /* only print events that are supported by host HW */
> > > +             if (!pinfo.is_present)
> > > +                     continue;
> > > +
> > > +             /* handled by perf directly */
> > > +             if (pinfo.pmu == PFM_PMU_PERF_EVENT)
> > > +                     continue;
> > > +
> > > +             for (i = pinfo.first_event; i != -1;
> > > +                  i = pfm_get_event_next(i)) {
> > > +
> > > +                     ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
> > > +                                             &info);
> > > +                     if (ret != PFM_SUCCESS)
> > > +                             continue;
> > > +
> > > +                     if (!name_only && !printed_pmu) {
> > > +                             printf("%s:\n", pinfo.name);
> > > +                             printed_pmu = true;
> > > +                     }
> > > +
> > > +                     if (!name_only)
> > > +                             print_libpfm_events_detailed(&info, long_desc);
> > > +                     else
> > > +                             print_libpfm_events_raw(&pinfo, &info);
> > > +             }
> > > +             if (!name_only && printed_pmu)
> > > +                     putchar('\n');
> > > +     }
> > > +}
> > > diff --git a/tools/perf/util/pfm.h b/tools/perf/util/pfm.h
> > > new file mode 100644
> > > index 000000000000..532dafe88bc0
> > > --- /dev/null
> > > +++ b/tools/perf/util/pfm.h
> > > @@ -0,0 +1,43 @@
> > > +/* SPDX-License-Identifier: GPL-2.0 */
> > > +/*
> > > + * Support for libpfm4 event encoding.
> > > + *
> > > + * Copyright 2020 Google LLC.
> > > + */
> > > +#ifndef __PERF_PFM_H
> > > +#define __PERF_PFM_H
> > > +
> > > +#include <subcmd/parse-options.h>
> > > +
> > > +#ifdef HAVE_LIBPFM
> > > +void libpfm_initialize(void);
> > > +
> > > +int parse_libpfm_events_option(const struct option *opt, const char *str,
> > > +                     int unset);
> > > +
> > > +void print_libpfm_events(bool name_only, bool long_desc);
> > > +
> > > +#else
> > > +#include <linux/compiler.h>
> > > +
> > > +static inline void libpfm_initialize(void)
> > > +{
> > > +}
> > > +
> > > +static inline int parse_libpfm_events_option(
> > > +     const struct option *opt __maybe_unused,
> > > +     const char *str __maybe_unused,
> > > +     int unset __maybe_unused)
> > > +{
> > > +     return 0;
> > > +}
> > > +
> > > +static inline void print_libpfm_events(bool name_only __maybe_unused,
> > > +                                    bool long_desc __maybe_unused)
> > > +{
> > > +}
> > > +
> > > +#endif
> > > +
> > > +
> > > +#endif /* __PERF_PFM_H */
> > > --
> > > 2.26.2.303.gf8c07b1a785-goog
> > >
> >
> > --
> >
> > - Arnaldo

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

* Re: [PATCH v12 4/4] perf tools: add support for libpfm4
@ 2020-05-05 18:31         ` Ian Rogers
  0 siblings, 0 replies; 14+ messages in thread
From: Ian Rogers @ 2020-05-05 18:31 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Alexei Starovoitov, Daniel Borkmann,
	Martin KaFai Lau, Yonghong Song, Andrii Nakryiko,
	Greg Kroah-Hartman, Thomas Gleixner, Igor Lubashev,
	Alexey Budankov, Florian Fainelli, Adrian Hunter, Andi Kleen,
	Jiwei Sun, yuzhoujian

On Thu, Apr 30, 2020 at 7:26 AM Ian Rogers <irogers@google.com> wrote:
>
> On Thu, Apr 30, 2020 at 6:34 AM Arnaldo Carvalho de Melo
> <arnaldo.melo@gmail.com> wrote:
> >
> > Em Wed, Apr 29, 2020 at 04:14:43PM -0700, Ian Rogers escreveu:
> > > From: Stephane Eranian <eranian@google.com>
> > >
> > > This patch links perf with the libpfm4 library if it is available
> > > and LIBPFM4 is passed to the build. The libpfm4 library
> > > contains hardware event tables for all processors supported by
> > > perf_events. It is a helper library that helps convert from a
> > > symbolic event name to the event encoding required by the
> > > underlying kernel interface. This library is open-source and
> > > available from: http://perfmon2.sf.net.
> >
> > Hey, I think we should not call libpfm_initialize() in all tools, better
> > leave it to when the --pfm-event callback gets called, because the user
> > is really using that event syntax, i.e. do it lazily, to reduce the
> > amount of initialization that perf does, which is already excessive and
> > needs triming down, to do more things only when they are really, really
> > needed.
> >
> > I have applied the previous, preparatory patches.
>
> Thanks! I didn't see the whole set rebasing on:
> https://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git/log/?h=perf/core
> and so sent it all again:
> https://lore.kernel.org/lkml/20200430142419.252180-5-irogers@google.com/T/#u

I now see the preparatory patches and so I've re-sent this patch
rebased on its own:
https://lore.kernel.org/lkml/20200505182943.218248-1-irogers@google.com/T/#t

Thanks,
Ian

> Thanks,
> Ian
>
> > - Arnaldo
> >
> > > With this patch, it is possible to specify full hardware events
> > > by name. Hardware filters are also supported. Events must be
> > > specified via the --pfm-events and not -e option. Both options
> > > are active at the same time and it is possible to mix and match:
> > >
> > > $ perf stat --pfm-events inst_retired:any_p:c=1:i -e cycles ....
> > >
> > > Signed-off-by: Stephane Eranian <eranian@google.com>
> > > Reviewed-by: Ian Rogers <irogers@google.com>
> > > ---
> > >  tools/perf/Documentation/perf-record.txt |  11 +
> > >  tools/perf/Documentation/perf-stat.txt   |  10 +
> > >  tools/perf/Documentation/perf-top.txt    |  11 +
> > >  tools/perf/Makefile.config               |  13 ++
> > >  tools/perf/Makefile.perf                 |   2 +
> > >  tools/perf/builtin-list.c                |   3 +
> > >  tools/perf/builtin-record.c              |   8 +
> > >  tools/perf/builtin-stat.c                |   8 +
> > >  tools/perf/builtin-top.c                 |   8 +
> > >  tools/perf/tests/Build                   |   1 +
> > >  tools/perf/tests/builtin-test.c          |   9 +
> > >  tools/perf/tests/pfm.c                   | 207 +++++++++++++++++
> > >  tools/perf/tests/tests.h                 |   3 +
> > >  tools/perf/util/Build                    |   2 +
> > >  tools/perf/util/evsel.c                  |   2 +-
> > >  tools/perf/util/evsel.h                  |   1 +
> > >  tools/perf/util/parse-events.c           |  30 ++-
> > >  tools/perf/util/parse-events.h           |   4 +
> > >  tools/perf/util/pfm.c                    | 277 +++++++++++++++++++++++
> > >  tools/perf/util/pfm.h                    |  43 ++++
> > >  20 files changed, 645 insertions(+), 8 deletions(-)
> > >  create mode 100644 tools/perf/tests/pfm.c
> > >  create mode 100644 tools/perf/util/pfm.c
> > >  create mode 100644 tools/perf/util/pfm.h
> > >
> > > diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
> > > index b3f3b3f1c161..ad2c41f595c2 100644
> > > --- a/tools/perf/Documentation/perf-record.txt
> > > +++ b/tools/perf/Documentation/perf-record.txt
> > > @@ -596,6 +596,17 @@ Make a copy of /proc/kcore and place it into a directory with the perf data file
> > >  Limit the sample data max size, <size> is expected to be a number with
> > >  appended unit character - B/K/M/G
> > >
> > > +ifdef::HAVE_LIBPFM[]
> > > +--pfm-events events::
> > > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > > +including support for event filters. For example '--pfm-events
> > > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > > +option using the comma separator. Hardware events and generic hardware
> > > +events cannot be mixed together. The latter must be used with the -e
> > > +option. The -e option and this one can be mixed and matched.  Events
> > > +can be grouped using the {} notation.
> > > +endif::HAVE_LIBPFM[]
> > > +
> > >  SEE ALSO
> > >  --------
> > >  linkperf:perf-stat[1], linkperf:perf-list[1], linkperf:perf-intel-pt[1]
> > > diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
> > > index 4d56586b2fb9..536952ad592c 100644
> > > --- a/tools/perf/Documentation/perf-stat.txt
> > > +++ b/tools/perf/Documentation/perf-stat.txt
> > > @@ -71,6 +71,16 @@ report::
> > >  --tid=<tid>::
> > >          stat events on existing thread id (comma separated list)
> > >
> > > +ifdef::HAVE_LIBPFM[]
> > > +--pfm-events events::
> > > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > > +including support for event filters. For example '--pfm-events
> > > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > > +option using the comma separator. Hardware events and generic hardware
> > > +events cannot be mixed together. The latter must be used with the -e
> > > +option. The -e option and this one can be mixed and matched.  Events
> > > +can be grouped using the {} notation.
> > > +endif::HAVE_LIBPFM[]
> > >
> > >  -a::
> > >  --all-cpus::
> > > diff --git a/tools/perf/Documentation/perf-top.txt b/tools/perf/Documentation/perf-top.txt
> > > index 20227dabc208..ee2024691d46 100644
> > > --- a/tools/perf/Documentation/perf-top.txt
> > > +++ b/tools/perf/Documentation/perf-top.txt
> > > @@ -329,6 +329,17 @@ Default is to monitor all CPUS.
> > >       The known limitations include exception handing such as
> > >       setjmp/longjmp will have calls/returns not match.
> > >
> > > +ifdef::HAVE_LIBPFM[]
> > > +--pfm-events events::
> > > +Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
> > > +including support for event filters. For example '--pfm-events
> > > +inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
> > > +option using the comma separator. Hardware events and generic hardware
> > > +events cannot be mixed together. The latter must be used with the -e
> > > +option. The -e option and this one can be mixed and matched.  Events
> > > +can be grouped using the {} notation.
> > > +endif::HAVE_LIBPFM[]
> > > +
> > >  INTERACTIVE PROMPTING KEYS
> > >  --------------------------
> > >
> > > diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> > > index 12a8204d63c6..b67804fee1e3 100644
> > > --- a/tools/perf/Makefile.config
> > > +++ b/tools/perf/Makefile.config
> > > @@ -1012,6 +1012,19 @@ ifdef LIBCLANGLLVM
> > >    endif
> > >  endif
> > >
> > > +ifdef LIBPFM4
> > > +  $(call feature_check,libpfm4)
> > > +  ifeq ($(feature-libpfm4), 1)
> > > +    CFLAGS += -DHAVE_LIBPFM
> > > +    EXTLIBS += -lpfm
> > > +    ASCIIDOC_EXTRA = -aHAVE_LIBPFM=1
> > > +    $(call detected,CONFIG_LIBPFM4)
> > > +  else
> > > +    msg := $(warning libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev);
> > > +    NO_LIBPFM4 := 1
> > > +  endif
> > > +endif
> > > +
> > >  # Among the variables below, these:
> > >  #   perfexecdir
> > >  #   perf_include_dir
> > > diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
> > > index 94a495594e99..dc82578c8773 100644
> > > --- a/tools/perf/Makefile.perf
> > > +++ b/tools/perf/Makefile.perf
> > > @@ -118,6 +118,8 @@ include ../scripts/utilities.mak
> > >  #
> > >  # Define LIBBPF_DYNAMIC to enable libbpf dynamic linking.
> > >  #
> > > +# Define LIBPFM4 to enable libpfm4 events extension.
> > > +#
> > >
> > >  # As per kernel Makefile, avoid funny character set dependencies
> > >  unexport LC_ALL
> > > diff --git a/tools/perf/builtin-list.c b/tools/perf/builtin-list.c
> > > index 965ef017496f..d2069bdcb469 100644
> > > --- a/tools/perf/builtin-list.c
> > > +++ b/tools/perf/builtin-list.c
> > > @@ -14,6 +14,7 @@
> > >  #include "util/pmu.h"
> > >  #include "util/debug.h"
> > >  #include "util/metricgroup.h"
> > > +#include "util/pfm.h"
> > >  #include <subcmd/pager.h>
> > >  #include <subcmd/parse-options.h>
> > >  #include <stdio.h>
> > > @@ -53,6 +54,8 @@ int cmd_list(int argc, const char **argv)
> > >
> > >       setup_pager();
> > >
> > > +     libpfm_initialize();
> > > +
> > >       if (!raw_dump && pager_in_use())
> > >               printf("\nList of pre-defined events (to be used in -e):\n\n");
> > >
> > > diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> > > index 1ab349abe904..fc55b641849f 100644
> > > --- a/tools/perf/builtin-record.c
> > > +++ b/tools/perf/builtin-record.c
> > > @@ -43,6 +43,7 @@
> > >  #include "util/time-utils.h"
> > >  #include "util/units.h"
> > >  #include "util/bpf-event.h"
> > > +#include "util/pfm.h"
> > >  #include "asm/bug.h"
> > >  #include "perf.h"
> > >
> > > @@ -2421,6 +2422,11 @@ static struct option __record_options[] = {
> > >  #endif
> > >       OPT_CALLBACK(0, "max-size", &record.output_max_size,
> > >                    "size", "Limit the maximum size of the output file", parse_output_max_size),
> > > +#ifdef HAVE_LIBPFM
> > > +     OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
> > > +             "libpfm4 event selector. use 'perf list' to list available events",
> > > +             parse_libpfm_events_option),
> > > +#endif
> > >       OPT_END()
> > >  };
> > >
> > > @@ -2461,6 +2467,8 @@ int cmd_record(int argc, const char **argv)
> > >       if (rec->evlist == NULL)
> > >               return -ENOMEM;
> > >
> > > +     libpfm_initialize();
> > > +
> > >       err = perf_config(perf_record_config, rec);
> > >       if (err)
> > >               return err;
> > > diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
> > > index 9207b6c45475..607920321c15 100644
> > > --- a/tools/perf/builtin-stat.c
> > > +++ b/tools/perf/builtin-stat.c
> > > @@ -66,6 +66,7 @@
> > >  #include "util/time-utils.h"
> > >  #include "util/top.h"
> > >  #include "util/affinity.h"
> > > +#include "util/pfm.h"
> > >  #include "asm/bug.h"
> > >
> > >  #include <linux/time64.h>
> > > @@ -936,6 +937,11 @@ static struct option stat_options[] = {
> > >                   "Use with 'percore' event qualifier to show the event "
> > >                   "counts of one hardware thread by sum up total hardware "
> > >                   "threads of same physical core"),
> > > +#ifdef HAVE_LIBPFM
> > > +     OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
> > > +             "libpfm4 event selector. use 'perf list' to list available events",
> > > +             parse_libpfm_events_option),
> > > +#endif
> > >       OPT_END()
> > >  };
> > >
> > > @@ -1874,6 +1880,8 @@ int cmd_stat(int argc, const char **argv)
> > >       unsigned int interval, timeout;
> > >       const char * const stat_subcommands[] = { "record", "report" };
> > >
> > > +     libpfm_initialize();
> > > +
> > >       setlocale(LC_ALL, "");
> > >
> > >       evsel_list = evlist__new();
> > > diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
> > > index 6b067a5ba1d5..e1f477a18f93 100644
> > > --- a/tools/perf/builtin-top.c
> > > +++ b/tools/perf/builtin-top.c
> > > @@ -53,6 +53,7 @@
> > >
> > >  #include "util/debug.h"
> > >  #include "util/ordered-events.h"
> > > +#include "util/pfm.h"
> > >
> > >  #include <assert.h>
> > >  #include <elf.h>
> > > @@ -1577,6 +1578,11 @@ int cmd_top(int argc, const char **argv)
> > >                   "WARNING: should be used on grouped events."),
> > >       OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
> > >                   "Enable LBR callgraph stitching approach"),
> > > +#ifdef HAVE_LIBPFM
> > > +     OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
> > > +             "libpfm4 event selector. use 'perf list' to list available events",
> > > +             parse_libpfm_events_option),
> > > +#endif
> > >       OPTS_EVSWITCH(&top.evswitch),
> > >       OPT_END()
> > >       };
> > > @@ -1590,6 +1596,8 @@ int cmd_top(int argc, const char **argv)
> > >       if (status < 0)
> > >               return status;
> > >
> > > +     libpfm_initialize();
> > > +
> > >       top.annotation_opts.min_pcnt = 5;
> > >       top.annotation_opts.context  = 4;
> > >
> > > diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
> > > index b3d1bf13ca07..7d2ccec9fd75 100644
> > > --- a/tools/perf/tests/Build
> > > +++ b/tools/perf/tests/Build
> > > @@ -56,6 +56,7 @@ perf-y += mem2node.o
> > >  perf-y += maps.o
> > >  perf-y += time-utils-test.o
> > >  perf-y += genelf.o
> > > +perf-y += pfm.o
> > >
> > >  $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
> > >       $(call rule_mkdir)
> > > diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
> > > index b6322eb0f423..8b323151f22c 100644
> > > --- a/tools/perf/tests/builtin-test.c
> > > +++ b/tools/perf/tests/builtin-test.c
> > > @@ -313,6 +313,15 @@ static struct test generic_tests[] = {
> > >               .desc = "maps__merge_in",
> > >               .func = test__maps__merge_in,
> > >       },
> > > +     {
> > > +             .desc = "Test libpfm4 support",
> > > +             .func = test__pfm,
> > > +             .subtest = {
> > > +                     .skip_if_fail   = true,
> > > +                     .get_nr         = test__pfm_subtest_get_nr,
> > > +                     .get_desc       = test__pfm_subtest_get_desc,
> > > +             }
> > > +     },
> > >       {
> > >               .func = NULL,
> > >       },
> > > diff --git a/tools/perf/tests/pfm.c b/tools/perf/tests/pfm.c
> > > new file mode 100644
> > > index 000000000000..1d594fd3d9c1
> > > --- /dev/null
> > > +++ b/tools/perf/tests/pfm.c
> > > @@ -0,0 +1,207 @@
> > > +// SPDX-License-Identifier: GPL-2.0
> > > +/*
> > > + * Test support for libpfm4 event encodings.
> > > + *
> > > + * Copyright 2020 Google LLC.
> > > + */
> > > +#include "tests.h"
> > > +#include "util/debug.h"
> > > +#include "util/evlist.h"
> > > +#include "util/pfm.h"
> > > +
> > > +#include <linux/kernel.h>
> > > +
> > > +#ifdef HAVE_LIBPFM
> > > +static int test__pfm_events(void);
> > > +static int test__pfm_group(void);
> > > +#endif
> > > +
> > > +static const struct {
> > > +     int (*func)(void);
> > > +     const char *desc;
> > > +} pfm_testcase_table[] = {
> > > +#ifdef HAVE_LIBPFM
> > > +     {
> > > +             .func = test__pfm_events,
> > > +             .desc = "test of individual --pfm-events",
> > > +     },
> > > +     {
> > > +             .func = test__pfm_group,
> > > +             .desc = "test groups of --pfm-events",
> > > +     },
> > > +#endif
> > > +};
> > > +
> > > +#ifdef HAVE_LIBPFM
> > > +static int count_pfm_events(struct perf_evlist *evlist)
> > > +{
> > > +     struct perf_evsel *evsel;
> > > +     int count = 0;
> > > +
> > > +     perf_evlist__for_each_entry(evlist, evsel) {
> > > +             count++;
> > > +     }
> > > +     return count;
> > > +}
> > > +
> > > +static int test__pfm_events(void)
> > > +{
> > > +     struct evlist *evlist;
> > > +     struct option opt;
> > > +     size_t i;
> > > +     const struct {
> > > +             const char *events;
> > > +             int nr_events;
> > > +     } table[] = {
> > > +             {
> > > +                     .events = "",
> > > +                     .nr_events = 0,
> > > +             },
> > > +             {
> > > +                     .events = "instructions",
> > > +                     .nr_events = 1,
> > > +             },
> > > +             {
> > > +                     .events = "instructions,cycles",
> > > +                     .nr_events = 2,
> > > +             },
> > > +             {
> > > +                     .events = "stereolab",
> > > +                     .nr_events = 0,
> > > +             },
> > > +             {
> > > +                     .events = "instructions,instructions",
> > > +                     .nr_events = 2,
> > > +             },
> > > +             {
> > > +                     .events = "stereolab,instructions",
> > > +                     .nr_events = 0,
> > > +             },
> > > +             {
> > > +                     .events = "instructions,stereolab",
> > > +                     .nr_events = 1,
> > > +             },
> > > +     };
> > > +
> > > +     libpfm_initialize();
> > > +
> > > +     for (i = 0; i < ARRAY_SIZE(table); i++) {
> > > +             evlist = evlist__new();
> > > +             if (evlist == NULL)
> > > +                     return -ENOMEM;
> > > +
> > > +             opt.value = evlist;
> > > +             parse_libpfm_events_option(&opt,
> > > +                                     table[i].events,
> > > +                                     0);
> > > +             TEST_ASSERT_EQUAL(table[i].events,
> > > +                             count_pfm_events(&evlist->core),
> > > +                             table[i].nr_events);
> > > +             TEST_ASSERT_EQUAL(table[i].events,
> > > +                             evlist->nr_groups,
> > > +                             0);
> > > +
> > > +             evlist__delete(evlist);
> > > +     }
> > > +     return 0;
> > > +}
> > > +
> > > +static int test__pfm_group(void)
> > > +{
> > > +     struct evlist *evlist;
> > > +     struct option opt;
> > > +     size_t i;
> > > +     const struct {
> > > +             const char *events;
> > > +             int nr_events;
> > > +             int nr_groups;
> > > +     } table[] = {
> > > +             {
> > > +                     .events = "{},",
> > > +                     .nr_events = 0,
> > > +                     .nr_groups = 0,
> > > +             },
> > > +             {
> > > +                     .events = "{instructions}",
> > > +                     .nr_events = 1,
> > > +                     .nr_groups = 1,
> > > +             },
> > > +             {
> > > +                     .events = "{instructions},{}",
> > > +                     .nr_events = 1,
> > > +                     .nr_groups = 1,
> > > +             },
> > > +             {
> > > +                     .events = "{},{instructions}",
> > > +                     .nr_events = 0,
> > > +                     .nr_groups = 0,
> > > +             },
> > > +             {
> > > +                     .events = "{instructions},{instructions}",
> > > +                     .nr_events = 2,
> > > +                     .nr_groups = 2,
> > > +             },
> > > +             {
> > > +                     .events = "{instructions,cycles},{instructions,cycles}",
> > > +                     .nr_events = 4,
> > > +                     .nr_groups = 2,
> > > +             },
> > > +             {
> > > +                     .events = "{stereolab}",
> > > +                     .nr_events = 0,
> > > +                     .nr_groups = 0,
> > > +             },
> > > +             {
> > > +                     .events =
> > > +                     "{instructions,cycles},{instructions,stereolab}",
> > > +                     .nr_events = 3,
> > > +                     .nr_groups = 1,
> > > +             },
> > > +     };
> > > +
> > > +     libpfm_initialize();
> > > +
> > > +     for (i = 0; i < ARRAY_SIZE(table); i++) {
> > > +             evlist = evlist__new();
> > > +             if (evlist == NULL)
> > > +                     return -ENOMEM;
> > > +
> > > +             opt.value = evlist;
> > > +             parse_libpfm_events_option(&opt,
> > > +                                     table[i].events,
> > > +                                     0);
> > > +             TEST_ASSERT_EQUAL(table[i].events,
> > > +                             count_pfm_events(&evlist->core),
> > > +                             table[i].nr_events);
> > > +             TEST_ASSERT_EQUAL(table[i].events,
> > > +                             evlist->nr_groups,
> > > +                             table[i].nr_groups);
> > > +
> > > +             evlist__delete(evlist);
> > > +     }
> > > +     return 0;
> > > +}
> > > +#endif
> > > +
> > > +const char *test__pfm_subtest_get_desc(int i)
> > > +{
> > > +     if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> > > +             return NULL;
> > > +     return pfm_testcase_table[i].desc;
> > > +}
> > > +
> > > +int test__pfm_subtest_get_nr(void)
> > > +{
> > > +     return (int)ARRAY_SIZE(pfm_testcase_table);
> > > +}
> > > +
> > > +int test__pfm(struct test *test __maybe_unused, int i __maybe_unused)
> > > +{
> > > +#ifdef HAVE_LIBPFM
> > > +     if (i < 0 || i >= (int)ARRAY_SIZE(pfm_testcase_table))
> > > +             return TEST_FAIL;
> > > +     return pfm_testcase_table[i].func();
> > > +#else
> > > +     return TEST_SKIP;
> > > +#endif
> > > +}
> > > diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
> > > index 61a1ab032080..47cbae753b42 100644
> > > --- a/tools/perf/tests/tests.h
> > > +++ b/tools/perf/tests/tests.h
> > > @@ -112,6 +112,9 @@ int test__mem2node(struct test *t, int subtest);
> > >  int test__maps__merge_in(struct test *t, int subtest);
> > >  int test__time_utils(struct test *t, int subtest);
> > >  int test__jit_write_elf(struct test *test, int subtest);
> > > +int test__pfm(struct test *test, int subtest);
> > > +const char *test__pfm_subtest_get_desc(int subtest);
> > > +int test__pfm_subtest_get_nr(void);
> > >
> > >  bool test__bp_signal_is_supported(void);
> > >  bool test__bp_account_is_supported(void);
> > > diff --git a/tools/perf/util/Build b/tools/perf/util/Build
> > > index c0cf8dff694e..afd3d2221c83 100644
> > > --- a/tools/perf/util/Build
> > > +++ b/tools/perf/util/Build
> > > @@ -177,6 +177,8 @@ perf-$(CONFIG_LIBBPF) += bpf-event.o
> > >
> > >  perf-$(CONFIG_CXX) += c++/
> > >
> > > +perf-$(CONFIG_LIBPFM4) += pfm.o
> > > +
> > >  CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
> > >  CFLAGS_llvm-utils.o += -DPERF_INCLUDE_DIR="BUILD_STR($(perf_include_dir_SQ))"
> > >
> > > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
> > > index 6a571d322bb2..a5c88d52c11c 100644
> > > --- a/tools/perf/util/evsel.c
> > > +++ b/tools/perf/util/evsel.c
> > > @@ -2433,7 +2433,7 @@ bool perf_evsel__fallback(struct evsel *evsel, int err,
> > >
> > >               /* Is there already the separator in the name. */
> > >               if (strchr(name, '/') ||
> > > -                 strchr(name, ':'))
> > > +                 (strchr(name, ':') && !evsel->is_libpfm_event))
> > >                       sep = "";
> > >
> > >               if (asprintf(&new_name, "%s%su", name, sep) < 0)
> > > diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
> > > index a463bc65b001..709a9d494767 100644
> > > --- a/tools/perf/util/evsel.h
> > > +++ b/tools/perf/util/evsel.h
> > > @@ -76,6 +76,7 @@ struct evsel {
> > >       bool                    ignore_missing_thread;
> > >       bool                    forced_leader;
> > >       bool                    use_uncore_alias;
> > > +     bool                    is_libpfm_event;
> > >       /* parse modifier helper */
> > >       int                     exclude_GH;
> > >       int                     sample_read;
> > > diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> > > index 10107747b361..e7e0faf36e3c 100644
> > > --- a/tools/perf/util/parse-events.c
> > > +++ b/tools/perf/util/parse-events.c
> > > @@ -36,6 +36,7 @@
> > >  #include "metricgroup.h"
> > >  #include "util/evsel_config.h"
> > >  #include "util/event.h"
> > > +#include "util/pfm.h"
> > >
> > >  #define MAX_NAME_LEN 100
> > >
> > > @@ -344,6 +345,7 @@ static char *get_config_name(struct list_head *head_terms)
> > >  static struct evsel *
> > >  __add_event(struct list_head *list, int *idx,
> > >           struct perf_event_attr *attr,
> > > +         bool init_attr,
> > >           char *name, struct perf_pmu *pmu,
> > >           struct list_head *config_terms, bool auto_merge_stats,
> > >           const char *cpu_list)
> > > @@ -352,7 +354,8 @@ __add_event(struct list_head *list, int *idx,
> > >       struct perf_cpu_map *cpus = pmu ? pmu->cpus :
> > >                              cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
> > >
> > > -     event_attr_init(attr);
> > > +     if (init_attr)
> > > +             event_attr_init(attr);
> > >
> > >       evsel = perf_evsel__new_idx(attr, *idx);
> > >       if (!evsel)
> > > @@ -370,15 +373,25 @@ __add_event(struct list_head *list, int *idx,
> > >       if (config_terms)
> > >               list_splice(config_terms, &evsel->config_terms);
> > >
> > > -     list_add_tail(&evsel->core.node, list);
> > > +     if (list)
> > > +             list_add_tail(&evsel->core.node, list);
> > > +
> > >       return evsel;
> > >  }
> > >
> > > +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> > > +                                     char *name, struct perf_pmu *pmu)
> > > +{
> > > +     return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
> > > +                        NULL);
> > > +}
> > > +
> > >  static int add_event(struct list_head *list, int *idx,
> > >                    struct perf_event_attr *attr, char *name,
> > >                    struct list_head *config_terms)
> > >  {
> > > -     return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
> > > +     return __add_event(list, idx, attr, true, name, NULL, config_terms,
> > > +                        false, NULL) ? 0 : -ENOMEM;
> > >  }
> > >
> > >  static int add_event_tool(struct list_head *list, int *idx,
> > > @@ -390,7 +403,8 @@ static int add_event_tool(struct list_head *list, int *idx,
> > >               .config = PERF_COUNT_SW_DUMMY,
> > >       };
> > >
> > > -     evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
> > > +     evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
> > > +                         "0");
> > >       if (!evsel)
> > >               return -ENOMEM;
> > >       evsel->tool_event = tool_event;
> > > @@ -1446,8 +1460,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> > >
> > >       if (!head_config) {
> > >               attr.type = pmu->type;
> > > -             evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
> > > -                                 auto_merge_stats, NULL);
> > > +             evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
> > > +                                 pmu, NULL, auto_merge_stats, NULL);
> > >               if (evsel) {
> > >                       evsel->pmu_name = name ? strdup(name) : NULL;
> > >                       evsel->use_uncore_alias = use_uncore_alias;
> > > @@ -1487,7 +1501,7 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> > >               return -EINVAL;
> > >       }
> > >
> > > -     evsel = __add_event(list, &parse_state->idx, &attr,
> > > +     evsel = __add_event(list, &parse_state->idx, &attr, true,
> > >                           get_config_name(head_config), pmu,
> > >                           &config_terms, auto_merge_stats, NULL);
> > >       if (evsel) {
> > > @@ -2794,6 +2808,8 @@ void print_events(const char *event_glob, bool name_only, bool quiet_flag,
> > >       print_sdt_events(NULL, NULL, name_only);
> > >
> > >       metricgroup__print(true, true, NULL, name_only, details_flag);
> > > +
> > > +     print_libpfm_events(name_only, long_desc);
> > >  }
> > >
> > >  int parse_events__is_hardcoded_term(struct parse_events_term *term)
> > > diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
> > > index 27596cbd0ba0..f7f555b1ca35 100644
> > > --- a/tools/perf/util/parse-events.h
> > > +++ b/tools/perf/util/parse-events.h
> > > @@ -17,6 +17,7 @@ struct evlist;
> > >  struct parse_events_error;
> > >
> > >  struct option;
> > > +struct perf_pmu;
> > >
> > >  struct tracepoint_path {
> > >       char *system;
> > > @@ -186,6 +187,9 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
> > >                        bool auto_merge_stats,
> > >                        bool use_alias);
> > >
> > > +struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
> > > +                                     char *name, struct perf_pmu *pmu);
> > > +
> > >  int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
> > >                              char *str,
> > >                              struct list_head **listp);
> > > diff --git a/tools/perf/util/pfm.c b/tools/perf/util/pfm.c
> > > new file mode 100644
> > > index 000000000000..4b11adf832a0
> > > --- /dev/null
> > > +++ b/tools/perf/util/pfm.c
> > > @@ -0,0 +1,277 @@
> > > +// SPDX-License-Identifier: GPL-2.0
> > > +/*
> > > + * Support for libpfm4 event encoding.
> > > + *
> > > + * Copyright 2020 Google LLC.
> > > + */
> > > +#include "util/cpumap.h"
> > > +#include "util/debug.h"
> > > +#include "util/event.h"
> > > +#include "util/evlist.h"
> > > +#include "util/evsel.h"
> > > +#include "util/parse-events.h"
> > > +#include "util/pmu.h"
> > > +#include "util/pfm.h"
> > > +
> > > +#include <string.h>
> > > +#include <linux/kernel.h>
> > > +#include <perfmon/pfmlib_perf_event.h>
> > > +
> > > +void libpfm_initialize(void)
> > > +{
> > > +     int ret;
> > > +
> > > +     ret = pfm_initialize();
> > > +     if (ret != PFM_SUCCESS) {
> > > +             ui__warning("libpfm failed to initialize: %s\n",
> > > +                     pfm_strerror(ret));
> > > +     }
> > > +}
> > > +
> > > +int parse_libpfm_events_option(const struct option *opt, const char *str,
> > > +                     int unset __maybe_unused)
> > > +{
> > > +     struct evlist *evlist = *(struct evlist **)opt->value;
> > > +     struct perf_event_attr attr;
> > > +     struct perf_pmu *pmu;
> > > +     struct evsel *evsel, *grp_leader = NULL;
> > > +     char *p, *q, *p_orig;
> > > +     const char *sep;
> > > +     int grp_evt = -1;
> > > +     int ret;
> > > +
> > > +     p_orig = p = strdup(str);
> > > +     if (!p)
> > > +             return -1;
> > > +     /*
> > > +      * force loading of the PMU list
> > > +      */
> > > +     perf_pmu__scan(NULL);
> > > +
> > > +     for (q = p; strsep(&p, ",{}"); q = p) {
> > > +             sep = p ? str + (p - p_orig - 1) : "";
> > > +             if (*sep == '{') {
> > > +                     if (grp_evt > -1) {
> > > +                             ui__error(
> > > +                                     "nested event groups not supported\n");
> > > +                             goto error;
> > > +                     }
> > > +                     grp_evt++;
> > > +             }
> > > +
> > > +             /* no event */
> > > +             if (*q == '\0')
> > > +                     continue;
> > > +
> > > +             memset(&attr, 0, sizeof(attr));
> > > +             event_attr_init(&attr);
> > > +
> > > +             ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
> > > +                                             &attr, NULL, NULL);
> > > +
> > > +             if (ret != PFM_SUCCESS) {
> > > +                     ui__error("failed to parse event %s : %s\n", str,
> > > +                               pfm_strerror(ret));
> > > +                     goto error;
> > > +             }
> > > +
> > > +             pmu = perf_pmu__find_by_type((unsigned int)attr.type);
> > > +             evsel = parse_events__add_event(evlist->core.nr_entries,
> > > +                                             &attr, q, pmu);
> > > +             if (evsel == NULL)
> > > +                     goto error;
> > > +
> > > +             evsel->is_libpfm_event = true;
> > > +
> > > +             evlist__add(evlist, evsel);
> > > +
> > > +             if (grp_evt == 0)
> > > +                     grp_leader = evsel;
> > > +
> > > +             if (grp_evt > -1) {
> > > +                     evsel->leader = grp_leader;
> > > +                     grp_leader->core.nr_members++;
> > > +                     grp_evt++;
> > > +             }
> > > +
> > > +             if (*sep == '}') {
> > > +                     if (grp_evt < 0) {
> > > +                             ui__error(
> > > +                                "cannot close a non-existing event group\n");
> > > +                             goto error;
> > > +                     }
> > > +                     evlist->nr_groups++;
> > > +                     grp_leader = NULL;
> > > +                     grp_evt = -1;
> > > +             }
> > > +     }
> > > +     return 0;
> > > +error:
> > > +     free(p_orig);
> > > +     return -1;
> > > +}
> > > +
> > > +static const char *srcs[PFM_ATTR_CTRL_MAX] = {
> > > +     [PFM_ATTR_CTRL_UNKNOWN] = "???",
> > > +     [PFM_ATTR_CTRL_PMU] = "PMU",
> > > +     [PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
> > > +};
> > > +
> > > +static void
> > > +print_attr_flags(pfm_event_attr_info_t *info)
> > > +{
> > > +     int n = 0;
> > > +
> > > +     if (info->is_dfl) {
> > > +             printf("[default] ");
> > > +             n++;
> > > +     }
> > > +
> > > +     if (info->is_precise) {
> > > +             printf("[precise] ");
> > > +             n++;
> > > +     }
> > > +
> > > +     if (!n)
> > > +             printf("- ");
> > > +}
> > > +
> > > +static void
> > > +print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc)
> > > +{
> > > +     pfm_event_attr_info_t ainfo;
> > > +     const char *src;
> > > +     int j, ret;
> > > +
> > > +     ainfo.size = sizeof(ainfo);
> > > +
> > > +     printf("  %s\n", info->name);
> > > +     printf("    [%s]\n", info->desc);
> > > +     if (long_desc) {
> > > +             if (info->equiv)
> > > +                     printf("      Equiv: %s\n", info->equiv);
> > > +
> > > +             printf("      Code  : 0x%"PRIx64"\n", info->code);
> > > +     }
> > > +     pfm_for_each_event_attr(j, info) {
> > > +             ret = pfm_get_event_attr_info(info->idx, j,
> > > +                                           PFM_OS_PERF_EVENT_EXT, &ainfo);
> > > +             if (ret != PFM_SUCCESS)
> > > +                     continue;
> > > +
> > > +             if (ainfo.type == PFM_ATTR_UMASK) {
> > > +                     printf("      %s:%s\n", info->name, ainfo.name);
> > > +                     printf("        [%s]\n", ainfo.desc);
> > > +             }
> > > +
> > > +             if (!long_desc)
> > > +                     continue;
> > > +
> > > +             if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
> > > +                     ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
> > > +
> > > +             src = srcs[ainfo.ctrl];
> > > +             switch (ainfo.type) {
> > > +             case PFM_ATTR_UMASK:
> > > +                     printf("        Umask : 0x%02"PRIx64" : %s: ",
> > > +                             ainfo.code, src);
> > > +                     print_attr_flags(&ainfo);
> > > +                     putchar('\n');
> > > +                     break;
> > > +             case PFM_ATTR_MOD_BOOL:
> > > +                     printf("      Modif : %s: [%s] : %s (boolean)\n", src,
> > > +                             ainfo.name, ainfo.desc);
> > > +                     break;
> > > +             case PFM_ATTR_MOD_INTEGER:
> > > +                     printf("      Modif : %s: [%s] : %s (integer)\n", src,
> > > +                             ainfo.name, ainfo.desc);
> > > +                     break;
> > > +             case PFM_ATTR_NONE:
> > > +             case PFM_ATTR_RAW_UMASK:
> > > +             case PFM_ATTR_MAX:
> > > +             default:
> > > +                     printf("      Attr  : %s: [%s] : %s\n", src,
> > > +                             ainfo.name, ainfo.desc);
> > > +             }
> > > +     }
> > > +}
> > > +
> > > +/*
> > > + * list all pmu::event:umask, pmu::event
> > > + * printed events may not be all valid combinations of umask for an event
> > > + */
> > > +static void
> > > +print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info)
> > > +{
> > > +     pfm_event_attr_info_t ainfo;
> > > +     int j, ret;
> > > +     bool has_umask = false;
> > > +
> > > +     ainfo.size = sizeof(ainfo);
> > > +
> > > +     pfm_for_each_event_attr(j, info) {
> > > +             ret = pfm_get_event_attr_info(info->idx, j,
> > > +                                           PFM_OS_PERF_EVENT_EXT, &ainfo);
> > > +             if (ret != PFM_SUCCESS)
> > > +                     continue;
> > > +
> > > +             if (ainfo.type != PFM_ATTR_UMASK)
> > > +                     continue;
> > > +
> > > +             printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name);
> > > +             has_umask = true;
> > > +     }
> > > +     if (!has_umask)
> > > +             printf("%s::%s\n", pinfo->name, info->name);
> > > +}
> > > +
> > > +void print_libpfm_events(bool name_only, bool long_desc)
> > > +{
> > > +     pfm_event_info_t info;
> > > +     pfm_pmu_info_t pinfo;
> > > +     int i, p, ret;
> > > +
> > > +     /* initialize to zero to indicate ABI version */
> > > +     info.size  = sizeof(info);
> > > +     pinfo.size = sizeof(pinfo);
> > > +
> > > +     if (!name_only)
> > > +             puts("\nList of pre-defined events (to be used in --pfm-events):\n");
> > > +
> > > +     pfm_for_all_pmus(p) {
> > > +             bool printed_pmu = false;
> > > +
> > > +             ret = pfm_get_pmu_info(p, &pinfo);
> > > +             if (ret != PFM_SUCCESS)
> > > +                     continue;
> > > +
> > > +             /* only print events that are supported by host HW */
> > > +             if (!pinfo.is_present)
> > > +                     continue;
> > > +
> > > +             /* handled by perf directly */
> > > +             if (pinfo.pmu == PFM_PMU_PERF_EVENT)
> > > +                     continue;
> > > +
> > > +             for (i = pinfo.first_event; i != -1;
> > > +                  i = pfm_get_event_next(i)) {
> > > +
> > > +                     ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
> > > +                                             &info);
> > > +                     if (ret != PFM_SUCCESS)
> > > +                             continue;
> > > +
> > > +                     if (!name_only && !printed_pmu) {
> > > +                             printf("%s:\n", pinfo.name);
> > > +                             printed_pmu = true;
> > > +                     }
> > > +
> > > +                     if (!name_only)
> > > +                             print_libpfm_events_detailed(&info, long_desc);
> > > +                     else
> > > +                             print_libpfm_events_raw(&pinfo, &info);
> > > +             }
> > > +             if (!name_only && printed_pmu)
> > > +                     putchar('\n');
> > > +     }
> > > +}
> > > diff --git a/tools/perf/util/pfm.h b/tools/perf/util/pfm.h
> > > new file mode 100644
> > > index 000000000000..532dafe88bc0
> > > --- /dev/null
> > > +++ b/tools/perf/util/pfm.h
> > > @@ -0,0 +1,43 @@
> > > +/* SPDX-License-Identifier: GPL-2.0 */
> > > +/*
> > > + * Support for libpfm4 event encoding.
> > > + *
> > > + * Copyright 2020 Google LLC.
> > > + */
> > > +#ifndef __PERF_PFM_H
> > > +#define __PERF_PFM_H
> > > +
> > > +#include <subcmd/parse-options.h>
> > > +
> > > +#ifdef HAVE_LIBPFM
> > > +void libpfm_initialize(void);
> > > +
> > > +int parse_libpfm_events_option(const struct option *opt, const char *str,
> > > +                     int unset);
> > > +
> > > +void print_libpfm_events(bool name_only, bool long_desc);
> > > +
> > > +#else
> > > +#include <linux/compiler.h>
> > > +
> > > +static inline void libpfm_initialize(void)
> > > +{
> > > +}
> > > +
> > > +static inline int parse_libpfm_events_option(
> > > +     const struct option *opt __maybe_unused,
> > > +     const char *str __maybe_unused,
> > > +     int unset __maybe_unused)
> > > +{
> > > +     return 0;
> > > +}
> > > +
> > > +static inline void print_libpfm_events(bool name_only __maybe_unused,
> > > +                                    bool long_desc __maybe_unused)
> > > +{
> > > +}
> > > +
> > > +#endif
> > > +
> > > +
> > > +#endif /* __PERF_PFM_H */
> > > --
> > > 2.26.2.303.gf8c07b1a785-goog
> > >
> >
> > --
> >
> > - Arnaldo

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

* [tip: perf/core] perf pmu: Add perf_pmu__find_by_type helper
  2020-04-29 23:14 ` [PATCH v12 3/4] perf pmu: add perf_pmu__find_by_type helper Ian Rogers
@ 2020-05-08 13:04   ` tip-bot2 for Stephane Eranian
  0 siblings, 0 replies; 14+ messages in thread
From: tip-bot2 for Stephane Eranian @ 2020-05-08 13:04 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Stephane Eranian, Ian Rogers, Jiri Olsa, Adrian Hunter,
	Alexander Shishkin, Alexei Starovoitov, Alexey Budankov,
	Andi Kleen, Andrii Nakryiko, Daniel Borkmann, Florian Fainelli,
	Greg Kroah-Hartman, Igor Lubashev, Jin Yao, Jiwei Sun,
	John Garry, Kan Liang, Leo Yan, Mark Rutland, Martin KaFai Lau,
	Namhyung Kim, Peter Zijlstra, Thomas Gleixner, Yonghong Song,
	bpf, netdev, yuzhoujian, Arnaldo Carvalho de Melo, x86, LKML

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     3a50dc76058d7cd8315f9c712b793d81a7ff4541
Gitweb:        https://git.kernel.org/tip/3a50dc76058d7cd8315f9c712b793d81a7ff4541
Author:        Stephane Eranian <eranian@google.com>
AuthorDate:    Wed, 29 Apr 2020 16:14:42 -07:00
Committer:     Arnaldo Carvalho de Melo <acme@redhat.com>
CommitterDate: Tue, 05 May 2020 16:35:31 -03:00

perf pmu: Add perf_pmu__find_by_type helper

This is used by libpfm4 during event parsing to locate the pmu for an
event.

Signed-off-by: Stephane Eranian <eranian@google.com>
Reviewed-by: Ian Rogers <irogers@google.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrii Nakryiko <andriin@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Igor Lubashev <ilubashe@akamai.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Jiwei Sun <jiwei.sun@windriver.com>
Cc: John Garry <john.garry@huawei.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Yonghong Song <yhs@fb.com>
Cc: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org
Cc: yuzhoujian <yuzhoujian@didichuxing.com>
Link: http://lore.kernel.org/lkml/20200429231443.207201-4-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/pmu.c | 11 +++++++++++
 tools/perf/util/pmu.h |  1 +
 2 files changed, 12 insertions(+)

diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 5642de7..92bd7fa 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -871,6 +871,17 @@ static struct perf_pmu *pmu_find(const char *name)
 	return NULL;
 }
 
+struct perf_pmu *perf_pmu__find_by_type(unsigned int type)
+{
+	struct perf_pmu *pmu;
+
+	list_for_each_entry(pmu, &pmus, list)
+		if (pmu->type == type)
+			return pmu;
+
+	return NULL;
+}
+
 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu)
 {
 	/*
diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index 1edd214..cb6fbec 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -72,6 +72,7 @@ struct perf_pmu_alias {
 };
 
 struct perf_pmu *perf_pmu__find(const char *name);
+struct perf_pmu *perf_pmu__find_by_type(unsigned int type);
 int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
 		     struct list_head *head_terms,
 		     struct parse_events_error *error);

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

* [tip: perf/core] tools feature: Add support for detecting libpfm4
  2020-04-29 23:14 ` [PATCH v12 2/4] tools feature: add support for detecting libpfm4 Ian Rogers
@ 2020-05-08 13:04   ` tip-bot2 for Stephane Eranian
  0 siblings, 0 replies; 14+ messages in thread
From: tip-bot2 for Stephane Eranian @ 2020-05-08 13:04 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Stephane Eranian, Ian Rogers, Jiri Olsa, Adrian Hunter,
	Alexander Shishkin, Alexei Starovoitov, Alexey Budankov,
	Andi Kleen, Andrii Nakryiko, Daniel Borkmann, Florian Fainelli,
	Greg Kroah-Hartman, Igor Lubashev, Jin Yao, Jiwei Sun,
	John Garry, Kan Liang, Leo Yan, Mark Rutland, Martin KaFai Lau,
	Namhyung Kim, Peter Zijlstra, Thomas Gleixner, Yonghong Song,
	bpf, netdev, yuzhoujian, Arnaldo Carvalho de Melo, x86, LKML

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     5ef86146de941f273d669a8e018036f549bf058c
Gitweb:        https://git.kernel.org/tip/5ef86146de941f273d669a8e018036f549bf058c
Author:        Stephane Eranian <eranian@google.com>
AuthorDate:    Wed, 29 Apr 2020 16:14:41 -07:00
Committer:     Arnaldo Carvalho de Melo <acme@redhat.com>
CommitterDate: Tue, 05 May 2020 16:35:31 -03:00

tools feature: Add support for detecting libpfm4

libpfm4 provides an alternate command line encoding of perf events.

Signed-off-by: Stephane Eranian <eranian@google.com>
Reviewed-by: Ian Rogers <irogers@google.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrii Nakryiko <andriin@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Igor Lubashev <ilubashe@akamai.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Jiwei Sun <jiwei.sun@windriver.com>
Cc: John Garry <john.garry@huawei.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Yonghong Song <yhs@fb.com>
Cc: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org
Cc: yuzhoujian <yuzhoujian@didichuxing.com>
Link: http://lore.kernel.org/lkml/20200429231443.207201-3-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/build/Makefile.feature       |  3 ++-
 tools/build/feature/Makefile       |  6 +++++-
 tools/build/feature/test-libpfm4.c |  9 +++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 tools/build/feature/test-libpfm4.c

diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature
index 3e0c019..3abd431 100644
--- a/tools/build/Makefile.feature
+++ b/tools/build/Makefile.feature
@@ -98,7 +98,8 @@ FEATURE_TESTS_EXTRA :=                  \
          llvm                           \
          llvm-version                   \
          clang                          \
-         libbpf
+         libbpf                         \
+         libpfm4
 
 FEATURE_TESTS ?= $(FEATURE_TESTS_BASIC)
 
diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index 9201238..84f845b 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -69,7 +69,8 @@ FILES=                                          \
          test-libaio.bin			\
          test-libzstd.bin			\
          test-clang-bpf-global-var.bin		\
-         test-file-handle.bin
+         test-file-handle.bin			\
+         test-libpfm4.bin
 
 FILES := $(addprefix $(OUTPUT),$(FILES))
 
@@ -331,6 +332,9 @@ $(OUTPUT)test-clang-bpf-global-var.bin:
 $(OUTPUT)test-file-handle.bin:
 	$(BUILD)
 
+$(OUTPUT)test-libpfm4.bin:
+	$(BUILD) -lpfm
+
 ###############################
 
 clean:
diff --git a/tools/build/feature/test-libpfm4.c b/tools/build/feature/test-libpfm4.c
new file mode 100644
index 0000000..af49b25
--- /dev/null
+++ b/tools/build/feature/test-libpfm4.c
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <sys/types.h>
+#include <perfmon/pfmlib.h>
+
+int main(void)
+{
+	pfm_initialize();
+	return 0;
+}

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

* [tip: perf/core] perf doc: Pass ASCIIDOC_EXTRA as an argument
  2020-04-29 23:14 ` [PATCH v12 1/4] perf doc: pass ASCIIDOC_EXTRA as an argument Ian Rogers
@ 2020-05-08 13:04   ` tip-bot2 for Ian Rogers
  0 siblings, 0 replies; 14+ messages in thread
From: tip-bot2 for Ian Rogers @ 2020-05-08 13:04 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: Ian Rogers, Jiri Olsa, Adrian Hunter, Alexander Shishkin,
	Alexei Starovoitov, Alexey Budankov, Andi Kleen, Andrii Nakryiko,
	Daniel Borkmann, Florian Fainelli, Greg Kroah-Hartman,
	Igor Lubashev, Jin Yao, Jiwei Sun, John Garry, Kan Liang,
	Leo Yan, Mark Rutland, Martin KaFai Lau, Namhyung Kim,
	Peter Zijlstra, Stephane Eranian, Thomas Gleixner, Yonghong Song,
	bpf, netdev, yuzhoujian, Arnaldo Carvalho de Melo, x86, LKML

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     4b1984491e65c9592f9f2bfbe8fb5bf4d4e51587
Gitweb:        https://git.kernel.org/tip/4b1984491e65c9592f9f2bfbe8fb5bf4d4e51587
Author:        Ian Rogers <irogers@google.com>
AuthorDate:    Wed, 29 Apr 2020 16:14:40 -07:00
Committer:     Arnaldo Carvalho de Melo <acme@redhat.com>
CommitterDate: Tue, 05 May 2020 16:35:31 -03:00

perf doc: Pass ASCIIDOC_EXTRA as an argument

commit e9cfa47e687d ("perf doc: allow ASCIIDOC_EXTRA to be an argument")
allowed ASCIIDOC_EXTRA to be passed as an option to the Documentation
Makefile. This change passes ASCIIDOC_EXTRA, set by detected features or
command line options, prior to doing a Documentation build. This is
necessary to allow conditional compilation, based on configuration
variables, in asciidoc code.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Alexey Budankov <alexey.budankov@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrii Nakryiko <andriin@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Igor Lubashev <ilubashe@akamai.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Jiwei Sun <jiwei.sun@windriver.com>
Cc: John Garry <john.garry@huawei.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Yonghong Song <yhs@fb.com>
Cc: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org
Cc: yuzhoujian <yuzhoujian@didichuxing.com>
Link: http://lore.kernel.org/lkml/20200429231443.207201-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/Makefile.perf | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index d15a311..94a4955 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -188,7 +188,7 @@ AWK     = awk
 # non-config cases
 config := 1
 
-NON_CONFIG_TARGETS := clean python-clean TAGS tags cscope help install-doc install-man install-html install-info install-pdf doc man html info pdf
+NON_CONFIG_TARGETS := clean python-clean TAGS tags cscope help
 
 ifdef MAKECMDGOALS
 ifeq ($(filter-out $(NON_CONFIG_TARGETS),$(MAKECMDGOALS)),)
@@ -832,7 +832,7 @@ INSTALL_DOC_TARGETS += quick-install-doc quick-install-man quick-install-html
 
 # 'make doc' should call 'make -C Documentation all'
 $(DOC_TARGETS):
-	$(Q)$(MAKE) -C $(DOC_DIR) O=$(OUTPUT) $(@:doc=all)
+	$(Q)$(MAKE) -C $(DOC_DIR) O=$(OUTPUT) $(@:doc=all) ASCIIDOC_EXTRA=$(ASCIIDOC_EXTRA)
 
 TAG_FOLDERS= . ../lib ../include
 TAG_FILES= ../../include/uapi/linux/perf_event.h
@@ -959,7 +959,7 @@ install-python_ext:
 
 # 'make install-doc' should call 'make -C Documentation install'
 $(INSTALL_DOC_TARGETS):
-	$(Q)$(MAKE) -C $(DOC_DIR) O=$(OUTPUT) $(@:-doc=)
+	$(Q)$(MAKE) -C $(DOC_DIR) O=$(OUTPUT) $(@:-doc=) ASCIIDOC_EXTRA=$(ASCIIDOC_EXTRA)
 
 ### Cleaning rules
 

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

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

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-29 23:14 [PATCH v12 0/4] perf tools: add support for libpfm4 Ian Rogers
2020-04-29 23:14 ` [PATCH v12 1/4] perf doc: pass ASCIIDOC_EXTRA as an argument Ian Rogers
2020-05-08 13:04   ` [tip: perf/core] perf doc: Pass " tip-bot2 for Ian Rogers
2020-04-29 23:14 ` [PATCH v12 2/4] tools feature: add support for detecting libpfm4 Ian Rogers
2020-05-08 13:04   ` [tip: perf/core] tools feature: Add " tip-bot2 for Stephane Eranian
2020-04-29 23:14 ` [PATCH v12 3/4] perf pmu: add perf_pmu__find_by_type helper Ian Rogers
2020-05-08 13:04   ` [tip: perf/core] perf pmu: Add " tip-bot2 for Stephane Eranian
2020-04-29 23:14 ` [PATCH v12 4/4] perf tools: add support for libpfm4 Ian Rogers
2020-04-30 13:34   ` Arnaldo Carvalho de Melo
2020-04-30 13:34     ` Arnaldo Carvalho de Melo
2020-04-30 14:26     ` Ian Rogers
2020-04-30 14:26       ` Ian Rogers
2020-05-05 18:31       ` Ian Rogers
2020-05-05 18:31         ` Ian Rogers

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.