builtin-diff.c 6.6 KB

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