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 <[email protected]>, 9 * Fenghua Yu <[email protected]> 10 */ 11 #include "resctrl.h" 12 13 static int detect_vendor(void) 14 { 15 FILE *inf = fopen("/proc/cpuinfo", "r"); 16 int vendor_id = 0; 17 char *s = NULL; 18 char *res; 19 20 if (!inf) 21 return vendor_id; 22 23 res = fgrep(inf, "vendor_id"); 24 25 if (res) 26 s = strchr(res, ':'); 27 28 if (s && !strcmp(s, ": GenuineIntel\n")) 29 vendor_id = ARCH_INTEL; 30 else if (s && !strcmp(s, ": AuthenticAMD\n")) 31 vendor_id = ARCH_AMD; 32 33 fclose(inf); 34 free(res); 35 return vendor_id; 36 } 37 38 int get_vendor(void) 39 { 40 static int vendor = -1; 41 42 if (vendor == -1) 43 vendor = detect_vendor(); 44 if (vendor == 0) 45 ksft_print_msg("Can not get vendor info...\n"); 46 47 return vendor; 48 } 49 50 static void cmd_help(void) 51 { 52 printf("usage: resctrl_tests [-h] [-t test list] [-n no_of_bits] [-b benchmark_cmd [option]...]\n"); 53 printf("\t-b benchmark_cmd [option]...: run specified benchmark for MBM, MBA and CMT\n"); 54 printf("\t default benchmark is builtin fill_buf\n"); 55 printf("\t-t test list: run tests specified in the test list, "); 56 printf("e.g. -t mbm,mba,cmt,cat\n"); 57 printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n"); 58 printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n"); 59 printf("\t-h: help\n"); 60 } 61 62 void tests_cleanup(void) 63 { 64 mbm_test_cleanup(); 65 mba_test_cleanup(); 66 cmt_test_cleanup(); 67 cat_test_cleanup(); 68 } 69 70 static int test_prepare(void) 71 { 72 int res; 73 74 res = signal_handler_register(); 75 if (res) { 76 ksft_print_msg("Failed to register signal handler\n"); 77 return res; 78 } 79 80 res = mount_resctrlfs(); 81 if (res) { 82 signal_handler_unregister(); 83 ksft_print_msg("Failed to mount resctrl FS\n"); 84 return res; 85 } 86 return 0; 87 } 88 89 static void test_cleanup(void) 90 { 91 umount_resctrlfs(); 92 signal_handler_unregister(); 93 } 94 95 static void run_mbm_test(const char * const *benchmark_cmd, int cpu_no) 96 { 97 int res; 98 99 ksft_print_msg("Starting MBM BW change ...\n"); 100 101 if (test_prepare()) { 102 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n"); 103 return; 104 } 105 106 if (!validate_resctrl_feature_request(MBM_STR) || (get_vendor() != ARCH_INTEL)) { 107 ksft_test_result_skip("Hardware does not support MBM or MBM is disabled\n"); 108 goto cleanup; 109 } 110 111 res = mbm_bw_change(cpu_no, benchmark_cmd); 112 ksft_test_result(!res, "MBM: bw change\n"); 113 if ((get_vendor() == ARCH_INTEL) && res) 114 ksft_print_msg("Intel MBM may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n"); 115 116 cleanup: 117 test_cleanup(); 118 } 119 120 static void run_mba_test(const char * const *benchmark_cmd, int cpu_no) 121 { 122 int res; 123 124 ksft_print_msg("Starting MBA Schemata change ...\n"); 125 126 if (test_prepare()) { 127 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n"); 128 return; 129 } 130 131 if (!validate_resctrl_feature_request(MBA_STR) || (get_vendor() != ARCH_INTEL)) { 132 ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n"); 133 goto cleanup; 134 } 135 136 res = mba_schemata_change(cpu_no, benchmark_cmd); 137 ksft_test_result(!res, "MBA: schemata change\n"); 138 139 cleanup: 140 test_cleanup(); 141 } 142 143 static void run_cmt_test(const char * const *benchmark_cmd, int cpu_no) 144 { 145 int res; 146 147 ksft_print_msg("Starting CMT test ...\n"); 148 149 if (test_prepare()) { 150 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n"); 151 return; 152 } 153 154 if (!validate_resctrl_feature_request(CMT_STR)) { 155 ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n"); 156 goto cleanup; 157 } 158 159 res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd); 160 ksft_test_result(!res, "CMT: test\n"); 161 if ((get_vendor() == ARCH_INTEL) && res) 162 ksft_print_msg("Intel CMT may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n"); 163 164 cleanup: 165 test_cleanup(); 166 } 167 168 static void run_cat_test(int cpu_no, int no_of_bits) 169 { 170 int res; 171 172 ksft_print_msg("Starting CAT test ...\n"); 173 174 if (test_prepare()) { 175 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n"); 176 return; 177 } 178 179 if (!validate_resctrl_feature_request(CAT_STR)) { 180 ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n"); 181 goto cleanup; 182 } 183 184 res = cat_perf_miss_val(cpu_no, no_of_bits, "L3"); 185 ksft_test_result(!res, "CAT: test\n"); 186 187 cleanup: 188 test_cleanup(); 189 } 190 191 int main(int argc, char **argv) 192 { 193 bool mbm_test = true, mba_test = true, cmt_test = true; 194 const char *benchmark_cmd[BENCHMARK_ARGS] = {}; 195 int c, cpu_no = 1, i, no_of_bits = 0; 196 char *span_str = NULL; 197 bool cat_test = true; 198 int tests = 0; 199 int ret; 200 201 while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) { 202 char *token; 203 204 switch (c) { 205 case 'b': 206 /* 207 * First move optind back to the (first) optarg and 208 * then build the benchmark command using the 209 * remaining arguments. 210 */ 211 optind--; 212 if (argc - optind >= BENCHMARK_ARGS) 213 ksft_exit_fail_msg("Too long benchmark command"); 214 215 /* Extract benchmark command from command line. */ 216 for (i = 0; i < argc - optind; i++) 217 benchmark_cmd[i] = argv[i + optind]; 218 benchmark_cmd[i] = NULL; 219 220 goto last_arg; 221 case 't': 222 token = strtok(optarg, ","); 223 224 mbm_test = false; 225 mba_test = false; 226 cmt_test = false; 227 cat_test = false; 228 while (token) { 229 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) { 230 mbm_test = true; 231 tests++; 232 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) { 233 mba_test = true; 234 tests++; 235 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) { 236 cmt_test = true; 237 tests++; 238 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) { 239 cat_test = true; 240 tests++; 241 } else { 242 printf("invalid argument\n"); 243 244 return -1; 245 } 246 token = strtok(NULL, ","); 247 } 248 break; 249 case 'p': 250 cpu_no = atoi(optarg); 251 break; 252 case 'n': 253 no_of_bits = atoi(optarg); 254 if (no_of_bits <= 0) { 255 printf("Bail out! invalid argument for no_of_bits\n"); 256 return -1; 257 } 258 break; 259 case 'h': 260 cmd_help(); 261 262 return 0; 263 default: 264 printf("invalid argument\n"); 265 266 return -1; 267 } 268 } 269 last_arg: 270 271 ksft_print_header(); 272 273 /* 274 * Typically we need root privileges, because: 275 * 1. We write to resctrl FS 276 * 2. We execute perf commands 277 */ 278 if (geteuid() != 0) 279 return ksft_exit_skip("Not running as root. Skipping...\n"); 280 281 if (!check_resctrlfs_support()) 282 return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n"); 283 284 if (umount_resctrlfs()) 285 return ksft_exit_skip("resctrl FS unmount failed.\n"); 286 287 filter_dmesg(); 288 289 if (!benchmark_cmd[0]) { 290 /* If no benchmark is given by "-b" argument, use fill_buf. */ 291 benchmark_cmd[0] = "fill_buf"; 292 ret = asprintf(&span_str, "%u", DEFAULT_SPAN); 293 if (ret < 0) 294 ksft_exit_fail_msg("Out of memory!\n"); 295 benchmark_cmd[1] = span_str; 296 benchmark_cmd[2] = "1"; 297 benchmark_cmd[3] = "0"; 298 benchmark_cmd[4] = "false"; 299 benchmark_cmd[5] = NULL; 300 } 301 302 ksft_set_plan(tests ? : 4); 303 304 if (mbm_test) 305 run_mbm_test(benchmark_cmd, cpu_no); 306 307 if (mba_test) 308 run_mba_test(benchmark_cmd, cpu_no); 309 310 if (cmt_test) 311 run_cmt_test(benchmark_cmd, cpu_no); 312 313 if (cat_test) 314 run_cat_test(cpu_no, no_of_bits); 315 316 free(span_str); 317 ksft_finished(); 318 } 319