builtin-diff.c 15 KB

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