drm/bridge: adv7511: Switch to atomic operations
[drm/drm-misc.git] / tools / testing / selftests / resctrl / resctrl_tests.c
blob3335af815b210767794ecf789860fec2d510f1c4
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Resctrl tests
5 * Copyright (C) 2018 Intel Corporation
7 * Authors:
8 * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
9 * Fenghua Yu <fenghua.yu@intel.com>
11 #include "resctrl.h"
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[] = {
18 &mbm_test,
19 &mba_test,
20 &cmt_test,
21 &l3_cat_test,
22 &l3_noncont_cat_test,
23 &l2_noncont_cat_test,
26 static int detect_vendor(void)
28 FILE *inf = fopen("/proc/cpuinfo", "r");
29 int vendor_id = 0;
30 char *s = NULL;
31 char *res;
33 if (!inf)
34 return vendor_id;
36 res = fgrep(inf, "vendor_id");
38 if (res)
39 s = strchr(res, ':');
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;
46 fclose(inf);
47 free(res);
48 return vendor_id;
51 int get_vendor(void)
53 static int vendor = -1;
55 if (vendor == -1)
56 vendor = detect_vendor();
57 if (vendor == 0)
58 ksft_print_msg("Can not get vendor info...\n");
60 return vendor;
63 static void cmd_help(void)
65 int i;
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);
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)
86 int res;
88 res = signal_handler_register(test);
89 if (res) {
90 ksft_print_msg("Failed to register signal handler\n");
91 return res;
94 res = mount_resctrlfs();
95 if (res) {
96 signal_handler_unregister();
97 ksft_print_msg("Failed to mount resctrl FS\n");
98 return res;
100 return 0;
103 static void test_cleanup(const struct resctrl_test *test)
105 if (test->cleanup)
106 test->cleanup();
107 umount_resctrlfs();
108 signal_handler_unregister();
111 static bool test_vendor_specific_check(const struct resctrl_test *test)
113 if (!test->vendor_specific)
114 return true;
116 return get_vendor() & test->vendor_specific;
119 static void run_single_test(const struct resctrl_test *test, const struct user_params *uparams)
121 int ret;
123 if (test->disabled)
124 return;
126 if (!test_vendor_specific_check(test)) {
127 ksft_test_result_skip("Hardware does not support %s\n", test->name);
128 return;
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");
135 return;
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;
144 ret = test->run_test(test, uparams);
145 ksft_test_result(!ret, "%s: test\n", test->name);
147 cleanup:
148 test_cleanup(test);
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
168 * "-b fill_buf").
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;
175 char *endptr = NULL;
177 if (!uparams->benchmark_cmd[0] || strcmp(uparams->benchmark_cmd[0], "fill_buf"))
178 return NULL;
180 fill_param = malloc(sizeof(*fill_param));
181 if (!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') {
185 errno = 0;
186 fill_param->buf_size = strtoul(uparams->benchmark_cmd[1], &endptr, 10);
187 if (errno || *endptr != '\0') {
188 free(fill_param);
189 ksft_exit_skip("Unable to parse benchmark buffer size.\n");
191 } else {
192 fill_param->buf_size = MINIMUM_SPAN;
195 if (uparams->benchmark_cmd[2] && *uparams->benchmark_cmd[2] != '\0') {
196 errno = 0;
197 fill_param->memflush = strtol(uparams->benchmark_cmd[2], &endptr, 10) != 0;
198 if (errno || *endptr != '\0') {
199 free(fill_param);
200 ksft_exit_skip("Unable to parse benchmark memflush parameter.\n");
202 } else {
203 fill_param->memflush = true;
206 if (uparams->benchmark_cmd[3] && *uparams->benchmark_cmd[3] != '\0') {
207 if (strcmp(uparams->benchmark_cmd[3], "0")) {
208 free(fill_param);
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")) {
215 free(fill_param);
216 ksft_exit_skip("fill_buf is required to run until termination.\n");
220 return fill_param;
223 static void init_user_params(struct user_params *uparams)
225 memset(uparams, 0, sizeof(*uparams));
227 uparams->cpu = 1;
228 uparams->bits = 0;
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;
237 int c, i;
239 init_user_params(&uparams);
241 while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) {
242 char *token;
244 switch (c) {
245 case 'b':
247 * First move optind back to the (first) optarg and
248 * then build the benchmark command using the
249 * remaining arguments.
251 optind--;
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;
260 goto last_arg;
261 case 't':
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;
267 tests = 0;
268 test_param_seen = true;
270 while (token) {
271 bool found = false;
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)
278 tests++;
279 resctrl_tests[i]->disabled = false;
280 found = true;
284 if (!found) {
285 printf("invalid test: %s\n", token);
287 return -1;
289 token = strtok(NULL, ",");
291 break;
292 case 'p':
293 uparams.cpu = atoi(optarg);
294 break;
295 case 'n':
296 uparams.bits = atoi(optarg);
297 if (uparams.bits <= 0) {
298 printf("Bail out! invalid argument for no_of_bits\n");
299 return -1;
301 break;
302 case 'h':
303 cmd_help();
305 return 0;
306 default:
307 printf("invalid argument\n");
309 return -1;
312 last_arg:
314 fill_param = alloc_fill_buf_param(&uparams);
315 if (fill_param)
316 uparams.fill_buf = fill_param;
318 ksft_print_header();
321 * Typically we need root privileges, because:
322 * 1. We write to resctrl FS
323 * 2. We execute perf commands
325 if (geteuid() != 0)
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");
334 filter_dmesg();
336 ksft_set_plan(tests);
338 for (i = 0; i < ARRAY_SIZE(resctrl_tests); i++)
339 run_single_test(resctrl_tests[i], &uparams);
341 free(fill_param);
342 ksft_finished();