1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * builtin-diff.c 4 * 5 * Builtin diff command: Analyze two perf.data input files, look up and read 6 * DSOs and symbol information, sort them and produce a diff. 7 */ 8 #include "builtin.h" 9 #include "perf.h" 10 11 #include "util/debug.h" 12 #include "util/event.h" 13 #include "util/hist.h" 14 #include "util/evsel.h" 15 #include "util/evlist.h" 16 #include "util/session.h" 17 #include "util/tool.h" 18 #include "util/sort.h" 19 #include "util/srcline.h" 20 #include "util/symbol.h" 21 #include "util/data.h" 22 #include "util/config.h" 23 #include "util/time-utils.h" 24 #include "util/annotate.h" 25 #include "util/map.h" 26 #include "util/spark.h" 27 #include "util/block-info.h" 28 #include "util/stream.h" 29 #include "util/util.h" 30 #include <linux/err.h> 31 #include <linux/zalloc.h> 32 #include <subcmd/pager.h> 33 #include <subcmd/parse-options.h> 34 35 #include <errno.h> 36 #include <inttypes.h> 37 #include <stdlib.h> 38 #include <math.h> 39 40 struct perf_diff { 41 struct perf_tool tool; 42 const char *time_str; 43 struct perf_time_interval *ptime_range; 44 int range_size; 45 int range_num; 46 bool has_br_stack; 47 bool stream; 48 }; 49 50 /* Diff command specific HPP columns. */ 51 enum { 52 PERF_HPP_DIFF__BASELINE, 53 PERF_HPP_DIFF__PERIOD, 54 PERF_HPP_DIFF__PERIOD_BASELINE, 55 PERF_HPP_DIFF__DELTA, 56 PERF_HPP_DIFF__RATIO, 57 PERF_HPP_DIFF__WEIGHTED_DIFF, 58 PERF_HPP_DIFF__FORMULA, 59 PERF_HPP_DIFF__DELTA_ABS, 60 PERF_HPP_DIFF__CYCLES, 61 PERF_HPP_DIFF__CYCLES_HIST, 62 63 PERF_HPP_DIFF__MAX_INDEX 64 }; 65 66 struct diff_hpp_fmt { 67 struct perf_hpp_fmt fmt; 68 int idx; 69 char *header; 70 int header_width; 71 }; 72 73 struct data__file { 74 struct perf_session *session; 75 struct perf_data data; 76 int idx; 77 struct hists *hists; 78 struct evlist_streams *evlist_streams; 79 struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX]; 80 }; 81 82 static struct data__file *data__files; 83 static int data__files_cnt; 84 85 #define data__for_each_file_start(i, d, s) \ 86 for (i = s, d = &data__files[s]; \ 87 i < data__files_cnt; \ 88 i++, d = &data__files[i]) 89 90 #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0) 91 #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1) 92 93 static bool force; 94 static bool show_period; 95 static bool show_formula; 96 static bool show_baseline_only; 97 static bool cycles_hist; 98 static unsigned int sort_compute = 1; 99 100 static s64 compute_wdiff_w1; 101 static s64 compute_wdiff_w2; 102 103 static const char *cpu_list; 104 static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 105 106 enum { 107 COMPUTE_DELTA, 108 COMPUTE_RATIO, 109 COMPUTE_WEIGHTED_DIFF, 110 COMPUTE_DELTA_ABS, 111 COMPUTE_CYCLES, 112 COMPUTE_MAX, 113 COMPUTE_STREAM, /* After COMPUTE_MAX to avoid use current compute arrays */ 114 }; 115 116 static const char *compute_names[COMPUTE_MAX] = { 117 [COMPUTE_DELTA] = "delta", 118 [COMPUTE_DELTA_ABS] = "delta-abs", 119 [COMPUTE_RATIO] = "ratio", 120 [COMPUTE_WEIGHTED_DIFF] = "wdiff", 121 [COMPUTE_CYCLES] = "cycles", 122 }; 123 124 static int compute = COMPUTE_DELTA_ABS; 125 126 static int compute_2_hpp[COMPUTE_MAX] = { 127 [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA, 128 [COMPUTE_DELTA_ABS] = PERF_HPP_DIFF__DELTA_ABS, 129 [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO, 130 [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF, 131 [COMPUTE_CYCLES] = PERF_HPP_DIFF__CYCLES, 132 }; 133 134 #define MAX_COL_WIDTH 70 135 136 static struct header_column { 137 const char *name; 138 int width; 139 } columns[PERF_HPP_DIFF__MAX_INDEX] = { 140 [PERF_HPP_DIFF__BASELINE] = { 141 .name = "Baseline", 142 }, 143 [PERF_HPP_DIFF__PERIOD] = { 144 .name = "Period", 145 .width = 14, 146 }, 147 [PERF_HPP_DIFF__PERIOD_BASELINE] = { 148 .name = "Base period", 149 .width = 14, 150 }, 151 [PERF_HPP_DIFF__DELTA] = { 152 .name = "Delta", 153 .width = 7, 154 }, 155 [PERF_HPP_DIFF__DELTA_ABS] = { 156 .name = "Delta Abs", 157 .width = 7, 158 }, 159 [PERF_HPP_DIFF__RATIO] = { 160 .name = "Ratio", 161 .width = 14, 162 }, 163 [PERF_HPP_DIFF__WEIGHTED_DIFF] = { 164 .name = "Weighted diff", 165 .width = 14, 166 }, 167 [PERF_HPP_DIFF__FORMULA] = { 168 .name = "Formula", 169 .width = MAX_COL_WIDTH, 170 }, 171 [PERF_HPP_DIFF__CYCLES] = { 172 .name = "[Program Block Range] Cycles Diff", 173 .width = 70, 174 }, 175 [PERF_HPP_DIFF__CYCLES_HIST] = { 176 .name = "stddev/Hist", 177 .width = NUM_SPARKS + 9, 178 } 179 }; 180 181 static int setup_compute_opt_wdiff(const char *opt) 182 { 183 const char *w1_str = opt, *w2_str; 184 185 int ret = -EINVAL; 186 187 if (!opt) 188 goto out; 189 190 w2_str = strchr(opt, ','); 191 if (!w2_str) 192 goto out; 193 194 if (!*++w2_str) 195 goto out; 196 197 compute_wdiff_w1 = strtol(w1_str, NULL, 10); 198 compute_wdiff_w2 = strtol(w2_str, NULL, 10); 199 200 if (!compute_wdiff_w1 || !compute_wdiff_w2) 201 goto out; 202 203 pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n", 204 compute_wdiff_w1, compute_wdiff_w2); 205 206 ret = 0; 207 208 out: 209 if (ret) 210 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n"); 211 212 return ret; 213 } 214 215 static int setup_compute_opt(const char *opt) 216 { 217 if (compute == COMPUTE_WEIGHTED_DIFF) 218 return setup_compute_opt_wdiff(opt); 219 220 if (opt) { 221 pr_err("Failed: extra option specified '%s'", opt); 222 return -EINVAL; 223 } 224 225 return 0; 226 } 227 228 static int setup_compute(const struct option *opt, const char *str, 229 int unset __maybe_unused) 230 { 231 int *cp = (int *) opt->value; 232 char *cstr = (char *) str; 233 char buf[50]; 234 unsigned i; 235 const char *option; 236 237 if (!str) { 238 *cp = COMPUTE_DELTA; 239 return 0; 240 } 241 242 option = strchr(str, ':'); 243 if (option) { 244 unsigned len = option++ - str; 245 246 /* 247 * The str data are not writeable, so we need 248 * to use another buffer. 249 */ 250 251 /* No option value is longer. */ 252 if (len >= sizeof(buf)) 253 return -EINVAL; 254 255 strncpy(buf, str, len); 256 buf[len] = 0x0; 257 cstr = buf; 258 } 259 260 for (i = 0; i < COMPUTE_MAX; i++) 261 if (!strcmp(cstr, compute_names[i])) { 262 *cp = i; 263 return setup_compute_opt(option); 264 } 265 266 pr_err("Failed: '%s' is not computation method " 267 "(use 'delta','ratio' or 'wdiff')\n", str); 268 return -EINVAL; 269 } 270 271 static double period_percent(struct hist_entry *he, u64 period) 272 { 273 u64 total = hists__total_period(he->hists); 274 275 return (period * 100.0) / total; 276 } 277 278 static double compute_delta(struct hist_entry *he, struct hist_entry *pair) 279 { 280 double old_percent = period_percent(he, he->stat.period); 281 double new_percent = period_percent(pair, pair->stat.period); 282 283 pair->diff.period_ratio_delta = new_percent - old_percent; 284 pair->diff.computed = true; 285 return pair->diff.period_ratio_delta; 286 } 287 288 static double compute_ratio(struct hist_entry *he, struct hist_entry *pair) 289 { 290 double old_period = he->stat.period ?: 1; 291 double new_period = pair->stat.period; 292 293 pair->diff.computed = true; 294 pair->diff.period_ratio = new_period / old_period; 295 return pair->diff.period_ratio; 296 } 297 298 static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair) 299 { 300 u64 old_period = he->stat.period; 301 u64 new_period = pair->stat.period; 302 303 pair->diff.computed = true; 304 pair->diff.wdiff = new_period * compute_wdiff_w2 - 305 old_period * compute_wdiff_w1; 306 307 return pair->diff.wdiff; 308 } 309 310 static int formula_delta(struct hist_entry *he, struct hist_entry *pair, 311 char *buf, size_t size) 312 { 313 u64 he_total = he->hists->stats.total_period; 314 u64 pair_total = pair->hists->stats.total_period; 315 316 if (symbol_conf.filter_relative) { 317 he_total = he->hists->stats.total_non_filtered_period; 318 pair_total = pair->hists->stats.total_non_filtered_period; 319 } 320 return scnprintf(buf, size, 321 "(%" PRIu64 " * 100 / %" PRIu64 ") - " 322 "(%" PRIu64 " * 100 / %" PRIu64 ")", 323 pair->stat.period, pair_total, 324 he->stat.period, he_total); 325 } 326 327 static int formula_ratio(struct hist_entry *he, struct hist_entry *pair, 328 char *buf, size_t size) 329 { 330 double old_period = he->stat.period; 331 double new_period = pair->stat.period; 332 333 return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period); 334 } 335 336 static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair, 337 char *buf, size_t size) 338 { 339 u64 old_period = he->stat.period; 340 u64 new_period = pair->stat.period; 341 342 return scnprintf(buf, size, 343 "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")", 344 new_period, compute_wdiff_w2, old_period, compute_wdiff_w1); 345 } 346 347 static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair, 348 char *buf, size_t size) 349 { 350 switch (compute) { 351 case COMPUTE_DELTA: 352 case COMPUTE_DELTA_ABS: 353 return formula_delta(he, pair, buf, size); 354 case COMPUTE_RATIO: 355 return formula_ratio(he, pair, buf, size); 356 case COMPUTE_WEIGHTED_DIFF: 357 return formula_wdiff(he, pair, buf, size); 358 default: 359 BUG_ON(1); 360 } 361 362 return -1; 363 } 364 365 static void *block_hist_zalloc(size_t size) 366 { 367 struct block_hist *bh; 368 369 bh = zalloc(size + sizeof(*bh)); 370 if (!bh) 371 return NULL; 372 373 return &bh->he; 374 } 375 376 static void block_hist_free(void *he) 377 { 378 struct block_hist *bh; 379 380 bh = container_of(he, struct block_hist, he); 381 hists__delete_entries(&bh->block_hists); 382 free(bh); 383 } 384 385 static struct hist_entry_ops block_hist_ops = { 386 .new = block_hist_zalloc, 387 .free = block_hist_free, 388 }; 389 390 static int diff__process_sample_event(const struct perf_tool *tool, 391 union perf_event *event, 392 struct perf_sample *sample, 393 struct evsel *evsel, 394 struct machine *machine) 395 { 396 struct perf_diff *pdiff = container_of(tool, struct perf_diff, tool); 397 struct addr_location al; 398 struct hists *hists = evsel__hists(evsel); 399 struct hist_entry_iter iter = { 400 .evsel = evsel, 401 .sample = sample, 402 .ops = &hist_iter_normal, 403 }; 404 int ret = -1; 405 406 if (perf_time__ranges_skip_sample(pdiff->ptime_range, pdiff->range_num, 407 sample->time)) { 408 return 0; 409 } 410 411 addr_location__init(&al); 412 if (machine__resolve(machine, &al, sample) < 0) { 413 pr_warning("problem processing %d event, skipping it.\n", 414 event->header.type); 415 ret = -1; 416 goto out; 417 } 418 419 if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) { 420 ret = 0; 421 goto out; 422 } 423 424 switch (compute) { 425 case COMPUTE_CYCLES: 426 if (!hists__add_entry_ops(hists, &block_hist_ops, &al, NULL, 427 NULL, NULL, NULL, sample, true)) { 428 pr_warning("problem incrementing symbol period, " 429 "skipping event\n"); 430 goto out; 431 } 432 433 hist__account_cycles(sample->branch_stack, &al, sample, 434 false, NULL, evsel); 435 break; 436 437 case COMPUTE_STREAM: 438 if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH, 439 NULL)) { 440 pr_debug("problem adding hist entry, skipping event\n"); 441 goto out; 442 } 443 break; 444 445 default: 446 if (!hists__add_entry(hists, &al, NULL, NULL, NULL, NULL, sample, 447 true)) { 448 pr_warning("problem incrementing symbol period, " 449 "skipping event\n"); 450 goto out; 451 } 452 } 453 454 /* 455 * The total_period is updated here before going to the output 456 * tree since normally only the baseline hists will call 457 * hists__output_resort() and precompute needs the total 458 * period in order to sort entries by percentage delta. 459 */ 460 hists->stats.total_period += sample->period; 461 if (!al.filtered) 462 hists->stats.total_non_filtered_period += sample->period; 463 ret = 0; 464 out: 465 addr_location__exit(&al); 466 return ret; 467 } 468 469 static struct perf_diff pdiff; 470 471 static struct evsel *evsel_match(struct evsel *evsel, struct evlist *evlist) 472 { 473 struct evsel *e; 474 475 evlist__for_each_entry(evlist, e) { 476 if ((evsel->core.attr.type == e->core.attr.type) && 477 (evsel->core.attr.config == e->core.attr.config)) 478 return e; 479 } 480 481 return NULL; 482 } 483 484 static void evlist__collapse_resort(struct evlist *evlist) 485 { 486 struct evsel *evsel; 487 488 evlist__for_each_entry(evlist, evsel) { 489 struct hists *hists = evsel__hists(evsel); 490 491 hists__collapse_resort(hists, NULL); 492 } 493 } 494 495 static struct data__file *fmt_to_data_file(struct perf_hpp_fmt *fmt) 496 { 497 struct diff_hpp_fmt *dfmt = container_of(fmt, struct diff_hpp_fmt, fmt); 498 void *ptr = dfmt - dfmt->idx; 499 struct data__file *d = container_of(ptr, struct data__file, fmt); 500 501 return d; 502 } 503 504 static struct hist_entry* 505 get_pair_data(struct hist_entry *he, struct data__file *d) 506 { 507 if (hist_entry__has_pairs(he)) { 508 struct hist_entry *pair; 509 510 list_for_each_entry(pair, &he->pairs.head, pairs.node) 511 if (pair->hists == d->hists) 512 return pair; 513 } 514 515 return NULL; 516 } 517 518 static struct hist_entry* 519 get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt) 520 { 521 struct data__file *d = fmt_to_data_file(&dfmt->fmt); 522 523 return get_pair_data(he, d); 524 } 525 526 static void hists__baseline_only(struct hists *hists) 527 { 528 struct rb_root_cached *root; 529 struct rb_node *next; 530 531 if (hists__has(hists, need_collapse)) 532 root = &hists->entries_collapsed; 533 else 534 root = hists->entries_in; 535 536 next = rb_first_cached(root); 537 while (next != NULL) { 538 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in); 539 540 next = rb_next(&he->rb_node_in); 541 if (!hist_entry__next_pair(he)) { 542 rb_erase_cached(&he->rb_node_in, root); 543 hist_entry__delete(he); 544 } 545 } 546 } 547 548 static int64_t block_cycles_diff_cmp(struct hist_entry *left, 549 struct hist_entry *right) 550 { 551 bool pairs_left = hist_entry__has_pairs(left); 552 bool pairs_right = hist_entry__has_pairs(right); 553 s64 l, r; 554 555 if (!pairs_left && !pairs_right) 556 return 0; 557 558 l = llabs(left->diff.cycles); 559 r = llabs(right->diff.cycles); 560 return r - l; 561 } 562 563 static int64_t block_sort(struct perf_hpp_fmt *fmt __maybe_unused, 564 struct hist_entry *left, struct hist_entry *right) 565 { 566 return block_cycles_diff_cmp(right, left); 567 } 568 569 static void init_block_hist(struct block_hist *bh) 570 { 571 __hists__init(&bh->block_hists, &bh->block_list); 572 perf_hpp_list__init(&bh->block_list); 573 574 INIT_LIST_HEAD(&bh->block_fmt.list); 575 INIT_LIST_HEAD(&bh->block_fmt.sort_list); 576 bh->block_fmt.cmp = block_info__cmp; 577 bh->block_fmt.sort = block_sort; 578 perf_hpp_list__register_sort_field(&bh->block_list, 579 &bh->block_fmt); 580 bh->valid = true; 581 } 582 583 static struct hist_entry *get_block_pair(struct hist_entry *he, 584 struct hists *hists_pair) 585 { 586 struct rb_root_cached *root = hists_pair->entries_in; 587 struct rb_node *next = rb_first_cached(root); 588 int64_t cmp; 589 590 while (next != NULL) { 591 struct hist_entry *he_pair = rb_entry(next, struct hist_entry, 592 rb_node_in); 593 594 next = rb_next(&he_pair->rb_node_in); 595 596 cmp = __block_info__cmp(he_pair, he); 597 if (!cmp) 598 return he_pair; 599 } 600 601 return NULL; 602 } 603 604 static void init_spark_values(unsigned long *svals, int num) 605 { 606 for (int i = 0; i < num; i++) 607 svals[i] = 0; 608 } 609 610 static void update_spark_value(unsigned long *svals, int num, 611 struct stats *stats, u64 val) 612 { 613 int n = stats->n; 614 615 if (n < num) 616 svals[n] = val; 617 } 618 619 static void compute_cycles_diff(struct hist_entry *he, 620 struct hist_entry *pair) 621 { 622 pair->diff.computed = true; 623 if (pair->block_info->num && he->block_info->num) { 624 pair->diff.cycles = 625 pair->block_info->cycles_aggr / pair->block_info->num_aggr - 626 he->block_info->cycles_aggr / he->block_info->num_aggr; 627 628 if (!cycles_hist) 629 return; 630 631 init_stats(&pair->diff.stats); 632 init_spark_values(pair->diff.svals, NUM_SPARKS); 633 634 for (int i = 0; i < pair->block_info->num; i++) { 635 u64 val; 636 637 if (i >= he->block_info->num || i >= NUM_SPARKS) 638 break; 639 640 val = llabs(pair->block_info->cycles_spark[i] - 641 he->block_info->cycles_spark[i]); 642 643 update_spark_value(pair->diff.svals, NUM_SPARKS, 644 &pair->diff.stats, val); 645 update_stats(&pair->diff.stats, val); 646 } 647 } 648 } 649 650 static void block_hists_match(struct hists *hists_base, 651 struct hists *hists_pair) 652 { 653 struct rb_root_cached *root = hists_base->entries_in; 654 struct rb_node *next = rb_first_cached(root); 655 656 while (next != NULL) { 657 struct hist_entry *he = rb_entry(next, struct hist_entry, 658 rb_node_in); 659 struct hist_entry *pair = get_block_pair(he, hists_pair); 660 661 next = rb_next(&he->rb_node_in); 662 663 if (pair) { 664 hist_entry__add_pair(pair, he); 665 compute_cycles_diff(he, pair); 666 } 667 } 668 } 669 670 static void hists__precompute(struct hists *hists) 671 { 672 struct rb_root_cached *root; 673 struct rb_node *next; 674 675 if (hists__has(hists, need_collapse)) 676 root = &hists->entries_collapsed; 677 else 678 root = hists->entries_in; 679 680 next = rb_first_cached(root); 681 while (next != NULL) { 682 struct block_hist *bh, *pair_bh; 683 struct hist_entry *he, *pair; 684 struct data__file *d; 685 int i; 686 687 he = rb_entry(next, struct hist_entry, rb_node_in); 688 next = rb_next(&he->rb_node_in); 689 690 if (compute == COMPUTE_CYCLES) { 691 bh = container_of(he, struct block_hist, he); 692 init_block_hist(bh); 693 block_info__process_sym(he, bh, NULL, 0, 0); 694 } 695 696 data__for_each_file_new(i, d) { 697 pair = get_pair_data(he, d); 698 if (!pair) 699 continue; 700 701 switch (compute) { 702 case COMPUTE_DELTA: 703 case COMPUTE_DELTA_ABS: 704 compute_delta(he, pair); 705 break; 706 case COMPUTE_RATIO: 707 compute_ratio(he, pair); 708 break; 709 case COMPUTE_WEIGHTED_DIFF: 710 compute_wdiff(he, pair); 711 break; 712 case COMPUTE_CYCLES: 713 pair_bh = container_of(pair, struct block_hist, 714 he); 715 init_block_hist(pair_bh); 716 block_info__process_sym(pair, pair_bh, NULL, 0, 0); 717 718 bh = container_of(he, struct block_hist, he); 719 720 if (bh->valid && pair_bh->valid) { 721 block_hists_match(&bh->block_hists, 722 &pair_bh->block_hists); 723 hists__output_resort(&pair_bh->block_hists, 724 NULL); 725 } 726 break; 727 default: 728 BUG_ON(1); 729 } 730 } 731 } 732 } 733 734 static int64_t cmp_doubles(double l, double r) 735 { 736 if (l > r) 737 return -1; 738 else if (l < r) 739 return 1; 740 else 741 return 0; 742 } 743 744 static int64_t 745 __hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right, 746 int c) 747 { 748 switch (c) { 749 case COMPUTE_DELTA: 750 { 751 double l = left->diff.period_ratio_delta; 752 double r = right->diff.period_ratio_delta; 753 754 return cmp_doubles(l, r); 755 } 756 case COMPUTE_DELTA_ABS: 757 { 758 double l = fabs(left->diff.period_ratio_delta); 759 double r = fabs(right->diff.period_ratio_delta); 760 761 return cmp_doubles(l, r); 762 } 763 case COMPUTE_RATIO: 764 { 765 double l = left->diff.period_ratio; 766 double r = right->diff.period_ratio; 767 768 return cmp_doubles(l, r); 769 } 770 case COMPUTE_WEIGHTED_DIFF: 771 { 772 s64 l = left->diff.wdiff; 773 s64 r = right->diff.wdiff; 774 775 return r - l; 776 } 777 default: 778 BUG_ON(1); 779 } 780 781 return 0; 782 } 783 784 static int64_t 785 hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right, 786 int c, int sort_idx) 787 { 788 bool pairs_left = hist_entry__has_pairs(left); 789 bool pairs_right = hist_entry__has_pairs(right); 790 struct hist_entry *p_right, *p_left; 791 792 if (!pairs_left && !pairs_right) 793 return 0; 794 795 if (!pairs_left || !pairs_right) 796 return pairs_left ? -1 : 1; 797 798 p_left = get_pair_data(left, &data__files[sort_idx]); 799 p_right = get_pair_data(right, &data__files[sort_idx]); 800 801 if (!p_left && !p_right) 802 return 0; 803 804 if (!p_left || !p_right) 805 return p_left ? -1 : 1; 806 807 /* 808 * We have 2 entries of same kind, let's 809 * make the data comparison. 810 */ 811 return __hist_entry__cmp_compute(p_left, p_right, c); 812 } 813 814 static int64_t 815 hist_entry__cmp_compute_idx(struct hist_entry *left, struct hist_entry *right, 816 int c, int sort_idx) 817 { 818 struct hist_entry *p_right, *p_left; 819 820 p_left = get_pair_data(left, &data__files[sort_idx]); 821 p_right = get_pair_data(right, &data__files[sort_idx]); 822 823 if (!p_left && !p_right) 824 return 0; 825 826 if (!p_left || !p_right) 827 return p_left ? -1 : 1; 828 829 if (c != COMPUTE_DELTA && c != COMPUTE_DELTA_ABS) { 830 /* 831 * The delta can be computed without the baseline, but 832 * others are not. Put those entries which have no 833 * values below. 834 */ 835 if (left->dummy && right->dummy) 836 return 0; 837 838 if (left->dummy || right->dummy) 839 return left->dummy ? 1 : -1; 840 } 841 842 return __hist_entry__cmp_compute(p_left, p_right, c); 843 } 844 845 static int64_t 846 hist_entry__cmp_nop(struct perf_hpp_fmt *fmt __maybe_unused, 847 struct hist_entry *left __maybe_unused, 848 struct hist_entry *right __maybe_unused) 849 { 850 return 0; 851 } 852 853 static int64_t 854 hist_entry__cmp_baseline(struct perf_hpp_fmt *fmt __maybe_unused, 855 struct hist_entry *left, struct hist_entry *right) 856 { 857 if (left->stat.period == right->stat.period) 858 return 0; 859 return left->stat.period > right->stat.period ? 1 : -1; 860 } 861 862 static int64_t 863 hist_entry__cmp_delta(struct perf_hpp_fmt *fmt, 864 struct hist_entry *left, struct hist_entry *right) 865 { 866 struct data__file *d = fmt_to_data_file(fmt); 867 868 return hist_entry__cmp_compute(right, left, COMPUTE_DELTA, d->idx); 869 } 870 871 static int64_t 872 hist_entry__cmp_delta_abs(struct perf_hpp_fmt *fmt, 873 struct hist_entry *left, struct hist_entry *right) 874 { 875 struct data__file *d = fmt_to_data_file(fmt); 876 877 return hist_entry__cmp_compute(right, left, COMPUTE_DELTA_ABS, d->idx); 878 } 879 880 static int64_t 881 hist_entry__cmp_ratio(struct perf_hpp_fmt *fmt, 882 struct hist_entry *left, struct hist_entry *right) 883 { 884 struct data__file *d = fmt_to_data_file(fmt); 885 886 return hist_entry__cmp_compute(right, left, COMPUTE_RATIO, d->idx); 887 } 888 889 static int64_t 890 hist_entry__cmp_wdiff(struct perf_hpp_fmt *fmt, 891 struct hist_entry *left, struct hist_entry *right) 892 { 893 struct data__file *d = fmt_to_data_file(fmt); 894 895 return hist_entry__cmp_compute(right, left, COMPUTE_WEIGHTED_DIFF, d->idx); 896 } 897 898 static int64_t 899 hist_entry__cmp_delta_idx(struct perf_hpp_fmt *fmt __maybe_unused, 900 struct hist_entry *left, struct hist_entry *right) 901 { 902 return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA, 903 sort_compute); 904 } 905 906 static int64_t 907 hist_entry__cmp_delta_abs_idx(struct perf_hpp_fmt *fmt __maybe_unused, 908 struct hist_entry *left, struct hist_entry *right) 909 { 910 return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA_ABS, 911 sort_compute); 912 } 913 914 static int64_t 915 hist_entry__cmp_ratio_idx(struct perf_hpp_fmt *fmt __maybe_unused, 916 struct hist_entry *left, struct hist_entry *right) 917 { 918 return hist_entry__cmp_compute_idx(right, left, COMPUTE_RATIO, 919 sort_compute); 920 } 921 922 static int64_t 923 hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt *fmt __maybe_unused, 924 struct hist_entry *left, struct hist_entry *right) 925 { 926 return hist_entry__cmp_compute_idx(right, left, COMPUTE_WEIGHTED_DIFF, 927 sort_compute); 928 } 929 930 static void hists__process(struct hists *hists) 931 { 932 if (show_baseline_only) 933 hists__baseline_only(hists); 934 935 hists__precompute(hists); 936 hists__output_resort(hists, NULL); 937 938 if (compute == COMPUTE_CYCLES) 939 symbol_conf.report_block = true; 940 941 hists__fprintf(hists, !quiet, 0, 0, 0, stdout, 942 !symbol_conf.use_callchain); 943 } 944 945 static void data__fprintf(void) 946 { 947 struct data__file *d; 948 int i; 949 950 fprintf(stdout, "# Data files:\n"); 951 952 data__for_each_file(i, d) 953 fprintf(stdout, "# [%d] %s %s\n", 954 d->idx, d->data.path, 955 !d->idx ? "(Baseline)" : ""); 956 957 fprintf(stdout, "#\n"); 958 } 959 960 static void data_process(void) 961 { 962 struct evlist *evlist_base = data__files[0].session->evlist; 963 struct evsel *evsel_base; 964 bool first = true; 965 966 evlist__for_each_entry(evlist_base, evsel_base) { 967 struct hists *hists_base = evsel__hists(evsel_base); 968 struct data__file *d; 969 int i; 970 971 data__for_each_file_new(i, d) { 972 struct evlist *evlist = d->session->evlist; 973 struct evsel *evsel; 974 struct hists *hists; 975 976 evsel = evsel_match(evsel_base, evlist); 977 if (!evsel) 978 continue; 979 980 hists = evsel__hists(evsel); 981 d->hists = hists; 982 983 hists__match(hists_base, hists); 984 985 if (!show_baseline_only) 986 hists__link(hists_base, hists); 987 } 988 989 if (!quiet) { 990 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n", 991 evsel__name(evsel_base)); 992 } 993 994 first = false; 995 996 if (verbose > 0 || ((data__files_cnt > 2) && !quiet)) 997 data__fprintf(); 998 999 /* Don't sort callchain for perf diff */ 1000 evsel__reset_sample_bit(evsel_base, CALLCHAIN); 1001 1002 hists__process(hists_base); 1003 } 1004 } 1005 1006 static int process_base_stream(struct data__file *data_base, 1007 struct data__file *data_pair, 1008 const char *title __maybe_unused) 1009 { 1010 struct evlist *evlist_base = data_base->session->evlist; 1011 struct evlist *evlist_pair = data_pair->session->evlist; 1012 struct evsel *evsel_base, *evsel_pair; 1013 struct evsel_streams *es_base, *es_pair; 1014 1015 evlist__for_each_entry(evlist_base, evsel_base) { 1016 evsel_pair = evsel_match(evsel_base, evlist_pair); 1017 if (!evsel_pair) 1018 continue; 1019 1020 es_base = evsel_streams__entry(data_base->evlist_streams, 1021 evsel_base); 1022 if (!es_base) 1023 return -1; 1024 1025 es_pair = evsel_streams__entry(data_pair->evlist_streams, 1026 evsel_pair); 1027 if (!es_pair) 1028 return -1; 1029 1030 evsel_streams__match(es_base, es_pair); 1031 evsel_streams__report(es_base, es_pair); 1032 } 1033 1034 return 0; 1035 } 1036 1037 static void stream_process(void) 1038 { 1039 /* 1040 * Stream comparison only supports two data files. 1041 * perf.data.old and perf.data. data__files[0] is perf.data.old, 1042 * data__files[1] is perf.data. 1043 */ 1044 process_base_stream(&data__files[0], &data__files[1], 1045 "# Output based on old perf data:\n#\n"); 1046 } 1047 1048 static void data__free(struct data__file *d) 1049 { 1050 int col; 1051 1052 if (d->evlist_streams) 1053 evlist_streams__delete(d->evlist_streams); 1054 1055 for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) { 1056 struct diff_hpp_fmt *fmt = &d->fmt[col]; 1057 1058 zfree(&fmt->header); 1059 } 1060 } 1061 1062 static int abstime_str_dup(char **pstr) 1063 { 1064 char *str = NULL; 1065 1066 if (pdiff.time_str && strchr(pdiff.time_str, ':')) { 1067 str = strdup(pdiff.time_str); 1068 if (!str) 1069 return -ENOMEM; 1070 } 1071 1072 *pstr = str; 1073 return 0; 1074 } 1075 1076 static int parse_absolute_time(struct data__file *d, char **pstr) 1077 { 1078 char *p = *pstr; 1079 int ret; 1080 1081 /* 1082 * Absolute timestamp for one file has the format: a.b,c.d 1083 * For multiple files, the format is: a.b,c.d:a.b,c.d 1084 */ 1085 p = strchr(*pstr, ':'); 1086 if (p) { 1087 if (p == *pstr) { 1088 pr_err("Invalid time string\n"); 1089 return -EINVAL; 1090 } 1091 1092 *p = 0; 1093 p++; 1094 if (*p == 0) { 1095 pr_err("Invalid time string\n"); 1096 return -EINVAL; 1097 } 1098 } 1099 1100 ret = perf_time__parse_for_ranges(*pstr, d->session, 1101 &pdiff.ptime_range, 1102 &pdiff.range_size, 1103 &pdiff.range_num); 1104 if (ret < 0) 1105 return ret; 1106 1107 if (!p || *p == 0) 1108 *pstr = NULL; 1109 else 1110 *pstr = p; 1111 1112 return ret; 1113 } 1114 1115 static int parse_percent_time(struct data__file *d) 1116 { 1117 int ret; 1118 1119 ret = perf_time__parse_for_ranges(pdiff.time_str, d->session, 1120 &pdiff.ptime_range, 1121 &pdiff.range_size, 1122 &pdiff.range_num); 1123 return ret; 1124 } 1125 1126 static int parse_time_str(struct data__file *d, char *abstime_ostr, 1127 char **pabstime_tmp) 1128 { 1129 int ret = 0; 1130 1131 if (abstime_ostr) 1132 ret = parse_absolute_time(d, pabstime_tmp); 1133 else if (pdiff.time_str) 1134 ret = parse_percent_time(d); 1135 1136 return ret; 1137 } 1138 1139 static int check_file_brstack(void) 1140 { 1141 struct data__file *d; 1142 bool has_br_stack; 1143 int i; 1144 1145 data__for_each_file(i, d) { 1146 d->session = perf_session__new(&d->data, &pdiff.tool); 1147 if (IS_ERR(d->session)) { 1148 pr_err("Failed to open %s\n", d->data.path); 1149 return PTR_ERR(d->session); 1150 } 1151 1152 has_br_stack = perf_header__has_feat(&d->session->header, 1153 HEADER_BRANCH_STACK); 1154 perf_session__delete(d->session); 1155 if (!has_br_stack) 1156 return 0; 1157 } 1158 1159 /* Set only all files having branch stacks */ 1160 pdiff.has_br_stack = true; 1161 return 0; 1162 } 1163 1164 static int __cmd_diff(void) 1165 { 1166 struct data__file *d; 1167 int ret, i; 1168 char *abstime_ostr, *abstime_tmp; 1169 1170 ret = abstime_str_dup(&abstime_ostr); 1171 if (ret) 1172 return ret; 1173 1174 abstime_tmp = abstime_ostr; 1175 ret = -EINVAL; 1176 1177 data__for_each_file(i, d) { 1178 d->session = perf_session__new(&d->data, &pdiff.tool); 1179 if (IS_ERR(d->session)) { 1180 ret = PTR_ERR(d->session); 1181 pr_err("Failed to open %s\n", d->data.path); 1182 goto out_delete; 1183 } 1184 1185 if (pdiff.time_str) { 1186 ret = parse_time_str(d, abstime_ostr, &abstime_tmp); 1187 if (ret < 0) 1188 goto out_delete; 1189 } 1190 1191 if (cpu_list) { 1192 ret = perf_session__cpu_bitmap(d->session, cpu_list, 1193 cpu_bitmap); 1194 if (ret < 0) 1195 goto out_delete; 1196 } 1197 1198 ret = perf_session__process_events(d->session); 1199 if (ret) { 1200 pr_err("Failed to process %s\n", d->data.path); 1201 goto out_delete; 1202 } 1203 1204 evlist__collapse_resort(d->session->evlist); 1205 1206 if (pdiff.ptime_range) 1207 zfree(&pdiff.ptime_range); 1208 1209 if (compute == COMPUTE_STREAM) { 1210 d->evlist_streams = evlist__create_streams( 1211 d->session->evlist, 5); 1212 if (!d->evlist_streams) { 1213 ret = -ENOMEM; 1214 goto out_delete; 1215 } 1216 } 1217 } 1218 1219 if (compute == COMPUTE_STREAM) 1220 stream_process(); 1221 else 1222 data_process(); 1223 1224 out_delete: 1225 data__for_each_file(i, d) { 1226 if (!IS_ERR(d->session)) 1227 perf_session__delete(d->session); 1228 data__free(d); 1229 } 1230 1231 free(data__files); 1232 1233 if (pdiff.ptime_range) 1234 zfree(&pdiff.ptime_range); 1235 1236 if (abstime_ostr) 1237 free(abstime_ostr); 1238 1239 return ret; 1240 } 1241 1242 static const char * const diff_usage[] = { 1243 "perf diff [<options>] [old_file] [new_file]", 1244 NULL, 1245 }; 1246 1247 static const struct option options[] = { 1248 OPT_INCR('v', "verbose", &verbose, 1249 "be more verbose (show symbol address, etc)"), 1250 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"), 1251 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only, 1252 "Show only items with match in baseline"), 1253 OPT_CALLBACK('c', "compute", &compute, 1254 "delta,delta-abs,ratio,wdiff:w1,w2 (default delta-abs),cycles", 1255 "Entries differential computation selection", 1256 setup_compute), 1257 OPT_BOOLEAN('p', "period", &show_period, 1258 "Show period values."), 1259 OPT_BOOLEAN('F', "formula", &show_formula, 1260 "Show formula."), 1261 OPT_BOOLEAN(0, "cycles-hist", &cycles_hist, 1262 "Show cycles histogram and standard deviation " 1263 "- WARNING: use only with -c cycles."), 1264 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 1265 "dump raw trace in ASCII"), 1266 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), 1267 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, 1268 "file", "kallsyms pathname"), 1269 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 1270 "load module symbols - WARNING: use only with -k and LIVE kernel"), 1271 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", 1272 "only consider symbols in these dsos"), 1273 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", 1274 "only consider symbols in these comms"), 1275 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", 1276 "only consider these symbols"), 1277 OPT_STRING('s', "sort", &sort_order, "key[,key2...]", 1278 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..." 1279 " Please refer the man page for the complete list."), 1280 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator", 1281 "separator for columns, no spaces will be added between " 1282 "columns '.' is reserved."), 1283 OPT_CALLBACK(0, "symfs", NULL, "directory[,layout]", SYMFS_HELP, 1284 symbol__config_symfs), 1285 OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."), 1286 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute", 1287 "How to display percentage of filtered entries", parse_filter_percentage), 1288 OPT_STRING(0, "time", &pdiff.time_str, "str", 1289 "Time span (time percent or absolute timestamp)"), 1290 OPT_STRING(0, "cpu", &cpu_list, "cpu", "list of cpus to profile"), 1291 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]", 1292 "only consider symbols in these pids"), 1293 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]", 1294 "only consider symbols in these tids"), 1295 OPT_BOOLEAN(0, "stream", &pdiff.stream, 1296 "Enable hot streams comparison."), 1297 OPT_END() 1298 }; 1299 1300 static double baseline_percent(struct hist_entry *he) 1301 { 1302 u64 total = hists__total_period(he->hists); 1303 1304 return 100.0 * he->stat.period / total; 1305 } 1306 1307 static int hpp__color_baseline(struct perf_hpp_fmt *fmt, 1308 struct perf_hpp *hpp, struct hist_entry *he) 1309 { 1310 struct diff_hpp_fmt *dfmt = 1311 container_of(fmt, struct diff_hpp_fmt, fmt); 1312 double percent = baseline_percent(he); 1313 char pfmt[20] = " "; 1314 1315 if (!he->dummy) { 1316 scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1); 1317 return percent_color_snprintf(hpp->buf, hpp->size, 1318 pfmt, percent); 1319 } else 1320 return scnprintf(hpp->buf, hpp->size, "%*s", 1321 dfmt->header_width, pfmt); 1322 } 1323 1324 static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size) 1325 { 1326 double percent = baseline_percent(he); 1327 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%"; 1328 int ret = 0; 1329 1330 if (!he->dummy) 1331 ret = scnprintf(buf, size, fmt, percent); 1332 1333 return ret; 1334 } 1335 1336 static int cycles_printf(struct hist_entry *he, struct hist_entry *pair, 1337 struct perf_hpp *hpp, int width) 1338 { 1339 struct block_hist *bh = container_of(he, struct block_hist, he); 1340 struct block_hist *bh_pair = container_of(pair, struct block_hist, he); 1341 struct hist_entry *block_he; 1342 struct block_info *bi; 1343 char buf[128]; 1344 char *start_line, *end_line; 1345 1346 block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx); 1347 if (!block_he) { 1348 hpp->skip = true; 1349 return 0; 1350 } 1351 1352 /* 1353 * Avoid printing the warning "addr2line_init failed for ..." 1354 */ 1355 symbol_conf.addr2line_disable_warn = true; 1356 1357 bi = block_he->block_info; 1358 1359 start_line = map__srcline(he->ms.map, bi->sym->start + bi->start, 1360 he->ms.sym); 1361 1362 end_line = map__srcline(he->ms.map, bi->sym->start + bi->end, 1363 he->ms.sym); 1364 1365 if (start_line != SRCLINE_UNKNOWN && 1366 end_line != SRCLINE_UNKNOWN) { 1367 scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld", 1368 start_line, end_line, block_he->diff.cycles); 1369 } else { 1370 scnprintf(buf, sizeof(buf), "[%7lx -> %7lx] %4ld", 1371 bi->start, bi->end, block_he->diff.cycles); 1372 } 1373 1374 zfree_srcline(&start_line); 1375 zfree_srcline(&end_line); 1376 1377 return scnprintf(hpp->buf, hpp->size, "%*s", width, buf); 1378 } 1379 1380 static int __hpp__color_compare(struct perf_hpp_fmt *fmt, 1381 struct perf_hpp *hpp, struct hist_entry *he, 1382 int comparison_method) 1383 { 1384 struct diff_hpp_fmt *dfmt = 1385 container_of(fmt, struct diff_hpp_fmt, fmt); 1386 struct hist_entry *pair = get_pair_fmt(he, dfmt); 1387 double diff; 1388 s64 wdiff; 1389 char pfmt[20] = " "; 1390 1391 if (!pair) { 1392 if (comparison_method == COMPUTE_CYCLES) { 1393 struct block_hist *bh; 1394 1395 bh = container_of(he, struct block_hist, he); 1396 if (bh->block_idx) 1397 hpp->skip = true; 1398 } 1399 1400 goto no_print; 1401 } 1402 1403 switch (comparison_method) { 1404 case COMPUTE_DELTA: 1405 if (pair->diff.computed) 1406 diff = pair->diff.period_ratio_delta; 1407 else 1408 diff = compute_delta(he, pair); 1409 1410 scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1); 1411 return percent_color_snprintf(hpp->buf, hpp->size, 1412 pfmt, diff); 1413 case COMPUTE_RATIO: 1414 if (he->dummy) 1415 goto dummy_print; 1416 if (pair->diff.computed) 1417 diff = pair->diff.period_ratio; 1418 else 1419 diff = compute_ratio(he, pair); 1420 1421 scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width); 1422 return value_color_snprintf(hpp->buf, hpp->size, 1423 pfmt, diff); 1424 case COMPUTE_WEIGHTED_DIFF: 1425 if (he->dummy) 1426 goto dummy_print; 1427 if (pair->diff.computed) 1428 wdiff = pair->diff.wdiff; 1429 else 1430 wdiff = compute_wdiff(he, pair); 1431 1432 scnprintf(pfmt, 20, "%%14ld", dfmt->header_width); 1433 return color_snprintf(hpp->buf, hpp->size, 1434 get_percent_color(wdiff), 1435 pfmt, wdiff); 1436 case COMPUTE_CYCLES: 1437 return cycles_printf(he, pair, hpp, dfmt->header_width); 1438 default: 1439 BUG_ON(1); 1440 } 1441 dummy_print: 1442 return scnprintf(hpp->buf, hpp->size, "%*s", 1443 dfmt->header_width, "N/A"); 1444 no_print: 1445 return scnprintf(hpp->buf, hpp->size, "%*s", 1446 dfmt->header_width, pfmt); 1447 } 1448 1449 static int hpp__color_delta(struct perf_hpp_fmt *fmt, 1450 struct perf_hpp *hpp, struct hist_entry *he) 1451 { 1452 return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA); 1453 } 1454 1455 static int hpp__color_ratio(struct perf_hpp_fmt *fmt, 1456 struct perf_hpp *hpp, struct hist_entry *he) 1457 { 1458 return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO); 1459 } 1460 1461 static int hpp__color_wdiff(struct perf_hpp_fmt *fmt, 1462 struct perf_hpp *hpp, struct hist_entry *he) 1463 { 1464 return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF); 1465 } 1466 1467 static int hpp__color_cycles(struct perf_hpp_fmt *fmt, 1468 struct perf_hpp *hpp, struct hist_entry *he) 1469 { 1470 return __hpp__color_compare(fmt, hpp, he, COMPUTE_CYCLES); 1471 } 1472 1473 static int all_zero(unsigned long *vals, int len) 1474 { 1475 int i; 1476 1477 for (i = 0; i < len; i++) 1478 if (vals[i] != 0) 1479 return 0; 1480 return 1; 1481 } 1482 1483 static int print_cycles_spark(char *bf, int size, unsigned long *svals, u64 n) 1484 { 1485 int printed; 1486 1487 if (n <= 1) 1488 return 0; 1489 1490 if (n > NUM_SPARKS) 1491 n = NUM_SPARKS; 1492 if (all_zero(svals, n)) 1493 return 0; 1494 1495 printed = print_spark(bf, size, svals, n); 1496 printed += scnprintf(bf + printed, size - printed, " "); 1497 return printed; 1498 } 1499 1500 static int hpp__color_cycles_hist(struct perf_hpp_fmt *fmt, 1501 struct perf_hpp *hpp, struct hist_entry *he) 1502 { 1503 struct diff_hpp_fmt *dfmt = 1504 container_of(fmt, struct diff_hpp_fmt, fmt); 1505 struct hist_entry *pair = get_pair_fmt(he, dfmt); 1506 struct block_hist *bh = container_of(he, struct block_hist, he); 1507 struct block_hist *bh_pair; 1508 struct hist_entry *block_he; 1509 char spark[32], buf[128]; 1510 double r; 1511 int ret, pad; 1512 1513 if (!pair) { 1514 if (bh->block_idx) 1515 hpp->skip = true; 1516 1517 goto no_print; 1518 } 1519 1520 bh_pair = container_of(pair, struct block_hist, he); 1521 1522 block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx); 1523 if (!block_he) { 1524 hpp->skip = true; 1525 goto no_print; 1526 } 1527 1528 ret = print_cycles_spark(spark, sizeof(spark), block_he->diff.svals, 1529 block_he->diff.stats.n); 1530 1531 r = rel_stddev_stats(stddev_stats(&block_he->diff.stats), 1532 avg_stats(&block_he->diff.stats)); 1533 1534 if (ret) { 1535 /* 1536 * Padding spaces if number of sparks less than NUM_SPARKS 1537 * otherwise the output is not aligned. 1538 */ 1539 pad = NUM_SPARKS - ((ret - 1) / 3); 1540 scnprintf(buf, sizeof(buf), "%s%5.1f%% %s", "\u00B1", r, spark); 1541 ret = scnprintf(hpp->buf, hpp->size, "%*s", 1542 dfmt->header_width, buf); 1543 1544 if (pad) { 1545 ret += scnprintf(hpp->buf + ret, hpp->size - ret, 1546 "%-*s", pad, " "); 1547 } 1548 1549 return ret; 1550 } 1551 1552 no_print: 1553 return scnprintf(hpp->buf, hpp->size, "%*s", 1554 dfmt->header_width, " "); 1555 } 1556 1557 static void 1558 hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size) 1559 { 1560 switch (idx) { 1561 case PERF_HPP_DIFF__PERIOD_BASELINE: 1562 scnprintf(buf, size, "%" PRIu64, he->stat.period); 1563 break; 1564 1565 default: 1566 break; 1567 } 1568 } 1569 1570 static void 1571 hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair, 1572 int idx, char *buf, size_t size) 1573 { 1574 double diff; 1575 double ratio; 1576 s64 wdiff; 1577 1578 switch (idx) { 1579 case PERF_HPP_DIFF__DELTA: 1580 case PERF_HPP_DIFF__DELTA_ABS: 1581 if (pair->diff.computed) 1582 diff = pair->diff.period_ratio_delta; 1583 else 1584 diff = compute_delta(he, pair); 1585 1586 scnprintf(buf, size, "%+4.2F%%", diff); 1587 break; 1588 1589 case PERF_HPP_DIFF__RATIO: 1590 /* No point for ratio number if we are dummy.. */ 1591 if (he->dummy) { 1592 scnprintf(buf, size, "N/A"); 1593 break; 1594 } 1595 1596 if (pair->diff.computed) 1597 ratio = pair->diff.period_ratio; 1598 else 1599 ratio = compute_ratio(he, pair); 1600 1601 if (ratio > 0.0) 1602 scnprintf(buf, size, "%14.6F", ratio); 1603 break; 1604 1605 case PERF_HPP_DIFF__WEIGHTED_DIFF: 1606 /* No point for wdiff number if we are dummy.. */ 1607 if (he->dummy) { 1608 scnprintf(buf, size, "N/A"); 1609 break; 1610 } 1611 1612 if (pair->diff.computed) 1613 wdiff = pair->diff.wdiff; 1614 else 1615 wdiff = compute_wdiff(he, pair); 1616 1617 if (wdiff != 0) 1618 scnprintf(buf, size, "%14ld", wdiff); 1619 break; 1620 1621 case PERF_HPP_DIFF__FORMULA: 1622 formula_fprintf(he, pair, buf, size); 1623 break; 1624 1625 case PERF_HPP_DIFF__PERIOD: 1626 scnprintf(buf, size, "%" PRIu64, pair->stat.period); 1627 break; 1628 1629 default: 1630 BUG_ON(1); 1631 } 1632 } 1633 1634 static void 1635 __hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt, 1636 char *buf, size_t size) 1637 { 1638 struct hist_entry *pair = get_pair_fmt(he, dfmt); 1639 int idx = dfmt->idx; 1640 1641 /* baseline is special */ 1642 if (idx == PERF_HPP_DIFF__BASELINE) 1643 hpp__entry_baseline(he, buf, size); 1644 else { 1645 if (pair) 1646 hpp__entry_pair(he, pair, idx, buf, size); 1647 else 1648 hpp__entry_unpair(he, idx, buf, size); 1649 } 1650 } 1651 1652 static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp, 1653 struct hist_entry *he) 1654 { 1655 struct diff_hpp_fmt *dfmt = 1656 container_of(_fmt, struct diff_hpp_fmt, fmt); 1657 char buf[MAX_COL_WIDTH] = " "; 1658 1659 __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH); 1660 1661 if (symbol_conf.field_sep) 1662 return scnprintf(hpp->buf, hpp->size, "%s", buf); 1663 else 1664 return scnprintf(hpp->buf, hpp->size, "%*s", 1665 dfmt->header_width, buf); 1666 } 1667 1668 static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, 1669 struct hists *hists __maybe_unused, 1670 int line __maybe_unused, 1671 int *span __maybe_unused) 1672 { 1673 struct diff_hpp_fmt *dfmt = 1674 container_of(fmt, struct diff_hpp_fmt, fmt); 1675 1676 BUG_ON(!dfmt->header); 1677 return scnprintf(hpp->buf, hpp->size, dfmt->header); 1678 } 1679 1680 static int hpp__width(struct perf_hpp_fmt *fmt, 1681 struct perf_hpp *hpp __maybe_unused, 1682 struct hists *hists __maybe_unused) 1683 { 1684 struct diff_hpp_fmt *dfmt = 1685 container_of(fmt, struct diff_hpp_fmt, fmt); 1686 1687 BUG_ON(dfmt->header_width <= 0); 1688 return dfmt->header_width; 1689 } 1690 1691 static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt) 1692 { 1693 #define MAX_HEADER_NAME 100 1694 char buf_indent[MAX_HEADER_NAME]; 1695 char buf[MAX_HEADER_NAME]; 1696 const char *header = NULL; 1697 int width = 0; 1698 1699 BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX); 1700 header = columns[dfmt->idx].name; 1701 width = columns[dfmt->idx].width; 1702 1703 /* Only our defined HPP fmts should appear here. */ 1704 BUG_ON(!header); 1705 1706 if (data__files_cnt > 2) 1707 scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx); 1708 1709 #define NAME (data__files_cnt > 2 ? buf : header) 1710 dfmt->header_width = width; 1711 width = (int) strlen(NAME); 1712 if (dfmt->header_width < width) 1713 dfmt->header_width = width; 1714 1715 scnprintf(buf_indent, MAX_HEADER_NAME, "%*s", 1716 dfmt->header_width, NAME); 1717 1718 dfmt->header = strdup(buf_indent); 1719 #undef MAX_HEADER_NAME 1720 #undef NAME 1721 } 1722 1723 static void data__hpp_register(struct data__file *d, int idx) 1724 { 1725 struct diff_hpp_fmt *dfmt = &d->fmt[idx]; 1726 struct perf_hpp_fmt *fmt = &dfmt->fmt; 1727 1728 dfmt->idx = idx; 1729 1730 fmt->header = hpp__header; 1731 fmt->width = hpp__width; 1732 fmt->entry = hpp__entry_global; 1733 fmt->cmp = hist_entry__cmp_nop; 1734 fmt->collapse = hist_entry__cmp_nop; 1735 1736 /* TODO more colors */ 1737 switch (idx) { 1738 case PERF_HPP_DIFF__BASELINE: 1739 fmt->color = hpp__color_baseline; 1740 fmt->sort = hist_entry__cmp_baseline; 1741 break; 1742 case PERF_HPP_DIFF__DELTA: 1743 fmt->color = hpp__color_delta; 1744 fmt->sort = hist_entry__cmp_delta; 1745 break; 1746 case PERF_HPP_DIFF__RATIO: 1747 fmt->color = hpp__color_ratio; 1748 fmt->sort = hist_entry__cmp_ratio; 1749 break; 1750 case PERF_HPP_DIFF__WEIGHTED_DIFF: 1751 fmt->color = hpp__color_wdiff; 1752 fmt->sort = hist_entry__cmp_wdiff; 1753 break; 1754 case PERF_HPP_DIFF__DELTA_ABS: 1755 fmt->color = hpp__color_delta; 1756 fmt->sort = hist_entry__cmp_delta_abs; 1757 break; 1758 case PERF_HPP_DIFF__CYCLES: 1759 fmt->color = hpp__color_cycles; 1760 fmt->sort = hist_entry__cmp_nop; 1761 break; 1762 case PERF_HPP_DIFF__CYCLES_HIST: 1763 fmt->color = hpp__color_cycles_hist; 1764 fmt->sort = hist_entry__cmp_nop; 1765 break; 1766 default: 1767 fmt->sort = hist_entry__cmp_nop; 1768 break; 1769 } 1770 1771 init_header(d, dfmt); 1772 perf_hpp__column_register(fmt); 1773 perf_hpp__register_sort_field(fmt); 1774 } 1775 1776 static int ui_init(void) 1777 { 1778 struct data__file *d; 1779 struct perf_hpp_fmt *fmt; 1780 int i; 1781 1782 data__for_each_file(i, d) { 1783 1784 /* 1785 * Baseline or compute related columns: 1786 * 1787 * PERF_HPP_DIFF__BASELINE 1788 * PERF_HPP_DIFF__DELTA 1789 * PERF_HPP_DIFF__RATIO 1790 * PERF_HPP_DIFF__WEIGHTED_DIFF 1791 * PERF_HPP_DIFF__CYCLES 1792 */ 1793 data__hpp_register(d, i ? compute_2_hpp[compute] : 1794 PERF_HPP_DIFF__BASELINE); 1795 1796 if (cycles_hist && i) 1797 data__hpp_register(d, PERF_HPP_DIFF__CYCLES_HIST); 1798 1799 /* 1800 * And the rest: 1801 * 1802 * PERF_HPP_DIFF__FORMULA 1803 * PERF_HPP_DIFF__PERIOD 1804 * PERF_HPP_DIFF__PERIOD_BASELINE 1805 */ 1806 if (show_formula && i) 1807 data__hpp_register(d, PERF_HPP_DIFF__FORMULA); 1808 1809 if (show_period) 1810 data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD : 1811 PERF_HPP_DIFF__PERIOD_BASELINE); 1812 } 1813 1814 if (!sort_compute) 1815 return 0; 1816 1817 /* 1818 * Prepend an fmt to sort on columns at 'sort_compute' first. 1819 * This fmt is added only to the sort list but not to the 1820 * output fields list. 1821 * 1822 * Note that this column (data) can be compared twice - one 1823 * for this 'sort_compute' fmt and another for the normal 1824 * diff_hpp_fmt. But it shouldn't a problem as most entries 1825 * will be sorted out by first try or baseline and comparing 1826 * is not a costly operation. 1827 */ 1828 fmt = zalloc(sizeof(*fmt)); 1829 if (fmt == NULL) { 1830 pr_err("Memory allocation failed\n"); 1831 return -1; 1832 } 1833 1834 fmt->cmp = hist_entry__cmp_nop; 1835 fmt->collapse = hist_entry__cmp_nop; 1836 1837 switch (compute) { 1838 case COMPUTE_DELTA: 1839 fmt->sort = hist_entry__cmp_delta_idx; 1840 break; 1841 case COMPUTE_RATIO: 1842 fmt->sort = hist_entry__cmp_ratio_idx; 1843 break; 1844 case COMPUTE_WEIGHTED_DIFF: 1845 fmt->sort = hist_entry__cmp_wdiff_idx; 1846 break; 1847 case COMPUTE_DELTA_ABS: 1848 fmt->sort = hist_entry__cmp_delta_abs_idx; 1849 break; 1850 case COMPUTE_CYCLES: 1851 /* 1852 * Should set since 'fmt->sort' is called without 1853 * checking valid during sorting 1854 */ 1855 fmt->sort = hist_entry__cmp_nop; 1856 break; 1857 default: 1858 BUG_ON(1); 1859 } 1860 1861 perf_hpp__prepend_sort_field(fmt); 1862 return 0; 1863 } 1864 1865 static int data_init(int argc, const char **argv) 1866 { 1867 struct data__file *d; 1868 static const char *defaults[] = { 1869 "perf.data.old", 1870 "perf.data", 1871 }; 1872 bool use_default = true; 1873 int i; 1874 1875 data__files_cnt = 2; 1876 1877 if (argc) { 1878 if (argc == 1) 1879 defaults[1] = argv[0]; 1880 else { 1881 data__files_cnt = argc; 1882 use_default = false; 1883 } 1884 } else if (perf_guest) { 1885 defaults[0] = "perf.data.host"; 1886 defaults[1] = "perf.data.guest"; 1887 } 1888 1889 if (sort_compute >= (unsigned int) data__files_cnt) { 1890 pr_err("Order option out of limit.\n"); 1891 return -EINVAL; 1892 } 1893 1894 data__files = calloc(data__files_cnt, sizeof(*data__files)); 1895 if (!data__files) 1896 return -ENOMEM; 1897 1898 data__for_each_file(i, d) { 1899 struct perf_data *data = &d->data; 1900 1901 data->path = use_default ? defaults[i] : argv[i]; 1902 data->mode = PERF_DATA_MODE_READ; 1903 data->force = force; 1904 1905 d->idx = i; 1906 } 1907 1908 return 0; 1909 } 1910 1911 static int diff__config(const char *var, const char *value, 1912 void *cb __maybe_unused) 1913 { 1914 if (!strcmp(var, "diff.order")) { 1915 int ret; 1916 if (perf_config_int(&ret, var, value) < 0) 1917 return -1; 1918 sort_compute = ret; 1919 return 0; 1920 } 1921 if (!strcmp(var, "diff.compute")) { 1922 if (!strcmp(value, "delta")) { 1923 compute = COMPUTE_DELTA; 1924 } else if (!strcmp(value, "delta-abs")) { 1925 compute = COMPUTE_DELTA_ABS; 1926 } else if (!strcmp(value, "ratio")) { 1927 compute = COMPUTE_RATIO; 1928 } else if (!strcmp(value, "wdiff")) { 1929 compute = COMPUTE_WEIGHTED_DIFF; 1930 } else { 1931 pr_err("Invalid compute method: %s\n", value); 1932 return -1; 1933 } 1934 } 1935 1936 return 0; 1937 } 1938 1939 int cmd_diff(int argc, const char **argv) 1940 { 1941 int ret = hists__init(); 1942 1943 if (ret < 0) 1944 return ret; 1945 1946 perf_tool__init(&pdiff.tool, /*ordered_events=*/true); 1947 pdiff.tool.sample = diff__process_sample_event; 1948 pdiff.tool.mmap = perf_event__process_mmap; 1949 pdiff.tool.mmap2 = perf_event__process_mmap2; 1950 pdiff.tool.comm = perf_event__process_comm; 1951 pdiff.tool.exit = perf_event__process_exit; 1952 pdiff.tool.fork = perf_event__process_fork; 1953 pdiff.tool.lost = perf_event__process_lost; 1954 pdiff.tool.namespaces = perf_event__process_namespaces; 1955 pdiff.tool.cgroup = perf_event__process_cgroup; 1956 pdiff.tool.ordering_requires_timestamps = true; 1957 1958 perf_config(diff__config, NULL); 1959 1960 argc = parse_options(argc, argv, options, diff_usage, 0); 1961 1962 if (quiet) 1963 perf_quiet_option(); 1964 1965 if (cycles_hist && (compute != COMPUTE_CYCLES)) 1966 usage_with_options(diff_usage, options); 1967 1968 if (pdiff.stream) 1969 compute = COMPUTE_STREAM; 1970 1971 symbol__annotation_init(); 1972 1973 if (symbol__init(NULL) < 0) 1974 return -1; 1975 1976 if (data_init(argc, argv) < 0) 1977 return -1; 1978 1979 if (check_file_brstack() < 0) 1980 return -1; 1981 1982 if ((compute == COMPUTE_CYCLES || compute == COMPUTE_STREAM) 1983 && !pdiff.has_br_stack) { 1984 return -1; 1985 } 1986 1987 if (compute == COMPUTE_STREAM) { 1988 symbol_conf.show_branchflag_count = true; 1989 symbol_conf.addr2line_disable_warn = true; 1990 callchain_param.mode = CHAIN_FLAT; 1991 callchain_param.key = CCKEY_SRCLINE; 1992 callchain_param.branch_callstack = 1; 1993 symbol_conf.use_callchain = true; 1994 callchain_register_param(&callchain_param); 1995 sort_order = "srcline,symbol,dso"; 1996 } else { 1997 if (ui_init() < 0) 1998 return -1; 1999 2000 sort__mode = SORT_MODE__DIFF; 2001 } 2002 2003 if (setup_sorting(/*evlist=*/NULL, perf_session__env(data__files[0].session)) < 0) 2004 usage_with_options(diff_usage, options); 2005 2006 setup_pager(); 2007 2008 sort__setup_elide(NULL); 2009 2010 return __cmd_diff(); 2011 } 2012