]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - tools/perf/tests/builtin-test.c
Merge remote-tracking branch 'regulator/fix/max77802' into regulator-linus
[mirror_ubuntu-artful-kernel.git] / tools / perf / tests / builtin-test.c
1 /*
2 * builtin-test.c
3 *
4 * Builtin regression testing command: ever growing number of sanity tests
5 */
6 #include <errno.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <sys/wait.h>
10 #include "builtin.h"
11 #include "hist.h"
12 #include "intlist.h"
13 #include "tests.h"
14 #include "debug.h"
15 #include "color.h"
16 #include <subcmd/parse-options.h>
17 #include "symbol.h"
18 #include <linux/kernel.h>
19
20 static bool dont_fork;
21
22 struct test __weak arch_tests[] = {
23 {
24 .func = NULL,
25 },
26 };
27
28 static struct test generic_tests[] = {
29 {
30 .desc = "vmlinux symtab matches kallsyms",
31 .func = test__vmlinux_matches_kallsyms,
32 },
33 {
34 .desc = "Detect openat syscall event",
35 .func = test__openat_syscall_event,
36 },
37 {
38 .desc = "Detect openat syscall event on all cpus",
39 .func = test__openat_syscall_event_on_all_cpus,
40 },
41 {
42 .desc = "Read samples using the mmap interface",
43 .func = test__basic_mmap,
44 },
45 {
46 .desc = "Parse event definition strings",
47 .func = test__parse_events,
48 },
49 {
50 .desc = "Simple expression parser",
51 .func = test__expr,
52 },
53 {
54 .desc = "PERF_RECORD_* events & perf_sample fields",
55 .func = test__PERF_RECORD,
56 },
57 {
58 .desc = "Parse perf pmu format",
59 .func = test__pmu,
60 },
61 {
62 .desc = "DSO data read",
63 .func = test__dso_data,
64 },
65 {
66 .desc = "DSO data cache",
67 .func = test__dso_data_cache,
68 },
69 {
70 .desc = "DSO data reopen",
71 .func = test__dso_data_reopen,
72 },
73 {
74 .desc = "Roundtrip evsel->name",
75 .func = test__perf_evsel__roundtrip_name_test,
76 },
77 {
78 .desc = "Parse sched tracepoints fields",
79 .func = test__perf_evsel__tp_sched_test,
80 },
81 {
82 .desc = "syscalls:sys_enter_openat event fields",
83 .func = test__syscall_openat_tp_fields,
84 },
85 {
86 .desc = "Setup struct perf_event_attr",
87 .func = test__attr,
88 },
89 {
90 .desc = "Match and link multiple hists",
91 .func = test__hists_link,
92 },
93 {
94 .desc = "'import perf' in python",
95 .func = test__python_use,
96 },
97 {
98 .desc = "Breakpoint overflow signal handler",
99 .func = test__bp_signal,
100 .is_supported = test__bp_signal_is_supported,
101 },
102 {
103 .desc = "Breakpoint overflow sampling",
104 .func = test__bp_signal_overflow,
105 .is_supported = test__bp_signal_is_supported,
106 },
107 {
108 .desc = "Number of exit events of a simple workload",
109 .func = test__task_exit,
110 },
111 {
112 .desc = "Software clock events period values",
113 .func = test__sw_clock_freq,
114 },
115 {
116 .desc = "Object code reading",
117 .func = test__code_reading,
118 },
119 {
120 .desc = "Sample parsing",
121 .func = test__sample_parsing,
122 },
123 {
124 .desc = "Use a dummy software event to keep tracking",
125 .func = test__keep_tracking,
126 },
127 {
128 .desc = "Parse with no sample_id_all bit set",
129 .func = test__parse_no_sample_id_all,
130 },
131 {
132 .desc = "Filter hist entries",
133 .func = test__hists_filter,
134 },
135 {
136 .desc = "Lookup mmap thread",
137 .func = test__mmap_thread_lookup,
138 },
139 {
140 .desc = "Share thread mg",
141 .func = test__thread_mg_share,
142 },
143 {
144 .desc = "Sort output of hist entries",
145 .func = test__hists_output,
146 },
147 {
148 .desc = "Cumulate child hist entries",
149 .func = test__hists_cumulate,
150 },
151 {
152 .desc = "Track with sched_switch",
153 .func = test__switch_tracking,
154 },
155 {
156 .desc = "Filter fds with revents mask in a fdarray",
157 .func = test__fdarray__filter,
158 },
159 {
160 .desc = "Add fd to a fdarray, making it autogrow",
161 .func = test__fdarray__add,
162 },
163 {
164 .desc = "kmod_path__parse",
165 .func = test__kmod_path__parse,
166 },
167 {
168 .desc = "Thread map",
169 .func = test__thread_map,
170 },
171 {
172 .desc = "LLVM search and compile",
173 .func = test__llvm,
174 .subtest = {
175 .skip_if_fail = true,
176 .get_nr = test__llvm_subtest_get_nr,
177 .get_desc = test__llvm_subtest_get_desc,
178 },
179 },
180 {
181 .desc = "Session topology",
182 .func = test_session_topology,
183 },
184 {
185 .desc = "BPF filter",
186 .func = test__bpf,
187 .subtest = {
188 .skip_if_fail = true,
189 .get_nr = test__bpf_subtest_get_nr,
190 .get_desc = test__bpf_subtest_get_desc,
191 },
192 },
193 {
194 .desc = "Synthesize thread map",
195 .func = test__thread_map_synthesize,
196 },
197 {
198 .desc = "Remove thread map",
199 .func = test__thread_map_remove,
200 },
201 {
202 .desc = "Synthesize cpu map",
203 .func = test__cpu_map_synthesize,
204 },
205 {
206 .desc = "Synthesize stat config",
207 .func = test__synthesize_stat_config,
208 },
209 {
210 .desc = "Synthesize stat",
211 .func = test__synthesize_stat,
212 },
213 {
214 .desc = "Synthesize stat round",
215 .func = test__synthesize_stat_round,
216 },
217 {
218 .desc = "Synthesize attr update",
219 .func = test__event_update,
220 },
221 {
222 .desc = "Event times",
223 .func = test__event_times,
224 },
225 {
226 .desc = "Read backward ring buffer",
227 .func = test__backward_ring_buffer,
228 },
229 {
230 .desc = "Print cpu map",
231 .func = test__cpu_map_print,
232 },
233 {
234 .desc = "Probe SDT events",
235 .func = test__sdt_event,
236 },
237 {
238 .desc = "is_printable_array",
239 .func = test__is_printable_array,
240 },
241 {
242 .desc = "Print bitmap",
243 .func = test__bitmap_print,
244 },
245 {
246 .desc = "perf hooks",
247 .func = test__perf_hooks,
248 },
249 {
250 .desc = "builtin clang support",
251 .func = test__clang,
252 .subtest = {
253 .skip_if_fail = true,
254 .get_nr = test__clang_subtest_get_nr,
255 .get_desc = test__clang_subtest_get_desc,
256 }
257 },
258 {
259 .desc = "unit_number__scnprintf",
260 .func = test__unit_number__scnprint,
261 },
262 {
263 .func = NULL,
264 },
265 };
266
267 static struct test *tests[] = {
268 generic_tests,
269 arch_tests,
270 };
271
272 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
273 {
274 int i;
275
276 if (argc == 0)
277 return true;
278
279 for (i = 0; i < argc; ++i) {
280 char *end;
281 long nr = strtoul(argv[i], &end, 10);
282
283 if (*end == '\0') {
284 if (nr == curr + 1)
285 return true;
286 continue;
287 }
288
289 if (strcasestr(test->desc, argv[i]))
290 return true;
291 }
292
293 return false;
294 }
295
296 static int run_test(struct test *test, int subtest)
297 {
298 int status, err = -1, child = dont_fork ? 0 : fork();
299 char sbuf[STRERR_BUFSIZE];
300
301 if (child < 0) {
302 pr_err("failed to fork test: %s\n",
303 str_error_r(errno, sbuf, sizeof(sbuf)));
304 return -1;
305 }
306
307 if (!child) {
308 if (!dont_fork) {
309 pr_debug("test child forked, pid %d\n", getpid());
310
311 if (verbose <= 0) {
312 int nullfd = open("/dev/null", O_WRONLY);
313
314 if (nullfd >= 0) {
315 close(STDERR_FILENO);
316 close(STDOUT_FILENO);
317
318 dup2(nullfd, STDOUT_FILENO);
319 dup2(STDOUT_FILENO, STDERR_FILENO);
320 close(nullfd);
321 }
322 } else {
323 signal(SIGSEGV, sighandler_dump_stack);
324 signal(SIGFPE, sighandler_dump_stack);
325 }
326 }
327
328 err = test->func(subtest);
329 if (!dont_fork)
330 exit(err);
331 }
332
333 if (!dont_fork) {
334 wait(&status);
335
336 if (WIFEXITED(status)) {
337 err = (signed char)WEXITSTATUS(status);
338 pr_debug("test child finished with %d\n", err);
339 } else if (WIFSIGNALED(status)) {
340 err = -1;
341 pr_debug("test child interrupted\n");
342 }
343 }
344
345 return err;
346 }
347
348 #define for_each_test(j, t) \
349 for (j = 0; j < ARRAY_SIZE(tests); j++) \
350 for (t = &tests[j][0]; t->func; t++)
351
352 static int test_and_print(struct test *t, bool force_skip, int subtest)
353 {
354 int err;
355
356 if (!force_skip) {
357 pr_debug("\n--- start ---\n");
358 err = run_test(t, subtest);
359 pr_debug("---- end ----\n");
360 } else {
361 pr_debug("\n--- force skipped ---\n");
362 err = TEST_SKIP;
363 }
364
365 if (!t->subtest.get_nr)
366 pr_debug("%s:", t->desc);
367 else
368 pr_debug("%s subtest %d:", t->desc, subtest);
369
370 switch (err) {
371 case TEST_OK:
372 pr_info(" Ok\n");
373 break;
374 case TEST_SKIP:
375 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
376 break;
377 case TEST_FAIL:
378 default:
379 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
380 break;
381 }
382
383 return err;
384 }
385
386 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
387 {
388 struct test *t;
389 unsigned int j;
390 int i = 0;
391 int width = 0;
392
393 for_each_test(j, t) {
394 int len = strlen(t->desc);
395
396 if (width < len)
397 width = len;
398 }
399
400 for_each_test(j, t) {
401 int curr = i++, err;
402
403 if (!perf_test__matches(t, curr, argc, argv))
404 continue;
405
406 if (t->is_supported && !t->is_supported()) {
407 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
408 continue;
409 }
410
411 pr_info("%2d: %-*s:", i, width, t->desc);
412
413 if (intlist__find(skiplist, i)) {
414 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
415 continue;
416 }
417
418 if (!t->subtest.get_nr) {
419 test_and_print(t, false, -1);
420 } else {
421 int subn = t->subtest.get_nr();
422 /*
423 * minus 2 to align with normal testcases.
424 * For subtest we print additional '.x' in number.
425 * for example:
426 *
427 * 35: Test LLVM searching and compiling :
428 * 35.1: Basic BPF llvm compiling test : Ok
429 */
430 int subw = width > 2 ? width - 2 : width;
431 bool skip = false;
432 int subi;
433
434 if (subn <= 0) {
435 color_fprintf(stderr, PERF_COLOR_YELLOW,
436 " Skip (not compiled in)\n");
437 continue;
438 }
439 pr_info("\n");
440
441 for (subi = 0; subi < subn; subi++) {
442 int len = strlen(t->subtest.get_desc(subi));
443
444 if (subw < len)
445 subw = len;
446 }
447
448 for (subi = 0; subi < subn; subi++) {
449 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
450 t->subtest.get_desc(subi));
451 err = test_and_print(t, skip, subi);
452 if (err != TEST_OK && t->subtest.skip_if_fail)
453 skip = true;
454 }
455 }
456 }
457
458 return 0;
459 }
460
461 static int perf_test__list(int argc, const char **argv)
462 {
463 unsigned int j;
464 struct test *t;
465 int i = 0;
466
467 for_each_test(j, t) {
468 if (argc > 1 && !strstr(t->desc, argv[1]))
469 continue;
470
471 pr_info("%2d: %s\n", ++i, t->desc);
472 }
473
474 return 0;
475 }
476
477 int cmd_test(int argc, const char **argv)
478 {
479 const char *test_usage[] = {
480 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
481 NULL,
482 };
483 const char *skip = NULL;
484 const struct option test_options[] = {
485 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
486 OPT_INCR('v', "verbose", &verbose,
487 "be more verbose (show symbol address, etc)"),
488 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
489 "Do not fork for testcase"),
490 OPT_END()
491 };
492 const char * const test_subcommands[] = { "list", NULL };
493 struct intlist *skiplist = NULL;
494 int ret = hists__init();
495
496 if (ret < 0)
497 return ret;
498
499 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
500 if (argc >= 1 && !strcmp(argv[0], "list"))
501 return perf_test__list(argc, argv);
502
503 symbol_conf.priv_size = sizeof(int);
504 symbol_conf.sort_by_name = true;
505 symbol_conf.try_vmlinux_path = true;
506
507 if (symbol__init(NULL) < 0)
508 return -1;
509
510 if (skip != NULL)
511 skiplist = intlist__new(skip);
512
513 return __cmd_test(argc, argv, skiplist);
514 }