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 <[email protected]>,
9  *    Fenghua Yu <[email protected]>
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 int detect_vendor(void)
18 {
19 	FILE *inf = fopen("/proc/cpuinfo", "r");
20 	int vendor_id = 0;
21 	char *s = NULL;
22 	char *res;
23 
24 	if (!inf)
25 		return vendor_id;
26 
27 	res = fgrep(inf, "vendor_id");
28 
29 	if (res)
30 		s = strchr(res, ':');
31 
32 	if (s && !strcmp(s, ": GenuineIntel\n"))
33 		vendor_id = ARCH_INTEL;
34 	else if (s && !strcmp(s, ": AuthenticAMD\n"))
35 		vendor_id = ARCH_AMD;
36 
37 	fclose(inf);
38 	free(res);
39 	return vendor_id;
40 }
41 
42 int get_vendor(void)
43 {
44 	static int vendor = -1;
45 
46 	if (vendor == -1)
47 		vendor = detect_vendor();
48 	if (vendor == 0)
49 		ksft_print_msg("Can not get vendor info...\n");
50 
51 	return vendor;
52 }
53 
54 static void cmd_help(void)
55 {
56 	printf("usage: resctrl_tests [-h] [-t test list] [-n no_of_bits] [-b benchmark_cmd [option]...]\n");
57 	printf("\t-b benchmark_cmd [option]...: run specified benchmark for MBM, MBA and CMT\n");
58 	printf("\t   default benchmark is builtin fill_buf\n");
59 	printf("\t-t test list: run tests specified in the test list, ");
60 	printf("e.g. -t mbm,mba,cmt,cat\n");
61 	printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n");
62 	printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n");
63 	printf("\t-h: help\n");
64 }
65 
66 void tests_cleanup(void)
67 {
68 	mbm_test_cleanup();
69 	mba_test_cleanup();
70 	cmt_test_cleanup();
71 	cat_test_cleanup();
72 }
73 
74 static int test_prepare(void)
75 {
76 	int res;
77 
78 	res = signal_handler_register();
79 	if (res) {
80 		ksft_print_msg("Failed to register signal handler\n");
81 		return res;
82 	}
83 
84 	res = mount_resctrlfs();
85 	if (res) {
86 		signal_handler_unregister();
87 		ksft_print_msg("Failed to mount resctrl FS\n");
88 		return res;
89 	}
90 	return 0;
91 }
92 
93 static void test_cleanup(void)
94 {
95 	umount_resctrlfs();
96 	signal_handler_unregister();
97 }
98 
99 static void run_mbm_test(const struct user_params *uparams)
100 {
101 	int res;
102 
103 	ksft_print_msg("Starting MBM BW change ...\n");
104 
105 	if (test_prepare()) {
106 		ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
107 		return;
108 	}
109 
110 	if (!validate_resctrl_feature_request("L3_MON", "mbm_total_bytes") ||
111 	    !validate_resctrl_feature_request("L3_MON", "mbm_local_bytes") ||
112 	    (get_vendor() != ARCH_INTEL)) {
113 		ksft_test_result_skip("Hardware does not support MBM or MBM is disabled\n");
114 		goto cleanup;
115 	}
116 
117 	res = mbm_bw_change(uparams);
118 	ksft_test_result(!res, "MBM: bw change\n");
119 	if ((get_vendor() == ARCH_INTEL) && res)
120 		ksft_print_msg("Intel MBM may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
121 
122 cleanup:
123 	test_cleanup();
124 }
125 
126 static void run_mba_test(const struct user_params *uparams)
127 {
128 	int res;
129 
130 	ksft_print_msg("Starting MBA Schemata change ...\n");
131 
132 	if (test_prepare()) {
133 		ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
134 		return;
135 	}
136 
137 	if (!validate_resctrl_feature_request("MB", NULL) ||
138 	    !validate_resctrl_feature_request("L3_MON", "mbm_local_bytes") ||
139 	    (get_vendor() != ARCH_INTEL)) {
140 		ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n");
141 		goto cleanup;
142 	}
143 
144 	res = mba_schemata_change(uparams);
145 	ksft_test_result(!res, "MBA: schemata change\n");
146 
147 cleanup:
148 	test_cleanup();
149 }
150 
151 static void run_cmt_test(const struct user_params *uparams)
152 {
153 	int res;
154 
155 	ksft_print_msg("Starting CMT test ...\n");
156 
157 	if (test_prepare()) {
158 		ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
159 		return;
160 	}
161 
162 	if (!validate_resctrl_feature_request("L3_MON", "llc_occupancy") ||
163 	    !validate_resctrl_feature_request("L3", NULL)) {
164 		ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n");
165 		goto cleanup;
166 	}
167 
168 	res = cmt_resctrl_val(uparams);
169 	ksft_test_result(!res, "CMT: test\n");
170 	if ((get_vendor() == ARCH_INTEL) && res)
171 		ksft_print_msg("Intel CMT may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
172 
173 cleanup:
174 	test_cleanup();
175 }
176 
177 static void run_cat_test(const struct user_params *uparams)
178 {
179 	int res;
180 
181 	ksft_print_msg("Starting CAT test ...\n");
182 
183 	if (test_prepare()) {
184 		ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
185 		return;
186 	}
187 
188 	if (!validate_resctrl_feature_request("L3", NULL)) {
189 		ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n");
190 		goto cleanup;
191 	}
192 
193 	res = cat_perf_miss_val(uparams, "L3");
194 	ksft_test_result(!res, "CAT: test\n");
195 
196 cleanup:
197 	test_cleanup();
198 }
199 
200 static void init_user_params(struct user_params *uparams)
201 {
202 	memset(uparams, 0, sizeof(*uparams));
203 
204 	uparams->cpu = 1;
205 	uparams->bits = 0;
206 }
207 
208 int main(int argc, char **argv)
209 {
210 	bool mbm_test = true, mba_test = true, cmt_test = true;
211 	struct user_params uparams;
212 	char *span_str = NULL;
213 	bool cat_test = true;
214 	int tests = 0;
215 	int ret, c, i;
216 
217 	init_user_params(&uparams);
218 
219 	while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) {
220 		char *token;
221 
222 		switch (c) {
223 		case 'b':
224 			/*
225 			 * First move optind back to the (first) optarg and
226 			 * then build the benchmark command using the
227 			 * remaining arguments.
228 			 */
229 			optind--;
230 			if (argc - optind >= BENCHMARK_ARGS)
231 				ksft_exit_fail_msg("Too long benchmark command");
232 
233 			/* Extract benchmark command from command line. */
234 			for (i = 0; i < argc - optind; i++)
235 				uparams.benchmark_cmd[i] = argv[i + optind];
236 			uparams.benchmark_cmd[i] = NULL;
237 
238 			goto last_arg;
239 		case 't':
240 			token = strtok(optarg, ",");
241 
242 			mbm_test = false;
243 			mba_test = false;
244 			cmt_test = false;
245 			cat_test = false;
246 			while (token) {
247 				if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
248 					mbm_test = true;
249 					tests++;
250 				} else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
251 					mba_test = true;
252 					tests++;
253 				} else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
254 					cmt_test = true;
255 					tests++;
256 				} else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
257 					cat_test = true;
258 					tests++;
259 				} else {
260 					printf("invalid argument\n");
261 
262 					return -1;
263 				}
264 				token = strtok(NULL, ",");
265 			}
266 			break;
267 		case 'p':
268 			uparams.cpu = atoi(optarg);
269 			break;
270 		case 'n':
271 			uparams.bits = atoi(optarg);
272 			if (uparams.bits <= 0) {
273 				printf("Bail out! invalid argument for no_of_bits\n");
274 				return -1;
275 			}
276 			break;
277 		case 'h':
278 			cmd_help();
279 
280 			return 0;
281 		default:
282 			printf("invalid argument\n");
283 
284 			return -1;
285 		}
286 	}
287 last_arg:
288 
289 	ksft_print_header();
290 
291 	/*
292 	 * Typically we need root privileges, because:
293 	 * 1. We write to resctrl FS
294 	 * 2. We execute perf commands
295 	 */
296 	if (geteuid() != 0)
297 		return ksft_exit_skip("Not running as root. Skipping...\n");
298 
299 	if (!check_resctrlfs_support())
300 		return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
301 
302 	if (umount_resctrlfs())
303 		return ksft_exit_skip("resctrl FS unmount failed.\n");
304 
305 	filter_dmesg();
306 
307 	if (!uparams.benchmark_cmd[0]) {
308 		/* If no benchmark is given by "-b" argument, use fill_buf. */
309 		uparams.benchmark_cmd[0] = "fill_buf";
310 		ret = asprintf(&span_str, "%u", DEFAULT_SPAN);
311 		if (ret < 0)
312 			ksft_exit_fail_msg("Out of memory!\n");
313 		uparams.benchmark_cmd[1] = span_str;
314 		uparams.benchmark_cmd[2] = "1";
315 		uparams.benchmark_cmd[3] = "0";
316 		uparams.benchmark_cmd[4] = "false";
317 		uparams.benchmark_cmd[5] = NULL;
318 	}
319 
320 	ksft_set_plan(tests ? : 4);
321 
322 	if (mbm_test)
323 		run_mbm_test(&uparams);
324 
325 	if (mba_test)
326 		run_mba_test(&uparams);
327 
328 	if (cmt_test)
329 		run_cmt_test(&uparams);
330 
331 	if (cat_test)
332 		run_cat_test(&uparams);
333 
334 	free(span_str);
335 	ksft_finished();
336 }
337