Skip to content

Commit 2e85d59

Browse files
Ravi Bangoriaacmel
authored andcommitted
perf test: Add watchpoint test
We don't have a 'perf test' entry available to test the watchpoint functionality. Add a simple set of tests: - Read only watchpoint - Write only watchpoint - Read / Write watchpoint - Runtime watchpoint modification Ex.: on powerpc: $ sudo perf test 22 22: Watchpoint : 22.1: Read Only Watchpoint : Ok 22.2: Write Only Watchpoint : Ok 22.3: Read / Write Watchpoint : Ok 22.4: Modify Watchpoint : Ok Signed-off-by: Ravi Bangoria <[email protected]> Acked-by: Jiri Olsa <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Hendrik Brueckner <[email protected]> Cc: Kate Stewart <[email protected]> Cc: Kim Phillips <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Naveen N. Rao <[email protected]> Cc: Sandipan Das <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Thomas Richter <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 1627314 commit 2e85d59

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed

tools/perf/tests/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ perf-y += python-use.o
2121
perf-y += bp_signal.o
2222
perf-y += bp_signal_overflow.o
2323
perf-y += bp_account.o
24+
perf-y += wp.o
2425
perf-y += task-exit.o
2526
perf-y += sw-clock.o
2627
perf-y += mmap-thread-lookup.o

