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