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