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 void run_mbm_test(const char * const *benchmark_cmd, int cpu_no) 71 { 72 int res; 73 74 ksft_print_msg("Starting MBM BW change ...\n"); 75 76 res = mount_resctrlfs(); 77 if (res) { 78 ksft_exit_fail_msg("Failed to mount resctrl FS\n"); 79 return; 80 } 81 82 if (!validate_resctrl_feature_request(MBM_STR) || (get_vendor() != ARCH_INTEL)) { 83 ksft_test_result_skip("Hardware does not support MBM or MBM is disabled\n"); 84 goto umount; 85 } 86 87 res = mbm_bw_change(cpu_no, benchmark_cmd); 88 ksft_test_result(!res, "MBM: bw change\n"); 89 if ((get_vendor() == ARCH_INTEL) && res) 90 ksft_print_msg("Intel MBM may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n"); 91 92 umount: 93 umount_resctrlfs(); 94 } 95 96 static void run_mba_test(const char * const *benchmark_cmd, int cpu_no) 97 { 98 int res; 99 100 ksft_print_msg("Starting MBA Schemata change ...\n"); 101 102 res = mount_resctrlfs(); 103 if (res) { 104 ksft_exit_fail_msg("Failed to mount resctrl FS\n"); 105 return; 106 } 107 108 if (!validate_resctrl_feature_request(MBA_STR) || (get_vendor() != ARCH_INTEL)) { 109 ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n"); 110 goto umount; 111 } 112 113 res = mba_schemata_change(cpu_no, benchmark_cmd); 114 ksft_test_result(!res, "MBA: schemata change\n"); 115 116 umount: 117 umount_resctrlfs(); 118 } 119 120 static void run_cmt_test(const char * const *benchmark_cmd, int cpu_no) 121 { 122 int res; 123 124 ksft_print_msg("Starting CMT test ...\n"); 125 126 res = mount_resctrlfs(); 127 if (res) { 128 ksft_exit_fail_msg("Failed to mount resctrl FS\n"); 129 return; 130 } 131 132 if (!validate_resctrl_feature_request(CMT_STR)) { 133 ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n"); 134 goto umount; 135 } 136 137 res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd); 138 ksft_test_result(!res, "CMT: test\n"); 139 if ((get_vendor() == ARCH_INTEL) && res) 140 ksft_print_msg("Intel CMT may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n"); 141 142 umount: 143 umount_resctrlfs(); 144 } 145 146 static void run_cat_test(int cpu_no, int no_of_bits) 147 { 148 int res; 149 150 ksft_print_msg("Starting CAT test ...\n"); 151 152 res = mount_resctrlfs(); 153 if (res) { 154 ksft_exit_fail_msg("Failed to mount resctrl FS\n"); 155 return; 156 } 157 158 if (!validate_resctrl_feature_request(CAT_STR)) { 159 ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n"); 160 goto umount; 161 } 162 163 res = cat_perf_miss_val(cpu_no, no_of_bits, "L3"); 164 ksft_test_result(!res, "CAT: test\n"); 165 166 umount: 167 umount_resctrlfs(); 168 } 169 170 int main(int argc, char **argv) 171 { 172 bool mbm_test = true, mba_test = true, cmt_test = true; 173 const char *benchmark_cmd[BENCHMARK_ARGS] = {}; 174 int c, cpu_no = 1, i, no_of_bits = 0; 175 char *span_str = NULL; 176 bool cat_test = true; 177 int tests = 0; 178 int ret; 179 180 while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) { 181 char *token; 182 183 switch (c) { 184 case 'b': 185 /* 186 * First move optind back to the (first) optarg and 187 * then build the benchmark command using the 188 * remaining arguments. 189 */ 190 optind--; 191 if (argc - optind >= BENCHMARK_ARGS) 192 ksft_exit_fail_msg("Too long benchmark command"); 193 194 /* Extract benchmark command from command line. */ 195 for (i = 0; i < argc - optind; i++) 196 benchmark_cmd[i] = argv[i + optind]; 197 benchmark_cmd[i] = NULL; 198 199 goto last_arg; 200 case 't': 201 token = strtok(optarg, ","); 202 203 mbm_test = false; 204 mba_test = false; 205 cmt_test = false; 206 cat_test = false; 207 while (token) { 208 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) { 209 mbm_test = true; 210 tests++; 211 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) { 212 mba_test = true; 213 tests++; 214 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) { 215 cmt_test = true; 216 tests++; 217 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) { 218 cat_test = true; 219 tests++; 220 } else { 221 printf("invalid argument\n"); 222 223 return -1; 224 } 225 token = strtok(NULL, ","); 226 } 227 break; 228 case 'p': 229 cpu_no = atoi(optarg); 230 break; 231 case 'n': 232 no_of_bits = atoi(optarg); 233 if (no_of_bits <= 0) { 234 printf("Bail out! invalid argument for no_of_bits\n"); 235 return -1; 236 } 237 break; 238 case 'h': 239 cmd_help(); 240 241 return 0; 242 default: 243 printf("invalid argument\n"); 244 245 return -1; 246 } 247 } 248 last_arg: 249 250 ksft_print_header(); 251 252 /* 253 * Typically we need root privileges, because: 254 * 1. We write to resctrl FS 255 * 2. We execute perf commands 256 */ 257 if (geteuid() != 0) 258 return ksft_exit_skip("Not running as root. Skipping...\n"); 259 260 if (!check_resctrlfs_support()) 261 return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n"); 262 263 if (umount_resctrlfs()) 264 return ksft_exit_skip("resctrl FS unmount failed.\n"); 265 266 filter_dmesg(); 267 268 if (!benchmark_cmd[0]) { 269 /* If no benchmark is given by "-b" argument, use fill_buf. */ 270 benchmark_cmd[0] = "fill_buf"; 271 ret = asprintf(&span_str, "%u", DEFAULT_SPAN); 272 if (ret < 0) 273 ksft_exit_fail_msg("Out of memory!\n"); 274 benchmark_cmd[1] = span_str; 275 benchmark_cmd[2] = "1"; 276 benchmark_cmd[3] = "0"; 277 benchmark_cmd[4] = "false"; 278 benchmark_cmd[5] = NULL; 279 } 280 281 ksft_set_plan(tests ? : 4); 282 283 if (mbm_test) 284 run_mbm_test(benchmark_cmd, cpu_no); 285 286 if (mba_test) 287 run_mba_test(benchmark_cmd, cpu_no); 288 289 if (cmt_test) 290 run_cmt_test(benchmark_cmd, cpu_no); 291 292 if (cat_test) 293 run_cat_test(cpu_no, no_of_bits); 294 295 free(span_str); 296 ksft_finished(); 297 } 298