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