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 }; 23 24 static int detect_vendor(void) 25 { 26 FILE *inf = fopen("/proc/cpuinfo", "r"); 27 int vendor_id = 0; 28 char *s = NULL; 29 char *res; 30 31 if (!inf) 32 return vendor_id; 33 34 res = fgrep(inf, "vendor_id"); 35 36 if (res) 37 s = strchr(res, ':'); 38 39 if (s && !strcmp(s, ": GenuineIntel\n")) 40 vendor_id = ARCH_INTEL; 41 else if (s && !strcmp(s, ": AuthenticAMD\n")) 42 vendor_id = ARCH_AMD; 43 44 fclose(inf); 45 free(res); 46 return vendor_id; 47 } 48 49 int get_vendor(void) 50 { 51 static int vendor = -1; 52 53 if (vendor == -1) 54 vendor = detect_vendor(); 55 if (vendor == 0) 56 ksft_print_msg("Can not get vendor info...\n"); 57 58 return vendor; 59 } 60 61 static void cmd_help(void) 62 { 63 int i; 64 65 printf("usage: resctrl_tests [-h] [-t test list] [-n no_of_bits] [-b benchmark_cmd [option]...]\n"); 66 printf("\t-b benchmark_cmd [option]...: run specified benchmark for MBM, MBA and CMT\n"); 67 printf("\t default benchmark is builtin fill_buf\n"); 68 printf("\t-t test list: run tests specified in the test list, "); 69 printf("e.g. -t mbm,mba,cmt,cat\n"); 70 printf("\t\tSupported tests:\n"); 71 for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) 72 printf("\t\t\t%s\n", resctrl_tests[i]->name); 73 printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n"); 74 printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n"); 75 printf("\t-h: help\n"); 76 } 77 78 void tests_cleanup(void) 79 { 80 mbm_test_cleanup(); 81 mba_test_cleanup(); 82 cmt_test_cleanup(); 83 cat_test_cleanup(); 84 } 85 86 static int test_prepare(void) 87 { 88 int res; 89 90 res = signal_handler_register(); 91 if (res) { 92 ksft_print_msg("Failed to register signal handler\n"); 93 return res; 94 } 95 96 res = mount_resctrlfs(); 97 if (res) { 98 signal_handler_unregister(); 99 ksft_print_msg("Failed to mount resctrl FS\n"); 100 return res; 101 } 102 return 0; 103 } 104 105 static void test_cleanup(void) 106 { 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()) { 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(); 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 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