builtin-diff.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. #include "util/debug.h"
  9. #include "util/event.h"
  10. #include "util/hist.h"
  11. #include "util/evsel.h"
  12. #include "util/evlist.h"
  13. #include "util/session.h"
  14. #include "util/tool.h"
  15. #include "util/sort.h"
  16. #include "util/symbol.h"
  17. #include "util/util.h"
  18. #include <stdlib.h>
  19. static char const *input_old = "perf.data.old",
  20. *input_new = "perf.data";
  21. static char diff__default_sort_order[] = "dso,symbol";
  22. static bool force;
  23. static bool show_displacement;
  24. static bool show_baseline_only;
  25. static bool sort_compute;
  26. static s64 compute_wdiff_w1;
  27. static s64 compute_wdiff_w2;
  28. enum {
  29. COMPUTE_DELTA,
  30. COMPUTE_RATIO,
  31. COMPUTE_WEIGHTED_DIFF,
  32. COMPUTE_MAX,
  33. };
  34. const char *compute_names[COMPUTE_MAX] = {
  35. [COMPUTE_DELTA] = "delta",
  36. [COMPUTE_RATIO] = "ratio",
  37. [COMPUTE_WEIGHTED_DIFF] = "wdiff",
  38. };
  39. static int compute;
  40. static int setup_compute_opt_wdiff(char *opt)
  41. {
  42. char *w1_str = opt;
  43. char *w2_str;
  44. int ret = -EINVAL;
  45. if (!opt)
  46. goto out;
  47. w2_str = strchr(opt, ',');
  48. if (!w2_str)
  49. goto out;
  50. *w2_str++ = 0x0;
  51. if (!*w2_str)
  52. goto out;
  53. compute_wdiff_w1 = strtol(w1_str, NULL, 10);
  54. compute_wdiff_w2 = strtol(w2_str, NULL, 10);
  55. if (!compute_wdiff_w1 || !compute_wdiff_w2)
  56. goto out;
  57. pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
  58. compute_wdiff_w1, compute_wdiff_w2);
  59. ret = 0;
  60. out:
  61. if (ret)
  62. pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
  63. return ret;
  64. }
  65. static int setup_compute_opt(char *opt)
  66. {
  67. if (compute == COMPUTE_WEIGHTED_DIFF)
  68. return setup_compute_opt_wdiff(opt);
  69. if (opt) {
  70. pr_err("Failed: extra option specified '%s'", opt);
  71. return -EINVAL;
  72. }
  73. return 0;
  74. }
  75. static int setup_compute(const struct option *opt, const char *str,
  76. int unset __maybe_unused)
  77. {
  78. int *cp = (int *) opt->value;
  79. char *cstr = (char *) str;
  80. char buf[50];
  81. unsigned i;
  82. char *option;
  83. if (!str) {
  84. *cp = COMPUTE_DELTA;
  85. return 0;
  86. }
  87. if (*str == '+') {
  88. sort_compute = true;
  89. cstr = (char *) ++str;
  90. if (!*str)
  91. return 0;
  92. }
  93. option = strchr(str, ':');
  94. if (option) {
  95. unsigned len = option++ - str;
  96. /*
  97. * The str data are not writeable, so we need
  98. * to use another buffer.
  99. */
  100. /* No option value is longer. */
  101. if (len >= sizeof(buf))
  102. return -EINVAL;
  103. strncpy(buf, str, len);
  104. buf[len] = 0x0;
  105. cstr = buf;
  106. }
  107. for (i = 0; i < COMPUTE_MAX; i++)
  108. if (!strcmp(cstr, compute_names[i])) {
  109. *cp = i;
  110. return setup_compute_opt(option);
  111. }
  112. pr_err("Failed: '%s' is not computation method "
  113. "(use 'delta','ratio' or 'wdiff')\n", str);
  114. return -EINVAL;
  115. }
  116. static double get_period_percent(struct hist_entry *he, u64 period)
  117. {
  118. u64 total = he->hists->stats.total_period;
  119. return (period * 100.0) / total;
  120. }
  121. double perf_diff__compute_delta(struct hist_entry *he)
  122. {
  123. struct hist_entry *pair = he->pair;
  124. double new_percent = get_period_percent(he, he->stat.period);
  125. double old_percent = pair ? get_period_percent(pair, pair->stat.period) : 0.0;
  126. he->diff.period_ratio_delta = new_percent - old_percent;
  127. he->diff.computed = true;
  128. return he->diff.period_ratio_delta;
  129. }
  130. double perf_diff__compute_ratio(struct hist_entry *he)
  131. {
  132. struct hist_entry *pair = he->pair;
  133. double new_period = he->stat.period;
  134. double old_period = pair ? pair->stat.period : 0;
  135. he->diff.computed = true;
  136. he->diff.period_ratio = pair ? (new_period / old_period) : 0;
  137. return he->diff.period_ratio;
  138. }
  139. s64 perf_diff__compute_wdiff(struct hist_entry *he)
  140. {
  141. struct hist_entry *pair = he->pair;
  142. u64 new_period = he->stat.period;
  143. u64 old_period = pair ? pair->stat.period : 0;
  144. he->diff.computed = true;
  145. if (!pair)
  146. he->diff.wdiff = 0;
  147. else
  148. he->diff.wdiff = new_period * compute_wdiff_w2 -
  149. old_period * compute_wdiff_w1;
  150. return he->diff.wdiff;
  151. }
  152. static int hists__add_entry(struct hists *self,
  153. struct addr_location *al, u64 period)
  154. {
  155. if (__hists__add_entry(self, al, NULL, period) != NULL)
  156. return 0;
  157. return -ENOMEM;
  158. }
  159. static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
  160. union perf_event *event,
  161. struct perf_sample *sample,
  162. struct perf_evsel *evsel,
  163. struct machine *machine)
  164. {
  165. struct addr_location al;
  166. if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
  167. pr_warning("problem processing %d event, skipping it.\n",
  168. event->header.type);
  169. return -1;
  170. }
  171. if (al.filtered || al.sym == NULL)
  172. return 0;
  173. if (hists__add_entry(&evsel->hists, &al, sample->period)) {
  174. pr_warning("problem incrementing symbol period, skipping event\n");
  175. return -1;
  176. }
  177. evsel->hists.stats.total_period += sample->period;
  178. return 0;
  179. }
  180. static struct perf_tool tool = {
  181. .sample = diff__process_sample_event,
  182. .mmap = perf_event__process_mmap,
  183. .comm = perf_event__process_comm,
  184. .exit = perf_event__process_task,
  185. .fork = perf_event__process_task,
  186. .lost = perf_event__process_lost,
  187. .ordered_samples = true,
  188. .ordering_requires_timestamps = true,
  189. };
  190. static void insert_hist_entry_by_name(struct rb_root *root,
  191. struct hist_entry *he)
  192. {
  193. struct rb_node **p = &root->rb_node;
  194. struct rb_node *parent = NULL;
  195. struct hist_entry *iter;
  196. while (*p != NULL) {
  197. parent = *p;
  198. iter = rb_entry(parent, struct hist_entry, rb_node);
  199. if (hist_entry__cmp(he, iter) < 0)
  200. p = &(*p)->rb_left;
  201. else
  202. p = &(*p)->rb_right;
  203. }
  204. rb_link_node(&he->rb_node, parent, p);
  205. rb_insert_color(&he->rb_node, root);
  206. }
  207. static void hists__name_resort(struct hists *self, bool sort)
  208. {
  209. unsigned long position = 1;
  210. struct rb_root tmp = RB_ROOT;
  211. struct rb_node *next = rb_first(&self->entries);
  212. while (next != NULL) {
  213. struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
  214. next = rb_next(&n->rb_node);
  215. n->position = position++;
  216. if (sort) {
  217. rb_erase(&n->rb_node, &self->entries);
  218. insert_hist_entry_by_name(&tmp, n);
  219. }
  220. }
  221. if (sort)
  222. self->entries = tmp;
  223. }
  224. static struct hist_entry *hists__find_entry(struct hists *self,
  225. struct hist_entry *he)
  226. {
  227. struct rb_node *n = self->entries.rb_node;
  228. while (n) {
  229. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
  230. int64_t cmp = hist_entry__cmp(he, iter);
  231. if (cmp < 0)
  232. n = n->rb_left;
  233. else if (cmp > 0)
  234. n = n->rb_right;
  235. else
  236. return iter;
  237. }
  238. return NULL;
  239. }
  240. static void hists__match(struct hists *older, struct hists *newer)
  241. {
  242. struct rb_node *nd;
  243. for (nd = rb_first(&newer->entries); nd; nd = rb_next(nd)) {
  244. struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
  245. pos->pair = hists__find_entry(older, pos);
  246. }
  247. }
  248. static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
  249. struct perf_evlist *evlist)
  250. {
  251. struct perf_evsel *e;
  252. list_for_each_entry(e, &evlist->entries, node)
  253. if (perf_evsel__match2(evsel, e))
  254. return e;
  255. return NULL;
  256. }
  257. static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name)
  258. {
  259. struct perf_evsel *evsel;
  260. list_for_each_entry(evsel, &evlist->entries, node) {
  261. struct hists *hists = &evsel->hists;
  262. hists__output_resort(hists);
  263. /*
  264. * The hists__name_resort only sets possition
  265. * if name is false.
  266. */
  267. if (name || ((!name) && show_displacement))
  268. hists__name_resort(hists, name);
  269. }
  270. }
  271. static void hists__baseline_only(struct hists *hists)
  272. {
  273. struct rb_node *next = rb_first(&hists->entries);
  274. while (next != NULL) {
  275. struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
  276. next = rb_next(&he->rb_node);
  277. if (!he->pair) {
  278. rb_erase(&he->rb_node, &hists->entries);
  279. hist_entry__free(he);
  280. }
  281. }
  282. }
  283. static void hists__precompute(struct hists *hists)
  284. {
  285. struct rb_node *next = rb_first(&hists->entries);
  286. while (next != NULL) {
  287. struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
  288. next = rb_next(&he->rb_node);
  289. switch (compute) {
  290. case COMPUTE_DELTA:
  291. perf_diff__compute_delta(he);
  292. break;
  293. case COMPUTE_RATIO:
  294. perf_diff__compute_ratio(he);
  295. break;
  296. case COMPUTE_WEIGHTED_DIFF:
  297. perf_diff__compute_wdiff(he);
  298. break;
  299. default:
  300. BUG_ON(1);
  301. }
  302. }
  303. }
  304. static int64_t cmp_doubles(double l, double r)
  305. {
  306. if (l > r)
  307. return -1;
  308. else if (l < r)
  309. return 1;
  310. else
  311. return 0;
  312. }
  313. static int64_t
  314. hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  315. int c)
  316. {
  317. switch (c) {
  318. case COMPUTE_DELTA:
  319. {
  320. double l = left->diff.period_ratio_delta;
  321. double r = right->diff.period_ratio_delta;
  322. return cmp_doubles(l, r);
  323. }
  324. case COMPUTE_RATIO:
  325. {
  326. double l = left->diff.period_ratio;
  327. double r = right->diff.period_ratio;
  328. return cmp_doubles(l, r);
  329. }
  330. case COMPUTE_WEIGHTED_DIFF:
  331. {
  332. s64 l = left->diff.wdiff;
  333. s64 r = right->diff.wdiff;
  334. return r - l;
  335. }
  336. default:
  337. BUG_ON(1);
  338. }
  339. return 0;
  340. }
  341. static void insert_hist_entry_by_compute(struct rb_root *root,
  342. struct hist_entry *he,
  343. int c)
  344. {
  345. struct rb_node **p = &root->rb_node;
  346. struct rb_node *parent = NULL;
  347. struct hist_entry *iter;
  348. while (*p != NULL) {
  349. parent = *p;
  350. iter = rb_entry(parent, struct hist_entry, rb_node);
  351. if (hist_entry__cmp_compute(he, iter, c) < 0)
  352. p = &(*p)->rb_left;
  353. else
  354. p = &(*p)->rb_right;
  355. }
  356. rb_link_node(&he->rb_node, parent, p);
  357. rb_insert_color(&he->rb_node, root);
  358. }
  359. static void hists__compute_resort(struct hists *hists)
  360. {
  361. struct rb_root tmp = RB_ROOT;
  362. struct rb_node *next = rb_first(&hists->entries);
  363. while (next != NULL) {
  364. struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node);
  365. next = rb_next(&he->rb_node);
  366. rb_erase(&he->rb_node, &hists->entries);
  367. insert_hist_entry_by_compute(&tmp, he, compute);
  368. }
  369. hists->entries = tmp;
  370. }
  371. static void hists__process(struct hists *old, struct hists *new)
  372. {
  373. hists__match(old, new);
  374. if (show_baseline_only)
  375. hists__baseline_only(new);
  376. if (sort_compute) {
  377. hists__precompute(new);
  378. hists__compute_resort(new);
  379. }
  380. hists__fprintf(new, true, 0, 0, stdout);
  381. }
  382. static int __cmd_diff(void)
  383. {
  384. int ret, i;
  385. #define older (session[0])
  386. #define newer (session[1])
  387. struct perf_session *session[2];
  388. struct perf_evlist *evlist_new, *evlist_old;
  389. struct perf_evsel *evsel;
  390. bool first = true;
  391. older = perf_session__new(input_old, O_RDONLY, force, false,
  392. &tool);
  393. newer = perf_session__new(input_new, O_RDONLY, force, false,
  394. &tool);
  395. if (session[0] == NULL || session[1] == NULL)
  396. return -ENOMEM;
  397. for (i = 0; i < 2; ++i) {
  398. ret = perf_session__process_events(session[i], &tool);
  399. if (ret)
  400. goto out_delete;
  401. }
  402. evlist_old = older->evlist;
  403. evlist_new = newer->evlist;
  404. perf_evlist__resort_hists(evlist_old, true);
  405. perf_evlist__resort_hists(evlist_new, false);
  406. list_for_each_entry(evsel, &evlist_new->entries, node) {
  407. struct perf_evsel *evsel_old;
  408. evsel_old = evsel_match(evsel, evlist_old);
  409. if (!evsel_old)
  410. continue;
  411. fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
  412. perf_evsel__name(evsel));
  413. first = false;
  414. hists__process(&evsel_old->hists, &evsel->hists);
  415. }
  416. out_delete:
  417. for (i = 0; i < 2; ++i)
  418. perf_session__delete(session[i]);
  419. return ret;
  420. #undef older
  421. #undef newer
  422. }
  423. static const char * const diff_usage[] = {
  424. "perf diff [<options>] [old_file] [new_file]",
  425. NULL,
  426. };
  427. static const struct option options[] = {
  428. OPT_INCR('v', "verbose", &verbose,
  429. "be more verbose (show symbol address, etc)"),
  430. OPT_BOOLEAN('M', "displacement", &show_displacement,
  431. "Show position displacement relative to baseline"),
  432. OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
  433. "Show only items with match in baseline"),
  434. OPT_CALLBACK('c', "compute", &compute,
  435. "delta,ratio,wdiff:w1,w2 (default delta)",
  436. "Entries differential computation selection",
  437. setup_compute),
  438. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  439. "dump raw trace in ASCII"),
  440. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  441. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  442. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  443. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  444. "only consider symbols in these dsos"),
  445. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  446. "only consider symbols in these comms"),
  447. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  448. "only consider these symbols"),
  449. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  450. "sort by key(s): pid, comm, dso, symbol, parent"),
  451. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  452. "separator for columns, no spaces will be added between "
  453. "columns '.' is reserved."),
  454. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  455. "Look for files with symbols relative to this directory"),
  456. OPT_END()
  457. };
  458. static void ui_init(void)
  459. {
  460. perf_hpp__init();
  461. /* No overhead column. */
  462. perf_hpp__column_enable(PERF_HPP__OVERHEAD, false);
  463. /* Display baseline/delta/ratio/displacement columns. */
  464. perf_hpp__column_enable(PERF_HPP__BASELINE, true);
  465. switch (compute) {
  466. case COMPUTE_DELTA:
  467. perf_hpp__column_enable(PERF_HPP__DELTA, true);
  468. break;
  469. case COMPUTE_RATIO:
  470. perf_hpp__column_enable(PERF_HPP__RATIO, true);
  471. break;
  472. case COMPUTE_WEIGHTED_DIFF:
  473. perf_hpp__column_enable(PERF_HPP__WEIGHTED_DIFF, true);
  474. break;
  475. default:
  476. BUG_ON(1);
  477. };
  478. if (show_displacement)
  479. perf_hpp__column_enable(PERF_HPP__DISPL, true);
  480. }
  481. int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
  482. {
  483. sort_order = diff__default_sort_order;
  484. argc = parse_options(argc, argv, options, diff_usage, 0);
  485. if (argc) {
  486. if (argc > 2)
  487. usage_with_options(diff_usage, options);
  488. if (argc == 2) {
  489. input_old = argv[0];
  490. input_new = argv[1];
  491. } else
  492. input_new = argv[0];
  493. } else if (symbol_conf.default_guest_vmlinux_name ||
  494. symbol_conf.default_guest_kallsyms) {
  495. input_old = "perf.data.host";
  496. input_new = "perf.data.guest";
  497. }
  498. symbol_conf.exclude_other = false;
  499. if (symbol__init() < 0)
  500. return -1;
  501. ui_init();
  502. setup_sorting(diff_usage, options);
  503. setup_pager();
  504. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
  505. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
  506. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
  507. return __cmd_diff();
  508. }