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