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