1945aea22SJiri Olsa /* 2945aea22SJiri Olsa * builtin-test.c 3945aea22SJiri Olsa * 4945aea22SJiri Olsa * Builtin regression testing command: ever growing number of sanity tests 5945aea22SJiri Olsa */ 6945aea22SJiri Olsa #include "builtin.h" 72ae82878SArnaldo Carvalho de Melo #include "intlist.h" 80a4e1ae6SJiri Olsa #include "tests.h" 9c81251e8SJiri Olsa #include "debug.h" 10c81251e8SJiri Olsa #include "color.h" 11c81251e8SJiri Olsa #include "parse-options.h" 12c81251e8SJiri Olsa #include "symbol.h" 13945aea22SJiri Olsa 14945aea22SJiri Olsa static struct test { 15945aea22SJiri Olsa const char *desc; 16945aea22SJiri Olsa int (*func)(void); 17945aea22SJiri Olsa } tests[] = { 18945aea22SJiri Olsa { 19945aea22SJiri Olsa .desc = "vmlinux symtab matches kallsyms", 20945aea22SJiri Olsa .func = test__vmlinux_matches_kallsyms, 21945aea22SJiri Olsa }, 22945aea22SJiri Olsa { 23945aea22SJiri Olsa .desc = "detect open syscall event", 24945aea22SJiri Olsa .func = test__open_syscall_event, 25945aea22SJiri Olsa }, 26945aea22SJiri Olsa { 27945aea22SJiri Olsa .desc = "detect open syscall event on all cpus", 28945aea22SJiri Olsa .func = test__open_syscall_event_on_all_cpus, 29945aea22SJiri Olsa }, 30945aea22SJiri Olsa { 31945aea22SJiri Olsa .desc = "read samples using the mmap interface", 32945aea22SJiri Olsa .func = test__basic_mmap, 33945aea22SJiri Olsa }, 34945aea22SJiri Olsa { 35945aea22SJiri Olsa .desc = "parse events tests", 36c81251e8SJiri Olsa .func = test__parse_events, 37945aea22SJiri Olsa }, 38945aea22SJiri Olsa #if defined(__x86_64__) || defined(__i386__) 39945aea22SJiri Olsa { 40945aea22SJiri Olsa .desc = "x86 rdpmc test", 41945aea22SJiri Olsa .func = test__rdpmc, 42945aea22SJiri Olsa }, 43945aea22SJiri Olsa #endif 44945aea22SJiri Olsa { 45945aea22SJiri Olsa .desc = "Validate PERF_RECORD_* events & perf_sample fields", 46945aea22SJiri Olsa .func = test__PERF_RECORD, 47945aea22SJiri Olsa }, 48945aea22SJiri Olsa { 49945aea22SJiri Olsa .desc = "Test perf pmu format parsing", 50cff7f956SJiri Olsa .func = test__pmu, 51945aea22SJiri Olsa }, 52945aea22SJiri Olsa { 53945aea22SJiri Olsa .desc = "Test dso data interface", 54c81251e8SJiri Olsa .func = test__dso_data, 55945aea22SJiri Olsa }, 56945aea22SJiri Olsa { 57945aea22SJiri Olsa .desc = "roundtrip evsel->name check", 58cfffae2eSJiri Olsa .func = test__perf_evsel__roundtrip_name_test, 59945aea22SJiri Olsa }, 60945aea22SJiri Olsa { 61945aea22SJiri Olsa .desc = "Check parsing of sched tracepoints fields", 625e24a090SJiri Olsa .func = test__perf_evsel__tp_sched_test, 63945aea22SJiri Olsa }, 64945aea22SJiri Olsa { 65945aea22SJiri Olsa .desc = "Generate and check syscalls:sys_enter_open event fields", 66945aea22SJiri Olsa .func = test__syscall_open_tp_fields, 67945aea22SJiri Olsa }, 68945aea22SJiri Olsa { 69d898b241SJiri Olsa .desc = "struct perf_event_attr setup", 70c81251e8SJiri Olsa .func = test__attr, 71d898b241SJiri Olsa }, 72d898b241SJiri Olsa { 73ffcbaa14SArnaldo Carvalho de Melo .desc = "Test matching and linking multiple hists", 74f8ebb0cdSNamhyung Kim .func = test__hists_link, 75f8ebb0cdSNamhyung Kim }, 76f8ebb0cdSNamhyung Kim { 7754359d33SArnaldo Carvalho de Melo .desc = "Try 'use perf' in python, checking link problems", 7854359d33SArnaldo Carvalho de Melo .func = test__python_use, 7954359d33SArnaldo Carvalho de Melo }, 8054359d33SArnaldo Carvalho de Melo { 815a6bef47SJiri Olsa .desc = "Test breakpoint overflow signal handler", 825a6bef47SJiri Olsa .func = test__bp_signal, 835a6bef47SJiri Olsa }, 845a6bef47SJiri Olsa { 8506933e3aSJiri Olsa .desc = "Test breakpoint overflow sampling", 8606933e3aSJiri Olsa .func = test__bp_signal_overflow, 8706933e3aSJiri Olsa }, 8806933e3aSJiri Olsa { 89d723a550SNamhyung Kim .desc = "Test number of exit event of a simple workload", 90d723a550SNamhyung Kim .func = test__task_exit, 91d723a550SNamhyung Kim }, 92d723a550SNamhyung Kim { 93bc96b361SNamhyung Kim .desc = "Test software clock events have valid period values", 94bc96b361SNamhyung Kim .func = test__sw_clock_freq, 95bc96b361SNamhyung Kim }, 963bd5a5fcSAdrian Hunter #if defined(__x86_64__) || defined(__i386__) 973bd5a5fcSAdrian Hunter { 983bd5a5fcSAdrian Hunter .desc = "Test converting perf time to TSC", 993bd5a5fcSAdrian Hunter .func = test__perf_time_to_tsc, 1003bd5a5fcSAdrian Hunter }, 1013bd5a5fcSAdrian Hunter #endif 102bc96b361SNamhyung Kim { 103b55ae0a9SAdrian Hunter .desc = "Test object code reading", 104b55ae0a9SAdrian Hunter .func = test__code_reading, 105b55ae0a9SAdrian Hunter }, 106b55ae0a9SAdrian Hunter { 107045f8cd8SAdrian Hunter .desc = "Test sample parsing", 108045f8cd8SAdrian Hunter .func = test__sample_parsing, 109045f8cd8SAdrian Hunter }, 110045f8cd8SAdrian Hunter { 111395c3070SAdrian Hunter .desc = "Test using a dummy software event to keep tracking", 112395c3070SAdrian Hunter .func = test__keep_tracking, 113395c3070SAdrian Hunter }, 114395c3070SAdrian Hunter { 11553a277e5SAdrian Hunter .desc = "Test parsing with no sample_id_all bit set", 11653a277e5SAdrian Hunter .func = test__parse_no_sample_id_all, 11753a277e5SAdrian Hunter }, 118aa16b81fSJiri Olsa #if defined(__x86_64__) || defined(__i386__) 1199ff125d1SJiri Olsa #ifdef HAVE_DWARF_UNWIND_SUPPORT 120aa16b81fSJiri Olsa { 121aa16b81fSJiri Olsa .desc = "Test dwarf unwind", 122aa16b81fSJiri Olsa .func = test__dwarf_unwind, 123aa16b81fSJiri Olsa }, 124aa16b81fSJiri Olsa #endif 125aa16b81fSJiri Olsa #endif 12653a277e5SAdrian Hunter { 127*3c3cfd99SNamhyung Kim .desc = "Test filtering hist entries", 128*3c3cfd99SNamhyung Kim .func = test__hists_filter, 129*3c3cfd99SNamhyung Kim }, 130*3c3cfd99SNamhyung Kim { 131945aea22SJiri Olsa .func = NULL, 132945aea22SJiri Olsa }, 133945aea22SJiri Olsa }; 134945aea22SJiri Olsa 135945aea22SJiri Olsa static bool perf_test__matches(int curr, int argc, const char *argv[]) 136945aea22SJiri Olsa { 137945aea22SJiri Olsa int i; 138945aea22SJiri Olsa 139945aea22SJiri Olsa if (argc == 0) 140945aea22SJiri Olsa return true; 141945aea22SJiri Olsa 142945aea22SJiri Olsa for (i = 0; i < argc; ++i) { 143945aea22SJiri Olsa char *end; 144945aea22SJiri Olsa long nr = strtoul(argv[i], &end, 10); 145945aea22SJiri Olsa 146945aea22SJiri Olsa if (*end == '\0') { 147945aea22SJiri Olsa if (nr == curr + 1) 148945aea22SJiri Olsa return true; 149945aea22SJiri Olsa continue; 150945aea22SJiri Olsa } 151945aea22SJiri Olsa 152945aea22SJiri Olsa if (strstr(tests[curr].desc, argv[i])) 153945aea22SJiri Olsa return true; 154945aea22SJiri Olsa } 155945aea22SJiri Olsa 156945aea22SJiri Olsa return false; 157945aea22SJiri Olsa } 158945aea22SJiri Olsa 1592ae82878SArnaldo Carvalho de Melo static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) 160945aea22SJiri Olsa { 161945aea22SJiri Olsa int i = 0; 162945aea22SJiri Olsa int width = 0; 163945aea22SJiri Olsa 164945aea22SJiri Olsa while (tests[i].func) { 165945aea22SJiri Olsa int len = strlen(tests[i].desc); 166945aea22SJiri Olsa 167945aea22SJiri Olsa if (width < len) 168945aea22SJiri Olsa width = len; 169945aea22SJiri Olsa ++i; 170945aea22SJiri Olsa } 171945aea22SJiri Olsa 172945aea22SJiri Olsa i = 0; 173945aea22SJiri Olsa while (tests[i].func) { 174945aea22SJiri Olsa int curr = i++, err; 175945aea22SJiri Olsa 176945aea22SJiri Olsa if (!perf_test__matches(curr, argc, argv)) 177945aea22SJiri Olsa continue; 178945aea22SJiri Olsa 179945aea22SJiri Olsa pr_info("%2d: %-*s:", i, width, tests[curr].desc); 1802ae82878SArnaldo Carvalho de Melo 1812ae82878SArnaldo Carvalho de Melo if (intlist__find(skiplist, i)) { 1822ae82878SArnaldo Carvalho de Melo color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); 1832ae82878SArnaldo Carvalho de Melo continue; 1842ae82878SArnaldo Carvalho de Melo } 1852ae82878SArnaldo Carvalho de Melo 186945aea22SJiri Olsa pr_debug("\n--- start ---\n"); 187945aea22SJiri Olsa err = tests[curr].func(); 188945aea22SJiri Olsa pr_debug("---- end ----\n%s:", tests[curr].desc); 189f4c1ea5fSJiri Olsa 190f4c1ea5fSJiri Olsa switch (err) { 191f4c1ea5fSJiri Olsa case TEST_OK: 192945aea22SJiri Olsa pr_info(" Ok\n"); 193f4c1ea5fSJiri Olsa break; 194f4c1ea5fSJiri Olsa case TEST_SKIP: 195f4c1ea5fSJiri Olsa color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n"); 196f4c1ea5fSJiri Olsa break; 197f4c1ea5fSJiri Olsa case TEST_FAIL: 198f4c1ea5fSJiri Olsa default: 199f4c1ea5fSJiri Olsa color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n"); 200f4c1ea5fSJiri Olsa break; 201f4c1ea5fSJiri Olsa } 202945aea22SJiri Olsa } 203945aea22SJiri Olsa 204945aea22SJiri Olsa return 0; 205945aea22SJiri Olsa } 206945aea22SJiri Olsa 207945aea22SJiri Olsa static int perf_test__list(int argc, const char **argv) 208945aea22SJiri Olsa { 209945aea22SJiri Olsa int i = 0; 210945aea22SJiri Olsa 211945aea22SJiri Olsa while (tests[i].func) { 212945aea22SJiri Olsa int curr = i++; 213945aea22SJiri Olsa 214945aea22SJiri Olsa if (argc > 1 && !strstr(tests[curr].desc, argv[1])) 215945aea22SJiri Olsa continue; 216945aea22SJiri Olsa 217945aea22SJiri Olsa pr_info("%2d: %s\n", i, tests[curr].desc); 218945aea22SJiri Olsa } 219945aea22SJiri Olsa 220945aea22SJiri Olsa return 0; 221945aea22SJiri Olsa } 222945aea22SJiri Olsa 223945aea22SJiri Olsa int cmd_test(int argc, const char **argv, const char *prefix __maybe_unused) 224945aea22SJiri Olsa { 225945aea22SJiri Olsa const char * const test_usage[] = { 226945aea22SJiri Olsa "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]", 227945aea22SJiri Olsa NULL, 228945aea22SJiri Olsa }; 2292ae82878SArnaldo Carvalho de Melo const char *skip = NULL; 230945aea22SJiri Olsa const struct option test_options[] = { 2312ae82878SArnaldo Carvalho de Melo OPT_STRING('s', "skip", &skip, "tests", "tests to skip"), 232945aea22SJiri Olsa OPT_INCR('v', "verbose", &verbose, 233945aea22SJiri Olsa "be more verbose (show symbol address, etc)"), 234945aea22SJiri Olsa OPT_END() 235945aea22SJiri Olsa }; 2362ae82878SArnaldo Carvalho de Melo struct intlist *skiplist = NULL; 237945aea22SJiri Olsa 238945aea22SJiri Olsa argc = parse_options(argc, argv, test_options, test_usage, 0); 239945aea22SJiri Olsa if (argc >= 1 && !strcmp(argv[0], "list")) 240945aea22SJiri Olsa return perf_test__list(argc, argv); 241945aea22SJiri Olsa 242945aea22SJiri Olsa symbol_conf.priv_size = sizeof(int); 243945aea22SJiri Olsa symbol_conf.sort_by_name = true; 244945aea22SJiri Olsa symbol_conf.try_vmlinux_path = true; 245945aea22SJiri Olsa 246945aea22SJiri Olsa if (symbol__init() < 0) 247945aea22SJiri Olsa return -1; 248945aea22SJiri Olsa 2492ae82878SArnaldo Carvalho de Melo if (skip != NULL) 2502ae82878SArnaldo Carvalho de Melo skiplist = intlist__new(skip); 2512ae82878SArnaldo Carvalho de Melo 2522ae82878SArnaldo Carvalho de Melo return __cmd_test(argc, argv, skiplist); 253945aea22SJiri Olsa } 254