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 "pmu-hybrid.h" 17 18 unsigned int perf_mem_events__loads_ldlat = 30; 19 20 #define E(t, n, s) { .tag = t, .name = n, .sysfs_name = s } 21 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"), 25 E(NULL, NULL, NULL), 26 }; 27 #undef E 28 29 static char mem_loads_name[100]; 30 static bool mem_loads_name__init; 31 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 40 char * __weak perf_mem_events__name(int i, 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 (char *)e->name; 57 } 58 59 __weak bool is_mem_loads_aux_event(struct evsel *leader __maybe_unused) 60 { 61 return false; 62 } 63 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++) { 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 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 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++) { 122 struct perf_mem_event *e = perf_mem_events__ptr(j); 123 struct perf_pmu *pmu; 124 char sysfs_name[100]; 125 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 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 } 144 145 if (e->supported) 146 found = true; 147 } 148 149 return found ? 0 : -ENOENT; 150 } 151 152 void 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 168 static void perf_mem_events__print_unsupport_hybrid(struct perf_mem_event *e, 169 int idx) 170 { 171 const char *mnt = sysfs__mount(); 172 char sysfs_name[100]; 173 struct perf_pmu *pmu; 174 175 perf_pmu__for_each_hybrid_pmu(pmu) { 176 scnprintf(sysfs_name, sizeof(sysfs_name), e->sysfs_name, 177 pmu->name); 178 if (!perf_mem_event__supported(mnt, sysfs_name)) { 179 pr_err("failed: event '%s' not supported\n", 180 perf_mem_events__name(idx, pmu->name)); 181 } 182 } 183 } 184 185 int perf_mem_events__record_args(const char **rec_argv, int *argv_nr, 186 char **rec_tmp, int *tmp_nr) 187 { 188 int i = *argv_nr, k = 0; 189 struct perf_mem_event *e; 190 struct perf_pmu *pmu; 191 char *s; 192 193 for (int j = 0; j < PERF_MEM_EVENTS__MAX; j++) { 194 e = perf_mem_events__ptr(j); 195 if (!e->record) 196 continue; 197 198 if (!perf_pmu__has_hybrid()) { 199 if (!e->supported) { 200 pr_err("failed: event '%s' not supported\n", 201 perf_mem_events__name(j, NULL)); 202 return -1; 203 } 204 205 rec_argv[i++] = "-e"; 206 rec_argv[i++] = perf_mem_events__name(j, NULL); 207 } else { 208 if (!e->supported) { 209 perf_mem_events__print_unsupport_hybrid(e, j); 210 return -1; 211 } 212 213 perf_pmu__for_each_hybrid_pmu(pmu) { 214 rec_argv[i++] = "-e"; 215 s = perf_mem_events__name(j, pmu->name); 216 if (s) { 217 s = strdup(s); 218 if (!s) 219 return -1; 220 221 rec_argv[i++] = s; 222 rec_tmp[k++] = s; 223 } 224 } 225 } 226 } 227 228 *argv_nr = i; 229 *tmp_nr = k; 230 return 0; 231 } 232 233 static const char * const tlb_access[] = { 234 "N/A", 235 "HIT", 236 "MISS", 237 "L1", 238 "L2", 239 "Walker", 240 "Fault", 241 }; 242 243 int perf_mem__tlb_scnprintf(char *out, size_t sz, struct mem_info *mem_info) 244 { 245 size_t l = 0, i; 246 u64 m = PERF_MEM_TLB_NA; 247 u64 hit, miss; 248 249 sz -= 1; /* -1 for null termination */ 250 out[0] = '\0'; 251 252 if (mem_info) 253 m = mem_info->data_src.mem_dtlb; 254 255 hit = m & PERF_MEM_TLB_HIT; 256 miss = m & PERF_MEM_TLB_MISS; 257 258 /* already taken care of */ 259 m &= ~(PERF_MEM_TLB_HIT|PERF_MEM_TLB_MISS); 260 261 for (i = 0; m && i < ARRAY_SIZE(tlb_access); i++, m >>= 1) { 262 if (!(m & 0x1)) 263 continue; 264 if (l) { 265 strcat(out, " or "); 266 l += 4; 267 } 268 l += scnprintf(out + l, sz - l, tlb_access[i]); 269 } 270 if (*out == '\0') 271 l += scnprintf(out, sz - l, "N/A"); 272 if (hit) 273 l += scnprintf(out + l, sz - l, " hit"); 274 if (miss) 275 l += scnprintf(out + l, sz - l, " miss"); 276 277 return l; 278 } 279 280 static const char * const mem_lvl[] = { 281 "N/A", 282 "HIT", 283 "MISS", 284 "L1", 285 "LFB/MAB", 286 "L2", 287 "L3", 288 "Local RAM", 289 "Remote RAM (1 hop)", 290 "Remote RAM (2 hops)", 291 "Remote Cache (1 hop)", 292 "Remote Cache (2 hops)", 293 "I/O", 294 "Uncached", 295 }; 296 297 static const char * const mem_lvlnum[] = { 298 [PERF_MEM_LVLNUM_UNC] = "Uncached", 299 [PERF_MEM_LVLNUM_CXL] = "CXL", 300 [PERF_MEM_LVLNUM_IO] = "I/O", 301 [PERF_MEM_LVLNUM_ANY_CACHE] = "Any cache", 302 [PERF_MEM_LVLNUM_LFB] = "LFB/MAB", 303 [PERF_MEM_LVLNUM_RAM] = "RAM", 304 [PERF_MEM_LVLNUM_PMEM] = "PMEM", 305 [PERF_MEM_LVLNUM_NA] = "N/A", 306 }; 307 308 static const char * const mem_hops[] = { 309 "N/A", 310 /* 311 * While printing, 'Remote' will be added to represent 312 * 'Remote core, same node' accesses as remote field need 313 * to be set with mem_hops field. 314 */ 315 "core, same node", 316 "node, same socket", 317 "socket, same board", 318 "board", 319 }; 320 321 static int perf_mem__op_scnprintf(char *out, size_t sz, struct mem_info *mem_info) 322 { 323 u64 op = PERF_MEM_LOCK_NA; 324 int l; 325 326 if (mem_info) 327 op = mem_info->data_src.mem_op; 328 329 if (op & PERF_MEM_OP_NA) 330 l = scnprintf(out, sz, "N/A"); 331 else if (op & PERF_MEM_OP_LOAD) 332 l = scnprintf(out, sz, "LOAD"); 333 else if (op & PERF_MEM_OP_STORE) 334 l = scnprintf(out, sz, "STORE"); 335 else if (op & PERF_MEM_OP_PFETCH) 336 l = scnprintf(out, sz, "PFETCH"); 337 else if (op & PERF_MEM_OP_EXEC) 338 l = scnprintf(out, sz, "EXEC"); 339 else 340 l = scnprintf(out, sz, "No"); 341 342 return l; 343 } 344 345 int perf_mem__lvl_scnprintf(char *out, size_t sz, struct mem_info *mem_info) 346 { 347 union perf_mem_data_src data_src; 348 int printed = 0; 349 size_t l = 0; 350 size_t i; 351 int lvl; 352 char hit_miss[5] = {0}; 353 354 sz -= 1; /* -1 for null termination */ 355 out[0] = '\0'; 356 357 if (!mem_info) 358 goto na; 359 360 data_src = mem_info->data_src; 361 362 if (data_src.mem_lvl & PERF_MEM_LVL_HIT) 363 memcpy(hit_miss, "hit", 3); 364 else if (data_src.mem_lvl & PERF_MEM_LVL_MISS) 365 memcpy(hit_miss, "miss", 4); 366 367 lvl = data_src.mem_lvl_num; 368 if (lvl && lvl != PERF_MEM_LVLNUM_NA) { 369 if (data_src.mem_remote) { 370 strcat(out, "Remote "); 371 l += 7; 372 } 373 374 if (data_src.mem_hops) 375 l += scnprintf(out + l, sz - l, "%s ", mem_hops[data_src.mem_hops]); 376 377 if (mem_lvlnum[lvl]) 378 l += scnprintf(out + l, sz - l, mem_lvlnum[lvl]); 379 else 380 l += scnprintf(out + l, sz - l, "L%d", lvl); 381 382 l += scnprintf(out + l, sz - l, " %s", hit_miss); 383 return l; 384 } 385 386 lvl = data_src.mem_lvl; 387 if (!lvl) 388 goto na; 389 390 lvl &= ~(PERF_MEM_LVL_NA | PERF_MEM_LVL_HIT | PERF_MEM_LVL_MISS); 391 if (!lvl) 392 goto na; 393 394 for (i = 0; lvl && i < ARRAY_SIZE(mem_lvl); i++, lvl >>= 1) { 395 if (!(lvl & 0x1)) 396 continue; 397 if (printed++) { 398 strcat(out, " or "); 399 l += 4; 400 } 401 l += scnprintf(out + l, sz - l, mem_lvl[i]); 402 } 403 404 if (printed) { 405 l += scnprintf(out + l, sz - l, " %s", hit_miss); 406 return l; 407 } 408 409 na: 410 strcat(out, "N/A"); 411 return 3; 412 } 413 414 static const char * const snoop_access[] = { 415 "N/A", 416 "None", 417 "Hit", 418 "Miss", 419 "HitM", 420 }; 421 422 static const char * const snoopx_access[] = { 423 "Fwd", 424 "Peer", 425 }; 426 427 int perf_mem__snp_scnprintf(char *out, size_t sz, struct mem_info *mem_info) 428 { 429 size_t i, l = 0; 430 u64 m = PERF_MEM_SNOOP_NA; 431 432 sz -= 1; /* -1 for null termination */ 433 out[0] = '\0'; 434 435 if (mem_info) 436 m = mem_info->data_src.mem_snoop; 437 438 for (i = 0; m && i < ARRAY_SIZE(snoop_access); i++, m >>= 1) { 439 if (!(m & 0x1)) 440 continue; 441 if (l) { 442 strcat(out, " or "); 443 l += 4; 444 } 445 l += scnprintf(out + l, sz - l, snoop_access[i]); 446 } 447 448 m = 0; 449 if (mem_info) 450 m = mem_info->data_src.mem_snoopx; 451 452 for (i = 0; m && i < ARRAY_SIZE(snoopx_access); i++, m >>= 1) { 453 if (!(m & 0x1)) 454 continue; 455 456 if (l) { 457 strcat(out, " or "); 458 l += 4; 459 } 460 l += scnprintf(out + l, sz - l, snoopx_access[i]); 461 } 462 463 if (*out == '\0') 464 l += scnprintf(out, sz - l, "N/A"); 465 466 return l; 467 } 468 469 int perf_mem__lck_scnprintf(char *out, size_t sz, struct mem_info *mem_info) 470 { 471 u64 mask = PERF_MEM_LOCK_NA; 472 int l; 473 474 if (mem_info) 475 mask = mem_info->data_src.mem_lock; 476 477 if (mask & PERF_MEM_LOCK_NA) 478 l = scnprintf(out, sz, "N/A"); 479 else if (mask & PERF_MEM_LOCK_LOCKED) 480 l = scnprintf(out, sz, "Yes"); 481 else 482 l = scnprintf(out, sz, "No"); 483 484 return l; 485 } 486 487 int perf_mem__blk_scnprintf(char *out, size_t sz, struct mem_info *mem_info) 488 { 489 size_t l = 0; 490 u64 mask = PERF_MEM_BLK_NA; 491 492 sz -= 1; /* -1 for null termination */ 493 out[0] = '\0'; 494 495 if (mem_info) 496 mask = mem_info->data_src.mem_blk; 497 498 if (!mask || (mask & PERF_MEM_BLK_NA)) { 499 l += scnprintf(out + l, sz - l, " N/A"); 500 return l; 501 } 502 if (mask & PERF_MEM_BLK_DATA) 503 l += scnprintf(out + l, sz - l, " Data"); 504 if (mask & PERF_MEM_BLK_ADDR) 505 l += scnprintf(out + l, sz - l, " Addr"); 506 507 return l; 508 } 509 510 int perf_script__meminfo_scnprintf(char *out, size_t sz, struct mem_info *mem_info) 511 { 512 int i = 0; 513 514 i += scnprintf(out, sz, "|OP "); 515 i += perf_mem__op_scnprintf(out + i, sz - i, mem_info); 516 i += scnprintf(out + i, sz - i, "|LVL "); 517 i += perf_mem__lvl_scnprintf(out + i, sz, mem_info); 518 i += scnprintf(out + i, sz - i, "|SNP "); 519 i += perf_mem__snp_scnprintf(out + i, sz - i, mem_info); 520 i += scnprintf(out + i, sz - i, "|TLB "); 521 i += perf_mem__tlb_scnprintf(out + i, sz - i, mem_info); 522 i += scnprintf(out + i, sz - i, "|LCK "); 523 i += perf_mem__lck_scnprintf(out + i, sz - i, mem_info); 524 i += scnprintf(out + i, sz - i, "|BLK "); 525 i += perf_mem__blk_scnprintf(out + i, sz - i, mem_info); 526 527 return i; 528 } 529 530 int c2c_decode_stats(struct c2c_stats *stats, struct mem_info *mi) 531 { 532 union perf_mem_data_src *data_src = &mi->data_src; 533 u64 daddr = mi->daddr.addr; 534 u64 op = data_src->mem_op; 535 u64 lvl = data_src->mem_lvl; 536 u64 snoop = data_src->mem_snoop; 537 u64 snoopx = data_src->mem_snoopx; 538 u64 lock = data_src->mem_lock; 539 u64 blk = data_src->mem_blk; 540 /* 541 * Skylake might report unknown remote level via this 542 * bit, consider it when evaluating remote HITMs. 543 * 544 * Incase of power, remote field can also be used to denote cache 545 * accesses from the another core of same node. Hence, setting 546 * mrem only when HOPS is zero along with set remote field. 547 */ 548 bool mrem = (data_src->mem_remote && !data_src->mem_hops); 549 int err = 0; 550 551 #define HITM_INC(__f) \ 552 do { \ 553 stats->__f++; \ 554 stats->tot_hitm++; \ 555 } while (0) 556 557 #define PEER_INC(__f) \ 558 do { \ 559 stats->__f++; \ 560 stats->tot_peer++; \ 561 } while (0) 562 563 #define P(a, b) PERF_MEM_##a##_##b 564 565 stats->nr_entries++; 566 567 if (lock & P(LOCK, LOCKED)) stats->locks++; 568 569 if (blk & P(BLK, DATA)) stats->blk_data++; 570 if (blk & P(BLK, ADDR)) stats->blk_addr++; 571 572 if (op & P(OP, LOAD)) { 573 /* load */ 574 stats->load++; 575 576 if (!daddr) { 577 stats->ld_noadrs++; 578 return -1; 579 } 580 581 if (lvl & P(LVL, HIT)) { 582 if (lvl & P(LVL, UNC)) stats->ld_uncache++; 583 if (lvl & P(LVL, IO)) stats->ld_io++; 584 if (lvl & P(LVL, LFB)) stats->ld_fbhit++; 585 if (lvl & P(LVL, L1 )) stats->ld_l1hit++; 586 if (lvl & P(LVL, L2)) { 587 stats->ld_l2hit++; 588 589 if (snoopx & P(SNOOPX, PEER)) 590 PEER_INC(lcl_peer); 591 } 592 if (lvl & P(LVL, L3 )) { 593 if (snoop & P(SNOOP, HITM)) 594 HITM_INC(lcl_hitm); 595 else 596 stats->ld_llchit++; 597 598 if (snoopx & P(SNOOPX, PEER)) 599 PEER_INC(lcl_peer); 600 } 601 602 if (lvl & P(LVL, LOC_RAM)) { 603 stats->lcl_dram++; 604 if (snoop & P(SNOOP, HIT)) 605 stats->ld_shared++; 606 else 607 stats->ld_excl++; 608 } 609 610 if ((lvl & P(LVL, REM_RAM1)) || 611 (lvl & P(LVL, REM_RAM2)) || 612 mrem) { 613 stats->rmt_dram++; 614 if (snoop & P(SNOOP, HIT)) 615 stats->ld_shared++; 616 else 617 stats->ld_excl++; 618 } 619 } 620 621 if ((lvl & P(LVL, REM_CCE1)) || 622 (lvl & P(LVL, REM_CCE2)) || 623 mrem) { 624 if (snoop & P(SNOOP, HIT)) { 625 stats->rmt_hit++; 626 } else if (snoop & P(SNOOP, HITM)) { 627 HITM_INC(rmt_hitm); 628 } else if (snoopx & P(SNOOPX, PEER)) { 629 stats->rmt_hit++; 630 PEER_INC(rmt_peer); 631 } 632 } 633 634 if ((lvl & P(LVL, MISS))) 635 stats->ld_miss++; 636 637 } else if (op & P(OP, STORE)) { 638 /* store */ 639 stats->store++; 640 641 if (!daddr) { 642 stats->st_noadrs++; 643 return -1; 644 } 645 646 if (lvl & P(LVL, HIT)) { 647 if (lvl & P(LVL, UNC)) stats->st_uncache++; 648 if (lvl & P(LVL, L1 )) stats->st_l1hit++; 649 } 650 if (lvl & P(LVL, MISS)) 651 if (lvl & P(LVL, L1)) stats->st_l1miss++; 652 if (lvl & P(LVL, NA)) 653 stats->st_na++; 654 } else { 655 /* unparsable data_src? */ 656 stats->noparse++; 657 return -1; 658 } 659 660 if (!mi->daddr.ms.map || !mi->iaddr.ms.map) { 661 stats->nomap++; 662 return -1; 663 } 664 665 #undef P 666 #undef HITM_INC 667 return err; 668 } 669 670 void c2c_add_stats(struct c2c_stats *stats, struct c2c_stats *add) 671 { 672 stats->nr_entries += add->nr_entries; 673 674 stats->locks += add->locks; 675 stats->store += add->store; 676 stats->st_uncache += add->st_uncache; 677 stats->st_noadrs += add->st_noadrs; 678 stats->st_l1hit += add->st_l1hit; 679 stats->st_l1miss += add->st_l1miss; 680 stats->st_na += add->st_na; 681 stats->load += add->load; 682 stats->ld_excl += add->ld_excl; 683 stats->ld_shared += add->ld_shared; 684 stats->ld_uncache += add->ld_uncache; 685 stats->ld_io += add->ld_io; 686 stats->ld_miss += add->ld_miss; 687 stats->ld_noadrs += add->ld_noadrs; 688 stats->ld_fbhit += add->ld_fbhit; 689 stats->ld_l1hit += add->ld_l1hit; 690 stats->ld_l2hit += add->ld_l2hit; 691 stats->ld_llchit += add->ld_llchit; 692 stats->lcl_hitm += add->lcl_hitm; 693 stats->rmt_hitm += add->rmt_hitm; 694 stats->tot_hitm += add->tot_hitm; 695 stats->lcl_peer += add->lcl_peer; 696 stats->rmt_peer += add->rmt_peer; 697 stats->tot_peer += add->tot_peer; 698 stats->rmt_hit += add->rmt_hit; 699 stats->lcl_dram += add->lcl_dram; 700 stats->rmt_dram += add->rmt_dram; 701 stats->blk_data += add->blk_data; 702 stats->blk_addr += add->blk_addr; 703 stats->nomap += add->nomap; 704 stats->noparse += add->noparse; 705 } 706