resctrl_tests.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 unsigned int detect_vendor(void)
  24. {
  25. static unsigned int vendor_id;
  26. static bool initialized;
  27. char *s = NULL;
  28. FILE *inf;
  29. char *res;
  30. if (initialized)
  31. return vendor_id;
  32. inf = fopen("/proc/cpuinfo", "r");
  33. if (!inf) {
  34. vendor_id = 0;
  35. initialized = true;
  36. return vendor_id;
  37. }
  38. res = fgrep(inf, "vendor_id");
  39. if (res)
  40. s = strchr(res, ':');
  41. if (s && !strcmp(s, ": GenuineIntel\n"))
  42. vendor_id = ARCH_INTEL;
  43. else if (s && !strcmp(s, ": AuthenticAMD\n"))
  44. vendor_id = ARCH_AMD;
  45. else if (s && !strcmp(s, ": HygonGenuine\n"))
  46. vendor_id = ARCH_HYGON;
  47. fclose(inf);
  48. free(res);
  49. initialized = true;
  50. return vendor_id;
  51. }
  52. unsigned int get_vendor(void)
  53. {
  54. unsigned int vendor;
  55. vendor = detect_vendor();
  56. if (vendor == 0)
  57. ksft_print_msg("Can not get vendor info...\n");
  58. return vendor;
  59. }
  60. static void cmd_help(void)
  61. {
  62. int i;
  63. printf("usage: resctrl_tests [-h] [-t test list] [-n no_of_bits] [-b benchmark_cmd [option]...]\n");
  64. printf("\t-b benchmark_cmd [option]...: run specified benchmark for MBM, MBA and CMT\n");
  65. printf("\t default benchmark is builtin fill_buf\n");
  66. printf("\t-t test list: run tests/groups specified by the list, ");
  67. printf("e.g. -t mbm,mba,cmt,cat\n");
  68. printf("\t\tSupported tests (group):\n");
  69. for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) {
  70. if (resctrl_tests[i]->group)
  71. printf("\t\t\t%s (%s)\n", resctrl_tests[i]->name, resctrl_tests[i]->group);
  72. else
  73. printf("\t\t\t%s\n", resctrl_tests[i]->name);
  74. }
  75. printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n");
  76. printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n");
  77. printf("\t-h: help\n");
  78. }
  79. static int test_prepare(const struct resctrl_test *test)
  80. {
  81. int res;
  82. res = signal_handler_register(test);
  83. if (res) {
  84. ksft_print_msg("Failed to register signal handler\n");
  85. return res;
  86. }
  87. res = mount_resctrlfs();
  88. if (res) {
  89. signal_handler_unregister();
  90. ksft_print_msg("Failed to mount resctrl FS\n");
  91. return res;
  92. }
  93. return 0;
  94. }
  95. static void test_cleanup(const struct resctrl_test *test)
  96. {
  97. if (test->cleanup)
  98. test->cleanup();
  99. umount_resctrlfs();
  100. signal_handler_unregister();
  101. }
  102. static bool test_vendor_specific_check(const struct resctrl_test *test)
  103. {
  104. if (!test->vendor_specific)
  105. return true;
  106. return get_vendor() & test->vendor_specific;
  107. }
  108. static void run_single_test(const struct resctrl_test *test, const struct user_params *uparams)
  109. {
  110. int ret, snc_mode;
  111. if (test->disabled)
  112. return;
  113. if (!test_vendor_specific_check(test)) {
  114. ksft_test_result_skip("Hardware does not support %s\n", test->name);
  115. return;
  116. }
  117. snc_mode = snc_nodes_per_l3_cache();
  118. ksft_print_msg("Starting %s test ...\n", test->name);
  119. if (snc_mode == 1 && snc_unreliable && get_vendor() == ARCH_INTEL) {
  120. ksft_test_result_skip("SNC detection unreliable due to offline CPUs. Test results may not be accurate if SNC enabled.\n");
  121. return;
  122. }
  123. if (test_prepare(test)) {
  124. ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
  125. return;
  126. }
  127. if (!test->feature_check(test)) {
  128. ksft_test_result_skip("Hardware does not support %s or %s is disabled\n",
  129. test->name, test->name);
  130. goto cleanup;
  131. }
  132. ret = test->run_test(test, uparams);
  133. ksft_test_result(!ret, "%s: test\n", test->name);
  134. cleanup:
  135. test_cleanup(test);
  136. }
  137. /*
  138. * Allocate and initialize a struct fill_buf_param with user provided
  139. * (via "-b fill_buf <fill_buf parameters>") parameters.
  140. *
  141. * Use defaults (that may not be appropriate for all tests) for any
  142. * fill_buf parameters omitted by the user.
  143. *
  144. * Historically it may have been possible for user space to provide
  145. * additional parameters, "operation" ("read" vs "write") in
  146. * benchmark_cmd[3] and "once" (run "once" or until terminated) in
  147. * benchmark_cmd[4]. Changing these parameters have never been
  148. * supported with the default of "read" operation and running until
  149. * terminated built into the tests. Any unsupported values for
  150. * (original) "fill_buf" parameters are treated as failure.
  151. *
  152. * Return: On failure, forcibly exits the test on any parsing failure,
  153. * returns NULL if no parsing needed (user did not actually provide
  154. * "-b fill_buf").
  155. * On success, returns pointer to newly allocated and fully
  156. * initialized struct fill_buf_param that caller must free.
  157. */
  158. static struct fill_buf_param *alloc_fill_buf_param(struct user_params *uparams)
  159. {
  160. struct fill_buf_param *fill_param = NULL;
  161. char *endptr = NULL;
  162. if (!uparams->benchmark_cmd[0] || strcmp(uparams->benchmark_cmd[0], "fill_buf"))
  163. return NULL;
  164. fill_param = malloc(sizeof(*fill_param));
  165. if (!fill_param)
  166. ksft_exit_skip("Unable to allocate memory for fill_buf parameters.\n");
  167. if (uparams->benchmark_cmd[1] && *uparams->benchmark_cmd[1] != '\0') {
  168. errno = 0;
  169. fill_param->buf_size = strtoul(uparams->benchmark_cmd[1], &endptr, 10);
  170. if (errno || *endptr != '\0') {
  171. free(fill_param);
  172. ksft_exit_skip("Unable to parse benchmark buffer size.\n");
  173. }
  174. } else {
  175. fill_param->buf_size = MINIMUM_SPAN;
  176. }
  177. if (uparams->benchmark_cmd[2] && *uparams->benchmark_cmd[2] != '\0') {
  178. errno = 0;
  179. fill_param->memflush = strtol(uparams->benchmark_cmd[2], &endptr, 10) != 0;
  180. if (errno || *endptr != '\0') {
  181. free(fill_param);
  182. ksft_exit_skip("Unable to parse benchmark memflush parameter.\n");
  183. }
  184. } else {
  185. fill_param->memflush = true;
  186. }
  187. if (uparams->benchmark_cmd[3] && *uparams->benchmark_cmd[3] != '\0') {
  188. if (strcmp(uparams->benchmark_cmd[3], "0")) {
  189. free(fill_param);
  190. ksft_exit_skip("Only read operations supported.\n");
  191. }
  192. }
  193. if (uparams->benchmark_cmd[4] && *uparams->benchmark_cmd[4] != '\0') {
  194. if (strcmp(uparams->benchmark_cmd[4], "false")) {
  195. free(fill_param);
  196. ksft_exit_skip("fill_buf is required to run until termination.\n");
  197. }
  198. }
  199. return fill_param;
  200. }
  201. static void init_user_params(struct user_params *uparams)
  202. {
  203. memset(uparams, 0, sizeof(*uparams));
  204. uparams->cpu = 1;
  205. uparams->bits = 0;
  206. }
  207. int main(int argc, char **argv)
  208. {
  209. struct fill_buf_param *fill_param = NULL;
  210. int tests = ARRAY_SIZE(resctrl_tests);
  211. bool test_param_seen = false;
  212. struct user_params uparams;
  213. int c, i;
  214. init_user_params(&uparams);
  215. while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) {
  216. char *token;
  217. switch (c) {
  218. case 'b':
  219. /*
  220. * First move optind back to the (first) optarg and
  221. * then build the benchmark command using the
  222. * remaining arguments.
  223. */
  224. optind--;
  225. if (argc - optind >= BENCHMARK_ARGS)
  226. ksft_exit_fail_msg("Too long benchmark command");
  227. /* Extract benchmark command from command line. */
  228. for (i = 0; i < argc - optind; i++)
  229. uparams.benchmark_cmd[i] = argv[i + optind];
  230. uparams.benchmark_cmd[i] = NULL;
  231. goto last_arg;
  232. case 't':
  233. token = strtok(optarg, ",");
  234. if (!test_param_seen) {
  235. for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++)
  236. resctrl_tests[i]->disabled = true;
  237. tests = 0;
  238. test_param_seen = true;
  239. }
  240. while (token) {
  241. bool found = false;
  242. for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) {
  243. if (!strcasecmp(token, resctrl_tests[i]->name) ||
  244. (resctrl_tests[i]->group &&
  245. !strcasecmp(token, resctrl_tests[i]->group))) {
  246. if (resctrl_tests[i]->disabled)
  247. tests++;
  248. resctrl_tests[i]->disabled = false;
  249. found = true;
  250. }
  251. }
  252. if (!found) {
  253. printf("invalid test: %s\n", token);
  254. return -1;
  255. }
  256. token = strtok(NULL, ",");
  257. }
  258. break;
  259. case 'p':
  260. uparams.cpu = atoi(optarg);
  261. break;
  262. case 'n':
  263. uparams.bits = atoi(optarg);
  264. if (uparams.bits <= 0) {
  265. printf("Bail out! invalid argument for no_of_bits\n");
  266. return -1;
  267. }
  268. break;
  269. case 'h':
  270. cmd_help();
  271. return 0;
  272. default:
  273. printf("invalid argument\n");
  274. return -1;
  275. }
  276. }
  277. last_arg:
  278. fill_param = alloc_fill_buf_param(&uparams);
  279. if (fill_param)
  280. uparams.fill_buf = fill_param;
  281. ksft_print_header();
  282. /*
  283. * Typically we need root privileges, because:
  284. * 1. We write to resctrl FS
  285. * 2. We execute perf commands
  286. */
  287. if (geteuid() != 0)
  288. ksft_exit_skip("Not running as root. Skipping...\n");
  289. if (!check_resctrlfs_support())
  290. ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
  291. if (umount_resctrlfs())
  292. ksft_exit_skip("resctrl FS unmount failed.\n");
  293. filter_dmesg();
  294. ksft_set_plan(tests);
  295. for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++)
  296. run_single_test(resctrl_tests[i], &uparams);
  297. free(fill_param);
  298. ksft_finished();
  299. }