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 /* Start counters to log values */ 33 static void ioctl_perf_event_ioc_reset_enable(void) 34 { 35 ioctl(fd_lm, PERF_EVENT_IOC_RESET, 0); 36 ioctl(fd_lm, PERF_EVENT_IOC_ENABLE, 0); 37 } 38 39 static void initialize_llc_perf(void) 40 { 41 memset(&pea_llc_miss, 0, sizeof(struct perf_event_attr)); 42 memset(&rf_cqm, 0, sizeof(struct read_format)); 43 44 /* Initialize perf_event_attr structures for HW_CACHE_MISSES */ 45 initialize_perf_event_attr(); 46 47 pea_llc_miss.config = PERF_COUNT_HW_CACHE_MISSES; 48 49 rf_cqm.nr = 1; 50 } 51 52 static int reset_enable_llc_perf(pid_t pid, int cpu_no) 53 { 54 fd_lm = perf_event_open(&pea_llc_miss, pid, cpu_no, -1, PERF_FLAG_FD_CLOEXEC); 55 if (fd_lm == -1) { 56 ksft_perror("Error opening leader"); 57 return -1; 58 } 59 60 ioctl_perf_event_ioc_reset_enable(); 61 62 return 0; 63 } 64 65 /* 66 * Get LLC Occupancy as reported by RESCTRL FS 67 * For CMT, 68 * 1. If con_mon grp and mon grp given, then read from mon grp in 69 * con_mon grp 70 * 2. If only con_mon grp given, then read from con_mon grp 71 * 3. If both not given, then read from root con_mon grp 72 * For CAT, 73 * 1. If con_mon grp given, then read from it 74 * 2. If con_mon grp not given, then read from root con_mon grp 75 * 76 * Return: =0 on success. <0 on failure. 77 */ 78 static int get_llc_occu_resctrl(unsigned long *llc_occupancy) 79 { 80 FILE *fp; 81 82 fp = fopen(llc_occup_path, "r"); 83 if (!fp) { 84 ksft_perror("Failed to open results file"); 85 86 return -1; 87 } 88 if (fscanf(fp, "%lu", llc_occupancy) <= 0) { 89 ksft_perror("Could not get llc occupancy"); 90 fclose(fp); 91 92 return -1; 93 } 94 fclose(fp); 95 96 return 0; 97 } 98 99 /* 100 * print_results_cache: the cache results are stored in a file 101 * @filename: file that stores the results 102 * @bm_pid: child pid that runs benchmark 103 * @llc_value: perf miss value / 104 * llc occupancy value reported by resctrl FS 105 * 106 * Return: 0 on success, < 0 on error. 107 */ 108 static int print_results_cache(const char *filename, int bm_pid, __u64 llc_value) 109 { 110 FILE *fp; 111 112 if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) { 113 printf("Pid: %d \t LLC_value: %llu\n", bm_pid, llc_value); 114 } else { 115 fp = fopen(filename, "a"); 116 if (!fp) { 117 ksft_perror("Cannot open results file"); 118 119 return -1; 120 } 121 fprintf(fp, "Pid: %d \t llc_value: %llu\n", bm_pid, llc_value); 122 fclose(fp); 123 } 124 125 return 0; 126 } 127 128 /* 129 * perf_event_measure - Measure perf events 130 * @filename: Filename for writing the results 131 * @bm_pid: PID that runs the benchmark 132 * 133 * Measures perf events (e.g., cache misses) and writes the results into 134 * @filename. @bm_pid is written to the results file along with the measured 135 * value. 136 * 137 * Return: =0 on success. <0 on failure. 138 */ 139 static int perf_event_measure(const char *filename, int bm_pid) 140 { 141 int ret; 142 143 /* Stop counters after one span to get miss rate */ 144 ioctl(fd_lm, PERF_EVENT_IOC_DISABLE, 0); 145 146 ret = read(fd_lm, &rf_cqm, sizeof(struct read_format)); 147 if (ret == -1) { 148 ksft_perror("Could not get perf value"); 149 return -1; 150 } 151 152 return print_results_cache(filename, bm_pid, rf_cqm.values[0].value); 153 } 154 155 /* 156 * measure_llc_resctrl - Measure resctrl LLC value from resctrl 157 * @filename: Filename for writing the results 158 * @bm_pid: PID that runs the benchmark 159 * 160 * Measures LLC occupancy from resctrl and writes the results into @filename. 161 * @bm_pid is written to the results file along with the measured value. 162 * 163 * Return: =0 on success. <0 on failure. 164 */ 165 int measure_llc_resctrl(const char *filename, int bm_pid) 166 { 167 unsigned long llc_occu_resc = 0; 168 int ret; 169 170 ret = get_llc_occu_resctrl(&llc_occu_resc); 171 if (ret < 0) 172 return ret; 173 174 return print_results_cache(filename, bm_pid, llc_occu_resc); 175 } 176 177 /* 178 * cache_val: execute benchmark and measure LLC occupancy resctrl 179 * and perf cache miss for the benchmark 180 * @param: parameters passed to cache_val() 181 * @span: buffer size for the benchmark 182 * 183 * Return: 0 when the test was run, < 0 on error. 184 */ 185 int cat_val(struct resctrl_val_param *param, size_t span) 186 { 187 int memflush = 1, operation = 0, ret = 0; 188 char *resctrl_val = param->resctrl_val; 189 pid_t bm_pid; 190 191 if (strcmp(param->filename, "") == 0) 192 sprintf(param->filename, "stdio"); 193 194 bm_pid = getpid(); 195 196 /* Taskset benchmark to specified cpu */ 197 ret = taskset_benchmark(bm_pid, param->cpu_no); 198 if (ret) 199 return ret; 200 201 /* Write benchmark to specified con_mon grp, mon_grp in resctrl FS*/ 202 ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp, 203 resctrl_val); 204 if (ret) 205 return ret; 206 207 initialize_llc_perf(); 208 209 /* Test runs until the callback setup() tells the test to stop. */ 210 while (1) { 211 ret = param->setup(param); 212 if (ret == END_OF_TESTS) { 213 ret = 0; 214 break; 215 } 216 if (ret < 0) 217 break; 218 ret = reset_enable_llc_perf(bm_pid, param->cpu_no); 219 if (ret) 220 break; 221 222 if (run_fill_buf(span, memflush, operation, true)) { 223 fprintf(stderr, "Error-running fill buffer\n"); 224 ret = -1; 225 goto pe_close; 226 } 227 228 sleep(1); 229 ret = perf_event_measure(param->filename, bm_pid); 230 if (ret) 231 goto pe_close; 232 } 233 234 return ret; 235 236 pe_close: 237 close(fd_lm); 238 return ret; 239 } 240 241 /* 242 * show_cache_info - Show generic cache test information 243 * @no_of_bits: Number of bits 244 * @avg_llc_val: Average of LLC cache result data 245 * @cache_span: Cache span 246 * @lines: @cache_span in lines or bytes 247 */ 248 void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines) 249 { 250 ksft_print_msg("Number of bits: %d\n", no_of_bits); 251 ksft_print_msg("Average LLC val: %llu\n", avg_llc_val); 252 ksft_print_msg("Cache span (%s): %zu\n", lines ? "lines" : "bytes", 253 cache_span); 254 } 255