1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <stdint.h>
4 #include "resctrl.h"
5 
6 struct read_format {
7 	__u64 nr;			/* The number of events */
8 	struct {
9 		__u64 value;		/* The value of the event */
10 	} values[2];
11 };
12 
13 static struct perf_event_attr pea_llc_miss;
14 static struct read_format rf_cqm;
15 static int fd_lm;
16 char llc_occup_path[1024];
17 
18 static void initialize_perf_event_attr(void)
19 {
20 	pea_llc_miss.type = PERF_TYPE_HARDWARE;
21 	pea_llc_miss.size = sizeof(struct perf_event_attr);
22 	pea_llc_miss.read_format = PERF_FORMAT_GROUP;
23 	pea_llc_miss.exclude_kernel = 1;
24 	pea_llc_miss.exclude_hv = 1;
25 	pea_llc_miss.exclude_idle = 1;
26 	pea_llc_miss.exclude_callchain_kernel = 1;
27 	pea_llc_miss.inherit = 1;
28 	pea_llc_miss.exclude_guest = 1;
29 	pea_llc_miss.disabled = 1;
30 }
31 
32 static void ioctl_perf_event_ioc_reset_enable(void)
33 {
34 	ioctl(fd_lm, PERF_EVENT_IOC_RESET, 0);
35 	ioctl(fd_lm, PERF_EVENT_IOC_ENABLE, 0);
36 }
37 
38 static int perf_event_open_llc_miss(pid_t pid, int cpu_no)
39 {
40 	fd_lm = perf_event_open(&pea_llc_miss, pid, cpu_no, -1,
41 				PERF_FLAG_FD_CLOEXEC);
42 	if (fd_lm == -1) {
43 		ksft_perror("Error opening leader");
44 		return -1;
45 	}
46 
47 	return 0;
48 }
49 
50 static void initialize_llc_perf(void)
51 {
52 	memset(&pea_llc_miss, 0, sizeof(struct perf_event_attr));
53 	memset(&rf_cqm, 0, sizeof(struct read_format));
54 
55 	/* Initialize perf_event_attr structures for HW_CACHE_MISSES */
56 	initialize_perf_event_attr();
57 
58 	pea_llc_miss.config = PERF_COUNT_HW_CACHE_MISSES;
59 
60 	rf_cqm.nr = 1;
61 }
62 
63 static int reset_enable_llc_perf(pid_t pid, int cpu_no)
64 {
65 	int ret = 0;
66 
67 	ret = perf_event_open_llc_miss(pid, cpu_no);
68 	if (ret < 0)
69 		return ret;
70 
71 	/* Start counters to log values */
72 	ioctl_perf_event_ioc_reset_enable();
73 
74 	return 0;
75 }
76 
77 /*
78  * get_llc_perf:	llc cache miss through perf events
79  * @llc_perf_miss:	LLC miss counter that is filled on success
80  *
81  * Perf events like HW_CACHE_MISSES could be used to validate number of
82  * cache lines allocated.
83  *
84  * Return: =0 on success.  <0 on failure.
85  */
86 static int get_llc_perf(unsigned long *llc_perf_miss)
87 {
88 	__u64 total_misses;
89 	int ret;
90 
91 	/* Stop counters after one span to get miss rate */
92 
93 	ioctl(fd_lm, PERF_EVENT_IOC_DISABLE, 0);
94 
95 	ret = read(fd_lm, &rf_cqm, sizeof(struct read_format));
96 	if (ret == -1) {
97 		ksft_perror("Could not get llc misses through perf");
98 		return -1;
99 	}
100 
101 	total_misses = rf_cqm.values[0].value;
102 	*llc_perf_miss = total_misses;
103 
104 	return 0;
105 }
106 
107 /*
108  * Get LLC Occupancy as reported by RESCTRL FS
109  * For CMT,
110  * 1. If con_mon grp and mon grp given, then read from mon grp in
111  * con_mon grp
112  * 2. If only con_mon grp given, then read from con_mon grp
113  * 3. If both not given, then read from root con_mon grp
114  * For CAT,
115  * 1. If con_mon grp given, then read from it
116  * 2. If con_mon grp not given, then read from root con_mon grp
117  *
118  * Return: =0 on success.  <0 on failure.
119  */
120 static int get_llc_occu_resctrl(unsigned long *llc_occupancy)
121 {
122 	FILE *fp;
123 
124 	fp = fopen(llc_occup_path, "r");
125 	if (!fp) {
126 		ksft_perror("Failed to open results file");
127 
128 		return -1;
129 	}
130 	if (fscanf(fp, "%lu", llc_occupancy) <= 0) {
131 		ksft_perror("Could not get llc occupancy");
132 		fclose(fp);
133 
134 		return -1;
135 	}
136 	fclose(fp);
137 
138 	return 0;
139 }
140 
141 /*
142  * print_results_cache:	the cache results are stored in a file
143  * @filename:		file that stores the results
144  * @bm_pid:		child pid that runs benchmark
145  * @llc_value:		perf miss value /
146  *			llc occupancy value reported by resctrl FS
147  *
148  * Return:		0 on success, < 0 on error.
149  */
150 static int print_results_cache(const char *filename, int bm_pid,
151 			       unsigned long llc_value)
152 {
153 	FILE *fp;
154 
155 	if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
156 		printf("Pid: %d \t LLC_value: %lu\n", bm_pid,
157 		       llc_value);
158 	} else {
159 		fp = fopen(filename, "a");
160 		if (!fp) {
161 			ksft_perror("Cannot open results file");
162 
163 			return -1;
164 		}
165 		fprintf(fp, "Pid: %d \t llc_value: %lu\n", bm_pid, llc_value);
166 		fclose(fp);
167 	}
168 
169 	return 0;
170 }
171 
172 /*
173  * perf_event_measure - Measure perf events
174  * @filename:	Filename for writing the results
175  * @bm_pid:	PID that runs the benchmark
176  *
177  * Measures perf events (e.g., cache misses) and writes the results into
178  * @filename. @bm_pid is written to the results file along with the measured
179  * value.
180  *
181  * Return: =0 on success. <0 on failure.
182  */
183 static int perf_event_measure(const char *filename, int bm_pid)
184 {
185 	unsigned long llc_perf_miss = 0;
186 	int ret;
187 
188 	ret = get_llc_perf(&llc_perf_miss);
189 	if (ret < 0)
190 		return ret;
191 
192 	return print_results_cache(filename, bm_pid, llc_perf_miss);
193 }
194 
195 /*
196  * measure_llc_resctrl - Measure resctrl LLC value from resctrl
197  * @filename:	Filename for writing the results
198  * @bm_pid:	PID that runs the benchmark
199  *
200  * Measures LLC occupancy from resctrl and writes the results into @filename.
201  * @bm_pid is written to the results file along with the measured value.
202  *
203  * Return: =0 on success. <0 on failure.
204  */
205 int measure_llc_resctrl(const char *filename, int bm_pid)
206 {
207 	unsigned long llc_occu_resc = 0;
208 	int ret;
209 
210 	ret = get_llc_occu_resctrl(&llc_occu_resc);
211 	if (ret < 0)
212 		return ret;
213 
214 	return print_results_cache(filename, bm_pid, llc_occu_resc);
215 }
216 
217 /*
218  * cache_val:		execute benchmark and measure LLC occupancy resctrl
219  * and perf cache miss for the benchmark
220  * @param:		parameters passed to cache_val()
221  * @span:		buffer size for the benchmark
222  *
223  * Return:		0 when the test was run, < 0 on error.
224  */
225 int cat_val(struct resctrl_val_param *param, size_t span)
226 {
227 	int memflush = 1, operation = 0, ret = 0;
228 	char *resctrl_val = param->resctrl_val;
229 	pid_t bm_pid;
230 
231 	if (strcmp(param->filename, "") == 0)
232 		sprintf(param->filename, "stdio");
233 
234 	bm_pid = getpid();
235 
236 	/* Taskset benchmark to specified cpu */
237 	ret = taskset_benchmark(bm_pid, param->cpu_no);
238 	if (ret)
239 		return ret;
240 
241 	/* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/
242 	ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
243 				      resctrl_val);
244 	if (ret)
245 		return ret;
246 
247 	initialize_llc_perf();
248 
249 	/* Test runs until the callback setup() tells the test to stop. */
250 	while (1) {
251 		ret = param->setup(param);
252 		if (ret == END_OF_TESTS) {
253 			ret = 0;
254 			break;
255 		}
256 		if (ret < 0)
257 			break;
258 		ret = reset_enable_llc_perf(bm_pid, param->cpu_no);
259 		if (ret)
260 			break;
261 
262 		if (run_fill_buf(span, memflush, operation, true)) {
263 			fprintf(stderr, "Error-running fill buffer\n");
264 			ret = -1;
265 			goto pe_close;
266 		}
267 
268 		sleep(1);
269 		ret = perf_event_measure(param->filename, bm_pid);
270 		if (ret)
271 			goto pe_close;
272 	}
273 
274 	return ret;
275 
276 pe_close:
277 	close(fd_lm);
278 	return ret;
279 }
280 
281 /*
282  * show_cache_info:	show cache test result information
283  * @sum_llc_val:	sum of LLC cache result data
284  * @no_of_bits:		number of bits
285  * @cache_span:		cache span in bytes for CMT or in lines for CAT
286  * @max_diff:		max difference
287  * @max_diff_percent:	max difference percentage
288  * @num_of_runs:	number of runs
289  * @platform:		show test information on this platform
290  * @cmt:		CMT test or CAT test
291  *
292  * Return:		0 on success. non-zero on failure.
293  */
294 int show_cache_info(unsigned long sum_llc_val, int no_of_bits,
295 		    size_t cache_span, unsigned long max_diff,
296 		    unsigned long max_diff_percent, unsigned long num_of_runs,
297 		    bool platform, bool cmt)
298 {
299 	unsigned long avg_llc_val = 0;
300 	float diff_percent;
301 	long avg_diff = 0;
302 	int ret;
303 
304 	avg_llc_val = sum_llc_val / num_of_runs;
305 	avg_diff = (long)abs(cache_span - avg_llc_val);
306 	diff_percent = ((float)cache_span - avg_llc_val) / cache_span * 100;
307 
308 	ret = platform && abs((int)diff_percent) > max_diff_percent &&
309 	      (cmt ? (abs(avg_diff) > max_diff) : true);
310 
311 	ksft_print_msg("%s Check cache miss rate within %lu%%\n",
312 		       ret ? "Fail:" : "Pass:", max_diff_percent);
313 
314 	ksft_print_msg("Percent diff=%d\n", abs((int)diff_percent));
315 	ksft_print_msg("Number of bits: %d\n", no_of_bits);
316 	ksft_print_msg("Average LLC val: %lu\n", avg_llc_val);
317 	ksft_print_msg("Cache span (%s): %zu\n", cmt ? "bytes" : "lines",
318 		       cache_span);
319 
320 	return ret;
321 }
322