aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/arch
diff options
context:
space:
mode:
authorMatt Fleming <matt.fleming@intel.com>2015-10-05 10:40:21 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2015-10-05 15:56:07 -0400
commit035827e9f2bd71a280f4eb58c65811d377ab2217 (patch)
treefd989e82be49f2363d7c9b5dc0e4ddb86fbe4eaa /tools/perf/arch
parentd8b167f9d8af817073ee35cf904e2e527465dbc1 (diff)
perf tests: Add Intel CQM test
Peter reports that it's possible to trigger a WARN_ON_ONCE() in the Intel CQM code by combining a hardware event and an Intel CQM (software) event into a group. Unfortunately, the perf tools are not able to create this bundle and we need to manually construct a test case. For posterity, record Peter's proof of concept test case in tools/perf so that it presents a model for how we can perform architecture specific tests, or "arch tests", in perf in the future. The particular issue triggered in the test case is that when the counter for the hardware event overflows and triggers a PMI we'll read both the hardware event and the software event counters. Unfortunately, for CQM that involves performing an IPI to read the CQM event counters on all sockets, which in NMI context triggers the WARN_ON_ONCE(). Reported-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Matt Fleming <matt.fleming@intel.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Fenghua Yu <fenghua.yu@intel.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Kanaka Juvva <kanaka.d.juvva@intel.com> Cc: Vikas Shivappa <vikas.shivappa@intel.com> Cc: Vince Weaver <vince@deater.net> Link: http://lkml.kernel.org/r/1437490509-15373-1-git-send-email-matt@codeblueprint.co.uk Link: http://lkml.kernel.org/n/tip-3p4ra0u8vzm7m289a1m799kf@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/arch')
-rw-r--r--tools/perf/arch/x86/include/arch-tests.h1
-rw-r--r--tools/perf/arch/x86/tests/Build1
-rw-r--r--tools/perf/arch/x86/tests/arch-tests.c4
-rw-r--r--tools/perf/arch/x86/tests/intel-cqm.c124
4 files changed, 130 insertions, 0 deletions
diff --git a/tools/perf/arch/x86/include/arch-tests.h b/tools/perf/arch/x86/include/arch-tests.h
index 5927cf224325..7ed00f4b0908 100644
--- a/tools/perf/arch/x86/include/arch-tests.h
+++ b/tools/perf/arch/x86/include/arch-tests.h
@@ -5,6 +5,7 @@
5int test__rdpmc(void); 5int test__rdpmc(void);
6int test__perf_time_to_tsc(void); 6int test__perf_time_to_tsc(void);
7int test__insn_x86(void); 7int test__insn_x86(void);
8int test__intel_cqm_count_nmi_context(void);
8 9
9#ifdef HAVE_DWARF_UNWIND_SUPPORT 10#ifdef HAVE_DWARF_UNWIND_SUPPORT
10struct thread; 11struct thread;
diff --git a/tools/perf/arch/x86/tests/Build b/tools/perf/arch/x86/tests/Build
index 8e2c5a38c3b9..cbb7e978166b 100644
--- a/tools/perf/arch/x86/tests/Build
+++ b/tools/perf/arch/x86/tests/Build
@@ -5,3 +5,4 @@ libperf-y += arch-tests.o
5libperf-y += rdpmc.o 5libperf-y += rdpmc.o
6libperf-y += perf-time-to-tsc.o 6libperf-y += perf-time-to-tsc.o
7libperf-$(CONFIG_AUXTRACE) += insn-x86.o 7libperf-$(CONFIG_AUXTRACE) += insn-x86.o
8libperf-y += intel-cqm.o
diff --git a/tools/perf/arch/x86/tests/arch-tests.c b/tools/perf/arch/x86/tests/arch-tests.c
index d116c217af99..2218cb64f840 100644
--- a/tools/perf/arch/x86/tests/arch-tests.c
+++ b/tools/perf/arch/x86/tests/arch-tests.c
@@ -24,6 +24,10 @@ struct test arch_tests[] = {
24 }, 24 },
25#endif 25#endif
26 { 26 {
27 .desc = "Test intel cqm nmi context read",
28 .func = test__intel_cqm_count_nmi_context,
29 },
30 {
27 .func = NULL, 31 .func = NULL,
28 }, 32 },
29 33
diff --git a/tools/perf/arch/x86/tests/intel-cqm.c b/tools/perf/arch/x86/tests/intel-cqm.c
new file mode 100644
index 000000000000..d28c1b6a3b54
--- /dev/null
+++ b/tools/perf/arch/x86/tests/intel-cqm.c
@@ -0,0 +1,124 @@
1#include "tests/tests.h"
2#include "perf.h"
3#include "cloexec.h"
4#include "debug.h"
5#include "evlist.h"
6#include "evsel.h"
7#include "arch-tests.h"
8
9#include <sys/mman.h>
10#include <string.h>
11
12static pid_t spawn(void)
13{
14 pid_t pid;
15
16 pid = fork();
17 if (pid)
18 return pid;
19
20 while(1);
21 sleep(5);
22 return 0;
23}
24
25/*
26 * Create an event group that contains both a sampled hardware
27 * (cpu-cycles) and software (intel_cqm/llc_occupancy/) event. We then
28 * wait for the hardware perf counter to overflow and generate a PMI,
29 * which triggers an event read for both of the events in the group.
30 *
31 * Since reading Intel CQM event counters requires sending SMP IPIs, the
32 * CQM pmu needs to handle the above situation gracefully, and return
33 * the last read counter value to avoid triggering a WARN_ON_ONCE() in
34 * smp_call_function_many() caused by sending IPIs from NMI context.
35 */
36int test__intel_cqm_count_nmi_context(void)
37{
38 struct perf_evlist *evlist = NULL;
39 struct perf_evsel *evsel = NULL;
40 struct perf_event_attr pe;
41 int i, fd[2], flag, ret;
42 size_t mmap_len;
43 void *event;
44 pid_t pid;
45 int err = TEST_FAIL;
46
47 flag = perf_event_open_cloexec_flag();
48
49 evlist = perf_evlist__new();
50 if (!evlist) {
51 pr_debug("perf_evlist__new failed\n");
52 return TEST_FAIL;
53 }
54
55 ret = parse_events(evlist, "intel_cqm/llc_occupancy/", NULL);
56 if (ret) {
57 pr_debug("parse_events failed\n");
58 err = TEST_SKIP;
59 goto out;
60 }
61
62 evsel = perf_evlist__first(evlist);
63 if (!evsel) {
64 pr_debug("perf_evlist__first failed\n");
65 goto out;
66 }
67
68 memset(&pe, 0, sizeof(pe));
69 pe.size = sizeof(pe);
70
71 pe.type = PERF_TYPE_HARDWARE;
72 pe.config = PERF_COUNT_HW_CPU_CYCLES;
73 pe.read_format = PERF_FORMAT_GROUP;
74
75 pe.sample_period = 128;
76 pe.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_READ;
77
78 pid = spawn();
79
80 fd[0] = sys_perf_event_open(&pe, pid, -1, -1, flag);
81 if (fd[0] < 0) {
82 pr_debug("failed to open event\n");
83 goto out;
84 }
85
86 memset(&pe, 0, sizeof(pe));
87 pe.size = sizeof(pe);
88
89 pe.type = evsel->attr.type;
90 pe.config = evsel->attr.config;
91
92 fd[1] = sys_perf_event_open(&pe, pid, -1, fd[0], flag);
93 if (fd[1] < 0) {
94 pr_debug("failed to open event\n");
95 goto out;
96 }
97
98 /*
99 * Pick a power-of-two number of pages + 1 for the meta-data
100 * page (struct perf_event_mmap_page). See tools/perf/design.txt.
101 */
102 mmap_len = page_size * 65;
103
104 event = mmap(NULL, mmap_len, PROT_READ, MAP_SHARED, fd[0], 0);
105 if (event == (void *)(-1)) {
106 pr_debug("failed to mmap %d\n", errno);
107 goto out;
108 }
109
110 sleep(1);
111
112 err = TEST_OK;
113
114 munmap(event, mmap_len);
115
116 for (i = 0; i < 2; i++)
117 close(fd[i]);
118
119 kill(pid, SIGKILL);
120 wait(NULL);
121out:
122 perf_evlist__delete(evlist);
123 return err;
124}