mba_test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Memory Bandwidth Allocation (MBA) 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. #define RESULT_FILE_NAME "result_mba"
  13. #define NUM_OF_RUNS 5
  14. #define MAX_DIFF_PERCENT 8
  15. #define ALLOCATION_MAX 100
  16. #define ALLOCATION_MIN 10
  17. #define ALLOCATION_STEP 10
  18. static int mba_init(const struct resctrl_val_param *param, int domain_id)
  19. {
  20. int ret;
  21. ret = initialize_read_mem_bw_imc();
  22. if (ret)
  23. return ret;
  24. initialize_mem_bw_resctrl(param, domain_id);
  25. return 0;
  26. }
  27. /*
  28. * Change schemata percentage from 100 to 10%. Write schemata to specified
  29. * con_mon grp, mon_grp in resctrl FS.
  30. * For each allocation, run 5 times in order to get average values.
  31. */
  32. static int mba_setup(const struct resctrl_test *test,
  33. const struct user_params *uparams,
  34. struct resctrl_val_param *p)
  35. {
  36. static unsigned int allocation = ALLOCATION_MIN;
  37. static int runs_per_allocation;
  38. char allocation_str[64];
  39. int ret;
  40. if (runs_per_allocation >= NUM_OF_RUNS)
  41. runs_per_allocation = 0;
  42. /* Only set up schemata once every NUM_OF_RUNS of allocations */
  43. if (runs_per_allocation++ != 0)
  44. return 0;
  45. if (allocation > ALLOCATION_MAX)
  46. return END_OF_TESTS;
  47. sprintf(allocation_str, "%d", allocation);
  48. ret = write_schemata(p->ctrlgrp, allocation_str, uparams->cpu, test->resource);
  49. if (ret < 0)
  50. return ret;
  51. allocation += ALLOCATION_STEP;
  52. return 0;
  53. }
  54. static int mba_measure(const struct user_params *uparams,
  55. struct resctrl_val_param *param, pid_t bm_pid)
  56. {
  57. return measure_read_mem_bw(uparams, param, bm_pid);
  58. }
  59. static bool show_mba_info(unsigned long *bw_imc, unsigned long *bw_resc)
  60. {
  61. unsigned int allocation;
  62. bool ret = false;
  63. int runs;
  64. ksft_print_msg("Results are displayed in (MB)\n");
  65. /* Memory bandwidth from 100% down to 10% */
  66. for (allocation = 0; allocation < ALLOCATION_MAX / ALLOCATION_STEP;
  67. allocation++) {
  68. unsigned long sum_bw_imc = 0, sum_bw_resc = 0;
  69. long avg_bw_imc, avg_bw_resc;
  70. int avg_diff_per;
  71. float avg_diff;
  72. for (runs = NUM_OF_RUNS * allocation;
  73. runs < NUM_OF_RUNS * allocation + NUM_OF_RUNS ; runs++) {
  74. sum_bw_imc += bw_imc[runs];
  75. sum_bw_resc += bw_resc[runs];
  76. }
  77. avg_bw_imc = sum_bw_imc / NUM_OF_RUNS;
  78. avg_bw_resc = sum_bw_resc / NUM_OF_RUNS;
  79. if (avg_bw_imc < THROTTLE_THRESHOLD || avg_bw_resc < THROTTLE_THRESHOLD) {
  80. ksft_print_msg("Bandwidth below threshold (%d MiB). Dropping results from MBA schemata %u.\n",
  81. THROTTLE_THRESHOLD,
  82. ALLOCATION_MIN + ALLOCATION_STEP * allocation);
  83. continue;
  84. }
  85. avg_diff = (float)labs(avg_bw_resc - avg_bw_imc) / avg_bw_imc;
  86. avg_diff_per = (int)(avg_diff * 100);
  87. ksft_print_msg("%s Check MBA diff within %d%% for schemata %u\n",
  88. avg_diff_per > MAX_DIFF_PERCENT ?
  89. "Fail:" : "Pass:",
  90. MAX_DIFF_PERCENT,
  91. ALLOCATION_MIN + ALLOCATION_STEP * allocation);
  92. ksft_print_msg("avg_diff_per: %d%%\n", avg_diff_per);
  93. ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc);
  94. ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc);
  95. if (avg_diff_per > MAX_DIFF_PERCENT)
  96. ret = true;
  97. }
  98. ksft_print_msg("%s Check schemata change using MBA\n",
  99. ret ? "Fail:" : "Pass:");
  100. if (ret)
  101. ksft_print_msg("At least one test failed\n");
  102. return ret;
  103. }
  104. static int check_results(void)
  105. {
  106. unsigned long bw_resc[NUM_OF_RUNS * ALLOCATION_MAX / ALLOCATION_STEP];
  107. unsigned long bw_imc[NUM_OF_RUNS * ALLOCATION_MAX / ALLOCATION_STEP];
  108. char *token_array[8], output[] = RESULT_FILE_NAME, temp[512];
  109. int runs;
  110. FILE *fp;
  111. fp = fopen(output, "r");
  112. if (!fp) {
  113. ksft_perror(output);
  114. return -1;
  115. }
  116. runs = 0;
  117. while (fgets(temp, sizeof(temp), fp)) {
  118. char *token = strtok(temp, ":\t");
  119. int fields = 0;
  120. while (token) {
  121. token_array[fields++] = token;
  122. token = strtok(NULL, ":\t");
  123. }
  124. /* Field 3 is perf imc value */
  125. bw_imc[runs] = strtoul(token_array[3], NULL, 0);
  126. /* Field 5 is resctrl value */
  127. bw_resc[runs] = strtoul(token_array[5], NULL, 0);
  128. runs++;
  129. }
  130. fclose(fp);
  131. return show_mba_info(bw_imc, bw_resc);
  132. }
  133. static void mba_test_cleanup(void)
  134. {
  135. remove(RESULT_FILE_NAME);
  136. }
  137. static int mba_run_test(const struct resctrl_test *test, const struct user_params *uparams)
  138. {
  139. struct resctrl_val_param param = {
  140. .ctrlgrp = "c1",
  141. .filename = RESULT_FILE_NAME,
  142. .init = mba_init,
  143. .setup = mba_setup,
  144. .measure = mba_measure,
  145. };
  146. struct fill_buf_param fill_buf = {};
  147. int ret;
  148. remove(RESULT_FILE_NAME);
  149. if (uparams->fill_buf) {
  150. fill_buf.buf_size = uparams->fill_buf->buf_size;
  151. fill_buf.memflush = uparams->fill_buf->memflush;
  152. param.fill_buf = &fill_buf;
  153. } else if (!uparams->benchmark_cmd[0]) {
  154. ssize_t buf_size;
  155. buf_size = get_fill_buf_size(uparams->cpu, "L3");
  156. if (buf_size < 0)
  157. return buf_size;
  158. fill_buf.buf_size = buf_size;
  159. fill_buf.memflush = true;
  160. param.fill_buf = &fill_buf;
  161. }
  162. ret = resctrl_val(test, uparams, &param);
  163. if (ret)
  164. return ret;
  165. ret = check_results();
  166. if (ret && (get_vendor() == ARCH_INTEL) && !snc_kernel_support())
  167. ksft_print_msg("Kernel doesn't support Sub-NUMA Clustering but it is enabled on the system.\n");
  168. return ret;
  169. }
  170. static bool mba_feature_check(const struct resctrl_test *test)
  171. {
  172. return test_resource_feature_check(test) &&
  173. resctrl_mon_feature_exists("L3_MON", "mbm_local_bytes");
  174. }
  175. struct resctrl_test mba_test = {
  176. .name = "MBA",
  177. .resource = "MB",
  178. .vendor_specific = ARCH_INTEL,
  179. .feature_check = mba_feature_check,
  180. .run_test = mba_run_test,
  181. .cleanup = mba_test_cleanup,
  182. };