xref: /openbmc/linux/tools/perf/tests/builtin-test.c (revision b47d2fb40f507a531417f3a893aa822be355ae5f)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2945aea22SJiri Olsa /*
3945aea22SJiri Olsa  * builtin-test.c
4945aea22SJiri Olsa  *
5945aea22SJiri Olsa  * Builtin regression testing command: ever growing number of sanity tests
6945aea22SJiri Olsa  */
7c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h>
8a43783aeSArnaldo Carvalho de Melo #include <errno.h>
90d8a5faaSJiri Olsa #include <unistd.h>
100d8a5faaSJiri Olsa #include <string.h>
11f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
121209b273SArnaldo Carvalho de Melo #include <sys/types.h>
131209b273SArnaldo Carvalho de Melo #include <dirent.h>
144208735dSArnaldo Carvalho de Melo #include <sys/wait.h>
151209b273SArnaldo Carvalho de Melo #include <sys/stat.h>
16945aea22SJiri Olsa #include "builtin.h"
17a635fc51SArnaldo Carvalho de Melo #include "hist.h"
182ae82878SArnaldo Carvalho de Melo #include "intlist.h"
190a4e1ae6SJiri Olsa #include "tests.h"
20c81251e8SJiri Olsa #include "debug.h"
21c81251e8SJiri Olsa #include "color.h"
224b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
231209b273SArnaldo Carvalho de Melo #include "string2.h"
24c81251e8SJiri Olsa #include "symbol.h"
25d3280ce0SArnaldo Carvalho de Melo #include "util/rlimit.h"
26877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
273ca43b60SArnaldo Carvalho de Melo #include <linux/string.h>
281209b273SArnaldo Carvalho de Melo #include <subcmd/exec-cmd.h>
29eb7261f1SRiccardo Mancini #include <linux/zalloc.h>
30945aea22SJiri Olsa 
317fa9b8fbSJiri Olsa static bool dont_fork;
327fa9b8fbSJiri Olsa 
3333f44bfdSIan Rogers struct test_suite *__weak arch_tests[] = {
34df225205SIan Rogers 	NULL,
3531b6753fSMatt Fleming };
3631b6753fSMatt Fleming 
3733f44bfdSIan Rogers static struct test_suite *generic_tests[] = {
38d68f0365SIan Rogers 	&suite__vmlinux_matches_kallsyms,
39d68f0365SIan Rogers 	&suite__openat_syscall_event,
40d68f0365SIan Rogers 	&suite__openat_syscall_event_on_all_cpus,
41d68f0365SIan Rogers 	&suite__basic_mmap,
42d68f0365SIan Rogers 	&suite__mem,
43d68f0365SIan Rogers 	&suite__parse_events,
44d68f0365SIan Rogers 	&suite__expr,
45d68f0365SIan Rogers 	&suite__PERF_RECORD,
46d68f0365SIan Rogers 	&suite__pmu,
47d68f0365SIan Rogers 	&suite__pmu_events,
48d68f0365SIan Rogers 	&suite__dso_data,
49d68f0365SIan Rogers 	&suite__dso_data_cache,
50d68f0365SIan Rogers 	&suite__dso_data_reopen,
51d68f0365SIan Rogers 	&suite__perf_evsel__roundtrip_name_test,
52d68f0365SIan Rogers 	&suite__perf_evsel__tp_sched_test,
53d68f0365SIan Rogers 	&suite__syscall_openat_tp_fields,
54d68f0365SIan Rogers 	&suite__attr,
55d68f0365SIan Rogers 	&suite__hists_link,
56d68f0365SIan Rogers 	&suite__python_use,
57d68f0365SIan Rogers 	&suite__bp_signal,
58d68f0365SIan Rogers 	&suite__bp_signal_overflow,
59d68f0365SIan Rogers 	&suite__bp_accounting,
60d68f0365SIan Rogers 	&suite__wp,
61d68f0365SIan Rogers 	&suite__task_exit,
62d68f0365SIan Rogers 	&suite__sw_clock_freq,
63d68f0365SIan Rogers 	&suite__code_reading,
64d68f0365SIan Rogers 	&suite__sample_parsing,
65d68f0365SIan Rogers 	&suite__keep_tracking,
66d68f0365SIan Rogers 	&suite__parse_no_sample_id_all,
67d68f0365SIan Rogers 	&suite__hists_filter,
68d68f0365SIan Rogers 	&suite__mmap_thread_lookup,
69d68f0365SIan Rogers 	&suite__thread_maps_share,
70d68f0365SIan Rogers 	&suite__hists_output,
71d68f0365SIan Rogers 	&suite__hists_cumulate,
72d68f0365SIan Rogers 	&suite__switch_tracking,
73d68f0365SIan Rogers 	&suite__fdarray__filter,
74d68f0365SIan Rogers 	&suite__fdarray__add,
75d68f0365SIan Rogers 	&suite__kmod_path__parse,
76d68f0365SIan Rogers 	&suite__thread_map,
77d68f0365SIan Rogers 	&suite__llvm,
78d68f0365SIan Rogers 	&suite__session_topology,
79d68f0365SIan Rogers 	&suite__bpf,
80d68f0365SIan Rogers 	&suite__thread_map_synthesize,
81d68f0365SIan Rogers 	&suite__thread_map_remove,
82d68f0365SIan Rogers 	&suite__cpu_map_synthesize,
83d68f0365SIan Rogers 	&suite__synthesize_stat_config,
84d68f0365SIan Rogers 	&suite__synthesize_stat,
85d68f0365SIan Rogers 	&suite__synthesize_stat_round,
86d68f0365SIan Rogers 	&suite__event_update,
87d68f0365SIan Rogers 	&suite__event_times,
88d68f0365SIan Rogers 	&suite__backward_ring_buffer,
89d68f0365SIan Rogers 	&suite__cpu_map_print,
90d68f0365SIan Rogers 	&suite__cpu_map_merge,
91d68f0365SIan Rogers 	&suite__sdt_event,
92d68f0365SIan Rogers 	&suite__is_printable_array,
93d68f0365SIan Rogers 	&suite__bitmap_print,
94d68f0365SIan Rogers 	&suite__perf_hooks,
95d68f0365SIan Rogers 	&suite__clang,
96d68f0365SIan Rogers 	&suite__unit_number__scnprint,
97d68f0365SIan Rogers 	&suite__mem2node,
98d68f0365SIan Rogers 	&suite__time_utils,
99d68f0365SIan Rogers 	&suite__jit_write_elf,
100d68f0365SIan Rogers 	&suite__pfm,
101d68f0365SIan Rogers 	&suite__api_io,
102d68f0365SIan Rogers 	&suite__maps__merge_in,
103d68f0365SIan Rogers 	&suite__demangle_java,
104d68f0365SIan Rogers 	&suite__demangle_ocaml,
105d68f0365SIan Rogers 	&suite__parse_metric,
106d68f0365SIan Rogers 	&suite__pe_file_parsing,
107d68f0365SIan Rogers 	&suite__expand_cgroup_events,
108d68f0365SIan Rogers 	&suite__perf_time_to_tsc,
109d68f0365SIan Rogers 	&suite__dlfilter,
110df225205SIan Rogers 	NULL,
111945aea22SJiri Olsa };
112945aea22SJiri Olsa 
11333f44bfdSIan Rogers static struct test_suite **tests[] = {
11431b6753fSMatt Fleming 	generic_tests,
11531b6753fSMatt Fleming 	arch_tests,
11631b6753fSMatt Fleming };
11731b6753fSMatt Fleming 
118f832044cSIan Rogers static int num_subtests(const struct test_suite *t)
119f832044cSIan Rogers {
12078244d2eSIan Rogers 	int num;
12178244d2eSIan Rogers 
12278244d2eSIan Rogers 	if (!t->test_cases)
123f832044cSIan Rogers 		return 0;
12478244d2eSIan Rogers 
12578244d2eSIan Rogers 	num = 0;
12678244d2eSIan Rogers 	while (t->test_cases[num].name)
12778244d2eSIan Rogers 		num++;
12878244d2eSIan Rogers 
12978244d2eSIan Rogers 	return num;
130f832044cSIan Rogers }
131f832044cSIan Rogers 
132f832044cSIan Rogers static bool has_subtests(const struct test_suite *t)
133f832044cSIan Rogers {
13494e11fc7SIan Rogers 	return num_subtests(t) > 1;
135f832044cSIan Rogers }
136f832044cSIan Rogers 
137f832044cSIan Rogers static const char *skip_reason(const struct test_suite *t, int subtest)
138f832044cSIan Rogers {
1399be56d30SIan Rogers 	if (t->test_cases && subtest >= 0)
1409be56d30SIan Rogers 		return t->test_cases[subtest].skip_reason;
1419be56d30SIan Rogers 
142f832044cSIan Rogers 	return NULL;
143f832044cSIan Rogers }
144f832044cSIan Rogers 
145f832044cSIan Rogers static const char *test_description(const struct test_suite *t, int subtest)
146f832044cSIan Rogers {
14778244d2eSIan Rogers 	if (t->test_cases && subtest >= 0)
14878244d2eSIan Rogers 		return t->test_cases[subtest].desc;
149f832044cSIan Rogers 
15078244d2eSIan Rogers 	return t->desc;
151f832044cSIan Rogers }
152f832044cSIan Rogers 
15378244d2eSIan Rogers static test_fnptr test_function(const struct test_suite *t, int subtest)
154f832044cSIan Rogers {
15578244d2eSIan Rogers 	if (subtest <= 0)
15678244d2eSIan Rogers 		return t->test_cases[0].run_case;
15778244d2eSIan Rogers 
15878244d2eSIan Rogers 	return t->test_cases[subtest].run_case;
159f832044cSIan Rogers }
160f832044cSIan Rogers 
161d685e6c1SJiri Olsa static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
162945aea22SJiri Olsa {
163945aea22SJiri Olsa 	int i;
164945aea22SJiri Olsa 
165945aea22SJiri Olsa 	if (argc == 0)
166945aea22SJiri Olsa 		return true;
167945aea22SJiri Olsa 
168945aea22SJiri Olsa 	for (i = 0; i < argc; ++i) {
169945aea22SJiri Olsa 		char *end;
170945aea22SJiri Olsa 		long nr = strtoul(argv[i], &end, 10);
171945aea22SJiri Olsa 
172945aea22SJiri Olsa 		if (*end == '\0') {
173945aea22SJiri Olsa 			if (nr == curr + 1)
174945aea22SJiri Olsa 				return true;
175945aea22SJiri Olsa 			continue;
176945aea22SJiri Olsa 		}
177945aea22SJiri Olsa 
178d685e6c1SJiri Olsa 		if (strcasestr(desc, argv[i]))
179945aea22SJiri Olsa 			return true;
180945aea22SJiri Olsa 	}
181945aea22SJiri Olsa 
182945aea22SJiri Olsa 	return false;
183945aea22SJiri Olsa }
184945aea22SJiri Olsa 
18533f44bfdSIan Rogers static int run_test(struct test_suite *test, int subtest)
1860d8a5faaSJiri Olsa {
1877fa9b8fbSJiri Olsa 	int status, err = -1, child = dont_fork ? 0 : fork();
188ba3dfff8SMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE];
1890d8a5faaSJiri Olsa 
1900d8a5faaSJiri Olsa 	if (child < 0) {
191ba3dfff8SMasami Hiramatsu 		pr_err("failed to fork test: %s\n",
192c8b5f2c9SArnaldo Carvalho de Melo 			str_error_r(errno, sbuf, sizeof(sbuf)));
1930d8a5faaSJiri Olsa 		return -1;
1940d8a5faaSJiri Olsa 	}
1950d8a5faaSJiri Olsa 
1960d8a5faaSJiri Olsa 	if (!child) {
1977fa9b8fbSJiri Olsa 		if (!dont_fork) {
1980d8a5faaSJiri Olsa 			pr_debug("test child forked, pid %d\n", getpid());
1997fa9b8fbSJiri Olsa 
200bb963e16SNamhyung Kim 			if (verbose <= 0) {
2015bcf2fe0SWang Nan 				int nullfd = open("/dev/null", O_WRONLY);
2027fa9b8fbSJiri Olsa 
2035bcf2fe0SWang Nan 				if (nullfd >= 0) {
2045bcf2fe0SWang Nan 					close(STDERR_FILENO);
2055bcf2fe0SWang Nan 					close(STDOUT_FILENO);
2065bcf2fe0SWang Nan 
2075bcf2fe0SWang Nan 					dup2(nullfd, STDOUT_FILENO);
2085bcf2fe0SWang Nan 					dup2(STDOUT_FILENO, STDERR_FILENO);
2095bcf2fe0SWang Nan 					close(nullfd);
2105bcf2fe0SWang Nan 				}
211b6847d2cSArnaldo Carvalho de Melo 			} else {
212b6847d2cSArnaldo Carvalho de Melo 				signal(SIGSEGV, sighandler_dump_stack);
213b6847d2cSArnaldo Carvalho de Melo 				signal(SIGFPE, sighandler_dump_stack);
2145bcf2fe0SWang Nan 			}
2157fa9b8fbSJiri Olsa 		}
2165bcf2fe0SWang Nan 
217f832044cSIan Rogers 		err = test_function(test, subtest)(test, subtest);
2187fa9b8fbSJiri Olsa 		if (!dont_fork)
2190d8a5faaSJiri Olsa 			exit(err);
2200d8a5faaSJiri Olsa 	}
2210d8a5faaSJiri Olsa 
2227fa9b8fbSJiri Olsa 	if (!dont_fork) {
2230d8a5faaSJiri Olsa 		wait(&status);
2240d8a5faaSJiri Olsa 
2250d8a5faaSJiri Olsa 		if (WIFEXITED(status)) {
226189c466fSHe Kuang 			err = (signed char)WEXITSTATUS(status);
2270d8a5faaSJiri Olsa 			pr_debug("test child finished with %d\n", err);
2280d8a5faaSJiri Olsa 		} else if (WIFSIGNALED(status)) {
2290d8a5faaSJiri Olsa 			err = -1;
2300d8a5faaSJiri Olsa 			pr_debug("test child interrupted\n");
2310d8a5faaSJiri Olsa 		}
2327fa9b8fbSJiri Olsa 	}
2330d8a5faaSJiri Olsa 
2340d8a5faaSJiri Olsa 	return err;
2350d8a5faaSJiri Olsa }
2360d8a5faaSJiri Olsa 
237df225205SIan Rogers #define for_each_test(j, k, t)			\
23831b6753fSMatt Fleming 	for (j = 0; j < ARRAY_SIZE(tests); j++)	\
239df225205SIan Rogers 		for (k = 0, t = tests[j][k]; tests[j][k]; k++, t = tests[j][k])
240e8210cefSMatt Fleming 
241*b47d2fb4SIan Rogers static int test_and_print(struct test_suite *t, int subtest)
242e8c6d500SWang Nan {
243e8c6d500SWang Nan 	int err;
244e8c6d500SWang Nan 
245e8c6d500SWang Nan 	pr_debug("\n--- start ---\n");
246e8c6d500SWang Nan 	err = run_test(t, subtest);
247e8c6d500SWang Nan 	pr_debug("---- end ----\n");
248e8c6d500SWang Nan 
249f832044cSIan Rogers 	if (!has_subtests(t))
250e8c6d500SWang Nan 		pr_debug("%s:", t->desc);
251e8c6d500SWang Nan 	else
252aa90f9f9SSandipan Das 		pr_debug("%s subtest %d:", t->desc, subtest + 1);
253e8c6d500SWang Nan 
254e8c6d500SWang Nan 	switch (err) {
255e8c6d500SWang Nan 	case TEST_OK:
256e8c6d500SWang Nan 		pr_info(" Ok\n");
257e8c6d500SWang Nan 		break;
2583b536651SIan Rogers 	case TEST_SKIP: {
259f832044cSIan Rogers 		const char *reason = skip_reason(t, subtest);
260f832044cSIan Rogers 
261f832044cSIan Rogers 		if (reason)
262f832044cSIan Rogers 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
2633b536651SIan Rogers 		else
264e8c6d500SWang Nan 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
2653b536651SIan Rogers 	}
266e8c6d500SWang Nan 		break;
267e8c6d500SWang Nan 	case TEST_FAIL:
268e8c6d500SWang Nan 	default:
269e8c6d500SWang Nan 		color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
270e8c6d500SWang Nan 		break;
271e8c6d500SWang Nan 	}
272e8c6d500SWang Nan 
273e8c6d500SWang Nan 	return err;
274e8c6d500SWang Nan }
275e8c6d500SWang Nan 
2761209b273SArnaldo Carvalho de Melo static const char *shell_test__description(char *description, size_t size,
2771209b273SArnaldo Carvalho de Melo 					   const char *path, const char *name)
2781209b273SArnaldo Carvalho de Melo {
2791209b273SArnaldo Carvalho de Melo 	FILE *fp;
2801209b273SArnaldo Carvalho de Melo 	char filename[PATH_MAX];
2811209b273SArnaldo Carvalho de Melo 
2821209b273SArnaldo Carvalho de Melo 	path__join(filename, sizeof(filename), path, name);
2831209b273SArnaldo Carvalho de Melo 	fp = fopen(filename, "r");
2841209b273SArnaldo Carvalho de Melo 	if (!fp)
2851209b273SArnaldo Carvalho de Melo 		return NULL;
2861209b273SArnaldo Carvalho de Melo 
2874eaf97e8SMichael Petlan 	/* Skip shebang */
2884eaf97e8SMichael Petlan 	while (fgetc(fp) != '\n');
2894eaf97e8SMichael Petlan 
2901209b273SArnaldo Carvalho de Melo 	description = fgets(description, size, fp);
2911209b273SArnaldo Carvalho de Melo 	fclose(fp);
2921209b273SArnaldo Carvalho de Melo 
2933ca43b60SArnaldo Carvalho de Melo 	return description ? strim(description + 1) : NULL;
2941209b273SArnaldo Carvalho de Melo }
2951209b273SArnaldo Carvalho de Melo 
296da963834SRiccardo Mancini #define for_each_shell_test(entlist, nr, base, ent)	                \
297da963834SRiccardo Mancini 	for (int __i = 0; __i < nr && (ent = entlist[__i]); __i++)	\
298db8fec58SKim Phillips 		if (!is_directory(base, ent) && ent->d_name[0] != '.')
2991209b273SArnaldo Carvalho de Melo 
3001209b273SArnaldo Carvalho de Melo static const char *shell_tests__dir(char *path, size_t size)
3011209b273SArnaldo Carvalho de Melo {
3021209b273SArnaldo Carvalho de Melo 	const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
3031209b273SArnaldo Carvalho de Melo         char *exec_path;
3041209b273SArnaldo Carvalho de Melo 	unsigned int i;
3051209b273SArnaldo Carvalho de Melo 
3061209b273SArnaldo Carvalho de Melo 	for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
3071209b273SArnaldo Carvalho de Melo 		struct stat st;
3081209b273SArnaldo Carvalho de Melo 		if (!lstat(devel_dirs[i], &st)) {
3091209b273SArnaldo Carvalho de Melo 			scnprintf(path, size, "%s/shell", devel_dirs[i]);
3101209b273SArnaldo Carvalho de Melo 			if (!lstat(devel_dirs[i], &st))
3111209b273SArnaldo Carvalho de Melo 				return path;
3121209b273SArnaldo Carvalho de Melo 		}
3131209b273SArnaldo Carvalho de Melo 	}
3141209b273SArnaldo Carvalho de Melo 
3151209b273SArnaldo Carvalho de Melo         /* Then installed path. */
3161209b273SArnaldo Carvalho de Melo         exec_path = get_argv_exec_path();
3171209b273SArnaldo Carvalho de Melo         scnprintf(path, size, "%s/tests/shell", exec_path);
3181209b273SArnaldo Carvalho de Melo 	free(exec_path);
3191209b273SArnaldo Carvalho de Melo 	return path;
3201209b273SArnaldo Carvalho de Melo }
3211209b273SArnaldo Carvalho de Melo 
3221209b273SArnaldo Carvalho de Melo static int shell_tests__max_desc_width(void)
3231209b273SArnaldo Carvalho de Melo {
324da963834SRiccardo Mancini 	struct dirent **entlist;
3251209b273SArnaldo Carvalho de Melo 	struct dirent *ent;
326eb7261f1SRiccardo Mancini 	int n_dirs, e;
3271209b273SArnaldo Carvalho de Melo 	char path_dir[PATH_MAX];
3281209b273SArnaldo Carvalho de Melo 	const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
3291209b273SArnaldo Carvalho de Melo 	int width = 0;
3301209b273SArnaldo Carvalho de Melo 
3311209b273SArnaldo Carvalho de Melo 	if (path == NULL)
3321209b273SArnaldo Carvalho de Melo 		return -1;
3331209b273SArnaldo Carvalho de Melo 
334da963834SRiccardo Mancini 	n_dirs = scandir(path, &entlist, NULL, alphasort);
335da963834SRiccardo Mancini 	if (n_dirs == -1)
3361209b273SArnaldo Carvalho de Melo 		return -1;
3371209b273SArnaldo Carvalho de Melo 
338da963834SRiccardo Mancini 	for_each_shell_test(entlist, n_dirs, path, ent) {
3391209b273SArnaldo Carvalho de Melo 		char bf[256];
3401209b273SArnaldo Carvalho de Melo 		const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
3411209b273SArnaldo Carvalho de Melo 
3421209b273SArnaldo Carvalho de Melo 		if (desc) {
3431209b273SArnaldo Carvalho de Melo 			int len = strlen(desc);
3441209b273SArnaldo Carvalho de Melo 
3451209b273SArnaldo Carvalho de Melo 			if (width < len)
3461209b273SArnaldo Carvalho de Melo 				width = len;
3471209b273SArnaldo Carvalho de Melo 		}
3481209b273SArnaldo Carvalho de Melo 	}
3491209b273SArnaldo Carvalho de Melo 
350eb7261f1SRiccardo Mancini 	for (e = 0; e < n_dirs; e++)
351eb7261f1SRiccardo Mancini 		zfree(&entlist[e]);
352da963834SRiccardo Mancini 	free(entlist);
3531209b273SArnaldo Carvalho de Melo 	return width;
3541209b273SArnaldo Carvalho de Melo }
3551209b273SArnaldo Carvalho de Melo 
3561209b273SArnaldo Carvalho de Melo struct shell_test {
3571209b273SArnaldo Carvalho de Melo 	const char *dir;
3581209b273SArnaldo Carvalho de Melo 	const char *file;
3591209b273SArnaldo Carvalho de Melo };
3601209b273SArnaldo Carvalho de Melo 
36133f44bfdSIan Rogers static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
3621209b273SArnaldo Carvalho de Melo {
3631209b273SArnaldo Carvalho de Melo 	int err;
3641209b273SArnaldo Carvalho de Melo 	char script[PATH_MAX];
3651209b273SArnaldo Carvalho de Melo 	struct shell_test *st = test->priv;
3661209b273SArnaldo Carvalho de Melo 
367e3c9cfd0SIan Rogers 	path__join(script, sizeof(script) - 3, st->dir, st->file);
368e3c9cfd0SIan Rogers 
369e3c9cfd0SIan Rogers 	if (verbose)
370e3c9cfd0SIan Rogers 		strncat(script, " -v", sizeof(script) - strlen(script) - 1);
3711209b273SArnaldo Carvalho de Melo 
3721209b273SArnaldo Carvalho de Melo 	err = system(script);
3731209b273SArnaldo Carvalho de Melo 	if (!err)
3741209b273SArnaldo Carvalho de Melo 		return TEST_OK;
3751209b273SArnaldo Carvalho de Melo 
3761209b273SArnaldo Carvalho de Melo 	return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
3771209b273SArnaldo Carvalho de Melo }
3781209b273SArnaldo Carvalho de Melo 
379ebdf90a4SRiccardo Mancini static int run_shell_tests(int argc, const char *argv[], int i, int width,
380ebdf90a4SRiccardo Mancini 				struct intlist *skiplist)
3811209b273SArnaldo Carvalho de Melo {
382da963834SRiccardo Mancini 	struct dirent **entlist;
3831209b273SArnaldo Carvalho de Melo 	struct dirent *ent;
384eb7261f1SRiccardo Mancini 	int n_dirs, e;
3851209b273SArnaldo Carvalho de Melo 	char path_dir[PATH_MAX];
3861209b273SArnaldo Carvalho de Melo 	struct shell_test st = {
3871209b273SArnaldo Carvalho de Melo 		.dir = shell_tests__dir(path_dir, sizeof(path_dir)),
3881209b273SArnaldo Carvalho de Melo 	};
3891209b273SArnaldo Carvalho de Melo 
3901209b273SArnaldo Carvalho de Melo 	if (st.dir == NULL)
3911209b273SArnaldo Carvalho de Melo 		return -1;
3921209b273SArnaldo Carvalho de Melo 
393da963834SRiccardo Mancini 	n_dirs = scandir(st.dir, &entlist, NULL, alphasort);
394da963834SRiccardo Mancini 	if (n_dirs == -1) {
395b2bf6660SIan Rogers 		pr_err("failed to open shell test directory: %s\n",
396b2bf6660SIan Rogers 			st.dir);
3971209b273SArnaldo Carvalho de Melo 		return -1;
398b2bf6660SIan Rogers 	}
3991209b273SArnaldo Carvalho de Melo 
400da963834SRiccardo Mancini 	for_each_shell_test(entlist, n_dirs, st.dir, ent) {
4011209b273SArnaldo Carvalho de Melo 		int curr = i++;
4021209b273SArnaldo Carvalho de Melo 		char desc[256];
403c76ec1cfSIan Rogers 		struct test_case test_cases[] = {
404c76ec1cfSIan Rogers 			{
405c76ec1cfSIan Rogers 				.desc = shell_test__description(desc,
406c76ec1cfSIan Rogers 								sizeof(desc),
407c76ec1cfSIan Rogers 								st.dir,
408c76ec1cfSIan Rogers 								ent->d_name),
409c76ec1cfSIan Rogers 				.run_case = shell_test__run,
410c76ec1cfSIan Rogers 			},
411c76ec1cfSIan Rogers 			{ .name = NULL, }
412c76ec1cfSIan Rogers 		};
413c76ec1cfSIan Rogers 		struct test_suite test_suite = {
414c76ec1cfSIan Rogers 			.desc = test_cases[0].desc,
415c76ec1cfSIan Rogers 			.test_cases = test_cases,
4161209b273SArnaldo Carvalho de Melo 			.priv = &st,
4171209b273SArnaldo Carvalho de Melo 		};
4181209b273SArnaldo Carvalho de Melo 
419c76ec1cfSIan Rogers 		if (!perf_test__matches(test_suite.desc, curr, argc, argv))
4201209b273SArnaldo Carvalho de Melo 			continue;
4211209b273SArnaldo Carvalho de Melo 
4221209b273SArnaldo Carvalho de Melo 		st.file = ent->d_name;
423c76ec1cfSIan Rogers 		pr_info("%2d: %-*s:", i, width, test_suite.desc);
424ebdf90a4SRiccardo Mancini 
425ebdf90a4SRiccardo Mancini 		if (intlist__find(skiplist, i)) {
426ebdf90a4SRiccardo Mancini 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
427ebdf90a4SRiccardo Mancini 			continue;
428ebdf90a4SRiccardo Mancini 		}
429ebdf90a4SRiccardo Mancini 
430*b47d2fb4SIan Rogers 		test_and_print(&test_suite, 0);
4311209b273SArnaldo Carvalho de Melo 	}
4321209b273SArnaldo Carvalho de Melo 
433eb7261f1SRiccardo Mancini 	for (e = 0; e < n_dirs; e++)
434eb7261f1SRiccardo Mancini 		zfree(&entlist[e]);
435da963834SRiccardo Mancini 	free(entlist);
4361209b273SArnaldo Carvalho de Melo 	return 0;
4371209b273SArnaldo Carvalho de Melo }
4381209b273SArnaldo Carvalho de Melo 
4392ae82878SArnaldo Carvalho de Melo static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
440945aea22SJiri Olsa {
44133f44bfdSIan Rogers 	struct test_suite *t;
442df225205SIan Rogers 	unsigned int j, k;
443945aea22SJiri Olsa 	int i = 0;
4441209b273SArnaldo Carvalho de Melo 	int width = shell_tests__max_desc_width();
445945aea22SJiri Olsa 
446df225205SIan Rogers 	for_each_test(j, k, t) {
447f832044cSIan Rogers 		int len = strlen(test_description(t, -1));
448945aea22SJiri Olsa 
449945aea22SJiri Olsa 		if (width < len)
450945aea22SJiri Olsa 			width = len;
451945aea22SJiri Olsa 	}
452945aea22SJiri Olsa 
453df225205SIan Rogers 	for_each_test(j, k, t) {
454*b47d2fb4SIan Rogers 		int curr = i++;
455d685e6c1SJiri Olsa 		int subi;
456945aea22SJiri Olsa 
457f832044cSIan Rogers 		if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
458d685e6c1SJiri Olsa 			bool skip = true;
459d685e6c1SJiri Olsa 			int subn;
460d685e6c1SJiri Olsa 
461f832044cSIan Rogers 			subn = num_subtests(t);
462d685e6c1SJiri Olsa 
463d685e6c1SJiri Olsa 			for (subi = 0; subi < subn; subi++) {
464f832044cSIan Rogers 				if (perf_test__matches(test_description(t, subi),
465f832044cSIan Rogers 							curr, argc, argv))
466d685e6c1SJiri Olsa 					skip = false;
467d685e6c1SJiri Olsa 			}
468d685e6c1SJiri Olsa 
469d685e6c1SJiri Olsa 			if (skip)
470d685e6c1SJiri Olsa 				continue;
471d685e6c1SJiri Olsa 		}
472d685e6c1SJiri Olsa 
473f832044cSIan Rogers 		pr_info("%2d: %-*s:", i, width, test_description(t, -1));
4742ae82878SArnaldo Carvalho de Melo 
4752ae82878SArnaldo Carvalho de Melo 		if (intlist__find(skiplist, i)) {
4762ae82878SArnaldo Carvalho de Melo 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
4772ae82878SArnaldo Carvalho de Melo 			continue;
4782ae82878SArnaldo Carvalho de Melo 		}
4792ae82878SArnaldo Carvalho de Melo 
480f832044cSIan Rogers 		if (!has_subtests(t)) {
481*b47d2fb4SIan Rogers 			test_and_print(t, -1);
482e8c6d500SWang Nan 		} else {
483f832044cSIan Rogers 			int subn = num_subtests(t);
484e8c6d500SWang Nan 			/*
485e8c6d500SWang Nan 			 * minus 2 to align with normal testcases.
486e8c6d500SWang Nan 			 * For subtest we print additional '.x' in number.
487e8c6d500SWang Nan 			 * for example:
488e8c6d500SWang Nan 			 *
489e8c6d500SWang Nan 			 * 35: Test LLVM searching and compiling                        :
490e8c6d500SWang Nan 			 * 35.1: Basic BPF llvm compiling test                          : Ok
491e8c6d500SWang Nan 			 */
492e8c6d500SWang Nan 			int subw = width > 2 ? width - 2 : width;
493f4c1ea5fSJiri Olsa 
494e8c6d500SWang Nan 			if (subn <= 0) {
495e8c6d500SWang Nan 				color_fprintf(stderr, PERF_COLOR_YELLOW,
496e8c6d500SWang Nan 					      " Skip (not compiled in)\n");
497e8c6d500SWang Nan 				continue;
498e8c6d500SWang Nan 			}
499e8c6d500SWang Nan 			pr_info("\n");
500e8c6d500SWang Nan 
501e8c6d500SWang Nan 			for (subi = 0; subi < subn; subi++) {
502f832044cSIan Rogers 				int len = strlen(test_description(t, subi));
503e8c6d500SWang Nan 
504e8c6d500SWang Nan 				if (subw < len)
505e8c6d500SWang Nan 					subw = len;
506e8c6d500SWang Nan 			}
507e8c6d500SWang Nan 
508e8c6d500SWang Nan 			for (subi = 0; subi < subn; subi++) {
509f832044cSIan Rogers 				if (!perf_test__matches(test_description(t, subi),
510f832044cSIan Rogers 							curr, argc, argv))
511d685e6c1SJiri Olsa 					continue;
512d685e6c1SJiri Olsa 
513e8c6d500SWang Nan 				pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
514f832044cSIan Rogers 					test_description(t, subi));
515*b47d2fb4SIan Rogers 				test_and_print(t, subi);
516e8c6d500SWang Nan 			}
517f4c1ea5fSJiri Olsa 		}
518945aea22SJiri Olsa 	}
519945aea22SJiri Olsa 
520ebdf90a4SRiccardo Mancini 	return run_shell_tests(argc, argv, i, width, skiplist);
5211209b273SArnaldo Carvalho de Melo }
5221209b273SArnaldo Carvalho de Melo 
5231209b273SArnaldo Carvalho de Melo static int perf_test__list_shell(int argc, const char **argv, int i)
5241209b273SArnaldo Carvalho de Melo {
525da963834SRiccardo Mancini 	struct dirent **entlist;
5261209b273SArnaldo Carvalho de Melo 	struct dirent *ent;
527eb7261f1SRiccardo Mancini 	int n_dirs, e;
5281209b273SArnaldo Carvalho de Melo 	char path_dir[PATH_MAX];
5291209b273SArnaldo Carvalho de Melo 	const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
5301209b273SArnaldo Carvalho de Melo 
5311209b273SArnaldo Carvalho de Melo 	if (path == NULL)
5321209b273SArnaldo Carvalho de Melo 		return -1;
5331209b273SArnaldo Carvalho de Melo 
534da963834SRiccardo Mancini 	n_dirs = scandir(path, &entlist, NULL, alphasort);
535da963834SRiccardo Mancini 	if (n_dirs == -1)
5361209b273SArnaldo Carvalho de Melo 		return -1;
5371209b273SArnaldo Carvalho de Melo 
538da963834SRiccardo Mancini 	for_each_shell_test(entlist, n_dirs, path, ent) {
5396d02acc1SArnaldo Carvalho de Melo 		int curr = i++;
5401209b273SArnaldo Carvalho de Melo 		char bf[256];
54133f44bfdSIan Rogers 		struct test_suite t = {
5426d02acc1SArnaldo Carvalho de Melo 			.desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
5436d02acc1SArnaldo Carvalho de Melo 		};
5441209b273SArnaldo Carvalho de Melo 
545d685e6c1SJiri Olsa 		if (!perf_test__matches(t.desc, curr, argc, argv))
5461209b273SArnaldo Carvalho de Melo 			continue;
5471209b273SArnaldo Carvalho de Melo 
5486d02acc1SArnaldo Carvalho de Melo 		pr_info("%2d: %s\n", i, t.desc);
549eb7261f1SRiccardo Mancini 
5501209b273SArnaldo Carvalho de Melo 	}
5511209b273SArnaldo Carvalho de Melo 
552eb7261f1SRiccardo Mancini 	for (e = 0; e < n_dirs; e++)
553eb7261f1SRiccardo Mancini 		zfree(&entlist[e]);
554da963834SRiccardo Mancini 	free(entlist);
555945aea22SJiri Olsa 	return 0;
556945aea22SJiri Olsa }
557945aea22SJiri Olsa 
558945aea22SJiri Olsa static int perf_test__list(int argc, const char **argv)
559945aea22SJiri Olsa {
560df225205SIan Rogers 	unsigned int j, k;
56133f44bfdSIan Rogers 	struct test_suite *t;
562945aea22SJiri Olsa 	int i = 0;
563945aea22SJiri Olsa 
564df225205SIan Rogers 	for_each_test(j, k, t) {
5656d02acc1SArnaldo Carvalho de Melo 		int curr = i++;
56628765bf2SArnaldo Carvalho de Melo 
567848ddf59SIan Rogers 		if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
568945aea22SJiri Olsa 			continue;
569945aea22SJiri Olsa 
570f832044cSIan Rogers 		pr_info("%2d: %s\n", i, test_description(t, -1));
571ea40b6d3SHendrik Brueckner 
572f832044cSIan Rogers 		if (has_subtests(t)) {
573f832044cSIan Rogers 			int subn = num_subtests(t);
574ea40b6d3SHendrik Brueckner 			int subi;
575ea40b6d3SHendrik Brueckner 
576ea40b6d3SHendrik Brueckner 			for (subi = 0; subi < subn; subi++)
577ea40b6d3SHendrik Brueckner 				pr_info("%2d:%1d: %s\n", i, subi + 1,
578f832044cSIan Rogers 					test_description(t, subi));
579ea40b6d3SHendrik Brueckner 		}
580945aea22SJiri Olsa 	}
581945aea22SJiri Olsa 
5821209b273SArnaldo Carvalho de Melo 	perf_test__list_shell(argc, argv, i);
5831209b273SArnaldo Carvalho de Melo 
584945aea22SJiri Olsa 	return 0;
585945aea22SJiri Olsa }
586945aea22SJiri Olsa 
587b0ad8ea6SArnaldo Carvalho de Melo int cmd_test(int argc, const char **argv)
588945aea22SJiri Olsa {
5891f9975f1SYunlong Song 	const char *test_usage[] = {
590945aea22SJiri Olsa 	"perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
591945aea22SJiri Olsa 	NULL,
592945aea22SJiri Olsa 	};
5932ae82878SArnaldo Carvalho de Melo 	const char *skip = NULL;
594945aea22SJiri Olsa 	const struct option test_options[] = {
5952ae82878SArnaldo Carvalho de Melo 	OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
596945aea22SJiri Olsa 	OPT_INCR('v', "verbose", &verbose,
597945aea22SJiri Olsa 		    "be more verbose (show symbol address, etc)"),
5987fa9b8fbSJiri Olsa 	OPT_BOOLEAN('F', "dont-fork", &dont_fork,
5997fa9b8fbSJiri Olsa 		    "Do not fork for testcase"),
600945aea22SJiri Olsa 	OPT_END()
601945aea22SJiri Olsa 	};
6021f9975f1SYunlong Song 	const char * const test_subcommands[] = { "list", NULL };
6032ae82878SArnaldo Carvalho de Melo 	struct intlist *skiplist = NULL;
604a635fc51SArnaldo Carvalho de Melo         int ret = hists__init();
605a635fc51SArnaldo Carvalho de Melo 
606a635fc51SArnaldo Carvalho de Melo         if (ret < 0)
607a635fc51SArnaldo Carvalho de Melo                 return ret;
608945aea22SJiri Olsa 
6091f9975f1SYunlong Song 	argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
610945aea22SJiri Olsa 	if (argc >= 1 && !strcmp(argv[0], "list"))
6116d02acc1SArnaldo Carvalho de Melo 		return perf_test__list(argc - 1, argv + 1);
612945aea22SJiri Olsa 
613945aea22SJiri Olsa 	symbol_conf.priv_size = sizeof(int);
614945aea22SJiri Olsa 	symbol_conf.sort_by_name = true;
615945aea22SJiri Olsa 	symbol_conf.try_vmlinux_path = true;
616945aea22SJiri Olsa 
6170a7e6d1bSNamhyung Kim 	if (symbol__init(NULL) < 0)
618945aea22SJiri Olsa 		return -1;
619945aea22SJiri Olsa 
6202ae82878SArnaldo Carvalho de Melo 	if (skip != NULL)
6212ae82878SArnaldo Carvalho de Melo 		skiplist = intlist__new(skip);
622d3280ce0SArnaldo Carvalho de Melo 	/*
623d3280ce0SArnaldo Carvalho de Melo 	 * Tests that create BPF maps, for instance, need more than the 64K
624d3280ce0SArnaldo Carvalho de Melo 	 * default:
625d3280ce0SArnaldo Carvalho de Melo 	 */
626d3280ce0SArnaldo Carvalho de Melo 	rlimit__bump_memlock();
6272ae82878SArnaldo Carvalho de Melo 
6282ae82878SArnaldo Carvalho de Melo 	return __cmd_test(argc, argv, skiplist);
629945aea22SJiri Olsa }
630