xref: /linux/tools/perf/tests/builtin-test.c (revision 390d5ea26622f794c2d29cefd5a01ef116b4fe1d)
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 <ctype.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #ifdef HAVE_BACKTRACE_SUPPORT
11 #include <execinfo.h>
12 #endif
13 #include <poll.h>
14 #include <unistd.h>
15 #include <setjmp.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <sys/types.h>
19 #include <dirent.h>
20 #include <sys/wait.h>
21 #include <sys/stat.h>
22 #include "builtin.h"
23 #include "config.h"
24 #include "hist.h"
25 #include "intlist.h"
26 #include "tests.h"
27 #include "debug.h"
28 #include "color.h"
29 #include <subcmd/parse-options.h>
30 #include <subcmd/run-command.h>
31 #include "string2.h"
32 #include "symbol.h"
33 #include "util/rlimit.h"
34 #include "util/strbuf.h"
35 #include <linux/kernel.h>
36 #include <linux/string.h>
37 #include <subcmd/exec-cmd.h>
38 #include <linux/zalloc.h>
39 
40 #include "tests-scripts.h"
41 
42 /*
43  * Command line option to not fork the test running in the same process and
44  * making them easier to debug.
45  */
46 static bool dont_fork;
47 /* Fork the tests in parallel and wait for their completion. */
48 static bool sequential;
49 /* Number of times each test is run. */
50 static unsigned int runs_per_test = 1;
51 const char *dso_to_test;
52 const char *test_objdump_path = "objdump";
53 
54 /*
55  * List of architecture specific tests. Not a weak symbol as the array length is
56  * dependent on the initialization, as such GCC with LTO complains of
57  * conflicting definitions with a weak symbol.
58  */
59 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
60 extern struct test_suite *arch_tests[];
61 #else
62 static struct test_suite *arch_tests[] = {
63 	NULL,
64 };
65 #endif
66 
67 static struct test_suite *generic_tests[] = {
68 	&suite__vmlinux_matches_kallsyms,
69 	&suite__openat_syscall_event,
70 	&suite__openat_syscall_event_on_all_cpus,
71 	&suite__basic_mmap,
72 	&suite__mem,
73 	&suite__parse_events,
74 	&suite__uncore_event_sorting,
75 	&suite__expr,
76 	&suite__PERF_RECORD,
77 	&suite__pmu,
78 	&suite__pmu_events,
79 	&suite__hwmon_pmu,
80 	&suite__tool_pmu,
81 	&suite__dso_data,
82 	&suite__perf_evsel__roundtrip_name_test,
83 #ifdef HAVE_LIBTRACEEVENT
84 	&suite__perf_evsel__tp_sched_test,
85 	&suite__syscall_openat_tp_fields,
86 #endif
87 	&suite__hists_link,
88 	&suite__bp_signal,
89 	&suite__bp_signal_overflow,
90 	&suite__bp_accounting,
91 	&suite__wp,
92 	&suite__task_exit,
93 	&suite__sw_clock_freq,
94 	&suite__code_reading,
95 	&suite__sample_parsing,
96 	&suite__keep_tracking,
97 	&suite__parse_no_sample_id_all,
98 	&suite__hists_filter,
99 	&suite__mmap_thread_lookup,
100 	&suite__thread_maps_share,
101 	&suite__hists_output,
102 	&suite__hists_cumulate,
103 #ifdef HAVE_LIBTRACEEVENT
104 	&suite__switch_tracking,
105 #endif
106 	&suite__fdarray__filter,
107 	&suite__fdarray__add,
108 	&suite__kmod_path__parse,
109 	&suite__thread_map,
110 	&suite__session_topology,
111 	&suite__thread_map_synthesize,
112 	&suite__thread_map_remove,
113 	&suite__cpu_map,
114 	&suite__synthesize_stat_config,
115 	&suite__synthesize_stat,
116 	&suite__synthesize_stat_round,
117 	&suite__event_update,
118 	&suite__event_times,
119 	&suite__backward_ring_buffer,
120 	&suite__sdt_event,
121 	&suite__is_printable_array,
122 	&suite__bitmap_print,
123 	&suite__perf_hooks,
124 	&suite__unit_number__scnprint,
125 	&suite__mem2node,
126 	&suite__time_utils,
127 	&suite__jit_write_elf,
128 	&suite__pfm,
129 	&suite__api_io,
130 	&suite__maps,
131 	&suite__demangle_java,
132 	&suite__demangle_ocaml,
133 	&suite__demangle_rust,
134 	&suite__parse_metric,
135 	&suite__pe_file_parsing,
136 	&suite__expand_cgroup_events,
137 	&suite__perf_time_to_tsc,
138 	&suite__dlfilter,
139 	&suite__sigtrap,
140 	&suite__event_groups,
141 	&suite__symbols,
142 	&suite__util,
143 	&suite__subcmd_help,
144 	&suite__kallsyms_split,
145 	NULL,
146 };
147 
148 static struct test_workload *workloads[] = {
149 	&workload__noploop,
150 	&workload__thloop,
151 	&workload__leafloop,
152 	&workload__sqrtloop,
153 	&workload__brstack,
154 	&workload__datasym,
155 	&workload__landlock,
156 	&workload__traploop,
157 	&workload__inlineloop,
158 
159 #ifdef HAVE_RUST_SUPPORT
160 	&workload__code_with_type,
161 #endif
162 };
163 
164 #define workloads__for_each(workload) \
165 	for (unsigned i = 0; i < ARRAY_SIZE(workloads) && ({ workload = workloads[i]; 1; }); i++)
166 
167 #define test_suite__for_each_test_case(suite, idx)			\
168 	for (idx = 0; (suite)->test_cases && (suite)->test_cases[idx].name != NULL; idx++)
169 
170 static void close_parent_fds(void)
171 {
172 	DIR *dir = opendir("/proc/self/fd");
173 	struct dirent *ent;
174 
175 	while ((ent = readdir(dir))) {
176 		char *end;
177 		long fd;
178 
179 		if (ent->d_type != DT_LNK)
180 			continue;
181 
182 		if (!isdigit(ent->d_name[0]))
183 			continue;
184 
185 		fd = strtol(ent->d_name, &end, 10);
186 		if (*end)
187 			continue;
188 
189 		if (fd <= 3 || fd == dirfd(dir))
190 			continue;
191 
192 		close(fd);
193 	}
194 	closedir(dir);
195 }
196 
197 static void check_leaks(void)
198 {
199 	DIR *dir = opendir("/proc/self/fd");
200 	struct dirent *ent;
201 	int leaks = 0;
202 
203 	while ((ent = readdir(dir))) {
204 		char path[PATH_MAX];
205 		char *end;
206 		long fd;
207 		ssize_t len;
208 
209 		if (ent->d_type != DT_LNK)
210 			continue;
211 
212 		if (!isdigit(ent->d_name[0]))
213 			continue;
214 
215 		fd = strtol(ent->d_name, &end, 10);
216 		if (*end)
217 			continue;
218 
219 		if (fd <= 3 || fd == dirfd(dir))
220 			continue;
221 
222 		leaks++;
223 		len = readlinkat(dirfd(dir), ent->d_name, path, sizeof(path));
224 		if (len > 0 && (size_t)len < sizeof(path))
225 			path[len] = '\0';
226 		else
227 			strncpy(path, ent->d_name, sizeof(path));
228 		pr_err("Leak of file descriptor %s that opened: '%s'\n", ent->d_name, path);
229 	}
230 	closedir(dir);
231 	if (leaks)
232 		abort();
233 }
234 
235 static int test_suite__num_test_cases(const struct test_suite *t)
236 {
237 	int num;
238 
239 	test_suite__for_each_test_case(t, num);
240 
241 	return num;
242 }
243 
244 static const char *skip_reason(const struct test_suite *t, int test_case)
245 {
246 	if (!t->test_cases)
247 		return NULL;
248 
249 	return t->test_cases[test_case >= 0 ? test_case : 0].skip_reason;
250 }
251 
252 static const char *test_description(const struct test_suite *t, int test_case)
253 {
254 	if (t->test_cases && test_case >= 0)
255 		return t->test_cases[test_case].desc;
256 
257 	return t->desc;
258 }
259 
260 static test_fnptr test_function(const struct test_suite *t, int test_case)
261 {
262 	if (test_case <= 0)
263 		return t->test_cases[0].run_case;
264 
265 	return t->test_cases[test_case].run_case;
266 }
267 
268 static bool test_exclusive(const struct test_suite *t, int test_case)
269 {
270 	if (test_case <= 0)
271 		return t->test_cases[0].exclusive;
272 
273 	return t->test_cases[test_case].exclusive;
274 }
275 
276 static bool perf_test__matches(const char *desc, int suite_num, int argc, const char *argv[])
277 {
278 	int i;
279 
280 	if (argc == 0)
281 		return true;
282 
283 	for (i = 0; i < argc; ++i) {
284 		char *end;
285 		long nr = strtoul(argv[i], &end, 10);
286 
287 		if (*end == '\0') {
288 			if (nr == suite_num + 1)
289 				return true;
290 			continue;
291 		}
292 
293 		if (strcasestr(desc, argv[i]))
294 			return true;
295 	}
296 
297 	return false;
298 }
299 
300 struct child_test {
301 	struct child_process process;
302 	struct test_suite *test;
303 	int suite_num;
304 	int test_case_num;
305 };
306 
307 static jmp_buf run_test_jmp_buf;
308 
309 static void child_test_sig_handler(int sig)
310 {
311 #ifdef HAVE_BACKTRACE_SUPPORT
312 	void *stackdump[32];
313 	size_t stackdump_size;
314 #endif
315 
316 	fprintf(stderr, "\n---- unexpected signal (%d) ----\n", sig);
317 #ifdef HAVE_BACKTRACE_SUPPORT
318 	stackdump_size = backtrace(stackdump, ARRAY_SIZE(stackdump));
319 	__dump_stack(stderr, stackdump, stackdump_size);
320 #endif
321 	siglongjmp(run_test_jmp_buf, sig);
322 }
323 
324 static int run_test_child(struct child_process *process)
325 {
326 	const int signals[] = {
327 		SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGINT, SIGPIPE, SIGQUIT, SIGSEGV, SIGTERM,
328 	};
329 	struct child_test *child = container_of(process, struct child_test, process);
330 	int err;
331 
332 	close_parent_fds();
333 
334 	err = sigsetjmp(run_test_jmp_buf, 1);
335 	if (err) {
336 		/* Received signal. */
337 		err = err > 0 ? -err : -1;
338 		goto err_out;
339 	}
340 
341 	for (size_t i = 0; i < ARRAY_SIZE(signals); i++)
342 		signal(signals[i], child_test_sig_handler);
343 
344 	pr_debug("--- start ---\n");
345 	pr_debug("test child forked, pid %d\n", getpid());
346 	err = test_function(child->test, child->test_case_num)(child->test, child->test_case_num);
347 	pr_debug("---- end(%d) ----\n", err);
348 
349 	check_leaks();
350 err_out:
351 	fflush(NULL);
352 	for (size_t i = 0; i < ARRAY_SIZE(signals); i++)
353 		signal(signals[i], SIG_DFL);
354 	return -err;
355 }
356 
357 #define TEST_RUNNING -3
358 
359 static int print_test_result(struct test_suite *t, int curr_suite, int curr_test_case,
360 			     int result, int width, int running)
361 {
362 	if (test_suite__num_test_cases(t) > 1) {
363 		int subw = width > 2 ? width - 2 : width;
364 
365 		pr_info("%3d.%1d: %-*s:", curr_suite + 1, curr_test_case + 1, subw,
366 			test_description(t, curr_test_case));
367 	} else
368 		pr_info("%3d: %-*s:", curr_suite + 1, width, test_description(t, curr_test_case));
369 
370 	switch (result) {
371 	case TEST_RUNNING:
372 		color_fprintf(stderr, PERF_COLOR_YELLOW, " Running (%d active)\n", running);
373 		break;
374 	case TEST_OK:
375 		pr_info(" Ok\n");
376 		break;
377 	case TEST_SKIP: {
378 		const char *reason = skip_reason(t, curr_test_case);
379 
380 		if (reason)
381 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
382 		else
383 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
384 	}
385 		break;
386 	case TEST_FAIL:
387 	default:
388 		color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
389 		break;
390 	}
391 
392 	return 0;
393 }
394 
395 static void finish_test(struct child_test **child_tests, int running_test, int child_test_num,
396 		int width)
397 {
398 	struct child_test *child_test = child_tests[running_test];
399 	struct test_suite *t;
400 	int curr_suite, curr_test_case, err;
401 	bool err_done = false;
402 	struct strbuf err_output = STRBUF_INIT;
403 	int last_running = -1;
404 	int ret;
405 
406 	if (child_test == NULL) {
407 		/* Test wasn't started. */
408 		return;
409 	}
410 	t = child_test->test;
411 	curr_suite = child_test->suite_num;
412 	curr_test_case = child_test->test_case_num;
413 	err = child_test->process.err;
414 	/*
415 	 * For test suites with subtests, display the suite name ahead of the
416 	 * sub test names.
417 	 */
418 	if (test_suite__num_test_cases(t) > 1 && curr_test_case == 0)
419 		pr_info("%3d: %-*s:\n", curr_suite + 1, width, test_description(t, -1));
420 
421 	/*
422 	 * Busy loop reading from the child's stdout/stderr that are set to be
423 	 * non-blocking until EOF.
424 	 */
425 	if (err > 0)
426 		fcntl(err, F_SETFL, O_NONBLOCK);
427 	if (verbose > 1) {
428 		if (test_suite__num_test_cases(t) > 1)
429 			pr_info("%3d.%1d: %s:\n", curr_suite + 1, curr_test_case + 1,
430 				test_description(t, curr_test_case));
431 		else
432 			pr_info("%3d: %s:\n", curr_suite + 1, test_description(t, -1));
433 	}
434 	while (!err_done) {
435 		struct pollfd pfds[1] = {
436 			{ .fd = err,
437 			  .events = POLLIN | POLLERR | POLLHUP | POLLNVAL,
438 			},
439 		};
440 		if (perf_use_color_default) {
441 			int running = 0;
442 
443 			for (int y = running_test; y < child_test_num; y++) {
444 				if (child_tests[y] == NULL)
445 					continue;
446 				if (check_if_command_finished(&child_tests[y]->process) == 0)
447 					running++;
448 			}
449 			if (running != last_running) {
450 				if (last_running != -1) {
451 					/*
452 					 * Erase "Running (.. active)" line
453 					 * printed before poll/sleep.
454 					 */
455 					fprintf(debug_file(), PERF_COLOR_DELETE_LINE);
456 				}
457 				print_test_result(t, curr_suite, curr_test_case, TEST_RUNNING,
458 						  width, running);
459 				last_running = running;
460 			}
461 		}
462 
463 		err_done = true;
464 		if (err <= 0) {
465 			/* No child stderr to poll, sleep for 10ms for child to complete. */
466 			usleep(10 * 1000);
467 		} else {
468 			/* Poll to avoid excessive spinning, timeout set for 100ms. */
469 			poll(pfds, ARRAY_SIZE(pfds), /*timeout=*/100);
470 			if (pfds[0].revents) {
471 				char buf[512];
472 				ssize_t len;
473 
474 				len = read(err, buf, sizeof(buf) - 1);
475 
476 				if (len > 0) {
477 					err_done = false;
478 					buf[len] = '\0';
479 					strbuf_addstr(&err_output, buf);
480 				}
481 			}
482 		}
483 		if (err_done)
484 			err_done = check_if_command_finished(&child_test->process);
485 	}
486 	if (perf_use_color_default && last_running != -1) {
487 		/* Erase "Running (.. active)" line printed before poll/sleep. */
488 		fprintf(debug_file(), PERF_COLOR_DELETE_LINE);
489 	}
490 	/* Clean up child process. */
491 	ret = finish_command(&child_test->process);
492 	if (verbose > 1 || (verbose == 1 && ret == TEST_FAIL))
493 		fprintf(stderr, "%s", err_output.buf);
494 
495 	strbuf_release(&err_output);
496 	print_test_result(t, curr_suite, curr_test_case, ret, width, /*running=*/0);
497 	if (err > 0)
498 		close(err);
499 	zfree(&child_tests[running_test]);
500 }
501 
502 static int start_test(struct test_suite *test, int curr_suite, int curr_test_case,
503 		struct child_test **child, int width, int pass)
504 {
505 	int err;
506 
507 	*child = NULL;
508 	if (dont_fork) {
509 		if (pass == 1) {
510 			pr_debug("--- start ---\n");
511 			err = test_function(test, curr_test_case)(test, curr_test_case);
512 			pr_debug("---- end ----\n");
513 			print_test_result(test, curr_suite, curr_test_case, err, width,
514 					  /*running=*/0);
515 		}
516 		return 0;
517 	}
518 	if (pass == 1 && !sequential && test_exclusive(test, curr_test_case)) {
519 		/* When parallel, skip exclusive tests on the first pass. */
520 		return 0;
521 	}
522 	if (pass != 1 && (sequential || !test_exclusive(test, curr_test_case))) {
523 		/* Sequential and non-exclusive tests were run on the first pass. */
524 		return 0;
525 	}
526 	*child = zalloc(sizeof(**child));
527 	if (!*child)
528 		return -ENOMEM;
529 
530 	(*child)->test = test;
531 	(*child)->suite_num = curr_suite;
532 	(*child)->test_case_num = curr_test_case;
533 	(*child)->process.pid = -1;
534 	(*child)->process.no_stdin = 1;
535 	if (verbose <= 0) {
536 		(*child)->process.no_stdout = 1;
537 		(*child)->process.no_stderr = 1;
538 	} else {
539 		(*child)->process.stdout_to_stderr = 1;
540 		(*child)->process.out = -1;
541 		(*child)->process.err = -1;
542 	}
543 	(*child)->process.no_exec_cmd = run_test_child;
544 	if (sequential || pass == 2) {
545 		err = start_command(&(*child)->process);
546 		if (err)
547 			return err;
548 		finish_test(child, /*running_test=*/0, /*child_test_num=*/1, width);
549 		return 0;
550 	}
551 	return start_command(&(*child)->process);
552 }
553 
554 /* State outside of __cmd_test for the sake of the signal handler. */
555 
556 static size_t num_tests;
557 static struct child_test **child_tests;
558 static jmp_buf cmd_test_jmp_buf;
559 
560 static void cmd_test_sig_handler(int sig)
561 {
562 	siglongjmp(cmd_test_jmp_buf, sig);
563 }
564 
565 static int __cmd_test(struct test_suite **suites, int argc, const char *argv[],
566 		      struct intlist *skiplist)
567 {
568 	static int width = 0;
569 	int err = 0;
570 
571 	for (struct test_suite **t = suites; *t; t++) {
572 		int i, len = strlen(test_description(*t, -1));
573 
574 		if (width < len)
575 			width = len;
576 
577 		test_suite__for_each_test_case(*t, i) {
578 			len = strlen(test_description(*t, i));
579 			if (width < len)
580 				width = len;
581 			num_tests += runs_per_test;
582 		}
583 	}
584 	child_tests = calloc(num_tests, sizeof(*child_tests));
585 	if (!child_tests)
586 		return -ENOMEM;
587 
588 	err = sigsetjmp(cmd_test_jmp_buf, 1);
589 	if (err) {
590 		pr_err("\nSignal (%d) while running tests.\nTerminating tests with the same signal\n",
591 		       err);
592 		for (size_t x = 0; x < num_tests; x++) {
593 			struct child_test *child_test = child_tests[x];
594 
595 			if (!child_test || child_test->process.pid <= 0)
596 				continue;
597 
598 			pr_debug3("Killing %d pid %d\n",
599 				  child_test->suite_num + 1,
600 				  child_test->process.pid);
601 			kill(child_test->process.pid, err);
602 		}
603 		goto err_out;
604 	}
605 	signal(SIGINT, cmd_test_sig_handler);
606 	signal(SIGTERM, cmd_test_sig_handler);
607 
608 	/*
609 	 * In parallel mode pass 1 runs non-exclusive tests in parallel, pass 2
610 	 * runs the exclusive tests sequentially. In other modes all tests are
611 	 * run in pass 1.
612 	 */
613 	for (int pass = 1; pass <= 2; pass++) {
614 		int child_test_num = 0;
615 		int curr_suite = 0;
616 
617 		for (struct test_suite **t = suites; *t; t++, curr_suite++) {
618 			int curr_test_case;
619 			bool suite_matched = false;
620 
621 			if (!perf_test__matches(test_description(*t, -1), curr_suite, argc, argv)) {
622 				/*
623 				 * Test suite shouldn't be run based on
624 				 * description. See if any test case should.
625 				 */
626 				bool skip = true;
627 
628 				test_suite__for_each_test_case(*t, curr_test_case) {
629 					if (perf_test__matches(test_description(*t, curr_test_case),
630 							       curr_suite, argc, argv)) {
631 						skip = false;
632 						break;
633 					}
634 				}
635 				if (skip)
636 					continue;
637 			} else {
638 				suite_matched = true;
639 			}
640 
641 			if (intlist__find(skiplist, curr_suite + 1)) {
642 				pr_info("%3d: %-*s:", curr_suite + 1, width,
643 					test_description(*t, -1));
644 				color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
645 				continue;
646 			}
647 
648 			for (unsigned int run = 0; run < runs_per_test; run++) {
649 				test_suite__for_each_test_case(*t, curr_test_case) {
650 					if (!suite_matched &&
651 					    !perf_test__matches(test_description(*t, curr_test_case),
652 								curr_suite, argc, argv))
653 						continue;
654 					err = start_test(*t, curr_suite, curr_test_case,
655 							 &child_tests[child_test_num++],
656 							 width, pass);
657 					if (err)
658 						goto err_out;
659 				}
660 			}
661 		}
662 		if (!sequential) {
663 			/* Parallel mode starts tests but doesn't finish them. Do that now. */
664 			for (size_t x = 0; x < num_tests; x++)
665 				finish_test(child_tests, x, num_tests, width);
666 		}
667 	}
668 err_out:
669 	signal(SIGINT, SIG_DFL);
670 	signal(SIGTERM, SIG_DFL);
671 	if (err) {
672 		pr_err("Internal test harness failure. Completing any started tests:\n:");
673 		for (size_t x = 0; x < num_tests; x++)
674 			finish_test(child_tests, x, num_tests, width);
675 	}
676 	free(child_tests);
677 	return err;
678 }
679 
680 static int perf_test__list(FILE *fp, struct test_suite **suites, int argc, const char **argv)
681 {
682 	int curr_suite = 0;
683 
684 	for (struct test_suite **t = suites; *t; t++, curr_suite++) {
685 		int curr_test_case;
686 
687 		if (!perf_test__matches(test_description(*t, -1), curr_suite, argc, argv))
688 			continue;
689 
690 		fprintf(fp, "%3d: %s\n", curr_suite + 1, test_description(*t, -1));
691 
692 		if (test_suite__num_test_cases(*t) <= 1)
693 			continue;
694 
695 		test_suite__for_each_test_case(*t, curr_test_case) {
696 			fprintf(fp, "%3d.%1d: %s\n", curr_suite + 1, curr_test_case + 1,
697 				test_description(*t, curr_test_case));
698 		}
699 	}
700 	return 0;
701 }
702 
703 static int workloads__fprintf_list(FILE *fp)
704 {
705 	struct test_workload *twl;
706 	int printed = 0;
707 
708 	workloads__for_each(twl)
709 		printed += fprintf(fp, "%s\n", twl->name);
710 
711 	return printed;
712 }
713 
714 static int run_workload(const char *work, int argc, const char **argv)
715 {
716 	struct test_workload *twl;
717 
718 	workloads__for_each(twl) {
719 		if (!strcmp(twl->name, work))
720 			return twl->func(argc, argv);
721 	}
722 
723 	pr_info("No workload found: %s\n", work);
724 	return -1;
725 }
726 
727 static int perf_test__config(const char *var, const char *value,
728 			     void *data __maybe_unused)
729 {
730 	if (!strcmp(var, "annotate.objdump"))
731 		test_objdump_path = value;
732 
733 	return 0;
734 }
735 
736 static struct test_suite **build_suites(void)
737 {
738 	/*
739 	 * TODO: suites is static to avoid needing to clean up the scripts tests
740 	 * for leak sanitizer.
741 	 */
742 	static struct test_suite **suites[] = {
743 		generic_tests,
744 		arch_tests,
745 		NULL,
746 	};
747 	struct test_suite **result;
748 	struct test_suite *t;
749 	size_t n = 0, num_suites = 0;
750 
751 	if (suites[2] == NULL)
752 		suites[2] = create_script_test_suites();
753 
754 #define for_each_suite(suite)						\
755 	for (size_t i = 0, j = 0; i < ARRAY_SIZE(suites); i++, j = 0)	\
756 		while ((suite = suites[i][j++]) != NULL)
757 
758 	for_each_suite(t)
759 		num_suites++;
760 
761 	result = calloc(num_suites + 1, sizeof(struct test_suite *));
762 
763 	for (int pass = 1; pass <= 2; pass++) {
764 		for_each_suite(t) {
765 			bool exclusive = false;
766 			int curr_test_case;
767 
768 			test_suite__for_each_test_case(t, curr_test_case) {
769 				if (test_exclusive(t, curr_test_case)) {
770 					exclusive = true;
771 					break;
772 				}
773 			}
774 			if ((!exclusive && pass == 1) || (exclusive && pass == 2))
775 				result[n++] = t;
776 		}
777 	}
778 	return result;
779 #undef for_each_suite
780 }
781 
782 int cmd_test(int argc, const char **argv)
783 {
784 	const char *test_usage[] = {
785 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
786 	NULL,
787 	};
788 	const char *skip = NULL;
789 	const char *workload = NULL;
790 	bool list_workloads = false;
791 	const struct option test_options[] = {
792 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
793 	OPT_INCR('v', "verbose", &verbose,
794 		    "be more verbose (show symbol address, etc)"),
795 	OPT_BOOLEAN('F', "dont-fork", &dont_fork,
796 		    "Do not fork for testcase"),
797 	OPT_BOOLEAN('S', "sequential", &sequential,
798 		    "Run the tests one after another rather than in parallel"),
799 	OPT_UINTEGER('r', "runs-per-test", &runs_per_test,
800 		     "Run each test the given number of times, default 1"),
801 	OPT_STRING('w', "workload", &workload, "work", "workload to run for testing, use '--list-workloads' to list the available ones."),
802 	OPT_BOOLEAN(0, "list-workloads", &list_workloads, "List the available builtin workloads to use with -w/--workload"),
803 	OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
804 	OPT_STRING(0, "objdump", &test_objdump_path, "path",
805 		   "objdump binary to use for disassembly and annotations"),
806 	OPT_END()
807 	};
808 	const char * const test_subcommands[] = { "list", NULL };
809 	struct intlist *skiplist = NULL;
810         int ret = hists__init();
811 	struct test_suite **suites;
812 
813         if (ret < 0)
814                 return ret;
815 
816 	perf_config(perf_test__config, NULL);
817 
818 	/* Unbuffered output */
819 	setvbuf(stdout, NULL, _IONBF, 0);
820 
821 	argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
822 	if (argc >= 1 && !strcmp(argv[0], "list")) {
823 		suites = build_suites();
824 		ret = perf_test__list(stdout, suites, argc - 1, argv + 1);
825 		free(suites);
826 		return ret;
827 	}
828 
829 	if (workload)
830 		return run_workload(workload, argc, argv);
831 
832 	if (list_workloads) {
833 		workloads__fprintf_list(stdout);
834 		return 0;
835 	}
836 
837 	if (dont_fork)
838 		sequential = true;
839 
840 	symbol_conf.priv_size = sizeof(int);
841 	symbol_conf.try_vmlinux_path = true;
842 
843 
844 	if (symbol__init(NULL) < 0)
845 		return -1;
846 
847 	if (skip != NULL)
848 		skiplist = intlist__new(skip);
849 	/*
850 	 * Tests that create BPF maps, for instance, need more than the 64K
851 	 * default:
852 	 */
853 	rlimit__bump_memlock();
854 
855 	suites = build_suites();
856 	ret = __cmd_test(suites, argc, argv, skiplist);
857 	free(suites);
858 	return ret;
859 }
860