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