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