Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 1 | /* |
| 2 | * builtin-diff.c |
| 3 | * |
| 4 | * Builtin diff command: Analyze two perf.data input files, look up and read |
| 5 | * DSOs and symbol information, sort them and produce a diff. |
| 6 | */ |
| 7 | #include "builtin.h" |
| 8 | |
| 9 | #include "util/debug.h" |
| 10 | #include "util/event.h" |
| 11 | #include "util/hist.h" |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 12 | #include "util/evsel.h" |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 13 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 14 | #include "util/session.h" |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 15 | #include "util/tool.h" |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 16 | #include "util/sort.h" |
| 17 | #include "util/symbol.h" |
| 18 | #include "util/util.h" |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 22 | static char const *input_old = "perf.data.old", |
| 23 | *input_new = "perf.data"; |
Arnaldo Carvalho de Melo | 604c5c9 | 2009-12-16 14:09:53 -0200 | [diff] [blame] | 24 | static char diff__default_sort_order[] = "dso,symbol"; |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 25 | static bool force; |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 26 | static bool show_displacement; |
Jiri Olsa | 61949b2 | 2012-10-05 16:44:44 +0200 | [diff] [blame] | 27 | static bool show_period; |
Jiri Olsa | ed279da | 2012-10-05 16:44:45 +0200 | [diff] [blame] | 28 | static bool show_formula; |
Jiri Olsa | a06d143 | 2012-10-05 16:44:40 +0200 | [diff] [blame] | 29 | static bool show_baseline_only; |
Jiri Olsa | 96c47f1 | 2012-10-05 16:44:42 +0200 | [diff] [blame] | 30 | static bool sort_compute; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 31 | |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 32 | static s64 compute_wdiff_w1; |
| 33 | static s64 compute_wdiff_w2; |
| 34 | |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 35 | enum { |
| 36 | COMPUTE_DELTA, |
| 37 | COMPUTE_RATIO, |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 38 | COMPUTE_WEIGHTED_DIFF, |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 39 | COMPUTE_MAX, |
| 40 | }; |
| 41 | |
| 42 | const char *compute_names[COMPUTE_MAX] = { |
| 43 | [COMPUTE_DELTA] = "delta", |
| 44 | [COMPUTE_RATIO] = "ratio", |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 45 | [COMPUTE_WEIGHTED_DIFF] = "wdiff", |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | static int compute; |
| 49 | |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 50 | static int setup_compute_opt_wdiff(char *opt) |
| 51 | { |
| 52 | char *w1_str = opt; |
| 53 | char *w2_str; |
| 54 | |
| 55 | int ret = -EINVAL; |
| 56 | |
| 57 | if (!opt) |
| 58 | goto out; |
| 59 | |
| 60 | w2_str = strchr(opt, ','); |
| 61 | if (!w2_str) |
| 62 | goto out; |
| 63 | |
| 64 | *w2_str++ = 0x0; |
| 65 | if (!*w2_str) |
| 66 | goto out; |
| 67 | |
| 68 | compute_wdiff_w1 = strtol(w1_str, NULL, 10); |
| 69 | compute_wdiff_w2 = strtol(w2_str, NULL, 10); |
| 70 | |
| 71 | if (!compute_wdiff_w1 || !compute_wdiff_w2) |
| 72 | goto out; |
| 73 | |
| 74 | pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n", |
| 75 | compute_wdiff_w1, compute_wdiff_w2); |
| 76 | |
| 77 | ret = 0; |
| 78 | |
| 79 | out: |
| 80 | if (ret) |
| 81 | pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n"); |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | static int setup_compute_opt(char *opt) |
| 87 | { |
| 88 | if (compute == COMPUTE_WEIGHTED_DIFF) |
| 89 | return setup_compute_opt_wdiff(opt); |
| 90 | |
| 91 | if (opt) { |
| 92 | pr_err("Failed: extra option specified '%s'", opt); |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 99 | static int setup_compute(const struct option *opt, const char *str, |
| 100 | int unset __maybe_unused) |
| 101 | { |
| 102 | int *cp = (int *) opt->value; |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 103 | char *cstr = (char *) str; |
| 104 | char buf[50]; |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 105 | unsigned i; |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 106 | char *option; |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 107 | |
| 108 | if (!str) { |
| 109 | *cp = COMPUTE_DELTA; |
| 110 | return 0; |
| 111 | } |
| 112 | |
Jiri Olsa | 96c47f1 | 2012-10-05 16:44:42 +0200 | [diff] [blame] | 113 | if (*str == '+') { |
| 114 | sort_compute = true; |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 115 | cstr = (char *) ++str; |
Jiri Olsa | 96c47f1 | 2012-10-05 16:44:42 +0200 | [diff] [blame] | 116 | if (!*str) |
| 117 | return 0; |
| 118 | } |
| 119 | |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 120 | option = strchr(str, ':'); |
| 121 | if (option) { |
| 122 | unsigned len = option++ - str; |
| 123 | |
| 124 | /* |
| 125 | * The str data are not writeable, so we need |
| 126 | * to use another buffer. |
| 127 | */ |
| 128 | |
| 129 | /* No option value is longer. */ |
| 130 | if (len >= sizeof(buf)) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | strncpy(buf, str, len); |
| 134 | buf[len] = 0x0; |
| 135 | cstr = buf; |
| 136 | } |
| 137 | |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 138 | for (i = 0; i < COMPUTE_MAX; i++) |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 139 | if (!strcmp(cstr, compute_names[i])) { |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 140 | *cp = i; |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 141 | return setup_compute_opt(option); |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | pr_err("Failed: '%s' is not computation method " |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 145 | "(use 'delta','ratio' or 'wdiff')\n", str); |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 146 | return -EINVAL; |
| 147 | } |
| 148 | |
Jiri Olsa | 96c47f1 | 2012-10-05 16:44:42 +0200 | [diff] [blame] | 149 | static double get_period_percent(struct hist_entry *he, u64 period) |
| 150 | { |
| 151 | u64 total = he->hists->stats.total_period; |
| 152 | return (period * 100.0) / total; |
| 153 | } |
| 154 | |
| 155 | double perf_diff__compute_delta(struct hist_entry *he) |
| 156 | { |
| 157 | struct hist_entry *pair = he->pair; |
| 158 | double new_percent = get_period_percent(he, he->stat.period); |
| 159 | double old_percent = pair ? get_period_percent(pair, pair->stat.period) : 0.0; |
| 160 | |
| 161 | he->diff.period_ratio_delta = new_percent - old_percent; |
| 162 | he->diff.computed = true; |
| 163 | return he->diff.period_ratio_delta; |
| 164 | } |
| 165 | |
| 166 | double perf_diff__compute_ratio(struct hist_entry *he) |
| 167 | { |
| 168 | struct hist_entry *pair = he->pair; |
| 169 | double new_period = he->stat.period; |
| 170 | double old_period = pair ? pair->stat.period : 0; |
| 171 | |
| 172 | he->diff.computed = true; |
| 173 | he->diff.period_ratio = pair ? (new_period / old_period) : 0; |
| 174 | return he->diff.period_ratio; |
| 175 | } |
| 176 | |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 177 | s64 perf_diff__compute_wdiff(struct hist_entry *he) |
| 178 | { |
| 179 | struct hist_entry *pair = he->pair; |
| 180 | u64 new_period = he->stat.period; |
| 181 | u64 old_period = pair ? pair->stat.period : 0; |
| 182 | |
| 183 | he->diff.computed = true; |
| 184 | |
| 185 | if (!pair) |
| 186 | he->diff.wdiff = 0; |
| 187 | else |
| 188 | he->diff.wdiff = new_period * compute_wdiff_w2 - |
| 189 | old_period * compute_wdiff_w1; |
| 190 | |
| 191 | return he->diff.wdiff; |
| 192 | } |
| 193 | |
Jiri Olsa | ed279da | 2012-10-05 16:44:45 +0200 | [diff] [blame] | 194 | static int formula_delta(struct hist_entry *he, char *buf, size_t size) |
| 195 | { |
| 196 | struct hist_entry *pair = he->pair; |
| 197 | |
| 198 | if (!pair) |
| 199 | return -1; |
| 200 | |
| 201 | return scnprintf(buf, size, |
| 202 | "(%" PRIu64 " * 100 / %" PRIu64 ") - " |
| 203 | "(%" PRIu64 " * 100 / %" PRIu64 ")", |
| 204 | he->stat.period, he->hists->stats.total_period, |
| 205 | pair->stat.period, pair->hists->stats.total_period); |
| 206 | } |
| 207 | |
| 208 | static int formula_ratio(struct hist_entry *he, char *buf, size_t size) |
| 209 | { |
| 210 | struct hist_entry *pair = he->pair; |
| 211 | double new_period = he->stat.period; |
| 212 | double old_period = pair ? pair->stat.period : 0; |
| 213 | |
| 214 | if (!pair) |
| 215 | return -1; |
| 216 | |
| 217 | return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period); |
| 218 | } |
| 219 | |
| 220 | static int formula_wdiff(struct hist_entry *he, char *buf, size_t size) |
| 221 | { |
| 222 | struct hist_entry *pair = he->pair; |
| 223 | u64 new_period = he->stat.period; |
| 224 | u64 old_period = pair ? pair->stat.period : 0; |
| 225 | |
| 226 | if (!pair) |
| 227 | return -1; |
| 228 | |
| 229 | return scnprintf(buf, size, |
| 230 | "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")", |
| 231 | new_period, compute_wdiff_w2, old_period, compute_wdiff_w1); |
| 232 | } |
| 233 | |
| 234 | int perf_diff__formula(char *buf, size_t size, struct hist_entry *he) |
| 235 | { |
| 236 | switch (compute) { |
| 237 | case COMPUTE_DELTA: |
| 238 | return formula_delta(he, buf, size); |
| 239 | case COMPUTE_RATIO: |
| 240 | return formula_ratio(he, buf, size); |
| 241 | case COMPUTE_WEIGHTED_DIFF: |
| 242 | return formula_wdiff(he, buf, size); |
| 243 | default: |
| 244 | BUG_ON(1); |
| 245 | } |
| 246 | |
| 247 | return -1; |
| 248 | } |
| 249 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 250 | static int hists__add_entry(struct hists *self, |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 251 | struct addr_location *al, u64 period) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 252 | { |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 253 | if (__hists__add_entry(self, al, NULL, period) != NULL) |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 254 | return 0; |
| 255 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 256 | } |
| 257 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 258 | static int diff__process_sample_event(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 259 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8d50e5b | 2011-01-29 13:02:00 -0200 | [diff] [blame] | 260 | struct perf_sample *sample, |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 261 | struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 262 | struct machine *machine) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 263 | { |
| 264 | struct addr_location al; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 265 | |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 266 | if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) { |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 267 | pr_warning("problem processing %d event, skipping it.\n", |
| 268 | event->header.type); |
| 269 | return -1; |
| 270 | } |
| 271 | |
Jiri Olsa | d88c48f | 2012-10-05 16:44:46 +0200 | [diff] [blame] | 272 | if (al.filtered) |
Arnaldo Carvalho de Melo | c410a33 | 2009-12-15 20:04:41 -0200 | [diff] [blame] | 273 | return 0; |
| 274 | |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 275 | if (hists__add_entry(&evsel->hists, &al, sample->period)) { |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 276 | pr_warning("problem incrementing symbol period, skipping event\n"); |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 277 | return -1; |
| 278 | } |
| 279 | |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 280 | evsel->hists.stats.total_period += sample->period; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 281 | return 0; |
| 282 | } |
| 283 | |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 284 | static struct perf_tool tool = { |
| 285 | .sample = diff__process_sample_event, |
| 286 | .mmap = perf_event__process_mmap, |
| 287 | .comm = perf_event__process_comm, |
Arnaldo Carvalho de Melo | f62d3f0 | 2012-10-06 15:44:59 -0300 | [diff] [blame] | 288 | .exit = perf_event__process_exit, |
| 289 | .fork = perf_event__process_fork, |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 290 | .lost = perf_event__process_lost, |
| 291 | .ordered_samples = true, |
| 292 | .ordering_requires_timestamps = true, |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 293 | }; |
| 294 | |
Jiri Olsa | dd46434 | 2012-10-04 21:49:36 +0900 | [diff] [blame] | 295 | static void insert_hist_entry_by_name(struct rb_root *root, |
| 296 | struct hist_entry *he) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 297 | { |
| 298 | struct rb_node **p = &root->rb_node; |
| 299 | struct rb_node *parent = NULL; |
| 300 | struct hist_entry *iter; |
| 301 | |
| 302 | while (*p != NULL) { |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 303 | parent = *p; |
| 304 | iter = rb_entry(parent, struct hist_entry, rb_node); |
Arnaldo Carvalho de Melo | 9c443df | 2009-12-28 22:48:36 -0200 | [diff] [blame] | 305 | if (hist_entry__cmp(he, iter) < 0) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 306 | p = &(*p)->rb_left; |
Arnaldo Carvalho de Melo | 9c443df | 2009-12-28 22:48:36 -0200 | [diff] [blame] | 307 | else |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 308 | p = &(*p)->rb_right; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | rb_link_node(&he->rb_node, parent, p); |
| 312 | rb_insert_color(&he->rb_node, root); |
| 313 | } |
| 314 | |
Jiri Olsa | dd46434 | 2012-10-04 21:49:36 +0900 | [diff] [blame] | 315 | static void hists__name_resort(struct hists *self, bool sort) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 316 | { |
| 317 | unsigned long position = 1; |
| 318 | struct rb_root tmp = RB_ROOT; |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 319 | struct rb_node *next = rb_first(&self->entries); |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 320 | |
| 321 | while (next != NULL) { |
| 322 | struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node); |
| 323 | |
| 324 | next = rb_next(&n->rb_node); |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 325 | n->position = position++; |
Jiri Olsa | dd46434 | 2012-10-04 21:49:36 +0900 | [diff] [blame] | 326 | |
| 327 | if (sort) { |
| 328 | rb_erase(&n->rb_node, &self->entries); |
| 329 | insert_hist_entry_by_name(&tmp, n); |
| 330 | } |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 331 | } |
| 332 | |
Jiri Olsa | dd46434 | 2012-10-04 21:49:36 +0900 | [diff] [blame] | 333 | if (sort) |
| 334 | self->entries = tmp; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 335 | } |
| 336 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 337 | static struct hist_entry *hists__find_entry(struct hists *self, |
| 338 | struct hist_entry *he) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 339 | { |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 340 | struct rb_node *n = self->entries.rb_node; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 341 | |
| 342 | while (n) { |
| 343 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node); |
Arnaldo Carvalho de Melo | 9c443df | 2009-12-28 22:48:36 -0200 | [diff] [blame] | 344 | int64_t cmp = hist_entry__cmp(he, iter); |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 345 | |
Arnaldo Carvalho de Melo | 9c443df | 2009-12-28 22:48:36 -0200 | [diff] [blame] | 346 | if (cmp < 0) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 347 | n = n->rb_left; |
Arnaldo Carvalho de Melo | 9c443df | 2009-12-28 22:48:36 -0200 | [diff] [blame] | 348 | else if (cmp > 0) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 349 | n = n->rb_right; |
Jiri Olsa | dd46434 | 2012-10-04 21:49:36 +0900 | [diff] [blame] | 350 | else |
Arnaldo Carvalho de Melo | 9c443df | 2009-12-28 22:48:36 -0200 | [diff] [blame] | 351 | return iter; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | return NULL; |
| 355 | } |
| 356 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 357 | static void hists__match(struct hists *older, struct hists *newer) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 358 | { |
| 359 | struct rb_node *nd; |
| 360 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 361 | for (nd = rb_first(&newer->entries); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 362 | struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node); |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 363 | pos->pair = hists__find_entry(older, pos); |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 367 | static struct perf_evsel *evsel_match(struct perf_evsel *evsel, |
| 368 | struct perf_evlist *evlist) |
| 369 | { |
| 370 | struct perf_evsel *e; |
| 371 | |
| 372 | list_for_each_entry(e, &evlist->entries, node) |
| 373 | if (perf_evsel__match2(evsel, e)) |
| 374 | return e; |
| 375 | |
| 376 | return NULL; |
| 377 | } |
| 378 | |
Jiri Olsa | dd46434 | 2012-10-04 21:49:36 +0900 | [diff] [blame] | 379 | static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name) |
| 380 | { |
| 381 | struct perf_evsel *evsel; |
| 382 | |
| 383 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 384 | struct hists *hists = &evsel->hists; |
| 385 | |
| 386 | hists__output_resort(hists); |
| 387 | |
| 388 | /* |
| 389 | * The hists__name_resort only sets possition |
| 390 | * if name is false. |
| 391 | */ |
| 392 | if (name || ((!name) && show_displacement)) |
| 393 | hists__name_resort(hists, name); |
| 394 | } |
| 395 | } |
| 396 | |
Jiri Olsa | a06d143 | 2012-10-05 16:44:40 +0200 | [diff] [blame] | 397 | static void hists__baseline_only(struct hists *hists) |
| 398 | { |
| 399 | struct rb_node *next = rb_first(&hists->entries); |
| 400 | |
| 401 | while (next != NULL) { |
| 402 | struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node); |
| 403 | |
| 404 | next = rb_next(&he->rb_node); |
| 405 | if (!he->pair) { |
| 406 | rb_erase(&he->rb_node, &hists->entries); |
| 407 | hist_entry__free(he); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
Jiri Olsa | 96c47f1 | 2012-10-05 16:44:42 +0200 | [diff] [blame] | 412 | static void hists__precompute(struct hists *hists) |
| 413 | { |
| 414 | struct rb_node *next = rb_first(&hists->entries); |
| 415 | |
| 416 | while (next != NULL) { |
| 417 | struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node); |
| 418 | |
| 419 | next = rb_next(&he->rb_node); |
| 420 | |
| 421 | switch (compute) { |
| 422 | case COMPUTE_DELTA: |
| 423 | perf_diff__compute_delta(he); |
| 424 | break; |
| 425 | case COMPUTE_RATIO: |
| 426 | perf_diff__compute_ratio(he); |
| 427 | break; |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 428 | case COMPUTE_WEIGHTED_DIFF: |
| 429 | perf_diff__compute_wdiff(he); |
| 430 | break; |
Jiri Olsa | 96c47f1 | 2012-10-05 16:44:42 +0200 | [diff] [blame] | 431 | default: |
| 432 | BUG_ON(1); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | static int64_t cmp_doubles(double l, double r) |
| 438 | { |
| 439 | if (l > r) |
| 440 | return -1; |
| 441 | else if (l < r) |
| 442 | return 1; |
| 443 | else |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int64_t |
| 448 | hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right, |
| 449 | int c) |
| 450 | { |
| 451 | switch (c) { |
| 452 | case COMPUTE_DELTA: |
| 453 | { |
| 454 | double l = left->diff.period_ratio_delta; |
| 455 | double r = right->diff.period_ratio_delta; |
| 456 | |
| 457 | return cmp_doubles(l, r); |
| 458 | } |
| 459 | case COMPUTE_RATIO: |
| 460 | { |
| 461 | double l = left->diff.period_ratio; |
| 462 | double r = right->diff.period_ratio; |
| 463 | |
| 464 | return cmp_doubles(l, r); |
| 465 | } |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 466 | case COMPUTE_WEIGHTED_DIFF: |
| 467 | { |
| 468 | s64 l = left->diff.wdiff; |
| 469 | s64 r = right->diff.wdiff; |
| 470 | |
| 471 | return r - l; |
| 472 | } |
Jiri Olsa | 96c47f1 | 2012-10-05 16:44:42 +0200 | [diff] [blame] | 473 | default: |
| 474 | BUG_ON(1); |
| 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static void insert_hist_entry_by_compute(struct rb_root *root, |
| 481 | struct hist_entry *he, |
| 482 | int c) |
| 483 | { |
| 484 | struct rb_node **p = &root->rb_node; |
| 485 | struct rb_node *parent = NULL; |
| 486 | struct hist_entry *iter; |
| 487 | |
| 488 | while (*p != NULL) { |
| 489 | parent = *p; |
| 490 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 491 | if (hist_entry__cmp_compute(he, iter, c) < 0) |
| 492 | p = &(*p)->rb_left; |
| 493 | else |
| 494 | p = &(*p)->rb_right; |
| 495 | } |
| 496 | |
| 497 | rb_link_node(&he->rb_node, parent, p); |
| 498 | rb_insert_color(&he->rb_node, root); |
| 499 | } |
| 500 | |
| 501 | static void hists__compute_resort(struct hists *hists) |
| 502 | { |
| 503 | struct rb_root tmp = RB_ROOT; |
| 504 | struct rb_node *next = rb_first(&hists->entries); |
| 505 | |
| 506 | while (next != NULL) { |
| 507 | struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node); |
| 508 | |
| 509 | next = rb_next(&he->rb_node); |
| 510 | |
| 511 | rb_erase(&he->rb_node, &hists->entries); |
| 512 | insert_hist_entry_by_compute(&tmp, he, compute); |
| 513 | } |
| 514 | |
| 515 | hists->entries = tmp; |
| 516 | } |
| 517 | |
Jiri Olsa | a06d143 | 2012-10-05 16:44:40 +0200 | [diff] [blame] | 518 | static void hists__process(struct hists *old, struct hists *new) |
| 519 | { |
| 520 | hists__match(old, new); |
| 521 | |
| 522 | if (show_baseline_only) |
| 523 | hists__baseline_only(new); |
| 524 | |
Jiri Olsa | 96c47f1 | 2012-10-05 16:44:42 +0200 | [diff] [blame] | 525 | if (sort_compute) { |
| 526 | hists__precompute(new); |
| 527 | hists__compute_resort(new); |
| 528 | } |
| 529 | |
Jiri Olsa | a06d143 | 2012-10-05 16:44:40 +0200 | [diff] [blame] | 530 | hists__fprintf(new, true, 0, 0, stdout); |
| 531 | } |
| 532 | |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 533 | static int __cmd_diff(void) |
| 534 | { |
| 535 | int ret, i; |
Jiri Olsa | 4bf9ce1 | 2012-03-22 14:37:26 +0100 | [diff] [blame] | 536 | #define older (session[0]) |
| 537 | #define newer (session[1]) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 538 | struct perf_session *session[2]; |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 539 | struct perf_evlist *evlist_new, *evlist_old; |
| 540 | struct perf_evsel *evsel; |
| 541 | bool first = true; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 542 | |
Jiri Olsa | 4bf9ce1 | 2012-03-22 14:37:26 +0100 | [diff] [blame] | 543 | older = perf_session__new(input_old, O_RDONLY, force, false, |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 544 | &tool); |
Jiri Olsa | 4bf9ce1 | 2012-03-22 14:37:26 +0100 | [diff] [blame] | 545 | newer = perf_session__new(input_new, O_RDONLY, force, false, |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 546 | &tool); |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 547 | if (session[0] == NULL || session[1] == NULL) |
| 548 | return -ENOMEM; |
| 549 | |
| 550 | for (i = 0; i < 2; ++i) { |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 551 | ret = perf_session__process_events(session[i], &tool); |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 552 | if (ret) |
| 553 | goto out_delete; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 554 | } |
| 555 | |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 556 | evlist_old = older->evlist; |
| 557 | evlist_new = newer->evlist; |
Arnaldo Carvalho de Melo | 9c443df | 2009-12-28 22:48:36 -0200 | [diff] [blame] | 558 | |
Jiri Olsa | dd46434 | 2012-10-04 21:49:36 +0900 | [diff] [blame] | 559 | perf_evlist__resort_hists(evlist_old, true); |
| 560 | perf_evlist__resort_hists(evlist_new, false); |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 561 | |
| 562 | list_for_each_entry(evsel, &evlist_new->entries, node) { |
| 563 | struct perf_evsel *evsel_old; |
| 564 | |
| 565 | evsel_old = evsel_match(evsel, evlist_old); |
| 566 | if (!evsel_old) |
| 567 | continue; |
| 568 | |
| 569 | fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n", |
| 570 | perf_evsel__name(evsel)); |
| 571 | |
| 572 | first = false; |
| 573 | |
Jiri Olsa | a06d143 | 2012-10-05 16:44:40 +0200 | [diff] [blame] | 574 | hists__process(&evsel_old->hists, &evsel->hists); |
Jiri Olsa | 863e451 | 2012-09-06 17:46:55 +0200 | [diff] [blame] | 575 | } |
| 576 | |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 577 | out_delete: |
| 578 | for (i = 0; i < 2; ++i) |
| 579 | perf_session__delete(session[i]); |
| 580 | return ret; |
Jiri Olsa | 4bf9ce1 | 2012-03-22 14:37:26 +0100 | [diff] [blame] | 581 | #undef older |
| 582 | #undef newer |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 583 | } |
| 584 | |
Arnaldo Carvalho de Melo | 0422a4f | 2009-12-18 16:35:58 -0200 | [diff] [blame] | 585 | static const char * const diff_usage[] = { |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 586 | "perf diff [<options>] [old_file] [new_file]", |
Arnaldo Carvalho de Melo | 0422a4f | 2009-12-18 16:35:58 -0200 | [diff] [blame] | 587 | NULL, |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 588 | }; |
| 589 | |
| 590 | static const struct option options[] = { |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 591 | OPT_INCR('v', "verbose", &verbose, |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 592 | "be more verbose (show symbol address, etc)"), |
Shawn Bohrer | 3429555 | 2010-11-30 19:57:11 -0600 | [diff] [blame] | 593 | OPT_BOOLEAN('M', "displacement", &show_displacement, |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 594 | "Show position displacement relative to baseline"), |
Jiri Olsa | a06d143 | 2012-10-05 16:44:40 +0200 | [diff] [blame] | 595 | OPT_BOOLEAN('b', "baseline-only", &show_baseline_only, |
| 596 | "Show only items with match in baseline"), |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 597 | OPT_CALLBACK('c', "compute", &compute, |
| 598 | "delta,ratio,wdiff:w1,w2 (default delta)", |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 599 | "Entries differential computation selection", |
| 600 | setup_compute), |
Jiri Olsa | 61949b2 | 2012-10-05 16:44:44 +0200 | [diff] [blame] | 601 | OPT_BOOLEAN('p', "period", &show_period, |
| 602 | "Show period values."), |
Jiri Olsa | ed279da | 2012-10-05 16:44:45 +0200 | [diff] [blame] | 603 | OPT_BOOLEAN('F', "formula", &show_formula, |
| 604 | "Show formula."), |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 605 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
| 606 | "dump raw trace in ASCII"), |
| 607 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
| 608 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, |
| 609 | "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] | 610 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
| 611 | "only consider symbols in these dsos"), |
| 612 | OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", |
| 613 | "only consider symbols in these comms"), |
| 614 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", |
| 615 | "only consider these symbols"), |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 616 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
| 617 | "sort by key(s): pid, comm, dso, symbol, parent"), |
| 618 | OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator", |
| 619 | "separator for columns, no spaces will be added between " |
| 620 | "columns '.' is reserved."), |
David Ahern | ec5761e | 2010-12-09 13:27:07 -0700 | [diff] [blame] | 621 | OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", |
| 622 | "Look for files with symbols relative to this directory"), |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 623 | OPT_END() |
| 624 | }; |
| 625 | |
Jiri Olsa | 1d77822 | 2012-10-04 21:49:39 +0900 | [diff] [blame] | 626 | static void ui_init(void) |
| 627 | { |
| 628 | perf_hpp__init(); |
| 629 | |
| 630 | /* No overhead column. */ |
| 631 | perf_hpp__column_enable(PERF_HPP__OVERHEAD, false); |
| 632 | |
Jiri Olsa | ed279da | 2012-10-05 16:44:45 +0200 | [diff] [blame] | 633 | /* |
| 634 | * Display baseline/delta/ratio/displacement/ |
| 635 | * formula/periods columns. |
| 636 | */ |
Jiri Olsa | 1d77822 | 2012-10-04 21:49:39 +0900 | [diff] [blame] | 637 | perf_hpp__column_enable(PERF_HPP__BASELINE, true); |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 638 | |
| 639 | switch (compute) { |
| 640 | case COMPUTE_DELTA: |
| 641 | perf_hpp__column_enable(PERF_HPP__DELTA, true); |
| 642 | break; |
| 643 | case COMPUTE_RATIO: |
| 644 | perf_hpp__column_enable(PERF_HPP__RATIO, true); |
| 645 | break; |
Jiri Olsa | 81d5f95 | 2012-10-05 16:44:43 +0200 | [diff] [blame] | 646 | case COMPUTE_WEIGHTED_DIFF: |
| 647 | perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF, true); |
| 648 | break; |
Jiri Olsa | 7aaf6b3 | 2012-10-05 16:44:41 +0200 | [diff] [blame] | 649 | default: |
| 650 | BUG_ON(1); |
| 651 | }; |
Jiri Olsa | 1d77822 | 2012-10-04 21:49:39 +0900 | [diff] [blame] | 652 | |
| 653 | if (show_displacement) |
| 654 | perf_hpp__column_enable(PERF_HPP__DISPL, true); |
Jiri Olsa | 61949b2 | 2012-10-05 16:44:44 +0200 | [diff] [blame] | 655 | |
Jiri Olsa | ed279da | 2012-10-05 16:44:45 +0200 | [diff] [blame] | 656 | if (show_formula) |
| 657 | perf_hpp__column_enable(PERF_HPP__FORMULA, true); |
| 658 | |
Jiri Olsa | 61949b2 | 2012-10-05 16:44:44 +0200 | [diff] [blame] | 659 | if (show_period) { |
| 660 | perf_hpp__column_enable(PERF_HPP__PERIOD, true); |
| 661 | perf_hpp__column_enable(PERF_HPP__PERIOD_BASELINE, true); |
| 662 | } |
Jiri Olsa | 1d77822 | 2012-10-04 21:49:39 +0900 | [diff] [blame] | 663 | } |
| 664 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 665 | int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused) |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 666 | { |
Arnaldo Carvalho de Melo | 604c5c9 | 2009-12-16 14:09:53 -0200 | [diff] [blame] | 667 | sort_order = diff__default_sort_order; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 668 | argc = parse_options(argc, argv, options, diff_usage, 0); |
| 669 | if (argc) { |
| 670 | if (argc > 2) |
| 671 | usage_with_options(diff_usage, options); |
| 672 | if (argc == 2) { |
| 673 | input_old = argv[0]; |
| 674 | input_new = argv[1]; |
| 675 | } else |
| 676 | input_new = argv[0]; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 677 | } else if (symbol_conf.default_guest_vmlinux_name || |
| 678 | symbol_conf.default_guest_kallsyms) { |
| 679 | input_old = "perf.data.host"; |
| 680 | input_new = "perf.data.guest"; |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 681 | } |
| 682 | |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 683 | symbol_conf.exclude_other = false; |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 684 | if (symbol__init() < 0) |
| 685 | return -1; |
| 686 | |
Jiri Olsa | 1d77822 | 2012-10-04 21:49:39 +0900 | [diff] [blame] | 687 | ui_init(); |
| 688 | |
Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 689 | setup_sorting(diff_usage, options); |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 690 | setup_pager(); |
Arnaldo Carvalho de Melo | c351c28 | 2009-12-16 13:49:27 -0200 | [diff] [blame] | 691 | |
| 692 | sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL); |
| 693 | sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL); |
| 694 | sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL); |
| 695 | |
Arnaldo Carvalho de Melo | 86a9eee | 2009-12-14 20:09:31 -0200 | [diff] [blame] | 696 | return __cmd_diff(); |
| 697 | } |