Skip to content

Commit 7094349

Browse files
Stephane Eranianacmel
authored andcommitted
perf tools: Add optional support for libpfm4
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 .... One needs to explicitely ask for its inclusion by using the LIBPFM4 make command line option, ie its opt-in rather than opt-out of feature detection and build support. Signed-off-by: Stephane Eranian <[email protected]> Reviewed-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Alexey Budankov <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Andrii Nakryiko <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: Florian Fainelli <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Igor Lubashev <[email protected]> Cc: Jin Yao <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Jiwei Sun <[email protected]> Cc: John Garry <[email protected]> Cc: Kan Liang <[email protected]> Cc: Leo Yan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Martin KaFai Lau <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Yonghong Song <[email protected]> Cc: [email protected] Cc: [email protected] Cc: yuzhoujian <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 82352ae commit 7094349

19 files changed

+631
-8
lines changed

tools/perf/Documentation/perf-record.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,17 @@ appended unit character - B/K/M/G
615615
The number of threads to run when synthesizing events for existing processes.
616616
By default, the number of threads equals 1.
617617

618+
ifdef::HAVE_LIBPFM[]
619+
--pfm-events events::
620+
Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
621+
including support for event filters. For example '--pfm-events
622+
inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
623+
option using the comma separator. Hardware events and generic hardware
624+
events cannot be mixed together. The latter must be used with the -e
625+
option. The -e option and this one can be mixed and matched. Events
626+
can be grouped using the {} notation.
627+
endif::HAVE_LIBPFM[]
628+
618629
SEE ALSO
619630
--------
620631
linkperf:perf-stat[1], linkperf:perf-list[1], linkperf:perf-intel-pt[1]

tools/perf/Documentation/perf-stat.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ report::
7171
--tid=<tid>::
7272
stat events on existing thread id (comma separated list)
7373

74+
ifdef::HAVE_LIBPFM[]
75+
--pfm-events events::
76+
Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
77+
including support for event filters. For example '--pfm-events
78+
inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
79+
option using the comma separator. Hardware events and generic hardware
80+
events cannot be mixed together. The latter must be used with the -e
81+
option. The -e option and this one can be mixed and matched. Events
82+
can be grouped using the {} notation.
83+
endif::HAVE_LIBPFM[]
7484

7585
-a::
7686
--all-cpus::

tools/perf/Documentation/perf-top.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,17 @@ Default is to monitor all CPUS.
329329
The known limitations include exception handing such as
330330
setjmp/longjmp will have calls/returns not match.
331331

