Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux/fpc-iii.git] / tools / perf / tests / builtin-test.c
blob778668a2a9661c7402135d62f4cefe69ceb04001
1 /*
2 * builtin-test.c
4 * Builtin regression testing command: ever growing number of sanity tests
5 */
6 #include <unistd.h>
7 #include <string.h>
8 #include "builtin.h"
9 #include "hist.h"
10 #include "intlist.h"
11 #include "tests.h"
12 #include "debug.h"
13 #include "color.h"
14 #include <subcmd/parse-options.h>
15 #include "symbol.h"
17 static bool dont_fork;
19 struct test __weak arch_tests[] = {
21 .func = NULL,
25 static struct test generic_tests[] = {
27 .desc = "vmlinux symtab matches kallsyms",
28 .func = test__vmlinux_matches_kallsyms,
31 .desc = "detect openat syscall event",
32 .func = test__openat_syscall_event,
35 .desc = "detect openat syscall event on all cpus",
36 .func = test__openat_syscall_event_on_all_cpus,
39 .desc = "read samples using the mmap interface",
40 .func = test__basic_mmap,
43 .desc = "parse events tests",
44 .func = test__parse_events,
47 .desc = "Validate PERF_RECORD_* events & perf_sample fields",
48 .func = test__PERF_RECORD,
51 .desc = "Test perf pmu format parsing",
52 .func = test__pmu,
55 .desc = "Test dso data read",
56 .func = test__dso_data,
59 .desc = "Test dso data cache",
60 .func = test__dso_data_cache,
63 .desc = "Test dso data reopen",
64 .func = test__dso_data_reopen,
67 .desc = "roundtrip evsel->name check",
68 .func = test__perf_evsel__roundtrip_name_test,
71 .desc = "Check parsing of sched tracepoints fields",
72 .func = test__perf_evsel__tp_sched_test,
75 .desc = "Generate and check syscalls:sys_enter_openat event fields",
76 .func = test__syscall_openat_tp_fields,
79 .desc = "struct perf_event_attr setup",
80 .func = test__attr,
83 .desc = "Test matching and linking multiple hists",
84 .func = test__hists_link,
87 .desc = "Try 'import perf' in python, checking link problems",
88 .func = test__python_use,
91 .desc = "Test breakpoint overflow signal handler",
92 .func = test__bp_signal,
95 .desc = "Test breakpoint overflow sampling",
96 .func = test__bp_signal_overflow,
99 .desc = "Test number of exit event of a simple workload",
100 .func = test__task_exit,
103 .desc = "Test software clock events have valid period values",
104 .func = test__sw_clock_freq,
107 .desc = "Test object code reading",
108 .func = test__code_reading,
111 .desc = "Test sample parsing",
112 .func = test__sample_parsing,
115 .desc = "Test using a dummy software event to keep tracking",
116 .func = test__keep_tracking,
119 .desc = "Test parsing with no sample_id_all bit set",
120 .func = test__parse_no_sample_id_all,
123 .desc = "Test filtering hist entries",
124 .func = test__hists_filter,
127 .desc = "Test mmap thread lookup",
128 .func = test__mmap_thread_lookup,
131 .desc = "Test thread mg sharing",
132 .func = test__thread_mg_share,
135 .desc = "Test output sorting of hist entries",
136 .func = test__hists_output,
139 .desc = "Test cumulation of child hist entries",
140 .func = test__hists_cumulate,
143 .desc = "Test tracking with sched_switch",
144 .func = test__switch_tracking,
147 .desc = "Filter fds with revents mask in a fdarray",
148 .func = test__fdarray__filter,
151 .desc = "Add fd to a fdarray, making it autogrow",
152 .func = test__fdarray__add,
155 .desc = "Test kmod_path__parse function",
156 .func = test__kmod_path__parse,
159 .desc = "Test thread map",
160 .func = test__thread_map,
163 .desc = "Test LLVM searching and compiling",
164 .func = test__llvm,
165 .subtest = {
166 .skip_if_fail = true,
167 .get_nr = test__llvm_subtest_get_nr,
168 .get_desc = test__llvm_subtest_get_desc,
172 .desc = "Test topology in session",
173 .func = test_session_topology,
176 .desc = "Test BPF filter",
177 .func = test__bpf,
178 .subtest = {
179 .skip_if_fail = true,
180 .get_nr = test__bpf_subtest_get_nr,
181 .get_desc = test__bpf_subtest_get_desc,
185 .desc = "Test thread map synthesize",
186 .func = test__thread_map_synthesize,
189 .desc = "Test cpu map synthesize",
190 .func = test__cpu_map_synthesize,
193 .desc = "Test stat config synthesize",
194 .func = test__synthesize_stat_config,
197 .desc = "Test stat synthesize",
198 .func = test__synthesize_stat,
201 .desc = "Test stat round synthesize",
202 .func = test__synthesize_stat_round,
205 .desc = "Test attr update synthesize",
206 .func = test__event_update,
209 .desc = "Test events times",
210 .func = test__event_times,
213 .desc = "Test backward reading from ring buffer",
214 .func = test__backward_ring_buffer,
217 .desc = "Test cpu map print",
218 .func = test__cpu_map_print,
221 .desc = "Test SDT event probing",
222 .func = test__sdt_event,
225 .desc = "Test is_printable_array function",
226 .func = test__is_printable_array,
229 .desc = "Test bitmap print",
230 .func = test__bitmap_print,
233 .func = NULL,
237 static struct test *tests[] = {
238 generic_tests,
239 arch_tests,
242 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
244 int i;
246 if (argc == 0)
247 return true;
249 for (i = 0; i < argc; ++i) {
250 char *end;
251 long nr = strtoul(argv[i], &end, 10);
253 if (*end == '\0') {
254 if (nr == curr + 1)
255 return true;
256 continue;
259 if (strcasestr(test->desc, argv[i]))
260 return true;
263 return false;
266 static int run_test(struct test *test, int subtest)
268 int status, err = -1, child = dont_fork ? 0 : fork();
269 char sbuf[STRERR_BUFSIZE];
271 if (child < 0) {
272 pr_err("failed to fork test: %s\n",
273 str_error_r(errno, sbuf, sizeof(sbuf)));
274 return -1;
277 if (!child) {
278 if (!dont_fork) {
279 pr_debug("test child forked, pid %d\n", getpid());
281 if (!verbose) {
282 int nullfd = open("/dev/null", O_WRONLY);
284 if (nullfd >= 0) {
285 close(STDERR_FILENO);
286 close(STDOUT_FILENO);
288 dup2(nullfd, STDOUT_FILENO);
289 dup2(STDOUT_FILENO, STDERR_FILENO);
290 close(nullfd);
292 } else {
293 signal(SIGSEGV, sighandler_dump_stack);
294 signal(SIGFPE, sighandler_dump_stack);
298 err = test->func(subtest);
299 if (!dont_fork)
300 exit(err);
303 if (!dont_fork) {
304 wait(&status);
306 if (WIFEXITED(status)) {
307 err = (signed char)WEXITSTATUS(status);
308 pr_debug("test child finished with %d\n", err);
309 } else if (WIFSIGNALED(status)) {
310 err = -1;
311 pr_debug("test child interrupted\n");
315 return err;
318 #define for_each_test(j, t) \
319 for (j = 0; j < ARRAY_SIZE(tests); j++) \
320 for (t = &tests[j][0]; t->func; t++)
322 static int test_and_print(struct test *t, bool force_skip, int subtest)
324 int err;
326 if (!force_skip) {
327 pr_debug("\n--- start ---\n");
328 err = run_test(t, subtest);
329 pr_debug("---- end ----\n");
330 } else {
331 pr_debug("\n--- force skipped ---\n");
332 err = TEST_SKIP;
335 if (!t->subtest.get_nr)
336 pr_debug("%s:", t->desc);
337 else
338 pr_debug("%s subtest %d:", t->desc, subtest);
340 switch (err) {
341 case TEST_OK:
342 pr_info(" Ok\n");
343 break;
344 case TEST_SKIP:
345 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
346 break;
347 case TEST_FAIL:
348 default:
349 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
350 break;
353 return err;
356 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
358 struct test *t;
359 unsigned int j;
360 int i = 0;
361 int width = 0;
363 for_each_test(j, t) {
364 int len = strlen(t->desc);
366 if (width < len)
367 width = len;
370 for_each_test(j, t) {
371 int curr = i++, err;
373 if (!perf_test__matches(t, curr, argc, argv))
374 continue;
376 pr_info("%2d: %-*s:", i, width, t->desc);
378 if (intlist__find(skiplist, i)) {
379 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
380 continue;
383 if (!t->subtest.get_nr) {
384 test_and_print(t, false, -1);
385 } else {
386 int subn = t->subtest.get_nr();
388 * minus 2 to align with normal testcases.
389 * For subtest we print additional '.x' in number.
390 * for example:
392 * 35: Test LLVM searching and compiling :
393 * 35.1: Basic BPF llvm compiling test : Ok
395 int subw = width > 2 ? width - 2 : width;
396 bool skip = false;
397 int subi;
399 if (subn <= 0) {
400 color_fprintf(stderr, PERF_COLOR_YELLOW,
401 " Skip (not compiled in)\n");
402 continue;
404 pr_info("\n");
406 for (subi = 0; subi < subn; subi++) {
407 int len = strlen(t->subtest.get_desc(subi));
409 if (subw < len)
410 subw = len;
413 for (subi = 0; subi < subn; subi++) {
414 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
415 t->subtest.get_desc(subi));
416 err = test_and_print(t, skip, subi);
417 if (err != TEST_OK && t->subtest.skip_if_fail)
418 skip = true;
423 return 0;
426 static int perf_test__list(int argc, const char **argv)
428 unsigned int j;
429 struct test *t;
430 int i = 0;
432 for_each_test(j, t) {
433 if (argc > 1 && !strstr(t->desc, argv[1]))
434 continue;
436 pr_info("%2d: %s\n", ++i, t->desc);
439 return 0;
442 int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
444 const char *test_usage[] = {
445 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
446 NULL,
448 const char *skip = NULL;
449 const struct option test_options[] = {
450 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
451 OPT_INCR('v', "verbose", &verbose,
452 "be more verbose (show symbol address, etc)"),
453 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
454 "Do not fork for testcase"),
455 OPT_END()
457 const char * const test_subcommands[] = { "list", NULL };
458 struct intlist *skiplist = NULL;
459 int ret = hists__init();
461 if (ret < 0)
462 return ret;
464 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
465 if (argc >= 1 && !strcmp(argv[0], "list"))
466 return perf_test__list(argc, argv);
468 symbol_conf.priv_size = sizeof(int);
469 symbol_conf.sort_by_name = true;
470 symbol_conf.try_vmlinux_path = true;
472 if (symbol__init(NULL) < 0)
473 return -1;
475 if (skip != NULL)
476 skiplist = intlist__new(skip);
478 return __cmd_test(argc, argv, skiplist);