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