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