runner.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
  4. * Copyright (c) 2024 David Vernet <dvernet@meta.com>
  5. * Copyright (c) 2024 Tejun Heo <tj@kernel.org>
  6. */
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <signal.h>
  10. #include <libgen.h>
  11. #include <bpf/bpf.h>
  12. #include "scx_test.h"
  13. const char help_fmt[] =
  14. "The runner for sched_ext tests.\n"
  15. "\n"
  16. "The runner is statically linked against all testcases, and runs them all serially.\n"
  17. "It's required for the testcases to be serial, as only a single host-wide sched_ext\n"
  18. "scheduler may be loaded at any given time."
  19. "\n"
  20. "Usage: %s [-t TEST] [-h]\n"
  21. "\n"
  22. " -t TEST Only run tests whose name includes this string\n"
  23. " -s Include print output for skipped tests\n"
  24. " -l List all available tests\n"
  25. " -q Don't print the test descriptions during run\n"
  26. " -h Display this help and exit\n";
  27. static volatile int exit_req;
  28. static bool quiet, print_skipped, list;
  29. #define MAX_SCX_TESTS 2048
  30. static struct scx_test __scx_tests[MAX_SCX_TESTS];
  31. static unsigned __scx_num_tests = 0;
  32. static void sigint_handler(int simple)
  33. {
  34. exit_req = 1;
  35. }
  36. static void print_test_preamble(const struct scx_test *test, bool quiet)
  37. {
  38. printf("===== START =====\n");
  39. printf("TEST: %s\n", test->name);
  40. if (!quiet)
  41. printf("DESCRIPTION: %s\n", test->description);
  42. printf("OUTPUT:\n");
  43. /*
  44. * The tests may fork with the preamble buffered
  45. * in the children's stdout. Flush before the test
  46. * to avoid printing the message multiple times.
  47. */
  48. fflush(stdout);
  49. fflush(stderr);
  50. }
  51. static const char *status_to_result(enum scx_test_status status)
  52. {
  53. switch (status) {
  54. case SCX_TEST_PASS:
  55. case SCX_TEST_SKIP:
  56. return "ok";
  57. case SCX_TEST_FAIL:
  58. return "not ok";
  59. default:
  60. return "<UNKNOWN>";
  61. }
  62. }
  63. static void print_test_result(const struct scx_test *test,
  64. enum scx_test_status status,
  65. unsigned int testnum)
  66. {
  67. const char *result = status_to_result(status);
  68. const char *directive = status == SCX_TEST_SKIP ? "SKIP " : "";
  69. printf("%s %u %s # %s\n", result, testnum, test->name, directive);
  70. printf("===== END =====\n");
  71. }
  72. static bool should_skip_test(const struct scx_test *test, const char * filter)
  73. {
  74. return !strstr(test->name, filter);
  75. }
  76. static enum scx_test_status run_test(const struct scx_test *test)
  77. {
  78. enum scx_test_status status;
  79. void *context = NULL;
  80. if (test->setup) {
  81. status = test->setup(&context);
  82. if (status != SCX_TEST_PASS)
  83. return status;
  84. }
  85. status = test->run(context);
  86. if (test->cleanup)
  87. test->cleanup(context);
  88. return status;
  89. }
  90. static bool test_valid(const struct scx_test *test)
  91. {
  92. if (!test) {
  93. fprintf(stderr, "NULL test detected\n");
  94. return false;
  95. }
  96. if (!test->name) {
  97. fprintf(stderr,
  98. "Test with no name found. Must specify test name.\n");
  99. return false;
  100. }
  101. if (!test->description) {
  102. fprintf(stderr, "Test %s requires description.\n", test->name);
  103. return false;
  104. }
  105. if (!test->run) {
  106. fprintf(stderr, "Test %s has no run() callback\n", test->name);
  107. return false;
  108. }
  109. return true;
  110. }
  111. int main(int argc, char **argv)
  112. {
  113. const char *filter = NULL;
  114. unsigned testnum = 0, i;
  115. unsigned passed = 0, skipped = 0, failed = 0;
  116. int opt;
  117. signal(SIGINT, sigint_handler);
  118. signal(SIGTERM, sigint_handler);
  119. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  120. while ((opt = getopt(argc, argv, "qslt:h")) != -1) {
  121. switch (opt) {
  122. case 'q':
  123. quiet = true;
  124. break;
  125. case 's':
  126. print_skipped = true;
  127. break;
  128. case 'l':
  129. list = true;
  130. break;
  131. case 't':
  132. filter = optarg;
  133. break;
  134. default:
  135. fprintf(stderr, help_fmt, basename(argv[0]));
  136. return opt != 'h';
  137. }
  138. }
  139. for (i = 0; i < __scx_num_tests; i++) {
  140. enum scx_test_status status;
  141. struct scx_test *test = &__scx_tests[i];
  142. if (exit_req)
  143. break;
  144. if (list) {
  145. printf("%s\n", test->name);
  146. if (i == (__scx_num_tests - 1))
  147. return 0;
  148. continue;
  149. }
  150. if (filter && should_skip_test(test, filter)) {
  151. /*
  152. * Printing the skipped tests and their preambles can
  153. * add a lot of noise to the runner output. Printing
  154. * this is only really useful for CI, so let's skip it
  155. * by default.
  156. */
  157. if (print_skipped) {
  158. print_test_preamble(test, quiet);
  159. print_test_result(test, SCX_TEST_SKIP, ++testnum);
  160. }
  161. continue;
  162. }
  163. print_test_preamble(test, quiet);
  164. status = run_test(test);
  165. print_test_result(test, status, ++testnum);
  166. switch (status) {
  167. case SCX_TEST_PASS:
  168. passed++;
  169. break;
  170. case SCX_TEST_SKIP:
  171. skipped++;
  172. break;
  173. case SCX_TEST_FAIL:
  174. failed++;
  175. break;
  176. }
  177. }
  178. printf("\n\n=============================\n\n");
  179. printf("RESULTS:\n\n");
  180. printf("PASSED: %u\n", passed);
  181. printf("SKIPPED: %u\n", skipped);
  182. printf("FAILED: %u\n", failed);
  183. return 0;
  184. }
  185. void scx_test_register(struct scx_test *test)
  186. {
  187. SCX_BUG_ON(!test_valid(test), "Invalid test found");
  188. SCX_BUG_ON(__scx_num_tests >= MAX_SCX_TESTS, "Maximum tests exceeded");
  189. __scx_tests[__scx_num_tests++] = *test;
  190. }