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 ctrlc_handler(0, NULL, NULL); 45 return -1; 46 } 47 48 return 0; 49 } 50 51 static void initialize_llc_perf(void) 52 { 53 memset(&pea_llc_miss, 0, sizeof(struct perf_event_attr)); 54 memset(&rf_cqm, 0, sizeof(struct read_format)); 55 56 /* Initialize perf_event_attr structures for HW_CACHE_MISSES */ 57 initialize_perf_event_attr(); 58 59 pea_llc_miss.config = PERF_COUNT_HW_CACHE_MISSES; 60 61 rf_cqm.nr = 1; 62 } 63 64 static int reset_enable_llc_perf(pid_t pid, int cpu_no) 65 { 66 int ret = 0; 67 68 ret = perf_event_open_llc_miss(pid, cpu_no); 69 if (ret < 0) 70 return ret; 71 72 /* Start counters to log values */ 73 ioctl_perf_event_ioc_reset_enable(); 74 75 return 0; 76 } 77 78 /* 79 * get_llc_perf: llc cache miss through perf events 80 * @llc_perf_miss: LLC miss counter that is filled on success 81 * 82 * Perf events like HW_CACHE_MISSES could be used to validate number of 83 * cache lines allocated. 84 * 85 * Return: =0 on success. <0 on failure. 86 */ 87 static int get_llc_perf(unsigned long *llc_perf_miss) 88 { 89 __u64 total_misses; 90 int ret; 91 92 /* Stop counters after one span to get miss rate */ 93 94 ioctl(fd_lm, PERF_EVENT_IOC_DISABLE, 0); 95 96 ret = read(fd_lm, &rf_cqm, sizeof(struct read_format)); 97 if (ret == -1) { 98 ksft_perror("Could not get llc misses through perf"); 99 return -1; 100 } 101 102 total_misses = rf_cqm.values[0].value; 103 *llc_perf_miss = total_misses; 104 105 return 0; 106 } 107 108 /* 109 * Get LLC Occupancy as reported by RESCTRL FS 110 * For CMT, 111 * 1. If con_mon grp and mon grp given, then read from mon grp in 112 * con_mon grp 113 * 2. If only con_mon grp given, then read from con_mon grp 114 * 3. If both not given, then read from root con_mon grp 115 * For CAT, 116 * 1. If con_mon grp given, then read from it 117 * 2. If con_mon grp not given, then read from root con_mon grp 118 * 119 * Return: =0 on success. <0 on failure. 120 */ 121 static int get_llc_occu_resctrl(unsigned long *llc_occupancy) 122 { 123 FILE *fp; 124 125 fp = fopen(llc_occup_path, "r"); 126 if (!fp) { 127 ksft_perror("Failed to open results file"); 128 129 return errno; 130 } 131 if (fscanf(fp, "%lu", llc_occupancy) <= 0) { 132 ksft_perror("Could not get llc occupancy"); 133 fclose(fp); 134 135 return -1; 136 } 137 fclose(fp); 138 139 return 0; 140 } 141 142 /* 143 * print_results_cache: the cache results are stored in a file 144 * @filename: file that stores the results 145 * @bm_pid: child pid that runs benchmark 146 * @llc_value: perf miss value / 147 * llc occupancy value reported by resctrl FS 148 * 149 * Return: 0 on success. non-zero on failure. 150 */ 151 static int print_results_cache(char *filename, int bm_pid, 152 unsigned long llc_value) 153 { 154 FILE *fp; 155 156 if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) { 157 printf("Pid: %d \t LLC_value: %lu\n", bm_pid, 158 llc_value); 159 } else { 160 fp = fopen(filename, "a"); 161 if (!fp) { 162 ksft_perror("Cannot open results file"); 163 164 return errno; 165 } 166 fprintf(fp, "Pid: %d \t llc_value: %lu\n", bm_pid, llc_value); 167 fclose(fp); 168 } 169 170 return 0; 171 } 172 173 int measure_cache_vals(struct resctrl_val_param *param, int bm_pid) 174 { 175 unsigned long llc_perf_miss = 0, llc_occu_resc = 0, llc_value = 0; 176 int ret; 177 178 /* 179 * Measure cache miss from perf. 180 */ 181 if (!strncmp(param->resctrl_val, CAT_STR, sizeof(CAT_STR))) { 182 ret = get_llc_perf(&llc_perf_miss); 183 if (ret < 0) 184 return ret; 185 llc_value = llc_perf_miss; 186 } 187 188 /* 189 * Measure llc occupancy from resctrl. 190 */ 191 if (!strncmp(param->resctrl_val, CMT_STR, sizeof(CMT_STR))) { 192 ret = get_llc_occu_resctrl(&llc_occu_resc); 193 if (ret < 0) 194 return ret; 195 llc_value = llc_occu_resc; 196 } 197 ret = print_results_cache(param->filename, bm_pid, llc_value); 198 if (ret) 199 return ret; 200 201 return 0; 202 } 203 204 /* 205 * cache_val: execute benchmark and measure LLC occupancy resctrl 206 * and perf cache miss for the benchmark 207 * @param: parameters passed to cache_val() 208 * @span: buffer size for the benchmark 209 * 210 * Return: 0 on success. non-zero on failure. 211 */ 212 int cat_val(struct resctrl_val_param *param, size_t span) 213 { 214 int memflush = 1, operation = 0, ret = 0; 215 char *resctrl_val = param->resctrl_val; 216 pid_t bm_pid; 217 218 if (strcmp(param->filename, "") == 0) 219 sprintf(param->filename, "stdio"); 220 221 bm_pid = getpid(); 222 223 /* Taskset benchmark to specified cpu */ 224 ret = taskset_benchmark(bm_pid, param->cpu_no); 225 if (ret) 226 return ret; 227 228 /* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/ 229 ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp, 230 resctrl_val); 231 if (ret) 232 return ret; 233 234 initialize_llc_perf(); 235 236 /* Test runs until the callback setup() tells the test to stop. */ 237 while (1) { 238 ret = param->setup(param); 239 if (ret == END_OF_TESTS) { 240 ret = 0; 241 break; 242 } 243 if (ret < 0) 244 break; 245 ret = reset_enable_llc_perf(bm_pid, param->cpu_no); 246 if (ret) 247 break; 248 249 if (run_fill_buf(span, memflush, operation, true)) { 250 fprintf(stderr, "Error-running fill buffer\n"); 251 ret = -1; 252 goto pe_close; 253 } 254 255 sleep(1); 256 ret = measure_cache_vals(param, bm_pid); 257 if (ret) 258 goto pe_close; 259 } 260 261 return ret; 262 263 pe_close: 264 close(fd_lm); 265 return ret; 266 } 267 268 /* 269 * show_cache_info: show cache test result information 270 * @sum_llc_val: sum of LLC cache result data 271 * @no_of_bits: number of bits 272 * @cache_span: cache span in bytes for CMT or in lines for CAT 273 * @max_diff: max difference 274 * @max_diff_percent: max difference percentage 275 * @num_of_runs: number of runs 276 * @platform: show test information on this platform 277 * @cmt: CMT test or CAT test 278 * 279 * Return: 0 on success. non-zero on failure. 280 */ 281 int show_cache_info(unsigned long sum_llc_val, int no_of_bits, 282 size_t cache_span, unsigned long max_diff, 283 unsigned long max_diff_percent, unsigned long num_of_runs, 284 bool platform, bool cmt) 285 { 286 unsigned long avg_llc_val = 0; 287 float diff_percent; 288 long avg_diff = 0; 289 int ret; 290 291 avg_llc_val = sum_llc_val / num_of_runs; 292 avg_diff = (long)abs(cache_span - avg_llc_val); 293 diff_percent = ((float)cache_span - avg_llc_val) / cache_span * 100; 294 295 ret = platform && abs((int)diff_percent) > max_diff_percent && 296 (cmt ? (abs(avg_diff) > max_diff) : true); 297 298 ksft_print_msg("%s Check cache miss rate within %d%%\n", 299 ret ? "Fail:" : "Pass:", max_diff_percent); 300 301 ksft_print_msg("Percent diff=%d\n", abs((int)diff_percent)); 302 ksft_print_msg("Number of bits: %d\n", no_of_bits); 303 ksft_print_msg("Average LLC val: %lu\n", avg_llc_val); 304 ksft_print_msg("Cache span (%s): %zu\n", cmt ? "bytes" : "lines", 305 cache_span); 306 307 return ret; 308 } 309