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