1945aea22SJiri Olsa /* 2945aea22SJiri Olsa * builtin-test.c 3945aea22SJiri Olsa * 4945aea22SJiri Olsa * Builtin regression testing command: ever growing number of sanity tests 5945aea22SJiri Olsa */ 60d8a5faaSJiri Olsa #include <unistd.h> 70d8a5faaSJiri Olsa #include <string.h> 8945aea22SJiri Olsa #include "builtin.h" 9a635fc51SArnaldo Carvalho de Melo #include "hist.h" 102ae82878SArnaldo Carvalho de Melo #include "intlist.h" 110a4e1ae6SJiri Olsa #include "tests.h" 12c81251e8SJiri Olsa #include "debug.h" 13c81251e8SJiri Olsa #include "color.h" 14c81251e8SJiri Olsa #include "parse-options.h" 15c81251e8SJiri Olsa #include "symbol.h" 16945aea22SJiri Olsa 17*31b6753fSMatt Fleming struct test __weak arch_tests[] = { 18*31b6753fSMatt Fleming { 19*31b6753fSMatt Fleming .func = NULL, 20*31b6753fSMatt Fleming }, 21*31b6753fSMatt Fleming }; 22*31b6753fSMatt Fleming 23*31b6753fSMatt Fleming static struct test generic_tests[] = { 24945aea22SJiri Olsa { 25945aea22SJiri Olsa .desc = "vmlinux symtab matches kallsyms", 26945aea22SJiri Olsa .func = test__vmlinux_matches_kallsyms, 27945aea22SJiri Olsa }, 28945aea22SJiri Olsa { 2943f322b4SRiku Voipio .desc = "detect openat syscall event", 3043f322b4SRiku Voipio .func = test__openat_syscall_event, 31945aea22SJiri Olsa }, 32945aea22SJiri Olsa { 3343f322b4SRiku Voipio .desc = "detect openat syscall event on all cpus", 3443f322b4SRiku Voipio .func = test__openat_syscall_event_on_all_cpus, 35945aea22SJiri Olsa }, 36945aea22SJiri Olsa { 37945aea22SJiri Olsa .desc = "read samples using the mmap interface", 38945aea22SJiri Olsa .func = test__basic_mmap, 39945aea22SJiri Olsa }, 40945aea22SJiri Olsa { 41945aea22SJiri Olsa .desc = "parse events tests", 42c81251e8SJiri Olsa .func = test__parse_events, 43945aea22SJiri Olsa }, 44945aea22SJiri Olsa #if defined(__x86_64__) || defined(__i386__) 45945aea22SJiri Olsa { 46945aea22SJiri Olsa .desc = "x86 rdpmc test", 47945aea22SJiri Olsa .func = test__rdpmc, 48945aea22SJiri Olsa }, 49945aea22SJiri Olsa #endif 50945aea22SJiri Olsa { 51945aea22SJiri Olsa .desc = "Validate PERF_RECORD_* events & perf_sample fields", 52945aea22SJiri Olsa .func = test__PERF_RECORD, 53945aea22SJiri Olsa }, 54945aea22SJiri Olsa { 55945aea22SJiri Olsa .desc = "Test perf pmu format parsing", 56cff7f956SJiri Olsa .func = test__pmu, 57945aea22SJiri Olsa }, 58945aea22SJiri Olsa { 594ebbcb84SJiri Olsa .desc = "Test dso data read", 60c81251e8SJiri Olsa .func = test__dso_data, 61945aea22SJiri Olsa }, 62945aea22SJiri Olsa { 634ebbcb84SJiri Olsa .desc = "Test dso data cache", 644ebbcb84SJiri Olsa .func = test__dso_data_cache, 654ebbcb84SJiri Olsa }, 664ebbcb84SJiri Olsa { 6745dc1bb5SJiri Olsa .desc = "Test dso data reopen", 6845dc1bb5SJiri Olsa .func = test__dso_data_reopen, 6945dc1bb5SJiri Olsa }, 7045dc1bb5SJiri Olsa { 71945aea22SJiri Olsa .desc = "roundtrip evsel->name check", 72cfffae2eSJiri Olsa .func = test__perf_evsel__roundtrip_name_test, 73945aea22SJiri Olsa }, 74945aea22SJiri Olsa { 75945aea22SJiri Olsa .desc = "Check parsing of sched tracepoints fields", 765e24a090SJiri Olsa .func = test__perf_evsel__tp_sched_test, 77945aea22SJiri Olsa }, 78945aea22SJiri Olsa { 7943f322b4SRiku Voipio .desc = "Generate and check syscalls:sys_enter_openat event fields", 8043f322b4SRiku Voipio .func = test__syscall_openat_tp_fields, 81945aea22SJiri Olsa }, 82945aea22SJiri Olsa { 83d898b241SJiri Olsa .desc = "struct perf_event_attr setup", 84c81251e8SJiri Olsa .func = test__attr, 85d898b241SJiri Olsa }, 86d898b241SJiri Olsa { 87ffcbaa14SArnaldo Carvalho de Melo .desc = "Test matching and linking multiple hists", 88f8ebb0cdSNamhyung Kim .func = test__hists_link, 89f8ebb0cdSNamhyung Kim }, 90f8ebb0cdSNamhyung Kim { 91887e73d7SWANG Chao .desc = "Try 'import perf' in python, checking link problems", 9254359d33SArnaldo Carvalho de Melo .func = test__python_use, 9354359d33SArnaldo Carvalho de Melo }, 9454359d33SArnaldo Carvalho de Melo { 955a6bef47SJiri Olsa .desc = "Test breakpoint overflow signal handler", 965a6bef47SJiri Olsa .func = test__bp_signal, 975a6bef47SJiri Olsa }, 985a6bef47SJiri Olsa { 9906933e3aSJiri Olsa .desc = "Test breakpoint overflow sampling", 10006933e3aSJiri Olsa .func = test__bp_signal_overflow, 10106933e3aSJiri Olsa }, 10206933e3aSJiri Olsa { 103d723a550SNamhyung Kim .desc = "Test number of exit event of a simple workload", 104d723a550SNamhyung Kim .func = test__task_exit, 105d723a550SNamhyung Kim }, 106d723a550SNamhyung Kim { 107bc96b361SNamhyung Kim .desc = "Test software clock events have valid period values", 108bc96b361SNamhyung Kim .func = test__sw_clock_freq, 109bc96b361SNamhyung Kim }, 1103bd5a5fcSAdrian Hunter #if defined(__x86_64__) || defined(__i386__) 1113bd5a5fcSAdrian Hunter { 1123bd5a5fcSAdrian Hunter .desc = "Test converting perf time to TSC", 1133bd5a5fcSAdrian Hunter .func = test__perf_time_to_tsc, 1143bd5a5fcSAdrian Hunter }, 1153bd5a5fcSAdrian Hunter #endif 116bc96b361SNamhyung Kim { 117b55ae0a9SAdrian Hunter .desc = "Test object code reading", 118b55ae0a9SAdrian Hunter .func = test__code_reading, 119b55ae0a9SAdrian Hunter }, 120b55ae0a9SAdrian Hunter { 121045f8cd8SAdrian Hunter .desc = "Test sample parsing", 122045f8cd8SAdrian Hunter .func = test__sample_parsing, 123045f8cd8SAdrian Hunter }, 124045f8cd8SAdrian Hunter { 125395c3070SAdrian Hunter .desc = "Test using a dummy software event to keep tracking", 126395c3070SAdrian Hunter .func = test__keep_tracking, 127395c3070SAdrian Hunter }, 128395c3070SAdrian Hunter { 12953a277e5SAdrian Hunter .desc = "Test parsing with no sample_id_all bit set", 13053a277e5SAdrian Hunter .func = test__parse_no_sample_id_all, 13153a277e5SAdrian Hunter }, 1323237f281SWang Nan #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) || defined(__aarch64__) 1339ff125d1SJiri Olsa #ifdef HAVE_DWARF_UNWIND_SUPPORT 134aa16b81fSJiri Olsa { 135aa16b81fSJiri Olsa .desc = "Test dwarf unwind", 136aa16b81fSJiri Olsa .func = test__dwarf_unwind, 137aa16b81fSJiri Olsa }, 138aa16b81fSJiri Olsa #endif 139aa16b81fSJiri Olsa #endif 14053a277e5SAdrian Hunter { 1413c3cfd99SNamhyung Kim .desc = "Test filtering hist entries", 1423c3cfd99SNamhyung Kim .func = test__hists_filter, 1433c3cfd99SNamhyung Kim }, 1443c3cfd99SNamhyung Kim { 1454e85edfcSJiri Olsa .desc = "Test mmap thread lookup", 1464e85edfcSJiri Olsa .func = test__mmap_thread_lookup, 1474e85edfcSJiri Olsa }, 1484e85edfcSJiri Olsa { 149fabf0123SJiri Olsa .desc = "Test thread mg sharing", 150fabf0123SJiri Olsa .func = test__thread_mg_share, 151fabf0123SJiri Olsa }, 152fabf0123SJiri Olsa { 153f21d1815SNamhyung Kim .desc = "Test output sorting of hist entries", 154f21d1815SNamhyung Kim .func = test__hists_output, 155f21d1815SNamhyung Kim }, 156f21d1815SNamhyung Kim { 1570506aeccSNamhyung Kim .desc = "Test cumulation of child hist entries", 1580506aeccSNamhyung Kim .func = test__hists_cumulate, 1590506aeccSNamhyung Kim }, 1600506aeccSNamhyung Kim { 161d44bc558SAdrian Hunter .desc = "Test tracking with sched_switch", 162d44bc558SAdrian Hunter .func = test__switch_tracking, 163d44bc558SAdrian Hunter }, 164d44bc558SAdrian Hunter { 1651b85337dSArnaldo Carvalho de Melo .desc = "Filter fds with revents mask in a fdarray", 1661b85337dSArnaldo Carvalho de Melo .func = test__fdarray__filter, 16754dbfae3SArnaldo Carvalho de Melo }, 16854dbfae3SArnaldo Carvalho de Melo { 1691b85337dSArnaldo Carvalho de Melo .desc = "Add fd to a fdarray, making it autogrow", 1701b85337dSArnaldo Carvalho de Melo .func = test__fdarray__add, 1719ae28035SArnaldo Carvalho de Melo }, 1729ae28035SArnaldo Carvalho de Melo { 1733c8a67f5SJiri Olsa .desc = "Test kmod_path__parse function", 1743c8a67f5SJiri Olsa .func = test__kmod_path__parse, 1753c8a67f5SJiri Olsa }, 1763c8a67f5SJiri Olsa { 177134aa44fSJiri Olsa .desc = "Test thread map", 178134aa44fSJiri Olsa .func = test__thread_map, 179134aa44fSJiri Olsa }, 180134aa44fSJiri Olsa { 1819bc898c7SWang Nan .desc = "Test LLVM searching and compiling", 1829bc898c7SWang Nan .func = test__llvm, 1839bc898c7SWang Nan }, 18498e4619fSAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 18598e4619fSAdrian Hunter #if defined(__x86_64__) || defined(__i386__) 18698e4619fSAdrian Hunter { 18798e4619fSAdrian Hunter .desc = "Test x86 instruction decoder - new instructions", 18898e4619fSAdrian Hunter .func = test__insn_x86, 18998e4619fSAdrian Hunter }, 19098e4619fSAdrian Hunter #endif 19198e4619fSAdrian Hunter #endif 1929bc898c7SWang Nan { 193c84974edSKan Liang .desc = "Test topology in session", 194c84974edSKan Liang .func = test_session_topology, 195c84974edSKan Liang }, 196c84974edSKan Liang { 197945aea22SJiri Olsa .func = NULL, 198945aea22SJiri Olsa }, 199945aea22SJiri Olsa }; 200945aea22SJiri Olsa 201*31b6753fSMatt Fleming static struct test *tests[] = { 202*31b6753fSMatt Fleming generic_tests, 203*31b6753fSMatt Fleming arch_tests, 204*31b6753fSMatt Fleming }; 205*31b6753fSMatt Fleming 206e8210cefSMatt Fleming static bool perf_test__matches(struct test *test, int curr, int argc, const char *argv[]) 207945aea22SJiri Olsa { 208945aea22SJiri Olsa int i; 209945aea22SJiri Olsa 210945aea22SJiri Olsa if (argc == 0) 211945aea22SJiri Olsa return true; 212945aea22SJiri Olsa 213945aea22SJiri Olsa for (i = 0; i < argc; ++i) { 214945aea22SJiri Olsa char *end; 215945aea22SJiri Olsa long nr = strtoul(argv[i], &end, 10); 216945aea22SJiri Olsa 217945aea22SJiri Olsa if (*end == '\0') { 218945aea22SJiri Olsa if (nr == curr + 1) 219945aea22SJiri Olsa return true; 220945aea22SJiri Olsa continue; 221945aea22SJiri Olsa } 222945aea22SJiri Olsa 223e8210cefSMatt Fleming if (strstr(test->desc, argv[i])) 224945aea22SJiri Olsa return true; 225945aea22SJiri Olsa } 226945aea22SJiri Olsa 227945aea22SJiri Olsa return false; 228945aea22SJiri Olsa } 229945aea22SJiri Olsa 2300d8a5faaSJiri Olsa static int run_test(struct test *test) 2310d8a5faaSJiri Olsa { 2320d8a5faaSJiri Olsa int status, err = -1, child = fork(); 233ba3dfff8SMasami Hiramatsu char sbuf[STRERR_BUFSIZE]; 2340d8a5faaSJiri Olsa 2350d8a5faaSJiri Olsa if (child < 0) { 236ba3dfff8SMasami Hiramatsu pr_err("failed to fork test: %s\n", 237ba3dfff8SMasami Hiramatsu strerror_r(errno, sbuf, sizeof(sbuf))); 2380d8a5faaSJiri Olsa return -1; 2390d8a5faaSJiri Olsa } 2400d8a5faaSJiri Olsa 2410d8a5faaSJiri Olsa if (!child) { 2420d8a5faaSJiri Olsa pr_debug("test child forked, pid %d\n", getpid()); 2430d8a5faaSJiri Olsa err = test->func(); 2440d8a5faaSJiri Olsa exit(err); 2450d8a5faaSJiri Olsa } 2460d8a5faaSJiri Olsa 2470d8a5faaSJiri Olsa wait(&status); 2480d8a5faaSJiri Olsa 2490d8a5faaSJiri Olsa if (WIFEXITED(status)) { 250189c466fSHe Kuang err = (signed char)WEXITSTATUS(status); 2510d8a5faaSJiri Olsa pr_debug("test child finished with %d\n", err); 2520d8a5faaSJiri Olsa } else if (WIFSIGNALED(status)) { 2530d8a5faaSJiri Olsa err = -1; 2540d8a5faaSJiri Olsa pr_debug("test child interrupted\n"); 2550d8a5faaSJiri Olsa } 2560d8a5faaSJiri Olsa 2570d8a5faaSJiri Olsa return err; 2580d8a5faaSJiri Olsa } 2590d8a5faaSJiri Olsa 260*31b6753fSMatt Fleming #define for_each_test(j, t) \ 261*31b6753fSMatt Fleming for (j = 0; j < ARRAY_SIZE(tests); j++) \ 262*31b6753fSMatt Fleming for (t = &tests[j][0]; t->func; t++) 263e8210cefSMatt Fleming 2642ae82878SArnaldo Carvalho de Melo static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) 265945aea22SJiri Olsa { 266e8210cefSMatt Fleming struct test *t; 267*31b6753fSMatt Fleming unsigned int j; 268945aea22SJiri Olsa int i = 0; 269945aea22SJiri Olsa int width = 0; 270945aea22SJiri Olsa 271*31b6753fSMatt Fleming for_each_test(j, t) { 272e8210cefSMatt Fleming int len = strlen(t->desc); 273945aea22SJiri Olsa 274945aea22SJiri Olsa if (width < len) 275945aea22SJiri Olsa width = len; 276945aea22SJiri Olsa } 277945aea22SJiri Olsa 278*31b6753fSMatt Fleming for_each_test(j, t) { 279945aea22SJiri Olsa int curr = i++, err; 280945aea22SJiri Olsa 281e8210cefSMatt Fleming if (!perf_test__matches(t, curr, argc, argv)) 282945aea22SJiri Olsa continue; 283945aea22SJiri Olsa 284e8210cefSMatt Fleming pr_info("%2d: %-*s:", i, width, t->desc); 2852ae82878SArnaldo Carvalho de Melo 2862ae82878SArnaldo Carvalho de Melo if (intlist__find(skiplist, i)) { 2872ae82878SArnaldo Carvalho de Melo color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 2882ae82878SArnaldo Carvalho de Melo continue; 2892ae82878SArnaldo Carvalho de Melo } 2902ae82878SArnaldo Carvalho de Melo 291945aea22SJiri Olsa pr_debug("\n--- start ---\n"); 292e8210cefSMatt Fleming err = run_test(t); 293e8210cefSMatt Fleming pr_debug("---- end ----\n%s:", t->desc); 294f4c1ea5fSJiri Olsa 295f4c1ea5fSJiri Olsa switch (err) { 296f4c1ea5fSJiri Olsa case TEST_OK: 297945aea22SJiri Olsa pr_info(" Ok\n"); 298f4c1ea5fSJiri Olsa break; 299f4c1ea5fSJiri Olsa case TEST_SKIP: 300f4c1ea5fSJiri Olsa color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n"); 301f4c1ea5fSJiri Olsa break; 302f4c1ea5fSJiri Olsa case TEST_FAIL: 303f4c1ea5fSJiri Olsa default: 304f4c1ea5fSJiri Olsa color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n"); 305f4c1ea5fSJiri Olsa break; 306f4c1ea5fSJiri Olsa } 307945aea22SJiri Olsa } 308945aea22SJiri Olsa 309945aea22SJiri Olsa return 0; 310945aea22SJiri Olsa } 311945aea22SJiri Olsa 312945aea22SJiri Olsa static int perf_test__list(int argc, const char **argv) 313945aea22SJiri Olsa { 314*31b6753fSMatt Fleming unsigned int j; 315e8210cefSMatt Fleming struct test *t; 316945aea22SJiri Olsa int i = 0; 317945aea22SJiri Olsa 318*31b6753fSMatt Fleming for_each_test(j, t) { 319e8210cefSMatt Fleming if (argc > 1 && !strstr(t->desc, argv[1])) 320945aea22SJiri Olsa continue; 321945aea22SJiri Olsa 322e8210cefSMatt Fleming pr_info("%2d: %s\n", ++i, t->desc); 323945aea22SJiri Olsa } 324945aea22SJiri Olsa 325945aea22SJiri Olsa return 0; 326945aea22SJiri Olsa } 327945aea22SJiri Olsa 328945aea22SJiri Olsa int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused) 329945aea22SJiri Olsa { 3301f9975f1SYunlong Song const char *test_usage[] = { 331945aea22SJiri Olsa "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", 332945aea22SJiri Olsa NULL, 333945aea22SJiri Olsa }; 3342ae82878SArnaldo Carvalho de Melo const char *skip = NULL; 335945aea22SJiri Olsa const struct option test_options[] = { 3362ae82878SArnaldo Carvalho de Melo OPT_STRING('s', "skip", &skip, "tests", "tests to skip"), 337945aea22SJiri Olsa OPT_INCR('v', "verbose", &verbose, 338945aea22SJiri Olsa "be more verbose (show symbol address, etc)"), 339945aea22SJiri Olsa OPT_END() 340945aea22SJiri Olsa }; 3411f9975f1SYunlong Song const char * const test_subcommands[] = { "list", NULL }; 3422ae82878SArnaldo Carvalho de Melo struct intlist *skiplist = NULL; 343a635fc51SArnaldo Carvalho de Melo int ret = hists__init(); 344a635fc51SArnaldo Carvalho de Melo 345a635fc51SArnaldo Carvalho de Melo if (ret < 0) 346a635fc51SArnaldo Carvalho de Melo return ret; 347945aea22SJiri Olsa 3481f9975f1SYunlong Song argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0); 349945aea22SJiri Olsa if (argc >= 1 && !strcmp(argv[0], "list")) 350945aea22SJiri Olsa return perf_test__list(argc, argv); 351945aea22SJiri Olsa 352945aea22SJiri Olsa symbol_conf.priv_size = sizeof(int); 353945aea22SJiri Olsa symbol_conf.sort_by_name = true; 354945aea22SJiri Olsa symbol_conf.try_vmlinux_path = true; 355945aea22SJiri Olsa 3560a7e6d1bSNamhyung Kim if (symbol__init(NULL) < 0) 357945aea22SJiri Olsa return -1; 358945aea22SJiri Olsa 3592ae82878SArnaldo Carvalho de Melo if (skip != NULL) 3602ae82878SArnaldo Carvalho de Melo skiplist = intlist__new(skip); 3612ae82878SArnaldo Carvalho de Melo 3622ae82878SArnaldo Carvalho de Melo return __cmd_test(argc, argv, skiplist); 363945aea22SJiri Olsa } 364