Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 1 | /* |
| 2 | * builtin-test.c |
| 3 | * |
| 4 | * Builtin regression testing command: ever growing number of sanity tests |
| 5 | */ |
Jiri Olsa | 0d8a5fa | 2014-05-10 17:22:30 +0200 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | #include <string.h> |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 8 | #include "builtin.h" |
Arnaldo Carvalho de Melo | 2ae8287 | 2013-01-24 16:22:55 -0300 | [diff] [blame] | 9 | #include "intlist.h" |
Jiri Olsa | 0a4e1ae | 2012-11-10 01:46:41 +0100 | [diff] [blame] | 10 | #include "tests.h" |
Jiri Olsa | c81251e | 2012-11-10 01:46:51 +0100 | [diff] [blame] | 11 | #include "debug.h" |
| 12 | #include "color.h" |
| 13 | #include "parse-options.h" |
| 14 | #include "symbol.h" |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 15 | |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 16 | static struct test { |
| 17 | const char *desc; |
| 18 | int (*func)(void); |
| 19 | } tests[] = { |
| 20 | { |
| 21 | .desc = "vmlinux symtab matches kallsyms", |
| 22 | .func = test__vmlinux_matches_kallsyms, |
| 23 | }, |
| 24 | { |
Arnaldo Carvalho de Melo | d854861 | 2011-01-04 00:16:20 -0200 | [diff] [blame] | 25 | .desc = "detect open syscall event", |
| 26 | .func = test__open_syscall_event, |
| 27 | }, |
| 28 | { |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 29 | .desc = "detect open syscall event on all cpus", |
| 30 | .func = test__open_syscall_event_on_all_cpus, |
| 31 | }, |
| 32 | { |
Arnaldo Carvalho de Melo | de5fa3a | 2011-01-15 10:42:46 -0200 | [diff] [blame] | 33 | .desc = "read samples using the mmap interface", |
| 34 | .func = test__basic_mmap, |
| 35 | }, |
| 36 | { |
Jiri Olsa | 13b6256 | 2011-07-14 11:25:33 +0200 | [diff] [blame] | 37 | .desc = "parse events tests", |
Jiri Olsa | c81251e | 2012-11-10 01:46:51 +0100 | [diff] [blame] | 38 | .func = test__parse_events, |
Jiri Olsa | 13b6256 | 2011-07-14 11:25:33 +0200 | [diff] [blame] | 39 | }, |
Peter Zijlstra | 08aa0d1 | 2011-11-21 14:42:47 +0100 | [diff] [blame] | 40 | #if defined(__x86_64__) || defined(__i386__) |
| 41 | { |
| 42 | .desc = "x86 rdpmc test", |
| 43 | .func = test__rdpmc, |
| 44 | }, |
| 45 | #endif |
Jiri Olsa | 13b6256 | 2011-07-14 11:25:33 +0200 | [diff] [blame] | 46 | { |
Arnaldo Carvalho de Melo | 3e7c439 | 2011-12-02 11:13:50 -0200 | [diff] [blame] | 47 | .desc = "Validate PERF_RECORD_* events & perf_sample fields", |
| 48 | .func = test__PERF_RECORD, |
| 49 | }, |
| 50 | { |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 51 | .desc = "Test perf pmu format parsing", |
Jiri Olsa | cff7f95 | 2012-11-10 01:46:50 +0100 | [diff] [blame] | 52 | .func = test__pmu, |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 53 | }, |
| 54 | { |
Jiri Olsa | 4ebbcb8 | 2014-05-12 14:43:53 +0200 | [diff] [blame^] | 55 | .desc = "Test dso data read", |
Jiri Olsa | c81251e | 2012-11-10 01:46:51 +0100 | [diff] [blame] | 56 | .func = test__dso_data, |
Jiri Olsa | f7add55 | 2012-07-22 14:14:40 +0200 | [diff] [blame] | 57 | }, |
| 58 | { |
Jiri Olsa | 4ebbcb8 | 2014-05-12 14:43:53 +0200 | [diff] [blame^] | 59 | .desc = "Test dso data cache", |
| 60 | .func = test__dso_data_cache, |
| 61 | }, |
| 62 | { |
Arnaldo Carvalho de Melo | 8ad7013 | 2012-09-06 13:11:18 -0300 | [diff] [blame] | 63 | .desc = "roundtrip evsel->name check", |
Jiri Olsa | cfffae2 | 2012-11-10 01:46:47 +0100 | [diff] [blame] | 64 | .func = test__perf_evsel__roundtrip_name_test, |
Arnaldo Carvalho de Melo | 8ad7013 | 2012-09-06 13:11:18 -0300 | [diff] [blame] | 65 | }, |
| 66 | { |
Arnaldo Carvalho de Melo | 6a6cd11 | 2012-09-18 11:56:28 -0300 | [diff] [blame] | 67 | .desc = "Check parsing of sched tracepoints fields", |
Jiri Olsa | 5e24a09 | 2012-11-10 01:46:48 +0100 | [diff] [blame] | 68 | .func = test__perf_evsel__tp_sched_test, |
Arnaldo Carvalho de Melo | 6a6cd11 | 2012-09-18 11:56:28 -0300 | [diff] [blame] | 69 | }, |
| 70 | { |
Arnaldo Carvalho de Melo | eb2f270 | 2012-09-26 13:23:10 -0300 | [diff] [blame] | 71 | .desc = "Generate and check syscalls:sys_enter_open event fields", |
| 72 | .func = test__syscall_open_tp_fields, |
| 73 | }, |
| 74 | { |
Jiri Olsa | d898b24 | 2012-10-30 23:02:05 +0100 | [diff] [blame] | 75 | .desc = "struct perf_event_attr setup", |
Jiri Olsa | c81251e | 2012-11-10 01:46:51 +0100 | [diff] [blame] | 76 | .func = test__attr, |
Jiri Olsa | d898b24 | 2012-10-30 23:02:05 +0100 | [diff] [blame] | 77 | }, |
| 78 | { |
Arnaldo Carvalho de Melo | ffcbaa1 | 2013-05-23 12:08:38 +0200 | [diff] [blame] | 79 | .desc = "Test matching and linking multiple hists", |
Namhyung Kim | f8ebb0c | 2012-12-10 17:29:57 +0900 | [diff] [blame] | 80 | .func = test__hists_link, |
| 81 | }, |
| 82 | { |
Arnaldo Carvalho de Melo | 54359d3 | 2012-12-14 13:06:13 -0300 | [diff] [blame] | 83 | .desc = "Try 'use perf' in python, checking link problems", |
| 84 | .func = test__python_use, |
| 85 | }, |
| 86 | { |
Jiri Olsa | 5a6bef4 | 2013-03-10 19:41:10 +0100 | [diff] [blame] | 87 | .desc = "Test breakpoint overflow signal handler", |
| 88 | .func = test__bp_signal, |
| 89 | }, |
| 90 | { |
Jiri Olsa | 06933e3 | 2013-03-10 19:41:11 +0100 | [diff] [blame] | 91 | .desc = "Test breakpoint overflow sampling", |
| 92 | .func = test__bp_signal_overflow, |
| 93 | }, |
| 94 | { |
Namhyung Kim | d723a55 | 2013-03-15 14:58:11 +0900 | [diff] [blame] | 95 | .desc = "Test number of exit event of a simple workload", |
| 96 | .func = test__task_exit, |
| 97 | }, |
| 98 | { |
Namhyung Kim | bc96b36 | 2013-03-18 11:41:47 +0900 | [diff] [blame] | 99 | .desc = "Test software clock events have valid period values", |
| 100 | .func = test__sw_clock_freq, |
| 101 | }, |
Adrian Hunter | 3bd5a5f | 2013-06-28 16:22:19 +0300 | [diff] [blame] | 102 | #if defined(__x86_64__) || defined(__i386__) |
| 103 | { |
| 104 | .desc = "Test converting perf time to TSC", |
| 105 | .func = test__perf_time_to_tsc, |
| 106 | }, |
| 107 | #endif |
Namhyung Kim | bc96b36 | 2013-03-18 11:41:47 +0900 | [diff] [blame] | 108 | { |
Adrian Hunter | b55ae0a | 2013-08-07 14:38:45 +0300 | [diff] [blame] | 109 | .desc = "Test object code reading", |
| 110 | .func = test__code_reading, |
| 111 | }, |
| 112 | { |
Adrian Hunter | 045f8cd8 | 2013-08-27 11:23:13 +0300 | [diff] [blame] | 113 | .desc = "Test sample parsing", |
| 114 | .func = test__sample_parsing, |
| 115 | }, |
| 116 | { |
Adrian Hunter | 395c307 | 2013-08-31 21:50:53 +0300 | [diff] [blame] | 117 | .desc = "Test using a dummy software event to keep tracking", |
| 118 | .func = test__keep_tracking, |
| 119 | }, |
| 120 | { |
Adrian Hunter | 53a277e | 2013-09-04 23:18:16 +0300 | [diff] [blame] | 121 | .desc = "Test parsing with no sample_id_all bit set", |
| 122 | .func = test__parse_no_sample_id_all, |
| 123 | }, |
Jean Pihet | 90fa9de | 2014-05-16 10:41:11 +0200 | [diff] [blame] | 124 | #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) |
Jiri Olsa | 9ff125d | 2014-01-07 13:47:28 +0100 | [diff] [blame] | 125 | #ifdef HAVE_DWARF_UNWIND_SUPPORT |
Jiri Olsa | aa16b81 | 2014-01-07 13:47:22 +0100 | [diff] [blame] | 126 | { |
| 127 | .desc = "Test dwarf unwind", |
| 128 | .func = test__dwarf_unwind, |
| 129 | }, |
| 130 | #endif |
| 131 | #endif |
Adrian Hunter | 53a277e | 2013-09-04 23:18:16 +0300 | [diff] [blame] | 132 | { |
Namhyung Kim | 3c3cfd9 | 2014-04-25 12:28:14 +0900 | [diff] [blame] | 133 | .desc = "Test filtering hist entries", |
| 134 | .func = test__hists_filter, |
| 135 | }, |
| 136 | { |
Jiri Olsa | 4e85edf | 2014-03-05 17:20:31 +0100 | [diff] [blame] | 137 | .desc = "Test mmap thread lookup", |
| 138 | .func = test__mmap_thread_lookup, |
| 139 | }, |
| 140 | { |
Jiri Olsa | fabf01238 | 2014-03-17 14:39:00 +0100 | [diff] [blame] | 141 | .desc = "Test thread mg sharing", |
| 142 | .func = test__thread_mg_share, |
| 143 | }, |
| 144 | { |
Namhyung Kim | f21d181 | 2014-05-12 14:43:18 +0900 | [diff] [blame] | 145 | .desc = "Test output sorting of hist entries", |
| 146 | .func = test__hists_output, |
| 147 | }, |
| 148 | { |
Namhyung Kim | 0506aec | 2014-05-23 18:04:42 +0900 | [diff] [blame] | 149 | .desc = "Test cumulation of child hist entries", |
| 150 | .func = test__hists_cumulate, |
| 151 | }, |
| 152 | { |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 153 | .func = NULL, |
| 154 | }, |
| 155 | }; |
| 156 | |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 157 | static bool perf_test__matches(int curr, int argc, const char *argv[]) |
| 158 | { |
| 159 | int i; |
| 160 | |
| 161 | if (argc == 0) |
| 162 | return true; |
| 163 | |
| 164 | for (i = 0; i < argc; ++i) { |
| 165 | char *end; |
| 166 | long nr = strtoul(argv[i], &end, 10); |
| 167 | |
| 168 | if (*end == '\0') { |
| 169 | if (nr == curr + 1) |
| 170 | return true; |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | if (strstr(tests[curr].desc, argv[i])) |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
Jiri Olsa | 0d8a5fa | 2014-05-10 17:22:30 +0200 | [diff] [blame] | 181 | static int run_test(struct test *test) |
| 182 | { |
| 183 | int status, err = -1, child = fork(); |
| 184 | |
| 185 | if (child < 0) { |
| 186 | pr_err("failed to fork test: %s\n", strerror(errno)); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | if (!child) { |
| 191 | pr_debug("test child forked, pid %d\n", getpid()); |
| 192 | err = test->func(); |
| 193 | exit(err); |
| 194 | } |
| 195 | |
| 196 | wait(&status); |
| 197 | |
| 198 | if (WIFEXITED(status)) { |
| 199 | err = WEXITSTATUS(status); |
| 200 | pr_debug("test child finished with %d\n", err); |
| 201 | } else if (WIFSIGNALED(status)) { |
| 202 | err = -1; |
| 203 | pr_debug("test child interrupted\n"); |
| 204 | } |
| 205 | |
| 206 | return err; |
| 207 | } |
| 208 | |
Arnaldo Carvalho de Melo | 2ae8287 | 2013-01-24 16:22:55 -0300 | [diff] [blame] | 209 | static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 210 | { |
| 211 | int i = 0; |
Arnaldo Carvalho de Melo | 9a8e85a | 2012-10-24 15:44:41 -0200 | [diff] [blame] | 212 | int width = 0; |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 213 | |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 214 | while (tests[i].func) { |
Arnaldo Carvalho de Melo | 9a8e85a | 2012-10-24 15:44:41 -0200 | [diff] [blame] | 215 | int len = strlen(tests[i].desc); |
| 216 | |
| 217 | if (width < len) |
| 218 | width = len; |
| 219 | ++i; |
| 220 | } |
Jiri Olsa | 945aea2 | 2012-10-30 23:01:43 +0100 | [diff] [blame] | 221 | |
Arnaldo Carvalho de Melo | 9a8e85a | 2012-10-24 15:44:41 -0200 | [diff] [blame] | 222 | i = 0; |
| 223 | while (tests[i].func) { |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 224 | int curr = i++, err; |
| 225 | |
| 226 | if (!perf_test__matches(curr, argc, argv)) |
| 227 | continue; |
| 228 | |
Arnaldo Carvalho de Melo | 9a8e85a | 2012-10-24 15:44:41 -0200 | [diff] [blame] | 229 | pr_info("%2d: %-*s:", i, width, tests[curr].desc); |
Arnaldo Carvalho de Melo | 2ae8287 | 2013-01-24 16:22:55 -0300 | [diff] [blame] | 230 | |
| 231 | if (intlist__find(skiplist, i)) { |
| 232 | color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); |
| 233 | continue; |
| 234 | } |
| 235 | |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 236 | pr_debug("\n--- start ---\n"); |
Jiri Olsa | 0d8a5fa | 2014-05-10 17:22:30 +0200 | [diff] [blame] | 237 | err = run_test(&tests[curr]); |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 238 | pr_debug("---- end ----\n%s:", tests[curr].desc); |
Jiri Olsa | f4c1ea5 | 2012-12-19 11:33:39 -0300 | [diff] [blame] | 239 | |
| 240 | switch (err) { |
| 241 | case TEST_OK: |
Arnaldo Carvalho de Melo | 9a8e85a | 2012-10-24 15:44:41 -0200 | [diff] [blame] | 242 | pr_info(" Ok\n"); |
Jiri Olsa | f4c1ea5 | 2012-12-19 11:33:39 -0300 | [diff] [blame] | 243 | break; |
| 244 | case TEST_SKIP: |
| 245 | color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n"); |
| 246 | break; |
| 247 | case TEST_FAIL: |
| 248 | default: |
| 249 | color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n"); |
| 250 | break; |
| 251 | } |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 257 | static int perf_test__list(int argc, const char **argv) |
| 258 | { |
| 259 | int i = 0; |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 260 | |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 261 | while (tests[i].func) { |
| 262 | int curr = i++; |
| 263 | |
| 264 | if (argc > 1 && !strstr(tests[curr].desc, argv[1])) |
| 265 | continue; |
| 266 | |
| 267 | pr_info("%2d: %s\n", i, tests[curr].desc); |
| 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 272 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 273 | int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused) |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 274 | { |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 275 | const char * const test_usage[] = { |
| 276 | "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", |
| 277 | NULL, |
| 278 | }; |
Arnaldo Carvalho de Melo | 2ae8287 | 2013-01-24 16:22:55 -0300 | [diff] [blame] | 279 | const char *skip = NULL; |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 280 | const struct option test_options[] = { |
Arnaldo Carvalho de Melo | 2ae8287 | 2013-01-24 16:22:55 -0300 | [diff] [blame] | 281 | OPT_STRING('s', "skip", &skip, "tests", "tests to skip"), |
Namhyung Kim | c30ab8a | 2012-01-08 02:25:26 +0900 | [diff] [blame] | 282 | OPT_INCR('v', "verbose", &verbose, |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 283 | "be more verbose (show symbol address, etc)"), |
| 284 | OPT_END() |
| 285 | }; |
Arnaldo Carvalho de Melo | 2ae8287 | 2013-01-24 16:22:55 -0300 | [diff] [blame] | 286 | struct intlist *skiplist = NULL; |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 287 | |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 288 | argc = parse_options(argc, argv, test_options, test_usage, 0); |
Arnaldo Carvalho de Melo | e60770a | 2011-11-29 12:52:07 -0200 | [diff] [blame] | 289 | if (argc >= 1 && !strcmp(argv[0], "list")) |
| 290 | return perf_test__list(argc, argv); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 291 | |
| 292 | symbol_conf.priv_size = sizeof(int); |
| 293 | symbol_conf.sort_by_name = true; |
| 294 | symbol_conf.try_vmlinux_path = true; |
| 295 | |
| 296 | if (symbol__init() < 0) |
| 297 | return -1; |
| 298 | |
Arnaldo Carvalho de Melo | 2ae8287 | 2013-01-24 16:22:55 -0300 | [diff] [blame] | 299 | if (skip != NULL) |
| 300 | skiplist = intlist__new(skip); |
| 301 | |
| 302 | return __cmd_test(argc, argv, skiplist); |
Arnaldo Carvalho de Melo | 1c6a800 | 2010-04-29 18:58:32 -0300 | [diff] [blame] | 303 | } |