runner.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. " -q Don't print the test descriptions during run\n"
  25. " -h Display this help and exit\n";
  26. static volatile int exit_req;
  27. static bool quiet, print_skipped;
  28. #define MAX_SCX_TESTS 2048
  29. static struct scx_test __scx_tests[MAX_SCX_TESTS];
  30. static unsigned __scx_num_tests = 0;
  31. static void sigint_handler(int simple)
  32. {
  33. exit_req = 1;
  34. }
  35. static void print_test_preamble(const struct scx_test *test, bool quiet)
  36. {
  37. printf("===== START =====\n");
  38. printf("TEST: %s\n", test->name);
  39. if (!quiet)
  40. printf("DESCRIPTION: %s\n", test->description);
  41. printf("OUTPUT:\n");
  42. }
  43. static const char *status_to_result(enum scx_test_status status)
  44. {
  45. switch (status) {
  46. case SCX_TEST_PASS:
  47. case SCX_TEST_SKIP:
  48. return "ok";
  49. case SCX_TEST_FAIL:
  50. return "not ok";
  51. default:
  52. return "<UNKNOWN>";
  53. }
  54. }
  55. static void print_test_result(const struct scx_test *test,
  56. enum scx_test_status status,
  57. unsigned int testnum)
  58. {
  59. const char *result = status_to_result(status);
  60. const char *directive = status == SCX_TEST_SKIP ? "SKIP " : "";
  61. printf("%s %u %s # %s\n", result, testnum, test->name, directive);
  62. printf("===== END =====\n");
  63. }
  64. static bool should_skip_test(const struct scx_test *test, const char * filter)
  65. {
  66. return !strstr(test->name, filter);
  67. }
  68. static enum scx_test_status run_test(const struct scx_test *test)
  69. {
  70. enum scx_test_status status;
  71. void *context = NULL;
  72. if (test->setup) {
  73. status = test->setup(&context);
  74. if (status != SCX_TEST_PASS)
  75. return status;
  76. }
  77. status = test->run(context);
  78. if (test->cleanup)
  79. test->cleanup(context);
  80. return status;
  81. }
  82. static bool test_valid(const struct scx_test *test)
  83. {
  84. if (!test) {
  85. fprintf(stderr, "NULL test detected\n");
  86. return false;
  87. }
  88. if (!test->name) {
  89. fprintf(stderr,
  90. "Test with no name found. Must specify test name.\n");
  91. return false;
  92. }
  93. if (!test->description) {
  94. fprintf(stderr, "Test %s requires description.\n", test->name);
  95. return false;
  96. }
  97. if (!test->run) {
  98. fprintf(stderr, "Test %s has no run() callback\n", test->name);
  99. return false;
  100. }
  101. return true;
  102. }
  103. int main(int argc, char **argv)
  104. {
  105. const char *filter = NULL;
  106. unsigned testnum = 0, i;
  107. unsigned passed = 0, skipped = 0, failed = 0;
  108. int opt;
  109. signal(SIGINT, sigint_handler);
  110. signal(SIGTERM, sigint_handler);
  111. libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
  112. while ((opt = getopt(argc, argv, "qst:h")) != -1) {
  113. switch (opt) {
  114. case 'q':
  115. quiet = true;
  116. break;
  117. case 's':
  118. print_skipped = true;
  119. break;
  120. case 't':
  121. filter = optarg;
  122. break;
  123. default:
  124. fprintf(stderr, help_fmt, basename(argv[0]));
  125. return opt != 'h';
  126. }
  127. }
  128. for (i = 0; i < __scx_num_tests; i++) {
  129. enum scx_test_status status;
  130. struct scx_test *test = &__scx_tests[i];
  131. if (filter && should_skip_test(test, filter)) {
  132. /*
  133. * Printing the skipped tests and their preambles can
  134. * add a lot of noise to the runner output. Printing
  135. * this is only really useful for CI, so let's skip it
  136. * by default.
  137. */
  138. if (print_skipped) {
  139. print_test_preamble(test, quiet);
  140. print_test_result(test, SCX_TEST_SKIP, ++testnum);
  141. }
  142. continue;
  143. }
  144. print_test_preamble(test, quiet);
  145. status = run_test(test);
  146. print_test_result(test, status, ++testnum);
  147. switch (status) {
  148. case SCX_TEST_PASS:
  149. passed++;
  150. break;
  151. case SCX_TEST_SKIP:
  152. skipped++;
  153. break;
  154. case SCX_TEST_FAIL:
  155. failed++;
  156. break;
  157. }
  158. }
  159. printf("\n\n=============================\n\n");
  160. printf("RESULTS:\n\n");
  161. printf("PASSED: %u\n", passed);
  162. printf("SKIPPED: %u\n", skipped);
  163. printf("FAILED: %u\n", failed);
  164. return 0;
  165. }
  166. void scx_test_register(struct scx_test *test)
  167. {
  168. SCX_BUG_ON(!test_valid(test), "Invalid test found");
  169. SCX_BUG_ON(__scx_num_tests >= MAX_SCX_TESTS, "Maximum tests exceeded");
  170. __scx_tests[__scx_num_tests++] = *test;
  171. }