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 
16 bool is_amd;
17 
18 void detect_amd(void)
19 {
20 	FILE *inf = fopen("/proc/cpuinfo", "r");
21 	char *res;
22 
23 	if (!inf)
24 		return;
25 
26 	res = fgrep(inf, "vendor_id");
27 
28 	if (res) {
29 		char *s = strchr(res, ':');
30 
31 		is_amd = s && !strcmp(s, ": AuthenticAMD\n");
32 		free(res);
33 	}
34 	fclose(inf);
35 }
36 
37 static void cmd_help(void)
38 {
39 	printf("usage: resctrl_tests [-h] [-b \"benchmark_cmd [options]\"] [-t test list] [-n no_of_bits]\n");
40 	printf("\t-b benchmark_cmd [options]: run specified benchmark for MBM, MBA and CQM");
41 	printf("\t default benchmark is builtin fill_buf\n");
42 	printf("\t-t test list: run tests specified in the test list, ");
43 	printf("e.g. -t mbm, mba, cqm, cat\n");
44 	printf("\t-n no_of_bits: run cache tests using specified no of bits in cache bit mask\n");
45 	printf("\t-p cpu_no: specify CPU number to run the test. 1 is default\n");
46 	printf("\t-h: help\n");
47 }
48 
49 void tests_cleanup(void)
50 {
51 	mbm_test_cleanup();
52 	mba_test_cleanup();
53 	cqm_test_cleanup();
54 	cat_test_cleanup();
55 }
56 
57 int main(int argc, char **argv)
58 {
59 	bool has_ben = false, mbm_test = true, mba_test = true, cqm_test = true;
60 	int res, c, cpu_no = 1, span = 250, argc_new = argc, i, no_of_bits = 5;
61 	char *benchmark_cmd[BENCHMARK_ARGS], bw_report[64], bm_type[64];
62 	char benchmark_cmd_area[BENCHMARK_ARGS][BENCHMARK_ARG_SIZE];
63 	int ben_ind, ben_count;
64 	bool cat_test = true;
65 
66 	for (i = 0; i < argc; i++) {
67 		if (strcmp(argv[i], "-b") == 0) {
68 			ben_ind = i + 1;
69 			ben_count = argc - ben_ind;
70 			argc_new = ben_ind - 1;
71 			has_ben = true;
72 			break;
73 		}
74 	}
75 
76 	while ((c = getopt(argc_new, argv, "ht:b:")) != -1) {
77 		char *token;
78 
79 		switch (c) {
80 		case 't':
81 			token = strtok(optarg, ",");
82 
83 			mbm_test = false;
84 			mba_test = false;
85 			cqm_test = false;
86 			cat_test = false;
87 			while (token) {
88 				if (!strcmp(token, "mbm")) {
89 					mbm_test = true;
90 				} else if (!strcmp(token, "mba")) {
91 					mba_test = true;
92 				} else if (!strcmp(token, "cqm")) {
93 					cqm_test = true;
94 				} else if (!strcmp(token, "cat")) {
95 					cat_test = true;
96 				} else {
97 					printf("invalid argument\n");
98 
99 					return -1;
100 				}
101 				token = strtok(NULL, ":\t");
102 			}
103 			break;
104 		case 'p':
105 			cpu_no = atoi(optarg);
106 			break;
107 		case 'n':
108 			no_of_bits = atoi(optarg);
109 			break;
110 		case 'h':
111 			cmd_help();
112 
113 			return 0;
114 		default:
115 			printf("invalid argument\n");
116 
117 			return -1;
118 		}
119 	}
120 
121 	printf("TAP version 13\n");
122 
123 	/*
124 	 * Typically we need root privileges, because:
125 	 * 1. We write to resctrl FS
126 	 * 2. We execute perf commands
127 	 */
128 	if (geteuid() != 0)
129 		printf("# WARNING: not running as root, tests may fail.\n");
130 
131 	/* Detect AMD vendor */
132 	detect_amd();
133 
134 	if (has_ben) {
135 		/* Extract benchmark command from command line. */
136 		for (i = ben_ind; i < argc; i++) {
137 			benchmark_cmd[i - ben_ind] = benchmark_cmd_area[i];
138 			sprintf(benchmark_cmd[i - ben_ind], "%s", argv[i]);
139 		}
140 		benchmark_cmd[ben_count] = NULL;
141 	} else {
142 		/* If no benchmark is given by "-b" argument, use fill_buf. */
143 		for (i = 0; i < 6; i++)
144 			benchmark_cmd[i] = benchmark_cmd_area[i];
145 
146 		strcpy(benchmark_cmd[0], "fill_buf");
147 		sprintf(benchmark_cmd[1], "%d", span);
148 		strcpy(benchmark_cmd[2], "1");
149 		strcpy(benchmark_cmd[3], "1");
150 		strcpy(benchmark_cmd[4], "0");
151 		strcpy(benchmark_cmd[5], "");
152 		benchmark_cmd[6] = NULL;
153 	}
154 
155 	sprintf(bw_report, "reads");
156 	sprintf(bm_type, "fill_buf");
157 
158 	check_resctrlfs_support();
159 	filter_dmesg();
160 
161 	if (!is_amd && mbm_test) {
162 		printf("# Starting MBM BW change ...\n");
163 		if (!has_ben)
164 			sprintf(benchmark_cmd[5], "%s", "mba");
165 		res = mbm_bw_change(span, cpu_no, bw_report, benchmark_cmd);
166 		printf("%sok MBM: bw change\n", res ? "not " : "");
167 		mbm_test_cleanup();
168 		tests_run++;
169 	}
170 
171 	if (!is_amd && mba_test) {
172 		printf("# Starting MBA Schemata change ...\n");
173 		if (!has_ben)
174 			sprintf(benchmark_cmd[1], "%d", span);
175 		res = mba_schemata_change(cpu_no, bw_report, benchmark_cmd);
176 		printf("%sok MBA: schemata change\n", res ? "not " : "");
177 		mba_test_cleanup();
178 		tests_run++;
179 	}
180 
181 	if (cqm_test) {
182 		printf("# Starting CQM test ...\n");
183 		if (!has_ben)
184 			sprintf(benchmark_cmd[5], "%s", "cqm");
185 		res = cqm_resctrl_val(cpu_no, no_of_bits, benchmark_cmd);
186 		printf("%sok CQM: test\n", res ? "not " : "");
187 		cqm_test_cleanup();
188 		tests_run++;
189 	}
190 
191 	if (cat_test) {
192 		printf("# Starting CAT test ...\n");
193 		res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
194 		printf("%sok CAT: test\n", res ? "not " : "");
195 		tests_run++;
196 		cat_test_cleanup();
197 	}
198 
199 	printf("1..%d\n", tests_run);
200 
201 	return 0;
202 }
203