diff options
| author | Ravi Bangoria <ravi.bangoria@linux.ibm.com> | 2018-09-12 02:12:29 -0400 |
|---|---|---|
| committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2018-09-18 16:21:13 -0400 |
| commit | 2e85d5979e8d2866db6185de231461b21159ef6f (patch) | |
| tree | e4c3b216d88b59c58b0da9f10e665a921078ba83 | |
| parent | 1627314fb54a33ebd23bd08f2e215eaed0f44712 (diff) | |
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 <ravi.bangoria@linux.ibm.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Hendrik Brueckner <brueckner@linux.ibm.com>
Cc: Kate Stewart <kstewart@linuxfoundation.org>
Cc: Kim Phillips <kim.phillips@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Naveen N. Rao <naveen.n.rao@linux.vnet.ibm.com>
Cc: Sandipan Das <sandipan@linux.ibm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Link: http://lkml.kernel.org/r/20180912061229.22832-1-ravi.bangoria@linux.ibm.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
| -rw-r--r-- | tools/perf/tests/Build | 1 | ||||
| -rw-r--r-- | tools/perf/tests/builtin-test.c | 9 | ||||
| -rw-r--r-- | tools/perf/tests/tests.h | 3 | ||||
| -rw-r--r-- | tools/perf/tests/wp.c | 229 |
4 files changed, 242 insertions, 0 deletions
diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build index 6c108fa79ae3..0b2b8305c965 100644 --- a/tools/perf/tests/Build +++ b/tools/perf/tests/Build | |||
| @@ -21,6 +21,7 @@ perf-y += python-use.o | |||
| 21 | perf-y += bp_signal.o | 21 | perf-y += bp_signal.o |
| 22 | perf-y += bp_signal_overflow.o | 22 | perf-y += bp_signal_overflow.o |
| 23 | perf-y += bp_account.o | 23 | perf-y += bp_account.o |
| 24 | perf-y += wp.o | ||
| 24 | perf-y += task-exit.o | 25 | perf-y += task-exit.o |
| 25 | perf-y += sw-clock.o | 26 | perf-y += sw-clock.o |
| 26 | perf-y += mmap-thread-lookup.o | 27 | perf-y += mmap-thread-lookup.o |
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c index d7a5e1b9aa6f..54ca7d87236f 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c | |||
| @@ -121,6 +121,15 @@ static struct test generic_tests[] = { | |||
| 121 | .is_supported = test__bp_signal_is_supported, | 121 | .is_supported = test__bp_signal_is_supported, |
| 122 | }, | 122 | }, |
| 123 | { | 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 | }, | ||
| 132 | { | ||
| 124 | .desc = "Number of exit events of a simple workload", | 133 | .desc = "Number of exit events of a simple workload", |
| 125 | .func = test__task_exit, | 134 | .func = test__task_exit, |
| 126 | }, | 135 | }, |
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h index a9760e790563..8e26a4148f30 100644 --- a/tools/perf/tests/tests.h +++ b/tools/perf/tests/tests.h | |||
| @@ -59,6 +59,9 @@ int test__python_use(struct test *test, int subtest); | |||
| 59 | int test__bp_signal(struct test *test, int subtest); | 59 | int test__bp_signal(struct test *test, int subtest); |
| 60 | int test__bp_signal_overflow(struct test *test, int subtest); | 60 | int test__bp_signal_overflow(struct test *test, int subtest); |
| 61 | int test__bp_accounting(struct test *test, int subtest); | 61 | 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); | ||
| 62 | int test__task_exit(struct test *test, int subtest); | 65 | int test__task_exit(struct test *test, int subtest); |
| 63 | int test__mem(struct test *test, int subtest); | 66 | int test__mem(struct test *test, int subtest); |
| 64 | int test__sw_clock_freq(struct test *test, int subtest); | 67 | int test__sw_clock_freq(struct test *test, int subtest); |
diff --git a/tools/perf/tests/wp.c b/tools/perf/tests/wp.c new file mode 100644 index 000000000000..017a99317f94 --- /dev/null +++ b/tools/perf/tests/wp.c | |||
| @@ -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 | } | ||
