1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * This is rewrite of original c2c tool introduced in here: 4 * http://lwn.net/Articles/588866/ 5 * 6 * The original tool was changed to fit in current perf state. 7 * 8 * Original authors: 9 * Don Zickus <dzickus@redhat.com> 10 * Dick Fowles <fowles@inreach.com> 11 * Joe Mario <jmario@redhat.com> 12 */ 13 #include <errno.h> 14 #include <inttypes.h> 15 #include <linux/compiler.h> 16 #include <linux/kernel.h> 17 #include <linux/stringify.h> 18 #include <asm/bug.h> 19 #include <sys/param.h> 20 #include "util.h" 21 #include "debug.h" 22 #include "builtin.h" 23 #include <subcmd/parse-options.h> 24 #include "mem-events.h" 25 #include "session.h" 26 #include "hist.h" 27 #include "sort.h" 28 #include "tool.h" 29 #include "data.h" 30 #include "event.h" 31 #include "evlist.h" 32 #include "evsel.h" 33 #include "ui/browsers/hists.h" 34 #include "thread.h" 35 #include "mem2node.h" 36 #include "symbol.h" 37 38 struct c2c_hists { 39 struct hists hists; 40 struct perf_hpp_list list; 41 struct c2c_stats stats; 42 }; 43 44 struct compute_stats { 45 struct stats lcl_hitm; 46 struct stats rmt_hitm; 47 struct stats load; 48 }; 49 50 struct c2c_hist_entry { 51 struct c2c_hists *hists; 52 struct c2c_stats stats; 53 unsigned long *cpuset; 54 unsigned long *nodeset; 55 struct c2c_stats *node_stats; 56 unsigned int cacheline_idx; 57 58 struct compute_stats cstats; 59 60 unsigned long paddr; 61 unsigned long paddr_cnt; 62 bool paddr_zero; 63 char *nodestr; 64 65 /* 66 * must be at the end, 67 * because of its callchain dynamic entry 68 */ 69 struct hist_entry he; 70 }; 71 72 static char const *coalesce_default = "iaddr"; 73 74 struct perf_c2c { 75 struct perf_tool tool; 76 struct c2c_hists hists; 77 struct mem2node mem2node; 78 79 unsigned long **nodes; 80 int nodes_cnt; 81 int cpus_cnt; 82 int *cpu2node; 83 int node_info; 84 85 bool show_src; 86 bool show_all; 87 bool use_stdio; 88 bool stats_only; 89 bool symbol_full; 90 91 /* HITM shared clines stats */ 92 struct c2c_stats hitm_stats; 93 int shared_clines; 94 95 int display; 96 97 const char *coalesce; 98 char *cl_sort; 99 char *cl_resort; 100 char *cl_output; 101 }; 102 103 enum { 104 DISPLAY_LCL, 105 DISPLAY_RMT, 106 DISPLAY_TOT, 107 DISPLAY_MAX, 108 }; 109 110 static const char *display_str[DISPLAY_MAX] = { 111 [DISPLAY_LCL] = "Local", 112 [DISPLAY_RMT] = "Remote", 113 [DISPLAY_TOT] = "Total", 114 }; 115 116 static const struct option c2c_options[] = { 117 OPT_INCR('v', "verbose", &verbose, "be more verbose (show counter open errors, etc)"), 118 OPT_END() 119 }; 120 121 static struct perf_c2c c2c; 122 123 static void *c2c_he_zalloc(size_t size) 124 { 125 struct c2c_hist_entry *c2c_he; 126 127 c2c_he = zalloc(size + sizeof(*c2c_he)); 128 if (!c2c_he) 129 return NULL; 130 131 c2c_he->cpuset = bitmap_alloc(c2c.cpus_cnt); 132 if (!c2c_he->cpuset) 133 return NULL; 134 135 c2c_he->nodeset = bitmap_alloc(c2c.nodes_cnt); 136 if (!c2c_he->nodeset) 137 return NULL; 138 139 c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats)); 140 if (!c2c_he->node_stats) 141 return NULL; 142 143 init_stats(&c2c_he->cstats.lcl_hitm); 144 init_stats(&c2c_he->cstats.rmt_hitm); 145 init_stats(&c2c_he->cstats.load); 146 147 return &c2c_he->he; 148 } 149 150 static void c2c_he_free(void *he) 151 { 152 struct c2c_hist_entry *c2c_he; 153 154 c2c_he = container_of(he, struct c2c_hist_entry, he); 155 if (c2c_he->hists) { 156 hists__delete_entries(&c2c_he->hists->hists); 157 free(c2c_he->hists); 158 } 159 160 free(c2c_he->cpuset); 161 free(c2c_he->nodeset); 162 free(c2c_he->nodestr); 163 free(c2c_he->node_stats); 164 free(c2c_he); 165 } 166 167 static struct hist_entry_ops c2c_entry_ops = { 168 .new = c2c_he_zalloc, 169 .free = c2c_he_free, 170 }; 171 172 static int c2c_hists__init(struct c2c_hists *hists, 173 const char *sort, 174 int nr_header_lines); 175 176 static struct c2c_hists* 177 he__get_c2c_hists(struct hist_entry *he, 178 const char *sort, 179 int nr_header_lines) 180 { 181 struct c2c_hist_entry *c2c_he; 182 struct c2c_hists *hists; 183 int ret; 184 185 c2c_he = container_of(he, struct c2c_hist_entry, he); 186 if (c2c_he->hists) 187 return c2c_he->hists; 188 189 hists = c2c_he->hists = zalloc(sizeof(*hists)); 190 if (!hists) 191 return NULL; 192 193 ret = c2c_hists__init(hists, sort, nr_header_lines); 194 if (ret) { 195 free(hists); 196 return NULL; 197 } 198 199 return hists; 200 } 201 202 static void c2c_he__set_cpu(struct c2c_hist_entry *c2c_he, 203 struct perf_sample *sample) 204 { 205 if (WARN_ONCE(sample->cpu == (unsigned int) -1, 206 "WARNING: no sample cpu value")) 207 return; 208 209 set_bit(sample->cpu, c2c_he->cpuset); 210 } 211 212 static void c2c_he__set_node(struct c2c_hist_entry *c2c_he, 213 struct perf_sample *sample) 214 { 215 int node; 216 217 if (!sample->phys_addr) { 218 c2c_he->paddr_zero = true; 219 return; 220 } 221 222 node = mem2node__node(&c2c.mem2node, sample->phys_addr); 223 if (WARN_ONCE(node < 0, "WARNING: failed to find node\n")) 224 return; 225 226 set_bit(node, c2c_he->nodeset); 227 228 if (c2c_he->paddr != sample->phys_addr) { 229 c2c_he->paddr_cnt++; 230 c2c_he->paddr = sample->phys_addr; 231 } 232 } 233 234 static void compute_stats(struct c2c_hist_entry *c2c_he, 235 struct c2c_stats *stats, 236 u64 weight) 237 { 238 struct compute_stats *cstats = &c2c_he->cstats; 239 240 if (stats->rmt_hitm) 241 update_stats(&cstats->rmt_hitm, weight); 242 else if (stats->lcl_hitm) 243 update_stats(&cstats->lcl_hitm, weight); 244 else if (stats->load) 245 update_stats(&cstats->load, weight); 246 } 247 248 static int process_sample_event(struct perf_tool *tool __maybe_unused, 249 union perf_event *event, 250 struct perf_sample *sample, 251 struct perf_evsel *evsel, 252 struct machine *machine) 253 { 254 struct c2c_hists *c2c_hists = &c2c.hists; 255 struct c2c_hist_entry *c2c_he; 256 struct c2c_stats stats = { .nr_entries = 0, }; 257 struct hist_entry *he; 258 struct addr_location al; 259 struct mem_info *mi, *mi_dup; 260 int ret; 261 262 if (machine__resolve(machine, &al, sample) < 0) { 263 pr_debug("problem processing %d event, skipping it.\n", 264 event->header.type); 265 return -1; 266 } 267 268 ret = sample__resolve_callchain(sample, &callchain_cursor, NULL, 269 evsel, &al, sysctl_perf_event_max_stack); 270 if (ret) 271 goto out; 272 273 mi = sample__resolve_mem(sample, &al); 274 if (mi == NULL) 275 return -ENOMEM; 276 277 /* 278 * The mi object is released in hists__add_entry_ops, 279 * if it gets sorted out into existing data, so we need 280 * to take the copy now. 281 */ 282 mi_dup = mem_info__get(mi); 283 284 c2c_decode_stats(&stats, mi); 285 286 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops, 287 &al, NULL, NULL, mi, 288 sample, true); 289 if (he == NULL) 290 goto free_mi; 291 292 c2c_he = container_of(he, struct c2c_hist_entry, he); 293 c2c_add_stats(&c2c_he->stats, &stats); 294 c2c_add_stats(&c2c_hists->stats, &stats); 295 296 c2c_he__set_cpu(c2c_he, sample); 297 c2c_he__set_node(c2c_he, sample); 298 299 hists__inc_nr_samples(&c2c_hists->hists, he->filtered); 300 ret = hist_entry__append_callchain(he, sample); 301 302 if (!ret) { 303 /* 304 * There's already been warning about missing 305 * sample's cpu value. Let's account all to 306 * node 0 in this case, without any further 307 * warning. 308 * 309 * Doing node stats only for single callchain data. 310 */ 311 int cpu = sample->cpu == (unsigned int) -1 ? 0 : sample->cpu; 312 int node = c2c.cpu2node[cpu]; 313 314 mi = mi_dup; 315 316 c2c_hists = he__get_c2c_hists(he, c2c.cl_sort, 2); 317 if (!c2c_hists) 318 goto free_mi; 319 320 he = hists__add_entry_ops(&c2c_hists->hists, &c2c_entry_ops, 321 &al, NULL, NULL, mi, 322 sample, true); 323 if (he == NULL) 324 goto free_mi; 325 326 c2c_he = container_of(he, struct c2c_hist_entry, he); 327 c2c_add_stats(&c2c_he->stats, &stats); 328 c2c_add_stats(&c2c_hists->stats, &stats); 329 c2c_add_stats(&c2c_he->node_stats[node], &stats); 330 331 compute_stats(c2c_he, &stats, sample->weight); 332 333 c2c_he__set_cpu(c2c_he, sample); 334 c2c_he__set_node(c2c_he, sample); 335 336 hists__inc_nr_samples(&c2c_hists->hists, he->filtered); 337 ret = hist_entry__append_callchain(he, sample); 338 } 339 340 out: 341 addr_location__put(&al); 342 return ret; 343 344 free_mi: 345 mem_info__put(mi_dup); 346 mem_info__put(mi); 347 ret = -ENOMEM; 348 goto out; 349 } 350 351 static struct perf_c2c c2c = { 352 .tool = { 353 .sample = process_sample_event, 354 .mmap = perf_event__process_mmap, 355 .mmap2 = perf_event__process_mmap2, 356 .comm = perf_event__process_comm, 357 .exit = perf_event__process_exit, 358 .fork = perf_event__process_fork, 359 .lost = perf_event__process_lost, 360 .ordered_events = true, 361 .ordering_requires_timestamps = true, 362 }, 363 }; 364 365 static const char * const c2c_usage[] = { 366 "perf c2c {record|report}", 367 NULL 368 }; 369 370 static const char * const __usage_report[] = { 371 "perf c2c report", 372 NULL 373 }; 374 375 static const char * const *report_c2c_usage = __usage_report; 376 377 #define C2C_HEADER_MAX 2 378 379 struct c2c_header { 380 struct { 381 const char *text; 382 int span; 383 } line[C2C_HEADER_MAX]; 384 }; 385 386 struct c2c_dimension { 387 struct c2c_header header; 388 const char *name; 389 int width; 390 struct sort_entry *se; 391 392 int64_t (*cmp)(struct perf_hpp_fmt *fmt, 393 struct hist_entry *, struct hist_entry *); 394 int (*entry)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 395 struct hist_entry *he); 396 int (*color)(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 397 struct hist_entry *he); 398 }; 399 400 struct c2c_fmt { 401 struct perf_hpp_fmt fmt; 402 struct c2c_dimension *dim; 403 }; 404 405 #define SYMBOL_WIDTH 30 406 407 static struct c2c_dimension dim_symbol; 408 static struct c2c_dimension dim_srcline; 409 410 static int symbol_width(struct hists *hists, struct sort_entry *se) 411 { 412 int width = hists__col_len(hists, se->se_width_idx); 413 414 if (!c2c.symbol_full) 415 width = MIN(width, SYMBOL_WIDTH); 416 417 return width; 418 } 419 420 static int c2c_width(struct perf_hpp_fmt *fmt, 421 struct perf_hpp *hpp __maybe_unused, 422 struct hists *hists) 423 { 424 struct c2c_fmt *c2c_fmt; 425 struct c2c_dimension *dim; 426 427 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt); 428 dim = c2c_fmt->dim; 429 430 if (dim == &dim_symbol || dim == &dim_srcline) 431 return symbol_width(hists, dim->se); 432 433 return dim->se ? hists__col_len(hists, dim->se->se_width_idx) : 434 c2c_fmt->dim->width; 435 } 436 437 static int c2c_header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 438 struct hists *hists, int line, int *span) 439 { 440 struct perf_hpp_list *hpp_list = hists->hpp_list; 441 struct c2c_fmt *c2c_fmt; 442 struct c2c_dimension *dim; 443 const char *text = NULL; 444 int width = c2c_width(fmt, hpp, hists); 445 446 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt); 447 dim = c2c_fmt->dim; 448 449 if (dim->se) { 450 text = dim->header.line[line].text; 451 /* Use the last line from sort_entry if not defined. */ 452 if (!text && (line == hpp_list->nr_header_lines - 1)) 453 text = dim->se->se_header; 454 } else { 455 text = dim->header.line[line].text; 456 457 if (*span) { 458 (*span)--; 459 return 0; 460 } else { 461 *span = dim->header.line[line].span; 462 } 463 } 464 465 if (text == NULL) 466 text = ""; 467 468 return scnprintf(hpp->buf, hpp->size, "%*s", width, text); 469 } 470 471 #define HEX_STR(__s, __v) \ 472 ({ \ 473 scnprintf(__s, sizeof(__s), "0x%" PRIx64, __v); \ 474 __s; \ 475 }) 476 477 static int64_t 478 dcacheline_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 479 struct hist_entry *left, struct hist_entry *right) 480 { 481 return sort__dcacheline_cmp(left, right); 482 } 483 484 static int dcacheline_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 485 struct hist_entry *he) 486 { 487 uint64_t addr = 0; 488 int width = c2c_width(fmt, hpp, he->hists); 489 char buf[20]; 490 491 if (he->mem_info) 492 addr = cl_address(he->mem_info->daddr.addr); 493 494 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr)); 495 } 496 497 static int 498 dcacheline_node_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 499 struct hist_entry *he) 500 { 501 struct c2c_hist_entry *c2c_he; 502 int width = c2c_width(fmt, hpp, he->hists); 503 504 c2c_he = container_of(he, struct c2c_hist_entry, he); 505 if (WARN_ON_ONCE(!c2c_he->nodestr)) 506 return 0; 507 508 return scnprintf(hpp->buf, hpp->size, "%*s", width, c2c_he->nodestr); 509 } 510 511 static int 512 dcacheline_node_count(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 513 struct hist_entry *he) 514 { 515 struct c2c_hist_entry *c2c_he; 516 int width = c2c_width(fmt, hpp, he->hists); 517 518 c2c_he = container_of(he, struct c2c_hist_entry, he); 519 return scnprintf(hpp->buf, hpp->size, "%*lu", width, c2c_he->paddr_cnt); 520 } 521 522 static int offset_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 523 struct hist_entry *he) 524 { 525 uint64_t addr = 0; 526 int width = c2c_width(fmt, hpp, he->hists); 527 char buf[20]; 528 529 if (he->mem_info) 530 addr = cl_offset(he->mem_info->daddr.al_addr); 531 532 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr)); 533 } 534 535 static int64_t 536 offset_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 537 struct hist_entry *left, struct hist_entry *right) 538 { 539 uint64_t l = 0, r = 0; 540 541 if (left->mem_info) 542 l = cl_offset(left->mem_info->daddr.addr); 543 if (right->mem_info) 544 r = cl_offset(right->mem_info->daddr.addr); 545 546 return (int64_t)(r - l); 547 } 548 549 static int 550 iaddr_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 551 struct hist_entry *he) 552 { 553 uint64_t addr = 0; 554 int width = c2c_width(fmt, hpp, he->hists); 555 char buf[20]; 556 557 if (he->mem_info) 558 addr = he->mem_info->iaddr.addr; 559 560 return scnprintf(hpp->buf, hpp->size, "%*s", width, HEX_STR(buf, addr)); 561 } 562 563 static int64_t 564 iaddr_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 565 struct hist_entry *left, struct hist_entry *right) 566 { 567 return sort__iaddr_cmp(left, right); 568 } 569 570 static int 571 tot_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 572 struct hist_entry *he) 573 { 574 struct c2c_hist_entry *c2c_he; 575 int width = c2c_width(fmt, hpp, he->hists); 576 unsigned int tot_hitm; 577 578 c2c_he = container_of(he, struct c2c_hist_entry, he); 579 tot_hitm = c2c_he->stats.lcl_hitm + c2c_he->stats.rmt_hitm; 580 581 return scnprintf(hpp->buf, hpp->size, "%*u", width, tot_hitm); 582 } 583 584 static int64_t 585 tot_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 586 struct hist_entry *left, struct hist_entry *right) 587 { 588 struct c2c_hist_entry *c2c_left; 589 struct c2c_hist_entry *c2c_right; 590 unsigned int tot_hitm_left; 591 unsigned int tot_hitm_right; 592 593 c2c_left = container_of(left, struct c2c_hist_entry, he); 594 c2c_right = container_of(right, struct c2c_hist_entry, he); 595 596 tot_hitm_left = c2c_left->stats.lcl_hitm + c2c_left->stats.rmt_hitm; 597 tot_hitm_right = c2c_right->stats.lcl_hitm + c2c_right->stats.rmt_hitm; 598 599 return tot_hitm_left - tot_hitm_right; 600 } 601 602 #define STAT_FN_ENTRY(__f) \ 603 static int \ 604 __f ## _entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, \ 605 struct hist_entry *he) \ 606 { \ 607 struct c2c_hist_entry *c2c_he; \ 608 int width = c2c_width(fmt, hpp, he->hists); \ 609 \ 610 c2c_he = container_of(he, struct c2c_hist_entry, he); \ 611 return scnprintf(hpp->buf, hpp->size, "%*u", width, \ 612 c2c_he->stats.__f); \ 613 } 614 615 #define STAT_FN_CMP(__f) \ 616 static int64_t \ 617 __f ## _cmp(struct perf_hpp_fmt *fmt __maybe_unused, \ 618 struct hist_entry *left, struct hist_entry *right) \ 619 { \ 620 struct c2c_hist_entry *c2c_left, *c2c_right; \ 621 \ 622 c2c_left = container_of(left, struct c2c_hist_entry, he); \ 623 c2c_right = container_of(right, struct c2c_hist_entry, he); \ 624 return c2c_left->stats.__f - c2c_right->stats.__f; \ 625 } 626 627 #define STAT_FN(__f) \ 628 STAT_FN_ENTRY(__f) \ 629 STAT_FN_CMP(__f) 630 631 STAT_FN(rmt_hitm) 632 STAT_FN(lcl_hitm) 633 STAT_FN(store) 634 STAT_FN(st_l1hit) 635 STAT_FN(st_l1miss) 636 STAT_FN(ld_fbhit) 637 STAT_FN(ld_l1hit) 638 STAT_FN(ld_l2hit) 639 STAT_FN(ld_llchit) 640 STAT_FN(rmt_hit) 641 642 static uint64_t llc_miss(struct c2c_stats *stats) 643 { 644 uint64_t llcmiss; 645 646 llcmiss = stats->lcl_dram + 647 stats->rmt_dram + 648 stats->rmt_hitm + 649 stats->rmt_hit; 650 651 return llcmiss; 652 } 653 654 static int 655 ld_llcmiss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 656 struct hist_entry *he) 657 { 658 struct c2c_hist_entry *c2c_he; 659 int width = c2c_width(fmt, hpp, he->hists); 660 661 c2c_he = container_of(he, struct c2c_hist_entry, he); 662 663 return scnprintf(hpp->buf, hpp->size, "%*lu", width, 664 llc_miss(&c2c_he->stats)); 665 } 666 667 static int64_t 668 ld_llcmiss_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 669 struct hist_entry *left, struct hist_entry *right) 670 { 671 struct c2c_hist_entry *c2c_left; 672 struct c2c_hist_entry *c2c_right; 673 674 c2c_left = container_of(left, struct c2c_hist_entry, he); 675 c2c_right = container_of(right, struct c2c_hist_entry, he); 676 677 return llc_miss(&c2c_left->stats) - llc_miss(&c2c_right->stats); 678 } 679 680 static uint64_t total_records(struct c2c_stats *stats) 681 { 682 uint64_t lclmiss, ldcnt, total; 683 684 lclmiss = stats->lcl_dram + 685 stats->rmt_dram + 686 stats->rmt_hitm + 687 stats->rmt_hit; 688 689 ldcnt = lclmiss + 690 stats->ld_fbhit + 691 stats->ld_l1hit + 692 stats->ld_l2hit + 693 stats->ld_llchit + 694 stats->lcl_hitm; 695 696 total = ldcnt + 697 stats->st_l1hit + 698 stats->st_l1miss; 699 700 return total; 701 } 702 703 static int 704 tot_recs_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 705 struct hist_entry *he) 706 { 707 struct c2c_hist_entry *c2c_he; 708 int width = c2c_width(fmt, hpp, he->hists); 709 uint64_t tot_recs; 710 711 c2c_he = container_of(he, struct c2c_hist_entry, he); 712 tot_recs = total_records(&c2c_he->stats); 713 714 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs); 715 } 716 717 static int64_t 718 tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 719 struct hist_entry *left, struct hist_entry *right) 720 { 721 struct c2c_hist_entry *c2c_left; 722 struct c2c_hist_entry *c2c_right; 723 uint64_t tot_recs_left; 724 uint64_t tot_recs_right; 725 726 c2c_left = container_of(left, struct c2c_hist_entry, he); 727 c2c_right = container_of(right, struct c2c_hist_entry, he); 728 729 tot_recs_left = total_records(&c2c_left->stats); 730 tot_recs_right = total_records(&c2c_right->stats); 731 732 return tot_recs_left - tot_recs_right; 733 } 734 735 static uint64_t total_loads(struct c2c_stats *stats) 736 { 737 uint64_t lclmiss, ldcnt; 738 739 lclmiss = stats->lcl_dram + 740 stats->rmt_dram + 741 stats->rmt_hitm + 742 stats->rmt_hit; 743 744 ldcnt = lclmiss + 745 stats->ld_fbhit + 746 stats->ld_l1hit + 747 stats->ld_l2hit + 748 stats->ld_llchit + 749 stats->lcl_hitm; 750 751 return ldcnt; 752 } 753 754 static int 755 tot_loads_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 756 struct hist_entry *he) 757 { 758 struct c2c_hist_entry *c2c_he; 759 int width = c2c_width(fmt, hpp, he->hists); 760 uint64_t tot_recs; 761 762 c2c_he = container_of(he, struct c2c_hist_entry, he); 763 tot_recs = total_loads(&c2c_he->stats); 764 765 return scnprintf(hpp->buf, hpp->size, "%*" PRIu64, width, tot_recs); 766 } 767 768 static int64_t 769 tot_loads_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 770 struct hist_entry *left, struct hist_entry *right) 771 { 772 struct c2c_hist_entry *c2c_left; 773 struct c2c_hist_entry *c2c_right; 774 uint64_t tot_recs_left; 775 uint64_t tot_recs_right; 776 777 c2c_left = container_of(left, struct c2c_hist_entry, he); 778 c2c_right = container_of(right, struct c2c_hist_entry, he); 779 780 tot_recs_left = total_loads(&c2c_left->stats); 781 tot_recs_right = total_loads(&c2c_right->stats); 782 783 return tot_recs_left - tot_recs_right; 784 } 785 786 typedef double (get_percent_cb)(struct c2c_hist_entry *); 787 788 static int 789 percent_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 790 struct hist_entry *he, get_percent_cb get_percent) 791 { 792 struct c2c_hist_entry *c2c_he; 793 int width = c2c_width(fmt, hpp, he->hists); 794 double per; 795 796 c2c_he = container_of(he, struct c2c_hist_entry, he); 797 per = get_percent(c2c_he); 798 799 #ifdef HAVE_SLANG_SUPPORT 800 if (use_browser) 801 return __hpp__slsmg_color_printf(hpp, "%*.2f%%", width - 1, per); 802 #endif 803 return hpp_color_scnprintf(hpp, "%*.2f%%", width - 1, per); 804 } 805 806 static double percent_hitm(struct c2c_hist_entry *c2c_he) 807 { 808 struct c2c_hists *hists; 809 struct c2c_stats *stats; 810 struct c2c_stats *total; 811 int tot = 0, st = 0; 812 double p; 813 814 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); 815 stats = &c2c_he->stats; 816 total = &hists->stats; 817 818 switch (c2c.display) { 819 case DISPLAY_RMT: 820 st = stats->rmt_hitm; 821 tot = total->rmt_hitm; 822 break; 823 case DISPLAY_LCL: 824 st = stats->lcl_hitm; 825 tot = total->lcl_hitm; 826 break; 827 case DISPLAY_TOT: 828 st = stats->tot_hitm; 829 tot = total->tot_hitm; 830 default: 831 break; 832 } 833 834 p = tot ? (double) st / tot : 0; 835 836 return 100 * p; 837 } 838 839 #define PERC_STR(__s, __v) \ 840 ({ \ 841 scnprintf(__s, sizeof(__s), "%.2F%%", __v); \ 842 __s; \ 843 }) 844 845 static int 846 percent_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 847 struct hist_entry *he) 848 { 849 struct c2c_hist_entry *c2c_he; 850 int width = c2c_width(fmt, hpp, he->hists); 851 char buf[10]; 852 double per; 853 854 c2c_he = container_of(he, struct c2c_hist_entry, he); 855 per = percent_hitm(c2c_he); 856 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per)); 857 } 858 859 static int 860 percent_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 861 struct hist_entry *he) 862 { 863 return percent_color(fmt, hpp, he, percent_hitm); 864 } 865 866 static int64_t 867 percent_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 868 struct hist_entry *left, struct hist_entry *right) 869 { 870 struct c2c_hist_entry *c2c_left; 871 struct c2c_hist_entry *c2c_right; 872 double per_left; 873 double per_right; 874 875 c2c_left = container_of(left, struct c2c_hist_entry, he); 876 c2c_right = container_of(right, struct c2c_hist_entry, he); 877 878 per_left = percent_hitm(c2c_left); 879 per_right = percent_hitm(c2c_right); 880 881 return per_left - per_right; 882 } 883 884 static struct c2c_stats *he_stats(struct hist_entry *he) 885 { 886 struct c2c_hist_entry *c2c_he; 887 888 c2c_he = container_of(he, struct c2c_hist_entry, he); 889 return &c2c_he->stats; 890 } 891 892 static struct c2c_stats *total_stats(struct hist_entry *he) 893 { 894 struct c2c_hists *hists; 895 896 hists = container_of(he->hists, struct c2c_hists, hists); 897 return &hists->stats; 898 } 899 900 static double percent(int st, int tot) 901 { 902 return tot ? 100. * (double) st / (double) tot : 0; 903 } 904 905 #define PERCENT(__h, __f) percent(he_stats(__h)->__f, total_stats(__h)->__f) 906 907 #define PERCENT_FN(__f) \ 908 static double percent_ ## __f(struct c2c_hist_entry *c2c_he) \ 909 { \ 910 struct c2c_hists *hists; \ 911 \ 912 hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); \ 913 return percent(c2c_he->stats.__f, hists->stats.__f); \ 914 } 915 916 PERCENT_FN(rmt_hitm) 917 PERCENT_FN(lcl_hitm) 918 PERCENT_FN(st_l1hit) 919 PERCENT_FN(st_l1miss) 920 921 static int 922 percent_rmt_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 923 struct hist_entry *he) 924 { 925 int width = c2c_width(fmt, hpp, he->hists); 926 double per = PERCENT(he, rmt_hitm); 927 char buf[10]; 928 929 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per)); 930 } 931 932 static int 933 percent_rmt_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 934 struct hist_entry *he) 935 { 936 return percent_color(fmt, hpp, he, percent_rmt_hitm); 937 } 938 939 static int64_t 940 percent_rmt_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 941 struct hist_entry *left, struct hist_entry *right) 942 { 943 double per_left; 944 double per_right; 945 946 per_left = PERCENT(left, lcl_hitm); 947 per_right = PERCENT(right, lcl_hitm); 948 949 return per_left - per_right; 950 } 951 952 static int 953 percent_lcl_hitm_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 954 struct hist_entry *he) 955 { 956 int width = c2c_width(fmt, hpp, he->hists); 957 double per = PERCENT(he, lcl_hitm); 958 char buf[10]; 959 960 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per)); 961 } 962 963 static int 964 percent_lcl_hitm_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 965 struct hist_entry *he) 966 { 967 return percent_color(fmt, hpp, he, percent_lcl_hitm); 968 } 969 970 static int64_t 971 percent_lcl_hitm_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 972 struct hist_entry *left, struct hist_entry *right) 973 { 974 double per_left; 975 double per_right; 976 977 per_left = PERCENT(left, lcl_hitm); 978 per_right = PERCENT(right, lcl_hitm); 979 980 return per_left - per_right; 981 } 982 983 static int 984 percent_stores_l1hit_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 985 struct hist_entry *he) 986 { 987 int width = c2c_width(fmt, hpp, he->hists); 988 double per = PERCENT(he, st_l1hit); 989 char buf[10]; 990 991 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per)); 992 } 993 994 static int 995 percent_stores_l1hit_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 996 struct hist_entry *he) 997 { 998 return percent_color(fmt, hpp, he, percent_st_l1hit); 999 } 1000 1001 static int64_t 1002 percent_stores_l1hit_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 1003 struct hist_entry *left, struct hist_entry *right) 1004 { 1005 double per_left; 1006 double per_right; 1007 1008 per_left = PERCENT(left, st_l1hit); 1009 per_right = PERCENT(right, st_l1hit); 1010 1011 return per_left - per_right; 1012 } 1013 1014 static int 1015 percent_stores_l1miss_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 1016 struct hist_entry *he) 1017 { 1018 int width = c2c_width(fmt, hpp, he->hists); 1019 double per = PERCENT(he, st_l1miss); 1020 char buf[10]; 1021 1022 return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per)); 1023 } 1024 1025 static int 1026 percent_stores_l1miss_color(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 1027 struct hist_entry *he) 1028 { 1029 return percent_color(fmt, hpp, he, percent_st_l1miss); 1030 } 1031 1032 static int64_t 1033 percent_stores_l1miss_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 1034 struct hist_entry *left, struct hist_entry *right) 1035 { 1036 double per_left; 1037 double per_right; 1038 1039 per_left = PERCENT(left, st_l1miss); 1040 per_right = PERCENT(right, st_l1miss); 1041 1042 return per_left - per_right; 1043 } 1044 1045 STAT_FN(lcl_dram) 1046 STAT_FN(rmt_dram) 1047 1048 static int 1049 pid_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 1050 struct hist_entry *he) 1051 { 1052 int width = c2c_width(fmt, hpp, he->hists); 1053 1054 return scnprintf(hpp->buf, hpp->size, "%*d", width, he->thread->pid_); 1055 } 1056 1057 static int64_t 1058 pid_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 1059 struct hist_entry *left, struct hist_entry *right) 1060 { 1061 return left->thread->pid_ - right->thread->pid_; 1062 } 1063 1064 static int64_t 1065 empty_cmp(struct perf_hpp_fmt *fmt __maybe_unused, 1066 struct hist_entry *left __maybe_unused, 1067 struct hist_entry *right __maybe_unused) 1068 { 1069 return 0; 1070 } 1071 1072 static int 1073 node_entry(struct perf_hpp_fmt *fmt __maybe_unused, struct perf_hpp *hpp, 1074 struct hist_entry *he) 1075 { 1076 struct c2c_hist_entry *c2c_he; 1077 bool first = true; 1078 int node; 1079 int ret = 0; 1080 1081 c2c_he = container_of(he, struct c2c_hist_entry, he); 1082 1083 for (node = 0; node < c2c.nodes_cnt; node++) { 1084 DECLARE_BITMAP(set, c2c.cpus_cnt); 1085 1086 bitmap_zero(set, c2c.cpus_cnt); 1087 bitmap_and(set, c2c_he->cpuset, c2c.nodes[node], c2c.cpus_cnt); 1088 1089 if (!bitmap_weight(set, c2c.cpus_cnt)) { 1090 if (c2c.node_info == 1) { 1091 ret = scnprintf(hpp->buf, hpp->size, "%21s", " "); 1092 advance_hpp(hpp, ret); 1093 } 1094 continue; 1095 } 1096 1097 if (!first) { 1098 ret = scnprintf(hpp->buf, hpp->size, " "); 1099 advance_hpp(hpp, ret); 1100 } 1101 1102 switch (c2c.node_info) { 1103 case 0: 1104 ret = scnprintf(hpp->buf, hpp->size, "%2d", node); 1105 advance_hpp(hpp, ret); 1106 break; 1107 case 1: 1108 { 1109 int num = bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt); 1110 struct c2c_stats *stats = &c2c_he->node_stats[node]; 1111 1112 ret = scnprintf(hpp->buf, hpp->size, "%2d{%2d ", node, num); 1113 advance_hpp(hpp, ret); 1114 1115 #define DISPLAY_HITM(__h) \ 1116 if (c2c_he->stats.__h> 0) { \ 1117 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%% ", \ 1118 percent(stats->__h, c2c_he->stats.__h));\ 1119 } else { \ 1120 ret = scnprintf(hpp->buf, hpp->size, "%6s ", "n/a"); \ 1121 } 1122 1123 switch (c2c.display) { 1124 case DISPLAY_RMT: 1125 DISPLAY_HITM(rmt_hitm); 1126 break; 1127 case DISPLAY_LCL: 1128 DISPLAY_HITM(lcl_hitm); 1129 break; 1130 case DISPLAY_TOT: 1131 DISPLAY_HITM(tot_hitm); 1132 default: 1133 break; 1134 } 1135 1136 #undef DISPLAY_HITM 1137 1138 advance_hpp(hpp, ret); 1139 1140 if (c2c_he->stats.store > 0) { 1141 ret = scnprintf(hpp->buf, hpp->size, "%5.1f%%}", 1142 percent(stats->store, c2c_he->stats.store)); 1143 } else { 1144 ret = scnprintf(hpp->buf, hpp->size, "%6s}", "n/a"); 1145 } 1146 1147 advance_hpp(hpp, ret); 1148 break; 1149 } 1150 case 2: 1151 ret = scnprintf(hpp->buf, hpp->size, "%2d{", node); 1152 advance_hpp(hpp, ret); 1153 1154 ret = bitmap_scnprintf(set, c2c.cpus_cnt, hpp->buf, hpp->size); 1155 advance_hpp(hpp, ret); 1156 1157 ret = scnprintf(hpp->buf, hpp->size, "}"); 1158 advance_hpp(hpp, ret); 1159 break; 1160 default: 1161 break; 1162 } 1163 1164 first = false; 1165 } 1166 1167 return 0; 1168 } 1169 1170 static int 1171 mean_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 1172 struct hist_entry *he, double mean) 1173 { 1174 int width = c2c_width(fmt, hpp, he->hists); 1175 char buf[10]; 1176 1177 scnprintf(buf, 10, "%6.0f", mean); 1178 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf); 1179 } 1180 1181 #define MEAN_ENTRY(__func, __val) \ 1182 static int \ 1183 __func(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, struct hist_entry *he) \ 1184 { \ 1185 struct c2c_hist_entry *c2c_he; \ 1186 c2c_he = container_of(he, struct c2c_hist_entry, he); \ 1187 return mean_entry(fmt, hpp, he, avg_stats(&c2c_he->cstats.__val)); \ 1188 } 1189 1190 MEAN_ENTRY(mean_rmt_entry, rmt_hitm); 1191 MEAN_ENTRY(mean_lcl_entry, lcl_hitm); 1192 MEAN_ENTRY(mean_load_entry, load); 1193 1194 static int 1195 cpucnt_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 1196 struct hist_entry *he) 1197 { 1198 struct c2c_hist_entry *c2c_he; 1199 int width = c2c_width(fmt, hpp, he->hists); 1200 char buf[10]; 1201 1202 c2c_he = container_of(he, struct c2c_hist_entry, he); 1203 1204 scnprintf(buf, 10, "%d", bitmap_weight(c2c_he->cpuset, c2c.cpus_cnt)); 1205 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf); 1206 } 1207 1208 static int 1209 cl_idx_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 1210 struct hist_entry *he) 1211 { 1212 struct c2c_hist_entry *c2c_he; 1213 int width = c2c_width(fmt, hpp, he->hists); 1214 char buf[10]; 1215 1216 c2c_he = container_of(he, struct c2c_hist_entry, he); 1217 1218 scnprintf(buf, 10, "%u", c2c_he->cacheline_idx); 1219 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf); 1220 } 1221 1222 static int 1223 cl_idx_empty_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 1224 struct hist_entry *he) 1225 { 1226 int width = c2c_width(fmt, hpp, he->hists); 1227 1228 return scnprintf(hpp->buf, hpp->size, "%*s", width, ""); 1229 } 1230 1231 #define HEADER_LOW(__h) \ 1232 { \ 1233 .line[1] = { \ 1234 .text = __h, \ 1235 }, \ 1236 } 1237 1238 #define HEADER_BOTH(__h0, __h1) \ 1239 { \ 1240 .line[0] = { \ 1241 .text = __h0, \ 1242 }, \ 1243 .line[1] = { \ 1244 .text = __h1, \ 1245 }, \ 1246 } 1247 1248 #define HEADER_SPAN(__h0, __h1, __s) \ 1249 { \ 1250 .line[0] = { \ 1251 .text = __h0, \ 1252 .span = __s, \ 1253 }, \ 1254 .line[1] = { \ 1255 .text = __h1, \ 1256 }, \ 1257 } 1258 1259 #define HEADER_SPAN_LOW(__h) \ 1260 { \ 1261 .line[1] = { \ 1262 .text = __h, \ 1263 }, \ 1264 } 1265 1266 static struct c2c_dimension dim_dcacheline = { 1267 .header = HEADER_SPAN("--- Cacheline ----", "Address", 2), 1268 .name = "dcacheline", 1269 .cmp = dcacheline_cmp, 1270 .entry = dcacheline_entry, 1271 .width = 18, 1272 }; 1273 1274 static struct c2c_dimension dim_dcacheline_node = { 1275 .header = HEADER_LOW("Node"), 1276 .name = "dcacheline_node", 1277 .cmp = empty_cmp, 1278 .entry = dcacheline_node_entry, 1279 .width = 4, 1280 }; 1281 1282 static struct c2c_dimension dim_dcacheline_count = { 1283 .header = HEADER_LOW("PA cnt"), 1284 .name = "dcacheline_count", 1285 .cmp = empty_cmp, 1286 .entry = dcacheline_node_count, 1287 .width = 6, 1288 }; 1289 1290 static struct c2c_header header_offset_tui = HEADER_SPAN("-----", "Off", 2); 1291 1292 static struct c2c_dimension dim_offset = { 1293 .header = HEADER_SPAN("--- Data address -", "Offset", 2), 1294 .name = "offset", 1295 .cmp = offset_cmp, 1296 .entry = offset_entry, 1297 .width = 18, 1298 }; 1299 1300 static struct c2c_dimension dim_offset_node = { 1301 .header = HEADER_LOW("Node"), 1302 .name = "offset_node", 1303 .cmp = empty_cmp, 1304 .entry = dcacheline_node_entry, 1305 .width = 4, 1306 }; 1307 1308 static struct c2c_dimension dim_iaddr = { 1309 .header = HEADER_LOW("Code address"), 1310 .name = "iaddr", 1311 .cmp = iaddr_cmp, 1312 .entry = iaddr_entry, 1313 .width = 18, 1314 }; 1315 1316 static struct c2c_dimension dim_tot_hitm = { 1317 .header = HEADER_SPAN("----- LLC Load Hitm -----", "Total", 2), 1318 .name = "tot_hitm", 1319 .cmp = tot_hitm_cmp, 1320 .entry = tot_hitm_entry, 1321 .width = 7, 1322 }; 1323 1324 static struct c2c_dimension dim_lcl_hitm = { 1325 .header = HEADER_SPAN_LOW("Lcl"), 1326 .name = "lcl_hitm", 1327 .cmp = lcl_hitm_cmp, 1328 .entry = lcl_hitm_entry, 1329 .width = 7, 1330 }; 1331 1332 static struct c2c_dimension dim_rmt_hitm = { 1333 .header = HEADER_SPAN_LOW("Rmt"), 1334 .name = "rmt_hitm", 1335 .cmp = rmt_hitm_cmp, 1336 .entry = rmt_hitm_entry, 1337 .width = 7, 1338 }; 1339 1340 static struct c2c_dimension dim_cl_rmt_hitm = { 1341 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1), 1342 .name = "cl_rmt_hitm", 1343 .cmp = rmt_hitm_cmp, 1344 .entry = rmt_hitm_entry, 1345 .width = 7, 1346 }; 1347 1348 static struct c2c_dimension dim_cl_lcl_hitm = { 1349 .header = HEADER_SPAN_LOW("Lcl"), 1350 .name = "cl_lcl_hitm", 1351 .cmp = lcl_hitm_cmp, 1352 .entry = lcl_hitm_entry, 1353 .width = 7, 1354 }; 1355 1356 static struct c2c_dimension dim_stores = { 1357 .header = HEADER_SPAN("---- Store Reference ----", "Total", 2), 1358 .name = "stores", 1359 .cmp = store_cmp, 1360 .entry = store_entry, 1361 .width = 7, 1362 }; 1363 1364 static struct c2c_dimension dim_stores_l1hit = { 1365 .header = HEADER_SPAN_LOW("L1Hit"), 1366 .name = "stores_l1hit", 1367 .cmp = st_l1hit_cmp, 1368 .entry = st_l1hit_entry, 1369 .width = 7, 1370 }; 1371 1372 static struct c2c_dimension dim_stores_l1miss = { 1373 .header = HEADER_SPAN_LOW("L1Miss"), 1374 .name = "stores_l1miss", 1375 .cmp = st_l1miss_cmp, 1376 .entry = st_l1miss_entry, 1377 .width = 7, 1378 }; 1379 1380 static struct c2c_dimension dim_cl_stores_l1hit = { 1381 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1), 1382 .name = "cl_stores_l1hit", 1383 .cmp = st_l1hit_cmp, 1384 .entry = st_l1hit_entry, 1385 .width = 7, 1386 }; 1387 1388 static struct c2c_dimension dim_cl_stores_l1miss = { 1389 .header = HEADER_SPAN_LOW("L1 Miss"), 1390 .name = "cl_stores_l1miss", 1391 .cmp = st_l1miss_cmp, 1392 .entry = st_l1miss_entry, 1393 .width = 7, 1394 }; 1395 1396 static struct c2c_dimension dim_ld_fbhit = { 1397 .header = HEADER_SPAN("----- Core Load Hit -----", "FB", 2), 1398 .name = "ld_fbhit", 1399 .cmp = ld_fbhit_cmp, 1400 .entry = ld_fbhit_entry, 1401 .width = 7, 1402 }; 1403 1404 static struct c2c_dimension dim_ld_l1hit = { 1405 .header = HEADER_SPAN_LOW("L1"), 1406 .name = "ld_l1hit", 1407 .cmp = ld_l1hit_cmp, 1408 .entry = ld_l1hit_entry, 1409 .width = 7, 1410 }; 1411 1412 static struct c2c_dimension dim_ld_l2hit = { 1413 .header = HEADER_SPAN_LOW("L2"), 1414 .name = "ld_l2hit", 1415 .cmp = ld_l2hit_cmp, 1416 .entry = ld_l2hit_entry, 1417 .width = 7, 1418 }; 1419 1420 static struct c2c_dimension dim_ld_llchit = { 1421 .header = HEADER_SPAN("-- LLC Load Hit --", "Llc", 1), 1422 .name = "ld_lclhit", 1423 .cmp = ld_llchit_cmp, 1424 .entry = ld_llchit_entry, 1425 .width = 8, 1426 }; 1427 1428 static struct c2c_dimension dim_ld_rmthit = { 1429 .header = HEADER_SPAN_LOW("Rmt"), 1430 .name = "ld_rmthit", 1431 .cmp = rmt_hit_cmp, 1432 .entry = rmt_hit_entry, 1433 .width = 8, 1434 }; 1435 1436 static struct c2c_dimension dim_ld_llcmiss = { 1437 .header = HEADER_BOTH("LLC", "Ld Miss"), 1438 .name = "ld_llcmiss", 1439 .cmp = ld_llcmiss_cmp, 1440 .entry = ld_llcmiss_entry, 1441 .width = 7, 1442 }; 1443 1444 static struct c2c_dimension dim_tot_recs = { 1445 .header = HEADER_BOTH("Total", "records"), 1446 .name = "tot_recs", 1447 .cmp = tot_recs_cmp, 1448 .entry = tot_recs_entry, 1449 .width = 7, 1450 }; 1451 1452 static struct c2c_dimension dim_tot_loads = { 1453 .header = HEADER_BOTH("Total", "Loads"), 1454 .name = "tot_loads", 1455 .cmp = tot_loads_cmp, 1456 .entry = tot_loads_entry, 1457 .width = 7, 1458 }; 1459 1460 static struct c2c_header percent_hitm_header[] = { 1461 [DISPLAY_LCL] = HEADER_BOTH("Lcl", "Hitm"), 1462 [DISPLAY_RMT] = HEADER_BOTH("Rmt", "Hitm"), 1463 [DISPLAY_TOT] = HEADER_BOTH("Tot", "Hitm"), 1464 }; 1465 1466 static struct c2c_dimension dim_percent_hitm = { 1467 .name = "percent_hitm", 1468 .cmp = percent_hitm_cmp, 1469 .entry = percent_hitm_entry, 1470 .color = percent_hitm_color, 1471 .width = 7, 1472 }; 1473 1474 static struct c2c_dimension dim_percent_rmt_hitm = { 1475 .header = HEADER_SPAN("----- HITM -----", "Rmt", 1), 1476 .name = "percent_rmt_hitm", 1477 .cmp = percent_rmt_hitm_cmp, 1478 .entry = percent_rmt_hitm_entry, 1479 .color = percent_rmt_hitm_color, 1480 .width = 7, 1481 }; 1482 1483 static struct c2c_dimension dim_percent_lcl_hitm = { 1484 .header = HEADER_SPAN_LOW("Lcl"), 1485 .name = "percent_lcl_hitm", 1486 .cmp = percent_lcl_hitm_cmp, 1487 .entry = percent_lcl_hitm_entry, 1488 .color = percent_lcl_hitm_color, 1489 .width = 7, 1490 }; 1491 1492 static struct c2c_dimension dim_percent_stores_l1hit = { 1493 .header = HEADER_SPAN("-- Store Refs --", "L1 Hit", 1), 1494 .name = "percent_stores_l1hit", 1495 .cmp = percent_stores_l1hit_cmp, 1496 .entry = percent_stores_l1hit_entry, 1497 .color = percent_stores_l1hit_color, 1498 .width = 7, 1499 }; 1500 1501 static struct c2c_dimension dim_percent_stores_l1miss = { 1502 .header = HEADER_SPAN_LOW("L1 Miss"), 1503 .name = "percent_stores_l1miss", 1504 .cmp = percent_stores_l1miss_cmp, 1505 .entry = percent_stores_l1miss_entry, 1506 .color = percent_stores_l1miss_color, 1507 .width = 7, 1508 }; 1509 1510 static struct c2c_dimension dim_dram_lcl = { 1511 .header = HEADER_SPAN("--- Load Dram ----", "Lcl", 1), 1512 .name = "dram_lcl", 1513 .cmp = lcl_dram_cmp, 1514 .entry = lcl_dram_entry, 1515 .width = 8, 1516 }; 1517 1518 static struct c2c_dimension dim_dram_rmt = { 1519 .header = HEADER_SPAN_LOW("Rmt"), 1520 .name = "dram_rmt", 1521 .cmp = rmt_dram_cmp, 1522 .entry = rmt_dram_entry, 1523 .width = 8, 1524 }; 1525 1526 static struct c2c_dimension dim_pid = { 1527 .header = HEADER_LOW("Pid"), 1528 .name = "pid", 1529 .cmp = pid_cmp, 1530 .entry = pid_entry, 1531 .width = 7, 1532 }; 1533 1534 static struct c2c_dimension dim_tid = { 1535 .header = HEADER_LOW("Tid"), 1536 .name = "tid", 1537 .se = &sort_thread, 1538 }; 1539 1540 static struct c2c_dimension dim_symbol = { 1541 .name = "symbol", 1542 .se = &sort_sym, 1543 }; 1544 1545 static struct c2c_dimension dim_dso = { 1546 .header = HEADER_BOTH("Shared", "Object"), 1547 .name = "dso", 1548 .se = &sort_dso, 1549 }; 1550 1551 static struct c2c_header header_node[3] = { 1552 HEADER_LOW("Node"), 1553 HEADER_LOW("Node{cpus %hitms %stores}"), 1554 HEADER_LOW("Node{cpu list}"), 1555 }; 1556 1557 static struct c2c_dimension dim_node = { 1558 .name = "node", 1559 .cmp = empty_cmp, 1560 .entry = node_entry, 1561 .width = 4, 1562 }; 1563 1564 static struct c2c_dimension dim_mean_rmt = { 1565 .header = HEADER_SPAN("---------- cycles ----------", "rmt hitm", 2), 1566 .name = "mean_rmt", 1567 .cmp = empty_cmp, 1568 .entry = mean_rmt_entry, 1569 .width = 8, 1570 }; 1571 1572 static struct c2c_dimension dim_mean_lcl = { 1573 .header = HEADER_SPAN_LOW("lcl hitm"), 1574 .name = "mean_lcl", 1575 .cmp = empty_cmp, 1576 .entry = mean_lcl_entry, 1577 .width = 8, 1578 }; 1579 1580 static struct c2c_dimension dim_mean_load = { 1581 .header = HEADER_SPAN_LOW("load"), 1582 .name = "mean_load", 1583 .cmp = empty_cmp, 1584 .entry = mean_load_entry, 1585 .width = 8, 1586 }; 1587 1588 static struct c2c_dimension dim_cpucnt = { 1589 .header = HEADER_BOTH("cpu", "cnt"), 1590 .name = "cpucnt", 1591 .cmp = empty_cmp, 1592 .entry = cpucnt_entry, 1593 .width = 8, 1594 }; 1595 1596 static struct c2c_dimension dim_srcline = { 1597 .name = "cl_srcline", 1598 .se = &sort_srcline, 1599 }; 1600 1601 static struct c2c_dimension dim_dcacheline_idx = { 1602 .header = HEADER_LOW("Index"), 1603 .name = "cl_idx", 1604 .cmp = empty_cmp, 1605 .entry = cl_idx_entry, 1606 .width = 5, 1607 }; 1608 1609 static struct c2c_dimension dim_dcacheline_num = { 1610 .header = HEADER_LOW("Num"), 1611 .name = "cl_num", 1612 .cmp = empty_cmp, 1613 .entry = cl_idx_entry, 1614 .width = 5, 1615 }; 1616 1617 static struct c2c_dimension dim_dcacheline_num_empty = { 1618 .header = HEADER_LOW("Num"), 1619 .name = "cl_num_empty", 1620 .cmp = empty_cmp, 1621 .entry = cl_idx_empty_entry, 1622 .width = 5, 1623 }; 1624 1625 static struct c2c_dimension *dimensions[] = { 1626 &dim_dcacheline, 1627 &dim_dcacheline_node, 1628 &dim_dcacheline_count, 1629 &dim_offset, 1630 &dim_offset_node, 1631 &dim_iaddr, 1632 &dim_tot_hitm, 1633 &dim_lcl_hitm, 1634 &dim_rmt_hitm, 1635 &dim_cl_lcl_hitm, 1636 &dim_cl_rmt_hitm, 1637 &dim_stores, 1638 &dim_stores_l1hit, 1639 &dim_stores_l1miss, 1640 &dim_cl_stores_l1hit, 1641 &dim_cl_stores_l1miss, 1642 &dim_ld_fbhit, 1643 &dim_ld_l1hit, 1644 &dim_ld_l2hit, 1645 &dim_ld_llchit, 1646 &dim_ld_rmthit, 1647 &dim_ld_llcmiss, 1648 &dim_tot_recs, 1649 &dim_tot_loads, 1650 &dim_percent_hitm, 1651 &dim_percent_rmt_hitm, 1652 &dim_percent_lcl_hitm, 1653 &dim_percent_stores_l1hit, 1654 &dim_percent_stores_l1miss, 1655 &dim_dram_lcl, 1656 &dim_dram_rmt, 1657 &dim_pid, 1658 &dim_tid, 1659 &dim_symbol, 1660 &dim_dso, 1661 &dim_node, 1662 &dim_mean_rmt, 1663 &dim_mean_lcl, 1664 &dim_mean_load, 1665 &dim_cpucnt, 1666 &dim_srcline, 1667 &dim_dcacheline_idx, 1668 &dim_dcacheline_num, 1669 &dim_dcacheline_num_empty, 1670 NULL, 1671 }; 1672 1673 static void fmt_free(struct perf_hpp_fmt *fmt) 1674 { 1675 struct c2c_fmt *c2c_fmt; 1676 1677 c2c_fmt = container_of(fmt, struct c2c_fmt, fmt); 1678 free(c2c_fmt); 1679 } 1680 1681 static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b) 1682 { 1683 struct c2c_fmt *c2c_a = container_of(a, struct c2c_fmt, fmt); 1684 struct c2c_fmt *c2c_b = container_of(b, struct c2c_fmt, fmt); 1685 1686 return c2c_a->dim == c2c_b->dim; 1687 } 1688 1689 static struct c2c_dimension *get_dimension(const char *name) 1690 { 1691 unsigned int i; 1692 1693 for (i = 0; dimensions[i]; i++) { 1694 struct c2c_dimension *dim = dimensions[i]; 1695 1696 if (!strcmp(dim->name, name)) 1697 return dim; 1698 }; 1699 1700 return NULL; 1701 } 1702 1703 static int c2c_se_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 1704 struct hist_entry *he) 1705 { 1706 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt); 1707 struct c2c_dimension *dim = c2c_fmt->dim; 1708 size_t len = fmt->user_len; 1709 1710 if (!len) { 1711 len = hists__col_len(he->hists, dim->se->se_width_idx); 1712 1713 if (dim == &dim_symbol || dim == &dim_srcline) 1714 len = symbol_width(he->hists, dim->se); 1715 } 1716 1717 return dim->se->se_snprintf(he, hpp->buf, hpp->size, len); 1718 } 1719 1720 static int64_t c2c_se_cmp(struct perf_hpp_fmt *fmt, 1721 struct hist_entry *a, struct hist_entry *b) 1722 { 1723 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt); 1724 struct c2c_dimension *dim = c2c_fmt->dim; 1725 1726 return dim->se->se_cmp(a, b); 1727 } 1728 1729 static int64_t c2c_se_collapse(struct perf_hpp_fmt *fmt, 1730 struct hist_entry *a, struct hist_entry *b) 1731 { 1732 struct c2c_fmt *c2c_fmt = container_of(fmt, struct c2c_fmt, fmt); 1733 struct c2c_dimension *dim = c2c_fmt->dim; 1734 int64_t (*collapse_fn)(struct hist_entry *, struct hist_entry *); 1735 1736 collapse_fn = dim->se->se_collapse ?: dim->se->se_cmp; 1737 return collapse_fn(a, b); 1738 } 1739 1740 static struct c2c_fmt *get_format(const char *name) 1741 { 1742 struct c2c_dimension *dim = get_dimension(name); 1743 struct c2c_fmt *c2c_fmt; 1744 struct perf_hpp_fmt *fmt; 1745 1746 if (!dim) 1747 return NULL; 1748 1749 c2c_fmt = zalloc(sizeof(*c2c_fmt)); 1750 if (!c2c_fmt) 1751 return NULL; 1752 1753 c2c_fmt->dim = dim; 1754 1755 fmt = &c2c_fmt->fmt; 1756 INIT_LIST_HEAD(&fmt->list); 1757 INIT_LIST_HEAD(&fmt->sort_list); 1758 1759 fmt->cmp = dim->se ? c2c_se_cmp : dim->cmp; 1760 fmt->sort = dim->se ? c2c_se_cmp : dim->cmp; 1761 fmt->color = dim->se ? NULL : dim->color; 1762 fmt->entry = dim->se ? c2c_se_entry : dim->entry; 1763 fmt->header = c2c_header; 1764 fmt->width = c2c_width; 1765 fmt->collapse = dim->se ? c2c_se_collapse : dim->cmp; 1766 fmt->equal = fmt_equal; 1767 fmt->free = fmt_free; 1768 1769 return c2c_fmt; 1770 } 1771 1772 static int c2c_hists__init_output(struct perf_hpp_list *hpp_list, char *name) 1773 { 1774 struct c2c_fmt *c2c_fmt = get_format(name); 1775 1776 if (!c2c_fmt) { 1777 reset_dimensions(); 1778 return output_field_add(hpp_list, name); 1779 } 1780 1781 perf_hpp_list__column_register(hpp_list, &c2c_fmt->fmt); 1782 return 0; 1783 } 1784 1785 static int c2c_hists__init_sort(struct perf_hpp_list *hpp_list, char *name) 1786 { 1787 struct c2c_fmt *c2c_fmt = get_format(name); 1788 struct c2c_dimension *dim; 1789 1790 if (!c2c_fmt) { 1791 reset_dimensions(); 1792 return sort_dimension__add(hpp_list, name, NULL, 0); 1793 } 1794 1795 dim = c2c_fmt->dim; 1796 if (dim == &dim_dso) 1797 hpp_list->dso = 1; 1798 1799 perf_hpp_list__register_sort_field(hpp_list, &c2c_fmt->fmt); 1800 return 0; 1801 } 1802 1803 #define PARSE_LIST(_list, _fn) \ 1804 do { \ 1805 char *tmp, *tok; \ 1806 ret = 0; \ 1807 \ 1808 if (!_list) \ 1809 break; \ 1810 \ 1811 for (tok = strtok_r((char *)_list, ", ", &tmp); \ 1812 tok; tok = strtok_r(NULL, ", ", &tmp)) { \ 1813 ret = _fn(hpp_list, tok); \ 1814 if (ret == -EINVAL) { \ 1815 pr_err("Invalid --fields key: `%s'", tok); \ 1816 break; \ 1817 } else if (ret == -ESRCH) { \ 1818 pr_err("Unknown --fields key: `%s'", tok); \ 1819 break; \ 1820 } \ 1821 } \ 1822 } while (0) 1823 1824 static int hpp_list__parse(struct perf_hpp_list *hpp_list, 1825 const char *output_, 1826 const char *sort_) 1827 { 1828 char *output = output_ ? strdup(output_) : NULL; 1829 char *sort = sort_ ? strdup(sort_) : NULL; 1830 int ret; 1831 1832 PARSE_LIST(output, c2c_hists__init_output); 1833 PARSE_LIST(sort, c2c_hists__init_sort); 1834 1835 /* copy sort keys to output fields */ 1836 perf_hpp__setup_output_field(hpp_list); 1837 1838 /* 1839 * We dont need other sorting keys other than those 1840 * we already specified. It also really slows down 1841 * the processing a lot with big number of output 1842 * fields, so switching this off for c2c. 1843 */ 1844 1845 #if 0 1846 /* and then copy output fields to sort keys */ 1847 perf_hpp__append_sort_keys(&hists->list); 1848 #endif 1849 1850 free(output); 1851 free(sort); 1852 return ret; 1853 } 1854 1855 static int c2c_hists__init(struct c2c_hists *hists, 1856 const char *sort, 1857 int nr_header_lines) 1858 { 1859 __hists__init(&hists->hists, &hists->list); 1860 1861 /* 1862 * Initialize only with sort fields, we need to resort 1863 * later anyway, and that's where we add output fields 1864 * as well. 1865 */ 1866 perf_hpp_list__init(&hists->list); 1867 1868 /* Overload number of header lines.*/ 1869 hists->list.nr_header_lines = nr_header_lines; 1870 1871 return hpp_list__parse(&hists->list, NULL, sort); 1872 } 1873 1874 static int c2c_hists__reinit(struct c2c_hists *c2c_hists, 1875 const char *output, 1876 const char *sort) 1877 { 1878 perf_hpp__reset_output_field(&c2c_hists->list); 1879 return hpp_list__parse(&c2c_hists->list, output, sort); 1880 } 1881 1882 #define DISPLAY_LINE_LIMIT 0.001 1883 1884 static bool he__display(struct hist_entry *he, struct c2c_stats *stats) 1885 { 1886 struct c2c_hist_entry *c2c_he; 1887 double ld_dist; 1888 1889 if (c2c.show_all) 1890 return true; 1891 1892 c2c_he = container_of(he, struct c2c_hist_entry, he); 1893 1894 #define FILTER_HITM(__h) \ 1895 if (stats->__h) { \ 1896 ld_dist = ((double)c2c_he->stats.__h / stats->__h); \ 1897 if (ld_dist < DISPLAY_LINE_LIMIT) \ 1898 he->filtered = HIST_FILTER__C2C; \ 1899 } else { \ 1900 he->filtered = HIST_FILTER__C2C; \ 1901 } 1902 1903 switch (c2c.display) { 1904 case DISPLAY_LCL: 1905 FILTER_HITM(lcl_hitm); 1906 break; 1907 case DISPLAY_RMT: 1908 FILTER_HITM(rmt_hitm); 1909 break; 1910 case DISPLAY_TOT: 1911 FILTER_HITM(tot_hitm); 1912 default: 1913 break; 1914 }; 1915 1916 #undef FILTER_HITM 1917 1918 return he->filtered == 0; 1919 } 1920 1921 static inline int valid_hitm_or_store(struct hist_entry *he) 1922 { 1923 struct c2c_hist_entry *c2c_he; 1924 bool has_hitm; 1925 1926 c2c_he = container_of(he, struct c2c_hist_entry, he); 1927 has_hitm = c2c.display == DISPLAY_TOT ? c2c_he->stats.tot_hitm : 1928 c2c.display == DISPLAY_LCL ? c2c_he->stats.lcl_hitm : 1929 c2c_he->stats.rmt_hitm; 1930 return has_hitm || c2c_he->stats.store; 1931 } 1932 1933 static void set_node_width(struct c2c_hist_entry *c2c_he, int len) 1934 { 1935 struct c2c_dimension *dim; 1936 1937 dim = &c2c.hists == c2c_he->hists ? 1938 &dim_dcacheline_node : &dim_offset_node; 1939 1940 if (len > dim->width) 1941 dim->width = len; 1942 } 1943 1944 static int set_nodestr(struct c2c_hist_entry *c2c_he) 1945 { 1946 char buf[30]; 1947 int len; 1948 1949 if (c2c_he->nodestr) 1950 return 0; 1951 1952 if (bitmap_weight(c2c_he->nodeset, c2c.nodes_cnt)) { 1953 len = bitmap_scnprintf(c2c_he->nodeset, c2c.nodes_cnt, 1954 buf, sizeof(buf)); 1955 } else { 1956 len = scnprintf(buf, sizeof(buf), "N/A"); 1957 } 1958 1959 set_node_width(c2c_he, len); 1960 c2c_he->nodestr = strdup(buf); 1961 return c2c_he->nodestr ? 0 : -ENOMEM; 1962 } 1963 1964 static void calc_width(struct c2c_hist_entry *c2c_he) 1965 { 1966 struct c2c_hists *c2c_hists; 1967 1968 c2c_hists = container_of(c2c_he->he.hists, struct c2c_hists, hists); 1969 hists__calc_col_len(&c2c_hists->hists, &c2c_he->he); 1970 set_nodestr(c2c_he); 1971 } 1972 1973 static int filter_cb(struct hist_entry *he, void *arg __maybe_unused) 1974 { 1975 struct c2c_hist_entry *c2c_he; 1976 1977 c2c_he = container_of(he, struct c2c_hist_entry, he); 1978 1979 if (c2c.show_src && !he->srcline) 1980 he->srcline = hist_entry__srcline(he); 1981 1982 calc_width(c2c_he); 1983 1984 if (!valid_hitm_or_store(he)) 1985 he->filtered = HIST_FILTER__C2C; 1986 1987 return 0; 1988 } 1989 1990 static int resort_cl_cb(struct hist_entry *he, void *arg __maybe_unused) 1991 { 1992 struct c2c_hist_entry *c2c_he; 1993 struct c2c_hists *c2c_hists; 1994 bool display = he__display(he, &c2c.hitm_stats); 1995 1996 c2c_he = container_of(he, struct c2c_hist_entry, he); 1997 c2c_hists = c2c_he->hists; 1998 1999 if (display && c2c_hists) { 2000 static unsigned int idx; 2001 2002 c2c_he->cacheline_idx = idx++; 2003 calc_width(c2c_he); 2004 2005 c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort); 2006 2007 hists__collapse_resort(&c2c_hists->hists, NULL); 2008 hists__output_resort_cb(&c2c_hists->hists, NULL, filter_cb); 2009 } 2010 2011 return 0; 2012 } 2013 2014 static void setup_nodes_header(void) 2015 { 2016 dim_node.header = header_node[c2c.node_info]; 2017 } 2018 2019 static int setup_nodes(struct perf_session *session) 2020 { 2021 struct numa_node *n; 2022 unsigned long **nodes; 2023 int node, cpu; 2024 int *cpu2node; 2025 2026 if (c2c.node_info > 2) 2027 c2c.node_info = 2; 2028 2029 c2c.nodes_cnt = session->header.env.nr_numa_nodes; 2030 c2c.cpus_cnt = session->header.env.nr_cpus_online; 2031 2032 n = session->header.env.numa_nodes; 2033 if (!n) 2034 return -EINVAL; 2035 2036 nodes = zalloc(sizeof(unsigned long *) * c2c.nodes_cnt); 2037 if (!nodes) 2038 return -ENOMEM; 2039 2040 c2c.nodes = nodes; 2041 2042 cpu2node = zalloc(sizeof(int) * c2c.cpus_cnt); 2043 if (!cpu2node) 2044 return -ENOMEM; 2045 2046 for (cpu = 0; cpu < c2c.cpus_cnt; cpu++) 2047 cpu2node[cpu] = -1; 2048 2049 c2c.cpu2node = cpu2node; 2050 2051 for (node = 0; node < c2c.nodes_cnt; node++) { 2052 struct cpu_map *map = n[node].map; 2053 unsigned long *set; 2054 2055 set = bitmap_alloc(c2c.cpus_cnt); 2056 if (!set) 2057 return -ENOMEM; 2058 2059 for (cpu = 0; cpu < map->nr; cpu++) { 2060 set_bit(map->map[cpu], set); 2061 2062 if (WARN_ONCE(cpu2node[map->map[cpu]] != -1, "node/cpu topology bug")) 2063 return -EINVAL; 2064 2065 cpu2node[map->map[cpu]] = node; 2066 } 2067 2068 nodes[node] = set; 2069 } 2070 2071 setup_nodes_header(); 2072 return 0; 2073 } 2074 2075 #define HAS_HITMS(__h) ((__h)->stats.lcl_hitm || (__h)->stats.rmt_hitm) 2076 2077 static int resort_hitm_cb(struct hist_entry *he, void *arg __maybe_unused) 2078 { 2079 struct c2c_hist_entry *c2c_he; 2080 c2c_he = container_of(he, struct c2c_hist_entry, he); 2081 2082 if (HAS_HITMS(c2c_he)) { 2083 c2c.shared_clines++; 2084 c2c_add_stats(&c2c.hitm_stats, &c2c_he->stats); 2085 } 2086 2087 return 0; 2088 } 2089 2090 static int hists__iterate_cb(struct hists *hists, hists__resort_cb_t cb) 2091 { 2092 struct rb_node *next = rb_first_cached(&hists->entries); 2093 int ret = 0; 2094 2095 while (next) { 2096 struct hist_entry *he; 2097 2098 he = rb_entry(next, struct hist_entry, rb_node); 2099 ret = cb(he, NULL); 2100 if (ret) 2101 break; 2102 next = rb_next(&he->rb_node); 2103 } 2104 2105 return ret; 2106 } 2107 2108 static void print_c2c__display_stats(FILE *out) 2109 { 2110 int llc_misses; 2111 struct c2c_stats *stats = &c2c.hists.stats; 2112 2113 llc_misses = stats->lcl_dram + 2114 stats->rmt_dram + 2115 stats->rmt_hit + 2116 stats->rmt_hitm; 2117 2118 fprintf(out, "=================================================\n"); 2119 fprintf(out, " Trace Event Information \n"); 2120 fprintf(out, "=================================================\n"); 2121 fprintf(out, " Total records : %10d\n", stats->nr_entries); 2122 fprintf(out, " Locked Load/Store Operations : %10d\n", stats->locks); 2123 fprintf(out, " Load Operations : %10d\n", stats->load); 2124 fprintf(out, " Loads - uncacheable : %10d\n", stats->ld_uncache); 2125 fprintf(out, " Loads - IO : %10d\n", stats->ld_io); 2126 fprintf(out, " Loads - Miss : %10d\n", stats->ld_miss); 2127 fprintf(out, " Loads - no mapping : %10d\n", stats->ld_noadrs); 2128 fprintf(out, " Load Fill Buffer Hit : %10d\n", stats->ld_fbhit); 2129 fprintf(out, " Load L1D hit : %10d\n", stats->ld_l1hit); 2130 fprintf(out, " Load L2D hit : %10d\n", stats->ld_l2hit); 2131 fprintf(out, " Load LLC hit : %10d\n", stats->ld_llchit + stats->lcl_hitm); 2132 fprintf(out, " Load Local HITM : %10d\n", stats->lcl_hitm); 2133 fprintf(out, " Load Remote HITM : %10d\n", stats->rmt_hitm); 2134 fprintf(out, " Load Remote HIT : %10d\n", stats->rmt_hit); 2135 fprintf(out, " Load Local DRAM : %10d\n", stats->lcl_dram); 2136 fprintf(out, " Load Remote DRAM : %10d\n", stats->rmt_dram); 2137 fprintf(out, " Load MESI State Exclusive : %10d\n", stats->ld_excl); 2138 fprintf(out, " Load MESI State Shared : %10d\n", stats->ld_shared); 2139 fprintf(out, " Load LLC Misses : %10d\n", llc_misses); 2140 fprintf(out, " LLC Misses to Local DRAM : %10.1f%%\n", ((double)stats->lcl_dram/(double)llc_misses) * 100.); 2141 fprintf(out, " LLC Misses to Remote DRAM : %10.1f%%\n", ((double)stats->rmt_dram/(double)llc_misses) * 100.); 2142 fprintf(out, " LLC Misses to Remote cache (HIT) : %10.1f%%\n", ((double)stats->rmt_hit /(double)llc_misses) * 100.); 2143 fprintf(out, " LLC Misses to Remote cache (HITM) : %10.1f%%\n", ((double)stats->rmt_hitm/(double)llc_misses) * 100.); 2144 fprintf(out, " Store Operations : %10d\n", stats->store); 2145 fprintf(out, " Store - uncacheable : %10d\n", stats->st_uncache); 2146 fprintf(out, " Store - no mapping : %10d\n", stats->st_noadrs); 2147 fprintf(out, " Store L1D Hit : %10d\n", stats->st_l1hit); 2148 fprintf(out, " Store L1D Miss : %10d\n", stats->st_l1miss); 2149 fprintf(out, " No Page Map Rejects : %10d\n", stats->nomap); 2150 fprintf(out, " Unable to parse data source : %10d\n", stats->noparse); 2151 } 2152 2153 static void print_shared_cacheline_info(FILE *out) 2154 { 2155 struct c2c_stats *stats = &c2c.hitm_stats; 2156 int hitm_cnt = stats->lcl_hitm + stats->rmt_hitm; 2157 2158 fprintf(out, "=================================================\n"); 2159 fprintf(out, " Global Shared Cache Line Event Information \n"); 2160 fprintf(out, "=================================================\n"); 2161 fprintf(out, " Total Shared Cache Lines : %10d\n", c2c.shared_clines); 2162 fprintf(out, " Load HITs on shared lines : %10d\n", stats->load); 2163 fprintf(out, " Fill Buffer Hits on shared lines : %10d\n", stats->ld_fbhit); 2164 fprintf(out, " L1D hits on shared lines : %10d\n", stats->ld_l1hit); 2165 fprintf(out, " L2D hits on shared lines : %10d\n", stats->ld_l2hit); 2166 fprintf(out, " LLC hits on shared lines : %10d\n", stats->ld_llchit + stats->lcl_hitm); 2167 fprintf(out, " Locked Access on shared lines : %10d\n", stats->locks); 2168 fprintf(out, " Store HITs on shared lines : %10d\n", stats->store); 2169 fprintf(out, " Store L1D hits on shared lines : %10d\n", stats->st_l1hit); 2170 fprintf(out, " Total Merged records : %10d\n", hitm_cnt + stats->store); 2171 } 2172 2173 static void print_cacheline(struct c2c_hists *c2c_hists, 2174 struct hist_entry *he_cl, 2175 struct perf_hpp_list *hpp_list, 2176 FILE *out) 2177 { 2178 char bf[1000]; 2179 struct perf_hpp hpp = { 2180 .buf = bf, 2181 .size = 1000, 2182 }; 2183 static bool once; 2184 2185 if (!once) { 2186 hists__fprintf_headers(&c2c_hists->hists, out); 2187 once = true; 2188 } else { 2189 fprintf(out, "\n"); 2190 } 2191 2192 fprintf(out, " -------------------------------------------------------------\n"); 2193 __hist_entry__snprintf(he_cl, &hpp, hpp_list); 2194 fprintf(out, "%s\n", bf); 2195 fprintf(out, " -------------------------------------------------------------\n"); 2196 2197 hists__fprintf(&c2c_hists->hists, false, 0, 0, 0, out, false); 2198 } 2199 2200 static void print_pareto(FILE *out) 2201 { 2202 struct perf_hpp_list hpp_list; 2203 struct rb_node *nd; 2204 int ret; 2205 2206 perf_hpp_list__init(&hpp_list); 2207 ret = hpp_list__parse(&hpp_list, 2208 "cl_num," 2209 "cl_rmt_hitm," 2210 "cl_lcl_hitm," 2211 "cl_stores_l1hit," 2212 "cl_stores_l1miss," 2213 "dcacheline", 2214 NULL); 2215 2216 if (WARN_ONCE(ret, "failed to setup sort entries\n")) 2217 return; 2218 2219 nd = rb_first_cached(&c2c.hists.hists.entries); 2220 2221 for (; nd; nd = rb_next(nd)) { 2222 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 2223 struct c2c_hist_entry *c2c_he; 2224 2225 if (he->filtered) 2226 continue; 2227 2228 c2c_he = container_of(he, struct c2c_hist_entry, he); 2229 print_cacheline(c2c_he->hists, he, &hpp_list, out); 2230 } 2231 } 2232 2233 static void print_c2c_info(FILE *out, struct perf_session *session) 2234 { 2235 struct perf_evlist *evlist = session->evlist; 2236 struct perf_evsel *evsel; 2237 bool first = true; 2238 2239 fprintf(out, "=================================================\n"); 2240 fprintf(out, " c2c details \n"); 2241 fprintf(out, "=================================================\n"); 2242 2243 evlist__for_each_entry(evlist, evsel) { 2244 fprintf(out, "%-36s: %s\n", first ? " Events" : "", 2245 perf_evsel__name(evsel)); 2246 first = false; 2247 } 2248 fprintf(out, " Cachelines sort on : %s HITMs\n", 2249 display_str[c2c.display]); 2250 fprintf(out, " Cacheline data grouping : %s\n", c2c.cl_sort); 2251 } 2252 2253 static void perf_c2c__hists_fprintf(FILE *out, struct perf_session *session) 2254 { 2255 setup_pager(); 2256 2257 print_c2c__display_stats(out); 2258 fprintf(out, "\n"); 2259 print_shared_cacheline_info(out); 2260 fprintf(out, "\n"); 2261 print_c2c_info(out, session); 2262 2263 if (c2c.stats_only) 2264 return; 2265 2266 fprintf(out, "\n"); 2267 fprintf(out, "=================================================\n"); 2268 fprintf(out, " Shared Data Cache Line Table \n"); 2269 fprintf(out, "=================================================\n"); 2270 fprintf(out, "#\n"); 2271 2272 hists__fprintf(&c2c.hists.hists, true, 0, 0, 0, stdout, true); 2273 2274 fprintf(out, "\n"); 2275 fprintf(out, "=================================================\n"); 2276 fprintf(out, " Shared Cache Line Distribution Pareto \n"); 2277 fprintf(out, "=================================================\n"); 2278 fprintf(out, "#\n"); 2279 2280 print_pareto(out); 2281 } 2282 2283 #ifdef HAVE_SLANG_SUPPORT 2284 static void c2c_browser__update_nr_entries(struct hist_browser *hb) 2285 { 2286 u64 nr_entries = 0; 2287 struct rb_node *nd = rb_first_cached(&hb->hists->entries); 2288 2289 while (nd) { 2290 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 2291 2292 if (!he->filtered) 2293 nr_entries++; 2294 2295 nd = rb_next(nd); 2296 } 2297 2298 hb->nr_non_filtered_entries = nr_entries; 2299 } 2300 2301 struct c2c_cacheline_browser { 2302 struct hist_browser hb; 2303 struct hist_entry *he; 2304 }; 2305 2306 static int 2307 perf_c2c_cacheline_browser__title(struct hist_browser *browser, 2308 char *bf, size_t size) 2309 { 2310 struct c2c_cacheline_browser *cl_browser; 2311 struct hist_entry *he; 2312 uint64_t addr = 0; 2313 2314 cl_browser = container_of(browser, struct c2c_cacheline_browser, hb); 2315 he = cl_browser->he; 2316 2317 if (he->mem_info) 2318 addr = cl_address(he->mem_info->daddr.addr); 2319 2320 scnprintf(bf, size, "Cacheline 0x%lx", addr); 2321 return 0; 2322 } 2323 2324 static struct c2c_cacheline_browser* 2325 c2c_cacheline_browser__new(struct hists *hists, struct hist_entry *he) 2326 { 2327 struct c2c_cacheline_browser *browser; 2328 2329 browser = zalloc(sizeof(*browser)); 2330 if (browser) { 2331 hist_browser__init(&browser->hb, hists); 2332 browser->hb.c2c_filter = true; 2333 browser->hb.title = perf_c2c_cacheline_browser__title; 2334 browser->he = he; 2335 } 2336 2337 return browser; 2338 } 2339 2340 static int perf_c2c__browse_cacheline(struct hist_entry *he) 2341 { 2342 struct c2c_hist_entry *c2c_he; 2343 struct c2c_hists *c2c_hists; 2344 struct c2c_cacheline_browser *cl_browser; 2345 struct hist_browser *browser; 2346 int key = -1; 2347 static const char help[] = 2348 " ENTER Toggle callchains (if present) \n" 2349 " n Toggle Node details info \n" 2350 " s Toggle full length of symbol and source line columns \n" 2351 " q Return back to cacheline list \n"; 2352 2353 if (!he) 2354 return 0; 2355 2356 /* Display compact version first. */ 2357 c2c.symbol_full = false; 2358 2359 c2c_he = container_of(he, struct c2c_hist_entry, he); 2360 c2c_hists = c2c_he->hists; 2361 2362 cl_browser = c2c_cacheline_browser__new(&c2c_hists->hists, he); 2363 if (cl_browser == NULL) 2364 return -1; 2365 2366 browser = &cl_browser->hb; 2367 2368 /* reset abort key so that it can get Ctrl-C as a key */ 2369 SLang_reset_tty(); 2370 SLang_init_tty(0, 0, 0); 2371 2372 c2c_browser__update_nr_entries(browser); 2373 2374 while (1) { 2375 key = hist_browser__run(browser, "? - help", true); 2376 2377 switch (key) { 2378 case 's': 2379 c2c.symbol_full = !c2c.symbol_full; 2380 break; 2381 case 'n': 2382 c2c.node_info = (c2c.node_info + 1) % 3; 2383 setup_nodes_header(); 2384 break; 2385 case 'q': 2386 goto out; 2387 case '?': 2388 ui_browser__help_window(&browser->b, help); 2389 break; 2390 default: 2391 break; 2392 } 2393 } 2394 2395 out: 2396 free(cl_browser); 2397 return 0; 2398 } 2399 2400 static int perf_c2c_browser__title(struct hist_browser *browser, 2401 char *bf, size_t size) 2402 { 2403 scnprintf(bf, size, 2404 "Shared Data Cache Line Table " 2405 "(%lu entries, sorted on %s HITMs)", 2406 browser->nr_non_filtered_entries, 2407 display_str[c2c.display]); 2408 return 0; 2409 } 2410 2411 static struct hist_browser* 2412 perf_c2c_browser__new(struct hists *hists) 2413 { 2414 struct hist_browser *browser = hist_browser__new(hists); 2415 2416 if (browser) { 2417 browser->title = perf_c2c_browser__title; 2418 browser->c2c_filter = true; 2419 } 2420 2421 return browser; 2422 } 2423 2424 static int perf_c2c__hists_browse(struct hists *hists) 2425 { 2426 struct hist_browser *browser; 2427 int key = -1; 2428 static const char help[] = 2429 " d Display cacheline details \n" 2430 " ENTER Toggle callchains (if present) \n" 2431 " q Quit \n"; 2432 2433 browser = perf_c2c_browser__new(hists); 2434 if (browser == NULL) 2435 return -1; 2436 2437 /* reset abort key so that it can get Ctrl-C as a key */ 2438 SLang_reset_tty(); 2439 SLang_init_tty(0, 0, 0); 2440 2441 c2c_browser__update_nr_entries(browser); 2442 2443 while (1) { 2444 key = hist_browser__run(browser, "? - help", true); 2445 2446 switch (key) { 2447 case 'q': 2448 goto out; 2449 case 'd': 2450 perf_c2c__browse_cacheline(browser->he_selection); 2451 break; 2452 case '?': 2453 ui_browser__help_window(&browser->b, help); 2454 break; 2455 default: 2456 break; 2457 } 2458 } 2459 2460 out: 2461 hist_browser__delete(browser); 2462 return 0; 2463 } 2464 2465 static void perf_c2c_display(struct perf_session *session) 2466 { 2467 if (use_browser == 0) 2468 perf_c2c__hists_fprintf(stdout, session); 2469 else 2470 perf_c2c__hists_browse(&c2c.hists.hists); 2471 } 2472 #else 2473 static void perf_c2c_display(struct perf_session *session) 2474 { 2475 use_browser = 0; 2476 perf_c2c__hists_fprintf(stdout, session); 2477 } 2478 #endif /* HAVE_SLANG_SUPPORT */ 2479 2480 static char *fill_line(const char *orig, int len) 2481 { 2482 int i, j, olen = strlen(orig); 2483 char *buf; 2484 2485 buf = zalloc(len + 1); 2486 if (!buf) 2487 return NULL; 2488 2489 j = len / 2 - olen / 2; 2490 2491 for (i = 0; i < j - 1; i++) 2492 buf[i] = '-'; 2493 2494 buf[i++] = ' '; 2495 2496 strcpy(buf + i, orig); 2497 2498 i += olen; 2499 2500 buf[i++] = ' '; 2501 2502 for (; i < len; i++) 2503 buf[i] = '-'; 2504 2505 return buf; 2506 } 2507 2508 static int ui_quirks(void) 2509 { 2510 const char *nodestr = "Data address"; 2511 char *buf; 2512 2513 if (!c2c.use_stdio) { 2514 dim_offset.width = 5; 2515 dim_offset.header = header_offset_tui; 2516 nodestr = "CL"; 2517 } 2518 2519 dim_percent_hitm.header = percent_hitm_header[c2c.display]; 2520 2521 /* Fix the zero line for dcacheline column. */ 2522 buf = fill_line("Cacheline", dim_dcacheline.width + 2523 dim_dcacheline_node.width + 2524 dim_dcacheline_count.width + 4); 2525 if (!buf) 2526 return -ENOMEM; 2527 2528 dim_dcacheline.header.line[0].text = buf; 2529 2530 /* Fix the zero line for offset column. */ 2531 buf = fill_line(nodestr, dim_offset.width + 2532 dim_offset_node.width + 2533 dim_dcacheline_count.width + 4); 2534 if (!buf) 2535 return -ENOMEM; 2536 2537 dim_offset.header.line[0].text = buf; 2538 2539 return 0; 2540 } 2541 2542 #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent" 2543 2544 const char callchain_help[] = "Display call graph (stack chain/backtrace):\n\n" 2545 CALLCHAIN_REPORT_HELP 2546 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT; 2547 2548 static int 2549 parse_callchain_opt(const struct option *opt, const char *arg, int unset) 2550 { 2551 struct callchain_param *callchain = opt->value; 2552 2553 callchain->enabled = !unset; 2554 /* 2555 * --no-call-graph 2556 */ 2557 if (unset) { 2558 symbol_conf.use_callchain = false; 2559 callchain->mode = CHAIN_NONE; 2560 return 0; 2561 } 2562 2563 return parse_callchain_report_opt(arg); 2564 } 2565 2566 static int setup_callchain(struct perf_evlist *evlist) 2567 { 2568 u64 sample_type = perf_evlist__combined_sample_type(evlist); 2569 enum perf_call_graph_mode mode = CALLCHAIN_NONE; 2570 2571 if ((sample_type & PERF_SAMPLE_REGS_USER) && 2572 (sample_type & PERF_SAMPLE_STACK_USER)) { 2573 mode = CALLCHAIN_DWARF; 2574 dwarf_callchain_users = true; 2575 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK) 2576 mode = CALLCHAIN_LBR; 2577 else if (sample_type & PERF_SAMPLE_CALLCHAIN) 2578 mode = CALLCHAIN_FP; 2579 2580 if (!callchain_param.enabled && 2581 callchain_param.mode != CHAIN_NONE && 2582 mode != CALLCHAIN_NONE) { 2583 symbol_conf.use_callchain = true; 2584 if (callchain_register_param(&callchain_param) < 0) { 2585 ui__error("Can't register callchain params.\n"); 2586 return -EINVAL; 2587 } 2588 } 2589 2590 callchain_param.record_mode = mode; 2591 callchain_param.min_percent = 0; 2592 return 0; 2593 } 2594 2595 static int setup_display(const char *str) 2596 { 2597 const char *display = str ?: "tot"; 2598 2599 if (!strcmp(display, "tot")) 2600 c2c.display = DISPLAY_TOT; 2601 else if (!strcmp(display, "rmt")) 2602 c2c.display = DISPLAY_RMT; 2603 else if (!strcmp(display, "lcl")) 2604 c2c.display = DISPLAY_LCL; 2605 else { 2606 pr_err("failed: unknown display type: %s\n", str); 2607 return -1; 2608 } 2609 2610 return 0; 2611 } 2612 2613 #define for_each_token(__tok, __buf, __sep, __tmp) \ 2614 for (__tok = strtok_r(__buf, __sep, &__tmp); __tok; \ 2615 __tok = strtok_r(NULL, __sep, &__tmp)) 2616 2617 static int build_cl_output(char *cl_sort, bool no_source) 2618 { 2619 char *tok, *tmp, *buf = strdup(cl_sort); 2620 bool add_pid = false; 2621 bool add_tid = false; 2622 bool add_iaddr = false; 2623 bool add_sym = false; 2624 bool add_dso = false; 2625 bool add_src = false; 2626 2627 if (!buf) 2628 return -ENOMEM; 2629 2630 for_each_token(tok, buf, ",", tmp) { 2631 if (!strcmp(tok, "tid")) { 2632 add_tid = true; 2633 } else if (!strcmp(tok, "pid")) { 2634 add_pid = true; 2635 } else if (!strcmp(tok, "iaddr")) { 2636 add_iaddr = true; 2637 add_sym = true; 2638 add_dso = true; 2639 add_src = no_source ? false : true; 2640 } else if (!strcmp(tok, "dso")) { 2641 add_dso = true; 2642 } else if (strcmp(tok, "offset")) { 2643 pr_err("unrecognized sort token: %s\n", tok); 2644 return -EINVAL; 2645 } 2646 } 2647 2648 if (asprintf(&c2c.cl_output, 2649 "%s%s%s%s%s%s%s%s%s%s", 2650 c2c.use_stdio ? "cl_num_empty," : "", 2651 "percent_rmt_hitm," 2652 "percent_lcl_hitm," 2653 "percent_stores_l1hit," 2654 "percent_stores_l1miss," 2655 "offset,offset_node,dcacheline_count,", 2656 add_pid ? "pid," : "", 2657 add_tid ? "tid," : "", 2658 add_iaddr ? "iaddr," : "", 2659 "mean_rmt," 2660 "mean_lcl," 2661 "mean_load," 2662 "tot_recs," 2663 "cpucnt,", 2664 add_sym ? "symbol," : "", 2665 add_dso ? "dso," : "", 2666 add_src ? "cl_srcline," : "", 2667 "node") < 0) 2668 return -ENOMEM; 2669 2670 c2c.show_src = add_src; 2671 2672 free(buf); 2673 return 0; 2674 } 2675 2676 static int setup_coalesce(const char *coalesce, bool no_source) 2677 { 2678 const char *c = coalesce ?: coalesce_default; 2679 2680 if (asprintf(&c2c.cl_sort, "offset,%s", c) < 0) 2681 return -ENOMEM; 2682 2683 if (build_cl_output(c2c.cl_sort, no_source)) 2684 return -1; 2685 2686 if (asprintf(&c2c.cl_resort, "offset,%s", 2687 c2c.display == DISPLAY_TOT ? 2688 "tot_hitm" : 2689 c2c.display == DISPLAY_RMT ? 2690 "rmt_hitm,lcl_hitm" : 2691 "lcl_hitm,rmt_hitm") < 0) 2692 return -ENOMEM; 2693 2694 pr_debug("coalesce sort fields: %s\n", c2c.cl_sort); 2695 pr_debug("coalesce resort fields: %s\n", c2c.cl_resort); 2696 pr_debug("coalesce output fields: %s\n", c2c.cl_output); 2697 return 0; 2698 } 2699 2700 static int perf_c2c__report(int argc, const char **argv) 2701 { 2702 struct perf_session *session; 2703 struct ui_progress prog; 2704 struct perf_data data = { 2705 .mode = PERF_DATA_MODE_READ, 2706 }; 2707 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT; 2708 const char *display = NULL; 2709 const char *coalesce = NULL; 2710 bool no_source = false; 2711 const struct option options[] = { 2712 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 2713 "file", "vmlinux pathname"), 2714 OPT_STRING('i', "input", &input_name, "file", 2715 "the input file to process"), 2716 OPT_INCR('N', "node-info", &c2c.node_info, 2717 "show extra node info in report (repeat for more info)"), 2718 #ifdef HAVE_SLANG_SUPPORT 2719 OPT_BOOLEAN(0, "stdio", &c2c.use_stdio, "Use the stdio interface"), 2720 #endif 2721 OPT_BOOLEAN(0, "stats", &c2c.stats_only, 2722 "Display only statistic tables (implies --stdio)"), 2723 OPT_BOOLEAN(0, "full-symbols", &c2c.symbol_full, 2724 "Display full length of symbols"), 2725 OPT_BOOLEAN(0, "no-source", &no_source, 2726 "Do not display Source Line column"), 2727 OPT_BOOLEAN(0, "show-all", &c2c.show_all, 2728 "Show all captured HITM lines."), 2729 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param, 2730 "print_type,threshold[,print_limit],order,sort_key[,branch],value", 2731 callchain_help, &parse_callchain_opt, 2732 callchain_default_opt), 2733 OPT_STRING('d', "display", &display, "Switch HITM output type", "lcl,rmt"), 2734 OPT_STRING('c', "coalesce", &coalesce, "coalesce fields", 2735 "coalesce fields: pid,tid,iaddr,dso"), 2736 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"), 2737 OPT_PARENT(c2c_options), 2738 OPT_END() 2739 }; 2740 int err = 0; 2741 2742 argc = parse_options(argc, argv, options, report_c2c_usage, 2743 PARSE_OPT_STOP_AT_NON_OPTION); 2744 if (argc) 2745 usage_with_options(report_c2c_usage, options); 2746 2747 if (c2c.stats_only) 2748 c2c.use_stdio = true; 2749 2750 if (!input_name || !strlen(input_name)) 2751 input_name = "perf.data"; 2752 2753 data.path = input_name; 2754 data.force = symbol_conf.force; 2755 2756 err = setup_display(display); 2757 if (err) 2758 goto out; 2759 2760 err = setup_coalesce(coalesce, no_source); 2761 if (err) { 2762 pr_debug("Failed to initialize hists\n"); 2763 goto out; 2764 } 2765 2766 err = c2c_hists__init(&c2c.hists, "dcacheline", 2); 2767 if (err) { 2768 pr_debug("Failed to initialize hists\n"); 2769 goto out; 2770 } 2771 2772 session = perf_session__new(&data, 0, &c2c.tool); 2773 if (session == NULL) { 2774 pr_debug("No memory for session\n"); 2775 goto out; 2776 } 2777 2778 err = setup_nodes(session); 2779 if (err) { 2780 pr_err("Failed setup nodes\n"); 2781 goto out; 2782 } 2783 2784 err = mem2node__init(&c2c.mem2node, &session->header.env); 2785 if (err) 2786 goto out_session; 2787 2788 err = setup_callchain(session->evlist); 2789 if (err) 2790 goto out_mem2node; 2791 2792 if (symbol__init(&session->header.env) < 0) 2793 goto out_mem2node; 2794 2795 /* No pipe support at the moment. */ 2796 if (perf_data__is_pipe(session->data)) { 2797 pr_debug("No pipe support at the moment.\n"); 2798 goto out_mem2node; 2799 } 2800 2801 if (c2c.use_stdio) 2802 use_browser = 0; 2803 else 2804 use_browser = 1; 2805 2806 setup_browser(false); 2807 2808 err = perf_session__process_events(session); 2809 if (err) { 2810 pr_err("failed to process sample\n"); 2811 goto out_mem2node; 2812 } 2813 2814 c2c_hists__reinit(&c2c.hists, 2815 "cl_idx," 2816 "dcacheline," 2817 "dcacheline_node," 2818 "dcacheline_count," 2819 "tot_recs," 2820 "percent_hitm," 2821 "tot_hitm,lcl_hitm,rmt_hitm," 2822 "stores,stores_l1hit,stores_l1miss," 2823 "dram_lcl,dram_rmt," 2824 "ld_llcmiss," 2825 "tot_loads," 2826 "ld_fbhit,ld_l1hit,ld_l2hit," 2827 "ld_lclhit,ld_rmthit", 2828 c2c.display == DISPLAY_TOT ? "tot_hitm" : 2829 c2c.display == DISPLAY_LCL ? "lcl_hitm" : "rmt_hitm" 2830 ); 2831 2832 ui_progress__init(&prog, c2c.hists.hists.nr_entries, "Sorting..."); 2833 2834 hists__collapse_resort(&c2c.hists.hists, NULL); 2835 hists__output_resort_cb(&c2c.hists.hists, &prog, resort_hitm_cb); 2836 hists__iterate_cb(&c2c.hists.hists, resort_cl_cb); 2837 2838 ui_progress__finish(); 2839 2840 if (ui_quirks()) { 2841 pr_err("failed to setup UI\n"); 2842 goto out_mem2node; 2843 } 2844 2845 perf_c2c_display(session); 2846 2847 out_mem2node: 2848 mem2node__exit(&c2c.mem2node); 2849 out_session: 2850 perf_session__delete(session); 2851 out: 2852 return err; 2853 } 2854 2855 static int parse_record_events(const struct option *opt, 2856 const char *str, int unset __maybe_unused) 2857 { 2858 bool *event_set = (bool *) opt->value; 2859 2860 *event_set = true; 2861 return perf_mem_events__parse(str); 2862 } 2863 2864 2865 static const char * const __usage_record[] = { 2866 "perf c2c record [<options>] [<command>]", 2867 "perf c2c record [<options>] -- <command> [<options>]", 2868 NULL 2869 }; 2870 2871 static const char * const *record_mem_usage = __usage_record; 2872 2873 static int perf_c2c__record(int argc, const char **argv) 2874 { 2875 int rec_argc, i = 0, j; 2876 const char **rec_argv; 2877 int ret; 2878 bool all_user = false, all_kernel = false; 2879 bool event_set = false; 2880 struct option options[] = { 2881 OPT_CALLBACK('e', "event", &event_set, "event", 2882 "event selector. Use 'perf mem record -e list' to list available events", 2883 parse_record_events), 2884 OPT_BOOLEAN('u', "all-user", &all_user, "collect only user level data"), 2885 OPT_BOOLEAN('k', "all-kernel", &all_kernel, "collect only kernel level data"), 2886 OPT_UINTEGER('l', "ldlat", &perf_mem_events__loads_ldlat, "setup mem-loads latency"), 2887 OPT_PARENT(c2c_options), 2888 OPT_END() 2889 }; 2890 2891 if (perf_mem_events__init()) { 2892 pr_err("failed: memory events not supported\n"); 2893 return -1; 2894 } 2895 2896 argc = parse_options(argc, argv, options, record_mem_usage, 2897 PARSE_OPT_KEEP_UNKNOWN); 2898 2899 rec_argc = argc + 11; /* max number of arguments */ 2900 rec_argv = calloc(rec_argc + 1, sizeof(char *)); 2901 if (!rec_argv) 2902 return -1; 2903 2904 rec_argv[i++] = "record"; 2905 2906 if (!event_set) { 2907 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true; 2908 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true; 2909 } 2910 2911 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record) 2912 rec_argv[i++] = "-W"; 2913 2914 rec_argv[i++] = "-d"; 2915 rec_argv[i++] = "--phys-data"; 2916 rec_argv[i++] = "--sample-cpu"; 2917 2918 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { 2919 if (!perf_mem_events[j].record) 2920 continue; 2921 2922 if (!perf_mem_events[j].supported) { 2923 pr_err("failed: event '%s' not supported\n", 2924 perf_mem_events[j].name); 2925 free(rec_argv); 2926 return -1; 2927 } 2928 2929 rec_argv[i++] = "-e"; 2930 rec_argv[i++] = perf_mem_events__name(j); 2931 }; 2932 2933 if (all_user) 2934 rec_argv[i++] = "--all-user"; 2935 2936 if (all_kernel) 2937 rec_argv[i++] = "--all-kernel"; 2938 2939 for (j = 0; j < argc; j++, i++) 2940 rec_argv[i] = argv[j]; 2941 2942 if (verbose > 0) { 2943 pr_debug("calling: "); 2944 2945 j = 0; 2946 2947 while (rec_argv[j]) { 2948 pr_debug("%s ", rec_argv[j]); 2949 j++; 2950 } 2951 pr_debug("\n"); 2952 } 2953 2954 ret = cmd_record(i, rec_argv); 2955 free(rec_argv); 2956 return ret; 2957 } 2958 2959 int cmd_c2c(int argc, const char **argv) 2960 { 2961 argc = parse_options(argc, argv, c2c_options, c2c_usage, 2962 PARSE_OPT_STOP_AT_NON_OPTION); 2963 2964 if (!argc) 2965 usage_with_options(c2c_usage, c2c_options); 2966 2967 if (!strncmp(argv[0], "rec", 3)) { 2968 return perf_c2c__record(argc, argv); 2969 } else if (!strncmp(argv[0], "rep", 3)) { 2970 return perf_c2c__report(argc, argv); 2971 } else { 2972 usage_with_options(c2c_usage, c2c_options); 2973 } 2974 2975 return 0; 2976 } 2977