Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jiri Olsa | ce1e22b | 2016-02-15 09:34:35 +0100 | [diff] [blame] | 2 | #include <stddef.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <errno.h> |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame] | 6 | #include <sys/types.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <unistd.h> |
| 9 | #include <api/fs/fs.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | d3300a3 | 2019-08-30 15:09:54 -0300 | [diff] [blame] | 11 | #include "map_symbol.h" |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 12 | #include "mem-events.h" |
Jiri Olsa | ce1e22b | 2016-02-15 09:34:35 +0100 | [diff] [blame] | 13 | #include "debug.h" |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 14 | #include "symbol.h" |
Jin Yao | e7ce8d1 | 2021-05-27 08:16:06 +0800 | [diff] [blame] | 15 | #include "pmu.h" |
| 16 | #include "pmu-hybrid.h" |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 17 | |
Jiri Olsa | b0d745b | 2016-06-14 20:19:11 +0200 | [diff] [blame] | 18 | unsigned int perf_mem_events__loads_ldlat = 30; |
| 19 | |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame] | 20 | #define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s } |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 21 | |
Leo Yan | eaf6aae | 2020-11-06 17:48:46 +0800 | [diff] [blame] | 22 | static struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = { |
Leo Yan | f9f16df | 2020-11-06 17:48:45 +0800 | [diff] [blame] | 23 | E("ldlat-loads", "cpu/mem-loads,ldlat=%u/P", "cpu/events/mem-loads"), |
| 24 | E("ldlat-stores", "cpu/mem-stores/P", "cpu/events/mem-stores"), |
Leo Yan | 4ba2452 | 2020-11-06 17:48:47 +0800 | [diff] [blame] | 25 | E(NULL, NULL, NULL), |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 26 | }; |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame] | 27 | #undef E |
Jiri Olsa | acbe613 | 2016-02-15 09:34:34 +0100 | [diff] [blame] | 28 | |
Jiri Olsa | b0d745b | 2016-06-14 20:19:11 +0200 | [diff] [blame] | 29 | static char mem_loads_name[100]; |
| 30 | static bool mem_loads_name__init; |
| 31 | |
Leo Yan | eaf6aae | 2020-11-06 17:48:46 +0800 | [diff] [blame] | 32 | struct perf_mem_event * __weak perf_mem_events__ptr(int i) |
| 33 | { |
| 34 | if (i >= PERF_MEM_EVENTS__MAX) |
| 35 | return NULL; |
| 36 | |
| 37 | return &perf_mem_events[i]; |
| 38 | } |
| 39 | |
Jin Yao | d2f327a | 2021-05-27 08:16:04 +0800 | [diff] [blame] | 40 | char * __weak perf_mem_events__name(int i, char *pmu_name __maybe_unused) |
Jiri Olsa | 2ba7ac5 | 2016-02-24 09:46:43 +0100 | [diff] [blame] | 41 | { |
Leo Yan | eaf6aae | 2020-11-06 17:48:46 +0800 | [diff] [blame] | 42 | struct perf_mem_event *e = perf_mem_events__ptr(i); |
| 43 | |
| 44 | if (!e) |
| 45 | return NULL; |
| 46 | |
Jiri Olsa | b0d745b | 2016-06-14 20:19:11 +0200 | [diff] [blame] | 47 | if (i == PERF_MEM_EVENTS__LOAD) { |
| 48 | if (!mem_loads_name__init) { |
| 49 | mem_loads_name__init = true; |
| 50 | scnprintf(mem_loads_name, sizeof(mem_loads_name), |
Leo Yan | eaf6aae | 2020-11-06 17:48:46 +0800 | [diff] [blame] | 51 | e->name, perf_mem_events__loads_ldlat); |
Jiri Olsa | b0d745b | 2016-06-14 20:19:11 +0200 | [diff] [blame] | 52 | } |
| 53 | return mem_loads_name; |
| 54 | } |
| 55 | |
Leo Yan | eaf6aae | 2020-11-06 17:48:46 +0800 | [diff] [blame] | 56 | return (char *)e->name; |
Jiri Olsa | 2ba7ac5 | 2016-02-24 09:46:43 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Kan Liang | 2a57d40 | 2021-02-02 12:09:06 -0800 | [diff] [blame] | 59 | __weak bool is_mem_loads_aux_event(struct evsel *leader __maybe_unused) |
| 60 | { |
| 61 | return false; |
| 62 | } |
| 63 | |
Jiri Olsa | ce1e22b | 2016-02-15 09:34:35 +0100 | [diff] [blame] | 64 | int perf_mem_events__parse(const char *str) |
| 65 | { |
| 66 | char *tok, *saveptr = NULL; |
| 67 | bool found = false; |
| 68 | char *buf; |
| 69 | int j; |
| 70 | |
| 71 | /* We need buffer that we know we can write to. */ |
| 72 | buf = malloc(strlen(str) + 1); |
| 73 | if (!buf) |
| 74 | return -ENOMEM; |
| 75 | |
| 76 | strcpy(buf, str); |
| 77 | |
| 78 | tok = strtok_r((char *)buf, ",", &saveptr); |
| 79 | |
| 80 | while (tok) { |
| 81 | for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { |
Leo Yan | eaf6aae | 2020-11-06 17:48:46 +0800 | [diff] [blame] | 82 | struct perf_mem_event *e = perf_mem_events__ptr(j); |
Jiri Olsa | ce1e22b | 2016-02-15 09:34:35 +0100 | [diff] [blame] | 83 | |
Leo Yan | 4ba2452 | 2020-11-06 17:48:47 +0800 | [diff] [blame] | 84 | if (!e->tag) |
| 85 | continue; |
| 86 | |
Jiri Olsa | ce1e22b | 2016-02-15 09:34:35 +0100 | [diff] [blame] | 87 | if (strstr(e->tag, tok)) |
| 88 | e->record = found = true; |
| 89 | } |
| 90 | |
| 91 | tok = strtok_r(NULL, ",", &saveptr); |
| 92 | } |
| 93 | |
| 94 | free(buf); |
| 95 | |
| 96 | if (found) |
| 97 | return 0; |
| 98 | |
| 99 | pr_err("failed: event '%s' not found, use '-e list' to get list of available events\n", str); |
| 100 | return -1; |
| 101 | } |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame] | 102 | |
Jin Yao | e7ce8d1 | 2021-05-27 08:16:06 +0800 | [diff] [blame] | 103 | static bool perf_mem_event__supported(const char *mnt, char *sysfs_name) |
| 104 | { |
| 105 | char path[PATH_MAX]; |
| 106 | struct stat st; |
| 107 | |
| 108 | scnprintf(path, PATH_MAX, "%s/devices/%s", mnt, sysfs_name); |
| 109 | return !stat(path, &st); |
| 110 | } |
| 111 | |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame] | 112 | int perf_mem_events__init(void) |
| 113 | { |
| 114 | const char *mnt = sysfs__mount(); |
| 115 | bool found = false; |
| 116 | int j; |
| 117 | |
| 118 | if (!mnt) |
| 119 | return -ENOENT; |
| 120 | |
| 121 | for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { |
Leo Yan | eaf6aae | 2020-11-06 17:48:46 +0800 | [diff] [blame] | 122 | struct perf_mem_event *e = perf_mem_events__ptr(j); |
Jin Yao | e7ce8d1 | 2021-05-27 08:16:06 +0800 | [diff] [blame] | 123 | struct perf_pmu *pmu; |
| 124 | char sysfs_name[100]; |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame] | 125 | |
Leo Yan | 4ba2452 | 2020-11-06 17:48:47 +0800 | [diff] [blame] | 126 | /* |
| 127 | * If the event entry isn't valid, skip initialization |
| 128 | * and "e->supported" will keep false. |
| 129 | */ |
| 130 | if (!e->tag) |
| 131 | continue; |
| 132 | |
Jin Yao | e7ce8d1 | 2021-05-27 08:16:06 +0800 | [diff] [blame] | 133 | if (!perf_pmu__has_hybrid()) { |
| 134 | scnprintf(sysfs_name, sizeof(sysfs_name), |
| 135 | e->sysfs_name, "cpu"); |
| 136 | e->supported = perf_mem_event__supported(mnt, sysfs_name); |
| 137 | } else { |
| 138 | perf_pmu__for_each_hybrid_pmu(pmu) { |
| 139 | scnprintf(sysfs_name, sizeof(sysfs_name), |
| 140 | e->sysfs_name, pmu->name); |
| 141 | e->supported |= perf_mem_event__supported(mnt, sysfs_name); |
| 142 | } |
| 143 | } |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame] | 144 | |
Jin Yao | e7ce8d1 | 2021-05-27 08:16:06 +0800 | [diff] [blame] | 145 | if (e->supported) |
| 146 | found = true; |
Jiri Olsa | 54fbad5 | 2016-02-24 09:46:42 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return found ? 0 : -ENOENT; |
| 150 | } |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 151 | |
Ian Rogers | b027cc6 | 2020-05-07 15:06:04 -0700 | [diff] [blame] | 152 | void perf_mem_events__list(void) |
| 153 | { |
| 154 | int j; |
| 155 | |
| 156 | for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { |
Leo Yan | eaf6aae | 2020-11-06 17:48:46 +0800 | [diff] [blame] | 157 | struct perf_mem_event *e = perf_mem_events__ptr(j); |
Ian Rogers | b027cc6 | 2020-05-07 15:06:04 -0700 | [diff] [blame] | 158 | |
| 159 | fprintf(stderr, "%-13s%-*s%s\n", |
Leo Yan | 4ba2452 | 2020-11-06 17:48:47 +0800 | [diff] [blame] | 160 | e->tag ?: "", |
Ian Rogers | b027cc6 | 2020-05-07 15:06:04 -0700 | [diff] [blame] | 161 | verbose > 0 ? 25 : 0, |
Jin Yao | d2f327a | 2021-05-27 08:16:04 +0800 | [diff] [blame] | 162 | verbose > 0 ? perf_mem_events__name(j, NULL) : "", |
Ian Rogers | b027cc6 | 2020-05-07 15:06:04 -0700 | [diff] [blame] | 163 | e->supported ? ": available" : ""); |
| 164 | } |
| 165 | } |
| 166 | |
Jin Yao | 4a9086a | 2021-05-27 08:16:07 +0800 | [diff] [blame] | 167 | static void perf_mem_events__print_unsupport_hybrid(struct perf_mem_event *e, |
| 168 | int idx) |
| 169 | { |
| 170 | const char *mnt = sysfs__mount(); |
| 171 | char sysfs_name[100]; |
| 172 | struct perf_pmu *pmu; |
| 173 | |
| 174 | perf_pmu__for_each_hybrid_pmu(pmu) { |
| 175 | scnprintf(sysfs_name, sizeof(sysfs_name), e->sysfs_name, |
| 176 | pmu->name); |
| 177 | if (!perf_mem_event__supported(mnt, sysfs_name)) { |
| 178 | pr_err("failed: event '%s' not supported\n", |
| 179 | perf_mem_events__name(idx, pmu->name)); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | int perf_mem_events__record_args(const char **rec_argv, int *argv_nr, |
| 185 | char **rec_tmp, int *tmp_nr) |
| 186 | { |
| 187 | int i = *argv_nr, k = 0; |
| 188 | struct perf_mem_event *e; |
| 189 | struct perf_pmu *pmu; |
| 190 | char *s; |
| 191 | |
| 192 | for (int j = 0; j < PERF_MEM_EVENTS__MAX; j++) { |
| 193 | e = perf_mem_events__ptr(j); |
| 194 | if (!e->record) |
| 195 | continue; |
| 196 | |
| 197 | if (!perf_pmu__has_hybrid()) { |
| 198 | if (!e->supported) { |
| 199 | pr_err("failed: event '%s' not supported\n", |
| 200 | perf_mem_events__name(j, NULL)); |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | rec_argv[i++] = "-e"; |
| 205 | rec_argv[i++] = perf_mem_events__name(j, NULL); |
| 206 | } else { |
| 207 | if (!e->supported) { |
| 208 | perf_mem_events__print_unsupport_hybrid(e, j); |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | perf_pmu__for_each_hybrid_pmu(pmu) { |
| 213 | rec_argv[i++] = "-e"; |
| 214 | s = perf_mem_events__name(j, pmu->name); |
| 215 | if (s) { |
| 216 | s = strdup(s); |
| 217 | if (!s) |
| 218 | return -1; |
| 219 | |
| 220 | rec_argv[i++] = s; |
| 221 | rec_tmp[k++] = s; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | *argv_nr = i; |
| 228 | *tmp_nr = k; |
| 229 | return 0; |
| 230 | } |
| 231 | |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 232 | static const char * const tlb_access[] = { |
| 233 | "N/A", |
| 234 | "HIT", |
| 235 | "MISS", |
| 236 | "L1", |
| 237 | "L2", |
| 238 | "Walker", |
| 239 | "Fault", |
| 240 | }; |
| 241 | |
Jiri Olsa | b1a5fbea | 2016-02-24 09:46:50 +0100 | [diff] [blame] | 242 | int perf_mem__tlb_scnprintf(char *out, size_t sz, struct mem_info *mem_info) |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 243 | { |
| 244 | size_t l = 0, i; |
| 245 | u64 m = PERF_MEM_TLB_NA; |
| 246 | u64 hit, miss; |
| 247 | |
| 248 | sz -= 1; /* -1 for null termination */ |
| 249 | out[0] = '\0'; |
| 250 | |
| 251 | if (mem_info) |
| 252 | m = mem_info->data_src.mem_dtlb; |
| 253 | |
| 254 | hit = m & PERF_MEM_TLB_HIT; |
| 255 | miss = m & PERF_MEM_TLB_MISS; |
| 256 | |
| 257 | /* already taken care of */ |
| 258 | m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS); |
| 259 | |
| 260 | for (i = 0; m && i < ARRAY_SIZE(tlb_access); i++, m >>= 1) { |
| 261 | if (!(m & 0x1)) |
| 262 | continue; |
| 263 | if (l) { |
| 264 | strcat(out, " or "); |
| 265 | l += 4; |
| 266 | } |
Jiri Olsa | b1a5fbea | 2016-02-24 09:46:50 +0100 | [diff] [blame] | 267 | l += scnprintf(out + l, sz - l, tlb_access[i]); |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 268 | } |
| 269 | if (*out == '\0') |
Jiri Olsa | b1a5fbea | 2016-02-24 09:46:50 +0100 | [diff] [blame] | 270 | l += scnprintf(out, sz - l, "N/A"); |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 271 | if (hit) |
Jiri Olsa | b1a5fbea | 2016-02-24 09:46:50 +0100 | [diff] [blame] | 272 | l += scnprintf(out + l, sz - l, " hit"); |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 273 | if (miss) |
Jiri Olsa | b1a5fbea | 2016-02-24 09:46:50 +0100 | [diff] [blame] | 274 | l += scnprintf(out + l, sz - l, " miss"); |
| 275 | |
| 276 | return l; |
Jiri Olsa | 0c877d7 | 2016-02-24 09:46:46 +0100 | [diff] [blame] | 277 | } |
Jiri Olsa | 071e9a1 | 2016-02-24 09:46:47 +0100 | [diff] [blame] | 278 | |
| 279 | static const char * const mem_lvl[] = { |
| 280 | "N/A", |
| 281 | "HIT", |
| 282 | "MISS", |
| 283 | "L1", |
| 284 | "LFB", |
| 285 | "L2", |
| 286 | "L3", |
| 287 | "Local RAM", |
| 288 | "Remote RAM (1 hop)", |
| 289 | "Remote RAM (2 hops)", |
| 290 | "Remote Cache (1 hop)", |
| 291 | "Remote Cache (2 hops)", |
| 292 | "I/O", |
| 293 | "Uncached", |
| 294 | }; |
| 295 | |
Andi Kleen | 52839e6 | 2017-08-16 15:21:55 -0700 | [diff] [blame] | 296 | static const char * const mem_lvlnum[] = { |
| 297 | [PERF_MEM_LVLNUM_ANY_CACHE] = "Any cache", |
| 298 | [PERF_MEM_LVLNUM_LFB] = "LFB", |
| 299 | [PERF_MEM_LVLNUM_RAM] = "RAM", |
| 300 | [PERF_MEM_LVLNUM_PMEM] = "PMEM", |
| 301 | [PERF_MEM_LVLNUM_NA] = "N/A", |
| 302 | }; |
| 303 | |
Kajol Jain | cae1d75 | 2021-10-06 19:36:53 +0530 | [diff] [blame] | 304 | static const char * const mem_hops[] = { |
| 305 | "N/A", |
| 306 | /* |
| 307 | * While printing, 'Remote' will be added to represent |
| 308 | * 'Remote core, same node' accesses as remote field need |
| 309 | * to be set with mem_hops field. |
| 310 | */ |
| 311 | "core, same node", |
Kajol Jain | 7fbddf4 | 2021-12-06 14:47:47 +0530 | [diff] [blame] | 312 | "node, same socket", |
| 313 | "socket, same board", |
| 314 | "board", |
Kajol Jain | cae1d75 | 2021-10-06 19:36:53 +0530 | [diff] [blame] | 315 | }; |
| 316 | |
Jiri Olsa | 9690756 | 2016-02-24 09:46:51 +0100 | [diff] [blame] | 317 | int perf_mem__lvl_scnprintf(char *out, size_t sz, struct mem_info *mem_info) |
Jiri Olsa | 071e9a1 | 2016-02-24 09:46:47 +0100 | [diff] [blame] | 318 | { |
| 319 | size_t i, l = 0; |
| 320 | u64 m = PERF_MEM_LVL_NA; |
| 321 | u64 hit, miss; |
Kajol Jain | 7fbddf4 | 2021-12-06 14:47:47 +0530 | [diff] [blame] | 322 | int printed = 0; |
Jiri Olsa | 071e9a1 | 2016-02-24 09:46:47 +0100 | [diff] [blame] | 323 | |
| 324 | if (mem_info) |
| 325 | m = mem_info->data_src.mem_lvl; |
| 326 | |
| 327 | sz -= 1; /* -1 for null termination */ |
| 328 | out[0] = '\0'; |
| 329 | |
| 330 | hit = m & PERF_MEM_LVL_HIT; |
| 331 | miss = m & PERF_MEM_LVL_MISS; |
| 332 | |
| 333 | /* already taken care of */ |
| 334 | m &= ~(PERF_MEM_LVL_HIT|PERF_MEM_LVL_MISS); |
| 335 | |
Andi Kleen | 52839e6 | 2017-08-16 15:21:55 -0700 | [diff] [blame] | 336 | if (mem_info && mem_info->data_src.mem_remote) { |
| 337 | strcat(out, "Remote "); |
| 338 | l += 7; |
| 339 | } |
| 340 | |
Kajol Jain | 7fbddf4 | 2021-12-06 14:47:47 +0530 | [diff] [blame] | 341 | /* |
| 342 | * Incase mem_hops field is set, we can skip printing data source via |
| 343 | * PERF_MEM_LVL namespace. |
| 344 | */ |
| 345 | if (mem_info && mem_info->data_src.mem_hops) { |
Kajol Jain | cae1d75 | 2021-10-06 19:36:53 +0530 | [diff] [blame] | 346 | l += scnprintf(out + l, sz - l, "%s ", mem_hops[mem_info->data_src.mem_hops]); |
Kajol Jain | 7fbddf4 | 2021-12-06 14:47:47 +0530 | [diff] [blame] | 347 | } else { |
| 348 | for (i = 0; m && i < ARRAY_SIZE(mem_lvl); i++, m >>= 1) { |
| 349 | if (!(m & 0x1)) |
| 350 | continue; |
| 351 | if (printed++) { |
| 352 | strcat(out, " or "); |
| 353 | l += 4; |
| 354 | } |
| 355 | l += scnprintf(out + l, sz - l, mem_lvl[i]); |
Jiri Olsa | 071e9a1 | 2016-02-24 09:46:47 +0100 | [diff] [blame] | 356 | } |
Jiri Olsa | 071e9a1 | 2016-02-24 09:46:47 +0100 | [diff] [blame] | 357 | } |
Andi Kleen | 52839e6 | 2017-08-16 15:21:55 -0700 | [diff] [blame] | 358 | |
| 359 | if (mem_info && mem_info->data_src.mem_lvl_num) { |
| 360 | int lvl = mem_info->data_src.mem_lvl_num; |
| 361 | if (printed++) { |
| 362 | strcat(out, " or "); |
| 363 | l += 4; |
| 364 | } |
| 365 | if (mem_lvlnum[lvl]) |
| 366 | l += scnprintf(out + l, sz - l, mem_lvlnum[lvl]); |
| 367 | else |
| 368 | l += scnprintf(out + l, sz - l, "L%d", lvl); |
| 369 | } |
| 370 | |
| 371 | if (l == 0) |
| 372 | l += scnprintf(out + l, sz - l, "N/A"); |
Jiri Olsa | 071e9a1 | 2016-02-24 09:46:47 +0100 | [diff] [blame] | 373 | if (hit) |
Jiri Olsa | 9690756 | 2016-02-24 09:46:51 +0100 | [diff] [blame] | 374 | l += scnprintf(out + l, sz - l, " hit"); |
Jiri Olsa | 071e9a1 | 2016-02-24 09:46:47 +0100 | [diff] [blame] | 375 | if (miss) |
Jiri Olsa | 9690756 | 2016-02-24 09:46:51 +0100 | [diff] [blame] | 376 | l += scnprintf(out + l, sz - l, " miss"); |
| 377 | |
| 378 | return l; |
Jiri Olsa | 071e9a1 | 2016-02-24 09:46:47 +0100 | [diff] [blame] | 379 | } |
Jiri Olsa | 2c07af1 | 2016-02-24 09:46:48 +0100 | [diff] [blame] | 380 | |
| 381 | static const char * const snoop_access[] = { |
| 382 | "N/A", |
| 383 | "None", |
Jiri Olsa | 2c07af1 | 2016-02-24 09:46:48 +0100 | [diff] [blame] | 384 | "Hit", |
Andi Kleen | 166ebdd | 2017-04-19 10:49:40 -0700 | [diff] [blame] | 385 | "Miss", |
Jiri Olsa | 2c07af1 | 2016-02-24 09:46:48 +0100 | [diff] [blame] | 386 | "HitM", |
| 387 | }; |
| 388 | |
Jiri Olsa | 149d750 | 2016-02-24 09:46:52 +0100 | [diff] [blame] | 389 | int perf_mem__snp_scnprintf(char *out, size_t sz, struct mem_info *mem_info) |
Jiri Olsa | 2c07af1 | 2016-02-24 09:46:48 +0100 | [diff] [blame] | 390 | { |
| 391 | size_t i, l = 0; |
| 392 | u64 m = PERF_MEM_SNOOP_NA; |
| 393 | |
| 394 | sz -= 1; /* -1 for null termination */ |
| 395 | out[0] = '\0'; |
| 396 | |
| 397 | if (mem_info) |
| 398 | m = mem_info->data_src.mem_snoop; |
| 399 | |
| 400 | for (i = 0; m && i < ARRAY_SIZE(snoop_access); i++, m >>= 1) { |
| 401 | if (!(m & 0x1)) |
| 402 | continue; |
| 403 | if (l) { |
| 404 | strcat(out, " or "); |
| 405 | l += 4; |
| 406 | } |
Jiri Olsa | 149d750 | 2016-02-24 09:46:52 +0100 | [diff] [blame] | 407 | l += scnprintf(out + l, sz - l, snoop_access[i]); |
Jiri Olsa | 2c07af1 | 2016-02-24 09:46:48 +0100 | [diff] [blame] | 408 | } |
Andi Kleen | 52839e6 | 2017-08-16 15:21:55 -0700 | [diff] [blame] | 409 | if (mem_info && |
| 410 | (mem_info->data_src.mem_snoopx & PERF_MEM_SNOOPX_FWD)) { |
| 411 | if (l) { |
| 412 | strcat(out, " or "); |
| 413 | l += 4; |
| 414 | } |
| 415 | l += scnprintf(out + l, sz - l, "Fwd"); |
| 416 | } |
Jiri Olsa | 2c07af1 | 2016-02-24 09:46:48 +0100 | [diff] [blame] | 417 | |
| 418 | if (*out == '\0') |
Jiri Olsa | 149d750 | 2016-02-24 09:46:52 +0100 | [diff] [blame] | 419 | l += scnprintf(out, sz - l, "N/A"); |
| 420 | |
| 421 | return l; |
Jiri Olsa | 2c07af1 | 2016-02-24 09:46:48 +0100 | [diff] [blame] | 422 | } |
Jiri Olsa | 69a7727 | 2016-02-24 09:46:49 +0100 | [diff] [blame] | 423 | |
Jiri Olsa | 8b0819c | 2016-02-24 09:46:53 +0100 | [diff] [blame] | 424 | int perf_mem__lck_scnprintf(char *out, size_t sz, struct mem_info *mem_info) |
Jiri Olsa | 69a7727 | 2016-02-24 09:46:49 +0100 | [diff] [blame] | 425 | { |
| 426 | u64 mask = PERF_MEM_LOCK_NA; |
Jiri Olsa | 8b0819c | 2016-02-24 09:46:53 +0100 | [diff] [blame] | 427 | int l; |
Jiri Olsa | 69a7727 | 2016-02-24 09:46:49 +0100 | [diff] [blame] | 428 | |
| 429 | if (mem_info) |
| 430 | mask = mem_info->data_src.mem_lock; |
| 431 | |
| 432 | if (mask & PERF_MEM_LOCK_NA) |
Jiri Olsa | 8b0819c | 2016-02-24 09:46:53 +0100 | [diff] [blame] | 433 | l = scnprintf(out, sz, "N/A"); |
Jiri Olsa | 69a7727 | 2016-02-24 09:46:49 +0100 | [diff] [blame] | 434 | else if (mask & PERF_MEM_LOCK_LOCKED) |
Jiri Olsa | 8b0819c | 2016-02-24 09:46:53 +0100 | [diff] [blame] | 435 | l = scnprintf(out, sz, "Yes"); |
Jiri Olsa | 69a7727 | 2016-02-24 09:46:49 +0100 | [diff] [blame] | 436 | else |
Jiri Olsa | 8b0819c | 2016-02-24 09:46:53 +0100 | [diff] [blame] | 437 | l = scnprintf(out, sz, "No"); |
| 438 | |
| 439 | return l; |
Jiri Olsa | 69a7727 | 2016-02-24 09:46:49 +0100 | [diff] [blame] | 440 | } |
Jiri Olsa | c19ac91 | 2016-02-24 09:46:54 +0100 | [diff] [blame] | 441 | |
Kan Liang | a054c29 | 2021-02-02 12:09:07 -0800 | [diff] [blame] | 442 | int perf_mem__blk_scnprintf(char *out, size_t sz, struct mem_info *mem_info) |
| 443 | { |
| 444 | size_t l = 0; |
| 445 | u64 mask = PERF_MEM_BLK_NA; |
| 446 | |
| 447 | sz -= 1; /* -1 for null termination */ |
| 448 | out[0] = '\0'; |
| 449 | |
| 450 | if (mem_info) |
| 451 | mask = mem_info->data_src.mem_blk; |
| 452 | |
| 453 | if (!mask || (mask & PERF_MEM_BLK_NA)) { |
| 454 | l += scnprintf(out + l, sz - l, " N/A"); |
| 455 | return l; |
| 456 | } |
| 457 | if (mask & PERF_MEM_BLK_DATA) |
| 458 | l += scnprintf(out + l, sz - l, " Data"); |
| 459 | if (mask & PERF_MEM_BLK_ADDR) |
| 460 | l += scnprintf(out + l, sz - l, " Addr"); |
| 461 | |
| 462 | return l; |
| 463 | } |
| 464 | |
Jiri Olsa | c19ac91 | 2016-02-24 09:46:54 +0100 | [diff] [blame] | 465 | int perf_script__meminfo_scnprintf(char *out, size_t sz, struct mem_info *mem_info) |
| 466 | { |
| 467 | int i = 0; |
| 468 | |
| 469 | i += perf_mem__lvl_scnprintf(out, sz, mem_info); |
| 470 | i += scnprintf(out + i, sz - i, "|SNP "); |
| 471 | i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info); |
| 472 | i += scnprintf(out + i, sz - i, "|TLB "); |
| 473 | i += perf_mem__tlb_scnprintf(out + i, sz - i, mem_info); |
| 474 | i += scnprintf(out + i, sz - i, "|LCK "); |
| 475 | i += perf_mem__lck_scnprintf(out + i, sz - i, mem_info); |
Kan Liang | a054c29 | 2021-02-02 12:09:07 -0800 | [diff] [blame] | 476 | i += scnprintf(out + i, sz - i, "|BLK "); |
| 477 | i += perf_mem__blk_scnprintf(out + i, sz - i, mem_info); |
Jiri Olsa | c19ac91 | 2016-02-24 09:46:54 +0100 | [diff] [blame] | 478 | |
| 479 | return i; |
| 480 | } |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 481 | |
| 482 | int c2c_decode_stats(struct c2c_stats *stats, struct mem_info *mi) |
| 483 | { |
| 484 | union perf_mem_data_src *data_src = &mi->data_src; |
| 485 | u64 daddr = mi->daddr.addr; |
| 486 | u64 op = data_src->mem_op; |
| 487 | u64 lvl = data_src->mem_lvl; |
| 488 | u64 snoop = data_src->mem_snoop; |
| 489 | u64 lock = data_src->mem_lock; |
Kan Liang | d9d5d767 | 2021-02-02 12:09:08 -0800 | [diff] [blame] | 490 | u64 blk = data_src->mem_blk; |
Jiri Olsa | 12c1530 | 2017-08-24 10:57:32 +0200 | [diff] [blame] | 491 | /* |
| 492 | * Skylake might report unknown remote level via this |
| 493 | * bit, consider it when evaluating remote HITMs. |
Kajol Jain | cae1d75 | 2021-10-06 19:36:53 +0530 | [diff] [blame] | 494 | * |
| 495 | * Incase of power, remote field can also be used to denote cache |
| 496 | * accesses from the another core of same node. Hence, setting |
| 497 | * mrem only when HOPS is zero along with set remote field. |
Jiri Olsa | 12c1530 | 2017-08-24 10:57:32 +0200 | [diff] [blame] | 498 | */ |
Kajol Jain | cae1d75 | 2021-10-06 19:36:53 +0530 | [diff] [blame] | 499 | bool mrem = (data_src->mem_remote && !data_src->mem_hops); |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 500 | int err = 0; |
| 501 | |
Jiri Olsa | dba8ab9 | 2016-11-21 22:33:29 +0100 | [diff] [blame] | 502 | #define HITM_INC(__f) \ |
| 503 | do { \ |
| 504 | stats->__f++; \ |
| 505 | stats->tot_hitm++; \ |
| 506 | } while (0) |
| 507 | |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 508 | #define P(a, b) PERF_MEM_##a##_##b |
| 509 | |
| 510 | stats->nr_entries++; |
| 511 | |
| 512 | if (lock & P(LOCK, LOCKED)) stats->locks++; |
| 513 | |
Kan Liang | d9d5d767 | 2021-02-02 12:09:08 -0800 | [diff] [blame] | 514 | if (blk & P(BLK, DATA)) stats->blk_data++; |
| 515 | if (blk & P(BLK, ADDR)) stats->blk_addr++; |
| 516 | |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 517 | if (op & P(OP, LOAD)) { |
| 518 | /* load */ |
| 519 | stats->load++; |
| 520 | |
| 521 | if (!daddr) { |
| 522 | stats->ld_noadrs++; |
| 523 | return -1; |
| 524 | } |
| 525 | |
| 526 | if (lvl & P(LVL, HIT)) { |
| 527 | if (lvl & P(LVL, UNC)) stats->ld_uncache++; |
| 528 | if (lvl & P(LVL, IO)) stats->ld_io++; |
| 529 | if (lvl & P(LVL, LFB)) stats->ld_fbhit++; |
| 530 | if (lvl & P(LVL, L1 )) stats->ld_l1hit++; |
| 531 | if (lvl & P(LVL, L2 )) stats->ld_l2hit++; |
| 532 | if (lvl & P(LVL, L3 )) { |
| 533 | if (snoop & P(SNOOP, HITM)) |
Jiri Olsa | dba8ab9 | 2016-11-21 22:33:29 +0100 | [diff] [blame] | 534 | HITM_INC(lcl_hitm); |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 535 | else |
| 536 | stats->ld_llchit++; |
| 537 | } |
| 538 | |
| 539 | if (lvl & P(LVL, LOC_RAM)) { |
| 540 | stats->lcl_dram++; |
| 541 | if (snoop & P(SNOOP, HIT)) |
| 542 | stats->ld_shared++; |
| 543 | else |
| 544 | stats->ld_excl++; |
| 545 | } |
| 546 | |
| 547 | if ((lvl & P(LVL, REM_RAM1)) || |
Jiri Olsa | 12c1530 | 2017-08-24 10:57:32 +0200 | [diff] [blame] | 548 | (lvl & P(LVL, REM_RAM2)) || |
| 549 | mrem) { |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 550 | stats->rmt_dram++; |
| 551 | if (snoop & P(SNOOP, HIT)) |
| 552 | stats->ld_shared++; |
| 553 | else |
| 554 | stats->ld_excl++; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | if ((lvl & P(LVL, REM_CCE1)) || |
Jiri Olsa | 12c1530 | 2017-08-24 10:57:32 +0200 | [diff] [blame] | 559 | (lvl & P(LVL, REM_CCE2)) || |
| 560 | mrem) { |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 561 | if (snoop & P(SNOOP, HIT)) |
| 562 | stats->rmt_hit++; |
| 563 | else if (snoop & P(SNOOP, HITM)) |
Jiri Olsa | dba8ab9 | 2016-11-21 22:33:29 +0100 | [diff] [blame] | 564 | HITM_INC(rmt_hitm); |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | if ((lvl & P(LVL, MISS))) |
| 568 | stats->ld_miss++; |
| 569 | |
| 570 | } else if (op & P(OP, STORE)) { |
| 571 | /* store */ |
| 572 | stats->store++; |
| 573 | |
| 574 | if (!daddr) { |
| 575 | stats->st_noadrs++; |
| 576 | return -1; |
| 577 | } |
| 578 | |
| 579 | if (lvl & P(LVL, HIT)) { |
| 580 | if (lvl & P(LVL, UNC)) stats->st_uncache++; |
| 581 | if (lvl & P(LVL, L1 )) stats->st_l1hit++; |
| 582 | } |
| 583 | if (lvl & P(LVL, MISS)) |
| 584 | if (lvl & P(LVL, L1)) stats->st_l1miss++; |
| 585 | } else { |
| 586 | /* unparsable data_src? */ |
| 587 | stats->noparse++; |
| 588 | return -1; |
| 589 | } |
| 590 | |
Arnaldo Carvalho de Melo | d46a4cd | 2019-11-04 15:57:38 -0300 | [diff] [blame] | 591 | if (!mi->daddr.ms.map || !mi->iaddr.ms.map) { |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 592 | stats->nomap++; |
| 593 | return -1; |
| 594 | } |
| 595 | |
| 596 | #undef P |
Jiri Olsa | dba8ab9 | 2016-11-21 22:33:29 +0100 | [diff] [blame] | 597 | #undef HITM_INC |
Jiri Olsa | aadddd6 | 2016-09-22 17:36:30 +0200 | [diff] [blame] | 598 | return err; |
| 599 | } |
Jiri Olsa | 0a9a24c | 2016-09-22 17:36:31 +0200 | [diff] [blame] | 600 | |
| 601 | void c2c_add_stats(struct c2c_stats *stats, struct c2c_stats *add) |
| 602 | { |
| 603 | stats->nr_entries += add->nr_entries; |
| 604 | |
| 605 | stats->locks += add->locks; |
| 606 | stats->store += add->store; |
| 607 | stats->st_uncache += add->st_uncache; |
| 608 | stats->st_noadrs += add->st_noadrs; |
| 609 | stats->st_l1hit += add->st_l1hit; |
| 610 | stats->st_l1miss += add->st_l1miss; |
| 611 | stats->load += add->load; |
| 612 | stats->ld_excl += add->ld_excl; |
| 613 | stats->ld_shared += add->ld_shared; |
| 614 | stats->ld_uncache += add->ld_uncache; |
| 615 | stats->ld_io += add->ld_io; |
| 616 | stats->ld_miss += add->ld_miss; |
| 617 | stats->ld_noadrs += add->ld_noadrs; |
| 618 | stats->ld_fbhit += add->ld_fbhit; |
| 619 | stats->ld_l1hit += add->ld_l1hit; |
| 620 | stats->ld_l2hit += add->ld_l2hit; |
| 621 | stats->ld_llchit += add->ld_llchit; |
| 622 | stats->lcl_hitm += add->lcl_hitm; |
| 623 | stats->rmt_hitm += add->rmt_hitm; |
Jiri Olsa | dba8ab9 | 2016-11-21 22:33:29 +0100 | [diff] [blame] | 624 | stats->tot_hitm += add->tot_hitm; |
Jiri Olsa | 0a9a24c | 2016-09-22 17:36:31 +0200 | [diff] [blame] | 625 | stats->rmt_hit += add->rmt_hit; |
| 626 | stats->lcl_dram += add->lcl_dram; |
| 627 | stats->rmt_dram += add->rmt_dram; |
Kan Liang | d9d5d767 | 2021-02-02 12:09:08 -0800 | [diff] [blame] | 628 | stats->blk_data += add->blk_data; |
| 629 | stats->blk_addr += add->blk_addr; |
Jiri Olsa | 0a9a24c | 2016-09-22 17:36:31 +0200 | [diff] [blame] | 630 | stats->nomap += add->nomap; |
| 631 | stats->noparse += add->noparse; |
| 632 | } |