xref: /openbmc/linux/tools/perf/tests/builtin-test.c (revision a36954f5)
1 /*
2  * builtin-test.c
3  *
4  * Builtin regression testing command: ever growing number of sanity tests
5  */
6 #include <errno.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <sys/wait.h>
10 #include "builtin.h"
11 #include "hist.h"
12 #include "intlist.h"
13 #include "tests.h"
14 #include "debug.h"
15 #include "color.h"
16 #include <subcmd/parse-options.h>
17 #include "symbol.h"
18 #include <linux/kernel.h>
19 
20 static bool dont_fork;
21 
22 struct test __weak arch_tests[] = {
23 	{
24 		.func = NULL,
25 	},
26 };
27 
28 static struct test generic_tests[] = {
29 	{
30 		.desc = "vmlinux symtab matches kallsyms",
31 		.func = test__vmlinux_matches_kallsyms,
32 	},
33 	{
34 		.desc = "Detect openat syscall event",
35 		.func = test__openat_syscall_event,
36 	},
37 	{
38 		.desc = "Detect openat syscall event on all cpus",
39 		.func = test__openat_syscall_event_on_all_cpus,
40 	},
41 	{
42 		.desc = "Read samples using the mmap interface",
43 		.func = test__basic_mmap,
44 	},
45 	{
46 		.desc = "Parse event definition strings",
47 		.func = test__parse_events,
48 	},
49 	{
50 		.desc = "Simple expression parser",
51 		.func = test__expr,
52 	},
53 	{
54 		.desc = "PERF_RECORD_* events & perf_sample fields",
55 		.func = test__PERF_RECORD,
56 	},
57 	{
58 		.desc = "Parse perf pmu format",
59 		.func = test__pmu,
60 	},
61 	{
62 		.desc = "DSO data read",
63 		.func = test__dso_data,
64 	},
65 	{
66 		.desc = "DSO data cache",
67 		.func = test__dso_data_cache,
68 	},
69 	{
70 		.desc = "DSO data reopen",
71 		.func = test__dso_data_reopen,
72 	},
73 	{
74 		.desc = "Roundtrip evsel->name",
75 		.func = test__perf_evsel__roundtrip_name_test,
76 	},
77 	{
78 		.desc = "Parse sched tracepoints fields",
79 		.func = test__perf_evsel__tp_sched_test,
80 	},
81 	{
82 		.desc = "syscalls:sys_enter_openat event fields",
83 		.func = test__syscall_openat_tp_fields,
84 	},
85 	{
86 		.desc = "Setup struct perf_event_attr",
87 		.func = test__attr,
88 	},
89 	{
90 		.desc = "Match and link multiple hists",
91 		.func = test__hists_link,
92 	},
93 	{
94 		.desc = "'import perf' in python",
95 		.func = test__python_use,
96 	},
97 	{
98 		.desc = "Breakpoint overflow signal handler",
99 		.func = test__bp_signal,
100 	},
101 	{
102 		.desc = "Breakpoint overflow sampling",
103 		.func = test__bp_signal_overflow,
104 	},
105 	{
106 		.desc = "Number of exit events of a simple workload",
107 		.func = test__task_exit,
108 	},
109 	{
110 		.desc = "Software clock events period values",
111 		.func = test__sw_clock_freq,
112 	},
113 	{
114 		.desc = "Object code reading",
115 		.func = test__code_reading,
116 	},
117 	{
118 		.desc = "Sample parsing",
119 		.func = test__sample_parsing,
120 	},
121 	{
122 		.desc = "Use a dummy software event to keep tracking",
123 		.func = test__keep_tracking,
124 	},
125 	{
126 		.desc = "Parse with no sample_id_all bit set",
127 		.func = test__parse_no_sample_id_all,
128 	},
129 	{
130 		.desc = "Filter hist entries",
131 		.func = test__hists_filter,
132 	},
133 	{
134 		.desc = "Lookup mmap thread",
135 		.func = test__mmap_thread_lookup,
136 	},
137 	{
138 		.desc = "Share thread mg",
139 		.func = test__thread_mg_share,
140 	},
141 	{
142 		.desc = "Sort output of hist entries",
143 		.func = test__hists_output,
144 	},
145 	{
146 		.desc = "Cumulate child hist entries",
147 		.func = test__hists_cumulate,
148 	},
149 	{
150 		.desc = "Track with sched_switch",
151 		.func = test__switch_tracking,
152 	},
153 	{
154 		.desc = "Filter fds with revents mask in a fdarray",
155 		.func = test__fdarray__filter,
156 	},
157 	{
158 		.desc = "Add fd to a fdarray, making it autogrow",
159 		.func = test__fdarray__add,
160 	},
161 	{
162 		.desc = "kmod_path__parse",
163 		.func = test__kmod_path__parse,
164 	},
165 	{
166 		.desc = "Thread map",
167 		.func = test__thread_map,
168 	},
169 	{
170 		.desc = "LLVM search and compile",
171 		.func = test__llvm,
172 		.subtest = {
173 			.skip_if_fail	= true,
174 			.get_nr		= test__llvm_subtest_get_nr,
175 			.get_desc	= test__llvm_subtest_get_desc,
176 		},
177 	},
178 	{
179 		.desc = "Session topology",
180 		.func = test_session_topology,
181 	},
182 	{
183 		.desc = "BPF filter",
184 		.func = test__bpf,
185 		.subtest = {
186 			.skip_if_fail	= true,
187 			.get_nr		= test__bpf_subtest_get_nr,
188 			.get_desc	= test__bpf_subtest_get_desc,
189 		},
190 	},
191 	{
192 		.desc = "Synthesize thread map",
193 		.func = test__thread_map_synthesize,
194 	},
195 	{
196 		.desc = "Remove thread map",
197 		.func = test__thread_map_remove,
198 	},
199 	{
200 		.desc = "Synthesize cpu map",
201 		.func = test__cpu_map_synthesize,
202 	},
203 	{
204 		.desc = "Synthesize stat config",
205 		.func = test__synthesize_stat_config,
206 	},
207 	{
208 		.desc = "Synthesize stat",
209 		.func = test__synthesize_stat,
210 	},
211 	{
212 		.desc = "Synthesize stat round",
213 		.func = test__synthesize_stat_round,
214 	},
215 	{
216 		.desc = "Synthesize attr update",
217 		.func = test__event_update,
218 	},
219 	{
220 		.desc = "Event times",
221 		.func = test__event_times,
222 	},
223 	{
224 		.desc = "Read backward ring buffer",
225 		.func = test__backward_ring_buffer,
226 	},
227 	{
228 		.desc = "Print cpu map",
229 		.func = test__cpu_map_print,
230 	},
231 	{
232 		.desc = "Probe SDT events",
233 		.func = test__sdt_event,
234 	},
235 	{
236 		.desc = "is_printable_array",
237 		.func = test__is_printable_array,
238 	},
239 	{
240 		.desc = "Print bitmap",
241 		.func = test__bitmap_print,
242 	},
243 	{
244 		.desc = "perf hooks",
245 		.func = test__perf_hooks,
246 	},
247 	{
248 		.desc = "builtin clang support",
249 		.func = test__clang,
250 		.subtest = {
251 			.skip_if_fail	= true,
252 			.get_nr		= test__clang_subtest_get_nr,
253 			.get_desc	= test__clang_subtest_get_desc,
254 		}
255 	},
256 	{
257 		.desc = "unit_number__scnprintf",
258 		.func = test__unit_number__scnprint,
259 	},
260 	{
261 		.func = NULL,
262 	},
263 };
264 
265 static struct test *tests[] = {
266 	generic_tests,
267 	arch_tests,
268 };
269 
270 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
271 {
272 	int i;
273 
274 	if (argc == 0)
275 		return true;
276 
277 	for (i = 0; i < argc; ++i) {
278 		char *end;
279 		long nr = strtoul(argv[i], &end, 10);
280 
281 		if (*end == '\0') {
282 			if (nr == curr + 1)
283 				return true;
284 			continue;
285 		}
286 
287 		if (strcasestr(test->desc, argv[i]))
288 			return true;
289 	}
290 
291 	return false;
292 }
293 
294 static int run_test(struct test *test, int subtest)
295 {
296 	int status, err = -1, child = dont_fork ? 0 : fork();
297 	char sbuf[STRERR_BUFSIZE];
298 
299 	if (child < 0) {
300 		pr_err("failed to fork test: %s\n",
301 			str_error_r(errno, sbuf, sizeof(sbuf)));
302 		return -1;
303 	}
304 
305 	if (!child) {
306 		if (!dont_fork) {
307 			pr_debug("test child forked, pid %d\n", getpid());
308 
309 			if (verbose <= 0) {
310 				int nullfd = open("/dev/null", O_WRONLY);
311 
312 				if (nullfd >= 0) {
313 					close(STDERR_FILENO);
314 					close(STDOUT_FILENO);
315 
316 					dup2(nullfd, STDOUT_FILENO);
317 					dup2(STDOUT_FILENO, STDERR_FILENO);
318 					close(nullfd);
319 				}
320 			} else {
321 				signal(SIGSEGV, sighandler_dump_stack);
322 				signal(SIGFPE, sighandler_dump_stack);
323 			}
324 		}
325 
326 		err = test->func(subtest);
327 		if (!dont_fork)
328 			exit(err);
329 	}
330 
331 	if (!dont_fork) {
332 		wait(&status);
333 
334 		if (WIFEXITED(status)) {
335 			err = (signed char)WEXITSTATUS(status);
336 			pr_debug("test child finished with %d\n", err);
337 		} else if (WIFSIGNALED(status)) {
338 			err = -1;
339 			pr_debug("test child interrupted\n");
340 		}
341 	}
342 
343 	return err;
344 }
345 
346 #define for_each_test(j, t)	 				\
347 	for (j = 0; j < ARRAY_SIZE(tests); j++)	\
348 		for (t = &tests[j][0]; t->func; t++)
349 
350 static int test_and_print(struct test *t, bool force_skip, int subtest)
351 {
352 	int err;
353 
354 	if (!force_skip) {
355 		pr_debug("\n--- start ---\n");
356 		err = run_test(t, subtest);
357 		pr_debug("---- end ----\n");
358 	} else {
359 		pr_debug("\n--- force skipped ---\n");
360 		err = TEST_SKIP;
361 	}
362 
363 	if (!t->subtest.get_nr)
364 		pr_debug("%s:", t->desc);
365 	else
366 		pr_debug("%s subtest %d:", t->desc, subtest);
367 
368 	switch (err) {
369 	case TEST_OK:
370 		pr_info(" Ok\n");
371 		break;
372 	case TEST_SKIP:
373 		color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
374 		break;
375 	case TEST_FAIL:
376 	default:
377 		color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
378 		break;
379 	}
380 
381 	return err;
382 }
383 
384 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
385 {
386 	struct test *t;
387 	unsigned int j;
388 	int i = 0;
389 	int width = 0;
390 
391 	for_each_test(j, t) {
392 		int len = strlen(t->desc);
393 
394 		if (width < len)
395 			width = len;
396 	}
397 
398 	for_each_test(j, t) {
399 		int curr = i++, err;
400 
401 		if (!perf_test__matches(t, curr, argc, argv))
402 			continue;
403 
404 		pr_info("%2d: %-*s:", i, width, t->desc);
405 
406 		if (intlist__find(skiplist, i)) {
407 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
408 			continue;
409 		}
410 
411 		if (!t->subtest.get_nr) {
412 			test_and_print(t, false, -1);
413 		} else {
414 			int subn = t->subtest.get_nr();
415 			/*
416 			 * minus 2 to align with normal testcases.
417 			 * For subtest we print additional '.x' in number.
418 			 * for example:
419 			 *
420 			 * 35: Test LLVM searching and compiling                        :
421 			 * 35.1: Basic BPF llvm compiling test                          : Ok
422 			 */
423 			int subw = width > 2 ? width - 2 : width;
424 			bool skip = false;
425 			int subi;
426 
427 			if (subn <= 0) {
428 				color_fprintf(stderr, PERF_COLOR_YELLOW,
429 					      " Skip (not compiled in)\n");
430 				continue;
431 			}
432 			pr_info("\n");
433 
434 			for (subi = 0; subi < subn; subi++) {
435 				int len = strlen(t->subtest.get_desc(subi));
436 
437 				if (subw < len)
438 					subw = len;
439 			}
440 
441 			for (subi = 0; subi < subn; subi++) {
442 				pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
443 					t->subtest.get_desc(subi));
444 				err = test_and_print(t, skip, subi);
445 				if (err != TEST_OK && t->subtest.skip_if_fail)
446 					skip = true;
447 			}
448 		}
449 	}
450 
451 	return 0;
452 }
453 
454 static int perf_test__list(int argc, const char **argv)
455 {
456 	unsigned int j;
457 	struct test *t;
458 	int i = 0;
459 
460 	for_each_test(j, t) {
461 		if (argc > 1 && !strstr(t->desc, argv[1]))
462 			continue;
463 
464 		pr_info("%2d: %s\n", ++i, t->desc);
465 	}
466 
467 	return 0;
468 }
469 
470 int cmd_test(int argc, const char **argv)
471 {
472 	const char *test_usage[] = {
473 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
474 	NULL,
475 	};
476 	const char *skip = NULL;
477 	const struct option test_options[] = {
478 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
479 	OPT_INCR('v', "verbose", &verbose,
480 		    "be more verbose (show symbol address, etc)"),
481 	OPT_BOOLEAN('F', "dont-fork", &dont_fork,
482 		    "Do not fork for testcase"),
483 	OPT_END()
484 	};
485 	const char * const test_subcommands[] = { "list", NULL };
486 	struct intlist *skiplist = NULL;
487         int ret = hists__init();
488 
489         if (ret < 0)
490                 return ret;
491 
492 	argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
493 	if (argc >= 1 && !strcmp(argv[0], "list"))
494 		return perf_test__list(argc, argv);
495 
496 	symbol_conf.priv_size = sizeof(int);
497 	symbol_conf.sort_by_name = true;
498 	symbol_conf.try_vmlinux_path = true;
499 
500 	if (symbol__init(NULL) < 0)
501 		return -1;
502 
503 	if (skip != NULL)
504 		skiplist = intlist__new(skip);
505 
506 	return __cmd_test(argc, argv, skiplist);
507 }
508