builtin-test.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-test.c
  4. *
  5. * Builtin regression testing command: ever growing number of sanity tests
  6. */
  7. #include <ctype.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #ifdef HAVE_BACKTRACE_SUPPORT
  11. #include <execinfo.h>
  12. #endif
  13. #include <poll.h>
  14. #include <unistd.h>
  15. #include <setjmp.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <sys/types.h>
  19. #include <dirent.h>
  20. #include <sys/wait.h>
  21. #include <sys/stat.h>
  22. #include "builtin.h"
  23. #include "config.h"
  24. #include "hist.h"
  25. #include "intlist.h"
  26. #include "tests.h"
  27. #include "debug.h"
  28. #include "color.h"
  29. #include <subcmd/parse-options.h>
  30. #include <subcmd/run-command.h>
  31. #include "string2.h"
  32. #include "symbol.h"
  33. #include "util/rlimit.h"
  34. #include "util/strbuf.h"
  35. #include <linux/kernel.h>
  36. #include <linux/string.h>
  37. #include <subcmd/exec-cmd.h>
  38. #include <linux/zalloc.h>
  39. #include "tests-scripts.h"
  40. /*
  41. * Command line option to not fork the test running in the same process and
  42. * making them easier to debug.
  43. */
  44. static bool dont_fork;
  45. /* Fork the tests in parallel and wait for their completion. */
  46. static bool sequential;
  47. /* Number of times each test is run. */
  48. static unsigned int runs_per_test = 1;
  49. const char *dso_to_test;
  50. const char *test_objdump_path = "objdump";
  51. /*
  52. * List of architecture specific tests. Not a weak symbol as the array length is
  53. * dependent on the initialization, as such GCC with LTO complains of
  54. * conflicting definitions with a weak symbol.
  55. */
  56. #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || defined(__powerpc64__)
  57. extern struct test_suite *arch_tests[];
  58. #else
  59. static struct test_suite *arch_tests[] = {
  60. NULL,
  61. };
  62. #endif
  63. static struct test_suite *generic_tests[] = {
  64. &suite__vmlinux_matches_kallsyms,
  65. &suite__openat_syscall_event,
  66. &suite__openat_syscall_event_on_all_cpus,
  67. &suite__basic_mmap,
  68. &suite__mem,
  69. &suite__parse_events,
  70. &suite__expr,
  71. &suite__PERF_RECORD,
  72. &suite__pmu,
  73. &suite__pmu_events,
  74. &suite__hwmon_pmu,
  75. &suite__tool_pmu,
  76. &suite__dso_data,
  77. &suite__perf_evsel__roundtrip_name_test,
  78. #ifdef HAVE_LIBTRACEEVENT
  79. &suite__perf_evsel__tp_sched_test,
  80. &suite__syscall_openat_tp_fields,
  81. #endif
  82. &suite__hists_link,
  83. &suite__bp_signal,
  84. &suite__bp_signal_overflow,
  85. &suite__bp_accounting,
  86. &suite__wp,
  87. &suite__task_exit,
  88. &suite__sw_clock_freq,
  89. &suite__code_reading,
  90. &suite__sample_parsing,
  91. &suite__keep_tracking,
  92. &suite__parse_no_sample_id_all,
  93. &suite__hists_filter,
  94. &suite__mmap_thread_lookup,
  95. &suite__thread_maps_share,
  96. &suite__hists_output,
  97. &suite__hists_cumulate,
  98. #ifdef HAVE_LIBTRACEEVENT
  99. &suite__switch_tracking,
  100. #endif
  101. &suite__fdarray__filter,
  102. &suite__fdarray__add,
  103. &suite__kmod_path__parse,
  104. &suite__thread_map,
  105. &suite__session_topology,
  106. &suite__thread_map_synthesize,
  107. &suite__thread_map_remove,
  108. &suite__cpu_map,
  109. &suite__synthesize_stat_config,
  110. &suite__synthesize_stat,
  111. &suite__synthesize_stat_round,
  112. &suite__event_update,
  113. &suite__event_times,
  114. &suite__backward_ring_buffer,
  115. &suite__sdt_event,
  116. &suite__is_printable_array,
  117. &suite__bitmap_print,
  118. &suite__perf_hooks,
  119. &suite__unit_number__scnprint,
  120. &suite__mem2node,
  121. &suite__time_utils,
  122. &suite__jit_write_elf,
  123. &suite__pfm,
  124. &suite__api_io,
  125. &suite__maps,
  126. &suite__demangle_java,
  127. &suite__demangle_ocaml,
  128. &suite__demangle_rust,
  129. &suite__parse_metric,
  130. &suite__pe_file_parsing,
  131. &suite__expand_cgroup_events,
  132. &suite__perf_time_to_tsc,
  133. &suite__dlfilter,
  134. &suite__sigtrap,
  135. &suite__event_groups,
  136. &suite__symbols,
  137. &suite__util,
  138. &suite__subcmd_help,
  139. &suite__kallsyms_split,
  140. NULL,
  141. };
  142. static struct test_workload *workloads[] = {
  143. &workload__noploop,
  144. &workload__thloop,
  145. &workload__leafloop,
  146. &workload__sqrtloop,
  147. &workload__brstack,
  148. &workload__datasym,
  149. &workload__landlock,
  150. &workload__traploop,
  151. &workload__inlineloop,
  152. #ifdef HAVE_RUST_SUPPORT
  153. &workload__code_with_type,
  154. #endif
  155. };
  156. #define workloads__for_each(workload) \
  157. for (unsigned i = 0; i < ARRAY_SIZE(workloads) && ({ workload = workloads[i]; 1; }); i++)
  158. #define test_suite__for_each_test_case(suite, idx) \
  159. for (idx = 0; (suite)->test_cases && (suite)->test_cases[idx].name != NULL; idx++)
  160. static void close_parent_fds(void)
  161. {
  162. DIR *dir = opendir("/proc/self/fd");
  163. struct dirent *ent;
  164. while ((ent = readdir(dir))) {
  165. char *end;
  166. long fd;
  167. if (ent->d_type != DT_LNK)
  168. continue;
  169. if (!isdigit(ent->d_name[0]))
  170. continue;
  171. fd = strtol(ent->d_name, &end, 10);
  172. if (*end)
  173. continue;
  174. if (fd <= 3 || fd == dirfd(dir))
  175. continue;
  176. close(fd);
  177. }
  178. closedir(dir);
  179. }
  180. static void check_leaks(void)
  181. {
  182. DIR *dir = opendir("/proc/self/fd");
  183. struct dirent *ent;
  184. int leaks = 0;
  185. while ((ent = readdir(dir))) {
  186. char path[PATH_MAX];
  187. char *end;
  188. long fd;
  189. ssize_t len;
  190. if (ent->d_type != DT_LNK)
  191. continue;
  192. if (!isdigit(ent->d_name[0]))
  193. continue;
  194. fd = strtol(ent->d_name, &end, 10);
  195. if (*end)
  196. continue;
  197. if (fd <= 3 || fd == dirfd(dir))
  198. continue;
  199. leaks++;
  200. len = readlinkat(dirfd(dir), ent->d_name, path, sizeof(path));
  201. if (len > 0 && (size_t)len < sizeof(path))
  202. path[len] = '\0';
  203. else
  204. strncpy(path, ent->d_name, sizeof(path));
  205. pr_err("Leak of file descriptor %s that opened: '%s'\n", ent->d_name, path);
  206. }
  207. closedir(dir);
  208. if (leaks)
  209. abort();
  210. }
  211. static int test_suite__num_test_cases(const struct test_suite *t)
  212. {
  213. int num;
  214. test_suite__for_each_test_case(t, num);
  215. return num;
  216. }
  217. static const char *skip_reason(const struct test_suite *t, int test_case)
  218. {
  219. if (!t->test_cases)
  220. return NULL;
  221. return t->test_cases[test_case >= 0 ? test_case : 0].skip_reason;
  222. }
  223. static const char *test_description(const struct test_suite *t, int test_case)
  224. {
  225. if (t->test_cases && test_case >= 0)
  226. return t->test_cases[test_case].desc;
  227. return t->desc;
  228. }
  229. static test_fnptr test_function(const struct test_suite *t, int test_case)
  230. {
  231. if (test_case <= 0)
  232. return t->test_cases[0].run_case;
  233. return t->test_cases[test_case].run_case;
  234. }
  235. static bool test_exclusive(const struct test_suite *t, int test_case)
  236. {
  237. if (test_case <= 0)
  238. return t->test_cases[0].exclusive;
  239. return t->test_cases[test_case].exclusive;
  240. }
  241. static bool perf_test__matches(const char *desc, int suite_num, int argc, const char *argv[])
  242. {
  243. int i;
  244. if (argc == 0)
  245. return true;
  246. for (i = 0; i < argc; ++i) {
  247. char *end;
  248. long nr = strtoul(argv[i], &end, 10);
  249. if (*end == '\0') {
  250. if (nr == suite_num + 1)
  251. return true;
  252. continue;
  253. }
  254. if (strcasestr(desc, argv[i]))
  255. return true;
  256. }
  257. return false;
  258. }
  259. struct child_test {
  260. struct child_process process;
  261. struct test_suite *test;
  262. int suite_num;
  263. int test_case_num;
  264. };
  265. static jmp_buf run_test_jmp_buf;
  266. static void child_test_sig_handler(int sig)
  267. {
  268. #ifdef HAVE_BACKTRACE_SUPPORT
  269. void *stackdump[32];
  270. size_t stackdump_size;
  271. #endif
  272. fprintf(stderr, "\n---- unexpected signal (%d) ----\n", sig);
  273. #ifdef HAVE_BACKTRACE_SUPPORT
  274. stackdump_size = backtrace(stackdump, ARRAY_SIZE(stackdump));
  275. __dump_stack(stderr, stackdump, stackdump_size);
  276. #endif
  277. siglongjmp(run_test_jmp_buf, sig);
  278. }
  279. static int run_test_child(struct child_process *process)
  280. {
  281. const int signals[] = {
  282. SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGINT, SIGPIPE, SIGQUIT, SIGSEGV, SIGTERM,
  283. };
  284. struct child_test *child = container_of(process, struct child_test, process);
  285. int err;
  286. close_parent_fds();
  287. err = sigsetjmp(run_test_jmp_buf, 1);
  288. if (err) {
  289. /* Received signal. */
  290. err = err > 0 ? -err : -1;
  291. goto err_out;
  292. }
  293. for (size_t i = 0; i < ARRAY_SIZE(signals); i++)
  294. signal(signals[i], child_test_sig_handler);
  295. pr_debug("--- start ---\n");
  296. pr_debug("test child forked, pid %d\n", getpid());
  297. err = test_function(child->test, child->test_case_num)(child->test, child->test_case_num);
  298. pr_debug("---- end(%d) ----\n", err);
  299. check_leaks();
  300. err_out:
  301. fflush(NULL);
  302. for (size_t i = 0; i < ARRAY_SIZE(signals); i++)
  303. signal(signals[i], SIG_DFL);
  304. return -err;
  305. }
  306. #define TEST_RUNNING -3
  307. static int print_test_result(struct test_suite *t, int curr_suite, int curr_test_case,
  308. int result, int width, int running)
  309. {
  310. if (test_suite__num_test_cases(t) > 1) {
  311. int subw = width > 2 ? width - 2 : width;
  312. pr_info("%3d.%1d: %-*s:", curr_suite + 1, curr_test_case + 1, subw,
  313. test_description(t, curr_test_case));
  314. } else
  315. pr_info("%3d: %-*s:", curr_suite + 1, width, test_description(t, curr_test_case));
  316. switch (result) {
  317. case TEST_RUNNING:
  318. color_fprintf(stderr, PERF_COLOR_YELLOW, " Running (%d active)\n", running);
  319. break;
  320. case TEST_OK:
  321. pr_info(" Ok\n");
  322. break;
  323. case TEST_SKIP: {
  324. const char *reason = skip_reason(t, curr_test_case);
  325. if (reason)
  326. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
  327. else
  328. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
  329. }
  330. break;
  331. case TEST_FAIL:
  332. default:
  333. color_fprintf(stderr, PERF_COLOR_RED, " FAILED!\n");
  334. break;
  335. }
  336. return 0;
  337. }
  338. static void finish_test(struct child_test **child_tests, int running_test, int child_test_num,
  339. int width)
  340. {
  341. struct child_test *child_test = child_tests[running_test];
  342. struct test_suite *t;
  343. int curr_suite, curr_test_case, err;
  344. bool err_done = false;
  345. struct strbuf err_output = STRBUF_INIT;
  346. int last_running = -1;
  347. int ret;
  348. if (child_test == NULL) {
  349. /* Test wasn't started. */
  350. return;
  351. }
  352. t = child_test->test;
  353. curr_suite = child_test->suite_num;
  354. curr_test_case = child_test->test_case_num;
  355. err = child_test->process.err;
  356. /*
  357. * For test suites with subtests, display the suite name ahead of the
  358. * sub test names.
  359. */
  360. if (test_suite__num_test_cases(t) > 1 && curr_test_case == 0)
  361. pr_info("%3d: %-*s:\n", curr_suite + 1, width, test_description(t, -1));
  362. /*
  363. * Busy loop reading from the child's stdout/stderr that are set to be
  364. * non-blocking until EOF.
  365. */
  366. if (err > 0)
  367. fcntl(err, F_SETFL, O_NONBLOCK);
  368. if (verbose > 1) {
  369. if (test_suite__num_test_cases(t) > 1)
  370. pr_info("%3d.%1d: %s:\n", curr_suite + 1, curr_test_case + 1,
  371. test_description(t, curr_test_case));
  372. else
  373. pr_info("%3d: %s:\n", curr_suite + 1, test_description(t, -1));
  374. }
  375. while (!err_done) {
  376. struct pollfd pfds[1] = {
  377. { .fd = err,
  378. .events = POLLIN | POLLERR | POLLHUP | POLLNVAL,
  379. },
  380. };
  381. if (perf_use_color_default) {
  382. int running = 0;
  383. for (int y = running_test; y < child_test_num; y++) {
  384. if (child_tests[y] == NULL)
  385. continue;
  386. if (check_if_command_finished(&child_tests[y]->process) == 0)
  387. running++;
  388. }
  389. if (running != last_running) {
  390. if (last_running != -1) {
  391. /*
  392. * Erase "Running (.. active)" line
  393. * printed before poll/sleep.
  394. */
  395. fprintf(debug_file(), PERF_COLOR_DELETE_LINE);
  396. }
  397. print_test_result(t, curr_suite, curr_test_case, TEST_RUNNING,
  398. width, running);
  399. last_running = running;
  400. }
  401. }
  402. err_done = true;
  403. if (err <= 0) {
  404. /* No child stderr to poll, sleep for 10ms for child to complete. */
  405. usleep(10 * 1000);
  406. } else {
  407. /* Poll to avoid excessive spinning, timeout set for 100ms. */
  408. poll(pfds, ARRAY_SIZE(pfds), /*timeout=*/100);
  409. if (pfds[0].revents) {
  410. char buf[512];
  411. ssize_t len;
  412. len = read(err, buf, sizeof(buf) - 1);
  413. if (len > 0) {
  414. err_done = false;
  415. buf[len] = '\0';
  416. strbuf_addstr(&err_output, buf);
  417. }
  418. }
  419. }
  420. if (err_done)
  421. err_done = check_if_command_finished(&child_test->process);
  422. }
  423. if (perf_use_color_default && last_running != -1) {
  424. /* Erase "Running (.. active)" line printed before poll/sleep. */
  425. fprintf(debug_file(), PERF_COLOR_DELETE_LINE);
  426. }
  427. /* Clean up child process. */
  428. ret = finish_command(&child_test->process);
  429. if (verbose > 1 || (verbose == 1 && ret == TEST_FAIL))
  430. fprintf(stderr, "%s", err_output.buf);
  431. strbuf_release(&err_output);
  432. print_test_result(t, curr_suite, curr_test_case, ret, width, /*running=*/0);
  433. if (err > 0)
  434. close(err);
  435. zfree(&child_tests[running_test]);
  436. }
  437. static int start_test(struct test_suite *test, int curr_suite, int curr_test_case,
  438. struct child_test **child, int width, int pass)
  439. {
  440. int err;
  441. *child = NULL;
  442. if (dont_fork) {
  443. if (pass == 1) {
  444. pr_debug("--- start ---\n");
  445. err = test_function(test, curr_test_case)(test, curr_test_case);
  446. pr_debug("---- end ----\n");
  447. print_test_result(test, curr_suite, curr_test_case, err, width,
  448. /*running=*/0);
  449. }
  450. return 0;
  451. }
  452. if (pass == 1 && !sequential && test_exclusive(test, curr_test_case)) {
  453. /* When parallel, skip exclusive tests on the first pass. */
  454. return 0;
  455. }
  456. if (pass != 1 && (sequential || !test_exclusive(test, curr_test_case))) {
  457. /* Sequential and non-exclusive tests were run on the first pass. */
  458. return 0;
  459. }
  460. *child = zalloc(sizeof(**child));
  461. if (!*child)
  462. return -ENOMEM;
  463. (*child)->test = test;
  464. (*child)->suite_num = curr_suite;
  465. (*child)->test_case_num = curr_test_case;
  466. (*child)->process.pid = -1;
  467. (*child)->process.no_stdin = 1;
  468. if (verbose <= 0) {
  469. (*child)->process.no_stdout = 1;
  470. (*child)->process.no_stderr = 1;
  471. } else {
  472. (*child)->process.stdout_to_stderr = 1;
  473. (*child)->process.out = -1;
  474. (*child)->process.err = -1;
  475. }
  476. (*child)->process.no_exec_cmd = run_test_child;
  477. if (sequential || pass == 2) {
  478. err = start_command(&(*child)->process);
  479. if (err)
  480. return err;
  481. finish_test(child, /*running_test=*/0, /*child_test_num=*/1, width);
  482. return 0;
  483. }
  484. return start_command(&(*child)->process);
  485. }
  486. /* State outside of __cmd_test for the sake of the signal handler. */
  487. static size_t num_tests;
  488. static struct child_test **child_tests;
  489. static jmp_buf cmd_test_jmp_buf;
  490. static void cmd_test_sig_handler(int sig)
  491. {
  492. siglongjmp(cmd_test_jmp_buf, sig);
  493. }
  494. static int __cmd_test(struct test_suite **suites, int argc, const char *argv[],
  495. struct intlist *skiplist)
  496. {
  497. static int width = 0;
  498. int err = 0;
  499. for (struct test_suite **t = suites; *t; t++) {
  500. int i, len = strlen(test_description(*t, -1));
  501. if (width < len)
  502. width = len;
  503. test_suite__for_each_test_case(*t, i) {
  504. len = strlen(test_description(*t, i));
  505. if (width < len)
  506. width = len;
  507. num_tests += runs_per_test;
  508. }
  509. }
  510. child_tests = calloc(num_tests, sizeof(*child_tests));
  511. if (!child_tests)
  512. return -ENOMEM;
  513. err = sigsetjmp(cmd_test_jmp_buf, 1);
  514. if (err) {
  515. pr_err("\nSignal (%d) while running tests.\nTerminating tests with the same signal\n",
  516. err);
  517. for (size_t x = 0; x < num_tests; x++) {
  518. struct child_test *child_test = child_tests[x];
  519. if (!child_test || child_test->process.pid <= 0)
  520. continue;
  521. pr_debug3("Killing %d pid %d\n",
  522. child_test->suite_num + 1,
  523. child_test->process.pid);
  524. kill(child_test->process.pid, err);
  525. }
  526. goto err_out;
  527. }
  528. signal(SIGINT, cmd_test_sig_handler);
  529. signal(SIGTERM, cmd_test_sig_handler);
  530. /*
  531. * In parallel mode pass 1 runs non-exclusive tests in parallel, pass 2
  532. * runs the exclusive tests sequentially. In other modes all tests are
  533. * run in pass 1.
  534. */
  535. for (int pass = 1; pass <= 2; pass++) {
  536. int child_test_num = 0;
  537. int curr_suite = 0;
  538. for (struct test_suite **t = suites; *t; t++, curr_suite++) {
  539. int curr_test_case;
  540. bool suite_matched = false;
  541. if (!perf_test__matches(test_description(*t, -1), curr_suite, argc, argv)) {
  542. /*
  543. * Test suite shouldn't be run based on
  544. * description. See if any test case should.
  545. */
  546. bool skip = true;
  547. test_suite__for_each_test_case(*t, curr_test_case) {
  548. if (perf_test__matches(test_description(*t, curr_test_case),
  549. curr_suite, argc, argv)) {
  550. skip = false;
  551. break;
  552. }
  553. }
  554. if (skip)
  555. continue;
  556. } else {
  557. suite_matched = true;
  558. }
  559. if (intlist__find(skiplist, curr_suite + 1)) {
  560. pr_info("%3d: %-*s:", curr_suite + 1, width,
  561. test_description(*t, -1));
  562. color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
  563. continue;
  564. }
  565. for (unsigned int run = 0; run < runs_per_test; run++) {
  566. test_suite__for_each_test_case(*t, curr_test_case) {
  567. if (!suite_matched &&
  568. !perf_test__matches(test_description(*t, curr_test_case),
  569. curr_suite, argc, argv))
  570. continue;
  571. err = start_test(*t, curr_suite, curr_test_case,
  572. &child_tests[child_test_num++],
  573. width, pass);
  574. if (err)
  575. goto err_out;
  576. }
  577. }
  578. }
  579. if (!sequential) {
  580. /* Parallel mode starts tests but doesn't finish them. Do that now. */
  581. for (size_t x = 0; x < num_tests; x++)
  582. finish_test(child_tests, x, num_tests, width);
  583. }
  584. }
  585. err_out:
  586. signal(SIGINT, SIG_DFL);
  587. signal(SIGTERM, SIG_DFL);
  588. if (err) {
  589. pr_err("Internal test harness failure. Completing any started tests:\n:");
  590. for (size_t x = 0; x < num_tests; x++)
  591. finish_test(child_tests, x, num_tests, width);
  592. }
  593. free(child_tests);
  594. return err;
  595. }
  596. static int perf_test__list(FILE *fp, struct test_suite **suites, int argc, const char **argv)
  597. {
  598. int curr_suite = 0;
  599. for (struct test_suite **t = suites; *t; t++, curr_suite++) {
  600. int curr_test_case;
  601. if (!perf_test__matches(test_description(*t, -1), curr_suite, argc, argv))
  602. continue;
  603. fprintf(fp, "%3d: %s\n", curr_suite + 1, test_description(*t, -1));
  604. if (test_suite__num_test_cases(*t) <= 1)
  605. continue;
  606. test_suite__for_each_test_case(*t, curr_test_case) {
  607. fprintf(fp, "%3d.%1d: %s\n", curr_suite + 1, curr_test_case + 1,
  608. test_description(*t, curr_test_case));
  609. }
  610. }
  611. return 0;
  612. }
  613. static int workloads__fprintf_list(FILE *fp)
  614. {
  615. struct test_workload *twl;
  616. int printed = 0;
  617. workloads__for_each(twl)
  618. printed += fprintf(fp, "%s\n", twl->name);
  619. return printed;
  620. }
  621. static int run_workload(const char *work, int argc, const char **argv)
  622. {
  623. struct test_workload *twl;
  624. workloads__for_each(twl) {
  625. if (!strcmp(twl->name, work))
  626. return twl->func(argc, argv);
  627. }
  628. pr_info("No workload found: %s\n", work);
  629. return -1;
  630. }
  631. static int perf_test__config(const char *var, const char *value,
  632. void *data __maybe_unused)
  633. {
  634. if (!strcmp(var, "annotate.objdump"))
  635. test_objdump_path = value;
  636. return 0;
  637. }
  638. static struct test_suite **build_suites(void)
  639. {
  640. /*
  641. * TODO: suites is static to avoid needing to clean up the scripts tests
  642. * for leak sanitizer.
  643. */
  644. static struct test_suite **suites[] = {
  645. generic_tests,
  646. arch_tests,
  647. NULL,
  648. };
  649. struct test_suite **result;
  650. struct test_suite *t;
  651. size_t n = 0, num_suites = 0;
  652. if (suites[2] == NULL)
  653. suites[2] = create_script_test_suites();
  654. #define for_each_suite(suite) \
  655. for (size_t i = 0, j = 0; i < ARRAY_SIZE(suites); i++, j = 0) \
  656. while ((suite = suites[i][j++]) != NULL)
  657. for_each_suite(t)
  658. num_suites++;
  659. result = calloc(num_suites + 1, sizeof(struct test_suite *));
  660. for (int pass = 1; pass <= 2; pass++) {
  661. for_each_suite(t) {
  662. bool exclusive = false;
  663. int curr_test_case;
  664. test_suite__for_each_test_case(t, curr_test_case) {
  665. if (test_exclusive(t, curr_test_case)) {
  666. exclusive = true;
  667. break;
  668. }
  669. }
  670. if ((!exclusive && pass == 1) || (exclusive && pass == 2))
  671. result[n++] = t;
  672. }
  673. }
  674. return result;
  675. #undef for_each_suite
  676. }
  677. int cmd_test(int argc, const char **argv)
  678. {
  679. const char *test_usage[] = {
  680. "perf test [<options>] [{list <test-name-fragment>|[<test-name-fragments>|<test-numbers>]}]",
  681. NULL,
  682. };
  683. const char *skip = NULL;
  684. const char *workload = NULL;
  685. bool list_workloads = false;
  686. const struct option test_options[] = {
  687. OPT_STRING('s', "skip", &skip, "tests", "tests to skip"),
  688. OPT_INCR('v', "verbose", &verbose,
  689. "be more verbose (show symbol address, etc)"),
  690. OPT_BOOLEAN('F', "dont-fork", &dont_fork,
  691. "Do not fork for testcase"),
  692. OPT_BOOLEAN('S', "sequential", &sequential,
  693. "Run the tests one after another rather than in parallel"),
  694. OPT_UINTEGER('r', "runs-per-test", &runs_per_test,
  695. "Run each test the given number of times, default 1"),
  696. OPT_STRING('w', "workload", &workload, "work", "workload to run for testing, use '--list-workloads' to list the available ones."),
  697. OPT_BOOLEAN(0, "list-workloads", &list_workloads, "List the available builtin workloads to use with -w/--workload"),
  698. OPT_STRING(0, "dso", &dso_to_test, "dso", "dso to test"),
  699. OPT_STRING(0, "objdump", &test_objdump_path, "path",
  700. "objdump binary to use for disassembly and annotations"),
  701. OPT_END()
  702. };
  703. const char * const test_subcommands[] = { "list", NULL };
  704. struct intlist *skiplist = NULL;
  705. int ret = hists__init();
  706. struct test_suite **suites;
  707. if (ret < 0)
  708. return ret;
  709. perf_config(perf_test__config, NULL);
  710. /* Unbuffered output */
  711. setvbuf(stdout, NULL, _IONBF, 0);
  712. argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
  713. if (argc >= 1 && !strcmp(argv[0], "list")) {
  714. suites = build_suites();
  715. ret = perf_test__list(stdout, suites, argc - 1, argv + 1);
  716. free(suites);
  717. return ret;
  718. }
  719. if (workload)
  720. return run_workload(workload, argc, argv);
  721. if (list_workloads) {
  722. workloads__fprintf_list(stdout);
  723. return 0;
  724. }
  725. if (dont_fork)
  726. sequential = true;
  727. symbol_conf.priv_size = sizeof(int);
  728. symbol_conf.try_vmlinux_path = true;
  729. if (symbol__init(NULL) < 0)
  730. return -1;
  731. if (skip != NULL)
  732. skiplist = intlist__new(skip);
  733. /*
  734. * Tests that create BPF maps, for instance, need more than the 64K
  735. * default:
  736. */
  737. rlimit__bump_memlock();
  738. suites = build_suites();
  739. ret = __cmd_test(suites, argc, argv, skiplist);
  740. free(suites);
  741. return ret;
  742. }