builtin-diff.c 14 KB

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