builtin-diff.c 16 KB

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