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 <sai.praneeth.prakhya@intel.com>, 9 * Fenghua Yu <fenghua.yu@intel.com> 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 has_ben = false, mbm_test = true, mba_test = true, cmt_test = true; 173 int c, cpu_no = 1, argc_new = argc, i, no_of_bits = 0; 174 const char *benchmark_cmd[BENCHMARK_ARGS]; 175 int ben_ind, ben_count, tests = 0; 176 char *span_str = NULL; 177 bool cat_test = true; 178 int ret; 179 180 for (i = 0; i < argc; i++) { 181 if (strcmp(argv[i], "-b") == 0) { 182 ben_ind = i + 1; 183 ben_count = argc - ben_ind; 184 argc_new = ben_ind - 1; 185 has_ben = true; 186 break; 187 } 188 } 189 190 while ((c = getopt(argc_new, argv, "ht:b:n:p:")) != -1) { 191 char *token; 192 193 switch (c) { 194 case 't': 195 token = strtok(optarg, ","); 196 197 mbm_test = false; 198 mba_test = false; 199 cmt_test = false; 200 cat_test = false; 201 while (token) { 202 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) { 203 mbm_test = true; 204 tests++; 205 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) { 206 mba_test = true; 207 tests++; 208 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) { 209 cmt_test = true; 210 tests++; 211 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) { 212 cat_test = true; 213 tests++; 214 } else { 215 printf("invalid argument\n"); 216 217 return -1; 218 } 219 token = strtok(NULL, ","); 220 } 221 break; 222 case 'p': 223 cpu_no = atoi(optarg); 224 break; 225 case 'n': 226 no_of_bits = atoi(optarg); 227 if (no_of_bits <= 0) { 228 printf("Bail out! invalid argument for no_of_bits\n"); 229 return -1; 230 } 231 break; 232 case 'h': 233 cmd_help(); 234 235 return 0; 236 default: 237 printf("invalid argument\n"); 238 239 return -1; 240 } 241 } 242 243 ksft_print_header(); 244 245 /* 246 * Typically we need root privileges, because: 247 * 1. We write to resctrl FS 248 * 2. We execute perf commands 249 */ 250 if (geteuid() != 0) 251 return ksft_exit_skip("Not running as root. Skipping...\n"); 252 253 if (!check_resctrlfs_support()) 254 return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n"); 255 256 if (umount_resctrlfs()) 257 return ksft_exit_skip("resctrl FS unmount failed.\n"); 258 259 filter_dmesg(); 260 261 if (has_ben) { 262 if (argc - ben_ind >= BENCHMARK_ARGS) 263 ksft_exit_fail_msg("Too long benchmark command.\n"); 264 265 /* Extract benchmark command from command line. */ 266 for (i = 0; i < argc - ben_ind; i++) 267 benchmark_cmd[i] = argv[i + ben_ind]; 268 benchmark_cmd[ben_count] = NULL; 269 } else { 270 /* If no benchmark is given by "-b" argument, use fill_buf. */ 271 benchmark_cmd[0] = "fill_buf"; 272 ret = asprintf(&span_str, "%u", DEFAULT_SPAN); 273 if (ret < 0) 274 ksft_exit_fail_msg("Out of memory!\n"); 275 benchmark_cmd[1] = span_str; 276 benchmark_cmd[2] = "1"; 277 benchmark_cmd[3] = "0"; 278 benchmark_cmd[4] = "false"; 279 benchmark_cmd[5] = NULL; 280 } 281 282 ksft_set_plan(tests ? : 4); 283 284 if (mbm_test) 285 run_mbm_test(benchmark_cmd, cpu_no); 286 287 if (mba_test) 288 run_mba_test(benchmark_cmd, cpu_no); 289 290 if (cmt_test) 291 run_cmt_test(benchmark_cmd, cpu_no); 292 293 if (cat_test) 294 run_cat_test(cpu_no, no_of_bits); 295 296 free(span_str); 297 ksft_finished(); 298 } 299