xref: /openbmc/linux/tools/perf/tests/builtin-test.c (revision f21d1815)
1945aea22SJiri Olsa /*
2945aea22SJiri Olsa  * builtin-test.c
3945aea22SJiri Olsa  *
4945aea22SJiri Olsa  * Builtin regression testing command: ever growing number of sanity tests
5945aea22SJiri Olsa  */
6945aea22SJiri Olsa #include "builtin.h"
72ae82878SArnaldo Carvalho de Melo #include "intlist.h"
80a4e1ae6SJiri Olsa #include "tests.h"
9c81251e8SJiri Olsa #include "debug.h"
10c81251e8SJiri Olsa #include "color.h"
11c81251e8SJiri Olsa #include "parse-options.h"
12c81251e8SJiri Olsa #include "symbol.h"
13945aea22SJiri Olsa 
14945aea22SJiri Olsa static struct test {
15945aea22SJiri Olsa 	const char *desc;
16945aea22SJiri Olsa 	int (*func)(void);
17945aea22SJiri Olsa } tests[] = {
18945aea22SJiri Olsa 	{
19945aea22SJiri Olsa 		.desc = "vmlinux symtab matches kallsyms",
20945aea22SJiri Olsa 		.func = test__vmlinux_matches_kallsyms,
21945aea22SJiri Olsa 	},
22945aea22SJiri Olsa 	{
23945aea22SJiri Olsa 		.desc = "detect open syscall event",
24945aea22SJiri Olsa 		.func = test__open_syscall_event,
25945aea22SJiri Olsa 	},
26945aea22SJiri Olsa 	{
27945aea22SJiri Olsa 		.desc = "detect open syscall event on all cpus",
28945aea22SJiri Olsa 		.func = test__open_syscall_event_on_all_cpus,
29945aea22SJiri Olsa 	},
30945aea22SJiri Olsa 	{
31945aea22SJiri Olsa 		.desc = "read samples using the mmap interface",
32945aea22SJiri Olsa 		.func = test__basic_mmap,
33945aea22SJiri Olsa 	},
34945aea22SJiri Olsa 	{
35945aea22SJiri Olsa 		.desc = "parse events tests",
36c81251e8SJiri Olsa 		.func = test__parse_events,
37945aea22SJiri Olsa 	},
38945aea22SJiri Olsa #if defined(__x86_64__) || defined(__i386__)
39945aea22SJiri Olsa 	{
40945aea22SJiri Olsa 		.desc = "x86 rdpmc test",
41945aea22SJiri Olsa 		.func = test__rdpmc,
42945aea22SJiri Olsa 	},
43945aea22SJiri Olsa #endif
44945aea22SJiri Olsa 	{
45945aea22SJiri Olsa 		.desc = "Validate PERF_RECORD_* events & perf_sample fields",
46945aea22SJiri Olsa 		.func = test__PERF_RECORD,
47945aea22SJiri Olsa 	},
48945aea22SJiri Olsa 	{
49945aea22SJiri Olsa 		.desc = "Test perf pmu format parsing",
50cff7f956SJiri Olsa 		.func = test__pmu,
51945aea22SJiri Olsa 	},
52945aea22SJiri Olsa 	{
53945aea22SJiri Olsa 		.desc = "Test dso data interface",
54c81251e8SJiri Olsa 		.func = test__dso_data,
55945aea22SJiri Olsa 	},
56945aea22SJiri Olsa 	{
57945aea22SJiri Olsa 		.desc = "roundtrip evsel->name check",
58cfffae2eSJiri Olsa 		.func = test__perf_evsel__roundtrip_name_test,
59945aea22SJiri Olsa 	},
60945aea22SJiri Olsa 	{
61945aea22SJiri Olsa 		.desc = "Check parsing of sched tracepoints fields",
625e24a090SJiri Olsa 		.func = test__perf_evsel__tp_sched_test,
63945aea22SJiri Olsa 	},
64945aea22SJiri Olsa 	{
65945aea22SJiri Olsa 		.desc = "Generate and check syscalls:sys_enter_open event fields",
66945aea22SJiri Olsa 		.func = test__syscall_open_tp_fields,
67945aea22SJiri Olsa 	},
68945aea22SJiri Olsa 	{
69d898b241SJiri Olsa 		.desc = "struct perf_event_attr setup",
70c81251e8SJiri Olsa 		.func = test__attr,
71d898b241SJiri Olsa 	},
72d898b241SJiri Olsa 	{
73ffcbaa14SArnaldo Carvalho de Melo 		.desc = "Test matching and linking multiple hists",
74f8ebb0cdSNamhyung Kim 		.func = test__hists_link,
75f8ebb0cdSNamhyung Kim 	},
76f8ebb0cdSNamhyung Kim 	{
7754359d33SArnaldo Carvalho de Melo 		.desc = "Try 'use perf' in python, checking link problems",
7854359d33SArnaldo Carvalho de Melo 		.func = test__python_use,
7954359d33SArnaldo Carvalho de Melo 	},
8054359d33SArnaldo Carvalho de Melo 	{
815a6bef47SJiri Olsa 		.desc = "Test breakpoint overflow signal handler",
825a6bef47SJiri Olsa 		.func = test__bp_signal,
835a6bef47SJiri Olsa 	},
845a6bef47SJiri Olsa 	{
8506933e3aSJiri Olsa 		.desc = "Test breakpoint overflow sampling",
8606933e3aSJiri Olsa 		.func = test__bp_signal_overflow,
8706933e3aSJiri Olsa 	},
8806933e3aSJiri Olsa 	{
89d723a550SNamhyung Kim 		.desc = "Test number of exit event of a simple workload",
90d723a550SNamhyung Kim 		.func = test__task_exit,
91d723a550SNamhyung Kim 	},
92d723a550SNamhyung Kim 	{
93bc96b361SNamhyung Kim 		.desc = "Test software clock events have valid period values",
94bc96b361SNamhyung Kim 		.func = test__sw_clock_freq,
95bc96b361SNamhyung Kim 	},
963bd5a5fcSAdrian Hunter #if defined(__x86_64__) || defined(__i386__)
973bd5a5fcSAdrian Hunter 	{
983bd5a5fcSAdrian Hunter 		.desc = "Test converting perf time to TSC",
993bd5a5fcSAdrian Hunter 		.func = test__perf_time_to_tsc,
1003bd5a5fcSAdrian Hunter 	},
1013bd5a5fcSAdrian Hunter #endif
102bc96b361SNamhyung Kim 	{
103b55ae0a9SAdrian Hunter 		.desc = "Test object code reading",
104b55ae0a9SAdrian Hunter 		.func = test__code_reading,
105b55ae0a9SAdrian Hunter 	},
106b55ae0a9SAdrian Hunter 	{
107045f8cd8SAdrian Hunter 		.desc = "Test sample parsing",
108045f8cd8SAdrian Hunter 		.func = test__sample_parsing,
109045f8cd8SAdrian Hunter 	},
110045f8cd8SAdrian Hunter 	{
111395c3070SAdrian Hunter 		.desc = "Test using a dummy software event to keep tracking",
112395c3070SAdrian Hunter 		.func = test__keep_tracking,
113395c3070SAdrian Hunter 	},
114395c3070SAdrian Hunter 	{
11553a277e5SAdrian Hunter 		.desc = "Test parsing with no sample_id_all bit set",
11653a277e5SAdrian Hunter 		.func = test__parse_no_sample_id_all,
11753a277e5SAdrian Hunter 	},
11890fa9debSJean Pihet #if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
1199ff125d1SJiri Olsa #ifdef HAVE_DWARF_UNWIND_SUPPORT
120aa16b81fSJiri Olsa 	{
121aa16b81fSJiri Olsa 		.desc = "Test dwarf unwind",
122aa16b81fSJiri Olsa 		.func = test__dwarf_unwind,
123aa16b81fSJiri Olsa 	},
124aa16b81fSJiri Olsa #endif
125aa16b81fSJiri Olsa #endif
12653a277e5SAdrian Hunter 	{
1273c3cfd99SNamhyung Kim 		.desc = "Test filtering hist entries",
1283c3cfd99SNamhyung Kim 		.func = test__hists_filter,
1293c3cfd99SNamhyung Kim 	},
1303c3cfd99SNamhyung Kim 	{
1314e85edfcSJiri Olsa 		.desc = "Test mmap thread lookup",
1324e85edfcSJiri Olsa 		.func = test__mmap_thread_lookup,
1334e85edfcSJiri Olsa 	},
1344e85edfcSJiri Olsa 	{
135fabf0123SJiri Olsa 		.desc = "Test thread mg sharing",
136fabf0123SJiri Olsa 		.func = test__thread_mg_share,
137fabf0123SJiri Olsa 	},
138fabf0123SJiri Olsa 	{
139f21d1815SNamhyung Kim 		.desc = "Test output sorting of hist entries",
140f21d1815SNamhyung Kim 		.func = test__hists_output,
141f21d1815SNamhyung Kim 	},
142f21d1815SNamhyung Kim 	{
143945aea22SJiri Olsa 		.func = NULL,
144945aea22SJiri Olsa 	},
145945aea22SJiri Olsa };
146945aea22SJiri Olsa 
147945aea22SJiri Olsa static bool perf_test__matches(int curr, int argc, const char *argv[])
148945aea22SJiri Olsa {
149945aea22SJiri Olsa 	int i;
150945aea22SJiri Olsa 
151945aea22SJiri Olsa 	if (argc == 0)
152945aea22SJiri Olsa 		return true;
153945aea22SJiri Olsa 
154945aea22SJiri Olsa 	for (i = 0; i < argc; ++i) {
155945aea22SJiri Olsa 		char *end;
156945aea22SJiri Olsa 		long nr = strtoul(argv[i], &end, 10);
157945aea22SJiri Olsa 
158945aea22SJiri Olsa 		if (*end == '\0') {
159945aea22SJiri Olsa 			if (nr == curr + 1)
160945aea22SJiri Olsa 				return true;
161945aea22SJiri Olsa 			continue;
162945aea22SJiri Olsa 		}
163945aea22SJiri Olsa 
164945aea22SJiri Olsa 		if (strstr(tests[curr].desc, argv[i]))
165945aea22SJiri Olsa 			return true;
166945aea22SJiri Olsa 	}
167945aea22SJiri Olsa 
168945aea22SJiri Olsa 	return false;
169945aea22SJiri Olsa }
170945aea22SJiri Olsa 
1712ae82878SArnaldo Carvalho de Melo static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
172945aea22SJiri Olsa {
173945aea22SJiri Olsa 	int i = 0;
174945aea22SJiri Olsa 	int width = 0;
175945aea22SJiri Olsa 
176945aea22SJiri Olsa 	while (tests[i].func) {
177945aea22SJiri Olsa 		int len = strlen(tests[i].desc);
178945aea22SJiri Olsa 
179945aea22SJiri Olsa 		if (width < len)
180945aea22SJiri Olsa 			width = len;
181945aea22SJiri Olsa 		++i;
182945aea22SJiri Olsa 	}
183945aea22SJiri Olsa 
184945aea22SJiri Olsa 	i = 0;
185945aea22SJiri Olsa 	while (tests[i].func) {
186945aea22SJiri Olsa 		int curr = i++, err;
187945aea22SJiri Olsa 
188945aea22SJiri Olsa 		if (!perf_test__matches(curr, argc, argv))
189945aea22SJiri Olsa 			continue;
190945aea22SJiri Olsa 
191945aea22SJiri Olsa 		pr_info("%2d: %-*s:", i, width, tests[curr].desc);
1922ae82878SArnaldo Carvalho de Melo 
1932ae82878SArnaldo Carvalho de Melo 		if (intlist__find(skiplist, i)) {
1942ae82878SArnaldo Carvalho de Melo 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
1952ae82878SArnaldo Carvalho de Melo 			continue;
1962ae82878SArnaldo Carvalho de Melo 		}
1972ae82878SArnaldo Carvalho de Melo 
198945aea22SJiri Olsa 		pr_debug("\n--- start ---\n");
199945aea22SJiri Olsa 		err = tests[curr].func();
200945aea22SJiri Olsa 		pr_debug("---- end ----\n%s:", tests[curr].desc);
201f4c1ea5fSJiri Olsa 
202f4c1ea5fSJiri Olsa 		switch (err) {
203f4c1ea5fSJiri Olsa 		case TEST_OK:
204945aea22SJiri Olsa 			pr_info(" Ok\n");
205f4c1ea5fSJiri Olsa 			break;
206f4c1ea5fSJiri Olsa 		case TEST_SKIP:
207f4c1ea5fSJiri Olsa 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
208f4c1ea5fSJiri Olsa 			break;
209f4c1ea5fSJiri Olsa 		case TEST_FAIL:
210f4c1ea5fSJiri Olsa 		default:
211f4c1ea5fSJiri Olsa 			color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
212f4c1ea5fSJiri Olsa 			break;
213f4c1ea5fSJiri Olsa 		}
214945aea22SJiri Olsa 	}
215945aea22SJiri Olsa 
216945aea22SJiri Olsa 	return 0;
217945aea22SJiri Olsa }
218945aea22SJiri Olsa 
219945aea22SJiri Olsa static int perf_test__list(int argc, const char **argv)
220945aea22SJiri Olsa {
221945aea22SJiri Olsa 	int i = 0;
222945aea22SJiri Olsa 
223945aea22SJiri Olsa 	while (tests[i].func) {
224945aea22SJiri Olsa 		int curr = i++;
225945aea22SJiri Olsa 
226945aea22SJiri Olsa 		if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
227945aea22SJiri Olsa 			continue;
228945aea22SJiri Olsa 
229945aea22SJiri Olsa 		pr_info("%2d: %s\n", i, tests[curr].desc);
230945aea22SJiri Olsa 	}
231945aea22SJiri Olsa 
232945aea22SJiri Olsa 	return 0;
233945aea22SJiri Olsa }
234945aea22SJiri Olsa 
235945aea22SJiri Olsa int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
236945aea22SJiri Olsa {
237945aea22SJiri Olsa 	const char * const test_usage[] = {
238945aea22SJiri Olsa 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
239945aea22SJiri Olsa 	NULL,
240945aea22SJiri Olsa 	};
2412ae82878SArnaldo Carvalho de Melo 	const char *skip = NULL;
242945aea22SJiri Olsa 	const struct option test_options[] = {
2432ae82878SArnaldo Carvalho de Melo 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
244945aea22SJiri Olsa 	OPT_INCR('v', "verbose", &verbose,
245945aea22SJiri Olsa 		    "be more verbose (show symbol address, etc)"),
246945aea22SJiri Olsa 	OPT_END()
247945aea22SJiri Olsa 	};
2482ae82878SArnaldo Carvalho de Melo 	struct intlist *skiplist = NULL;
249945aea22SJiri Olsa 
250945aea22SJiri Olsa 	argc = parse_options(argc, argv, test_options, test_usage, 0);
251945aea22SJiri Olsa 	if (argc >= 1 && !strcmp(argv[0], "list"))
252945aea22SJiri Olsa 		return perf_test__list(argc, argv);
253945aea22SJiri Olsa 
254945aea22SJiri Olsa 	symbol_conf.priv_size = sizeof(int);
255945aea22SJiri Olsa 	symbol_conf.sort_by_name = true;
256945aea22SJiri Olsa 	symbol_conf.try_vmlinux_path = true;
257945aea22SJiri Olsa 
258945aea22SJiri Olsa 	if (symbol__init() < 0)
259945aea22SJiri Olsa 		return -1;
260945aea22SJiri Olsa 
2612ae82878SArnaldo Carvalho de Melo 	if (skip != NULL)
2622ae82878SArnaldo Carvalho de Melo 		skiplist = intlist__new(skip);
2632ae82878SArnaldo Carvalho de Melo 
2642ae82878SArnaldo Carvalho de Melo 	return __cmd_test(argc, argv, skiplist);
265945aea22SJiri Olsa }
266