4 * Builtin diff command: Analyze two perf.data input files, look up and read
5 * DSOs and symbol information, sort them and produce a diff.
9 #include "util/debug.h"
10 #include "util/event.h"
11 #include "util/hist.h"
12 #include "util/evsel.h"
13 #include "util/evlist.h"
14 #include "util/session.h"
15 #include "util/tool.h"
16 #include "util/sort.h"
17 #include "util/symbol.h"
18 #include "util/util.h"
19 #include "util/data.h"
24 /* Diff command specific HPP columns. */
26 PERF_HPP_DIFF__BASELINE,
27 PERF_HPP_DIFF__PERIOD,
28 PERF_HPP_DIFF__PERIOD_BASELINE,
31 PERF_HPP_DIFF__WEIGHTED_DIFF,
32 PERF_HPP_DIFF__FORMULA,
34 PERF_HPP_DIFF__MAX_INDEX
38 struct perf_hpp_fmt fmt;
45 struct perf_session *session;
46 struct perf_data_file file;
49 struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX];
52 static struct data__file *data__files;
53 static int data__files_cnt;
55 #define data__for_each_file_start(i, d, s) \
56 for (i = s, d = &data__files[s]; \
57 i < data__files_cnt; \
58 i++, d = &data__files[i])
60 #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
61 #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
63 static char diff__default_sort_order[] = "dso,symbol";
65 static bool show_period;
66 static bool show_formula;
67 static bool show_baseline_only;
68 static unsigned int sort_compute;
70 static s64 compute_wdiff_w1;
71 static s64 compute_wdiff_w2;
76 COMPUTE_WEIGHTED_DIFF,
80 const char *compute_names[COMPUTE_MAX] = {
81 [COMPUTE_DELTA] = "delta",
82 [COMPUTE_RATIO] = "ratio",
83 [COMPUTE_WEIGHTED_DIFF] = "wdiff",
88 static int compute_2_hpp[COMPUTE_MAX] = {
89 [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA,
90 [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO,
91 [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF,
94 #define MAX_COL_WIDTH 70
96 static struct header_column {
99 } columns[PERF_HPP_DIFF__MAX_INDEX] = {
100 [PERF_HPP_DIFF__BASELINE] = {
103 [PERF_HPP_DIFF__PERIOD] = {
107 [PERF_HPP_DIFF__PERIOD_BASELINE] = {
108 .name = "Base period",
111 [PERF_HPP_DIFF__DELTA] = {
115 [PERF_HPP_DIFF__RATIO] = {
119 [PERF_HPP_DIFF__WEIGHTED_DIFF] = {
120 .name = "Weighted diff",
123 [PERF_HPP_DIFF__FORMULA] = {
125 .width = MAX_COL_WIDTH,
129 static int setup_compute_opt_wdiff(char *opt)
139 w2_str = strchr(opt, ',');
147 compute_wdiff_w1 = strtol(w1_str, NULL, 10);
148 compute_wdiff_w2 = strtol(w2_str, NULL, 10);
150 if (!compute_wdiff_w1 || !compute_wdiff_w2)
153 pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
154 compute_wdiff_w1, compute_wdiff_w2);
160 pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
165 static int setup_compute_opt(char *opt)
167 if (compute == COMPUTE_WEIGHTED_DIFF)
168 return setup_compute_opt_wdiff(opt);
171 pr_err("Failed: extra option specified '%s'", opt);
178 static int setup_compute(const struct option *opt, const char *str,
179 int unset __maybe_unused)
181 int *cp = (int *) opt->value;
182 char *cstr = (char *) str;
192 option = strchr(str, ':');
194 unsigned len = option++ - str;
197 * The str data are not writeable, so we need
198 * to use another buffer.
201 /* No option value is longer. */
202 if (len >= sizeof(buf))
205 strncpy(buf, str, len);
210 for (i = 0; i < COMPUTE_MAX; i++)
211 if (!strcmp(cstr, compute_names[i])) {
213 return setup_compute_opt(option);
216 pr_err("Failed: '%s' is not computation method "
217 "(use 'delta','ratio' or 'wdiff')\n", str);
221 static double period_percent(struct hist_entry *he, u64 period)
223 u64 total = hists__total_period(he->hists);
225 return (period * 100.0) / total;
228 static double compute_delta(struct hist_entry *he, struct hist_entry *pair)
230 double old_percent = period_percent(he, he->stat.period);
231 double new_percent = period_percent(pair, pair->stat.period);
233 pair->diff.period_ratio_delta = new_percent - old_percent;
234 pair->diff.computed = true;
235 return pair->diff.period_ratio_delta;
238 static double compute_ratio(struct hist_entry *he, struct hist_entry *pair)
240 double old_period = he->stat.period ?: 1;
241 double new_period = pair->stat.period;
243 pair->diff.computed = true;
244 pair->diff.period_ratio = new_period / old_period;
245 return pair->diff.period_ratio;
248 static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair)
250 u64 old_period = he->stat.period;
251 u64 new_period = pair->stat.period;
253 pair->diff.computed = true;
254 pair->diff.wdiff = new_period * compute_wdiff_w2 -
255 old_period * compute_wdiff_w1;
257 return pair->diff.wdiff;
260 static int formula_delta(struct hist_entry *he, struct hist_entry *pair,
261 char *buf, size_t size)
263 u64 he_total = he->hists->stats.total_period;
264 u64 pair_total = pair->hists->stats.total_period;
266 if (symbol_conf.filter_relative) {
267 he_total = he->hists->stats.total_non_filtered_period;
268 pair_total = pair->hists->stats.total_non_filtered_period;
270 return scnprintf(buf, size,
271 "(%" PRIu64 " * 100 / %" PRIu64 ") - "
272 "(%" PRIu64 " * 100 / %" PRIu64 ")",
273 pair->stat.period, pair_total,
274 he->stat.period, he_total);
277 static int formula_ratio(struct hist_entry *he, struct hist_entry *pair,
278 char *buf, size_t size)
280 double old_period = he->stat.period;
281 double new_period = pair->stat.period;
283 return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
286 static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair,
287 char *buf, size_t size)
289 u64 old_period = he->stat.period;
290 u64 new_period = pair->stat.period;
292 return scnprintf(buf, size,
293 "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
294 new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
297 static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,
298 char *buf, size_t size)
302 return formula_delta(he, pair, buf, size);
304 return formula_ratio(he, pair, buf, size);
305 case COMPUTE_WEIGHTED_DIFF:
306 return formula_wdiff(he, pair, buf, size);
314 static int hists__add_entry(struct hists *hists,
315 struct addr_location *al, u64 period,
316 u64 weight, u64 transaction)
318 if (__hists__add_entry(hists, al, NULL, NULL, NULL, period, weight,
319 transaction) != NULL)
324 static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
325 union perf_event *event,
326 struct perf_sample *sample,
327 struct perf_evsel *evsel,
328 struct machine *machine)
330 struct addr_location al;
332 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
333 pr_warning("problem processing %d event, skipping it.\n",
338 if (hists__add_entry(&evsel->hists, &al, sample->period,
339 sample->weight, sample->transaction)) {
340 pr_warning("problem incrementing symbol period, skipping event\n");
345 * The total_period is updated here before going to the output
346 * tree since normally only the baseline hists will call
347 * hists__output_resort() and precompute needs the total
348 * period in order to sort entries by percentage delta.
350 evsel->hists.stats.total_period += sample->period;
352 evsel->hists.stats.total_non_filtered_period += sample->period;
357 static struct perf_tool tool = {
358 .sample = diff__process_sample_event,
359 .mmap = perf_event__process_mmap,
360 .comm = perf_event__process_comm,
361 .exit = perf_event__process_exit,
362 .fork = perf_event__process_fork,
363 .lost = perf_event__process_lost,
364 .ordered_samples = true,
365 .ordering_requires_timestamps = true,
368 static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
369 struct perf_evlist *evlist)
371 struct perf_evsel *e;
373 evlist__for_each(evlist, e) {
374 if (perf_evsel__match2(evsel, e))
381 static void perf_evlist__collapse_resort(struct perf_evlist *evlist)
383 struct perf_evsel *evsel;
385 evlist__for_each(evlist, evsel) {
386 struct hists *hists = &evsel->hists;
388 hists__collapse_resort(hists, NULL);
392 static struct hist_entry*
393 get_pair_data(struct hist_entry *he, struct data__file *d)
395 if (hist_entry__has_pairs(he)) {
396 struct hist_entry *pair;
398 list_for_each_entry(pair, &he->pairs.head, pairs.node)
399 if (pair->hists == d->hists)
406 static struct hist_entry*
407 get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt)
409 void *ptr = dfmt - dfmt->idx;
410 struct data__file *d = container_of(ptr, struct data__file, fmt);
412 return get_pair_data(he, d);
415 static void hists__baseline_only(struct hists *hists)
417 struct rb_root *root;
418 struct rb_node *next;
420 if (sort__need_collapse)
421 root = &hists->entries_collapsed;
423 root = hists->entries_in;
425 next = rb_first(root);
426 while (next != NULL) {
427 struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
429 next = rb_next(&he->rb_node_in);
430 if (!hist_entry__next_pair(he)) {
431 rb_erase(&he->rb_node_in, root);
432 hist_entry__free(he);
437 static void hists__precompute(struct hists *hists)
439 struct rb_root *root;
440 struct rb_node *next;
442 if (sort__need_collapse)
443 root = &hists->entries_collapsed;
445 root = hists->entries_in;
447 next = rb_first(root);
448 while (next != NULL) {
449 struct hist_entry *he, *pair;
451 he = rb_entry(next, struct hist_entry, rb_node_in);
452 next = rb_next(&he->rb_node_in);
454 pair = get_pair_data(he, &data__files[sort_compute]);
460 compute_delta(he, pair);
463 compute_ratio(he, pair);
465 case COMPUTE_WEIGHTED_DIFF:
466 compute_wdiff(he, pair);
474 static int64_t cmp_doubles(double l, double r)
485 __hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
491 double l = left->diff.period_ratio_delta;
492 double r = right->diff.period_ratio_delta;
494 return cmp_doubles(l, r);
498 double l = left->diff.period_ratio;
499 double r = right->diff.period_ratio;
501 return cmp_doubles(l, r);
503 case COMPUTE_WEIGHTED_DIFF:
505 s64 l = left->diff.wdiff;
506 s64 r = right->diff.wdiff;
518 hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
521 bool pairs_left = hist_entry__has_pairs(left);
522 bool pairs_right = hist_entry__has_pairs(right);
523 struct hist_entry *p_right, *p_left;
525 if (!pairs_left && !pairs_right)
528 if (!pairs_left || !pairs_right)
529 return pairs_left ? -1 : 1;
531 p_left = get_pair_data(left, &data__files[sort_compute]);
532 p_right = get_pair_data(right, &data__files[sort_compute]);
534 if (!p_left && !p_right)
537 if (!p_left || !p_right)
538 return p_left ? -1 : 1;
541 * We have 2 entries of same kind, let's
542 * make the data comparison.
544 return __hist_entry__cmp_compute(p_left, p_right, c);
547 static void insert_hist_entry_by_compute(struct rb_root *root,
548 struct hist_entry *he,
551 struct rb_node **p = &root->rb_node;
552 struct rb_node *parent = NULL;
553 struct hist_entry *iter;
557 iter = rb_entry(parent, struct hist_entry, rb_node);
558 if (hist_entry__cmp_compute(he, iter, c) < 0)
564 rb_link_node(&he->rb_node, parent, p);
565 rb_insert_color(&he->rb_node, root);
568 static void hists__compute_resort(struct hists *hists)
570 struct rb_root *root;
571 struct rb_node *next;
573 if (sort__need_collapse)
574 root = &hists->entries_collapsed;
576 root = hists->entries_in;
578 hists->entries = RB_ROOT;
579 next = rb_first(root);
581 hists__reset_stats(hists);
582 hists__reset_col_len(hists);
584 while (next != NULL) {
585 struct hist_entry *he;
587 he = rb_entry(next, struct hist_entry, rb_node_in);
588 next = rb_next(&he->rb_node_in);
590 insert_hist_entry_by_compute(&hists->entries, he, compute);
591 hists__inc_stats(hists, he);
594 hists__calc_col_len(hists, he);
598 static void hists__process(struct hists *hists)
600 if (show_baseline_only)
601 hists__baseline_only(hists);
604 hists__precompute(hists);
605 hists__compute_resort(hists);
607 hists__output_resort(hists);
610 hists__fprintf(hists, true, 0, 0, 0, stdout);
613 static void data__fprintf(void)
615 struct data__file *d;
618 fprintf(stdout, "# Data files:\n");
620 data__for_each_file(i, d)
621 fprintf(stdout, "# [%d] %s %s\n",
622 d->idx, d->file.path,
623 !d->idx ? "(Baseline)" : "");
625 fprintf(stdout, "#\n");
628 static void data_process(void)
630 struct perf_evlist *evlist_base = data__files[0].session->evlist;
631 struct perf_evsel *evsel_base;
634 evlist__for_each(evlist_base, evsel_base) {
635 struct data__file *d;
638 data__for_each_file_new(i, d) {
639 struct perf_evlist *evlist = d->session->evlist;
640 struct perf_evsel *evsel;
642 evsel = evsel_match(evsel_base, evlist);
646 d->hists = &evsel->hists;
648 hists__match(&evsel_base->hists, &evsel->hists);
650 if (!show_baseline_only)
651 hists__link(&evsel_base->hists,
655 fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
656 perf_evsel__name(evsel_base));
660 if (verbose || data__files_cnt > 2)
663 hists__process(&evsel_base->hists);
667 static void data__free(struct data__file *d)
671 for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) {
672 struct diff_hpp_fmt *fmt = &d->fmt[col];
678 static int __cmd_diff(void)
680 struct data__file *d;
681 int ret = -EINVAL, i;
683 data__for_each_file(i, d) {
684 d->session = perf_session__new(&d->file, false, &tool);
686 pr_err("Failed to open %s\n", d->file.path);
691 ret = perf_session__process_events(d->session, &tool);
693 pr_err("Failed to process %s\n", d->file.path);
697 perf_evlist__collapse_resort(d->session->evlist);
703 data__for_each_file(i, d) {
705 perf_session__delete(d->session);
714 static const char * const diff_usage[] = {
715 "perf diff [<options>] [old_file] [new_file]",
719 static const struct option options[] = {
720 OPT_INCR('v', "verbose", &verbose,
721 "be more verbose (show symbol address, etc)"),
722 OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
723 "Show only items with match in baseline"),
724 OPT_CALLBACK('c', "compute", &compute,
725 "delta,ratio,wdiff:w1,w2 (default delta)",
726 "Entries differential computation selection",
728 OPT_BOOLEAN('p', "period", &show_period,
729 "Show period values."),
730 OPT_BOOLEAN('F', "formula", &show_formula,
732 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
733 "dump raw trace in ASCII"),
734 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
735 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
736 "load module symbols - WARNING: use only with -k and LIVE kernel"),
737 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
738 "only consider symbols in these dsos"),
739 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
740 "only consider symbols in these comms"),
741 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
742 "only consider these symbols"),
743 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
744 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
745 " Please refer the man page for the complete list."),
746 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
747 "separator for columns, no spaces will be added between "
748 "columns '.' is reserved."),
749 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
750 "Look for files with symbols relative to this directory"),
751 OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
752 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
753 "How to display percentage of filtered entries", parse_filter_percentage),
757 static double baseline_percent(struct hist_entry *he)
759 u64 total = hists__total_period(he->hists);
761 return 100.0 * he->stat.period / total;
764 static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
765 struct perf_hpp *hpp, struct hist_entry *he)
767 struct diff_hpp_fmt *dfmt =
768 container_of(fmt, struct diff_hpp_fmt, fmt);
769 double percent = baseline_percent(he);
773 scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
774 return percent_color_snprintf(hpp->buf, hpp->size,
777 return scnprintf(hpp->buf, hpp->size, "%*s",
778 dfmt->header_width, pfmt);
781 static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
783 double percent = baseline_percent(he);
784 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
788 ret = scnprintf(buf, size, fmt, percent);
793 static int __hpp__color_compare(struct perf_hpp_fmt *fmt,
794 struct perf_hpp *hpp, struct hist_entry *he,
795 int comparison_method)
797 struct diff_hpp_fmt *dfmt =
798 container_of(fmt, struct diff_hpp_fmt, fmt);
799 struct hist_entry *pair = get_pair_fmt(he, dfmt);
807 switch (comparison_method) {
809 if (pair->diff.computed)
810 diff = pair->diff.period_ratio_delta;
812 diff = compute_delta(he, pair);
814 if (fabs(diff) < 0.01)
816 scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1);
817 return percent_color_snprintf(hpp->buf, hpp->size,
822 if (pair->diff.computed)
823 diff = pair->diff.period_ratio;
825 diff = compute_ratio(he, pair);
827 scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width);
828 return value_color_snprintf(hpp->buf, hpp->size,
830 case COMPUTE_WEIGHTED_DIFF:
833 if (pair->diff.computed)
834 wdiff = pair->diff.wdiff;
836 wdiff = compute_wdiff(he, pair);
838 scnprintf(pfmt, 20, "%%14ld", dfmt->header_width);
839 return color_snprintf(hpp->buf, hpp->size,
840 get_percent_color(wdiff),
846 return scnprintf(hpp->buf, hpp->size, "%*s",
847 dfmt->header_width, pfmt);
850 static int hpp__color_delta(struct perf_hpp_fmt *fmt,
851 struct perf_hpp *hpp, struct hist_entry *he)
853 return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA);
856 static int hpp__color_ratio(struct perf_hpp_fmt *fmt,
857 struct perf_hpp *hpp, struct hist_entry *he)
859 return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO);
862 static int hpp__color_wdiff(struct perf_hpp_fmt *fmt,
863 struct perf_hpp *hpp, struct hist_entry *he)
865 return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF);
869 hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
872 case PERF_HPP_DIFF__PERIOD_BASELINE:
873 scnprintf(buf, size, "%" PRIu64, he->stat.period);
882 hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
883 int idx, char *buf, size_t size)
890 case PERF_HPP_DIFF__DELTA:
891 if (pair->diff.computed)
892 diff = pair->diff.period_ratio_delta;
894 diff = compute_delta(he, pair);
896 if (fabs(diff) >= 0.01)
897 scnprintf(buf, size, "%+4.2F%%", diff);
900 case PERF_HPP_DIFF__RATIO:
901 /* No point for ratio number if we are dummy.. */
905 if (pair->diff.computed)
906 ratio = pair->diff.period_ratio;
908 ratio = compute_ratio(he, pair);
911 scnprintf(buf, size, "%14.6F", ratio);
914 case PERF_HPP_DIFF__WEIGHTED_DIFF:
915 /* No point for wdiff number if we are dummy.. */
919 if (pair->diff.computed)
920 wdiff = pair->diff.wdiff;
922 wdiff = compute_wdiff(he, pair);
925 scnprintf(buf, size, "%14ld", wdiff);
928 case PERF_HPP_DIFF__FORMULA:
929 formula_fprintf(he, pair, buf, size);
932 case PERF_HPP_DIFF__PERIOD:
933 scnprintf(buf, size, "%" PRIu64, pair->stat.period);
942 __hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
943 char *buf, size_t size)
945 struct hist_entry *pair = get_pair_fmt(he, dfmt);
948 /* baseline is special */
949 if (idx == PERF_HPP_DIFF__BASELINE)
950 hpp__entry_baseline(he, buf, size);
953 hpp__entry_pair(he, pair, idx, buf, size);
955 hpp__entry_unpair(he, idx, buf, size);
959 static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
960 struct hist_entry *he)
962 struct diff_hpp_fmt *dfmt =
963 container_of(_fmt, struct diff_hpp_fmt, fmt);
964 char buf[MAX_COL_WIDTH] = " ";
966 __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
968 if (symbol_conf.field_sep)
969 return scnprintf(hpp->buf, hpp->size, "%s", buf);
971 return scnprintf(hpp->buf, hpp->size, "%*s",
972 dfmt->header_width, buf);
975 static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
976 struct perf_evsel *evsel __maybe_unused)
978 struct diff_hpp_fmt *dfmt =
979 container_of(fmt, struct diff_hpp_fmt, fmt);
981 BUG_ON(!dfmt->header);
982 return scnprintf(hpp->buf, hpp->size, dfmt->header);
985 static int hpp__width(struct perf_hpp_fmt *fmt,
986 struct perf_hpp *hpp __maybe_unused,
987 struct perf_evsel *evsel __maybe_unused)
989 struct diff_hpp_fmt *dfmt =
990 container_of(fmt, struct diff_hpp_fmt, fmt);
992 BUG_ON(dfmt->header_width <= 0);
993 return dfmt->header_width;
996 static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
998 #define MAX_HEADER_NAME 100
999 char buf_indent[MAX_HEADER_NAME];
1000 char buf[MAX_HEADER_NAME];
1001 const char *header = NULL;
1004 BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
1005 header = columns[dfmt->idx].name;
1006 width = columns[dfmt->idx].width;
1008 /* Only our defined HPP fmts should appear here. */
1011 if (data__files_cnt > 2)
1012 scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
1014 #define NAME (data__files_cnt > 2 ? buf : header)
1015 dfmt->header_width = width;
1016 width = (int) strlen(NAME);
1017 if (dfmt->header_width < width)
1018 dfmt->header_width = width;
1020 scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
1021 dfmt->header_width, NAME);
1023 dfmt->header = strdup(buf_indent);
1024 #undef MAX_HEADER_NAME
1028 static void data__hpp_register(struct data__file *d, int idx)
1030 struct diff_hpp_fmt *dfmt = &d->fmt[idx];
1031 struct perf_hpp_fmt *fmt = &dfmt->fmt;
1035 fmt->header = hpp__header;
1036 fmt->width = hpp__width;
1037 fmt->entry = hpp__entry_global;
1039 /* TODO more colors */
1041 case PERF_HPP_DIFF__BASELINE:
1042 fmt->color = hpp__color_baseline;
1044 case PERF_HPP_DIFF__DELTA:
1045 fmt->color = hpp__color_delta;
1047 case PERF_HPP_DIFF__RATIO:
1048 fmt->color = hpp__color_ratio;
1050 case PERF_HPP_DIFF__WEIGHTED_DIFF:
1051 fmt->color = hpp__color_wdiff;
1057 init_header(d, dfmt);
1058 perf_hpp__column_register(fmt);
1061 static void ui_init(void)
1063 struct data__file *d;
1066 data__for_each_file(i, d) {
1069 * Baseline or compute realted columns:
1071 * PERF_HPP_DIFF__BASELINE
1072 * PERF_HPP_DIFF__DELTA
1073 * PERF_HPP_DIFF__RATIO
1074 * PERF_HPP_DIFF__WEIGHTED_DIFF
1076 data__hpp_register(d, i ? compute_2_hpp[compute] :
1077 PERF_HPP_DIFF__BASELINE);
1082 * PERF_HPP_DIFF__FORMULA
1083 * PERF_HPP_DIFF__PERIOD
1084 * PERF_HPP_DIFF__PERIOD_BASELINE
1086 if (show_formula && i)
1087 data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
1090 data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
1091 PERF_HPP_DIFF__PERIOD_BASELINE);
1095 static int data_init(int argc, const char **argv)
1097 struct data__file *d;
1098 static const char *defaults[] = {
1102 bool use_default = true;
1105 data__files_cnt = 2;
1109 defaults[1] = argv[0];
1111 data__files_cnt = argc;
1112 use_default = false;
1114 } else if (perf_guest) {
1115 defaults[0] = "perf.data.host";
1116 defaults[1] = "perf.data.guest";
1119 if (sort_compute >= (unsigned int) data__files_cnt) {
1120 pr_err("Order option out of limit.\n");
1124 data__files = zalloc(sizeof(*data__files) * data__files_cnt);
1128 data__for_each_file(i, d) {
1129 struct perf_data_file *file = &d->file;
1131 file->path = use_default ? defaults[i] : argv[i];
1132 file->mode = PERF_DATA_MODE_READ,
1133 file->force = force,
1141 int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
1143 perf_config(perf_default_config, NULL);
1145 sort_order = diff__default_sort_order;
1146 argc = parse_options(argc, argv, options, diff_usage, 0);
1148 if (symbol__init() < 0)
1151 if (data_init(argc, argv) < 0)
1156 if (setup_sorting() < 0)
1157 usage_with_options(diff_usage, options);
1161 sort__setup_elide(NULL);
1163 return __cmd_diff();