• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define BENCHMARK_ARGS		64
14 #define BENCHMARK_ARG_SIZE	64
15 
detect_vendor(void)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 
get_vendor(void)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 
cmd_help(void)53 static void cmd_help(void)
54 {
55 	printf("usage: resctrl_tests [-h] [-b \"benchmark_cmd [options]\"] [-t test list] [-n no_of_bits]\n");
56 	printf("\t-b benchmark_cmd [options]: 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 
tests_cleanup(void)65 void tests_cleanup(void)
66 {
67 	mbm_test_cleanup();
68 	mba_test_cleanup();
69 	cmt_test_cleanup();
70 	cat_test_cleanup();
71 }
72 
run_mbm_test(bool has_ben,char ** benchmark_cmd,int span,int cpu_no,char * bw_report)73 static void run_mbm_test(bool has_ben, char **benchmark_cmd, int span,
74 			 int cpu_no, char *bw_report)
75 {
76 	int res;
77 
78 	ksft_print_msg("Starting MBM BW change ...\n");
79 
80 	if (!validate_resctrl_feature_request(MBM_STR)) {
81 		ksft_test_result_skip("Hardware does not support MBM or MBM is disabled\n");
82 		return;
83 	}
84 
85 	if (!has_ben)
86 		sprintf(benchmark_cmd[5], "%s", MBA_STR);
87 	res = mbm_bw_change(span, cpu_no, bw_report, benchmark_cmd);
88 	ksft_test_result(!res, "MBM: bw change\n");
89 	mbm_test_cleanup();
90 }
91 
run_mba_test(bool has_ben,char ** benchmark_cmd,int span,int cpu_no,char * bw_report)92 static void run_mba_test(bool has_ben, char **benchmark_cmd, int span,
93 			 int cpu_no, char *bw_report)
94 {
95 	int res;
96 
97 	ksft_print_msg("Starting MBA Schemata change ...\n");
98 
99 	if (!validate_resctrl_feature_request(MBA_STR)) {
100 		ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n");
101 		return;
102 	}
103 
104 	if (!has_ben)
105 		sprintf(benchmark_cmd[1], "%d", span);
106 	res = mba_schemata_change(cpu_no, bw_report, benchmark_cmd);
107 	ksft_test_result(!res, "MBA: schemata change\n");
108 	mba_test_cleanup();
109 }
110 
run_cmt_test(bool has_ben,char ** benchmark_cmd,int cpu_no)111 static void run_cmt_test(bool has_ben, char **benchmark_cmd, int cpu_no)
112 {
113 	int res;
114 
115 	ksft_print_msg("Starting CMT test ...\n");
116 	if (!validate_resctrl_feature_request(CMT_STR)) {
117 		ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n");
118 		return;
119 	}
120 
121 	if (!has_ben)
122 		sprintf(benchmark_cmd[5], "%s", CMT_STR);
123 	res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd);
124 	ksft_test_result(!res, "CMT: test\n");
125 	cmt_test_cleanup();
126 }
127 
run_cat_test(int cpu_no,int no_of_bits)128 static void run_cat_test(int cpu_no, int no_of_bits)
129 {
130 	int res;
131 
132 	ksft_print_msg("Starting CAT test ...\n");
133 
134 	if (!validate_resctrl_feature_request(CAT_STR)) {
135 		ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n");
136 		return;
137 	}
138 
139 	res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
140 	ksft_test_result(!res, "CAT: test\n");
141 	cat_test_cleanup();
142 }
143 
main(int argc,char ** argv)144 int main(int argc, char **argv)
145 {
146 	bool has_ben = false, mbm_test = true, mba_test = true, cmt_test = true;
147 	int c, cpu_no = 1, span = 250, argc_new = argc, i, no_of_bits = 0;
148 	char *benchmark_cmd[BENCHMARK_ARGS], bw_report[64], bm_type[64];
149 	char benchmark_cmd_area[BENCHMARK_ARGS][BENCHMARK_ARG_SIZE];
150 	int ben_ind, ben_count, tests = 0;
151 	bool cat_test = true;
152 
153 	for (i = 0; i < argc; i++) {
154 		if (strcmp(argv[i], "-b") == 0) {
155 			ben_ind = i + 1;
156 			ben_count = argc - ben_ind;
157 			argc_new = ben_ind - 1;
158 			has_ben = true;
159 			break;
160 		}
161 	}
162 
163 	while ((c = getopt(argc_new, argv, "ht:b:n:p:")) != -1) {
164 		char *token;
165 
166 		switch (c) {
167 		case 't':
168 			token = strtok(optarg, ",");
169 
170 			mbm_test = false;
171 			mba_test = false;
172 			cmt_test = false;
173 			cat_test = false;
174 			while (token) {
175 				if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) {
176 					mbm_test = true;
177 					tests++;
178 				} else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) {
179 					mba_test = true;
180 					tests++;
181 				} else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) {
182 					cmt_test = true;
183 					tests++;
184 				} else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) {
185 					cat_test = true;
186 					tests++;
187 				} else {
188 					printf("invalid argument\n");
189 
190 					return -1;
191 				}
192 				token = strtok(NULL, ",");
193 			}
194 			break;
195 		case 'p':
196 			cpu_no = atoi(optarg);
197 			break;
198 		case 'n':
199 			no_of_bits = atoi(optarg);
200 			if (no_of_bits <= 0) {
201 				printf("Bail out! invalid argument for no_of_bits\n");
202 				return -1;
203 			}
204 			break;
205 		case 'h':
206 			cmd_help();
207 
208 			return 0;
209 		default:
210 			printf("invalid argument\n");
211 
212 			return -1;
213 		}
214 	}
215 
216 	ksft_print_header();
217 
218 	/*
219 	 * Typically we need root privileges, because:
220 	 * 1. We write to resctrl FS
221 	 * 2. We execute perf commands
222 	 */
223 	if (geteuid() != 0)
224 		return ksft_exit_fail_msg("Not running as root, abort testing.\n");
225 
226 	if (has_ben) {
227 		if (argc - ben_ind >= BENCHMARK_ARGS)
228 			ksft_exit_fail_msg("Too long benchmark command.\n");
229 
230 		/* Extract benchmark command from command line. */
231 		for (i = ben_ind; i < argc; i++) {
232 			benchmark_cmd[i - ben_ind] = benchmark_cmd_area[i];
233 			if (strlen(argv[i]) >= BENCHMARK_ARG_SIZE)
234 				ksft_exit_fail_msg("Too long benchmark command argument.\n");
235 			sprintf(benchmark_cmd[i - ben_ind], "%s", argv[i]);
236 		}
237 		benchmark_cmd[ben_count] = NULL;
238 	} else {
239 		/* If no benchmark is given by "-b" argument, use fill_buf. */
240 		for (i = 0; i < 6; i++)
241 			benchmark_cmd[i] = benchmark_cmd_area[i];
242 
243 		strcpy(benchmark_cmd[0], "fill_buf");
244 		sprintf(benchmark_cmd[1], "%d", span);
245 		strcpy(benchmark_cmd[2], "1");
246 		strcpy(benchmark_cmd[3], "1");
247 		strcpy(benchmark_cmd[4], "0");
248 		strcpy(benchmark_cmd[5], "");
249 		benchmark_cmd[6] = NULL;
250 	}
251 
252 	sprintf(bw_report, "reads");
253 	sprintf(bm_type, "fill_buf");
254 
255 	if (!check_resctrlfs_support())
256 		return ksft_exit_fail_msg("resctrl FS does not exist\n");
257 
258 	filter_dmesg();
259 
260 	ksft_set_plan(tests ? : 4);
261 
262 	if ((get_vendor() == ARCH_INTEL) && mbm_test)
263 		run_mbm_test(has_ben, benchmark_cmd, span, cpu_no, bw_report);
264 
265 	if ((get_vendor() == ARCH_INTEL) && mba_test)
266 		run_mba_test(has_ben, benchmark_cmd, span, cpu_no, bw_report);
267 
268 	if (cmt_test)
269 		run_cmt_test(has_ben, benchmark_cmd, cpu_no);
270 
271 	if (cat_test)
272 		run_cat_test(cpu_no, no_of_bits);
273 
274 	umount_resctrlfs();
275 
276 	return ksft_exit_pass();
277 }
278