mba_test.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_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 int runs_per_allocation, allocation = 100;
  37. char allocation_str[64];
  38. int ret;
  39. if (runs_per_allocation >= NUM_OF_RUNS)
  40. runs_per_allocation = 0;
  41. /* Only set up schemata once every NUM_OF_RUNS of allocations */
  42. if (runs_per_allocation++ != 0)
  43. return 0;
  44. if (allocation < ALLOCATION_MIN || allocation > ALLOCATION_MAX)
  45. return END_OF_TESTS;
  46. sprintf(allocation_str, "%d", allocation);
  47. ret = write_schemata(p->ctrlgrp, allocation_str, uparams->cpu, test->resource);
  48. if (ret < 0)
  49. return ret;
  50. allocation -= ALLOCATION_STEP;
  51. return 0;
  52. }
  53. static int mba_measure(const struct user_params *uparams,
  54. struct resctrl_val_param *param, pid_t bm_pid)
  55. {
  56. return measure_mem_bw(uparams, param, bm_pid, "reads");
  57. }
  58. static bool show_mba_info(unsigned long *bw_imc, unsigned long *bw_resc)
  59. {
  60. int allocation, runs;
  61. bool ret = false;
  62. ksft_print_msg("Results are displayed in (MB)\n");
  63. /* Memory bandwidth from 100% down to 10% */
  64. for (allocation = 0; allocation < ALLOCATION_MAX / ALLOCATION_STEP;
  65. allocation++) {
  66. unsigned long sum_bw_imc = 0, sum_bw_resc = 0;
  67. long avg_bw_imc, avg_bw_resc;
  68. int avg_diff_per;
  69. float avg_diff;
  70. /*
  71. * The first run is discarded due to inaccurate value from
  72. * phase transition.
  73. */
  74. for (runs = NUM_OF_RUNS * allocation + 1;
  75. runs < NUM_OF_RUNS * allocation + NUM_OF_RUNS ; runs++) {
  76. sum_bw_imc += bw_imc[runs];
  77. sum_bw_resc += bw_resc[runs];
  78. }
  79. avg_bw_imc = sum_bw_imc / (NUM_OF_RUNS - 1);
  80. avg_bw_resc = sum_bw_resc / (NUM_OF_RUNS - 1);
  81. avg_diff = (float)labs(avg_bw_resc - avg_bw_imc) / avg_bw_imc;
  82. avg_diff_per = (int)(avg_diff * 100);
  83. ksft_print_msg("%s Check MBA diff within %d%% for schemata %u\n",
  84. avg_diff_per > MAX_DIFF_PERCENT ?
  85. "Fail:" : "Pass:",
  86. MAX_DIFF_PERCENT,
  87. ALLOCATION_MAX - ALLOCATION_STEP * allocation);
  88. ksft_print_msg("avg_diff_per: %d%%\n", avg_diff_per);
  89. ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc);
  90. ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc);
  91. if (avg_diff_per > MAX_DIFF_PERCENT)
  92. ret = true;
  93. }
  94. ksft_print_msg("%s Check schemata change using MBA\n",
  95. ret ? "Fail:" : "Pass:");
  96. if (ret)
  97. ksft_print_msg("At least one test failed\n");
  98. return ret;
  99. }
  100. static int check_results(void)
  101. {
  102. char *token_array[8], output[] = RESULT_FILE_NAME, temp[512];
  103. unsigned long bw_imc[1024], bw_resc[1024];
  104. int runs;
  105. FILE *fp;
  106. fp = fopen(output, "r");
  107. if (!fp) {
  108. ksft_perror(output);
  109. return -1;
  110. }
  111. runs = 0;
  112. while (fgets(temp, sizeof(temp), fp)) {
  113. char *token = strtok(temp, ":\t");
  114. int fields = 0;
  115. while (token) {
  116. token_array[fields++] = token;
  117. token = strtok(NULL, ":\t");
  118. }
  119. /* Field 3 is perf imc value */
  120. bw_imc[runs] = strtoul(token_array[3], NULL, 0);
  121. /* Field 5 is resctrl value */
  122. bw_resc[runs] = strtoul(token_array[5], NULL, 0);
  123. runs++;
  124. }
  125. fclose(fp);
  126. return show_mba_info(bw_imc, bw_resc);
  127. }
  128. static void mba_test_cleanup(void)
  129. {
  130. remove(RESULT_FILE_NAME);
  131. }
  132. static int mba_run_test(const struct resctrl_test *test, const struct user_params *uparams)
  133. {
  134. struct resctrl_val_param param = {
  135. .ctrlgrp = "c1",
  136. .filename = RESULT_FILE_NAME,
  137. .init = mba_init,
  138. .setup = mba_setup,
  139. .measure = mba_measure,
  140. };
  141. int ret;
  142. remove(RESULT_FILE_NAME);
  143. ret = resctrl_val(test, uparams, uparams->benchmark_cmd, &param);
  144. if (ret)
  145. return ret;
  146. ret = check_results();
  147. return ret;
  148. }
  149. static bool mba_feature_check(const struct resctrl_test *test)
  150. {
  151. return test_resource_feature_check(test) &&
  152. resctrl_mon_feature_exists("L3_MON", "mbm_local_bytes");
  153. }
  154. struct resctrl_test mba_test = {
  155. .name = "MBA",
  156. .resource = "MB",
  157. .vendor_specific = ARCH_INTEL,
  158. .feature_check = mba_feature_check,
  159. .run_test = mba_run_test,
  160. .cleanup = mba_test_cleanup,
  161. };