1 // SPDX-License-Identifier: GPL-2.0 2 #include "callchain.h" 3 #include "debug.h" 4 #include "build-id.h" 5 #include "hist.h" 6 #include "map.h" 7 #include "session.h" 8 #include "namespaces.h" 9 #include "sort.h" 10 #include "units.h" 11 #include "evlist.h" 12 #include "evsel.h" 13 #include "annotate.h" 14 #include "srcline.h" 15 #include "symbol.h" 16 #include "thread.h" 17 #include "ui/progress.h" 18 #include <errno.h> 19 #include <math.h> 20 #include <inttypes.h> 21 #include <sys/param.h> 22 #include <linux/string.h> 23 #include <linux/time64.h> 24 #include <linux/zalloc.h> 25 26 static bool hists__filter_entry_by_dso(struct hists *hists, 27 struct hist_entry *he); 28 static bool hists__filter_entry_by_thread(struct hists *hists, 29 struct hist_entry *he); 30 static bool hists__filter_entry_by_symbol(struct hists *hists, 31 struct hist_entry *he); 32 static bool hists__filter_entry_by_socket(struct hists *hists, 33 struct hist_entry *he); 34 35 u16 hists__col_len(struct hists *hists, enum hist_column col) 36 { 37 return hists->col_len[col]; 38 } 39 40 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len) 41 { 42 hists->col_len[col] = len; 43 } 44 45 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) 46 { 47 if (len > hists__col_len(hists, col)) { 48 hists__set_col_len(hists, col, len); 49 return true; 50 } 51 return false; 52 } 53 54 void hists__reset_col_len(struct hists *hists) 55 { 56 enum hist_column col; 57 58 for (col = 0; col < HISTC_NR_COLS; ++col) 59 hists__set_col_len(hists, col, 0); 60 } 61 62 static void hists__set_unres_dso_col_len(struct hists *hists, int dso) 63 { 64 const unsigned int unresolved_col_width = BITS_PER_LONG / 4; 65 66 if (hists__col_len(hists, dso) < unresolved_col_width && 67 !symbol_conf.col_width_list_str && !symbol_conf.field_sep && 68 !symbol_conf.dso_list) 69 hists__set_col_len(hists, dso, unresolved_col_width); 70 } 71 72 void hists__calc_col_len(struct hists *hists, struct hist_entry *h) 73 { 74 const unsigned int unresolved_col_width = BITS_PER_LONG / 4; 75 int symlen; 76 u16 len; 77 78 /* 79 * +4 accounts for '[x] ' priv level info 80 * +2 accounts for 0x prefix on raw addresses 81 * +3 accounts for ' y ' symtab origin info 82 */ 83 if (h->ms.sym) { 84 symlen = h->ms.sym->namelen + 4; 85 if (verbose > 0) 86 symlen += BITS_PER_LONG / 4 + 2 + 3; 87 hists__new_col_len(hists, HISTC_SYMBOL, symlen); 88 } else { 89 symlen = unresolved_col_width + 4 + 2; 90 hists__new_col_len(hists, HISTC_SYMBOL, symlen); 91 hists__set_unres_dso_col_len(hists, HISTC_DSO); 92 } 93 94 len = thread__comm_len(h->thread); 95 if (hists__new_col_len(hists, HISTC_COMM, len)) 96 hists__set_col_len(hists, HISTC_THREAD, len + 8); 97 98 if (h->ms.map) { 99 len = dso__name_len(h->ms.map->dso); 100 hists__new_col_len(hists, HISTC_DSO, len); 101 } 102 103 if (h->parent) 104 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen); 105 106 if (h->branch_info) { 107 if (h->branch_info->from.sym) { 108 symlen = (int)h->branch_info->from.sym->namelen + 4; 109 if (verbose > 0) 110 symlen += BITS_PER_LONG / 4 + 2 + 3; 111 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); 112 113 symlen = dso__name_len(h->branch_info->from.map->dso); 114 hists__new_col_len(hists, HISTC_DSO_FROM, symlen); 115 } else { 116 symlen = unresolved_col_width + 4 + 2; 117 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); 118 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM); 119 } 120 121 if (h->branch_info->to.sym) { 122 symlen = (int)h->branch_info->to.sym->namelen + 4; 123 if (verbose > 0) 124 symlen += BITS_PER_LONG / 4 + 2 + 3; 125 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); 126 127 symlen = dso__name_len(h->branch_info->to.map->dso); 128 hists__new_col_len(hists, HISTC_DSO_TO, symlen); 129 } else { 130 symlen = unresolved_col_width + 4 + 2; 131 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); 132 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); 133 } 134 135 if (h->branch_info->srcline_from) 136 hists__new_col_len(hists, HISTC_SRCLINE_FROM, 137 strlen(h->branch_info->srcline_from)); 138 if (h->branch_info->srcline_to) 139 hists__new_col_len(hists, HISTC_SRCLINE_TO, 140 strlen(h->branch_info->srcline_to)); 141 } 142 143 if (h->mem_info) { 144 if (h->mem_info->daddr.sym) { 145 symlen = (int)h->mem_info->daddr.sym->namelen + 4 146 + unresolved_col_width + 2; 147 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, 148 symlen); 149 hists__new_col_len(hists, HISTC_MEM_DCACHELINE, 150 symlen + 1); 151 } else { 152 symlen = unresolved_col_width + 4 + 2; 153 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, 154 symlen); 155 hists__new_col_len(hists, HISTC_MEM_DCACHELINE, 156 symlen); 157 } 158 159 if (h->mem_info->iaddr.sym) { 160 symlen = (int)h->mem_info->iaddr.sym->namelen + 4 161 + unresolved_col_width + 2; 162 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, 163 symlen); 164 } else { 165 symlen = unresolved_col_width + 4 + 2; 166 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, 167 symlen); 168 } 169 170 if (h->mem_info->daddr.map) { 171 symlen = dso__name_len(h->mem_info->daddr.map->dso); 172 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO, 173 symlen); 174 } else { 175 symlen = unresolved_col_width + 4 + 2; 176 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); 177 } 178 179 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR, 180 unresolved_col_width + 4 + 2); 181 182 } else { 183 symlen = unresolved_col_width + 4 + 2; 184 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen); 185 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen); 186 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); 187 } 188 189 hists__new_col_len(hists, HISTC_CGROUP_ID, 20); 190 hists__new_col_len(hists, HISTC_CPU, 3); 191 hists__new_col_len(hists, HISTC_SOCKET, 6); 192 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6); 193 hists__new_col_len(hists, HISTC_MEM_TLB, 22); 194 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12); 195 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3); 196 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12); 197 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12); 198 if (symbol_conf.nanosecs) 199 hists__new_col_len(hists, HISTC_TIME, 16); 200 else 201 hists__new_col_len(hists, HISTC_TIME, 12); 202 203 if (h->srcline) { 204 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header)); 205 hists__new_col_len(hists, HISTC_SRCLINE, len); 206 } 207 208 if (h->srcfile) 209 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile)); 210 211 if (h->transaction) 212 hists__new_col_len(hists, HISTC_TRANSACTION, 213 hist_entry__transaction_len()); 214 215 if (h->trace_output) 216 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output)); 217 } 218 219 void hists__output_recalc_col_len(struct hists *hists, int max_rows) 220 { 221 struct rb_node *next = rb_first_cached(&hists->entries); 222 struct hist_entry *n; 223 int row = 0; 224 225 hists__reset_col_len(hists); 226 227 while (next && row++ < max_rows) { 228 n = rb_entry(next, struct hist_entry, rb_node); 229 if (!n->filtered) 230 hists__calc_col_len(hists, n); 231 next = rb_next(&n->rb_node); 232 } 233 } 234 235 static void he_stat__add_cpumode_period(struct he_stat *he_stat, 236 unsigned int cpumode, u64 period) 237 { 238 switch (cpumode) { 239 case PERF_RECORD_MISC_KERNEL: 240 he_stat->period_sys += period; 241 break; 242 case PERF_RECORD_MISC_USER: 243 he_stat->period_us += period; 244 break; 245 case PERF_RECORD_MISC_GUEST_KERNEL: 246 he_stat->period_guest_sys += period; 247 break; 248 case PERF_RECORD_MISC_GUEST_USER: 249 he_stat->period_guest_us += period; 250 break; 251 default: 252 break; 253 } 254 } 255 256 static long hist_time(unsigned long htime) 257 { 258 unsigned long time_quantum = symbol_conf.time_quantum; 259 if (time_quantum) 260 return (htime / time_quantum) * time_quantum; 261 return htime; 262 } 263 264 static void he_stat__add_period(struct he_stat *he_stat, u64 period, 265 u64 weight) 266 { 267 268 he_stat->period += period; 269 he_stat->weight += weight; 270 he_stat->nr_events += 1; 271 } 272 273 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) 274 { 275 dest->period += src->period; 276 dest->period_sys += src->period_sys; 277 dest->period_us += src->period_us; 278 dest->period_guest_sys += src->period_guest_sys; 279 dest->period_guest_us += src->period_guest_us; 280 dest->nr_events += src->nr_events; 281 dest->weight += src->weight; 282 } 283 284 static void he_stat__decay(struct he_stat *he_stat) 285 { 286 he_stat->period = (he_stat->period * 7) / 8; 287 he_stat->nr_events = (he_stat->nr_events * 7) / 8; 288 /* XXX need decay for weight too? */ 289 } 290 291 static void hists__delete_entry(struct hists *hists, struct hist_entry *he); 292 293 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) 294 { 295 u64 prev_period = he->stat.period; 296 u64 diff; 297 298 if (prev_period == 0) 299 return true; 300 301 he_stat__decay(&he->stat); 302 if (symbol_conf.cumulate_callchain) 303 he_stat__decay(he->stat_acc); 304 decay_callchain(he->callchain); 305 306 diff = prev_period - he->stat.period; 307 308 if (!he->depth) { 309 hists->stats.total_period -= diff; 310 if (!he->filtered) 311 hists->stats.total_non_filtered_period -= diff; 312 } 313 314 if (!he->leaf) { 315 struct hist_entry *child; 316 struct rb_node *node = rb_first_cached(&he->hroot_out); 317 while (node) { 318 child = rb_entry(node, struct hist_entry, rb_node); 319 node = rb_next(node); 320 321 if (hists__decay_entry(hists, child)) 322 hists__delete_entry(hists, child); 323 } 324 } 325 326 return he->stat.period == 0; 327 } 328 329 static void hists__delete_entry(struct hists *hists, struct hist_entry *he) 330 { 331 struct rb_root_cached *root_in; 332 struct rb_root_cached *root_out; 333 334 if (he->parent_he) { 335 root_in = &he->parent_he->hroot_in; 336 root_out = &he->parent_he->hroot_out; 337 } else { 338 if (hists__has(hists, need_collapse)) 339 root_in = &hists->entries_collapsed; 340 else 341 root_in = hists->entries_in; 342 root_out = &hists->entries; 343 } 344 345 rb_erase_cached(&he->rb_node_in, root_in); 346 rb_erase_cached(&he->rb_node, root_out); 347 348 --hists->nr_entries; 349 if (!he->filtered) 350 --hists->nr_non_filtered_entries; 351 352 hist_entry__delete(he); 353 } 354 355 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) 356 { 357 struct rb_node *next = rb_first_cached(&hists->entries); 358 struct hist_entry *n; 359 360 while (next) { 361 n = rb_entry(next, struct hist_entry, rb_node); 362 next = rb_next(&n->rb_node); 363 if (((zap_user && n->level == '.') || 364 (zap_kernel && n->level != '.') || 365 hists__decay_entry(hists, n))) { 366 hists__delete_entry(hists, n); 367 } 368 } 369 } 370 371 void hists__delete_entries(struct hists *hists) 372 { 373 struct rb_node *next = rb_first_cached(&hists->entries); 374 struct hist_entry *n; 375 376 while (next) { 377 n = rb_entry(next, struct hist_entry, rb_node); 378 next = rb_next(&n->rb_node); 379 380 hists__delete_entry(hists, n); 381 } 382 } 383 384 struct hist_entry *hists__get_entry(struct hists *hists, int idx) 385 { 386 struct rb_node *next = rb_first_cached(&hists->entries); 387 struct hist_entry *n; 388 int i = 0; 389 390 while (next) { 391 n = rb_entry(next, struct hist_entry, rb_node); 392 if (i == idx) 393 return n; 394 395 next = rb_next(&n->rb_node); 396 i++; 397 } 398 399 return NULL; 400 } 401 402 /* 403 * histogram, sorted on item, collects periods 404 */ 405 406 static int hist_entry__init(struct hist_entry *he, 407 struct hist_entry *template, 408 bool sample_self, 409 size_t callchain_size) 410 { 411 *he = *template; 412 he->callchain_size = callchain_size; 413 414 if (symbol_conf.cumulate_callchain) { 415 he->stat_acc = malloc(sizeof(he->stat)); 416 if (he->stat_acc == NULL) 417 return -ENOMEM; 418 memcpy(he->stat_acc, &he->stat, sizeof(he->stat)); 419 if (!sample_self) 420 memset(&he->stat, 0, sizeof(he->stat)); 421 } 422 423 map__get(he->ms.map); 424 425 if (he->branch_info) { 426 /* 427 * This branch info is (a part of) allocated from 428 * sample__resolve_bstack() and will be freed after 429 * adding new entries. So we need to save a copy. 430 */ 431 he->branch_info = malloc(sizeof(*he->branch_info)); 432 if (he->branch_info == NULL) 433 goto err; 434 435 memcpy(he->branch_info, template->branch_info, 436 sizeof(*he->branch_info)); 437 438 map__get(he->branch_info->from.map); 439 map__get(he->branch_info->to.map); 440 } 441 442 if (he->mem_info) { 443 map__get(he->mem_info->iaddr.map); 444 map__get(he->mem_info->daddr.map); 445 } 446 447 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) 448 callchain_init(he->callchain); 449 450 if (he->raw_data) { 451 he->raw_data = memdup(he->raw_data, he->raw_size); 452 if (he->raw_data == NULL) 453 goto err_infos; 454 } 455 456 if (he->srcline) { 457 he->srcline = strdup(he->srcline); 458 if (he->srcline == NULL) 459 goto err_rawdata; 460 } 461 462 if (symbol_conf.res_sample) { 463 he->res_samples = calloc(sizeof(struct res_sample), 464 symbol_conf.res_sample); 465 if (!he->res_samples) 466 goto err_srcline; 467 } 468 469 INIT_LIST_HEAD(&he->pairs.node); 470 thread__get(he->thread); 471 he->hroot_in = RB_ROOT_CACHED; 472 he->hroot_out = RB_ROOT_CACHED; 473 474 if (!symbol_conf.report_hierarchy) 475 he->leaf = true; 476 477 return 0; 478 479 err_srcline: 480 zfree(&he->srcline); 481 482 err_rawdata: 483 zfree(&he->raw_data); 484 485 err_infos: 486 if (he->branch_info) { 487 map__put(he->branch_info->from.map); 488 map__put(he->branch_info->to.map); 489 zfree(&he->branch_info); 490 } 491 if (he->mem_info) { 492 map__put(he->mem_info->iaddr.map); 493 map__put(he->mem_info->daddr.map); 494 } 495 err: 496 map__zput(he->ms.map); 497 zfree(&he->stat_acc); 498 return -ENOMEM; 499 } 500 501 static void *hist_entry__zalloc(size_t size) 502 { 503 return zalloc(size + sizeof(struct hist_entry)); 504 } 505 506 static void hist_entry__free(void *ptr) 507 { 508 free(ptr); 509 } 510 511 static struct hist_entry_ops default_ops = { 512 .new = hist_entry__zalloc, 513 .free = hist_entry__free, 514 }; 515 516 static struct hist_entry *hist_entry__new(struct hist_entry *template, 517 bool sample_self) 518 { 519 struct hist_entry_ops *ops = template->ops; 520 size_t callchain_size = 0; 521 struct hist_entry *he; 522 int err = 0; 523 524 if (!ops) 525 ops = template->ops = &default_ops; 526 527 if (symbol_conf.use_callchain) 528 callchain_size = sizeof(struct callchain_root); 529 530 he = ops->new(callchain_size); 531 if (he) { 532 err = hist_entry__init(he, template, sample_self, callchain_size); 533 if (err) { 534 ops->free(he); 535 he = NULL; 536 } 537 } 538 539 return he; 540 } 541 542 static u8 symbol__parent_filter(const struct symbol *parent) 543 { 544 if (symbol_conf.exclude_other && parent == NULL) 545 return 1 << HIST_FILTER__PARENT; 546 return 0; 547 } 548 549 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period) 550 { 551 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain) 552 return; 553 554 he->hists->callchain_period += period; 555 if (!he->filtered) 556 he->hists->callchain_non_filtered_period += period; 557 } 558 559 static struct hist_entry *hists__findnew_entry(struct hists *hists, 560 struct hist_entry *entry, 561 struct addr_location *al, 562 bool sample_self) 563 { 564 struct rb_node **p; 565 struct rb_node *parent = NULL; 566 struct hist_entry *he; 567 int64_t cmp; 568 u64 period = entry->stat.period; 569 u64 weight = entry->stat.weight; 570 bool leftmost = true; 571 572 p = &hists->entries_in->rb_root.rb_node; 573 574 while (*p != NULL) { 575 parent = *p; 576 he = rb_entry(parent, struct hist_entry, rb_node_in); 577 578 /* 579 * Make sure that it receives arguments in a same order as 580 * hist_entry__collapse() so that we can use an appropriate 581 * function when searching an entry regardless which sort 582 * keys were used. 583 */ 584 cmp = hist_entry__cmp(he, entry); 585 586 if (!cmp) { 587 if (sample_self) { 588 he_stat__add_period(&he->stat, period, weight); 589 hist_entry__add_callchain_period(he, period); 590 } 591 if (symbol_conf.cumulate_callchain) 592 he_stat__add_period(he->stat_acc, period, weight); 593 594 /* 595 * This mem info was allocated from sample__resolve_mem 596 * and will not be used anymore. 597 */ 598 mem_info__zput(entry->mem_info); 599 600 block_info__zput(entry->block_info); 601 602 /* If the map of an existing hist_entry has 603 * become out-of-date due to an exec() or 604 * similar, update it. Otherwise we will 605 * mis-adjust symbol addresses when computing 606 * the history counter to increment. 607 */ 608 if (he->ms.map != entry->ms.map) { 609 map__put(he->ms.map); 610 he->ms.map = map__get(entry->ms.map); 611 } 612 goto out; 613 } 614 615 if (cmp < 0) 616 p = &(*p)->rb_left; 617 else { 618 p = &(*p)->rb_right; 619 leftmost = false; 620 } 621 } 622 623 he = hist_entry__new(entry, sample_self); 624 if (!he) 625 return NULL; 626 627 if (sample_self) 628 hist_entry__add_callchain_period(he, period); 629 hists->nr_entries++; 630 631 rb_link_node(&he->rb_node_in, parent, p); 632 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost); 633 out: 634 if (sample_self) 635 he_stat__add_cpumode_period(&he->stat, al->cpumode, period); 636 if (symbol_conf.cumulate_callchain) 637 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period); 638 return he; 639 } 640 641 static unsigned random_max(unsigned high) 642 { 643 unsigned thresh = -high % high; 644 for (;;) { 645 unsigned r = random(); 646 if (r >= thresh) 647 return r % high; 648 } 649 } 650 651 static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample) 652 { 653 struct res_sample *r; 654 int j; 655 656 if (he->num_res < symbol_conf.res_sample) { 657 j = he->num_res++; 658 } else { 659 j = random_max(symbol_conf.res_sample); 660 } 661 r = &he->res_samples[j]; 662 r->time = sample->time; 663 r->cpu = sample->cpu; 664 r->tid = sample->tid; 665 } 666 667 static struct hist_entry* 668 __hists__add_entry(struct hists *hists, 669 struct addr_location *al, 670 struct symbol *sym_parent, 671 struct branch_info *bi, 672 struct mem_info *mi, 673 struct block_info *block_info, 674 struct perf_sample *sample, 675 bool sample_self, 676 struct hist_entry_ops *ops) 677 { 678 struct namespaces *ns = thread__namespaces(al->thread); 679 struct hist_entry entry = { 680 .thread = al->thread, 681 .comm = thread__comm(al->thread), 682 .cgroup_id = { 683 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0, 684 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0, 685 }, 686 .ms = { 687 .map = al->map, 688 .sym = al->sym, 689 }, 690 .srcline = (char *) al->srcline, 691 .socket = al->socket, 692 .cpu = al->cpu, 693 .cpumode = al->cpumode, 694 .ip = al->addr, 695 .level = al->level, 696 .stat = { 697 .nr_events = 1, 698 .period = sample->period, 699 .weight = sample->weight, 700 }, 701 .parent = sym_parent, 702 .filtered = symbol__parent_filter(sym_parent) | al->filtered, 703 .hists = hists, 704 .branch_info = bi, 705 .mem_info = mi, 706 .block_info = block_info, 707 .transaction = sample->transaction, 708 .raw_data = sample->raw_data, 709 .raw_size = sample->raw_size, 710 .ops = ops, 711 .time = hist_time(sample->time), 712 }, *he = hists__findnew_entry(hists, &entry, al, sample_self); 713 714 if (!hists->has_callchains && he && he->callchain_size != 0) 715 hists->has_callchains = true; 716 if (he && symbol_conf.res_sample) 717 hists__res_sample(he, sample); 718 return he; 719 } 720 721 struct hist_entry *hists__add_entry(struct hists *hists, 722 struct addr_location *al, 723 struct symbol *sym_parent, 724 struct branch_info *bi, 725 struct mem_info *mi, 726 struct perf_sample *sample, 727 bool sample_self) 728 { 729 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL, 730 sample, sample_self, NULL); 731 } 732 733 struct hist_entry *hists__add_entry_ops(struct hists *hists, 734 struct hist_entry_ops *ops, 735 struct addr_location *al, 736 struct symbol *sym_parent, 737 struct branch_info *bi, 738 struct mem_info *mi, 739 struct perf_sample *sample, 740 bool sample_self) 741 { 742 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL, 743 sample, sample_self, ops); 744 } 745 746 struct hist_entry *hists__add_entry_block(struct hists *hists, 747 struct addr_location *al, 748 struct block_info *block_info) 749 { 750 struct hist_entry entry = { 751 .block_info = block_info, 752 .hists = hists, 753 }, *he = hists__findnew_entry(hists, &entry, al, false); 754 755 return he; 756 } 757 758 static int 759 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, 760 struct addr_location *al __maybe_unused) 761 { 762 return 0; 763 } 764 765 static int 766 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, 767 struct addr_location *al __maybe_unused) 768 { 769 return 0; 770 } 771 772 static int 773 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) 774 { 775 struct perf_sample *sample = iter->sample; 776 struct mem_info *mi; 777 778 mi = sample__resolve_mem(sample, al); 779 if (mi == NULL) 780 return -ENOMEM; 781 782 iter->priv = mi; 783 return 0; 784 } 785 786 static int 787 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) 788 { 789 u64 cost; 790 struct mem_info *mi = iter->priv; 791 struct hists *hists = evsel__hists(iter->evsel); 792 struct perf_sample *sample = iter->sample; 793 struct hist_entry *he; 794 795 if (mi == NULL) 796 return -EINVAL; 797 798 cost = sample->weight; 799 if (!cost) 800 cost = 1; 801 802 /* 803 * must pass period=weight in order to get the correct 804 * sorting from hists__collapse_resort() which is solely 805 * based on periods. We want sorting be done on nr_events * weight 806 * and this is indirectly achieved by passing period=weight here 807 * and the he_stat__add_period() function. 808 */ 809 sample->period = cost; 810 811 he = hists__add_entry(hists, al, iter->parent, NULL, mi, 812 sample, true); 813 if (!he) 814 return -ENOMEM; 815 816 iter->he = he; 817 return 0; 818 } 819 820 static int 821 iter_finish_mem_entry(struct hist_entry_iter *iter, 822 struct addr_location *al __maybe_unused) 823 { 824 struct evsel *evsel = iter->evsel; 825 struct hists *hists = evsel__hists(evsel); 826 struct hist_entry *he = iter->he; 827 int err = -EINVAL; 828 829 if (he == NULL) 830 goto out; 831 832 hists__inc_nr_samples(hists, he->filtered); 833 834 err = hist_entry__append_callchain(he, iter->sample); 835 836 out: 837 /* 838 * We don't need to free iter->priv (mem_info) here since the mem info 839 * was either already freed in hists__findnew_entry() or passed to a 840 * new hist entry by hist_entry__new(). 841 */ 842 iter->priv = NULL; 843 844 iter->he = NULL; 845 return err; 846 } 847 848 static int 849 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) 850 { 851 struct branch_info *bi; 852 struct perf_sample *sample = iter->sample; 853 854 bi = sample__resolve_bstack(sample, al); 855 if (!bi) 856 return -ENOMEM; 857 858 iter->curr = 0; 859 iter->total = sample->branch_stack->nr; 860 861 iter->priv = bi; 862 return 0; 863 } 864 865 static int 866 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused, 867 struct addr_location *al __maybe_unused) 868 { 869 return 0; 870 } 871 872 static int 873 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) 874 { 875 struct branch_info *bi = iter->priv; 876 int i = iter->curr; 877 878 if (bi == NULL) 879 return 0; 880 881 if (iter->curr >= iter->total) 882 return 0; 883 884 al->map = bi[i].to.map; 885 al->sym = bi[i].to.sym; 886 al->addr = bi[i].to.addr; 887 return 1; 888 } 889 890 static int 891 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) 892 { 893 struct branch_info *bi; 894 struct evsel *evsel = iter->evsel; 895 struct hists *hists = evsel__hists(evsel); 896 struct perf_sample *sample = iter->sample; 897 struct hist_entry *he = NULL; 898 int i = iter->curr; 899 int err = 0; 900 901 bi = iter->priv; 902 903 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym)) 904 goto out; 905 906 /* 907 * The report shows the percentage of total branches captured 908 * and not events sampled. Thus we use a pseudo period of 1. 909 */ 910 sample->period = 1; 911 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1; 912 913 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL, 914 sample, true); 915 if (he == NULL) 916 return -ENOMEM; 917 918 hists__inc_nr_samples(hists, he->filtered); 919 920 out: 921 iter->he = he; 922 iter->curr++; 923 return err; 924 } 925 926 static int 927 iter_finish_branch_entry(struct hist_entry_iter *iter, 928 struct addr_location *al __maybe_unused) 929 { 930 zfree(&iter->priv); 931 iter->he = NULL; 932 933 return iter->curr >= iter->total ? 0 : -1; 934 } 935 936 static int 937 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused, 938 struct addr_location *al __maybe_unused) 939 { 940 return 0; 941 } 942 943 static int 944 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al) 945 { 946 struct evsel *evsel = iter->evsel; 947 struct perf_sample *sample = iter->sample; 948 struct hist_entry *he; 949 950 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, 951 sample, true); 952 if (he == NULL) 953 return -ENOMEM; 954 955 iter->he = he; 956 return 0; 957 } 958 959 static int 960 iter_finish_normal_entry(struct hist_entry_iter *iter, 961 struct addr_location *al __maybe_unused) 962 { 963 struct hist_entry *he = iter->he; 964 struct evsel *evsel = iter->evsel; 965 struct perf_sample *sample = iter->sample; 966 967 if (he == NULL) 968 return 0; 969 970 iter->he = NULL; 971 972 hists__inc_nr_samples(evsel__hists(evsel), he->filtered); 973 974 return hist_entry__append_callchain(he, sample); 975 } 976 977 static int 978 iter_prepare_cumulative_entry(struct hist_entry_iter *iter, 979 struct addr_location *al __maybe_unused) 980 { 981 struct hist_entry **he_cache; 982 983 callchain_cursor_commit(&callchain_cursor); 984 985 /* 986 * This is for detecting cycles or recursions so that they're 987 * cumulated only one time to prevent entries more than 100% 988 * overhead. 989 */ 990 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1)); 991 if (he_cache == NULL) 992 return -ENOMEM; 993 994 iter->priv = he_cache; 995 iter->curr = 0; 996 997 return 0; 998 } 999 1000 static int 1001 iter_add_single_cumulative_entry(struct hist_entry_iter *iter, 1002 struct addr_location *al) 1003 { 1004 struct evsel *evsel = iter->evsel; 1005 struct hists *hists = evsel__hists(evsel); 1006 struct perf_sample *sample = iter->sample; 1007 struct hist_entry **he_cache = iter->priv; 1008 struct hist_entry *he; 1009 int err = 0; 1010 1011 he = hists__add_entry(hists, al, iter->parent, NULL, NULL, 1012 sample, true); 1013 if (he == NULL) 1014 return -ENOMEM; 1015 1016 iter->he = he; 1017 he_cache[iter->curr++] = he; 1018 1019 hist_entry__append_callchain(he, sample); 1020 1021 /* 1022 * We need to re-initialize the cursor since callchain_append() 1023 * advanced the cursor to the end. 1024 */ 1025 callchain_cursor_commit(&callchain_cursor); 1026 1027 hists__inc_nr_samples(hists, he->filtered); 1028 1029 return err; 1030 } 1031 1032 static int 1033 iter_next_cumulative_entry(struct hist_entry_iter *iter, 1034 struct addr_location *al) 1035 { 1036 struct callchain_cursor_node *node; 1037 1038 node = callchain_cursor_current(&callchain_cursor); 1039 if (node == NULL) 1040 return 0; 1041 1042 return fill_callchain_info(al, node, iter->hide_unresolved); 1043 } 1044 1045 static int 1046 iter_add_next_cumulative_entry(struct hist_entry_iter *iter, 1047 struct addr_location *al) 1048 { 1049 struct evsel *evsel = iter->evsel; 1050 struct perf_sample *sample = iter->sample; 1051 struct hist_entry **he_cache = iter->priv; 1052 struct hist_entry *he; 1053 struct hist_entry he_tmp = { 1054 .hists = evsel__hists(evsel), 1055 .cpu = al->cpu, 1056 .thread = al->thread, 1057 .comm = thread__comm(al->thread), 1058 .ip = al->addr, 1059 .ms = { 1060 .map = al->map, 1061 .sym = al->sym, 1062 }, 1063 .srcline = (char *) al->srcline, 1064 .parent = iter->parent, 1065 .raw_data = sample->raw_data, 1066 .raw_size = sample->raw_size, 1067 }; 1068 int i; 1069 struct callchain_cursor cursor; 1070 1071 callchain_cursor_snapshot(&cursor, &callchain_cursor); 1072 1073 callchain_cursor_advance(&callchain_cursor); 1074 1075 /* 1076 * Check if there's duplicate entries in the callchain. 1077 * It's possible that it has cycles or recursive calls. 1078 */ 1079 for (i = 0; i < iter->curr; i++) { 1080 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) { 1081 /* to avoid calling callback function */ 1082 iter->he = NULL; 1083 return 0; 1084 } 1085 } 1086 1087 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, 1088 sample, false); 1089 if (he == NULL) 1090 return -ENOMEM; 1091 1092 iter->he = he; 1093 he_cache[iter->curr++] = he; 1094 1095 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) 1096 callchain_append(he->callchain, &cursor, sample->period); 1097 return 0; 1098 } 1099 1100 static int 1101 iter_finish_cumulative_entry(struct hist_entry_iter *iter, 1102 struct addr_location *al __maybe_unused) 1103 { 1104 zfree(&iter->priv); 1105 iter->he = NULL; 1106 1107 return 0; 1108 } 1109 1110 const struct hist_iter_ops hist_iter_mem = { 1111 .prepare_entry = iter_prepare_mem_entry, 1112 .add_single_entry = iter_add_single_mem_entry, 1113 .next_entry = iter_next_nop_entry, 1114 .add_next_entry = iter_add_next_nop_entry, 1115 .finish_entry = iter_finish_mem_entry, 1116 }; 1117 1118 const struct hist_iter_ops hist_iter_branch = { 1119 .prepare_entry = iter_prepare_branch_entry, 1120 .add_single_entry = iter_add_single_branch_entry, 1121 .next_entry = iter_next_branch_entry, 1122 .add_next_entry = iter_add_next_branch_entry, 1123 .finish_entry = iter_finish_branch_entry, 1124 }; 1125 1126 const struct hist_iter_ops hist_iter_normal = { 1127 .prepare_entry = iter_prepare_normal_entry, 1128 .add_single_entry = iter_add_single_normal_entry, 1129 .next_entry = iter_next_nop_entry, 1130 .add_next_entry = iter_add_next_nop_entry, 1131 .finish_entry = iter_finish_normal_entry, 1132 }; 1133 1134 const struct hist_iter_ops hist_iter_cumulative = { 1135 .prepare_entry = iter_prepare_cumulative_entry, 1136 .add_single_entry = iter_add_single_cumulative_entry, 1137 .next_entry = iter_next_cumulative_entry, 1138 .add_next_entry = iter_add_next_cumulative_entry, 1139 .finish_entry = iter_finish_cumulative_entry, 1140 }; 1141 1142 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al, 1143 int max_stack_depth, void *arg) 1144 { 1145 int err, err2; 1146 struct map *alm = NULL; 1147 1148 if (al) 1149 alm = map__get(al->map); 1150 1151 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent, 1152 iter->evsel, al, max_stack_depth); 1153 if (err) { 1154 map__put(alm); 1155 return err; 1156 } 1157 1158 err = iter->ops->prepare_entry(iter, al); 1159 if (err) 1160 goto out; 1161 1162 err = iter->ops->add_single_entry(iter, al); 1163 if (err) 1164 goto out; 1165 1166 if (iter->he && iter->add_entry_cb) { 1167 err = iter->add_entry_cb(iter, al, true, arg); 1168 if (err) 1169 goto out; 1170 } 1171 1172 while (iter->ops->next_entry(iter, al)) { 1173 err = iter->ops->add_next_entry(iter, al); 1174 if (err) 1175 break; 1176 1177 if (iter->he && iter->add_entry_cb) { 1178 err = iter->add_entry_cb(iter, al, false, arg); 1179 if (err) 1180 goto out; 1181 } 1182 } 1183 1184 out: 1185 err2 = iter->ops->finish_entry(iter, al); 1186 if (!err) 1187 err = err2; 1188 1189 map__put(alm); 1190 1191 return err; 1192 } 1193 1194 int64_t 1195 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) 1196 { 1197 struct hists *hists = left->hists; 1198 struct perf_hpp_fmt *fmt; 1199 int64_t cmp = 0; 1200 1201 hists__for_each_sort_list(hists, fmt) { 1202 if (perf_hpp__is_dynamic_entry(fmt) && 1203 !perf_hpp__defined_dynamic_entry(fmt, hists)) 1204 continue; 1205 1206 cmp = fmt->cmp(fmt, left, right); 1207 if (cmp) 1208 break; 1209 } 1210 1211 return cmp; 1212 } 1213 1214 int64_t 1215 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) 1216 { 1217 struct hists *hists = left->hists; 1218 struct perf_hpp_fmt *fmt; 1219 int64_t cmp = 0; 1220 1221 hists__for_each_sort_list(hists, fmt) { 1222 if (perf_hpp__is_dynamic_entry(fmt) && 1223 !perf_hpp__defined_dynamic_entry(fmt, hists)) 1224 continue; 1225 1226 cmp = fmt->collapse(fmt, left, right); 1227 if (cmp) 1228 break; 1229 } 1230 1231 return cmp; 1232 } 1233 1234 void hist_entry__delete(struct hist_entry *he) 1235 { 1236 struct hist_entry_ops *ops = he->ops; 1237 1238 thread__zput(he->thread); 1239 map__zput(he->ms.map); 1240 1241 if (he->branch_info) { 1242 map__zput(he->branch_info->from.map); 1243 map__zput(he->branch_info->to.map); 1244 free_srcline(he->branch_info->srcline_from); 1245 free_srcline(he->branch_info->srcline_to); 1246 zfree(&he->branch_info); 1247 } 1248 1249 if (he->mem_info) { 1250 map__zput(he->mem_info->iaddr.map); 1251 map__zput(he->mem_info->daddr.map); 1252 mem_info__zput(he->mem_info); 1253 } 1254 1255 if (he->block_info) 1256 block_info__zput(he->block_info); 1257 1258 zfree(&he->res_samples); 1259 zfree(&he->stat_acc); 1260 free_srcline(he->srcline); 1261 if (he->srcfile && he->srcfile[0]) 1262 zfree(&he->srcfile); 1263 free_callchain(he->callchain); 1264 zfree(&he->trace_output); 1265 zfree(&he->raw_data); 1266 ops->free(he); 1267 } 1268 1269 /* 1270 * If this is not the last column, then we need to pad it according to the 1271 * pre-calculated max length for this column, otherwise don't bother adding 1272 * spaces because that would break viewing this with, for instance, 'less', 1273 * that would show tons of trailing spaces when a long C++ demangled method 1274 * names is sampled. 1275 */ 1276 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp, 1277 struct perf_hpp_fmt *fmt, int printed) 1278 { 1279 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) { 1280 const int width = fmt->width(fmt, hpp, he->hists); 1281 if (printed < width) { 1282 advance_hpp(hpp, printed); 1283 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " "); 1284 } 1285 } 1286 1287 return printed; 1288 } 1289 1290 /* 1291 * collapse the histogram 1292 */ 1293 1294 static void hists__apply_filters(struct hists *hists, struct hist_entry *he); 1295 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he, 1296 enum hist_filter type); 1297 1298 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt); 1299 1300 static bool check_thread_entry(struct perf_hpp_fmt *fmt) 1301 { 1302 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt); 1303 } 1304 1305 static void hist_entry__check_and_remove_filter(struct hist_entry *he, 1306 enum hist_filter type, 1307 fmt_chk_fn check) 1308 { 1309 struct perf_hpp_fmt *fmt; 1310 bool type_match = false; 1311 struct hist_entry *parent = he->parent_he; 1312 1313 switch (type) { 1314 case HIST_FILTER__THREAD: 1315 if (symbol_conf.comm_list == NULL && 1316 symbol_conf.pid_list == NULL && 1317 symbol_conf.tid_list == NULL) 1318 return; 1319 break; 1320 case HIST_FILTER__DSO: 1321 if (symbol_conf.dso_list == NULL) 1322 return; 1323 break; 1324 case HIST_FILTER__SYMBOL: 1325 if (symbol_conf.sym_list == NULL) 1326 return; 1327 break; 1328 case HIST_FILTER__PARENT: 1329 case HIST_FILTER__GUEST: 1330 case HIST_FILTER__HOST: 1331 case HIST_FILTER__SOCKET: 1332 case HIST_FILTER__C2C: 1333 default: 1334 return; 1335 } 1336 1337 /* if it's filtered by own fmt, it has to have filter bits */ 1338 perf_hpp_list__for_each_format(he->hpp_list, fmt) { 1339 if (check(fmt)) { 1340 type_match = true; 1341 break; 1342 } 1343 } 1344 1345 if (type_match) { 1346 /* 1347 * If the filter is for current level entry, propagate 1348 * filter marker to parents. The marker bit was 1349 * already set by default so it only needs to clear 1350 * non-filtered entries. 1351 */ 1352 if (!(he->filtered & (1 << type))) { 1353 while (parent) { 1354 parent->filtered &= ~(1 << type); 1355 parent = parent->parent_he; 1356 } 1357 } 1358 } else { 1359 /* 1360 * If current entry doesn't have matching formats, set 1361 * filter marker for upper level entries. it will be 1362 * cleared if its lower level entries is not filtered. 1363 * 1364 * For lower-level entries, it inherits parent's 1365 * filter bit so that lower level entries of a 1366 * non-filtered entry won't set the filter marker. 1367 */ 1368 if (parent == NULL) 1369 he->filtered |= (1 << type); 1370 else 1371 he->filtered |= (parent->filtered & (1 << type)); 1372 } 1373 } 1374 1375 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he) 1376 { 1377 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD, 1378 check_thread_entry); 1379 1380 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO, 1381 perf_hpp__is_dso_entry); 1382 1383 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL, 1384 perf_hpp__is_sym_entry); 1385 1386 hists__apply_filters(he->hists, he); 1387 } 1388 1389 static struct hist_entry *hierarchy_insert_entry(struct hists *hists, 1390 struct rb_root_cached *root, 1391 struct hist_entry *he, 1392 struct hist_entry *parent_he, 1393 struct perf_hpp_list *hpp_list) 1394 { 1395 struct rb_node **p = &root->rb_root.rb_node; 1396 struct rb_node *parent = NULL; 1397 struct hist_entry *iter, *new; 1398 struct perf_hpp_fmt *fmt; 1399 int64_t cmp; 1400 bool leftmost = true; 1401 1402 while (*p != NULL) { 1403 parent = *p; 1404 iter = rb_entry(parent, struct hist_entry, rb_node_in); 1405 1406 cmp = 0; 1407 perf_hpp_list__for_each_sort_list(hpp_list, fmt) { 1408 cmp = fmt->collapse(fmt, iter, he); 1409 if (cmp) 1410 break; 1411 } 1412 1413 if (!cmp) { 1414 he_stat__add_stat(&iter->stat, &he->stat); 1415 return iter; 1416 } 1417 1418 if (cmp < 0) 1419 p = &parent->rb_left; 1420 else { 1421 p = &parent->rb_right; 1422 leftmost = false; 1423 } 1424 } 1425 1426 new = hist_entry__new(he, true); 1427 if (new == NULL) 1428 return NULL; 1429 1430 hists->nr_entries++; 1431 1432 /* save related format list for output */ 1433 new->hpp_list = hpp_list; 1434 new->parent_he = parent_he; 1435 1436 hist_entry__apply_hierarchy_filters(new); 1437 1438 /* some fields are now passed to 'new' */ 1439 perf_hpp_list__for_each_sort_list(hpp_list, fmt) { 1440 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt)) 1441 he->trace_output = NULL; 1442 else 1443 new->trace_output = NULL; 1444 1445 if (perf_hpp__is_srcline_entry(fmt)) 1446 he->srcline = NULL; 1447 else 1448 new->srcline = NULL; 1449 1450 if (perf_hpp__is_srcfile_entry(fmt)) 1451 he->srcfile = NULL; 1452 else 1453 new->srcfile = NULL; 1454 } 1455 1456 rb_link_node(&new->rb_node_in, parent, p); 1457 rb_insert_color_cached(&new->rb_node_in, root, leftmost); 1458 return new; 1459 } 1460 1461 static int hists__hierarchy_insert_entry(struct hists *hists, 1462 struct rb_root_cached *root, 1463 struct hist_entry *he) 1464 { 1465 struct perf_hpp_list_node *node; 1466 struct hist_entry *new_he = NULL; 1467 struct hist_entry *parent = NULL; 1468 int depth = 0; 1469 int ret = 0; 1470 1471 list_for_each_entry(node, &hists->hpp_formats, list) { 1472 /* skip period (overhead) and elided columns */ 1473 if (node->level == 0 || node->skip) 1474 continue; 1475 1476 /* insert copy of 'he' for each fmt into the hierarchy */ 1477 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp); 1478 if (new_he == NULL) { 1479 ret = -1; 1480 break; 1481 } 1482 1483 root = &new_he->hroot_in; 1484 new_he->depth = depth++; 1485 parent = new_he; 1486 } 1487 1488 if (new_he) { 1489 new_he->leaf = true; 1490 1491 if (hist_entry__has_callchains(new_he) && 1492 symbol_conf.use_callchain) { 1493 callchain_cursor_reset(&callchain_cursor); 1494 if (callchain_merge(&callchain_cursor, 1495 new_he->callchain, 1496 he->callchain) < 0) 1497 ret = -1; 1498 } 1499 } 1500 1501 /* 'he' is no longer used */ 1502 hist_entry__delete(he); 1503 1504 /* return 0 (or -1) since it already applied filters */ 1505 return ret; 1506 } 1507 1508 static int hists__collapse_insert_entry(struct hists *hists, 1509 struct rb_root_cached *root, 1510 struct hist_entry *he) 1511 { 1512 struct rb_node **p = &root->rb_root.rb_node; 1513 struct rb_node *parent = NULL; 1514 struct hist_entry *iter; 1515 int64_t cmp; 1516 bool leftmost = true; 1517 1518 if (symbol_conf.report_hierarchy) 1519 return hists__hierarchy_insert_entry(hists, root, he); 1520 1521 while (*p != NULL) { 1522 parent = *p; 1523 iter = rb_entry(parent, struct hist_entry, rb_node_in); 1524 1525 cmp = hist_entry__collapse(iter, he); 1526 1527 if (!cmp) { 1528 int ret = 0; 1529 1530 he_stat__add_stat(&iter->stat, &he->stat); 1531 if (symbol_conf.cumulate_callchain) 1532 he_stat__add_stat(iter->stat_acc, he->stat_acc); 1533 1534 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) { 1535 callchain_cursor_reset(&callchain_cursor); 1536 if (callchain_merge(&callchain_cursor, 1537 iter->callchain, 1538 he->callchain) < 0) 1539 ret = -1; 1540 } 1541 hist_entry__delete(he); 1542 return ret; 1543 } 1544 1545 if (cmp < 0) 1546 p = &(*p)->rb_left; 1547 else { 1548 p = &(*p)->rb_right; 1549 leftmost = false; 1550 } 1551 } 1552 hists->nr_entries++; 1553 1554 rb_link_node(&he->rb_node_in, parent, p); 1555 rb_insert_color_cached(&he->rb_node_in, root, leftmost); 1556 return 1; 1557 } 1558 1559 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists) 1560 { 1561 struct rb_root_cached *root; 1562 1563 pthread_mutex_lock(&hists->lock); 1564 1565 root = hists->entries_in; 1566 if (++hists->entries_in > &hists->entries_in_array[1]) 1567 hists->entries_in = &hists->entries_in_array[0]; 1568 1569 pthread_mutex_unlock(&hists->lock); 1570 1571 return root; 1572 } 1573 1574 static void hists__apply_filters(struct hists *hists, struct hist_entry *he) 1575 { 1576 hists__filter_entry_by_dso(hists, he); 1577 hists__filter_entry_by_thread(hists, he); 1578 hists__filter_entry_by_symbol(hists, he); 1579 hists__filter_entry_by_socket(hists, he); 1580 } 1581 1582 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog) 1583 { 1584 struct rb_root_cached *root; 1585 struct rb_node *next; 1586 struct hist_entry *n; 1587 int ret; 1588 1589 if (!hists__has(hists, need_collapse)) 1590 return 0; 1591 1592 hists->nr_entries = 0; 1593 1594 root = hists__get_rotate_entries_in(hists); 1595 1596 next = rb_first_cached(root); 1597 1598 while (next) { 1599 if (session_done()) 1600 break; 1601 n = rb_entry(next, struct hist_entry, rb_node_in); 1602 next = rb_next(&n->rb_node_in); 1603 1604 rb_erase_cached(&n->rb_node_in, root); 1605 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n); 1606 if (ret < 0) 1607 return -1; 1608 1609 if (ret) { 1610 /* 1611 * If it wasn't combined with one of the entries already 1612 * collapsed, we need to apply the filters that may have 1613 * been set by, say, the hist_browser. 1614 */ 1615 hists__apply_filters(hists, n); 1616 } 1617 if (prog) 1618 ui_progress__update(prog, 1); 1619 } 1620 return 0; 1621 } 1622 1623 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b) 1624 { 1625 struct hists *hists = a->hists; 1626 struct perf_hpp_fmt *fmt; 1627 int64_t cmp = 0; 1628 1629 hists__for_each_sort_list(hists, fmt) { 1630 if (perf_hpp__should_skip(fmt, a->hists)) 1631 continue; 1632 1633 cmp = fmt->sort(fmt, a, b); 1634 if (cmp) 1635 break; 1636 } 1637 1638 return cmp; 1639 } 1640 1641 static void hists__reset_filter_stats(struct hists *hists) 1642 { 1643 hists->nr_non_filtered_entries = 0; 1644 hists->stats.total_non_filtered_period = 0; 1645 } 1646 1647 void hists__reset_stats(struct hists *hists) 1648 { 1649 hists->nr_entries = 0; 1650 hists->stats.total_period = 0; 1651 1652 hists__reset_filter_stats(hists); 1653 } 1654 1655 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h) 1656 { 1657 hists->nr_non_filtered_entries++; 1658 hists->stats.total_non_filtered_period += h->stat.period; 1659 } 1660 1661 void hists__inc_stats(struct hists *hists, struct hist_entry *h) 1662 { 1663 if (!h->filtered) 1664 hists__inc_filter_stats(hists, h); 1665 1666 hists->nr_entries++; 1667 hists->stats.total_period += h->stat.period; 1668 } 1669 1670 static void hierarchy_recalc_total_periods(struct hists *hists) 1671 { 1672 struct rb_node *node; 1673 struct hist_entry *he; 1674 1675 node = rb_first_cached(&hists->entries); 1676 1677 hists->stats.total_period = 0; 1678 hists->stats.total_non_filtered_period = 0; 1679 1680 /* 1681 * recalculate total period using top-level entries only 1682 * since lower level entries only see non-filtered entries 1683 * but upper level entries have sum of both entries. 1684 */ 1685 while (node) { 1686 he = rb_entry(node, struct hist_entry, rb_node); 1687 node = rb_next(node); 1688 1689 hists->stats.total_period += he->stat.period; 1690 if (!he->filtered) 1691 hists->stats.total_non_filtered_period += he->stat.period; 1692 } 1693 } 1694 1695 static void hierarchy_insert_output_entry(struct rb_root_cached *root, 1696 struct hist_entry *he) 1697 { 1698 struct rb_node **p = &root->rb_root.rb_node; 1699 struct rb_node *parent = NULL; 1700 struct hist_entry *iter; 1701 struct perf_hpp_fmt *fmt; 1702 bool leftmost = true; 1703 1704 while (*p != NULL) { 1705 parent = *p; 1706 iter = rb_entry(parent, struct hist_entry, rb_node); 1707 1708 if (hist_entry__sort(he, iter) > 0) 1709 p = &parent->rb_left; 1710 else { 1711 p = &parent->rb_right; 1712 leftmost = false; 1713 } 1714 } 1715 1716 rb_link_node(&he->rb_node, parent, p); 1717 rb_insert_color_cached(&he->rb_node, root, leftmost); 1718 1719 /* update column width of dynamic entry */ 1720 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) { 1721 if (perf_hpp__is_dynamic_entry(fmt)) 1722 fmt->sort(fmt, he, NULL); 1723 } 1724 } 1725 1726 static void hists__hierarchy_output_resort(struct hists *hists, 1727 struct ui_progress *prog, 1728 struct rb_root_cached *root_in, 1729 struct rb_root_cached *root_out, 1730 u64 min_callchain_hits, 1731 bool use_callchain) 1732 { 1733 struct rb_node *node; 1734 struct hist_entry *he; 1735 1736 *root_out = RB_ROOT_CACHED; 1737 node = rb_first_cached(root_in); 1738 1739 while (node) { 1740 he = rb_entry(node, struct hist_entry, rb_node_in); 1741 node = rb_next(node); 1742 1743 hierarchy_insert_output_entry(root_out, he); 1744 1745 if (prog) 1746 ui_progress__update(prog, 1); 1747 1748 hists->nr_entries++; 1749 if (!he->filtered) { 1750 hists->nr_non_filtered_entries++; 1751 hists__calc_col_len(hists, he); 1752 } 1753 1754 if (!he->leaf) { 1755 hists__hierarchy_output_resort(hists, prog, 1756 &he->hroot_in, 1757 &he->hroot_out, 1758 min_callchain_hits, 1759 use_callchain); 1760 continue; 1761 } 1762 1763 if (!use_callchain) 1764 continue; 1765 1766 if (callchain_param.mode == CHAIN_GRAPH_REL) { 1767 u64 total = he->stat.period; 1768 1769 if (symbol_conf.cumulate_callchain) 1770 total = he->stat_acc->period; 1771 1772 min_callchain_hits = total * (callchain_param.min_percent / 100); 1773 } 1774 1775 callchain_param.sort(&he->sorted_chain, he->callchain, 1776 min_callchain_hits, &callchain_param); 1777 } 1778 } 1779 1780 static void __hists__insert_output_entry(struct rb_root_cached *entries, 1781 struct hist_entry *he, 1782 u64 min_callchain_hits, 1783 bool use_callchain) 1784 { 1785 struct rb_node **p = &entries->rb_root.rb_node; 1786 struct rb_node *parent = NULL; 1787 struct hist_entry *iter; 1788 struct perf_hpp_fmt *fmt; 1789 bool leftmost = true; 1790 1791 if (use_callchain) { 1792 if (callchain_param.mode == CHAIN_GRAPH_REL) { 1793 u64 total = he->stat.period; 1794 1795 if (symbol_conf.cumulate_callchain) 1796 total = he->stat_acc->period; 1797 1798 min_callchain_hits = total * (callchain_param.min_percent / 100); 1799 } 1800 callchain_param.sort(&he->sorted_chain, he->callchain, 1801 min_callchain_hits, &callchain_param); 1802 } 1803 1804 while (*p != NULL) { 1805 parent = *p; 1806 iter = rb_entry(parent, struct hist_entry, rb_node); 1807 1808 if (hist_entry__sort(he, iter) > 0) 1809 p = &(*p)->rb_left; 1810 else { 1811 p = &(*p)->rb_right; 1812 leftmost = false; 1813 } 1814 } 1815 1816 rb_link_node(&he->rb_node, parent, p); 1817 rb_insert_color_cached(&he->rb_node, entries, leftmost); 1818 1819 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) { 1820 if (perf_hpp__is_dynamic_entry(fmt) && 1821 perf_hpp__defined_dynamic_entry(fmt, he->hists)) 1822 fmt->sort(fmt, he, NULL); /* update column width */ 1823 } 1824 } 1825 1826 static void output_resort(struct hists *hists, struct ui_progress *prog, 1827 bool use_callchain, hists__resort_cb_t cb, 1828 void *cb_arg) 1829 { 1830 struct rb_root_cached *root; 1831 struct rb_node *next; 1832 struct hist_entry *n; 1833 u64 callchain_total; 1834 u64 min_callchain_hits; 1835 1836 callchain_total = hists->callchain_period; 1837 if (symbol_conf.filter_relative) 1838 callchain_total = hists->callchain_non_filtered_period; 1839 1840 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100); 1841 1842 hists__reset_stats(hists); 1843 hists__reset_col_len(hists); 1844 1845 if (symbol_conf.report_hierarchy) { 1846 hists__hierarchy_output_resort(hists, prog, 1847 &hists->entries_collapsed, 1848 &hists->entries, 1849 min_callchain_hits, 1850 use_callchain); 1851 hierarchy_recalc_total_periods(hists); 1852 return; 1853 } 1854 1855 if (hists__has(hists, need_collapse)) 1856 root = &hists->entries_collapsed; 1857 else 1858 root = hists->entries_in; 1859 1860 next = rb_first_cached(root); 1861 hists->entries = RB_ROOT_CACHED; 1862 1863 while (next) { 1864 n = rb_entry(next, struct hist_entry, rb_node_in); 1865 next = rb_next(&n->rb_node_in); 1866 1867 if (cb && cb(n, cb_arg)) 1868 continue; 1869 1870 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain); 1871 hists__inc_stats(hists, n); 1872 1873 if (!n->filtered) 1874 hists__calc_col_len(hists, n); 1875 1876 if (prog) 1877 ui_progress__update(prog, 1); 1878 } 1879 } 1880 1881 void perf_evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog, 1882 hists__resort_cb_t cb, void *cb_arg) 1883 { 1884 bool use_callchain; 1885 1886 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph) 1887 use_callchain = evsel__has_callchain(evsel); 1888 else 1889 use_callchain = symbol_conf.use_callchain; 1890 1891 use_callchain |= symbol_conf.show_branchflag_count; 1892 1893 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg); 1894 } 1895 1896 void perf_evsel__output_resort(struct evsel *evsel, struct ui_progress *prog) 1897 { 1898 return perf_evsel__output_resort_cb(evsel, prog, NULL, NULL); 1899 } 1900 1901 void hists__output_resort(struct hists *hists, struct ui_progress *prog) 1902 { 1903 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL); 1904 } 1905 1906 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog, 1907 hists__resort_cb_t cb) 1908 { 1909 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL); 1910 } 1911 1912 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd) 1913 { 1914 if (he->leaf || hmd == HMD_FORCE_SIBLING) 1915 return false; 1916 1917 if (he->unfolded || hmd == HMD_FORCE_CHILD) 1918 return true; 1919 1920 return false; 1921 } 1922 1923 struct rb_node *rb_hierarchy_last(struct rb_node *node) 1924 { 1925 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node); 1926 1927 while (can_goto_child(he, HMD_NORMAL)) { 1928 node = rb_last(&he->hroot_out.rb_root); 1929 he = rb_entry(node, struct hist_entry, rb_node); 1930 } 1931 return node; 1932 } 1933 1934 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd) 1935 { 1936 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node); 1937 1938 if (can_goto_child(he, hmd)) 1939 node = rb_first_cached(&he->hroot_out); 1940 else 1941 node = rb_next(node); 1942 1943 while (node == NULL) { 1944 he = he->parent_he; 1945 if (he == NULL) 1946 break; 1947 1948 node = rb_next(&he->rb_node); 1949 } 1950 return node; 1951 } 1952 1953 struct rb_node *rb_hierarchy_prev(struct rb_node *node) 1954 { 1955 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node); 1956 1957 node = rb_prev(node); 1958 if (node) 1959 return rb_hierarchy_last(node); 1960 1961 he = he->parent_he; 1962 if (he == NULL) 1963 return NULL; 1964 1965 return &he->rb_node; 1966 } 1967 1968 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit) 1969 { 1970 struct rb_node *node; 1971 struct hist_entry *child; 1972 float percent; 1973 1974 if (he->leaf) 1975 return false; 1976 1977 node = rb_first_cached(&he->hroot_out); 1978 child = rb_entry(node, struct hist_entry, rb_node); 1979 1980 while (node && child->filtered) { 1981 node = rb_next(node); 1982 child = rb_entry(node, struct hist_entry, rb_node); 1983 } 1984 1985 if (node) 1986 percent = hist_entry__get_percent_limit(child); 1987 else 1988 percent = 0; 1989 1990 return node && percent >= limit; 1991 } 1992 1993 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, 1994 enum hist_filter filter) 1995 { 1996 h->filtered &= ~(1 << filter); 1997 1998 if (symbol_conf.report_hierarchy) { 1999 struct hist_entry *parent = h->parent_he; 2000 2001 while (parent) { 2002 he_stat__add_stat(&parent->stat, &h->stat); 2003 2004 parent->filtered &= ~(1 << filter); 2005 2006 if (parent->filtered) 2007 goto next; 2008 2009 /* force fold unfiltered entry for simplicity */ 2010 parent->unfolded = false; 2011 parent->has_no_entry = false; 2012 parent->row_offset = 0; 2013 parent->nr_rows = 0; 2014 next: 2015 parent = parent->parent_he; 2016 } 2017 } 2018 2019 if (h->filtered) 2020 return; 2021 2022 /* force fold unfiltered entry for simplicity */ 2023 h->unfolded = false; 2024 h->has_no_entry = false; 2025 h->row_offset = 0; 2026 h->nr_rows = 0; 2027 2028 hists->stats.nr_non_filtered_samples += h->stat.nr_events; 2029 2030 hists__inc_filter_stats(hists, h); 2031 hists__calc_col_len(hists, h); 2032 } 2033 2034 2035 static bool hists__filter_entry_by_dso(struct hists *hists, 2036 struct hist_entry *he) 2037 { 2038 if (hists->dso_filter != NULL && 2039 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { 2040 he->filtered |= (1 << HIST_FILTER__DSO); 2041 return true; 2042 } 2043 2044 return false; 2045 } 2046 2047 static bool hists__filter_entry_by_thread(struct hists *hists, 2048 struct hist_entry *he) 2049 { 2050 if (hists->thread_filter != NULL && 2051 he->thread != hists->thread_filter) { 2052 he->filtered |= (1 << HIST_FILTER__THREAD); 2053 return true; 2054 } 2055 2056 return false; 2057 } 2058 2059 static bool hists__filter_entry_by_symbol(struct hists *hists, 2060 struct hist_entry *he) 2061 { 2062 if (hists->symbol_filter_str != NULL && 2063 (!he->ms.sym || strstr(he->ms.sym->name, 2064 hists->symbol_filter_str) == NULL)) { 2065 he->filtered |= (1 << HIST_FILTER__SYMBOL); 2066 return true; 2067 } 2068 2069 return false; 2070 } 2071 2072 static bool hists__filter_entry_by_socket(struct hists *hists, 2073 struct hist_entry *he) 2074 { 2075 if ((hists->socket_filter > -1) && 2076 (he->socket != hists->socket_filter)) { 2077 he->filtered |= (1 << HIST_FILTER__SOCKET); 2078 return true; 2079 } 2080 2081 return false; 2082 } 2083 2084 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he); 2085 2086 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter) 2087 { 2088 struct rb_node *nd; 2089 2090 hists->stats.nr_non_filtered_samples = 0; 2091 2092 hists__reset_filter_stats(hists); 2093 hists__reset_col_len(hists); 2094 2095 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) { 2096 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); 2097 2098 if (filter(hists, h)) 2099 continue; 2100 2101 hists__remove_entry_filter(hists, h, type); 2102 } 2103 } 2104 2105 static void resort_filtered_entry(struct rb_root_cached *root, 2106 struct hist_entry *he) 2107 { 2108 struct rb_node **p = &root->rb_root.rb_node; 2109 struct rb_node *parent = NULL; 2110 struct hist_entry *iter; 2111 struct rb_root_cached new_root = RB_ROOT_CACHED; 2112 struct rb_node *nd; 2113 bool leftmost = true; 2114 2115 while (*p != NULL) { 2116 parent = *p; 2117 iter = rb_entry(parent, struct hist_entry, rb_node); 2118 2119 if (hist_entry__sort(he, iter) > 0) 2120 p = &(*p)->rb_left; 2121 else { 2122 p = &(*p)->rb_right; 2123 leftmost = false; 2124 } 2125 } 2126 2127 rb_link_node(&he->rb_node, parent, p); 2128 rb_insert_color_cached(&he->rb_node, root, leftmost); 2129 2130 if (he->leaf || he->filtered) 2131 return; 2132 2133 nd = rb_first_cached(&he->hroot_out); 2134 while (nd) { 2135 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); 2136 2137 nd = rb_next(nd); 2138 rb_erase_cached(&h->rb_node, &he->hroot_out); 2139 2140 resort_filtered_entry(&new_root, h); 2141 } 2142 2143 he->hroot_out = new_root; 2144 } 2145 2146 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg) 2147 { 2148 struct rb_node *nd; 2149 struct rb_root_cached new_root = RB_ROOT_CACHED; 2150 2151 hists->stats.nr_non_filtered_samples = 0; 2152 2153 hists__reset_filter_stats(hists); 2154 hists__reset_col_len(hists); 2155 2156 nd = rb_first_cached(&hists->entries); 2157 while (nd) { 2158 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); 2159 int ret; 2160 2161 ret = hist_entry__filter(h, type, arg); 2162 2163 /* 2164 * case 1. non-matching type 2165 * zero out the period, set filter marker and move to child 2166 */ 2167 if (ret < 0) { 2168 memset(&h->stat, 0, sizeof(h->stat)); 2169 h->filtered |= (1 << type); 2170 2171 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD); 2172 } 2173 /* 2174 * case 2. matched type (filter out) 2175 * set filter marker and move to next 2176 */ 2177 else if (ret == 1) { 2178 h->filtered |= (1 << type); 2179 2180 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING); 2181 } 2182 /* 2183 * case 3. ok (not filtered) 2184 * add period to hists and parents, erase the filter marker 2185 * and move to next sibling 2186 */ 2187 else { 2188 hists__remove_entry_filter(hists, h, type); 2189 2190 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING); 2191 } 2192 } 2193 2194 hierarchy_recalc_total_periods(hists); 2195 2196 /* 2197 * resort output after applying a new filter since filter in a lower 2198 * hierarchy can change periods in a upper hierarchy. 2199 */ 2200 nd = rb_first_cached(&hists->entries); 2201 while (nd) { 2202 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); 2203 2204 nd = rb_next(nd); 2205 rb_erase_cached(&h->rb_node, &hists->entries); 2206 2207 resort_filtered_entry(&new_root, h); 2208 } 2209 2210 hists->entries = new_root; 2211 } 2212 2213 void hists__filter_by_thread(struct hists *hists) 2214 { 2215 if (symbol_conf.report_hierarchy) 2216 hists__filter_hierarchy(hists, HIST_FILTER__THREAD, 2217 hists->thread_filter); 2218 else 2219 hists__filter_by_type(hists, HIST_FILTER__THREAD, 2220 hists__filter_entry_by_thread); 2221 } 2222 2223 void hists__filter_by_dso(struct hists *hists) 2224 { 2225 if (symbol_conf.report_hierarchy) 2226 hists__filter_hierarchy(hists, HIST_FILTER__DSO, 2227 hists->dso_filter); 2228 else 2229 hists__filter_by_type(hists, HIST_FILTER__DSO, 2230 hists__filter_entry_by_dso); 2231 } 2232 2233 void hists__filter_by_symbol(struct hists *hists) 2234 { 2235 if (symbol_conf.report_hierarchy) 2236 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL, 2237 hists->symbol_filter_str); 2238 else 2239 hists__filter_by_type(hists, HIST_FILTER__SYMBOL, 2240 hists__filter_entry_by_symbol); 2241 } 2242 2243 void hists__filter_by_socket(struct hists *hists) 2244 { 2245 if (symbol_conf.report_hierarchy) 2246 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET, 2247 &hists->socket_filter); 2248 else 2249 hists__filter_by_type(hists, HIST_FILTER__SOCKET, 2250 hists__filter_entry_by_socket); 2251 } 2252 2253 void events_stats__inc(struct events_stats *stats, u32 type) 2254 { 2255 ++stats->nr_events[0]; 2256 ++stats->nr_events[type]; 2257 } 2258 2259 void hists__inc_nr_events(struct hists *hists, u32 type) 2260 { 2261 events_stats__inc(&hists->stats, type); 2262 } 2263 2264 void hists__inc_nr_samples(struct hists *hists, bool filtered) 2265 { 2266 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE); 2267 if (!filtered) 2268 hists->stats.nr_non_filtered_samples++; 2269 } 2270 2271 static struct hist_entry *hists__add_dummy_entry(struct hists *hists, 2272 struct hist_entry *pair) 2273 { 2274 struct rb_root_cached *root; 2275 struct rb_node **p; 2276 struct rb_node *parent = NULL; 2277 struct hist_entry *he; 2278 int64_t cmp; 2279 bool leftmost = true; 2280 2281 if (hists__has(hists, need_collapse)) 2282 root = &hists->entries_collapsed; 2283 else 2284 root = hists->entries_in; 2285 2286 p = &root->rb_root.rb_node; 2287 2288 while (*p != NULL) { 2289 parent = *p; 2290 he = rb_entry(parent, struct hist_entry, rb_node_in); 2291 2292 cmp = hist_entry__collapse(he, pair); 2293 2294 if (!cmp) 2295 goto out; 2296 2297 if (cmp < 0) 2298 p = &(*p)->rb_left; 2299 else { 2300 p = &(*p)->rb_right; 2301 leftmost = false; 2302 } 2303 } 2304 2305 he = hist_entry__new(pair, true); 2306 if (he) { 2307 memset(&he->stat, 0, sizeof(he->stat)); 2308 he->hists = hists; 2309 if (symbol_conf.cumulate_callchain) 2310 memset(he->stat_acc, 0, sizeof(he->stat)); 2311 rb_link_node(&he->rb_node_in, parent, p); 2312 rb_insert_color_cached(&he->rb_node_in, root, leftmost); 2313 hists__inc_stats(hists, he); 2314 he->dummy = true; 2315 } 2316 out: 2317 return he; 2318 } 2319 2320 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists, 2321 struct rb_root_cached *root, 2322 struct hist_entry *pair) 2323 { 2324 struct rb_node **p; 2325 struct rb_node *parent = NULL; 2326 struct hist_entry *he; 2327 struct perf_hpp_fmt *fmt; 2328 bool leftmost = true; 2329 2330 p = &root->rb_root.rb_node; 2331 while (*p != NULL) { 2332 int64_t cmp = 0; 2333 2334 parent = *p; 2335 he = rb_entry(parent, struct hist_entry, rb_node_in); 2336 2337 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) { 2338 cmp = fmt->collapse(fmt, he, pair); 2339 if (cmp) 2340 break; 2341 } 2342 if (!cmp) 2343 goto out; 2344 2345 if (cmp < 0) 2346 p = &parent->rb_left; 2347 else { 2348 p = &parent->rb_right; 2349 leftmost = false; 2350 } 2351 } 2352 2353 he = hist_entry__new(pair, true); 2354 if (he) { 2355 rb_link_node(&he->rb_node_in, parent, p); 2356 rb_insert_color_cached(&he->rb_node_in, root, leftmost); 2357 2358 he->dummy = true; 2359 he->hists = hists; 2360 memset(&he->stat, 0, sizeof(he->stat)); 2361 hists__inc_stats(hists, he); 2362 } 2363 out: 2364 return he; 2365 } 2366 2367 static struct hist_entry *hists__find_entry(struct hists *hists, 2368 struct hist_entry *he) 2369 { 2370 struct rb_node *n; 2371 2372 if (hists__has(hists, need_collapse)) 2373 n = hists->entries_collapsed.rb_root.rb_node; 2374 else 2375 n = hists->entries_in->rb_root.rb_node; 2376 2377 while (n) { 2378 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); 2379 int64_t cmp = hist_entry__collapse(iter, he); 2380 2381 if (cmp < 0) 2382 n = n->rb_left; 2383 else if (cmp > 0) 2384 n = n->rb_right; 2385 else 2386 return iter; 2387 } 2388 2389 return NULL; 2390 } 2391 2392 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root, 2393 struct hist_entry *he) 2394 { 2395 struct rb_node *n = root->rb_root.rb_node; 2396 2397 while (n) { 2398 struct hist_entry *iter; 2399 struct perf_hpp_fmt *fmt; 2400 int64_t cmp = 0; 2401 2402 iter = rb_entry(n, struct hist_entry, rb_node_in); 2403 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) { 2404 cmp = fmt->collapse(fmt, iter, he); 2405 if (cmp) 2406 break; 2407 } 2408 2409 if (cmp < 0) 2410 n = n->rb_left; 2411 else if (cmp > 0) 2412 n = n->rb_right; 2413 else 2414 return iter; 2415 } 2416 2417 return NULL; 2418 } 2419 2420 static void hists__match_hierarchy(struct rb_root_cached *leader_root, 2421 struct rb_root_cached *other_root) 2422 { 2423 struct rb_node *nd; 2424 struct hist_entry *pos, *pair; 2425 2426 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) { 2427 pos = rb_entry(nd, struct hist_entry, rb_node_in); 2428 pair = hists__find_hierarchy_entry(other_root, pos); 2429 2430 if (pair) { 2431 hist_entry__add_pair(pair, pos); 2432 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in); 2433 } 2434 } 2435 } 2436 2437 /* 2438 * Look for pairs to link to the leader buckets (hist_entries): 2439 */ 2440 void hists__match(struct hists *leader, struct hists *other) 2441 { 2442 struct rb_root_cached *root; 2443 struct rb_node *nd; 2444 struct hist_entry *pos, *pair; 2445 2446 if (symbol_conf.report_hierarchy) { 2447 /* hierarchy report always collapses entries */ 2448 return hists__match_hierarchy(&leader->entries_collapsed, 2449 &other->entries_collapsed); 2450 } 2451 2452 if (hists__has(leader, need_collapse)) 2453 root = &leader->entries_collapsed; 2454 else 2455 root = leader->entries_in; 2456 2457 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) { 2458 pos = rb_entry(nd, struct hist_entry, rb_node_in); 2459 pair = hists__find_entry(other, pos); 2460 2461 if (pair) 2462 hist_entry__add_pair(pair, pos); 2463 } 2464 } 2465 2466 static int hists__link_hierarchy(struct hists *leader_hists, 2467 struct hist_entry *parent, 2468 struct rb_root_cached *leader_root, 2469 struct rb_root_cached *other_root) 2470 { 2471 struct rb_node *nd; 2472 struct hist_entry *pos, *leader; 2473 2474 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) { 2475 pos = rb_entry(nd, struct hist_entry, rb_node_in); 2476 2477 if (hist_entry__has_pairs(pos)) { 2478 bool found = false; 2479 2480 list_for_each_entry(leader, &pos->pairs.head, pairs.node) { 2481 if (leader->hists == leader_hists) { 2482 found = true; 2483 break; 2484 } 2485 } 2486 if (!found) 2487 return -1; 2488 } else { 2489 leader = add_dummy_hierarchy_entry(leader_hists, 2490 leader_root, pos); 2491 if (leader == NULL) 2492 return -1; 2493 2494 /* do not point parent in the pos */ 2495 leader->parent_he = parent; 2496 2497 hist_entry__add_pair(pos, leader); 2498 } 2499 2500 if (!pos->leaf) { 2501 if (hists__link_hierarchy(leader_hists, leader, 2502 &leader->hroot_in, 2503 &pos->hroot_in) < 0) 2504 return -1; 2505 } 2506 } 2507 return 0; 2508 } 2509 2510 /* 2511 * Look for entries in the other hists that are not present in the leader, if 2512 * we find them, just add a dummy entry on the leader hists, with period=0, 2513 * nr_events=0, to serve as the list header. 2514 */ 2515 int hists__link(struct hists *leader, struct hists *other) 2516 { 2517 struct rb_root_cached *root; 2518 struct rb_node *nd; 2519 struct hist_entry *pos, *pair; 2520 2521 if (symbol_conf.report_hierarchy) { 2522 /* hierarchy report always collapses entries */ 2523 return hists__link_hierarchy(leader, NULL, 2524 &leader->entries_collapsed, 2525 &other->entries_collapsed); 2526 } 2527 2528 if (hists__has(other, need_collapse)) 2529 root = &other->entries_collapsed; 2530 else 2531 root = other->entries_in; 2532 2533 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) { 2534 pos = rb_entry(nd, struct hist_entry, rb_node_in); 2535 2536 if (!hist_entry__has_pairs(pos)) { 2537 pair = hists__add_dummy_entry(leader, pos); 2538 if (pair == NULL) 2539 return -1; 2540 hist_entry__add_pair(pos, pair); 2541 } 2542 } 2543 2544 return 0; 2545 } 2546 2547 int hists__unlink(struct hists *hists) 2548 { 2549 struct rb_root_cached *root; 2550 struct rb_node *nd; 2551 struct hist_entry *pos; 2552 2553 if (hists__has(hists, need_collapse)) 2554 root = &hists->entries_collapsed; 2555 else 2556 root = hists->entries_in; 2557 2558 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) { 2559 pos = rb_entry(nd, struct hist_entry, rb_node_in); 2560 list_del_init(&pos->pairs.node); 2561 } 2562 2563 return 0; 2564 } 2565 2566 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al, 2567 struct perf_sample *sample, bool nonany_branch_mode) 2568 { 2569 struct branch_info *bi; 2570 2571 /* If we have branch cycles always annotate them. */ 2572 if (bs && bs->nr && bs->entries[0].flags.cycles) { 2573 int i; 2574 2575 bi = sample__resolve_bstack(sample, al); 2576 if (bi) { 2577 struct addr_map_symbol *prev = NULL; 2578 2579 /* 2580 * Ignore errors, still want to process the 2581 * other entries. 2582 * 2583 * For non standard branch modes always 2584 * force no IPC (prev == NULL) 2585 * 2586 * Note that perf stores branches reversed from 2587 * program order! 2588 */ 2589 for (i = bs->nr - 1; i >= 0; i--) { 2590 addr_map_symbol__account_cycles(&bi[i].from, 2591 nonany_branch_mode ? NULL : prev, 2592 bi[i].flags.cycles); 2593 prev = &bi[i].to; 2594 } 2595 free(bi); 2596 } 2597 } 2598 } 2599 2600 size_t perf_evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp) 2601 { 2602 struct evsel *pos; 2603 size_t ret = 0; 2604 2605 evlist__for_each_entry(evlist, pos) { 2606 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos)); 2607 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp); 2608 } 2609 2610 return ret; 2611 } 2612 2613 2614 u64 hists__total_period(struct hists *hists) 2615 { 2616 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period : 2617 hists->stats.total_period; 2618 } 2619 2620 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq) 2621 { 2622 char unit; 2623 int printed; 2624 const struct dso *dso = hists->dso_filter; 2625 struct thread *thread = hists->thread_filter; 2626 int socket_id = hists->socket_filter; 2627 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE]; 2628 u64 nr_events = hists->stats.total_period; 2629 struct evsel *evsel = hists_to_evsel(hists); 2630 const char *ev_name = perf_evsel__name(evsel); 2631 char buf[512], sample_freq_str[64] = ""; 2632 size_t buflen = sizeof(buf); 2633 char ref[30] = " show reference callgraph, "; 2634 bool enable_ref = false; 2635 2636 if (symbol_conf.filter_relative) { 2637 nr_samples = hists->stats.nr_non_filtered_samples; 2638 nr_events = hists->stats.total_non_filtered_period; 2639 } 2640 2641 if (perf_evsel__is_group_event(evsel)) { 2642 struct evsel *pos; 2643 2644 perf_evsel__group_desc(evsel, buf, buflen); 2645 ev_name = buf; 2646 2647 for_each_group_member(pos, evsel) { 2648 struct hists *pos_hists = evsel__hists(pos); 2649 2650 if (symbol_conf.filter_relative) { 2651 nr_samples += pos_hists->stats.nr_non_filtered_samples; 2652 nr_events += pos_hists->stats.total_non_filtered_period; 2653 } else { 2654 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE]; 2655 nr_events += pos_hists->stats.total_period; 2656 } 2657 } 2658 } 2659 2660 if (symbol_conf.show_ref_callgraph && 2661 strstr(ev_name, "call-graph=no")) 2662 enable_ref = true; 2663 2664 if (show_freq) 2665 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq); 2666 2667 nr_samples = convert_unit(nr_samples, &unit); 2668 printed = scnprintf(bf, size, 2669 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64, 2670 nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "", 2671 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events); 2672 2673 2674 if (hists->uid_filter_str) 2675 printed += snprintf(bf + printed, size - printed, 2676 ", UID: %s", hists->uid_filter_str); 2677 if (thread) { 2678 if (hists__has(hists, thread)) { 2679 printed += scnprintf(bf + printed, size - printed, 2680 ", Thread: %s(%d)", 2681 (thread->comm_set ? thread__comm_str(thread) : ""), 2682 thread->tid); 2683 } else { 2684 printed += scnprintf(bf + printed, size - printed, 2685 ", Thread: %s", 2686 (thread->comm_set ? thread__comm_str(thread) : "")); 2687 } 2688 } 2689 if (dso) 2690 printed += scnprintf(bf + printed, size - printed, 2691 ", DSO: %s", dso->short_name); 2692 if (socket_id > -1) 2693 printed += scnprintf(bf + printed, size - printed, 2694 ", Processor Socket: %d", socket_id); 2695 2696 return printed; 2697 } 2698 2699 int parse_filter_percentage(const struct option *opt __maybe_unused, 2700 const char *arg, int unset __maybe_unused) 2701 { 2702 if (!strcmp(arg, "relative")) 2703 symbol_conf.filter_relative = true; 2704 else if (!strcmp(arg, "absolute")) 2705 symbol_conf.filter_relative = false; 2706 else { 2707 pr_debug("Invalid percentage: %s\n", arg); 2708 return -1; 2709 } 2710 2711 return 0; 2712 } 2713 2714 int perf_hist_config(const char *var, const char *value) 2715 { 2716 if (!strcmp(var, "hist.percentage")) 2717 return parse_filter_percentage(NULL, value, 0); 2718 2719 return 0; 2720 } 2721 2722 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list) 2723 { 2724 memset(hists, 0, sizeof(*hists)); 2725 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED; 2726 hists->entries_in = &hists->entries_in_array[0]; 2727 hists->entries_collapsed = RB_ROOT_CACHED; 2728 hists->entries = RB_ROOT_CACHED; 2729 pthread_mutex_init(&hists->lock, NULL); 2730 hists->socket_filter = -1; 2731 hists->hpp_list = hpp_list; 2732 INIT_LIST_HEAD(&hists->hpp_formats); 2733 return 0; 2734 } 2735 2736 static void hists__delete_remaining_entries(struct rb_root_cached *root) 2737 { 2738 struct rb_node *node; 2739 struct hist_entry *he; 2740 2741 while (!RB_EMPTY_ROOT(&root->rb_root)) { 2742 node = rb_first_cached(root); 2743 rb_erase_cached(node, root); 2744 2745 he = rb_entry(node, struct hist_entry, rb_node_in); 2746 hist_entry__delete(he); 2747 } 2748 } 2749 2750 static void hists__delete_all_entries(struct hists *hists) 2751 { 2752 hists__delete_entries(hists); 2753 hists__delete_remaining_entries(&hists->entries_in_array[0]); 2754 hists__delete_remaining_entries(&hists->entries_in_array[1]); 2755 hists__delete_remaining_entries(&hists->entries_collapsed); 2756 } 2757 2758 static void hists_evsel__exit(struct evsel *evsel) 2759 { 2760 struct hists *hists = evsel__hists(evsel); 2761 struct perf_hpp_fmt *fmt, *pos; 2762 struct perf_hpp_list_node *node, *tmp; 2763 2764 hists__delete_all_entries(hists); 2765 2766 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) { 2767 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) { 2768 list_del_init(&fmt->list); 2769 free(fmt); 2770 } 2771 list_del_init(&node->list); 2772 free(node); 2773 } 2774 } 2775 2776 static int hists_evsel__init(struct evsel *evsel) 2777 { 2778 struct hists *hists = evsel__hists(evsel); 2779 2780 __hists__init(hists, &perf_hpp_list); 2781 return 0; 2782 } 2783 2784 /* 2785 * XXX We probably need a hists_evsel__exit() to free the hist_entries 2786 * stored in the rbtree... 2787 */ 2788 2789 int hists__init(void) 2790 { 2791 int err = perf_evsel__object_config(sizeof(struct hists_evsel), 2792 hists_evsel__init, 2793 hists_evsel__exit); 2794 if (err) 2795 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr); 2796 2797 return err; 2798 } 2799 2800 void perf_hpp_list__init(struct perf_hpp_list *list) 2801 { 2802 INIT_LIST_HEAD(&list->fields); 2803 INIT_LIST_HEAD(&list->sorts); 2804 } 2805