builtin-diff.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 perf_session__add_hist_entry(struct perf_session *self,
  22. struct addr_location *al, u64 count)
  23. {
  24. if (__perf_session__add_hist_entry(&self->hists, al, NULL, count) != NULL)
  25. return 0;
  26. return -ENOMEM;
  27. }
  28. static int diff__process_sample_event(event_t *event, struct perf_session *session)
  29. {
  30. struct addr_location al;
  31. struct sample_data data = { .period = 1, };
  32. dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
  33. event->ip.pid, event->ip.ip);
  34. if (event__preprocess_sample(event, session, &al, NULL) < 0) {
  35. pr_warning("problem processing %d event, skipping it.\n",
  36. event->header.type);
  37. return -1;
  38. }
  39. if (al.filtered || al.sym == NULL)
  40. return 0;
  41. event__parse_sample(event, session->sample_type, &data);
  42. if (perf_session__add_hist_entry(session, &al, data.period)) {
  43. pr_warning("problem incrementing symbol count, skipping event\n");
  44. return -1;
  45. }
  46. session->events_stats.total += data.period;
  47. return 0;
  48. }
  49. static struct perf_event_ops event_ops = {
  50. .sample = diff__process_sample_event,
  51. .mmap = event__process_mmap,
  52. .comm = event__process_comm,
  53. .exit = event__process_task,
  54. .fork = event__process_task,
  55. .lost = event__process_lost,
  56. };
  57. static void perf_session__insert_hist_entry_by_name(struct rb_root *root,
  58. struct hist_entry *he)
  59. {
  60. struct rb_node **p = &root->rb_node;
  61. struct rb_node *parent = NULL;
  62. struct hist_entry *iter;
  63. while (*p != NULL) {
  64. parent = *p;
  65. iter = rb_entry(parent, struct hist_entry, rb_node);
  66. if (hist_entry__cmp(he, iter) < 0)
  67. p = &(*p)->rb_left;
  68. else
  69. p = &(*p)->rb_right;
  70. }
  71. rb_link_node(&he->rb_node, parent, p);
  72. rb_insert_color(&he->rb_node, root);
  73. }
  74. static void perf_session__resort_hist_entries(struct perf_session *self)
  75. {
  76. unsigned long position = 1;
  77. struct rb_root tmp = RB_ROOT;
  78. struct rb_node *next = rb_first(&self->hists);
  79. while (next != NULL) {
  80. struct hist_entry *n = rb_entry(next, struct hist_entry, rb_node);
  81. next = rb_next(&n->rb_node);
  82. rb_erase(&n->rb_node, &self->hists);
  83. n->position = position++;
  84. perf_session__insert_hist_entry_by_name(&tmp, n);
  85. }
  86. self->hists = tmp;
  87. }
  88. static void perf_session__set_hist_entries_positions(struct perf_session *self)
  89. {
  90. perf_session__output_resort(&self->hists, self->events_stats.total);
  91. perf_session__resort_hist_entries(self);
  92. }
  93. static struct hist_entry *
  94. perf_session__find_hist_entry(struct perf_session *self,
  95. struct hist_entry *he)
  96. {
  97. struct rb_node *n = self->hists.rb_node;
  98. while (n) {
  99. struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node);
  100. int64_t cmp = hist_entry__cmp(he, iter);
  101. if (cmp < 0)
  102. n = n->rb_left;
  103. else if (cmp > 0)
  104. n = n->rb_right;
  105. else
  106. return iter;
  107. }
  108. return NULL;
  109. }
  110. static void perf_session__match_hists(struct perf_session *old_session,
  111. struct perf_session *new_session)
  112. {
  113. struct rb_node *nd;
  114. for (nd = rb_first(&new_session->hists); nd; nd = rb_next(nd)) {
  115. struct hist_entry *pos = rb_entry(nd, struct hist_entry, rb_node);
  116. pos->pair = perf_session__find_hist_entry(old_session, 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);
  124. session[1] = perf_session__new(input_new, O_RDONLY, force, false);
  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. perf_session__output_resort(&session[1]->hists,
  133. session[1]->events_stats.total);
  134. if (show_displacement)
  135. perf_session__set_hist_entries_positions(session[0]);
  136. perf_session__match_hists(session[0], session[1]);
  137. perf_session__fprintf_hists(&session[1]->hists, session[0],
  138. show_displacement, stdout,
  139. session[1]->events_stats.total);
  140. out_delete:
  141. for (i = 0; i < 2; ++i)
  142. perf_session__delete(session[i]);
  143. return ret;
  144. }
  145. static const char * const diff_usage[] = {
  146. "perf diff [<options>] [old_file] [new_file]",
  147. NULL,
  148. };
  149. static const struct option options[] = {
  150. OPT_INCR('v', "verbose", &verbose,
  151. "be more verbose (show symbol address, etc)"),
  152. OPT_BOOLEAN('m', "displacement", &show_displacement,
  153. "Show position displacement relative to baseline"),
  154. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  155. "dump raw trace in ASCII"),
  156. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  157. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  158. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  159. OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
  160. "Don't shorten the pathnames taking into account the cwd"),
  161. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  162. "only consider symbols in these dsos"),
  163. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  164. "only consider symbols in these comms"),
  165. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  166. "only consider these symbols"),
  167. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  168. "sort by key(s): pid, comm, dso, symbol, parent"),
  169. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  170. "separator for columns, no spaces will be added between "
  171. "columns '.' is reserved."),
  172. OPT_END()
  173. };
  174. int cmd_diff(int argc, const char **argv, const char *prefix __used)
  175. {
  176. sort_order = diff__default_sort_order;
  177. argc = parse_options(argc, argv, options, diff_usage, 0);
  178. if (argc) {
  179. if (argc > 2)
  180. usage_with_options(diff_usage, options);
  181. if (argc == 2) {
  182. input_old = argv[0];
  183. input_new = argv[1];
  184. } else
  185. input_new = argv[0];
  186. } else if (symbol_conf.default_guest_vmlinux_name ||
  187. symbol_conf.default_guest_kallsyms) {
  188. input_old = "perf.data.host";
  189. input_new = "perf.data.guest";
  190. }
  191. symbol_conf.exclude_other = false;
  192. if (symbol__init() < 0)
  193. return -1;
  194. setup_sorting(diff_usage, options);
  195. setup_pager();
  196. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", NULL);
  197. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", NULL);
  198. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", NULL);
  199. return __cmd_diff();
  200. }