xref: /openbmc/linux/tools/perf/tests/builtin-test.c (revision 4ebbcb84b19b8472fb5b9c8be89b3d0ea17c902e)
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"
92ae82878SArnaldo Carvalho de Melo #include "intlist.h"
100a4e1ae6SJiri Olsa #include "tests.h"
11c81251e8SJiri Olsa #include "debug.h"
12c81251e8SJiri Olsa #include "color.h"
13c81251e8SJiri Olsa #include "parse-options.h"
14c81251e8SJiri Olsa #include "symbol.h"
15945aea22SJiri Olsa 
16945aea22SJiri Olsa static struct test {
17945aea22SJiri Olsa 	const char *desc;
18945aea22SJiri Olsa 	int (*func)(void);
19945aea22SJiri Olsa } tests[] = {
20945aea22SJiri Olsa 	{
21945aea22SJiri Olsa 		.desc = "vmlinux symtab matches kallsyms",
22945aea22SJiri Olsa 		.func = test__vmlinux_matches_kallsyms,
23945aea22SJiri Olsa 	},
24945aea22SJiri Olsa 	{
25945aea22SJiri Olsa 		.desc = "detect open syscall event",
26945aea22SJiri Olsa 		.func = test__open_syscall_event,
27945aea22SJiri Olsa 	},
28945aea22SJiri Olsa 	{
29945aea22SJiri Olsa 		.desc = "detect open syscall event on all cpus",
30945aea22SJiri Olsa 		.func = test__open_syscall_event_on_all_cpus,
31945aea22SJiri Olsa 	},
32945aea22SJiri Olsa 	{
33945aea22SJiri Olsa 		.desc = "read samples using the mmap interface",
34945aea22SJiri Olsa 		.func = test__basic_mmap,
35945aea22SJiri Olsa 	},
36945aea22SJiri Olsa 	{
37945aea22SJiri Olsa 		.desc = "parse events tests",
38c81251e8SJiri Olsa 		.func = test__parse_events,
39945aea22SJiri Olsa 	},
40945aea22SJiri Olsa #if defined(__x86_64__) || defined(__i386__)
41945aea22SJiri Olsa 	{
42945aea22SJiri Olsa 		.desc = "x86 rdpmc test",
43945aea22SJiri Olsa 		.func = test__rdpmc,
44945aea22SJiri Olsa 	},
45945aea22SJiri Olsa #endif
46945aea22SJiri Olsa 	{
47945aea22SJiri Olsa 		.desc = "Validate PERF_RECORD_* events & perf_sample fields",
48945aea22SJiri Olsa 		.func = test__PERF_RECORD,
49945aea22SJiri Olsa 	},
50945aea22SJiri Olsa 	{
51945aea22SJiri Olsa 		.desc = "Test perf pmu format parsing",
52cff7f956SJiri Olsa 		.func = test__pmu,
53945aea22SJiri Olsa 	},
54945aea22SJiri Olsa 	{
55*4ebbcb84SJiri Olsa 		.desc = "Test dso data read",
56c81251e8SJiri Olsa 		.func = test__dso_data,
57945aea22SJiri Olsa 	},
58945aea22SJiri Olsa 	{
59*4ebbcb84SJiri Olsa 		.desc = "Test dso data cache",
60*4ebbcb84SJiri Olsa 		.func = test__dso_data_cache,
61*4ebbcb84SJiri Olsa 	},
62*4ebbcb84SJiri Olsa 	{
63945aea22SJiri Olsa 		.desc = "roundtrip evsel->name check",
64cfffae2eSJiri Olsa 		.func = test__perf_evsel__roundtrip_name_test,
65945aea22SJiri Olsa 	},
66945aea22SJiri Olsa 	{
67945aea22SJiri Olsa 		.desc = "Check parsing of sched tracepoints fields",
685e24a090SJiri Olsa 		.func = test__perf_evsel__tp_sched_test,
69945aea22SJiri Olsa 	},
70945aea22SJiri Olsa 	{
71945aea22SJiri Olsa 		.desc = "Generate and check syscalls:sys_enter_open event fields",
72945aea22SJiri Olsa 		.func = test__syscall_open_tp_fields,
73945aea22SJiri Olsa 	},
74945aea22SJiri Olsa 	{
75d898b241SJiri Olsa 		.desc = "struct perf_event_attr setup",
76c81251e8SJiri Olsa 		.func = test__attr,
77d898b241SJiri Olsa 	},
78d898b241SJiri Olsa 	{
79ffcbaa14SArnaldo Carvalho de Melo 		.desc = "Test matching and linking multiple hists",
80f8ebb0cdSNamhyung Kim 		.func = test__hists_link,
81f8ebb0cdSNamhyung Kim 	},
82f8ebb0cdSNamhyung Kim 	{
8354359d33SArnaldo Carvalho de Melo 		.desc = "Try 'use perf' in python, checking link problems",
8454359d33SArnaldo Carvalho de Melo 		.func = test__python_use,
8554359d33SArnaldo Carvalho de Melo 	},
8654359d33SArnaldo Carvalho de Melo 	{
875a6bef47SJiri Olsa 		.desc = "Test breakpoint overflow signal handler",
885a6bef47SJiri Olsa 		.func = test__bp_signal,
895a6bef47SJiri Olsa 	},
905a6bef47SJiri Olsa 	{
9106933e3aSJiri Olsa 		.desc = "Test breakpoint overflow sampling",
9206933e3aSJiri Olsa 		.func = test__bp_signal_overflow,
9306933e3aSJiri Olsa 	},
9406933e3aSJiri Olsa 	{
95d723a550SNamhyung Kim 		.desc = "Test number of exit event of a simple workload",
96d723a550SNamhyung Kim 		.func = test__task_exit,
97d723a550SNamhyung Kim 	},
98d723a550SNamhyung Kim 	{
99bc96b361SNamhyung Kim 		.desc = "Test software clock events have valid period values",
100bc96b361SNamhyung Kim 		.func = test__sw_clock_freq,
101bc96b361SNamhyung Kim 	},
1023bd5a5fcSAdrian Hunter #if defined(__x86_64__) || defined(__i386__)
1033bd5a5fcSAdrian Hunter 	{
1043bd5a5fcSAdrian Hunter 		.desc = "Test converting perf time to TSC",
1053bd5a5fcSAdrian Hunter 		.func = test__perf_time_to_tsc,
1063bd5a5fcSAdrian Hunter 	},
1073bd5a5fcSAdrian Hunter #endif
108bc96b361SNamhyung Kim 	{
109b55ae0a9SAdrian Hunter 		.desc = "Test object code reading",
110b55ae0a9SAdrian Hunter 		.func = test__code_reading,
111b55ae0a9SAdrian Hunter 	},
112b55ae0a9SAdrian Hunter 	{
113045f8cd8SAdrian Hunter 		.desc = "Test sample parsing",
114045f8cd8SAdrian Hunter 		.func = test__sample_parsing,
115045f8cd8SAdrian Hunter 	},
116045f8cd8SAdrian Hunter 	{
117395c3070SAdrian Hunter 		.desc = "Test using a dummy software event to keep tracking",
118395c3070SAdrian Hunter 		.func = test__keep_tracking,
119395c3070SAdrian Hunter 	},
120395c3070SAdrian Hunter 	{
12153a277e5SAdrian Hunter 		.desc = "Test parsing with no sample_id_all bit set",
12253a277e5SAdrian Hunter 		.func = test__parse_no_sample_id_all,
12353a277e5SAdrian Hunter 	},
12490fa9debSJean Pihet #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
1259ff125d1SJiri Olsa #ifdef HAVE_DWARF_UNWIND_SUPPORT
126aa16b81fSJiri Olsa 	{
127aa16b81fSJiri Olsa 		.desc = "Test dwarf unwind",
128aa16b81fSJiri Olsa 		.func = test__dwarf_unwind,
129aa16b81fSJiri Olsa 	},
130aa16b81fSJiri Olsa #endif
131aa16b81fSJiri Olsa #endif
13253a277e5SAdrian Hunter 	{
1333c3cfd99SNamhyung Kim 		.desc = "Test filtering hist entries",
1343c3cfd99SNamhyung Kim 		.func = test__hists_filter,
1353c3cfd99SNamhyung Kim 	},
1363c3cfd99SNamhyung Kim 	{
1374e85edfcSJiri Olsa 		.desc = "Test mmap thread lookup",
1384e85edfcSJiri Olsa 		.func = test__mmap_thread_lookup,
1394e85edfcSJiri Olsa 	},
1404e85edfcSJiri Olsa 	{
141fabf0123SJiri Olsa 		.desc = "Test thread mg sharing",
142fabf0123SJiri Olsa 		.func = test__thread_mg_share,
143fabf0123SJiri Olsa 	},
144fabf0123SJiri Olsa 	{
145f21d1815SNamhyung Kim 		.desc = "Test output sorting of hist entries",
146f21d1815SNamhyung Kim 		.func = test__hists_output,
147f21d1815SNamhyung Kim 	},
148f21d1815SNamhyung Kim 	{
1490506aeccSNamhyung Kim 		.desc = "Test cumulation of child hist entries",
1500506aeccSNamhyung Kim 		.func = test__hists_cumulate,
1510506aeccSNamhyung Kim 	},
1520506aeccSNamhyung Kim 	{
153945aea22SJiri Olsa 		.func = NULL,
154945aea22SJiri Olsa 	},
155945aea22SJiri Olsa };
156945aea22SJiri Olsa 
157945aea22SJiri Olsa static bool perf_test__matches(int curr, int argc, const char *argv[])
158945aea22SJiri Olsa {
159945aea22SJiri Olsa 	int i;
160945aea22SJiri Olsa 
161945aea22SJiri Olsa 	if (argc == 0)
162945aea22SJiri Olsa 		return true;
163945aea22SJiri Olsa 
164945aea22SJiri Olsa 	for (i = 0; i < argc; ++i) {
165945aea22SJiri Olsa 		char *end;
166945aea22SJiri Olsa 		long nr = strtoul(argv[i], &end, 10);
167945aea22SJiri Olsa 
168945aea22SJiri Olsa 		if (*end == '\0') {
169945aea22SJiri Olsa 			if (nr == curr + 1)
170945aea22SJiri Olsa 				return true;
171945aea22SJiri Olsa 			continue;
172945aea22SJiri Olsa 		}
173945aea22SJiri Olsa 
174945aea22SJiri Olsa 		if (strstr(tests[curr].desc, argv[i]))
175945aea22SJiri Olsa 			return true;
176945aea22SJiri Olsa 	}
177945aea22SJiri Olsa 
178945aea22SJiri Olsa 	return false;
179945aea22SJiri Olsa }
180945aea22SJiri Olsa 
1810d8a5faaSJiri Olsa static int run_test(struct test *test)
1820d8a5faaSJiri Olsa {
1830d8a5faaSJiri Olsa 	int status, err = -1, child = fork();
1840d8a5faaSJiri Olsa 
1850d8a5faaSJiri Olsa 	if (child < 0) {
1860d8a5faaSJiri Olsa 		pr_err("failed to fork test: %s\n", strerror(errno));
1870d8a5faaSJiri Olsa 		return -1;
1880d8a5faaSJiri Olsa 	}
1890d8a5faaSJiri Olsa 
1900d8a5faaSJiri Olsa 	if (!child) {
1910d8a5faaSJiri Olsa 		pr_debug("test child forked, pid %d\n", getpid());
1920d8a5faaSJiri Olsa 		err = test->func();
1930d8a5faaSJiri Olsa 		exit(err);
1940d8a5faaSJiri Olsa 	}
1950d8a5faaSJiri Olsa 
1960d8a5faaSJiri Olsa 	wait(&status);
1970d8a5faaSJiri Olsa 
1980d8a5faaSJiri Olsa 	if (WIFEXITED(status)) {
1990d8a5faaSJiri Olsa 		err = WEXITSTATUS(status);
2000d8a5faaSJiri Olsa 		pr_debug("test child finished with %d\n", err);
2010d8a5faaSJiri Olsa 	} else if (WIFSIGNALED(status)) {
2020d8a5faaSJiri Olsa 		err = -1;
2030d8a5faaSJiri Olsa 		pr_debug("test child interrupted\n");
2040d8a5faaSJiri Olsa 	}
2050d8a5faaSJiri Olsa 
2060d8a5faaSJiri Olsa 	return err;
2070d8a5faaSJiri Olsa }
2080d8a5faaSJiri Olsa 
2092ae82878SArnaldo Carvalho de Melo static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
210945aea22SJiri Olsa {
211945aea22SJiri Olsa 	int i = 0;
212945aea22SJiri Olsa 	int width = 0;
213945aea22SJiri Olsa 
214945aea22SJiri Olsa 	while (tests[i].func) {
215945aea22SJiri Olsa 		int len = strlen(tests[i].desc);
216945aea22SJiri Olsa 
217945aea22SJiri Olsa 		if (width < len)
218945aea22SJiri Olsa 			width = len;
219945aea22SJiri Olsa 		++i;
220945aea22SJiri Olsa 	}
221945aea22SJiri Olsa 
222945aea22SJiri Olsa 	i = 0;
223945aea22SJiri Olsa 	while (tests[i].func) {
224945aea22SJiri Olsa 		int curr = i++, err;
225945aea22SJiri Olsa 
226945aea22SJiri Olsa 		if (!perf_test__matches(curr, argc, argv))
227945aea22SJiri Olsa 			continue;
228945aea22SJiri Olsa 
229945aea22SJiri Olsa 		pr_info("%2d: %-*s:", i, width, tests[curr].desc);
2302ae82878SArnaldo Carvalho de Melo 
2312ae82878SArnaldo Carvalho de Melo 		if (intlist__find(skiplist, i)) {
2322ae82878SArnaldo Carvalho de Melo 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
2332ae82878SArnaldo Carvalho de Melo 			continue;
2342ae82878SArnaldo Carvalho de Melo 		}
2352ae82878SArnaldo Carvalho de Melo 
236945aea22SJiri Olsa 		pr_debug("\n--- start ---\n");
2370d8a5faaSJiri Olsa 		err = run_test(&tests[curr]);
238945aea22SJiri Olsa 		pr_debug("---- end ----\n%s:", tests[curr].desc);
239f4c1ea5fSJiri Olsa 
240f4c1ea5fSJiri Olsa 		switch (err) {
241f4c1ea5fSJiri Olsa 		case TEST_OK:
242945aea22SJiri Olsa 			pr_info(" Ok\n");
243f4c1ea5fSJiri Olsa 			break;
244f4c1ea5fSJiri Olsa 		case TEST_SKIP:
245f4c1ea5fSJiri Olsa 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
246f4c1ea5fSJiri Olsa 			break;
247f4c1ea5fSJiri Olsa 		case TEST_FAIL:
248f4c1ea5fSJiri Olsa 		default:
249f4c1ea5fSJiri Olsa 			color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
250f4c1ea5fSJiri Olsa 			break;
251f4c1ea5fSJiri Olsa 		}
252945aea22SJiri Olsa 	}
253945aea22SJiri Olsa 
254945aea22SJiri Olsa 	return 0;
255945aea22SJiri Olsa }
256945aea22SJiri Olsa 
257945aea22SJiri Olsa static int perf_test__list(int argc, const char **argv)
258945aea22SJiri Olsa {
259945aea22SJiri Olsa 	int i = 0;
260945aea22SJiri Olsa 
261945aea22SJiri Olsa 	while (tests[i].func) {
262945aea22SJiri Olsa 		int curr = i++;
263945aea22SJiri Olsa 
264945aea22SJiri Olsa 		if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
265945aea22SJiri Olsa 			continue;
266945aea22SJiri Olsa 
267945aea22SJiri Olsa 		pr_info("%2d: %s\n", i, tests[curr].desc);
268945aea22SJiri Olsa 	}
269945aea22SJiri Olsa 
270945aea22SJiri Olsa 	return 0;
271945aea22SJiri Olsa }
272945aea22SJiri Olsa 
273945aea22SJiri Olsa int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
274945aea22SJiri Olsa {
275945aea22SJiri Olsa 	const char * const test_usage[] = {
276945aea22SJiri Olsa 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
277945aea22SJiri Olsa 	NULL,
278945aea22SJiri Olsa 	};
2792ae82878SArnaldo Carvalho de Melo 	const char *skip = NULL;
280945aea22SJiri Olsa 	const struct option test_options[] = {
2812ae82878SArnaldo Carvalho de Melo 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
282945aea22SJiri Olsa 	OPT_INCR('v', "verbose", &verbose,
283945aea22SJiri Olsa 		    "be more verbose (show symbol address, etc)"),
284945aea22SJiri Olsa 	OPT_END()
285945aea22SJiri Olsa 	};
2862ae82878SArnaldo Carvalho de Melo 	struct intlist *skiplist = NULL;
287945aea22SJiri Olsa 
288945aea22SJiri Olsa 	argc = parse_options(argc, argv, test_options, test_usage, 0);
289945aea22SJiri Olsa 	if (argc >= 1 && !strcmp(argv[0], "list"))
290945aea22SJiri Olsa 		return perf_test__list(argc, argv);
291945aea22SJiri Olsa 
292945aea22SJiri Olsa 	symbol_conf.priv_size = sizeof(int);
293945aea22SJiri Olsa 	symbol_conf.sort_by_name = true;
294945aea22SJiri Olsa 	symbol_conf.try_vmlinux_path = true;
295945aea22SJiri Olsa 
296945aea22SJiri Olsa 	if (symbol__init() < 0)
297945aea22SJiri Olsa 		return -1;
298945aea22SJiri Olsa 
2992ae82878SArnaldo Carvalho de Melo 	if (skip != NULL)
3002ae82878SArnaldo Carvalho de Melo 		skiplist = intlist__new(skip);
3012ae82878SArnaldo Carvalho de Melo 
3022ae82878SArnaldo Carvalho de Melo 	return __cmd_test(argc, argv, skiplist);
303945aea22SJiri Olsa }
304