xref: /openbmc/linux/tools/perf/tests/builtin-test.c (revision a635fc51)
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 	{
26945aea22SJiri Olsa 		.desc = "detect open syscall event",
27945aea22SJiri Olsa 		.func = test__open_syscall_event,
28945aea22SJiri Olsa 	},
29945aea22SJiri Olsa 	{
30945aea22SJiri Olsa 		.desc = "detect open syscall event on all cpus",
31945aea22SJiri Olsa 		.func = test__open_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 	{
76945aea22SJiri Olsa 		.desc = "Generate and check syscalls:sys_enter_open event fields",
77945aea22SJiri Olsa 		.func = test__syscall_open_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 	{
8854359d33SArnaldo Carvalho de Melo 		.desc = "Try 'use 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 	},
12990fa9debSJean Pihet #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
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 	{
170945aea22SJiri Olsa 		.func = NULL,
171945aea22SJiri Olsa 	},
172945aea22SJiri Olsa };
173945aea22SJiri Olsa 
174945aea22SJiri Olsa static bool perf_test__matches(int curr, int argc, const char *argv[])
175945aea22SJiri Olsa {
176945aea22SJiri Olsa 	int i;
177945aea22SJiri Olsa 
178945aea22SJiri Olsa 	if (argc == 0)
179945aea22SJiri Olsa 		return true;
180945aea22SJiri Olsa 
181945aea22SJiri Olsa 	for (i = 0; i < argc; ++i) {
182945aea22SJiri Olsa 		char *end;
183945aea22SJiri Olsa 		long nr = strtoul(argv[i], &end, 10);
184945aea22SJiri Olsa 
185945aea22SJiri Olsa 		if (*end == '\0') {
186945aea22SJiri Olsa 			if (nr == curr + 1)
187945aea22SJiri Olsa 				return true;
188945aea22SJiri Olsa 			continue;
189945aea22SJiri Olsa 		}
190945aea22SJiri Olsa 
191945aea22SJiri Olsa 		if (strstr(tests[curr].desc, argv[i]))
192945aea22SJiri Olsa 			return true;
193945aea22SJiri Olsa 	}
194945aea22SJiri Olsa 
195945aea22SJiri Olsa 	return false;
196945aea22SJiri Olsa }
197945aea22SJiri Olsa 
1980d8a5faaSJiri Olsa static int run_test(struct test *test)
1990d8a5faaSJiri Olsa {
2000d8a5faaSJiri Olsa 	int status, err = -1, child = fork();
201ba3dfff8SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
2020d8a5faaSJiri Olsa 
2030d8a5faaSJiri Olsa 	if (child < 0) {
204ba3dfff8SMasami Hiramatsu 		pr_err("failed to fork test: %s\n",
205ba3dfff8SMasami Hiramatsu 			strerror_r(errno, sbuf, sizeof(sbuf)));
2060d8a5faaSJiri Olsa 		return -1;
2070d8a5faaSJiri Olsa 	}
2080d8a5faaSJiri Olsa 
2090d8a5faaSJiri Olsa 	if (!child) {
2100d8a5faaSJiri Olsa 		pr_debug("test child forked, pid %d\n", getpid());
2110d8a5faaSJiri Olsa 		err = test->func();
2120d8a5faaSJiri Olsa 		exit(err);
2130d8a5faaSJiri Olsa 	}
2140d8a5faaSJiri Olsa 
2150d8a5faaSJiri Olsa 	wait(&status);
2160d8a5faaSJiri Olsa 
2170d8a5faaSJiri Olsa 	if (WIFEXITED(status)) {
2180d8a5faaSJiri Olsa 		err = WEXITSTATUS(status);
2190d8a5faaSJiri Olsa 		pr_debug("test child finished with %d\n", err);
2200d8a5faaSJiri Olsa 	} else if (WIFSIGNALED(status)) {
2210d8a5faaSJiri Olsa 		err = -1;
2220d8a5faaSJiri Olsa 		pr_debug("test child interrupted\n");
2230d8a5faaSJiri Olsa 	}
2240d8a5faaSJiri Olsa 
2250d8a5faaSJiri Olsa 	return err;
2260d8a5faaSJiri Olsa }
2270d8a5faaSJiri Olsa 
2282ae82878SArnaldo Carvalho de Melo static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
229945aea22SJiri Olsa {
230945aea22SJiri Olsa 	int i = 0;
231945aea22SJiri Olsa 	int width = 0;
232945aea22SJiri Olsa 
233945aea22SJiri Olsa 	while (tests[i].func) {
234945aea22SJiri Olsa 		int len = strlen(tests[i].desc);
235945aea22SJiri Olsa 
236945aea22SJiri Olsa 		if (width < len)
237945aea22SJiri Olsa 			width = len;
238945aea22SJiri Olsa 		++i;
239945aea22SJiri Olsa 	}
240945aea22SJiri Olsa 
241945aea22SJiri Olsa 	i = 0;
242945aea22SJiri Olsa 	while (tests[i].func) {
243945aea22SJiri Olsa 		int curr = i++, err;
244945aea22SJiri Olsa 
245945aea22SJiri Olsa 		if (!perf_test__matches(curr, argc, argv))
246945aea22SJiri Olsa 			continue;
247945aea22SJiri Olsa 
248945aea22SJiri Olsa 		pr_info("%2d: %-*s:", i, width, tests[curr].desc);
2492ae82878SArnaldo Carvalho de Melo 
2502ae82878SArnaldo Carvalho de Melo 		if (intlist__find(skiplist, i)) {
2512ae82878SArnaldo Carvalho de Melo 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
2522ae82878SArnaldo Carvalho de Melo 			continue;
2532ae82878SArnaldo Carvalho de Melo 		}
2542ae82878SArnaldo Carvalho de Melo 
255945aea22SJiri Olsa 		pr_debug("\n--- start ---\n");
2560d8a5faaSJiri Olsa 		err = run_test(&tests[curr]);
257945aea22SJiri Olsa 		pr_debug("---- end ----\n%s:", tests[curr].desc);
258f4c1ea5fSJiri Olsa 
259f4c1ea5fSJiri Olsa 		switch (err) {
260f4c1ea5fSJiri Olsa 		case TEST_OK:
261945aea22SJiri Olsa 			pr_info(" Ok\n");
262f4c1ea5fSJiri Olsa 			break;
263f4c1ea5fSJiri Olsa 		case TEST_SKIP:
264f4c1ea5fSJiri Olsa 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
265f4c1ea5fSJiri Olsa 			break;
266f4c1ea5fSJiri Olsa 		case TEST_FAIL:
267f4c1ea5fSJiri Olsa 		default:
268f4c1ea5fSJiri Olsa 			color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
269f4c1ea5fSJiri Olsa 			break;
270f4c1ea5fSJiri Olsa 		}
271945aea22SJiri Olsa 	}
272945aea22SJiri Olsa 
273945aea22SJiri Olsa 	return 0;
274945aea22SJiri Olsa }
275945aea22SJiri Olsa 
276945aea22SJiri Olsa static int perf_test__list(int argc, const char **argv)
277945aea22SJiri Olsa {
278945aea22SJiri Olsa 	int i = 0;
279945aea22SJiri Olsa 
280945aea22SJiri Olsa 	while (tests[i].func) {
281945aea22SJiri Olsa 		int curr = i++;
282945aea22SJiri Olsa 
283945aea22SJiri Olsa 		if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
284945aea22SJiri Olsa 			continue;
285945aea22SJiri Olsa 
286945aea22SJiri Olsa 		pr_info("%2d: %s\n", i, tests[curr].desc);
287945aea22SJiri Olsa 	}
288945aea22SJiri Olsa 
289945aea22SJiri Olsa 	return 0;
290945aea22SJiri Olsa }
291945aea22SJiri Olsa 
292945aea22SJiri Olsa int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
293945aea22SJiri Olsa {
294945aea22SJiri Olsa 	const char * const test_usage[] = {
295945aea22SJiri Olsa 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
296945aea22SJiri Olsa 	NULL,
297945aea22SJiri Olsa 	};
2982ae82878SArnaldo Carvalho de Melo 	const char *skip = NULL;
299945aea22SJiri Olsa 	const struct option test_options[] = {
3002ae82878SArnaldo Carvalho de Melo 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
301945aea22SJiri Olsa 	OPT_INCR('v', "verbose", &verbose,
302945aea22SJiri Olsa 		    "be more verbose (show symbol address, etc)"),
303945aea22SJiri Olsa 	OPT_END()
304945aea22SJiri Olsa 	};
3052ae82878SArnaldo Carvalho de Melo 	struct intlist *skiplist = NULL;
306a635fc51SArnaldo Carvalho de Melo         int ret = hists__init();
307a635fc51SArnaldo Carvalho de Melo 
308a635fc51SArnaldo Carvalho de Melo         if (ret < 0)
309a635fc51SArnaldo Carvalho de Melo                 return ret;
310945aea22SJiri Olsa 
311945aea22SJiri Olsa 	argc = parse_options(argc, argv, test_options, test_usage, 0);
312945aea22SJiri Olsa 	if (argc >= 1 && !strcmp(argv[0], "list"))
313945aea22SJiri Olsa 		return perf_test__list(argc, argv);
314945aea22SJiri Olsa 
315945aea22SJiri Olsa 	symbol_conf.priv_size = sizeof(int);
316945aea22SJiri Olsa 	symbol_conf.sort_by_name = true;
317945aea22SJiri Olsa 	symbol_conf.try_vmlinux_path = true;
318945aea22SJiri Olsa 
3190a7e6d1bSNamhyung Kim 	if (symbol__init(NULL) < 0)
320945aea22SJiri Olsa 		return -1;
321945aea22SJiri Olsa 
3222ae82878SArnaldo Carvalho de Melo 	if (skip != NULL)
3232ae82878SArnaldo Carvalho de Melo 		skiplist = intlist__new(skip);
3242ae82878SArnaldo Carvalho de Melo 
3252ae82878SArnaldo Carvalho de Melo 	return __cmd_test(argc, argv, skiplist);
326945aea22SJiri Olsa }
327