1790bf585SFenghua Yu // SPDX-License-Identifier: GPL-2.0
2790bf585SFenghua Yu /*
3790bf585SFenghua Yu  * Cache Allocation Technology (CAT) test
4790bf585SFenghua Yu  *
5790bf585SFenghua Yu  * Copyright (C) 2018 Intel Corporation
6790bf585SFenghua Yu  *
7790bf585SFenghua Yu  * Authors:
8790bf585SFenghua Yu  *    Sai Praneeth Prakhya <[email protected]>,
9790bf585SFenghua Yu  *    Fenghua Yu <[email protected]>
10790bf585SFenghua Yu  */
11790bf585SFenghua Yu #include "resctrl.h"
12790bf585SFenghua Yu #include <unistd.h>
13790bf585SFenghua Yu 
14205de6ddSIlpo Järvinen #define RESULT_FILE_NAME	"result_cat"
15790bf585SFenghua Yu #define NUM_OF_RUNS		5
16790bf585SFenghua Yu 
17790bf585SFenghua Yu /*
18205de6ddSIlpo Järvinen  * Minimum difference in LLC misses between a test with n+1 bits CBM to the
19205de6ddSIlpo Järvinen  * test with n bits is MIN_DIFF_PERCENT_PER_BIT * (n - 1). With e.g. 5 vs 4
20205de6ddSIlpo Järvinen  * bits in the CBM mask, the minimum difference must be at least
21205de6ddSIlpo Järvinen  * MIN_DIFF_PERCENT_PER_BIT * (4 - 1) = 3 percent.
22205de6ddSIlpo Järvinen  *
23205de6ddSIlpo Järvinen  * The relationship between number of used CBM bits and difference in LLC
24205de6ddSIlpo Järvinen  * misses is not expected to be linear. With a small number of bits, the
25205de6ddSIlpo Järvinen  * margin is smaller than with larger number of bits. For selftest purposes,
26205de6ddSIlpo Järvinen  * however, linear approach is enough because ultimately only pass/fail
27205de6ddSIlpo Järvinen  * decision has to be made and distinction between strong and stronger
28205de6ddSIlpo Järvinen  * signal is irrelevant.
29790bf585SFenghua Yu  */
30205de6ddSIlpo Järvinen #define MIN_DIFF_PERCENT_PER_BIT	1UL
31790bf585SFenghua Yu 
3233403bc7SIlpo Järvinen static int show_results_info(__u64 sum_llc_val, int no_of_bits,
33205de6ddSIlpo Järvinen 			     unsigned long cache_span,
34205de6ddSIlpo Järvinen 			     unsigned long min_diff_percent,
35205de6ddSIlpo Järvinen 			     unsigned long num_of_runs, bool platform,
36205de6ddSIlpo Järvinen 			     __s64 *prev_avg_llc_val)
375caf1b64SIlpo Järvinen {
3833403bc7SIlpo Järvinen 	__u64 avg_llc_val = 0;
39205de6ddSIlpo Järvinen 	float avg_diff;
40205de6ddSIlpo Järvinen 	int ret = 0;
415caf1b64SIlpo Järvinen 
425caf1b64SIlpo Järvinen 	avg_llc_val = sum_llc_val / num_of_runs;
43205de6ddSIlpo Järvinen 	if (*prev_avg_llc_val) {
44205de6ddSIlpo Järvinen 		float delta = (__s64)(avg_llc_val - *prev_avg_llc_val);
455caf1b64SIlpo Järvinen 
46205de6ddSIlpo Järvinen 		avg_diff = delta / *prev_avg_llc_val;
47205de6ddSIlpo Järvinen 		ret = platform && (avg_diff * 100) < (float)min_diff_percent;
485caf1b64SIlpo Järvinen 
49205de6ddSIlpo Järvinen 		ksft_print_msg("%s Check cache miss rate changed more than %.1f%%\n",
50205de6ddSIlpo Järvinen 			       ret ? "Fail:" : "Pass:", (float)min_diff_percent);
515caf1b64SIlpo Järvinen 
52205de6ddSIlpo Järvinen 		ksft_print_msg("Percent diff=%.1f\n", avg_diff * 100);
53205de6ddSIlpo Järvinen 	}
54205de6ddSIlpo Järvinen 	*prev_avg_llc_val = avg_llc_val;
555caf1b64SIlpo Järvinen 
565caf1b64SIlpo Järvinen 	show_cache_info(no_of_bits, avg_llc_val, cache_span, true);
575caf1b64SIlpo Järvinen 
585caf1b64SIlpo Järvinen 	return ret;
595caf1b64SIlpo Järvinen }
605caf1b64SIlpo Järvinen 
61205de6ddSIlpo Järvinen /* Remove the highest bit from CBM */
62205de6ddSIlpo Järvinen static unsigned long next_mask(unsigned long current_mask)
63205de6ddSIlpo Järvinen {
64205de6ddSIlpo Järvinen 	return current_mask & (current_mask >> 1);
65205de6ddSIlpo Järvinen }
66205de6ddSIlpo Järvinen 
67205de6ddSIlpo Järvinen static int check_results(struct resctrl_val_param *param, const char *cache_type,
68205de6ddSIlpo Järvinen 			 unsigned long cache_total_size, unsigned long full_cache_mask,
69205de6ddSIlpo Järvinen 			 unsigned long current_mask)
70790bf585SFenghua Yu {
71790bf585SFenghua Yu 	char *token_array[8], temp[512];
7233403bc7SIlpo Järvinen 	__u64 sum_llc_perf_miss = 0;
73205de6ddSIlpo Järvinen 	__s64 prev_avg_llc_val = 0;
74205de6ddSIlpo Järvinen 	unsigned long alloc_size;
75205de6ddSIlpo Järvinen 	int runs = 0;
76205de6ddSIlpo Järvinen 	int fail = 0;
77205de6ddSIlpo Järvinen 	int ret;
78790bf585SFenghua Yu 	FILE *fp;
79790bf585SFenghua Yu 
80ca2f4214SFenghua Yu 	ksft_print_msg("Checking for pass/fail\n");
81790bf585SFenghua Yu 	fp = fopen(param->filename, "r");
82790bf585SFenghua Yu 	if (!fp) {
83cc8ff7f5SIlpo Järvinen 		ksft_perror("Cannot open file");
84790bf585SFenghua Yu 
85c90fba60SIlpo Järvinen 		return -1;
86790bf585SFenghua Yu 	}
87790bf585SFenghua Yu 
88790bf585SFenghua Yu 	while (fgets(temp, sizeof(temp), fp)) {
89790bf585SFenghua Yu 		char *token = strtok(temp, ":\t");
90790bf585SFenghua Yu 		int fields = 0;
91205de6ddSIlpo Järvinen 		int bits;
92790bf585SFenghua Yu 
93790bf585SFenghua Yu 		while (token) {
94790bf585SFenghua Yu 			token_array[fields++] = token;
95790bf585SFenghua Yu 			token = strtok(NULL, ":\t");
96790bf585SFenghua Yu 		}
97205de6ddSIlpo Järvinen 
9833403bc7SIlpo Järvinen 		sum_llc_perf_miss += strtoull(token_array[3], NULL, 0);
99790bf585SFenghua Yu 		runs++;
100205de6ddSIlpo Järvinen 
101205de6ddSIlpo Järvinen 		if (runs < NUM_OF_RUNS)
102205de6ddSIlpo Järvinen 			continue;
103205de6ddSIlpo Järvinen 
104205de6ddSIlpo Järvinen 		if (!current_mask) {
105205de6ddSIlpo Järvinen 			ksft_print_msg("Unexpected empty cache mask\n");
106205de6ddSIlpo Järvinen 			break;
107205de6ddSIlpo Järvinen 		}
108205de6ddSIlpo Järvinen 
109205de6ddSIlpo Järvinen 		alloc_size = cache_portion_size(cache_total_size, current_mask, full_cache_mask);
110205de6ddSIlpo Järvinen 
111205de6ddSIlpo Järvinen 		bits = count_bits(current_mask);
112205de6ddSIlpo Järvinen 
113205de6ddSIlpo Järvinen 		ret = show_results_info(sum_llc_perf_miss, bits,
114205de6ddSIlpo Järvinen 					alloc_size / 64,
115205de6ddSIlpo Järvinen 					MIN_DIFF_PERCENT_PER_BIT * (bits - 1),
116205de6ddSIlpo Järvinen 					runs, get_vendor() == ARCH_INTEL,
117205de6ddSIlpo Järvinen 					&prev_avg_llc_val);
118205de6ddSIlpo Järvinen 		if (ret)
119205de6ddSIlpo Järvinen 			fail = 1;
120205de6ddSIlpo Järvinen 
121205de6ddSIlpo Järvinen 		runs = 0;
122205de6ddSIlpo Järvinen 		sum_llc_perf_miss = 0;
123205de6ddSIlpo Järvinen 		current_mask = next_mask(current_mask);
124790bf585SFenghua Yu 	}
125790bf585SFenghua Yu 
126790bf585SFenghua Yu 	fclose(fp);
127790bf585SFenghua Yu 
128205de6ddSIlpo Järvinen 	return fail;
129790bf585SFenghua Yu }
130790bf585SFenghua Yu 
131790bf585SFenghua Yu void cat_test_cleanup(void)
132790bf585SFenghua Yu {
133205de6ddSIlpo Järvinen 	remove(RESULT_FILE_NAME);
134790bf585SFenghua Yu }
135790bf585SFenghua Yu 
136433f437bSIlpo Järvinen /*
137433f437bSIlpo Järvinen  * cat_test - Execute CAT benchmark and measure cache misses
138433f437bSIlpo Järvinen  * @param:		Parameters passed to cat_test()
139433f437bSIlpo Järvinen  * @span:		Buffer size for the benchmark
140205de6ddSIlpo Järvinen  * @current_mask	Start mask for the first iteration
141205de6ddSIlpo Järvinen  *
142205de6ddSIlpo Järvinen  * Run CAT selftest by varying the allocated cache portion and comparing the
143205de6ddSIlpo Järvinen  * impact on cache misses (the result analysis is done in check_results()
144205de6ddSIlpo Järvinen  * and show_results_info(), not in this function).
145205de6ddSIlpo Järvinen  *
146205de6ddSIlpo Järvinen  * One bit is removed from the CAT allocation bit mask (in current_mask) for
147205de6ddSIlpo Järvinen  * each subsequent test which keeps reducing the size of the allocated cache
148205de6ddSIlpo Järvinen  * portion. A single test flushes the buffer, reads it to warm up the cache,
149205de6ddSIlpo Järvinen  * and reads the buffer again. The cache misses are measured during the last
150205de6ddSIlpo Järvinen  * read pass.
151433f437bSIlpo Järvinen  *
152433f437bSIlpo Järvinen  * Return:		0 when the test was run, < 0 on error.
153433f437bSIlpo Järvinen  */
154205de6ddSIlpo Järvinen static int cat_test(struct resctrl_val_param *param, size_t span, unsigned long current_mask)
155433f437bSIlpo Järvinen {
156433f437bSIlpo Järvinen 	char *resctrl_val = param->resctrl_val;
157433f437bSIlpo Järvinen 	struct perf_event_read pe_read;
158433f437bSIlpo Järvinen 	struct perf_event_attr pea;
159*6c8cb747SIlpo Järvinen 	cpu_set_t old_affinity;
160205de6ddSIlpo Järvinen 	unsigned char *buf;
161205de6ddSIlpo Järvinen 	char schemata[64];
162205de6ddSIlpo Järvinen 	int ret, i, pe_fd;
163433f437bSIlpo Järvinen 	pid_t bm_pid;
164433f437bSIlpo Järvinen 
165433f437bSIlpo Järvinen 	if (strcmp(param->filename, "") == 0)
166433f437bSIlpo Järvinen 		sprintf(param->filename, "stdio");
167433f437bSIlpo Järvinen 
168433f437bSIlpo Järvinen 	bm_pid = getpid();
169433f437bSIlpo Järvinen 
170433f437bSIlpo Järvinen 	/* Taskset benchmark to specified cpu */
171*6c8cb747SIlpo Järvinen 	ret = taskset_benchmark(bm_pid, param->cpu_no, &old_affinity);
172433f437bSIlpo Järvinen 	if (ret)
173433f437bSIlpo Järvinen 		return ret;
174433f437bSIlpo Järvinen 
175433f437bSIlpo Järvinen 	/* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/
176433f437bSIlpo Järvinen 	ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
177433f437bSIlpo Järvinen 				      resctrl_val);
178433f437bSIlpo Järvinen 	if (ret)
179*6c8cb747SIlpo Järvinen 		goto reset_affinity;
180433f437bSIlpo Järvinen 
181433f437bSIlpo Järvinen 	perf_event_attr_initialize(&pea, PERF_COUNT_HW_CACHE_MISSES);
182433f437bSIlpo Järvinen 	perf_event_initialize_read_format(&pe_read);
1832892731eSIlpo Järvinen 	pe_fd = perf_open(&pea, bm_pid, param->cpu_no);
184*6c8cb747SIlpo Järvinen 	if (pe_fd < 0) {
185*6c8cb747SIlpo Järvinen 		ret = -1;
186*6c8cb747SIlpo Järvinen 		goto reset_affinity;
187*6c8cb747SIlpo Järvinen 	}
188433f437bSIlpo Järvinen 
189205de6ddSIlpo Järvinen 	buf = alloc_buffer(span, 1);
190205de6ddSIlpo Järvinen 	if (!buf) {
191433f437bSIlpo Järvinen 		ret = -1;
192433f437bSIlpo Järvinen 		goto pe_close;
193433f437bSIlpo Järvinen 	}
194433f437bSIlpo Järvinen 
195205de6ddSIlpo Järvinen 	while (current_mask) {
196205de6ddSIlpo Järvinen 		snprintf(schemata, sizeof(schemata), "%lx", param->mask & ~current_mask);
197205de6ddSIlpo Järvinen 		ret = write_schemata("", schemata, param->cpu_no, param->resctrl_val);
198205de6ddSIlpo Järvinen 		if (ret)
199205de6ddSIlpo Järvinen 			goto free_buf;
200205de6ddSIlpo Järvinen 		snprintf(schemata, sizeof(schemata), "%lx", current_mask);
201205de6ddSIlpo Järvinen 		ret = write_schemata(param->ctrlgrp, schemata, param->cpu_no, param->resctrl_val);
202205de6ddSIlpo Järvinen 		if (ret)
203205de6ddSIlpo Järvinen 			goto free_buf;
204205de6ddSIlpo Järvinen 
205205de6ddSIlpo Järvinen 		for (i = 0; i < NUM_OF_RUNS; i++) {
206205de6ddSIlpo Järvinen 			mem_flush(buf, span);
207205de6ddSIlpo Järvinen 			fill_cache_read(buf, span, true);
208205de6ddSIlpo Järvinen 
209205de6ddSIlpo Järvinen 			ret = perf_event_reset_enable(pe_fd);
210205de6ddSIlpo Järvinen 			if (ret)
211205de6ddSIlpo Järvinen 				goto free_buf;
212205de6ddSIlpo Järvinen 
213205de6ddSIlpo Järvinen 			fill_cache_read(buf, span, true);
214205de6ddSIlpo Järvinen 
215433f437bSIlpo Järvinen 			ret = perf_event_measure(pe_fd, &pe_read, param->filename, bm_pid);
216433f437bSIlpo Järvinen 			if (ret)
217205de6ddSIlpo Järvinen 				goto free_buf;
218205de6ddSIlpo Järvinen 		}
219205de6ddSIlpo Järvinen 		current_mask = next_mask(current_mask);
220433f437bSIlpo Järvinen 	}
221433f437bSIlpo Järvinen 
222205de6ddSIlpo Järvinen free_buf:
223205de6ddSIlpo Järvinen 	free(buf);
224433f437bSIlpo Järvinen pe_close:
225433f437bSIlpo Järvinen 	close(pe_fd);
226*6c8cb747SIlpo Järvinen reset_affinity:
227*6c8cb747SIlpo Järvinen 	taskset_restore(bm_pid, &old_affinity);
228205de6ddSIlpo Järvinen 
229433f437bSIlpo Järvinen 	return ret;
230433f437bSIlpo Järvinen }
231433f437bSIlpo Järvinen 
232790bf585SFenghua Yu int cat_perf_miss_val(int cpu_no, int n, char *cache_type)
233790bf585SFenghua Yu {
234205de6ddSIlpo Järvinen 	unsigned long long_mask, start_mask, full_cache_mask;
23519e94a23SIlpo Järvinen 	unsigned long cache_total_size = 0;
236205de6ddSIlpo Järvinen 	unsigned int start;
23785b73447SIlpo Järvinen 	int count_of_bits;
238b1a901e0SIlpo Järvinen 	size_t span;
239205de6ddSIlpo Järvinen 	int ret;
240790bf585SFenghua Yu 
241b6dfac94SIlpo Järvinen 	ret = get_full_cbm(cache_type, &full_cache_mask);
242b6dfac94SIlpo Järvinen 	if (ret)
243b6dfac94SIlpo Järvinen 		return ret;
244b6dfac94SIlpo Järvinen 	/* Get the largest contiguous exclusive portion of the cache */
245b6dfac94SIlpo Järvinen 	ret = get_mask_no_shareable(cache_type, &long_mask);
246790bf585SFenghua Yu 	if (ret)
247790bf585SFenghua Yu 		return ret;
248790bf585SFenghua Yu 
249790bf585SFenghua Yu 	/* Get L3/L2 cache size */
25019e94a23SIlpo Järvinen 	ret = get_cache_size(cpu_no, cache_type, &cache_total_size);
251790bf585SFenghua Yu 	if (ret)
252790bf585SFenghua Yu 		return ret;
25319e94a23SIlpo Järvinen 	ksft_print_msg("Cache size :%lu\n", cache_total_size);
254790bf585SFenghua Yu 
255205de6ddSIlpo Järvinen 	count_of_bits = count_contiguous_bits(long_mask, &start);
256790bf585SFenghua Yu 
25709a67934SFenghua Yu 	if (!n)
25809a67934SFenghua Yu 		n = count_of_bits / 2;
25909a67934SFenghua Yu 
26009a67934SFenghua Yu 	if (n > count_of_bits - 1) {
261ca2f4214SFenghua Yu 		ksft_print_msg("Invalid input value for no_of_bits n!\n");
262ca2f4214SFenghua Yu 		ksft_print_msg("Please enter value in range 1 to %d\n",
263790bf585SFenghua Yu 			       count_of_bits - 1);
264790bf585SFenghua Yu 		return -1;
265790bf585SFenghua Yu 	}
266205de6ddSIlpo Järvinen 	start_mask = create_bit_mask(start, n);
267790bf585SFenghua Yu 
268790bf585SFenghua Yu 	struct resctrl_val_param param = {
26924286736SFenghua Yu 		.resctrl_val	= CAT_STR,
270790bf585SFenghua Yu 		.cpu_no		= cpu_no,
271205de6ddSIlpo Järvinen 		.ctrlgrp	= "c1",
272205de6ddSIlpo Järvinen 		.filename	= RESULT_FILE_NAME,
273205de6ddSIlpo Järvinen 		.num_of_runs	= 0,
274790bf585SFenghua Yu 	};
275205de6ddSIlpo Järvinen 	param.mask = long_mask;
276205de6ddSIlpo Järvinen 	span = cache_portion_size(cache_total_size, start_mask, full_cache_mask);
277790bf585SFenghua Yu 
278790bf585SFenghua Yu 	remove(param.filename);
279790bf585SFenghua Yu 
280205de6ddSIlpo Järvinen 	ret = cat_test(&param, span, start_mask);
281205de6ddSIlpo Järvinen 	if (ret)
282205de6ddSIlpo Järvinen 		goto out;
283790bf585SFenghua Yu 
284205de6ddSIlpo Järvinen 	ret = check_results(&param, cache_type, cache_total_size, full_cache_mask, start_mask);
285205de6ddSIlpo Järvinen out:
286790bf585SFenghua Yu 	cat_test_cleanup();
287790bf585SFenghua Yu 
28839e34ddcSShaopeng Tan 	return ret;
289790bf585SFenghua Yu }
290