xref: /linux/tools/perf/tests/builtin-test.c (revision 8e947f1e84fd1588f66e5f2ea69c80647de72cd4)
1 /*
2  * builtin-test.c
3  *
4  * Builtin regression testing command: ever growing number of sanity tests
5  */
6 #include <unistd.h>
7 #include <string.h>
8 #include "builtin.h"
9 #include "hist.h"
10 #include "intlist.h"
11 #include "tests.h"
12 #include "debug.h"
13 #include "color.h"
14 #include "parse-options.h"
15 #include "symbol.h"
16 
17 static struct test {
18 	const char *desc;
19 	int (*func)(void);
20 } tests[] = {
21 	{
22 		.desc = "vmlinux symtab matches kallsyms",
23 		.func = test__vmlinux_matches_kallsyms,
24 	},
25 	{
26 		.desc = "detect openat syscall event",
27 		.func = test__openat_syscall_event,
28 	},
29 	{
30 		.desc = "detect openat syscall event on all cpus",
31 		.func = test__openat_syscall_event_on_all_cpus,
32 	},
33 	{
34 		.desc = "read samples using the mmap interface",
35 		.func = test__basic_mmap,
36 	},
37 	{
38 		.desc = "parse events tests",
39 		.func = test__parse_events,
40 	},
41 #if defined(__x86_64__) || defined(__i386__)
42 	{
43 		.desc = "x86 rdpmc test",
44 		.func = test__rdpmc,
45 	},
46 #endif
47 	{
48 		.desc = "Validate PERF_RECORD_* events & perf_sample fields",
49 		.func = test__PERF_RECORD,
50 	},
51 	{
52 		.desc = "Test perf pmu format parsing",
53 		.func = test__pmu,
54 	},
55 	{
56 		.desc = "Test dso data read",
57 		.func = test__dso_data,
58 	},
59 	{
60 		.desc = "Test dso data cache",
61 		.func = test__dso_data_cache,
62 	},
63 	{
64 		.desc = "Test dso data reopen",
65 		.func = test__dso_data_reopen,
66 	},
67 	{
68 		.desc = "roundtrip evsel->name check",
69 		.func = test__perf_evsel__roundtrip_name_test,
70 	},
71 	{
72 		.desc = "Check parsing of sched tracepoints fields",
73 		.func = test__perf_evsel__tp_sched_test,
74 	},
75 	{
76 		.desc = "Generate and check syscalls:sys_enter_openat event fields",
77 		.func = test__syscall_openat_tp_fields,
78 	},
79 	{
80 		.desc = "struct perf_event_attr setup",
81 		.func = test__attr,
82 	},
83 	{
84 		.desc = "Test matching and linking multiple hists",
85 		.func = test__hists_link,
86 	},
87 	{
88 		.desc = "Try 'import perf' in python, checking link problems",
89 		.func = test__python_use,
90 	},
91 	{
92 		.desc = "Test breakpoint overflow signal handler",
93 		.func = test__bp_signal,
94 	},
95 	{
96 		.desc = "Test breakpoint overflow sampling",
97 		.func = test__bp_signal_overflow,
98 	},
99 	{
100 		.desc = "Test number of exit event of a simple workload",
101 		.func = test__task_exit,
102 	},
103 	{
104 		.desc = "Test software clock events have valid period values",
105 		.func = test__sw_clock_freq,
106 	},
107 #if defined(__x86_64__) || defined(__i386__)
108 	{
109 		.desc = "Test converting perf time to TSC",
110 		.func = test__perf_time_to_tsc,
111 	},
112 #endif
113 	{
114 		.desc = "Test object code reading",
115 		.func = test__code_reading,
116 	},
117 	{
118 		.desc = "Test sample parsing",
119 		.func = test__sample_parsing,
120 	},
121 	{
122 		.desc = "Test using a dummy software event to keep tracking",
123 		.func = test__keep_tracking,
124 	},
125 	{
126 		.desc = "Test parsing with no sample_id_all bit set",
127 		.func = test__parse_no_sample_id_all,
128 	},
129 #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__)
130 #ifdef HAVE_DWARF_UNWIND_SUPPORT
131 	{
132 		.desc = "Test dwarf unwind",
133 		.func = test__dwarf_unwind,
134 	},
135 #endif
136 #endif
137 	{
138 		.desc = "Test filtering hist entries",
139 		.func = test__hists_filter,
140 	},
141 	{
142 		.desc = "Test mmap thread lookup",
143 		.func = test__mmap_thread_lookup,
144 	},
145 	{
146 		.desc = "Test thread mg sharing",
147 		.func = test__thread_mg_share,
148 	},
149 	{
150 		.desc = "Test output sorting of hist entries",
151 		.func = test__hists_output,
152 	},
153 	{
154 		.desc = "Test cumulation of child hist entries",
155 		.func = test__hists_cumulate,
156 	},
157 	{
158 		.desc = "Test tracking with sched_switch",
159 		.func = test__switch_tracking,
160 	},
161 	{
162 		.desc = "Filter fds with revents mask in a fdarray",
163 		.func = test__fdarray__filter,
164 	},
165 	{
166 		.desc = "Add fd to a fdarray, making it autogrow",
167 		.func = test__fdarray__add,
168 	},
169 	{
170 		.desc = "Test kmod_path__parse function",
171 		.func = test__kmod_path__parse,
172 	},
173 	{
174 		.desc = "Test thread map",
175 		.func = test__thread_map,
176 	},
177 	{
178 		.desc = "Test LLVM searching and compiling",
179 		.func = test__llvm,
180 	},
181 #ifdef HAVE_AUXTRACE_SUPPORT
182 #if defined(__x86_64__) || defined(__i386__)
183 	{
184 		.desc = "Test x86 instruction decoder - new instructions",
185 		.func = test__insn_x86,
186 	},
187 #endif
188 #endif
189 	{
190 		.desc = "Test topology in session",
191 		.func = test_session_topology,
192 	},
193 	{
194 		.func = NULL,
195 	},
196 };
197 
198 static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[])
199 {
200 	int i;
201 
202 	if (argc == 0)
203 		return true;
204 
205 	for (i = 0; i < argc; ++i) {
206 		char *end;
207 		long nr = strtoul(argv[i], &end, 10);
208 
209 		if (*end == '\0') {
210 			if (nr == curr + 1)
211 				return true;
212 			continue;
213 		}
214 
215 		if (strstr(test->desc, argv[i]))
216 			return true;
217 	}
218 
219 	return false;
220 }
221 
222 static int run_test(struct test *test)
223 {
224 	int status, err = -1, child = fork();
225 	char sbuf[STRERR_BUFSIZE];
226 
227 	if (child < 0) {
228 		pr_err("failed to fork test: %s\n",
229 			strerror_r(errno, sbuf, sizeof(sbuf)));
230 		return -1;
231 	}
232 
233 	if (!child) {
234 		pr_debug("test child forked, pid %d\n", getpid());
235 		err = test->func();
236 		exit(err);
237 	}
238 
239 	wait(&status);
240 
241 	if (WIFEXITED(status)) {
242 		err = (signed char)WEXITSTATUS(status);
243 		pr_debug("test child finished with %d\n", err);
244 	} else if (WIFSIGNALED(status)) {
245 		err = -1;
246 		pr_debug("test child interrupted\n");
247 	}
248 
249 	return err;
250 }
251 
252 #define for_each_test(t)	 for (t = &tests[0]; t->func; t++)
253 
254 static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
255 {
256 	struct test *t;
257 	int i = 0;
258 	int width = 0;
259 
260 	for_each_test(t) {
261 		int len = strlen(t->desc);
262 
263 		if (width < len)
264 			width = len;
265 	}
266 
267 	for_each_test(t) {
268 		int curr = i++, err;
269 
270 		if (!perf_test__matches(t, curr, argc, argv))
271 			continue;
272 
273 		pr_info("%2d: %-*s:", i, width, t->desc);
274 
275 		if (intlist__find(skiplist, i)) {
276 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
277 			continue;
278 		}
279 
280 		pr_debug("\n--- start ---\n");
281 		err = run_test(t);
282 		pr_debug("---- end ----\n%s:", t->desc);
283 
284 		switch (err) {
285 		case TEST_OK:
286 			pr_info(" Ok\n");
287 			break;
288 		case TEST_SKIP:
289 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
290 			break;
291 		case TEST_FAIL:
292 		default:
293 			color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
294 			break;
295 		}
296 	}
297 
298 	return 0;
299 }
300 
301 static int perf_test__list(int argc, const char **argv)
302 {
303 	struct test *t;
304 	int i = 0;
305 
306 	for_each_test(t) {
307 		if (argc > 1 && !strstr(t->desc, argv[1]))
308 			continue;
309 
310 		pr_info("%2d: %s\n", ++i, t->desc);
311 	}
312 
313 	return 0;
314 }
315 
316 int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused)
317 {
318 	const char *test_usage[] = {
319 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
320 	NULL,
321 	};
322 	const char *skip = NULL;
323 	const struct option test_options[] = {
324 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
325 	OPT_INCR('v', "verbose", &verbose,
326 		    "be more verbose (show symbol address, etc)"),
327 	OPT_END()
328 	};
329 	const char * const test_subcommands[] = { "list", NULL };
330 	struct intlist *skiplist = NULL;
331         int ret = hists__init();
332 
333         if (ret < 0)
334                 return ret;
335 
336 	argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
337 	if (argc >= 1 && !strcmp(argv[0], "list"))
338 		return perf_test__list(argc, argv);
339 
340 	symbol_conf.priv_size = sizeof(int);
341 	symbol_conf.sort_by_name = true;
342 	symbol_conf.try_vmlinux_path = true;
343 
344 	if (symbol__init(NULL) < 0)
345 		return -1;
346 
347 	if (skip != NULL)
348 		skiplist = intlist__new(skip);
349 
350 	return __cmd_test(argc, argv, skiplist);
351 }
352