tools/perf/tests/builtin-test.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,15 @@ static struct test generic_tests[] = {
120120
.func = test__bp_accounting,
121121
.is_supported = test__bp_signal_is_supported,
122122
},
123+
{
124+
.desc = "Watchpoint",
125+
.func = test__wp,
126+
.subtest = {
127+
.skip_if_fail = false,
128+
.get_nr = test__wp_subtest_get_nr,
129+
.get_desc = test__wp_subtest_get_desc,
130+
},
131+
},
123132
{
124133
.desc = "Number of exit events of a simple workload",
125134
.func = test__task_exit,

tools/perf/tests/tests.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ int test__python_use(struct test *test, int subtest);
5959
int test__bp_signal(struct test *test, int subtest);
6060
int test__bp_signal_overflow(struct test *test, int subtest);
6161
int test__bp_accounting(struct test *test, int subtest);
62+
int test__wp(struct test *test, int subtest);
63+
const char *test__wp_subtest_get_desc(int subtest);
64+
int test__wp_subtest_get_nr(void);
6265
int test__task_exit(struct test *test, int subtest);
6366
int test__mem(struct test *test, int subtest);
6467
int test__sw_clock_freq(struct test *test, int subtest);

tools/perf/tests/wp.c

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <stdlib.h>
3+
#include <sys/ioctl.h>
4+
#include <linux/hw_breakpoint.h>
5+
#include "tests.h"
6+
#include "debug.h"
7+
#include "cloexec.h"
8+
9+
#define WP_TEST_ASSERT_VAL(fd, text, val) \
10+
do { \
11+
long long count; \
12+
wp_read(fd, &count, sizeof(long long)); \
13+
TEST_ASSERT_VAL(text, count == val); \
14+
} while (0)
15+
16+
volatile u64 data1;
17+
volatile u8 data2[3];
18+
19+
static int wp_read(int fd, long long *count, int size)
20+
{
21+
int ret = read(fd, count, size);
22+
23+
if (ret != size) {
24+
pr_debug("failed to read: %d\n", ret);
25+
return -1;
26+
}
27+
return 0;
28+
}
29+
30+
static void get__perf_event_attr(struct perf_event_attr *attr, int wp_type,
31+
void *wp_addr, unsigned long wp_len)
32+
{
33+
memset(attr, 0, sizeof(struct perf_event_attr));
34+
attr->type = PERF_TYPE_BREAKPOINT;
35+
attr->size = sizeof(struct perf_event_attr);
36+
attr->config = 0;
37+
attr->bp_type = wp_type;
38+
attr->bp_addr = (unsigned long)wp_addr;
39+
attr->bp_len = wp_len;
40+
attr->sample_period = 1;
41+
attr->sample_type = PERF_SAMPLE_IP;
42+
attr->exclude_kernel = 1;
43+
attr->exclude_hv = 1;
44+
}
45+
46+
static int __event(int wp_type, void *wp_addr, unsigned long wp_len)
47+
{
48+
int fd;
49+
struct perf_event_attr attr;
50+
51+
get__perf_event_attr(&attr, wp_type, wp_addr, wp_len);
52+
fd = sys_perf_event_open(&attr, 0, -1, -1,
53+
perf_event_open_cloexec_flag());
54+
if (fd < 0)
55+
pr_debug("failed opening event %x\n", attr.bp_type);
56+
57+
return fd;
58+
}
59+
60+
static int wp_ro_test(void)
61+
{
62+
int fd;
63+
unsigned long tmp, tmp1 = rand();
64+
65+
fd = __event(HW_BREAKPOINT_R, (void *)&data1, sizeof(data1));
66+
if (fd < 0)
67+
return -1;
68+
69+
tmp = data1;
70+
WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
71+
72+
data1 = tmp1 + tmp;
73+
WP_TEST_ASSERT_VAL(fd, "RO watchpoint", 1);
74+
75+
close(fd);
76+
return 0;
77+
}
78+
79+
static int wp_wo_test(void)
80+
{
81+
int fd;
82+
unsigned long tmp, tmp1 = rand();
83+
84+
fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
85+
if (fd < 0)
86+
return -1;
87+
88+
tmp = data1;
89+
WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 0);
90+
91+
data1 = tmp1 + tmp;
92+
WP_TEST_ASSERT_VAL(fd, "WO watchpoint", 1);
93+
94+
close(fd);
95+
return 0;
96+
}
97+
98+
static int wp_rw_test(void)
99+
{
100+
int fd;
101+
unsigned long tmp, tmp1 = rand();
102+
103+
fd = __event(HW_BREAKPOINT_R | HW_BREAKPOINT_W, (void *)&data1,
104+
sizeof(data1));
105+
if (fd < 0)
106+
return -1;
107+
108+
tmp = data1;
109+
WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 1);
110+
111+
data1 = tmp1 + tmp;
112+
WP_TEST_ASSERT_VAL(fd, "RW watchpoint", 2);
113+
114+
close(fd);
115+
return 0;
116+
}
117+
118+
static int wp_modify_test(void)
119+
{
120+
int fd, ret;
121+
unsigned long tmp = rand();
122+
struct perf_event_attr new_attr;
123+
124+
fd = __event(HW_BREAKPOINT_W, (void *)&data1, sizeof(data1));
125+
if (fd < 0)
126+
return -1;
127+
128+
data1 = tmp;
129+
WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
130+
131+
/* Modify watchpoint with disabled = 1 */
132+
get__perf_event_attr(&new_attr, HW_BREAKPOINT_W, (void *)&data2[0],
133+
sizeof(u8) * 2);
134+
new_attr.disabled = 1;
135+
ret = ioctl(fd, PERF_EVENT_IOC_MODIFY_ATTRIBUTES, &new_attr);
136+
if (ret < 0) {
137+
pr_debug("ioctl(PERF_EVENT_IOC_MODIFY_ATTRIBUTES) failed\n");
138+
close(fd);
139+
return ret;
140+
}
141+
142+
data2[1] = tmp; /* Not Counted */
143+
WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 1);
144+
145+
/* Enable the event */
146+
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
147+
if (ret < 0) {
148+
pr_debug("Failed to enable event\n");
149+
close(fd);
150+
return ret;
151+
}
152+
153+
data2[1] = tmp; /* Counted */
154+
WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
155+
156+
data2[2] = tmp; /* Not Counted */
157+
WP_TEST_ASSERT_VAL(fd, "Modify watchpoint", 2);
158+
159+
close(fd);
160+
return 0;
161+
}
162+
163+
static bool wp_ro_supported(void)
164+
{
165+
#if defined (__x86_64__) || defined (__i386__)
166+
return false;
167+
#else
168+
return true;
169+
#endif
170+
}
171+
172+
static void wp_ro_skip_msg(void)
173+
{
174+
#if defined (__x86_64__) || defined (__i386__)
175+
pr_debug("Hardware does not support read only watchpoints.\n");
176+
#endif
177+
}
178+
179+
static struct {
180+
const char *desc;
181+
int (*target_func)(void);
182+
bool (*is_supported)(void);
183+
void (*skip_msg)(void);
184+
} wp_testcase_table[] = {
185+
{
186+
.desc = "Read Only Watchpoint",
187+
.target_func = &wp_ro_test,
188+
.is_supported = &wp_ro_supported,
189+
.skip_msg = &wp_ro_skip_msg,
190+
},
191+
{
192+
.desc = "Write Only Watchpoint",
193+
.target_func = &wp_wo_test,
194+
},
195+
{
196+
.desc = "Read / Write Watchpoint",
197+
.target_func = &wp_rw_test,
198+
},
199+
{
200+
.desc = "Modify Watchpoint",
201+
.target_func = &wp_modify_test,
202+
},
203+
};
204+
205+
int test__wp_subtest_get_nr(void)
206+
{
207+
return (int)ARRAY_SIZE(wp_testcase_table);
208+
}
209+
210+
const char *test__wp_subtest_get_desc(int i)
211+
{
212+
if (i < 0 || i >= (int)ARRAY_SIZE(wp_testcase_table))
213+
return NULL;
214+
return wp_testcase_table[i].desc;
215+
}
216+
217+
int test__wp(struct test *test __maybe_unused, int i)
218+
{
219+
if (i < 0 || i >= (int)ARRAY_SIZE(wp_testcase_table))
220+
return TEST_FAIL;
221+
222+
if (wp_testcase_table[i].is_supported &&
223+
!wp_testcase_table[i].is_supported()) {
224+
wp_testcase_table[i].skip_msg();
225+
return TEST_SKIP;
226+
}
227+
228+
return !wp_testcase_table[i].target_func() ? TEST_OK : TEST_FAIL;
229+
}

0 commit comments

Comments
 (0)