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 char * const *benchmark_cmd, int cpu_no)
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(cpu_no, benchmark_cmd);
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 char * const *benchmark_cmd, int cpu_no)
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(cpu_no, benchmark_cmd);
145 	ksft_test_result(!res, "MBA: schemata change\n");
146 
147 cleanup:
148 	test_cleanup();
149 }
150 
151 static void run_cmt_test(const char * const *benchmark_cmd, int cpu_no)
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(cpu_no, 5, benchmark_cmd);
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(int cpu_no, int no_of_bits)
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(cpu_no, no_of_bits, "L3");
194 	ksft_test_result(!res, "CAT: test\n");
195 
196 cleanup:
197 	test_cleanup();
198 }
199 
200 int main(int argc, char **argv)
201 {
202 	bool mbm_test = true, mba_test = true, cmt_test = true;
203 	const char *benchmark_cmd[BENCHMARK_ARGS] = {};
204 	int c, cpu_no = 1, i, no_of_bits = 0;
205 	char *span_str = NULL;
206 	bool cat_test = true;
207 	int tests = 0;
208 	int ret;
209 
210 	while ((c = getopt(argc, argv, "ht:b:n:p:")) != -1) {
211 		char *token;
212 
213 		switch (c) {
214 		case 'b':
215 			/*
216 			 * First move optind back to the (first) optarg and
217 			 * then build the benchmark command using the
218 			 * remaining arguments.
219 			 */
220 			optind--;
221 			if (argc - optind >= BENCHMARK_ARGS)
222 				ksft_exit_fail_msg("Too long benchmark command");
223 
224 			/* Extract benchmark command from command line. */
225 			for (i = 0; i < argc - optind; i++)
226 				benchmark_cmd[i] = argv[i + optind];
227 			benchmark_cmd[i] = NULL;
228 
229 			goto last_arg;
230 		case 't':
231 			token = strtok(optarg, ",");
232 
233 			mbm_test = false;
234 			mba_test = false;
235 			cmt_test = false;
236 			cat_test = false;
237 			while (token) {
238 				if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
239 					mbm_test = true;
240 					tests++;
241 				} else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
242 					mba_test = true;
243 					tests++;
244 				} else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
245 					cmt_test = true;
246 					tests++;
247 				} else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
248 					cat_test = true;
249 					tests++;
250 				} else {
251 					printf("invalid argument\n");
252 
253 					return -1;
254 				}
255 				token = strtok(NULL, ",");
256 			}
257 			break;
258 		case 'p':
259 			cpu_no = atoi(optarg);
260 			break;
261 		case 'n':
262 			no_of_bits = atoi(optarg);
263 			if (no_of_bits <= 0) {
264 				printf("Bail out! invalid argument for no_of_bits\n");
265 				return -1;
266 			}
267 			break;
268 		case 'h':
269 			cmd_help();
270 
271 			return 0;
272 		default:
273 			printf("invalid argument\n");
274 
275 			return -1;
276 		}
277 	}
278 last_arg:
279 
280 	ksft_print_header();
281 
282 	/*
283 	 * Typically we need root privileges, because:
284 	 * 1. We write to resctrl FS
285 	 * 2. We execute perf commands
286 	 */
287 	if (geteuid() != 0)
288 		return ksft_exit_skip("Not running as root. Skipping...\n");
289 
290 	if (!check_resctrlfs_support())
291 		return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
292 
293 	if (umount_resctrlfs())
294 		return ksft_exit_skip("resctrl FS unmount failed.\n");
295 
296 	filter_dmesg();
297 
298 	if (!benchmark_cmd[0]) {
299 		/* If no benchmark is given by "-b" argument, use fill_buf. */
300 		benchmark_cmd[0] = "fill_buf";
301 		ret = asprintf(&span_str, "%u", DEFAULT_SPAN);
302 		if (ret < 0)
303 			ksft_exit_fail_msg("Out of memory!\n");
304 		benchmark_cmd[1] = span_str;
305 		benchmark_cmd[2] = "1";
306 		benchmark_cmd[3] = "0";
307 		benchmark_cmd[4] = "false";
308 		benchmark_cmd[5] = NULL;
309 	}
310 
311 	ksft_set_plan(tests ? : 4);
312 
313 	if (mbm_test)
314 		run_mbm_test(benchmark_cmd, cpu_no);
315 
316 	if (mba_test)
317 		run_mba_test(benchmark_cmd, cpu_no);
318 
319 	if (cmt_test)
320 		run_cmt_test(benchmark_cmd, cpu_no);
321 
322 	if (cat_test)
323 		run_cat_test(cpu_no, no_of_bits);
324 
325 	free(span_str);
326 	ksft_finished();
327 }
328