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