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 int 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;
66 static int reset_enable_llc_perf(pid_t pid, int cpu_no)
70 ret = perf_event_open_llc_miss(pid, cpu_no);
74 /* Start counters to log values */
75 ioctl_perf_event_ioc_reset_enable();
81 * get_llc_perf: llc cache miss through perf events
82 * @cpu_no: CPU number that the benchmark PID is binded to
84 * Perf events like HW_CACHE_MISSES could be used to validate number of
85 * cache lines allocated.
87 * Return: =0 on success. <0 on failure.
89 static int get_llc_perf(unsigned long *llc_perf_miss)
93 /* Stop counters after one span to get miss rate */
95 ioctl(fd_lm, PERF_EVENT_IOC_DISABLE, 0);
97 if (read(fd_lm, &rf_cqm, sizeof(struct read_format)) == -1) {
98 perror("Could not get llc misses through perf");
103 total_misses = rf_cqm.values[0].value;
107 *llc_perf_miss = total_misses;
113 * Get LLC Occupancy as reported by RESCTRL FS
115 * 1. If con_mon grp and mon grp given, then read from mon grp in
117 * 2. If only con_mon grp given, then read from con_mon grp
118 * 3. If both not given, then read from root con_mon grp
120 * 1. If con_mon grp given, then read from it
121 * 2. If con_mon grp not given, then read from root con_mon grp
123 * Return: =0 on success. <0 on failure.
125 static int get_llc_occu_resctrl(unsigned long *llc_occupancy)
129 fp = fopen(llc_occup_path, "r");
131 perror("Failed to open results file");
135 if (fscanf(fp, "%lu", llc_occupancy) <= 0) {
136 perror("Could not get llc occupancy");
147 * print_results_cache: the cache results are stored in a file
148 * @filename: file that stores the results
149 * @bm_pid: child pid that runs benchmark
150 * @llc_value: perf miss value /
151 * llc occupancy value reported by resctrl FS
153 * Return: 0 on success. non-zero on failure.
155 static int print_results_cache(char *filename, int bm_pid,
156 unsigned long llc_value)
160 if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
161 printf("Pid: %d \t LLC_value: %lu\n", bm_pid,
164 fp = fopen(filename, "a");
166 perror("Cannot open results file");
170 fprintf(fp, "Pid: %d \t llc_value: %lu\n", bm_pid, llc_value);
177 int measure_cache_vals(struct resctrl_val_param *param, int bm_pid)
179 unsigned long llc_perf_miss = 0, llc_occu_resc = 0, llc_value = 0;
183 * Measure cache miss from perf.
185 if (!strcmp(param->resctrl_val, "cat")) {
186 ret = get_llc_perf(&llc_perf_miss);
189 llc_value = llc_perf_miss;
193 * Measure llc occupancy from resctrl.
195 if (!strcmp(param->resctrl_val, "cqm")) {
196 ret = get_llc_occu_resctrl(&llc_occu_resc);
199 llc_value = llc_occu_resc;
201 ret = print_results_cache(param->filename, bm_pid, llc_value);
209 * cache_val: execute benchmark and measure LLC occupancy resctrl
210 * and perf cache miss for the benchmark
211 * @param: parameters passed to cache_val()
213 * Return: 0 on success. non-zero on failure.
215 int cat_val(struct resctrl_val_param *param)
217 int malloc_and_init_memory = 1, memflush = 1, operation = 0, ret = 0;
218 char *resctrl_val = param->resctrl_val;
221 if (strcmp(param->filename, "") == 0)
222 sprintf(param->filename, "stdio");
226 /* Taskset benchmark to specified cpu */
227 ret = taskset_benchmark(bm_pid, param->cpu_no);
231 /* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/
232 ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
237 if ((strcmp(resctrl_val, "cat") == 0)) {
238 ret = initialize_llc_perf();
243 /* Test runs until the callback setup() tells the test to stop. */
245 if (strcmp(resctrl_val, "cat") == 0) {
246 ret = param->setup(1, param);
251 ret = reset_enable_llc_perf(bm_pid, param->cpu_no);
255 if (run_fill_buf(param->span, malloc_and_init_memory,
256 memflush, operation, resctrl_val)) {
257 fprintf(stderr, "Error-running fill buffer\n");
263 ret = measure_cache_vals(param, bm_pid);