Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / perf / tests / builtin-test.c
blobfafa014240cd8bd9e79bc273cf1a05874abe6ea2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * builtin-test.c
5 * Builtin regression testing command: ever growing number of sanity tests
6 */
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <dirent.h>
13 #include <sys/wait.h>
14 #include <sys/stat.h>
15 #include "builtin.h"
16 #include "hist.h"
17 #include "intlist.h"
18 #include "tests.h"
19 #include "debug.h"
20 #include "color.h"
21 #include <subcmd/parse-options.h>
22 #include "string2.h"
23 #include "symbol.h"
24 #include <linux/kernel.h>
25 #include <subcmd/exec-cmd.h>
27 static bool dont_fork;
29 struct test __weak arch_tests[] = {
31 .func = NULL,
35 static struct test generic_tests[] = {
37 .desc = "vmlinux symtab matches kallsyms",
38 .func = test__vmlinux_matches_kallsyms,
41 .desc = "Detect openat syscall event",
42 .func = test__openat_syscall_event,
45 .desc = "Detect openat syscall event on all cpus",
46 .func = test__openat_syscall_event_on_all_cpus,
49 .desc = "Read samples using the mmap interface",
50 .func = test__basic_mmap,
53 .desc = "Test data source output",
54 .func = test__mem,
57 .desc = "Parse event definition strings",
58 .func = test__parse_events,
61 .desc = "Simple expression parser",
62 .func = test__expr,
65 .desc = "PERF_RECORD_* events & perf_sample fields",
66 .func = test__PERF_RECORD,
69 .desc = "Parse perf pmu format",
70 .func = test__pmu,
73 .desc = "DSO data read",
74 .func = test__dso_data,
77 .desc = "DSO data cache",
78 .func = test__dso_data_cache,
81 .desc = "DSO data reopen",
82 .func = test__dso_data_reopen,
85 .desc = "Roundtrip evsel->name",
86 .func = test__perf_evsel__roundtrip_name_test,
89 .desc = "Parse sched tracepoints fields",
90 .func = test__perf_evsel__tp_sched_test,
93 .desc = "syscalls:sys_enter_openat event fields",
94 .func = test__syscall_openat_tp_fields,
97 .desc = "Setup struct perf_event_attr",
98 .func = test__attr,
101 .desc = "Match and link multiple hists",
102 .func = test__hists_link,
105 .desc = "'import perf' in python",
106 .func = test__python_use,
109 .desc = "Breakpoint overflow signal handler",
110 .func = test__bp_signal,
111 .is_supported = test__bp_signal_is_supported,
114 .desc = "Breakpoint overflow sampling",
115 .func = test__bp_signal_overflow,
116 .is_supported = test__bp_signal_is_supported,
119 .desc = "Number of exit events of a simple workload",
120 .func = test__task_exit,
123 .desc = "Software clock events period values",
124 .func = test__sw_clock_freq,
127 .desc = "Object code reading",
128 .func = test__code_reading,
131 .desc = "Sample parsing",
132 .func = test__sample_parsing,
135 .desc = "Use a dummy software event to keep tracking",
136 .func = test__keep_tracking,
139 .desc = "Parse with no sample_id_all bit set",
140 .func = test__parse_no_sample_id_all,
143 .desc = "Filter hist entries",
144 .func = test__hists_filter,
147 .desc = "Lookup mmap thread",
148 .func = test__mmap_thread_lookup,
151 .desc = "Share thread mg",
152 .func = test__thread_mg_share,
155 .desc = "Sort output of hist entries",
156 .func = test__hists_output,
159 .desc = "Cumulate child hist entries",
160 .func = test__hists_cumulate,
163 .desc = "Track with sched_switch",
164 .func = test__switch_tracking,
167 .desc = "Filter fds with revents mask in a fdarray",
168 .func = test__fdarray__filter,
171 .desc = "Add fd to a fdarray, making it autogrow",
172 .func = test__fdarray__add,
175 .desc = "kmod_path__parse",
176 .func = test__kmod_path__parse,
179 .desc = "Thread map",
180 .func = test__thread_map,
183 .desc = "LLVM search and compile",
184 .func = test__llvm,
185 .subtest = {
186 .skip_if_fail = true,
187 .get_nr = test__llvm_subtest_get_nr,
188 .get_desc = test__llvm_subtest_get_desc,
192 .desc = "Session topology",
193 .func = test__session_topology,
196 .desc = "BPF filter",
197 .func = test__bpf,
198 .subtest = {
199 .skip_if_fail = true,
200 .get_nr = test__bpf_subtest_get_nr,
201 .get_desc = test__bpf_subtest_get_desc,
205 .desc = "Synthesize thread map",
206 .func = test__thread_map_synthesize,
209 .desc = "Remove thread map",
210 .func = test__thread_map_remove,
213 .desc = "Synthesize cpu map",
214 .func = test__cpu_map_synthesize,
217 .desc = "Synthesize stat config",
218 .func = test__synthesize_stat_config,
221 .desc = "Synthesize stat",
222 .func = test__synthesize_stat,
225 .desc = "Synthesize stat round",
226 .func = test__synthesize_stat_round,
229 .desc = "Synthesize attr update",
230 .func = test__event_update,
233 .desc = "Event times",
234 .func = test__event_times,
237 .desc = "Read backward ring buffer",
238 .func = test__backward_ring_buffer,
241 .desc = "Print cpu map",
242 .func = test__cpu_map_print,
245 .desc = "Probe SDT events",
246 .func = test__sdt_event,
249 .desc = "is_printable_array",
250 .func = test__is_printable_array,
253 .desc = "Print bitmap",
254 .func = test__bitmap_print,
257 .desc = "perf hooks",
258 .func = test__perf_hooks,
261 .desc = "builtin clang support",
262 .func = test__clang,
263 .subtest = {
264 .skip_if_fail = true,
265 .get_nr = test__clang_subtest_get_nr,
266 .get_desc = test__clang_subtest_get_desc,
270 .desc = "unit_number__scnprintf",
271 .func = test__unit_number__scnprint,
274 .func = NULL,
278 static struct test *tests[] = {
279 generic_tests,
280 arch_tests,
283 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
285 int i;
287 if (argc == 0)
288 return true;
290 for (i = 0; i < argc; ++i) {
291 char *end;
292 long nr = strtoul(argv[i], &end, 10);
294 if (*end == '\0') {
295 if (nr == curr + 1)
296 return true;
297 continue;
300 if (strcasestr(test->desc, argv[i]))
301 return true;
304 return false;
307 static int run_test(struct test *test, int subtest)
309 int status, err = -1, child = dont_fork ? 0 : fork();
310 char sbuf[STRERR_BUFSIZE];
312 if (child < 0) {
313 pr_err("failed to fork test: %s\n",
314 str_error_r(errno, sbuf, sizeof(sbuf)));
315 return -1;
318 if (!child) {
319 if (!dont_fork) {
320 pr_debug("test child forked, pid %d\n", getpid());
322 if (verbose <= 0) {
323 int nullfd = open("/dev/null", O_WRONLY);
325 if (nullfd >= 0) {
326 close(STDERR_FILENO);
327 close(STDOUT_FILENO);
329 dup2(nullfd, STDOUT_FILENO);
330 dup2(STDOUT_FILENO, STDERR_FILENO);
331 close(nullfd);
333 } else {
334 signal(SIGSEGV, sighandler_dump_stack);
335 signal(SIGFPE, sighandler_dump_stack);
339 err = test->func(test, subtest);
340 if (!dont_fork)
341 exit(err);
344 if (!dont_fork) {
345 wait(&status);
347 if (WIFEXITED(status)) {
348 err = (signed char)WEXITSTATUS(status);
349 pr_debug("test child finished with %d\n", err);
350 } else if (WIFSIGNALED(status)) {
351 err = -1;
352 pr_debug("test child interrupted\n");
356 return err;
359 #define for_each_test(j, t) \
360 for (j = 0; j < ARRAY_SIZE(tests); j++) \
361 for (t = &tests[j][0]; t->func; t++)
363 static int test_and_print(struct test *t, bool force_skip, int subtest)
365 int err;
367 if (!force_skip) {
368 pr_debug("\n--- start ---\n");
369 err = run_test(t, subtest);
370 pr_debug("---- end ----\n");
371 } else {
372 pr_debug("\n--- force skipped ---\n");
373 err = TEST_SKIP;
376 if (!t->subtest.get_nr)
377 pr_debug("%s:", t->desc);
378 else
379 pr_debug("%s subtest %d:", t->desc, subtest);
381 switch (err) {
382 case TEST_OK:
383 pr_info(" Ok\n");
384 break;
385 case TEST_SKIP:
386 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
387 break;
388 case TEST_FAIL:
389 default:
390 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
391 break;
394 return err;
397 static const char *shell_test__description(char *description, size_t size,
398 const char *path, const char *name)
400 FILE *fp;
401 char filename[PATH_MAX];
403 path__join(filename, sizeof(filename), path, name);
404 fp = fopen(filename, "r");
405 if (!fp)
406 return NULL;
408 description = fgets(description, size, fp);
409 fclose(fp);
411 return description ? trim(description + 1) : NULL;
414 #define for_each_shell_test(dir, base, ent) \
415 while ((ent = readdir(dir)) != NULL) \
416 if (!is_directory(base, ent))
418 static const char *shell_tests__dir(char *path, size_t size)
420 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
421 char *exec_path;
422 unsigned int i;
424 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
425 struct stat st;
426 if (!lstat(devel_dirs[i], &st)) {
427 scnprintf(path, size, "%s/shell", devel_dirs[i]);
428 if (!lstat(devel_dirs[i], &st))
429 return path;
433 /* Then installed path. */
434 exec_path = get_argv_exec_path();
435 scnprintf(path, size, "%s/tests/shell", exec_path);
436 free(exec_path);
437 return path;
440 static int shell_tests__max_desc_width(void)
442 DIR *dir;
443 struct dirent *ent;
444 char path_dir[PATH_MAX];
445 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
446 int width = 0;
448 if (path == NULL)
449 return -1;
451 dir = opendir(path);
452 if (!dir)
453 return -1;
455 for_each_shell_test(dir, path, ent) {
456 char bf[256];
457 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
459 if (desc) {
460 int len = strlen(desc);
462 if (width < len)
463 width = len;
467 closedir(dir);
468 return width;
471 struct shell_test {
472 const char *dir;
473 const char *file;
476 static int shell_test__run(struct test *test, int subdir __maybe_unused)
478 int err;
479 char script[PATH_MAX];
480 struct shell_test *st = test->priv;
482 path__join(script, sizeof(script), st->dir, st->file);
484 err = system(script);
485 if (!err)
486 return TEST_OK;
488 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
491 static int run_shell_tests(int argc, const char *argv[], int i, int width)
493 DIR *dir;
494 struct dirent *ent;
495 char path_dir[PATH_MAX];
496 struct shell_test st = {
497 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
500 if (st.dir == NULL)
501 return -1;
503 dir = opendir(st.dir);
504 if (!dir)
505 return -1;
507 for_each_shell_test(dir, st.dir, ent) {
508 int curr = i++;
509 char desc[256];
510 struct test test = {
511 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name),
512 .func = shell_test__run,
513 .priv = &st,
516 if (!perf_test__matches(&test, curr, argc, argv))
517 continue;
519 st.file = ent->d_name;
520 pr_info("%2d: %-*s:", i, width, test.desc);
521 test_and_print(&test, false, -1);
524 closedir(dir);
525 return 0;
528 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
530 struct test *t;
531 unsigned int j;
532 int i = 0;
533 int width = shell_tests__max_desc_width();
535 for_each_test(j, t) {
536 int len = strlen(t->desc);
538 if (width < len)
539 width = len;
542 for_each_test(j, t) {
543 int curr = i++, err;
545 if (!perf_test__matches(t, curr, argc, argv))
546 continue;
548 if (t->is_supported && !t->is_supported()) {
549 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
550 continue;
553 pr_info("%2d: %-*s:", i, width, t->desc);
555 if (intlist__find(skiplist, i)) {
556 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
557 continue;
560 if (!t->subtest.get_nr) {
561 test_and_print(t, false, -1);
562 } else {
563 int subn = t->subtest.get_nr();
565 * minus 2 to align with normal testcases.
566 * For subtest we print additional '.x' in number.
567 * for example:
569 * 35: Test LLVM searching and compiling :
570 * 35.1: Basic BPF llvm compiling test : Ok
572 int subw = width > 2 ? width - 2 : width;
573 bool skip = false;
574 int subi;
576 if (subn <= 0) {
577 color_fprintf(stderr, PERF_COLOR_YELLOW,
578 " Skip (not compiled in)\n");
579 continue;
581 pr_info("\n");
583 for (subi = 0; subi < subn; subi++) {
584 int len = strlen(t->subtest.get_desc(subi));
586 if (subw < len)
587 subw = len;
590 for (subi = 0; subi < subn; subi++) {
591 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
592 t->subtest.get_desc(subi));
593 err = test_and_print(t, skip, subi);
594 if (err != TEST_OK && t->subtest.skip_if_fail)
595 skip = true;
600 return run_shell_tests(argc, argv, i, width);
603 static int perf_test__list_shell(int argc, const char **argv, int i)
605 DIR *dir;
606 struct dirent *ent;
607 char path_dir[PATH_MAX];
608 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
610 if (path == NULL)
611 return -1;
613 dir = opendir(path);
614 if (!dir)
615 return -1;
617 for_each_shell_test(dir, path, ent) {
618 int curr = i++;
619 char bf[256];
620 struct test t = {
621 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
624 if (!perf_test__matches(&t, curr, argc, argv))
625 continue;
627 pr_info("%2d: %s\n", i, t.desc);
630 closedir(dir);
631 return 0;
634 static int perf_test__list(int argc, const char **argv)
636 unsigned int j;
637 struct test *t;
638 int i = 0;
640 for_each_test(j, t) {
641 int curr = i++;
643 if (!perf_test__matches(t, curr, argc, argv) ||
644 (t->is_supported && !t->is_supported()))
645 continue;
647 pr_info("%2d: %s\n", i, t->desc);
650 perf_test__list_shell(argc, argv, i);
652 return 0;
655 int cmd_test(int argc, const char **argv)
657 const char *test_usage[] = {
658 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
659 NULL,
661 const char *skip = NULL;
662 const struct option test_options[] = {
663 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
664 OPT_INCR('v', "verbose", &verbose,
665 "be more verbose (show symbol address, etc)"),
666 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
667 "Do not fork for testcase"),
668 OPT_END()
670 const char * const test_subcommands[] = { "list", NULL };
671 struct intlist *skiplist = NULL;
672 int ret = hists__init();
674 if (ret < 0)
675 return ret;
677 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
678 if (argc >= 1 && !strcmp(argv[0], "list"))
679 return perf_test__list(argc - 1, argv + 1);
681 symbol_conf.priv_size = sizeof(int);
682 symbol_conf.sort_by_name = true;
683 symbol_conf.try_vmlinux_path = true;
685 if (symbol__init(NULL) < 0)
686 return -1;
688 if (skip != NULL)
689 skiplist = intlist__new(skip);
691 return __cmd_test(argc, argv, skiplist);