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" 92ae82878SArnaldo Carvalho de Melo #include "intlist.h" 100a4e1ae6SJiri Olsa #include "tests.h" 11c81251e8SJiri Olsa #include "debug.h" 12c81251e8SJiri Olsa #include "color.h" 13c81251e8SJiri Olsa #include "parse-options.h" 14c81251e8SJiri Olsa #include "symbol.h" 15945aea22SJiri Olsa 16945aea22SJiri Olsa static struct test { 17945aea22SJiri Olsa const char *desc; 18945aea22SJiri Olsa int (*func)(void); 19945aea22SJiri Olsa } tests[] = { 20945aea22SJiri Olsa { 21945aea22SJiri Olsa .desc = "vmlinux symtab matches kallsyms", 22945aea22SJiri Olsa .func = test__vmlinux_matches_kallsyms, 23945aea22SJiri Olsa }, 24945aea22SJiri Olsa { 25945aea22SJiri Olsa .desc = "detect open syscall event", 26945aea22SJiri Olsa .func = test__open_syscall_event, 27945aea22SJiri Olsa }, 28945aea22SJiri Olsa { 29945aea22SJiri Olsa .desc = "detect open syscall event on all cpus", 30945aea22SJiri Olsa .func = test__open_syscall_event_on_all_cpus, 31945aea22SJiri Olsa }, 32945aea22SJiri Olsa { 33945aea22SJiri Olsa .desc = "read samples using the mmap interface", 34945aea22SJiri Olsa .func = test__basic_mmap, 35945aea22SJiri Olsa }, 36945aea22SJiri Olsa { 37945aea22SJiri Olsa .desc = "parse events tests", 38c81251e8SJiri Olsa .func = test__parse_events, 39945aea22SJiri Olsa }, 40945aea22SJiri Olsa #if defined(__x86_64__) || defined(__i386__) 41945aea22SJiri Olsa { 42945aea22SJiri Olsa .desc = "x86 rdpmc test", 43945aea22SJiri Olsa .func = test__rdpmc, 44945aea22SJiri Olsa }, 45945aea22SJiri Olsa #endif 46945aea22SJiri Olsa { 47945aea22SJiri Olsa .desc = "Validate PERF_RECORD_* events & perf_sample fields", 48945aea22SJiri Olsa .func = test__PERF_RECORD, 49945aea22SJiri Olsa }, 50945aea22SJiri Olsa { 51945aea22SJiri Olsa .desc = "Test perf pmu format parsing", 52cff7f956SJiri Olsa .func = test__pmu, 53945aea22SJiri Olsa }, 54945aea22SJiri Olsa { 554ebbcb84SJiri Olsa .desc = "Test dso data read", 56c81251e8SJiri Olsa .func = test__dso_data, 57945aea22SJiri Olsa }, 58945aea22SJiri Olsa { 594ebbcb84SJiri Olsa .desc = "Test dso data cache", 604ebbcb84SJiri Olsa .func = test__dso_data_cache, 614ebbcb84SJiri Olsa }, 624ebbcb84SJiri Olsa { 6345dc1bb5SJiri Olsa .desc = "Test dso data reopen", 6445dc1bb5SJiri Olsa .func = test__dso_data_reopen, 6545dc1bb5SJiri Olsa }, 6645dc1bb5SJiri Olsa { 67945aea22SJiri Olsa .desc = "roundtrip evsel->name check", 68cfffae2eSJiri Olsa .func = test__perf_evsel__roundtrip_name_test, 69945aea22SJiri Olsa }, 70945aea22SJiri Olsa { 71945aea22SJiri Olsa .desc = "Check parsing of sched tracepoints fields", 725e24a090SJiri Olsa .func = test__perf_evsel__tp_sched_test, 73945aea22SJiri Olsa }, 74945aea22SJiri Olsa { 75945aea22SJiri Olsa .desc = "Generate and check syscalls:sys_enter_open event fields", 76945aea22SJiri Olsa .func = test__syscall_open_tp_fields, 77945aea22SJiri Olsa }, 78945aea22SJiri Olsa { 79d898b241SJiri Olsa .desc = "struct perf_event_attr setup", 80c81251e8SJiri Olsa .func = test__attr, 81d898b241SJiri Olsa }, 82d898b241SJiri Olsa { 83ffcbaa14SArnaldo Carvalho de Melo .desc = "Test matching and linking multiple hists", 84f8ebb0cdSNamhyung Kim .func = test__hists_link, 85f8ebb0cdSNamhyung Kim }, 86f8ebb0cdSNamhyung Kim { 8754359d33SArnaldo Carvalho de Melo .desc = "Try 'use perf' in python, checking link problems", 8854359d33SArnaldo Carvalho de Melo .func = test__python_use, 8954359d33SArnaldo Carvalho de Melo }, 9054359d33SArnaldo Carvalho de Melo { 915a6bef47SJiri Olsa .desc = "Test breakpoint overflow signal handler", 925a6bef47SJiri Olsa .func = test__bp_signal, 935a6bef47SJiri Olsa }, 945a6bef47SJiri Olsa { 9506933e3aSJiri Olsa .desc = "Test breakpoint overflow sampling", 9606933e3aSJiri Olsa .func = test__bp_signal_overflow, 9706933e3aSJiri Olsa }, 9806933e3aSJiri Olsa { 99d723a550SNamhyung Kim .desc = "Test number of exit event of a simple workload", 100d723a550SNamhyung Kim .func = test__task_exit, 101d723a550SNamhyung Kim }, 102d723a550SNamhyung Kim { 103bc96b361SNamhyung Kim .desc = "Test software clock events have valid period values", 104bc96b361SNamhyung Kim .func = test__sw_clock_freq, 105bc96b361SNamhyung Kim }, 1063bd5a5fcSAdrian Hunter #if defined(__x86_64__) || defined(__i386__) 1073bd5a5fcSAdrian Hunter { 1083bd5a5fcSAdrian Hunter .desc = "Test converting perf time to TSC", 1093bd5a5fcSAdrian Hunter .func = test__perf_time_to_tsc, 1103bd5a5fcSAdrian Hunter }, 1113bd5a5fcSAdrian Hunter #endif 112bc96b361SNamhyung Kim { 113b55ae0a9SAdrian Hunter .desc = "Test object code reading", 114b55ae0a9SAdrian Hunter .func = test__code_reading, 115b55ae0a9SAdrian Hunter }, 116b55ae0a9SAdrian Hunter { 117045f8cd8SAdrian Hunter .desc = "Test sample parsing", 118045f8cd8SAdrian Hunter .func = test__sample_parsing, 119045f8cd8SAdrian Hunter }, 120045f8cd8SAdrian Hunter { 121395c3070SAdrian Hunter .desc = "Test using a dummy software event to keep tracking", 122395c3070SAdrian Hunter .func = test__keep_tracking, 123395c3070SAdrian Hunter }, 124395c3070SAdrian Hunter { 12553a277e5SAdrian Hunter .desc = "Test parsing with no sample_id_all bit set", 12653a277e5SAdrian Hunter .func = test__parse_no_sample_id_all, 12753a277e5SAdrian Hunter }, 12890fa9debSJean Pihet #if defined(__x86_64__) || defined(__i386__) || defined(__arm__) 1299ff125d1SJiri Olsa #ifdef HAVE_DWARF_UNWIND_SUPPORT 130aa16b81fSJiri Olsa { 131aa16b81fSJiri Olsa .desc = "Test dwarf unwind", 132aa16b81fSJiri Olsa .func = test__dwarf_unwind, 133aa16b81fSJiri Olsa }, 134aa16b81fSJiri Olsa #endif 135aa16b81fSJiri Olsa #endif 13653a277e5SAdrian Hunter { 1373c3cfd99SNamhyung Kim .desc = "Test filtering hist entries", 1383c3cfd99SNamhyung Kim .func = test__hists_filter, 1393c3cfd99SNamhyung Kim }, 1403c3cfd99SNamhyung Kim { 1414e85edfcSJiri Olsa .desc = "Test mmap thread lookup", 1424e85edfcSJiri Olsa .func = test__mmap_thread_lookup, 1434e85edfcSJiri Olsa }, 1444e85edfcSJiri Olsa { 145fabf0123SJiri Olsa .desc = "Test thread mg sharing", 146fabf0123SJiri Olsa .func = test__thread_mg_share, 147fabf0123SJiri Olsa }, 148fabf0123SJiri Olsa { 149f21d1815SNamhyung Kim .desc = "Test output sorting of hist entries", 150f21d1815SNamhyung Kim .func = test__hists_output, 151f21d1815SNamhyung Kim }, 152f21d1815SNamhyung Kim { 1530506aeccSNamhyung Kim .desc = "Test cumulation of child hist entries", 1540506aeccSNamhyung Kim .func = test__hists_cumulate, 1550506aeccSNamhyung Kim }, 1560506aeccSNamhyung Kim { 157945aea22SJiri Olsa .func = NULL, 158945aea22SJiri Olsa }, 159945aea22SJiri Olsa }; 160945aea22SJiri Olsa 161945aea22SJiri Olsa static bool perf_test__matches(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 178945aea22SJiri Olsa if (strstr(tests[curr].desc, argv[i])) 179945aea22SJiri Olsa return true; 180945aea22SJiri Olsa } 181945aea22SJiri Olsa 182945aea22SJiri Olsa return false; 183945aea22SJiri Olsa } 184945aea22SJiri Olsa 1850d8a5faaSJiri Olsa static int run_test(struct test *test) 1860d8a5faaSJiri Olsa { 1870d8a5faaSJiri Olsa int status, err = -1, child = fork(); 188*ba3dfff8SMasami Hiramatsu char sbuf[STRERR_BUFSIZE]; 1890d8a5faaSJiri Olsa 1900d8a5faaSJiri Olsa if (child < 0) { 191*ba3dfff8SMasami Hiramatsu pr_err("failed to fork test: %s\n", 192*ba3dfff8SMasami Hiramatsu strerror_r(errno, sbuf, sizeof(sbuf))); 1930d8a5faaSJiri Olsa return -1; 1940d8a5faaSJiri Olsa } 1950d8a5faaSJiri Olsa 1960d8a5faaSJiri Olsa if (!child) { 1970d8a5faaSJiri Olsa pr_debug("test child forked, pid %d\n", getpid()); 1980d8a5faaSJiri Olsa err = test->func(); 1990d8a5faaSJiri Olsa exit(err); 2000d8a5faaSJiri Olsa } 2010d8a5faaSJiri Olsa 2020d8a5faaSJiri Olsa wait(&status); 2030d8a5faaSJiri Olsa 2040d8a5faaSJiri Olsa if (WIFEXITED(status)) { 2050d8a5faaSJiri Olsa err = WEXITSTATUS(status); 2060d8a5faaSJiri Olsa pr_debug("test child finished with %d\n", err); 2070d8a5faaSJiri Olsa } else if (WIFSIGNALED(status)) { 2080d8a5faaSJiri Olsa err = -1; 2090d8a5faaSJiri Olsa pr_debug("test child interrupted\n"); 2100d8a5faaSJiri Olsa } 2110d8a5faaSJiri Olsa 2120d8a5faaSJiri Olsa return err; 2130d8a5faaSJiri Olsa } 2140d8a5faaSJiri Olsa 2152ae82878SArnaldo Carvalho de Melo static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) 216945aea22SJiri Olsa { 217945aea22SJiri Olsa int i = 0; 218945aea22SJiri Olsa int width = 0; 219945aea22SJiri Olsa 220945aea22SJiri Olsa while (tests[i].func) { 221945aea22SJiri Olsa int len = strlen(tests[i].desc); 222945aea22SJiri Olsa 223945aea22SJiri Olsa if (width < len) 224945aea22SJiri Olsa width = len; 225945aea22SJiri Olsa ++i; 226945aea22SJiri Olsa } 227945aea22SJiri Olsa 228945aea22SJiri Olsa i = 0; 229945aea22SJiri Olsa while (tests[i].func) { 230945aea22SJiri Olsa int curr = i++, err; 231945aea22SJiri Olsa 232945aea22SJiri Olsa if (!perf_test__matches(curr, argc, argv)) 233945aea22SJiri Olsa continue; 234945aea22SJiri Olsa 235945aea22SJiri Olsa pr_info("%2d: %-*s:", i, width, tests[curr].desc); 2362ae82878SArnaldo Carvalho de Melo 2372ae82878SArnaldo Carvalho de Melo if (intlist__find(skiplist, i)) { 2382ae82878SArnaldo Carvalho de Melo color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 2392ae82878SArnaldo Carvalho de Melo continue; 2402ae82878SArnaldo Carvalho de Melo } 2412ae82878SArnaldo Carvalho de Melo 242945aea22SJiri Olsa pr_debug("\n--- start ---\n"); 2430d8a5faaSJiri Olsa err = run_test(&tests[curr]); 244945aea22SJiri Olsa pr_debug("---- end ----\n%s:", tests[curr].desc); 245f4c1ea5fSJiri Olsa 246f4c1ea5fSJiri Olsa switch (err) { 247f4c1ea5fSJiri Olsa case TEST_OK: 248945aea22SJiri Olsa pr_info(" Ok\n"); 249f4c1ea5fSJiri Olsa break; 250f4c1ea5fSJiri Olsa case TEST_SKIP: 251f4c1ea5fSJiri Olsa color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n"); 252f4c1ea5fSJiri Olsa break; 253f4c1ea5fSJiri Olsa case TEST_FAIL: 254f4c1ea5fSJiri Olsa default: 255f4c1ea5fSJiri Olsa color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n"); 256f4c1ea5fSJiri Olsa break; 257f4c1ea5fSJiri Olsa } 258945aea22SJiri Olsa } 259945aea22SJiri Olsa 260945aea22SJiri Olsa return 0; 261945aea22SJiri Olsa } 262945aea22SJiri Olsa 263945aea22SJiri Olsa static int perf_test__list(int argc, const char **argv) 264945aea22SJiri Olsa { 265945aea22SJiri Olsa int i = 0; 266945aea22SJiri Olsa 267945aea22SJiri Olsa while (tests[i].func) { 268945aea22SJiri Olsa int curr = i++; 269945aea22SJiri Olsa 270945aea22SJiri Olsa if (argc > 1 && !strstr(tests[curr].desc, argv[1])) 271945aea22SJiri Olsa continue; 272945aea22SJiri Olsa 273945aea22SJiri Olsa pr_info("%2d: %s\n", i, tests[curr].desc); 274945aea22SJiri Olsa } 275945aea22SJiri Olsa 276945aea22SJiri Olsa return 0; 277945aea22SJiri Olsa } 278945aea22SJiri Olsa 279945aea22SJiri Olsa int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused) 280945aea22SJiri Olsa { 281945aea22SJiri Olsa const char * const test_usage[] = { 282945aea22SJiri Olsa "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", 283945aea22SJiri Olsa NULL, 284945aea22SJiri Olsa }; 2852ae82878SArnaldo Carvalho de Melo const char *skip = NULL; 286945aea22SJiri Olsa const struct option test_options[] = { 2872ae82878SArnaldo Carvalho de Melo OPT_STRING('s', "skip", &skip, "tests", "tests to skip"), 288945aea22SJiri Olsa OPT_INCR('v', "verbose", &verbose, 289945aea22SJiri Olsa "be more verbose (show symbol address, etc)"), 290945aea22SJiri Olsa OPT_END() 291945aea22SJiri Olsa }; 2922ae82878SArnaldo Carvalho de Melo struct intlist *skiplist = NULL; 293945aea22SJiri Olsa 294945aea22SJiri Olsa argc = parse_options(argc, argv, test_options, test_usage, 0); 295945aea22SJiri Olsa if (argc >= 1 && !strcmp(argv[0], "list")) 296945aea22SJiri Olsa return perf_test__list(argc, argv); 297945aea22SJiri Olsa 298945aea22SJiri Olsa symbol_conf.priv_size = sizeof(int); 299945aea22SJiri Olsa symbol_conf.sort_by_name = true; 300945aea22SJiri Olsa symbol_conf.try_vmlinux_path = true; 301945aea22SJiri Olsa 3020a7e6d1bSNamhyung Kim if (symbol__init(NULL) < 0) 303945aea22SJiri Olsa return -1; 304945aea22SJiri Olsa 3052ae82878SArnaldo Carvalho de Melo if (skip != NULL) 3062ae82878SArnaldo Carvalho de Melo skiplist = intlist__new(skip); 3072ae82878SArnaldo Carvalho de Melo 3082ae82878SArnaldo Carvalho de Melo return __cmd_test(argc, argv, skiplist); 309945aea22SJiri Olsa } 310