| 1 | #include "util.h" |
| 2 | #include "build-id.h" |
| 3 | #include "hist.h" |
| 4 | #include "session.h" |
| 5 | #include "sort.h" |
| 6 | #include "evsel.h" |
| 7 | #include <math.h> |
| 8 | |
| 9 | static bool hists__filter_entry_by_dso(struct hists *hists, |
| 10 | struct hist_entry *he); |
| 11 | static bool hists__filter_entry_by_thread(struct hists *hists, |
| 12 | struct hist_entry *he); |
| 13 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
| 14 | struct hist_entry *he); |
| 15 | |
| 16 | struct callchain_param callchain_param = { |
| 17 | .mode = CHAIN_GRAPH_REL, |
| 18 | .min_percent = 0.5, |
| 19 | .order = ORDER_CALLEE, |
| 20 | .key = CCKEY_FUNCTION |
| 21 | }; |
| 22 | |
| 23 | u16 hists__col_len(struct hists *hists, enum hist_column col) |
| 24 | { |
| 25 | return hists->col_len[col]; |
| 26 | } |
| 27 | |
| 28 | void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len) |
| 29 | { |
| 30 | hists->col_len[col] = len; |
| 31 | } |
| 32 | |
| 33 | bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) |
| 34 | { |
| 35 | if (len > hists__col_len(hists, col)) { |
| 36 | hists__set_col_len(hists, col, len); |
| 37 | return true; |
| 38 | } |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | void hists__reset_col_len(struct hists *hists) |
| 43 | { |
| 44 | enum hist_column col; |
| 45 | |
| 46 | for (col = 0; col < HISTC_NR_COLS; ++col) |
| 47 | hists__set_col_len(hists, col, 0); |
| 48 | } |
| 49 | |
| 50 | static void hists__set_unres_dso_col_len(struct hists *hists, int dso) |
| 51 | { |
| 52 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
| 53 | |
| 54 | if (hists__col_len(hists, dso) < unresolved_col_width && |
| 55 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 56 | !symbol_conf.dso_list) |
| 57 | hists__set_col_len(hists, dso, unresolved_col_width); |
| 58 | } |
| 59 | |
| 60 | void hists__calc_col_len(struct hists *hists, struct hist_entry *h) |
| 61 | { |
| 62 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
| 63 | int symlen; |
| 64 | u16 len; |
| 65 | |
| 66 | /* |
| 67 | * +4 accounts for '[x] ' priv level info |
| 68 | * +2 accounts for 0x prefix on raw addresses |
| 69 | * +3 accounts for ' y ' symtab origin info |
| 70 | */ |
| 71 | if (h->ms.sym) { |
| 72 | symlen = h->ms.sym->namelen + 4; |
| 73 | if (verbose) |
| 74 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
| 75 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); |
| 76 | } else { |
| 77 | symlen = unresolved_col_width + 4 + 2; |
| 78 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); |
| 79 | hists__set_unres_dso_col_len(hists, HISTC_DSO); |
| 80 | } |
| 81 | |
| 82 | len = thread__comm_len(h->thread); |
| 83 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
| 84 | hists__set_col_len(hists, HISTC_THREAD, len + 6); |
| 85 | |
| 86 | if (h->ms.map) { |
| 87 | len = dso__name_len(h->ms.map->dso); |
| 88 | hists__new_col_len(hists, HISTC_DSO, len); |
| 89 | } |
| 90 | |
| 91 | if (h->parent) |
| 92 | hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen); |
| 93 | |
| 94 | if (h->branch_info) { |
| 95 | if (h->branch_info->from.sym) { |
| 96 | symlen = (int)h->branch_info->from.sym->namelen + 4; |
| 97 | if (verbose) |
| 98 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
| 99 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); |
| 100 | |
| 101 | symlen = dso__name_len(h->branch_info->from.map->dso); |
| 102 | hists__new_col_len(hists, HISTC_DSO_FROM, symlen); |
| 103 | } else { |
| 104 | symlen = unresolved_col_width + 4 + 2; |
| 105 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); |
| 106 | hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM); |
| 107 | } |
| 108 | |
| 109 | if (h->branch_info->to.sym) { |
| 110 | symlen = (int)h->branch_info->to.sym->namelen + 4; |
| 111 | if (verbose) |
| 112 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
| 113 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
| 114 | |
| 115 | symlen = dso__name_len(h->branch_info->to.map->dso); |
| 116 | hists__new_col_len(hists, HISTC_DSO_TO, symlen); |
| 117 | } else { |
| 118 | symlen = unresolved_col_width + 4 + 2; |
| 119 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
| 120 | hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (h->mem_info) { |
| 125 | if (h->mem_info->daddr.sym) { |
| 126 | symlen = (int)h->mem_info->daddr.sym->namelen + 4 |
| 127 | + unresolved_col_width + 2; |
| 128 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, |
| 129 | symlen); |
| 130 | } else { |
| 131 | symlen = unresolved_col_width + 4 + 2; |
| 132 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, |
| 133 | symlen); |
| 134 | } |
| 135 | if (h->mem_info->daddr.map) { |
| 136 | symlen = dso__name_len(h->mem_info->daddr.map->dso); |
| 137 | hists__new_col_len(hists, HISTC_MEM_DADDR_DSO, |
| 138 | symlen); |
| 139 | } else { |
| 140 | symlen = unresolved_col_width + 4 + 2; |
| 141 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); |
| 142 | } |
| 143 | } else { |
| 144 | symlen = unresolved_col_width + 4 + 2; |
| 145 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen); |
| 146 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); |
| 147 | } |
| 148 | |
| 149 | hists__new_col_len(hists, HISTC_MEM_LOCKED, 6); |
| 150 | hists__new_col_len(hists, HISTC_MEM_TLB, 22); |
| 151 | hists__new_col_len(hists, HISTC_MEM_SNOOP, 12); |
| 152 | hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3); |
| 153 | hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12); |
| 154 | hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12); |
| 155 | |
| 156 | if (h->transaction) |
| 157 | hists__new_col_len(hists, HISTC_TRANSACTION, |
| 158 | hist_entry__transaction_len()); |
| 159 | } |
| 160 | |
| 161 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
| 162 | { |
| 163 | struct rb_node *next = rb_first(&hists->entries); |
| 164 | struct hist_entry *n; |
| 165 | int row = 0; |
| 166 | |
| 167 | hists__reset_col_len(hists); |
| 168 | |
| 169 | while (next && row++ < max_rows) { |
| 170 | n = rb_entry(next, struct hist_entry, rb_node); |
| 171 | if (!n->filtered) |
| 172 | hists__calc_col_len(hists, n); |
| 173 | next = rb_next(&n->rb_node); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void he_stat__add_cpumode_period(struct he_stat *he_stat, |
| 178 | unsigned int cpumode, u64 period) |
| 179 | { |
| 180 | switch (cpumode) { |
| 181 | case PERF_RECORD_MISC_KERNEL: |
| 182 | he_stat->period_sys += period; |
| 183 | break; |
| 184 | case PERF_RECORD_MISC_USER: |
| 185 | he_stat->period_us += period; |
| 186 | break; |
| 187 | case PERF_RECORD_MISC_GUEST_KERNEL: |
| 188 | he_stat->period_guest_sys += period; |
| 189 | break; |
| 190 | case PERF_RECORD_MISC_GUEST_USER: |
| 191 | he_stat->period_guest_us += period; |
| 192 | break; |
| 193 | default: |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static void he_stat__add_period(struct he_stat *he_stat, u64 period, |
| 199 | u64 weight) |
| 200 | { |
| 201 | |
| 202 | he_stat->period += period; |
| 203 | he_stat->weight += weight; |
| 204 | he_stat->nr_events += 1; |
| 205 | } |
| 206 | |
| 207 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) |
| 208 | { |
| 209 | dest->period += src->period; |
| 210 | dest->period_sys += src->period_sys; |
| 211 | dest->period_us += src->period_us; |
| 212 | dest->period_guest_sys += src->period_guest_sys; |
| 213 | dest->period_guest_us += src->period_guest_us; |
| 214 | dest->nr_events += src->nr_events; |
| 215 | dest->weight += src->weight; |
| 216 | } |
| 217 | |
| 218 | static void he_stat__decay(struct he_stat *he_stat) |
| 219 | { |
| 220 | he_stat->period = (he_stat->period * 7) / 8; |
| 221 | he_stat->nr_events = (he_stat->nr_events * 7) / 8; |
| 222 | /* XXX need decay for weight too? */ |
| 223 | } |
| 224 | |
| 225 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) |
| 226 | { |
| 227 | u64 prev_period = he->stat.period; |
| 228 | |
| 229 | if (prev_period == 0) |
| 230 | return true; |
| 231 | |
| 232 | he_stat__decay(&he->stat); |
| 233 | |
| 234 | if (!he->filtered) |
| 235 | hists->stats.total_period -= prev_period - he->stat.period; |
| 236 | |
| 237 | return he->stat.period == 0; |
| 238 | } |
| 239 | |
| 240 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) |
| 241 | { |
| 242 | struct rb_node *next = rb_first(&hists->entries); |
| 243 | struct hist_entry *n; |
| 244 | |
| 245 | while (next) { |
| 246 | n = rb_entry(next, struct hist_entry, rb_node); |
| 247 | next = rb_next(&n->rb_node); |
| 248 | /* |
| 249 | * We may be annotating this, for instance, so keep it here in |
| 250 | * case some it gets new samples, we'll eventually free it when |
| 251 | * the user stops browsing and it agains gets fully decayed. |
| 252 | */ |
| 253 | if (((zap_user && n->level == '.') || |
| 254 | (zap_kernel && n->level != '.') || |
| 255 | hists__decay_entry(hists, n)) && |
| 256 | !n->used) { |
| 257 | rb_erase(&n->rb_node, &hists->entries); |
| 258 | |
| 259 | if (sort__need_collapse) |
| 260 | rb_erase(&n->rb_node_in, &hists->entries_collapsed); |
| 261 | |
| 262 | hist_entry__free(n); |
| 263 | --hists->nr_entries; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * histogram, sorted on item, collects periods |
| 270 | */ |
| 271 | |
| 272 | static struct hist_entry *hist_entry__new(struct hist_entry *template) |
| 273 | { |
| 274 | size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0; |
| 275 | struct hist_entry *he = zalloc(sizeof(*he) + callchain_size); |
| 276 | |
| 277 | if (he != NULL) { |
| 278 | *he = *template; |
| 279 | |
| 280 | if (he->ms.map) |
| 281 | he->ms.map->referenced = true; |
| 282 | |
| 283 | if (he->branch_info) { |
| 284 | /* |
| 285 | * This branch info is (a part of) allocated from |
| 286 | * sample__resolve_bstack() and will be freed after |
| 287 | * adding new entries. So we need to save a copy. |
| 288 | */ |
| 289 | he->branch_info = malloc(sizeof(*he->branch_info)); |
| 290 | if (he->branch_info == NULL) { |
| 291 | free(he); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | memcpy(he->branch_info, template->branch_info, |
| 296 | sizeof(*he->branch_info)); |
| 297 | |
| 298 | if (he->branch_info->from.map) |
| 299 | he->branch_info->from.map->referenced = true; |
| 300 | if (he->branch_info->to.map) |
| 301 | he->branch_info->to.map->referenced = true; |
| 302 | } |
| 303 | |
| 304 | if (he->mem_info) { |
| 305 | if (he->mem_info->iaddr.map) |
| 306 | he->mem_info->iaddr.map->referenced = true; |
| 307 | if (he->mem_info->daddr.map) |
| 308 | he->mem_info->daddr.map->referenced = true; |
| 309 | } |
| 310 | |
| 311 | if (symbol_conf.use_callchain) |
| 312 | callchain_init(he->callchain); |
| 313 | |
| 314 | INIT_LIST_HEAD(&he->pairs.node); |
| 315 | } |
| 316 | |
| 317 | return he; |
| 318 | } |
| 319 | |
| 320 | static u8 symbol__parent_filter(const struct symbol *parent) |
| 321 | { |
| 322 | if (symbol_conf.exclude_other && parent == NULL) |
| 323 | return 1 << HIST_FILTER__PARENT; |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static struct hist_entry *add_hist_entry(struct hists *hists, |
| 328 | struct hist_entry *entry, |
| 329 | struct addr_location *al) |
| 330 | { |
| 331 | struct rb_node **p; |
| 332 | struct rb_node *parent = NULL; |
| 333 | struct hist_entry *he; |
| 334 | int64_t cmp; |
| 335 | u64 period = entry->stat.period; |
| 336 | u64 weight = entry->stat.weight; |
| 337 | |
| 338 | p = &hists->entries_in->rb_node; |
| 339 | |
| 340 | while (*p != NULL) { |
| 341 | parent = *p; |
| 342 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
| 343 | |
| 344 | /* |
| 345 | * Make sure that it receives arguments in a same order as |
| 346 | * hist_entry__collapse() so that we can use an appropriate |
| 347 | * function when searching an entry regardless which sort |
| 348 | * keys were used. |
| 349 | */ |
| 350 | cmp = hist_entry__cmp(he, entry); |
| 351 | |
| 352 | if (!cmp) { |
| 353 | he_stat__add_period(&he->stat, period, weight); |
| 354 | |
| 355 | /* |
| 356 | * This mem info was allocated from sample__resolve_mem |
| 357 | * and will not be used anymore. |
| 358 | */ |
| 359 | zfree(&entry->mem_info); |
| 360 | |
| 361 | /* If the map of an existing hist_entry has |
| 362 | * become out-of-date due to an exec() or |
| 363 | * similar, update it. Otherwise we will |
| 364 | * mis-adjust symbol addresses when computing |
| 365 | * the history counter to increment. |
| 366 | */ |
| 367 | if (he->ms.map != entry->ms.map) { |
| 368 | he->ms.map = entry->ms.map; |
| 369 | if (he->ms.map) |
| 370 | he->ms.map->referenced = true; |
| 371 | } |
| 372 | goto out; |
| 373 | } |
| 374 | |
| 375 | if (cmp < 0) |
| 376 | p = &(*p)->rb_left; |
| 377 | else |
| 378 | p = &(*p)->rb_right; |
| 379 | } |
| 380 | |
| 381 | he = hist_entry__new(entry); |
| 382 | if (!he) |
| 383 | return NULL; |
| 384 | |
| 385 | hists->nr_entries++; |
| 386 | rb_link_node(&he->rb_node_in, parent, p); |
| 387 | rb_insert_color(&he->rb_node_in, hists->entries_in); |
| 388 | out: |
| 389 | he_stat__add_cpumode_period(&he->stat, al->cpumode, period); |
| 390 | return he; |
| 391 | } |
| 392 | |
| 393 | struct hist_entry *__hists__add_entry(struct hists *hists, |
| 394 | struct addr_location *al, |
| 395 | struct symbol *sym_parent, |
| 396 | struct branch_info *bi, |
| 397 | struct mem_info *mi, |
| 398 | u64 period, u64 weight, u64 transaction) |
| 399 | { |
| 400 | struct hist_entry entry = { |
| 401 | .thread = al->thread, |
| 402 | .comm = thread__comm(al->thread), |
| 403 | .ms = { |
| 404 | .map = al->map, |
| 405 | .sym = al->sym, |
| 406 | }, |
| 407 | .cpu = al->cpu, |
| 408 | .ip = al->addr, |
| 409 | .level = al->level, |
| 410 | .stat = { |
| 411 | .nr_events = 1, |
| 412 | .period = period, |
| 413 | .weight = weight, |
| 414 | }, |
| 415 | .parent = sym_parent, |
| 416 | .filtered = symbol__parent_filter(sym_parent) | al->filtered, |
| 417 | .hists = hists, |
| 418 | .branch_info = bi, |
| 419 | .mem_info = mi, |
| 420 | .transaction = transaction, |
| 421 | }; |
| 422 | |
| 423 | return add_hist_entry(hists, &entry, al); |
| 424 | } |
| 425 | |
| 426 | int64_t |
| 427 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 428 | { |
| 429 | struct sort_entry *se; |
| 430 | int64_t cmp = 0; |
| 431 | |
| 432 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 433 | cmp = se->se_cmp(left, right); |
| 434 | if (cmp) |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | return cmp; |
| 439 | } |
| 440 | |
| 441 | int64_t |
| 442 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 443 | { |
| 444 | struct sort_entry *se; |
| 445 | int64_t cmp = 0; |
| 446 | |
| 447 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 448 | int64_t (*f)(struct hist_entry *, struct hist_entry *); |
| 449 | |
| 450 | f = se->se_collapse ?: se->se_cmp; |
| 451 | |
| 452 | cmp = f(left, right); |
| 453 | if (cmp) |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | return cmp; |
| 458 | } |
| 459 | |
| 460 | void hist_entry__free(struct hist_entry *he) |
| 461 | { |
| 462 | zfree(&he->branch_info); |
| 463 | zfree(&he->mem_info); |
| 464 | free_srcline(he->srcline); |
| 465 | free(he); |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * collapse the histogram |
| 470 | */ |
| 471 | |
| 472 | static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused, |
| 473 | struct rb_root *root, |
| 474 | struct hist_entry *he) |
| 475 | { |
| 476 | struct rb_node **p = &root->rb_node; |
| 477 | struct rb_node *parent = NULL; |
| 478 | struct hist_entry *iter; |
| 479 | int64_t cmp; |
| 480 | |
| 481 | while (*p != NULL) { |
| 482 | parent = *p; |
| 483 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
| 484 | |
| 485 | cmp = hist_entry__collapse(iter, he); |
| 486 | |
| 487 | if (!cmp) { |
| 488 | he_stat__add_stat(&iter->stat, &he->stat); |
| 489 | |
| 490 | if (symbol_conf.use_callchain) { |
| 491 | callchain_cursor_reset(&callchain_cursor); |
| 492 | callchain_merge(&callchain_cursor, |
| 493 | iter->callchain, |
| 494 | he->callchain); |
| 495 | } |
| 496 | hist_entry__free(he); |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | if (cmp < 0) |
| 501 | p = &(*p)->rb_left; |
| 502 | else |
| 503 | p = &(*p)->rb_right; |
| 504 | } |
| 505 | |
| 506 | rb_link_node(&he->rb_node_in, parent, p); |
| 507 | rb_insert_color(&he->rb_node_in, root); |
| 508 | return true; |
| 509 | } |
| 510 | |
| 511 | static struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
| 512 | { |
| 513 | struct rb_root *root; |
| 514 | |
| 515 | pthread_mutex_lock(&hists->lock); |
| 516 | |
| 517 | root = hists->entries_in; |
| 518 | if (++hists->entries_in > &hists->entries_in_array[1]) |
| 519 | hists->entries_in = &hists->entries_in_array[0]; |
| 520 | |
| 521 | pthread_mutex_unlock(&hists->lock); |
| 522 | |
| 523 | return root; |
| 524 | } |
| 525 | |
| 526 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) |
| 527 | { |
| 528 | hists__filter_entry_by_dso(hists, he); |
| 529 | hists__filter_entry_by_thread(hists, he); |
| 530 | hists__filter_entry_by_symbol(hists, he); |
| 531 | } |
| 532 | |
| 533 | void hists__collapse_resort(struct hists *hists, struct ui_progress *prog) |
| 534 | { |
| 535 | struct rb_root *root; |
| 536 | struct rb_node *next; |
| 537 | struct hist_entry *n; |
| 538 | |
| 539 | if (!sort__need_collapse) |
| 540 | return; |
| 541 | |
| 542 | root = hists__get_rotate_entries_in(hists); |
| 543 | next = rb_first(root); |
| 544 | |
| 545 | while (next) { |
| 546 | if (session_done()) |
| 547 | break; |
| 548 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 549 | next = rb_next(&n->rb_node_in); |
| 550 | |
| 551 | rb_erase(&n->rb_node_in, root); |
| 552 | if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) { |
| 553 | /* |
| 554 | * If it wasn't combined with one of the entries already |
| 555 | * collapsed, we need to apply the filters that may have |
| 556 | * been set by, say, the hist_browser. |
| 557 | */ |
| 558 | hists__apply_filters(hists, n); |
| 559 | } |
| 560 | if (prog) |
| 561 | ui_progress__update(prog, 1); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * reverse the map, sort on period. |
| 567 | */ |
| 568 | |
| 569 | static int period_cmp(u64 period_a, u64 period_b) |
| 570 | { |
| 571 | if (period_a > period_b) |
| 572 | return 1; |
| 573 | if (period_a < period_b) |
| 574 | return -1; |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static int hist_entry__sort_on_period(struct hist_entry *a, |
| 579 | struct hist_entry *b) |
| 580 | { |
| 581 | int ret; |
| 582 | int i, nr_members; |
| 583 | struct perf_evsel *evsel; |
| 584 | struct hist_entry *pair; |
| 585 | u64 *periods_a, *periods_b; |
| 586 | |
| 587 | ret = period_cmp(a->stat.period, b->stat.period); |
| 588 | if (ret || !symbol_conf.event_group) |
| 589 | return ret; |
| 590 | |
| 591 | evsel = hists_to_evsel(a->hists); |
| 592 | nr_members = evsel->nr_members; |
| 593 | if (nr_members <= 1) |
| 594 | return ret; |
| 595 | |
| 596 | periods_a = zalloc(sizeof(periods_a) * nr_members); |
| 597 | periods_b = zalloc(sizeof(periods_b) * nr_members); |
| 598 | |
| 599 | if (!periods_a || !periods_b) |
| 600 | goto out; |
| 601 | |
| 602 | list_for_each_entry(pair, &a->pairs.head, pairs.node) { |
| 603 | evsel = hists_to_evsel(pair->hists); |
| 604 | periods_a[perf_evsel__group_idx(evsel)] = pair->stat.period; |
| 605 | } |
| 606 | |
| 607 | list_for_each_entry(pair, &b->pairs.head, pairs.node) { |
| 608 | evsel = hists_to_evsel(pair->hists); |
| 609 | periods_b[perf_evsel__group_idx(evsel)] = pair->stat.period; |
| 610 | } |
| 611 | |
| 612 | for (i = 1; i < nr_members; i++) { |
| 613 | ret = period_cmp(periods_a[i], periods_b[i]); |
| 614 | if (ret) |
| 615 | break; |
| 616 | } |
| 617 | |
| 618 | out: |
| 619 | free(periods_a); |
| 620 | free(periods_b); |
| 621 | |
| 622 | return ret; |
| 623 | } |
| 624 | |
| 625 | static void hists__reset_filter_stats(struct hists *hists) |
| 626 | { |
| 627 | hists->nr_non_filtered_entries = 0; |
| 628 | hists->stats.total_non_filtered_period = 0; |
| 629 | } |
| 630 | |
| 631 | void hists__reset_stats(struct hists *hists) |
| 632 | { |
| 633 | hists->nr_entries = 0; |
| 634 | hists->stats.total_period = 0; |
| 635 | |
| 636 | hists__reset_filter_stats(hists); |
| 637 | } |
| 638 | |
| 639 | static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h) |
| 640 | { |
| 641 | hists->nr_non_filtered_entries++; |
| 642 | hists->stats.total_non_filtered_period += h->stat.period; |
| 643 | } |
| 644 | |
| 645 | void hists__inc_stats(struct hists *hists, struct hist_entry *h) |
| 646 | { |
| 647 | if (!h->filtered) |
| 648 | hists__inc_filter_stats(hists, h); |
| 649 | |
| 650 | hists->nr_entries++; |
| 651 | hists->stats.total_period += h->stat.period; |
| 652 | } |
| 653 | |
| 654 | static void __hists__insert_output_entry(struct rb_root *entries, |
| 655 | struct hist_entry *he, |
| 656 | u64 min_callchain_hits) |
| 657 | { |
| 658 | struct rb_node **p = &entries->rb_node; |
| 659 | struct rb_node *parent = NULL; |
| 660 | struct hist_entry *iter; |
| 661 | |
| 662 | if (symbol_conf.use_callchain) |
| 663 | callchain_param.sort(&he->sorted_chain, he->callchain, |
| 664 | min_callchain_hits, &callchain_param); |
| 665 | |
| 666 | while (*p != NULL) { |
| 667 | parent = *p; |
| 668 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 669 | |
| 670 | if (hist_entry__sort_on_period(he, iter) > 0) |
| 671 | p = &(*p)->rb_left; |
| 672 | else |
| 673 | p = &(*p)->rb_right; |
| 674 | } |
| 675 | |
| 676 | rb_link_node(&he->rb_node, parent, p); |
| 677 | rb_insert_color(&he->rb_node, entries); |
| 678 | } |
| 679 | |
| 680 | void hists__output_resort(struct hists *hists) |
| 681 | { |
| 682 | struct rb_root *root; |
| 683 | struct rb_node *next; |
| 684 | struct hist_entry *n; |
| 685 | u64 min_callchain_hits; |
| 686 | |
| 687 | min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100); |
| 688 | |
| 689 | if (sort__need_collapse) |
| 690 | root = &hists->entries_collapsed; |
| 691 | else |
| 692 | root = hists->entries_in; |
| 693 | |
| 694 | next = rb_first(root); |
| 695 | hists->entries = RB_ROOT; |
| 696 | |
| 697 | hists__reset_stats(hists); |
| 698 | hists__reset_col_len(hists); |
| 699 | |
| 700 | while (next) { |
| 701 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 702 | next = rb_next(&n->rb_node_in); |
| 703 | |
| 704 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits); |
| 705 | hists__inc_stats(hists, n); |
| 706 | |
| 707 | if (!n->filtered) |
| 708 | hists__calc_col_len(hists, n); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, |
| 713 | enum hist_filter filter) |
| 714 | { |
| 715 | h->filtered &= ~(1 << filter); |
| 716 | if (h->filtered) |
| 717 | return; |
| 718 | |
| 719 | if (h->ms.unfolded) |
| 720 | hists->nr_non_filtered_entries += h->nr_rows; |
| 721 | h->row_offset = 0; |
| 722 | |
| 723 | hists->stats.nr_non_filtered_samples += h->stat.nr_events; |
| 724 | |
| 725 | hists__inc_filter_stats(hists, h); |
| 726 | hists__calc_col_len(hists, h); |
| 727 | } |
| 728 | |
| 729 | |
| 730 | static bool hists__filter_entry_by_dso(struct hists *hists, |
| 731 | struct hist_entry *he) |
| 732 | { |
| 733 | if (hists->dso_filter != NULL && |
| 734 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { |
| 735 | he->filtered |= (1 << HIST_FILTER__DSO); |
| 736 | return true; |
| 737 | } |
| 738 | |
| 739 | return false; |
| 740 | } |
| 741 | |
| 742 | void hists__filter_by_dso(struct hists *hists) |
| 743 | { |
| 744 | struct rb_node *nd; |
| 745 | |
| 746 | hists->stats.nr_non_filtered_samples = 0; |
| 747 | |
| 748 | hists__reset_filter_stats(hists); |
| 749 | hists__reset_col_len(hists); |
| 750 | |
| 751 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
| 752 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 753 | |
| 754 | if (symbol_conf.exclude_other && !h->parent) |
| 755 | continue; |
| 756 | |
| 757 | if (hists__filter_entry_by_dso(hists, h)) |
| 758 | continue; |
| 759 | |
| 760 | hists__remove_entry_filter(hists, h, HIST_FILTER__DSO); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | static bool hists__filter_entry_by_thread(struct hists *hists, |
| 765 | struct hist_entry *he) |
| 766 | { |
| 767 | if (hists->thread_filter != NULL && |
| 768 | he->thread != hists->thread_filter) { |
| 769 | he->filtered |= (1 << HIST_FILTER__THREAD); |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | return false; |
| 774 | } |
| 775 | |
| 776 | void hists__filter_by_thread(struct hists *hists) |
| 777 | { |
| 778 | struct rb_node *nd; |
| 779 | |
| 780 | hists->stats.nr_non_filtered_samples = 0; |
| 781 | |
| 782 | hists__reset_filter_stats(hists); |
| 783 | hists__reset_col_len(hists); |
| 784 | |
| 785 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
| 786 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 787 | |
| 788 | if (hists__filter_entry_by_thread(hists, h)) |
| 789 | continue; |
| 790 | |
| 791 | hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
| 796 | struct hist_entry *he) |
| 797 | { |
| 798 | if (hists->symbol_filter_str != NULL && |
| 799 | (!he->ms.sym || strstr(he->ms.sym->name, |
| 800 | hists->symbol_filter_str) == NULL)) { |
| 801 | he->filtered |= (1 << HIST_FILTER__SYMBOL); |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | return false; |
| 806 | } |
| 807 | |
| 808 | void hists__filter_by_symbol(struct hists *hists) |
| 809 | { |
| 810 | struct rb_node *nd; |
| 811 | |
| 812 | hists->stats.nr_non_filtered_samples = 0; |
| 813 | |
| 814 | hists__reset_filter_stats(hists); |
| 815 | hists__reset_col_len(hists); |
| 816 | |
| 817 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
| 818 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 819 | |
| 820 | if (hists__filter_entry_by_symbol(hists, h)) |
| 821 | continue; |
| 822 | |
| 823 | hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | void events_stats__inc(struct events_stats *stats, u32 type) |
| 828 | { |
| 829 | ++stats->nr_events[0]; |
| 830 | ++stats->nr_events[type]; |
| 831 | } |
| 832 | |
| 833 | void hists__inc_nr_events(struct hists *hists, u32 type) |
| 834 | { |
| 835 | events_stats__inc(&hists->stats, type); |
| 836 | } |
| 837 | |
| 838 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, |
| 839 | struct hist_entry *pair) |
| 840 | { |
| 841 | struct rb_root *root; |
| 842 | struct rb_node **p; |
| 843 | struct rb_node *parent = NULL; |
| 844 | struct hist_entry *he; |
| 845 | int64_t cmp; |
| 846 | |
| 847 | if (sort__need_collapse) |
| 848 | root = &hists->entries_collapsed; |
| 849 | else |
| 850 | root = hists->entries_in; |
| 851 | |
| 852 | p = &root->rb_node; |
| 853 | |
| 854 | while (*p != NULL) { |
| 855 | parent = *p; |
| 856 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
| 857 | |
| 858 | cmp = hist_entry__collapse(he, pair); |
| 859 | |
| 860 | if (!cmp) |
| 861 | goto out; |
| 862 | |
| 863 | if (cmp < 0) |
| 864 | p = &(*p)->rb_left; |
| 865 | else |
| 866 | p = &(*p)->rb_right; |
| 867 | } |
| 868 | |
| 869 | he = hist_entry__new(pair); |
| 870 | if (he) { |
| 871 | memset(&he->stat, 0, sizeof(he->stat)); |
| 872 | he->hists = hists; |
| 873 | rb_link_node(&he->rb_node_in, parent, p); |
| 874 | rb_insert_color(&he->rb_node_in, root); |
| 875 | hists__inc_stats(hists, he); |
| 876 | he->dummy = true; |
| 877 | } |
| 878 | out: |
| 879 | return he; |
| 880 | } |
| 881 | |
| 882 | static struct hist_entry *hists__find_entry(struct hists *hists, |
| 883 | struct hist_entry *he) |
| 884 | { |
| 885 | struct rb_node *n; |
| 886 | |
| 887 | if (sort__need_collapse) |
| 888 | n = hists->entries_collapsed.rb_node; |
| 889 | else |
| 890 | n = hists->entries_in->rb_node; |
| 891 | |
| 892 | while (n) { |
| 893 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); |
| 894 | int64_t cmp = hist_entry__collapse(iter, he); |
| 895 | |
| 896 | if (cmp < 0) |
| 897 | n = n->rb_left; |
| 898 | else if (cmp > 0) |
| 899 | n = n->rb_right; |
| 900 | else |
| 901 | return iter; |
| 902 | } |
| 903 | |
| 904 | return NULL; |
| 905 | } |
| 906 | |
| 907 | /* |
| 908 | * Look for pairs to link to the leader buckets (hist_entries): |
| 909 | */ |
| 910 | void hists__match(struct hists *leader, struct hists *other) |
| 911 | { |
| 912 | struct rb_root *root; |
| 913 | struct rb_node *nd; |
| 914 | struct hist_entry *pos, *pair; |
| 915 | |
| 916 | if (sort__need_collapse) |
| 917 | root = &leader->entries_collapsed; |
| 918 | else |
| 919 | root = leader->entries_in; |
| 920 | |
| 921 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { |
| 922 | pos = rb_entry(nd, struct hist_entry, rb_node_in); |
| 923 | pair = hists__find_entry(other, pos); |
| 924 | |
| 925 | if (pair) |
| 926 | hist_entry__add_pair(pair, pos); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * Look for entries in the other hists that are not present in the leader, if |
| 932 | * we find them, just add a dummy entry on the leader hists, with period=0, |
| 933 | * nr_events=0, to serve as the list header. |
| 934 | */ |
| 935 | int hists__link(struct hists *leader, struct hists *other) |
| 936 | { |
| 937 | struct rb_root *root; |
| 938 | struct rb_node *nd; |
| 939 | struct hist_entry *pos, *pair; |
| 940 | |
| 941 | if (sort__need_collapse) |
| 942 | root = &other->entries_collapsed; |
| 943 | else |
| 944 | root = other->entries_in; |
| 945 | |
| 946 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { |
| 947 | pos = rb_entry(nd, struct hist_entry, rb_node_in); |
| 948 | |
| 949 | if (!hist_entry__has_pairs(pos)) { |
| 950 | pair = hists__add_dummy_entry(leader, pos); |
| 951 | if (pair == NULL) |
| 952 | return -1; |
| 953 | hist_entry__add_pair(pos, pair); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | u64 hists__total_period(struct hists *hists) |
| 961 | { |
| 962 | return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period : |
| 963 | hists->stats.total_period; |
| 964 | } |
| 965 | |
| 966 | int parse_filter_percentage(const struct option *opt __maybe_unused, |
| 967 | const char *arg, int unset __maybe_unused) |
| 968 | { |
| 969 | if (!strcmp(arg, "relative")) |
| 970 | symbol_conf.filter_relative = true; |
| 971 | else if (!strcmp(arg, "absolute")) |
| 972 | symbol_conf.filter_relative = false; |
| 973 | else |
| 974 | return -1; |
| 975 | |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | int perf_hist_config(const char *var, const char *value) |
| 980 | { |
| 981 | if (!strcmp(var, "hist.percentage")) |
| 982 | return parse_filter_percentage(NULL, value, 0); |
| 983 | |
| 984 | return 0; |
| 985 | } |