staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / perf / tests / builtin-test.c
blobc3bec9d2c20101abab7e17d3324141524801e110
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 "util/rlimit.h"
25 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <subcmd/exec-cmd.h>
29 static bool dont_fork;
31 struct test __weak arch_tests[] = {
33 .func = NULL,
37 static struct test generic_tests[] = {
39 .desc = "vmlinux symtab matches kallsyms",
40 .func = test__vmlinux_matches_kallsyms,
43 .desc = "Detect openat syscall event",
44 .func = test__openat_syscall_event,
47 .desc = "Detect openat syscall event on all cpus",
48 .func = test__openat_syscall_event_on_all_cpus,
51 .desc = "Read samples using the mmap interface",
52 .func = test__basic_mmap,
55 .desc = "Test data source output",
56 .func = test__mem,
59 .desc = "Parse event definition strings",
60 .func = test__parse_events,
63 .desc = "Simple expression parser",
64 .func = test__expr,
67 .desc = "PERF_RECORD_* events & perf_sample fields",
68 .func = test__PERF_RECORD,
71 .desc = "Parse perf pmu format",
72 .func = test__pmu,
75 .desc = "DSO data read",
76 .func = test__dso_data,
79 .desc = "DSO data cache",
80 .func = test__dso_data_cache,
83 .desc = "DSO data reopen",
84 .func = test__dso_data_reopen,
87 .desc = "Roundtrip evsel->name",
88 .func = test__perf_evsel__roundtrip_name_test,
91 .desc = "Parse sched tracepoints fields",
92 .func = test__perf_evsel__tp_sched_test,
95 .desc = "syscalls:sys_enter_openat event fields",
96 .func = test__syscall_openat_tp_fields,
99 .desc = "Setup struct perf_event_attr",
100 .func = test__attr,
103 .desc = "Match and link multiple hists",
104 .func = test__hists_link,
107 .desc = "'import perf' in python",
108 .func = test__python_use,
111 .desc = "Breakpoint overflow signal handler",
112 .func = test__bp_signal,
113 .is_supported = test__bp_signal_is_supported,
116 .desc = "Breakpoint overflow sampling",
117 .func = test__bp_signal_overflow,
118 .is_supported = test__bp_signal_is_supported,
121 .desc = "Breakpoint accounting",
122 .func = test__bp_accounting,
123 .is_supported = test__bp_signal_is_supported,
126 .desc = "Watchpoint",
127 .func = test__wp,
128 .is_supported = test__wp_is_supported,
129 .subtest = {
130 .skip_if_fail = false,
131 .get_nr = test__wp_subtest_get_nr,
132 .get_desc = test__wp_subtest_get_desc,
136 .desc = "Number of exit events of a simple workload",
137 .func = test__task_exit,
140 .desc = "Software clock events period values",
141 .func = test__sw_clock_freq,
144 .desc = "Object code reading",
145 .func = test__code_reading,
148 .desc = "Sample parsing",
149 .func = test__sample_parsing,
152 .desc = "Use a dummy software event to keep tracking",
153 .func = test__keep_tracking,
156 .desc = "Parse with no sample_id_all bit set",
157 .func = test__parse_no_sample_id_all,
160 .desc = "Filter hist entries",
161 .func = test__hists_filter,
164 .desc = "Lookup mmap thread",
165 .func = test__mmap_thread_lookup,
168 .desc = "Share thread mg",
169 .func = test__thread_mg_share,
172 .desc = "Sort output of hist entries",
173 .func = test__hists_output,
176 .desc = "Cumulate child hist entries",
177 .func = test__hists_cumulate,
180 .desc = "Track with sched_switch",
181 .func = test__switch_tracking,
184 .desc = "Filter fds with revents mask in a fdarray",
185 .func = test__fdarray__filter,
188 .desc = "Add fd to a fdarray, making it autogrow",
189 .func = test__fdarray__add,
192 .desc = "kmod_path__parse",
193 .func = test__kmod_path__parse,
196 .desc = "Thread map",
197 .func = test__thread_map,
200 .desc = "LLVM search and compile",
201 .func = test__llvm,
202 .subtest = {
203 .skip_if_fail = true,
204 .get_nr = test__llvm_subtest_get_nr,
205 .get_desc = test__llvm_subtest_get_desc,
209 .desc = "Session topology",
210 .func = test__session_topology,
213 .desc = "BPF filter",
214 .func = test__bpf,
215 .subtest = {
216 .skip_if_fail = true,
217 .get_nr = test__bpf_subtest_get_nr,
218 .get_desc = test__bpf_subtest_get_desc,
222 .desc = "Synthesize thread map",
223 .func = test__thread_map_synthesize,
226 .desc = "Remove thread map",
227 .func = test__thread_map_remove,
230 .desc = "Synthesize cpu map",
231 .func = test__cpu_map_synthesize,
234 .desc = "Synthesize stat config",
235 .func = test__synthesize_stat_config,
238 .desc = "Synthesize stat",
239 .func = test__synthesize_stat,
242 .desc = "Synthesize stat round",
243 .func = test__synthesize_stat_round,
246 .desc = "Synthesize attr update",
247 .func = test__event_update,
250 .desc = "Event times",
251 .func = test__event_times,
254 .desc = "Read backward ring buffer",
255 .func = test__backward_ring_buffer,
258 .desc = "Print cpu map",
259 .func = test__cpu_map_print,
262 .desc = "Probe SDT events",
263 .func = test__sdt_event,
266 .desc = "is_printable_array",
267 .func = test__is_printable_array,
270 .desc = "Print bitmap",
271 .func = test__bitmap_print,
274 .desc = "perf hooks",
275 .func = test__perf_hooks,
278 .desc = "builtin clang support",
279 .func = test__clang,
280 .subtest = {
281 .skip_if_fail = true,
282 .get_nr = test__clang_subtest_get_nr,
283 .get_desc = test__clang_subtest_get_desc,
287 .desc = "unit_number__scnprintf",
288 .func = test__unit_number__scnprint,
291 .desc = "mem2node",
292 .func = test__mem2node,
295 .desc = "time utils",
296 .func = test__time_utils,
299 .desc = "map_groups__merge_in",
300 .func = test__map_groups__merge_in,
303 .func = NULL,
307 static struct test *tests[] = {
308 generic_tests,
309 arch_tests,
312 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
314 int i;
316 if (argc == 0)
317 return true;
319 for (i = 0; i < argc; ++i) {
320 char *end;
321 long nr = strtoul(argv[i], &end, 10);
323 if (*end == '\0') {
324 if (nr == curr + 1)
325 return true;
326 continue;
329 if (strcasestr(test->desc, argv[i]))
330 return true;
333 return false;
336 static int run_test(struct test *test, int subtest)
338 int status, err = -1, child = dont_fork ? 0 : fork();
339 char sbuf[STRERR_BUFSIZE];
341 if (child < 0) {
342 pr_err("failed to fork test: %s\n",
343 str_error_r(errno, sbuf, sizeof(sbuf)));
344 return -1;
347 if (!child) {
348 if (!dont_fork) {
349 pr_debug("test child forked, pid %d\n", getpid());
351 if (verbose <= 0) {
352 int nullfd = open("/dev/null", O_WRONLY);
354 if (nullfd >= 0) {
355 close(STDERR_FILENO);
356 close(STDOUT_FILENO);
358 dup2(nullfd, STDOUT_FILENO);
359 dup2(STDOUT_FILENO, STDERR_FILENO);
360 close(nullfd);
362 } else {
363 signal(SIGSEGV, sighandler_dump_stack);
364 signal(SIGFPE, sighandler_dump_stack);
368 err = test->func(test, subtest);
369 if (!dont_fork)
370 exit(err);
373 if (!dont_fork) {
374 wait(&status);
376 if (WIFEXITED(status)) {
377 err = (signed char)WEXITSTATUS(status);
378 pr_debug("test child finished with %d\n", err);
379 } else if (WIFSIGNALED(status)) {
380 err = -1;
381 pr_debug("test child interrupted\n");
385 return err;
388 #define for_each_test(j, t) \
389 for (j = 0; j < ARRAY_SIZE(tests); j++) \
390 for (t = &tests[j][0]; t->func; t++)
392 static int test_and_print(struct test *t, bool force_skip, int subtest)
394 int err;
396 if (!force_skip) {
397 pr_debug("\n--- start ---\n");
398 err = run_test(t, subtest);
399 pr_debug("---- end ----\n");
400 } else {
401 pr_debug("\n--- force skipped ---\n");
402 err = TEST_SKIP;
405 if (!t->subtest.get_nr)
406 pr_debug("%s:", t->desc);
407 else
408 pr_debug("%s subtest %d:", t->desc, subtest + 1);
410 switch (err) {
411 case TEST_OK:
412 pr_info(" Ok\n");
413 break;
414 case TEST_SKIP:
415 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
416 break;
417 case TEST_FAIL:
418 default:
419 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
420 break;
423 return err;
426 static const char *shell_test__description(char *description, size_t size,
427 const char *path, const char *name)
429 FILE *fp;
430 char filename[PATH_MAX];
432 path__join(filename, sizeof(filename), path, name);
433 fp = fopen(filename, "r");
434 if (!fp)
435 return NULL;
437 /* Skip shebang */
438 while (fgetc(fp) != '\n');
440 description = fgets(description, size, fp);
441 fclose(fp);
443 return description ? strim(description + 1) : NULL;
446 #define for_each_shell_test(dir, base, ent) \
447 while ((ent = readdir(dir)) != NULL) \
448 if (!is_directory(base, ent) && ent->d_name[0] != '.')
450 static const char *shell_tests__dir(char *path, size_t size)
452 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
453 char *exec_path;
454 unsigned int i;
456 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
457 struct stat st;
458 if (!lstat(devel_dirs[i], &st)) {
459 scnprintf(path, size, "%s/shell", devel_dirs[i]);
460 if (!lstat(devel_dirs[i], &st))
461 return path;
465 /* Then installed path. */
466 exec_path = get_argv_exec_path();
467 scnprintf(path, size, "%s/tests/shell", exec_path);
468 free(exec_path);
469 return path;
472 static int shell_tests__max_desc_width(void)
474 DIR *dir;
475 struct dirent *ent;
476 char path_dir[PATH_MAX];
477 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
478 int width = 0;
480 if (path == NULL)
481 return -1;
483 dir = opendir(path);
484 if (!dir)
485 return -1;
487 for_each_shell_test(dir, path, ent) {
488 char bf[256];
489 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
491 if (desc) {
492 int len = strlen(desc);
494 if (width < len)
495 width = len;
499 closedir(dir);
500 return width;
503 struct shell_test {
504 const char *dir;
505 const char *file;
508 static int shell_test__run(struct test *test, int subdir __maybe_unused)
510 int err;
511 char script[PATH_MAX];
512 struct shell_test *st = test->priv;
514 path__join(script, sizeof(script), st->dir, st->file);
516 err = system(script);
517 if (!err)
518 return TEST_OK;
520 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
523 static int run_shell_tests(int argc, const char *argv[], int i, int width)
525 DIR *dir;
526 struct dirent *ent;
527 char path_dir[PATH_MAX];
528 struct shell_test st = {
529 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
532 if (st.dir == NULL)
533 return -1;
535 dir = opendir(st.dir);
536 if (!dir)
537 return -1;
539 for_each_shell_test(dir, st.dir, ent) {
540 int curr = i++;
541 char desc[256];
542 struct test test = {
543 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name),
544 .func = shell_test__run,
545 .priv = &st,
548 if (!perf_test__matches(&test, curr, argc, argv))
549 continue;
551 st.file = ent->d_name;
552 pr_info("%2d: %-*s:", i, width, test.desc);
553 test_and_print(&test, false, -1);
556 closedir(dir);
557 return 0;
560 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
562 struct test *t;
563 unsigned int j;
564 int i = 0;
565 int width = shell_tests__max_desc_width();
567 for_each_test(j, t) {
568 int len = strlen(t->desc);
570 if (width < len)
571 width = len;
574 for_each_test(j, t) {
575 int curr = i++, err;
577 if (!perf_test__matches(t, curr, argc, argv))
578 continue;
580 if (t->is_supported && !t->is_supported()) {
581 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
582 continue;
585 pr_info("%2d: %-*s:", i, width, t->desc);
587 if (intlist__find(skiplist, i)) {
588 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
589 continue;
592 if (!t->subtest.get_nr) {
593 test_and_print(t, false, -1);
594 } else {
595 int subn = t->subtest.get_nr();
597 * minus 2 to align with normal testcases.
598 * For subtest we print additional '.x' in number.
599 * for example:
601 * 35: Test LLVM searching and compiling :
602 * 35.1: Basic BPF llvm compiling test : Ok
604 int subw = width > 2 ? width - 2 : width;
605 bool skip = false;
606 int subi;
608 if (subn <= 0) {
609 color_fprintf(stderr, PERF_COLOR_YELLOW,
610 " Skip (not compiled in)\n");
611 continue;
613 pr_info("\n");
615 for (subi = 0; subi < subn; subi++) {
616 int len = strlen(t->subtest.get_desc(subi));
618 if (subw < len)
619 subw = len;
622 for (subi = 0; subi < subn; subi++) {
623 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
624 t->subtest.get_desc(subi));
625 err = test_and_print(t, skip, subi);
626 if (err != TEST_OK && t->subtest.skip_if_fail)
627 skip = true;
632 return run_shell_tests(argc, argv, i, width);
635 static int perf_test__list_shell(int argc, const char **argv, int i)
637 DIR *dir;
638 struct dirent *ent;
639 char path_dir[PATH_MAX];
640 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
642 if (path == NULL)
643 return -1;
645 dir = opendir(path);
646 if (!dir)
647 return -1;
649 for_each_shell_test(dir, path, ent) {
650 int curr = i++;
651 char bf[256];
652 struct test t = {
653 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
656 if (!perf_test__matches(&t, curr, argc, argv))
657 continue;
659 pr_info("%2d: %s\n", i, t.desc);
662 closedir(dir);
663 return 0;
666 static int perf_test__list(int argc, const char **argv)
668 unsigned int j;
669 struct test *t;
670 int i = 0;
672 for_each_test(j, t) {
673 int curr = i++;
675 if (!perf_test__matches(t, curr, argc, argv) ||
676 (t->is_supported && !t->is_supported()))
677 continue;
679 pr_info("%2d: %s\n", i, t->desc);
681 if (t->subtest.get_nr) {
682 int subn = t->subtest.get_nr();
683 int subi;
685 for (subi = 0; subi < subn; subi++)
686 pr_info("%2d:%1d: %s\n", i, subi + 1,
687 t->subtest.get_desc(subi));
691 perf_test__list_shell(argc, argv, i);
693 return 0;
696 int cmd_test(int argc, const char **argv)
698 const char *test_usage[] = {
699 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
700 NULL,
702 const char *skip = NULL;
703 const struct option test_options[] = {
704 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
705 OPT_INCR('v', "verbose", &verbose,
706 "be more verbose (show symbol address, etc)"),
707 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
708 "Do not fork for testcase"),
709 OPT_END()
711 const char * const test_subcommands[] = { "list", NULL };
712 struct intlist *skiplist = NULL;
713 int ret = hists__init();
715 if (ret < 0)
716 return ret;
718 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
719 if (argc >= 1 && !strcmp(argv[0], "list"))
720 return perf_test__list(argc - 1, argv + 1);
722 symbol_conf.priv_size = sizeof(int);
723 symbol_conf.sort_by_name = true;
724 symbol_conf.try_vmlinux_path = true;
726 if (symbol__init(NULL) < 0)
727 return -1;
729 if (skip != NULL)
730 skiplist = intlist__new(skip);
732 * Tests that create BPF maps, for instance, need more than the 64K
733 * default:
735 rlimit__bump_memlock();
737 return __cmd_test(argc, argv, skiplist);