builtin-diff.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 int hists__add_entry(struct hists *self,
  25. struct addr_location *al, u64 period)
  26. {
  27. if (__hists__add_entry(self, al, NULL, period) != NULL)
  28. return 0;
  29. return -ENOMEM;
  30. }
  31. static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
  32. union perf_event *event,
  33. struct perf_sample *sample,
  34. struct perf_evsel *evsel,
  35. struct machine *machine)
  36. {
  37. struct addr_location al;
  38. if (perf_event__preprocess_sample(event, machine, &al, sample, NULL) < 0) {
  39. pr_warning("problem processing %d event, skipping it.\n",
  40. event->header.type);
  41. return -1;
  42. }
  43. if (al.filtered || al.sym == NULL)
  44. return 0;
  45. if (hists__add_entry(&evsel->hists, &al, sample->period)) {
  46. pr_warning("problem incrementing symbol period, skipping event\n");
  47. return -1;
  48. }
  49. evsel->hists.stats.total_period += sample->period;
  50. return 0;
  51. }
  52. static struct perf_tool tool = {
  53. .sample = diff__process_sample_event,
  54. .mmap = perf_event__process_mmap,
  55. .comm = perf_event__process_comm,
  56. .exit = perf_event__process_task,
  57. .fork = perf_event__process_task,
  58. .lost = perf_event__process_lost,
  59. .ordered_samples = true,
  60. .ordering_requires_timestamps = true,
  61. };
  62. static void insert_hist_entry_by_name(struct rb_root *root,
  63. struct hist_entry *he)
  64. {
  65. struct rb_node **p = &root->rb_node;
  66. struct rb_node *parent = NULL;
  67. struct hist_entry *iter;
  68. while (*p != NULL) {
  69. parent = *p;
  70. iter = rb_entry(parent, struct hist_entry, rb_node);
  71. if (hist_entry__cmp(he, iter) < 0)
  72. p = &(*p)->rb_left;
  73. else
  74. p = &(*p)->rb_right;
  75. }
  76. rb_link_node(&he->rb_node, parent, p);
  77. rb_insert_color(&he->rb_node, root);
  78. }
  79. static void hists__name_resort(struct hists *self, bool sort)
  80. {
  81. unsigned long position = 1;
  82. struct rb_root tmp = RB_ROOT;
  83. struct rb_node *next = rb_first(&self->entries);
  84. while (next != NULL) {
  85. struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
  86. next = rb_next(&n->rb_node);
  87. n->position = position++;
  88. if (sort) {
  89. rb_erase(&n->rb_node, &self->entries);
  90. insert_hist_entry_by_name(&tmp, n);
  91. }
  92. }
  93. if (sort)
  94. self->entries = tmp;
  95. }
  96. static struct hist_entry *hists__find_entry(struct hists *self,
  97. struct hist_entry *he)
  98. {
  99. struct rb_node *n = self->entries.rb_node;
  100. while (n) {
  101. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
  102. int64_t cmp = hist_entry__cmp(he, iter);
  103. if (cmp < 0)
  104. n = n->rb_left;
  105. else if (cmp > 0)
  106. n = n->rb_right;
  107. else
  108. return iter;
  109. }
  110. return NULL;
  111. }
  112. static void hists__match(struct hists *older, struct hists *newer)
  113. {
  114. struct rb_node *nd;
  115. for (nd = rb_first(&newer->entries); nd; nd = rb_next(nd)) {
  116. struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
  117. pos->pair = hists__find_entry(older, pos);
  118. }
  119. }
  120. static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
  121. struct perf_evlist *evlist)
  122. {
  123. struct perf_evsel *e;
  124. list_for_each_entry(e, &evlist->entries, node)
  125. if (perf_evsel__match2(evsel, e))
  126. return e;
  127. return NULL;
  128. }
  129. static void perf_evlist__resort_hists(struct perf_evlist *evlist, bool name)
  130. {
  131. struct perf_evsel *evsel;
  132. list_for_each_entry(evsel, &evlist->entries, node) {
  133. struct hists *hists = &evsel->hists;
  134. hists__output_resort(hists);
  135. /*
  136. * The hists__name_resort only sets possition
  137. * if name is false.
  138. */
  139. if (name || ((!name) && show_displacement))
  140. hists__name_resort(hists, name);
  141. }
  142. }
  143. static int __cmd_diff(void)
  144. {
  145. int ret, i;
  146. #define older (session[0])
  147. #define newer (session[1])
  148. struct perf_session *session[2];
  149. struct perf_evlist *evlist_new, *evlist_old;
  150. struct perf_evsel *evsel;
  151. bool first = true;
  152. older = perf_session__new(input_old, O_RDONLY, force, false,
  153. &tool);
  154. newer = perf_session__new(input_new, O_RDONLY, force, false,
  155. &tool);
  156. if (session[0] == NULL || session[1] == NULL)
  157. return -ENOMEM;
  158. for (i = 0; i < 2; ++i) {
  159. ret = perf_session__process_events(session[i], &tool);
  160. if (ret)
  161. goto out_delete;
  162. }
  163. evlist_old = older->evlist;
  164. evlist_new = newer->evlist;
  165. perf_evlist__resort_hists(evlist_old, true);
  166. perf_evlist__resort_hists(evlist_new, false);
  167. list_for_each_entry(evsel, &evlist_new->entries, node) {
  168. struct perf_evsel *evsel_old;
  169. evsel_old = evsel_match(evsel, evlist_old);
  170. if (!evsel_old)
  171. continue;
  172. fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
  173. perf_evsel__name(evsel));
  174. first = false;
  175. hists__match(&evsel_old->hists, &evsel->hists);
  176. hists__fprintf(&evsel->hists, true, 0, 0, stdout);
  177. }
  178. out_delete:
  179. for (i = 0; i < 2; ++i)
  180. perf_session__delete(session[i]);
  181. return ret;
  182. #undef older
  183. #undef newer
  184. }
  185. static const char * const diff_usage[] = {
  186. "perf diff [<options>] [old_file] [new_file]",
  187. NULL,
  188. };
  189. static const struct option options[] = {
  190. OPT_INCR('v', "verbose", &verbose,
  191. "be more verbose (show symbol address, etc)"),
  192. OPT_BOOLEAN('M', "displacement", &show_displacement,
  193. "Show position displacement relative to baseline"),
  194. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  195. "dump raw trace in ASCII"),
  196. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  197. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  198. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  199. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  200. "only consider symbols in these dsos"),
  201. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  202. "only consider symbols in these comms"),
  203. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  204. "only consider these symbols"),
  205. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  206. "sort by key(s): pid, comm, dso, symbol, parent"),
  207. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  208. "separator for columns, no spaces will be added between "
  209. "columns '.' is reserved."),
  210. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  211. "Look for files with symbols relative to this directory"),
  212. OPT_END()
  213. };
  214. static void ui_init(void)
  215. {
  216. perf_hpp__init();
  217. /* No overhead column. */
  218. perf_hpp__column_enable(PERF_HPP__OVERHEAD, false);
  219. /* Display baseline/delta/displacement columns. */
  220. perf_hpp__column_enable(PERF_HPP__BASELINE, true);
  221. perf_hpp__column_enable(PERF_HPP__DELTA, true);
  222. if (show_displacement)
  223. perf_hpp__column_enable(PERF_HPP__DISPL, true);
  224. }
  225. int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
  226. {
  227. sort_order = diff__default_sort_order;
  228. argc = parse_options(argc, argv, options, diff_usage, 0);
  229. if (argc) {
  230. if (argc > 2)
  231. usage_with_options(diff_usage, options);
  232. if (argc == 2) {
  233. input_old = argv[0];
  234. input_new = argv[1];
  235. } else
  236. input_new = argv[0];
  237. } else if (symbol_conf.default_guest_vmlinux_name ||
  238. symbol_conf.default_guest_kallsyms) {
  239. input_old = "perf.data.host";
  240. input_new = "perf.data.guest";
  241. }
  242. symbol_conf.exclude_other = false;
  243. if (symbol__init() < 0)
  244. return -1;
  245. ui_init();
  246. setup_sorting(diff_usage, options);
  247. setup_pager();
  248. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
  249. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
  250. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
  251. return __cmd_diff();
  252. }