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 317391db64SCarsten Haitzler #include "builtin-test-list.h" 327391db64SCarsten Haitzler 337fa9b8fbSJiri Olsa static bool dont_fork; 341b69346eSAdrian Hunter const char *dso_to_test; 357fa9b8fbSJiri Olsa 365cfb0cc0SIan Rogers /* 375cfb0cc0SIan Rogers * List of architecture specific tests. Not a weak symbol as the array length is 385cfb0cc0SIan Rogers * dependent on the initialization, as such GCC with LTO complains of 395cfb0cc0SIan Rogers * conflicting definitions with a weak symbol. 405cfb0cc0SIan Rogers */ 415cfb0cc0SIan Rogers #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__) 425cfb0cc0SIan Rogers extern struct test_suite *arch_tests[]; 435cfb0cc0SIan Rogers #else 445cfb0cc0SIan Rogers static struct test_suite *arch_tests[] = { 45df225205SIan Rogers NULL, 4631b6753fSMatt Fleming }; 475cfb0cc0SIan Rogers #endif 4831b6753fSMatt Fleming 4933f44bfdSIan Rogers static struct test_suite *generic_tests[] = { 50d68f0365SIan Rogers &suite__vmlinux_matches_kallsyms, 51378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 52d68f0365SIan Rogers &suite__openat_syscall_event, 53d68f0365SIan Rogers &suite__openat_syscall_event_on_all_cpus, 54d68f0365SIan Rogers &suite__basic_mmap, 55378ef0f5SIan Rogers #endif 56d68f0365SIan Rogers &suite__mem, 57d68f0365SIan Rogers &suite__parse_events, 58d68f0365SIan Rogers &suite__expr, 59d68f0365SIan Rogers &suite__PERF_RECORD, 60d68f0365SIan Rogers &suite__pmu, 61d68f0365SIan Rogers &suite__pmu_events, 62d68f0365SIan Rogers &suite__dso_data, 63d68f0365SIan Rogers &suite__dso_data_cache, 64d68f0365SIan Rogers &suite__dso_data_reopen, 65d68f0365SIan Rogers &suite__perf_evsel__roundtrip_name_test, 66378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 67d68f0365SIan Rogers &suite__perf_evsel__tp_sched_test, 68d68f0365SIan Rogers &suite__syscall_openat_tp_fields, 69378ef0f5SIan Rogers #endif 70d68f0365SIan Rogers &suite__attr, 71d68f0365SIan Rogers &suite__hists_link, 72d68f0365SIan Rogers &suite__python_use, 73d68f0365SIan Rogers &suite__bp_signal, 74d68f0365SIan Rogers &suite__bp_signal_overflow, 75d68f0365SIan Rogers &suite__bp_accounting, 76d68f0365SIan Rogers &suite__wp, 77d68f0365SIan Rogers &suite__task_exit, 78d68f0365SIan Rogers &suite__sw_clock_freq, 79d68f0365SIan Rogers &suite__code_reading, 80d68f0365SIan Rogers &suite__sample_parsing, 81d68f0365SIan Rogers &suite__keep_tracking, 82d68f0365SIan Rogers &suite__parse_no_sample_id_all, 83d68f0365SIan Rogers &suite__hists_filter, 84d68f0365SIan Rogers &suite__mmap_thread_lookup, 85d68f0365SIan Rogers &suite__thread_maps_share, 86d68f0365SIan Rogers &suite__hists_output, 87d68f0365SIan Rogers &suite__hists_cumulate, 88378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT 89d68f0365SIan Rogers &suite__switch_tracking, 90378ef0f5SIan Rogers #endif 91d68f0365SIan Rogers &suite__fdarray__filter, 92d68f0365SIan Rogers &suite__fdarray__add, 93d68f0365SIan Rogers &suite__kmod_path__parse, 94d68f0365SIan Rogers &suite__thread_map, 95d68f0365SIan Rogers &suite__session_topology, 96d68f0365SIan Rogers &suite__thread_map_synthesize, 97d68f0365SIan Rogers &suite__thread_map_remove, 985cebb33fSIan Rogers &suite__cpu_map, 99d68f0365SIan Rogers &suite__synthesize_stat_config, 100d68f0365SIan Rogers &suite__synthesize_stat, 101d68f0365SIan Rogers &suite__synthesize_stat_round, 102d68f0365SIan Rogers &suite__event_update, 103d68f0365SIan Rogers &suite__event_times, 104d68f0365SIan Rogers &suite__backward_ring_buffer, 105d68f0365SIan Rogers &suite__sdt_event, 106d68f0365SIan Rogers &suite__is_printable_array, 107d68f0365SIan Rogers &suite__bitmap_print, 108d68f0365SIan Rogers &suite__perf_hooks, 109d68f0365SIan Rogers &suite__unit_number__scnprint, 110d68f0365SIan Rogers &suite__mem2node, 111d68f0365SIan Rogers &suite__time_utils, 112d68f0365SIan Rogers &suite__jit_write_elf, 113d68f0365SIan Rogers &suite__pfm, 114d68f0365SIan Rogers &suite__api_io, 115d68f0365SIan Rogers &suite__maps__merge_in, 116d68f0365SIan Rogers &suite__demangle_java, 117d68f0365SIan Rogers &suite__demangle_ocaml, 118d68f0365SIan Rogers &suite__parse_metric, 119d68f0365SIan Rogers &suite__pe_file_parsing, 120d68f0365SIan Rogers &suite__expand_cgroup_events, 121d68f0365SIan Rogers &suite__perf_time_to_tsc, 122d68f0365SIan Rogers &suite__dlfilter, 1235504f679SMarco Elver &suite__sigtrap, 1249d9b22beSRavi Bangoria &suite__event_groups, 1251b69346eSAdrian Hunter &suite__symbols, 126*95e33c0fSJames Clark &suite__util, 127df225205SIan Rogers NULL, 128945aea22SJiri Olsa }; 129945aea22SJiri Olsa 13033f44bfdSIan Rogers static struct test_suite **tests[] = { 13131b6753fSMatt Fleming generic_tests, 13231b6753fSMatt Fleming arch_tests, 13331b6753fSMatt Fleming }; 13431b6753fSMatt Fleming 135f215054dSNamhyung Kim static struct test_workload *workloads[] = { 136f215054dSNamhyung Kim &workload__noploop, 13769b35292SNamhyung Kim &workload__thloop, 13841522f74SNamhyung Kim &workload__leafloop, 13939281709SNamhyung Kim &workload__sqrtloop, 140a104f0eaSNamhyung Kim &workload__brstack, 1413dfc01feSNamhyung Kim &workload__datasym, 142f215054dSNamhyung Kim }; 143f215054dSNamhyung Kim 144f832044cSIan Rogers static int num_subtests(const struct test_suite *t) 145f832044cSIan Rogers { 14678244d2eSIan Rogers int num; 14778244d2eSIan Rogers 14878244d2eSIan Rogers if (!t->test_cases) 149f832044cSIan Rogers return 0; 15078244d2eSIan Rogers 15178244d2eSIan Rogers num = 0; 15278244d2eSIan Rogers while (t->test_cases[num].name) 15378244d2eSIan Rogers num++; 15478244d2eSIan Rogers 15578244d2eSIan Rogers return num; 156f832044cSIan Rogers } 157f832044cSIan Rogers 158f832044cSIan Rogers static bool has_subtests(const struct test_suite *t) 159f832044cSIan Rogers { 16094e11fc7SIan Rogers return num_subtests(t) > 1; 161f832044cSIan Rogers } 162f832044cSIan Rogers 163f832044cSIan Rogers static const char *skip_reason(const struct test_suite *t, int subtest) 164f832044cSIan Rogers { 165cfa5013aSIan Rogers if (!t->test_cases) 166f832044cSIan Rogers return NULL; 167cfa5013aSIan Rogers 168cfa5013aSIan Rogers return t->test_cases[subtest >= 0 ? subtest : 0].skip_reason; 169f832044cSIan Rogers } 170f832044cSIan Rogers 171f832044cSIan Rogers static const char *test_description(const struct test_suite *t, int subtest) 172f832044cSIan Rogers { 17378244d2eSIan Rogers if (t->test_cases && subtest >= 0) 17478244d2eSIan Rogers return t->test_cases[subtest].desc; 175f832044cSIan Rogers 17678244d2eSIan Rogers return t->desc; 177f832044cSIan Rogers } 178f832044cSIan Rogers 17978244d2eSIan Rogers static test_fnptr test_function(const struct test_suite *t, int subtest) 180f832044cSIan Rogers { 18178244d2eSIan Rogers if (subtest <= 0) 18278244d2eSIan Rogers return t->test_cases[0].run_case; 18378244d2eSIan Rogers 18478244d2eSIan Rogers return t->test_cases[subtest].run_case; 185f832044cSIan Rogers } 186f832044cSIan Rogers 187d685e6c1SJiri Olsa static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[]) 188945aea22SJiri Olsa { 189945aea22SJiri Olsa int i; 190945aea22SJiri Olsa 191945aea22SJiri Olsa if (argc == 0) 192945aea22SJiri Olsa return true; 193945aea22SJiri Olsa 194945aea22SJiri Olsa for (i = 0; i < argc; ++i) { 195945aea22SJiri Olsa char *end; 196945aea22SJiri Olsa long nr = strtoul(argv[i], &end, 10); 197945aea22SJiri Olsa 198945aea22SJiri Olsa if (*end == '\0') { 199945aea22SJiri Olsa if (nr == curr + 1) 200945aea22SJiri Olsa return true; 201945aea22SJiri Olsa continue; 202945aea22SJiri Olsa } 203945aea22SJiri Olsa 204d685e6c1SJiri Olsa if (strcasestr(desc, argv[i])) 205945aea22SJiri Olsa return true; 206945aea22SJiri Olsa } 207945aea22SJiri Olsa 208945aea22SJiri Olsa return false; 209945aea22SJiri Olsa } 210945aea22SJiri Olsa 21133f44bfdSIan Rogers static int run_test(struct test_suite *test, int subtest) 2120d8a5faaSJiri Olsa { 2137fa9b8fbSJiri Olsa int status, err = -1, child = dont_fork ? 0 : fork(); 214ba3dfff8SMasami Hiramatsu char sbuf[STRERR_BUFSIZE]; 2150d8a5faaSJiri Olsa 2160d8a5faaSJiri Olsa if (child < 0) { 217ba3dfff8SMasami Hiramatsu pr_err("failed to fork test: %s\n", 218c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, sbuf, sizeof(sbuf))); 2190d8a5faaSJiri Olsa return -1; 2200d8a5faaSJiri Olsa } 2210d8a5faaSJiri Olsa 2220d8a5faaSJiri Olsa if (!child) { 2237fa9b8fbSJiri Olsa if (!dont_fork) { 2240d8a5faaSJiri Olsa pr_debug("test child forked, pid %d\n", getpid()); 2257fa9b8fbSJiri Olsa 226bb963e16SNamhyung Kim if (verbose <= 0) { 2275bcf2fe0SWang Nan int nullfd = open("/dev/null", O_WRONLY); 2287fa9b8fbSJiri Olsa 2295bcf2fe0SWang Nan if (nullfd >= 0) { 2305bcf2fe0SWang Nan close(STDERR_FILENO); 2315bcf2fe0SWang Nan close(STDOUT_FILENO); 2325bcf2fe0SWang Nan 2335bcf2fe0SWang Nan dup2(nullfd, STDOUT_FILENO); 2345bcf2fe0SWang Nan dup2(STDOUT_FILENO, STDERR_FILENO); 2355bcf2fe0SWang Nan close(nullfd); 2365bcf2fe0SWang Nan } 237b6847d2cSArnaldo Carvalho de Melo } else { 238b6847d2cSArnaldo Carvalho de Melo signal(SIGSEGV, sighandler_dump_stack); 239b6847d2cSArnaldo Carvalho de Melo signal(SIGFPE, sighandler_dump_stack); 2405bcf2fe0SWang Nan } 2417fa9b8fbSJiri Olsa } 2425bcf2fe0SWang Nan 243f832044cSIan Rogers err = test_function(test, subtest)(test, subtest); 2447fa9b8fbSJiri Olsa if (!dont_fork) 2450d8a5faaSJiri Olsa exit(err); 2460d8a5faaSJiri Olsa } 2470d8a5faaSJiri Olsa 2487fa9b8fbSJiri Olsa if (!dont_fork) { 2490d8a5faaSJiri Olsa wait(&status); 2500d8a5faaSJiri Olsa 2510d8a5faaSJiri Olsa if (WIFEXITED(status)) { 252189c466fSHe Kuang err = (signed char)WEXITSTATUS(status); 2530d8a5faaSJiri Olsa pr_debug("test child finished with %d\n", err); 2540d8a5faaSJiri Olsa } else if (WIFSIGNALED(status)) { 2550d8a5faaSJiri Olsa err = -1; 2560d8a5faaSJiri Olsa pr_debug("test child interrupted\n"); 2570d8a5faaSJiri Olsa } 2587fa9b8fbSJiri Olsa } 2590d8a5faaSJiri Olsa 2600d8a5faaSJiri Olsa return err; 2610d8a5faaSJiri Olsa } 2620d8a5faaSJiri Olsa 263df225205SIan Rogers #define for_each_test(j, k, t) \ 26417354d15SArnaldo Carvalho de Melo for (j = 0, k = 0; j < ARRAY_SIZE(tests); j++, k = 0) \ 26517354d15SArnaldo Carvalho de Melo while ((t = tests[j][k++]) != NULL) 266e8210cefSMatt Fleming 267b47d2fb4SIan Rogers static int test_and_print(struct test_suite *t, int subtest) 268e8c6d500SWang Nan { 269e8c6d500SWang Nan int err; 270e8c6d500SWang Nan 271e8c6d500SWang Nan pr_debug("\n--- start ---\n"); 272e8c6d500SWang Nan err = run_test(t, subtest); 273e8c6d500SWang Nan pr_debug("---- end ----\n"); 274e8c6d500SWang Nan 275f832044cSIan Rogers if (!has_subtests(t)) 276e8c6d500SWang Nan pr_debug("%s:", t->desc); 277e8c6d500SWang Nan else 278aa90f9f9SSandipan Das pr_debug("%s subtest %d:", t->desc, subtest + 1); 279e8c6d500SWang Nan 280e8c6d500SWang Nan switch (err) { 281e8c6d500SWang Nan case TEST_OK: 282e8c6d500SWang Nan pr_info(" Ok\n"); 283e8c6d500SWang Nan break; 2843b536651SIan Rogers case TEST_SKIP: { 285f832044cSIan Rogers const char *reason = skip_reason(t, subtest); 286f832044cSIan Rogers 287f832044cSIan Rogers if (reason) 288f832044cSIan Rogers color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason); 2893b536651SIan Rogers else 290e8c6d500SWang Nan color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n"); 2913b536651SIan Rogers } 292e8c6d500SWang Nan break; 293e8c6d500SWang Nan case TEST_FAIL: 294e8c6d500SWang Nan default: 295e8c6d500SWang Nan color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n"); 296e8c6d500SWang Nan break; 297e8c6d500SWang Nan } 298e8c6d500SWang Nan 299e8c6d500SWang Nan return err; 300e8c6d500SWang Nan } 301e8c6d500SWang Nan 3021209b273SArnaldo Carvalho de Melo struct shell_test { 3031209b273SArnaldo Carvalho de Melo const char *dir; 3041209b273SArnaldo Carvalho de Melo const char *file; 3051209b273SArnaldo Carvalho de Melo }; 3061209b273SArnaldo Carvalho de Melo 30733f44bfdSIan Rogers static int shell_test__run(struct test_suite *test, int subdir __maybe_unused) 3081209b273SArnaldo Carvalho de Melo { 3091209b273SArnaldo Carvalho de Melo int err; 3101209b273SArnaldo Carvalho de Melo char script[PATH_MAX]; 3111209b273SArnaldo Carvalho de Melo struct shell_test *st = test->priv; 3121209b273SArnaldo Carvalho de Melo 313e3c9cfd0SIan Rogers path__join(script, sizeof(script) - 3, st->dir, st->file); 314e3c9cfd0SIan Rogers 3157c0a6144SYang Jihong if (verbose > 0) 316e3c9cfd0SIan Rogers strncat(script, " -v", sizeof(script) - strlen(script) - 1); 3171209b273SArnaldo Carvalho de Melo 3181209b273SArnaldo Carvalho de Melo err = system(script); 3191209b273SArnaldo Carvalho de Melo if (!err) 3201209b273SArnaldo Carvalho de Melo return TEST_OK; 3211209b273SArnaldo Carvalho de Melo 3221209b273SArnaldo Carvalho de Melo return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL; 3231209b273SArnaldo Carvalho de Melo } 3241209b273SArnaldo Carvalho de Melo 325ebdf90a4SRiccardo Mancini static int run_shell_tests(int argc, const char *argv[], int i, int width, 326ebdf90a4SRiccardo Mancini struct intlist *skiplist) 3271209b273SArnaldo Carvalho de Melo { 3287391db64SCarsten Haitzler struct shell_test st; 3297391db64SCarsten Haitzler const struct script_file *files, *file; 3301209b273SArnaldo Carvalho de Melo 3317391db64SCarsten Haitzler files = list_script_files(); 3327391db64SCarsten Haitzler if (!files) 3337391db64SCarsten Haitzler return 0; 3347391db64SCarsten Haitzler for (file = files; file->dir; file++) { 3351209b273SArnaldo Carvalho de Melo int curr = i++; 336c76ec1cfSIan Rogers struct test_case test_cases[] = { 337c76ec1cfSIan Rogers { 3387391db64SCarsten Haitzler .desc = file->desc, 339c76ec1cfSIan Rogers .run_case = shell_test__run, 340c76ec1cfSIan Rogers }, 341c76ec1cfSIan Rogers { .name = NULL, } 342c76ec1cfSIan Rogers }; 343c76ec1cfSIan Rogers struct test_suite test_suite = { 344c76ec1cfSIan Rogers .desc = test_cases[0].desc, 345c76ec1cfSIan Rogers .test_cases = test_cases, 3461209b273SArnaldo Carvalho de Melo .priv = &st, 3471209b273SArnaldo Carvalho de Melo }; 3487391db64SCarsten Haitzler st.dir = file->dir; 3491209b273SArnaldo Carvalho de Melo 350caaaa554SIan Rogers if (test_suite.desc == NULL || 351caaaa554SIan Rogers !perf_test__matches(test_suite.desc, curr, argc, argv)) 3521209b273SArnaldo Carvalho de Melo continue; 3531209b273SArnaldo Carvalho de Melo 3547391db64SCarsten Haitzler st.file = file->file; 355f8464e08SCarsten Haitzler pr_info("%3d: %-*s:", i, width, test_suite.desc); 356ebdf90a4SRiccardo Mancini 357ebdf90a4SRiccardo Mancini if (intlist__find(skiplist, i)) { 358ebdf90a4SRiccardo Mancini color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 359ebdf90a4SRiccardo Mancini continue; 360ebdf90a4SRiccardo Mancini } 361ebdf90a4SRiccardo Mancini 362b47d2fb4SIan Rogers test_and_print(&test_suite, 0); 3631209b273SArnaldo Carvalho de Melo } 3641209b273SArnaldo Carvalho de Melo return 0; 3651209b273SArnaldo Carvalho de Melo } 3661209b273SArnaldo Carvalho de Melo 3672ae82878SArnaldo Carvalho de Melo static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) 368945aea22SJiri Olsa { 36933f44bfdSIan Rogers struct test_suite *t; 370df225205SIan Rogers unsigned int j, k; 371945aea22SJiri Olsa int i = 0; 3727391db64SCarsten Haitzler int width = list_script_max_width(); 373945aea22SJiri Olsa 374df225205SIan Rogers for_each_test(j, k, t) { 375f832044cSIan Rogers int len = strlen(test_description(t, -1)); 376945aea22SJiri Olsa 377945aea22SJiri Olsa if (width < len) 378945aea22SJiri Olsa width = len; 379945aea22SJiri Olsa } 380945aea22SJiri Olsa 381df225205SIan Rogers for_each_test(j, k, t) { 382b47d2fb4SIan Rogers int curr = i++; 383d685e6c1SJiri Olsa int subi; 384945aea22SJiri Olsa 385f832044cSIan Rogers if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) { 386d685e6c1SJiri Olsa bool skip = true; 387d685e6c1SJiri Olsa int subn; 388d685e6c1SJiri Olsa 389f832044cSIan Rogers subn = num_subtests(t); 390d685e6c1SJiri Olsa 391d685e6c1SJiri Olsa for (subi = 0; subi < subn; subi++) { 392f832044cSIan Rogers if (perf_test__matches(test_description(t, subi), 393f832044cSIan Rogers curr, argc, argv)) 394d685e6c1SJiri Olsa skip = false; 395d685e6c1SJiri Olsa } 396d685e6c1SJiri Olsa 397d685e6c1SJiri Olsa if (skip) 398d685e6c1SJiri Olsa continue; 399d685e6c1SJiri Olsa } 400d685e6c1SJiri Olsa 401f8464e08SCarsten Haitzler pr_info("%3d: %-*s:", i, width, test_description(t, -1)); 4022ae82878SArnaldo Carvalho de Melo 4032ae82878SArnaldo Carvalho de Melo if (intlist__find(skiplist, i)) { 4042ae82878SArnaldo Carvalho de Melo color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 4052ae82878SArnaldo Carvalho de Melo continue; 4062ae82878SArnaldo Carvalho de Melo } 4072ae82878SArnaldo Carvalho de Melo 408f832044cSIan Rogers if (!has_subtests(t)) { 409b47d2fb4SIan Rogers test_and_print(t, -1); 410e8c6d500SWang Nan } else { 411f832044cSIan Rogers int subn = num_subtests(t); 412e8c6d500SWang Nan /* 413e8c6d500SWang Nan * minus 2 to align with normal testcases. 414e8c6d500SWang Nan * For subtest we print additional '.x' in number. 415e8c6d500SWang Nan * for example: 416e8c6d500SWang Nan * 417e8c6d500SWang Nan * 35: Test LLVM searching and compiling : 418e8c6d500SWang Nan * 35.1: Basic BPF llvm compiling test : Ok 419e8c6d500SWang Nan */ 420e8c6d500SWang Nan int subw = width > 2 ? width - 2 : width; 421f4c1ea5fSJiri Olsa 422e8c6d500SWang Nan if (subn <= 0) { 423e8c6d500SWang Nan color_fprintf(stderr, PERF_COLOR_YELLOW, 424e8c6d500SWang Nan " Skip (not compiled in)\n"); 425e8c6d500SWang Nan continue; 426e8c6d500SWang Nan } 427e8c6d500SWang Nan pr_info("\n"); 428e8c6d500SWang Nan 429e8c6d500SWang Nan for (subi = 0; subi < subn; subi++) { 430f832044cSIan Rogers int len = strlen(test_description(t, subi)); 431e8c6d500SWang Nan 432e8c6d500SWang Nan if (subw < len) 433e8c6d500SWang Nan subw = len; 434e8c6d500SWang Nan } 435e8c6d500SWang Nan 436e8c6d500SWang Nan for (subi = 0; subi < subn; subi++) { 437f832044cSIan Rogers if (!perf_test__matches(test_description(t, subi), 438f832044cSIan Rogers curr, argc, argv)) 439d685e6c1SJiri Olsa continue; 440d685e6c1SJiri Olsa 441f8464e08SCarsten Haitzler pr_info("%3d.%1d: %-*s:", i, subi + 1, subw, 442f832044cSIan Rogers test_description(t, subi)); 443b47d2fb4SIan Rogers test_and_print(t, subi); 444e8c6d500SWang Nan } 445f4c1ea5fSJiri Olsa } 446945aea22SJiri Olsa } 447945aea22SJiri Olsa 448ebdf90a4SRiccardo Mancini return run_shell_tests(argc, argv, i, width, skiplist); 4491209b273SArnaldo Carvalho de Melo } 4501209b273SArnaldo Carvalho de Melo 4511209b273SArnaldo Carvalho de Melo static int perf_test__list_shell(int argc, const char **argv, int i) 4521209b273SArnaldo Carvalho de Melo { 4537391db64SCarsten Haitzler const struct script_file *files, *file; 4541209b273SArnaldo Carvalho de Melo 4557391db64SCarsten Haitzler files = list_script_files(); 4567391db64SCarsten Haitzler if (!files) 4577391db64SCarsten Haitzler return 0; 4587391db64SCarsten Haitzler for (file = files; file->dir; file++) { 4596d02acc1SArnaldo Carvalho de Melo int curr = i++; 46033f44bfdSIan Rogers struct test_suite t = { 4617391db64SCarsten Haitzler .desc = file->desc 4626d02acc1SArnaldo Carvalho de Melo }; 4631209b273SArnaldo Carvalho de Melo 464d685e6c1SJiri Olsa if (!perf_test__matches(t.desc, curr, argc, argv)) 4651209b273SArnaldo Carvalho de Melo continue; 4661209b273SArnaldo Carvalho de Melo 467f8464e08SCarsten Haitzler pr_info("%3d: %s\n", i, t.desc); 4681209b273SArnaldo Carvalho de Melo } 469945aea22SJiri Olsa return 0; 470945aea22SJiri Olsa } 471945aea22SJiri Olsa 472945aea22SJiri Olsa static int perf_test__list(int argc, const char **argv) 473945aea22SJiri Olsa { 474df225205SIan Rogers unsigned int j, k; 47533f44bfdSIan Rogers struct test_suite *t; 476945aea22SJiri Olsa int i = 0; 477945aea22SJiri Olsa 478df225205SIan Rogers for_each_test(j, k, t) { 4796d02acc1SArnaldo Carvalho de Melo int curr = i++; 48028765bf2SArnaldo Carvalho de Melo 481848ddf59SIan Rogers if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) 482945aea22SJiri Olsa continue; 483945aea22SJiri Olsa 484f8464e08SCarsten Haitzler pr_info("%3d: %s\n", i, test_description(t, -1)); 485ea40b6d3SHendrik Brueckner 486f832044cSIan Rogers if (has_subtests(t)) { 487f832044cSIan Rogers int subn = num_subtests(t); 488ea40b6d3SHendrik Brueckner int subi; 489ea40b6d3SHendrik Brueckner 490ea40b6d3SHendrik Brueckner for (subi = 0; subi < subn; subi++) 491f8464e08SCarsten Haitzler pr_info("%3d:%1d: %s\n", i, subi + 1, 492f832044cSIan Rogers test_description(t, subi)); 493ea40b6d3SHendrik Brueckner } 494945aea22SJiri Olsa } 495945aea22SJiri Olsa 4961209b273SArnaldo Carvalho de Melo perf_test__list_shell(argc, argv, i); 4971209b273SArnaldo Carvalho de Melo 498945aea22SJiri Olsa return 0; 499945aea22SJiri Olsa } 500945aea22SJiri Olsa 501f215054dSNamhyung Kim static int run_workload(const char *work, int argc, const char **argv) 502f215054dSNamhyung Kim { 503f215054dSNamhyung Kim unsigned int i = 0; 504f215054dSNamhyung Kim struct test_workload *twl; 505f215054dSNamhyung Kim 506f215054dSNamhyung Kim for (i = 0; i < ARRAY_SIZE(workloads); i++) { 507f215054dSNamhyung Kim twl = workloads[i]; 508f215054dSNamhyung Kim if (!strcmp(twl->name, work)) 509f215054dSNamhyung Kim return twl->func(argc, argv); 510f215054dSNamhyung Kim } 511f215054dSNamhyung Kim 512f215054dSNamhyung Kim pr_info("No workload found: %s\n", work); 513f215054dSNamhyung Kim return -1; 514f215054dSNamhyung Kim } 515f215054dSNamhyung Kim 516b0ad8ea6SArnaldo Carvalho de Melo int cmd_test(int argc, const char **argv) 517945aea22SJiri Olsa { 5181f9975f1SYunlong Song const char *test_usage[] = { 519945aea22SJiri Olsa "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", 520945aea22SJiri Olsa NULL, 521945aea22SJiri Olsa }; 5222ae82878SArnaldo Carvalho de Melo const char *skip = NULL; 523f215054dSNamhyung Kim const char *workload = NULL; 524945aea22SJiri Olsa const struct option test_options[] = { 5252ae82878SArnaldo Carvalho de Melo OPT_STRING('s', "skip", &skip, "tests", "tests to skip"), 526945aea22SJiri Olsa OPT_INCR('v', "verbose", &verbose, 527945aea22SJiri Olsa "be more verbose (show symbol address, etc)"), 5287fa9b8fbSJiri Olsa OPT_BOOLEAN('F', "dont-fork", &dont_fork, 5297fa9b8fbSJiri Olsa "Do not fork for testcase"), 530f215054dSNamhyung Kim OPT_STRING('w', "workload", &workload, "work", "workload to run for testing"), 5311b69346eSAdrian Hunter OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"), 532945aea22SJiri Olsa OPT_END() 533945aea22SJiri Olsa }; 5341f9975f1SYunlong Song const char * const test_subcommands[] = { "list", NULL }; 5352ae82878SArnaldo Carvalho de Melo struct intlist *skiplist = NULL; 536a635fc51SArnaldo Carvalho de Melo int ret = hists__init(); 537a635fc51SArnaldo Carvalho de Melo 538a635fc51SArnaldo Carvalho de Melo if (ret < 0) 539a635fc51SArnaldo Carvalho de Melo return ret; 540945aea22SJiri Olsa 541f0a29c96SSohaib Mohamed /* Unbuffered output */ 542f0a29c96SSohaib Mohamed setvbuf(stdout, NULL, _IONBF, 0); 543f0a29c96SSohaib Mohamed 5441f9975f1SYunlong Song argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0); 545945aea22SJiri Olsa if (argc >= 1 && !strcmp(argv[0], "list")) 5466d02acc1SArnaldo Carvalho de Melo return perf_test__list(argc - 1, argv + 1); 547945aea22SJiri Olsa 548f215054dSNamhyung Kim if (workload) 549f215054dSNamhyung Kim return run_workload(workload, argc, argv); 550f215054dSNamhyung Kim 551945aea22SJiri Olsa symbol_conf.priv_size = sizeof(int); 552945aea22SJiri Olsa symbol_conf.try_vmlinux_path = true; 553945aea22SJiri Olsa 5540a7e6d1bSNamhyung Kim if (symbol__init(NULL) < 0) 555945aea22SJiri Olsa return -1; 556945aea22SJiri Olsa 5572ae82878SArnaldo Carvalho de Melo if (skip != NULL) 5582ae82878SArnaldo Carvalho de Melo skiplist = intlist__new(skip); 559d3280ce0SArnaldo Carvalho de Melo /* 560d3280ce0SArnaldo Carvalho de Melo * Tests that create BPF maps, for instance, need more than the 64K 561d3280ce0SArnaldo Carvalho de Melo * default: 562d3280ce0SArnaldo Carvalho de Melo */ 563d3280ce0SArnaldo Carvalho de Melo rlimit__bump_memlock(); 5642ae82878SArnaldo Carvalho de Melo 5652ae82878SArnaldo Carvalho de Melo return __cmd_test(argc, argv, skiplist); 566945aea22SJiri Olsa } 567