332+
ifdef::HAVE_LIBPFM[]
333+
--pfm-events events::
334+
Select a PMU event using libpfm4 syntax (see http://perfmon2.sf.net)
335+
including support for event filters. For example '--pfm-events
336+
inst_retired:any_p:u:c=1:i'. More than one event can be passed to the
337+
option using the comma separator. Hardware events and generic hardware
338+
events cannot be mixed together. The latter must be used with the -e
339+
option. The -e option and this one can be mixed and matched. Events
340+
can be grouped using the {} notation.
341+
endif::HAVE_LIBPFM[]
342+
332343
INTERACTIVE PROMPTING KEYS
333344
--------------------------
334345

tools/perf/Makefile.config

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,19 @@ ifdef LIBCLANGLLVM
10221022
endif
10231023
endif
10241024

1025+
ifdef LIBPFM4
1026+
$(call feature_check,libpfm4)
1027+
ifeq ($(feature-libpfm4), 1)
1028+
CFLAGS += -DHAVE_LIBPFM
1029+
EXTLIBS += -lpfm
1030+
ASCIIDOC_EXTRA = -aHAVE_LIBPFM=1
1031+
$(call detected,CONFIG_LIBPFM4)
1032+
else
1033+
msg := $(warning libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev);
1034+
NO_LIBPFM4 := 1
1035+
endif
1036+
endif
1037+
10251038
# Among the variables below, these:
10261039
# perfexecdir
10271040
# perf_include_dir

tools/perf/Makefile.perf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ include ../scripts/utilities.mak
121121
# Define NO_SYSCALL_TABLE=1 to disable the use of syscall id to/from name tables
122122
# generated from the kernel .tbl or unistd.h files and use, if available, libaudit
123123
# for doing the conversions to/from strings/id.
124+
#
125+
# Define LIBPFM4 to enable libpfm4 events extension.
126+
#
124127

125128
# As per kernel Makefile, avoid funny character set dependencies
126129
unexport LC_ALL

tools/perf/builtin-record.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "util/units.h"
4646
#include "util/bpf-event.h"
4747
#include "util/util.h"
48+
#include "util/pfm.h"
4849
#include "asm/bug.h"
4950
#include "perf.h"
5051

@@ -2555,6 +2556,11 @@ static struct option __record_options[] = {
25552556
OPT_UINTEGER(0, "num-thread-synthesize",
25562557
&record.opts.nr_threads_synthesize,
25572558
"number of threads to run for event synthesis"),
2559+
#ifdef HAVE_LIBPFM
2560+
OPT_CALLBACK(0, "pfm-events", &record.evlist, "event",
2561+
"libpfm4 event selector. use 'perf list' to list available events",
2562+
parse_libpfm_events_option),
2563+
#endif
25582564
OPT_END()
25592565
};
25602566

tools/perf/builtin-stat.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "util/time-utils.h"
6767
#include "util/top.h"
6868
#include "util/affinity.h"
69+
#include "util/pfm.h"
6970
#include "asm/bug.h"
7071

7172
#include <linux/time64.h>
@@ -1012,6 +1013,11 @@ static struct option stat_options[] = {
10121013
"Use with 'percore' event qualifier to show the event "
10131014
"counts of one hardware thread by sum up total hardware "
10141015
"threads of same physical core"),
1016+
#ifdef HAVE_LIBPFM
1017+
OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
1018+
"libpfm4 event selector. use 'perf list' to list available events",
1019+
parse_libpfm_events_option),
1020+
#endif
10151021
OPT_END()
10161022
};
10171023

tools/perf/builtin-top.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353

5454
#include "util/debug.h"
5555
#include "util/ordered-events.h"
56+
#include "util/pfm.h"
5657

5758
#include <assert.h>
5859
#include <elf.h>
@@ -1575,6 +1576,11 @@ int cmd_top(int argc, const char **argv)
15751576
"WARNING: should be used on grouped events."),
15761577
OPT_BOOLEAN(0, "stitch-lbr", &top.stitch_lbr,
15771578
"Enable LBR callgraph stitching approach"),
1579+
#ifdef HAVE_LIBPFM
1580+
OPT_CALLBACK(0, "pfm-events", &top.evlist, "event",
1581+
"libpfm4 event selector. use 'perf list' to list available events",
1582+
parse_libpfm_events_option),
1583+
#endif
15781584
OPTS_EVSWITCH(&top.evswitch),
15791585
OPT_END()
15801586
};

tools/perf/tests/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ perf-y += time-utils-test.o
5858
perf-y += genelf.o
5959
perf-y += api-io.o
6060
perf-y += demangle-java-test.o
61+
perf-y += pfm.o
6162

6263
$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
6364
$(call rule_mkdir)

tools/perf/tests/builtin-test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,15 @@ static struct test generic_tests[] = {
316316
.desc = "Test jit_write_elf",
317317
.func = test__jit_write_elf,
318318
},
319+
{
320+
.desc = "Test libpfm4 support",
321+
.func = test__pfm,
322+
.subtest = {
323+
.skip_if_fail = true,
324+
.get_nr = test__pfm_subtest_get_nr,
325+
.get_desc = test__pfm_subtest_get_desc,
326+
}
327+
},
319328
{
320329
.desc = "Test api io",
321330
.func = test__api_io,

0 commit comments

Comments
 (0)