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