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(const struct resctrl_test *test) 104 { 105 if (test->cleanup) 106 test->cleanup(); 107 umount_resctrlfs(); 108 signal_handler_unregister(); 109 } 110 111 static bool test_vendor_specific_check(const struct resctrl_test *test) 112 { 113 if (!test->vendor_specific) 114 return true; 115 116 return get_vendor() & test->vendor_specific; 117 } 118 119 static void run_single_test(const struct resctrl_test *test, const struct user_params *uparams) 120 { 121 int ret; 122 123 if (test->disabled) 124 return; 125 126 if (!test_vendor_specific_check(test)) { 127 ksft_test_result_skip("Hardware does not support %s\n", test->name); 128 return; 129 } 130 131 ksft_print_msg("Starting %s test ...\n", test->name); 132 133 if (test_prepare(test)) { 134 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n"); 135 return; 136 } 137 138 if (!test->feature_check(test)) { 139 ksft_test_result_skip("Hardware does not support %s or %s is disabled\n", 140 test->name, test->name); 141 goto cleanup; 142 } 143 144 ret = test->run_test(test, uparams); 145 ksft_test_result(!ret, "%s: test\n", test->name); 146 147 cleanup: 148 test_cleanup(test); 149 } 150 151 static void init_user_params(struct user_params *uparams) 152 { 153 memset(uparams, 0, sizeof(*uparams)); 154 155 uparams->cpu = 1; 156 uparams->bits = 0; 157 } 158 159 int main(int argc, char **argv) 160 { 161 int tests = ARRAY_SIZE(resctrl_tests); 162 bool test_param_seen = false; 163 struct user_params uparams; 164 char *span_str = NULL; 165 int ret, c, i; 166 167 init_user_params(&uparams); 168 169 while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) { 170 char *token; 171 172 switch (c) { 173 case 'b': 174 /* 175 * First move optind back to the (first) optarg and 176 * then build the benchmark command using the 177 * remaining arguments. 178 */ 179 optind--; 180 if (argc - optind >= BENCHMARK_ARGS) 181 ksft_exit_fail_msg("Too long benchmark command"); 182 183 /* Extract benchmark command from command line. */ 184 for (i = 0; i < argc - optind; i++) 185 uparams.benchmark_cmd[i] = argv[i + optind]; 186 uparams.benchmark_cmd[i] = NULL; 187 188 goto last_arg; 189 case 't': 190 token = strtok(optarg, ","); 191 192 if (!test_param_seen) { 193 for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) 194 resctrl_tests[i]->disabled = true; 195 tests = 0; 196 test_param_seen = true; 197 } 198 while (token) { 199 bool found = false; 200 201 for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) { 202 if (!strcasecmp(token, resctrl_tests[i]->name) || 203 (resctrl_tests[i]->group && 204 !strcasecmp(token, resctrl_tests[i]->group))) { 205 if (resctrl_tests[i]->disabled) 206 tests++; 207 resctrl_tests[i]->disabled = false; 208 found = true; 209 } 210 } 211 212 if (!found) { 213 printf("invalid test: %s\n", token); 214 215 return -1; 216 } 217 token = strtok(NULL, ","); 218 } 219 break; 220 case 'p': 221 uparams.cpu = atoi(optarg); 222 break; 223 case 'n': 224 uparams.bits = atoi(optarg); 225 if (uparams.bits <= 0) { 226 printf("Bail out! invalid argument for no_of_bits\n"); 227 return -1; 228 } 229 break; 230 case 'h': 231 cmd_help(); 232 233 return 0; 234 default: 235 printf("invalid argument\n"); 236 237 return -1; 238 } 239 } 240 last_arg: 241 242 ksft_print_header(); 243 244 /* 245 * Typically we need root privileges, because: 246 * 1. We write to resctrl FS 247 * 2. We execute perf commands 248 */ 249 if (geteuid() != 0) 250 ksft_exit_skip("Not running as root. Skipping...\n"); 251 252 if (!check_resctrlfs_support()) 253 ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n"); 254 255 if (umount_resctrlfs()) 256 ksft_exit_skip("resctrl FS unmount failed.\n"); 257 258 filter_dmesg(); 259 260 if (!uparams.benchmark_cmd[0]) { 261 /* If no benchmark is given by "-b" argument, use fill_buf. */ 262 uparams.benchmark_cmd[0] = "fill_buf"; 263 ret = asprintf(&span_str, "%u", DEFAULT_SPAN); 264 if (ret < 0) 265 ksft_exit_fail_msg("Out of memory!\n"); 266 uparams.benchmark_cmd[1] = span_str; 267 uparams.benchmark_cmd[2] = "1"; 268 uparams.benchmark_cmd[3] = "0"; 269 uparams.benchmark_cmd[4] = "false"; 270 uparams.benchmark_cmd[5] = NULL; 271 } 272 273 ksft_set_plan(tests); 274 275 for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) 276 run_single_test(resctrl_tests[i], &uparams); 277 278 free(span_str); 279 ksft_finished(); 280 } 281