xref: /openbmc/linux/tools/perf/tests/builtin-test.c (revision e8210cefb7e1ec0760a6fe581ad0727a2dcf8dd1)
1945aea22SJiri Olsa /*
2945aea22SJiri Olsa  * builtin-test.c
3945aea22SJiri Olsa  *
4945aea22SJiri Olsa  * Builtin regression testing command: ever growing number of sanity tests
5945aea22SJiri Olsa  */
60d8a5faaSJiri Olsa #include <unistd.h>
70d8a5faaSJiri Olsa #include <string.h>
8945aea22SJiri Olsa #include "builtin.h"
9a635fc51SArnaldo Carvalho de Melo #include "hist.h"
102ae82878SArnaldo Carvalho de Melo #include "intlist.h"
110a4e1ae6SJiri Olsa #include "tests.h"
12c81251e8SJiri Olsa #include "debug.h"
13c81251e8SJiri Olsa #include "color.h"
14c81251e8SJiri Olsa #include "parse-options.h"
15c81251e8SJiri Olsa #include "symbol.h"
16945aea22SJiri Olsa 
17945aea22SJiri Olsa static struct test {
18945aea22SJiri Olsa 	const char *desc;
19945aea22SJiri Olsa 	int (*func)(void);
20945aea22SJiri Olsa } tests[] = {
21945aea22SJiri Olsa 	{
22945aea22SJiri Olsa 		.desc = "vmlinux symtab matches kallsyms",
23945aea22SJiri Olsa 		.func = test__vmlinux_matches_kallsyms,
24945aea22SJiri Olsa 	},
25945aea22SJiri Olsa 	{
2643f322b4SRiku Voipio 		.desc = "detect openat syscall event",
2743f322b4SRiku Voipio 		.func = test__openat_syscall_event,
28945aea22SJiri Olsa 	},
29945aea22SJiri Olsa 	{
3043f322b4SRiku Voipio 		.desc = "detect openat syscall event on all cpus",
3143f322b4SRiku Voipio 		.func = test__openat_syscall_event_on_all_cpus,
32945aea22SJiri Olsa 	},
33945aea22SJiri Olsa 	{
34945aea22SJiri Olsa 		.desc = "read samples using the mmap interface",
35945aea22SJiri Olsa 		.func = test__basic_mmap,
36945aea22SJiri Olsa 	},
37945aea22SJiri Olsa 	{
38945aea22SJiri Olsa 		.desc = "parse events tests",
39c81251e8SJiri Olsa 		.func = test__parse_events,
40945aea22SJiri Olsa 	},
41945aea22SJiri Olsa #if defined(__x86_64__) || defined(__i386__)
42945aea22SJiri Olsa 	{
43945aea22SJiri Olsa 		.desc = "x86 rdpmc test",
44945aea22SJiri Olsa 		.func = test__rdpmc,
45945aea22SJiri Olsa 	},
46945aea22SJiri Olsa #endif
47945aea22SJiri Olsa 	{
48945aea22SJiri Olsa 		.desc = "Validate PERF_RECORD_* events & perf_sample fields",
49945aea22SJiri Olsa 		.func = test__PERF_RECORD,
50945aea22SJiri Olsa 	},
51945aea22SJiri Olsa 	{
52945aea22SJiri Olsa 		.desc = "Test perf pmu format parsing",
53cff7f956SJiri Olsa 		.func = test__pmu,
54945aea22SJiri Olsa 	},
55945aea22SJiri Olsa 	{
564ebbcb84SJiri Olsa 		.desc = "Test dso data read",
57c81251e8SJiri Olsa 		.func = test__dso_data,
58945aea22SJiri Olsa 	},
59945aea22SJiri Olsa 	{
604ebbcb84SJiri Olsa 		.desc = "Test dso data cache",
614ebbcb84SJiri Olsa 		.func = test__dso_data_cache,
624ebbcb84SJiri Olsa 	},
634ebbcb84SJiri Olsa 	{
6445dc1bb5SJiri Olsa 		.desc = "Test dso data reopen",
6545dc1bb5SJiri Olsa 		.func = test__dso_data_reopen,
6645dc1bb5SJiri Olsa 	},
6745dc1bb5SJiri Olsa 	{
68945aea22SJiri Olsa 		.desc = "roundtrip evsel->name check",
69cfffae2eSJiri Olsa 		.func = test__perf_evsel__roundtrip_name_test,
70945aea22SJiri Olsa 	},
71945aea22SJiri Olsa 	{
72945aea22SJiri Olsa 		.desc = "Check parsing of sched tracepoints fields",
735e24a090SJiri Olsa 		.func = test__perf_evsel__tp_sched_test,
74945aea22SJiri Olsa 	},
75945aea22SJiri Olsa 	{
7643f322b4SRiku Voipio 		.desc = "Generate and check syscalls:sys_enter_openat event fields",
7743f322b4SRiku Voipio 		.func = test__syscall_openat_tp_fields,
78945aea22SJiri Olsa 	},
79945aea22SJiri Olsa 	{
80d898b241SJiri Olsa 		.desc = "struct perf_event_attr setup",
81c81251e8SJiri Olsa 		.func = test__attr,
82d898b241SJiri Olsa 	},
83d898b241SJiri Olsa 	{
84ffcbaa14SArnaldo Carvalho de Melo 		.desc = "Test matching and linking multiple hists",
85f8ebb0cdSNamhyung Kim 		.func = test__hists_link,
86f8ebb0cdSNamhyung Kim 	},
87f8ebb0cdSNamhyung Kim 	{
88887e73d7SWANG Chao 		.desc = "Try 'import perf' in python, checking link problems",
8954359d33SArnaldo Carvalho de Melo 		.func = test__python_use,
9054359d33SArnaldo Carvalho de Melo 	},
9154359d33SArnaldo Carvalho de Melo 	{
925a6bef47SJiri Olsa 		.desc = "Test breakpoint overflow signal handler",
935a6bef47SJiri Olsa 		.func = test__bp_signal,
945a6bef47SJiri Olsa 	},
955a6bef47SJiri Olsa 	{
9606933e3aSJiri Olsa 		.desc = "Test breakpoint overflow sampling",
9706933e3aSJiri Olsa 		.func = test__bp_signal_overflow,
9806933e3aSJiri Olsa 	},
9906933e3aSJiri Olsa 	{
100d723a550SNamhyung Kim 		.desc = "Test number of exit event of a simple workload",
101d723a550SNamhyung Kim 		.func = test__task_exit,
102d723a550SNamhyung Kim 	},
103d723a550SNamhyung Kim 	{
104bc96b361SNamhyung Kim 		.desc = "Test software clock events have valid period values",
105bc96b361SNamhyung Kim 		.func = test__sw_clock_freq,
106bc96b361SNamhyung Kim 	},
1073bd5a5fcSAdrian Hunter #if defined(__x86_64__) || defined(__i386__)
1083bd5a5fcSAdrian Hunter 	{
1093bd5a5fcSAdrian Hunter 		.desc = "Test converting perf time to TSC",
1103bd5a5fcSAdrian Hunter 		.func = test__perf_time_to_tsc,
1113bd5a5fcSAdrian Hunter 	},
1123bd5a5fcSAdrian Hunter #endif
113bc96b361SNamhyung Kim 	{
114b55ae0a9SAdrian Hunter 		.desc = "Test object code reading",
115b55ae0a9SAdrian Hunter 		.func = test__code_reading,
116b55ae0a9SAdrian Hunter 	},
117b55ae0a9SAdrian Hunter 	{
118045f8cd8SAdrian Hunter 		.desc = "Test sample parsing",
119045f8cd8SAdrian Hunter 		.func = test__sample_parsing,
120045f8cd8SAdrian Hunter 	},
121045f8cd8SAdrian Hunter 	{
122395c3070SAdrian Hunter 		.desc = "Test using a dummy software event to keep tracking",
123395c3070SAdrian Hunter 		.func = test__keep_tracking,
124395c3070SAdrian Hunter 	},
125395c3070SAdrian Hunter 	{
12653a277e5SAdrian Hunter 		.desc = "Test parsing with no sample_id_all bit set",
12753a277e5SAdrian Hunter 		.func = test__parse_no_sample_id_all,
12853a277e5SAdrian Hunter 	},
1293237f281SWang Nan #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__)
1309ff125d1SJiri Olsa #ifdef HAVE_DWARF_UNWIND_SUPPORT
131aa16b81fSJiri Olsa 	{
132aa16b81fSJiri Olsa 		.desc = "Test dwarf unwind",
133aa16b81fSJiri Olsa 		.func = test__dwarf_unwind,
134aa16b81fSJiri Olsa 	},
135aa16b81fSJiri Olsa #endif
136aa16b81fSJiri Olsa #endif
13753a277e5SAdrian Hunter 	{
1383c3cfd99SNamhyung Kim 		.desc = "Test filtering hist entries",
1393c3cfd99SNamhyung Kim 		.func = test__hists_filter,
1403c3cfd99SNamhyung Kim 	},
1413c3cfd99SNamhyung Kim 	{
1424e85edfcSJiri Olsa 		.desc = "Test mmap thread lookup",
1434e85edfcSJiri Olsa 		.func = test__mmap_thread_lookup,
1444e85edfcSJiri Olsa 	},
1454e85edfcSJiri Olsa 	{
146fabf0123SJiri Olsa 		.desc = "Test thread mg sharing",
147fabf0123SJiri Olsa 		.func = test__thread_mg_share,
148fabf0123SJiri Olsa 	},
149fabf0123SJiri Olsa 	{
150f21d1815SNamhyung Kim 		.desc = "Test output sorting of hist entries",
151f21d1815SNamhyung Kim 		.func = test__hists_output,
152f21d1815SNamhyung Kim 	},
153f21d1815SNamhyung Kim 	{
1540506aeccSNamhyung Kim 		.desc = "Test cumulation of child hist entries",
1550506aeccSNamhyung Kim 		.func = test__hists_cumulate,
1560506aeccSNamhyung Kim 	},
1570506aeccSNamhyung Kim 	{
158d44bc558SAdrian Hunter 		.desc = "Test tracking with sched_switch",
159d44bc558SAdrian Hunter 		.func = test__switch_tracking,
160d44bc558SAdrian Hunter 	},
161d44bc558SAdrian Hunter 	{
1621b85337dSArnaldo Carvalho de Melo 		.desc = "Filter fds with revents mask in a fdarray",
1631b85337dSArnaldo Carvalho de Melo 		.func = test__fdarray__filter,
16454dbfae3SArnaldo Carvalho de Melo 	},
16554dbfae3SArnaldo Carvalho de Melo 	{
1661b85337dSArnaldo Carvalho de Melo 		.desc = "Add fd to a fdarray, making it autogrow",
1671b85337dSArnaldo Carvalho de Melo 		.func = test__fdarray__add,
1689ae28035SArnaldo Carvalho de Melo 	},
1699ae28035SArnaldo Carvalho de Melo 	{
1703c8a67f5SJiri Olsa 		.desc = "Test kmod_path__parse function",
1713c8a67f5SJiri Olsa 		.func = test__kmod_path__parse,
1723c8a67f5SJiri Olsa 	},
1733c8a67f5SJiri Olsa 	{
174134aa44fSJiri Olsa 		.desc = "Test thread map",
175134aa44fSJiri Olsa 		.func = test__thread_map,
176134aa44fSJiri Olsa 	},
177134aa44fSJiri Olsa 	{
1789bc898c7SWang Nan 		.desc = "Test LLVM searching and compiling",
1799bc898c7SWang Nan 		.func = test__llvm,
1809bc898c7SWang Nan 	},
18198e4619fSAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT
18298e4619fSAdrian Hunter #if defined(__x86_64__) || defined(__i386__)
18398e4619fSAdrian Hunter 	{
18498e4619fSAdrian Hunter 		.desc = "Test x86 instruction decoder - new instructions",
18598e4619fSAdrian Hunter 		.func = test__insn_x86,
18698e4619fSAdrian Hunter 	},
18798e4619fSAdrian Hunter #endif
18898e4619fSAdrian Hunter #endif
1899bc898c7SWang Nan 	{
190c84974edSKan Liang 		.desc = "Test topology in session",
191c84974edSKan Liang 		.func = test_session_topology,
192c84974edSKan Liang 	},
193c84974edSKan Liang 	{
194945aea22SJiri Olsa 		.func = NULL,
195945aea22SJiri Olsa 	},
196945aea22SJiri Olsa };
197945aea22SJiri Olsa 
198*e8210cefSMatt Fleming static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
199945aea22SJiri Olsa {
200945aea22SJiri Olsa 	int i;
201945aea22SJiri Olsa 
202945aea22SJiri Olsa 	if (argc == 0)
203945aea22SJiri Olsa 		return true;
204945aea22SJiri Olsa 
205945aea22SJiri Olsa 	for (i = 0; i < argc; ++i) {
206945aea22SJiri Olsa 		char *end;
207945aea22SJiri Olsa 		long nr = strtoul(argv[i], &end, 10);
208945aea22SJiri Olsa 
209945aea22SJiri Olsa 		if (*end == '\0') {
210945aea22SJiri Olsa 			if (nr == curr + 1)
211945aea22SJiri Olsa 				return true;
212945aea22SJiri Olsa 			continue;
213945aea22SJiri Olsa 		}
214945aea22SJiri Olsa 
215*e8210cefSMatt Fleming 		if (strstr(test->desc, argv[i]))
216945aea22SJiri Olsa 			return true;
217945aea22SJiri Olsa 	}
218945aea22SJiri Olsa 
219945aea22SJiri Olsa 	return false;
220945aea22SJiri Olsa }
221945aea22SJiri Olsa 
2220d8a5faaSJiri Olsa static int run_test(struct test *test)
2230d8a5faaSJiri Olsa {
2240d8a5faaSJiri Olsa 	int status, err = -1, child = fork();
225ba3dfff8SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
2260d8a5faaSJiri Olsa 
2270d8a5faaSJiri Olsa 	if (child < 0) {
228ba3dfff8SMasami Hiramatsu 		pr_err("failed to fork test: %s\n",
229ba3dfff8SMasami Hiramatsu 			strerror_r(errno, sbuf, sizeof(sbuf)));
2300d8a5faaSJiri Olsa 		return -1;
2310d8a5faaSJiri Olsa 	}
2320d8a5faaSJiri Olsa 
2330d8a5faaSJiri Olsa 	if (!child) {
2340d8a5faaSJiri Olsa 		pr_debug("test child forked, pid %d\n", getpid());
2350d8a5faaSJiri Olsa 		err = test->func();
2360d8a5faaSJiri Olsa 		exit(err);
2370d8a5faaSJiri Olsa 	}
2380d8a5faaSJiri Olsa 
2390d8a5faaSJiri Olsa 	wait(&status);
2400d8a5faaSJiri Olsa 
2410d8a5faaSJiri Olsa 	if (WIFEXITED(status)) {
242189c466fSHe Kuang 		err = (signed char)WEXITSTATUS(status);
2430d8a5faaSJiri Olsa 		pr_debug("test child finished with %d\n", err);
2440d8a5faaSJiri Olsa 	} else if (WIFSIGNALED(status)) {
2450d8a5faaSJiri Olsa 		err = -1;
2460d8a5faaSJiri Olsa 		pr_debug("test child interrupted\n");
2470d8a5faaSJiri Olsa 	}
2480d8a5faaSJiri Olsa 
2490d8a5faaSJiri Olsa 	return err;
2500d8a5faaSJiri Olsa }
2510d8a5faaSJiri Olsa 
252*e8210cefSMatt Fleming #define for_each_test(t)	 for (t = &tests[0]; t->func; t++)
253*e8210cefSMatt Fleming 
2542ae82878SArnaldo Carvalho de Melo static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
255945aea22SJiri Olsa {
256*e8210cefSMatt Fleming 	struct test *t;
257945aea22SJiri Olsa 	int i = 0;
258945aea22SJiri Olsa 	int width = 0;
259945aea22SJiri Olsa 
260*e8210cefSMatt Fleming 	for_each_test(t) {
261*e8210cefSMatt Fleming 		int len = strlen(t->desc);
262945aea22SJiri Olsa 
263945aea22SJiri Olsa 		if (width < len)
264945aea22SJiri Olsa 			width = len;
265945aea22SJiri Olsa 	}
266945aea22SJiri Olsa 
267*e8210cefSMatt Fleming 	for_each_test(t) {
268945aea22SJiri Olsa 		int curr = i++, err;
269945aea22SJiri Olsa 
270*e8210cefSMatt Fleming 		if (!perf_test__matches(t, curr, argc, argv))
271945aea22SJiri Olsa 			continue;
272945aea22SJiri Olsa 
273*e8210cefSMatt Fleming 		pr_info("%2d: %-*s:", i, width, t->desc);
2742ae82878SArnaldo Carvalho de Melo 
2752ae82878SArnaldo Carvalho de Melo 		if (intlist__find(skiplist, i)) {
2762ae82878SArnaldo Carvalho de Melo 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
2772ae82878SArnaldo Carvalho de Melo 			continue;
2782ae82878SArnaldo Carvalho de Melo 		}
2792ae82878SArnaldo Carvalho de Melo 
280945aea22SJiri Olsa 		pr_debug("\n--- start ---\n");
281*e8210cefSMatt Fleming 		err = run_test(t);
282*e8210cefSMatt Fleming 		pr_debug("---- end ----\n%s:", t->desc);
283f4c1ea5fSJiri Olsa 
284f4c1ea5fSJiri Olsa 		switch (err) {
285f4c1ea5fSJiri Olsa 		case TEST_OK:
286945aea22SJiri Olsa 			pr_info(" Ok\n");
287f4c1ea5fSJiri Olsa 			break;
288f4c1ea5fSJiri Olsa 		case TEST_SKIP:
289f4c1ea5fSJiri Olsa 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
290f4c1ea5fSJiri Olsa 			break;
291f4c1ea5fSJiri Olsa 		case TEST_FAIL:
292f4c1ea5fSJiri Olsa 		default:
293f4c1ea5fSJiri Olsa 			color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
294f4c1ea5fSJiri Olsa 			break;
295f4c1ea5fSJiri Olsa 		}
296945aea22SJiri Olsa 	}
297945aea22SJiri Olsa 
298945aea22SJiri Olsa 	return 0;
299945aea22SJiri Olsa }
300945aea22SJiri Olsa 
301945aea22SJiri Olsa static int perf_test__list(int argc, const char **argv)
302945aea22SJiri Olsa {
303*e8210cefSMatt Fleming 	struct test *t;
304945aea22SJiri Olsa 	int i = 0;
305945aea22SJiri Olsa 
306*e8210cefSMatt Fleming 	for_each_test(t) {
307*e8210cefSMatt Fleming 		if (argc > 1 && !strstr(t->desc, argv[1]))
308945aea22SJiri Olsa 			continue;
309945aea22SJiri Olsa 
310*e8210cefSMatt Fleming 		pr_info("%2d: %s\n", ++i, t->desc);
311945aea22SJiri Olsa 	}
312945aea22SJiri Olsa 
313945aea22SJiri Olsa 	return 0;
314945aea22SJiri Olsa }
315945aea22SJiri Olsa 
316945aea22SJiri Olsa int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
317945aea22SJiri Olsa {
3181f9975f1SYunlong Song 	const char *test_usage[] = {
319945aea22SJiri Olsa 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
320945aea22SJiri Olsa 	NULL,
321945aea22SJiri Olsa 	};
3222ae82878SArnaldo Carvalho de Melo 	const char *skip = NULL;
323945aea22SJiri Olsa 	const struct option test_options[] = {
3242ae82878SArnaldo Carvalho de Melo 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
325945aea22SJiri Olsa 	OPT_INCR('v', "verbose", &verbose,
326945aea22SJiri Olsa 		    "be more verbose (show symbol address, etc)"),
327945aea22SJiri Olsa 	OPT_END()
328945aea22SJiri Olsa 	};
3291f9975f1SYunlong Song 	const char * const test_subcommands[] = { "list", NULL };
3302ae82878SArnaldo Carvalho de Melo 	struct intlist *skiplist = NULL;
331a635fc51SArnaldo Carvalho de Melo         int ret = hists__init();
332a635fc51SArnaldo Carvalho de Melo 
333a635fc51SArnaldo Carvalho de Melo         if (ret < 0)
334a635fc51SArnaldo Carvalho de Melo                 return ret;
335945aea22SJiri Olsa 
3361f9975f1SYunlong Song 	argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
337945aea22SJiri Olsa 	if (argc >= 1 && !strcmp(argv[0], "list"))
338945aea22SJiri Olsa 		return perf_test__list(argc, argv);
339945aea22SJiri Olsa 
340945aea22SJiri Olsa 	symbol_conf.priv_size = sizeof(int);
341945aea22SJiri Olsa 	symbol_conf.sort_by_name = true;
342945aea22SJiri Olsa 	symbol_conf.try_vmlinux_path = true;
343945aea22SJiri Olsa 
3440a7e6d1bSNamhyung Kim 	if (symbol__init(NULL) < 0)
345945aea22SJiri Olsa 		return -1;
346945aea22SJiri Olsa 
3472ae82878SArnaldo Carvalho de Melo 	if (skip != NULL)
3482ae82878SArnaldo Carvalho de Melo 		skiplist = intlist__new(skip);
3492ae82878SArnaldo Carvalho de Melo 
3502ae82878SArnaldo Carvalho de Melo 	return __cmd_test(argc, argv, skiplist);
351945aea22SJiri Olsa }
352