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