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
13815f29882SIlpo Järvinen  * @uparams:		User supplied parameters
139433f437bSIlpo Järvinen  * @param:		Parameters passed to cat_test()
140433f437bSIlpo Järvinen  * @span:		Buffer size for the benchmark
141205de6ddSIlpo Järvinen  * @current_mask	Start mask for the first iteration
142205de6ddSIlpo Järvinen  *
143205de6ddSIlpo Järvinen  * Run CAT selftest by varying the allocated cache portion and comparing the
144205de6ddSIlpo Järvinen  * impact on cache misses (the result analysis is done in check_results()
145205de6ddSIlpo Järvinen  * and show_results_info(), not in this function).
146205de6ddSIlpo Järvinen  *
147205de6ddSIlpo Järvinen  * One bit is removed from the CAT allocation bit mask (in current_mask) for
148205de6ddSIlpo Järvinen  * each subsequent test which keeps reducing the size of the allocated cache
149205de6ddSIlpo Järvinen  * portion. A single test flushes the buffer, reads it to warm up the cache,
150205de6ddSIlpo Järvinen  * and reads the buffer again. The cache misses are measured during the last
151205de6ddSIlpo Järvinen  * read pass.
152433f437bSIlpo Järvinen  *
153433f437bSIlpo Järvinen  * Return:		0 when the test was run, < 0 on error.
154433f437bSIlpo Järvinen  */
15515f29882SIlpo Järvinen static int cat_test(const struct user_params *uparams, struct resctrl_val_param *param,
15615f29882SIlpo Järvinen 		    size_t span, unsigned long current_mask)
157433f437bSIlpo Järvinen {
158433f437bSIlpo Järvinen 	char *resctrl_val = param->resctrl_val;
159433f437bSIlpo Järvinen 	struct perf_event_read pe_read;
160433f437bSIlpo Järvinen 	struct perf_event_attr pea;
1616c8cb747SIlpo Järvinen 	cpu_set_t old_affinity;
162205de6ddSIlpo Järvinen 	unsigned char *buf;
163205de6ddSIlpo Järvinen 	char schemata[64];
164205de6ddSIlpo Järvinen 	int ret, i, pe_fd;
165433f437bSIlpo Järvinen 	pid_t bm_pid;
166433f437bSIlpo Järvinen 
167433f437bSIlpo Järvinen 	if (strcmp(param->filename, "") == 0)
168433f437bSIlpo Järvinen 		sprintf(param->filename, "stdio");
169433f437bSIlpo Järvinen 
170433f437bSIlpo Järvinen 	bm_pid = getpid();
171433f437bSIlpo Järvinen 
172433f437bSIlpo Järvinen 	/* Taskset benchmark to specified cpu */
17315f29882SIlpo Järvinen 	ret = taskset_benchmark(bm_pid, uparams->cpu, &old_affinity);
174433f437bSIlpo Järvinen 	if (ret)
175433f437bSIlpo Järvinen 		return ret;
176433f437bSIlpo Järvinen 
177433f437bSIlpo Järvinen 	/* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/
178433f437bSIlpo Järvinen 	ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
179433f437bSIlpo Järvinen 				      resctrl_val);
180433f437bSIlpo Järvinen 	if (ret)
1816c8cb747SIlpo Järvinen 		goto reset_affinity;
182433f437bSIlpo Järvinen 
183433f437bSIlpo Järvinen 	perf_event_attr_initialize(&pea, PERF_COUNT_HW_CACHE_MISSES);
184433f437bSIlpo Järvinen 	perf_event_initialize_read_format(&pe_read);
18515f29882SIlpo Järvinen 	pe_fd = perf_open(&pea, bm_pid, uparams->cpu);
1866c8cb747SIlpo Järvinen 	if (pe_fd < 0) {
1876c8cb747SIlpo Järvinen 		ret = -1;
1886c8cb747SIlpo Järvinen 		goto reset_affinity;
1896c8cb747SIlpo Järvinen 	}
190433f437bSIlpo Järvinen 
191205de6ddSIlpo Järvinen 	buf = alloc_buffer(span, 1);
192205de6ddSIlpo Järvinen 	if (!buf) {
193433f437bSIlpo Järvinen 		ret = -1;
194433f437bSIlpo Järvinen 		goto pe_close;
195433f437bSIlpo Järvinen 	}
196433f437bSIlpo Järvinen 
197205de6ddSIlpo Järvinen 	while (current_mask) {
198205de6ddSIlpo Järvinen 		snprintf(schemata, sizeof(schemata), "%lx", param->mask & ~current_mask);
19915f29882SIlpo Järvinen 		ret = write_schemata("", schemata, uparams->cpu, param->resctrl_val);
200205de6ddSIlpo Järvinen 		if (ret)
201205de6ddSIlpo Järvinen 			goto free_buf;
202205de6ddSIlpo Järvinen 		snprintf(schemata, sizeof(schemata), "%lx", current_mask);
20315f29882SIlpo Järvinen 		ret = write_schemata(param->ctrlgrp, schemata, uparams->cpu, param->resctrl_val);
204205de6ddSIlpo Järvinen 		if (ret)
205205de6ddSIlpo Järvinen 			goto free_buf;
206205de6ddSIlpo Järvinen 
207205de6ddSIlpo Järvinen 		for (i = 0; i < NUM_OF_RUNS; i++) {
208205de6ddSIlpo Järvinen 			mem_flush(buf, span);
209205de6ddSIlpo Järvinen 			fill_cache_read(buf, span, true);
210205de6ddSIlpo Järvinen 
211205de6ddSIlpo Järvinen 			ret = perf_event_reset_enable(pe_fd);
212205de6ddSIlpo Järvinen 			if (ret)
213205de6ddSIlpo Järvinen 				goto free_buf;
214205de6ddSIlpo Järvinen 
215205de6ddSIlpo Järvinen 			fill_cache_read(buf, span, true);
216205de6ddSIlpo Järvinen 
217433f437bSIlpo Järvinen 			ret = perf_event_measure(pe_fd, &pe_read, param->filename, bm_pid);
218433f437bSIlpo Järvinen 			if (ret)
219205de6ddSIlpo Järvinen 				goto free_buf;
220205de6ddSIlpo Järvinen 		}
221205de6ddSIlpo Järvinen 		current_mask = next_mask(current_mask);
222433f437bSIlpo Järvinen 	}
223433f437bSIlpo Järvinen 
224205de6ddSIlpo Järvinen free_buf:
225205de6ddSIlpo Järvinen 	free(buf);
226433f437bSIlpo Järvinen pe_close:
227433f437bSIlpo Järvinen 	close(pe_fd);
2286c8cb747SIlpo Järvinen reset_affinity:
2296c8cb747SIlpo Järvinen 	taskset_restore(bm_pid, &old_affinity);
230205de6ddSIlpo Järvinen 
231433f437bSIlpo Järvinen 	return ret;
232433f437bSIlpo Järvinen }
233433f437bSIlpo Järvinen 
234*c603ff5bSIlpo Järvinen static int cat_run_test(const struct resctrl_test *test, const struct user_params *uparams)
235790bf585SFenghua Yu {
236205de6ddSIlpo Järvinen 	unsigned long long_mask, start_mask, full_cache_mask;
23719e94a23SIlpo Järvinen 	unsigned long cache_total_size = 0;
23815f29882SIlpo Järvinen 	int n = uparams->bits;
239205de6ddSIlpo Järvinen 	unsigned int start;
24085b73447SIlpo Järvinen 	int count_of_bits;
241b1a901e0SIlpo Järvinen 	size_t span;
242205de6ddSIlpo Järvinen 	int ret;
243790bf585SFenghua Yu 
244*c603ff5bSIlpo Järvinen 	ret = get_full_cbm(test->resource, &full_cache_mask);
245b6dfac94SIlpo Järvinen 	if (ret)
246b6dfac94SIlpo Järvinen 		return ret;
247b6dfac94SIlpo Järvinen 	/* Get the largest contiguous exclusive portion of the cache */
248*c603ff5bSIlpo Järvinen 	ret = get_mask_no_shareable(test->resource, &long_mask);
249790bf585SFenghua Yu 	if (ret)
250790bf585SFenghua Yu 		return ret;
251790bf585SFenghua Yu 
252790bf585SFenghua Yu 	/* Get L3/L2 cache size */
253*c603ff5bSIlpo Järvinen 	ret = get_cache_size(uparams->cpu, test->resource, &cache_total_size);
254790bf585SFenghua Yu 	if (ret)
255790bf585SFenghua Yu 		return ret;
25619e94a23SIlpo Järvinen 	ksft_print_msg("Cache size :%lu\n", cache_total_size);
257790bf585SFenghua Yu 
258205de6ddSIlpo Järvinen 	count_of_bits = count_contiguous_bits(long_mask, &start);
259790bf585SFenghua Yu 
26009a67934SFenghua Yu 	if (!n)
26109a67934SFenghua Yu 		n = count_of_bits / 2;
26209a67934SFenghua Yu 
26309a67934SFenghua Yu 	if (n > count_of_bits - 1) {
264ca2f4214SFenghua Yu 		ksft_print_msg("Invalid input value for no_of_bits n!\n");
265ca2f4214SFenghua Yu 		ksft_print_msg("Please enter value in range 1 to %d\n",
266790bf585SFenghua Yu 			       count_of_bits - 1);
267790bf585SFenghua Yu 		return -1;
268790bf585SFenghua Yu 	}
269205de6ddSIlpo Järvinen 	start_mask = create_bit_mask(start, n);
270790bf585SFenghua Yu 
271790bf585SFenghua Yu 	struct resctrl_val_param param = {
27224286736SFenghua Yu 		.resctrl_val	= CAT_STR,
273205de6ddSIlpo Järvinen 		.ctrlgrp	= "c1",
274205de6ddSIlpo Järvinen 		.filename	= RESULT_FILE_NAME,
275205de6ddSIlpo Järvinen 		.num_of_runs	= 0,
276790bf585SFenghua Yu 	};
277205de6ddSIlpo Järvinen 	param.mask = long_mask;
278205de6ddSIlpo Järvinen 	span = cache_portion_size(cache_total_size, start_mask, full_cache_mask);
279790bf585SFenghua Yu 
280790bf585SFenghua Yu 	remove(param.filename);
281790bf585SFenghua Yu 
28215f29882SIlpo Järvinen 	ret = cat_test(uparams, &param, span, start_mask);
283205de6ddSIlpo Järvinen 	if (ret)
284205de6ddSIlpo Järvinen 		goto out;
285790bf585SFenghua Yu 
286*c603ff5bSIlpo Järvinen 	ret = check_results(&param, test->resource,
287*c603ff5bSIlpo Järvinen 			    cache_total_size, full_cache_mask, start_mask);
288205de6ddSIlpo Järvinen out:
289790bf585SFenghua Yu 	cat_test_cleanup();
290790bf585SFenghua Yu 
29139e34ddcSShaopeng Tan 	return ret;
292790bf585SFenghua Yu }
293*c603ff5bSIlpo Järvinen 
294*c603ff5bSIlpo Järvinen struct resctrl_test l3_cat_test = {
295*c603ff5bSIlpo Järvinen 	.name = "CAT",
296*c603ff5bSIlpo Järvinen 	.resource = "L3",
297*c603ff5bSIlpo Järvinen 	.feature_check = test_resource_feature_check,
298*c603ff5bSIlpo Järvinen 	.run_test = cat_run_test,
299*c603ff5bSIlpo Järvinen };
300