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