blob: 8cb5a1c3489eafc822f574691336c164db115238 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -03002/*
3 * builtin-test.c
4 *
5 * Builtin regression testing command: ever growing number of sanity tests
6 */
Arnaldo Carvalho de Meloc23c2a02017-09-11 10:50:26 -03007#include <fcntl.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03008#include <errno.h>
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +02009#include <unistd.h>
10#include <string.h>
Arnaldo Carvalho de Melof2a39fe2019-08-30 14:45:20 -030011#include <stdlib.h>
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -030012#include <sys/types.h>
13#include <dirent.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030014#include <sys/wait.h>
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -030015#include <sys/stat.h>
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -030016#include "builtin.h"
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -030017#include "hist.h"
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -030018#include "intlist.h"
Jiri Olsa0a4e1ae2012-11-10 01:46:41 +010019#include "tests.h"
Jiri Olsac81251e2012-11-10 01:46:51 +010020#include "debug.h"
21#include "color.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060022#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -030023#include "string2.h"
Jiri Olsac81251e2012-11-10 01:46:51 +010024#include "symbol.h"
Arnaldo Carvalho de Melod3280ce2019-07-09 16:27:01 -030025#include "util/rlimit.h"
Arnaldo Carvalho de Melo877a7a12017-04-17 11:39:06 -030026#include <linux/kernel.h>
Arnaldo Carvalho de Melo3ca43b62019-06-26 12:06:20 -030027#include <linux/string.h>
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -030028#include <subcmd/exec-cmd.h>
Riccardo Mancinieb7261f2021-07-09 18:34:53 +020029#include <linux/zalloc.h>
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -020030
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +020031static bool dont_fork;
32
Ian Rogers33f44bf2021-11-03 23:41:51 -070033struct test_suite *__weak arch_tests[] = {
Ian Rogersdf225202021-11-03 23:41:49 -070034 NULL,
Matt Fleming31b67532015-10-05 15:40:19 +010035};
36
Ian Rogers33f44bf2021-11-03 23:41:51 -070037static struct test_suite *generic_tests[] = {
Ian Rogersd68f0362021-11-03 23:41:50 -070038 &suite__vmlinux_matches_kallsyms,
39 &suite__openat_syscall_event,
40 &suite__openat_syscall_event_on_all_cpus,
41 &suite__basic_mmap,
42 &suite__mem,
43 &suite__parse_events,
44 &suite__expr,
45 &suite__PERF_RECORD,
46 &suite__pmu,
47 &suite__pmu_events,
48 &suite__dso_data,
49 &suite__dso_data_cache,
50 &suite__dso_data_reopen,
51 &suite__perf_evsel__roundtrip_name_test,
52 &suite__perf_evsel__tp_sched_test,
53 &suite__syscall_openat_tp_fields,
54 &suite__attr,
55 &suite__hists_link,
56 &suite__python_use,
57 &suite__bp_signal,
58 &suite__bp_signal_overflow,
59 &suite__bp_accounting,
60 &suite__wp,
61 &suite__task_exit,
62 &suite__sw_clock_freq,
63 &suite__code_reading,
64 &suite__sample_parsing,
65 &suite__keep_tracking,
66 &suite__parse_no_sample_id_all,
67 &suite__hists_filter,
68 &suite__mmap_thread_lookup,
69 &suite__thread_maps_share,
70 &suite__hists_output,
71 &suite__hists_cumulate,
72 &suite__switch_tracking,
73 &suite__fdarray__filter,
74 &suite__fdarray__add,
75 &suite__kmod_path__parse,
76 &suite__thread_map,
77 &suite__llvm,
78 &suite__session_topology,
79 &suite__bpf,
80 &suite__thread_map_synthesize,
81 &suite__thread_map_remove,
82 &suite__cpu_map_synthesize,
83 &suite__synthesize_stat_config,
84 &suite__synthesize_stat,
85 &suite__synthesize_stat_round,
86 &suite__event_update,
87 &suite__event_times,
88 &suite__backward_ring_buffer,
89 &suite__cpu_map_print,
90 &suite__cpu_map_merge,
91 &suite__sdt_event,
92 &suite__is_printable_array,
93 &suite__bitmap_print,
94 &suite__perf_hooks,
95 &suite__clang,
96 &suite__unit_number__scnprint,
97 &suite__mem2node,
98 &suite__time_utils,
99 &suite__jit_write_elf,
100 &suite__pfm,
101 &suite__api_io,
102 &suite__maps__merge_in,
103 &suite__demangle_java,
104 &suite__demangle_ocaml,
105 &suite__parse_metric,
106 &suite__pe_file_parsing,
107 &suite__expand_cgroup_events,
108 &suite__perf_time_to_tsc,
109 &suite__dlfilter,
Ian Rogersdf225202021-11-03 23:41:49 -0700110 NULL,
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300111};
112
Ian Rogers33f44bf2021-11-03 23:41:51 -0700113static struct test_suite **tests[] = {
Matt Fleming31b67532015-10-05 15:40:19 +0100114 generic_tests,
115 arch_tests,
116};
117
Ian Rogersf8320442021-11-03 23:41:52 -0700118static int num_subtests(const struct test_suite *t)
119{
Ian Rogers78244d22021-11-03 23:41:53 -0700120 int num;
121
Ian Rogers78244d22021-11-03 23:41:53 -0700122 if (!t->test_cases)
123 return 0;
124
125 num = 0;
126 while (t->test_cases[num].name)
127 num++;
128
129 return num;
Ian Rogersf8320442021-11-03 23:41:52 -0700130}
131
132static bool has_subtests(const struct test_suite *t)
133{
Ian Rogers94e11fc2021-11-03 23:42:01 -0700134 return num_subtests(t) > 1;
Ian Rogersf8320442021-11-03 23:41:52 -0700135}
136
137static const char *skip_reason(const struct test_suite *t, int subtest)
138{
Ian Rogers9be56d32021-11-03 23:41:54 -0700139 if (t->test_cases && subtest >= 0)
140 return t->test_cases[subtest].skip_reason;
141
Ian Rogersf8320442021-11-03 23:41:52 -0700142 return NULL;
143}
144
145static const char *test_description(const struct test_suite *t, int subtest)
146{
Ian Rogers78244d22021-11-03 23:41:53 -0700147 if (t->test_cases && subtest >= 0)
148 return t->test_cases[subtest].desc;
Ian Rogersf8320442021-11-03 23:41:52 -0700149
Ian Rogers78244d22021-11-03 23:41:53 -0700150 return t->desc;
Ian Rogersf8320442021-11-03 23:41:52 -0700151}
152
Ian Rogers78244d22021-11-03 23:41:53 -0700153static test_fnptr test_function(const struct test_suite *t, int subtest)
Ian Rogersf8320442021-11-03 23:41:52 -0700154{
Ian Rogers78244d22021-11-03 23:41:53 -0700155 if (subtest <= 0)
156 return t->test_cases[0].run_case;
157
158 return t->test_cases[subtest].run_case;
Ian Rogersf8320442021-11-03 23:41:52 -0700159}
160
Jiri Olsad685e6c2020-05-25 00:42:06 +0200161static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200162{
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
Jiri Olsad685e6c2020-05-25 00:42:06 +0200178 if (strcasestr(desc, argv[i]))
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200179 return true;
180 }
181
182 return false;
183}
184
Ian Rogers33f44bf2021-11-03 23:41:51 -0700185static int run_test(struct test_suite *test, int subtest)
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200186{
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200187 int status, err = -1, child = dont_fork ? 0 : fork();
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +0000188 char sbuf[STRERR_BUFSIZE];
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200189
190 if (child < 0) {
Masami Hiramatsuba3dfff2014-08-14 02:22:45 +0000191 pr_err("failed to fork test: %s\n",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300192 str_error_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200193 return -1;
194 }
195
196 if (!child) {
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200197 if (!dont_fork) {
198 pr_debug("test child forked, pid %d\n", getpid());
Wang Nan5bcf2fe2015-11-17 08:32:50 +0000199
Namhyung Kimbb963e12017-02-17 17:17:38 +0900200 if (verbose <= 0) {
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200201 int nullfd = open("/dev/null", O_WRONLY);
202
203 if (nullfd >= 0) {
204 close(STDERR_FILENO);
205 close(STDOUT_FILENO);
206
207 dup2(nullfd, STDOUT_FILENO);
208 dup2(STDOUT_FILENO, STDERR_FILENO);
209 close(nullfd);
210 }
211 } else {
212 signal(SIGSEGV, sighandler_dump_stack);
213 signal(SIGFPE, sighandler_dump_stack);
Wang Nan5bcf2fe2015-11-17 08:32:50 +0000214 }
215 }
216
Ian Rogersf8320442021-11-03 23:41:52 -0700217 err = test_function(test, subtest)(test, subtest);
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200218 if (!dont_fork)
219 exit(err);
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200220 }
221
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200222 if (!dont_fork) {
223 wait(&status);
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200224
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200225 if (WIFEXITED(status)) {
226 err = (signed char)WEXITSTATUS(status);
227 pr_debug("test child finished with %d\n", err);
228 } else if (WIFSIGNALED(status)) {
229 err = -1;
230 pr_debug("test child interrupted\n");
231 }
Jiri Olsa0d8a5fa2014-05-10 17:22:30 +0200232 }
233
234 return err;
235}
236
Ian Rogersdf225202021-11-03 23:41:49 -0700237#define for_each_test(j, k, t) \
Matt Fleming31b67532015-10-05 15:40:19 +0100238 for (j = 0; j < ARRAY_SIZE(tests); j++) \
Ian Rogersdf225202021-11-03 23:41:49 -0700239 for (k = 0, t = tests[j][k]; tests[j][k]; k++, t = tests[j][k])
Matt Fleminge8210ce2015-09-05 20:02:20 +0100240
Ian Rogersb47d2fb2021-11-03 23:42:08 -0700241static int test_and_print(struct test_suite *t, int subtest)
Wang Nane8c6d502015-11-17 08:32:48 +0000242{
243 int err;
244
Ian Rogersb47d2fb2021-11-03 23:42:08 -0700245 pr_debug("\n--- start ---\n");
246 err = run_test(t, subtest);
247 pr_debug("---- end ----\n");
Wang Nane8c6d502015-11-17 08:32:48 +0000248
Ian Rogersf8320442021-11-03 23:41:52 -0700249 if (!has_subtests(t))
Wang Nane8c6d502015-11-17 08:32:48 +0000250 pr_debug("%s:", t->desc);
251 else
Sandipan Dasaa90f9f2018-07-26 22:47:33 +0530252 pr_debug("%s subtest %d:", t->desc, subtest + 1);
Wang Nane8c6d502015-11-17 08:32:48 +0000253
254 switch (err) {
255 case TEST_OK:
256 pr_info(" Ok\n");
257 break;
Ian Rogers3b536652020-05-13 14:29:33 -0700258 case TEST_SKIP: {
Ian Rogersf8320442021-11-03 23:41:52 -0700259 const char *reason = skip_reason(t, subtest);
260
261 if (reason)
262 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
Ian Rogers3b536652020-05-13 14:29:33 -0700263 else
264 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
265 }
Wang Nane8c6d502015-11-17 08:32:48 +0000266 break;
267 case TEST_FAIL:
268 default:
269 color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
270 break;
271 }
272
273 return err;
274}
275
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300276static const char *shell_test__description(char *description, size_t size,
277 const char *path, const char *name)
278{
279 FILE *fp;
280 char filename[PATH_MAX];
281
282 path__join(filename, sizeof(filename), path, name);
283 fp = fopen(filename, "r");
284 if (!fp)
285 return NULL;
286
Michael Petlan4eaf97e2018-11-21 16:39:18 -0500287 /* Skip shebang */
288 while (fgetc(fp) != '\n');
289
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300290 description = fgets(description, size, fp);
291 fclose(fp);
292
Arnaldo Carvalho de Melo3ca43b62019-06-26 12:06:20 -0300293 return description ? strim(description + 1) : NULL;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300294}
295
Riccardo Mancinida963832021-05-26 01:05:17 +0200296#define for_each_shell_test(entlist, nr, base, ent) \
297 for (int __i = 0; __i < nr && (ent = entlist[__i]); __i++) \
Kim Phillipsdb8fec52018-06-29 12:46:58 -0500298 if (!is_directory(base, ent) && ent->d_name[0] != '.')
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300299
300static const char *shell_tests__dir(char *path, size_t size)
301{
302 const char *devel_dirs[] = { "./tools/perf/tests", "./tests", };
303 char *exec_path;
304 unsigned int i;
305
306 for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) {
307 struct stat st;
308 if (!lstat(devel_dirs[i], &st)) {
309 scnprintf(path, size, "%s/shell", devel_dirs[i]);
310 if (!lstat(devel_dirs[i], &st))
311 return path;
312 }
313 }
314
315 /* Then installed path. */
316 exec_path = get_argv_exec_path();
317 scnprintf(path, size, "%s/tests/shell", exec_path);
318 free(exec_path);
319 return path;
320}
321
322static int shell_tests__max_desc_width(void)
323{
Riccardo Mancinida963832021-05-26 01:05:17 +0200324 struct dirent **entlist;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300325 struct dirent *ent;
Riccardo Mancinieb7261f2021-07-09 18:34:53 +0200326 int n_dirs, e;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300327 char path_dir[PATH_MAX];
328 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
329 int width = 0;
330
331 if (path == NULL)
332 return -1;
333
Riccardo Mancinida963832021-05-26 01:05:17 +0200334 n_dirs = scandir(path, &entlist, NULL, alphasort);
335 if (n_dirs == -1)
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300336 return -1;
337
Riccardo Mancinida963832021-05-26 01:05:17 +0200338 for_each_shell_test(entlist, n_dirs, path, ent) {
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300339 char bf[256];
340 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name);
341
342 if (desc) {
343 int len = strlen(desc);
344
345 if (width < len)
346 width = len;
347 }
348 }
349
Riccardo Mancinieb7261f2021-07-09 18:34:53 +0200350 for (e = 0; e < n_dirs; e++)
351 zfree(&entlist[e]);
Riccardo Mancinida963832021-05-26 01:05:17 +0200352 free(entlist);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300353 return width;
354}
355
356struct shell_test {
357 const char *dir;
358 const char *file;
359};
360
Ian Rogers33f44bf2021-11-03 23:41:51 -0700361static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300362{
363 int err;
364 char script[PATH_MAX];
365 struct shell_test *st = test->priv;
366
Ian Rogerse3c9cfd2021-06-21 14:56:46 -0700367 path__join(script, sizeof(script) - 3, st->dir, st->file);
368
369 if (verbose)
370 strncat(script, " -v", sizeof(script) - strlen(script) - 1);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300371
372 err = system(script);
373 if (!err)
374 return TEST_OK;
375
376 return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
377}
378
Riccardo Manciniebdf90a42021-08-11 20:06:26 +0200379static int run_shell_tests(int argc, const char *argv[], int i, int width,
380 struct intlist *skiplist)
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300381{
Riccardo Mancinida963832021-05-26 01:05:17 +0200382 struct dirent **entlist;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300383 struct dirent *ent;
Riccardo Mancinieb7261f2021-07-09 18:34:53 +0200384 int n_dirs, e;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300385 char path_dir[PATH_MAX];
386 struct shell_test st = {
387 .dir = shell_tests__dir(path_dir, sizeof(path_dir)),
388 };
389
390 if (st.dir == NULL)
391 return -1;
392
Riccardo Mancinida963832021-05-26 01:05:17 +0200393 n_dirs = scandir(st.dir, &entlist, NULL, alphasort);
394 if (n_dirs == -1) {
Ian Rogersb2bf6662020-03-12 17:56:02 -0700395 pr_err("failed to open shell test directory: %s\n",
396 st.dir);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300397 return -1;
Ian Rogersb2bf6662020-03-12 17:56:02 -0700398 }
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300399
Riccardo Mancinida963832021-05-26 01:05:17 +0200400 for_each_shell_test(entlist, n_dirs, st.dir, ent) {
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300401 int curr = i++;
402 char desc[256];
Ian Rogersc76ec1c2021-11-03 23:42:04 -0700403 struct test_case test_cases[] = {
404 {
405 .desc = shell_test__description(desc,
406 sizeof(desc),
407 st.dir,
408 ent->d_name),
409 .run_case = shell_test__run,
410 },
411 { .name = NULL, }
412 };
413 struct test_suite test_suite = {
414 .desc = test_cases[0].desc,
415 .test_cases = test_cases,
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300416 .priv = &st,
417 };
418
Ian Rogersc76ec1c2021-11-03 23:42:04 -0700419 if (!perf_test__matches(test_suite.desc, curr, argc, argv))
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300420 continue;
421
422 st.file = ent->d_name;
Ian Rogersc76ec1c2021-11-03 23:42:04 -0700423 pr_info("%2d: %-*s:", i, width, test_suite.desc);
Riccardo Manciniebdf90a42021-08-11 20:06:26 +0200424
425 if (intlist__find(skiplist, i)) {
426 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
427 continue;
428 }
429
Ian Rogersb47d2fb2021-11-03 23:42:08 -0700430 test_and_print(&test_suite, 0);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300431 }
432
Riccardo Mancinieb7261f2021-07-09 18:34:53 +0200433 for (e = 0; e < n_dirs; e++)
434 zfree(&entlist[e]);
Riccardo Mancinida963832021-05-26 01:05:17 +0200435 free(entlist);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300436 return 0;
437}
438
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300439static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300440{
Ian Rogers33f44bf2021-11-03 23:41:51 -0700441 struct test_suite *t;
Ian Rogersdf225202021-11-03 23:41:49 -0700442 unsigned int j, k;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300443 int i = 0;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300444 int width = shell_tests__max_desc_width();
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300445
Ian Rogersdf225202021-11-03 23:41:49 -0700446 for_each_test(j, k, t) {
Ian Rogersf8320442021-11-03 23:41:52 -0700447 int len = strlen(test_description(t, -1));
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200448
449 if (width < len)
450 width = len;
Arnaldo Carvalho de Melo9a8e85a2012-10-24 15:44:41 -0200451 }
Jiri Olsa945aea22012-10-30 23:01:43 +0100452
Ian Rogersdf225202021-11-03 23:41:49 -0700453 for_each_test(j, k, t) {
Ian Rogersb47d2fb2021-11-03 23:42:08 -0700454 int curr = i++;
Jiri Olsad685e6c2020-05-25 00:42:06 +0200455 int subi;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200456
Ian Rogersf8320442021-11-03 23:41:52 -0700457 if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
Jiri Olsad685e6c2020-05-25 00:42:06 +0200458 bool skip = true;
459 int subn;
460
Ian Rogersf8320442021-11-03 23:41:52 -0700461 subn = num_subtests(t);
Jiri Olsad685e6c2020-05-25 00:42:06 +0200462
463 for (subi = 0; subi < subn; subi++) {
Ian Rogersf8320442021-11-03 23:41:52 -0700464 if (perf_test__matches(test_description(t, subi),
465 curr, argc, argv))
Jiri Olsad685e6c2020-05-25 00:42:06 +0200466 skip = false;
467 }
468
469 if (skip)
470 continue;
471 }
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200472
Ian Rogersf8320442021-11-03 23:41:52 -0700473 pr_info("%2d: %-*s:", i, width, test_description(t, -1));
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300474
475 if (intlist__find(skiplist, i)) {
476 color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
477 continue;
478 }
479
Ian Rogersf8320442021-11-03 23:41:52 -0700480 if (!has_subtests(t)) {
Ian Rogersb47d2fb2021-11-03 23:42:08 -0700481 test_and_print(t, -1);
Wang Nane8c6d502015-11-17 08:32:48 +0000482 } else {
Ian Rogersf8320442021-11-03 23:41:52 -0700483 int subn = num_subtests(t);
Wang Nane8c6d502015-11-17 08:32:48 +0000484 /*
485 * minus 2 to align with normal testcases.
486 * For subtest we print additional '.x' in number.
487 * for example:
488 *
489 * 35: Test LLVM searching and compiling :
490 * 35.1: Basic BPF llvm compiling test : Ok
491 */
492 int subw = width > 2 ? width - 2 : width;
Jiri Olsaf4c1ea52012-12-19 11:33:39 -0300493
Wang Nane8c6d502015-11-17 08:32:48 +0000494 if (subn <= 0) {
495 color_fprintf(stderr, PERF_COLOR_YELLOW,
496 " Skip (not compiled in)\n");
497 continue;
498 }
499 pr_info("\n");
500
501 for (subi = 0; subi < subn; subi++) {
Ian Rogersf8320442021-11-03 23:41:52 -0700502 int len = strlen(test_description(t, subi));
Wang Nane8c6d502015-11-17 08:32:48 +0000503
504 if (subw < len)
505 subw = len;
506 }
507
508 for (subi = 0; subi < subn; subi++) {
Ian Rogersf8320442021-11-03 23:41:52 -0700509 if (!perf_test__matches(test_description(t, subi),
510 curr, argc, argv))
Jiri Olsad685e6c2020-05-25 00:42:06 +0200511 continue;
512
Wang Nane8c6d502015-11-17 08:32:48 +0000513 pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
Ian Rogersf8320442021-11-03 23:41:52 -0700514 test_description(t, subi));
Ian Rogersb47d2fb2021-11-03 23:42:08 -0700515 test_and_print(t, subi);
Wang Nane8c6d502015-11-17 08:32:48 +0000516 }
Jiri Olsaf4c1ea52012-12-19 11:33:39 -0300517 }
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300518 }
519
Riccardo Manciniebdf90a42021-08-11 20:06:26 +0200520 return run_shell_tests(argc, argv, i, width, skiplist);
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300521}
522
523static int perf_test__list_shell(int argc, const char **argv, int i)
524{
Riccardo Mancinida963832021-05-26 01:05:17 +0200525 struct dirent **entlist;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300526 struct dirent *ent;
Riccardo Mancinieb7261f2021-07-09 18:34:53 +0200527 int n_dirs, e;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300528 char path_dir[PATH_MAX];
529 const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
530
531 if (path == NULL)
532 return -1;
533
Riccardo Mancinida963832021-05-26 01:05:17 +0200534 n_dirs = scandir(path, &entlist, NULL, alphasort);
535 if (n_dirs == -1)
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300536 return -1;
537
Riccardo Mancinida963832021-05-26 01:05:17 +0200538 for_each_shell_test(entlist, n_dirs, path, ent) {
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300539 int curr = i++;
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300540 char bf[256];
Ian Rogers33f44bf2021-11-03 23:41:51 -0700541 struct test_suite t = {
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300542 .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name),
543 };
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300544
Jiri Olsad685e6c2020-05-25 00:42:06 +0200545 if (!perf_test__matches(t.desc, curr, argc, argv))
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300546 continue;
547
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300548 pr_info("%2d: %s\n", i, t.desc);
Riccardo Mancinieb7261f2021-07-09 18:34:53 +0200549
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300550 }
551
Riccardo Mancinieb7261f2021-07-09 18:34:53 +0200552 for (e = 0; e < n_dirs; e++)
553 zfree(&entlist[e]);
Riccardo Mancinida963832021-05-26 01:05:17 +0200554 free(entlist);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300555 return 0;
556}
557
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200558static int perf_test__list(int argc, const char **argv)
559{
Ian Rogersdf225202021-11-03 23:41:49 -0700560 unsigned int j, k;
Ian Rogers33f44bf2021-11-03 23:41:51 -0700561 struct test_suite *t;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200562 int i = 0;
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300563
Ian Rogersdf225202021-11-03 23:41:49 -0700564 for_each_test(j, k, t) {
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300565 int curr = i++;
Arnaldo Carvalho de Melo28765bf2017-08-04 11:16:40 -0300566
Ian Rogers848ddf52021-11-03 23:42:07 -0700567 if (!perf_test__matches(test_description(t, -1), curr, argc, argv))
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200568 continue;
569
Ian Rogersf8320442021-11-03 23:41:52 -0700570 pr_info("%2d: %s\n", i, test_description(t, -1));
Hendrik Bruecknerea40b6d2018-04-13 09:42:23 +0200571
Ian Rogersf8320442021-11-03 23:41:52 -0700572 if (has_subtests(t)) {
573 int subn = num_subtests(t);
Hendrik Bruecknerea40b6d2018-04-13 09:42:23 +0200574 int subi;
575
576 for (subi = 0; subi < subn; subi++)
577 pr_info("%2d:%1d: %s\n", i, subi + 1,
Ian Rogersf8320442021-11-03 23:41:52 -0700578 test_description(t, subi));
Hendrik Bruecknerea40b6d2018-04-13 09:42:23 +0200579 }
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200580 }
581
Arnaldo Carvalho de Melo1209b272017-08-03 16:49:15 -0300582 perf_test__list_shell(argc, argv, i);
583
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200584 return 0;
585}
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300586
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300587int cmd_test(int argc, const char **argv)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300588{
Yunlong Song1f9975f2015-03-18 21:35:55 +0800589 const char *test_usage[] = {
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200590 "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
591 NULL,
592 };
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300593 const char *skip = NULL;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200594 const struct option test_options[] = {
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300595 OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
Namhyung Kimc30ab8a2012-01-08 02:25:26 +0900596 OPT_INCR('v', "verbose", &verbose,
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200597 "be more verbose (show symbol address, etc)"),
Jiri Olsa7fa9b8f2016-06-28 13:29:01 +0200598 OPT_BOOLEAN('F', "dont-fork", &dont_fork,
599 "Do not fork for testcase"),
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200600 OPT_END()
601 };
Yunlong Song1f9975f2015-03-18 21:35:55 +0800602 const char * const test_subcommands[] = { "list", NULL };
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300603 struct intlist *skiplist = NULL;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -0300604 int ret = hists__init();
605
606 if (ret < 0)
607 return ret;
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200608
Yunlong Song1f9975f2015-03-18 21:35:55 +0800609 argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
Arnaldo Carvalho de Meloe60770a2011-11-29 12:52:07 -0200610 if (argc >= 1 && !strcmp(argv[0], "list"))
Arnaldo Carvalho de Melo6d02acc2017-08-04 11:16:40 -0300611 return perf_test__list(argc - 1, argv + 1);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300612
613 symbol_conf.priv_size = sizeof(int);
614 symbol_conf.sort_by_name = true;
615 symbol_conf.try_vmlinux_path = true;
616
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900617 if (symbol__init(NULL) < 0)
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300618 return -1;
619
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300620 if (skip != NULL)
621 skiplist = intlist__new(skip);
Arnaldo Carvalho de Melod3280ce2019-07-09 16:27:01 -0300622 /*
623 * Tests that create BPF maps, for instance, need more than the 64K
624 * default:
625 */
626 rlimit__bump_memlock();
Arnaldo Carvalho de Melo2ae82872013-01-24 16:22:55 -0300627
628 return __cmd_test(argc, argv, skiplist);
Arnaldo Carvalho de Melo1c6a8002010-04-29 18:58:32 -0300629}