]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - tools/perf/tests/builtin-test.c
perf test: Add infrastructure to run shell based tests
[thirdparty/kernel/stable.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/types.h>
10 #include <dirent.h>
11 #include <sys/wait.h>
12 #include <sys/stat.h>
13 #include "builtin.h"
14 #include "hist.h"
15 #include "intlist.h"
16 #include "tests.h"
17 #include "debug.h"
18 #include "color.h"
19 #include <subcmd/parse-options.h>
20 #include "string2.h"
21 #include "symbol.h"
22 #include <linux/kernel.h>
23 #include <subcmd/exec-cmd.h>
24
25 static bool dont_fork;
26
27 struct test __weak arch_tests[] = {
28 {
29 .func = NULL,
30 },
31 };
32
33 static struct test generic_tests[] = {
34 {
35 .desc = "vmlinux symtab matches kallsyms",
36 .func = test__vmlinux_matches_kallsyms,
37 },
38 {
39 .desc = "Detect openat syscall event",
40 .func = test__openat_syscall_event,
41 },
42 {
43 .desc = "Detect openat syscall event on all cpus",
44 .func = test__openat_syscall_event_on_all_cpus,
45 },
46 {
47 .desc = "Read samples using the mmap interface",
48 .func = test__basic_mmap,
49 },
50 {
51 .desc = "Parse event definition strings",
52 .func = test__parse_events,
53 },
54 {
55 .desc = "Simple expression parser",
56 .func = test__expr,
57 },
58 {
59 .desc = "PERF_RECORD_* events & perf_sample fields",
60 .func = test__PERF_RECORD,
61 },
62 {
63 .desc = "Parse perf pmu format",
64 .func = test__pmu,
65 },
66 {
67 .desc = "DSO data read",
68 .func = test__dso_data,
69 },
70 {
71 .desc = "DSO data cache",
72 .func = test__dso_data_cache,
73 },
74 {
75 .desc = "DSO data reopen",
76 .func = test__dso_data_reopen,
77 },
78 {
79 .desc = "Roundtrip evsel->name",
80 .func = test__perf_evsel__roundtrip_name_test,
81 },
82 {
83 .desc = "Parse sched tracepoints fields",
84 .func = test__perf_evsel__tp_sched_test,
85 },
86 {
87 .desc = "syscalls:sys_enter_openat event fields",
88 .func = test__syscall_openat_tp_fields,
89 },
90 {
91 .desc = "Setup struct perf_event_attr",
92 .func = test__attr,
93 },
94 {
95 .desc = "Match and link multiple hists",
96 .func = test__hists_link,
97 },
98 {
99 .desc = "'import perf' in python",
100 .func = test__python_use,
101 },
102 {
103 .desc = "Breakpoint overflow signal handler",
104 .func = test__bp_signal,
105 .is_supported = test__bp_signal_is_supported,
106 },
107 {
108 .desc = "Breakpoint overflow sampling",
109 .func = test__bp_signal_overflow,
110 .is_supported = test__bp_signal_is_supported,
111 },
112 {
113 .desc = "Number of exit events of a simple workload",
114 .func = test__task_exit,
115 },
116 {
117 .desc = "Software clock events period values",
118 .func = test__sw_clock_freq,
119 },
120 {
121 .desc = "Object code reading",
122 .func = test__code_reading,
123 },
124 {
125 .desc = "Sample parsing",
126 .func = test__sample_parsing,
127 },
128 {
129 .desc = "Use a dummy software event to keep tracking",
130 .func = test__keep_tracking,
131 },
132 {
133 .desc = "Parse with no sample_id_all bit set",
134 .func = test__parse_no_sample_id_all,
135 },
136 {
137 .desc = "Filter hist entries",
138 .func = test__hists_filter,
139 },
140 {
141 .desc = "Lookup mmap thread",
142 .func = test__mmap_thread_lookup,
143 },
144 {
145 .desc = "Share thread mg",
146 .func = test__thread_mg_share,
147 },
148 {
149 .desc = "Sort output of hist entries",
150 .func = test__hists_output,
151 },
152 {
153 .desc = "Cumulate child hist entries",
154 .func = test__hists_cumulate,
155 },
156 {
157 .desc = "Track with sched_switch",
158 .func = test__switch_tracking,
159 },
160 {
161 .desc = "Filter fds with revents mask in a fdarray",
162 .func = test__fdarray__filter,
163 },
164 {
165 .desc = "Add fd to a fdarray, making it autogrow",
166 .func = test__fdarray__add,
167 },
168 {
169 .desc = "kmod_path__parse",
170 .func = test__kmod_path__parse,
171 },
172 {
173 .desc = "Thread map",
174 .func = test__thread_map,
175 },
176 {
177 .desc = "LLVM search and compile",
178 .func = test__llvm,
179 .subtest = {
180 .skip_if_fail = true,
181 .get_nr = test__llvm_subtest_get_nr,
182 .get_desc = test__llvm_subtest_get_desc,
183 },
184 },
185 {
186 .desc = "Session topology",
187 .func = test__session_topology,
188 },
189 {
190 .desc = "BPF filter",
191 .func = test__bpf,
192 .subtest = {
193 .skip_if_fail = true,
194 .get_nr = test__bpf_subtest_get_nr,
195 .get_desc = test__bpf_subtest_get_desc,
196 },
197 },
198 {
199 .desc = "Synthesize thread map",
200 .func = test__thread_map_synthesize,
201 },
202 {
203 .desc = "Remove thread map",
204 .func = test__thread_map_remove,
205 },
206 {
207 .desc = "Synthesize cpu map",
208 .func = test__cpu_map_synthesize,
209 },
210 {
211 .desc = "Synthesize stat config",
212 .func = test__synthesize_stat_config,
213 },
214 {
215 .desc = "Synthesize stat",
216 .func = test__synthesize_stat,
217 },
218 {
219 .desc = "Synthesize stat round",
220 .func = test__synthesize_stat_round,
221 },
222 {
223 .desc = "Synthesize attr update",
224 .func = test__event_update,
225 },
226 {
227 .desc = "Event times",
228 .func = test__event_times,
229 },
230 {
231 .desc = "Read backward ring buffer",
232 .func = test__backward_ring_buffer,
233 },
234 {
235 .desc = "Print cpu map",
236 .func = test__cpu_map_print,
237 },
238 {
239 .desc = "Probe SDT events",
240 .func = test__sdt_event,
241 },
242 {
243 .desc = "is_printable_array",
244 .func = test__is_printable_array,
245 },
246 {
247 .desc = "Print bitmap",
248 .func = test__bitmap_print,
249 },
250 {
251 .desc = "perf hooks",
252 .func = test__perf_hooks,
253 },
254 {
255 .desc = "builtin clang support",
256 .func = test__clang,
257 .subtest = {
258 .skip_if_fail = true,
259 .get_nr = test__clang_subtest_get_nr,
260 .get_desc = test__clang_subtest_get_desc,
261 }
262 },
263 {
264 .desc = "unit_number__scnprintf",
265 .func = test__unit_number__scnprint,
266 },
267 {
268 .func = NULL,
269 },
270 };
271
272 static struct test *tests[] = {
273 generic_tests,
274 arch_tests,
275 };
276
277 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
278 {
279 int i;
280
281 if (argc == 0)
282 return true;
283
284 for (i = 0; i < argc; ++i) {
285 char *end;
286 long nr = strtoul(argv[i], &end, 10);
287
288 if (*end == '\0') {
289 if (nr == curr + 1)
290 return true;
291 continue;
292 }
293
294 if (strcasestr(test->desc, argv[i]))
295 return true;
296 }
297
298 return false;
299 }
300
301 static int run_test(struct test *test, int subtest)
302 {
303 int status, err = -1, child = dont_fork ? 0 : fork();
304 char sbuf[STRERR_BUFSIZE];
305
306 if (child < 0) {
307 pr_err("failed to fork test: %s\n",
308 str_error_r(errno, sbuf, sizeof(sbuf)));
309 return -1;
310 }
311
312 if (!child) {
313 if (!dont_fork) {
314 pr_debug("test child forked, pid %d\n", getpid());
315
316 if (verbose <= 0) {
317 int nullfd = open("/dev/null", O_WRONLY);
318
319 if (nullfd >= 0) {
320 close(STDERR_FILENO);
321 close(STDOUT_FILENO);
322
323 dup2(nullfd, STDOUT_FILENO);
324 dup2(STDOUT_FILENO, STDERR_FILENO);
325 close(nullfd);
326 }
327 } else {
328 signal(SIGSEGV, sighandler_dump_stack);
329 signal(SIGFPE, sighandler_dump_stack);
330 }
331 }
332
333 err = test->func(test, subtest);
334 if (!dont_fork)
335 exit(err);
336 }
337
338 if (!dont_fork) {
339 wait(&status);
340
341 if (WIFEXITED(status)) {
342 err = (signed char)WEXITSTATUS(status);
343 pr_debug("test child finished with %d\n", err);
344 } else if (WIFSIGNALED(status)) {
345 err = -1;
346 pr_debug("test child interrupted\n");
347 }
348 }
349
350 return err;
351 }
352
353 #define for_each_test(j, t) \
354 for (j = 0; j < ARRAY_SIZE(tests); j++) \
355 for (t = &tests[j][0]; t->func; t++)
356
357 static int test_and_print(struct test *t, bool force_skip, int subtest)
358 {
359 int err;
360
361 if (!force_skip) {
362 pr_debug("\n--- start ---\n");
363 err = run_test(t, subtest);
364 pr_debug("---- end ----\n");
365 } else {
366 pr_debug("\n--- force skipped ---\n");
367 err = TEST_SKIP;
368 }
369
370 if (!t->subtest.get_nr)
371 pr_debug("%s:", t->desc);
372 else
373 pr_debug("%s subtest %d:", t->desc, subtest);
374
375 switch (err) {
376 case TEST_OK:
377 pr_info(" Ok\n");
378 break;
379 case TEST_SKIP:
380 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
381 break;
382 case TEST_FAIL:
383 default:
384 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
385 break;
386 }
387
388 return err;
389 }
390
391 static const char *shell_test__description(char *description, size_t size,
392 const char *path, const char *name)
393 {
394 FILE *fp;
395 char filename[PATH_MAX];
396
397 path__join(filename, sizeof(filename), path, name);
398 fp = fopen(filename, "r");
399 if (!fp)
400 return NULL;
401
402 description = fgets(description, size, fp);
403 fclose(fp);
404
405 return description ? trim(description + 1) : NULL;
406 }
407
408 #define for_each_shell_test(dir, ent) \
409 while ((ent = readdir(dir)) != NULL) \
410 if (ent->d_type == DT_REG && ent->d_name[0] != '.')
411
412 static const char *shell_tests__dir(char *path, size_t size)
413 {
414 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
415 char *exec_path;
416 unsigned int i;
417
418 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
419 struct stat st;
420 if (!lstat(devel_dirs[i], &st)) {
421 scnprintf(path, size, "%s/shell", devel_dirs[i]);
422 if (!lstat(devel_dirs[i], &st))
423 return path;
424 }
425 }
426
427 /* Then installed path. */
428 exec_path = get_argv_exec_path();
429 scnprintf(path, size, "%s/tests/shell", exec_path);
430 free(exec_path);
431 return path;
432 }
433
434 static int shell_tests__max_desc_width(void)
435 {
436 DIR *dir;
437 struct dirent *ent;
438 char path_dir[PATH_MAX];
439 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
440 int width = 0;
441
442 if (path == NULL)
443 return -1;
444
445 dir = opendir(path);
446 if (!dir)
447 return -1;
448
449 for_each_shell_test(dir, ent) {
450 char bf[256];
451 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
452
453 if (desc) {
454 int len = strlen(desc);
455
456 if (width < len)
457 width = len;
458 }
459 }
460
461 closedir(dir);
462 return width;
463 }
464
465 struct shell_test {
466 const char *dir;
467 const char *file;
468 };
469
470 static int shell_test__run(struct test *test, int subdir __maybe_unused)
471 {
472 int err;
473 char script[PATH_MAX];
474 struct shell_test *st = test->priv;
475
476 path__join(script, sizeof(script), st->dir, st->file);
477
478 err = system(script);
479 if (!err)
480 return TEST_OK;
481
482 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
483 }
484
485 static int run_shell_tests(int argc, const char *argv[], int i, int width)
486 {
487 DIR *dir;
488 struct dirent *ent;
489 char path_dir[PATH_MAX];
490 struct shell_test st = {
491 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
492 };
493
494 if (st.dir == NULL)
495 return -1;
496
497 dir = opendir(st.dir);
498 if (!dir)
499 return -1;
500
501 for_each_shell_test(dir, ent) {
502 int curr = i++;
503 char desc[256];
504 struct test test = {
505 .desc = shell_test__description(desc, sizeof(desc), st.dir, ent->d_name),
506 .func = shell_test__run,
507 .priv = &st,
508 };
509
510 if (!perf_test__matches(&test, curr, argc, argv))
511 continue;
512
513 st.file = ent->d_name;
514 pr_info("%2d: %-*s:", i, width, test.desc);
515 test_and_print(&test, false, -1);
516 }
517
518 closedir(dir);
519 return 0;
520 }
521
522 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
523 {
524 struct test *t;
525 unsigned int j;
526 int i = 0;
527 int width = shell_tests__max_desc_width();
528
529 for_each_test(j, t) {
530 int len = strlen(t->desc);
531
532 if (width < len)
533 width = len;
534 }
535
536 for_each_test(j, t) {
537 int curr = i++, err;
538
539 if (!perf_test__matches(t, curr, argc, argv))
540 continue;
541
542 if (t->is_supported && !t->is_supported()) {
543 pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
544 continue;
545 }
546
547 pr_info("%2d: %-*s:", i, width, t->desc);
548
549 if (intlist__find(skiplist, i)) {
550 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
551 continue;
552 }
553
554 if (!t->subtest.get_nr) {
555 test_and_print(t, false, -1);
556 } else {
557 int subn = t->subtest.get_nr();
558 /*
559 * minus 2 to align with normal testcases.
560 * For subtest we print additional '.x' in number.
561 * for example:
562 *
563 * 35: Test LLVM searching and compiling :
564 * 35.1: Basic BPF llvm compiling test : Ok
565 */
566 int subw = width > 2 ? width - 2 : width;
567 bool skip = false;
568 int subi;
569
570 if (subn <= 0) {
571 color_fprintf(stderr, PERF_COLOR_YELLOW,
572 " Skip (not compiled in)\n");
573 continue;
574 }
575 pr_info("\n");
576
577 for (subi = 0; subi < subn; subi++) {
578 int len = strlen(t->subtest.get_desc(subi));
579
580 if (subw < len)
581 subw = len;
582 }
583
584 for (subi = 0; subi < subn; subi++) {
585 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
586 t->subtest.get_desc(subi));
587 err = test_and_print(t, skip, subi);
588 if (err != TEST_OK && t->subtest.skip_if_fail)
589 skip = true;
590 }
591 }
592 }
593
594 return run_shell_tests(argc, argv, i, width);
595 }
596
597 static int perf_test__list_shell(int argc, const char **argv, int i)
598 {
599 DIR *dir;
600 struct dirent *ent;
601 char path_dir[PATH_MAX];
602 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
603
604 if (path == NULL)
605 return -1;
606
607 dir = opendir(path);
608 if (!dir)
609 return -1;
610
611 for_each_shell_test(dir, ent) {
612 char bf[256];
613 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
614
615 ++i;
616
617 if (argc > 1 && !strstr(desc, argv[1]))
618 continue;
619
620 pr_info("%2d: %s\n", i, desc);
621 }
622
623 closedir(dir);
624 return 0;
625 }
626
627 static int perf_test__list(int argc, const char **argv)
628 {
629 unsigned int j;
630 struct test *t;
631 int i = 0;
632
633 for_each_test(j, t) {
634 ++i;
635
636 if (argc > 1 && !strstr(t->desc, argv[1]))
637 continue;
638
639 pr_info("%2d: %s\n", i, t->desc);
640 }
641
642 perf_test__list_shell(argc, argv, i);
643
644 return 0;
645 }
646
647 int cmd_test(int argc, const char **argv)
648 {
649 const char *test_usage[] = {
650 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
651 NULL,
652 };
653 const char *skip = NULL;
654 const struct option test_options[] = {
655 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
656 OPT_INCR('v', "verbose", &verbose,
657 "be more verbose (show symbol address, etc)"),
658 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
659 "Do not fork for testcase"),
660 OPT_END()
661 };
662 const char * const test_subcommands[] = { "list", NULL };
663 struct intlist *skiplist = NULL;
664 int ret = hists__init();
665
666 if (ret < 0)
667 return ret;
668
669 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
670 if (argc >= 1 && !strcmp(argv[0], "list"))
671 return perf_test__list(argc, argv);
672
673 symbol_conf.priv_size = sizeof(int);
674 symbol_conf.sort_by_name = true;
675 symbol_conf.try_vmlinux_path = true;
676
677 if (symbol__init(NULL) < 0)
678 return -1;
679
680 if (skip != NULL)
681 skiplist = intlist__new(skip);
682
683 return __cmd_test(argc, argv, skiplist);
684 }