xref: /openbmc/linux/tools/perf/tests/builtin-test.c (revision 24069d81)
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 <stdlib.h>
12 #include <sys/types.h>
13 #include <dirent.h>
14 #include <sys/wait.h>
15 #include <sys/stat.h>
16 #include "builtin.h"
17 #include "hist.h"
18 #include "intlist.h"
19 #include "tests.h"
20 #include "debug.h"
21 #include "color.h"
22 #include <subcmd/parse-options.h>
23 #include "string2.h"
24 #include "symbol.h"
25 #include "util/rlimit.h"
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <subcmd/exec-cmd.h>
29 #include <linux/zalloc.h>
30 
31 #include "builtin-test-list.h"
32 
33 static bool dont_fork;
34 const char *dso_to_test;
35 
36 /*
37  * List of architecture specific tests. Not a weak symbol as the array length is
38  * dependent on the initialization, as such GCC with LTO complains of
39  * conflicting definitions with a weak symbol.
40  */
41 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
42 extern struct test_suite *arch_tests[];
43 #else
44 static struct test_suite *arch_tests[] = {
45 	NULL,
46 };
47 #endif
48 
49 static struct test_suite *generic_tests[] = {
50 	&suite__vmlinux_matches_kallsyms,
51 #ifdef HAVE_LIBTRACEEVENT
52 	&suite__openat_syscall_event,
53 	&suite__openat_syscall_event_on_all_cpus,
54 	&suite__basic_mmap,
55 #endif
56 	&suite__mem,
57 	&suite__parse_events,
58 	&suite__expr,
59 	&suite__PERF_RECORD,
60 	&suite__pmu,
61 	&suite__pmu_events,
62 	&suite__dso_data,
63 	&suite__dso_data_cache,
64 	&suite__dso_data_reopen,
65 	&suite__perf_evsel__roundtrip_name_test,
66 #ifdef HAVE_LIBTRACEEVENT
67 	&suite__perf_evsel__tp_sched_test,
68 	&suite__syscall_openat_tp_fields,
69 #endif
70 	&suite__attr,
71 	&suite__hists_link,
72 	&suite__python_use,
73 	&suite__bp_signal,
74 	&suite__bp_signal_overflow,
75 	&suite__bp_accounting,
76 	&suite__wp,
77 	&suite__task_exit,
78 	&suite__sw_clock_freq,
79 	&suite__code_reading,
80 	&suite__sample_parsing,
81 	&suite__keep_tracking,
82 	&suite__parse_no_sample_id_all,
83 	&suite__hists_filter,
84 	&suite__mmap_thread_lookup,
85 	&suite__thread_maps_share,
86 	&suite__hists_output,
87 	&suite__hists_cumulate,
88 #ifdef HAVE_LIBTRACEEVENT
89 	&suite__switch_tracking,
90 #endif
91 	&suite__fdarray__filter,
92 	&suite__fdarray__add,
93 	&suite__kmod_path__parse,
94 	&suite__thread_map,
95 	&suite__llvm,
96 	&suite__session_topology,
97 	&suite__bpf,
98 	&suite__thread_map_synthesize,
99 	&suite__thread_map_remove,
100 	&suite__cpu_map,
101 	&suite__synthesize_stat_config,
102 	&suite__synthesize_stat,
103 	&suite__synthesize_stat_round,
104 	&suite__event_update,
105 	&suite__event_times,
106 	&suite__backward_ring_buffer,
107 	&suite__sdt_event,
108 	&suite__is_printable_array,
109 	&suite__bitmap_print,
110 	&suite__perf_hooks,
111 	&suite__clang,
112 	&suite__unit_number__scnprint,
113 	&suite__mem2node,
114 	&suite__time_utils,
115 	&suite__jit_write_elf,
116 	&suite__pfm,
117 	&suite__api_io,
118 	&suite__maps__merge_in,
119 	&suite__demangle_java,
120 	&suite__demangle_ocaml,
121 	&suite__parse_metric,
122 	&suite__pe_file_parsing,
123 	&suite__expand_cgroup_events,
124 	&suite__perf_time_to_tsc,
125 	&suite__dlfilter,
126 	&suite__sigtrap,
127 	&suite__event_groups,
128 	&suite__symbols,
129 	NULL,
130 };
131 
132 static struct test_suite **tests[] = {
133 	generic_tests,
134 	arch_tests,
135 };
136 
137 static struct test_workload *workloads[] = {
138 	&workload__noploop,
139 	&workload__thloop,
140 	&workload__leafloop,
141 	&workload__sqrtloop,
142 	&workload__brstack,
143 	&workload__datasym,
144 };
145 
146 static int num_subtests(const struct test_suite *t)
147 {
148 	int num;
149 
150 	if (!t->test_cases)
151 		return 0;
152 
153 	num = 0;
154 	while (t->test_cases[num].name)
155 		num++;
156 
157 	return num;
158 }
159 
160 static bool has_subtests(const struct test_suite *t)
161 {
162 	return num_subtests(t) > 1;
163 }
164 
165 static const char *skip_reason(const struct test_suite *t, int subtest)
166 {
167 	if (!t->test_cases)
168 		return NULL;
169 
170 	return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason;
171 }
172 
173 static const char *test_description(const struct test_suite *t, int subtest)
174 {
175 	if (t->test_cases && subtest >= 0)
176 		return t->test_cases[subtest].desc;
177 
178 	return t->desc;
179 }
180 
181 static test_fnptr test_function(const struct test_suite *t, int subtest)
182 {
183 	if (subtest <= 0)
184 		return t->test_cases[0].run_case;
185 
186 	return t->test_cases[subtest].run_case;
187 }
188 
189 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
190 {
191 	int i;
192 
193 	if (argc == 0)
194 		return true;
195 
196 	for (i = 0; i < argc; ++i) {
197 		char *end;
198 		long nr = strtoul(argv[i], &end, 10);
199 
200 		if (*end == '\0') {
201 			if (nr == curr + 1)
202 				return true;
203 			continue;
204 		}
205 
206 		if (strcasestr(desc, argv[i]))
207 			return true;
208 	}
209 
210 	return false;
211 }
212 
213 static int run_test(struct test_suite *test, int subtest)
214 {
215 	int status, err = -1, child = dont_fork ? 0 : fork();
216 	char sbuf[STRERR_BUFSIZE];
217 
218 	if (child < 0) {
219 		pr_err("failed to fork test: %s\n",
220 			str_error_r(errno, sbuf, sizeof(sbuf)));
221 		return -1;
222 	}
223 
224 	if (!child) {
225 		if (!dont_fork) {
226 			pr_debug("test child forked, pid %d\n", getpid());
227 
228 			if (verbose <= 0) {
229 				int nullfd = open("/dev/null", O_WRONLY);
230 
231 				if (nullfd >= 0) {
232 					close(STDERR_FILENO);
233 					close(STDOUT_FILENO);
234 
235 					dup2(nullfd, STDOUT_FILENO);
236 					dup2(STDOUT_FILENO, STDERR_FILENO);
237 					close(nullfd);
238 				}
239 			} else {
240 				signal(SIGSEGV, sighandler_dump_stack);
241 				signal(SIGFPE, sighandler_dump_stack);
242 			}
243 		}
244 
245 		err = test_function(test, subtest)(test, subtest);
246 		if (!dont_fork)
247 			exit(err);
248 	}
249 
250 	if (!dont_fork) {
251 		wait(&status);
252 
253 		if (WIFEXITED(status)) {
254 			err = (signed char)WEXITSTATUS(status);
255 			pr_debug("test child finished with %d\n", err);
256 		} else if (WIFSIGNALED(status)) {
257 			err = -1;
258 			pr_debug("test child interrupted\n");
259 		}
260 	}
261 
262 	return err;
263 }
264 
265 #define for_each_test(j, k, t)			\
266 	for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0)	\
267 		while ((t = tests[j][k++]) != NULL)
268 
269 static int test_and_print(struct test_suite *t, int subtest)
270 {
271 	int err;
272 
273 	pr_debug("\n--- start ---\n");
274 	err = run_test(t, subtest);
275 	pr_debug("---- end ----\n");
276 
277 	if (!has_subtests(t))
278 		pr_debug("%s:", t->desc);
279 	else
280 		pr_debug("%s subtest %d:", t->desc, subtest + 1);
281 
282 	switch (err) {
283 	case TEST_OK:
284 		pr_info(" Ok\n");
285 		break;
286 	case TEST_SKIP: {
287 		const char *reason = skip_reason(t, subtest);
288 
289 		if (reason)
290 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
291 		else
292 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
293 	}
294 		break;
295 	case TEST_FAIL:
296 	default:
297 		color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
298 		break;
299 	}
300 
301 	return err;
302 }
303 
304 struct shell_test {
305 	const char *dir;
306 	const char *file;
307 };
308 
309 static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
310 {
311 	int err;
312 	char script[PATH_MAX];
313 	struct shell_test *st = test->priv;
314 
315 	path__join(script, sizeof(script) - 3, st->dir, st->file);
316 
317 	if (verbose > 0)
318 		strncat(script, " -v", sizeof(script) - strlen(script) - 1);
319 
320 	err = system(script);
321 	if (!err)
322 		return TEST_OK;
323 
324 	return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
325 }
326 
327 static int run_shell_tests(int argc, const char *argv[], int i, int width,
328 				struct intlist *skiplist)
329 {
330 	struct shell_test st;
331 	const struct script_file *files, *file;
332 
333 	files = list_script_files();
334 	if (!files)
335 		return 0;
336 	for (file = files; file->dir; file++) {
337 		int curr = i++;
338 		struct test_case test_cases[] = {
339 			{
340 				.desc = file->desc,
341 				.run_case = shell_test__run,
342 			},
343 			{ .name = NULL, }
344 		};
345 		struct test_suite test_suite = {
346 			.desc = test_cases[0].desc,
347 			.test_cases = test_cases,
348 			.priv = &st,
349 		};
350 		st.dir = file->dir;
351 
352 		if (test_suite.desc == NULL ||
353 		    !perf_test__matches(test_suite.desc, curr, argc, argv))
354 			continue;
355 
356 		st.file = file->file;
357 		pr_info("%3d: %-*s:", i, width, test_suite.desc);
358 
359 		if (intlist__find(skiplist, i)) {
360 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
361 			continue;
362 		}
363 
364 		test_and_print(&test_suite, 0);
365 	}
366 	return 0;
367 }
368 
369 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
370 {
371 	struct test_suite *t;
372 	unsigned int j, k;
373 	int i = 0;
374 	int width = list_script_max_width();
375 
376 	for_each_test(j, k, t) {
377 		int len = strlen(test_description(t, -1));
378 
379 		if (width < len)
380 			width = len;
381 	}
382 
383 	for_each_test(j, k, t) {
384 		int curr = i++;
385 		int subi;
386 
387 		if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
388 			bool skip = true;
389 			int subn;
390 
391 			subn = num_subtests(t);
392 
393 			for (subi = 0; subi < subn; subi++) {
394 				if (perf_test__matches(test_description(t, subi),
395 							curr, argc, argv))
396 					skip = false;
397 			}
398 
399 			if (skip)
400 				continue;
401 		}
402 
403 		pr_info("%3d: %-*s:", i, width, test_description(t, -1));
404 
405 		if (intlist__find(skiplist, i)) {
406 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
407 			continue;
408 		}
409 
410 		if (!has_subtests(t)) {
411 			test_and_print(t, -1);
412 		} else {
413 			int subn = num_subtests(t);
414 			/*
415 			 * minus 2 to align with normal testcases.
416 			 * For subtest we print additional '.x' in number.
417 			 * for example:
418 			 *
419 			 * 35: Test LLVM searching and compiling                        :
420 			 * 35.1: Basic BPF llvm compiling test                          : Ok
421 			 */
422 			int subw = width > 2 ? width - 2 : width;
423 
424 			if (subn <= 0) {
425 				color_fprintf(stderr, PERF_COLOR_YELLOW,
426 					      " Skip (not compiled in)\n");
427 				continue;
428 			}
429 			pr_info("\n");
430 
431 			for (subi = 0; subi < subn; subi++) {
432 				int len = strlen(test_description(t, subi));
433 
434 				if (subw < len)
435 					subw = len;
436 			}
437 
438 			for (subi = 0; subi < subn; subi++) {
439 				if (!perf_test__matches(test_description(t, subi),
440 							curr, argc, argv))
441 					continue;
442 
443 				pr_info("%3d.%1d: %-*s:", i, subi + 1, subw,
444 					test_description(t, subi));
445 				test_and_print(t, subi);
446 			}
447 		}
448 	}
449 
450 	return run_shell_tests(argc, argv, i, width, skiplist);
451 }
452 
453 static int perf_test__list_shell(int argc, const char **argv, int i)
454 {
455 	const struct script_file *files, *file;
456 
457 	files = list_script_files();
458 	if (!files)
459 		return 0;
460 	for (file = files; file->dir; file++) {
461 		int curr = i++;
462 		struct test_suite t = {
463 			.desc = file->desc
464 		};
465 
466 		if (!perf_test__matches(t.desc, curr, argc, argv))
467 			continue;
468 
469 		pr_info("%3d: %s\n", i, t.desc);
470 	}
471 	return 0;
472 }
473 
474 static int perf_test__list(int argc, const char **argv)
475 {
476 	unsigned int j, k;
477 	struct test_suite *t;
478 	int i = 0;
479 
480 	for_each_test(j, k, t) {
481 		int curr = i++;
482 
483 		if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
484 			continue;
485 
486 		pr_info("%3d: %s\n", i, test_description(t, -1));
487 
488 		if (has_subtests(t)) {
489 			int subn = num_subtests(t);
490 			int subi;
491 
492 			for (subi = 0; subi < subn; subi++)
493 				pr_info("%3d:%1d: %s\n", i, subi + 1,
494 					test_description(t, subi));
495 		}
496 	}
497 
498 	perf_test__list_shell(argc, argv, i);
499 
500 	return 0;
501 }
502 
503 static int run_workload(const char *work, int argc, const char **argv)
504 {
505 	unsigned int i = 0;
506 	struct test_workload *twl;
507 
508 	for (i = 0; i < ARRAY_SIZE(workloads); i++) {
509 		twl = workloads[i];
510 		if (!strcmp(twl->name, work))
511 			return twl->func(argc, argv);
512 	}
513 
514 	pr_info("No workload found: %s\n", work);
515 	return -1;
516 }
517 
518 int cmd_test(int argc, const char **argv)
519 {
520 	const char *test_usage[] = {
521 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
522 	NULL,
523 	};
524 	const char *skip = NULL;
525 	const char *workload = NULL;
526 	const struct option test_options[] = {
527 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
528 	OPT_INCR('v', "verbose", &verbose,
529 		    "be more verbose (show symbol address, etc)"),
530 	OPT_BOOLEAN('F', "dont-fork", &dont_fork,
531 		    "Do not fork for testcase"),
532 	OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"),
533 	OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
534 	OPT_END()
535 	};
536 	const char * const test_subcommands[] = { "list", NULL };
537 	struct intlist *skiplist = NULL;
538         int ret = hists__init();
539 
540         if (ret < 0)
541                 return ret;
542 
543 	/* Unbuffered output */
544 	setvbuf(stdout, NULL, _IONBF, 0);
545 
546 	argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
547 	if (argc >= 1 && !strcmp(argv[0], "list"))
548 		return perf_test__list(argc - 1, argv + 1);
549 
550 	if (workload)
551 		return run_workload(workload, argc, argv);
552 
553 	symbol_conf.priv_size = sizeof(int);
554 	symbol_conf.try_vmlinux_path = true;
555 
556 	if (symbol__init(NULL) < 0)
557 		return -1;
558 
559 	if (skip != NULL)
560 		skiplist = intlist__new(skip);
561 	/*
562 	 * Tests that create BPF maps, for instance, need more than the 64K
563 	 * default:
564 	 */
565 	rlimit__bump_memlock();
566 
567 	return __cmd_test(argc, argv, skiplist);
568 }
569