builtin-diff.c 14 KB

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