Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <stddef.h>
3#include <stdlib.h>
4#include <string.h>
5#include <errno.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <unistd.h>
9#include <api/fs/fs.h>
10#include <linux/kernel.h>
11#include "map_symbol.h"
12#include "mem-events.h"
13#include "debug.h"
14#include "symbol.h"
15#include "pmu.h"
16#include "pmus.h"
17
18unsigned int perf_mem_events__loads_ldlat = 30;
19
20#define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s }
21
22static struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = {
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"),
25 E(NULL, NULL, NULL),
26};
27#undef E
28
29static char mem_loads_name[100];
30static bool mem_loads_name__init;
31
32struct 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
40const char * __weak perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
41{
42 struct perf_mem_event *e = perf_mem_events__ptr(i);
43
44 if (!e)
45 return NULL;
46
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),
51 e->name, perf_mem_events__loads_ldlat);
52 }
53 return mem_loads_name;
54 }
55
56 return e->name;
57}
58
59__weak bool is_mem_loads_aux_event(struct evsel *leader __maybe_unused)
60{
61 return false;
62}
63
64int 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++) {
82 struct perf_mem_event *e = perf_mem_events__ptr(j);
83
84 if (!e->tag)
85 continue;
86
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}
102
103static bool perf_mem_event__supported(const char *mnt, struct perf_pmu *pmu,
104 struct perf_mem_event *e)
105{
106 char sysfs_name[100];
107 char path[PATH_MAX];
108 struct stat st;
109
110 scnprintf(sysfs_name, sizeof(sysfs_name), e->sysfs_name, pmu->name);
111 scnprintf(path, PATH_MAX, "%s/devices/%s", mnt, sysfs_name);
112 return !stat(path, &st);
113}
114
115int perf_mem_events__init(void)
116{
117 const char *mnt = sysfs__mount();
118 bool found = false;
119 int j;
120
121 if (!mnt)
122 return -ENOENT;
123
124 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
125 struct perf_mem_event *e = perf_mem_events__ptr(j);
126 struct perf_pmu *pmu = NULL;
127
128 /*
129 * If the event entry isn't valid, skip initialization
130 * and "e->supported" will keep false.
131 */
132 if (!e->tag)
133 continue;
134
135 /*
136 * Scan all PMUs not just core ones, since perf mem/c2c on
137 * platforms like AMD uses IBS OP PMU which is independent
138 * of core PMU.
139 */
140 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
141 e->supported |= perf_mem_event__supported(mnt, pmu, e);
142 if (e->supported) {
143 found = true;
144 break;
145 }
146 }
147 }
148
149 return found ? 0 : -ENOENT;
150}
151
152void perf_mem_events__list(void)
153{
154 int j;
155
156 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
157 struct perf_mem_event *e = perf_mem_events__ptr(j);
158
159 fprintf(stderr, "%-*s%-*s%s",
160 e->tag ? 13 : 0,
161 e->tag ? : "",
162 e->tag && verbose > 0 ? 25 : 0,
163 e->tag && verbose > 0 ? perf_mem_events__name(j, NULL) : "",
164 e->supported ? ": available\n" : "");
165 }
166}
167
168static void perf_mem_events__print_unsupport_hybrid(struct perf_mem_event *e,
169 int idx)
170{
171 const char *mnt = sysfs__mount();
172 struct perf_pmu *pmu = NULL;
173
174 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
175 if (!perf_mem_event__supported(mnt, pmu, e)) {
176 pr_err("failed: event '%s' not supported\n",
177 perf_mem_events__name(idx, pmu->name));
178 }
179 }
180}
181
182int perf_mem_events__record_args(const char **rec_argv, int *argv_nr,
183 char **rec_tmp, int *tmp_nr)
184{
185 const char *mnt = sysfs__mount();
186 int i = *argv_nr, k = 0;
187 struct perf_mem_event *e;
188
189 for (int j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
190 e = perf_mem_events__ptr(j);
191 if (!e->record)
192 continue;
193
194 if (perf_pmus__num_mem_pmus() == 1) {
195 if (!e->supported) {
196 pr_err("failed: event '%s' not supported\n",
197 perf_mem_events__name(j, NULL));
198 return -1;
199 }
200
201 rec_argv[i++] = "-e";
202 rec_argv[i++] = perf_mem_events__name(j, NULL);
203 } else {
204 struct perf_pmu *pmu = NULL;
205
206 if (!e->supported) {
207 perf_mem_events__print_unsupport_hybrid(e, j);
208 return -1;
209 }
210
211 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
212 const char *s = perf_mem_events__name(j, pmu->name);
213
214 if (!perf_mem_event__supported(mnt, pmu, e))
215 continue;
216
217 rec_argv[i++] = "-e";
218 if (s) {
219 char *copy = strdup(s);
220 if (!copy)
221 return -1;
222
223 rec_argv[i++] = copy;
224 rec_tmp[k++] = copy;
225 }
226 }
227 }
228 }
229
230 *argv_nr = i;
231 *tmp_nr = k;
232 return 0;
233}
234
235static const char * const tlb_access[] = {
236 "N/A",
237 "HIT",
238 "MISS",
239 "L1",
240 "L2",
241 "Walker",
242 "Fault",
243};
244
245int perf_mem__tlb_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
246{
247 size_t l = 0, i;
248 u64 m = PERF_MEM_TLB_NA;
249 u64 hit, miss;
250
251 sz -= 1; /* -1 for null termination */
252 out[0] = '\0';
253
254 if (mem_info)
255 m = mem_info->data_src.mem_dtlb;
256
257 hit = m & PERF_MEM_TLB_HIT;
258 miss = m & PERF_MEM_TLB_MISS;
259
260 /* already taken care of */
261 m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS);
262
263 for (i = 0; m && i < ARRAY_SIZE(tlb_access); i++, m >>= 1) {
264 if (!(m & 0x1))
265 continue;
266 if (l) {
267 strcat(out, " or ");
268 l += 4;
269 }
270 l += scnprintf(out + l, sz - l, tlb_access[i]);
271 }
272 if (*out == '\0')
273 l += scnprintf(out, sz - l, "N/A");
274 if (hit)
275 l += scnprintf(out + l, sz - l, " hit");
276 if (miss)
277 l += scnprintf(out + l, sz - l, " miss");
278
279 return l;
280}
281
282static const char * const mem_lvl[] = {
283 "N/A",
284 "HIT",
285 "MISS",
286 "L1",
287 "LFB/MAB",
288 "L2",
289 "L3",
290 "Local RAM",
291 "Remote RAM (1 hop)",
292 "Remote RAM (2 hops)",
293 "Remote Cache (1 hop)",
294 "Remote Cache (2 hops)",
295 "I/O",
296 "Uncached",
297};
298
299static const char * const mem_lvlnum[] = {
300 [PERF_MEM_LVLNUM_UNC] = "Uncached",
301 [PERF_MEM_LVLNUM_CXL] = "CXL",
302 [PERF_MEM_LVLNUM_IO] = "I/O",
303 [PERF_MEM_LVLNUM_ANY_CACHE] = "Any cache",
304 [PERF_MEM_LVLNUM_LFB] = "LFB/MAB",
305 [PERF_MEM_LVLNUM_RAM] = "RAM",
306 [PERF_MEM_LVLNUM_PMEM] = "PMEM",
307 [PERF_MEM_LVLNUM_NA] = "N/A",
308};
309
310static const char * const mem_hops[] = {
311 "N/A",
312 /*
313 * While printing, 'Remote' will be added to represent
314 * 'Remote core, same node' accesses as remote field need
315 * to be set with mem_hops field.
316 */
317 "core, same node",
318 "node, same socket",
319 "socket, same board",
320 "board",
321};
322
323static int perf_mem__op_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
324{
325 u64 op = PERF_MEM_LOCK_NA;
326 int l;
327
328 if (mem_info)
329 op = mem_info->data_src.mem_op;
330
331 if (op & PERF_MEM_OP_NA)
332 l = scnprintf(out, sz, "N/A");
333 else if (op & PERF_MEM_OP_LOAD)
334 l = scnprintf(out, sz, "LOAD");
335 else if (op & PERF_MEM_OP_STORE)
336 l = scnprintf(out, sz, "STORE");
337 else if (op & PERF_MEM_OP_PFETCH)
338 l = scnprintf(out, sz, "PFETCH");
339 else if (op & PERF_MEM_OP_EXEC)
340 l = scnprintf(out, sz, "EXEC");
341 else
342 l = scnprintf(out, sz, "No");
343
344 return l;
345}
346
347int perf_mem__lvl_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
348{
349 union perf_mem_data_src data_src;
350 int printed = 0;
351 size_t l = 0;
352 size_t i;
353 int lvl;
354 char hit_miss[5] = {0};
355
356 sz -= 1; /* -1 for null termination */
357 out[0] = '\0';
358
359 if (!mem_info)
360 goto na;
361
362 data_src = mem_info->data_src;
363
364 if (data_src.mem_lvl & PERF_MEM_LVL_HIT)
365 memcpy(hit_miss, "hit", 3);
366 else if (data_src.mem_lvl & PERF_MEM_LVL_MISS)
367 memcpy(hit_miss, "miss", 4);
368
369 lvl = data_src.mem_lvl_num;
370 if (lvl && lvl != PERF_MEM_LVLNUM_NA) {
371 if (data_src.mem_remote) {
372 strcat(out, "Remote ");
373 l += 7;
374 }
375
376 if (data_src.mem_hops)
377 l += scnprintf(out + l, sz - l, "%s ", mem_hops[data_src.mem_hops]);
378
379 if (mem_lvlnum[lvl])
380 l += scnprintf(out + l, sz - l, mem_lvlnum[lvl]);
381 else
382 l += scnprintf(out + l, sz - l, "L%d", lvl);
383
384 l += scnprintf(out + l, sz - l, " %s", hit_miss);
385 return l;
386 }
387
388 lvl = data_src.mem_lvl;
389 if (!lvl)
390 goto na;
391
392 lvl &= ~(PERF_MEM_LVL_NA | PERF_MEM_LVL_HIT | PERF_MEM_LVL_MISS);
393 if (!lvl)
394 goto na;
395
396 for (i = 0; lvl && i < ARRAY_SIZE(mem_lvl); i++, lvl >>= 1) {
397 if (!(lvl & 0x1))
398 continue;
399 if (printed++) {
400 strcat(out, " or ");
401 l += 4;
402 }
403 l += scnprintf(out + l, sz - l, mem_lvl[i]);
404 }
405
406 if (printed) {
407 l += scnprintf(out + l, sz - l, " %s", hit_miss);
408 return l;
409 }
410
411na:
412 strcat(out, "N/A");
413 return 3;
414}
415
416static const char * const snoop_access[] = {
417 "N/A",
418 "None",
419 "Hit",
420 "Miss",
421 "HitM",
422};
423
424static const char * const snoopx_access[] = {
425 "Fwd",
426 "Peer",
427};
428
429int perf_mem__snp_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
430{
431 size_t i, l = 0;
432 u64 m = PERF_MEM_SNOOP_NA;
433
434 sz -= 1; /* -1 for null termination */
435 out[0] = '\0';
436
437 if (mem_info)
438 m = mem_info->data_src.mem_snoop;
439
440 for (i = 0; m && i < ARRAY_SIZE(snoop_access); i++, m >>= 1) {
441 if (!(m & 0x1))
442 continue;
443 if (l) {
444 strcat(out, " or ");
445 l += 4;
446 }
447 l += scnprintf(out + l, sz - l, snoop_access[i]);
448 }
449
450 m = 0;
451 if (mem_info)
452 m = mem_info->data_src.mem_snoopx;
453
454 for (i = 0; m && i < ARRAY_SIZE(snoopx_access); i++, m >>= 1) {
455 if (!(m & 0x1))
456 continue;
457
458 if (l) {
459 strcat(out, " or ");
460 l += 4;
461 }
462 l += scnprintf(out + l, sz - l, snoopx_access[i]);
463 }
464
465 if (*out == '\0')
466 l += scnprintf(out, sz - l, "N/A");
467
468 return l;
469}
470
471int perf_mem__lck_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
472{
473 u64 mask = PERF_MEM_LOCK_NA;
474 int l;
475
476 if (mem_info)
477 mask = mem_info->data_src.mem_lock;
478
479 if (mask & PERF_MEM_LOCK_NA)
480 l = scnprintf(out, sz, "N/A");
481 else if (mask & PERF_MEM_LOCK_LOCKED)
482 l = scnprintf(out, sz, "Yes");
483 else
484 l = scnprintf(out, sz, "No");
485
486 return l;
487}
488
489int perf_mem__blk_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
490{
491 size_t l = 0;
492 u64 mask = PERF_MEM_BLK_NA;
493
494 sz -= 1; /* -1 for null termination */
495 out[0] = '\0';
496
497 if (mem_info)
498 mask = mem_info->data_src.mem_blk;
499
500 if (!mask || (mask & PERF_MEM_BLK_NA)) {
501 l += scnprintf(out + l, sz - l, " N/A");
502 return l;
503 }
504 if (mask & PERF_MEM_BLK_DATA)
505 l += scnprintf(out + l, sz - l, " Data");
506 if (mask & PERF_MEM_BLK_ADDR)
507 l += scnprintf(out + l, sz - l, " Addr");
508
509 return l;
510}
511
512int perf_script__meminfo_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
513{
514 int i = 0;
515
516 i += scnprintf(out, sz, "|OP ");
517 i += perf_mem__op_scnprintf(out + i, sz - i, mem_info);
518 i += scnprintf(out + i, sz - i, "|LVL ");
519 i += perf_mem__lvl_scnprintf(out + i, sz, mem_info);
520 i += scnprintf(out + i, sz - i, "|SNP ");
521 i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info);
522 i += scnprintf(out + i, sz - i, "|TLB ");
523 i += perf_mem__tlb_scnprintf(out + i, sz - i, mem_info);
524 i += scnprintf(out + i, sz - i, "|LCK ");
525 i += perf_mem__lck_scnprintf(out + i, sz - i, mem_info);
526 i += scnprintf(out + i, sz - i, "|BLK ");
527 i += perf_mem__blk_scnprintf(out + i, sz - i, mem_info);
528
529 return i;
530}
531
532int c2c_decode_stats(struct c2c_stats *stats, struct mem_info *mi)
533{
534 union perf_mem_data_src *data_src = &mi->data_src;
535 u64 daddr = mi->daddr.addr;
536 u64 op = data_src->mem_op;
537 u64 lvl = data_src->mem_lvl;
538 u64 snoop = data_src->mem_snoop;
539 u64 snoopx = data_src->mem_snoopx;
540 u64 lock = data_src->mem_lock;
541 u64 blk = data_src->mem_blk;
542 /*
543 * Skylake might report unknown remote level via this
544 * bit, consider it when evaluating remote HITMs.
545 *
546 * Incase of power, remote field can also be used to denote cache
547 * accesses from the another core of same node. Hence, setting
548 * mrem only when HOPS is zero along with set remote field.
549 */
550 bool mrem = (data_src->mem_remote && !data_src->mem_hops);
551 int err = 0;
552
553#define HITM_INC(__f) \
554do { \
555 stats->__f++; \
556 stats->tot_hitm++; \
557} while (0)
558
559#define PEER_INC(__f) \
560do { \
561 stats->__f++; \
562 stats->tot_peer++; \
563} while (0)
564
565#define P(a, b) PERF_MEM_##a##_##b
566
567 stats->nr_entries++;
568
569 if (lock & P(LOCK, LOCKED)) stats->locks++;
570
571 if (blk & P(BLK, DATA)) stats->blk_data++;
572 if (blk & P(BLK, ADDR)) stats->blk_addr++;
573
574 if (op & P(OP, LOAD)) {
575 /* load */
576 stats->load++;
577
578 if (!daddr) {
579 stats->ld_noadrs++;
580 return -1;
581 }
582
583 if (lvl & P(LVL, HIT)) {
584 if (lvl & P(LVL, UNC)) stats->ld_uncache++;
585 if (lvl & P(LVL, IO)) stats->ld_io++;
586 if (lvl & P(LVL, LFB)) stats->ld_fbhit++;
587 if (lvl & P(LVL, L1 )) stats->ld_l1hit++;
588 if (lvl & P(LVL, L2)) {
589 stats->ld_l2hit++;
590
591 if (snoopx & P(SNOOPX, PEER))
592 PEER_INC(lcl_peer);
593 }
594 if (lvl & P(LVL, L3 )) {
595 if (snoop & P(SNOOP, HITM))
596 HITM_INC(lcl_hitm);
597 else
598 stats->ld_llchit++;
599
600 if (snoopx & P(SNOOPX, PEER))
601 PEER_INC(lcl_peer);
602 }
603
604 if (lvl & P(LVL, LOC_RAM)) {
605 stats->lcl_dram++;
606 if (snoop & P(SNOOP, HIT))
607 stats->ld_shared++;
608 else
609 stats->ld_excl++;
610 }
611
612 if ((lvl & P(LVL, REM_RAM1)) ||
613 (lvl & P(LVL, REM_RAM2)) ||
614 mrem) {
615 stats->rmt_dram++;
616 if (snoop & P(SNOOP, HIT))
617 stats->ld_shared++;
618 else
619 stats->ld_excl++;
620 }
621 }
622
623 if ((lvl & P(LVL, REM_CCE1)) ||
624 (lvl & P(LVL, REM_CCE2)) ||
625 mrem) {
626 if (snoop & P(SNOOP, HIT)) {
627 stats->rmt_hit++;
628 } else if (snoop & P(SNOOP, HITM)) {
629 HITM_INC(rmt_hitm);
630 } else if (snoopx & P(SNOOPX, PEER)) {
631 stats->rmt_hit++;
632 PEER_INC(rmt_peer);
633 }
634 }
635
636 if ((lvl & P(LVL, MISS)))
637 stats->ld_miss++;
638
639 } else if (op & P(OP, STORE)) {
640 /* store */
641 stats->store++;
642
643 if (!daddr) {
644 stats->st_noadrs++;
645 return -1;
646 }
647
648 if (lvl & P(LVL, HIT)) {
649 if (lvl & P(LVL, UNC)) stats->st_uncache++;
650 if (lvl & P(LVL, L1 )) stats->st_l1hit++;
651 }
652 if (lvl & P(LVL, MISS))
653 if (lvl & P(LVL, L1)) stats->st_l1miss++;
654 if (lvl & P(LVL, NA))
655 stats->st_na++;
656 } else {
657 /* unparsable data_src? */
658 stats->noparse++;
659 return -1;
660 }
661
662 if (!mi->daddr.ms.map || !mi->iaddr.ms.map) {
663 stats->nomap++;
664 return -1;
665 }
666
667#undef P
668#undef HITM_INC
669 return err;
670}
671
672void c2c_add_stats(struct c2c_stats *stats, struct c2c_stats *add)
673{
674 stats->nr_entries += add->nr_entries;
675
676 stats->locks += add->locks;
677 stats->store += add->store;
678 stats->st_uncache += add->st_uncache;
679 stats->st_noadrs += add->st_noadrs;
680 stats->st_l1hit += add->st_l1hit;
681 stats->st_l1miss += add->st_l1miss;
682 stats->st_na += add->st_na;
683 stats->load += add->load;
684 stats->ld_excl += add->ld_excl;
685 stats->ld_shared += add->ld_shared;
686 stats->ld_uncache += add->ld_uncache;
687 stats->ld_io += add->ld_io;
688 stats->ld_miss += add->ld_miss;
689 stats->ld_noadrs += add->ld_noadrs;
690 stats->ld_fbhit += add->ld_fbhit;
691 stats->ld_l1hit += add->ld_l1hit;
692 stats->ld_l2hit += add->ld_l2hit;
693 stats->ld_llchit += add->ld_llchit;
694 stats->lcl_hitm += add->lcl_hitm;
695 stats->rmt_hitm += add->rmt_hitm;
696 stats->tot_hitm += add->tot_hitm;
697 stats->lcl_peer += add->lcl_peer;
698 stats->rmt_peer += add->rmt_peer;
699 stats->tot_peer += add->tot_peer;
700 stats->rmt_hit += add->rmt_hit;
701 stats->lcl_dram += add->lcl_dram;
702 stats->rmt_dram += add->rmt_dram;
703 stats->blk_data += add->blk_data;
704 stats->blk_addr += add->blk_addr;
705 stats->nomap += add->nomap;
706 stats->noparse += add->noparse;
707}
1// SPDX-License-Identifier: GPL-2.0
2#include <stddef.h>
3#include <stdlib.h>
4#include <string.h>
5#include <errno.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <unistd.h>
9#include <api/fs/fs.h>
10#include <linux/kernel.h>
11#include "cpumap.h"
12#include "map_symbol.h"
13#include "mem-events.h"
14#include "mem-info.h"
15#include "debug.h"
16#include "evsel.h"
17#include "symbol.h"
18#include "pmu.h"
19#include "pmus.h"
20
21unsigned int perf_mem_events__loads_ldlat = 30;
22
23#define E(t, n, s, l, a) { .tag = t, .name = n, .event_name = s, .ldlat = l, .aux_event = a }
24
25struct perf_mem_event perf_mem_events[PERF_MEM_EVENTS__MAX] = {
26 E("ldlat-loads", "%s/mem-loads,ldlat=%u/P", "mem-loads", true, 0),
27 E("ldlat-stores", "%s/mem-stores/P", "mem-stores", false, 0),
28 E(NULL, NULL, NULL, false, 0),
29};
30#undef E
31
32bool perf_mem_record[PERF_MEM_EVENTS__MAX] = { 0 };
33
34static char mem_loads_name[100];
35static char mem_stores_name[100];
36
37struct perf_mem_event *perf_pmu__mem_events_ptr(struct perf_pmu *pmu, int i)
38{
39 if (i >= PERF_MEM_EVENTS__MAX || !pmu)
40 return NULL;
41
42 return &pmu->mem_events[i];
43}
44
45static struct perf_pmu *perf_pmus__scan_mem(struct perf_pmu *pmu)
46{
47 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
48 if (pmu->mem_events)
49 return pmu;
50 }
51 return NULL;
52}
53
54struct perf_pmu *perf_mem_events_find_pmu(void)
55{
56 /*
57 * The current perf mem doesn't support per-PMU configuration.
58 * The exact same configuration is applied to all the
59 * mem_events supported PMUs.
60 * Return the first mem_events supported PMU.
61 *
62 * Notes: The only case which may support multiple mem_events
63 * supported PMUs is Intel hybrid. The exact same mem_events
64 * is shared among the PMUs. Only configure the first PMU
65 * is good enough as well.
66 */
67 return perf_pmus__scan_mem(NULL);
68}
69
70/**
71 * perf_pmu__mem_events_num_mem_pmus - Get the number of mem PMUs since the given pmu
72 * @pmu: Start pmu. If it's NULL, search the entire PMU list.
73 */
74int perf_pmu__mem_events_num_mem_pmus(struct perf_pmu *pmu)
75{
76 int num = 0;
77
78 while ((pmu = perf_pmus__scan_mem(pmu)) != NULL)
79 num++;
80
81 return num;
82}
83
84static const char *perf_pmu__mem_events_name(int i, struct perf_pmu *pmu)
85{
86 struct perf_mem_event *e;
87
88 if (i >= PERF_MEM_EVENTS__MAX || !pmu)
89 return NULL;
90
91 e = &pmu->mem_events[i];
92 if (!e || !e->name)
93 return NULL;
94
95 if (i == PERF_MEM_EVENTS__LOAD || i == PERF_MEM_EVENTS__LOAD_STORE) {
96 if (e->ldlat) {
97 if (!e->aux_event) {
98 /* ARM and Most of Intel */
99 scnprintf(mem_loads_name, sizeof(mem_loads_name),
100 e->name, pmu->name,
101 perf_mem_events__loads_ldlat);
102 } else {
103 /* Intel with mem-loads-aux event */
104 scnprintf(mem_loads_name, sizeof(mem_loads_name),
105 e->name, pmu->name, pmu->name,
106 perf_mem_events__loads_ldlat);
107 }
108 } else {
109 if (!e->aux_event) {
110 /* AMD and POWER */
111 scnprintf(mem_loads_name, sizeof(mem_loads_name),
112 e->name, pmu->name);
113 } else
114 return NULL;
115 }
116
117 return mem_loads_name;
118 }
119
120 if (i == PERF_MEM_EVENTS__STORE) {
121 scnprintf(mem_stores_name, sizeof(mem_stores_name),
122 e->name, pmu->name);
123 return mem_stores_name;
124 }
125
126 return NULL;
127}
128
129bool is_mem_loads_aux_event(struct evsel *leader)
130{
131 struct perf_pmu *pmu = leader->pmu;
132 struct perf_mem_event *e;
133
134 if (!pmu || !pmu->mem_events)
135 return false;
136
137 e = &pmu->mem_events[PERF_MEM_EVENTS__LOAD];
138 if (!e->aux_event)
139 return false;
140
141 return leader->core.attr.config == e->aux_event;
142}
143
144int perf_pmu__mem_events_parse(struct perf_pmu *pmu, const char *str)
145{
146 char *tok, *saveptr = NULL;
147 bool found = false;
148 char *buf;
149 int j;
150
151 /* We need buffer that we know we can write to. */
152 buf = malloc(strlen(str) + 1);
153 if (!buf)
154 return -ENOMEM;
155
156 strcpy(buf, str);
157
158 tok = strtok_r((char *)buf, ",", &saveptr);
159
160 while (tok) {
161 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
162 struct perf_mem_event *e = perf_pmu__mem_events_ptr(pmu, j);
163
164 if (!e->tag)
165 continue;
166
167 if (strstr(e->tag, tok))
168 perf_mem_record[j] = found = true;
169 }
170
171 tok = strtok_r(NULL, ",", &saveptr);
172 }
173
174 free(buf);
175
176 if (found)
177 return 0;
178
179 pr_err("failed: event '%s' not found, use '-e list' to get list of available events\n", str);
180 return -1;
181}
182
183static bool perf_pmu__mem_events_supported(const char *mnt, struct perf_pmu *pmu,
184 struct perf_mem_event *e)
185{
186 char path[PATH_MAX];
187 struct stat st;
188
189 if (!e->event_name)
190 return true;
191
192 scnprintf(path, PATH_MAX, "%s/devices/%s/events/%s", mnt, pmu->name, e->event_name);
193
194 return !stat(path, &st);
195}
196
197static int __perf_pmu__mem_events_init(struct perf_pmu *pmu)
198{
199 const char *mnt = sysfs__mount();
200 bool found = false;
201 int j;
202
203 if (!mnt)
204 return -ENOENT;
205
206 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
207 struct perf_mem_event *e = perf_pmu__mem_events_ptr(pmu, j);
208
209 /*
210 * If the event entry isn't valid, skip initialization
211 * and "e->supported" will keep false.
212 */
213 if (!e->tag)
214 continue;
215
216 e->supported |= perf_pmu__mem_events_supported(mnt, pmu, e);
217 if (e->supported)
218 found = true;
219 }
220
221 return found ? 0 : -ENOENT;
222}
223
224int perf_pmu__mem_events_init(void)
225{
226 struct perf_pmu *pmu = NULL;
227
228 while ((pmu = perf_pmus__scan_mem(pmu)) != NULL) {
229 if (__perf_pmu__mem_events_init(pmu))
230 return -ENOENT;
231 }
232
233 return 0;
234}
235
236void perf_pmu__mem_events_list(struct perf_pmu *pmu)
237{
238 int j;
239
240 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
241 struct perf_mem_event *e = perf_pmu__mem_events_ptr(pmu, j);
242
243 fprintf(stderr, "%-*s%-*s%s",
244 e->tag ? 13 : 0,
245 e->tag ? : "",
246 e->tag && verbose > 0 ? 25 : 0,
247 e->tag && verbose > 0 ? perf_pmu__mem_events_name(j, pmu) : "",
248 e->supported ? ": available\n" : "");
249 }
250}
251
252int perf_mem_events__record_args(const char **rec_argv, int *argv_nr)
253{
254 const char *mnt = sysfs__mount();
255 struct perf_pmu *pmu = NULL;
256 struct perf_mem_event *e;
257 int i = *argv_nr;
258 const char *s;
259 char *copy;
260 struct perf_cpu_map *cpu_map = NULL;
261
262 while ((pmu = perf_pmus__scan_mem(pmu)) != NULL) {
263 for (int j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
264 e = perf_pmu__mem_events_ptr(pmu, j);
265
266 if (!perf_mem_record[j])
267 continue;
268
269 if (!e->supported) {
270 pr_err("failed: event '%s' not supported\n",
271 perf_pmu__mem_events_name(j, pmu));
272 return -1;
273 }
274
275 s = perf_pmu__mem_events_name(j, pmu);
276 if (!s || !perf_pmu__mem_events_supported(mnt, pmu, e))
277 continue;
278
279 copy = strdup(s);
280 if (!copy)
281 return -1;
282
283 rec_argv[i++] = "-e";
284 rec_argv[i++] = copy;
285
286 cpu_map = perf_cpu_map__merge(cpu_map, pmu->cpus);
287 }
288 }
289
290 if (cpu_map) {
291 if (!perf_cpu_map__equal(cpu_map, cpu_map__online())) {
292 char buf[200];
293
294 cpu_map__snprint(cpu_map, buf, sizeof(buf));
295 pr_warning("Memory events are enabled on a subset of CPUs: %s\n", buf);
296 }
297 perf_cpu_map__put(cpu_map);
298 }
299
300 *argv_nr = i;
301 return 0;
302}
303
304static const char * const tlb_access[] = {
305 "N/A",
306 "HIT",
307 "MISS",
308 "L1",
309 "L2",
310 "Walker",
311 "Fault",
312};
313
314int perf_mem__tlb_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
315{
316 size_t l = 0, i;
317 u64 m = PERF_MEM_TLB_NA;
318 u64 hit, miss;
319
320 sz -= 1; /* -1 for null termination */
321 out[0] = '\0';
322
323 if (mem_info)
324 m = mem_info__const_data_src(mem_info)->mem_dtlb;
325
326 hit = m & PERF_MEM_TLB_HIT;
327 miss = m & PERF_MEM_TLB_MISS;
328
329 /* already taken care of */
330 m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS);
331
332 for (i = 0; m && i < ARRAY_SIZE(tlb_access); i++, m >>= 1) {
333 if (!(m & 0x1))
334 continue;
335 if (l) {
336 strcat(out, " or ");
337 l += 4;
338 }
339 l += scnprintf(out + l, sz - l, tlb_access[i]);
340 }
341 if (*out == '\0')
342 l += scnprintf(out, sz - l, "N/A");
343 if (hit)
344 l += scnprintf(out + l, sz - l, " hit");
345 if (miss)
346 l += scnprintf(out + l, sz - l, " miss");
347
348 return l;
349}
350
351static const char * const mem_lvl[] = {
352 "N/A",
353 "HIT",
354 "MISS",
355 "L1",
356 "LFB/MAB",
357 "L2",
358 "L3",
359 "Local RAM",
360 "Remote RAM (1 hop)",
361 "Remote RAM (2 hops)",
362 "Remote Cache (1 hop)",
363 "Remote Cache (2 hops)",
364 "I/O",
365 "Uncached",
366};
367
368static const char * const mem_lvlnum[] = {
369 [PERF_MEM_LVLNUM_L1] = "L1",
370 [PERF_MEM_LVLNUM_L2] = "L2",
371 [PERF_MEM_LVLNUM_L3] = "L3",
372 [PERF_MEM_LVLNUM_L4] = "L4",
373 [PERF_MEM_LVLNUM_L2_MHB] = "L2 MHB",
374 [PERF_MEM_LVLNUM_MSC] = "Memory-side Cache",
375 [PERF_MEM_LVLNUM_UNC] = "Uncached",
376 [PERF_MEM_LVLNUM_CXL] = "CXL",
377 [PERF_MEM_LVLNUM_IO] = "I/O",
378 [PERF_MEM_LVLNUM_ANY_CACHE] = "Any cache",
379 [PERF_MEM_LVLNUM_LFB] = "LFB/MAB",
380 [PERF_MEM_LVLNUM_RAM] = "RAM",
381 [PERF_MEM_LVLNUM_PMEM] = "PMEM",
382 [PERF_MEM_LVLNUM_NA] = "N/A",
383};
384
385static const char * const mem_hops[] = {
386 "N/A",
387 /*
388 * While printing, 'Remote' will be added to represent
389 * 'Remote core, same node' accesses as remote field need
390 * to be set with mem_hops field.
391 */
392 "core, same node",
393 "node, same socket",
394 "socket, same board",
395 "board",
396};
397
398static int perf_mem__op_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
399{
400 u64 op = PERF_MEM_LOCK_NA;
401 int l;
402
403 if (mem_info)
404 op = mem_info__const_data_src(mem_info)->mem_op;
405
406 if (op & PERF_MEM_OP_NA)
407 l = scnprintf(out, sz, "N/A");
408 else if (op & PERF_MEM_OP_LOAD)
409 l = scnprintf(out, sz, "LOAD");
410 else if (op & PERF_MEM_OP_STORE)
411 l = scnprintf(out, sz, "STORE");
412 else if (op & PERF_MEM_OP_PFETCH)
413 l = scnprintf(out, sz, "PFETCH");
414 else if (op & PERF_MEM_OP_EXEC)
415 l = scnprintf(out, sz, "EXEC");
416 else
417 l = scnprintf(out, sz, "No");
418
419 return l;
420}
421
422int perf_mem__lvl_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
423{
424 union perf_mem_data_src data_src;
425 int printed = 0;
426 size_t l = 0;
427 size_t i;
428 int lvl;
429 char hit_miss[5] = {0};
430
431 sz -= 1; /* -1 for null termination */
432 out[0] = '\0';
433
434 if (!mem_info)
435 goto na;
436
437 data_src = *mem_info__const_data_src(mem_info);
438
439 if (data_src.mem_lvl & PERF_MEM_LVL_HIT)
440 memcpy(hit_miss, "hit", 3);
441 else if (data_src.mem_lvl & PERF_MEM_LVL_MISS)
442 memcpy(hit_miss, "miss", 4);
443
444 lvl = data_src.mem_lvl_num;
445 if (lvl && lvl != PERF_MEM_LVLNUM_NA) {
446 if (data_src.mem_remote) {
447 strcat(out, "Remote ");
448 l += 7;
449 }
450
451 if (data_src.mem_hops)
452 l += scnprintf(out + l, sz - l, "%s ", mem_hops[data_src.mem_hops]);
453
454 if (mem_lvlnum[lvl])
455 l += scnprintf(out + l, sz - l, mem_lvlnum[lvl]);
456 else
457 l += scnprintf(out + l, sz - l, "Unknown level %d", lvl);
458
459 l += scnprintf(out + l, sz - l, " %s", hit_miss);
460 return l;
461 }
462
463 lvl = data_src.mem_lvl;
464 if (!lvl)
465 goto na;
466
467 lvl &= ~(PERF_MEM_LVL_NA | PERF_MEM_LVL_HIT | PERF_MEM_LVL_MISS);
468 if (!lvl)
469 goto na;
470
471 for (i = 0; lvl && i < ARRAY_SIZE(mem_lvl); i++, lvl >>= 1) {
472 if (!(lvl & 0x1))
473 continue;
474 if (printed++) {
475 strcat(out, " or ");
476 l += 4;
477 }
478 l += scnprintf(out + l, sz - l, mem_lvl[i]);
479 }
480
481 if (printed) {
482 l += scnprintf(out + l, sz - l, " %s", hit_miss);
483 return l;
484 }
485
486na:
487 strcat(out, "N/A");
488 return 3;
489}
490
491static const char * const snoop_access[] = {
492 "N/A",
493 "None",
494 "Hit",
495 "Miss",
496 "HitM",
497};
498
499static const char * const snoopx_access[] = {
500 "Fwd",
501 "Peer",
502};
503
504int perf_mem__snp_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
505{
506 size_t i, l = 0;
507 u64 m = PERF_MEM_SNOOP_NA;
508
509 sz -= 1; /* -1 for null termination */
510 out[0] = '\0';
511
512 if (mem_info)
513 m = mem_info__const_data_src(mem_info)->mem_snoop;
514
515 for (i = 0; m && i < ARRAY_SIZE(snoop_access); i++, m >>= 1) {
516 if (!(m & 0x1))
517 continue;
518 if (l) {
519 strcat(out, " or ");
520 l += 4;
521 }
522 l += scnprintf(out + l, sz - l, snoop_access[i]);
523 }
524
525 m = 0;
526 if (mem_info)
527 m = mem_info__const_data_src(mem_info)->mem_snoopx;
528
529 for (i = 0; m && i < ARRAY_SIZE(snoopx_access); i++, m >>= 1) {
530 if (!(m & 0x1))
531 continue;
532
533 if (l) {
534 strcat(out, " or ");
535 l += 4;
536 }
537 l += scnprintf(out + l, sz - l, snoopx_access[i]);
538 }
539
540 if (*out == '\0')
541 l += scnprintf(out, sz - l, "N/A");
542
543 return l;
544}
545
546int perf_mem__lck_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
547{
548 u64 mask = PERF_MEM_LOCK_NA;
549 int l;
550
551 if (mem_info)
552 mask = mem_info__const_data_src(mem_info)->mem_lock;
553
554 if (mask & PERF_MEM_LOCK_NA)
555 l = scnprintf(out, sz, "N/A");
556 else if (mask & PERF_MEM_LOCK_LOCKED)
557 l = scnprintf(out, sz, "Yes");
558 else
559 l = scnprintf(out, sz, "No");
560
561 return l;
562}
563
564int perf_mem__blk_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
565{
566 size_t l = 0;
567 u64 mask = PERF_MEM_BLK_NA;
568
569 sz -= 1; /* -1 for null termination */
570 out[0] = '\0';
571
572 if (mem_info)
573 mask = mem_info__const_data_src(mem_info)->mem_blk;
574
575 if (!mask || (mask & PERF_MEM_BLK_NA)) {
576 l += scnprintf(out + l, sz - l, " N/A");
577 return l;
578 }
579 if (mask & PERF_MEM_BLK_DATA)
580 l += scnprintf(out + l, sz - l, " Data");
581 if (mask & PERF_MEM_BLK_ADDR)
582 l += scnprintf(out + l, sz - l, " Addr");
583
584 return l;
585}
586
587int perf_script__meminfo_scnprintf(char *out, size_t sz, const struct mem_info *mem_info)
588{
589 int i = 0;
590
591 i += scnprintf(out, sz, "|OP ");
592 i += perf_mem__op_scnprintf(out + i, sz - i, mem_info);
593 i += scnprintf(out + i, sz - i, "|LVL ");
594 i += perf_mem__lvl_scnprintf(out + i, sz, mem_info);
595 i += scnprintf(out + i, sz - i, "|SNP ");
596 i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info);
597 i += scnprintf(out + i, sz - i, "|TLB ");
598 i += perf_mem__tlb_scnprintf(out + i, sz - i, mem_info);
599 i += scnprintf(out + i, sz - i, "|LCK ");
600 i += perf_mem__lck_scnprintf(out + i, sz - i, mem_info);
601 i += scnprintf(out + i, sz - i, "|BLK ");
602 i += perf_mem__blk_scnprintf(out + i, sz - i, mem_info);
603
604 return i;
605}
606
607int c2c_decode_stats(struct c2c_stats *stats, struct mem_info *mi)
608{
609 union perf_mem_data_src *data_src = mem_info__data_src(mi);
610 u64 daddr = mem_info__daddr(mi)->addr;
611 u64 op = data_src->mem_op;
612 u64 lvl = data_src->mem_lvl;
613 u64 snoop = data_src->mem_snoop;
614 u64 snoopx = data_src->mem_snoopx;
615 u64 lock = data_src->mem_lock;
616 u64 blk = data_src->mem_blk;
617 /*
618 * Skylake might report unknown remote level via this
619 * bit, consider it when evaluating remote HITMs.
620 *
621 * Incase of power, remote field can also be used to denote cache
622 * accesses from the another core of same node. Hence, setting
623 * mrem only when HOPS is zero along with set remote field.
624 */
625 bool mrem = (data_src->mem_remote && !data_src->mem_hops);
626 int err = 0;
627
628#define HITM_INC(__f) \
629do { \
630 stats->__f++; \
631 stats->tot_hitm++; \
632} while (0)
633
634#define PEER_INC(__f) \
635do { \
636 stats->__f++; \
637 stats->tot_peer++; \
638} while (0)
639
640#define P(a, b) PERF_MEM_##a##_##b
641
642 stats->nr_entries++;
643
644 if (lock & P(LOCK, LOCKED)) stats->locks++;
645
646 if (blk & P(BLK, DATA)) stats->blk_data++;
647 if (blk & P(BLK, ADDR)) stats->blk_addr++;
648
649 if (op & P(OP, LOAD)) {
650 /* load */
651 stats->load++;
652
653 if (!daddr) {
654 stats->ld_noadrs++;
655 return -1;
656 }
657
658 if (lvl & P(LVL, HIT)) {
659 if (lvl & P(LVL, UNC)) stats->ld_uncache++;
660 if (lvl & P(LVL, IO)) stats->ld_io++;
661 if (lvl & P(LVL, LFB)) stats->ld_fbhit++;
662 if (lvl & P(LVL, L1 )) stats->ld_l1hit++;
663 if (lvl & P(LVL, L2)) {
664 stats->ld_l2hit++;
665
666 if (snoopx & P(SNOOPX, PEER))
667 PEER_INC(lcl_peer);
668 }
669 if (lvl & P(LVL, L3 )) {
670 if (snoop & P(SNOOP, HITM))
671 HITM_INC(lcl_hitm);
672 else
673 stats->ld_llchit++;
674
675 if (snoopx & P(SNOOPX, PEER))
676 PEER_INC(lcl_peer);
677 }
678
679 if (lvl & P(LVL, LOC_RAM)) {
680 stats->lcl_dram++;
681 if (snoop & P(SNOOP, HIT))
682 stats->ld_shared++;
683 else
684 stats->ld_excl++;
685 }
686
687 if ((lvl & P(LVL, REM_RAM1)) ||
688 (lvl & P(LVL, REM_RAM2)) ||
689 mrem) {
690 stats->rmt_dram++;
691 if (snoop & P(SNOOP, HIT))
692 stats->ld_shared++;
693 else
694 stats->ld_excl++;
695 }
696 }
697
698 if ((lvl & P(LVL, REM_CCE1)) ||
699 (lvl & P(LVL, REM_CCE2)) ||
700 mrem) {
701 if (snoop & P(SNOOP, HIT)) {
702 stats->rmt_hit++;
703 } else if (snoop & P(SNOOP, HITM)) {
704 HITM_INC(rmt_hitm);
705 } else if (snoopx & P(SNOOPX, PEER)) {
706 stats->rmt_hit++;
707 PEER_INC(rmt_peer);
708 }
709 }
710
711 if ((lvl & P(LVL, MISS)))
712 stats->ld_miss++;
713
714 } else if (op & P(OP, STORE)) {
715 /* store */
716 stats->store++;
717
718 if (!daddr) {
719 stats->st_noadrs++;
720 return -1;
721 }
722
723 if (lvl & P(LVL, HIT)) {
724 if (lvl & P(LVL, UNC)) stats->st_uncache++;
725 if (lvl & P(LVL, L1 )) stats->st_l1hit++;
726 }
727 if (lvl & P(LVL, MISS))
728 if (lvl & P(LVL, L1)) stats->st_l1miss++;
729 if (lvl & P(LVL, NA))
730 stats->st_na++;
731 } else {
732 /* unparsable data_src? */
733 stats->noparse++;
734 return -1;
735 }
736
737 if (!mem_info__daddr(mi)->ms.map || !mem_info__iaddr(mi)->ms.map) {
738 stats->nomap++;
739 return -1;
740 }
741
742#undef P
743#undef HITM_INC
744 return err;
745}
746
747void c2c_add_stats(struct c2c_stats *stats, struct c2c_stats *add)
748{
749 stats->nr_entries += add->nr_entries;
750
751 stats->locks += add->locks;
752 stats->store += add->store;
753 stats->st_uncache += add->st_uncache;
754 stats->st_noadrs += add->st_noadrs;
755 stats->st_l1hit += add->st_l1hit;
756 stats->st_l1miss += add->st_l1miss;
757 stats->st_na += add->st_na;
758 stats->load += add->load;
759 stats->ld_excl += add->ld_excl;
760 stats->ld_shared += add->ld_shared;
761 stats->ld_uncache += add->ld_uncache;
762 stats->ld_io += add->ld_io;
763 stats->ld_miss += add->ld_miss;
764 stats->ld_noadrs += add->ld_noadrs;
765 stats->ld_fbhit += add->ld_fbhit;
766 stats->ld_l1hit += add->ld_l1hit;
767 stats->ld_l2hit += add->ld_l2hit;
768 stats->ld_llchit += add->ld_llchit;
769 stats->lcl_hitm += add->lcl_hitm;
770 stats->rmt_hitm += add->rmt_hitm;
771 stats->tot_hitm += add->tot_hitm;
772 stats->lcl_peer += add->lcl_peer;
773 stats->rmt_peer += add->rmt_peer;
774 stats->tot_peer += add->tot_peer;
775 stats->rmt_hit += add->rmt_hit;
776 stats->lcl_dram += add->lcl_dram;
777 stats->rmt_dram += add->rmt_dram;
778 stats->blk_data += add->blk_data;
779 stats->blk_addr += add->blk_addr;
780 stats->nomap += add->nomap;
781 stats->noparse += add->noparse;
782}