xref: /linux/tools/perf/tests/builtin-test.c (revision 21a8b756b84ecaa0b7f29199669c151e2ce5e723)
1 /*
2  * builtin-test.c
3  *
4  * Builtin regression testing command: ever growing number of sanity tests
5  */
6 #include "builtin.h"
7 #include "intlist.h"
8 #include "tests.h"
9 #include "debug.h"
10 #include "color.h"
11 #include "parse-options.h"
12 #include "symbol.h"
13 
14 static struct test {
15 	const char *desc;
16 	int (*func)(void);
17 } tests[] = {
18 	{
19 		.desc = "vmlinux symtab matches kallsyms",
20 		.func = test__vmlinux_matches_kallsyms,
21 	},
22 	{
23 		.desc = "detect open syscall event",
24 		.func = test__open_syscall_event,
25 	},
26 	{
27 		.desc = "detect open syscall event on all cpus",
28 		.func = test__open_syscall_event_on_all_cpus,
29 	},
30 	{
31 		.desc = "read samples using the mmap interface",
32 		.func = test__basic_mmap,
33 	},
34 	{
35 		.desc = "parse events tests",
36 		.func = test__parse_events,
37 	},
38 #if defined(__x86_64__) || defined(__i386__)
39 	{
40 		.desc = "x86 rdpmc test",
41 		.func = test__rdpmc,
42 	},
43 #endif
44 	{
45 		.desc = "Validate PERF_RECORD_* events & perf_sample fields",
46 		.func = test__PERF_RECORD,
47 	},
48 	{
49 		.desc = "Test perf pmu format parsing",
50 		.func = test__pmu,
51 	},
52 	{
53 		.desc = "Test dso data interface",
54 		.func = test__dso_data,
55 	},
56 	{
57 		.desc = "roundtrip evsel->name check",
58 		.func = test__perf_evsel__roundtrip_name_test,
59 	},
60 	{
61 		.desc = "Check parsing of sched tracepoints fields",
62 		.func = test__perf_evsel__tp_sched_test,
63 	},
64 	{
65 		.desc = "Generate and check syscalls:sys_enter_open event fields",
66 		.func = test__syscall_open_tp_fields,
67 	},
68 	{
69 		.desc = "struct perf_event_attr setup",
70 		.func = test__attr,
71 	},
72 	{
73 		.desc = "Test matching and linking multiple hists",
74 		.func = test__hists_link,
75 	},
76 	{
77 		.desc = "Try 'use perf' in python, checking link problems",
78 		.func = test__python_use,
79 	},
80 	{
81 		.desc = "Test breakpoint overflow signal handler",
82 		.func = test__bp_signal,
83 	},
84 	{
85 		.desc = "Test breakpoint overflow sampling",
86 		.func = test__bp_signal_overflow,
87 	},
88 	{
89 		.desc = "Test number of exit event of a simple workload",
90 		.func = test__task_exit,
91 	},
92 	{
93 		.desc = "Test software clock events have valid period values",
94 		.func = test__sw_clock_freq,
95 	},
96 #if defined(__x86_64__) || defined(__i386__)
97 	{
98 		.desc = "Test converting perf time to TSC",
99 		.func = test__perf_time_to_tsc,
100 	},
101 #endif
102 	{
103 		.desc = "Test object code reading",
104 		.func = test__code_reading,
105 	},
106 	{
107 		.desc = "Test sample parsing",
108 		.func = test__sample_parsing,
109 	},
110 	{
111 		.desc = "Test using a dummy software event to keep tracking",
112 		.func = test__keep_tracking,
113 	},
114 	{
115 		.desc = "Test parsing with no sample_id_all bit set",
116 		.func = test__parse_no_sample_id_all,
117 	},
118 #if defined(__x86_64__) || defined(__i386__)
119 #ifdef HAVE_DWARF_UNWIND_SUPPORT
120 	{
121 		.desc = "Test dwarf unwind",
122 		.func = test__dwarf_unwind,
123 	},
124 #endif
125 #endif
126 	{
127 		.desc = "Test filtering hist entries",
128 		.func = test__hists_filter,
129 	},
130 	{
131 		.desc = "Test mmap thread lookup",
132 		.func = test__mmap_thread_lookup,
133 	},
134 	{
135 		.desc = "Test thread mg sharing",
136 		.func = test__thread_mg_share,
137 	},
138 	{
139 		.func = NULL,
140 	},
141 };
142 
143 static bool perf_test__matches(int curr, int argc, const char *argv[])
144 {
145 	int i;
146 
147 	if (argc == 0)
148 		return true;
149 
150 	for (i = 0; i < argc; ++i) {
151 		char *end;
152 		long nr = strtoul(argv[i], &end, 10);
153 
154 		if (*end == '\0') {
155 			if (nr == curr + 1)
156 				return true;
157 			continue;
158 		}
159 
160 		if (strstr(tests[curr].desc, argv[i]))
161 			return true;
162 	}
163 
164 	return false;
165 }
166 
167 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
168 {
169 	int i = 0;
170 	int width = 0;
171 
172 	while (tests[i].func) {
173 		int len = strlen(tests[i].desc);
174 
175 		if (width < len)
176 			width = len;
177 		++i;
178 	}
179 
180 	i = 0;
181 	while (tests[i].func) {
182 		int curr = i++, err;
183 
184 		if (!perf_test__matches(curr, argc, argv))
185 			continue;
186 
187 		pr_info("%2d: %-*s:", i, width, tests[curr].desc);
188 
189 		if (intlist__find(skiplist, i)) {
190 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
191 			continue;
192 		}
193 
194 		pr_debug("\n--- start ---\n");
195 		err = tests[curr].func();
196 		pr_debug("---- end ----\n%s:", tests[curr].desc);
197 
198 		switch (err) {
199 		case TEST_OK:
200 			pr_info(" Ok\n");
201 			break;
202 		case TEST_SKIP:
203 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
204 			break;
205 		case TEST_FAIL:
206 		default:
207 			color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
208 			break;
209 		}
210 	}
211 
212 	return 0;
213 }
214 
215 static int perf_test__list(int argc, const char **argv)
216 {
217 	int i = 0;
218 
219 	while (tests[i].func) {
220 		int curr = i++;
221 
222 		if (argc > 1 && !strstr(tests[curr].desc, argv[1]))
223 			continue;
224 
225 		pr_info("%2d: %s\n", i, tests[curr].desc);
226 	}
227 
228 	return 0;
229 }
230 
231 int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
232 {
233 	const char * const test_usage[] = {
234 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
235 	NULL,
236 	};
237 	const char *skip = NULL;
238 	const struct option test_options[] = {
239 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
240 	OPT_INCR('v', "verbose", &verbose,
241 		    "be more verbose (show symbol address, etc)"),
242 	OPT_END()
243 	};
244 	struct intlist *skiplist = NULL;
245 
246 	argc = parse_options(argc, argv, test_options, test_usage, 0);
247 	if (argc >= 1 && !strcmp(argv[0], "list"))
248 		return perf_test__list(argc, argv);
249 
250 	symbol_conf.priv_size = sizeof(int);
251 	symbol_conf.sort_by_name = true;
252 	symbol_conf.try_vmlinux_path = true;
253 
254 	if (symbol__init() < 0)
255 		return -1;
256 
257 	if (skip != NULL)
258 		skiplist = intlist__new(skip);
259 
260 	return __cmd_test(argc, argv, skiplist);
261 }
262