1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018 Intel Corporation
13 /* Volatile memory sink to prevent compiler optimizations */
14 static volatile int sink_target;
15 volatile int *value_sink = &sink_target;
17 static struct resctrl_test *resctrl_tests[] = {
26 static int detect_vendor(void)
28 FILE *inf = fopen("/proc/cpuinfo", "r");
36 res = fgrep(inf, "vendor_id");
41 if (s && !strcmp(s, ": GenuineIntel\n"))
42 vendor_id = ARCH_INTEL;
43 else if (s && !strcmp(s, ": AuthenticAMD\n"))
53 static int vendor = -1;
56 vendor = detect_vendor();
58 ksft_print_msg("Can not get vendor info...\n");
63 static void cmd_help(void)
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);
77 printf("\t\t\t%s\n", resctrl_tests[i]->name);
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");
84 static int test_prepare(const struct resctrl_test *test)
88 res = signal_handler_register(test);
90 ksft_print_msg("Failed to register signal handler\n");
94 res = mount_resctrlfs();
96 signal_handler_unregister();
97 ksft_print_msg("Failed to mount resctrl FS\n");
103 static void test_cleanup(const struct resctrl_test *test)
108 signal_handler_unregister();
111 static bool test_vendor_specific_check(const struct resctrl_test *test)
113 if (!test->vendor_specific)
116 return get_vendor() & test->vendor_specific;
119 static void run_single_test(const struct resctrl_test *test, const struct user_params *uparams)
126 if (!test_vendor_specific_check(test)) {
127 ksft_test_result_skip("Hardware does not support %s\n", test->name);
131 ksft_print_msg("Starting %s test ...\n", test->name);
133 if (test_prepare(test)) {
134 ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
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);
144 ret = test->run_test(test, uparams);
145 ksft_test_result(!ret, "%s: test\n", test->name);
152 * Allocate and initialize a struct fill_buf_param with user provided
153 * (via "-b fill_buf <fill_buf parameters>") parameters.
155 * Use defaults (that may not be appropriate for all tests) for any
156 * fill_buf parameters omitted by the user.
158 * Historically it may have been possible for user space to provide
159 * additional parameters, "operation" ("read" vs "write") in
160 * benchmark_cmd[3] and "once" (run "once" or until terminated) in
161 * benchmark_cmd[4]. Changing these parameters have never been
162 * supported with the default of "read" operation and running until
163 * terminated built into the tests. Any unsupported values for
164 * (original) "fill_buf" parameters are treated as failure.
166 * Return: On failure, forcibly exits the test on any parsing failure,
167 * returns NULL if no parsing needed (user did not actually provide
169 * On success, returns pointer to newly allocated and fully
170 * initialized struct fill_buf_param that caller must free.
172 static struct fill_buf_param *alloc_fill_buf_param(struct user_params *uparams)
174 struct fill_buf_param *fill_param = NULL;
177 if (!uparams->benchmark_cmd[0] || strcmp(uparams->benchmark_cmd[0], "fill_buf"))
180 fill_param = malloc(sizeof(*fill_param));
182 ksft_exit_skip("Unable to allocate memory for fill_buf parameters.\n");
184 if (uparams->benchmark_cmd[1] && *uparams->benchmark_cmd[1] != '\0') {
186 fill_param->buf_size = strtoul(uparams->benchmark_cmd[1], &endptr, 10);
187 if (errno || *endptr != '\0') {
189 ksft_exit_skip("Unable to parse benchmark buffer size.\n");
192 fill_param->buf_size = MINIMUM_SPAN;
195 if (uparams->benchmark_cmd[2] && *uparams->benchmark_cmd[2] != '\0') {
197 fill_param->memflush = strtol(uparams->benchmark_cmd[2], &endptr, 10) != 0;
198 if (errno || *endptr != '\0') {
200 ksft_exit_skip("Unable to parse benchmark memflush parameter.\n");
203 fill_param->memflush = true;
206 if (uparams->benchmark_cmd[3] && *uparams->benchmark_cmd[3] != '\0') {
207 if (strcmp(uparams->benchmark_cmd[3], "0")) {
209 ksft_exit_skip("Only read operations supported.\n");
213 if (uparams->benchmark_cmd[4] && *uparams->benchmark_cmd[4] != '\0') {
214 if (strcmp(uparams->benchmark_cmd[4], "false")) {
216 ksft_exit_skip("fill_buf is required to run until termination.\n");
223 static void init_user_params(struct user_params *uparams)
225 memset(uparams, 0, sizeof(*uparams));
231 int main(int argc, char **argv)
233 struct fill_buf_param *fill_param = NULL;
234 int tests = ARRAY_SIZE(resctrl_tests);
235 bool test_param_seen = false;
236 struct user_params uparams;
239 init_user_params(&uparams);
241 while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) {
247 * First move optind back to the (first) optarg and
248 * then build the benchmark command using the
249 * remaining arguments.
252 if (argc - optind >= BENCHMARK_ARGS)
253 ksft_exit_fail_msg("Too long benchmark command");
255 /* Extract benchmark command from command line. */
256 for (i = 0; i < argc - optind; i++)
257 uparams.benchmark_cmd[i] = argv[i + optind];
258 uparams.benchmark_cmd[i] = NULL;
262 token = strtok(optarg, ",");
264 if (!test_param_seen) {
265 for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++)
266 resctrl_tests[i]->disabled = true;
268 test_param_seen = true;
273 for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++) {
274 if (!strcasecmp(token, resctrl_tests[i]->name) ||
275 (resctrl_tests[i]->group &&
276 !strcasecmp(token, resctrl_tests[i]->group))) {
277 if (resctrl_tests[i]->disabled)
279 resctrl_tests[i]->disabled = false;
285 printf("invalid test: %s\n", token);
289 token = strtok(NULL, ",");
293 uparams.cpu = atoi(optarg);
296 uparams.bits = atoi(optarg);
297 if (uparams.bits <= 0) {
298 printf("Bail out! invalid argument for no_of_bits\n");
307 printf("invalid argument\n");
314 fill_param = alloc_fill_buf_param(&uparams);
316 uparams.fill_buf = fill_param;
321 * Typically we need root privileges, because:
322 * 1. We write to resctrl FS
323 * 2. We execute perf commands
326 ksft_exit_skip("Not running as root. Skipping...\n");
328 if (!check_resctrlfs_support())
329 ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
331 if (umount_resctrlfs())
332 ksft_exit_skip("resctrl FS unmount failed.\n");
336 ksft_set_plan(tests);
338 for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++)
339 run_single_test(resctrl_tests[i], &uparams);