1 // SPDX-License-Identifier: GPL-2.0
7 __u64 nr; /* The number of events */
9 __u64 value; /* The value of the event */
13 static struct perf_event_attr pea_llc_miss;
14 static struct read_format rf_cqm;
16 char llc_occup_path[1024];
18 static void initialize_perf_event_attr(void)
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;
32 static void ioctl_perf_event_ioc_reset_enable(void)
34 ioctl(fd_lm, PERF_EVENT_IOC_RESET, 0);
35 ioctl(fd_lm, PERF_EVENT_IOC_ENABLE, 0);
38 static int perf_event_open_llc_miss(pid_t pid, int cpu_no)
40 fd_lm = perf_event_open(&pea_llc_miss, pid, cpu_no, -1,
41 PERF_FLAG_FD_CLOEXEC);
43 perror("Error opening leader");
44 ctrlc_handler(0, NULL, NULL);
51 static void initialize_llc_perf(void)
53 memset(&pea_llc_miss, 0, sizeof(struct perf_event_attr));
54 memset(&rf_cqm, 0, sizeof(struct read_format));
56 /* Initialize perf_event_attr structures for HW_CACHE_MISSES */
57 initialize_perf_event_attr();
59 pea_llc_miss.config = PERF_COUNT_HW_CACHE_MISSES;
64 static int reset_enable_llc_perf(pid_t pid, int cpu_no)
68 ret = perf_event_open_llc_miss(pid, cpu_no);
72 /* Start counters to log values */
73 ioctl_perf_event_ioc_reset_enable();
79 * get_llc_perf: llc cache miss through perf events
80 * @llc_perf_miss: LLC miss counter that is filled on success
82 * Perf events like HW_CACHE_MISSES could be used to validate number of
83 * cache lines allocated.
85 * Return: =0 on success. <0 on failure.
87 static int get_llc_perf(unsigned long *llc_perf_miss)
91 /* Stop counters after one span to get miss rate */
93 ioctl(fd_lm, PERF_EVENT_IOC_DISABLE, 0);
95 if (read(fd_lm, &rf_cqm, sizeof(struct read_format)) == -1) {
96 perror("Could not get llc misses through perf");
101 total_misses = rf_cqm.values[0].value;
105 *llc_perf_miss = total_misses;
111 * Get LLC Occupancy as reported by RESCTRL FS
113 * 1. If con_mon grp and mon grp given, then read from mon grp in
115 * 2. If only con_mon grp given, then read from con_mon grp
116 * 3. If both not given, then read from root con_mon grp
118 * 1. If con_mon grp given, then read from it
119 * 2. If con_mon grp not given, then read from root con_mon grp
121 * Return: =0 on success. <0 on failure.
123 static int get_llc_occu_resctrl(unsigned long *llc_occupancy)
127 fp = fopen(llc_occup_path, "r");
129 perror("Failed to open results file");
133 if (fscanf(fp, "%lu", llc_occupancy) <= 0) {
134 perror("Could not get llc occupancy");
145 * print_results_cache: the cache results are stored in a file
146 * @filename: file that stores the results
147 * @bm_pid: child pid that runs benchmark
148 * @llc_value: perf miss value /
149 * llc occupancy value reported by resctrl FS
151 * Return: 0 on success. non-zero on failure.
153 static int print_results_cache(char *filename, int bm_pid,
154 unsigned long llc_value)
158 if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
159 printf("Pid: %d \t LLC_value: %lu\n", bm_pid,
162 fp = fopen(filename, "a");
164 perror("Cannot open results file");
168 fprintf(fp, "Pid: %d \t llc_value: %lu\n", bm_pid, llc_value);
175 int measure_cache_vals(struct resctrl_val_param *param, int bm_pid)
177 unsigned long llc_perf_miss = 0, llc_occu_resc = 0, llc_value = 0;
181 * Measure cache miss from perf.
183 if (!strncmp(param->resctrl_val, CAT_STR, sizeof(CAT_STR))) {
184 ret = get_llc_perf(&llc_perf_miss);
187 llc_value = llc_perf_miss;
191 * Measure llc occupancy from resctrl.
193 if (!strncmp(param->resctrl_val, CMT_STR, sizeof(CMT_STR))) {
194 ret = get_llc_occu_resctrl(&llc_occu_resc);
197 llc_value = llc_occu_resc;
199 ret = print_results_cache(param->filename, bm_pid, llc_value);
207 * cache_val: execute benchmark and measure LLC occupancy resctrl
208 * and perf cache miss for the benchmark
209 * @param: parameters passed to cache_val()
211 * Return: 0 on success. non-zero on failure.
213 int cat_val(struct resctrl_val_param *param)
215 int malloc_and_init_memory = 1, memflush = 1, operation = 0, ret = 0;
216 char *resctrl_val = param->resctrl_val;
219 if (strcmp(param->filename, "") == 0)
220 sprintf(param->filename, "stdio");
224 /* Taskset benchmark to specified cpu */
225 ret = taskset_benchmark(bm_pid, param->cpu_no);
229 /* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/
230 ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
235 if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)))
236 initialize_llc_perf();
238 /* Test runs until the callback setup() tells the test to stop. */
240 if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) {
241 ret = param->setup(1, param);
242 if (ret == END_OF_TESTS) {
248 ret = reset_enable_llc_perf(bm_pid, param->cpu_no);
252 if (run_fill_buf(param->span, malloc_and_init_memory,
253 memflush, operation, resctrl_val)) {
254 fprintf(stderr, "Error-running fill buffer\n");
260 ret = measure_cache_vals(param, bm_pid);
272 * show_cache_info: show cache test result information
273 * @sum_llc_val: sum of LLC cache result data
274 * @no_of_bits: number of bits
275 * @cache_span: cache span in bytes for CMT or in lines for CAT
276 * @max_diff: max difference
277 * @max_diff_percent: max difference percentage
278 * @num_of_runs: number of runs
279 * @platform: show test information on this platform
280 * @cmt: CMT test or CAT test
282 * Return: 0 on success. non-zero on failure.
284 int show_cache_info(unsigned long sum_llc_val, int no_of_bits,
285 unsigned long cache_span, unsigned long max_diff,
286 unsigned long max_diff_percent, unsigned long num_of_runs,
287 bool platform, bool cmt)
289 unsigned long avg_llc_val = 0;
294 avg_llc_val = sum_llc_val / (num_of_runs - 1);
295 avg_diff = (long)abs(cache_span - avg_llc_val);
296 diff_percent = ((float)cache_span - avg_llc_val) / cache_span * 100;
298 ret = platform && abs((int)diff_percent) > max_diff_percent &&
299 (cmt ? (abs(avg_diff) > max_diff) : true);
301 ksft_print_msg("%s Check cache miss rate within %d%%\n",
302 ret ? "Fail:" : "Pass:", max_diff_percent);
304 ksft_print_msg("Percent diff=%d\n", abs((int)diff_percent));
305 ksft_print_msg("Number of bits: %d\n", no_of_bits);
306 ksft_print_msg("Average LLC val: %lu\n", avg_llc_val);
307 ksft_print_msg("Cache span (%s): %lu\n", cmt ? "bytes" : "lines",