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 <sai.praneeth.prakhya@intel.com>,
9  *    Fenghua Yu <fenghua.yu@intel.com>
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] [-b \"benchmark_cmd [options]\"] [-t test list] [-n no_of_bits]\n");
53 	printf("\t-b benchmark_cmd [options]: 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) ||
134 	    !validate_resctrl_feature_request("L3_MON", "mbm_local_bytes") ||
135 	    (get_vendor() != ARCH_INTEL)) {
136 		ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n");
137 		goto cleanup;
138 	}
139 
140 	res = mba_schemata_change(cpu_no, benchmark_cmd);
141 	ksft_test_result(!res, "MBA: schemata change\n");
142 
143 cleanup:
144 	test_cleanup();
145 }
146 
147 static void run_cmt_test(const char * const *benchmark_cmd, int cpu_no)
148 {
149 	int res;
150 
151 	ksft_print_msg("Starting CMT test ...\n");
152 
153 	if (test_prepare()) {
154 		ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
155 		return;
156 	}
157 
158 	if (!validate_resctrl_feature_request("L3_MON", "llc_occupancy") ||
159 	    !validate_resctrl_feature_request("L3", NULL)) {
160 		ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n");
161 		goto cleanup;
162 	}
163 
164 	res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd);
165 	ksft_test_result(!res, "CMT: test\n");
166 	if ((get_vendor() == ARCH_INTEL) && res)
167 		ksft_print_msg("Intel CMT may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
168 
169 cleanup:
170 	test_cleanup();
171 }
172 
173 static void run_cat_test(int cpu_no, int no_of_bits)
174 {
175 	int res;
176 
177 	ksft_print_msg("Starting CAT test ...\n");
178 
179 	if (test_prepare()) {
180 		ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
181 		return;
182 	}
183 
184 	if (!validate_resctrl_feature_request("L3", NULL)) {
185 		ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n");
186 		goto cleanup;
187 	}
188 
189 	res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
190 	ksft_test_result(!res, "CAT: test\n");
191 
192 cleanup:
193 	test_cleanup();
194 }
195 
196 int main(int argc, char **argv)
197 {
198 	bool has_ben = false, mbm_test = true, mba_test = true, cmt_test = true;
199 	int c, cpu_no = 1, argc_new = argc, i, no_of_bits = 0;
200 	const char *benchmark_cmd[BENCHMARK_ARGS];
201 	int ben_ind, ben_count, tests = 0;
202 	char *span_str = NULL;
203 	bool cat_test = true;
204 	int ret;
205 
206 	for (i = 0; i < argc; i++) {
207 		if (strcmp(argv[i], "-b") == 0) {
208 			ben_ind = i + 1;
209 			ben_count = argc - ben_ind;
210 			argc_new = ben_ind - 1;
211 			has_ben = true;
212 			break;
213 		}
214 	}
215 
216 	while ((c = getopt(argc_new, argv, "ht:b:n:p:")) != -1) {
217 		char *token;
218 
219 		switch (c) {
220 		case 't':
221 			token = strtok(optarg, ",");
222 
223 			mbm_test = false;
224 			mba_test = false;
225 			cmt_test = false;
226 			cat_test = false;
227 			while (token) {
228 				if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
229 					mbm_test = true;
230 					tests++;
231 				} else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
232 					mba_test = true;
233 					tests++;
234 				} else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
235 					cmt_test = true;
236 					tests++;
237 				} else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
238 					cat_test = true;
239 					tests++;
240 				} else {
241 					printf("invalid argument\n");
242 
243 					return -1;
244 				}
245 				token = strtok(NULL, ",");
246 			}
247 			break;
248 		case 'p':
249 			cpu_no = atoi(optarg);
250 			break;
251 		case 'n':
252 			no_of_bits = atoi(optarg);
253 			if (no_of_bits <= 0) {
254 				printf("Bail out! invalid argument for no_of_bits\n");
255 				return -1;
256 			}
257 			break;
258 		case 'h':
259 			cmd_help();
260 
261 			return 0;
262 		default:
263 			printf("invalid argument\n");
264 
265 			return -1;
266 		}
267 	}
268 
269 	ksft_print_header();
270 
271 	/*
272 	 * Typically we need root privileges, because:
273 	 * 1. We write to resctrl FS
274 	 * 2. We execute perf commands
275 	 */
276 	if (geteuid() != 0)
277 		return ksft_exit_skip("Not running as root. Skipping...\n");
278 
279 	if (has_ben) {
280 		if (argc - ben_ind >= BENCHMARK_ARGS)
281 			ksft_exit_fail_msg("Too long benchmark command.\n");
282 
283 		/* Extract benchmark command from command line. */
284 		for (i = 0; i < argc - ben_ind; i++)
285 			benchmark_cmd[i] = argv[i + ben_ind];
286 		benchmark_cmd[ben_count] = NULL;
287 	} else {
288 		/* If no benchmark is given by "-b" argument, use fill_buf. */
289 		benchmark_cmd[0] = "fill_buf";
290 		ret = asprintf(&span_str, "%u", DEFAULT_SPAN);
291 		if (ret < 0)
292 			ksft_exit_fail_msg("Out of memory!\n");
293 		benchmark_cmd[1] = span_str;
294 		benchmark_cmd[2] = "1";
295 		benchmark_cmd[3] = "0";
296 		benchmark_cmd[4] = "false";
297 		benchmark_cmd[5] = NULL;
298 	}
299 
300 	if (!check_resctrlfs_support())
301 		return ksft_exit_skip("resctrl FS does not exist. Enable X86_CPU_RESCTRL config option.\n");
302 
303 	if (umount_resctrlfs())
304 		return ksft_exit_skip("resctrl FS unmount failed.\n");
305 
306 	filter_dmesg();
307 
308 	ksft_set_plan(tests ? : 4);
309 
310 	if (mbm_test)
311 		run_mbm_test(benchmark_cmd, cpu_no);
312 
313 	if (mba_test)
314 		run_mba_test(benchmark_cmd, cpu_no);
315 
316 	if (cmt_test)
317 		run_cmt_test(benchmark_cmd, cpu_no);
318 
319 	if (cat_test)
320 		run_cat_test(cpu_no, no_of_bits);
321 
322 	free(span_str);
323 	ksft_finished();
324 }
325