resctrl_tests.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Resctrl tests
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. *
  7. * Authors:
  8. * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
  9. * Fenghua Yu <fenghua.yu@intel.com>
  10. */
  11. #include "resctrl.h"
  12. /* Volatile memory sink to prevent compiler optimizations */
  13. static volatile int sink_target;
  14. volatile int *value_sink = &sink_target;
  15. static struct resctrl_test *resctrl_tests[] = {
  16. &mbm_test,
  17. &mba_test,
  18. &cmt_test,
  19. &l3_cat_test,
  20. &l3_noncont_cat_test,
  21. &l2_noncont_cat_test,
  22. };
  23. static int detect_vendor(void)
  24. {
  25. FILE *inf = fopen("/proc/cpuinfo", "r");
  26. int vendor_id = 0;
  27. char *s = NULL;
  28. char *res;
  29. if (!inf)
  30. return vendor_id;
  31. res = fgrep(inf, "vendor_id");
  32. if (res)
  33. s = strchr(res, ':');
  34. if (s && !strcmp(s, ": GenuineIntel\n"))
  35. vendor_id = ARCH_INTEL;
  36. else if (s && !strcmp(s, ": AuthenticAMD\n"))
  37. vendor_id = ARCH_AMD;
  38. fclose(inf);
  39. free(res);
  40. return vendor_id;
  41. }
  42. int get_vendor(void)
  43. {
  44. static int vendor = -1;
  45. if (vendor == -1)
  46. vendor = detect_vendor();
  47. if (vendor == 0)
  48. ksft_print_msg("Can not get vendor info...\n");
  49. return vendor;
  50. }
  51. static void cmd_help(void)
  52. {
  53. int i;
  54. printf("usage: resctrl_tests [-h] [-t test list] [-n no_of_bits] [-b benchmark_cmd [option]...]\n");
  55. printf("\t-b benchmark_cmd [option]...: run specified benchmark for MBM, MBA and CMT\n");
  56. printf("\t default benchmark is builtin fill_buf\n");
  57. printf("\t-t test list: run tests/groups specified by the list, ");
  58. printf("e.g. -t mbm,mba,cmt,cat\n");
  59. printf("\t\tSupported tests (group):\n");
  60. for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) {
  61. if (resctrl_tests[i]->group)
  62. printf("\t\t\t%s (%s)\n", resctrl_tests[i]->name, resctrl_tests[i]->group);
  63. else
  64. printf("\t\t\t%s\n", resctrl_tests[i]->name);
  65. }
  66. printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n");
  67. printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n");
  68. printf("\t-h: help\n");
  69. }
  70. static int test_prepare(const struct resctrl_test *test)
  71. {
  72. int res;
  73. res = signal_handler_register(test);
  74. if (res) {
  75. ksft_print_msg("Failed to register signal handler\n");
  76. return res;
  77. }
  78. res = mount_resctrlfs();
  79. if (res) {
  80. signal_handler_unregister();
  81. ksft_print_msg("Failed to mount resctrl FS\n");
  82. return res;
  83. }
  84. return 0;
  85. }
  86. static void test_cleanup(const struct resctrl_test *test)
  87. {
  88. if (test->cleanup)
  89. test->cleanup();
  90. umount_resctrlfs();
  91. signal_handler_unregister();
  92. }
  93. static bool test_vendor_specific_check(const struct resctrl_test *test)
  94. {
  95. if (!test->vendor_specific)
  96. return true;
  97. return get_vendor() & test->vendor_specific;
  98. }
  99. static void run_single_test(const struct resctrl_test *test, const struct user_params *uparams)
  100. {
  101. int ret;
  102. if (test->disabled)
  103. return;
  104. if (!test_vendor_specific_check(test)) {
  105. ksft_test_result_skip("Hardware does not support %s\n", test->name);
  106. return;
  107. }
  108. ksft_print_msg("Starting %s test ...\n", test->name);
  109. if (test_prepare(test)) {
  110. ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
  111. return;
  112. }
  113. if (!test->feature_check(test)) {
  114. ksft_test_result_skip("Hardware does not support %s or %s is disabled\n",
  115. test->name, test->name);
  116. goto cleanup;
  117. }
  118. ret = test->run_test(test, uparams);
  119. ksft_test_result(!ret, "%s: test\n", test->name);
  120. cleanup:
  121. test_cleanup(test);
  122. }
  123. static void init_user_params(struct user_params *uparams)
  124. {
  125. memset(uparams, 0, sizeof(*uparams));
  126. uparams->cpu = 1;
  127. uparams->bits = 0;
  128. }
  129. int main(int argc, char **argv)
  130. {
  131. int tests = ARRAY_SIZE(resctrl_tests);
  132. bool test_param_seen = false;
  133. struct user_params uparams;
  134. char *span_str = NULL;
  135. int ret, c, i;
  136. init_user_params(&uparams);
  137. while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) {
  138. char *token;
  139. switch (c) {
  140. case 'b':
  141. /*
  142. * First move optind back to the (first) optarg and
  143. * then build the benchmark command using the
  144. * remaining arguments.
  145. */
  146. optind--;
  147. if (argc - optind >= BENCHMARK_ARGS)
  148. ksft_exit_fail_msg("Too long benchmark command");
  149. /* Extract benchmark command from command line. */
  150. for (i = 0; i < argc - optind; i++)
  151. uparams.benchmark_cmd[i] = argv[i + optind];
  152. uparams.benchmark_cmd[i] = NULL;
  153. goto last_arg;
  154. case 't':
  155. token = strtok(optarg, ",");
  156. if (!test_param_seen) {
  157. for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++)
  158. resctrl_tests[i]->disabled = true;
  159. tests = 0;
  160. test_param_seen = true;
  161. }
  162. while (token) {
  163. bool found = false;
  164. for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) {
  165. if (!strcasecmp(token, resctrl_tests[i]->name) ||
  166. (resctrl_tests[i]->group &&
  167. !strcasecmp(token, resctrl_tests[i]->group))) {
  168. if (resctrl_tests[i]->disabled)
  169. tests++;
  170. resctrl_tests[i]->disabled = false;
  171. found = true;
  172. }
  173. }
  174. if (!found) {
  175. printf("invalid test: %s\n", token);
  176. return -1;
  177. }
  178. token = strtok(NULL, ",");
  179. }
  180. break;
  181. case 'p':
  182. uparams.cpu = atoi(optarg);
  183. break;
  184. case 'n':
  185. uparams.bits = atoi(optarg);
  186. if (uparams.bits <= 0) {
  187. printf("Bail out! invalid argument for no_of_bits\n");
  188. return -1;
  189. }
  190. break;
  191. case 'h':
  192. cmd_help();
  193. return 0;
  194. default:
  195. printf("invalid argument\n");
  196. return -1;
  197. }
  198. }
  199. last_arg:
  200. ksft_print_header();
  201. /*
  202. * Typically we need root privileges, because:
  203. * 1. We write to resctrl FS
  204. * 2. We execute perf commands
  205. */
  206. if (geteuid() != 0)
  207. ksft_exit_skip("Not running as root. Skipping...\n");
  208. if (!check_resctrlfs_support())
  209. ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
  210. if (umount_resctrlfs())
  211. ksft_exit_skip("resctrl FS unmount failed.\n");
  212. filter_dmesg();
  213. if (!uparams.benchmark_cmd[0]) {
  214. /* If no benchmark is given by "-b" argument, use fill_buf. */
  215. uparams.benchmark_cmd[0] = "fill_buf";
  216. ret = asprintf(&span_str, "%u", DEFAULT_SPAN);
  217. if (ret < 0)
  218. ksft_exit_fail_msg("Out of memory!\n");
  219. uparams.benchmark_cmd[1] = span_str;
  220. uparams.benchmark_cmd[2] = "1";
  221. uparams.benchmark_cmd[3] = "0";
  222. uparams.benchmark_cmd[4] = "false";
  223. uparams.benchmark_cmd[5] = NULL;
  224. }
  225. ksft_set_plan(tests);
  226. for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++)
  227. run_single_test(resctrl_tests[i], &uparams);
  228. free(span_str);
  229. ksft_finished();
  230. }