1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/kernel.h>
11 #include "map_symbol.h"
12 #include "mem-events.h"
18 unsigned int perf_mem_events__loads_ldlat = 30;
20 #define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s }
22 static 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"),
29 static char mem_loads_name[100];
30 static bool mem_loads_name__init;
32 struct perf_mem_event * __weak perf_mem_events__ptr(int i)
34 if (i >= PERF_MEM_EVENTS__MAX)
37 return &perf_mem_events[i];
40 const char * __weak perf_mem_events__name(int i, const char *pmu_name __maybe_unused)
42 struct perf_mem_event *e = perf_mem_events__ptr(i);
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);
53 return mem_loads_name;
59 __weak bool is_mem_loads_aux_event(struct evsel *leader __maybe_unused)
64 int perf_mem_events__parse(const char *str)
66 char *tok, *saveptr = NULL;
71 /* We need buffer that we know we can write to. */
72 buf = malloc(strlen(str) + 1);
78 tok = strtok_r((char *)buf, ",", &saveptr);
81 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
82 struct perf_mem_event *e = perf_mem_events__ptr(j);
87 if (strstr(e->tag, tok))
88 e->record = found = true;
91 tok = strtok_r(NULL, ",", &saveptr);
99 pr_err("failed: event '%s' not found, use '-e list' to get list of available events\n", str);
103 static bool perf_mem_event__supported(const char *mnt, char *sysfs_name)
108 scnprintf(path, PATH_MAX, "%s/devices/%s", mnt, sysfs_name);
109 return !stat(path, &st);
112 int perf_mem_events__init(void)
114 const char *mnt = sysfs__mount();
121 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
122 struct perf_mem_event *e = perf_mem_events__ptr(j);
123 char sysfs_name[100];
124 struct perf_pmu *pmu = NULL;
127 * If the event entry isn't valid, skip initialization
128 * and "e->supported" will keep false.
134 * Scan all PMUs not just core ones, since perf mem/c2c on
135 * platforms like AMD uses IBS OP PMU which is independent
138 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
139 scnprintf(sysfs_name, sizeof(sysfs_name), e->sysfs_name, pmu->name);
140 e->supported |= perf_mem_event__supported(mnt, sysfs_name);
147 return found ? 0 : -ENOENT;
150 void perf_mem_events__list(void)
154 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
155 struct perf_mem_event *e = perf_mem_events__ptr(j);
157 fprintf(stderr, "%-*s%-*s%s",
160 e->tag && verbose > 0 ? 25 : 0,
161 e->tag && verbose > 0 ? perf_mem_events__name(j, NULL) : "",
162 e->supported ? ": available\n" : "");
166 static void perf_mem_events__print_unsupport_hybrid(struct perf_mem_event *e,
169 const char *mnt = sysfs__mount();
170 char sysfs_name[100];
171 struct perf_pmu *pmu = NULL;
173 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
174 scnprintf(sysfs_name, sizeof(sysfs_name), e->sysfs_name,
176 if (!perf_mem_event__supported(mnt, sysfs_name)) {
177 pr_err("failed: event '%s' not supported\n",
178 perf_mem_events__name(idx, pmu->name));
183 int perf_mem_events__record_args(const char **rec_argv, int *argv_nr,
184 char **rec_tmp, int *tmp_nr)
186 int i = *argv_nr, k = 0;
187 struct perf_mem_event *e;
188 struct perf_pmu *pmu;
190 for (int j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
191 e = perf_mem_events__ptr(j);
195 if (perf_pmus__num_mem_pmus() == 1) {
197 pr_err("failed: event '%s' not supported\n",
198 perf_mem_events__name(j, NULL));
202 rec_argv[i++] = "-e";
203 rec_argv[i++] = perf_mem_events__name(j, NULL);
206 perf_mem_events__print_unsupport_hybrid(e, j);
210 while ((pmu = perf_pmus__scan(pmu)) != NULL) {
211 const char *s = perf_mem_events__name(j, pmu->name);
213 rec_argv[i++] = "-e";
215 char *copy = strdup(s);
219 rec_argv[i++] = copy;
231 static const char * const tlb_access[] = {
241 int perf_mem__tlb_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
244 u64 m = PERF_MEM_TLB_NA;
247 sz -= 1; /* -1 for null termination */
251 m = mem_info->data_src.mem_dtlb;
253 hit = m & PERF_MEM_TLB_HIT;
254 miss = m & PERF_MEM_TLB_MISS;
256 /* already taken care of */
257 m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS);
259 for (i = 0; m && i < ARRAY_SIZE(tlb_access); i++, m >>= 1) {
266 l += scnprintf(out + l, sz - l, tlb_access[i]);
269 l += scnprintf(out, sz - l, "N/A");
271 l += scnprintf(out + l, sz - l, " hit");
273 l += scnprintf(out + l, sz - l, " miss");
278 static const char * const mem_lvl[] = {
287 "Remote RAM (1 hop)",
288 "Remote RAM (2 hops)",
289 "Remote Cache (1 hop)",
290 "Remote Cache (2 hops)",
295 static const char * const mem_lvlnum[] = {
296 [PERF_MEM_LVLNUM_UNC] = "Uncached",
297 [PERF_MEM_LVLNUM_CXL] = "CXL",
298 [PERF_MEM_LVLNUM_IO] = "I/O",
299 [PERF_MEM_LVLNUM_ANY_CACHE] = "Any cache",
300 [PERF_MEM_LVLNUM_LFB] = "LFB/MAB",
301 [PERF_MEM_LVLNUM_RAM] = "RAM",
302 [PERF_MEM_LVLNUM_PMEM] = "PMEM",
303 [PERF_MEM_LVLNUM_NA] = "N/A",
306 static const char * const mem_hops[] = {
309 * While printing, 'Remote' will be added to represent
310 * 'Remote core, same node' accesses as remote field need
311 * to be set with mem_hops field.
315 "socket, same board",
319 static int perf_mem__op_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
321 u64 op = PERF_MEM_LOCK_NA;
325 op = mem_info->data_src.mem_op;
327 if (op & PERF_MEM_OP_NA)
328 l = scnprintf(out, sz, "N/A");
329 else if (op & PERF_MEM_OP_LOAD)
330 l = scnprintf(out, sz, "LOAD");
331 else if (op & PERF_MEM_OP_STORE)
332 l = scnprintf(out, sz, "STORE");
333 else if (op & PERF_MEM_OP_PFETCH)
334 l = scnprintf(out, sz, "PFETCH");
335 else if (op & PERF_MEM_OP_EXEC)
336 l = scnprintf(out, sz, "EXEC");
338 l = scnprintf(out, sz, "No");
343 int perf_mem__lvl_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
345 union perf_mem_data_src data_src;
350 char hit_miss[5] = {0};
352 sz -= 1; /* -1 for null termination */
358 data_src = mem_info->data_src;
360 if (data_src.mem_lvl & PERF_MEM_LVL_HIT)
361 memcpy(hit_miss, "hit", 3);
362 else if (data_src.mem_lvl & PERF_MEM_LVL_MISS)
363 memcpy(hit_miss, "miss", 4);
365 lvl = data_src.mem_lvl_num;
366 if (lvl && lvl != PERF_MEM_LVLNUM_NA) {
367 if (data_src.mem_remote) {
368 strcat(out, "Remote ");
372 if (data_src.mem_hops)
373 l += scnprintf(out + l, sz - l, "%s ", mem_hops[data_src.mem_hops]);
376 l += scnprintf(out + l, sz - l, mem_lvlnum[lvl]);
378 l += scnprintf(out + l, sz - l, "L%d", lvl);
380 l += scnprintf(out + l, sz - l, " %s", hit_miss);
384 lvl = data_src.mem_lvl;
388 lvl &= ~(PERF_MEM_LVL_NA | PERF_MEM_LVL_HIT | PERF_MEM_LVL_MISS);
392 for (i = 0; lvl && i < ARRAY_SIZE(mem_lvl); i++, lvl >>= 1) {
399 l += scnprintf(out + l, sz - l, mem_lvl[i]);
403 l += scnprintf(out + l, sz - l, " %s", hit_miss);
412 static const char * const snoop_access[] = {
420 static const char * const snoopx_access[] = {
425 int perf_mem__snp_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
428 u64 m = PERF_MEM_SNOOP_NA;
430 sz -= 1; /* -1 for null termination */
434 m = mem_info->data_src.mem_snoop;
436 for (i = 0; m && i < ARRAY_SIZE(snoop_access); i++, m >>= 1) {
443 l += scnprintf(out + l, sz - l, snoop_access[i]);
448 m = mem_info->data_src.mem_snoopx;
450 for (i = 0; m && i < ARRAY_SIZE(snoopx_access); i++, m >>= 1) {
458 l += scnprintf(out + l, sz - l, snoopx_access[i]);
462 l += scnprintf(out, sz - l, "N/A");
467 int perf_mem__lck_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
469 u64 mask = PERF_MEM_LOCK_NA;
473 mask = mem_info->data_src.mem_lock;
475 if (mask & PERF_MEM_LOCK_NA)
476 l = scnprintf(out, sz, "N/A");
477 else if (mask & PERF_MEM_LOCK_LOCKED)
478 l = scnprintf(out, sz, "Yes");
480 l = scnprintf(out, sz, "No");
485 int perf_mem__blk_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
488 u64 mask = PERF_MEM_BLK_NA;
490 sz -= 1; /* -1 for null termination */
494 mask = mem_info->data_src.mem_blk;
496 if (!mask || (mask & PERF_MEM_BLK_NA)) {
497 l += scnprintf(out + l, sz - l, " N/A");
500 if (mask & PERF_MEM_BLK_DATA)
501 l += scnprintf(out + l, sz - l, " Data");
502 if (mask & PERF_MEM_BLK_ADDR)
503 l += scnprintf(out + l, sz - l, " Addr");
508 int perf_script__meminfo_scnprintf(char *out, size_t sz, struct mem_info *mem_info)
512 i += scnprintf(out, sz, "|OP ");
513 i += perf_mem__op_scnprintf(out + i, sz - i, mem_info);
514 i += scnprintf(out + i, sz - i, "|LVL ");
515 i += perf_mem__lvl_scnprintf(out + i, sz, mem_info);
516 i += scnprintf(out + i, sz - i, "|SNP ");
517 i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info);
518 i += scnprintf(out + i, sz - i, "|TLB ");
519 i += perf_mem__tlb_scnprintf(out + i, sz - i, mem_info);
520 i += scnprintf(out + i, sz - i, "|LCK ");
521 i += perf_mem__lck_scnprintf(out + i, sz - i, mem_info);
522 i += scnprintf(out + i, sz - i, "|BLK ");
523 i += perf_mem__blk_scnprintf(out + i, sz - i, mem_info);
528 int c2c_decode_stats(struct c2c_stats *stats, struct mem_info *mi)
530 union perf_mem_data_src *data_src = &mi->data_src;
531 u64 daddr = mi->daddr.addr;
532 u64 op = data_src->mem_op;
533 u64 lvl = data_src->mem_lvl;
534 u64 snoop = data_src->mem_snoop;
535 u64 snoopx = data_src->mem_snoopx;
536 u64 lock = data_src->mem_lock;
537 u64 blk = data_src->mem_blk;
539 * Skylake might report unknown remote level via this
540 * bit, consider it when evaluating remote HITMs.
542 * Incase of power, remote field can also be used to denote cache
543 * accesses from the another core of same node. Hence, setting
544 * mrem only when HOPS is zero along with set remote field.
546 bool mrem = (data_src->mem_remote && !data_src->mem_hops);
549 #define HITM_INC(__f) \
555 #define PEER_INC(__f) \
561 #define P(a, b) PERF_MEM_##a##_##b
565 if (lock & P(LOCK, LOCKED)) stats->locks++;
567 if (blk & P(BLK, DATA)) stats->blk_data++;
568 if (blk & P(BLK, ADDR)) stats->blk_addr++;
570 if (op & P(OP, LOAD)) {
579 if (lvl & P(LVL, HIT)) {
580 if (lvl & P(LVL, UNC)) stats->ld_uncache++;
581 if (lvl & P(LVL, IO)) stats->ld_io++;
582 if (lvl & P(LVL, LFB)) stats->ld_fbhit++;
583 if (lvl & P(LVL, L1 )) stats->ld_l1hit++;
584 if (lvl & P(LVL, L2)) {
587 if (snoopx & P(SNOOPX, PEER))
590 if (lvl & P(LVL, L3 )) {
591 if (snoop & P(SNOOP, HITM))
596 if (snoopx & P(SNOOPX, PEER))
600 if (lvl & P(LVL, LOC_RAM)) {
602 if (snoop & P(SNOOP, HIT))
608 if ((lvl & P(LVL, REM_RAM1)) ||
609 (lvl & P(LVL, REM_RAM2)) ||
612 if (snoop & P(SNOOP, HIT))
619 if ((lvl & P(LVL, REM_CCE1)) ||
620 (lvl & P(LVL, REM_CCE2)) ||
622 if (snoop & P(SNOOP, HIT)) {
624 } else if (snoop & P(SNOOP, HITM)) {
626 } else if (snoopx & P(SNOOPX, PEER)) {
632 if ((lvl & P(LVL, MISS)))
635 } else if (op & P(OP, STORE)) {
644 if (lvl & P(LVL, HIT)) {
645 if (lvl & P(LVL, UNC)) stats->st_uncache++;
646 if (lvl & P(LVL, L1 )) stats->st_l1hit++;
648 if (lvl & P(LVL, MISS))
649 if (lvl & P(LVL, L1)) stats->st_l1miss++;
650 if (lvl & P(LVL, NA))
653 /* unparsable data_src? */
658 if (!mi->daddr.ms.map || !mi->iaddr.ms.map) {
668 void c2c_add_stats(struct c2c_stats *stats, struct c2c_stats *add)
670 stats->nr_entries += add->nr_entries;
672 stats->locks += add->locks;
673 stats->store += add->store;
674 stats->st_uncache += add->st_uncache;
675 stats->st_noadrs += add->st_noadrs;
676 stats->st_l1hit += add->st_l1hit;
677 stats->st_l1miss += add->st_l1miss;
678 stats->st_na += add->st_na;
679 stats->load += add->load;
680 stats->ld_excl += add->ld_excl;
681 stats->ld_shared += add->ld_shared;
682 stats->ld_uncache += add->ld_uncache;
683 stats->ld_io += add->ld_io;
684 stats->ld_miss += add->ld_miss;
685 stats->ld_noadrs += add->ld_noadrs;
686 stats->ld_fbhit += add->ld_fbhit;
687 stats->ld_l1hit += add->ld_l1hit;
688 stats->ld_l2hit += add->ld_l2hit;
689 stats->ld_llchit += add->ld_llchit;
690 stats->lcl_hitm += add->lcl_hitm;
691 stats->rmt_hitm += add->rmt_hitm;
692 stats->tot_hitm += add->tot_hitm;
693 stats->lcl_peer += add->lcl_peer;
694 stats->rmt_peer += add->rmt_peer;
695 stats->tot_peer += add->tot_peer;
696 stats->rmt_hit += add->rmt_hit;
697 stats->lcl_dram += add->lcl_dram;
698 stats->rmt_dram += add->rmt_dram;
699 stats->blk_data += add->blk_data;
700 stats->blk_addr += add->blk_addr;
701 stats->nomap += add->nomap;
702 stats->noparse += add->noparse;