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(__u64 *llc_perf_miss)
87 {
88 	int ret;
89 
90 	/* Stop counters after one span to get miss rate */
91 
92 	ioctl(fd_lm, PERF_EVENT_IOC_DISABLE, 0);
93 
94 	ret = read(fd_lm, &rf_cqm, sizeof(struct read_format));
95 	if (ret == -1) {
96 		ksft_perror("Could not get llc misses through perf");
97 		return -1;
98 	}
99 
100 	*llc_perf_miss = rf_cqm.values[0].value;
101 
102 	return 0;
103 }
104 
105 /*
106  * Get LLC Occupancy as reported by RESCTRL FS
107  * For CMT,
108  * 1. If con_mon grp and mon grp given, then read from mon grp in
109  * con_mon grp
110  * 2. If only con_mon grp given, then read from con_mon grp
111  * 3. If both not given, then read from root con_mon grp
112  * For CAT,
113  * 1. If con_mon grp given, then read from it
114  * 2. If con_mon grp not given, then read from root con_mon grp
115  *
116  * Return: =0 on success.  <0 on failure.
117  */
118 static int get_llc_occu_resctrl(unsigned long *llc_occupancy)
119 {
120 	FILE *fp;
121 
122 	fp = fopen(llc_occup_path, "r");
123 	if (!fp) {
124 		ksft_perror("Failed to open results file");
125 
126 		return -1;
127 	}
128 	if (fscanf(fp, "%lu", llc_occupancy) <= 0) {
129 		ksft_perror("Could not get llc occupancy");
130 		fclose(fp);
131 
132 		return -1;
133 	}
134 	fclose(fp);
135 
136 	return 0;
137 }
138 
139 /*
140  * print_results_cache:	the cache results are stored in a file
141  * @filename:		file that stores the results
142  * @bm_pid:		child pid that runs benchmark
143  * @llc_value:		perf miss value /
144  *			llc occupancy value reported by resctrl FS
145  *
146  * Return:		0 on success, < 0 on error.
147  */
148 static int print_results_cache(const char *filename, int bm_pid, __u64 llc_value)
149 {
150 	FILE *fp;
151 
152 	if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
153 		printf("Pid: %d \t LLC_value: %llu\n", bm_pid, llc_value);
154 	} else {
155 		fp = fopen(filename, "a");
156 		if (!fp) {
157 			ksft_perror("Cannot open results file");
158 
159 			return -1;
160 		}
161 		fprintf(fp, "Pid: %d \t llc_value: %llu\n", bm_pid, llc_value);
162 		fclose(fp);
163 	}
164 
165 	return 0;
166 }
167 
168 /*
169  * perf_event_measure - Measure perf events
170  * @filename:	Filename for writing the results
171  * @bm_pid:	PID that runs the benchmark
172  *
173  * Measures perf events (e.g., cache misses) and writes the results into
174  * @filename. @bm_pid is written to the results file along with the measured
175  * value.
176  *
177  * Return: =0 on success. <0 on failure.
178  */
179 static int perf_event_measure(const char *filename, int bm_pid)
180 {
181 	__u64 llc_perf_miss = 0;
182 	int ret;
183 
184 	ret = get_llc_perf(&llc_perf_miss);
185 	if (ret < 0)
186 		return ret;
187 
188 	return print_results_cache(filename, bm_pid, llc_perf_miss);
189 }
190 
191 /*
192  * measure_llc_resctrl - Measure resctrl LLC value from resctrl
193  * @filename:	Filename for writing the results
194  * @bm_pid:	PID that runs the benchmark
195  *
196  * Measures LLC occupancy from resctrl and writes the results into @filename.
197  * @bm_pid is written to the results file along with the measured value.
198  *
199  * Return: =0 on success. <0 on failure.
200  */
201 int measure_llc_resctrl(const char *filename, int bm_pid)
202 {
203 	unsigned long llc_occu_resc = 0;
204 	int ret;
205 
206 	ret = get_llc_occu_resctrl(&llc_occu_resc);
207 	if (ret < 0)
208 		return ret;
209 
210 	return print_results_cache(filename, bm_pid, llc_occu_resc);
211 }
212 
213 /*
214  * cache_val:		execute benchmark and measure LLC occupancy resctrl
215  * and perf cache miss for the benchmark
216  * @param:		parameters passed to cache_val()
217  * @span:		buffer size for the benchmark
218  *
219  * Return:		0 when the test was run, < 0 on error.
220  */
221 int cat_val(struct resctrl_val_param *param, size_t span)
222 {
223 	int memflush = 1, operation = 0, ret = 0;
224 	char *resctrl_val = param->resctrl_val;
225 	pid_t bm_pid;
226 
227 	if (strcmp(param->filename, "") == 0)
228 		sprintf(param->filename, "stdio");
229 
230 	bm_pid = getpid();
231 
232 	/* Taskset benchmark to specified cpu */
233 	ret = taskset_benchmark(bm_pid, param->cpu_no);
234 	if (ret)
235 		return ret;
236 
237 	/* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/
238 	ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
239 				      resctrl_val);
240 	if (ret)
241 		return ret;
242 
243 	initialize_llc_perf();
244 
245 	/* Test runs until the callback setup() tells the test to stop. */
246 	while (1) {
247 		ret = param->setup(param);
248 		if (ret == END_OF_TESTS) {
249 			ret = 0;
250 			break;
251 		}
252 		if (ret < 0)
253 			break;
254 		ret = reset_enable_llc_perf(bm_pid, param->cpu_no);
255 		if (ret)
256 			break;
257 
258 		if (run_fill_buf(span, memflush, operation, true)) {
259 			fprintf(stderr, "Error-running fill buffer\n");
260 			ret = -1;
261 			goto pe_close;
262 		}
263 
264 		sleep(1);
265 		ret = perf_event_measure(param->filename, bm_pid);
266 		if (ret)
267 			goto pe_close;
268 	}
269 
270 	return ret;
271 
272 pe_close:
273 	close(fd_lm);
274 	return ret;
275 }
276 
277 /*
278  * show_cache_info - Show generic cache test information
279  * @no_of_bits:		Number of bits
280  * @avg_llc_val:	Average of LLC cache result data
281  * @cache_span:		Cache span
282  * @lines:		@cache_span in lines or bytes
283  */
284 void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines)
285 {
286 	ksft_print_msg("Number of bits: %d\n", no_of_bits);
287 	ksft_print_msg("Average LLC val: %llu\n", avg_llc_val);
288 	ksft_print_msg("Cache span (%s): %zu\n", lines ? "lines" : "bytes",
289 		       cache_span);
290 }
291