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