builtin-diff.c 12 KB

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