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