builtin-diff.c 6.4 KB

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