cat_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cache Allocation Technology (CAT) test
  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. #include <unistd.h>
  13. #define RESULT_FILE_NAME "result_cat"
  14. #define NUM_OF_RUNS 5
  15. /*
  16. * Minimum difference in LLC misses between a test with n+1 bits CBM to the
  17. * test with n bits is MIN_DIFF_PERCENT_PER_BIT * (n - 1). With e.g. 5 vs 4
  18. * bits in the CBM mask, the minimum difference must be at least
  19. * MIN_DIFF_PERCENT_PER_BIT * (4 - 1) = 3 percent.
  20. *
  21. * The relationship between number of used CBM bits and difference in LLC
  22. * misses is not expected to be linear. With a small number of bits, the
  23. * margin is smaller than with larger number of bits. For selftest purposes,
  24. * however, linear approach is enough because ultimately only pass/fail
  25. * decision has to be made and distinction between strong and stronger
  26. * signal is irrelevant.
  27. */
  28. #define MIN_DIFF_PERCENT_PER_BIT 1UL
  29. static int show_results_info(__u64 sum_llc_val, int no_of_bits,
  30. unsigned long cache_span,
  31. unsigned long min_diff_percent,
  32. unsigned long num_of_runs, bool platform,
  33. __s64 *prev_avg_llc_val)
  34. {
  35. __u64 avg_llc_val = 0;
  36. float avg_diff;
  37. int ret = 0;
  38. avg_llc_val = sum_llc_val / num_of_runs;
  39. if (*prev_avg_llc_val) {
  40. float delta = (__s64)(avg_llc_val - *prev_avg_llc_val);
  41. avg_diff = delta / *prev_avg_llc_val;
  42. ret = platform && (avg_diff * 100) < (float)min_diff_percent;
  43. ksft_print_msg("%s Check cache miss rate changed more than %.1f%%\n",
  44. ret ? "Fail:" : "Pass:", (float)min_diff_percent);
  45. ksft_print_msg("Percent diff=%.1f\n", avg_diff * 100);
  46. }
  47. *prev_avg_llc_val = avg_llc_val;
  48. show_cache_info(no_of_bits, avg_llc_val, cache_span, true);
  49. return ret;
  50. }
  51. /* Remove the highest bit from CBM */
  52. static unsigned long next_mask(unsigned long current_mask)
  53. {
  54. return current_mask & (current_mask >> 1);
  55. }
  56. static int check_results(struct resctrl_val_param *param, const char *cache_type,
  57. unsigned long cache_total_size, unsigned long full_cache_mask,
  58. unsigned long current_mask)
  59. {
  60. char *token_array[8], temp[512];
  61. __u64 sum_llc_perf_miss = 0;
  62. __s64 prev_avg_llc_val = 0;
  63. unsigned long alloc_size;
  64. int runs = 0;
  65. int fail = 0;
  66. int ret;
  67. FILE *fp;
  68. ksft_print_msg("Checking for pass/fail\n");
  69. fp = fopen(param->filename, "r");
  70. if (!fp) {
  71. ksft_perror("Cannot open file");
  72. return -1;
  73. }
  74. while (fgets(temp, sizeof(temp), fp)) {
  75. char *token = strtok(temp, ":\t");
  76. int fields = 0;
  77. int bits;
  78. while (token) {
  79. token_array[fields++] = token;
  80. token = strtok(NULL, ":\t");
  81. }
  82. sum_llc_perf_miss += strtoull(token_array[3], NULL, 0);
  83. runs++;
  84. if (runs < NUM_OF_RUNS)
  85. continue;
  86. if (!current_mask) {
  87. ksft_print_msg("Unexpected empty cache mask\n");
  88. break;
  89. }
  90. alloc_size = cache_portion_size(cache_total_size, current_mask, full_cache_mask);
  91. bits = count_bits(current_mask);
  92. ret = show_results_info(sum_llc_perf_miss, bits,
  93. alloc_size / 64,
  94. MIN_DIFF_PERCENT_PER_BIT * (bits - 1),
  95. runs, get_vendor() == ARCH_INTEL,
  96. &prev_avg_llc_val);
  97. if (ret)
  98. fail = 1;
  99. runs = 0;
  100. sum_llc_perf_miss = 0;
  101. current_mask = next_mask(current_mask);
  102. }
  103. fclose(fp);
  104. return fail;
  105. }
  106. static void cat_test_cleanup(void)
  107. {
  108. remove(RESULT_FILE_NAME);
  109. }
  110. /*
  111. * cat_test - Execute CAT benchmark and measure cache misses
  112. * @test: Test information structure
  113. * @uparams: User supplied parameters
  114. * @param: Parameters passed to cat_test()
  115. * @span: Buffer size for the benchmark
  116. * @current_mask Start mask for the first iteration
  117. *
  118. * Run CAT selftest by varying the allocated cache portion and comparing the
  119. * impact on cache misses (the result analysis is done in check_results()
  120. * and show_results_info(), not in this function).
  121. *
  122. * One bit is removed from the CAT allocation bit mask (in current_mask) for
  123. * each subsequent test which keeps reducing the size of the allocated cache
  124. * portion. A single test flushes the buffer, reads it to warm up the cache,
  125. * and reads the buffer again. The cache misses are measured during the last
  126. * read pass.
  127. *
  128. * Return: 0 when the test was run, < 0 on error.
  129. */
  130. static int cat_test(const struct resctrl_test *test,
  131. const struct user_params *uparams,
  132. struct resctrl_val_param *param,
  133. size_t span, unsigned long current_mask)
  134. {
  135. struct perf_event_read pe_read;
  136. struct perf_event_attr pea;
  137. cpu_set_t old_affinity;
  138. unsigned char *buf;
  139. char schemata[64];
  140. int ret, i, pe_fd;
  141. pid_t bm_pid;
  142. if (strcmp(param->filename, "") == 0)
  143. sprintf(param->filename, "stdio");
  144. bm_pid = getpid();
  145. /* Taskset benchmark to specified cpu */
  146. ret = taskset_benchmark(bm_pid, uparams->cpu, &old_affinity);
  147. if (ret)
  148. return ret;
  149. /* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/
  150. ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp);
  151. if (ret)
  152. goto reset_affinity;
  153. perf_event_attr_initialize(&pea, PERF_COUNT_HW_CACHE_MISSES);
  154. perf_event_initialize_read_format(&pe_read);
  155. pe_fd = perf_open(&pea, bm_pid, uparams->cpu);
  156. if (pe_fd < 0) {
  157. ret = -1;
  158. goto reset_affinity;
  159. }
  160. buf = alloc_buffer(span, 1);
  161. if (!buf) {
  162. ret = -1;
  163. goto pe_close;
  164. }
  165. while (current_mask) {
  166. snprintf(schemata, sizeof(schemata), "%lx", param->mask & ~current_mask);
  167. ret = write_schemata("", schemata, uparams->cpu, test->resource);
  168. if (ret)
  169. goto free_buf;
  170. snprintf(schemata, sizeof(schemata), "%lx", current_mask);
  171. ret = write_schemata(param->ctrlgrp, schemata, uparams->cpu, test->resource);
  172. if (ret)
  173. goto free_buf;
  174. for (i = 0; i < NUM_OF_RUNS; i++) {
  175. mem_flush(buf, span);
  176. fill_cache_read(buf, span, true);
  177. ret = perf_event_reset_enable(pe_fd);
  178. if (ret)
  179. goto free_buf;
  180. fill_cache_read(buf, span, true);
  181. ret = perf_event_measure(pe_fd, &pe_read, param->filename, bm_pid);
  182. if (ret)
  183. goto free_buf;
  184. }
  185. current_mask = next_mask(current_mask);
  186. }
  187. free_buf:
  188. free(buf);
  189. pe_close:
  190. close(pe_fd);
  191. reset_affinity:
  192. taskset_restore(bm_pid, &old_affinity);
  193. return ret;
  194. }
  195. static int cat_run_test(const struct resctrl_test *test, const struct user_params *uparams)
  196. {
  197. unsigned long long_mask, start_mask, full_cache_mask;
  198. unsigned long cache_total_size = 0;
  199. int n = uparams->bits;
  200. unsigned int start;
  201. int count_of_bits;
  202. size_t span;
  203. int ret;
  204. ret = get_full_cbm(test->resource, &full_cache_mask);
  205. if (ret)
  206. return ret;
  207. /* Get the largest contiguous exclusive portion of the cache */
  208. ret = get_mask_no_shareable(test->resource, &long_mask);
  209. if (ret)
  210. return ret;
  211. /* Get L3/L2 cache size */
  212. ret = get_cache_size(uparams->cpu, test->resource, &cache_total_size);
  213. if (ret)
  214. return ret;
  215. ksft_print_msg("Cache size :%lu\n", cache_total_size);
  216. count_of_bits = count_contiguous_bits(long_mask, &start);
  217. if (!n)
  218. n = count_of_bits / 2;
  219. if (n > count_of_bits - 1) {
  220. ksft_print_msg("Invalid input value for no_of_bits n!\n");
  221. ksft_print_msg("Please enter value in range 1 to %d\n",
  222. count_of_bits - 1);
  223. return -1;
  224. }
  225. start_mask = create_bit_mask(start, n);
  226. struct resctrl_val_param param = {
  227. .ctrlgrp = "c1",
  228. .filename = RESULT_FILE_NAME,
  229. .num_of_runs = 0,
  230. };
  231. param.mask = long_mask;
  232. span = cache_portion_size(cache_total_size, start_mask, full_cache_mask);
  233. remove(param.filename);
  234. ret = cat_test(test, uparams, &param, span, start_mask);
  235. if (ret)
  236. return ret;
  237. ret = check_results(&param, test->resource,
  238. cache_total_size, full_cache_mask, start_mask);
  239. return ret;
  240. }
  241. static bool arch_supports_noncont_cat(const struct resctrl_test *test)
  242. {
  243. unsigned int vendor_id = get_vendor();
  244. /* AMD and Hygon always support non-contiguous CBM. */
  245. if (vendor_id == ARCH_AMD || vendor_id == ARCH_HYGON)
  246. return true;
  247. #if defined(__i386__) || defined(__x86_64__) /* arch */
  248. unsigned int eax, ebx, ecx, edx;
  249. /* Intel support for non-contiguous CBM needs to be discovered. */
  250. if (!strcmp(test->resource, "L3"))
  251. __cpuid_count(0x10, 1, eax, ebx, ecx, edx);
  252. else if (!strcmp(test->resource, "L2"))
  253. __cpuid_count(0x10, 2, eax, ebx, ecx, edx);
  254. else
  255. return false;
  256. return ((ecx >> 3) & 1);
  257. #endif /* end arch */
  258. return false;
  259. }
  260. static int noncont_cat_run_test(const struct resctrl_test *test,
  261. const struct user_params *uparams)
  262. {
  263. unsigned long full_cache_mask, cont_mask, noncont_mask;
  264. unsigned int sparse_masks;
  265. int bit_center, ret;
  266. char schemata[64];
  267. /* Check to compare sparse_masks content to CPUID output. */
  268. ret = resource_info_unsigned_get(test->resource, "sparse_masks", &sparse_masks);
  269. if (ret)
  270. return ret;
  271. if (arch_supports_noncont_cat(test) != sparse_masks) {
  272. ksft_print_msg("Hardware and kernel differ on non-contiguous CBM support!\n");
  273. return 1;
  274. }
  275. /* Write checks initialization. */
  276. ret = get_full_cbm(test->resource, &full_cache_mask);
  277. if (ret < 0)
  278. return ret;
  279. bit_center = count_bits(full_cache_mask) / 2;
  280. /*
  281. * The bit_center needs to be at least 3 to properly calculate the CBM
  282. * hole in the noncont_mask. If it's smaller return an error since the
  283. * cache mask is too short and that shouldn't happen.
  284. */
  285. if (bit_center < 3)
  286. return -EINVAL;
  287. cont_mask = full_cache_mask >> bit_center;
  288. /* Contiguous mask write check. */
  289. snprintf(schemata, sizeof(schemata), "%lx", cont_mask);
  290. ret = write_schemata("", schemata, uparams->cpu, test->resource);
  291. if (ret) {
  292. ksft_print_msg("Write of contiguous CBM failed\n");
  293. return 1;
  294. }
  295. /*
  296. * Non-contiguous mask write check. CBM has a 0xf hole approximately in the middle.
  297. * Output is compared with support information to catch any edge case errors.
  298. */
  299. noncont_mask = ~(0xfUL << (bit_center - 2)) & full_cache_mask;
  300. snprintf(schemata, sizeof(schemata), "%lx", noncont_mask);
  301. ret = write_schemata("", schemata, uparams->cpu, test->resource);
  302. if (ret && sparse_masks)
  303. ksft_print_msg("Non-contiguous CBMs supported but write of non-contiguous CBM failed\n");
  304. else if (ret && !sparse_masks)
  305. ksft_print_msg("Non-contiguous CBMs not supported and write of non-contiguous CBM failed as expected\n");
  306. else if (!ret && !sparse_masks)
  307. ksft_print_msg("Non-contiguous CBMs not supported but write of non-contiguous CBM succeeded\n");
  308. return !ret == !sparse_masks;
  309. }
  310. static bool noncont_cat_feature_check(const struct resctrl_test *test)
  311. {
  312. if (!resctrl_resource_exists(test->resource))
  313. return false;
  314. return resource_info_file_exists(test->resource, "sparse_masks");
  315. }
  316. struct resctrl_test l3_cat_test = {
  317. .name = "L3_CAT",
  318. .group = "CAT",
  319. .resource = "L3",
  320. .feature_check = test_resource_feature_check,
  321. .run_test = cat_run_test,
  322. .cleanup = cat_test_cleanup,
  323. };
  324. struct resctrl_test l3_noncont_cat_test = {
  325. .name = "L3_NONCONT_CAT",
  326. .group = "CAT",
  327. .resource = "L3",
  328. .feature_check = noncont_cat_feature_check,
  329. .run_test = noncont_cat_run_test,
  330. };
  331. struct resctrl_test l2_noncont_cat_test = {
  332. .name = "L2_NONCONT_CAT",
  333. .group = "CAT",
  334. .resource = "L2",
  335. .feature_check = noncont_cat_feature_check,
  336. .run_test = noncont_cat_run_test,
  337. };